diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2019-04-26 16:50:31 +0000 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2019-04-26 16:50:31 +0000 |
commit | c0fa4ec01dd1c3837b31e142ceb20845421e34ab (patch) | |
tree | 6042ac43d048e3a892c23205d17652c9ef4d025d /llvm/unittests/IR/ConstantRangeTest.cpp | |
parent | f30f261dc531b289bc74c157178e5bbc4a6ff7c9 (diff) | |
download | llvm-c0fa4ec01dd1c3837b31e142ceb20845421e34ab.zip llvm-c0fa4ec01dd1c3837b31e142ceb20845421e34ab.tar.gz llvm-c0fa4ec01dd1c3837b31e142ceb20845421e34ab.tar.bz2 |
[ConstantRange] Add abs() support
Add support for abs() to ConstantRange. This will allow to handle
SPF_ABS select flavor in LVI and will also come in handy as a
primitive for the srem implementation.
The implementation is slightly tricky, because a) abs of signed min
is signed min and b) sign-wrapped ranges may have an abs() that is
smaller than a full range, so we need to explicitly handle them.
Differential Revision: https://reviews.llvm.org/D61084
llvm-svn: 359321
Diffstat (limited to 'llvm/unittests/IR/ConstantRangeTest.cpp')
-rw-r--r-- | llvm/unittests/IR/ConstantRangeTest.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp index b9d8fe0..fdd1e6a 100644 --- a/llvm/unittests/IR/ConstantRangeTest.cpp +++ b/llvm/unittests/IR/ConstantRangeTest.cpp @@ -1797,4 +1797,30 @@ TEST_F(ConstantRangeTest, SSubSat) { }); } +TEST_F(ConstantRangeTest, Abs) { + unsigned Bits = 4; + EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) { + // We're working with unsigned integers here, because it makes the signed + // min case non-wrapping. + APInt Min = APInt::getMaxValue(Bits); + APInt Max = APInt::getMinValue(Bits); + ForeachNumInConstantRange(CR, [&](const APInt &N) { + APInt AbsN = N.abs(); + if (AbsN.ult(Min)) + Min = AbsN; + if (AbsN.ugt(Max)) + Max = AbsN; + }); + + ConstantRange AbsCR = CR.abs(); + if (Min.ugt(Max)) { + EXPECT_TRUE(AbsCR.isEmptySet()); + return; + } + + ConstantRange Exact = ConstantRange::getNonEmpty(Min, Max + 1); + EXPECT_EQ(Exact, AbsCR); + }); +} + } // anonymous namespace |