LLVM API Documentation

Value.h

Go to the documentation of this file.
00001 //===-- llvm/Value.h - Definition of the Value class ------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares the Value class. 
00011 // This file also defines the Use<> template for users of value.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_VALUE_H
00016 #define LLVM_VALUE_H
00017 
00018 #include "llvm/AbstractTypeUser.h"
00019 #include "llvm/Use.h"
00020 #include "llvm/Support/Casting.h"
00021 #include <string>
00022 
00023 namespace llvm {
00024 
00025 class Constant;
00026 class Argument;
00027 class Instruction;
00028 class BasicBlock;
00029 class GlobalValue;
00030 class Function;
00031 class GlobalVariable;
00032 class InlineAsm;
00033 class SymbolTable;
00034 
00035 //===----------------------------------------------------------------------===//
00036 //                                 Value Class
00037 //===----------------------------------------------------------------------===//
00038 
00039 /// This is a very important LLVM class. It is the base class of all values 
00040 /// computed by a program that may be used as operands to other values. Value is
00041 /// the super class of other important classes such as Instruction and Function.
00042 /// All Values have a Type. Type is not a subclass of Value. All types can have
00043 /// a name and they should belong to some Module. Setting the name on the Value
00044 /// automatically update's the module's symbol table.
00045 ///
00046 /// Every value has a "use list" that keeps track of which other Values are
00047 /// using this Value.
00048 /// @brief LLVM Value Representation
00049 class Value {
00050   unsigned short SubclassID;         // Subclass identifier (for isa/dyn_cast)
00051 protected:
00052   /// SubclassData - This member is defined by this class, but is not used for
00053   /// anything.  Subclasses can use it to hold whatever state they find useful.
00054   /// This field is initialized to zero by the ctor.
00055   unsigned short SubclassData;
00056 private:
00057   PATypeHolder Ty;
00058   Use *UseList;
00059 
00060   friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
00061   friend class SymbolTable;      // Allow SymbolTable to directly poke Name.
00062   std::string Name;
00063 
00064   void operator=(const Value &);     // Do not implement
00065   Value(const Value &);              // Do not implement
00066 
00067 public:
00068   Value(const Type *Ty, unsigned scid, const std::string &name = "");
00069   virtual ~Value();
00070 
00071   /// dump - Support for debugging, callable in GDB: V->dump()
00072   //
00073   virtual void dump() const;
00074 
00075   /// print - Implement operator<< on Value...
00076   ///
00077   virtual void print(std::ostream &O) const = 0;
00078 
00079   /// All values are typed, get the type of this value.
00080   ///
00081   inline const Type *getType() const { return Ty; }
00082 
00083   // All values can potentially be named...
00084   inline bool               hasName() const { return !Name.empty(); }
00085   inline const std::string &getName() const { return Name; }
00086 
00087   void setName(const std::string &name);
00088 
00089   /// replaceAllUsesWith - Go through the uses list for this definition and make
00090   /// each use point to "V" instead of "this".  After this completes, 'this's
00091   /// use list is guaranteed to be empty.
00092   ///
00093   void replaceAllUsesWith(Value *V);
00094 
00095   // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
00096   // Only use when in type resolution situations!
00097   void uncheckedReplaceAllUsesWith(Value *V);
00098 
00099   //----------------------------------------------------------------------
00100   // Methods for handling the vector of uses of this Value.
00101   //
00102   typedef value_use_iterator<User>       use_iterator;
00103   typedef value_use_iterator<const User> use_const_iterator;
00104 
00105   bool               use_empty() const { return UseList == 0; }
00106   use_iterator       use_begin()       { return use_iterator(UseList); }
00107   use_const_iterator use_begin() const { return use_const_iterator(UseList); }
00108   use_iterator       use_end()         { return use_iterator(0);   }
00109   use_const_iterator use_end()   const { return use_const_iterator(0);   }
00110   User              *use_back()        { return *use_begin(); }
00111   const User        *use_back() const  { return *use_begin(); }
00112 
00113   /// hasOneUse - Return true if there is exactly one user of this value.  This
00114   /// is specialized because it is a common request and does not require
00115   /// traversing the whole use list.
00116   ///
00117   bool hasOneUse() const {
00118     use_const_iterator I = use_begin(), E = use_end();
00119     if (I == E) return false;
00120     return ++I == E;
00121   }
00122 
00123   /// hasNUses - Return true if this Value has exactly N users.
00124   ///
00125   bool hasNUses(unsigned N) const;
00126 
00127   /// hasNUsesOrMore - Return true if this value has N users or more.  This is
00128   /// logically equivalent to getNumUses() >= N.
00129   ///
00130   bool hasNUsesOrMore(unsigned N) const;
00131 
00132   /// getNumUses - This method computes the number of uses of this Value.  This
00133   /// is a linear time operation.  Use hasOneUse, hasNUses, or hasMoreThanNUses
00134   /// to check for specific values.
00135   unsigned getNumUses() const;
00136 
00137   /// addUse/killUse - These two methods should only be used by the Use class.
00138   ///
00139   void addUse(Use &U) { U.addToList(&UseList); }
00140 
00141   /// An enumeration for keeping track of the concrete subclass of Value that
00142   /// is actually instantiated. Values of this enumeration are kept in the 
00143   /// Value classes SubclassID field. They are used for concrete type
00144   /// identification.
00145   enum ValueTy {
00146     ArgumentVal,              // This is an instance of Argument
00147     BasicBlockVal,            // This is an instance of BasicBlock
00148     FunctionVal,              // This is an instance of Function
00149     GlobalVariableVal,        // This is an instance of GlobalVariable
00150     UndefValueVal,            // This is an instance of UndefValue
00151     ConstantExprVal,          // This is an instance of ConstantExpr
00152     ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
00153     ConstantBoolVal,          // This is an instance of ConstantBool
00154     ConstantSIntVal,          // This is an instance of ConstantSInt
00155     ConstantUIntVal,          // This is an instance of ConstantUInt
00156     ConstantFPVal,            // This is an instance of ConstantFP
00157     ConstantArrayVal,         // This is an instance of ConstantArray
00158     ConstantStructVal,        // This is an instance of ConstantStruct
00159     ConstantPackedVal,        // This is an instance of ConstantPacked
00160     ConstantPointerNullVal,   // This is an instance of ConstantPointerNull
00161     InlineAsmVal,             // This is an instance of InlineAsm
00162     InstructionVal,           // This is an instance of Instruction
00163     
00164     // Markers:
00165     ConstantFirstVal = FunctionVal,
00166     ConstantLastVal  = ConstantPointerNullVal
00167   };
00168 
00169   /// getValueType - Return an ID for the concrete type of this object.  This is
00170   /// used to implement the classof checks.  This should not be used for any
00171   /// other purpose, as the values may change as LLVM evolves.  Also, note that
00172   /// starting with the InstructionVal value, the value stored is actually the
00173   /// Instruction opcode, so there are more than just these values possible here
00174   /// (and Instruction must be last).
00175   ///
00176   unsigned getValueType() const {
00177     return SubclassID;
00178   }
00179 
00180   // Methods for support type inquiry through isa, cast, and dyn_cast:
00181   static inline bool classof(const Value *) {
00182     return true; // Values are always values.
00183   }
00184 
00185   /// getRawType - This should only be used to implement the vmcore library.
00186   ///
00187   const Type *getRawType() const { return Ty.getRawType(); }
00188 
00189 private:
00190   /// FIXME: this is a gross hack, needed by another gross hack.  Eliminate!
00191   void setValueType(unsigned short VT) { SubclassID = VT; }
00192   friend class Instruction;
00193 };
00194 
00195 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
00196   V.print(OS);
00197   return OS;
00198 }
00199 
00200 void Use::init(Value *v, User *user) {
00201   Val = v;
00202   U = user;
00203   if (Val) Val->addUse(*this);
00204 }
00205 
00206 Use::~Use() {
00207   if (Val) removeFromList();
00208 }
00209 
00210 void Use::set(Value *V) {
00211   if (Val) removeFromList();
00212   Val = V;
00213   if (V) V->addUse(*this);
00214 }
00215 
00216 
00217 // isa - Provide some specializations of isa so that we don't have to include
00218 // the subtype header files to test to see if the value is a subclass...
00219 //
00220 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
00221   return Val.getValueType() >= Value::ConstantFirstVal &&
00222          Val.getValueType() <= Value::ConstantLastVal;
00223 }
00224 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
00225   return Val.getValueType() == Value::ArgumentVal;
00226 }
00227 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
00228   return Val.getValueType() == Value::InlineAsmVal;
00229 }
00230 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
00231   return Val.getValueType() >= Value::InstructionVal;
00232 }
00233 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
00234   return Val.getValueType() == Value::BasicBlockVal;
00235 }
00236 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
00237   return Val.getValueType() == Value::FunctionVal;
00238 }
00239 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
00240   return Val.getValueType() == Value::GlobalVariableVal;
00241 }
00242 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
00243   return isa<GlobalVariable>(Val) || isa<Function>(Val);
00244 }
00245 
00246 } // End llvm namespace
00247 
00248 #endif