Home | History | Annotate | Download | only in IR

Lines Matching full:constantrange

1 //===- ConstantRangeTest.cpp - ConstantRange tests ------------------------===//
10 #include "llvm/IR/ConstantRange.h"
21 static ConstantRange Full;
22 static ConstantRange Empty;
23 static ConstantRange One;
24 static ConstantRange Some;
25 static ConstantRange Wrap;
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));
122 ConstantRange Wrap(APInt(4, 7), APInt(4, 3));
123 ConstantRange Wrap2(APInt(4, 8), APInt(4, 7));
150 EXPECT_EQ(ConstantRange(APInt(4, 7), APInt(4, 0)).getSignedMax(),
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());
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);
178 EXPECT_EQ(TOne, ConstantRange(One.getLower().trunc(10),
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)));
191 EXPECT_EQ(ZOne, ConstantRange(One.getLower().zext(20),
193 EXPECT_EQ(ZSome, ConstantRange(Some.getLower().zext(20),
195 EXPECT_EQ(ZWrap, ConstantRange(APInt(20, 0), APInt(20, 0x10000)));
198 ConstantRange FiveZero(APInt(3, 5), APInt(3, 0));
199 EXPECT_EQ(FiveZero.zeroExtend(7), ConstantRange(APInt(7, 5), APInt(7, 8)));
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),
211 EXPECT_EQ(SOne, ConstantRange(One.getLower().sext(20),
213 EXPECT_EQ(SSome, ConstantRange(Some.getLower().sext(20),
215 EXPECT_EQ(SWrap, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true),
218 EXPECT_EQ(ConstantRange(APInt(8, 120), APInt(8, 140)).signExtend(16),
219 ConstantRange(APInt(16, -128), APInt(16, 128)));
221 EXPECT_EQ(ConstantRange(APInt(16, 0x0200), APInt(16, 0x8000)).signExtend(19),
222 ConstantRange(APInt(19, 0x0200), APInt(19, 0x8000)));
243 ConstantRange LHS(APInt(16, 4), APInt(16, 2));
244 ConstantRange RHS(APInt(16, 6), APInt(16, 5));
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)));
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)));
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)));
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)));
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)));
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)));
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)));
285 ConstantRange(APInt(16, 0xaaa), APInt(16, 0xb)));
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));
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));
324 ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
326 ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
328 ConstantRange(APInt(16, 0x6)));
344 ConstantRange(APInt(16, 0xe), APInt(16, 0xaae)));
346 ConstantRange(APInt(16, 0xaae), APInt(16, 0xe)));
348 ConstantRange(APInt(16, 0xe)));
364 ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6)));
366 ConstantRange(APInt(16, 0xf561), APInt(16, 0xaa0)));
368 ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6)));
370 ConstantRange(APInt(16, 0x6)));
383 EXPECT_EQ(One.multiply(One), ConstantRange(APInt(16, 0xa*0xa),
385 EXPECT_EQ(One.multiply(Some), ConstantRange(APInt(16, 0xa*0xa),
392 ConstantRange Zero(APInt(16, 0));
401 EXPECT_EQ(ConstantRange(APInt(4, 1), APInt(4, 6)).multiply(
402 ConstantRange(APInt(4, 6), APInt(4, 2))),
403 ConstantRange(4, /*isFullSet=*/true));
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)));
416 ConstantRange(APInt(16, 0xa), APInt(16, 0)));
418 EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
424 EXPECT_EQ(Some.umax(Wrap), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
426 // TODO: ConstantRange is currently over-conservative here.
428 EXPECT_EQ(Wrap.umax(One), ConstantRange(APInt(16, 0xa), APInt(16, 0)));
435 EXPECT_EQ(Full.smax(Some), ConstantRange(APInt(16, 0xa),
438 EXPECT_EQ(Full.smax(One), ConstantRange(APInt(16, 0xa),
445 EXPECT_EQ(Some.smax(Wrap), ConstantRange(APInt(16, 0xa),
448 EXPECT_EQ(Wrap.smax(One), ConstantRange(APInt(16, 0xa),
456 EXPECT_EQ(Full.udiv(One), ConstantRange(APInt(16, 0),
458 EXPECT_EQ(Full.udiv(Some), ConstantRange(APInt(16, 0),
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)));
483 EXPECT_EQ(One.shl(One), ConstantRange(APInt(16, 0xa << 0xa),
495 EXPECT_EQ(Full.lshr(One), ConstantRange(APInt(16, 0),
497 EXPECT_EQ(Full.lshr(Some), ConstantRange(APInt(16, 0),
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),
509 EXPECT_EQ(Some.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa)));
513 TEST(ConstantRange, MakeAllowedICmpRegion) {
515 ConstantRange SMax = ConstantRange(APInt::getSignedMaxValue(32));
516 EXPECT_TRUE(ConstantRange::makeAllowedICmpRegion(ICmpInst::ICMP_SGT, SMax)
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);
525 EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_NE, LowHalf),
529 ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_NE, HighHalf),
532 EXPECT_TRUE(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_EQ,
535 ConstantRange UnsignedSample(APInt(8, 5), APInt(8, 200));
537 EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_ULT,
539 ConstantRange(APInt(8, 0), APInt(8, 5)));
541 EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_ULE,
543 ConstantRange(APInt(8, 0), APInt(8, 6)));
545 EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_UGT,
547 ConstantRange(APInt(8, 200), APInt(8, 0)));
549 EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_UGE,
551 ConstantRange(APInt(8, 199), APInt(8, 0)));
553 ConstantRange SignedSample(APInt(8, -5), APInt(8, 5));
556 ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SLT, SignedSample),
557 ConstantRange(APInt(8, -128), APInt(8, -5)));
560 ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SLE, SignedSample),
561 ConstantRange(APInt(8, -128), APInt(8, -4)));
564 ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SGT, SignedSample),
565 ConstantRange(APInt(8, 5), APInt(8, -128)));
568 ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SGE, SignedSample),
569 ConstantRange(APInt(8, 4), APInt(8, -128)));
572 TEST(ConstantRange, MakeOverflowingRegion) {
581 ConstantRange::makeNoWrapRegion(Instruction::Add, C, OBO::NoUnsignedWrap);
586 ConstantRange::makeNoWrapRegion(Instruction::Add, C, OBO::NoSignedWrap);
590 auto NoWrapRegion = ConstantRange::makeNoWrapRegion(