teach ConstantRange that zero times X is always zero
authorNuno Lopes <nunoplopes@sapo.pt>
Mon, 16 Jul 2012 20:47:16 +0000 (20:47 +0000)
committerNuno Lopes <nunoplopes@sapo.pt>
Mon, 16 Jul 2012 20:47:16 +0000 (20:47 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@160317 91177308-0d34-0410-b5e6-96231b3b80d8

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

index b83dcccca67a3adb21c06d8cdb58c7d2937e48cc..221ca948ca5ecf18325dc7bdafc03be62cee15be 100644 (file)
@@ -537,6 +537,12 @@ ConstantRange::multiply(const ConstantRange &Other) const {
 
   if (isEmptySet() || Other.isEmptySet())
     return ConstantRange(getBitWidth(), /*isFullSet=*/false);
+
+  // If any of the operands is zero, then the result is also zero.
+  if ((getSingleElement() && *getSingleElement() == 0) ||
+      (Other.getSingleElement() && *Other.getSingleElement() == 0))
+    return ConstantRange(APInt(getBitWidth(), 0));
+
   if (isFullSet() || Other.isFullSet())
     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
 
index 6d2510ced5656a9155fed7c64b2226ca7e9b1570..7d4055f889f55cfab10f15b1467fc45ddf3b472c 100644 (file)
@@ -382,6 +382,14 @@ TEST_F(ConstantRangeTest, Multiply) {
   EXPECT_EQ(Some.multiply(Wrap), Full);
   EXPECT_EQ(Wrap.multiply(Wrap), Full);
 
+  ConstantRange Zero(APInt(16, 0));
+  EXPECT_EQ(Zero.multiply(Full), Zero);
+  EXPECT_EQ(Zero.multiply(Some), Zero);
+  EXPECT_EQ(Zero.multiply(Wrap), Zero);
+  EXPECT_EQ(Full.multiply(Zero), Zero);
+  EXPECT_EQ(Some.multiply(Zero), Zero);
+  EXPECT_EQ(Wrap.multiply(Zero), Zero);
+
   // http://llvm.org/PR4545
   EXPECT_EQ(ConstantRange(APInt(4, 1), APInt(4, 6)).multiply(
                 ConstantRange(APInt(4, 6), APInt(4, 2))),