ConstantRange.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. //===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
  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 (other integral ranges use min/max values for special range values):
  16. //
  17. // [F, F) = {} = Empty set
  18. // [T, F) = {T}
  19. // [F, T) = {F}
  20. // [T, T) = {F, T} = Full set
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #include "llvm/IR/InstrTypes.h"
  24. #include "llvm/IR/ConstantRange.h"
  25. #include "llvm/Support/Debug.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. using namespace llvm;
  28. /// Initialize a full (the default) or empty set for the specified type.
  29. ///
  30. ConstantRange::ConstantRange(uint32_t BitWidth, bool Full) {
  31. if (Full)
  32. Lower = Upper = APInt::getMaxValue(BitWidth);
  33. else
  34. Lower = Upper = APInt::getMinValue(BitWidth);
  35. }
  36. /// Initialize a range to hold the single specified value.
  37. ///
  38. ConstantRange::ConstantRange(APIntMoveTy V)
  39. : Lower(std::move(V)), Upper(Lower + 1) {}
  40. ConstantRange::ConstantRange(APIntMoveTy L, APIntMoveTy U)
  41. : Lower(std::move(L)), Upper(std::move(U)) {
  42. assert(Lower.getBitWidth() == Upper.getBitWidth() &&
  43. "ConstantRange with unequal bit widths");
  44. assert((Lower != Upper || (Lower.isMaxValue() || Lower.isMinValue())) &&
  45. "Lower == Upper, but they aren't min or max value!");
  46. }
  47. ConstantRange ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred,
  48. const ConstantRange &CR) {
  49. if (CR.isEmptySet())
  50. return CR;
  51. uint32_t W = CR.getBitWidth();
  52. switch (Pred) {
  53. default:
  54. llvm_unreachable("Invalid ICmp predicate to makeAllowedICmpRegion()");
  55. case CmpInst::ICMP_EQ:
  56. return CR;
  57. case CmpInst::ICMP_NE:
  58. if (CR.isSingleElement())
  59. return ConstantRange(CR.getUpper(), CR.getLower());
  60. return ConstantRange(W);
  61. case CmpInst::ICMP_ULT: {
  62. APInt UMax(CR.getUnsignedMax());
  63. if (UMax.isMinValue())
  64. return ConstantRange(W, /* empty */ false);
  65. return ConstantRange(APInt::getMinValue(W), UMax);
  66. }
  67. case CmpInst::ICMP_SLT: {
  68. APInt SMax(CR.getSignedMax());
  69. if (SMax.isMinSignedValue())
  70. return ConstantRange(W, /* empty */ false);
  71. return ConstantRange(APInt::getSignedMinValue(W), SMax);
  72. }
  73. case CmpInst::ICMP_ULE: {
  74. APInt UMax(CR.getUnsignedMax());
  75. if (UMax.isMaxValue())
  76. return ConstantRange(W);
  77. return ConstantRange(APInt::getMinValue(W), UMax + 1);
  78. }
  79. case CmpInst::ICMP_SLE: {
  80. APInt SMax(CR.getSignedMax());
  81. if (SMax.isMaxSignedValue())
  82. return ConstantRange(W);
  83. return ConstantRange(APInt::getSignedMinValue(W), SMax + 1);
  84. }
  85. case CmpInst::ICMP_UGT: {
  86. APInt UMin(CR.getUnsignedMin());
  87. if (UMin.isMaxValue())
  88. return ConstantRange(W, /* empty */ false);
  89. return ConstantRange(UMin + 1, APInt::getNullValue(W));
  90. }
  91. case CmpInst::ICMP_SGT: {
  92. APInt SMin(CR.getSignedMin());
  93. if (SMin.isMaxSignedValue())
  94. return ConstantRange(W, /* empty */ false);
  95. return ConstantRange(SMin + 1, APInt::getSignedMinValue(W));
  96. }
  97. case CmpInst::ICMP_UGE: {
  98. APInt UMin(CR.getUnsignedMin());
  99. if (UMin.isMinValue())
  100. return ConstantRange(W);
  101. return ConstantRange(UMin, APInt::getNullValue(W));
  102. }
  103. case CmpInst::ICMP_SGE: {
  104. APInt SMin(CR.getSignedMin());
  105. if (SMin.isMinSignedValue())
  106. return ConstantRange(W);
  107. return ConstantRange(SMin, APInt::getSignedMinValue(W));
  108. }
  109. }
  110. }
  111. ConstantRange ConstantRange::makeSatisfyingICmpRegion(CmpInst::Predicate Pred,
  112. const ConstantRange &CR) {
  113. // Follows from De-Morgan's laws:
  114. //
  115. // ~(~A union ~B) == A intersect B.
  116. //
  117. return makeAllowedICmpRegion(CmpInst::getInversePredicate(Pred), CR)
  118. .inverse();
  119. }
  120. /// isFullSet - Return true if this set contains all of the elements possible
  121. /// for this data-type
  122. bool ConstantRange::isFullSet() const {
  123. return Lower == Upper && Lower.isMaxValue();
  124. }
  125. /// isEmptySet - Return true if this set contains no members.
  126. ///
  127. bool ConstantRange::isEmptySet() const {
  128. return Lower == Upper && Lower.isMinValue();
  129. }
  130. /// isWrappedSet - Return true if this set wraps around the top of the range,
  131. /// for example: [100, 8)
  132. ///
  133. bool ConstantRange::isWrappedSet() const {
  134. return Lower.ugt(Upper);
  135. }
  136. /// isSignWrappedSet - Return true if this set wraps around the INT_MIN of
  137. /// its bitwidth, for example: i8 [120, 140).
  138. ///
  139. bool ConstantRange::isSignWrappedSet() const {
  140. return contains(APInt::getSignedMaxValue(getBitWidth())) &&
  141. contains(APInt::getSignedMinValue(getBitWidth()));
  142. }
  143. /// getSetSize - Return the number of elements in this set.
  144. ///
  145. APInt ConstantRange::getSetSize() const {
  146. if (isFullSet()) {
  147. APInt Size(getBitWidth()+1, 0);
  148. Size.setBit(getBitWidth());
  149. return Size;
  150. }
  151. // This is also correct for wrapped sets.
  152. return (Upper - Lower).zext(getBitWidth()+1);
  153. }
  154. /// getUnsignedMax - Return the largest unsigned value contained in the
  155. /// ConstantRange.
  156. ///
  157. APInt ConstantRange::getUnsignedMax() const {
  158. if (isFullSet() || isWrappedSet())
  159. return APInt::getMaxValue(getBitWidth());
  160. return getUpper() - 1;
  161. }
  162. /// getUnsignedMin - Return the smallest unsigned value contained in the
  163. /// ConstantRange.
  164. ///
  165. APInt ConstantRange::getUnsignedMin() const {
  166. if (isFullSet() || (isWrappedSet() && getUpper() != 0))
  167. return APInt::getMinValue(getBitWidth());
  168. return getLower();
  169. }
  170. /// getSignedMax - Return the largest signed value contained in the
  171. /// ConstantRange.
  172. ///
  173. APInt ConstantRange::getSignedMax() const {
  174. APInt SignedMax(APInt::getSignedMaxValue(getBitWidth()));
  175. if (!isWrappedSet()) {
  176. if (getLower().sle(getUpper() - 1))
  177. return getUpper() - 1;
  178. return SignedMax;
  179. }
  180. if (getLower().isNegative() == getUpper().isNegative())
  181. return SignedMax;
  182. return getUpper() - 1;
  183. }
  184. /// getSignedMin - Return the smallest signed value contained in the
  185. /// ConstantRange.
  186. ///
  187. APInt ConstantRange::getSignedMin() const {
  188. APInt SignedMin(APInt::getSignedMinValue(getBitWidth()));
  189. if (!isWrappedSet()) {
  190. if (getLower().sle(getUpper() - 1))
  191. return getLower();
  192. return SignedMin;
  193. }
  194. if ((getUpper() - 1).slt(getLower())) {
  195. if (getUpper() != SignedMin)
  196. return SignedMin;
  197. }
  198. return getLower();
  199. }
  200. /// contains - Return true if the specified value is in the set.
  201. ///
  202. bool ConstantRange::contains(const APInt &V) const {
  203. if (Lower == Upper)
  204. return isFullSet();
  205. if (!isWrappedSet())
  206. return Lower.ule(V) && V.ult(Upper);
  207. return Lower.ule(V) || V.ult(Upper);
  208. }
  209. /// contains - Return true if the argument is a subset of this range.
  210. /// Two equal sets contain each other. The empty set contained by all other
  211. /// sets.
  212. ///
  213. bool ConstantRange::contains(const ConstantRange &Other) const {
  214. if (isFullSet() || Other.isEmptySet()) return true;
  215. if (isEmptySet() || Other.isFullSet()) return false;
  216. if (!isWrappedSet()) {
  217. if (Other.isWrappedSet())
  218. return false;
  219. return Lower.ule(Other.getLower()) && Other.getUpper().ule(Upper);
  220. }
  221. if (!Other.isWrappedSet())
  222. return Other.getUpper().ule(Upper) ||
  223. Lower.ule(Other.getLower());
  224. return Other.getUpper().ule(Upper) && Lower.ule(Other.getLower());
  225. }
  226. /// subtract - Subtract the specified constant from the endpoints of this
  227. /// constant range.
  228. ConstantRange ConstantRange::subtract(const APInt &Val) const {
  229. assert(Val.getBitWidth() == getBitWidth() && "Wrong bit width");
  230. // If the set is empty or full, don't modify the endpoints.
  231. if (Lower == Upper)
  232. return *this;
  233. return ConstantRange(Lower - Val, Upper - Val);
  234. }
  235. /// \brief Subtract the specified range from this range (aka relative complement
  236. /// of the sets).
  237. ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
  238. return intersectWith(CR.inverse());
  239. }
  240. /// intersectWith - Return the range that results from the intersection of this
  241. /// range with another range. The resultant range is guaranteed to include all
  242. /// elements contained in both input ranges, and to have the smallest possible
  243. /// set size that does so. Because there may be two intersections with the
  244. /// same set size, A.intersectWith(B) might not be equal to B.intersectWith(A).
  245. ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
  246. assert(getBitWidth() == CR.getBitWidth() &&
  247. "ConstantRange types don't agree!");
  248. // Handle common cases.
  249. if ( isEmptySet() || CR.isFullSet()) return *this;
  250. if (CR.isEmptySet() || isFullSet()) return CR;
  251. if (!isWrappedSet() && CR.isWrappedSet())
  252. return CR.intersectWith(*this);
  253. if (!isWrappedSet() && !CR.isWrappedSet()) {
  254. if (Lower.ult(CR.Lower)) {
  255. if (Upper.ule(CR.Lower))
  256. return ConstantRange(getBitWidth(), false);
  257. if (Upper.ult(CR.Upper))
  258. return ConstantRange(CR.Lower, Upper);
  259. return CR;
  260. }
  261. if (Upper.ult(CR.Upper))
  262. return *this;
  263. if (Lower.ult(CR.Upper))
  264. return ConstantRange(Lower, CR.Upper);
  265. return ConstantRange(getBitWidth(), false);
  266. }
  267. if (isWrappedSet() && !CR.isWrappedSet()) {
  268. if (CR.Lower.ult(Upper)) {
  269. if (CR.Upper.ult(Upper))
  270. return CR;
  271. if (CR.Upper.ule(Lower))
  272. return ConstantRange(CR.Lower, Upper);
  273. if (getSetSize().ult(CR.getSetSize()))
  274. return *this;
  275. return CR;
  276. }
  277. if (CR.Lower.ult(Lower)) {
  278. if (CR.Upper.ule(Lower))
  279. return ConstantRange(getBitWidth(), false);
  280. return ConstantRange(Lower, CR.Upper);
  281. }
  282. return CR;
  283. }
  284. if (CR.Upper.ult(Upper)) {
  285. if (CR.Lower.ult(Upper)) {
  286. if (getSetSize().ult(CR.getSetSize()))
  287. return *this;
  288. return CR;
  289. }
  290. if (CR.Lower.ult(Lower))
  291. return ConstantRange(Lower, CR.Upper);
  292. return CR;
  293. }
  294. if (CR.Upper.ule(Lower)) {
  295. if (CR.Lower.ult(Lower))
  296. return *this;
  297. return ConstantRange(CR.Lower, Upper);
  298. }
  299. if (getSetSize().ult(CR.getSetSize()))
  300. return *this;
  301. return CR;
  302. }
  303. /// unionWith - Return the range that results from the union of this range with
  304. /// another range. The resultant range is guaranteed to include the elements of
  305. /// both sets, but may contain more. For example, [3, 9) union [12,15) is
  306. /// [3, 15), which includes 9, 10, and 11, which were not included in either
  307. /// set before.
  308. ///
  309. ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
  310. assert(getBitWidth() == CR.getBitWidth() &&
  311. "ConstantRange types don't agree!");
  312. if ( isFullSet() || CR.isEmptySet()) return *this;
  313. if (CR.isFullSet() || isEmptySet()) return CR;
  314. if (!isWrappedSet() && CR.isWrappedSet()) return CR.unionWith(*this);
  315. if (!isWrappedSet() && !CR.isWrappedSet()) {
  316. if (CR.Upper.ult(Lower) || Upper.ult(CR.Lower)) {
  317. // If the two ranges are disjoint, find the smaller gap and bridge it.
  318. APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
  319. if (d1.ult(d2))
  320. return ConstantRange(Lower, CR.Upper);
  321. return ConstantRange(CR.Lower, Upper);
  322. }
  323. APInt L = Lower, U = Upper;
  324. if (CR.Lower.ult(L))
  325. L = CR.Lower;
  326. if ((CR.Upper - 1).ugt(U - 1))
  327. U = CR.Upper;
  328. if (L == 0 && U == 0)
  329. return ConstantRange(getBitWidth());
  330. return ConstantRange(L, U);
  331. }
  332. if (!CR.isWrappedSet()) {
  333. // ------U L----- and ------U L----- : this
  334. // L--U L--U : CR
  335. if (CR.Upper.ule(Upper) || CR.Lower.uge(Lower))
  336. return *this;
  337. // ------U L----- : this
  338. // L---------U : CR
  339. if (CR.Lower.ule(Upper) && Lower.ule(CR.Upper))
  340. return ConstantRange(getBitWidth());
  341. // ----U L---- : this
  342. // L---U : CR
  343. // <d1> <d2>
  344. if (Upper.ule(CR.Lower) && CR.Upper.ule(Lower)) {
  345. APInt d1 = CR.Lower - Upper, d2 = Lower - CR.Upper;
  346. if (d1.ult(d2))
  347. return ConstantRange(Lower, CR.Upper);
  348. return ConstantRange(CR.Lower, Upper);
  349. }
  350. // ----U L----- : this
  351. // L----U : CR
  352. if (Upper.ult(CR.Lower) && Lower.ult(CR.Upper))
  353. return ConstantRange(CR.Lower, Upper);
  354. // ------U L---- : this
  355. // L-----U : CR
  356. assert(CR.Lower.ult(Upper) && CR.Upper.ult(Lower) &&
  357. "ConstantRange::unionWith missed a case with one range wrapped");
  358. return ConstantRange(Lower, CR.Upper);
  359. }
  360. // ------U L---- and ------U L---- : this
  361. // -U L----------- and ------------U L : CR
  362. if (CR.Lower.ule(Upper) || Lower.ule(CR.Upper))
  363. return ConstantRange(getBitWidth());
  364. APInt L = Lower, U = Upper;
  365. if (CR.Upper.ugt(U))
  366. U = CR.Upper;
  367. if (CR.Lower.ult(L))
  368. L = CR.Lower;
  369. return ConstantRange(L, U);
  370. }
  371. /// zeroExtend - Return a new range in the specified integer type, which must
  372. /// be strictly larger than the current type. The returned range will
  373. /// correspond to the possible range of values as if the source range had been
  374. /// zero extended.
  375. ConstantRange ConstantRange::zeroExtend(uint32_t DstTySize) const {
  376. if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
  377. unsigned SrcTySize = getBitWidth();
  378. assert(SrcTySize < DstTySize && "Not a value extension");
  379. if (isFullSet() || isWrappedSet()) {
  380. // Change into [0, 1 << src bit width)
  381. APInt LowerExt(DstTySize, 0);
  382. if (!Upper) // special case: [X, 0) -- not really wrapping around
  383. LowerExt = Lower.zext(DstTySize);
  384. return ConstantRange(LowerExt, APInt::getOneBitSet(DstTySize, SrcTySize));
  385. }
  386. return ConstantRange(Lower.zext(DstTySize), Upper.zext(DstTySize));
  387. }
  388. /// signExtend - Return a new range in the specified integer type, which must
  389. /// be strictly larger than the current type. The returned range will
  390. /// correspond to the possible range of values as if the source range had been
  391. /// sign extended.
  392. ConstantRange ConstantRange::signExtend(uint32_t DstTySize) const {
  393. if (isEmptySet()) return ConstantRange(DstTySize, /*isFullSet=*/false);
  394. unsigned SrcTySize = getBitWidth();
  395. assert(SrcTySize < DstTySize && "Not a value extension");
  396. // special case: [X, INT_MIN) -- not really wrapping around
  397. if (Upper.isMinSignedValue())
  398. return ConstantRange(Lower.sext(DstTySize), Upper.zext(DstTySize));
  399. if (isFullSet() || isSignWrappedSet()) {
  400. return ConstantRange(APInt::getHighBitsSet(DstTySize,DstTySize-SrcTySize+1),
  401. APInt::getLowBitsSet(DstTySize, SrcTySize-1) + 1);
  402. }
  403. return ConstantRange(Lower.sext(DstTySize), Upper.sext(DstTySize));
  404. }
  405. /// truncate - Return a new range in the specified integer type, which must be
  406. /// strictly smaller than the current type. The returned range will
  407. /// correspond to the possible range of values as if the source range had been
  408. /// truncated to the specified type.
  409. ConstantRange ConstantRange::truncate(uint32_t DstTySize) const {
  410. assert(getBitWidth() > DstTySize && "Not a value truncation");
  411. if (isEmptySet())
  412. return ConstantRange(DstTySize, /*isFullSet=*/false);
  413. if (isFullSet())
  414. return ConstantRange(DstTySize, /*isFullSet=*/true);
  415. APInt MaxValue = APInt::getMaxValue(DstTySize).zext(getBitWidth());
  416. APInt MaxBitValue(getBitWidth(), 0);
  417. MaxBitValue.setBit(DstTySize);
  418. APInt LowerDiv(Lower), UpperDiv(Upper);
  419. ConstantRange Union(DstTySize, /*isFullSet=*/false);
  420. // Analyze wrapped sets in their two parts: [0, Upper) \/ [Lower, MaxValue]
  421. // We use the non-wrapped set code to analyze the [Lower, MaxValue) part, and
  422. // then we do the union with [MaxValue, Upper)
  423. if (isWrappedSet()) {
  424. // if Upper is greater than Max Value, it covers the whole truncated range.
  425. if (Upper.uge(MaxValue))
  426. return ConstantRange(DstTySize, /*isFullSet=*/true);
  427. Union = ConstantRange(APInt::getMaxValue(DstTySize),Upper.trunc(DstTySize));
  428. UpperDiv = APInt::getMaxValue(getBitWidth());
  429. // Union covers the MaxValue case, so return if the remaining range is just
  430. // MaxValue.
  431. if (LowerDiv == UpperDiv)
  432. return Union;
  433. }
  434. // Chop off the most significant bits that are past the destination bitwidth.
  435. if (LowerDiv.uge(MaxValue)) {
  436. APInt Div(getBitWidth(), 0);
  437. APInt::udivrem(LowerDiv, MaxBitValue, Div, LowerDiv);
  438. UpperDiv = UpperDiv - MaxBitValue * Div;
  439. }
  440. if (UpperDiv.ule(MaxValue))
  441. return ConstantRange(LowerDiv.trunc(DstTySize),
  442. UpperDiv.trunc(DstTySize)).unionWith(Union);
  443. // The truncated value wrapps around. Check if we can do better than fullset.
  444. APInt UpperModulo = UpperDiv - MaxBitValue;
  445. if (UpperModulo.ult(LowerDiv))
  446. return ConstantRange(LowerDiv.trunc(DstTySize),
  447. UpperModulo.trunc(DstTySize)).unionWith(Union);
  448. return ConstantRange(DstTySize, /*isFullSet=*/true);
  449. }
  450. /// zextOrTrunc - make this range have the bit width given by \p DstTySize. The
  451. /// value is zero extended, truncated, or left alone to make it that width.
  452. ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const {
  453. unsigned SrcTySize = getBitWidth();
  454. if (SrcTySize > DstTySize)
  455. return truncate(DstTySize);
  456. if (SrcTySize < DstTySize)
  457. return zeroExtend(DstTySize);
  458. return *this;
  459. }
  460. /// sextOrTrunc - make this range have the bit width given by \p DstTySize. The
  461. /// value is sign extended, truncated, or left alone to make it that width.
  462. ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const {
  463. unsigned SrcTySize = getBitWidth();
  464. if (SrcTySize > DstTySize)
  465. return truncate(DstTySize);
  466. if (SrcTySize < DstTySize)
  467. return signExtend(DstTySize);
  468. return *this;
  469. }
  470. ConstantRange
  471. ConstantRange::add(const ConstantRange &Other) const {
  472. if (isEmptySet() || Other.isEmptySet())
  473. return ConstantRange(getBitWidth(), /*isFullSet=*/false);
  474. if (isFullSet() || Other.isFullSet())
  475. return ConstantRange(getBitWidth(), /*isFullSet=*/true);
  476. APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
  477. APInt NewLower = getLower() + Other.getLower();
  478. APInt NewUpper = getUpper() + Other.getUpper() - 1;
  479. if (NewLower == NewUpper)
  480. return ConstantRange(getBitWidth(), /*isFullSet=*/true);
  481. ConstantRange X = ConstantRange(NewLower, NewUpper);
  482. if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
  483. // We've wrapped, therefore, full set.
  484. return ConstantRange(getBitWidth(), /*isFullSet=*/true);
  485. return X;
  486. }
  487. ConstantRange
  488. ConstantRange::sub(const ConstantRange &Other) const {
  489. if (isEmptySet() || Other.isEmptySet())
  490. return ConstantRange(getBitWidth(), /*isFullSet=*/false);
  491. if (isFullSet() || Other.isFullSet())
  492. return ConstantRange(getBitWidth(), /*isFullSet=*/true);
  493. APInt Spread_X = getSetSize(), Spread_Y = Other.getSetSize();
  494. APInt NewLower = getLower() - Other.getUpper() + 1;
  495. APInt NewUpper = getUpper() - Other.getLower();
  496. if (NewLower == NewUpper)
  497. return ConstantRange(getBitWidth(), /*isFullSet=*/true);
  498. ConstantRange X = ConstantRange(NewLower, NewUpper);
  499. if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
  500. // We've wrapped, therefore, full set.
  501. return ConstantRange(getBitWidth(), /*isFullSet=*/true);
  502. return X;
  503. }
  504. ConstantRange
  505. ConstantRange::multiply(const ConstantRange &Other) const {
  506. // TODO: If either operand is a single element and the multiply is known to
  507. // be non-wrapping, round the result min and max value to the appropriate
  508. // multiple of that element. If wrapping is possible, at least adjust the
  509. // range according to the greatest power-of-two factor of the single element.
  510. if (isEmptySet() || Other.isEmptySet())
  511. return ConstantRange(getBitWidth(), /*isFullSet=*/false);
  512. // Multiplication is signedness-independent. However different ranges can be
  513. // obtained depending on how the input ranges are treated. These different
  514. // ranges are all conservatively correct, but one might be better than the
  515. // other. We calculate two ranges; one treating the inputs as unsigned
  516. // and the other signed, then return the smallest of these ranges.
  517. // Unsigned range first.
  518. APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
  519. APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
  520. APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
  521. APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
  522. ConstantRange Result_zext = ConstantRange(this_min * Other_min,
  523. this_max * Other_max + 1);
  524. ConstantRange UR = Result_zext.truncate(getBitWidth());
  525. // Now the signed range. Because we could be dealing with negative numbers
  526. // here, the lower bound is the smallest of the cartesian product of the
  527. // lower and upper ranges; for example:
  528. // [-1,4) * [-2,3) = min(-1*-2, -1*2, 3*-2, 3*2) = -6.
  529. // Similarly for the upper bound, swapping min for max.
  530. this_min = getSignedMin().sext(getBitWidth() * 2);
  531. this_max = getSignedMax().sext(getBitWidth() * 2);
  532. Other_min = Other.getSignedMin().sext(getBitWidth() * 2);
  533. Other_max = Other.getSignedMax().sext(getBitWidth() * 2);
  534. auto L = {this_min * Other_min, this_min * Other_max,
  535. this_max * Other_min, this_max * Other_max};
  536. auto Compare = [](const APInt &A, const APInt &B) { return A.slt(B); };
  537. ConstantRange Result_sext(std::min(L, Compare), std::max(L, Compare) + 1);
  538. ConstantRange SR = Result_sext.truncate(getBitWidth());
  539. return UR.getSetSize().ult(SR.getSetSize()) ? UR : SR;
  540. }
  541. ConstantRange
  542. ConstantRange::smax(const ConstantRange &Other) const {
  543. // X smax Y is: range(smax(X_smin, Y_smin),
  544. // smax(X_smax, Y_smax))
  545. if (isEmptySet() || Other.isEmptySet())
  546. return ConstantRange(getBitWidth(), /*isFullSet=*/false);
  547. APInt NewL = APIntOps::smax(getSignedMin(), Other.getSignedMin());
  548. APInt NewU = APIntOps::smax(getSignedMax(), Other.getSignedMax()) + 1;
  549. if (NewU == NewL)
  550. return ConstantRange(getBitWidth(), /*isFullSet=*/true);
  551. return ConstantRange(NewL, NewU);
  552. }
  553. ConstantRange
  554. ConstantRange::umax(const ConstantRange &Other) const {
  555. // X umax Y is: range(umax(X_umin, Y_umin),
  556. // umax(X_umax, Y_umax))
  557. if (isEmptySet() || Other.isEmptySet())
  558. return ConstantRange(getBitWidth(), /*isFullSet=*/false);
  559. APInt NewL = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
  560. APInt NewU = APIntOps::umax(getUnsignedMax(), Other.getUnsignedMax()) + 1;
  561. if (NewU == NewL)
  562. return ConstantRange(getBitWidth(), /*isFullSet=*/true);
  563. return ConstantRange(NewL, NewU);
  564. }
  565. ConstantRange
  566. ConstantRange::udiv(const ConstantRange &RHS) const {
  567. if (isEmptySet() || RHS.isEmptySet() || RHS.getUnsignedMax() == 0)
  568. return ConstantRange(getBitWidth(), /*isFullSet=*/false);
  569. if (RHS.isFullSet())
  570. return ConstantRange(getBitWidth(), /*isFullSet=*/true);
  571. APInt Lower = getUnsignedMin().udiv(RHS.getUnsignedMax());
  572. APInt RHS_umin = RHS.getUnsignedMin();
  573. if (RHS_umin == 0) {
  574. // We want the lowest value in RHS excluding zero. Usually that would be 1
  575. // except for a range in the form of [X, 1) in which case it would be X.
  576. if (RHS.getUpper() == 1)
  577. RHS_umin = RHS.getLower();
  578. else
  579. RHS_umin = APInt(getBitWidth(), 1);
  580. }
  581. APInt Upper = getUnsignedMax().udiv(RHS_umin) + 1;
  582. // If the LHS is Full and the RHS is a wrapped interval containing 1 then
  583. // this could occur.
  584. if (Lower == Upper)
  585. return ConstantRange(getBitWidth(), /*isFullSet=*/true);
  586. return ConstantRange(Lower, Upper);
  587. }
  588. ConstantRange
  589. ConstantRange::binaryAnd(const ConstantRange &Other) const {
  590. if (isEmptySet() || Other.isEmptySet())
  591. return ConstantRange(getBitWidth(), /*isFullSet=*/false);
  592. // TODO: replace this with something less conservative
  593. APInt umin = APIntOps::umin(Other.getUnsignedMax(), getUnsignedMax());
  594. if (umin.isAllOnesValue())
  595. return ConstantRange(getBitWidth(), /*isFullSet=*/true);
  596. return ConstantRange(APInt::getNullValue(getBitWidth()), umin + 1);
  597. }
  598. ConstantRange
  599. ConstantRange::binaryOr(const ConstantRange &Other) const {
  600. if (isEmptySet() || Other.isEmptySet())
  601. return ConstantRange(getBitWidth(), /*isFullSet=*/false);
  602. // TODO: replace this with something less conservative
  603. APInt umax = APIntOps::umax(getUnsignedMin(), Other.getUnsignedMin());
  604. if (umax.isMinValue())
  605. return ConstantRange(getBitWidth(), /*isFullSet=*/true);
  606. return ConstantRange(umax, APInt::getNullValue(getBitWidth()));
  607. }
  608. ConstantRange
  609. ConstantRange::shl(const ConstantRange &Other) const {
  610. if (isEmptySet() || Other.isEmptySet())
  611. return ConstantRange(getBitWidth(), /*isFullSet=*/false);
  612. APInt min = getUnsignedMin().shl(Other.getUnsignedMin());
  613. APInt max = getUnsignedMax().shl(Other.getUnsignedMax());
  614. // there's no overflow!
  615. APInt Zeros(getBitWidth(), getUnsignedMax().countLeadingZeros());
  616. if (Zeros.ugt(Other.getUnsignedMax()))
  617. return ConstantRange(min, max + 1);
  618. // FIXME: implement the other tricky cases
  619. return ConstantRange(getBitWidth(), /*isFullSet=*/true);
  620. }
  621. ConstantRange
  622. ConstantRange::lshr(const ConstantRange &Other) const {
  623. if (isEmptySet() || Other.isEmptySet())
  624. return ConstantRange(getBitWidth(), /*isFullSet=*/false);
  625. APInt max = getUnsignedMax().lshr(Other.getUnsignedMin());
  626. APInt min = getUnsignedMin().lshr(Other.getUnsignedMax());
  627. if (min == max + 1)
  628. return ConstantRange(getBitWidth(), /*isFullSet=*/true);
  629. return ConstantRange(min, max + 1);
  630. }
  631. ConstantRange ConstantRange::inverse() const {
  632. if (isFullSet())
  633. return ConstantRange(getBitWidth(), /*isFullSet=*/false);
  634. if (isEmptySet())
  635. return ConstantRange(getBitWidth(), /*isFullSet=*/true);
  636. return ConstantRange(Upper, Lower);
  637. }
  638. /// print - Print out the bounds to a stream...
  639. ///
  640. void ConstantRange::print(raw_ostream &OS) const {
  641. if (isFullSet())
  642. OS << "full-set";
  643. else if (isEmptySet())
  644. OS << "empty-set";
  645. else
  646. OS << "[" << Lower << "," << Upper << ")";
  647. }
  648. /// dump - Allow printing from a debugger easily...
  649. ///
  650. void ConstantRange::dump() const {
  651. print(dbgs());
  652. }