[llvm-commits] [llvm] r159352 - in /llvm/trunk: include/llvm/Support/ConstantRange.h lib/Support/ConstantRange.cpp unittests/Support/ConstantRangeTest.cpp
Nuno Lopes
nunoplopes at sapo.pt
Thu Jun 28 09:10:13 PDT 2012
Author: nlopes
Date: Thu Jun 28 11:10:13 2012
New Revision: 159352
URL: http://llvm.org/viewvc/llvm-project?rev=159352&view=rev
Log:
add ConstantRange::difference (to perform set difference/relative complement)
Modified:
llvm/trunk/include/llvm/Support/ConstantRange.h
llvm/trunk/lib/Support/ConstantRange.cpp
llvm/trunk/unittests/Support/ConstantRangeTest.cpp
Modified: llvm/trunk/include/llvm/Support/ConstantRange.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/ConstantRange.h?rev=159352&r1=159351&r2=159352&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/ConstantRange.h (original)
+++ llvm/trunk/include/llvm/Support/ConstantRange.h Thu Jun 28 11:10:13 2012
@@ -155,6 +155,10 @@
/// constant range.
ConstantRange subtract(const APInt &CI) const;
+ /// \brief Subtract the specified range from this range (aka relative
+ /// complement of the sets).
+ ConstantRange difference(const ConstantRange &CR) const;
+
/// intersectWith - Return the range that results from the intersection of
/// this range with another range. The resultant range is guaranteed to
/// include all elements contained in both input ranges, and to have the
Modified: llvm/trunk/lib/Support/ConstantRange.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/ConstantRange.cpp?rev=159352&r1=159351&r2=159352&view=diff
==============================================================================
--- llvm/trunk/lib/Support/ConstantRange.cpp (original)
+++ llvm/trunk/lib/Support/ConstantRange.cpp Thu Jun 28 11:10:13 2012
@@ -248,6 +248,12 @@
return ConstantRange(Lower - Val, Upper - Val);
}
+/// \brief Subtract the specified range from this range (aka relative complement
+/// of the sets).
+ConstantRange ConstantRange::difference(const ConstantRange &CR) const {
+ return intersectWith(CR.inverse());
+}
+
/// intersectWith - Return the range that results from the intersection of this
/// range with another range. The resultant range is guaranteed to include all
/// elements contained in both input ranges, and to have the smallest possible
Modified: llvm/trunk/unittests/Support/ConstantRangeTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ConstantRangeTest.cpp?rev=159352&r1=159351&r2=159352&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ConstantRangeTest.cpp (original)
+++ llvm/trunk/unittests/Support/ConstantRangeTest.cpp Thu Jun 28 11:10:13 2012
@@ -289,6 +289,23 @@
ConstantRange(16));
}
+TEST_F(ConstantRangeTest, SetDifference) {
+ EXPECT_EQ(Full.difference(Empty), Full);
+ EXPECT_EQ(Full.difference(Full), Empty);
+ EXPECT_EQ(Empty.difference(Empty), Empty);
+ EXPECT_EQ(Empty.difference(Full), Empty);
+
+ ConstantRange A(APInt(16, 3), APInt(16, 7));
+ ConstantRange B(APInt(16, 5), APInt(16, 9));
+ ConstantRange C(APInt(16, 3), APInt(16, 5));
+ ConstantRange D(APInt(16, 7), APInt(16, 9));
+ ConstantRange E(APInt(16, 5), APInt(16, 4));
+ ConstantRange F(APInt(16, 7), APInt(16, 3));
+ EXPECT_EQ(A.difference(B), C);
+ EXPECT_EQ(B.difference(A), D);
+ EXPECT_EQ(E.difference(A), F);
+}
+
TEST_F(ConstantRangeTest, SubtractAPInt) {
EXPECT_EQ(Full.subtract(APInt(16, 4)), Full);
EXPECT_EQ(Empty.subtract(APInt(16, 4)), Empty);
More information about the llvm-commits
mailing list