llvm.org GIT mirror llvm / 58c0bdd
[ConstantRange] Add isWrappedSet() and isUpperSignWrapped() Split off from D59749. This adds isWrappedSet() and isUpperSignWrapped() set with the same behavior as isSignWrappedSet() and isUpperWrapped() for the respectively other domain. The methods isWrappedSet() and isSignWrappedSet() will not consider ranges of the form [X, Max] == [X, 0) and [X, SignedMax] == [X, SignedMin) to be wrapping, while isUpperWrapped() and isUpperSignWrapped() will. Also replace the checks in getUnsignedMin() and friends with method calls that implement the same logic. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@357112 91177308-0d34-0410-b5e6-96231b3b80d8 Nikita Popov 6 months ago
3 changed file(s) with 52 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
162162 /// Return true if this set contains no members.
163163 bool isEmptySet() const;
164164
165 /// Return true if this set wraps around the unsigned domain. Special cases:
166 /// * Empty set: Not wrapped.
167 /// * Full set: Not wrapped.
168 /// * [X, 0) == [X, Max]: Not wrapped.
169 bool isWrappedSet() const;
170
165171 /// Return true if the exclusive upper bound wraps around the unsigned
166172 /// domain. Special cases:
167173 /// * Empty set: Not wrapped.
174180 /// * Full set: Not wrapped.
175181 /// * [X, SignedMin) == [X, SignedMax]: Not wrapped.
176182 bool isSignWrappedSet() const;
183
184 /// Return true if the (exclusive) upper bound wraps around the signed
185 /// domain. Special cases:
186 /// * Empty set: Not wrapped.
187 /// * Full set: Not wrapped.
188 /// * [X, SignedMin): Wrapped.
189 bool isUpperSignWrapped() const;
177190
178191 /// Return true if the specified value is in the set.
179192 bool contains(const APInt &Val) const;
343343 return Lower == Upper && Lower.isMinValue();
344344 }
345345
346 bool ConstantRange::isWrappedSet() const {
347 return Lower.ugt(Upper) && !Upper.isNullValue();
348 }
349
346350 bool ConstantRange::isUpperWrapped() const {
347351 return Lower.ugt(Upper);
348352 }
349353
350354 bool ConstantRange::isSignWrappedSet() const {
351355 return Lower.sgt(Upper) && !Upper.isMinSignedValue();
356 }
357
358 bool ConstantRange::isUpperSignWrapped() const {
359 return Lower.sgt(Upper);
352360 }
353361
354362 APInt ConstantRange::getSetSize() const {
387395 }
388396
389397 APInt ConstantRange::getUnsignedMin() const {
390 if (isFullSet() || (isUpperWrapped() && !getUpper().isNullValue()))
398 if (isFullSet() || isWrappedSet())
391399 return APInt::getMinValue(getBitWidth());
392400 return getLower();
393401 }
394402
395403 APInt ConstantRange::getSignedMax() const {
396 if (isFullSet() || Lower.sgt(Upper))
404 if (isFullSet() || isUpperSignWrapped())
397405 return APInt::getSignedMaxValue(getBitWidth());
398406 return getUpper() - 1;
399407 }
400408
401409 APInt ConstantRange::getSignedMin() const {
402 if (isFullSet() || (Lower.sgt(Upper) && !getUpper().isMinSignedValue()))
410 if (isFullSet() || isSignWrappedSet())
403411 return APInt::getSignedMinValue(getBitWidth());
404412 return getLower();
405413 }
3434 EXPECT_TRUE(Full.isFullSet());
3535 EXPECT_FALSE(Full.isEmptySet());
3636 EXPECT_TRUE(Full.inverse().isEmptySet());
37 EXPECT_FALSE(Full.isUpperWrapped());
37 EXPECT_FALSE(Full.isWrappedSet());
3838 EXPECT_TRUE(Full.contains(APInt(16, 0x0)));
3939 EXPECT_TRUE(Full.contains(APInt(16, 0x9)));
4040 EXPECT_TRUE(Full.contains(APInt(16, 0xa)));
4444 EXPECT_FALSE(Empty.isFullSet());
4545 EXPECT_TRUE(Empty.isEmptySet());
4646 EXPECT_TRUE(Empty.inverse().isFullSet());
47 EXPECT_FALSE(Empty.isUpperWrapped());
47 EXPECT_FALSE(Empty.isWrappedSet());
4848 EXPECT_FALSE(Empty.contains(APInt(16, 0x0)));
4949 EXPECT_FALSE(Empty.contains(APInt(16, 0x9)));
5050 EXPECT_FALSE(Empty.contains(APInt(16, 0xa)));
5353
5454 EXPECT_FALSE(One.isFullSet());
5555 EXPECT_FALSE(One.isEmptySet());
56 EXPECT_FALSE(One.isUpperWrapped());
56 EXPECT_FALSE(One.isWrappedSet());
5757 EXPECT_FALSE(One.contains(APInt(16, 0x0)));
5858 EXPECT_FALSE(One.contains(APInt(16, 0x9)));
5959 EXPECT_TRUE(One.contains(APInt(16, 0xa)));
6363
6464 EXPECT_FALSE(Some.isFullSet());
6565 EXPECT_FALSE(Some.isEmptySet());
66 EXPECT_FALSE(Some.isUpperWrapped());
66 EXPECT_FALSE(Some.isWrappedSet());
6767 EXPECT_FALSE(Some.contains(APInt(16, 0x0)));
6868 EXPECT_FALSE(Some.contains(APInt(16, 0x9)));
6969 EXPECT_TRUE(Some.contains(APInt(16, 0xa)));
7272
7373 EXPECT_FALSE(Wrap.isFullSet());
7474 EXPECT_FALSE(Wrap.isEmptySet());
75 EXPECT_TRUE(Wrap.isUpperWrapped());
75 EXPECT_TRUE(Wrap.isWrappedSet());
7676 EXPECT_TRUE(Wrap.contains(APInt(16, 0x0)));
7777 EXPECT_TRUE(Wrap.contains(APInt(16, 0x9)));
7878 EXPECT_FALSE(Wrap.contains(APInt(16, 0xa)));
173173 EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 250)).isSignWrappedSet());
174174 EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 10)).isSignWrappedSet());
175175 EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 251)).isSignWrappedSet());
176 }
177
178 TEST_F(ConstantRangeTest, UpperWrapped) {
179 // The behavior here is the same as for isWrappedSet() / isSignWrappedSet().
180 EXPECT_FALSE(Full.isUpperWrapped());
181 EXPECT_FALSE(Empty.isUpperWrapped());
182 EXPECT_FALSE(One.isUpperWrapped());
183 EXPECT_FALSE(Some.isUpperWrapped());
184 EXPECT_TRUE(Wrap.isUpperWrapped());
185 EXPECT_FALSE(Full.isUpperSignWrapped());
186 EXPECT_FALSE(Empty.isUpperSignWrapped());
187 EXPECT_FALSE(One.isUpperSignWrapped());
188 EXPECT_FALSE(Some.isUpperSignWrapped());
189 EXPECT_TRUE(Wrap.isUpperSignWrapped());
190
191 // The behavior differs if Upper is the Min/SignedMin value.
192 ConstantRange CR1(APInt(8, 42), APInt::getMinValue(8));
193 EXPECT_FALSE(CR1.isWrappedSet());
194 EXPECT_TRUE(CR1.isUpperWrapped());
195
196 ConstantRange CR2(APInt(8, 42), APInt::getSignedMinValue(8));
197 EXPECT_FALSE(CR2.isSignWrappedSet());
198 EXPECT_TRUE(CR2.isUpperSignWrapped());
176199 }
177200
178201 TEST_F(ConstantRangeTest, Trunc) {