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_IR_VALUE_H
00015 #define LLVM_IR_VALUE_H
00016 
00017 #include "llvm/IR/Use.h"
00018 #include "llvm/Support/Casting.h"
00019 #include "llvm/Support/CBindingWrapping.h"
00020 #include "llvm/Support/Compiler.h"
00021 #include "llvm-c/Core.h"
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 GlobalAlias;
00033 class InlineAsm;
00034 class ValueSymbolTable;
00035 template<typename ValueTy> class StringMapEntry;
00036 typedef StringMapEntry<Value*> ValueName;
00037 class raw_ostream;
00038 class AssemblyAnnotationWriter;
00039 class ValueHandleBase;
00040 class LLVMContext;
00041 class Twine;
00042 class MDNode;
00043 class Type;
00044 class StringRef;
00045 
00046 //===----------------------------------------------------------------------===//
00047 //                                 Value Class
00048 //===----------------------------------------------------------------------===//
00049 
00050 /// This is a very important LLVM class. It is the base class of all values 
00051 /// computed by a program that may be used as operands to other values. Value is
00052 /// the super class of other important classes such as Instruction and Function.
00053 /// All Values have a Type. Type is not a subclass of Value. Some values can
00054 /// have a name and they belong to some Module.  Setting the name on the Value
00055 /// automatically updates the module's symbol table.
00056 ///
00057 /// Every value has a "use list" that keeps track of which other Values are
00058 /// using this Value.  A Value can also have an arbitrary number of ValueHandle
00059 /// objects that watch it and listen to RAUW and Destroy events.  See
00060 /// llvm/Support/ValueHandle.h for details.
00061 ///
00062 /// @brief LLVM Value Representation
00063 class Value {
00064   const unsigned char SubclassID;   // Subclass identifier (for isa/dyn_cast)
00065   unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
00066 protected:
00067   /// SubclassOptionalData - This member is similar to SubclassData, however it
00068   /// is for holding information which may be used to aid optimization, but
00069   /// which may be cleared to zero without affecting conservative
00070   /// interpretation.
00071   unsigned char SubclassOptionalData : 7;
00072 
00073 private:
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 
00079   Type *VTy;
00080   Use *UseList;
00081 
00082   friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
00083   friend class ValueHandleBase;
00084   ValueName *Name;
00085 
00086   void operator=(const Value &) LLVM_DELETED_FUNCTION;
00087   Value(const Value &) LLVM_DELETED_FUNCTION;
00088 
00089 protected:
00090   /// printCustom - Value subclasses can override this to implement custom
00091   /// printing behavior.
00092   virtual void printCustom(raw_ostream &O) const;
00093 
00094   Value(Type *Ty, unsigned scid);
00095 public:
00096   virtual ~Value();
00097 
00098   /// dump - Support for debugging, callable in GDB: V->dump()
00099   //
00100   void dump() const;
00101 
00102   /// print - Implement operator<< on Value.
00103   ///
00104   void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
00105 
00106   /// All values are typed, get the type of this value.
00107   ///
00108   Type *getType() const { return VTy; }
00109 
00110   /// All values hold a context through their type.
00111   LLVMContext &getContext() const;
00112 
00113   // All values can potentially be named.
00114   bool hasName() const { return Name != 0 && SubclassID != MDStringVal; }
00115   ValueName *getValueName() const { return Name; }
00116   void setValueName(ValueName *VN) { Name = VN; }
00117   
00118   /// getName() - Return a constant reference to the value's name. This is cheap
00119   /// and guaranteed to return the same reference as long as the value is not
00120   /// modified.
00121   StringRef getName() const;
00122 
00123   /// setName() - Change the name of the value, choosing a new unique name if
00124   /// the provided name is taken.
00125   ///
00126   /// \param Name The new name; or "" if the value's name should be removed.
00127   void setName(const Twine &Name);
00128 
00129   
00130   /// takeName - transfer the name from V to this value, setting V's name to
00131   /// empty.  It is an error to call V->takeName(V). 
00132   void takeName(Value *V);
00133 
00134   /// replaceAllUsesWith - Go through the uses list for this definition and make
00135   /// each use point to "V" instead of "this".  After this completes, 'this's
00136   /// use list is guaranteed to be empty.
00137   ///
00138   void replaceAllUsesWith(Value *V);
00139 
00140   //----------------------------------------------------------------------
00141   // Methods for handling the chain of uses of this Value.
00142   //
00143   typedef value_use_iterator<User>       use_iterator;
00144   typedef value_use_iterator<const User> const_use_iterator;
00145 
00146   bool               use_empty() const { return UseList == 0; }
00147   use_iterator       use_begin()       { return use_iterator(UseList); }
00148   const_use_iterator use_begin() const { return const_use_iterator(UseList); }
00149   use_iterator       use_end()         { return use_iterator(0);   }
00150   const_use_iterator use_end()   const { return const_use_iterator(0);   }
00151   User              *use_back()        { return *use_begin(); }
00152   const User        *use_back()  const { return *use_begin(); }
00153 
00154   /// hasOneUse - Return true if there is exactly one user of this value.  This
00155   /// is specialized because it is a common request and does not require
00156   /// traversing the whole use list.
00157   ///
00158   bool hasOneUse() const {
00159     const_use_iterator I = use_begin(), E = use_end();
00160     if (I == E) return false;
00161     return ++I == E;
00162   }
00163 
00164   /// hasNUses - Return true if this Value has exactly N users.
00165   ///
00166   bool hasNUses(unsigned N) const;
00167 
00168   /// hasNUsesOrMore - Return true if this value has N users or more.  This is
00169   /// logically equivalent to getNumUses() >= N.
00170   ///
00171   bool hasNUsesOrMore(unsigned N) const;
00172 
00173   bool isUsedInBasicBlock(const BasicBlock *BB) const;
00174 
00175   /// getNumUses - This method computes the number of uses of this Value.  This
00176   /// is a linear time operation.  Use hasOneUse, hasNUses, or hasNUsesOrMore
00177   /// to check for specific values.
00178   unsigned getNumUses() const;
00179 
00180   /// addUse - This method should only be used by the Use class.
00181   ///
00182   void addUse(Use &U) { U.addToList(&UseList); }
00183 
00184   /// An enumeration for keeping track of the concrete subclass of Value that
00185   /// is actually instantiated. Values of this enumeration are kept in the 
00186   /// Value classes SubclassID field. They are used for concrete type
00187   /// identification.
00188   enum ValueTy {
00189     ArgumentVal,              // This is an instance of Argument
00190     BasicBlockVal,            // This is an instance of BasicBlock
00191     FunctionVal,              // This is an instance of Function
00192     GlobalAliasVal,           // This is an instance of GlobalAlias
00193     GlobalVariableVal,        // This is an instance of GlobalVariable
00194     UndefValueVal,            // This is an instance of UndefValue
00195     BlockAddressVal,          // This is an instance of BlockAddress
00196     ConstantExprVal,          // This is an instance of ConstantExpr
00197     ConstantAggregateZeroVal, // This is an instance of ConstantAggregateZero
00198     ConstantDataArrayVal,     // This is an instance of ConstantDataArray
00199     ConstantDataVectorVal,    // This is an instance of ConstantDataVector
00200     ConstantIntVal,           // This is an instance of ConstantInt
00201     ConstantFPVal,            // This is an instance of ConstantFP
00202     ConstantArrayVal,         // This is an instance of ConstantArray
00203     ConstantStructVal,        // This is an instance of ConstantStruct
00204     ConstantVectorVal,        // This is an instance of ConstantVector
00205     ConstantPointerNullVal,   // This is an instance of ConstantPointerNull
00206     MDNodeVal,                // This is an instance of MDNode
00207     MDStringVal,              // This is an instance of MDString
00208     InlineAsmVal,             // This is an instance of InlineAsm
00209     PseudoSourceValueVal,     // This is an instance of PseudoSourceValue
00210     FixedStackPseudoSourceValueVal, // This is an instance of 
00211                                     // FixedStackPseudoSourceValue
00212     InstructionVal,           // This is an instance of Instruction
00213     // Enum values starting at InstructionVal are used for Instructions;
00214     // don't add new values here!
00215 
00216     // Markers:
00217     ConstantFirstVal = FunctionVal,
00218     ConstantLastVal  = ConstantPointerNullVal
00219   };
00220 
00221   /// getValueID - Return an ID for the concrete type of this object.  This is
00222   /// used to implement the classof checks.  This should not be used for any
00223   /// other purpose, as the values may change as LLVM evolves.  Also, note that
00224   /// for instructions, the Instruction's opcode is added to InstructionVal. So
00225   /// this means three things:
00226   /// # there is no value with code InstructionVal (no opcode==0).
00227   /// # there are more possible values for the value type than in ValueTy enum.
00228   /// # the InstructionVal enumerator must be the highest valued enumerator in
00229   ///   the ValueTy enum.
00230   unsigned getValueID() const {
00231     return SubclassID;
00232   }
00233 
00234   /// getRawSubclassOptionalData - Return the raw optional flags value
00235   /// contained in this value. This should only be used when testing two
00236   /// Values for equivalence.
00237   unsigned getRawSubclassOptionalData() const {
00238     return SubclassOptionalData;
00239   }
00240 
00241   /// clearSubclassOptionalData - Clear the optional flags contained in
00242   /// this value.
00243   void clearSubclassOptionalData() {
00244     SubclassOptionalData = 0;
00245   }
00246 
00247   /// hasSameSubclassOptionalData - Test whether the optional flags contained
00248   /// in this value are equal to the optional flags in the given value.
00249   bool hasSameSubclassOptionalData(const Value *V) const {
00250     return SubclassOptionalData == V->SubclassOptionalData;
00251   }
00252 
00253   /// intersectOptionalDataWith - Clear any optional flags in this value
00254   /// that are not also set in the given value.
00255   void intersectOptionalDataWith(const Value *V) {
00256     SubclassOptionalData &= V->SubclassOptionalData;
00257   }
00258 
00259   /// hasValueHandle - Return true if there is a value handle associated with
00260   /// this value.
00261   bool hasValueHandle() const { return HasValueHandle; }
00262 
00263   /// \brief This method strips off any unneeded pointer casts,
00264   /// all-zero GEPs and aliases from the specified value, returning the original
00265   /// uncasted value. If this is called on a non-pointer value, it returns
00266   /// 'this'.
00267   Value *stripPointerCasts();
00268   const Value *stripPointerCasts() const {
00269     return const_cast<Value*>(this)->stripPointerCasts();
00270   }
00271 
00272   /// \brief This method strips off any unneeded pointer casts and
00273   /// all-zero GEPs from the specified value, returning the original
00274   /// uncasted value. If this is called on a non-pointer value, it returns
00275   /// 'this'.
00276   Value *stripPointerCastsNoFollowAliases();
00277   const Value *stripPointerCastsNoFollowAliases() const {
00278     return const_cast<Value*>(this)->stripPointerCastsNoFollowAliases();
00279   }
00280 
00281   /// stripInBoundsConstantOffsets - This method strips off unneeded pointer casts and
00282   /// all-constant GEPs from the specified value, returning the original
00283   /// pointer value. If this is called on a non-pointer value, it returns
00284   /// 'this'.
00285   Value *stripInBoundsConstantOffsets();
00286   const Value *stripInBoundsConstantOffsets() const {
00287     return const_cast<Value*>(this)->stripInBoundsConstantOffsets();
00288   }
00289 
00290   /// stripInBoundsOffsets - This method strips off unneeded pointer casts and
00291   /// any in-bounds Offsets from the specified value, returning the original
00292   /// pointer value. If this is called on a non-pointer value, it returns
00293   /// 'this'.
00294   Value *stripInBoundsOffsets();
00295   const Value *stripInBoundsOffsets() const {
00296     return const_cast<Value*>(this)->stripInBoundsOffsets();
00297   }
00298 
00299   /// isDereferenceablePointer - Test if this value is always a pointer to
00300   /// allocated and suitably aligned memory for a simple load or store.
00301   bool isDereferenceablePointer() const;
00302   
00303   /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
00304   /// return the value in the PHI node corresponding to PredBB.  If not, return
00305   /// ourself.  This is useful if you want to know the value something has in a
00306   /// predecessor block.
00307   Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
00308 
00309   const Value *DoPHITranslation(const BasicBlock *CurBB,
00310                                 const BasicBlock *PredBB) const{
00311     return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
00312   }
00313   
00314   /// MaximumAlignment - This is the greatest alignment value supported by
00315   /// load, store, and alloca instructions, and global values.
00316   static const unsigned MaximumAlignment = 1u << 29;
00317   
00318   /// mutateType - Mutate the type of this Value to be of the specified type.
00319   /// Note that this is an extremely dangerous operation which can create
00320   /// completely invalid IR very easily.  It is strongly recommended that you
00321   /// recreate IR objects with the right types instead of mutating them in
00322   /// place.
00323   void mutateType(Type *Ty) {
00324     VTy = Ty;
00325   }
00326   
00327 protected:
00328   unsigned short getSubclassDataFromValue() const { return SubclassData; }
00329   void setValueSubclassData(unsigned short D) { SubclassData = D; }
00330 };
00331 
00332 inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
00333   V.print(OS);
00334   return OS;
00335 }
00336   
00337 void Use::set(Value *V) {
00338   if (Val) removeFromList();
00339   Val = V;
00340   if (V) V->addUse(*this);
00341 }
00342 
00343 
00344 // isa - Provide some specializations of isa so that we don't have to include
00345 // the subtype header files to test to see if the value is a subclass...
00346 //
00347 template <> struct isa_impl<Constant, Value> {
00348   static inline bool doit(const Value &Val) {
00349     return Val.getValueID() >= Value::ConstantFirstVal &&
00350       Val.getValueID() <= Value::ConstantLastVal;
00351   }
00352 };
00353 
00354 template <> struct isa_impl<Argument, Value> {
00355   static inline bool doit (const Value &Val) {
00356     return Val.getValueID() == Value::ArgumentVal;
00357   }
00358 };
00359 
00360 template <> struct isa_impl<InlineAsm, Value> { 
00361   static inline bool doit(const Value &Val) {
00362     return Val.getValueID() == Value::InlineAsmVal;
00363   }
00364 };
00365 
00366 template <> struct isa_impl<Instruction, Value> { 
00367   static inline bool doit(const Value &Val) {
00368     return Val.getValueID() >= Value::InstructionVal;
00369   }
00370 };
00371 
00372 template <> struct isa_impl<BasicBlock, Value> { 
00373   static inline bool doit(const Value &Val) {
00374     return Val.getValueID() == Value::BasicBlockVal;
00375   }
00376 };
00377 
00378 template <> struct isa_impl<Function, Value> { 
00379   static inline bool doit(const Value &Val) {
00380     return Val.getValueID() == Value::FunctionVal;
00381   }
00382 };
00383 
00384 template <> struct isa_impl<GlobalVariable, Value> { 
00385   static inline bool doit(const Value &Val) {
00386     return Val.getValueID() == Value::GlobalVariableVal;
00387   }
00388 };
00389 
00390 template <> struct isa_impl<GlobalAlias, Value> { 
00391   static inline bool doit(const Value &Val) {
00392     return Val.getValueID() == Value::GlobalAliasVal;
00393   }
00394 };
00395 
00396 template <> struct isa_impl<GlobalValue, Value> { 
00397   static inline bool doit(const Value &Val) {
00398     return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
00399       isa<GlobalAlias>(Val);
00400   }
00401 };
00402 
00403 template <> struct isa_impl<MDNode, Value> { 
00404   static inline bool doit(const Value &Val) {
00405     return Val.getValueID() == Value::MDNodeVal;
00406   }
00407 };
00408   
00409 // Value* is only 4-byte aligned.
00410 template<>
00411 class PointerLikeTypeTraits<Value*> {
00412   typedef Value* PT;
00413 public:
00414   static inline void *getAsVoidPointer(PT P) { return P; }
00415   static inline PT getFromVoidPointer(void *P) {
00416     return static_cast<PT>(P);
00417   }
00418   enum { NumLowBitsAvailable = 2 };
00419 };
00420 
00421 // Create wrappers for C Binding types (see CBindingWrapping.h).
00422 DEFINE_ISA_CONVERSION_FUNCTIONS(Value, LLVMValueRef)
00423 
00424 /* Specialized opaque value conversions.
00425  */ 
00426 inline Value **unwrap(LLVMValueRef *Vals) {
00427   return reinterpret_cast<Value**>(Vals);
00428 }
00429 
00430 template<typename T>
00431 inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
00432 #ifdef DEBUG
00433   for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
00434     cast<T>(*I);
00435 #endif
00436   (void)Length;
00437   return reinterpret_cast<T**>(Vals);
00438 }
00439 
00440 inline LLVMValueRef *wrap(const Value **Vals) {
00441   return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
00442 }
00443 
00444 } // End llvm namespace
00445 
00446 #endif