@@ -94,28 +94,37 @@ bool PreferSmallestNonFullSigned(const ConstantRange &CR1,
94
94
return PreferSmallestSigned (CR1, CR2);
95
95
}
96
96
97
+
98
+
97
99
// Check whether constant range CR is an optimal approximation of the set
98
100
// Elems under the given PreferenceFn. The preference function should return
99
101
// true if the first range argument is strictly preferred to the second one.
100
102
static void TestRange (const ConstantRange &CR, const SmallBitVector &Elems,
101
- PreferFn PreferenceFn) {
103
+ PreferFn PreferenceFn, ArrayRef<ConstantRange> Inputs ) {
102
104
unsigned BitWidth = CR.getBitWidth ();
103
105
104
106
// Check conservative correctness.
105
107
for (unsigned Elem : Elems.set_bits ()) {
106
108
EXPECT_TRUE (CR.contains (APInt (BitWidth, Elem)));
107
109
}
108
110
109
- // Make sure we have at least two elements for the code below.
111
+ // Make sure we have at least one element for the code below.
110
112
if (Elems.none ()) {
111
113
EXPECT_TRUE (CR.isEmptySet ());
112
114
return ;
113
115
}
114
116
115
- if (Elems.count () == 1 ) {
116
- EXPECT_TRUE (CR.isSingleElement ());
117
- return ;
118
- }
117
+ auto NotPreferred = [&](const ConstantRange &PossibleCR) {
118
+ if (!PreferenceFn (PossibleCR, CR))
119
+ return testing::AssertionSuccess ();
120
+
121
+ testing::AssertionResult Result = testing::AssertionFailure ();
122
+ Result << " Inputs = " ;
123
+ for (const ConstantRange &Input : Inputs)
124
+ Result << Input << " , " ;
125
+ Result << " CR = " << CR << " , BetterCR = " << PossibleCR;
126
+ return Result;
127
+ };
119
128
120
129
// Look at all pairs of adjacent elements and the slack-free ranges
121
130
// [Elem, PrevElem] they imply. Check that none of the ranges are strictly
@@ -130,12 +139,16 @@ static void TestRange(const ConstantRange &CR, const SmallBitVector &Elems,
130
139
ConstantRange PossibleCR =
131
140
ConstantRange::getNonEmpty (APInt (BitWidth, Elem),
132
141
APInt (BitWidth, PrevElem) + 1 );
133
- // There should be no range that is preferred over CR.
134
- EXPECT_FALSE (PreferenceFn (PossibleCR, CR))
135
- << " CR = " << CR << " , BetterCR = " << PossibleCR;
142
+ // We get a full range any time PrevElem and Elem are adjacent. Avoid
143
+ // repeated checks by skipping here, and explicitly checking below instead.
144
+ if (!PossibleCR.isFullSet ()) {
145
+ EXPECT_TRUE (NotPreferred (PossibleCR));
146
+ }
136
147
137
148
PrevElem = Elem;
138
149
} while (Elem != FirstElem);
150
+
151
+ EXPECT_TRUE (NotPreferred (ConstantRange::getFull (BitWidth)));
139
152
}
140
153
141
154
using UnaryRangeFn = llvm::function_ref<ConstantRange(const ConstantRange &)>;
@@ -150,7 +163,7 @@ static void TestUnaryOpExhaustive(UnaryRangeFn RangeFn, UnaryIntFn IntFn,
150
163
if (Optional<APInt> ResultN = IntFn (N))
151
164
Elems.set (ResultN->getZExtValue ());
152
165
});
153
- TestRange (RangeFn (CR), Elems, PreferenceFn);
166
+ TestRange (RangeFn (CR), Elems, PreferenceFn, {CR} );
154
167
});
155
168
}
156
169
@@ -171,7 +184,7 @@ static void TestBinaryOpExhaustive(BinaryRangeFn RangeFn, BinaryIntFn IntFn,
171
184
Elems.set (ResultN->getZExtValue ());
172
185
});
173
186
});
174
- TestRange (RangeFn (CR1, CR2), Elems, PreferenceFn);
187
+ TestRange (RangeFn (CR1, CR2), Elems, PreferenceFn, {CR1, CR2} );
175
188
});
176
189
}
177
190
@@ -543,13 +556,13 @@ void testBinarySetOperationExhaustive(Fn1 OpFn, Fn2 InResultFn) {
543
556
Elems.set (Num.getZExtValue ());
544
557
545
558
ConstantRange SmallestCR = OpFn (CR1, CR2, ConstantRange::Smallest);
546
- TestRange (SmallestCR, Elems, PreferSmallest);
559
+ TestRange (SmallestCR, Elems, PreferSmallest, {CR1, CR2} );
547
560
548
561
ConstantRange UnsignedCR = OpFn (CR1, CR2, ConstantRange::Unsigned);
549
- TestRange (UnsignedCR, Elems, PreferSmallestNonFullUnsigned);
562
+ TestRange (UnsignedCR, Elems, PreferSmallestNonFullUnsigned, {CR1, CR2} );
550
563
551
564
ConstantRange SignedCR = OpFn (CR1, CR2, ConstantRange::Signed);
552
- TestRange (SignedCR, Elems, PreferSmallestNonFullSigned);
565
+ TestRange (SignedCR, Elems, PreferSmallestNonFullSigned, {CR1, CR2} );
553
566
});
554
567
}
555
568
0 commit comments