1 //===- ConstantRangeTest.cpp - ConstantRange tests ------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/IR/ConstantRange.h"
11 #include "llvm/IR/Instructions.h"
12 #include "gtest/gtest.h"
13 
14 using namespace llvm;
15 
16 namespace {
17 
18 class ConstantRangeTest : public ::testing::Test {
19 protected:
20   static ConstantRange Full;
21   static ConstantRange Empty;
22   static ConstantRange One;
23   static ConstantRange Some;
24   static ConstantRange Wrap;
25 };
26 
27 ConstantRange ConstantRangeTest::Full(16);
28 ConstantRange ConstantRangeTest::Empty(16, false);
29 ConstantRange ConstantRangeTest::One(APInt(16, 0xa));
30 ConstantRange ConstantRangeTest::Some(APInt(16, 0xa), APInt(16, 0xaaa));
31 ConstantRange ConstantRangeTest::Wrap(APInt(16, 0xaaa), APInt(16, 0xa));
32 
TEST_F(ConstantRangeTest,Basics)33 TEST_F(ConstantRangeTest, Basics) {
34   EXPECT_TRUE(Full.isFullSet());
35   EXPECT_FALSE(Full.isEmptySet());
36   EXPECT_TRUE(Full.inverse().isEmptySet());
37   EXPECT_FALSE(Full.isWrappedSet());
38   EXPECT_TRUE(Full.contains(APInt(16, 0x0)));
39   EXPECT_TRUE(Full.contains(APInt(16, 0x9)));
40   EXPECT_TRUE(Full.contains(APInt(16, 0xa)));
41   EXPECT_TRUE(Full.contains(APInt(16, 0xaa9)));
42   EXPECT_TRUE(Full.contains(APInt(16, 0xaaa)));
43 
44   EXPECT_FALSE(Empty.isFullSet());
45   EXPECT_TRUE(Empty.isEmptySet());
46   EXPECT_TRUE(Empty.inverse().isFullSet());
47   EXPECT_FALSE(Empty.isWrappedSet());
48   EXPECT_FALSE(Empty.contains(APInt(16, 0x0)));
49   EXPECT_FALSE(Empty.contains(APInt(16, 0x9)));
50   EXPECT_FALSE(Empty.contains(APInt(16, 0xa)));
51   EXPECT_FALSE(Empty.contains(APInt(16, 0xaa9)));
52   EXPECT_FALSE(Empty.contains(APInt(16, 0xaaa)));
53 
54   EXPECT_FALSE(One.isFullSet());
55   EXPECT_FALSE(One.isEmptySet());
56   EXPECT_FALSE(One.isWrappedSet());
57   EXPECT_FALSE(One.contains(APInt(16, 0x0)));
58   EXPECT_FALSE(One.contains(APInt(16, 0x9)));
59   EXPECT_TRUE(One.contains(APInt(16, 0xa)));
60   EXPECT_FALSE(One.contains(APInt(16, 0xaa9)));
61   EXPECT_FALSE(One.contains(APInt(16, 0xaaa)));
62   EXPECT_FALSE(One.inverse().contains(APInt(16, 0xa)));
63 
64   EXPECT_FALSE(Some.isFullSet());
65   EXPECT_FALSE(Some.isEmptySet());
66   EXPECT_FALSE(Some.isWrappedSet());
67   EXPECT_FALSE(Some.contains(APInt(16, 0x0)));
68   EXPECT_FALSE(Some.contains(APInt(16, 0x9)));
69   EXPECT_TRUE(Some.contains(APInt(16, 0xa)));
70   EXPECT_TRUE(Some.contains(APInt(16, 0xaa9)));
71   EXPECT_FALSE(Some.contains(APInt(16, 0xaaa)));
72 
73   EXPECT_FALSE(Wrap.isFullSet());
74   EXPECT_FALSE(Wrap.isEmptySet());
75   EXPECT_TRUE(Wrap.isWrappedSet());
76   EXPECT_TRUE(Wrap.contains(APInt(16, 0x0)));
77   EXPECT_TRUE(Wrap.contains(APInt(16, 0x9)));
78   EXPECT_FALSE(Wrap.contains(APInt(16, 0xa)));
79   EXPECT_FALSE(Wrap.contains(APInt(16, 0xaa9)));
80   EXPECT_TRUE(Wrap.contains(APInt(16, 0xaaa)));
81 }
82 
TEST_F(ConstantRangeTest,Equality)83 TEST_F(ConstantRangeTest, Equality) {
84   EXPECT_EQ(Full, Full);
85   EXPECT_EQ(Empty, Empty);
86   EXPECT_EQ(One, One);
87   EXPECT_EQ(Some, Some);
88   EXPECT_EQ(Wrap, Wrap);
89   EXPECT_NE(Full, Empty);
90   EXPECT_NE(Full, One);
91   EXPECT_NE(Full, Some);
92   EXPECT_NE(Full, Wrap);
93   EXPECT_NE(Empty, One);
94   EXPECT_NE(Empty, Some);
95   EXPECT_NE(Empty, Wrap);
96   EXPECT_NE(One, Some);
97   EXPECT_NE(One, Wrap);
98   EXPECT_NE(Some, Wrap);
99 }
100 
TEST_F(ConstantRangeTest,SingleElement)101 TEST_F(ConstantRangeTest, SingleElement) {
102   EXPECT_EQ(Full.getSingleElement(), static_cast<APInt *>(nullptr));
103   EXPECT_EQ(Empty.getSingleElement(), static_cast<APInt *>(nullptr));
104   EXPECT_EQ(*One.getSingleElement(), APInt(16, 0xa));
105   EXPECT_EQ(Some.getSingleElement(), static_cast<APInt *>(nullptr));
106   EXPECT_EQ(Wrap.getSingleElement(), static_cast<APInt *>(nullptr));
107 
108   EXPECT_FALSE(Full.isSingleElement());
109   EXPECT_FALSE(Empty.isSingleElement());
110   EXPECT_TRUE(One.isSingleElement());
111   EXPECT_FALSE(Some.isSingleElement());
112   EXPECT_FALSE(Wrap.isSingleElement());
113 }
114 
TEST_F(ConstantRangeTest,GetSetSize)115 TEST_F(ConstantRangeTest, GetSetSize) {
116   EXPECT_EQ(Full.getSetSize(), APInt(17, 65536));
117   EXPECT_EQ(Empty.getSetSize(), APInt(17, 0));
118   EXPECT_EQ(One.getSetSize(), APInt(17, 1));
119   EXPECT_EQ(Some.getSetSize(), APInt(17, 0xaa0));
120 
121   ConstantRange Wrap(APInt(4, 7), APInt(4, 3));
122   ConstantRange Wrap2(APInt(4, 8), APInt(4, 7));
123   EXPECT_EQ(Wrap.getSetSize(), APInt(5, 12));
124   EXPECT_EQ(Wrap2.getSetSize(), APInt(5, 15));
125 }
126 
TEST_F(ConstantRangeTest,GetMinsAndMaxes)127 TEST_F(ConstantRangeTest, GetMinsAndMaxes) {
128   EXPECT_EQ(Full.getUnsignedMax(), APInt(16, UINT16_MAX));
129   EXPECT_EQ(One.getUnsignedMax(), APInt(16, 0xa));
130   EXPECT_EQ(Some.getUnsignedMax(), APInt(16, 0xaa9));
131   EXPECT_EQ(Wrap.getUnsignedMax(), APInt(16, UINT16_MAX));
132 
133   EXPECT_EQ(Full.getUnsignedMin(), APInt(16, 0));
134   EXPECT_EQ(One.getUnsignedMin(), APInt(16, 0xa));
135   EXPECT_EQ(Some.getUnsignedMin(), APInt(16, 0xa));
136   EXPECT_EQ(Wrap.getUnsignedMin(), APInt(16, 0));
137 
138   EXPECT_EQ(Full.getSignedMax(), APInt(16, INT16_MAX));
139   EXPECT_EQ(One.getSignedMax(), APInt(16, 0xa));
140   EXPECT_EQ(Some.getSignedMax(), APInt(16, 0xaa9));
141   EXPECT_EQ(Wrap.getSignedMax(), APInt(16, INT16_MAX));
142 
143   EXPECT_EQ(Full.getSignedMin(), APInt(16, (uint64_t)INT16_MIN));
144   EXPECT_EQ(One.getSignedMin(), APInt(16, 0xa));
145   EXPECT_EQ(Some.getSignedMin(), APInt(16, 0xa));
146   EXPECT_EQ(Wrap.getSignedMin(), APInt(16, (uint64_t)INT16_MIN));
147 
148   // Found by Klee
149   EXPECT_EQ(ConstantRange(APInt(4, 7), APInt(4, 0)).getSignedMax(),
150             APInt(4, 7));
151 }
152 
TEST_F(ConstantRangeTest,SignWrapped)153 TEST_F(ConstantRangeTest, SignWrapped) {
154   EXPECT_TRUE(Full.isSignWrappedSet());
155   EXPECT_FALSE(Empty.isSignWrappedSet());
156   EXPECT_FALSE(One.isSignWrappedSet());
157   EXPECT_FALSE(Some.isSignWrappedSet());
158   EXPECT_TRUE(Wrap.isSignWrappedSet());
159 
160   EXPECT_FALSE(ConstantRange(APInt(8, 127), APInt(8, 128)).isSignWrappedSet());
161   EXPECT_TRUE(ConstantRange(APInt(8, 127), APInt(8, 129)).isSignWrappedSet());
162   EXPECT_FALSE(ConstantRange(APInt(8, 128), APInt(8, 129)).isSignWrappedSet());
163   EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 9)).isSignWrappedSet());
164   EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 250)).isSignWrappedSet());
165   EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 10)).isSignWrappedSet());
166   EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 251)).isSignWrappedSet());
167 }
168 
TEST_F(ConstantRangeTest,Trunc)169 TEST_F(ConstantRangeTest, Trunc) {
170   ConstantRange TFull = Full.truncate(10);
171   ConstantRange TEmpty = Empty.truncate(10);
172   ConstantRange TOne = One.truncate(10);
173   ConstantRange TSome = Some.truncate(10);
174   ConstantRange TWrap = Wrap.truncate(10);
175   EXPECT_TRUE(TFull.isFullSet());
176   EXPECT_TRUE(TEmpty.isEmptySet());
177   EXPECT_EQ(TOne, ConstantRange(One.getLower().trunc(10),
178                                 One.getUpper().trunc(10)));
179   EXPECT_TRUE(TSome.isFullSet());
180 }
181 
TEST_F(ConstantRangeTest,ZExt)182 TEST_F(ConstantRangeTest, ZExt) {
183   ConstantRange ZFull = Full.zeroExtend(20);
184   ConstantRange ZEmpty = Empty.zeroExtend(20);
185   ConstantRange ZOne = One.zeroExtend(20);
186   ConstantRange ZSome = Some.zeroExtend(20);
187   ConstantRange ZWrap = Wrap.zeroExtend(20);
188   EXPECT_EQ(ZFull, ConstantRange(APInt(20, 0), APInt(20, 0x10000)));
189   EXPECT_TRUE(ZEmpty.isEmptySet());
190   EXPECT_EQ(ZOne, ConstantRange(One.getLower().zext(20),
191                                 One.getUpper().zext(20)));
192   EXPECT_EQ(ZSome, ConstantRange(Some.getLower().zext(20),
193                                  Some.getUpper().zext(20)));
194   EXPECT_EQ(ZWrap, ConstantRange(APInt(20, 0), APInt(20, 0x10000)));
195 
196   // zext([5, 0), 3->7) = [5, 8)
197   ConstantRange FiveZero(APInt(3, 5), APInt(3, 0));
198   EXPECT_EQ(FiveZero.zeroExtend(7), ConstantRange(APInt(7, 5), APInt(7, 8)));
199 }
200 
TEST_F(ConstantRangeTest,SExt)201 TEST_F(ConstantRangeTest, SExt) {
202   ConstantRange SFull = Full.signExtend(20);
203   ConstantRange SEmpty = Empty.signExtend(20);
204   ConstantRange SOne = One.signExtend(20);
205   ConstantRange SSome = Some.signExtend(20);
206   ConstantRange SWrap = Wrap.signExtend(20);
207   EXPECT_EQ(SFull, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true),
208                                  APInt(20, INT16_MAX + 1, true)));
209   EXPECT_TRUE(SEmpty.isEmptySet());
210   EXPECT_EQ(SOne, ConstantRange(One.getLower().sext(20),
211                                 One.getUpper().sext(20)));
212   EXPECT_EQ(SSome, ConstantRange(Some.getLower().sext(20),
213                                  Some.getUpper().sext(20)));
214   EXPECT_EQ(SWrap, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true),
215                                  APInt(20, INT16_MAX + 1, true)));
216 
217   EXPECT_EQ(ConstantRange(APInt(8, 120), APInt(8, 140)).signExtend(16),
218             ConstantRange(APInt(16, -128), APInt(16, 128)));
219 
220   EXPECT_EQ(ConstantRange(APInt(16, 0x0200), APInt(16, 0x8000)).signExtend(19),
221             ConstantRange(APInt(19, 0x0200), APInt(19, 0x8000)));
222 }
223 
TEST_F(ConstantRangeTest,IntersectWith)224 TEST_F(ConstantRangeTest, IntersectWith) {
225   EXPECT_EQ(Empty.intersectWith(Full), Empty);
226   EXPECT_EQ(Empty.intersectWith(Empty), Empty);
227   EXPECT_EQ(Empty.intersectWith(One), Empty);
228   EXPECT_EQ(Empty.intersectWith(Some), Empty);
229   EXPECT_EQ(Empty.intersectWith(Wrap), Empty);
230   EXPECT_EQ(Full.intersectWith(Full), Full);
231   EXPECT_EQ(Some.intersectWith(Some), Some);
232   EXPECT_EQ(Some.intersectWith(One), One);
233   EXPECT_EQ(Full.intersectWith(One), One);
234   EXPECT_EQ(Full.intersectWith(Some), Some);
235   EXPECT_EQ(Some.intersectWith(Wrap), Empty);
236   EXPECT_EQ(One.intersectWith(Wrap), Empty);
237   EXPECT_EQ(One.intersectWith(Wrap), Wrap.intersectWith(One));
238 
239   // Klee generated testcase from PR4545.
240   // The intersection of i16 [4, 2) and [6, 5) is disjoint, looking like
241   // 01..4.6789ABCDEF where the dots represent values not in the intersection.
242   ConstantRange LHS(APInt(16, 4), APInt(16, 2));
243   ConstantRange RHS(APInt(16, 6), APInt(16, 5));
244   EXPECT_TRUE(LHS.intersectWith(RHS) == LHS);
245 
246   // previous bug: intersection of [min, 3) and [2, max) should be 2
247   LHS = ConstantRange(APInt(32, -2147483646), APInt(32, 3));
248   RHS = ConstantRange(APInt(32, 2), APInt(32, 2147483646));
249   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2)));
250 
251   // [2, 0) /\ [4, 3) = [2, 0)
252   LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
253   RHS = ConstantRange(APInt(32, 4), APInt(32, 3));
254   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2), APInt(32, 0)));
255 
256   // [2, 0) /\ [4, 2) = [4, 0)
257   LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
258   RHS = ConstantRange(APInt(32, 4), APInt(32, 2));
259   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 0)));
260 
261   // [4, 2) /\ [5, 1) = [5, 1)
262   LHS = ConstantRange(APInt(32, 4), APInt(32, 2));
263   RHS = ConstantRange(APInt(32, 5), APInt(32, 1));
264   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 5), APInt(32, 1)));
265 
266   // [2, 0) /\ [7, 4) = [7, 4)
267   LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
268   RHS = ConstantRange(APInt(32, 7), APInt(32, 4));
269   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 7), APInt(32, 4)));
270 
271   // [4, 2) /\ [1, 0) = [1, 0)
272   LHS = ConstantRange(APInt(32, 4), APInt(32, 2));
273   RHS = ConstantRange(APInt(32, 1), APInt(32, 0));
274   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 2)));
275 
276   // [15, 0) /\ [7, 6) = [15, 0)
277   LHS = ConstantRange(APInt(32, 15), APInt(32, 0));
278   RHS = ConstantRange(APInt(32, 7), APInt(32, 6));
279   EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 15), APInt(32, 0)));
280 }
281 
TEST_F(ConstantRangeTest,UnionWith)282 TEST_F(ConstantRangeTest, UnionWith) {
283   EXPECT_EQ(Wrap.unionWith(One),
284             ConstantRange(APInt(16, 0xaaa), APInt(16, 0xb)));
285   EXPECT_EQ(One.unionWith(Wrap), Wrap.unionWith(One));
286   EXPECT_EQ(Empty.unionWith(Empty), Empty);
287   EXPECT_EQ(Full.unionWith(Full), Full);
288   EXPECT_EQ(Some.unionWith(Wrap), Full);
289 
290   // PR4545
291   EXPECT_EQ(ConstantRange(APInt(16, 14), APInt(16, 1)).unionWith(
292                                     ConstantRange(APInt(16, 0), APInt(16, 8))),
293             ConstantRange(APInt(16, 14), APInt(16, 8)));
294   EXPECT_EQ(ConstantRange(APInt(16, 6), APInt(16, 4)).unionWith(
295                                     ConstantRange(APInt(16, 4), APInt(16, 0))),
296               ConstantRange(16));
297   EXPECT_EQ(ConstantRange(APInt(16, 1), APInt(16, 0)).unionWith(
298                                     ConstantRange(APInt(16, 2), APInt(16, 1))),
299               ConstantRange(16));
300 }
301 
TEST_F(ConstantRangeTest,SetDifference)302 TEST_F(ConstantRangeTest, SetDifference) {
303   EXPECT_EQ(Full.difference(Empty), Full);
304   EXPECT_EQ(Full.difference(Full), Empty);
305   EXPECT_EQ(Empty.difference(Empty), Empty);
306   EXPECT_EQ(Empty.difference(Full), Empty);
307 
308   ConstantRange A(APInt(16, 3), APInt(16, 7));
309   ConstantRange B(APInt(16, 5), APInt(16, 9));
310   ConstantRange C(APInt(16, 3), APInt(16, 5));
311   ConstantRange D(APInt(16, 7), APInt(16, 9));
312   ConstantRange E(APInt(16, 5), APInt(16, 4));
313   ConstantRange F(APInt(16, 7), APInt(16, 3));
314   EXPECT_EQ(A.difference(B), C);
315   EXPECT_EQ(B.difference(A), D);
316   EXPECT_EQ(E.difference(A), F);
317 }
318 
TEST_F(ConstantRangeTest,SubtractAPInt)319 TEST_F(ConstantRangeTest, SubtractAPInt) {
320   EXPECT_EQ(Full.subtract(APInt(16, 4)), Full);
321   EXPECT_EQ(Empty.subtract(APInt(16, 4)), Empty);
322   EXPECT_EQ(Some.subtract(APInt(16, 4)),
323             ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
324   EXPECT_EQ(Wrap.subtract(APInt(16, 4)),
325             ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
326   EXPECT_EQ(One.subtract(APInt(16, 4)),
327             ConstantRange(APInt(16, 0x6)));
328 }
329 
TEST_F(ConstantRangeTest,Add)330 TEST_F(ConstantRangeTest, Add) {
331   EXPECT_EQ(Full.add(APInt(16, 4)), Full);
332   EXPECT_EQ(Full.add(Full), Full);
333   EXPECT_EQ(Full.add(Empty), Empty);
334   EXPECT_EQ(Full.add(One), Full);
335   EXPECT_EQ(Full.add(Some), Full);
336   EXPECT_EQ(Full.add(Wrap), Full);
337   EXPECT_EQ(Empty.add(Empty), Empty);
338   EXPECT_EQ(Empty.add(One), Empty);
339   EXPECT_EQ(Empty.add(Some), Empty);
340   EXPECT_EQ(Empty.add(Wrap), Empty);
341   EXPECT_EQ(Empty.add(APInt(16, 4)), Empty);
342   EXPECT_EQ(Some.add(APInt(16, 4)),
343             ConstantRange(APInt(16, 0xe), APInt(16, 0xaae)));
344   EXPECT_EQ(Wrap.add(APInt(16, 4)),
345             ConstantRange(APInt(16, 0xaae), APInt(16, 0xe)));
346   EXPECT_EQ(One.add(APInt(16, 4)),
347             ConstantRange(APInt(16, 0xe)));
348 }
349 
TEST_F(ConstantRangeTest,Sub)350 TEST_F(ConstantRangeTest, Sub) {
351   EXPECT_EQ(Full.sub(APInt(16, 4)), Full);
352   EXPECT_EQ(Full.sub(Full), Full);
353   EXPECT_EQ(Full.sub(Empty), Empty);
354   EXPECT_EQ(Full.sub(One), Full);
355   EXPECT_EQ(Full.sub(Some), Full);
356   EXPECT_EQ(Full.sub(Wrap), Full);
357   EXPECT_EQ(Empty.sub(Empty), Empty);
358   EXPECT_EQ(Empty.sub(One), Empty);
359   EXPECT_EQ(Empty.sub(Some), Empty);
360   EXPECT_EQ(Empty.sub(Wrap), Empty);
361   EXPECT_EQ(Empty.sub(APInt(16, 4)), Empty);
362   EXPECT_EQ(Some.sub(APInt(16, 4)),
363             ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
364   EXPECT_EQ(Some.sub(Some),
365             ConstantRange(APInt(16, 0xf561), APInt(16, 0xaa0)));
366   EXPECT_EQ(Wrap.sub(APInt(16, 4)),
367             ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
368   EXPECT_EQ(One.sub(APInt(16, 4)),
369             ConstantRange(APInt(16, 0x6)));
370 }
371 
TEST_F(ConstantRangeTest,Multiply)372 TEST_F(ConstantRangeTest, Multiply) {
373   EXPECT_EQ(Full.multiply(Full), Full);
374   EXPECT_EQ(Full.multiply(Empty), Empty);
375   EXPECT_EQ(Full.multiply(One), Full);
376   EXPECT_EQ(Full.multiply(Some), Full);
377   EXPECT_EQ(Full.multiply(Wrap), Full);
378   EXPECT_EQ(Empty.multiply(Empty), Empty);
379   EXPECT_EQ(Empty.multiply(One), Empty);
380   EXPECT_EQ(Empty.multiply(Some), Empty);
381   EXPECT_EQ(Empty.multiply(Wrap), Empty);
382   EXPECT_EQ(One.multiply(One), ConstantRange(APInt(16, 0xa*0xa),
383                                              APInt(16, 0xa*0xa + 1)));
384   EXPECT_EQ(One.multiply(Some), ConstantRange(APInt(16, 0xa*0xa),
385                                               APInt(16, 0xa*0xaa9 + 1)));
386   EXPECT_EQ(One.multiply(Wrap), Full);
387   EXPECT_EQ(Some.multiply(Some), Full);
388   EXPECT_EQ(Some.multiply(Wrap), Full);
389   EXPECT_EQ(Wrap.multiply(Wrap), Full);
390 
391   ConstantRange Zero(APInt(16, 0));
392   EXPECT_EQ(Zero.multiply(Full), Zero);
393   EXPECT_EQ(Zero.multiply(Some), Zero);
394   EXPECT_EQ(Zero.multiply(Wrap), Zero);
395   EXPECT_EQ(Full.multiply(Zero), Zero);
396   EXPECT_EQ(Some.multiply(Zero), Zero);
397   EXPECT_EQ(Wrap.multiply(Zero), Zero);
398 
399   // http://llvm.org/PR4545
400   EXPECT_EQ(ConstantRange(APInt(4, 1), APInt(4, 6)).multiply(
401                 ConstantRange(APInt(4, 6), APInt(4, 2))),
402             ConstantRange(4, /*isFullSet=*/true));
403 
404   EXPECT_EQ(ConstantRange(APInt(8, 254), APInt(8, 0)).multiply(
405               ConstantRange(APInt(8, 252), APInt(8, 4))),
406             ConstantRange(APInt(8, 250), APInt(8, 9)));
407   EXPECT_EQ(ConstantRange(APInt(8, 254), APInt(8, 255)).multiply(
408               ConstantRange(APInt(8, 2), APInt(8, 4))),
409             ConstantRange(APInt(8, 250), APInt(8, 253)));
410 }
411 
TEST_F(ConstantRangeTest,UMax)412 TEST_F(ConstantRangeTest, UMax) {
413   EXPECT_EQ(Full.umax(Full), Full);
414   EXPECT_EQ(Full.umax(Empty), Empty);
415   EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
416   EXPECT_EQ(Full.umax(Wrap), Full);
417   EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
418   EXPECT_EQ(Empty.umax(Empty), Empty);
419   EXPECT_EQ(Empty.umax(Some), Empty);
420   EXPECT_EQ(Empty.umax(Wrap), Empty);
421   EXPECT_EQ(Empty.umax(One), Empty);
422   EXPECT_EQ(Some.umax(Some), Some);
423   EXPECT_EQ(Some.umax(Wrap), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
424   EXPECT_EQ(Some.umax(One), Some);
425   // TODO: ConstantRange is currently over-conservative here.
426   EXPECT_EQ(Wrap.umax(Wrap), Full);
427   EXPECT_EQ(Wrap.umax(One), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
428   EXPECT_EQ(One.umax(One), One);
429 }
430 
TEST_F(ConstantRangeTest,SMax)431 TEST_F(ConstantRangeTest, SMax) {
432   EXPECT_EQ(Full.smax(Full), Full);
433   EXPECT_EQ(Full.smax(Empty), Empty);
434   EXPECT_EQ(Full.smax(Some), ConstantRange(APInt(16, 0xa),
435                                            APInt::getSignedMinValue(16)));
436   EXPECT_EQ(Full.smax(Wrap), Full);
437   EXPECT_EQ(Full.smax(One), ConstantRange(APInt(16, 0xa),
438                                           APInt::getSignedMinValue(16)));
439   EXPECT_EQ(Empty.smax(Empty), Empty);
440   EXPECT_EQ(Empty.smax(Some), Empty);
441   EXPECT_EQ(Empty.smax(Wrap), Empty);
442   EXPECT_EQ(Empty.smax(One), Empty);
443   EXPECT_EQ(Some.smax(Some), Some);
444   EXPECT_EQ(Some.smax(Wrap), ConstantRange(APInt(16, 0xa),
445                                            APInt(16, (uint64_t)INT16_MIN)));
446   EXPECT_EQ(Some.smax(One), Some);
447   EXPECT_EQ(Wrap.smax(One), ConstantRange(APInt(16, 0xa),
448                                           APInt(16, (uint64_t)INT16_MIN)));
449   EXPECT_EQ(One.smax(One), One);
450 }
451 
TEST_F(ConstantRangeTest,UDiv)452 TEST_F(ConstantRangeTest, UDiv) {
453   EXPECT_EQ(Full.udiv(Full), Full);
454   EXPECT_EQ(Full.udiv(Empty), Empty);
455   EXPECT_EQ(Full.udiv(One), ConstantRange(APInt(16, 0),
456                                           APInt(16, 0xffff / 0xa + 1)));
457   EXPECT_EQ(Full.udiv(Some), ConstantRange(APInt(16, 0),
458                                            APInt(16, 0xffff / 0xa + 1)));
459   EXPECT_EQ(Full.udiv(Wrap), Full);
460   EXPECT_EQ(Empty.udiv(Empty), Empty);
461   EXPECT_EQ(Empty.udiv(One), Empty);
462   EXPECT_EQ(Empty.udiv(Some), Empty);
463   EXPECT_EQ(Empty.udiv(Wrap), Empty);
464   EXPECT_EQ(One.udiv(One), ConstantRange(APInt(16, 1)));
465   EXPECT_EQ(One.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 2)));
466   EXPECT_EQ(One.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
467   EXPECT_EQ(Some.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 0x111)));
468   EXPECT_EQ(Some.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
469   EXPECT_EQ(Wrap.udiv(Wrap), Full);
470 }
471 
TEST_F(ConstantRangeTest,Shl)472 TEST_F(ConstantRangeTest, Shl) {
473   EXPECT_EQ(Full.shl(Full), Full);
474   EXPECT_EQ(Full.shl(Empty), Empty);
475   EXPECT_EQ(Full.shl(One), Full);    // TODO: [0, (-1 << 0xa) + 1)
476   EXPECT_EQ(Full.shl(Some), Full);   // TODO: [0, (-1 << 0xa) + 1)
477   EXPECT_EQ(Full.shl(Wrap), Full);
478   EXPECT_EQ(Empty.shl(Empty), Empty);
479   EXPECT_EQ(Empty.shl(One), Empty);
480   EXPECT_EQ(Empty.shl(Some), Empty);
481   EXPECT_EQ(Empty.shl(Wrap), Empty);
482   EXPECT_EQ(One.shl(One), ConstantRange(APInt(16, 0xa << 0xa),
483                                         APInt(16, (0xa << 0xa) + 1)));
484   EXPECT_EQ(One.shl(Some), Full);    // TODO: [0xa << 0xa, 0)
485   EXPECT_EQ(One.shl(Wrap), Full);    // TODO: [0xa, 0xa << 14 + 1)
486   EXPECT_EQ(Some.shl(Some), Full);   // TODO: [0xa << 0xa, 0xfc01)
487   EXPECT_EQ(Some.shl(Wrap), Full);   // TODO: [0xa, 0x7ff << 0x5 + 1)
488   EXPECT_EQ(Wrap.shl(Wrap), Full);
489 }
490 
TEST_F(ConstantRangeTest,Lshr)491 TEST_F(ConstantRangeTest, Lshr) {
492   EXPECT_EQ(Full.lshr(Full), Full);
493   EXPECT_EQ(Full.lshr(Empty), Empty);
494   EXPECT_EQ(Full.lshr(One), ConstantRange(APInt(16, 0),
495                                           APInt(16, (0xffff >> 0xa) + 1)));
496   EXPECT_EQ(Full.lshr(Some), ConstantRange(APInt(16, 0),
497                                            APInt(16, (0xffff >> 0xa) + 1)));
498   EXPECT_EQ(Full.lshr(Wrap), Full);
499   EXPECT_EQ(Empty.lshr(Empty), Empty);
500   EXPECT_EQ(Empty.lshr(One), Empty);
501   EXPECT_EQ(Empty.lshr(Some), Empty);
502   EXPECT_EQ(Empty.lshr(Wrap), Empty);
503   EXPECT_EQ(One.lshr(One), ConstantRange(APInt(16, 0)));
504   EXPECT_EQ(One.lshr(Some), ConstantRange(APInt(16, 0)));
505   EXPECT_EQ(One.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
506   EXPECT_EQ(Some.lshr(Some), ConstantRange(APInt(16, 0),
507                                            APInt(16, (0xaaa >> 0xa) + 1)));
508   EXPECT_EQ(Some.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
509   EXPECT_EQ(Wrap.lshr(Wrap), Full);
510 }
511 
TEST(ConstantRange,MakeAllowedICmpRegion)512 TEST(ConstantRange, MakeAllowedICmpRegion) {
513   // PR8250
514   ConstantRange SMax = ConstantRange(APInt::getSignedMaxValue(32));
515   EXPECT_TRUE(ConstantRange::makeAllowedICmpRegion(ICmpInst::ICMP_SGT, SMax)
516                   .isEmptySet());
517 }
518 
TEST(ConstantRange,MakeSatisfyingICmpRegion)519 TEST(ConstantRange, MakeSatisfyingICmpRegion) {
520   ConstantRange LowHalf(APInt(8, 0), APInt(8, 128));
521   ConstantRange HighHalf(APInt(8, 128), APInt(8, 0));
522   ConstantRange EmptySet(8, /* isFullSet = */ false);
523 
524   EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_NE, LowHalf),
525             HighHalf);
526 
527   EXPECT_EQ(
528       ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_NE, HighHalf),
529       LowHalf);
530 
531   EXPECT_TRUE(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_EQ,
532                                                       HighHalf).isEmptySet());
533 
534   ConstantRange UnsignedSample(APInt(8, 5), APInt(8, 200));
535 
536   EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_ULT,
537                                                     UnsignedSample),
538             ConstantRange(APInt(8, 0), APInt(8, 5)));
539 
540   EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_ULE,
541                                                     UnsignedSample),
542             ConstantRange(APInt(8, 0), APInt(8, 6)));
543 
544   EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_UGT,
545                                                     UnsignedSample),
546             ConstantRange(APInt(8, 200), APInt(8, 0)));
547 
548   EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_UGE,
549                                                     UnsignedSample),
550             ConstantRange(APInt(8, 199), APInt(8, 0)));
551 
552   ConstantRange SignedSample(APInt(8, -5), APInt(8, 5));
553 
554   EXPECT_EQ(
555       ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SLT, SignedSample),
556       ConstantRange(APInt(8, -128), APInt(8, -5)));
557 
558   EXPECT_EQ(
559       ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SLE, SignedSample),
560       ConstantRange(APInt(8, -128), APInt(8, -4)));
561 
562   EXPECT_EQ(
563       ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SGT, SignedSample),
564       ConstantRange(APInt(8, 5), APInt(8, -128)));
565 
566   EXPECT_EQ(
567       ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SGE, SignedSample),
568       ConstantRange(APInt(8, 4), APInt(8, -128)));
569 }
570 
571 }  // anonymous namespace
572