diff options
author | Roman Lebedev <lebedev.ri@gmail.com> | 2021-04-10 17:58:47 +0300 |
---|---|---|
committer | Roman Lebedev <lebedev.ri@gmail.com> | 2021-04-10 19:09:52 +0300 |
commit | 17cf2c94230bc107e7294ef84fad3b47f4cd1b73 (patch) | |
tree | 85a7b7ec57c83340778733ffba90c16d38ed80f4 /llvm/unittests/IR/ConstantRangeTest.cpp | |
parent | 0c184154969c020db416bd7066af80ffd2a27ac4 (diff) | |
download | llvm-17cf2c94230bc107e7294ef84fad3b47f4cd1b73.zip llvm-17cf2c94230bc107e7294ef84fad3b47f4cd1b73.tar.gz llvm-17cf2c94230bc107e7294ef84fad3b47f4cd1b73.tar.bz2 |
[NFC][ConstantRange] Add 'icmp' helper method
"Does the predicate hold between two ranges?"
Not very surprisingly, some places were already doing this check,
without explicitly naming the algorithm, cleanup them all.
Diffstat (limited to 'llvm/unittests/IR/ConstantRangeTest.cpp')
-rw-r--r-- | llvm/unittests/IR/ConstantRangeTest.cpp | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp index 12362b9..f8816e4 100644 --- a/llvm/unittests/IR/ConstantRangeTest.cpp +++ b/llvm/unittests/IR/ConstantRangeTest.cpp @@ -6,9 +6,10 @@ // //===----------------------------------------------------------------------===// +#include "llvm/IR/ConstantRange.h" #include "llvm/ADT/BitVector.h" +#include "llvm/ADT/Sequence.h" #include "llvm/ADT/SmallBitVector.h" -#include "llvm/IR/ConstantRange.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Operator.h" #include "llvm/Support/KnownBits.h" @@ -1509,6 +1510,52 @@ TEST(ConstantRange, MakeSatisfyingICmpRegion) { ConstantRange(APInt(8, 4), APInt(8, -128))); } +static bool icmp(CmpInst::Predicate Pred, const APInt &LHS, const APInt &RHS) { + switch (Pred) { + case CmpInst::Predicate::ICMP_EQ: + return LHS.eq(RHS); + case CmpInst::Predicate::ICMP_NE: + return LHS.ne(RHS); + case CmpInst::Predicate::ICMP_UGT: + return LHS.ugt(RHS); + case CmpInst::Predicate::ICMP_UGE: + return LHS.uge(RHS); + case CmpInst::Predicate::ICMP_ULT: + return LHS.ult(RHS); + case CmpInst::Predicate::ICMP_ULE: + return LHS.ule(RHS); + case CmpInst::Predicate::ICMP_SGT: + return LHS.sgt(RHS); + case CmpInst::Predicate::ICMP_SGE: + return LHS.sge(RHS); + case CmpInst::Predicate::ICMP_SLT: + return LHS.slt(RHS); + case CmpInst::Predicate::ICMP_SLE: + return LHS.sle(RHS); + default: + llvm_unreachable("Not an ICmp predicate!"); + } +} + +void ICmpTestImpl(CmpInst::Predicate Pred) { + unsigned Bits = 4; + EnumerateTwoConstantRanges( + Bits, [&](const ConstantRange &CR1, const ConstantRange &CR2) { + bool Exhaustive = true; + ForeachNumInConstantRange(CR1, [&](const APInt &N1) { + ForeachNumInConstantRange( + CR2, [&](const APInt &N2) { Exhaustive &= icmp(Pred, N1, N2); }); + }); + EXPECT_EQ(CR1.icmp(Pred, CR2), Exhaustive); + }); +} + +TEST(ConstantRange, ICmp) { + for (auto Pred : seq<unsigned>(CmpInst::Predicate::FIRST_ICMP_PREDICATE, + 1 + CmpInst::Predicate::LAST_ICMP_PREDICATE)) + ICmpTestImpl((CmpInst::Predicate)Pred); +} + TEST(ConstantRange, MakeGuaranteedNoWrapRegion) { const int IntMin4Bits = 8; const int IntMax4Bits = 7; |