Multiply was very wrong for wrapped ranges. This supplies a half-fix that will
authorNick Lewycky <nicholas@mxc.ca>
Mon, 13 Jul 2009 03:27:41 +0000 (03:27 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Mon, 13 Jul 2009 03:27:41 +0000 (03:27 +0000)
generally return Full on all wrapped inputs. "Fixes" PR4545.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75444 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/ConstantRange.cpp
unittests/Support/ConstantRangeTest.cpp

index 8bab53779528a98b89e6b063b7fce63852ef29bb..d7a57bb134c6bd40ea83eefc5d51da78c2cf5312 100644 (file)
@@ -557,13 +557,13 @@ ConstantRange::multiply(const ConstantRange &Other) const {
   if (isFullSet() || Other.isFullSet())
     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
 
-  ConstantRange this_zext = zeroExtend(getBitWidth() * 2);
-  ConstantRange Other_zext = Other.zeroExtend(getBitWidth() * 2);
-
-  ConstantRange Result_zext = ConstantRange(
-      this_zext.getLower() * Other_zext.getLower(),
-      ((this_zext.getUpper()-1) * (Other_zext.getUpper()-1)) + 1);
+  APInt this_min = getUnsignedMin().zext(getBitWidth() * 2);
+  APInt this_max = getUnsignedMax().zext(getBitWidth() * 2);
+  APInt Other_min = Other.getUnsignedMin().zext(getBitWidth() * 2);
+  APInt Other_max = Other.getUnsignedMax().zext(getBitWidth() * 2);
 
+  ConstantRange Result_zext = ConstantRange(this_min * Other_min,
+                                            this_max * Other_max + 1);
   return Result_zext.truncate(getBitWidth());
 }
 
index cd91a9e103cf5c6bafdddb3864ddfd6e844d130c..2b073d8203979ab6303eabcfeb93969baa64a2f0 100644 (file)
@@ -275,6 +275,11 @@ TEST_F(ConstantRangeTest, Multiply) {
   EXPECT_TRUE(Some.multiply(Some).isFullSet());
   EXPECT_EQ(Some.multiply(Wrap), Full);
   EXPECT_EQ(Wrap.multiply(Wrap), Full);
+
+  // http://llvm.org/PR4545
+  EXPECT_EQ(ConstantRange(APInt(4, 1), APInt(4, 6)).multiply(
+              ConstantRange(APInt(4, 6), APInt(4, 2))),
+            ConstantRange(4, /*isFullSet=*/true));
 }
 
 TEST_F(ConstantRangeTest, UMax) {