ConstantRange.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ConstantRange.h - Represent a range ----------------------*- 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. // Represent a range of possible values that may occur when the program is run
  15. // for an integral value. This keeps track of a lower and upper bound for the
  16. // constant, which MAY wrap around the end of the numeric range. To do this, it
  17. // keeps track of a [lower, upper) bound, which specifies an interval just like
  18. // STL iterators. When used with boolean values, the following are important
  19. // ranges: :
  20. //
  21. // [F, F) = {} = Empty set
  22. // [T, F) = {T}
  23. // [F, T) = {F}
  24. // [T, T) = {F, T} = Full set
  25. //
  26. // The other integral ranges use min/max values for special range values. For
  27. // example, for 8-bit types, it uses:
  28. // [0, 0) = {} = Empty set
  29. // [255, 255) = {0..255} = Full Set
  30. //
  31. // Note that ConstantRange can be used to represent either signed or
  32. // unsigned ranges.
  33. //
  34. //===----------------------------------------------------------------------===//
  35. #ifndef LLVM_IR_CONSTANTRANGE_H
  36. #define LLVM_IR_CONSTANTRANGE_H
  37. #include "llvm/ADT/APInt.h"
  38. #include "llvm/IR/InstrTypes.h"
  39. #include "llvm/IR/Instruction.h"
  40. #include "llvm/Support/Compiler.h"
  41. #include <cstdint>
  42. namespace llvm {
  43. class MDNode;
  44. class raw_ostream;
  45. struct KnownBits;
  46. /// This class represents a range of values.
  47. class [[nodiscard]] ConstantRange {
  48. APInt Lower, Upper;
  49. /// Create empty constant range with same bitwidth.
  50. ConstantRange getEmpty() const {
  51. return ConstantRange(getBitWidth(), false);
  52. }
  53. /// Create full constant range with same bitwidth.
  54. ConstantRange getFull() const {
  55. return ConstantRange(getBitWidth(), true);
  56. }
  57. public:
  58. /// Initialize a full or empty set for the specified bit width.
  59. explicit ConstantRange(uint32_t BitWidth, bool isFullSet);
  60. /// Initialize a range to hold the single specified value.
  61. ConstantRange(APInt Value);
  62. /// Initialize a range of values explicitly. This will assert out if
  63. /// Lower==Upper and Lower != Min or Max value for its type. It will also
  64. /// assert out if the two APInt's are not the same bit width.
  65. ConstantRange(APInt Lower, APInt Upper);
  66. /// Create empty constant range with the given bit width.
  67. static ConstantRange getEmpty(uint32_t BitWidth) {
  68. return ConstantRange(BitWidth, false);
  69. }
  70. /// Create full constant range with the given bit width.
  71. static ConstantRange getFull(uint32_t BitWidth) {
  72. return ConstantRange(BitWidth, true);
  73. }
  74. /// Create non-empty constant range with the given bounds. If Lower and
  75. /// Upper are the same, a full range is returned.
  76. static ConstantRange getNonEmpty(APInt Lower, APInt Upper) {
  77. if (Lower == Upper)
  78. return getFull(Lower.getBitWidth());
  79. return ConstantRange(std::move(Lower), std::move(Upper));
  80. }
  81. /// Initialize a range based on a known bits constraint. The IsSigned flag
  82. /// indicates whether the constant range should not wrap in the signed or
  83. /// unsigned domain.
  84. static ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned);
  85. /// Produce the smallest range such that all values that may satisfy the given
  86. /// predicate with any value contained within Other is contained in the
  87. /// returned range. Formally, this returns a superset of
  88. /// 'union over all y in Other . { x : icmp op x y is true }'. If the exact
  89. /// answer is not representable as a ConstantRange, the return value will be a
  90. /// proper superset of the above.
  91. ///
  92. /// Example: Pred = ult and Other = i8 [2, 5) returns Result = [0, 4)
  93. static ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred,
  94. const ConstantRange &Other);
  95. /// Produce the largest range such that all values in the returned range
  96. /// satisfy the given predicate with all values contained within Other.
  97. /// Formally, this returns a subset of
  98. /// 'intersection over all y in Other . { x : icmp op x y is true }'. If the
  99. /// exact answer is not representable as a ConstantRange, the return value
  100. /// will be a proper subset of the above.
  101. ///
  102. /// Example: Pred = ult and Other = i8 [2, 5) returns [0, 2)
  103. static ConstantRange makeSatisfyingICmpRegion(CmpInst::Predicate Pred,
  104. const ConstantRange &Other);
  105. /// Produce the exact range such that all values in the returned range satisfy
  106. /// the given predicate with any value contained within Other. Formally, this
  107. /// returns the exact answer when the superset of 'union over all y in Other
  108. /// is exactly same as the subset of intersection over all y in Other.
  109. /// { x : icmp op x y is true}'.
  110. ///
  111. /// Example: Pred = ult and Other = i8 3 returns [0, 3)
  112. static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred,
  113. const APInt &Other);
  114. /// Does the predicate \p Pred hold between ranges this and \p Other?
  115. /// NOTE: false does not mean that inverse predicate holds!
  116. bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const;
  117. /// Return true iff CR1 ult CR2 is equivalent to CR1 slt CR2.
  118. /// Does not depend on strictness/direction of the predicate.
  119. static bool
  120. areInsensitiveToSignednessOfICmpPredicate(const ConstantRange &CR1,
  121. const ConstantRange &CR2);
  122. /// Return true iff CR1 ult CR2 is equivalent to CR1 sge CR2.
  123. /// Does not depend on strictness/direction of the predicate.
  124. static bool
  125. areInsensitiveToSignednessOfInvertedICmpPredicate(const ConstantRange &CR1,
  126. const ConstantRange &CR2);
  127. /// If the comparison between constant ranges this and Other
  128. /// is insensitive to the signedness of the comparison predicate,
  129. /// return a predicate equivalent to \p Pred, with flipped signedness
  130. /// (i.e. unsigned instead of signed or vice versa), and maybe inverted,
  131. /// otherwise returns CmpInst::Predicate::BAD_ICMP_PREDICATE.
  132. static CmpInst::Predicate
  133. getEquivalentPredWithFlippedSignedness(CmpInst::Predicate Pred,
  134. const ConstantRange &CR1,
  135. const ConstantRange &CR2);
  136. /// Produce the largest range containing all X such that "X BinOp Y" is
  137. /// guaranteed not to wrap (overflow) for *all* Y in Other. However, there may
  138. /// be *some* Y in Other for which additional X not contained in the result
  139. /// also do not overflow.
  140. ///
  141. /// NoWrapKind must be one of OBO::NoUnsignedWrap or OBO::NoSignedWrap.
  142. ///
  143. /// Examples:
  144. /// typedef OverflowingBinaryOperator OBO;
  145. /// #define MGNR makeGuaranteedNoWrapRegion
  146. /// MGNR(Add, [i8 1, 2), OBO::NoSignedWrap) == [-128, 127)
  147. /// MGNR(Add, [i8 1, 2), OBO::NoUnsignedWrap) == [0, -1)
  148. /// MGNR(Add, [i8 0, 1), OBO::NoUnsignedWrap) == Full Set
  149. /// MGNR(Add, [i8 -1, 6), OBO::NoSignedWrap) == [INT_MIN+1, INT_MAX-4)
  150. /// MGNR(Sub, [i8 1, 2), OBO::NoSignedWrap) == [-127, 128)
  151. /// MGNR(Sub, [i8 1, 2), OBO::NoUnsignedWrap) == [1, 0)
  152. static ConstantRange makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp,
  153. const ConstantRange &Other,
  154. unsigned NoWrapKind);
  155. /// Produce the range that contains X if and only if "X BinOp Other" does
  156. /// not wrap.
  157. static ConstantRange makeExactNoWrapRegion(Instruction::BinaryOps BinOp,
  158. const APInt &Other,
  159. unsigned NoWrapKind);
  160. /// Returns true if ConstantRange calculations are supported for intrinsic
  161. /// with \p IntrinsicID.
  162. static bool isIntrinsicSupported(Intrinsic::ID IntrinsicID);
  163. /// Compute range of intrinsic result for the given operand ranges.
  164. static ConstantRange intrinsic(Intrinsic::ID IntrinsicID,
  165. ArrayRef<ConstantRange> Ops);
  166. /// Set up \p Pred and \p RHS such that
  167. /// ConstantRange::makeExactICmpRegion(Pred, RHS) == *this. Return true if
  168. /// successful.
  169. bool getEquivalentICmp(CmpInst::Predicate &Pred, APInt &RHS) const;
  170. /// Set up \p Pred, \p RHS and \p Offset such that (V + Offset) Pred RHS
  171. /// is true iff V is in the range. Prefers using Offset == 0 if possible.
  172. void
  173. getEquivalentICmp(CmpInst::Predicate &Pred, APInt &RHS, APInt &Offset) const;
  174. /// Return the lower value for this range.
  175. const APInt &getLower() const { return Lower; }
  176. /// Return the upper value for this range.
  177. const APInt &getUpper() const { return Upper; }
  178. /// Get the bit width of this ConstantRange.
  179. uint32_t getBitWidth() const { return Lower.getBitWidth(); }
  180. /// Return true if this set contains all of the elements possible
  181. /// for this data-type.
  182. bool isFullSet() const;
  183. /// Return true if this set contains no members.
  184. bool isEmptySet() const;
  185. /// Return true if this set wraps around the unsigned domain. Special cases:
  186. /// * Empty set: Not wrapped.
  187. /// * Full set: Not wrapped.
  188. /// * [X, 0) == [X, Max]: Not wrapped.
  189. bool isWrappedSet() const;
  190. /// Return true if the exclusive upper bound wraps around the unsigned
  191. /// domain. Special cases:
  192. /// * Empty set: Not wrapped.
  193. /// * Full set: Not wrapped.
  194. /// * [X, 0): Wrapped.
  195. bool isUpperWrapped() const;
  196. /// Return true if this set wraps around the signed domain. Special cases:
  197. /// * Empty set: Not wrapped.
  198. /// * Full set: Not wrapped.
  199. /// * [X, SignedMin) == [X, SignedMax]: Not wrapped.
  200. bool isSignWrappedSet() const;
  201. /// Return true if the (exclusive) upper bound wraps around the signed
  202. /// domain. Special cases:
  203. /// * Empty set: Not wrapped.
  204. /// * Full set: Not wrapped.
  205. /// * [X, SignedMin): Wrapped.
  206. bool isUpperSignWrapped() const;
  207. /// Return true if the specified value is in the set.
  208. bool contains(const APInt &Val) const;
  209. /// Return true if the other range is a subset of this one.
  210. bool contains(const ConstantRange &CR) const;
  211. /// If this set contains a single element, return it, otherwise return null.
  212. const APInt *getSingleElement() const {
  213. if (Upper == Lower + 1)
  214. return &Lower;
  215. return nullptr;
  216. }
  217. /// If this set contains all but a single element, return it, otherwise return
  218. /// null.
  219. const APInt *getSingleMissingElement() const {
  220. if (Lower == Upper + 1)
  221. return &Upper;
  222. return nullptr;
  223. }
  224. /// Return true if this set contains exactly one member.
  225. bool isSingleElement() const { return getSingleElement() != nullptr; }
  226. /// Compare set size of this range with the range CR.
  227. bool isSizeStrictlySmallerThan(const ConstantRange &CR) const;
  228. /// Compare set size of this range with Value.
  229. bool isSizeLargerThan(uint64_t MaxSize) const;
  230. /// Return true if all values in this range are negative.
  231. bool isAllNegative() const;
  232. /// Return true if all values in this range are non-negative.
  233. bool isAllNonNegative() const;
  234. /// Return the largest unsigned value contained in the ConstantRange.
  235. APInt getUnsignedMax() const;
  236. /// Return the smallest unsigned value contained in the ConstantRange.
  237. APInt getUnsignedMin() const;
  238. /// Return the largest signed value contained in the ConstantRange.
  239. APInt getSignedMax() const;
  240. /// Return the smallest signed value contained in the ConstantRange.
  241. APInt getSignedMin() const;
  242. /// Return true if this range is equal to another range.
  243. bool operator==(const ConstantRange &CR) const {
  244. return Lower == CR.Lower && Upper == CR.Upper;
  245. }
  246. bool operator!=(const ConstantRange &CR) const {
  247. return !operator==(CR);
  248. }
  249. /// Compute the maximal number of active bits needed to represent every value
  250. /// in this range.
  251. unsigned getActiveBits() const;
  252. /// Compute the maximal number of bits needed to represent every value
  253. /// in this signed range.
  254. unsigned getMinSignedBits() const;
  255. /// Subtract the specified constant from the endpoints of this constant range.
  256. ConstantRange subtract(const APInt &CI) const;
  257. /// Subtract the specified range from this range (aka relative complement of
  258. /// the sets).
  259. ConstantRange difference(const ConstantRange &CR) const;
  260. /// If represented precisely, the result of some range operations may consist
  261. /// of multiple disjoint ranges. As only a single range may be returned, any
  262. /// range covering these disjoint ranges constitutes a valid result, but some
  263. /// may be more useful than others depending on context. The preferred range
  264. /// type specifies whether a range that is non-wrapping in the unsigned or
  265. /// signed domain, or has the smallest size, is preferred. If a signedness is
  266. /// preferred but all ranges are non-wrapping or all wrapping, then the
  267. /// smallest set size is preferred. If there are multiple smallest sets, any
  268. /// one of them may be returned.
  269. enum PreferredRangeType { Smallest, Unsigned, Signed };
  270. /// Return the range that results from the intersection of this range with
  271. /// another range. If the intersection is disjoint, such that two results
  272. /// are possible, the preferred range is determined by the PreferredRangeType.
  273. ConstantRange intersectWith(const ConstantRange &CR,
  274. PreferredRangeType Type = Smallest) const;
  275. /// Return the range that results from the union of this range
  276. /// with another range. The resultant range is guaranteed to include the
  277. /// elements of both sets, but may contain more. For example, [3, 9) union
  278. /// [12,15) is [3, 15), which includes 9, 10, and 11, which were not included
  279. /// in either set before.
  280. ConstantRange unionWith(const ConstantRange &CR,
  281. PreferredRangeType Type = Smallest) const;
  282. /// Intersect the two ranges and return the result if it can be represented
  283. /// exactly, otherwise return std::nullopt.
  284. std::optional<ConstantRange>
  285. exactIntersectWith(const ConstantRange &CR) const;
  286. /// Union the two ranges and return the result if it can be represented
  287. /// exactly, otherwise return std::nullopt.
  288. std::optional<ConstantRange> exactUnionWith(const ConstantRange &CR) const;
  289. /// Return a new range representing the possible values resulting
  290. /// from an application of the specified cast operator to this range. \p
  291. /// BitWidth is the target bitwidth of the cast. For casts which don't
  292. /// change bitwidth, it must be the same as the source bitwidth. For casts
  293. /// which do change bitwidth, the bitwidth must be consistent with the
  294. /// requested cast and source bitwidth.
  295. ConstantRange castOp(Instruction::CastOps CastOp,
  296. uint32_t BitWidth) const;
  297. /// Return a new range in the specified integer type, which must
  298. /// be strictly larger than the current type. The returned range will
  299. /// correspond to the possible range of values if the source range had been
  300. /// zero extended to BitWidth.
  301. ConstantRange zeroExtend(uint32_t BitWidth) const;
  302. /// Return a new range in the specified integer type, which must
  303. /// be strictly larger than the current type. The returned range will
  304. /// correspond to the possible range of values if the source range had been
  305. /// sign extended to BitWidth.
  306. ConstantRange signExtend(uint32_t BitWidth) const;
  307. /// Return a new range in the specified integer type, which must be
  308. /// strictly smaller than the current type. The returned range will
  309. /// correspond to the possible range of values if the source range had been
  310. /// truncated to the specified type.
  311. ConstantRange truncate(uint32_t BitWidth) const;
  312. /// Make this range have the bit width given by \p BitWidth. The
  313. /// value is zero extended, truncated, or left alone to make it that width.
  314. ConstantRange zextOrTrunc(uint32_t BitWidth) const;
  315. /// Make this range have the bit width given by \p BitWidth. The
  316. /// value is sign extended, truncated, or left alone to make it that width.
  317. ConstantRange sextOrTrunc(uint32_t BitWidth) const;
  318. /// Return a new range representing the possible values resulting
  319. /// from an application of the specified binary operator to an left hand side
  320. /// of this range and a right hand side of \p Other.
  321. ConstantRange binaryOp(Instruction::BinaryOps BinOp,
  322. const ConstantRange &Other) const;
  323. /// Return a new range representing the possible values resulting
  324. /// from an application of the specified overflowing binary operator to a
  325. /// left hand side of this range and a right hand side of \p Other given
  326. /// the provided knowledge about lack of wrapping \p NoWrapKind.
  327. ConstantRange overflowingBinaryOp(Instruction::BinaryOps BinOp,
  328. const ConstantRange &Other,
  329. unsigned NoWrapKind) const;
  330. /// Return a new range representing the possible values resulting
  331. /// from an addition of a value in this range and a value in \p Other.
  332. ConstantRange add(const ConstantRange &Other) const;
  333. /// Return a new range representing the possible values resulting
  334. /// from an addition with wrap type \p NoWrapKind of a value in this
  335. /// range and a value in \p Other.
  336. /// If the result range is disjoint, the preferred range is determined by the
  337. /// \p PreferredRangeType.
  338. ConstantRange addWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind,
  339. PreferredRangeType RangeType = Smallest) const;
  340. /// Return a new range representing the possible values resulting
  341. /// from a subtraction of a value in this range and a value in \p Other.
  342. ConstantRange sub(const ConstantRange &Other) const;
  343. /// Return a new range representing the possible values resulting
  344. /// from an subtraction with wrap type \p NoWrapKind of a value in this
  345. /// range and a value in \p Other.
  346. /// If the result range is disjoint, the preferred range is determined by the
  347. /// \p PreferredRangeType.
  348. ConstantRange subWithNoWrap(const ConstantRange &Other, unsigned NoWrapKind,
  349. PreferredRangeType RangeType = Smallest) const;
  350. /// Return a new range representing the possible values resulting
  351. /// from a multiplication of a value in this range and a value in \p Other,
  352. /// treating both this and \p Other as unsigned ranges.
  353. ConstantRange multiply(const ConstantRange &Other) const;
  354. /// Return range of possible values for a signed multiplication of this and
  355. /// \p Other. However, if overflow is possible always return a full range
  356. /// rather than trying to determine a more precise result.
  357. ConstantRange smul_fast(const ConstantRange &Other) const;
  358. /// Return a new range representing the possible values resulting
  359. /// from a signed maximum of a value in this range and a value in \p Other.
  360. ConstantRange smax(const ConstantRange &Other) const;
  361. /// Return a new range representing the possible values resulting
  362. /// from an unsigned maximum of a value in this range and a value in \p Other.
  363. ConstantRange umax(const ConstantRange &Other) const;
  364. /// Return a new range representing the possible values resulting
  365. /// from a signed minimum of a value in this range and a value in \p Other.
  366. ConstantRange smin(const ConstantRange &Other) const;
  367. /// Return a new range representing the possible values resulting
  368. /// from an unsigned minimum of a value in this range and a value in \p Other.
  369. ConstantRange umin(const ConstantRange &Other) const;
  370. /// Return a new range representing the possible values resulting
  371. /// from an unsigned division of a value in this range and a value in
  372. /// \p Other.
  373. ConstantRange udiv(const ConstantRange &Other) const;
  374. /// Return a new range representing the possible values resulting
  375. /// from a signed division of a value in this range and a value in
  376. /// \p Other. Division by zero and division of SignedMin by -1 are considered
  377. /// undefined behavior, in line with IR, and do not contribute towards the
  378. /// result.
  379. ConstantRange sdiv(const ConstantRange &Other) const;
  380. /// Return a new range representing the possible values resulting
  381. /// from an unsigned remainder operation of a value in this range and a
  382. /// value in \p Other.
  383. ConstantRange urem(const ConstantRange &Other) const;
  384. /// Return a new range representing the possible values resulting
  385. /// from a signed remainder operation of a value in this range and a
  386. /// value in \p Other.
  387. ConstantRange srem(const ConstantRange &Other) const;
  388. /// Return a new range representing the possible values resulting from
  389. /// a binary-xor of a value in this range by an all-one value,
  390. /// aka bitwise complement operation.
  391. ConstantRange binaryNot() const;
  392. /// Return a new range representing the possible values resulting
  393. /// from a binary-and of a value in this range by a value in \p Other.
  394. ConstantRange binaryAnd(const ConstantRange &Other) const;
  395. /// Return a new range representing the possible values resulting
  396. /// from a binary-or of a value in this range by a value in \p Other.
  397. ConstantRange binaryOr(const ConstantRange &Other) const;
  398. /// Return a new range representing the possible values resulting
  399. /// from a binary-xor of a value in this range by a value in \p Other.
  400. ConstantRange binaryXor(const ConstantRange &Other) const;
  401. /// Return a new range representing the possible values resulting
  402. /// from a left shift of a value in this range by a value in \p Other.
  403. /// TODO: This isn't fully implemented yet.
  404. ConstantRange shl(const ConstantRange &Other) const;
  405. /// Return a new range representing the possible values resulting from a
  406. /// logical right shift of a value in this range and a value in \p Other.
  407. ConstantRange lshr(const ConstantRange &Other) const;
  408. /// Return a new range representing the possible values resulting from a
  409. /// arithmetic right shift of a value in this range and a value in \p Other.
  410. ConstantRange ashr(const ConstantRange &Other) const;
  411. /// Perform an unsigned saturating addition of two constant ranges.
  412. ConstantRange uadd_sat(const ConstantRange &Other) const;
  413. /// Perform a signed saturating addition of two constant ranges.
  414. ConstantRange sadd_sat(const ConstantRange &Other) const;
  415. /// Perform an unsigned saturating subtraction of two constant ranges.
  416. ConstantRange usub_sat(const ConstantRange &Other) const;
  417. /// Perform a signed saturating subtraction of two constant ranges.
  418. ConstantRange ssub_sat(const ConstantRange &Other) const;
  419. /// Perform an unsigned saturating multiplication of two constant ranges.
  420. ConstantRange umul_sat(const ConstantRange &Other) const;
  421. /// Perform a signed saturating multiplication of two constant ranges.
  422. ConstantRange smul_sat(const ConstantRange &Other) const;
  423. /// Perform an unsigned saturating left shift of this constant range by a
  424. /// value in \p Other.
  425. ConstantRange ushl_sat(const ConstantRange &Other) const;
  426. /// Perform a signed saturating left shift of this constant range by a
  427. /// value in \p Other.
  428. ConstantRange sshl_sat(const ConstantRange &Other) const;
  429. /// Return a new range that is the logical not of the current set.
  430. ConstantRange inverse() const;
  431. /// Calculate absolute value range. If the original range contains signed
  432. /// min, then the resulting range will contain signed min if and only if
  433. /// \p IntMinIsPoison is false.
  434. ConstantRange abs(bool IntMinIsPoison = false) const;
  435. /// Represents whether an operation on the given constant range is known to
  436. /// always or never overflow.
  437. enum class OverflowResult {
  438. /// Always overflows in the direction of signed/unsigned min value.
  439. AlwaysOverflowsLow,
  440. /// Always overflows in the direction of signed/unsigned max value.
  441. AlwaysOverflowsHigh,
  442. /// May or may not overflow.
  443. MayOverflow,
  444. /// Never overflows.
  445. NeverOverflows,
  446. };
  447. /// Return whether unsigned add of the two ranges always/never overflows.
  448. OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const;
  449. /// Return whether signed add of the two ranges always/never overflows.
  450. OverflowResult signedAddMayOverflow(const ConstantRange &Other) const;
  451. /// Return whether unsigned sub of the two ranges always/never overflows.
  452. OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const;
  453. /// Return whether signed sub of the two ranges always/never overflows.
  454. OverflowResult signedSubMayOverflow(const ConstantRange &Other) const;
  455. /// Return whether unsigned mul of the two ranges always/never overflows.
  456. OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const;
  457. /// Return known bits for values in this range.
  458. KnownBits toKnownBits() const;
  459. /// Print out the bounds to a stream.
  460. void print(raw_ostream &OS) const;
  461. /// Allow printing from a debugger easily.
  462. void dump() const;
  463. };
  464. inline raw_ostream &operator<<(raw_ostream &OS, const ConstantRange &CR) {
  465. CR.print(OS);
  466. return OS;
  467. }
  468. /// Parse out a conservative ConstantRange from !range metadata.
  469. ///
  470. /// E.g. if RangeMD is !{i32 0, i32 10, i32 15, i32 20} then return [0, 20).
  471. ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD);
  472. } // end namespace llvm
  473. #endif // LLVM_IR_CONSTANTRANGE_H
  474. #ifdef __GNUC__
  475. #pragma GCC diagnostic pop
  476. #endif