make ConstantRange::getSetSize() properly compute the size of wrapped and full sets.
authorNuno Lopes <nunoplopes@sapo.pt>
Mon, 16 Jul 2012 18:08:12 +0000 (18:08 +0000)
committerNuno Lopes <nunoplopes@sapo.pt>
Mon, 16 Jul 2012 18:08:12 +0000 (18:08 +0000)
Make it always return APInts with the same bitwidth for the same ConstantRange bitwidth to simply clients

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@160294 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 91d086b6890d08e21892ab3a5d5f721cacf55fe2..b83dcccca67a3adb21c06d8cdb58c7d2937e48cc 100644 (file)
@@ -143,16 +143,18 @@ bool ConstantRange::isSignWrappedSet() const {
 /// getSetSize - Return the number of elements in this set.
 ///
 APInt ConstantRange::getSetSize() const {
-  if (isEmptySet()) 
-    return APInt(getBitWidth(), 0);
-  if (getBitWidth() == 1) {
-    if (Lower != Upper)  // One of T or F in the set...
-      return APInt(2, 1);
-    return APInt(2, 2);      // Must be full set...
+  if (isEmptySet())
+    return APInt(getBitWidth()+1, 0);
+
+  if (isFullSet())
+    return APInt::getMaxValue(getBitWidth()).zext(getBitWidth()+1) + 1;
+
+  if (isWrappedSet()) {
+    APInt Result = Upper + (APInt::getMaxValue(getBitWidth()) - Lower + 1);
+    return Result.zext(getBitWidth()+1);
   }
 
-  // Simply subtract the bounds...
-  return Upper - Lower;
+  return (Upper - Lower).zext(getBitWidth()+1);
 }
 
 /// getUnsignedMax - Return the largest unsigned value contained in the
index 72540c6999cb6a326fa8bccad1dedd297dfc1a0a..6d2510ced5656a9155fed7c64b2226ca7e9b1570 100644 (file)
@@ -114,11 +114,15 @@ TEST_F(ConstantRangeTest, SingleElement) {
 }
 
 TEST_F(ConstantRangeTest, GetSetSize) {
-  EXPECT_EQ(Full.getSetSize(), APInt(16, 0));
-  EXPECT_EQ(Empty.getSetSize(), APInt(16, 0));
-  EXPECT_EQ(One.getSetSize(), APInt(16, 1));
-  EXPECT_EQ(Some.getSetSize(), APInt(16, 0xaa0));
-  EXPECT_EQ(Wrap.getSetSize(), APInt(16, 0x10000 - 0xaa0));
+  EXPECT_EQ(Full.getSetSize(), APInt(17, 65536));
+  EXPECT_EQ(Empty.getSetSize(), APInt(17, 0));
+  EXPECT_EQ(One.getSetSize(), APInt(17, 1));
+  EXPECT_EQ(Some.getSetSize(), APInt(17, 0xaa0));
+
+  ConstantRange Wrap(APInt(4, 7), APInt(4, 3));
+  ConstantRange Wrap2(APInt(4, 8), APInt(4, 7));
+  EXPECT_EQ(Wrap.getSetSize(), APInt(5, 12));
+  EXPECT_EQ(Wrap2.getSetSize(), APInt(5, 15));
 }
 
 TEST_F(ConstantRangeTest, GetMinsAndMaxes) {