forked from OSchip/llvm-project
[ConstantRangeTest] Add helper to enumerate APInts (NFC)
While ForeachNumInConstantRange(ConstantRange::getFull(Bits)) works, it's somewhat roundabout, and I keep looking for this function.
This commit is contained in:
parent
52a3ed5b93
commit
1c5d636af1
|
@ -28,6 +28,14 @@ protected:
|
||||||
static ConstantRange Wrap;
|
static ConstantRange Wrap;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename Fn>
|
||||||
|
static void EnumerateAPInts(unsigned Bits, Fn TestFn) {
|
||||||
|
APInt N(Bits, 0);
|
||||||
|
do {
|
||||||
|
TestFn(N);
|
||||||
|
} while (++N != 0);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename Fn>
|
template<typename Fn>
|
||||||
static void EnumerateConstantRanges(unsigned Bits, Fn TestFn) {
|
static void EnumerateConstantRanges(unsigned Bits, Fn TestFn) {
|
||||||
unsigned Max = 1 << Bits;
|
unsigned Max = 1 << Bits;
|
||||||
|
@ -1824,8 +1832,7 @@ void TestNoWrapRegionExhaustive(Instruction::BinaryOps BinOp,
|
||||||
|
|
||||||
ConstantRange NoWrap =
|
ConstantRange NoWrap =
|
||||||
ConstantRange::makeGuaranteedNoWrapRegion(BinOp, CR, NoWrapKind);
|
ConstantRange::makeGuaranteedNoWrapRegion(BinOp, CR, NoWrapKind);
|
||||||
ConstantRange Full = ConstantRange::getFull(Bits);
|
EnumerateAPInts(Bits, [&](const APInt &N1) {
|
||||||
ForeachNumInConstantRange(Full, [&](const APInt &N1) {
|
|
||||||
bool NoOverflow = true;
|
bool NoOverflow = true;
|
||||||
bool Overflow = true;
|
bool Overflow = true;
|
||||||
ForeachNumInConstantRange(CR, [&](const APInt &N2) {
|
ForeachNumInConstantRange(CR, [&](const APInt &N2) {
|
||||||
|
@ -1986,17 +1993,18 @@ TEST(ConstantRange, GetEquivalentICmp) {
|
||||||
EXPECT_EQ(Pred, CmpInst::ICMP_NE);
|
EXPECT_EQ(Pred, CmpInst::ICMP_NE);
|
||||||
EXPECT_EQ(RHS, APInt(32, -1));
|
EXPECT_EQ(RHS, APInt(32, -1));
|
||||||
|
|
||||||
EnumerateConstantRanges(4, [](const ConstantRange &CR) {
|
unsigned Bits = 4;
|
||||||
|
EnumerateConstantRanges(Bits, [Bits](const ConstantRange &CR) {
|
||||||
CmpInst::Predicate Pred;
|
CmpInst::Predicate Pred;
|
||||||
APInt RHS, Offset;
|
APInt RHS, Offset;
|
||||||
CR.getEquivalentICmp(Pred, RHS, Offset);
|
CR.getEquivalentICmp(Pred, RHS, Offset);
|
||||||
ForeachNumInConstantRange(ConstantRange::getFull(4), [&](const APInt &N) {
|
EnumerateAPInts(Bits, [&](const APInt &N) {
|
||||||
bool Result = ICmpInst::compare(N + Offset, RHS, Pred);
|
bool Result = ICmpInst::compare(N + Offset, RHS, Pred);
|
||||||
EXPECT_EQ(CR.contains(N), Result);
|
EXPECT_EQ(CR.contains(N), Result);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (CR.getEquivalentICmp(Pred, RHS)) {
|
if (CR.getEquivalentICmp(Pred, RHS)) {
|
||||||
ForeachNumInConstantRange(ConstantRange::getFull(4), [&](const APInt &N) {
|
EnumerateAPInts(Bits, [&](const APInt &N) {
|
||||||
bool Result = ICmpInst::compare(N, RHS, Pred);
|
bool Result = ICmpInst::compare(N, RHS, Pred);
|
||||||
EXPECT_EQ(CR.contains(N), Result);
|
EXPECT_EQ(CR.contains(N), Result);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue