Implement ConstantRange::multiply based on the code in LoopVR.
authorNick Lewycky <nicholas@mxc.ca>
Sun, 12 Jul 2009 02:19:05 +0000 (02:19 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Sun, 12 Jul 2009 02:19:05 +0000 (02:19 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75410 91177308-0d34-0410-b5e6-96231b3b80d8

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

index ad301c3e18af140e5dc833b4e0caf336049fb307..7fe156835dbd2ad10592e09ab4d6b48ddb8a7bc5 100644 (file)
@@ -550,9 +550,19 @@ ConstantRange::add(const ConstantRange &Other) const {
 
 ConstantRange
 ConstantRange::multiply(const ConstantRange &Other) const {
-  // TODO: Implement multiply.
-  return ConstantRange(getBitWidth(),
-                       !(isEmptySet() || Other.isEmptySet()));
+  if (isEmptySet() || Other.isEmptySet())
+    return ConstantRange(getBitWidth(), /*isFullSet=*/false);
+  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);
+
+  return Result_zext.truncate(getBitWidth());
 }
 
 ConstantRange
index 83bf5350283b6cade50b63a04609f30c3b69f74a..4a9e972802c5039d1874e03589fd4d67a96996c1 100644 (file)
@@ -267,17 +267,13 @@ TEST_F(ConstantRangeTest, Multiply) {
   EXPECT_EQ(Empty.multiply(One), Empty);
   EXPECT_EQ(Empty.multiply(Some), Empty);
   EXPECT_EQ(Empty.multiply(Wrap), Empty);
-  // TODO: ConstantRange is currently over-conservative here.
-  EXPECT_EQ(One.multiply(One), Full);
-  // TODO: ConstantRange is currently over-conservative here.
-  EXPECT_EQ(One.multiply(Some), Full);
-  // TODO: ConstantRange is currently over-conservative here.
-  EXPECT_EQ(One.multiply(Wrap), Full);
-  // TODO: ConstantRange is currently over-conservative here.
-  EXPECT_EQ(Some.multiply(Some), Full);
-  // TODO: ConstantRange is currently over-conservative here.
+  EXPECT_EQ(One.multiply(One), ConstantRange(APInt(16, 0xa*0xa),
+                                             APInt(16, 0xa*0xa + 1)));
+  EXPECT_EQ(One.multiply(Some), ConstantRange(APInt(16, 0xa*0xa),
+                                              APInt(16, 0xa*0xaa9 + 1)));
+  EXPECT_TRUE(One.multiply(Wrap).isFullSet());
+  EXPECT_TRUE(Some.multiply(Some).isFullSet());
   EXPECT_EQ(Some.multiply(Wrap), Full);
-  // TODO: ConstantRange is currently over-conservative here.
   EXPECT_EQ(Wrap.multiply(Wrap), Full);
 }