Constant.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //===-- llvm/Constant.h - Constant class definition -------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains the declaration of the Constant class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_IR_CONSTANT_H
  13. #define LLVM_IR_CONSTANT_H
  14. #include "llvm/IR/User.h"
  15. #include "llvm/IR/Value.h"
  16. #include "llvm/Support/Casting.h"
  17. namespace llvm {
  18. class APInt;
  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. /// LLVM Constant Representation
  36. class Constant : public User {
  37. protected:
  38. Constant(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps)
  39. : User(ty, vty, Ops, NumOps) {}
  40. ~Constant() = default;
  41. public:
  42. void operator=(const Constant &) = delete;
  43. Constant(const Constant &) = delete;
  44. /// Return true if this is the value that would be returned by getNullValue.
  45. bool isNullValue() const;
  46. /// Returns true if the value is one.
  47. bool isOneValue() const;
  48. /// Return true if the value is not the one value, or,
  49. /// for vectors, does not contain one value elements.
  50. bool isNotOneValue() const;
  51. /// Return true if this is the value that would be returned by
  52. /// getAllOnesValue.
  53. bool isAllOnesValue() const;
  54. /// Return true if the value is what would be returned by
  55. /// getZeroValueForNegation.
  56. bool isNegativeZeroValue() const;
  57. /// Return true if the value is negative zero or null value.
  58. bool isZeroValue() const;
  59. /// Return true if the value is not the smallest signed value, or,
  60. /// for vectors, does not contain smallest signed value elements.
  61. bool isNotMinSignedValue() const;
  62. /// Return true if the value is the smallest signed value.
  63. bool isMinSignedValue() const;
  64. /// Return true if this is a finite and non-zero floating-point scalar
  65. /// constant or a fixed width vector constant with all finite and non-zero
  66. /// elements.
  67. bool isFiniteNonZeroFP() const;
  68. /// Return true if this is a normal (as opposed to denormal, infinity, nan,
  69. /// or zero) floating-point scalar constant or a vector constant with all
  70. /// normal elements. See APFloat::isNormal.
  71. bool isNormalFP() const;
  72. /// Return true if this scalar has an exact multiplicative inverse or this
  73. /// vector has an exact multiplicative inverse for each element in the vector.
  74. bool hasExactInverseFP() const;
  75. /// Return true if this is a floating-point NaN constant or a vector
  76. /// floating-point constant with all NaN elements.
  77. bool isNaN() const;
  78. /// Return true if this constant and a constant 'Y' are element-wise equal.
  79. /// This is identical to just comparing the pointers, with the exception that
  80. /// for vectors, if only one of the constants has an `undef` element in some
  81. /// lane, the constants still match.
  82. bool isElementWiseEqual(Value *Y) const;
  83. /// Return true if this is a vector constant that includes any undef or
  84. /// poison elements. Since it is impossible to inspect a scalable vector
  85. /// element- wise at compile time, this function returns true only if the
  86. /// entire vector is undef or poison.
  87. bool containsUndefOrPoisonElement() const;
  88. /// Return true if this is a vector constant that includes any poison
  89. /// elements.
  90. bool containsPoisonElement() const;
  91. /// Return true if this is a fixed width vector constant that includes
  92. /// any constant expressions.
  93. bool containsConstantExpression() const;
  94. /// Return true if evaluation of this constant could trap. This is true for
  95. /// things like constant expressions that could divide by zero.
  96. bool canTrap() const;
  97. /// Return true if the value can vary between threads.
  98. bool isThreadDependent() const;
  99. /// Return true if the value is dependent on a dllimport variable.
  100. bool isDLLImportDependent() const;
  101. /// Return true if the constant has users other than constant expressions and
  102. /// other dangling things.
  103. bool isConstantUsed() const;
  104. /// This method classifies the entry according to whether or not it may
  105. /// generate a relocation entry (either static or dynamic). This must be
  106. /// conservative, so if it might codegen to a relocatable entry, it should say
  107. /// so.
  108. ///
  109. /// FIXME: This really should not be in IR.
  110. bool needsRelocation() const;
  111. bool needsDynamicRelocation() const;
  112. /// For aggregates (struct/array/vector) return the constant that corresponds
  113. /// to the specified element if possible, or null if not. This can return null
  114. /// if the element index is a ConstantExpr, if 'this' is a constant expr or
  115. /// if the constant does not fit into an uint64_t.
  116. Constant *getAggregateElement(unsigned Elt) const;
  117. Constant *getAggregateElement(Constant *Elt) const;
  118. /// If all elements of the vector constant have the same value, return that
  119. /// value. Otherwise, return nullptr. Ignore undefined elements by setting
  120. /// AllowUndefs to true.
  121. Constant *getSplatValue(bool AllowUndefs = false) const;
  122. /// If C is a constant integer then return its value, otherwise C must be a
  123. /// vector of constant integers, all equal, and the common value is returned.
  124. const APInt &getUniqueInteger() const;
  125. /// Called if some element of this constant is no longer valid.
  126. /// At this point only other constants may be on the use_list for this
  127. /// constant. Any constants on our Use list must also be destroy'd. The
  128. /// implementation must be sure to remove the constant from the list of
  129. /// available cached constants. Implementations should implement
  130. /// destroyConstantImpl to remove constants from any pools/maps they are
  131. /// contained it.
  132. void destroyConstant();
  133. //// Methods for support type inquiry through isa, cast, and dyn_cast:
  134. static bool classof(const Value *V) {
  135. static_assert(ConstantFirstVal == 0, "V->getValueID() >= ConstantFirstVal always succeeds");
  136. return V->getValueID() <= ConstantLastVal;
  137. }
  138. /// This method is a special form of User::replaceUsesOfWith
  139. /// (which does not work on constants) that does work
  140. /// on constants. Basically this method goes through the trouble of building
  141. /// a new constant that is equivalent to the current one, with all uses of
  142. /// From replaced with uses of To. After this construction is completed, all
  143. /// of the users of 'this' are replaced to use the new constant, and then
  144. /// 'this' is deleted. In general, you should not call this method, instead,
  145. /// use Value::replaceAllUsesWith, which automatically dispatches to this
  146. /// method as needed.
  147. ///
  148. void handleOperandChange(Value *, Value *);
  149. static Constant *getNullValue(Type* Ty);
  150. /// @returns the value for an integer or vector of integer constant of the
  151. /// given type that has all its bits set to true.
  152. /// Get the all ones value
  153. static Constant *getAllOnesValue(Type* Ty);
  154. /// Return the value for an integer or pointer constant, or a vector thereof,
  155. /// with the given scalar value.
  156. static Constant *getIntegerValue(Type *Ty, const APInt &V);
  157. /// If there are any dead constant users dangling off of this constant, remove
  158. /// them. This method is useful for clients that want to check to see if a
  159. /// global is unused, but don't want to deal with potentially dead constants
  160. /// hanging off of the globals.
  161. void removeDeadConstantUsers() const;
  162. const Constant *stripPointerCasts() const {
  163. return cast<Constant>(Value::stripPointerCasts());
  164. }
  165. Constant *stripPointerCasts() {
  166. return const_cast<Constant*>(
  167. static_cast<const Constant *>(this)->stripPointerCasts());
  168. }
  169. /// Try to replace undefined constant C or undefined elements in C with
  170. /// Replacement. If no changes are made, the constant C is returned.
  171. static Constant *replaceUndefsWith(Constant *C, Constant *Replacement);
  172. /// Merges undefs of a Constant with another Constant, along with the
  173. /// undefs already present. Other doesn't have to be the same type as C, but
  174. /// both must either be scalars or vectors with the same element count. If no
  175. /// changes are made, the constant C is returned.
  176. static Constant *mergeUndefsWith(Constant *C, Constant *Other);
  177. /// Return true if a constant is ConstantData or a ConstantAggregate or
  178. /// ConstantExpr that contain only ConstantData.
  179. bool isManifestConstant() const;
  180. private:
  181. enum PossibleRelocationsTy {
  182. /// This constant requires no relocations. That is, it holds simple
  183. /// constants (like integrals).
  184. NoRelocation = 0,
  185. /// This constant holds static relocations that can be resolved by the
  186. /// static linker.
  187. LocalRelocation = 1,
  188. /// This constant holds dynamic relocations that the dynamic linker will
  189. /// need to resolve.
  190. GlobalRelocation = 2,
  191. };
  192. /// Determine what potential relocations may be needed by this constant.
  193. PossibleRelocationsTy getRelocationInfo() const;
  194. };
  195. } // end namespace llvm
  196. #endif // LLVM_IR_CONSTANT_H