ConstantRange.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //===- ConstantRange.h - Represent a range ----------------------*- 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. // Represent a range of possible values that may occur when the program is run
  11. // for an integral value. This keeps track of a lower and upper bound for the
  12. // constant, which MAY wrap around the end of the numeric range. To do this, it
  13. // keeps track of a [lower, upper) bound, which specifies an interval just like
  14. // STL iterators. When used with boolean values, the following are important
  15. // ranges: :
  16. //
  17. // [F, F) = {} = Empty set
  18. // [T, F) = {T}
  19. // [F, T) = {F}
  20. // [T, T) = {F, T} = Full set
  21. //
  22. // The other integral ranges use min/max values for special range values. For
  23. // example, for 8-bit types, it uses:
  24. // [0, 0) = {} = Empty set
  25. // [255, 255) = {0..255} = Full Set
  26. //
  27. // Note that ConstantRange can be used to represent either signed or
  28. // unsigned ranges.
  29. //
  30. //===----------------------------------------------------------------------===//
  31. #ifndef LLVM_IR_CONSTANTRANGE_H
  32. #define LLVM_IR_CONSTANTRANGE_H
  33. #include "llvm/ADT/APInt.h"
  34. #include "llvm/IR/InstrTypes.h"
  35. #include "llvm/Support/DataTypes.h"
  36. namespace llvm {
  37. /// This class represents a range of values.
  38. ///
  39. class ConstantRange {
  40. APInt Lower, Upper;
  41. // If we have move semantics, pass APInts by value and move them into place.
  42. typedef APInt APIntMoveTy;
  43. public:
  44. /// Initialize a full (the default) or empty set for the specified bit width.
  45. ///
  46. explicit ConstantRange(uint32_t BitWidth, bool isFullSet = true);
  47. /// Initialize a range to hold the single specified value.
  48. ///
  49. ConstantRange(APIntMoveTy Value);
  50. /// @brief Initialize a range of values explicitly. This will assert out if
  51. /// Lower==Upper and Lower != Min or Max value for its type. It will also
  52. /// assert out if the two APInt's are not the same bit width.
  53. ConstantRange(APIntMoveTy Lower, APIntMoveTy Upper);
  54. /// Produce the smallest range such that all values that may satisfy the given
  55. /// predicate with any value contained within Other is contained in the
  56. /// returned range. Formally, this returns a superset of
  57. /// 'union over all y in Other . { x : icmp op x y is true }'. If the exact
  58. /// answer is not representable as a ConstantRange, the return value will be a
  59. /// proper superset of the above.
  60. ///
  61. /// Example: Pred = ult and Other = i8 [2, 5) returns Result = [0, 4)
  62. static ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred,
  63. const ConstantRange &Other);
  64. /// Produce the largest range such that all values in the returned range
  65. /// satisfy the given predicate with all values contained within Other.
  66. /// Formally, this returns a subset of
  67. /// 'intersection over all y in Other . { x : icmp op x y is true }'. If the
  68. /// exact answer is not representable as a ConstantRange, the return value
  69. /// will be a proper subset of the above.
  70. ///
  71. /// Example: Pred = ult and Other = i8 [2, 5) returns [0, 2)
  72. static ConstantRange makeSatisfyingICmpRegion(CmpInst::Predicate Pred,
  73. const ConstantRange &Other);
  74. /// Return the lower value for this range.
  75. ///
  76. const APInt &getLower() const { return Lower; }
  77. /// Return the upper value for this range.
  78. ///
  79. const APInt &getUpper() const { return Upper; }
  80. /// Get the bit width of this ConstantRange.
  81. ///
  82. uint32_t getBitWidth() const { return Lower.getBitWidth(); }
  83. /// Return true if this set contains all of the elements possible
  84. /// for this data-type.
  85. ///
  86. bool isFullSet() const;
  87. /// Return true if this set contains no members.
  88. ///
  89. bool isEmptySet() const;
  90. /// Return true if this set wraps around the top of the range.
  91. /// For example: [100, 8).
  92. ///
  93. bool isWrappedSet() const;
  94. /// Return true if this set wraps around the INT_MIN of
  95. /// its bitwidth. For example: i8 [120, 140).
  96. ///
  97. bool isSignWrappedSet() const;
  98. /// Return true if the specified value is in the set.
  99. ///
  100. bool contains(const APInt &Val) const;
  101. /// Return true if the other range is a subset of this one.
  102. ///
  103. bool contains(const ConstantRange &CR) const;
  104. /// If this set contains a single element, return it, otherwise return null.
  105. ///
  106. const APInt *getSingleElement() const {
  107. if (Upper == Lower + 1)
  108. return &Lower;
  109. return nullptr;
  110. }
  111. /// Return true if this set contains exactly one member.
  112. ///
  113. bool isSingleElement() const { return getSingleElement() != nullptr; }
  114. /// Return the number of elements in this set.
  115. ///
  116. APInt getSetSize() const;
  117. /// Return the largest unsigned value contained in the ConstantRange.
  118. ///
  119. APInt getUnsignedMax() const;
  120. /// Return the smallest unsigned value contained in the ConstantRange.
  121. ///
  122. APInt getUnsignedMin() const;
  123. /// Return the largest signed value contained in the ConstantRange.
  124. ///
  125. APInt getSignedMax() const;
  126. /// Return the smallest signed value contained in the ConstantRange.
  127. ///
  128. APInt getSignedMin() const;
  129. /// Return true if this range is equal to another range.
  130. ///
  131. bool operator==(const ConstantRange &CR) const {
  132. return Lower == CR.Lower && Upper == CR.Upper;
  133. }
  134. bool operator!=(const ConstantRange &CR) const {
  135. return !operator==(CR);
  136. }
  137. /// Subtract the specified constant from the endpoints of this constant range.
  138. ConstantRange subtract(const APInt &CI) const;
  139. /// \brief Subtract the specified range from this range (aka relative
  140. /// complement of the sets).
  141. ConstantRange difference(const ConstantRange &CR) const;
  142. /// Return the range that results from the intersection of
  143. /// this range with another range. The resultant range is guaranteed to
  144. /// include all elements contained in both input ranges, and to have the
  145. /// smallest possible set size that does so. Because there may be two
  146. /// intersections with the same set size, A.intersectWith(B) might not
  147. /// be equal to B.intersectWith(A).
  148. ///
  149. ConstantRange intersectWith(const ConstantRange &CR) const;
  150. /// Return the range that results from the union of this range
  151. /// with another range. The resultant range is guaranteed to include the
  152. /// elements of both sets, but may contain more. For example, [3, 9) union
  153. /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
  154. /// in either set before.
  155. ///
  156. ConstantRange unionWith(const ConstantRange &CR) const;
  157. /// Return a new range in the specified integer type, which must
  158. /// be strictly larger than the current type. The returned range will
  159. /// correspond to the possible range of values if the source range had been
  160. /// zero extended to BitWidth.
  161. ConstantRange zeroExtend(uint32_t BitWidth) const;
  162. /// Return a new range in the specified integer type, which must
  163. /// be strictly larger than the current type. The returned range will
  164. /// correspond to the possible range of values if the source range had been
  165. /// sign extended to BitWidth.
  166. ConstantRange signExtend(uint32_t BitWidth) const;
  167. /// Return a new range in the specified integer type, which must be
  168. /// strictly smaller than the current type. The returned range will
  169. /// correspond to the possible range of values if the source range had been
  170. /// truncated to the specified type.
  171. ConstantRange truncate(uint32_t BitWidth) const;
  172. /// Make this range have the bit width given by \p BitWidth. The
  173. /// value is zero extended, truncated, or left alone to make it that width.
  174. ConstantRange zextOrTrunc(uint32_t BitWidth) const;
  175. /// Make this range have the bit width given by \p BitWidth. The
  176. /// value is sign extended, truncated, or left alone to make it that width.
  177. ConstantRange sextOrTrunc(uint32_t BitWidth) const;
  178. /// Return a new range representing the possible values resulting
  179. /// from an addition of a value in this range and a value in \p Other.
  180. ConstantRange add(const ConstantRange &Other) const;
  181. /// Return a new range representing the possible values resulting
  182. /// from a subtraction of a value in this range and a value in \p Other.
  183. ConstantRange sub(const ConstantRange &Other) const;
  184. /// Return a new range representing the possible values resulting
  185. /// from a multiplication of a value in this range and a value in \p Other,
  186. /// treating both this and \p Other as unsigned ranges.
  187. ConstantRange multiply(const ConstantRange &Other) const;
  188. /// Return a new range representing the possible values resulting
  189. /// from a signed maximum of a value in this range and a value in \p Other.
  190. ConstantRange smax(const ConstantRange &Other) const;
  191. /// Return a new range representing the possible values resulting
  192. /// from an unsigned maximum of a value in this range and a value in \p Other.
  193. ConstantRange umax(const ConstantRange &Other) const;
  194. /// Return a new range representing the possible values resulting
  195. /// from an unsigned division of a value in this range and a value in
  196. /// \p Other.
  197. ConstantRange udiv(const ConstantRange &Other) const;
  198. /// Return a new range representing the possible values resulting
  199. /// from a binary-and of a value in this range by a value in \p Other.
  200. ConstantRange binaryAnd(const ConstantRange &Other) const;
  201. /// Return a new range representing the possible values resulting
  202. /// from a binary-or of a value in this range by a value in \p Other.
  203. ConstantRange binaryOr(const ConstantRange &Other) const;
  204. /// Return a new range representing the possible values resulting
  205. /// from a left shift of a value in this range by a value in \p Other.
  206. /// TODO: This isn't fully implemented yet.
  207. ConstantRange shl(const ConstantRange &Other) const;
  208. /// Return a new range representing the possible values resulting from a
  209. /// logical right shift of a value in this range and a value in \p Other.
  210. ConstantRange lshr(const ConstantRange &Other) const;
  211. /// Return a new range that is the logical not of the current set.
  212. ///
  213. ConstantRange inverse() const;
  214. /// Print out the bounds to a stream.
  215. ///
  216. void print(raw_ostream &OS) const;
  217. /// Allow printing from a debugger easily.
  218. ///
  219. void dump() const;
  220. };
  221. inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
  222. CR.print(OS);
  223. return OS;
  224. }
  225. } // End llvm namespace
  226. #endif