]> granicus.if.org Git - llvm/commitdiff
[ConstantRange] Add unsignedMulMayOverflow()
authorNikita Popov <nikita.ppv@gmail.com>
Thu, 11 Apr 2019 21:10:33 +0000 (21:10 +0000)
committerNikita Popov <nikita.ppv@gmail.com>
Thu, 11 Apr 2019 21:10:33 +0000 (21:10 +0000)
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

include/llvm/IR/ConstantRange.h
lib/IR/ConstantRange.cpp
unittests/IR/ConstantRangeTest.cpp

index a8d51b379571a74eb8d8c466563732a4ea396108..d98bf96a21cf52f3811dffd4ef039a0b00391834 100644 (file)
@@ -399,6 +399,9 @@ public:
   /// Return whether signed sub of the two ranges always/never overflows.
   OverflowResult signedSubMayOverflow(const ConstantRange &Other) const;
 
+  /// Return whether unsigned mul of the two ranges always/never overflows.
+  OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const;
+
   /// Print out the bounds to a stream.
   void print(raw_ostream &OS) const;
 
index 391d9653db5ca831c57e9bf225e9ddaf43ca46b8..35c2b0e2e2cc7168c3feef1c722c3368aab4144e 100644 (file)
@@ -1252,6 +1252,26 @@ ConstantRange::OverflowResult ConstantRange::signedSubMayOverflow(
   return OverflowResult::NeverOverflows;
 }
 
+ConstantRange::OverflowResult ConstantRange::unsignedMulMayOverflow(
+    const ConstantRange &Other) const {
+  if (isEmptySet() || Other.isEmptySet())
+    return OverflowResult::MayOverflow;
+
+  APInt Min = getUnsignedMin(), Max = getUnsignedMax();
+  APInt OtherMin = Other.getUnsignedMin(), OtherMax = Other.getUnsignedMax();
+  bool Overflow;
+
+  (void) Min.umul_ov(OtherMin, Overflow);
+  if (Overflow)
+    return OverflowResult::AlwaysOverflows;
+
+  (void) Max.umul_ov(OtherMax, Overflow);
+  if (Overflow)
+    return OverflowResult::MayOverflow;
+
+  return OverflowResult::NeverOverflows;
+}
+
 void ConstantRange::print(raw_ostream &OS) const {
   if (isFullSet())
     OS << "full-set";
index 55e71a1f29b2e61d363c4dc9a0421445bf60b407..6d1b512d2292078a88c00c077912f7841182affa 100644 (file)
@@ -1578,6 +1578,18 @@ TEST_F(ConstantRangeTest, UnsignedSubOverflowExhaustive) {
       });
 }
 
+TEST_F(ConstantRangeTest, UnsignedMulOverflowExhaustive) {
+  TestOverflowExhaustive(
+      [](const APInt &N1, const APInt &N2) {
+        bool Overflow;
+        (void) N1.umul_ov(N2, Overflow);
+        return Overflow;
+      },
+      [](const ConstantRange &CR1, const ConstantRange &CR2) {
+        return CR1.unsignedMulMayOverflow(CR2);
+      });
+}
+
 TEST_F(ConstantRangeTest, SignedAddOverflowExhaustive) {
   TestOverflowExhaustive(
       [](const APInt &N1, const APInt &N2) {