Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

269 lines
10 KiB

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