LLVM API Documentation

Constant.h
Go to the documentation of this file.
00001 //===-- llvm/Constant.h - Constant class definition -------------*- 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 contains the declaration of the Constant class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_IR_CONSTANT_H
00015 #define LLVM_IR_CONSTANT_H
00016 
00017 #include "llvm/IR/User.h"
00018 
00019 namespace llvm {
00020   class APInt;
00021 
00022   template<typename T> class SmallVectorImpl;
00023 
00024 /// This is an important base class in LLVM. It provides the common facilities
00025 /// of all constant values in an LLVM program. A constant is a value that is
00026 /// immutable at runtime. Functions are constants because their address is
00027 /// immutable. Same with global variables. 
00028 /// 
00029 /// All constants share the capabilities provided in this class. All constants
00030 /// can have a null value. They can have an operand list. Constants can be
00031 /// simple (integer and floating point values), complex (arrays and structures),
00032 /// or expression based (computations yielding a constant value composed of 
00033 /// only certain operators and other constant values).
00034 /// 
00035 /// Note that Constants are immutable (once created they never change) 
00036 /// and are fully shared by structural equivalence.  This means that two 
00037 /// structurally equivalent constants will always have the same address.  
00038 /// Constants are created on demand as needed and never deleted: thus clients 
00039 /// don't have to worry about the lifetime of the objects.
00040 /// @brief LLVM Constant Representation
00041 class Constant : public User {
00042   void operator=(const Constant &) LLVM_DELETED_FUNCTION;
00043   Constant(const Constant &) LLVM_DELETED_FUNCTION;
00044   void anchor() override;
00045 
00046 protected:
00047   Constant(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps)
00048     : User(ty, vty, Ops, NumOps) {}
00049 
00050   void destroyConstantImpl();
00051   void replaceUsesOfWithOnConstantImpl(Constant *Replacement);
00052 
00053 public:
00054   /// isNullValue - Return true if this is the value that would be returned by
00055   /// getNullValue.
00056   bool isNullValue() const;
00057 
00058   /// \brief Returns true if the value is one.
00059   bool isOneValue() const;
00060 
00061   /// isAllOnesValue - Return true if this is the value that would be returned by
00062   /// getAllOnesValue.
00063   bool isAllOnesValue() const;
00064 
00065   /// isNegativeZeroValue - Return true if the value is what would be returned 
00066   /// by getZeroValueForNegation.
00067   bool isNegativeZeroValue() const;
00068 
00069   /// Return true if the value is negative zero or null value.
00070   bool isZeroValue() const;
00071 
00072   /// \brief Return true if the value is not the smallest signed value.
00073   bool isNotMinSignedValue() const;
00074 
00075   /// \brief Return true if the value is the smallest signed value.
00076   bool isMinSignedValue() const;
00077 
00078   /// canTrap - Return true if evaluation of this constant could trap.  This is
00079   /// true for things like constant expressions that could divide by zero.
00080   bool canTrap() const;
00081 
00082   /// isThreadDependent - Return true if the value can vary between threads.
00083   bool isThreadDependent() const;
00084 
00085   /// Return true if the value is dependent on a dllimport variable.
00086   bool isDLLImportDependent() const;
00087 
00088   /// isConstantUsed - Return true if the constant has users other than constant
00089   /// exprs and other dangling things.
00090   bool isConstantUsed() const;
00091   
00092   enum PossibleRelocationsTy {
00093     NoRelocation = 0,
00094     LocalRelocation = 1,
00095     GlobalRelocations = 2
00096   };
00097   
00098   /// getRelocationInfo - This method classifies the entry according to
00099   /// whether or not it may generate a relocation entry.  This must be
00100   /// conservative, so if it might codegen to a relocatable entry, it should say
00101   /// so.  The return values are:
00102   /// 
00103   ///  NoRelocation: This constant pool entry is guaranteed to never have a
00104   ///     relocation applied to it (because it holds a simple constant like
00105   ///     '4').
00106   ///  LocalRelocation: This entry has relocations, but the entries are
00107   ///     guaranteed to be resolvable by the static linker, so the dynamic
00108   ///     linker will never see them.
00109   ///  GlobalRelocations: This entry may have arbitrary relocations.
00110   ///
00111   /// FIXME: This really should not be in VMCore.
00112   PossibleRelocationsTy getRelocationInfo() const;
00113   
00114   /// getAggregateElement - For aggregates (struct/array/vector) return the
00115   /// constant that corresponds to the specified element if possible, or null if
00116   /// not.  This can return null if the element index is a ConstantExpr, or if
00117   /// 'this' is a constant expr.
00118   Constant *getAggregateElement(unsigned Elt) const;
00119   Constant *getAggregateElement(Constant *Elt) const;
00120 
00121   /// getSplatValue - If this is a splat vector constant, meaning that all of
00122   /// the elements have the same value, return that value. Otherwise return 0.
00123   Constant *getSplatValue() const;
00124 
00125   /// If C is a constant integer then return its value, otherwise C must be a
00126   /// vector of constant integers, all equal, and the common value is returned.
00127   const APInt &getUniqueInteger() const;
00128 
00129   /// destroyConstant - Called if some element of this constant is no longer
00130   /// valid.  At this point only other constants may be on the use_list for this
00131   /// constant.  Any constants on our Use list must also be destroy'd.  The
00132   /// implementation must be sure to remove the constant from the list of
00133   /// available cached constants.  Implementations should call
00134   /// destroyConstantImpl as the last thing they do, to destroy all users and
00135   /// delete this.
00136   virtual void destroyConstant() { llvm_unreachable("Not reached!"); }
00137 
00138   //// Methods for support type inquiry through isa, cast, and dyn_cast:
00139   static inline bool classof(const Value *V) {
00140     return V->getValueID() >= ConstantFirstVal &&
00141            V->getValueID() <= ConstantLastVal;
00142   }
00143 
00144   /// replaceUsesOfWithOnConstant - This method is a special form of
00145   /// User::replaceUsesOfWith (which does not work on constants) that does work
00146   /// on constants.  Basically this method goes through the trouble of building
00147   /// a new constant that is equivalent to the current one, with all uses of
00148   /// From replaced with uses of To.  After this construction is completed, all
00149   /// of the users of 'this' are replaced to use the new constant, and then
00150   /// 'this' is deleted.  In general, you should not call this method, instead,
00151   /// use Value::replaceAllUsesWith, which automatically dispatches to this
00152   /// method as needed.
00153   ///
00154   virtual void replaceUsesOfWithOnConstant(Value *, Value *, Use *) {
00155     // Provide a default implementation for constants (like integers) that
00156     // cannot use any other values.  This cannot be called at runtime, but needs
00157     // to be here to avoid link errors.
00158     assert(getNumOperands() == 0 && "replaceUsesOfWithOnConstant must be "
00159            "implemented for all constants that have operands!");
00160     llvm_unreachable("Constants that do not have operands cannot be using "
00161                      "'From'!");
00162   }
00163 
00164   static Constant *getNullValue(Type* Ty);
00165 
00166   /// @returns the value for an integer or vector of integer constant of the
00167   /// given type that has all its bits set to true.
00168   /// @brief Get the all ones value
00169   static Constant *getAllOnesValue(Type* Ty);
00170 
00171   /// getIntegerValue - Return the value for an integer or pointer constant,
00172   /// or a vector thereof, with the given scalar value.
00173   static Constant *getIntegerValue(Type* Ty, const APInt &V);
00174   
00175   /// removeDeadConstantUsers - If there are any dead constant users dangling
00176   /// off of this constant, remove them.  This method is useful for clients
00177   /// that want to check to see if a global is unused, but don't want to deal
00178   /// with potentially dead constants hanging off of the globals.
00179   void removeDeadConstantUsers() const;
00180 
00181   Constant *stripPointerCasts() {
00182     return cast<Constant>(Value::stripPointerCasts());
00183   }
00184 
00185   const Constant *stripPointerCasts() const {
00186     return const_cast<Constant*>(this)->stripPointerCasts();
00187   }
00188 };
00189 
00190 } // End llvm namespace
00191 
00192 #endif