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 is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares the Value class. 
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_VALUE_H
00015 #define LLVM_VALUE_H
00016 
00017 #include "llvm/AbstractTypeUser.h"
00018 #include "llvm/Use.h"
00019 #include "llvm/ADT/StringRef.h"
00020 #include "llvm/ADT/Twine.h"
00021 #include "llvm/Support/Casting.h"
00022 #include <iosfwd>
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 GlobalAlias;
00035 class InlineAsm;
00036 class ValueSymbolTable;
00037 class TypeSymbolTable;
00038 template<typename ValueTy> class StringMapEntry;
00039 template <typename ValueTy = Value>
00040 class AssertingVH;
00041 typedef StringMapEntry<Value*> ValueName;
00042 class raw_ostream;
00043 class AssemblyAnnotationWriter;
00044 class ValueHandleBase;
00045 class LLVMContext;
00046 
00047 //===----------------------------------------------------------------------===//
00048 //                                 Value Class
00049 //===----------------------------------------------------------------------===//
00050 
00051 /// This is a very important LLVM class. It is the base class of all values 
00052 /// computed by a program that may be used as operands to other values. Value is
00053 /// the super class of other important classes such as Instruction and Function.
00054 /// All Values have a Type. Type is not a subclass of Value. All types can have
00055 /// a name and they should belong to some Module. Setting the name on the Value
00056 /// automatically updates the module's symbol table.
00057 ///
00058 /// Every value has a "use list" that keeps track of which other Values are
00059 /// using this Value.  A Value can also have an arbitrary number of ValueHandle
00060 /// objects that watch it and listen to RAUW and Destroy events see
00061 /// llvm/Support/ValueHandle.h for details.
00062 ///
00063 /// @brief LLVM Value Representation
00064 class Value {
00065   const unsigned char SubclassID;   // Subclass identifier (for isa/dyn_cast)
00066   unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
00067 protected:
00068   /// SubclassOptionalData - This member is similar to SubclassData, however it
00069   /// is for holding information which may be used to aid optimization, but
00070   /// which may be cleared to zero without affecting conservative
00071   /// interpretation.
00072   unsigned char SubclassOptionalData : 7;
00073 
00074   /// SubclassData - This member is defined by this class, but is not used for
00075   /// anything.  Subclasses can use it to hold whatever state they find useful.
00076   /// This field is initialized to zero by the ctor.
00077   unsigned short SubclassData;
00078 private:
00079   PATypeHolder VTy;
00080   Use *UseList;
00081 
00082   friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
00083   friend class SymbolTable;      // Allow SymbolTable to directly poke Name.
00084   friend class ValueHandleBase;
00085   ValueName *Name;
00086 
00087   void operator=(const Value &);     // Do not implement
00088   Value(const Value &);              // Do not implement
00089 
00090 public:
00091   Value(const Type *Ty, unsigned scid);
00092   virtual ~Value();
00093 
00094   /// dump - Support for debugging, callable in GDB: V->dump()
00095   //
00096   virtual void dump() const;
00097 
00098   /// print - Implement operator<< on Value.
00099   ///
00100   void print(std::ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
00101   void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
00102 
00103   /// All values are typed, get the type of this value.
00104   ///
00105   inline const Type *getType() const { return VTy; }
00106 
00107   /// All values hold a context through their type.
00108   LLVMContext &getContext() const;
00109 
00110   // All values can potentially be named...
00111   inline bool hasName() const { return Name != 0; }
00112   ValueName *getValueName() const { return Name; }
00113   
00114   /// getName() - Return a constant reference to the value's name. This is cheap
00115   /// and guaranteed to return the same reference as long as the value is not
00116   /// modified.
00117   ///
00118   /// This is currently guaranteed to return a StringRef for which data() points
00119   /// to a valid null terminated string. The use of StringRef.data() is 
00120   /// deprecated here, however, and clients should not rely on it. If such 
00121   /// behavior is needed, clients should use expensive getNameStr(), or switch 
00122   /// to an interface that does not depend on null termination.
00123   StringRef getName() const;
00124 
00125   /// getNameStr() - Return the name of the specified value, *constructing a
00126   /// string* to hold it.  This is guaranteed to construct a string and is very
00127   /// expensive, clients should use getName() unless necessary.
00128   std::string getNameStr() const;
00129 
00130   /// setName() - Change the name of the value, choosing a new unique name if
00131   /// the provided name is taken.
00132   ///
00133   /// \arg Name - The new name; or "" if the value's name should be removed.
00134   void setName(const Twine &Name);
00135 
00136   
00137   /// takeName - transfer the name from V to this value, setting V's name to
00138   /// empty.  It is an error to call V->takeName(V). 
00139   void takeName(Value *V);
00140 
00141   /// replaceAllUsesWith - Go through the uses list for this definition and make
00142   /// each use point to "V" instead of "this".  After this completes, 'this's
00143   /// use list is guaranteed to be empty.
00144   ///
00145   void replaceAllUsesWith(Value *V);
00146 
00147   // uncheckedReplaceAllUsesWith - Just like replaceAllUsesWith but dangerous.
00148   // Only use when in type resolution situations!
00149   void uncheckedReplaceAllUsesWith(Value *V);
00150 
00151   /// clearOptionalData - Clear any optional optimization data from this Value.
00152   /// Transformation passes must call this method whenever changing the IR
00153   /// in a way that would affect the values produced by this Value, unless
00154   /// it takes special care to ensure correctness in some other way.
00155   void clearOptionalData() { SubclassOptionalData = 0; }
00156 
00157   //----------------------------------------------------------------------
00158   // Methods for handling the chain of uses of this Value.
00159   //
00160   typedef value_use_iterator<User>       use_iterator;
00161   typedef value_use_iterator<const User> use_const_iterator;
00162 
00163   bool               use_empty() const { return UseList == 0; }
00164   use_iterator       use_begin()       { return use_iterator(UseList); }
00165   use_const_iterator use_begin() const { return use_const_iterator(UseList); }
00166   use_iterator       use_end()         { return use_iterator(0);   }
00167   use_const_iterator use_end()   const { return use_const_iterator(0);   }
00168   User              *use_back()        { return *use_begin(); }
00169   const User        *use_back()  const { return *use_begin(); }
00170 
00171   /// hasOneUse - Return true if there is exactly one user of this value.  This
00172   /// is specialized because it is a common request and does not require
00173   /// traversing the whole use list.
00174   ///
00175   bool hasOneUse() const {
00176     use_const_iterator I = use_begin(), E = use_end();
00177     if (I == E) return false;
00178     return ++I == E;
00179   }
00180 
00181   /// hasNUses - Return true if this Value has exactly N users.
00182   ///
00183   bool hasNUses(unsigned N) const;
00184 
00185   /// hasNUsesOrMore - Return true if this value has N users or more.  This is
00186   /// logically equivalent to getNumUses() >= N.
00187   ///
00188   bool hasNUsesOrMore(unsigned N) const;
00189 
00190   bool isUsedInBasicBlock(const BasicBlock *BB) const;
00191 
00192   /// getNumUses - This method computes the number of uses of this Value.  This
00193   /// is a linear time operation.  Use hasOneUse, hasNUses, or hasMoreThanNUses
00194   /// to check for specific values.
00195   unsigned getNumUses() const;
00196 
00197   /// addUse - This method should only be used by the Use class.
00198   ///
00199   void addUse(Use &U) { U.addToList(&UseList); }
00200 
00201   /// An enumeration for keeping track of the concrete subclass of Value that
00202   /// is actually instantiated. Values of this enumeration are kept in the 
00203   /// Value classes SubclassID field. They are used for concrete type
00204   /// identification.
00205   enum ValueTy {
00206     ArgumentVal,              // This is an instance of Argument
00207     BasicBlockVal,            // This is an instance of BasicBlock
00208     FunctionVal,              // This is an instance of Function
00209     GlobalAliasVal,           // This is an instance of GlobalAlias
00210     GlobalVariableVal,        // This is an instance of GlobalVariable
00211     UndefValueVal,            // This is an instance of UndefValue
00212     ConstantExprVal,          // This is an instance of ConstantExpr
00213     ConstantAggregateZeroVal, // This is an instance of ConstantAggregateNull
00214     ConstantIntVal,           // This is an instance of ConstantInt
00215     ConstantFPVal,            // This is an instance of ConstantFP
00216     ConstantArrayVal,         // This is an instance of ConstantArray
00217     ConstantStructVal,        // This is an instance of ConstantStruct
00218     ConstantVectorVal,        // This is an instance of ConstantVector
00219     ConstantPointerNullVal,   // This is an instance of ConstantPointerNull
00220     MDNodeVal,                // This is an instance of MDNode
00221     MDStringVal,              // This is an instance of MDString
00222     NamedMDNodeVal,           // This is an instance of NamedMDNode
00223     InlineAsmVal,             // This is an instance of InlineAsm
00224     PseudoSourceValueVal,     // This is an instance of PseudoSourceValue
00225     InstructionVal,           // This is an instance of Instruction
00226     
00227     // Markers:
00228     ConstantFirstVal = FunctionVal,
00229     ConstantLastVal  = ConstantPointerNullVal
00230   };
00231 
00232   /// getValueID - Return an ID for the concrete type of this object.  This is
00233   /// used to implement the classof checks.  This should not be used for any
00234   /// other purpose, as the values may change as LLVM evolves.  Also, note that
00235   /// for instructions, the Instruction's opcode is added to InstructionVal. So
00236   /// this means three things:
00237   /// # there is no value with code InstructionVal (no opcode==0).
00238   /// # there are more possible values for the value type than in ValueTy enum.
00239   /// # the InstructionVal enumerator must be the highest valued enumerator in
00240   ///   the ValueTy enum.
00241   unsigned getValueID() const {
00242     return SubclassID;
00243   }
00244 
00245   /// hasSameSubclassOptionalData - Test whether the optional flags contained
00246   /// in this value are equal to the optional flags in the given value.
00247   bool hasSameSubclassOptionalData(const Value *V) const {
00248     return SubclassOptionalData == V->SubclassOptionalData;
00249   }
00250 
00251   /// intersectOptionalDataWith - Clear any optional flags in this value
00252   /// that are not also set in the given value.
00253   void intersectOptionalDataWith(const Value *V) {
00254     SubclassOptionalData &= V->SubclassOptionalData;
00255   }
00256 
00257   // Methods for support type inquiry through isa, cast, and dyn_cast:
00258   static inline bool classof(const Value *) {
00259     return true; // Values are always values.
00260   }
00261 
00262   /// getRawType - This should only be used to implement the vmcore library.
00263   ///
00264   const Type *getRawType() const { return VTy.getRawType(); }
00265 
00266   /// stripPointerCasts - This method strips off any unneeded pointer
00267   /// casts from the specified value, returning the original uncasted value.
00268   /// Note that the returned value has pointer type if the specified value does.
00269   Value *stripPointerCasts();
00270   const Value *stripPointerCasts() const {
00271     return const_cast<Value*>(this)->stripPointerCasts();
00272   }
00273 
00274   /// getUnderlyingObject - This method strips off any GEP address adjustments
00275   /// and pointer casts from the specified value, returning the original object
00276   /// being addressed.  Note that the returned value has pointer type if the
00277   /// specified value does.
00278   Value *getUnderlyingObject();
00279   const Value *getUnderlyingObject() const {
00280     return const_cast<Value*>(this)->getUnderlyingObject();
00281   }
00282   
00283   /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
00284   /// return the value in the PHI node corresponding to PredBB.  If not, return
00285   /// ourself.  This is useful if you want to know the value something has in a
00286   /// predecessor block.
00287   Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
00288 
00289   const Value *DoPHITranslation(const BasicBlock *CurBB,
00290                                 const BasicBlock *PredBB) const{
00291     return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
00292   }
00293 };
00294 
00295 inline std::ostream &operator<<(std::ostream &OS, const Value &V) {
00296   V.print(OS);
00297   return OS;
00298 }
00299 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
00300   V.print(OS);
00301   return OS;
00302 }
00303   
00304 void Use::set(Value *V) {
00305   if (Val) removeFromList();
00306   Val = V;
00307   if (V) V->addUse(*this);
00308 }
00309 
00310 
00311 // isa - Provide some specializations of isa so that we don't have to include
00312 // the subtype header files to test to see if the value is a subclass...
00313 //
00314 template <> inline bool isa_impl<Constant, Value>(const Value &Val) {
00315   return Val.getValueID() >= Value::ConstantFirstVal &&
00316          Val.getValueID() <= Value::ConstantLastVal;
00317 }
00318 template <> inline bool isa_impl<Argument, Value>(const Value &Val) {
00319   return Val.getValueID() == Value::ArgumentVal;
00320 }
00321 template <> inline bool isa_impl<InlineAsm, Value>(const Value &Val) {
00322   return Val.getValueID() == Value::InlineAsmVal;
00323 }
00324 template <> inline bool isa_impl<Instruction, Value>(const Value &Val) {
00325   return Val.getValueID() >= Value::InstructionVal;
00326 }
00327 template <> inline bool isa_impl<BasicBlock, Value>(const Value &Val) {
00328   return Val.getValueID() == Value::BasicBlockVal;
00329 }
00330 template <> inline bool isa_impl<Function, Value>(const Value &Val) {
00331   return Val.getValueID() == Value::FunctionVal;
00332 }
00333 template <> inline bool isa_impl<GlobalVariable, Value>(const Value &Val) {
00334   return Val.getValueID() == Value::GlobalVariableVal;
00335 }
00336 template <> inline bool isa_impl<GlobalAlias, Value>(const Value &Val) {
00337   return Val.getValueID() == Value::GlobalAliasVal;
00338 }
00339 template <> inline bool isa_impl<GlobalValue, Value>(const Value &Val) {
00340   return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
00341          isa<GlobalAlias>(Val);
00342 }
00343   
00344   
00345 // Value* is only 4-byte aligned.
00346 template<>
00347 class PointerLikeTypeTraits<Value*> {
00348   typedef Value* PT;
00349 public:
00350   static inline void *getAsVoidPointer(PT P) { return P; }
00351   static inline PT getFromVoidPointer(void *P) {
00352     return static_cast<PT>(P);
00353   }
00354   enum { NumLowBitsAvailable = 2 };
00355 };
00356 
00357 } // End llvm namespace
00358 
00359 #endif