1//===- llvm/unittest/Support/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/Support/ConstantRange.h"
11#include "llvm/IR/Instructions.h"
12#include "gtest/gtest.h"
13
14using namespace llvm;
15
16namespace {
17
18class ConstantRangeTest : public ::testing::Test {
19protected:
20  static ConstantRange Full;
21  static ConstantRange Empty;
22  static ConstantRange One;
23  static ConstantRange Some;
24  static ConstantRange Wrap;
25};
26
27ConstantRange ConstantRangeTest::Full(16);
28ConstantRange ConstantRangeTest::Empty(16, false);
29ConstantRange ConstantRangeTest::One(APInt(16, 0xa));
30ConstantRange ConstantRangeTest::Some(APInt(16, 0xa), APInt(16, 0xaaa));
31ConstantRange ConstantRangeTest::Wrap(APInt(16, 0xaaa), APInt(16, 0xa));
32
33TEST_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
83TEST_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
101TEST_F(ConstantRangeTest, SingleElement) {
102  EXPECT_EQ(Full.getSingleElement(), static_cast<APInt *>(NULL));
103  EXPECT_EQ(Empty.getSingleElement(), static_cast<APInt *>(NULL));
104  EXPECT_EQ(*One.getSingleElement(), APInt(16, 0xa));
105  EXPECT_EQ(Some.getSingleElement(), static_cast<APInt *>(NULL));
106  EXPECT_EQ(Wrap.getSingleElement(), static_cast<APInt *>(NULL));
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
115TEST_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
127TEST_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
153TEST_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
169TEST_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
182TEST_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
201TEST_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
221TEST_F(ConstantRangeTest, IntersectWith) {
222  EXPECT_EQ(Empty.intersectWith(Full), Empty);
223  EXPECT_EQ(Empty.intersectWith(Empty), Empty);
224  EXPECT_EQ(Empty.intersectWith(One), Empty);
225  EXPECT_EQ(Empty.intersectWith(Some), Empty);
226  EXPECT_EQ(Empty.intersectWith(Wrap), Empty);
227  EXPECT_EQ(Full.intersectWith(Full), Full);
228  EXPECT_EQ(Some.intersectWith(Some), Some);
229  EXPECT_EQ(Some.intersectWith(One), One);
230  EXPECT_EQ(Full.intersectWith(One), One);
231  EXPECT_EQ(Full.intersectWith(Some), Some);
232  EXPECT_EQ(Some.intersectWith(Wrap), Empty);
233  EXPECT_EQ(One.intersectWith(Wrap), Empty);
234  EXPECT_EQ(One.intersectWith(Wrap), Wrap.intersectWith(One));
235
236  // Klee generated testcase from PR4545.
237  // The intersection of i16 [4, 2) and [6, 5) is disjoint, looking like
238  // 01..4.6789ABCDEF where the dots represent values not in the intersection.
239  ConstantRange LHS(APInt(16, 4), APInt(16, 2));
240  ConstantRange RHS(APInt(16, 6), APInt(16, 5));
241  EXPECT_TRUE(LHS.intersectWith(RHS) == LHS);
242
243  // previous bug: intersection of [min, 3) and [2, max) should be 2
244  LHS = ConstantRange(APInt(32, -2147483646), APInt(32, 3));
245  RHS = ConstantRange(APInt(32, 2), APInt(32, 2147483646));
246  EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2)));
247
248  // [2, 0) /\ [4, 3) = [2, 0)
249  LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
250  RHS = ConstantRange(APInt(32, 4), APInt(32, 3));
251  EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2), APInt(32, 0)));
252
253  // [2, 0) /\ [4, 2) = [4, 0)
254  LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
255  RHS = ConstantRange(APInt(32, 4), APInt(32, 2));
256  EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 0)));
257
258  // [4, 2) /\ [5, 1) = [5, 1)
259  LHS = ConstantRange(APInt(32, 4), APInt(32, 2));
260  RHS = ConstantRange(APInt(32, 5), APInt(32, 1));
261  EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 5), APInt(32, 1)));
262
263  // [2, 0) /\ [7, 4) = [7, 4)
264  LHS = ConstantRange(APInt(32, 2), APInt(32, 0));
265  RHS = ConstantRange(APInt(32, 7), APInt(32, 4));
266  EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 7), APInt(32, 4)));
267
268  // [4, 2) /\ [1, 0) = [1, 0)
269  LHS = ConstantRange(APInt(32, 4), APInt(32, 2));
270  RHS = ConstantRange(APInt(32, 1), APInt(32, 0));
271  EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 2)));
272
273  // [15, 0) /\ [7, 6) = [15, 0)
274  LHS = ConstantRange(APInt(32, 15), APInt(32, 0));
275  RHS = ConstantRange(APInt(32, 7), APInt(32, 6));
276  EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 15), APInt(32, 0)));
277}
278
279TEST_F(ConstantRangeTest, UnionWith) {
280  EXPECT_EQ(Wrap.unionWith(One),
281            ConstantRange(APInt(16, 0xaaa), APInt(16, 0xb)));
282  EXPECT_EQ(One.unionWith(Wrap), Wrap.unionWith(One));
283  EXPECT_EQ(Empty.unionWith(Empty), Empty);
284  EXPECT_EQ(Full.unionWith(Full), Full);
285  EXPECT_EQ(Some.unionWith(Wrap), Full);
286
287  // PR4545
288  EXPECT_EQ(ConstantRange(APInt(16, 14), APInt(16, 1)).unionWith(
289                                    ConstantRange(APInt(16, 0), APInt(16, 8))),
290            ConstantRange(APInt(16, 14), APInt(16, 8)));
291  EXPECT_EQ(ConstantRange(APInt(16, 6), APInt(16, 4)).unionWith(
292                                    ConstantRange(APInt(16, 4), APInt(16, 0))),
293              ConstantRange(16));
294  EXPECT_EQ(ConstantRange(APInt(16, 1), APInt(16, 0)).unionWith(
295                                    ConstantRange(APInt(16, 2), APInt(16, 1))),
296              ConstantRange(16));
297}
298
299TEST_F(ConstantRangeTest, SetDifference) {
300  EXPECT_EQ(Full.difference(Empty), Full);
301  EXPECT_EQ(Full.difference(Full), Empty);
302  EXPECT_EQ(Empty.difference(Empty), Empty);
303  EXPECT_EQ(Empty.difference(Full), Empty);
304
305  ConstantRange A(APInt(16, 3), APInt(16, 7));
306  ConstantRange B(APInt(16, 5), APInt(16, 9));
307  ConstantRange C(APInt(16, 3), APInt(16, 5));
308  ConstantRange D(APInt(16, 7), APInt(16, 9));
309  ConstantRange E(APInt(16, 5), APInt(16, 4));
310  ConstantRange F(APInt(16, 7), APInt(16, 3));
311  EXPECT_EQ(A.difference(B), C);
312  EXPECT_EQ(B.difference(A), D);
313  EXPECT_EQ(E.difference(A), F);
314}
315
316TEST_F(ConstantRangeTest, SubtractAPInt) {
317  EXPECT_EQ(Full.subtract(APInt(16, 4)), Full);
318  EXPECT_EQ(Empty.subtract(APInt(16, 4)), Empty);
319  EXPECT_EQ(Some.subtract(APInt(16, 4)),
320            ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
321  EXPECT_EQ(Wrap.subtract(APInt(16, 4)),
322            ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
323  EXPECT_EQ(One.subtract(APInt(16, 4)),
324            ConstantRange(APInt(16, 0x6)));
325}
326
327TEST_F(ConstantRangeTest, Add) {
328  EXPECT_EQ(Full.add(APInt(16, 4)), Full);
329  EXPECT_EQ(Full.add(Full), Full);
330  EXPECT_EQ(Full.add(Empty), Empty);
331  EXPECT_EQ(Full.add(One), Full);
332  EXPECT_EQ(Full.add(Some), Full);
333  EXPECT_EQ(Full.add(Wrap), Full);
334  EXPECT_EQ(Empty.add(Empty), Empty);
335  EXPECT_EQ(Empty.add(One), Empty);
336  EXPECT_EQ(Empty.add(Some), Empty);
337  EXPECT_EQ(Empty.add(Wrap), Empty);
338  EXPECT_EQ(Empty.add(APInt(16, 4)), Empty);
339  EXPECT_EQ(Some.add(APInt(16, 4)),
340            ConstantRange(APInt(16, 0xe), APInt(16, 0xaae)));
341  EXPECT_EQ(Wrap.add(APInt(16, 4)),
342            ConstantRange(APInt(16, 0xaae), APInt(16, 0xe)));
343  EXPECT_EQ(One.add(APInt(16, 4)),
344            ConstantRange(APInt(16, 0xe)));
345}
346
347TEST_F(ConstantRangeTest, Sub) {
348  EXPECT_EQ(Full.sub(APInt(16, 4)), Full);
349  EXPECT_EQ(Full.sub(Full), Full);
350  EXPECT_EQ(Full.sub(Empty), Empty);
351  EXPECT_EQ(Full.sub(One), Full);
352  EXPECT_EQ(Full.sub(Some), Full);
353  EXPECT_EQ(Full.sub(Wrap), Full);
354  EXPECT_EQ(Empty.sub(Empty), Empty);
355  EXPECT_EQ(Empty.sub(One), Empty);
356  EXPECT_EQ(Empty.sub(Some), Empty);
357  EXPECT_EQ(Empty.sub(Wrap), Empty);
358  EXPECT_EQ(Empty.sub(APInt(16, 4)), Empty);
359  EXPECT_EQ(Some.sub(APInt(16, 4)),
360            ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
361  EXPECT_EQ(Some.sub(Some),
362            ConstantRange(APInt(16, 0xf561), APInt(16, 0xaa0)));
363  EXPECT_EQ(Wrap.sub(APInt(16, 4)),
364            ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
365  EXPECT_EQ(One.sub(APInt(16, 4)),
366            ConstantRange(APInt(16, 0x6)));
367}
368
369TEST_F(ConstantRangeTest, Multiply) {
370  EXPECT_EQ(Full.multiply(Full), Full);
371  EXPECT_EQ(Full.multiply(Empty), Empty);
372  EXPECT_EQ(Full.multiply(One), Full);
373  EXPECT_EQ(Full.multiply(Some), Full);
374  EXPECT_EQ(Full.multiply(Wrap), Full);
375  EXPECT_EQ(Empty.multiply(Empty), Empty);
376  EXPECT_EQ(Empty.multiply(One), Empty);
377  EXPECT_EQ(Empty.multiply(Some), Empty);
378  EXPECT_EQ(Empty.multiply(Wrap), Empty);
379  EXPECT_EQ(One.multiply(One), ConstantRange(APInt(16, 0xa*0xa),
380                                             APInt(16, 0xa*0xa + 1)));
381  EXPECT_EQ(One.multiply(Some), ConstantRange(APInt(16, 0xa*0xa),
382                                              APInt(16, 0xa*0xaa9 + 1)));
383  EXPECT_EQ(One.multiply(Wrap), Full);
384  EXPECT_EQ(Some.multiply(Some), Full);
385  EXPECT_EQ(Some.multiply(Wrap), Full);
386  EXPECT_EQ(Wrap.multiply(Wrap), Full);
387
388  ConstantRange Zero(APInt(16, 0));
389  EXPECT_EQ(Zero.multiply(Full), Zero);
390  EXPECT_EQ(Zero.multiply(Some), Zero);
391  EXPECT_EQ(Zero.multiply(Wrap), Zero);
392  EXPECT_EQ(Full.multiply(Zero), Zero);
393  EXPECT_EQ(Some.multiply(Zero), Zero);
394  EXPECT_EQ(Wrap.multiply(Zero), Zero);
395
396  // http://llvm.org/PR4545
397  EXPECT_EQ(ConstantRange(APInt(4, 1), APInt(4, 6)).multiply(
398                ConstantRange(APInt(4, 6), APInt(4, 2))),
399            ConstantRange(4, /*isFullSet=*/true));
400}
401
402TEST_F(ConstantRangeTest, UMax) {
403  EXPECT_EQ(Full.umax(Full), Full);
404  EXPECT_EQ(Full.umax(Empty), Empty);
405  EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
406  EXPECT_EQ(Full.umax(Wrap), Full);
407  EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
408  EXPECT_EQ(Empty.umax(Empty), Empty);
409  EXPECT_EQ(Empty.umax(Some), Empty);
410  EXPECT_EQ(Empty.umax(Wrap), Empty);
411  EXPECT_EQ(Empty.umax(One), Empty);
412  EXPECT_EQ(Some.umax(Some), Some);
413  EXPECT_EQ(Some.umax(Wrap), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
414  EXPECT_EQ(Some.umax(One), Some);
415  // TODO: ConstantRange is currently over-conservative here.
416  EXPECT_EQ(Wrap.umax(Wrap), Full);
417  EXPECT_EQ(Wrap.umax(One), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
418  EXPECT_EQ(One.umax(One), One);
419}
420
421TEST_F(ConstantRangeTest, SMax) {
422  EXPECT_EQ(Full.smax(Full), Full);
423  EXPECT_EQ(Full.smax(Empty), Empty);
424  EXPECT_EQ(Full.smax(Some), ConstantRange(APInt(16, 0xa),
425                                           APInt::getSignedMinValue(16)));
426  EXPECT_EQ(Full.smax(Wrap), Full);
427  EXPECT_EQ(Full.smax(One), ConstantRange(APInt(16, 0xa),
428                                          APInt::getSignedMinValue(16)));
429  EXPECT_EQ(Empty.smax(Empty), Empty);
430  EXPECT_EQ(Empty.smax(Some), Empty);
431  EXPECT_EQ(Empty.smax(Wrap), Empty);
432  EXPECT_EQ(Empty.smax(One), Empty);
433  EXPECT_EQ(Some.smax(Some), Some);
434  EXPECT_EQ(Some.smax(Wrap), ConstantRange(APInt(16, 0xa),
435                                           APInt(16, (uint64_t)INT16_MIN)));
436  EXPECT_EQ(Some.smax(One), Some);
437  EXPECT_EQ(Wrap.smax(One), ConstantRange(APInt(16, 0xa),
438                                          APInt(16, (uint64_t)INT16_MIN)));
439  EXPECT_EQ(One.smax(One), One);
440}
441
442TEST_F(ConstantRangeTest, UDiv) {
443  EXPECT_EQ(Full.udiv(Full), Full);
444  EXPECT_EQ(Full.udiv(Empty), Empty);
445  EXPECT_EQ(Full.udiv(One), ConstantRange(APInt(16, 0),
446                                          APInt(16, 0xffff / 0xa + 1)));
447  EXPECT_EQ(Full.udiv(Some), ConstantRange(APInt(16, 0),
448                                           APInt(16, 0xffff / 0xa + 1)));
449  EXPECT_EQ(Full.udiv(Wrap), Full);
450  EXPECT_EQ(Empty.udiv(Empty), Empty);
451  EXPECT_EQ(Empty.udiv(One), Empty);
452  EXPECT_EQ(Empty.udiv(Some), Empty);
453  EXPECT_EQ(Empty.udiv(Wrap), Empty);
454  EXPECT_EQ(One.udiv(One), ConstantRange(APInt(16, 1)));
455  EXPECT_EQ(One.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 2)));
456  EXPECT_EQ(One.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
457  EXPECT_EQ(Some.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 0x111)));
458  EXPECT_EQ(Some.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
459  EXPECT_EQ(Wrap.udiv(Wrap), Full);
460}
461
462TEST_F(ConstantRangeTest, Shl) {
463  EXPECT_EQ(Full.shl(Full), Full);
464  EXPECT_EQ(Full.shl(Empty), Empty);
465  EXPECT_EQ(Full.shl(One), Full);    // TODO: [0, (-1 << 0xa) + 1)
466  EXPECT_EQ(Full.shl(Some), Full);   // TODO: [0, (-1 << 0xa) + 1)
467  EXPECT_EQ(Full.shl(Wrap), Full);
468  EXPECT_EQ(Empty.shl(Empty), Empty);
469  EXPECT_EQ(Empty.shl(One), Empty);
470  EXPECT_EQ(Empty.shl(Some), Empty);
471  EXPECT_EQ(Empty.shl(Wrap), Empty);
472  EXPECT_EQ(One.shl(One), ConstantRange(APInt(16, 0xa << 0xa),
473                                        APInt(16, (0xa << 0xa) + 1)));
474  EXPECT_EQ(One.shl(Some), Full);    // TODO: [0xa << 0xa, 0)
475  EXPECT_EQ(One.shl(Wrap), Full);    // TODO: [0xa, 0xa << 14 + 1)
476  EXPECT_EQ(Some.shl(Some), Full);   // TODO: [0xa << 0xa, 0xfc01)
477  EXPECT_EQ(Some.shl(Wrap), Full);   // TODO: [0xa, 0x7ff << 0x5 + 1)
478  EXPECT_EQ(Wrap.shl(Wrap), Full);
479}
480
481TEST_F(ConstantRangeTest, Lshr) {
482  EXPECT_EQ(Full.lshr(Full), Full);
483  EXPECT_EQ(Full.lshr(Empty), Empty);
484  EXPECT_EQ(Full.lshr(One), ConstantRange(APInt(16, 0),
485                                          APInt(16, (0xffff >> 0xa) + 1)));
486  EXPECT_EQ(Full.lshr(Some), ConstantRange(APInt(16, 0),
487                                           APInt(16, (0xffff >> 0xa) + 1)));
488  EXPECT_EQ(Full.lshr(Wrap), Full);
489  EXPECT_EQ(Empty.lshr(Empty), Empty);
490  EXPECT_EQ(Empty.lshr(One), Empty);
491  EXPECT_EQ(Empty.lshr(Some), Empty);
492  EXPECT_EQ(Empty.lshr(Wrap), Empty);
493  EXPECT_EQ(One.lshr(One), ConstantRange(APInt(16, 0)));
494  EXPECT_EQ(One.lshr(Some), ConstantRange(APInt(16, 0)));
495  EXPECT_EQ(One.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb)));
496  EXPECT_EQ(Some.lshr(Some), ConstantRange(APInt(16, 0),
497                                           APInt(16, (0xaaa >> 0xa) + 1)));
498  EXPECT_EQ(Some.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
499  EXPECT_EQ(Wrap.lshr(Wrap), Full);
500}
501
502TEST(ConstantRange, MakeICmpRegion) {
503  // PR8250
504  ConstantRange SMax = ConstantRange(APInt::getSignedMaxValue(32));
505  EXPECT_TRUE(ConstantRange::makeICmpRegion(ICmpInst::ICMP_SGT,
506                                            SMax).isEmptySet());
507}
508
509}  // anonymous namespace
510