@@ -2788,4 +2788,52 @@ TEST_F(ConstantRangeTest, isSizeLargerThan) {
27882788 EXPECT_FALSE (One.isSizeLargerThan (1 ));
27892789}
27902790
2791+ TEST_F (ConstantRangeTest, MakeMaskNotEqualRange) {
2792+ // Mask: 0b0001, C: 0b0001. MMNE() = [2, 1)
2793+ ConstantRange CR (APInt (4 , 2 ), APInt (4 , 1 ));
2794+ EXPECT_EQ (CR, ConstantRange::makeMaskNotEqualRange (APInt (4 , 1 ), APInt (4 , 1 )));
2795+ EXPECT_NE (CR, ConstantRange::makeMaskNotEqualRange (APInt (4 , 0 ),
2796+ APInt (4 , -1 , true )));
2797+ EXPECT_TRUE (CR.contains (APInt (4 , 7 )));
2798+ EXPECT_TRUE (CR.contains (APInt (4 , 15 )));
2799+
2800+ // Mask: 0b0100, C: 0b0100. MMNE() = [-8, 4)
2801+ ConstantRange CR2 (APInt (4 , -8 , true ), APInt (4 , 4 ));
2802+ auto MMNE = ConstantRange::makeMaskNotEqualRange (APInt (4 , 4 ), APInt (4 , 4 ));
2803+ EXPECT_EQ (CR2, MMNE);
2804+ EXPECT_NE (ConstantRange::getNonEmpty (APInt (4 , 0 ), APInt (4 , -4 , true )), MMNE);
2805+
2806+ // CR: [-16, -8). MMNE() = [-8, -16)
2807+ ConstantRange CR3 (APInt (8 , 240 ), APInt (8 , 248 ));
2808+ EXPECT_EQ (CR3.inverse (),
2809+ ConstantRange::makeMaskNotEqualRange (APInt (8 , 248 ), APInt (8 , 240 )));
2810+
2811+ // Mask: 0, C: 0b1111: unsatisfiable.
2812+ EXPECT_EQ (ConstantRange::getFull (4 ),
2813+ ConstantRange::makeMaskNotEqualRange (APInt (4 , 0 ), APInt (4 , 15 )));
2814+ }
2815+
2816+ TEST_F (ConstantRangeTest, MakeMaskNotEqualRangeExhaustive) {
2817+ unsigned Bits = 4 ;
2818+ unsigned Max = 1 << Bits;
2819+
2820+ EnumerateAPInts (Bits, [&](const APInt &Mask) {
2821+ EnumerateAPInts (Bits, [&](const APInt &C) {
2822+ SmallBitVector Elems (Max);
2823+ for (unsigned N = 0 ; N < Max; ++N) {
2824+ APInt Num (Bits, N);
2825+ if ((Num & Mask) == C)
2826+ continue ;
2827+ Elems.set (Num.getZExtValue ());
2828+ }
2829+
2830+ // Only test optimality with PreferSmallest. E.g., given Mask = 0b0001, C
2831+ // = 0b0001, a possible better range would be [0, 15) when preferring the
2832+ // smallest unsigned, however we conservatively return [2, 1).
2833+ TestRange (ConstantRange::makeMaskNotEqualRange (Mask, C), Elems,
2834+ PreferSmallest, {});
2835+ });
2836+ });
2837+ }
2838+
27912839} // anonymous namespace
0 commit comments