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