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