LLVM API Documentation
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 template<typename T> class SmallVectorImpl; 00021 00022 /// This is an important base class in LLVM. It provides the common facilities 00023 /// of all constant values in an LLVM program. A constant is a value that is 00024 /// immutable at runtime. Functions are constants because their address is 00025 /// immutable. Same with global variables. 00026 /// 00027 /// All constants share the capabilities provided in this class. All constants 00028 /// can have a null value. They can have an operand list. Constants can be 00029 /// simple (integer and floating point values), complex (arrays and structures), 00030 /// or expression based (computations yielding a constant value composed of 00031 /// only certain operators and other constant values). 00032 /// 00033 /// Note that Constants are immutable (once created they never change) 00034 /// and are fully shared by structural equivalence. This means that two 00035 /// structurally equivalent constants will always have the same address. 00036 /// Constants are created on demand as needed and never deleted: thus clients 00037 /// don't have to worry about the lifetime of the objects. 00038 /// @brief LLVM Constant Representation 00039 class Constant : public User { 00040 void operator=(const Constant &); // Do not implement 00041 Constant(const Constant &); // Do not implement 00042 protected: 00043 Constant(const Type *ty, ValueTy vty, Use *Ops, unsigned NumOps) 00044 : User(ty, vty, Ops, NumOps) {} 00045 00046 void destroyConstantImpl(); 00047 public: 00048 /// Static constructor to get a '0' constant of arbitrary type... 00049 /// 00050 static Constant *getNullValue(const Type *Ty); 00051 00052 /// Static constructor to get a '-1' constant. This supports integers and 00053 /// vectors. 00054 /// 00055 static Constant *getAllOnesValue(const Type *Ty); 00056 00057 /// isNullValue - Return true if this is the value that would be returned by 00058 /// getNullValue. 00059 virtual bool isNullValue() const = 0; 00060 00061 /// canTrap - Return true if evaluation of this constant could trap. This is 00062 /// true for things like constant expressions that could divide by zero. 00063 bool canTrap() const; 00064 00065 /// ContaintsRelocations - Return true if the constant value contains 00066 /// relocations which cannot be resolved at compile time. 00067 bool ContainsRelocations() const; 00068 00069 // Specialize get/setOperand for Constants as their operands are always 00070 // constants as well. 00071 Constant *getOperand(unsigned i) { 00072 return static_cast<Constant*>(User::getOperand(i)); 00073 } 00074 const Constant *getOperand(unsigned i) const { 00075 return static_cast<const Constant*>(User::getOperand(i)); 00076 } 00077 void setOperand(unsigned i, Constant *C) { 00078 User::setOperand(i, C); 00079 } 00080 00081 /// getVectorElements - This method, which is only valid on constant of vector 00082 /// type, returns the elements of the vector in the specified smallvector. 00083 /// This handles breaking down a vector undef into undef elements, etc. For 00084 /// constant exprs and other cases we can't handle, we return an empty vector. 00085 void getVectorElements(SmallVectorImpl<Constant*> &Elts) const; 00086 00087 /// destroyConstant - Called if some element of this constant is no longer 00088 /// valid. At this point only other constants may be on the use_list for this 00089 /// constant. Any constants on our Use list must also be destroy'd. The 00090 /// implementation must be sure to remove the constant from the list of 00091 /// available cached constants. Implementations should call 00092 /// destroyConstantImpl as the last thing they do, to destroy all users and 00093 /// delete this. 00094 virtual void destroyConstant() { assert(0 && "Not reached!"); } 00095 00096 //// Methods for support type inquiry through isa, cast, and dyn_cast: 00097 static inline bool classof(const Constant *) { return true; } 00098 static inline bool classof(const GlobalValue *) { return true; } 00099 static inline bool classof(const Value *V) { 00100 return V->getValueID() >= ConstantFirstVal && 00101 V->getValueID() <= ConstantLastVal; 00102 } 00103 00104 /// replaceUsesOfWithOnConstant - This method is a special form of 00105 /// User::replaceUsesOfWith (which does not work on constants) that does work 00106 /// on constants. Basically this method goes through the trouble of building 00107 /// a new constant that is equivalent to the current one, with all uses of 00108 /// From replaced with uses of To. After this construction is completed, all 00109 /// of the users of 'this' are replaced to use the new constant, and then 00110 /// 'this' is deleted. In general, you should not call this method, instead, 00111 /// use Value::replaceAllUsesWith, which automatically dispatches to this 00112 /// method as needed. 00113 /// 00114 virtual void replaceUsesOfWithOnConstant(Value *, Value *, Use *) { 00115 // Provide a default implementation for constants (like integers) that 00116 // cannot use any other values. This cannot be called at runtime, but needs 00117 // to be here to avoid link errors. 00118 assert(getNumOperands() == 0 && "replaceUsesOfWithOnConstant must be " 00119 "implemented for all constants that have operands!"); 00120 assert(0 && "Constants that do not have operands cannot be using 'From'!"); 00121 } 00122 }; 00123 00124 } // End llvm namespace 00125 00126 #endif