From 986cc181b0ac566e4de37da82b0ea66451ec0b7c Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Mon, 16 Jul 2012 20:47:16 +0000 Subject: [PATCH] teach ConstantRange that zero times X is always zero llvm-svn: 160317 --- llvm/lib/Support/ConstantRange.cpp | 6 ++++++ llvm/unittests/Support/ConstantRangeTest.cpp | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/llvm/lib/Support/ConstantRange.cpp b/llvm/lib/Support/ConstantRange.cpp index b83dcccca67a..221ca948ca5e 100644 --- a/llvm/lib/Support/ConstantRange.cpp +++ b/llvm/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/llvm/unittests/Support/ConstantRangeTest.cpp b/llvm/unittests/Support/ConstantRangeTest.cpp index 6d2510ced565..7d4055f889f5 100644 --- a/llvm/unittests/Support/ConstantRangeTest.cpp +++ b/llvm/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))),