llvm.org GIT mirror llvm / d6ce6ef
[ConstantRange] Add unsignedMulMayOverflow() Same as the other ConstantRange overflow checking methods, but for unsigned mul. In this case there is no cheap overflow criterion, so using umul_ov for the implementation. Differential Revision: https://reviews.llvm.org/D60574 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358228 91177308-0d34-0410-b5e6-96231b3b80d8 Nikita Popov 6 months ago
3 changed file(s) with 35 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
398398 /// Return whether signed sub of the two ranges always/never overflows.
399399 OverflowResult signedSubMayOverflow(const ConstantRange &Other) const;
400400
401 /// Return whether unsigned mul of the two ranges always/never overflows.
402 OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const;
403
401404 /// Print out the bounds to a stream.
402405 void print(raw_ostream &OS) const;
403406
12511251 return OverflowResult::NeverOverflows;
12521252 }
12531253
1254 ConstantRange::OverflowResult ConstantRange::unsignedMulMayOverflow(
1255 const ConstantRange &Other) const {
1256 if (isEmptySet() || Other.isEmptySet())
1257 return OverflowResult::MayOverflow;
1258
1259 APInt Min = getUnsignedMin(), Max = getUnsignedMax();
1260 APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
1261 bool Overflow;
1262
1263 (void) Min.umul_ov(OtherMin, Overflow);
1264 if (Overflow)
1265 return OverflowResult::AlwaysOverflows;
1266
1267 (void) Max.umul_ov(OtherMax, Overflow);
1268 if (Overflow)
1269 return OverflowResult::MayOverflow;
1270
1271 return OverflowResult::NeverOverflows;
1272 }
1273
12541274 void ConstantRange::print(raw_ostream &OS) const {
12551275 if (isFullSet())
12561276 OS << "full-set";
15771577 });
15781578 }
15791579
1580 TEST_F(ConstantRangeTest, UnsignedMulOverflowExhaustive) {
1581 TestOverflowExhaustive(
1582 [](const APInt &N1, const APInt &N2) {
1583 bool Overflow;
1584 (void) N1.umul_ov(N2, Overflow);
1585 return Overflow;
1586 },
1587 [](const ConstantRange &CR1, const ConstantRange &CR2) {
1588 return CR1.unsignedMulMayOverflow(CR2);
1589 });
1590 }
1591
15801592 TEST_F(ConstantRangeTest, SignedAddOverflowExhaustive) {
15811593 TestOverflowExhaustive(
15821594 [](const APInt &N1, const APInt &N2) {