add ConstantRange::difference (to perform set difference/relative complement)
authorNuno Lopes <nunoplopes@sapo.pt>
Thu, 28 Jun 2012 16:10:13 +0000 (16:10 +0000)
committerNuno Lopes <nunoplopes@sapo.pt>
Thu, 28 Jun 2012 16:10:13 +0000 (16:10 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@159352 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/ConstantRange.h
lib/Support/ConstantRange.cpp
unittests/Support/ConstantRangeTest.cpp

index ced3a2cf2dbd8b6d5f62ea4212be1582c9b5a6bf..90dd69fa478fe269b7ca3b8a005e3049d28c6826 100644 (file)
@@ -155,6 +155,10 @@ public:
   /// 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
index 61d333f24a0f092081d7707e3b7fc888c36ede8c..91d086b6890d08e21892ab3a5d5f721cacf55fe2 100644 (file)
@@ -248,6 +248,12 @@ ConstantRange ConstantRange::subtract(const APInt &Val) const {
   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
index a562080981ae8141053624175450d99f8fd408ce..72540c6999cb6a326fa8bccad1dedd297dfc1a0a 100644 (file)
@@ -289,6 +289,23 @@ TEST_F(ConstantRangeTest, UnionWith) {
               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);