Constant.h 10 KB

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