summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNuno Lopes <nunoplopes@sapo.pt>2012-07-16 20:47:16 +0000
committerNuno Lopes <nunoplopes@sapo.pt>2012-07-16 20:47:16 +0000
commit7e733eab2f11fceb24d6b4f25c27d7ba7d92d97e (patch)
treeab82e7cc0758a49e83f62f6991993c738edcf2a6
parent98819c9d1e3b929e9ebab0e8cd3edb31aad21bd8 (diff)
downloadllvm-7e733eab2f11fceb24d6b4f25c27d7ba7d92d97e.tar.gz
llvm-7e733eab2f11fceb24d6b4f25c27d7ba7d92d97e.tar.bz2
llvm-7e733eab2f11fceb24d6b4f25c27d7ba7d92d97e.tar.xz
teach ConstantRange that zero times X is always zero
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@160317 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Support/ConstantRange.cpp6
-rw-r--r--unittests/Support/ConstantRangeTest.cpp8
2 files changed, 14 insertions, 0 deletions
diff --git a/lib/Support/ConstantRange.cpp b/lib/Support/ConstantRange.cpp
index b83dcccca6..221ca948ca 100644
--- a/lib/Support/ConstantRange.cpp
+++ b/lib/Support/ConstantRange.cpp
@@ -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);
diff --git a/unittests/Support/ConstantRangeTest.cpp b/unittests/Support/ConstantRangeTest.cpp
index 6d2510ced5..7d4055f889 100644
--- a/unittests/Support/ConstantRangeTest.cpp
+++ b/unittests/Support/ConstantRangeTest.cpp
@@ -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))),