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