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_CONSTANT_H
00015 #define LLVM_CONSTANT_H
00016 
00017 #include "llvm/User.h"
00018 
00019 namespace llvm {
00020   class APInt;
00021 
00022   template<typename T> class SmallVectorImpl;
00023   class LLVMContext;
00024 
00025 /// This is an important base class in LLVM. It provides the common facilities
00026 /// of all constant values in an LLVM program. A constant is a value that is
00027 /// immutable at runtime. Functions are constants because their address is
00028 /// immutable. Same with global variables. 
00029 /// 
00030 /// All constants share the capabilities provided in this class. All constants
00031 /// can have a null value. They can have an operand list. Constants can be
00032 /// simple (integer and floating point values), complex (arrays and structures),
00033 /// or expression based (computations yielding a constant value composed of 
00034 /// only certain operators and other constant values).
00035 /// 
00036 /// Note that Constants are immutable (once created they never change) 
00037 /// and are fully shared by structural equivalence.  This means that two 
00038 /// structurally equivalent constants will always have the same address.  
00039 /// Constants are created on demand as needed and never deleted: thus clients 
00040 /// don't have to worry about the lifetime of the objects.
00041 /// @brief LLVM Constant Representation
00042 class Constant : public User {
00043   void operator=(const Constant &);     // Do not implement
00044   Constant(const Constant &);           // Do not implement
00045   
00046 protected:
00047   Constant(const Type *ty, ValueTy vty, Use *Ops, unsigned NumOps)
00048     : User(ty, vty, Ops, NumOps) {}
00049 
00050   void destroyConstantImpl();
00051 public:
00052   /// isNullValue - Return true if this is the value that would be returned by
00053   /// getNullValue.
00054   virtual bool isNullValue() const = 0;
00055 
00056   /// isNegativeZeroValue - Return true if the value is what would be returned 
00057   /// by getZeroValueForNegation.
00058   virtual bool isNegativeZeroValue() const { return isNullValue(); }
00059 
00060   /// canTrap - Return true if evaluation of this constant could trap.  This is
00061   /// true for things like constant expressions that could divide by zero.
00062   bool canTrap() const;
00063 
00064   enum PossibleRelocationsTy {
00065     NoRelocation = 0,
00066     LocalRelocation = 1,
00067     GlobalRelocations = 2
00068   };
00069   
00070   /// getRelocationInfo - This method classifies the entry according to
00071   /// whether or not it may generate a relocation entry.  This must be
00072   /// conservative, so if it might codegen to a relocatable entry, it should say
00073   /// so.  The return values are:
00074   /// 
00075   ///  NoRelocation: This constant pool entry is guaranteed to never have a
00076   ///     relocation applied to it (because it holds a simple constant like
00077   ///     '4').
00078   ///  LocalRelocation: This entry has relocations, but the entries are
00079   ///     guaranteed to be resolvable by the static linker, so the dynamic
00080   ///     linker will never see them.
00081   ///  GlobalRelocations: This entry may have arbitrary relocations.
00082   ///
00083   /// FIXME: This really should not be in VMCore.
00084   PossibleRelocationsTy getRelocationInfo() const;
00085   
00086   // Specialize get/setOperand for Constants as their operands are always
00087   // constants as well.
00088   Constant *getOperand(unsigned i) {
00089     return static_cast<Constant*>(User::getOperand(i));
00090   }
00091   const Constant *getOperand(unsigned i) const {
00092     return static_cast<const Constant*>(User::getOperand(i));
00093   }
00094   void setOperand(unsigned i, Constant *C) {
00095     User::setOperand(i, C);
00096   }
00097   
00098   /// getVectorElements - This method, which is only valid on constant of vector
00099   /// type, returns the elements of the vector in the specified smallvector.
00100   /// This handles breaking down a vector undef into undef elements, etc.  For
00101   /// constant exprs and other cases we can't handle, we return an empty vector.
00102   void getVectorElements(LLVMContext &Context, 
00103                          SmallVectorImpl<Constant*> &Elts) const;
00104 
00105   /// destroyConstant - Called if some element of this constant is no longer
00106   /// valid.  At this point only other constants may be on the use_list for this
00107   /// constant.  Any constants on our Use list must also be destroy'd.  The
00108   /// implementation must be sure to remove the constant from the list of
00109   /// available cached constants.  Implementations should call
00110   /// destroyConstantImpl as the last thing they do, to destroy all users and
00111   /// delete this.
00112   virtual void destroyConstant() { assert(0 && "Not reached!"); }
00113 
00114   //// Methods for support type inquiry through isa, cast, and dyn_cast:
00115   static inline bool classof(const Constant *) { return true; }
00116   static inline bool classof(const GlobalValue *) { return true; }
00117   static inline bool classof(const Value *V) {
00118     return V->getValueID() >= ConstantFirstVal &&
00119            V->getValueID() <= ConstantLastVal;
00120   }
00121 
00122   /// replaceUsesOfWithOnConstant - This method is a special form of
00123   /// User::replaceUsesOfWith (which does not work on constants) that does work
00124   /// on constants.  Basically this method goes through the trouble of building
00125   /// a new constant that is equivalent to the current one, with all uses of
00126   /// From replaced with uses of To.  After this construction is completed, all
00127   /// of the users of 'this' are replaced to use the new constant, and then
00128   /// 'this' is deleted.  In general, you should not call this method, instead,
00129   /// use Value::replaceAllUsesWith, which automatically dispatches to this
00130   /// method as needed.
00131   ///
00132   virtual void replaceUsesOfWithOnConstant(Value *, Value *, Use *) {
00133     // Provide a default implementation for constants (like integers) that
00134     // cannot use any other values.  This cannot be called at runtime, but needs
00135     // to be here to avoid link errors.
00136     assert(getNumOperands() == 0 && "replaceUsesOfWithOnConstant must be "
00137            "implemented for all constants that have operands!");
00138     assert(0 && "Constants that do not have operands cannot be using 'From'!");
00139   }
00140   
00141   static Constant* getNullValue(const Type* Ty);
00142   
00143   /// @returns the value for an integer constant of the given type that has all
00144   /// its bits set to true.
00145   /// @brief Get the all ones value
00146   static Constant* getAllOnesValue(const Type* Ty);
00147 
00148   /// getIntegerValue - Return the value for an integer or pointer constant,
00149   /// or a vector thereof, with the given scalar value.
00150   static Constant* getIntegerValue(const Type* Ty, const APInt &V);
00151 };
00152 
00153 } // End llvm namespace
00154 
00155 #endif