1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file declares the Value class.
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef LLVM_VALUE_H
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LLVM_VALUE_H
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Use.h"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/StringRef.h"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Casting.h"
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <string>
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm {
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass Constant;
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass Argument;
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass Instruction;
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass BasicBlock;
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass GlobalValue;
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass Function;
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass GlobalVariable;
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass GlobalAlias;
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass InlineAsm;
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass ValueSymbolTable;
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<typename ValueTy> class StringMapEntry;
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <typename ValueTy = Value>
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass AssertingVH;
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantypedef StringMapEntry<Value*> ValueName;
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass raw_ostream;
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass AssemblyAnnotationWriter;
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass ValueHandleBase;
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass LLVMContext;
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass Twine;
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass MDNode;
4419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanclass Type;
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                                 Value Class
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This is a very important LLVM class. It is the base class of all values
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// computed by a program that may be used as operands to other values. Value is
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the super class of other important classes such as Instruction and Function.
5319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// All Values have a Type. Type is not a subclass of Value. Some values can
5419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// have a name and they belong to some Module.  Setting the name on the Value
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// automatically updates the module's symbol table.
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Every value has a "use list" that keeps track of which other Values are
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// using this Value.  A Value can also have an arbitrary number of ValueHandle
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// objects that watch it and listen to RAUW and Destroy events.  See
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// llvm/Support/ValueHandle.h for details.
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// @brief LLVM Value Representation
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass Value {
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const unsigned char SubclassID;   // Subclass identifier (for isa/dyn_cast)
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprotected:
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// SubclassOptionalData - This member is similar to SubclassData, however it
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// is for holding information which may be used to aid optimization, but
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// which may be cleared to zero without affecting conservative
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// interpretation.
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned char SubclassOptionalData : 7;
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprivate:
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// SubclassData - This member is defined by this class, but is not used for
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// anything.  Subclasses can use it to hold whatever state they find useful.
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// This field is initialized to zero by the ctor.
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned short SubclassData;
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *VTy;
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Use *UseList;
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  friend class ValueHandleBase;
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ValueName *Name;
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void operator=(const Value &);     // Do not implement
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Value(const Value &);              // Do not implement
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprotected:
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// printCustom - Value subclasses can override this to implement custom
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// printing behavior.
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void printCustom(raw_ostream &O) const;
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Value(Type *Ty, unsigned scid);
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual ~Value();
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// dump - Support for debugging, callable in GDB: V->dump()
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void dump() const;
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// print - Implement operator<< on Value.
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// All values are typed, get the type of this value.
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
10819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *getType() const { return VTy; }
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// All values hold a context through their type.
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LLVMContext &getContext() const;
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // All values can potentially be named...
11419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool hasName() const { return Name != 0; }
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ValueName *getValueName() const { return Name; }
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getName() - Return a constant reference to the value's name. This is cheap
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// and guaranteed to return the same reference as long as the value is not
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// modified.
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// This is currently guaranteed to return a StringRef for which data() points
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// to a valid null terminated string. The use of StringRef.data() is
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// deprecated here, however, and clients should not rely on it. If such
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// behavior is needed, clients should use expensive getNameStr(), or switch
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// to an interface that does not depend on null termination.
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  StringRef getName() const;
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getNameStr() - Return the name of the specified value, *constructing a
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// string* to hold it.  This is guaranteed to construct a string and is very
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// expensive, clients should use getName() unless necessary.
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::string getNameStr() const;
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// setName() - Change the name of the value, choosing a new unique name if
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// the provided name is taken.
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// \arg Name - The new name; or "" if the value's name should be removed.
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void setName(const Twine &Name);
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// takeName - transfer the name from V to this value, setting V's name to
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// empty.  It is an error to call V->takeName(V).
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void takeName(Value *V);
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// replaceAllUsesWith - Go through the uses list for this definition and make
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// each use point to "V" instead of "this".  After this completes, 'this's
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// use list is guaranteed to be empty.
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void replaceAllUsesWith(Value *V);
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //----------------------------------------------------------------------
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Methods for handling the chain of uses of this Value.
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef value_use_iterator<User>       use_iterator;
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef value_use_iterator<const User> const_use_iterator;
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool               use_empty() const { return UseList == 0; }
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  use_iterator       use_begin()       { return use_iterator(UseList); }
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_use_iterator use_begin() const { return const_use_iterator(UseList); }
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  use_iterator       use_end()         { return use_iterator(0);   }
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const_use_iterator use_end()   const { return const_use_iterator(0);   }
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  User              *use_back()        { return *use_begin(); }
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const User        *use_back()  const { return *use_begin(); }
163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// hasOneUse - Return true if there is exactly one user of this value.  This
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// is specialized because it is a common request and does not require
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// traversing the whole use list.
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool hasOneUse() const {
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const_use_iterator I = use_begin(), E = use_end();
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (I == E) return false;
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return ++I == E;
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// hasNUses - Return true if this Value has exactly N users.
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool hasNUses(unsigned N) const;
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// hasNUsesOrMore - Return true if this value has N users or more.  This is
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// logically equivalent to getNumUses() >= N.
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool hasNUsesOrMore(unsigned N) const;
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isUsedInBasicBlock(const BasicBlock *BB) const;
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getNumUses - This method computes the number of uses of this Value.  This
18619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// is a linear time operation.  Use hasOneUse, hasNUses, or hasNUsesOrMore
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// to check for specific values.
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned getNumUses() const;
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// addUse - This method should only be used by the Use class.
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void addUse(Use &U) { U.addToList(&UseList); }
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// An enumeration for keeping track of the concrete subclass of Value that
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// is actually instantiated. Values of this enumeration are kept in the
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Value classes SubclassID field. They are used for concrete type
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// identification.
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  enum ValueTy {
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ArgumentVal,              // This is an instance of Argument
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BasicBlockVal,            // This is an instance of BasicBlock
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FunctionVal,              // This is an instance of Function
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    GlobalAliasVal,           // This is an instance of GlobalAlias
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    GlobalVariableVal,        // This is an instance of GlobalVariable
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    UndefValueVal,            // This is an instance of UndefValue
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BlockAddressVal,          // This is an instance of BlockAddress
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantExprVal,          // This is an instance of ConstantExpr
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantAggregateZeroVal, // This is an instance of ConstantAggregateZero
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantIntVal,           // This is an instance of ConstantInt
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantFPVal,            // This is an instance of ConstantFP
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantArrayVal,         // This is an instance of ConstantArray
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantStructVal,        // This is an instance of ConstantStruct
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantVectorVal,        // This is an instance of ConstantVector
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantPointerNullVal,   // This is an instance of ConstantPointerNull
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MDNodeVal,                // This is an instance of MDNode
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MDStringVal,              // This is an instance of MDString
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InlineAsmVal,             // This is an instance of InlineAsm
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    PseudoSourceValueVal,     // This is an instance of PseudoSourceValue
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FixedStackPseudoSourceValueVal, // This is an instance of
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    // FixedStackPseudoSourceValue
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InstructionVal,           // This is an instance of Instruction
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Enum values starting at InstructionVal are used for Instructions;
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // don't add new values here!
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Markers:
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantFirstVal = FunctionVal,
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ConstantLastVal  = ConstantPointerNullVal
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getValueID - Return an ID for the concrete type of this object.  This is
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// used to implement the classof checks.  This should not be used for any
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// other purpose, as the values may change as LLVM evolves.  Also, note that
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// for instructions, the Instruction's opcode is added to InstructionVal. So
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// this means three things:
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// # there is no value with code InstructionVal (no opcode==0).
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// # there are more possible values for the value type than in ValueTy enum.
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// # the InstructionVal enumerator must be the highest valued enumerator in
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///   the ValueTy enum.
238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned getValueID() const {
239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SubclassID;
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getRawSubclassOptionalData - Return the raw optional flags value
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// contained in this value. This should only be used when testing two
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Values for equivalence.
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned getRawSubclassOptionalData() const {
246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SubclassOptionalData;
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
24919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// clearSubclassOptionalData - Clear the optional flags contained in
25019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// this value.
25119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  void clearSubclassOptionalData() {
25219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SubclassOptionalData = 0;
25319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
25419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// hasSameSubclassOptionalData - Test whether the optional flags contained
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// in this value are equal to the optional flags in the given value.
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool hasSameSubclassOptionalData(const Value *V) const {
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return SubclassOptionalData == V->SubclassOptionalData;
259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// intersectOptionalDataWith - Clear any optional flags in this value
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// that are not also set in the given value.
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void intersectOptionalDataWith(const Value *V) {
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SubclassOptionalData &= V->SubclassOptionalData;
265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// hasValueHandle - Return true if there is a value handle associated with
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// this value.
269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool hasValueHandle() const { return HasValueHandle; }
270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Methods for support type inquiry through isa, cast, and dyn_cast:
272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline bool classof(const Value *) {
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true; // Values are always values.
274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// stripPointerCasts - This method strips off any unneeded pointer
277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// casts from the specified value, returning the original uncasted value.
278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Note that the returned value has pointer type if the specified value does.
279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Value *stripPointerCasts();
280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Value *stripPointerCasts() const {
281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return const_cast<Value*>(this)->stripPointerCasts();
282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
28419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// isDereferenceablePointer - Test if this value is always a pointer to
28519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// allocated and suitably aligned memory for a simple load or store.
28619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool isDereferenceablePointer() const;
287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// return the value in the PHI node corresponding to PredBB.  If not, return
290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// ourself.  This is useful if you want to know the value something has in a
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// predecessor block.
292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Value *DoPHITranslation(const BasicBlock *CurBB,
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                const BasicBlock *PredBB) const{
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// MaximumAlignment - This is the greatest alignment value supported by
300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// load, store, and alloca instructions, and global values.
301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static const unsigned MaximumAlignment = 1u << 29;
302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
30319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// mutateType - Mutate the type of this Value to be of the specified type.
30419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// Note that this is an extremely dangerous operation which can create
30519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// completely invalid IR very easily.  It is strongly recommended that you
30619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// recreate IR objects with the right types instead of mutating them in
30719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// place.
30819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  void mutateType(Type *Ty) {
30919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    VTy = Ty;
31019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
31119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprotected:
313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned short getSubclassDataFromValue() const { return SubclassData; }
314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void setValueSubclassData(unsigned short D) { SubclassData = D; }
315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumaninline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  V.print(OS);
319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return OS;
320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid Use::set(Value *V) {
323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Val) removeFromList();
324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Val = V;
325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (V) V->addUse(*this);
326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// isa - Provide some specializations of isa so that we don't have to include
330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// the subtype header files to test to see if the value is a subclass...
331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <> struct isa_impl<Constant, Value> {
333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline bool doit(const Value &Val) {
334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Val.getValueID() >= Value::ConstantFirstVal &&
335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Val.getValueID() <= Value::ConstantLastVal;
336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <> struct isa_impl<Argument, Value> {
340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline bool doit (const Value &Val) {
341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Val.getValueID() == Value::ArgumentVal;
342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <> struct isa_impl<InlineAsm, Value> {
346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline bool doit(const Value &Val) {
347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Val.getValueID() == Value::InlineAsmVal;
348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <> struct isa_impl<Instruction, Value> {
352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline bool doit(const Value &Val) {
353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Val.getValueID() >= Value::InstructionVal;
354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <> struct isa_impl<BasicBlock, Value> {
358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline bool doit(const Value &Val) {
359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Val.getValueID() == Value::BasicBlockVal;
360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <> struct isa_impl<Function, Value> {
364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline bool doit(const Value &Val) {
365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Val.getValueID() == Value::FunctionVal;
366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <> struct isa_impl<GlobalVariable, Value> {
370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline bool doit(const Value &Val) {
371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Val.getValueID() == Value::GlobalVariableVal;
372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <> struct isa_impl<GlobalAlias, Value> {
376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline bool doit(const Value &Val) {
377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Val.getValueID() == Value::GlobalAliasVal;
378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <> struct isa_impl<GlobalValue, Value> {
382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline bool doit(const Value &Val) {
383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      isa<GlobalAlias>(Val);
385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate <> struct isa_impl<MDNode, Value> {
389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline bool doit(const Value &Val) {
390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Val.getValueID() == Value::MDNodeVal;
391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Value* is only 4-byte aligned.
395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumantemplate<>
396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass PointerLikeTypeTraits<Value*> {
397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  typedef Value* PT;
398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline void *getAsVoidPointer(PT P) { return P; }
400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static inline PT getFromVoidPointer(void *P) {
401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return static_cast<PT>(P);
402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  enum { NumLowBitsAvailable = 2 };
404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // End llvm namespace
407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
409