Constant.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //===-- llvm/Constant.h - Constant class definition -------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains the declaration of the Constant class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_IR_CONSTANT_H
  14. #define LLVM_IR_CONSTANT_H
  15. #include "llvm/IR/User.h"
  16. namespace llvm {
  17. class APInt;
  18. template<typename T> class SmallVectorImpl;
  19. /// This is an important base class in LLVM. It provides the common facilities
  20. /// of all constant values in an LLVM program. A constant is a value that is
  21. /// immutable at runtime. Functions are constants because their address is
  22. /// immutable. Same with global variables.
  23. ///
  24. /// All constants share the capabilities provided in this class. All constants
  25. /// can have a null value. They can have an operand list. Constants can be
  26. /// simple (integer and floating point values), complex (arrays and structures),
  27. /// or expression based (computations yielding a constant value composed of
  28. /// only certain operators and other constant values).
  29. ///
  30. /// Note that Constants are immutable (once created they never change)
  31. /// and are fully shared by structural equivalence. This means that two
  32. /// structurally equivalent constants will always have the same address.
  33. /// Constants are created on demand as needed and never deleted: thus clients
  34. /// don't have to worry about the lifetime of the objects.
  35. /// @brief LLVM Constant Representation
  36. class Constant : public User {
  37. void operator=(const Constant &) = delete;
  38. Constant(const Constant &) = delete;
  39. void anchor() override;
  40. protected:
  41. Constant(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps)
  42. : User(ty, vty, Ops, NumOps) {}
  43. public:
  44. /// isNullValue - Return true if this is the value that would be returned by
  45. /// getNullValue.
  46. bool isNullValue() const;
  47. /// \brief Returns true if the value is one.
  48. bool isOneValue() const;
  49. /// isAllOnesValue - Return true if this is the value that would be returned by
  50. /// getAllOnesValue.
  51. bool isAllOnesValue() const;
  52. /// isNegativeZeroValue - Return true if the value is what would be returned
  53. /// by getZeroValueForNegation.
  54. bool isNegativeZeroValue() const;
  55. /// Return true if the value is negative zero or null value.
  56. bool isZeroValue() const;
  57. /// \brief Return true if the value is not the smallest signed value.
  58. bool isNotMinSignedValue() const;
  59. /// \brief Return true if the value is the smallest signed value.
  60. bool isMinSignedValue() const;
  61. /// canTrap - Return true if evaluation of this constant could trap. This is
  62. /// true for things like constant expressions that could divide by zero.
  63. bool canTrap() const;
  64. /// isThreadDependent - Return true if the value can vary between threads.
  65. bool isThreadDependent() const;
  66. /// Return true if the value is dependent on a dllimport variable.
  67. bool isDLLImportDependent() const;
  68. /// isConstantUsed - Return true if the constant has users other than constant
  69. /// exprs and other dangling things.
  70. bool isConstantUsed() const;
  71. enum PossibleRelocationsTy {
  72. NoRelocation = 0,
  73. LocalRelocation = 1,
  74. GlobalRelocations = 2
  75. };
  76. /// getRelocationInfo - This method classifies the entry according to
  77. /// whether or not it may generate a relocation entry. This must be
  78. /// conservative, so if it might codegen to a relocatable entry, it should say
  79. /// so. The return values are:
  80. ///
  81. /// NoRelocation: This constant pool entry is guaranteed to never have a
  82. /// relocation applied to it (because it holds a simple constant like
  83. /// '4').
  84. /// LocalRelocation: This entry has relocations, but the entries are
  85. /// guaranteed to be resolvable by the static linker, so the dynamic
  86. /// linker will never see them.
  87. /// GlobalRelocations: This entry may have arbitrary relocations.
  88. ///
  89. /// FIXME: This really should not be in VMCore.
  90. PossibleRelocationsTy getRelocationInfo() const;
  91. /// getAggregateElement - For aggregates (struct/array/vector) return the
  92. /// constant that corresponds to the specified element if possible, or null if
  93. /// not. This can return null if the element index is a ConstantExpr, or if
  94. /// 'this' is a constant expr.
  95. Constant *getAggregateElement(unsigned Elt) const;
  96. Constant *getAggregateElement(Constant *Elt) const;
  97. /// getSplatValue - If this is a splat vector constant, meaning that all of
  98. /// the elements have the same value, return that value. Otherwise return 0.
  99. Constant *getSplatValue() const;
  100. /// If C is a constant integer then return its value, otherwise C must be a
  101. /// vector of constant integers, all equal, and the common value is returned.
  102. const APInt &getUniqueInteger() const;
  103. /// Called if some element of this constant is no longer valid.
  104. /// At this point only other constants may be on the use_list for this
  105. /// constant. Any constants on our Use list must also be destroy'd. The
  106. /// implementation must be sure to remove the constant from the list of
  107. /// available cached constants. Implementations should implement
  108. /// destroyConstantImpl to remove constants from any pools/maps they are
  109. /// contained it.
  110. void destroyConstant();
  111. //// Methods for support type inquiry through isa, cast, and dyn_cast:
  112. static inline bool classof(const Value *V) {
  113. return V->getValueID() >= ConstantFirstVal &&
  114. V->getValueID() <= ConstantLastVal;
  115. }
  116. /// This method is a special form of User::replaceUsesOfWith
  117. /// (which does not work on constants) that does work
  118. /// on constants. Basically this method goes through the trouble of building
  119. /// a new constant that is equivalent to the current one, with all uses of
  120. /// From replaced with uses of To. After this construction is completed, all
  121. /// of the users of 'this' are replaced to use the new constant, and then
  122. /// 'this' is deleted. In general, you should not call this method, instead,
  123. /// use Value::replaceAllUsesWith, which automatically dispatches to this
  124. /// method as needed.
  125. ///
  126. void handleOperandChange(Value *, Value *, Use *);
  127. static Constant *getNullValue(Type* Ty);
  128. /// @returns the value for an integer or vector of integer constant of the
  129. /// given type that has all its bits set to true.
  130. /// @brief Get the all ones value
  131. static Constant *getAllOnesValue(Type* Ty);
  132. /// getIntegerValue - Return the value for an integer or pointer constant,
  133. /// or a vector thereof, with the given scalar value.
  134. static Constant *getIntegerValue(Type* Ty, const APInt &V);
  135. /// removeDeadConstantUsers - If there are any dead constant users dangling
  136. /// off of this constant, remove them. This method is useful for clients
  137. /// that want to check to see if a global is unused, but don't want to deal
  138. /// with potentially dead constants hanging off of the globals.
  139. void removeDeadConstantUsers() const;
  140. Constant *stripPointerCasts() {
  141. return cast<Constant>(Value::stripPointerCasts());
  142. }
  143. const Constant *stripPointerCasts() const {
  144. return const_cast<Constant*>(this)->stripPointerCasts();
  145. }
  146. };
  147. } // End llvm namespace
  148. #endif