xref: /llvm-project/llvm/unittests/IR/ConstantRangeTest.cpp (revision 77c90c8ce0fe763b6ee2809a6dc437d6f18e1af2)
1 //===- ConstantRangeTest.cpp - ConstantRange tests ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/IR/ConstantRange.h"
10 #include "llvm/ADT/BitVector.h"
11 #include "llvm/ADT/Sequence.h"
12 #include "llvm/ADT/SmallBitVector.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/Operator.h"
15 #include "llvm/Support/KnownBits.h"
16 #include "gtest/gtest.h"
17 
18 using namespace llvm;
19 
20 namespace {
21 
22 class ConstantRangeTest : public ::testing::Test {
23 protected:
24   static ConstantRange Full;
25   static ConstantRange Empty;
26   static ConstantRange One;
27   static ConstantRange Some;
28   static ConstantRange Wrap;
29 };
30 
31 template<typename Fn>
32 static void EnumerateAPInts(unsigned Bits, Fn TestFn) {
33   APInt N(Bits, 0);
34   do {
35     TestFn(N);
36   } while (++N != 0);
37 }
38 
39 template<typename Fn>
40 static void EnumerateConstantRanges(unsigned Bits, Fn TestFn) {
41   unsigned Max = 1 << Bits;
42   for (unsigned Lo = 0; Lo < Max; Lo++) {
43     for (unsigned Hi = 0; Hi < Max; Hi++) {
44       // Enforce ConstantRange invariant.
45       if (Lo == Hi && Lo != 0 && Lo != Max - 1)
46         continue;
47 
48       ConstantRange CR(APInt(Bits, Lo), APInt(Bits, Hi));
49       TestFn(CR);
50     }
51   }
52 }
53 
54 template<typename Fn>
55 static void EnumerateTwoConstantRanges(unsigned Bits, Fn TestFn) {
56   EnumerateConstantRanges(Bits, [&](const ConstantRange &CR1) {
57     EnumerateConstantRanges(Bits, [&](const ConstantRange &CR2) {
58       TestFn(CR1, CR2);
59     });
60   });
61 }
62 
63 template<typename Fn>
64 static void ForeachNumInConstantRange(const ConstantRange &CR, Fn TestFn) {
65   if (!CR.isEmptySet()) {
66     APInt N = CR.getLower();
67     do TestFn(N);
68     while (++N != CR.getUpper());
69   }
70 }
71 
72 using PreferFn = llvm::function_ref<bool(const ConstantRange &,
73                                          const ConstantRange &)>;
74 
75 bool PreferSmallest(const ConstantRange &CR1, const ConstantRange &CR2) {
76   return CR1.isSizeStrictlySmallerThan(CR2);
77 }
78 
79 bool PreferSmallestUnsigned(const ConstantRange &CR1,
80                             const ConstantRange &CR2) {
81   if (CR1.isWrappedSet() != CR2.isWrappedSet())
82     return CR1.isWrappedSet() < CR2.isWrappedSet();
83   return PreferSmallest(CR1, CR2);
84 }
85 
86 bool PreferSmallestSigned(const ConstantRange &CR1, const ConstantRange &CR2) {
87   if (CR1.isSignWrappedSet() != CR2.isSignWrappedSet())
88     return CR1.isSignWrappedSet() < CR2.isSignWrappedSet();
89   return PreferSmallest(CR1, CR2);
90 }
91 
92 bool PreferSmallestNonFullUnsigned(const ConstantRange &CR1,
93                                    const ConstantRange &CR2) {
94   if (CR1.isFullSet() != CR2.isFullSet())
95     return CR1.isFullSet() < CR2.isFullSet();
96   return PreferSmallestUnsigned(CR1, CR2);
97 }
98 
99 bool PreferSmallestNonFullSigned(const ConstantRange &CR1,
100                                  const ConstantRange &CR2) {
101   if (CR1.isFullSet() != CR2.isFullSet())
102     return CR1.isFullSet() < CR2.isFullSet();
103   return PreferSmallestSigned(CR1, CR2);
104 }
105 
106 testing::AssertionResult rangeContains(const ConstantRange &CR, const APInt &N,
107                                        ArrayRef<ConstantRange> Inputs) {
108   if (CR.contains(N))
109     return testing::AssertionSuccess();
110 
111   testing::AssertionResult Result = testing::AssertionFailure();
112   Result << CR << " does not contain " << N << " for inputs: ";
113   for (const ConstantRange &Input : Inputs)
114     Result << Input << ", ";
115   return Result;
116 }
117 
118 // Check whether constant range CR is an optimal approximation of the set
119 // Elems under the given PreferenceFn. The preference function should return
120 // true if the first range argument is strictly preferred to the second one.
121 static void TestRange(const ConstantRange &CR, const SmallBitVector &Elems,
122                       PreferFn PreferenceFn, ArrayRef<ConstantRange> Inputs,
123                       bool CheckOptimality = true) {
124   unsigned BitWidth = CR.getBitWidth();
125 
126   // Check conservative correctness.
127   for (unsigned Elem : Elems.set_bits()) {
128     EXPECT_TRUE(rangeContains(CR, APInt(BitWidth, Elem), Inputs));
129   }
130 
131   if (!CheckOptimality)
132     return;
133 
134   // Make sure we have at least one element for the code below.
135   if (Elems.none()) {
136     EXPECT_TRUE(CR.isEmptySet());
137     return;
138   }
139 
140   auto NotPreferred = [&](const ConstantRange &PossibleCR) {
141     if (!PreferenceFn(PossibleCR, CR))
142       return testing::AssertionSuccess();
143 
144     testing::AssertionResult Result = testing::AssertionFailure();
145     Result << "Inputs = ";
146     for (const ConstantRange &Input : Inputs)
147       Result << Input << ", ";
148     Result << "CR = " << CR << ", BetterCR = " << PossibleCR;
149     return Result;
150   };
151 
152   // Look at all pairs of adjacent elements and the slack-free ranges
153   // [Elem, PrevElem] they imply. Check that none of the ranges are strictly
154   // preferred over the computed range (they may have equal preference).
155   int FirstElem = Elems.find_first();
156   int PrevElem = FirstElem, Elem;
157   do {
158     Elem = Elems.find_next(PrevElem);
159     if (Elem < 0)
160       Elem = FirstElem; // Wrap around to first element.
161 
162     ConstantRange PossibleCR =
163         ConstantRange::getNonEmpty(APInt(BitWidth, Elem),
164                                    APInt(BitWidth, PrevElem) + 1);
165     // We get a full range any time PrevElem and Elem are adjacent. Avoid
166     // repeated checks by skipping here, and explicitly checking below instead.
167     if (!PossibleCR.isFullSet()) {
168       EXPECT_TRUE(NotPreferred(PossibleCR));
169     }
170 
171     PrevElem = Elem;
172   } while (Elem != FirstElem);
173 
174   EXPECT_TRUE(NotPreferred(ConstantRange::getFull(BitWidth)));
175 }
176 
177 using UnaryRangeFn = llvm::function_ref<ConstantRange(const ConstantRange &)>;
178 using UnaryIntFn = llvm::function_ref<std::optional<APInt>(const APInt &)>;
179 
180 static void TestUnaryOpExhaustive(UnaryRangeFn RangeFn, UnaryIntFn IntFn,
181                                   PreferFn PreferenceFn = PreferSmallest) {
182   unsigned Bits = 4;
183   EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) {
184     SmallBitVector Elems(1 << Bits);
185     ForeachNumInConstantRange(CR, [&](const APInt &N) {
186       if (std::optional<APInt> ResultN = IntFn(N))
187         Elems.set(ResultN->getZExtValue());
188     });
189     TestRange(RangeFn(CR), Elems, PreferenceFn, {CR});
190   });
191 }
192 
193 using BinaryRangeFn = llvm::function_ref<ConstantRange(const ConstantRange &,
194                                                        const ConstantRange &)>;
195 using BinaryIntFn =
196     llvm::function_ref<std::optional<APInt>(const APInt &, const APInt &)>;
197 using BinaryCheckFn = llvm::function_ref<bool(const ConstantRange &,
198                                               const ConstantRange &)>;
199 
200 static bool CheckAll(const ConstantRange &, const ConstantRange &) {
201   return true;
202 }
203 
204 static bool CheckSingleElementsOnly(const ConstantRange &CR1,
205                                     const ConstantRange &CR2) {
206   return CR1.isSingleElement() && CR2.isSingleElement();
207 }
208 
209 static bool CheckNonWrappedOnly(const ConstantRange &CR1,
210                                 const ConstantRange &CR2) {
211   return !CR1.isWrappedSet() && !CR2.isWrappedSet();
212 }
213 
214 static bool CheckNonSignWrappedOnly(const ConstantRange &CR1,
215                                     const ConstantRange &CR2) {
216   return !CR1.isSignWrappedSet() && !CR2.isSignWrappedSet();
217 }
218 
219 static bool CheckNonWrappedOrSignWrappedOnly(const ConstantRange &CR1,
220                                              const ConstantRange &CR2) {
221   return !CR1.isWrappedSet() && !CR1.isSignWrappedSet() &&
222          !CR2.isWrappedSet() && !CR2.isSignWrappedSet();
223 }
224 
225 // CheckFn determines whether optimality is checked for a given range pair.
226 // Correctness is always checked.
227 static void TestBinaryOpExhaustive(BinaryRangeFn RangeFn, BinaryIntFn IntFn,
228                                    PreferFn PreferenceFn = PreferSmallest,
229                                    BinaryCheckFn CheckFn = CheckAll) {
230   unsigned Bits = 4;
231   EnumerateTwoConstantRanges(
232       Bits, [&](const ConstantRange &CR1, const ConstantRange &CR2) {
233         SmallBitVector Elems(1 << Bits);
234         ForeachNumInConstantRange(CR1, [&](const APInt &N1) {
235           ForeachNumInConstantRange(CR2, [&](const APInt &N2) {
236             if (std::optional<APInt> ResultN = IntFn(N1, N2))
237               Elems.set(ResultN->getZExtValue());
238           });
239         });
240         TestRange(RangeFn(CR1, CR2), Elems, PreferenceFn, {CR1, CR2},
241                   CheckFn(CR1, CR2));
242       });
243 }
244 
245 ConstantRange ConstantRangeTest::Full(16, true);
246 ConstantRange ConstantRangeTest::Empty(16, false);
247 ConstantRange ConstantRangeTest::One(APInt(16, 0xa));
248 ConstantRange ConstantRangeTest::Some(APInt(16, 0xa), APInt(16, 0xaaa));
249 ConstantRange ConstantRangeTest::Wrap(APInt(16, 0xaaa), APInt(16, 0xa));
250 
251 TEST_F(ConstantRangeTest, Basics) {
252   EXPECT_TRUE(Full.isFullSet());
253   EXPECT_FALSE(Full.isEmptySet());
254   EXPECT_TRUE(Full.inverse().isEmptySet());
255   EXPECT_FALSE(Full.isWrappedSet());
256   EXPECT_TRUE(Full.contains(APInt(16, 0x0)));
257   EXPECT_TRUE(Full.contains(APInt(16, 0x9)));
258   EXPECT_TRUE(Full.contains(APInt(16, 0xa)));
259   EXPECT_TRUE(Full.contains(APInt(16, 0xaa9)));
260   EXPECT_TRUE(Full.contains(APInt(16, 0xaaa)));
261 
262   EXPECT_FALSE(Empty.isFullSet());
263   EXPECT_TRUE(Empty.isEmptySet());
264   EXPECT_TRUE(Empty.inverse().isFullSet());
265   EXPECT_FALSE(Empty.isWrappedSet());
266   EXPECT_FALSE(Empty.contains(APInt(16, 0x0)));
267   EXPECT_FALSE(Empty.contains(APInt(16, 0x9)));
268   EXPECT_FALSE(Empty.contains(APInt(16, 0xa)));
269   EXPECT_FALSE(Empty.contains(APInt(16, 0xaa9)));
270   EXPECT_FALSE(Empty.contains(APInt(16, 0xaaa)));
271 
272   EXPECT_FALSE(One.isFullSet());
273   EXPECT_FALSE(One.isEmptySet());
274   EXPECT_FALSE(One.isWrappedSet());
275   EXPECT_FALSE(One.contains(APInt(16, 0x0)));
276   EXPECT_FALSE(One.contains(APInt(16, 0x9)));
277   EXPECT_TRUE(One.contains(APInt(16, 0xa)));
278   EXPECT_FALSE(One.contains(APInt(16, 0xaa9)));
279   EXPECT_FALSE(One.contains(APInt(16, 0xaaa)));
280   EXPECT_FALSE(One.inverse().contains(APInt(16, 0xa)));
281 
282   EXPECT_FALSE(Some.isFullSet());
283   EXPECT_FALSE(Some.isEmptySet());
284   EXPECT_FALSE(Some.isWrappedSet());
285   EXPECT_FALSE(Some.contains(APInt(16, 0x0)));
286   EXPECT_FALSE(Some.contains(APInt(16, 0x9)));
287   EXPECT_TRUE(Some.contains(APInt(16, 0xa)));
288   EXPECT_TRUE(Some.contains(APInt(16, 0xaa9)));
289   EXPECT_FALSE(Some.contains(APInt(16, 0xaaa)));
290 
291   EXPECT_FALSE(Wrap.isFullSet());
292   EXPECT_FALSE(Wrap.isEmptySet());
293   EXPECT_TRUE(Wrap.isWrappedSet());
294   EXPECT_TRUE(Wrap.contains(APInt(16, 0x0)));
295   EXPECT_TRUE(Wrap.contains(APInt(16, 0x9)));
296   EXPECT_FALSE(Wrap.contains(APInt(16, 0xa)));
297   EXPECT_FALSE(Wrap.contains(APInt(16, 0xaa9)));
298   EXPECT_TRUE(Wrap.contains(APInt(16, 0xaaa)));
299 }
300 
301 TEST_F(ConstantRangeTest, Equality) {
302   EXPECT_EQ(Full, Full);
303   EXPECT_EQ(Empty, Empty);
304   EXPECT_EQ(One, One);
305   EXPECT_EQ(Some, Some);
306   EXPECT_EQ(Wrap, Wrap);
307   EXPECT_NE(Full, Empty);
308   EXPECT_NE(Full, One);
309   EXPECT_NE(Full, Some);
310   EXPECT_NE(Full, Wrap);
311   EXPECT_NE(Empty, One);
312   EXPECT_NE(Empty, Some);
313   EXPECT_NE(Empty, Wrap);
314   EXPECT_NE(One, Some);
315   EXPECT_NE(One, Wrap);
316   EXPECT_NE(Some, Wrap);
317 }
318 
319 TEST_F(ConstantRangeTest, SingleElement) {
320   EXPECT_EQ(Full.getSingleElement(), static_cast<APInt *>(nullptr));
321   EXPECT_EQ(Empty.getSingleElement(), static_cast<APInt *>(nullptr));
322   EXPECT_EQ(Full.getSingleMissingElement(), static_cast<APInt *>(nullptr));
323   EXPECT_EQ(Empty.getSingleMissingElement(), static_cast<APInt *>(nullptr));
324 
325   EXPECT_EQ(*One.getSingleElement(), APInt(16, 0xa));
326   EXPECT_EQ(Some.getSingleElement(), static_cast<APInt *>(nullptr));
327   EXPECT_EQ(Wrap.getSingleElement(), static_cast<APInt *>(nullptr));
328 
329   EXPECT_EQ(One.getSingleMissingElement(), static_cast<APInt *>(nullptr));
330   EXPECT_EQ(Some.getSingleMissingElement(), static_cast<APInt *>(nullptr));
331 
332   ConstantRange OneInverse = One.inverse();
333   EXPECT_EQ(*OneInverse.getSingleMissingElement(), *One.getSingleElement());
334 
335   EXPECT_FALSE(Full.isSingleElement());
336   EXPECT_FALSE(Empty.isSingleElement());
337   EXPECT_TRUE(One.isSingleElement());
338   EXPECT_FALSE(Some.isSingleElement());
339   EXPECT_FALSE(Wrap.isSingleElement());
340 }
341 
342 TEST_F(ConstantRangeTest, GetMinsAndMaxes) {
343   EXPECT_EQ(Full.getUnsignedMax(), APInt(16, UINT16_MAX));
344   EXPECT_EQ(One.getUnsignedMax(), APInt(16, 0xa));
345   EXPECT_EQ(Some.getUnsignedMax(), APInt(16, 0xaa9));
346   EXPECT_EQ(Wrap.getUnsignedMax(), APInt(16, UINT16_MAX));
347 
348   EXPECT_EQ(Full.getUnsignedMin(), APInt(16, 0));
349   EXPECT_EQ(One.getUnsignedMin(), APInt(16, 0xa));
350   EXPECT_EQ(Some.getUnsignedMin(), APInt(16, 0xa));
351   EXPECT_EQ(Wrap.getUnsignedMin(), APInt(16, 0));
352 
353   EXPECT_EQ(Full.getSignedMax(), APInt(16, INT16_MAX));
354   EXPECT_EQ(One.getSignedMax(), APInt(16, 0xa));
355   EXPECT_EQ(Some.getSignedMax(), APInt(16, 0xaa9));
356   EXPECT_EQ(Wrap.getSignedMax(), APInt(16, INT16_MAX));
357 
358   EXPECT_EQ(Full.getSignedMin(), APInt(16, (uint64_t)INT16_MIN));
359   EXPECT_EQ(One.getSignedMin(), APInt(16, 0xa));
360   EXPECT_EQ(Some.getSignedMin(), APInt(16, 0xa));
361   EXPECT_EQ(Wrap.getSignedMin(), APInt(16, (uint64_t)INT16_MIN));
362 
363   // Found by Klee
364   EXPECT_EQ(ConstantRange(APInt(4, 7), APInt(4, 0)).getSignedMax(),
365             APInt(4, 7));
366 }
367 
368 TEST_F(ConstantRangeTest, SignWrapped) {
369   EXPECT_FALSE(Full.isSignWrappedSet());
370   EXPECT_FALSE(Empty.isSignWrappedSet());
371   EXPECT_FALSE(One.isSignWrappedSet());
372   EXPECT_FALSE(Some.isSignWrappedSet());
373   EXPECT_TRUE(Wrap.isSignWrappedSet());
374 
375   EXPECT_FALSE(ConstantRange(APInt(8, 127), APInt(8, 128)).isSignWrappedSet());
376   EXPECT_TRUE(ConstantRange(APInt(8, 127), APInt(8, 129)).isSignWrappedSet());
377   EXPECT_FALSE(ConstantRange(APInt(8, 128), APInt(8, 129)).isSignWrappedSet());
378   EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 9)).isSignWrappedSet());
379   EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 250)).isSignWrappedSet());
380   EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 10)).isSignWrappedSet());
381   EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 251)).isSignWrappedSet());
382 }
383 
384 TEST_F(ConstantRangeTest, UpperWrapped) {
385   // The behavior here is the same as for isWrappedSet() / isSignWrappedSet().
386   EXPECT_FALSE(Full.isUpperWrapped());
387   EXPECT_FALSE(Empty.isUpperWrapped());
388   EXPECT_FALSE(One.isUpperWrapped());
389   EXPECT_FALSE(Some.isUpperWrapped());
390   EXPECT_TRUE(Wrap.isUpperWrapped());
391   EXPECT_FALSE(Full.isUpperSignWrapped());
392   EXPECT_FALSE(Empty.isUpperSignWrapped());
393   EXPECT_FALSE(One.isUpperSignWrapped());
394   EXPECT_FALSE(Some.isUpperSignWrapped());
395   EXPECT_TRUE(Wrap.isUpperSignWrapped());
396 
397   // The behavior differs if Upper is the Min/SignedMin value.
398   ConstantRange CR1(APInt(8, 42), APInt::getMinValue(8));
399   EXPECT_FALSE(CR1.isWrappedSet());
400   EXPECT_TRUE(CR1.isUpperWrapped());
401 
402   ConstantRange CR2(APInt(8, 42), APInt::getSignedMinValue(8));
403   EXPECT_FALSE(CR2.isSignWrappedSet());
404   EXPECT_TRUE(CR2.isUpperSignWrapped());
405 }
406 
407 TEST_F(ConstantRangeTest, Trunc) {
408   ConstantRange TFull = Full.truncate(10);
409   ConstantRange TEmpty = Empty.truncate(10);
410   ConstantRange TOne = One.truncate(10);
411   ConstantRange TSome = Some.truncate(10);
412   ConstantRange TWrap = Wrap.truncate(10);
413   EXPECT_TRUE(TFull.isFullSet());
414   EXPECT_TRUE(TEmpty.isEmptySet());
415   EXPECT_EQ(TOne, ConstantRange(One.getLower().trunc(10),
416                                 One.getUpper().trunc(10)));
417   EXPECT_TRUE(TSome.isFullSet());
418   EXPECT_TRUE(TWrap.isFullSet());
419 
420   // trunc([2, 5), 3->2) = [2, 1)
421   ConstantRange TwoFive(APInt(3, 2), APInt(3, 5));
422   EXPECT_EQ(TwoFive.truncate(2), ConstantRange(APInt(2, 2), APInt(2, 1)));
423 
424   // trunc([2, 6), 3->2) = full
425   ConstantRange TwoSix(APInt(3, 2), APInt(3, 6));
426   EXPECT_TRUE(TwoSix.truncate(2).isFullSet());
427 
428   // trunc([5, 7), 3->2) = [1, 3)
429   ConstantRange FiveSeven(APInt(3, 5), APInt(3, 7));
430   EXPECT_EQ(FiveSeven.truncate(2), ConstantRange(APInt(2, 1), APInt(2, 3)));
431 
432   // trunc([7, 1), 3->2) = [3, 1)
433   ConstantRange SevenOne(APInt(3, 7), APInt(3, 1));
434   EXPECT_EQ(SevenOne.truncate(2), ConstantRange(APInt(2, 3), APInt(2, 1)));
435 }
436 
437 TEST_F(ConstantRangeTest, ZExt) {
438   ConstantRange ZFull = Full.zeroExtend(20);
439   ConstantRange ZEmpty = Empty.zeroExtend(20);
440   ConstantRange ZOne = One.zeroExtend(20);
441   ConstantRange ZSome = Some.zeroExtend(20);
442   ConstantRange ZWrap = Wrap.zeroExtend(20);
443   EXPECT_EQ(ZFull, ConstantRange(APInt(20, 0), APInt(20, 0x10000)));
444   EXPECT_TRUE(ZEmpty.isEmptySet());
445   EXPECT_EQ(ZOne, ConstantRange(One.getLower().zext(20),
446                                 One.getUpper().zext(20)));
447   EXPECT_EQ(ZSome, ConstantRange(Some.getLower().zext(20),
448                                  Some.getUpper().zext(20)));
449   EXPECT_EQ(ZWrap, ConstantRange(APInt(20, 0), APInt(20, 0x10000)));
450 
451   // zext([5, 0), 3->7) = [5, 8)
452   ConstantRange FiveZero(APInt(3, 5), APInt(3, 0));
453   EXPECT_EQ(FiveZero.zeroExtend(7), ConstantRange(APInt(7, 5), APInt(7, 8)));
454 }
455 
456 TEST_F(ConstantRangeTest, SExt) {
457   ConstantRange SFull = Full.signExtend(20);
458   ConstantRange SEmpty = Empty.signExtend(20);
459   ConstantRange SOne = One.signExtend(20);
460   ConstantRange SSome = Some.signExtend(20);
461   ConstantRange SWrap = Wrap.signExtend(20);
462   EXPECT_EQ(SFull, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true),
463                                  APInt(20, INT16_MAX + 1, true)));
464   EXPECT_TRUE(SEmpty.isEmptySet());
465   EXPECT_EQ(SOne, ConstantRange(One.getLower().sext(20),
466                                 One.getUpper().sext(20)));
467   EXPECT_EQ(SSome, ConstantRange(Some.getLower().sext(20),
468                                  Some.getUpper().sext(20)));
469   EXPECT_EQ(SWrap, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true),
470                                  APInt(20, INT16_MAX + 1, true)));
471 
472   EXPECT_EQ(ConstantRange(APInt(8, 120), APInt(8, 140)).signExtend(16),
473             ConstantRange(APInt(16, -128), APInt(16, 128)));
474 
475   EXPECT_EQ(ConstantRange(APInt(16, 0x0200), APInt(16, 0x8000)).signExtend(19),
476             ConstantRange(APInt(19, 0x0200), APInt(19, 0x8000)));
477 }
478 
479 TEST_F(ConstantRangeTest, IntersectWith) {
480   EXPECT_EQ(Empty.intersectWith(Full), Empty);
481   EXPECT_EQ(Empty.intersectWith(Empty), Empty);
482   EXPECT_EQ(Empty.intersectWith(One), Empty);
483   EXPECT_EQ(Empty.intersectWith(Some), Empty);
484   EXPECT_EQ(Empty.intersectWith(Wrap), Empty);
485   EXPECT_EQ(Full.intersectWith(Full), Full);
486   EXPECT_EQ(Some.intersectWith(Some), Some);
487   EXPECT_EQ(Some.intersectWith(One), One);
488   EXPECT_EQ(Full.intersectWith(One), One);
489   EXPECT_EQ(Full.intersectWith(Some), Some);
490   EXPECT_EQ(Some.intersectWith(Wrap), Empty);
491   EXPECT_EQ(One.intersectWith(Wrap), Empty);
492   EXPECT_EQ(One.intersectWith(Wrap), Wrap.intersectWith(One));
493 
494   // Klee generated testcase from PR4545.
495   // The intersection of i16 [4, 2) and [6, 5) is disjoint, looking like
496   // 01..4.6789ABCDEF where the dots represent values not in the intersection.
497   ConstantRange LHS(APInt(16, 4), APInt(16, 2));
498   ConstantRange RHS(APInt(16, 6), APInt(16, 5));
499   EXPECT_TRUE(LHS.intersectWith(RHS) == LHS);
500 
501   // previous bug: intersection of [min, 3) and [2, max) should be 2
502   LHS = ConstantRange(APInt(32, -2147483646), APInt(32, 3));
503   RHS = ConstantRange(APInt(32, 2), APInt(32, 2147483646));
504   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2)));
505 
506   // [2, 0) /\ [4, 3) = [2, 0)
507   LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
508   RHS = ConstantRange(APInt(32, 4), APInt(32, 3));
509   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2), APInt(32, 0)));
510 
511   // [2, 0) /\ [4, 2) = [4, 0)
512   LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
513   RHS = ConstantRange(APInt(32, 4), APInt(32, 2));
514   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 0)));
515 
516   // [4, 2) /\ [5, 1) = [5, 1)
517   LHS = ConstantRange(APInt(32, 4), APInt(32, 2));
518   RHS = ConstantRange(APInt(32, 5), APInt(32, 1));
519   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 5), APInt(32, 1)));
520 
521   // [2, 0) /\ [7, 4) = [7, 4)
522   LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
523   RHS = ConstantRange(APInt(32, 7), APInt(32, 4));
524   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 7), APInt(32, 4)));
525 
526   // [4, 2) /\ [1, 0) = [1, 0)
527   LHS = ConstantRange(APInt(32, 4), APInt(32, 2));
528   RHS = ConstantRange(APInt(32, 1), APInt(32, 0));
529   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 2)));
530 
531   // [15, 0) /\ [7, 6) = [15, 0)
532   LHS = ConstantRange(APInt(32, 15), APInt(32, 0));
533   RHS = ConstantRange(APInt(32, 7), APInt(32, 6));
534   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 15), APInt(32, 0)));
535 }
536 
537 template<typename Fn1, typename Fn2, typename Fn3>
538 void testBinarySetOperationExhaustive(Fn1 OpFn, Fn2 ExactOpFn, Fn3 InResultFn) {
539   unsigned Bits = 4;
540   EnumerateTwoConstantRanges(Bits,
541       [=](const ConstantRange &CR1, const ConstantRange &CR2) {
542         SmallBitVector Elems(1 << Bits);
543         APInt Num(Bits, 0);
544         for (unsigned I = 0, Limit = 1 << Bits; I < Limit; ++I, ++Num)
545           if (InResultFn(CR1, CR2, Num))
546             Elems.set(Num.getZExtValue());
547 
548         ConstantRange SmallestCR = OpFn(CR1, CR2, ConstantRange::Smallest);
549         TestRange(SmallestCR, Elems, PreferSmallest, {CR1, CR2});
550 
551         ConstantRange UnsignedCR = OpFn(CR1, CR2, ConstantRange::Unsigned);
552         TestRange(UnsignedCR, Elems, PreferSmallestNonFullUnsigned, {CR1, CR2});
553 
554         ConstantRange SignedCR = OpFn(CR1, CR2, ConstantRange::Signed);
555         TestRange(SignedCR, Elems, PreferSmallestNonFullSigned, {CR1, CR2});
556 
557         std::optional<ConstantRange> ExactCR = ExactOpFn(CR1, CR2);
558         if (SmallestCR.isSizeLargerThan(Elems.count())) {
559           EXPECT_TRUE(!ExactCR);
560         } else {
561           EXPECT_EQ(SmallestCR, *ExactCR);
562         }
563       });
564 }
565 
566 TEST_F(ConstantRangeTest, IntersectWithExhaustive) {
567   testBinarySetOperationExhaustive(
568       [](const ConstantRange &CR1, const ConstantRange &CR2,
569          ConstantRange::PreferredRangeType Type) {
570         return CR1.intersectWith(CR2, Type);
571       },
572       [](const ConstantRange &CR1, const ConstantRange &CR2) {
573         return CR1.exactIntersectWith(CR2);
574       },
575       [](const ConstantRange &CR1, const ConstantRange &CR2, const APInt &N) {
576         return CR1.contains(N) && CR2.contains(N);
577       });
578 }
579 
580 TEST_F(ConstantRangeTest, UnionWithExhaustive) {
581   testBinarySetOperationExhaustive(
582       [](const ConstantRange &CR1, const ConstantRange &CR2,
583          ConstantRange::PreferredRangeType Type) {
584         return CR1.unionWith(CR2, Type);
585       },
586       [](const ConstantRange &CR1, const ConstantRange &CR2) {
587         return CR1.exactUnionWith(CR2);
588       },
589       [](const ConstantRange &CR1, const ConstantRange &CR2, const APInt &N) {
590         return CR1.contains(N) || CR2.contains(N);
591       });
592 }
593 
594 TEST_F(ConstantRangeTest, UnionWith) {
595   EXPECT_EQ(Wrap.unionWith(One),
596             ConstantRange(APInt(16, 0xaaa), APInt(16, 0xb)));
597   EXPECT_EQ(One.unionWith(Wrap), Wrap.unionWith(One));
598   EXPECT_EQ(Empty.unionWith(Empty), Empty);
599   EXPECT_EQ(Full.unionWith(Full), Full);
600   EXPECT_EQ(Some.unionWith(Wrap), Full);
601 
602   // PR4545
603   EXPECT_EQ(ConstantRange(APInt(16, 14), APInt(16, 1)).unionWith(
604                                     ConstantRange(APInt(16, 0), APInt(16, 8))),
605             ConstantRange(APInt(16, 14), APInt(16, 8)));
606   EXPECT_EQ(ConstantRange(APInt(16, 6), APInt(16, 4)).unionWith(
607                                     ConstantRange(APInt(16, 4), APInt(16, 0))),
608             ConstantRange::getFull(16));
609   EXPECT_EQ(ConstantRange(APInt(16, 1), APInt(16, 0)).unionWith(
610                                     ConstantRange(APInt(16, 2), APInt(16, 1))),
611             ConstantRange::getFull(16));
612 }
613 
614 TEST_F(ConstantRangeTest, SetDifference) {
615   EXPECT_EQ(Full.difference(Empty), Full);
616   EXPECT_EQ(Full.difference(Full), Empty);
617   EXPECT_EQ(Empty.difference(Empty), Empty);
618   EXPECT_EQ(Empty.difference(Full), Empty);
619 
620   ConstantRange A(APInt(16, 3), APInt(16, 7));
621   ConstantRange B(APInt(16, 5), APInt(16, 9));
622   ConstantRange C(APInt(16, 3), APInt(16, 5));
623   ConstantRange D(APInt(16, 7), APInt(16, 9));
624   ConstantRange E(APInt(16, 5), APInt(16, 4));
625   ConstantRange F(APInt(16, 7), APInt(16, 3));
626   EXPECT_EQ(A.difference(B), C);
627   EXPECT_EQ(B.difference(A), D);
628   EXPECT_EQ(E.difference(A), F);
629 }
630 
631 TEST_F(ConstantRangeTest, getActiveBits) {
632   unsigned Bits = 4;
633   EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) {
634     unsigned Exact = 0;
635     ForeachNumInConstantRange(CR, [&](const APInt &N) {
636       Exact = std::max(Exact, N.getActiveBits());
637     });
638 
639     unsigned ResultCR = CR.getActiveBits();
640     EXPECT_EQ(Exact, ResultCR);
641   });
642 }
643 TEST_F(ConstantRangeTest, losslessUnsignedTruncationZeroext) {
644   unsigned Bits = 4;
645   EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) {
646     unsigned MinBitWidth = CR.getActiveBits();
647     if (MinBitWidth == 0) {
648       EXPECT_TRUE(CR.isEmptySet() ||
649                   (CR.isSingleElement() && CR.getSingleElement()->isZero()));
650       return;
651     }
652     if (MinBitWidth == Bits)
653       return;
654     EXPECT_EQ(CR, CR.truncate(MinBitWidth).zeroExtend(Bits));
655   });
656 }
657 
658 TEST_F(ConstantRangeTest, getMinSignedBits) {
659   unsigned Bits = 4;
660   EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) {
661     unsigned Exact = 0;
662     ForeachNumInConstantRange(CR, [&](const APInt &N) {
663       Exact = std::max(Exact, N.getMinSignedBits());
664     });
665 
666     unsigned ResultCR = CR.getMinSignedBits();
667     EXPECT_EQ(Exact, ResultCR);
668   });
669 }
670 TEST_F(ConstantRangeTest, losslessSignedTruncationSignext) {
671   unsigned Bits = 4;
672   EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) {
673     unsigned MinBitWidth = CR.getMinSignedBits();
674     if (MinBitWidth == 0) {
675       EXPECT_TRUE(CR.isEmptySet());
676       return;
677     }
678     if (MinBitWidth == Bits)
679       return;
680     EXPECT_EQ(CR, CR.truncate(MinBitWidth).signExtend(Bits));
681   });
682 }
683 
684 TEST_F(ConstantRangeTest, SubtractAPInt) {
685   EXPECT_EQ(Full.subtract(APInt(16, 4)), Full);
686   EXPECT_EQ(Empty.subtract(APInt(16, 4)), Empty);
687   EXPECT_EQ(Some.subtract(APInt(16, 4)),
688             ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
689   EXPECT_EQ(Wrap.subtract(APInt(16, 4)),
690             ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
691   EXPECT_EQ(One.subtract(APInt(16, 4)),
692             ConstantRange(APInt(16, 0x6)));
693 }
694 
695 TEST_F(ConstantRangeTest, Add) {
696   EXPECT_EQ(Full.add(APInt(16, 4)), Full);
697   EXPECT_EQ(Full.add(Full), Full);
698   EXPECT_EQ(Full.add(Empty), Empty);
699   EXPECT_EQ(Full.add(One), Full);
700   EXPECT_EQ(Full.add(Some), Full);
701   EXPECT_EQ(Full.add(Wrap), Full);
702   EXPECT_EQ(Empty.add(Empty), Empty);
703   EXPECT_EQ(Empty.add(One), Empty);
704   EXPECT_EQ(Empty.add(Some), Empty);
705   EXPECT_EQ(Empty.add(Wrap), Empty);
706   EXPECT_EQ(Empty.add(APInt(16, 4)), Empty);
707   EXPECT_EQ(Some.add(APInt(16, 4)),
708             ConstantRange(APInt(16, 0xe), APInt(16, 0xaae)));
709   EXPECT_EQ(Wrap.add(APInt(16, 4)),
710             ConstantRange(APInt(16, 0xaae), APInt(16, 0xe)));
711   EXPECT_EQ(One.add(APInt(16, 4)),
712             ConstantRange(APInt(16, 0xe)));
713 
714   TestBinaryOpExhaustive(
715       [](const ConstantRange &CR1, const ConstantRange &CR2) {
716         return CR1.add(CR2);
717       },
718       [](const APInt &N1, const APInt &N2) {
719         return N1 + N2;
720       });
721 }
722 
723 TEST_F(ConstantRangeTest, AddWithNoWrap) {
724   typedef OverflowingBinaryOperator OBO;
725   EXPECT_EQ(Empty.addWithNoWrap(Some, OBO::NoSignedWrap), Empty);
726   EXPECT_EQ(Some.addWithNoWrap(Empty, OBO::NoSignedWrap), Empty);
727   EXPECT_EQ(Full.addWithNoWrap(Full, OBO::NoSignedWrap), Full);
728   EXPECT_NE(Full.addWithNoWrap(Some, OBO::NoSignedWrap), Full);
729   EXPECT_NE(Some.addWithNoWrap(Full, OBO::NoSignedWrap), Full);
730   EXPECT_EQ(Full.addWithNoWrap(ConstantRange(APInt(16, 1), APInt(16, 2)),
731                                OBO::NoSignedWrap),
732             ConstantRange(APInt(16, INT16_MIN + 1), APInt(16, INT16_MIN)));
733   EXPECT_EQ(ConstantRange(APInt(16, 1), APInt(16, 2))
734                 .addWithNoWrap(Full, OBO::NoSignedWrap),
735             ConstantRange(APInt(16, INT16_MIN + 1), APInt(16, INT16_MIN)));
736   EXPECT_EQ(Full.addWithNoWrap(ConstantRange(APInt(16, -1), APInt(16, 0)),
737                                OBO::NoSignedWrap),
738             ConstantRange(APInt(16, INT16_MIN), APInt(16, INT16_MAX)));
739   EXPECT_EQ(ConstantRange(APInt(8, 100), APInt(8, 120))
740                 .addWithNoWrap(ConstantRange(APInt(8, 120), APInt(8, 123)),
741                                OBO::NoSignedWrap),
742             ConstantRange(8, false));
743   EXPECT_EQ(ConstantRange(APInt(8, -120), APInt(8, -100))
744                 .addWithNoWrap(ConstantRange(APInt(8, -110), APInt(8, -100)),
745                                OBO::NoSignedWrap),
746             ConstantRange(8, false));
747   EXPECT_EQ(ConstantRange(APInt(8, 0), APInt(8, 101))
748                 .addWithNoWrap(ConstantRange(APInt(8, -128), APInt(8, 28)),
749                                OBO::NoSignedWrap),
750             ConstantRange(8, true));
751   EXPECT_EQ(ConstantRange(APInt(8, 0), APInt(8, 101))
752                 .addWithNoWrap(ConstantRange(APInt(8, -120), APInt(8, 29)),
753                                OBO::NoSignedWrap),
754             ConstantRange(APInt(8, -120), APInt(8, -128)));
755   EXPECT_EQ(ConstantRange(APInt(8, -50), APInt(8, 50))
756                 .addWithNoWrap(ConstantRange(APInt(8, 10), APInt(8, 20)),
757                                OBO::NoSignedWrap),
758             ConstantRange(APInt(8, -40), APInt(8, 69)));
759   EXPECT_EQ(ConstantRange(APInt(8, 10), APInt(8, 20))
760                 .addWithNoWrap(ConstantRange(APInt(8, -50), APInt(8, 50)),
761                                OBO::NoSignedWrap),
762             ConstantRange(APInt(8, -40), APInt(8, 69)));
763   EXPECT_EQ(ConstantRange(APInt(8, 120), APInt(8, -10))
764                 .addWithNoWrap(ConstantRange(APInt(8, 5), APInt(8, 20)),
765                                OBO::NoSignedWrap),
766             ConstantRange(APInt(8, 125), APInt(8, 9)));
767   EXPECT_EQ(ConstantRange(APInt(8, 5), APInt(8, 20))
768                 .addWithNoWrap(ConstantRange(APInt(8, 120), APInt(8, -10)),
769                                OBO::NoSignedWrap),
770             ConstantRange(APInt(8, 125), APInt(8, 9)));
771 
772   TestBinaryOpExhaustive(
773       [](const ConstantRange &CR1, const ConstantRange &CR2) {
774         return CR1.addWithNoWrap(CR2, OBO::NoSignedWrap);
775       },
776       [](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
777         bool IsOverflow;
778         APInt Res = N1.sadd_ov(N2, IsOverflow);
779         if (IsOverflow)
780           return std::nullopt;
781         return Res;
782       },
783       PreferSmallest, CheckNonSignWrappedOnly);
784 
785   EXPECT_EQ(Empty.addWithNoWrap(Some, OBO::NoUnsignedWrap), Empty);
786   EXPECT_EQ(Some.addWithNoWrap(Empty, OBO::NoUnsignedWrap), Empty);
787   EXPECT_EQ(Full.addWithNoWrap(Full, OBO::NoUnsignedWrap), Full);
788   EXPECT_NE(Full.addWithNoWrap(Some, OBO::NoUnsignedWrap), Full);
789   EXPECT_NE(Some.addWithNoWrap(Full, OBO::NoUnsignedWrap), Full);
790   EXPECT_EQ(Full.addWithNoWrap(ConstantRange(APInt(16, 1), APInt(16, 2)),
791                                OBO::NoUnsignedWrap),
792             ConstantRange(APInt(16, 1), APInt(16, 0)));
793   EXPECT_EQ(ConstantRange(APInt(16, 1), APInt(16, 2))
794                 .addWithNoWrap(Full, OBO::NoUnsignedWrap),
795             ConstantRange(APInt(16, 1), APInt(16, 0)));
796   EXPECT_EQ(ConstantRange(APInt(8, 200), APInt(8, 220))
797                 .addWithNoWrap(ConstantRange(APInt(8, 100), APInt(8, 123)),
798                                OBO::NoUnsignedWrap),
799             ConstantRange(8, false));
800   EXPECT_EQ(ConstantRange(APInt(8, 0), APInt(8, 101))
801                 .addWithNoWrap(ConstantRange(APInt(8, 0), APInt(8, 156)),
802                                OBO::NoUnsignedWrap),
803             ConstantRange(8, true));
804   EXPECT_EQ(ConstantRange(APInt(8, 0), APInt(8, 101))
805                 .addWithNoWrap(ConstantRange(APInt(8, 10), APInt(8, 29)),
806                                OBO::NoUnsignedWrap),
807             ConstantRange(APInt(8, 10), APInt(8, 129)));
808   EXPECT_EQ(ConstantRange(APInt(8, 20), APInt(8, 10))
809                 .addWithNoWrap(ConstantRange(APInt(8, 50), APInt(8, 200)),
810                                OBO::NoUnsignedWrap),
811             ConstantRange(APInt(8, 50), APInt(8, 0)));
812   EXPECT_EQ(ConstantRange(APInt(8, 10), APInt(8, 20))
813                 .addWithNoWrap(ConstantRange(APInt(8, 50), APInt(8, 200)),
814                                OBO::NoUnsignedWrap),
815             ConstantRange(APInt(8, 60), APInt(8, -37)));
816   EXPECT_EQ(ConstantRange(APInt(8, 20), APInt(8, -30))
817                 .addWithNoWrap(ConstantRange(APInt(8, 5), APInt(8, 20)),
818                                OBO::NoUnsignedWrap),
819             ConstantRange(APInt(8, 25), APInt(8, -11)));
820   EXPECT_EQ(ConstantRange(APInt(8, 5), APInt(8, 20))
821                 .addWithNoWrap(ConstantRange(APInt(8, 20), APInt(8, -30)),
822                                OBO::NoUnsignedWrap),
823             ConstantRange(APInt(8, 25), APInt(8, -11)));
824 
825   TestBinaryOpExhaustive(
826       [](const ConstantRange &CR1, const ConstantRange &CR2) {
827         return CR1.addWithNoWrap(CR2, OBO::NoUnsignedWrap);
828       },
829       [](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
830         bool IsOverflow;
831         APInt Res = N1.uadd_ov(N2, IsOverflow);
832         if (IsOverflow)
833           return std::nullopt;
834         return Res;
835       },
836       PreferSmallest, CheckNonWrappedOnly);
837 
838   EXPECT_EQ(ConstantRange(APInt(8, 50), APInt(8, 100))
839                 .addWithNoWrap(ConstantRange(APInt(8, 20), APInt(8, 70)),
840                                OBO::NoSignedWrap),
841             ConstantRange(APInt(8, 70), APInt(8, -128)));
842   EXPECT_EQ(ConstantRange(APInt(8, 50), APInt(8, 100))
843                 .addWithNoWrap(ConstantRange(APInt(8, 20), APInt(8, 70)),
844                                OBO::NoUnsignedWrap),
845             ConstantRange(APInt(8, 70), APInt(8, 169)));
846   EXPECT_EQ(ConstantRange(APInt(8, 50), APInt(8, 100))
847                 .addWithNoWrap(ConstantRange(APInt(8, 20), APInt(8, 70)),
848                                OBO::NoUnsignedWrap | OBO::NoSignedWrap),
849             ConstantRange(APInt(8, 70), APInt(8, -128)));
850 
851   EXPECT_EQ(ConstantRange(APInt(8, -100), APInt(8, -50))
852                 .addWithNoWrap(ConstantRange(APInt(8, 20), APInt(8, 30)),
853                                OBO::NoSignedWrap),
854             ConstantRange(APInt(8, -80), APInt(8, -21)));
855   EXPECT_EQ(ConstantRange(APInt(8, -100), APInt(8, -50))
856                 .addWithNoWrap(ConstantRange(APInt(8, 20), APInt(8, 30)),
857                                OBO::NoUnsignedWrap),
858             ConstantRange(APInt(8, 176), APInt(8, 235)));
859   EXPECT_EQ(ConstantRange(APInt(8, -100), APInt(8, -50))
860                 .addWithNoWrap(ConstantRange(APInt(8, 20), APInt(8, 30)),
861                                OBO::NoUnsignedWrap | OBO::NoSignedWrap),
862             ConstantRange(APInt(8, 176), APInt(8, 235)));
863 
864   TestBinaryOpExhaustive(
865       [](const ConstantRange &CR1, const ConstantRange &CR2) {
866         return CR1.addWithNoWrap(CR2, OBO::NoUnsignedWrap | OBO::NoSignedWrap);
867       },
868       [](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
869         bool IsOverflow1, IsOverflow2;
870         APInt Res1 = N1.uadd_ov(N2, IsOverflow1);
871         APInt Res2 = N1.sadd_ov(N2, IsOverflow2);
872         if (IsOverflow1 || IsOverflow2)
873           return std::nullopt;
874         assert(Res1 == Res2 && "Addition results differ?");
875         return Res1;
876       },
877       PreferSmallest, CheckNonWrappedOrSignWrappedOnly);
878 }
879 
880 TEST_F(ConstantRangeTest, Sub) {
881   EXPECT_EQ(Full.sub(APInt(16, 4)), Full);
882   EXPECT_EQ(Full.sub(Full), Full);
883   EXPECT_EQ(Full.sub(Empty), Empty);
884   EXPECT_EQ(Full.sub(One), Full);
885   EXPECT_EQ(Full.sub(Some), Full);
886   EXPECT_EQ(Full.sub(Wrap), Full);
887   EXPECT_EQ(Empty.sub(Empty), Empty);
888   EXPECT_EQ(Empty.sub(One), Empty);
889   EXPECT_EQ(Empty.sub(Some), Empty);
890   EXPECT_EQ(Empty.sub(Wrap), Empty);
891   EXPECT_EQ(Empty.sub(APInt(16, 4)), Empty);
892   EXPECT_EQ(Some.sub(APInt(16, 4)),
893             ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
894   EXPECT_EQ(Some.sub(Some),
895             ConstantRange(APInt(16, 0xf561), APInt(16, 0xaa0)));
896   EXPECT_EQ(Wrap.sub(APInt(16, 4)),
897             ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
898   EXPECT_EQ(One.sub(APInt(16, 4)),
899             ConstantRange(APInt(16, 0x6)));
900 
901   TestBinaryOpExhaustive(
902       [](const ConstantRange &CR1, const ConstantRange &CR2) {
903         return CR1.sub(CR2);
904       },
905       [](const APInt &N1, const APInt &N2) {
906         return N1 - N2;
907       });
908 }
909 
910 TEST_F(ConstantRangeTest, SubWithNoWrap) {
911   typedef OverflowingBinaryOperator OBO;
912   TestBinaryOpExhaustive(
913       [](const ConstantRange &CR1, const ConstantRange &CR2) {
914         return CR1.subWithNoWrap(CR2, OBO::NoSignedWrap);
915       },
916       [](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
917         bool IsOverflow;
918         APInt Res = N1.ssub_ov(N2, IsOverflow);
919         if (IsOverflow)
920           return std::nullopt;
921         return Res;
922       },
923       PreferSmallest, CheckNonSignWrappedOnly);
924   TestBinaryOpExhaustive(
925       [](const ConstantRange &CR1, const ConstantRange &CR2) {
926         return CR1.subWithNoWrap(CR2, OBO::NoUnsignedWrap);
927       },
928       [](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
929         bool IsOverflow;
930         APInt Res = N1.usub_ov(N2, IsOverflow);
931         if (IsOverflow)
932           return std::nullopt;
933         return Res;
934       },
935       PreferSmallest, CheckNonWrappedOnly);
936   TestBinaryOpExhaustive(
937       [](const ConstantRange &CR1, const ConstantRange &CR2) {
938         return CR1.subWithNoWrap(CR2, OBO::NoUnsignedWrap | OBO::NoSignedWrap);
939       },
940       [](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
941         bool IsOverflow1, IsOverflow2;
942         APInt Res1 = N1.usub_ov(N2, IsOverflow1);
943         APInt Res2 = N1.ssub_ov(N2, IsOverflow2);
944         if (IsOverflow1 || IsOverflow2)
945           return std::nullopt;
946         assert(Res1 == Res2 && "Subtraction results differ?");
947         return Res1;
948       },
949       PreferSmallest, CheckNonWrappedOrSignWrappedOnly);
950 }
951 
952 TEST_F(ConstantRangeTest, Multiply) {
953   EXPECT_EQ(Full.multiply(Full), Full);
954   EXPECT_EQ(Full.multiply(Empty), Empty);
955   EXPECT_EQ(Full.multiply(One), Full);
956   EXPECT_EQ(Full.multiply(Some), Full);
957   EXPECT_EQ(Full.multiply(Wrap), Full);
958   EXPECT_EQ(Empty.multiply(Empty), Empty);
959   EXPECT_EQ(Empty.multiply(One), Empty);
960   EXPECT_EQ(Empty.multiply(Some), Empty);
961   EXPECT_EQ(Empty.multiply(Wrap), Empty);
962   EXPECT_EQ(One.multiply(One), ConstantRange(APInt(16, 0xa*0xa),
963                                              APInt(16, 0xa*0xa + 1)));
964   EXPECT_EQ(One.multiply(Some), ConstantRange(APInt(16, 0xa*0xa),
965                                               APInt(16, 0xa*0xaa9 + 1)));
966   EXPECT_EQ(One.multiply(Wrap), Full);
967   EXPECT_EQ(Some.multiply(Some), Full);
968   EXPECT_EQ(Some.multiply(Wrap), Full);
969   EXPECT_EQ(Wrap.multiply(Wrap), Full);
970 
971   ConstantRange Zero(APInt(16, 0));
972   EXPECT_EQ(Zero.multiply(Full), Zero);
973   EXPECT_EQ(Zero.multiply(Some), Zero);
974   EXPECT_EQ(Zero.multiply(Wrap), Zero);
975   EXPECT_EQ(Full.multiply(Zero), Zero);
976   EXPECT_EQ(Some.multiply(Zero), Zero);
977   EXPECT_EQ(Wrap.multiply(Zero), Zero);
978 
979   // http://llvm.org/PR4545
980   EXPECT_EQ(ConstantRange(APInt(4, 1), APInt(4, 6)).multiply(
981                 ConstantRange(APInt(4, 6), APInt(4, 2))),
982             ConstantRange(4, /*isFullSet=*/true));
983 
984   EXPECT_EQ(ConstantRange(APInt(8, 254), APInt(8, 0)).multiply(
985               ConstantRange(APInt(8, 252), APInt(8, 4))),
986             ConstantRange(APInt(8, 250), APInt(8, 9)));
987   EXPECT_EQ(ConstantRange(APInt(8, 254), APInt(8, 255)).multiply(
988               ConstantRange(APInt(8, 2), APInt(8, 4))),
989             ConstantRange(APInt(8, 250), APInt(8, 253)));
990 
991   // TODO: This should be return [-2, 0]
992   EXPECT_EQ(ConstantRange(APInt(8, -2)).multiply(
993               ConstantRange(APInt(8, 0), APInt(8, 2))),
994             ConstantRange(APInt(8, -2), APInt(8, 1)));
995 }
996 
997 TEST_F(ConstantRangeTest, smul_fast) {
998   TestBinaryOpExhaustive(
999       [](const ConstantRange &CR1, const ConstantRange &CR2) {
1000         return CR1.smul_fast(CR2);
1001       },
1002       [](const APInt &N1, const APInt &N2) {
1003         return N1 * N2;
1004       },
1005       PreferSmallest,
1006       [](const ConstantRange &, const ConstantRange &) {
1007         return false; // Check correctness only.
1008       });
1009 }
1010 
1011 TEST_F(ConstantRangeTest, UMax) {
1012   EXPECT_EQ(Full.umax(Full), Full);
1013   EXPECT_EQ(Full.umax(Empty), Empty);
1014   EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
1015   EXPECT_EQ(Full.umax(Wrap), Full);
1016   EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
1017   EXPECT_EQ(Empty.umax(Empty), Empty);
1018   EXPECT_EQ(Empty.umax(Some), Empty);
1019   EXPECT_EQ(Empty.umax(Wrap), Empty);
1020   EXPECT_EQ(Empty.umax(One), Empty);
1021   EXPECT_EQ(Some.umax(Some), Some);
1022   EXPECT_EQ(Some.umax(Wrap), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
1023   EXPECT_EQ(Some.umax(One), Some);
1024   EXPECT_EQ(Wrap.umax(Wrap), Wrap);
1025   EXPECT_EQ(Wrap.umax(One), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
1026   EXPECT_EQ(One.umax(One), One);
1027 
1028   TestBinaryOpExhaustive(
1029       [](const ConstantRange &CR1, const ConstantRange &CR2) {
1030         return CR1.umax(CR2);
1031       },
1032       [](const APInt &N1, const APInt &N2) {
1033         return APIntOps::umax(N1, N2);
1034       },
1035       PreferSmallestNonFullUnsigned);
1036 }
1037 
1038 TEST_F(ConstantRangeTest, SMax) {
1039   EXPECT_EQ(Full.smax(Full), Full);
1040   EXPECT_EQ(Full.smax(Empty), Empty);
1041   EXPECT_EQ(Full.smax(Some), ConstantRange(APInt(16, 0xa),
1042                                            APInt::getSignedMinValue(16)));
1043   EXPECT_EQ(Full.smax(Wrap), Full);
1044   EXPECT_EQ(Full.smax(One), ConstantRange(APInt(16, 0xa),
1045                                           APInt::getSignedMinValue(16)));
1046   EXPECT_EQ(Empty.smax(Empty), Empty);
1047   EXPECT_EQ(Empty.smax(Some), Empty);
1048   EXPECT_EQ(Empty.smax(Wrap), Empty);
1049   EXPECT_EQ(Empty.smax(One), Empty);
1050   EXPECT_EQ(Some.smax(Some), Some);
1051   EXPECT_EQ(Some.smax(Wrap), ConstantRange(APInt(16, 0xa),
1052                                            APInt(16, (uint64_t)INT16_MIN)));
1053   EXPECT_EQ(Some.smax(One), Some);
1054   EXPECT_EQ(Wrap.smax(One), ConstantRange(APInt(16, 0xa),
1055                                           APInt(16, (uint64_t)INT16_MIN)));
1056   EXPECT_EQ(One.smax(One), One);
1057 
1058   TestBinaryOpExhaustive(
1059       [](const ConstantRange &CR1, const ConstantRange &CR2) {
1060         return CR1.smax(CR2);
1061       },
1062       [](const APInt &N1, const APInt &N2) {
1063         return APIntOps::smax(N1, N2);
1064       },
1065       PreferSmallestNonFullSigned);
1066 }
1067 
1068 TEST_F(ConstantRangeTest, UMin) {
1069   EXPECT_EQ(Full.umin(Full), Full);
1070   EXPECT_EQ(Full.umin(Empty), Empty);
1071   EXPECT_EQ(Full.umin(Some), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
1072   EXPECT_EQ(Full.umin(Wrap), Full);
1073   EXPECT_EQ(Empty.umin(Empty), Empty);
1074   EXPECT_EQ(Empty.umin(Some), Empty);
1075   EXPECT_EQ(Empty.umin(Wrap), Empty);
1076   EXPECT_EQ(Empty.umin(One), Empty);
1077   EXPECT_EQ(Some.umin(Some), Some);
1078   EXPECT_EQ(Some.umin(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
1079   EXPECT_EQ(Some.umin(One), One);
1080   EXPECT_EQ(Wrap.umin(Wrap), Wrap);
1081   EXPECT_EQ(Wrap.umin(One), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
1082   EXPECT_EQ(One.umin(One), One);
1083 
1084   TestBinaryOpExhaustive(
1085       [](const ConstantRange &CR1, const ConstantRange &CR2) {
1086         return CR1.umin(CR2);
1087       },
1088       [](const APInt &N1, const APInt &N2) {
1089         return APIntOps::umin(N1, N2);
1090       },
1091       PreferSmallestNonFullUnsigned);
1092 }
1093 
1094 TEST_F(ConstantRangeTest, SMin) {
1095   EXPECT_EQ(Full.smin(Full), Full);
1096   EXPECT_EQ(Full.smin(Empty), Empty);
1097   EXPECT_EQ(Full.smin(Some), ConstantRange(APInt(16, (uint64_t)INT16_MIN),
1098                                            APInt(16, 0xaaa)));
1099   EXPECT_EQ(Full.smin(Wrap), Full);
1100   EXPECT_EQ(Empty.smin(Empty), Empty);
1101   EXPECT_EQ(Empty.smin(Some), Empty);
1102   EXPECT_EQ(Empty.smin(Wrap), Empty);
1103   EXPECT_EQ(Empty.smin(One), Empty);
1104   EXPECT_EQ(Some.smin(Some), Some);
1105   EXPECT_EQ(Some.smin(Wrap), ConstantRange(APInt(16, (uint64_t)INT16_MIN),
1106                                            APInt(16, 0xaaa)));
1107   EXPECT_EQ(Some.smin(One), One);
1108   EXPECT_EQ(Wrap.smin(Wrap), Wrap);
1109   EXPECT_EQ(Wrap.smin(One), ConstantRange(APInt(16, (uint64_t)INT16_MIN),
1110                                           APInt(16, 0xb)));
1111   EXPECT_EQ(One.smin(One), One);
1112 
1113   TestBinaryOpExhaustive(
1114       [](const ConstantRange &CR1, const ConstantRange &CR2) {
1115         return CR1.smin(CR2);
1116       },
1117       [](const APInt &N1, const APInt &N2) {
1118         return APIntOps::smin(N1, N2);
1119       },
1120       PreferSmallestNonFullSigned);
1121 }
1122 
1123 TEST_F(ConstantRangeTest, UDiv) {
1124   EXPECT_EQ(Full.udiv(Full), Full);
1125   EXPECT_EQ(Full.udiv(Empty), Empty);
1126   EXPECT_EQ(Full.udiv(One), ConstantRange(APInt(16, 0),
1127                                           APInt(16, 0xffff / 0xa + 1)));
1128   EXPECT_EQ(Full.udiv(Some), ConstantRange(APInt(16, 0),
1129                                            APInt(16, 0xffff / 0xa + 1)));
1130   EXPECT_EQ(Full.udiv(Wrap), Full);
1131   EXPECT_EQ(Empty.udiv(Empty), Empty);
1132   EXPECT_EQ(Empty.udiv(One), Empty);
1133   EXPECT_EQ(Empty.udiv(Some), Empty);
1134   EXPECT_EQ(Empty.udiv(Wrap), Empty);
1135   EXPECT_EQ(One.udiv(One), ConstantRange(APInt(16, 1)));
1136   EXPECT_EQ(One.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 2)));
1137   EXPECT_EQ(One.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
1138   EXPECT_EQ(Some.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 0x111)));
1139   EXPECT_EQ(Some.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
1140   EXPECT_EQ(Wrap.udiv(Wrap), Full);
1141 
1142 
1143   ConstantRange Zero(APInt(16, 0));
1144   EXPECT_EQ(Zero.udiv(One), Zero);
1145   EXPECT_EQ(Zero.udiv(Full), Zero);
1146 
1147   EXPECT_EQ(ConstantRange(APInt(16, 0), APInt(16, 99)).udiv(Full),
1148             ConstantRange(APInt(16, 0), APInt(16, 99)));
1149   EXPECT_EQ(ConstantRange(APInt(16, 10), APInt(16, 99)).udiv(Full),
1150             ConstantRange(APInt(16, 0), APInt(16, 99)));
1151 }
1152 
1153 TEST_F(ConstantRangeTest, SDiv) {
1154   ConstantRange OneBit = ConstantRange::getFull(1);
1155   EXPECT_EQ(OneBit.sdiv(OneBit), ConstantRange(APInt(1, 0)));
1156 
1157   unsigned Bits = 4;
1158   EnumerateTwoConstantRanges(Bits, [&](const ConstantRange &CR1,
1159                                        const ConstantRange &CR2) {
1160     // Collect possible results in a bit vector. We store the signed value plus
1161     // a bias to make it unsigned.
1162     int Bias = 1 << (Bits - 1);
1163     BitVector Results(1 << Bits);
1164     ForeachNumInConstantRange(CR1, [&](const APInt &N1) {
1165       ForeachNumInConstantRange(CR2, [&](const APInt &N2) {
1166         // Division by zero is UB.
1167         if (N2 == 0)
1168           return;
1169 
1170         // SignedMin / -1 is UB.
1171         if (N1.isMinSignedValue() && N2.isAllOnes())
1172           return;
1173 
1174         APInt N = N1.sdiv(N2);
1175         Results.set(N.getSExtValue() + Bias);
1176       });
1177     });
1178 
1179     ConstantRange CR = CR1.sdiv(CR2);
1180     if (Results.none()) {
1181       EXPECT_TRUE(CR.isEmptySet());
1182       return;
1183     }
1184 
1185     // If there is a non-full signed envelope, that should be the result.
1186     APInt SMin(Bits, Results.find_first() - Bias);
1187     APInt SMax(Bits, Results.find_last() - Bias);
1188     ConstantRange Envelope = ConstantRange::getNonEmpty(SMin, SMax + 1);
1189     if (!Envelope.isFullSet()) {
1190       EXPECT_EQ(Envelope, CR);
1191       return;
1192     }
1193 
1194     // If the signed envelope is a full set, try to find a smaller sign wrapped
1195     // set that is separated in negative and positive components (or one which
1196     // can also additionally contain zero).
1197     int LastNeg = Results.find_last_in(0, Bias) - Bias;
1198     int LastPos = Results.find_next(Bias) - Bias;
1199     if (Results[Bias]) {
1200       if (LastNeg == -1)
1201         ++LastNeg;
1202       else if (LastPos == 1)
1203         --LastPos;
1204     }
1205 
1206     APInt WMax(Bits, LastNeg);
1207     APInt WMin(Bits, LastPos);
1208     ConstantRange Wrapped = ConstantRange::getNonEmpty(WMin, WMax + 1);
1209     EXPECT_EQ(Wrapped, CR);
1210   });
1211 }
1212 
1213 TEST_F(ConstantRangeTest, URem) {
1214   EXPECT_EQ(Full.urem(Empty), Empty);
1215   EXPECT_EQ(Empty.urem(Full), Empty);
1216   // urem by zero is poison.
1217   EXPECT_EQ(Full.urem(ConstantRange(APInt(16, 0))), Empty);
1218   // urem by full range doesn't contain MaxValue.
1219   EXPECT_EQ(Full.urem(Full), ConstantRange(APInt(16, 0), APInt(16, 0xffff)));
1220   // urem is upper bounded by maximum RHS minus one.
1221   EXPECT_EQ(Full.urem(ConstantRange(APInt(16, 0), APInt(16, 123))),
1222             ConstantRange(APInt(16, 0), APInt(16, 122)));
1223   // urem is upper bounded by maximum LHS.
1224   EXPECT_EQ(ConstantRange(APInt(16, 0), APInt(16, 123)).urem(Full),
1225             ConstantRange(APInt(16, 0), APInt(16, 123)));
1226   // If the LHS is always lower than the RHS, the result is the LHS.
1227   EXPECT_EQ(ConstantRange(APInt(16, 10), APInt(16, 20))
1228                 .urem(ConstantRange(APInt(16, 20), APInt(16, 30))),
1229             ConstantRange(APInt(16, 10), APInt(16, 20)));
1230   // It has to be strictly lower, otherwise the top value may wrap to zero.
1231   EXPECT_EQ(ConstantRange(APInt(16, 10), APInt(16, 20))
1232                 .urem(ConstantRange(APInt(16, 19), APInt(16, 30))),
1233             ConstantRange(APInt(16, 0), APInt(16, 20)));
1234   // [12, 14] % 10 is [2, 4], but we conservatively compute [0, 9].
1235   EXPECT_EQ(ConstantRange(APInt(16, 12), APInt(16, 15))
1236                 .urem(ConstantRange(APInt(16, 10))),
1237             ConstantRange(APInt(16, 0), APInt(16, 10)));
1238 
1239   TestBinaryOpExhaustive(
1240       [](const ConstantRange &CR1, const ConstantRange &CR2) {
1241         return CR1.urem(CR2);
1242       },
1243       [](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
1244         if (N2.isZero())
1245           return std::nullopt;
1246         return N1.urem(N2);
1247       },
1248       PreferSmallest, CheckSingleElementsOnly);
1249 }
1250 
1251 TEST_F(ConstantRangeTest, SRem) {
1252   EXPECT_EQ(Full.srem(Empty), Empty);
1253   EXPECT_EQ(Empty.srem(Full), Empty);
1254   // srem by zero is UB.
1255   EXPECT_EQ(Full.srem(ConstantRange(APInt(16, 0))), Empty);
1256   // srem by full range doesn't contain SignedMinValue.
1257   EXPECT_EQ(Full.srem(Full), ConstantRange(APInt::getSignedMinValue(16) + 1,
1258                                            APInt::getSignedMinValue(16)));
1259 
1260   ConstantRange PosMod(APInt(16, 10), APInt(16, 21));  // [10, 20]
1261   ConstantRange NegMod(APInt(16, -20), APInt(16, -9)); // [-20, -10]
1262   ConstantRange IntMinMod(APInt::getSignedMinValue(16));
1263 
1264   ConstantRange Expected(16, true);
1265 
1266   // srem is bounded by abs(RHS) minus one.
1267   ConstantRange PosLargeLHS(APInt(16, 0), APInt(16, 41));
1268   Expected = ConstantRange(APInt(16, 0), APInt(16, 20));
1269   EXPECT_EQ(PosLargeLHS.srem(PosMod), Expected);
1270   EXPECT_EQ(PosLargeLHS.srem(NegMod), Expected);
1271   ConstantRange NegLargeLHS(APInt(16, -40), APInt(16, 1));
1272   Expected = ConstantRange(APInt(16, -19), APInt(16, 1));
1273   EXPECT_EQ(NegLargeLHS.srem(PosMod), Expected);
1274   EXPECT_EQ(NegLargeLHS.srem(NegMod), Expected);
1275   ConstantRange PosNegLargeLHS(APInt(16, -32), APInt(16, 38));
1276   Expected = ConstantRange(APInt(16, -19), APInt(16, 20));
1277   EXPECT_EQ(PosNegLargeLHS.srem(PosMod), Expected);
1278   EXPECT_EQ(PosNegLargeLHS.srem(NegMod), Expected);
1279 
1280   // srem is bounded by LHS.
1281   ConstantRange PosLHS(APInt(16, 0), APInt(16, 16));
1282   EXPECT_EQ(PosLHS.srem(PosMod), PosLHS);
1283   EXPECT_EQ(PosLHS.srem(NegMod), PosLHS);
1284   EXPECT_EQ(PosLHS.srem(IntMinMod), PosLHS);
1285   ConstantRange NegLHS(APInt(16, -15), APInt(16, 1));
1286   EXPECT_EQ(NegLHS.srem(PosMod), NegLHS);
1287   EXPECT_EQ(NegLHS.srem(NegMod), NegLHS);
1288   EXPECT_EQ(NegLHS.srem(IntMinMod), NegLHS);
1289   ConstantRange PosNegLHS(APInt(16, -12), APInt(16, 18));
1290   EXPECT_EQ(PosNegLHS.srem(PosMod), PosNegLHS);
1291   EXPECT_EQ(PosNegLHS.srem(NegMod), PosNegLHS);
1292   EXPECT_EQ(PosNegLHS.srem(IntMinMod), PosNegLHS);
1293 
1294   // srem is LHS if it is smaller than RHS.
1295   ConstantRange PosSmallLHS(APInt(16, 3), APInt(16, 8));
1296   EXPECT_EQ(PosSmallLHS.srem(PosMod), PosSmallLHS);
1297   EXPECT_EQ(PosSmallLHS.srem(NegMod), PosSmallLHS);
1298   EXPECT_EQ(PosSmallLHS.srem(IntMinMod), PosSmallLHS);
1299   ConstantRange NegSmallLHS(APInt(16, -7), APInt(16, -2));
1300   EXPECT_EQ(NegSmallLHS.srem(PosMod), NegSmallLHS);
1301   EXPECT_EQ(NegSmallLHS.srem(NegMod), NegSmallLHS);
1302   EXPECT_EQ(NegSmallLHS.srem(IntMinMod), NegSmallLHS);
1303   ConstantRange PosNegSmallLHS(APInt(16, -3), APInt(16, 8));
1304   EXPECT_EQ(PosNegSmallLHS.srem(PosMod), PosNegSmallLHS);
1305   EXPECT_EQ(PosNegSmallLHS.srem(NegMod), PosNegSmallLHS);
1306   EXPECT_EQ(PosNegSmallLHS.srem(IntMinMod), PosNegSmallLHS);
1307 
1308   // Example of a suboptimal result:
1309   // [12, 14] srem 10 is [2, 4], but we conservatively compute [0, 9].
1310   EXPECT_EQ(ConstantRange(APInt(16, 12), APInt(16, 15))
1311                 .srem(ConstantRange(APInt(16, 10))),
1312             ConstantRange(APInt(16, 0), APInt(16, 10)));
1313 
1314   TestBinaryOpExhaustive(
1315       [](const ConstantRange &CR1, const ConstantRange &CR2) {
1316         return CR1.srem(CR2);
1317       },
1318       [](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
1319         if (N2.isZero())
1320           return std::nullopt;
1321         return N1.srem(N2);
1322       },
1323       PreferSmallest, CheckSingleElementsOnly);
1324 }
1325 
1326 TEST_F(ConstantRangeTest, Shl) {
1327   ConstantRange Some2(APInt(16, 0xfff), APInt(16, 0x8000));
1328   ConstantRange WrapNullMax(APInt(16, 0x1), APInt(16, 0x0));
1329   EXPECT_EQ(Full.shl(Full), Full);
1330   EXPECT_EQ(Full.shl(Empty), Empty);
1331   EXPECT_EQ(Full.shl(One), ConstantRange(APInt(16, 0),
1332                                          APInt(16, 0xfc00) + 1));
1333   EXPECT_EQ(Full.shl(Some), Full);   // TODO: [0, (-1 << 0xa) + 1)
1334   EXPECT_EQ(Full.shl(Wrap), Full);
1335   EXPECT_EQ(Empty.shl(Empty), Empty);
1336   EXPECT_EQ(Empty.shl(One), Empty);
1337   EXPECT_EQ(Empty.shl(Some), Empty);
1338   EXPECT_EQ(Empty.shl(Wrap), Empty);
1339   EXPECT_EQ(One.shl(One), ConstantRange(APInt(16, 0xa << 0xa),
1340                                         APInt(16, (0xa << 0xa) + 1)));
1341   EXPECT_EQ(One.shl(Some), Full);    // TODO: [0xa << 0xa, 0)
1342   EXPECT_EQ(One.shl(Wrap), Full);    // TODO: [0xa, 0xa << 14 + 1)
1343   EXPECT_EQ(Some.shl(Some), Full);   // TODO: [0xa << 0xa, 0xfc01)
1344   EXPECT_EQ(Some.shl(Wrap), Full);   // TODO: [0xa, 0x7ff << 0x5 + 1)
1345   EXPECT_EQ(Wrap.shl(Wrap), Full);
1346   EXPECT_EQ(
1347       Some2.shl(ConstantRange(APInt(16, 0x1))),
1348       ConstantRange(APInt(16, 0xfff << 0x1), APInt(16, 0x7fff << 0x1) + 1));
1349   EXPECT_EQ(One.shl(WrapNullMax), Full);
1350 
1351   TestBinaryOpExhaustive(
1352       [](const ConstantRange &CR1, const ConstantRange &CR2) {
1353         return CR1.shl(CR2);
1354       },
1355       [](const APInt &N1, const APInt &N2) -> std::optional<APInt> {
1356         if (N2.uge(N2.getBitWidth()))
1357           return std::nullopt;
1358         return N1.shl(N2);
1359       },
1360       PreferSmallestUnsigned,
1361       [](const ConstantRange &, const ConstantRange &CR2) {
1362         // We currently only produce precise results for single element RHS.
1363         return CR2.isSingleElement();
1364       });
1365 }
1366 
1367 TEST_F(ConstantRangeTest, Lshr) {
1368   EXPECT_EQ(Full.lshr(Full), Full);
1369   EXPECT_EQ(Full.lshr(Empty), Empty);
1370   EXPECT_EQ(Full.lshr(One), ConstantRange(APInt(16, 0),
1371                                           APInt(16, (0xffff >> 0xa) + 1)));
1372   EXPECT_EQ(Full.lshr(Some), ConstantRange(APInt(16, 0),
1373                                            APInt(16, (0xffff >> 0xa) + 1)));
1374   EXPECT_EQ(Full.lshr(Wrap), Full);
1375   EXPECT_EQ(Empty.lshr(Empty), Empty);
1376   EXPECT_EQ(Empty.lshr(One), Empty);
1377   EXPECT_EQ(Empty.lshr(Some), Empty);
1378   EXPECT_EQ(Empty.lshr(Wrap), Empty);
1379   EXPECT_EQ(One.lshr(One), ConstantRange(APInt(16, 0)));
1380   EXPECT_EQ(One.lshr(Some), ConstantRange(APInt(16, 0)));
1381   EXPECT_EQ(One.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
1382   EXPECT_EQ(Some.lshr(Some), ConstantRange(APInt(16, 0),
1383                                            APInt(16, (0xaaa >> 0xa) + 1)));
1384   EXPECT_EQ(Some.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
1385   EXPECT_EQ(Wrap.lshr(Wrap), Full);
1386 }
1387 
1388 TEST_F(ConstantRangeTest, Ashr) {
1389   EXPECT_EQ(Full.ashr(Full), Full);
1390   EXPECT_EQ(Full.ashr(Empty), Empty);
1391   EXPECT_EQ(Full.ashr(One), ConstantRange(APInt(16, 0xffe0),
1392                                           APInt(16, (0x7fff >> 0xa) + 1 )));
1393   ConstantRange Small(APInt(16, 0xa), APInt(16, 0xb));
1394   EXPECT_EQ(Full.ashr(Small), ConstantRange(APInt(16, 0xffe0),
1395                                            APInt(16, (0x7fff >> 0xa) + 1 )));
1396   EXPECT_EQ(Full.ashr(Some), ConstantRange(APInt(16, 0xffe0),
1397                                            APInt(16, (0x7fff >> 0xa) + 1 )));
1398   EXPECT_EQ(Full.ashr(Wrap), Full);
1399   EXPECT_EQ(Empty.ashr(Empty), Empty);
1400   EXPECT_EQ(Empty.ashr(One), Empty);
1401   EXPECT_EQ(Empty.ashr(Some), Empty);
1402   EXPECT_EQ(Empty.ashr(Wrap), Empty);
1403   EXPECT_EQ(One.ashr(One), ConstantRange(APInt(16, 0)));
1404   EXPECT_EQ(One.ashr(Some), ConstantRange(APInt(16, 0)));
1405   EXPECT_EQ(One.ashr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
1406   EXPECT_EQ(Some.ashr(Some), ConstantRange(APInt(16, 0),
1407                                            APInt(16, (0xaaa >> 0xa) + 1)));
1408   EXPECT_EQ(Some.ashr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
1409   EXPECT_EQ(Wrap.ashr(Wrap), Full);
1410   ConstantRange Neg(APInt(16, 0xf3f0, true), APInt(16, 0xf7f8, true));
1411   EXPECT_EQ(Neg.ashr(Small), ConstantRange(APInt(16, 0xfffc, true),
1412                                            APInt(16, 0xfffe, true)));
1413 }
1414 
1415 TEST(ConstantRange, MakeAllowedICmpRegion) {
1416   // PR8250
1417   ConstantRange SMax = ConstantRange(APInt::getSignedMaxValue(32));
1418   EXPECT_TRUE(ConstantRange::makeAllowedICmpRegion(ICmpInst::ICMP_SGT, SMax)
1419                   .isEmptySet());
1420 }
1421 
1422 TEST(ConstantRange, MakeSatisfyingICmpRegion) {
1423   ConstantRange LowHalf(APInt(8, 0), APInt(8, 128));
1424   ConstantRange HighHalf(APInt(8, 128), APInt(8, 0));
1425   ConstantRange EmptySet(8, /* isFullSet = */ false);
1426 
1427   EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_NE, LowHalf),
1428             HighHalf);
1429 
1430   EXPECT_EQ(
1431       ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_NE, HighHalf),
1432       LowHalf);
1433 
1434   EXPECT_TRUE(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_EQ,
1435                                                       HighHalf).isEmptySet());
1436 
1437   ConstantRange UnsignedSample(APInt(8, 5), APInt(8, 200));
1438 
1439   EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_ULT,
1440                                                     UnsignedSample),
1441             ConstantRange(APInt(8, 0), APInt(8, 5)));
1442 
1443   EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_ULE,
1444                                                     UnsignedSample),
1445             ConstantRange(APInt(8, 0), APInt(8, 6)));
1446 
1447   EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_UGT,
1448                                                     UnsignedSample),
1449             ConstantRange(APInt(8, 200), APInt(8, 0)));
1450 
1451   EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_UGE,
1452                                                     UnsignedSample),
1453             ConstantRange(APInt(8, 199), APInt(8, 0)));
1454 
1455   ConstantRange SignedSample(APInt(8, -5), APInt(8, 5));
1456 
1457   EXPECT_EQ(
1458       ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SLT, SignedSample),
1459       ConstantRange(APInt(8, -128), APInt(8, -5)));
1460 
1461   EXPECT_EQ(
1462       ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SLE, SignedSample),
1463       ConstantRange(APInt(8, -128), APInt(8, -4)));
1464 
1465   EXPECT_EQ(
1466       ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SGT, SignedSample),
1467       ConstantRange(APInt(8, 5), APInt(8, -128)));
1468 
1469   EXPECT_EQ(
1470       ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SGE, SignedSample),
1471       ConstantRange(APInt(8, 4), APInt(8, -128)));
1472 }
1473 
1474 void ICmpTestImpl(CmpInst::Predicate Pred) {
1475   unsigned Bits = 4;
1476   EnumerateTwoConstantRanges(
1477       Bits, [&](const ConstantRange &CR1, const ConstantRange &CR2) {
1478         bool Exhaustive = true;
1479         ForeachNumInConstantRange(CR1, [&](const APInt &N1) {
1480           ForeachNumInConstantRange(CR2, [&](const APInt &N2) {
1481             Exhaustive &= ICmpInst::compare(N1, N2, Pred);
1482           });
1483         });
1484         EXPECT_EQ(CR1.icmp(Pred, CR2), Exhaustive);
1485       });
1486 }
1487 
1488 TEST(ConstantRange, ICmp) {
1489   for (auto Pred : ICmpInst::predicates())
1490     ICmpTestImpl(Pred);
1491 }
1492 
1493 TEST(ConstantRange, MakeGuaranteedNoWrapRegion) {
1494   const int IntMin4Bits = 8;
1495   const int IntMax4Bits = 7;
1496   typedef OverflowingBinaryOperator OBO;
1497 
1498   for (int Const : {0, -1, -2, 1, 2, IntMin4Bits, IntMax4Bits}) {
1499     APInt C(4, Const, true /* = isSigned */);
1500 
1501     auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
1502         Instruction::Add, C, OBO::NoUnsignedWrap);
1503 
1504     EXPECT_FALSE(NUWRegion.isEmptySet());
1505 
1506     auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
1507         Instruction::Add, C, OBO::NoSignedWrap);
1508 
1509     EXPECT_FALSE(NSWRegion.isEmptySet());
1510 
1511     for (APInt I = NUWRegion.getLower(), E = NUWRegion.getUpper(); I != E;
1512          ++I) {
1513       bool Overflow = false;
1514       (void)I.uadd_ov(C, Overflow);
1515       EXPECT_FALSE(Overflow);
1516     }
1517 
1518     for (APInt I = NSWRegion.getLower(), E = NSWRegion.getUpper(); I != E;
1519          ++I) {
1520       bool Overflow = false;
1521       (void)I.sadd_ov(C, Overflow);
1522       EXPECT_FALSE(Overflow);
1523     }
1524   }
1525 
1526   for (int Const : {0, -1, -2, 1, 2, IntMin4Bits, IntMax4Bits}) {
1527     APInt C(4, Const, true /* = isSigned */);
1528 
1529     auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
1530         Instruction::Sub, C, OBO::NoUnsignedWrap);
1531 
1532     EXPECT_FALSE(NUWRegion.isEmptySet());
1533 
1534     auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
1535         Instruction::Sub, C, OBO::NoSignedWrap);
1536 
1537     EXPECT_FALSE(NSWRegion.isEmptySet());
1538 
1539     for (APInt I = NUWRegion.getLower(), E = NUWRegion.getUpper(); I != E;
1540          ++I) {
1541       bool Overflow = false;
1542       (void)I.usub_ov(C, Overflow);
1543       EXPECT_FALSE(Overflow);
1544     }
1545 
1546     for (APInt I = NSWRegion.getLower(), E = NSWRegion.getUpper(); I != E;
1547          ++I) {
1548       bool Overflow = false;
1549       (void)I.ssub_ov(C, Overflow);
1550       EXPECT_FALSE(Overflow);
1551     }
1552   }
1553 
1554   auto NSWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
1555       Instruction::Add, ConstantRange(32, /* isFullSet = */ true),
1556       OBO::NoSignedWrap);
1557   EXPECT_TRUE(NSWForAllValues.isSingleElement() &&
1558               NSWForAllValues.getSingleElement()->isMinValue());
1559 
1560   NSWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
1561       Instruction::Sub, ConstantRange(32, /* isFullSet = */ true),
1562       OBO::NoSignedWrap);
1563   EXPECT_TRUE(NSWForAllValues.isSingleElement() &&
1564               NSWForAllValues.getSingleElement()->isMaxValue());
1565 
1566   auto NUWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
1567       Instruction::Add, ConstantRange(32, /* isFullSet = */ true),
1568       OBO::NoUnsignedWrap);
1569   EXPECT_TRUE(NUWForAllValues.isSingleElement() &&
1570               NUWForAllValues.getSingleElement()->isMinValue());
1571 
1572   NUWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
1573       Instruction::Sub, ConstantRange(32, /* isFullSet = */ true),
1574       OBO::NoUnsignedWrap);
1575   EXPECT_TRUE(NUWForAllValues.isSingleElement() &&
1576               NUWForAllValues.getSingleElement()->isMaxValue());
1577 
1578   EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion(
1579       Instruction::Add, APInt(32, 0), OBO::NoUnsignedWrap).isFullSet());
1580   EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion(
1581       Instruction::Add, APInt(32, 0), OBO::NoSignedWrap).isFullSet());
1582   EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion(
1583       Instruction::Sub, APInt(32, 0), OBO::NoUnsignedWrap).isFullSet());
1584   EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion(
1585       Instruction::Sub, APInt(32, 0), OBO::NoSignedWrap).isFullSet());
1586 
1587   ConstantRange OneToFive(APInt(32, 1), APInt(32, 6));
1588   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1589                 Instruction::Add, OneToFive, OBO::NoSignedWrap),
1590             ConstantRange(APInt::getSignedMinValue(32),
1591                           APInt::getSignedMaxValue(32) - 4));
1592   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1593                 Instruction::Add, OneToFive, OBO::NoUnsignedWrap),
1594             ConstantRange(APInt::getMinValue(32), APInt::getMinValue(32) - 5));
1595   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1596                 Instruction::Sub, OneToFive, OBO::NoSignedWrap),
1597             ConstantRange(APInt::getSignedMinValue(32) + 5,
1598                           APInt::getSignedMinValue(32)));
1599   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1600                 Instruction::Sub, OneToFive, OBO::NoUnsignedWrap),
1601             ConstantRange(APInt::getMinValue(32) + 5, APInt::getMinValue(32)));
1602 
1603   ConstantRange MinusFiveToMinusTwo(APInt(32, -5), APInt(32, -1));
1604   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1605                 Instruction::Add, MinusFiveToMinusTwo, OBO::NoSignedWrap),
1606             ConstantRange(APInt::getSignedMinValue(32) + 5,
1607                           APInt::getSignedMinValue(32)));
1608   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1609                 Instruction::Add, MinusFiveToMinusTwo, OBO::NoUnsignedWrap),
1610             ConstantRange(APInt(32, 0), APInt(32, 2)));
1611   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1612                 Instruction::Sub, MinusFiveToMinusTwo, OBO::NoSignedWrap),
1613             ConstantRange(APInt::getSignedMinValue(32),
1614                           APInt::getSignedMaxValue(32) - 4));
1615   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1616                 Instruction::Sub, MinusFiveToMinusTwo, OBO::NoUnsignedWrap),
1617             ConstantRange(APInt::getMaxValue(32) - 1,
1618                           APInt::getMinValue(32)));
1619 
1620   ConstantRange MinusOneToOne(APInt(32, -1), APInt(32, 2));
1621   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1622                 Instruction::Add, MinusOneToOne, OBO::NoSignedWrap),
1623             ConstantRange(APInt::getSignedMinValue(32) + 1,
1624                           APInt::getSignedMinValue(32) - 1));
1625   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1626                 Instruction::Add, MinusOneToOne, OBO::NoUnsignedWrap),
1627             ConstantRange(APInt(32, 0), APInt(32, 1)));
1628   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1629                 Instruction::Sub, MinusOneToOne, OBO::NoSignedWrap),
1630             ConstantRange(APInt::getSignedMinValue(32) + 1,
1631                           APInt::getSignedMinValue(32) - 1));
1632   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1633                 Instruction::Sub, MinusOneToOne, OBO::NoUnsignedWrap),
1634             ConstantRange(APInt::getMaxValue(32),
1635                           APInt::getMinValue(32)));
1636 
1637   ConstantRange One(APInt(32, 1), APInt(32, 2));
1638   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1639                 Instruction::Add, One, OBO::NoSignedWrap),
1640             ConstantRange(APInt::getSignedMinValue(32),
1641                           APInt::getSignedMaxValue(32)));
1642   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1643                 Instruction::Add, One, OBO::NoUnsignedWrap),
1644             ConstantRange(APInt::getMinValue(32), APInt::getMaxValue(32)));
1645   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1646                 Instruction::Sub, One, OBO::NoSignedWrap),
1647             ConstantRange(APInt::getSignedMinValue(32) + 1,
1648                           APInt::getSignedMinValue(32)));
1649   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1650                 Instruction::Sub, One, OBO::NoUnsignedWrap),
1651             ConstantRange(APInt::getMinValue(32) + 1, APInt::getMinValue(32)));
1652 
1653   ConstantRange OneLessThanBitWidth(APInt(32, 0), APInt(32, 31) + 1);
1654   ConstantRange UpToBitWidth(APInt(32, 0), APInt(32, 32) + 1);
1655   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1656                 Instruction::Shl, UpToBitWidth, OBO::NoUnsignedWrap),
1657             ConstantRange::makeGuaranteedNoWrapRegion(
1658                 Instruction::Shl, OneLessThanBitWidth, OBO::NoUnsignedWrap));
1659   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1660                 Instruction::Shl, UpToBitWidth, OBO::NoSignedWrap),
1661             ConstantRange::makeGuaranteedNoWrapRegion(
1662                 Instruction::Shl, OneLessThanBitWidth, OBO::NoSignedWrap));
1663   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1664                 Instruction::Shl, UpToBitWidth, OBO::NoUnsignedWrap),
1665             ConstantRange(APInt(32, 0), APInt(32, 1) + 1));
1666   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1667                 Instruction::Shl, UpToBitWidth, OBO::NoSignedWrap),
1668             ConstantRange(APInt(32, -1), APInt(32, 0) + 1));
1669 
1670   EXPECT_EQ(
1671       ConstantRange::makeGuaranteedNoWrapRegion(
1672           Instruction::Shl, ConstantRange::getFull(32), OBO::NoUnsignedWrap),
1673       ConstantRange::makeGuaranteedNoWrapRegion(
1674           Instruction::Shl, OneLessThanBitWidth, OBO::NoUnsignedWrap));
1675   EXPECT_EQ(
1676       ConstantRange::makeGuaranteedNoWrapRegion(
1677           Instruction::Shl, ConstantRange::getFull(32), OBO::NoSignedWrap),
1678       ConstantRange::makeGuaranteedNoWrapRegion(
1679           Instruction::Shl, OneLessThanBitWidth, OBO::NoSignedWrap));
1680 
1681   ConstantRange IllegalShAmt(APInt(32, 32), APInt(32, 0) + 1);
1682   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1683                 Instruction::Shl, IllegalShAmt, OBO::NoUnsignedWrap),
1684             ConstantRange::getFull(32));
1685   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1686                 Instruction::Shl, IllegalShAmt, OBO::NoSignedWrap),
1687             ConstantRange::getFull(32));
1688 
1689   EXPECT_EQ(
1690       ConstantRange::makeGuaranteedNoWrapRegion(
1691           Instruction::Shl, ConstantRange(APInt(32, -32), APInt(32, 16) + 1),
1692           OBO::NoUnsignedWrap),
1693       ConstantRange::makeGuaranteedNoWrapRegion(
1694           Instruction::Shl, ConstantRange(APInt(32, 0), APInt(32, 16) + 1),
1695           OBO::NoUnsignedWrap));
1696   EXPECT_EQ(
1697       ConstantRange::makeGuaranteedNoWrapRegion(
1698           Instruction::Shl, ConstantRange(APInt(32, -32), APInt(32, 16) + 1),
1699           OBO::NoSignedWrap),
1700       ConstantRange::makeGuaranteedNoWrapRegion(
1701           Instruction::Shl, ConstantRange(APInt(32, 0), APInt(32, 16) + 1),
1702           OBO::NoSignedWrap));
1703 
1704   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1705                 Instruction::Shl,
1706                 ConstantRange(APInt(32, -32), APInt(32, 16) + 1),
1707                 OBO::NoUnsignedWrap),
1708             ConstantRange(APInt(32, 0), APInt(32, 65535) + 1));
1709   EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
1710                 Instruction::Shl,
1711                 ConstantRange(APInt(32, -32), APInt(32, 16) + 1),
1712                 OBO::NoSignedWrap),
1713             ConstantRange(APInt(32, -32768), APInt(32, 32767) + 1));
1714 }
1715 
1716 template<typename Fn>
1717 void TestNoWrapRegionExhaustive(Instruction::BinaryOps BinOp,
1718                                 unsigned NoWrapKind, Fn OverflowFn) {
1719   for (unsigned Bits : {1, 5}) {
1720     EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) {
1721       if (CR.isEmptySet())
1722         return;
1723       if (Instruction::isShift(BinOp) && CR.getUnsignedMax().uge(Bits))
1724         return;
1725 
1726       ConstantRange NoWrap =
1727           ConstantRange::makeGuaranteedNoWrapRegion(BinOp, CR, NoWrapKind);
1728       EnumerateAPInts(Bits, [&](const APInt &N1) {
1729         bool NoOverflow = true;
1730         bool Overflow = true;
1731         ForeachNumInConstantRange(CR, [&](const APInt &N2) {
1732           if (OverflowFn(N1, N2))
1733             NoOverflow = false;
1734           else
1735             Overflow = false;
1736         });
1737         EXPECT_EQ(NoOverflow, NoWrap.contains(N1));
1738 
1739         // The no-wrap range is exact for single-element ranges.
1740         if (CR.isSingleElement()) {
1741           EXPECT_EQ(Overflow, !NoWrap.contains(N1));
1742         }
1743       });
1744     });
1745   }
1746 }
1747 
1748 // Show that makeGuaranteedNoWrapRegion() is maximal, and for single-element
1749 // ranges also exact.
1750 TEST(ConstantRange, NoWrapRegionExhaustive) {
1751   TestNoWrapRegionExhaustive(
1752       Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap,
1753       [](const APInt &N1, const APInt &N2) {
1754         bool Overflow;
1755         (void) N1.uadd_ov(N2, Overflow);
1756         return Overflow;
1757       });
1758   TestNoWrapRegionExhaustive(
1759       Instruction::Add, OverflowingBinaryOperator::NoSignedWrap,
1760       [](const APInt &N1, const APInt &N2) {
1761         bool Overflow;
1762         (void) N1.sadd_ov(N2, Overflow);
1763         return Overflow;
1764       });
1765   TestNoWrapRegionExhaustive(
1766       Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap,
1767       [](const APInt &N1, const APInt &N2) {
1768         bool Overflow;
1769         (void) N1.usub_ov(N2, Overflow);
1770         return Overflow;
1771       });
1772   TestNoWrapRegionExhaustive(
1773       Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap,
1774       [](const APInt &N1, const APInt &N2) {
1775         bool Overflow;
1776         (void) N1.ssub_ov(N2, Overflow);
1777         return Overflow;
1778       });
1779   TestNoWrapRegionExhaustive(
1780       Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap,
1781       [](const APInt &N1, const APInt &N2) {
1782         bool Overflow;
1783         (void) N1.umul_ov(N2, Overflow);
1784         return Overflow;
1785       });
1786   TestNoWrapRegionExhaustive(
1787       Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap,
1788       [](const APInt &N1, const APInt &N2) {
1789         bool Overflow;
1790         (void) N1.smul_ov(N2, Overflow);
1791         return Overflow;
1792       });
1793   TestNoWrapRegionExhaustive(Instruction::Shl,
1794                              OverflowingBinaryOperator::NoUnsignedWrap,
1795                              [](const APInt &N1, const APInt &N2) {
1796                                bool Overflow;
1797                                (void)N1.ushl_ov(N2, Overflow);
1798                                return Overflow;
1799                              });
1800   TestNoWrapRegionExhaustive(Instruction::Shl,
1801                              OverflowingBinaryOperator::NoSignedWrap,
1802                              [](const APInt &N1, const APInt &N2) {
1803                                bool Overflow;
1804                                (void)N1.sshl_ov(N2, Overflow);
1805                                return Overflow;
1806                              });
1807 }
1808 
1809 TEST(ConstantRange, GetEquivalentICmp) {
1810   APInt RHS;
1811   CmpInst::Predicate Pred;
1812 
1813   EXPECT_TRUE(ConstantRange(APInt::getMinValue(32), APInt(32, 100))
1814                   .getEquivalentICmp(Pred, RHS));
1815   EXPECT_EQ(Pred, CmpInst::ICMP_ULT);
1816   EXPECT_EQ(RHS, APInt(32, 100));
1817 
1818   EXPECT_TRUE(ConstantRange(APInt::getSignedMinValue(32), APInt(32, 100))
1819                   .getEquivalentICmp(Pred, RHS));
1820   EXPECT_EQ(Pred, CmpInst::ICMP_SLT);
1821   EXPECT_EQ(RHS, APInt(32, 100));
1822 
1823   EXPECT_TRUE(ConstantRange(APInt(32, 100), APInt::getMinValue(32))
1824                   .getEquivalentICmp(Pred, RHS));
1825   EXPECT_EQ(Pred, CmpInst::ICMP_UGE);
1826   EXPECT_EQ(RHS, APInt(32, 100));
1827 
1828   EXPECT_TRUE(ConstantRange(APInt(32, 100), APInt::getSignedMinValue(32))
1829                   .getEquivalentICmp(Pred, RHS));
1830   EXPECT_EQ(Pred, CmpInst::ICMP_SGE);
1831   EXPECT_EQ(RHS, APInt(32, 100));
1832 
1833   EXPECT_TRUE(
1834       ConstantRange(32, /*isFullSet=*/true).getEquivalentICmp(Pred, RHS));
1835   EXPECT_EQ(Pred, CmpInst::ICMP_UGE);
1836   EXPECT_EQ(RHS, APInt(32, 0));
1837 
1838   EXPECT_TRUE(
1839       ConstantRange(32, /*isFullSet=*/false).getEquivalentICmp(Pred, RHS));
1840   EXPECT_EQ(Pred, CmpInst::ICMP_ULT);
1841   EXPECT_EQ(RHS, APInt(32, 0));
1842 
1843   EXPECT_FALSE(ConstantRange(APInt(32, 100), APInt(32, 200))
1844                    .getEquivalentICmp(Pred, RHS));
1845 
1846   EXPECT_FALSE(ConstantRange(APInt::getSignedMinValue(32) - APInt(32, 100),
1847                              APInt::getSignedMinValue(32) + APInt(32, 100))
1848                    .getEquivalentICmp(Pred, RHS));
1849 
1850   EXPECT_FALSE(ConstantRange(APInt::getMinValue(32) - APInt(32, 100),
1851                              APInt::getMinValue(32) + APInt(32, 100))
1852                    .getEquivalentICmp(Pred, RHS));
1853 
1854   EXPECT_TRUE(ConstantRange(APInt(32, 100)).getEquivalentICmp(Pred, RHS));
1855   EXPECT_EQ(Pred, CmpInst::ICMP_EQ);
1856   EXPECT_EQ(RHS, APInt(32, 100));
1857 
1858   EXPECT_TRUE(
1859       ConstantRange(APInt(32, 100)).inverse().getEquivalentICmp(Pred, RHS));
1860   EXPECT_EQ(Pred, CmpInst::ICMP_NE);
1861   EXPECT_EQ(RHS, APInt(32, 100));
1862 
1863   EXPECT_TRUE(
1864       ConstantRange(APInt(512, 100)).inverse().getEquivalentICmp(Pred, RHS));
1865   EXPECT_EQ(Pred, CmpInst::ICMP_NE);
1866   EXPECT_EQ(RHS, APInt(512, 100));
1867 
1868   // NB!  It would be correct for the following four calls to getEquivalentICmp
1869   // to return ordered predicates like CmpInst::ICMP_ULT or CmpInst::ICMP_UGT.
1870   // However, that's not the case today.
1871 
1872   EXPECT_TRUE(ConstantRange(APInt(32, 0)).getEquivalentICmp(Pred, RHS));
1873   EXPECT_EQ(Pred, CmpInst::ICMP_EQ);
1874   EXPECT_EQ(RHS, APInt(32, 0));
1875 
1876   EXPECT_TRUE(
1877       ConstantRange(APInt(32, 0)).inverse().getEquivalentICmp(Pred, RHS));
1878   EXPECT_EQ(Pred, CmpInst::ICMP_NE);
1879   EXPECT_EQ(RHS, APInt(32, 0));
1880 
1881   EXPECT_TRUE(ConstantRange(APInt(32, -1)).getEquivalentICmp(Pred, RHS));
1882   EXPECT_EQ(Pred, CmpInst::ICMP_EQ);
1883   EXPECT_EQ(RHS, APInt(32, -1));
1884 
1885   EXPECT_TRUE(
1886       ConstantRange(APInt(32, -1)).inverse().getEquivalentICmp(Pred, RHS));
1887   EXPECT_EQ(Pred, CmpInst::ICMP_NE);
1888   EXPECT_EQ(RHS, APInt(32, -1));
1889 
1890   unsigned Bits = 4;
1891   EnumerateConstantRanges(Bits, [Bits](const ConstantRange &CR) {
1892     CmpInst::Predicate Pred;
1893     APInt RHS, Offset;
1894     CR.getEquivalentICmp(Pred, RHS, Offset);
1895     EnumerateAPInts(Bits, [&](const APInt &N) {
1896       bool Result = ICmpInst::compare(N + Offset, RHS, Pred);
1897       EXPECT_EQ(CR.contains(N), Result);
1898     });
1899 
1900     if (CR.getEquivalentICmp(Pred, RHS)) {
1901       EnumerateAPInts(Bits, [&](const APInt &N) {
1902         bool Result = ICmpInst::compare(N, RHS, Pred);
1903         EXPECT_EQ(CR.contains(N), Result);
1904       });
1905     }
1906   });
1907 }
1908 
1909 #define EXPECT_MAY_OVERFLOW(op) \
1910   EXPECT_EQ(ConstantRange::OverflowResult::MayOverflow, (op))
1911 #define EXPECT_ALWAYS_OVERFLOWS_LOW(op) \
1912   EXPECT_EQ(ConstantRange::OverflowResult::AlwaysOverflowsLow, (op))
1913 #define EXPECT_ALWAYS_OVERFLOWS_HIGH(op) \
1914   EXPECT_EQ(ConstantRange::OverflowResult::AlwaysOverflowsHigh, (op))
1915 #define EXPECT_NEVER_OVERFLOWS(op) \
1916   EXPECT_EQ(ConstantRange::OverflowResult::NeverOverflows, (op))
1917 
1918 TEST_F(ConstantRangeTest, UnsignedAddOverflow) {
1919   // Ill-defined - may overflow is a conservative result.
1920   EXPECT_MAY_OVERFLOW(Some.unsignedAddMayOverflow(Empty));
1921   EXPECT_MAY_OVERFLOW(Empty.unsignedAddMayOverflow(Some));
1922 
1923   // Never overflow despite one full/wrap set.
1924   ConstantRange Zero(APInt::getZero(16));
1925   EXPECT_NEVER_OVERFLOWS(Full.unsignedAddMayOverflow(Zero));
1926   EXPECT_NEVER_OVERFLOWS(Wrap.unsignedAddMayOverflow(Zero));
1927   EXPECT_NEVER_OVERFLOWS(Zero.unsignedAddMayOverflow(Full));
1928   EXPECT_NEVER_OVERFLOWS(Zero.unsignedAddMayOverflow(Wrap));
1929 
1930   // But usually full/wrap always may overflow.
1931   EXPECT_MAY_OVERFLOW(Full.unsignedAddMayOverflow(One));
1932   EXPECT_MAY_OVERFLOW(Wrap.unsignedAddMayOverflow(One));
1933   EXPECT_MAY_OVERFLOW(One.unsignedAddMayOverflow(Full));
1934   EXPECT_MAY_OVERFLOW(One.unsignedAddMayOverflow(Wrap));
1935 
1936   ConstantRange A(APInt(16, 0xfd00), APInt(16, 0xfe00));
1937   ConstantRange B1(APInt(16, 0x0100), APInt(16, 0x0201));
1938   ConstantRange B2(APInt(16, 0x0100), APInt(16, 0x0202));
1939   EXPECT_NEVER_OVERFLOWS(A.unsignedAddMayOverflow(B1));
1940   EXPECT_MAY_OVERFLOW(A.unsignedAddMayOverflow(B2));
1941   EXPECT_NEVER_OVERFLOWS(B1.unsignedAddMayOverflow(A));
1942   EXPECT_MAY_OVERFLOW(B2.unsignedAddMayOverflow(A));
1943 
1944   ConstantRange C1(APInt(16, 0x0299), APInt(16, 0x0400));
1945   ConstantRange C2(APInt(16, 0x0300), APInt(16, 0x0400));
1946   EXPECT_MAY_OVERFLOW(A.unsignedAddMayOverflow(C1));
1947   EXPECT_ALWAYS_OVERFLOWS_HIGH(A.unsignedAddMayOverflow(C2));
1948   EXPECT_MAY_OVERFLOW(C1.unsignedAddMayOverflow(A));
1949   EXPECT_ALWAYS_OVERFLOWS_HIGH(C2.unsignedAddMayOverflow(A));
1950 }
1951 
1952 TEST_F(ConstantRangeTest, UnsignedSubOverflow) {
1953   // Ill-defined - may overflow is a conservative result.
1954   EXPECT_MAY_OVERFLOW(Some.unsignedSubMayOverflow(Empty));
1955   EXPECT_MAY_OVERFLOW(Empty.unsignedSubMayOverflow(Some));
1956 
1957   // Never overflow despite one full/wrap set.
1958   ConstantRange Zero(APInt::getZero(16));
1959   ConstantRange Max(APInt::getAllOnes(16));
1960   EXPECT_NEVER_OVERFLOWS(Full.unsignedSubMayOverflow(Zero));
1961   EXPECT_NEVER_OVERFLOWS(Wrap.unsignedSubMayOverflow(Zero));
1962   EXPECT_NEVER_OVERFLOWS(Max.unsignedSubMayOverflow(Full));
1963   EXPECT_NEVER_OVERFLOWS(Max.unsignedSubMayOverflow(Wrap));
1964 
1965   // But usually full/wrap always may overflow.
1966   EXPECT_MAY_OVERFLOW(Full.unsignedSubMayOverflow(One));
1967   EXPECT_MAY_OVERFLOW(Wrap.unsignedSubMayOverflow(One));
1968   EXPECT_MAY_OVERFLOW(One.unsignedSubMayOverflow(Full));
1969   EXPECT_MAY_OVERFLOW(One.unsignedSubMayOverflow(Wrap));
1970 
1971   ConstantRange A(APInt(16, 0x0000), APInt(16, 0x0100));
1972   ConstantRange B(APInt(16, 0x0100), APInt(16, 0x0200));
1973   EXPECT_NEVER_OVERFLOWS(B.unsignedSubMayOverflow(A));
1974   EXPECT_ALWAYS_OVERFLOWS_LOW(A.unsignedSubMayOverflow(B));
1975 
1976   ConstantRange A1(APInt(16, 0x0000), APInt(16, 0x0101));
1977   ConstantRange B1(APInt(16, 0x0100), APInt(16, 0x0201));
1978   EXPECT_NEVER_OVERFLOWS(B1.unsignedSubMayOverflow(A1));
1979   EXPECT_MAY_OVERFLOW(A1.unsignedSubMayOverflow(B1));
1980 
1981   ConstantRange A2(APInt(16, 0x0000), APInt(16, 0x0102));
1982   ConstantRange B2(APInt(16, 0x0100), APInt(16, 0x0202));
1983   EXPECT_MAY_OVERFLOW(B2.unsignedSubMayOverflow(A2));
1984   EXPECT_MAY_OVERFLOW(A2.unsignedSubMayOverflow(B2));
1985 }
1986 
1987 TEST_F(ConstantRangeTest, SignedAddOverflow) {
1988   // Ill-defined - may overflow is a conservative result.
1989   EXPECT_MAY_OVERFLOW(Some.signedAddMayOverflow(Empty));
1990   EXPECT_MAY_OVERFLOW(Empty.signedAddMayOverflow(Some));
1991 
1992   // Never overflow despite one full/wrap set.
1993   ConstantRange Zero(APInt::getZero(16));
1994   EXPECT_NEVER_OVERFLOWS(Full.signedAddMayOverflow(Zero));
1995   EXPECT_NEVER_OVERFLOWS(Wrap.signedAddMayOverflow(Zero));
1996   EXPECT_NEVER_OVERFLOWS(Zero.signedAddMayOverflow(Full));
1997   EXPECT_NEVER_OVERFLOWS(Zero.signedAddMayOverflow(Wrap));
1998 
1999   // But usually full/wrap always may overflow.
2000   EXPECT_MAY_OVERFLOW(Full.signedAddMayOverflow(One));
2001   EXPECT_MAY_OVERFLOW(Wrap.signedAddMayOverflow(One));
2002   EXPECT_MAY_OVERFLOW(One.signedAddMayOverflow(Full));
2003   EXPECT_MAY_OVERFLOW(One.signedAddMayOverflow(Wrap));
2004 
2005   ConstantRange A(APInt(16, 0x7d00), APInt(16, 0x7e00));
2006   ConstantRange B1(APInt(16, 0x0100), APInt(16, 0x0201));
2007   ConstantRange B2(APInt(16, 0x0100), APInt(16, 0x0202));
2008   EXPECT_NEVER_OVERFLOWS(A.signedAddMayOverflow(B1));
2009   EXPECT_MAY_OVERFLOW(A.signedAddMayOverflow(B2));
2010   ConstantRange B3(APInt(16, 0x8000), APInt(16, 0x0201));
2011   ConstantRange B4(APInt(16, 0x8000), APInt(16, 0x0202));
2012   EXPECT_NEVER_OVERFLOWS(A.signedAddMayOverflow(B3));
2013   EXPECT_MAY_OVERFLOW(A.signedAddMayOverflow(B4));
2014   ConstantRange B5(APInt(16, 0x0299), APInt(16, 0x0400));
2015   ConstantRange B6(APInt(16, 0x0300), APInt(16, 0x0400));
2016   EXPECT_MAY_OVERFLOW(A.signedAddMayOverflow(B5));
2017   EXPECT_ALWAYS_OVERFLOWS_HIGH(A.signedAddMayOverflow(B6));
2018 
2019   ConstantRange C(APInt(16, 0x8200), APInt(16, 0x8300));
2020   ConstantRange D1(APInt(16, 0xfe00), APInt(16, 0xff00));
2021   ConstantRange D2(APInt(16, 0xfd99), APInt(16, 0xff00));
2022   EXPECT_NEVER_OVERFLOWS(C.signedAddMayOverflow(D1));
2023   EXPECT_MAY_OVERFLOW(C.signedAddMayOverflow(D2));
2024   ConstantRange D3(APInt(16, 0xfe00), APInt(16, 0x8000));
2025   ConstantRange D4(APInt(16, 0xfd99), APInt(16, 0x8000));
2026   EXPECT_NEVER_OVERFLOWS(C.signedAddMayOverflow(D3));
2027   EXPECT_MAY_OVERFLOW(C.signedAddMayOverflow(D4));
2028   ConstantRange D5(APInt(16, 0xfc00), APInt(16, 0xfd02));
2029   ConstantRange D6(APInt(16, 0xfc00), APInt(16, 0xfd01));
2030   EXPECT_MAY_OVERFLOW(C.signedAddMayOverflow(D5));
2031   EXPECT_ALWAYS_OVERFLOWS_LOW(C.signedAddMayOverflow(D6));
2032 
2033   ConstantRange E(APInt(16, 0xff00), APInt(16, 0x0100));
2034   EXPECT_NEVER_OVERFLOWS(E.signedAddMayOverflow(E));
2035   ConstantRange F(APInt(16, 0xf000), APInt(16, 0x7000));
2036   EXPECT_MAY_OVERFLOW(F.signedAddMayOverflow(F));
2037 }
2038 
2039 TEST_F(ConstantRangeTest, SignedSubOverflow) {
2040   // Ill-defined - may overflow is a conservative result.
2041   EXPECT_MAY_OVERFLOW(Some.signedSubMayOverflow(Empty));
2042   EXPECT_MAY_OVERFLOW(Empty.signedSubMayOverflow(Some));
2043 
2044   // Never overflow despite one full/wrap set.
2045   ConstantRange Zero(APInt::getZero(16));
2046   EXPECT_NEVER_OVERFLOWS(Full.signedSubMayOverflow(Zero));
2047   EXPECT_NEVER_OVERFLOWS(Wrap.signedSubMayOverflow(Zero));
2048 
2049   // But usually full/wrap always may overflow.
2050   EXPECT_MAY_OVERFLOW(Full.signedSubMayOverflow(One));
2051   EXPECT_MAY_OVERFLOW(Wrap.signedSubMayOverflow(One));
2052   EXPECT_MAY_OVERFLOW(One.signedSubMayOverflow(Full));
2053   EXPECT_MAY_OVERFLOW(One.signedSubMayOverflow(Wrap));
2054 
2055   ConstantRange A(APInt(16, 0x7d00), APInt(16, 0x7e00));
2056   ConstantRange B1(APInt(16, 0xfe00), APInt(16, 0xff00));
2057   ConstantRange B2(APInt(16, 0xfd99), APInt(16, 0xff00));
2058   EXPECT_NEVER_OVERFLOWS(A.signedSubMayOverflow(B1));
2059   EXPECT_MAY_OVERFLOW(A.signedSubMayOverflow(B2));
2060   ConstantRange B3(APInt(16, 0xfc00), APInt(16, 0xfd02));
2061   ConstantRange B4(APInt(16, 0xfc00), APInt(16, 0xfd01));
2062   EXPECT_MAY_OVERFLOW(A.signedSubMayOverflow(B3));
2063   EXPECT_ALWAYS_OVERFLOWS_HIGH(A.signedSubMayOverflow(B4));
2064 
2065   ConstantRange C(APInt(16, 0x8200), APInt(16, 0x8300));
2066   ConstantRange D1(APInt(16, 0x0100), APInt(16, 0x0201));
2067   ConstantRange D2(APInt(16, 0x0100), APInt(16, 0x0202));
2068   EXPECT_NEVER_OVERFLOWS(C.signedSubMayOverflow(D1));
2069   EXPECT_MAY_OVERFLOW(C.signedSubMayOverflow(D2));
2070   ConstantRange D3(APInt(16, 0x0299), APInt(16, 0x0400));
2071   ConstantRange D4(APInt(16, 0x0300), APInt(16, 0x0400));
2072   EXPECT_MAY_OVERFLOW(C.signedSubMayOverflow(D3));
2073   EXPECT_ALWAYS_OVERFLOWS_LOW(C.signedSubMayOverflow(D4));
2074 
2075   ConstantRange E(APInt(16, 0xff00), APInt(16, 0x0100));
2076   EXPECT_NEVER_OVERFLOWS(E.signedSubMayOverflow(E));
2077   ConstantRange F(APInt(16, 0xf000), APInt(16, 0x7001));
2078   EXPECT_MAY_OVERFLOW(F.signedSubMayOverflow(F));
2079 }
2080 
2081 template<typename Fn1, typename Fn2>
2082 static void TestOverflowExhaustive(Fn1 OverflowFn, Fn2 MayOverflowFn) {
2083   // Constant range overflow checks are tested exhaustively on 4-bit numbers.
2084   unsigned Bits = 4;
2085   EnumerateTwoConstantRanges(Bits, [=](const ConstantRange &CR1,
2086                                        const ConstantRange &CR2) {
2087     // Loop over all N1 in CR1 and N2 in CR2 and check whether any of the
2088     // operations have overflow / have no overflow.
2089     bool RangeHasOverflowLow = false;
2090     bool RangeHasOverflowHigh = false;
2091     bool RangeHasNoOverflow = false;
2092     ForeachNumInConstantRange(CR1, [&](const APInt &N1) {
2093       ForeachNumInConstantRange(CR2, [&](const APInt &N2) {
2094         bool IsOverflowHigh;
2095         if (!OverflowFn(IsOverflowHigh, N1, N2)) {
2096           RangeHasNoOverflow = true;
2097           return;
2098         }
2099 
2100         if (IsOverflowHigh)
2101           RangeHasOverflowHigh = true;
2102         else
2103           RangeHasOverflowLow = true;
2104       });
2105     });
2106 
2107     ConstantRange::OverflowResult OR = MayOverflowFn(CR1, CR2);
2108     switch (OR) {
2109     case ConstantRange::OverflowResult::AlwaysOverflowsLow:
2110       EXPECT_TRUE(RangeHasOverflowLow);
2111       EXPECT_FALSE(RangeHasOverflowHigh);
2112       EXPECT_FALSE(RangeHasNoOverflow);
2113       break;
2114     case ConstantRange::OverflowResult::AlwaysOverflowsHigh:
2115       EXPECT_TRUE(RangeHasOverflowHigh);
2116       EXPECT_FALSE(RangeHasOverflowLow);
2117       EXPECT_FALSE(RangeHasNoOverflow);
2118       break;
2119     case ConstantRange::OverflowResult::NeverOverflows:
2120       EXPECT_FALSE(RangeHasOverflowLow);
2121       EXPECT_FALSE(RangeHasOverflowHigh);
2122       EXPECT_TRUE(RangeHasNoOverflow);
2123       break;
2124     case ConstantRange::OverflowResult::MayOverflow:
2125       // We return MayOverflow for empty sets as a conservative result,
2126       // but of course neither the RangeHasOverflow nor the
2127       // RangeHasNoOverflow flags will be set.
2128       if (CR1.isEmptySet() || CR2.isEmptySet())
2129         break;
2130 
2131       EXPECT_TRUE(RangeHasOverflowLow || RangeHasOverflowHigh);
2132       EXPECT_TRUE(RangeHasNoOverflow);
2133       break;
2134     }
2135   });
2136 }
2137 
2138 TEST_F(ConstantRangeTest, UnsignedAddOverflowExhaustive) {
2139   TestOverflowExhaustive(
2140       [](bool &IsOverflowHigh, const APInt &N1, const APInt &N2) {
2141         bool Overflow;
2142         (void) N1.uadd_ov(N2, Overflow);
2143         IsOverflowHigh = true;
2144         return Overflow;
2145       },
2146       [](const ConstantRange &CR1, const ConstantRange &CR2) {
2147         return CR1.unsignedAddMayOverflow(CR2);
2148       });
2149 }
2150 
2151 TEST_F(ConstantRangeTest, UnsignedSubOverflowExhaustive) {
2152   TestOverflowExhaustive(
2153       [](bool &IsOverflowHigh, const APInt &N1, const APInt &N2) {
2154         bool Overflow;
2155         (void) N1.usub_ov(N2, Overflow);
2156         IsOverflowHigh = false;
2157         return Overflow;
2158       },
2159       [](const ConstantRange &CR1, const ConstantRange &CR2) {
2160         return CR1.unsignedSubMayOverflow(CR2);
2161       });
2162 }
2163 
2164 TEST_F(ConstantRangeTest, UnsignedMulOverflowExhaustive) {
2165   TestOverflowExhaustive(
2166       [](bool &IsOverflowHigh, const APInt &N1, const APInt &N2) {
2167         bool Overflow;
2168         (void) N1.umul_ov(N2, Overflow);
2169         IsOverflowHigh = true;
2170         return Overflow;
2171       },
2172       [](const ConstantRange &CR1, const ConstantRange &CR2) {
2173         return CR1.unsignedMulMayOverflow(CR2);
2174       });
2175 }
2176 
2177 TEST_F(ConstantRangeTest, SignedAddOverflowExhaustive) {
2178   TestOverflowExhaustive(
2179       [](bool &IsOverflowHigh, const APInt &N1, const APInt &N2) {
2180         bool Overflow;
2181         (void) N1.sadd_ov(N2, Overflow);
2182         IsOverflowHigh = N1.isNonNegative();
2183         return Overflow;
2184       },
2185       [](const ConstantRange &CR1, const ConstantRange &CR2) {
2186         return CR1.signedAddMayOverflow(CR2);
2187       });
2188 }
2189 
2190 TEST_F(ConstantRangeTest, SignedSubOverflowExhaustive) {
2191   TestOverflowExhaustive(
2192       [](bool &IsOverflowHigh, const APInt &N1, const APInt &N2) {
2193         bool Overflow;
2194         (void) N1.ssub_ov(N2, Overflow);
2195         IsOverflowHigh = N1.isNonNegative();
2196         return Overflow;
2197       },
2198       [](const ConstantRange &CR1, const ConstantRange &CR2) {
2199         return CR1.signedSubMayOverflow(CR2);
2200       });
2201 }
2202 
2203 TEST_F(ConstantRangeTest, FromKnownBits) {
2204   KnownBits Unknown(16);
2205   EXPECT_EQ(Full, ConstantRange::fromKnownBits(Unknown, /*signed*/false));
2206   EXPECT_EQ(Full, ConstantRange::fromKnownBits(Unknown, /*signed*/true));
2207 
2208   // .10..01. -> unsigned 01000010 (66)  to 11011011 (219)
2209   //          -> signed   11000010 (194) to 01011011 (91)
2210   KnownBits Known(8);
2211   Known.Zero = 36;
2212   Known.One = 66;
2213   ConstantRange Unsigned(APInt(8, 66), APInt(8, 219 + 1));
2214   ConstantRange Signed(APInt(8, 194), APInt(8, 91 + 1));
2215   EXPECT_EQ(Unsigned, ConstantRange::fromKnownBits(Known, /*signed*/false));
2216   EXPECT_EQ(Signed, ConstantRange::fromKnownBits(Known, /*signed*/true));
2217 
2218   // 1.10.10. -> 10100100 (164) to 11101101 (237)
2219   Known.Zero = 18;
2220   Known.One = 164;
2221   ConstantRange CR1(APInt(8, 164), APInt(8, 237 + 1));
2222   EXPECT_EQ(CR1, ConstantRange::fromKnownBits(Known, /*signed*/false));
2223   EXPECT_EQ(CR1, ConstantRange::fromKnownBits(Known, /*signed*/true));
2224 
2225   // 01.0.1.0 -> 01000100 (68) to 01101110 (110)
2226   Known.Zero = 145;
2227   Known.One = 68;
2228   ConstantRange CR2(APInt(8, 68), APInt(8, 110 + 1));
2229   EXPECT_EQ(CR2, ConstantRange::fromKnownBits(Known, /*signed*/false));
2230   EXPECT_EQ(CR2, ConstantRange::fromKnownBits(Known, /*signed*/true));
2231 }
2232 
2233 TEST_F(ConstantRangeTest, FromKnownBitsExhaustive) {
2234   unsigned Bits = 4;
2235   unsigned Max = 1 << Bits;
2236   KnownBits Known(Bits);
2237   for (unsigned Zero = 0; Zero < Max; ++Zero) {
2238     for (unsigned One = 0; One < Max; ++One) {
2239       Known.Zero = Zero;
2240       Known.One = One;
2241       if (Known.hasConflict() || Known.isUnknown())
2242         continue;
2243 
2244       SmallBitVector Elems(1 << Bits);
2245       for (unsigned N = 0; N < Max; ++N) {
2246         APInt Num(Bits, N);
2247         if ((Num & Known.Zero) != 0 || (~Num & Known.One) != 0)
2248           continue;
2249         Elems.set(Num.getZExtValue());
2250       }
2251 
2252       TestRange(ConstantRange::fromKnownBits(Known, false),
2253                 Elems, PreferSmallestUnsigned, {});
2254       TestRange(ConstantRange::fromKnownBits(Known, true),
2255                 Elems, PreferSmallestSigned, {});
2256     }
2257   }
2258 }
2259 
2260 TEST_F(ConstantRangeTest, ToKnownBits) {
2261   unsigned Bits = 4;
2262   EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) {
2263     KnownBits Known = CR.toKnownBits();
2264     KnownBits ExpectedKnown(Bits);
2265     ExpectedKnown.Zero.setAllBits();
2266     ExpectedKnown.One.setAllBits();
2267     ForeachNumInConstantRange(CR, [&](const APInt &N) {
2268       ExpectedKnown.One &= N;
2269       ExpectedKnown.Zero &= ~N;
2270     });
2271     // For an empty CR any result would be legal.
2272     if (!CR.isEmptySet()) {
2273       EXPECT_EQ(ExpectedKnown, Known);
2274     }
2275   });
2276 }
2277 
2278 TEST_F(ConstantRangeTest, Negative) {
2279   // All elements in an empty set (of which there are none) are both negative
2280   // and non-negative. Empty & full sets checked explicitly for clarity, but
2281   // they are also covered by the exhaustive test below.
2282   EXPECT_TRUE(Empty.isAllNegative());
2283   EXPECT_TRUE(Empty.isAllNonNegative());
2284   EXPECT_FALSE(Full.isAllNegative());
2285   EXPECT_FALSE(Full.isAllNonNegative());
2286 
2287   unsigned Bits = 4;
2288   EnumerateConstantRanges(Bits, [](const ConstantRange &CR) {
2289     bool AllNegative = true;
2290     bool AllNonNegative = true;
2291     ForeachNumInConstantRange(CR, [&](const APInt &N) {
2292       if (!N.isNegative())
2293         AllNegative = false;
2294       if (!N.isNonNegative())
2295         AllNonNegative = false;
2296     });
2297     assert((CR.isEmptySet() || !AllNegative || !AllNonNegative) &&
2298            "Only empty set can be both all negative and all non-negative");
2299 
2300     EXPECT_EQ(AllNegative, CR.isAllNegative());
2301     EXPECT_EQ(AllNonNegative, CR.isAllNonNegative());
2302   });
2303 }
2304 
2305 TEST_F(ConstantRangeTest, UAddSat) {
2306   TestBinaryOpExhaustive(
2307       [](const ConstantRange &CR1, const ConstantRange &CR2) {
2308         return CR1.uadd_sat(CR2);
2309       },
2310       [](const APInt &N1, const APInt &N2) {
2311         return N1.uadd_sat(N2);
2312       },
2313       PreferSmallestUnsigned);
2314 }
2315 
2316 TEST_F(ConstantRangeTest, USubSat) {
2317   TestBinaryOpExhaustive(
2318       [](const ConstantRange &CR1, const ConstantRange &CR2) {
2319         return CR1.usub_sat(CR2);
2320       },
2321       [](const APInt &N1, const APInt &N2) {
2322         return N1.usub_sat(N2);
2323       },
2324       PreferSmallestUnsigned);
2325 }
2326 
2327 TEST_F(ConstantRangeTest, UMulSat) {
2328   TestBinaryOpExhaustive(
2329       [](const ConstantRange &CR1, const ConstantRange &CR2) {
2330         return CR1.umul_sat(CR2);
2331       },
2332       [](const APInt &N1, const APInt &N2) { return N1.umul_sat(N2); },
2333       PreferSmallestUnsigned);
2334 }
2335 
2336 TEST_F(ConstantRangeTest, UShlSat) {
2337   TestBinaryOpExhaustive(
2338       [](const ConstantRange &CR1, const ConstantRange &CR2) {
2339         return CR1.ushl_sat(CR2);
2340       },
2341       [](const APInt &N1, const APInt &N2) { return N1.ushl_sat(N2); },
2342       PreferSmallestUnsigned);
2343 }
2344 
2345 TEST_F(ConstantRangeTest, SAddSat) {
2346   TestBinaryOpExhaustive(
2347       [](const ConstantRange &CR1, const ConstantRange &CR2) {
2348         return CR1.sadd_sat(CR2);
2349       },
2350       [](const APInt &N1, const APInt &N2) {
2351         return N1.sadd_sat(N2);
2352       },
2353       PreferSmallestSigned);
2354 }
2355 
2356 TEST_F(ConstantRangeTest, SSubSat) {
2357   TestBinaryOpExhaustive(
2358       [](const ConstantRange &CR1, const ConstantRange &CR2) {
2359         return CR1.ssub_sat(CR2);
2360       },
2361       [](const APInt &N1, const APInt &N2) {
2362         return N1.ssub_sat(N2);
2363       },
2364       PreferSmallestSigned);
2365 }
2366 
2367 TEST_F(ConstantRangeTest, SMulSat) {
2368   TestBinaryOpExhaustive(
2369       [](const ConstantRange &CR1, const ConstantRange &CR2) {
2370         return CR1.smul_sat(CR2);
2371       },
2372       [](const APInt &N1, const APInt &N2) { return N1.smul_sat(N2); },
2373       PreferSmallestSigned);
2374 }
2375 
2376 TEST_F(ConstantRangeTest, SShlSat) {
2377   TestBinaryOpExhaustive(
2378       [](const ConstantRange &CR1, const ConstantRange &CR2) {
2379         return CR1.sshl_sat(CR2);
2380       },
2381       [](const APInt &N1, const APInt &N2) { return N1.sshl_sat(N2); },
2382       PreferSmallestSigned);
2383 }
2384 
2385 TEST_F(ConstantRangeTest, Abs) {
2386   TestUnaryOpExhaustive(
2387       [](const ConstantRange &CR) { return CR.abs(); },
2388       [](const APInt &N) { return N.abs(); });
2389 
2390   TestUnaryOpExhaustive(
2391       [](const ConstantRange &CR) { return CR.abs(/*IntMinIsPoison=*/true); },
2392       [](const APInt &N) -> std::optional<APInt> {
2393         if (N.isMinSignedValue())
2394           return std::nullopt;
2395         return N.abs();
2396       });
2397 }
2398 
2399 TEST_F(ConstantRangeTest, castOps) {
2400   ConstantRange A(APInt(16, 66), APInt(16, 128));
2401   ConstantRange FpToI8 = A.castOp(Instruction::FPToSI, 8);
2402   EXPECT_EQ(8u, FpToI8.getBitWidth());
2403   EXPECT_TRUE(FpToI8.isFullSet());
2404 
2405   ConstantRange FpToI16 = A.castOp(Instruction::FPToSI, 16);
2406   EXPECT_EQ(16u, FpToI16.getBitWidth());
2407   EXPECT_EQ(A, FpToI16);
2408 
2409   ConstantRange FPExtToDouble = A.castOp(Instruction::FPExt, 64);
2410   EXPECT_EQ(64u, FPExtToDouble.getBitWidth());
2411   EXPECT_TRUE(FPExtToDouble.isFullSet());
2412 
2413   ConstantRange PtrToInt = A.castOp(Instruction::PtrToInt, 64);
2414   EXPECT_EQ(64u, PtrToInt.getBitWidth());
2415   EXPECT_TRUE(PtrToInt.isFullSet());
2416 
2417   ConstantRange IntToPtr = A.castOp(Instruction::IntToPtr, 64);
2418   EXPECT_EQ(64u, IntToPtr.getBitWidth());
2419   EXPECT_TRUE(IntToPtr.isFullSet());
2420 }
2421 
2422 TEST_F(ConstantRangeTest, binaryAnd) {
2423   // Single element ranges.
2424   ConstantRange R16(APInt(8, 16));
2425   ConstantRange R20(APInt(8, 20));
2426   EXPECT_EQ(*R16.binaryAnd(R16).getSingleElement(), APInt(8, 16));
2427   EXPECT_EQ(*R16.binaryAnd(R20).getSingleElement(), APInt(8, 16 & 20));
2428 
2429   ConstantRange R16_32(APInt(8, 16), APInt(8, 32));
2430   // 'And' with a high bits mask.
2431   ConstantRange R32(APInt(8, 32));
2432   EXPECT_TRUE(R16_32.binaryAnd(R32).getSingleElement()->isZero());
2433   EXPECT_TRUE(R32.binaryAnd(R16_32).getSingleElement()->isZero());
2434   // 'And' with a low bits mask. Handled conservatively for now.
2435   ConstantRange R4(APInt(8, 4));
2436   ConstantRange R0_5(APInt(8, 0), APInt(8, 5));
2437   EXPECT_EQ(R16_32.binaryAnd(R4), R0_5);
2438   EXPECT_EQ(R4.binaryAnd(R16_32), R0_5);
2439 
2440   // Ranges with more than one element. Handled conservatively for now.
2441   ConstantRange R0_99(APInt(8, 0), APInt(8, 99));
2442   ConstantRange R0_32(APInt(8, 0), APInt(8, 32));
2443   EXPECT_EQ(R16_32.binaryAnd(R0_99), R0_32);
2444   EXPECT_EQ(R0_99.binaryAnd(R16_32), R0_32);
2445 
2446   TestBinaryOpExhaustive(
2447       [](const ConstantRange &CR1, const ConstantRange &CR2) {
2448         return CR1.binaryAnd(CR2);
2449       },
2450       [](const APInt &N1, const APInt &N2) { return N1 & N2; }, PreferSmallest,
2451       CheckSingleElementsOnly);
2452 }
2453 
2454 TEST_F(ConstantRangeTest, binaryOr) {
2455   // Single element ranges.
2456   ConstantRange R16(APInt(8, 16));
2457   ConstantRange R20(APInt(8, 20));
2458   EXPECT_EQ(*R16.binaryOr(R16).getSingleElement(), APInt(8, 16));
2459   EXPECT_EQ(*R16.binaryOr(R20).getSingleElement(), APInt(8, 16 | 20));
2460 
2461   ConstantRange R16_32(APInt(8, 16), APInt(8, 32));
2462   // 'Or' with a high bits mask.
2463   // KnownBits estimate is important, otherwise the maximum included element
2464   // would be 2^8 - 1.
2465   ConstantRange R32(APInt(8, 32));
2466   ConstantRange R48_64(APInt(8, 48), APInt(8, 64));
2467   EXPECT_EQ(R16_32.binaryOr(R32), R48_64);
2468   EXPECT_EQ(R32.binaryOr(R16_32), R48_64);
2469   // 'Or' with a low bits mask.
2470   ConstantRange R4(APInt(8, 4));
2471   ConstantRange R0_16(APInt(8, 0), APInt(8, 16));
2472   ConstantRange R4_16(APInt(8, 4), APInt(8, 16));
2473   EXPECT_EQ(R0_16.binaryOr(R4), R4_16);
2474   EXPECT_EQ(R4.binaryOr(R0_16), R4_16);
2475 
2476   // Ranges with more than one element. Handled conservatively for now.
2477   // UMaxUMin estimate is important, otherwise the lower bound would be zero.
2478   ConstantRange R0_64(APInt(8, 0), APInt(8, 64));
2479   ConstantRange R5_32(APInt(8, 5), APInt(8, 32));
2480   ConstantRange R5_64(APInt(8, 5), APInt(8, 64));
2481   EXPECT_EQ(R0_64.binaryOr(R5_32), R5_64);
2482   EXPECT_EQ(R5_32.binaryOr(R0_64), R5_64);
2483 
2484   TestBinaryOpExhaustive(
2485       [](const ConstantRange &CR1, const ConstantRange &CR2) {
2486         return CR1.binaryOr(CR2);
2487       },
2488       [](const APInt &N1, const APInt &N2) { return N1 | N2; }, PreferSmallest,
2489       CheckSingleElementsOnly);
2490 }
2491 
2492 TEST_F(ConstantRangeTest, binaryXor) {
2493   // Single element ranges.
2494   ConstantRange R16(APInt(8, 16));
2495   ConstantRange R20(APInt(8, 20));
2496   EXPECT_EQ(*R16.binaryXor(R16).getSingleElement(), APInt(8, 0));
2497   EXPECT_EQ(*R16.binaryXor(R20).getSingleElement(), APInt(8, 16 ^ 20));
2498 
2499   // Ranges with more than a single element.
2500   ConstantRange R16_35(APInt(8, 16), APInt(8, 35));
2501   ConstantRange R0_99(APInt(8, 0), APInt(8, 99));
2502   EXPECT_EQ(R16_35.binaryXor(R16_35), ConstantRange(APInt(8, 0), APInt(8, 64)));
2503   EXPECT_EQ(R16_35.binaryXor(R0_99), ConstantRange(APInt(8, 0), APInt(8, 128)));
2504   EXPECT_EQ(R0_99.binaryXor(R16_35), ConstantRange(APInt(8, 0), APInt(8, 128)));
2505 
2506   TestBinaryOpExhaustive(
2507       [](const ConstantRange &CR1, const ConstantRange &CR2) {
2508         return CR1.binaryXor(CR2);
2509       },
2510       [](const APInt &N1, const APInt &N2) {
2511         return N1 ^ N2;
2512       },
2513       PreferSmallest,
2514       CheckSingleElementsOnly);
2515 }
2516 
2517 TEST_F(ConstantRangeTest, binaryNot) {
2518   TestUnaryOpExhaustive(
2519       [](const ConstantRange &CR) { return CR.binaryNot(); },
2520       [](const APInt &N) { return ~N; },
2521       PreferSmallest);
2522   TestUnaryOpExhaustive(
2523       [](const ConstantRange &CR) {
2524         return CR.binaryXor(ConstantRange(APInt::getAllOnes(CR.getBitWidth())));
2525       },
2526       [](const APInt &N) { return ~N; }, PreferSmallest);
2527   TestUnaryOpExhaustive(
2528       [](const ConstantRange &CR) {
2529         return ConstantRange(APInt::getAllOnes(CR.getBitWidth())).binaryXor(CR);
2530       },
2531       [](const APInt &N) { return ~N; }, PreferSmallest);
2532 }
2533 
2534 template <typename T>
2535 void testConstantRangeICmpPredEquivalence(ICmpInst::Predicate SrcPred, T Func) {
2536   unsigned Bits = 4;
2537   EnumerateTwoConstantRanges(
2538       Bits, [&](const ConstantRange &CR1, const ConstantRange &CR2) {
2539         ICmpInst::Predicate TgtPred;
2540         bool ExpectedEquivalent;
2541         std::tie(TgtPred, ExpectedEquivalent) = Func(CR1, CR2);
2542         if (TgtPred == CmpInst::Predicate::BAD_ICMP_PREDICATE)
2543           return;
2544         bool TrulyEquivalent = true;
2545         ForeachNumInConstantRange(CR1, [&](const APInt &N1) {
2546           if (!TrulyEquivalent)
2547             return;
2548           ForeachNumInConstantRange(CR2, [&](const APInt &N2) {
2549             if (!TrulyEquivalent)
2550               return;
2551             TrulyEquivalent &= ICmpInst::compare(N1, N2, SrcPred) ==
2552                                ICmpInst::compare(N1, N2, TgtPred);
2553           });
2554         });
2555         ASSERT_EQ(TrulyEquivalent, ExpectedEquivalent);
2556       });
2557 }
2558 
2559 TEST_F(ConstantRangeTest, areInsensitiveToSignednessOfICmpPredicate) {
2560   for (auto Pred : ICmpInst::predicates()) {
2561     if (ICmpInst::isEquality(Pred))
2562       continue;
2563     ICmpInst::Predicate FlippedSignednessPred =
2564         ICmpInst::getFlippedSignednessPredicate(Pred);
2565     testConstantRangeICmpPredEquivalence(Pred, [FlippedSignednessPred](
2566                                                    const ConstantRange &CR1,
2567                                                    const ConstantRange &CR2) {
2568       return std::make_pair(
2569           FlippedSignednessPred,
2570           ConstantRange::areInsensitiveToSignednessOfICmpPredicate(CR1, CR2));
2571     });
2572   }
2573 }
2574 
2575 TEST_F(ConstantRangeTest, areInsensitiveToSignednessOfInvertedICmpPredicate) {
2576   for (auto Pred : ICmpInst::predicates()) {
2577     if (ICmpInst::isEquality(Pred))
2578       continue;
2579     ICmpInst::Predicate InvertedFlippedSignednessPred =
2580         ICmpInst::getInversePredicate(
2581             ICmpInst::getFlippedSignednessPredicate(Pred));
2582     testConstantRangeICmpPredEquivalence(
2583         Pred, [InvertedFlippedSignednessPred](const ConstantRange &CR1,
2584                                               const ConstantRange &CR2) {
2585           return std::make_pair(
2586               InvertedFlippedSignednessPred,
2587               ConstantRange::areInsensitiveToSignednessOfInvertedICmpPredicate(
2588                   CR1, CR2));
2589         });
2590   }
2591 }
2592 
2593 TEST_F(ConstantRangeTest, getEquivalentPredWithFlippedSignedness) {
2594   for (auto Pred : ICmpInst::predicates()) {
2595     if (ICmpInst::isEquality(Pred))
2596       continue;
2597     testConstantRangeICmpPredEquivalence(
2598         Pred, [Pred](const ConstantRange &CR1, const ConstantRange &CR2) {
2599           return std::make_pair(
2600               ConstantRange::getEquivalentPredWithFlippedSignedness(Pred, CR1,
2601                                                                     CR2),
2602               /*ExpectedEquivalent=*/true);
2603         });
2604   }
2605 }
2606 
2607 TEST_F(ConstantRangeTest, isSizeLargerThan) {
2608   EXPECT_FALSE(Empty.isSizeLargerThan(0));
2609 
2610   EXPECT_TRUE(Full.isSizeLargerThan(0));
2611   EXPECT_TRUE(Full.isSizeLargerThan(65535));
2612   EXPECT_FALSE(Full.isSizeLargerThan(65536));
2613 
2614   EXPECT_TRUE(One.isSizeLargerThan(0));
2615   EXPECT_FALSE(One.isSizeLargerThan(1));
2616 }
2617 
2618 } // anonymous namespace
2619