Add some new methods
authorChris Lattner <sabre@nondot.org>
Tue, 30 Mar 2004 00:20:08 +0000 (00:20 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 30 Mar 2004 00:20:08 +0000 (00:20 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12539 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/ConstantRange.h
lib/Analysis/ConstantRange.cpp
lib/Support/ConstantRange.cpp
lib/VMCore/ConstantRange.cpp

index cc3b23fd8ed1bc1acbc9852981fdbdb2825d2c8f..30618a1692134896240d1eb6da1362a157684d0d 100644 (file)
@@ -30,6 +30,7 @@
 namespace llvm {
 class Constant;
 class ConstantIntegral;
+class ConstantInt;
 class Type;
 
 class ConstantRange {
@@ -39,6 +40,10 @@ class ConstantRange {
   ///
   ConstantRange(const Type *Ty, bool isFullSet = true);
   
+  /// Initialize a range to hold the single specified value.
+  ///
+  ConstantRange(Constant *Value);
+
   /// Initialize a range of values explicitly... this will assert out if
   /// Lower==Upper and Lower != Min or Max for its type, if the two constants
   /// have different types, or if the constant are not integral values.
@@ -74,6 +79,10 @@ class ConstantRange {
   /// for example: [100, 8)
   ///
   bool isWrappedSet() const;
+
+  /// contains - Return true if the specified value is in the set.
+  ///
+  bool contains(ConstantInt *Val) const;
   
   /// getSingleElement - If this set contains a single element, return it,
   /// otherwise return null.
@@ -97,6 +106,10 @@ class ConstantRange {
     return !operator==(CR);
   }
 
+  /// subtract - Subtract the specified constant from the endpoints of this
+  /// constant range.
+  ConstantRange subtract(ConstantInt *CI) const;
+
   /// intersect - Return the range that results from the intersection of this
   /// range with another range.  The resultant range is pruned as much as
   /// possible, but there may be cases where elements are included that are in
@@ -113,6 +126,18 @@ class ConstantRange {
   ///
   ConstantRange unionWith(const ConstantRange &CR) const;
 
+  /// zeroExtend - Return a new range in the specified integer type, which must
+  /// be strictly larger than the current type.  The returned range will
+  /// correspond to the possible range of values if the source range had been
+  /// zero extended.
+  ConstantRange zeroExtend(const Type *Ty) const;
+
+  /// truncate - Return a new range in the specified integer type, which must be
+  /// strictly smaller than the current type.  The returned range will
+  /// correspond to the possible range of values if the source range had been
+  /// truncated to the specified type.
+  ConstantRange truncate(const Type *Ty) const;
+
   /// print - Print out the bounds to a stream...
   ///
   void print(std::ostream &OS) const;
index d07e4af8b27d15fa019a7bc6a104710633cb0597..cb4be83bbd1c89a240601125931b7a8cb8001e09 100644 (file)
 #include "llvm/Type.h"
 using namespace llvm;
 
+static ConstantIntegral *Next(ConstantIntegral *CI) {
+  if (CI->getType() == Type::BoolTy)
+    return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
+      
+  Constant *Result = ConstantExpr::getAdd(CI,
+                                          ConstantInt::get(CI->getType(), 1));
+  return cast<ConstantIntegral>(Result);
+}
+
 static bool LT(ConstantIntegral *A, ConstantIntegral *B) {
-  Constant *C = ConstantExpr::get(Instruction::SetLT, A, B);
+  Constant *C = ConstantExpr::getSetLT(A, B);
   assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
   return cast<ConstantBool>(C)->getValue();
 }
 
-static bool GT(ConstantIntegral *A, ConstantIntegral *B) {
-  Constant *C = ConstantExpr::get(Instruction::SetGT, A, B);
+static bool LTE(ConstantIntegral *A, ConstantIntegral *B) {
+  Constant *C = ConstantExpr::getSetLE(A, B);
   assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
   return cast<ConstantBool>(C)->getValue();
 }
 
+static bool GT(ConstantIntegral *A, ConstantIntegral *B) { return LT(B, A); }
+
 static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
   return LT(A, B) ? A : B;
 }
@@ -46,7 +57,6 @@ static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
   return GT(A, B) ? A : B;
 }
 
-
 /// Initialize a full (the default) or empty set for the specified type.
 ///
 ConstantRange::ConstantRange(const Type *Ty, bool Full) {
@@ -58,6 +68,12 @@ ConstantRange::ConstantRange(const Type *Ty, bool Full) {
     Lower = Upper = ConstantIntegral::getMinValue(Ty);
 }
 
+/// Initialize a range to hold the single specified value.
+///
+ConstantRange::ConstantRange(Constant *V)
+  : Lower(cast<ConstantIntegral>(V)), Upper(Next(cast<ConstantIntegral>(V))) {
+}
+
 /// Initialize a range of values explicitly... this will assert out if
 /// Lower==Upper and Lower != Min or Max for its type (or if the two constants
 /// have different types)
@@ -73,15 +89,6 @@ ConstantRange::ConstantRange(Constant *L, Constant *U)
          "Lower == Upper, but they aren't min or max for type!");
 }
 
-static ConstantIntegral *Next(ConstantIntegral *CI) {
-  if (CI->getType() == Type::BoolTy)
-    return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
-      
-  Constant *Result = ConstantExpr::get(Instruction::Add, CI,
-                                       ConstantInt::get(CI->getType(), 1));
-  return cast<ConstantIntegral>(Result);
-}
-
 /// Initialize a set of values that all satisfy the condition with C.
 ///
 ConstantRange::ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C) {
@@ -151,14 +158,37 @@ uint64_t ConstantRange::getSetSize() const {
   }
   
   // Simply subtract the bounds...
-  Constant *Result =
-    ConstantExpr::get(Instruction::Sub, (Constant*)Upper, (Constant*)Lower);
+  Constant *Result = ConstantExpr::getSub(Upper, Lower);
   return cast<ConstantInt>(Result)->getRawValue();
 }
 
+/// contains - Return true if the specified value is in the set.
+///
+bool ConstantRange::contains(ConstantInt *Val) const {
+  if (Lower == Upper) {
+    if (isFullSet()) return true;
+    return false;
+  }
+
+  if (!isWrappedSet())
+    return LTE(Lower, Val) && LT(Val, Upper);
+  return LTE(Lower, Val) || LT(Val, Upper);
+}
 
 
 
+/// subtract - Subtract the specified constant from the endpoints of this
+/// constant range.
+ConstantRange ConstantRange::subtract(ConstantInt *CI) const {
+  assert(CI->getType() == getType() && getType()->isInteger() &&
+         "Cannot subtract from different type range or non-integer!");
+  // If the set is empty or full, don't modify the endpoints.
+  if (Lower == Upper) return *this;
+  return ConstantRange(ConstantExpr::getSub(Lower, CI),
+                       ConstantExpr::getSub(Upper, CI));
+}
+
+
 // intersect1Wrapped - This helper function is used to intersect two ranges when
 // it is known that LHS is wrapped and RHS isn't.
 //
@@ -245,6 +275,48 @@ ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
   return *this;
 }
 
+/// zeroExtend - Return a new range in the specified integer type, which must
+/// be strictly larger than the current type.  The returned range will
+/// correspond to the possible range of values if the source range had been
+/// zero extended.
+ConstantRange ConstantRange::zeroExtend(const Type *Ty) const {
+  assert(getLower()->getType()->getPrimitiveSize() < Ty->getPrimitiveSize() &&
+         "Not a value extension");
+  if (isFullSet()) {
+    // Change a source full set into [0, 1 << 8*numbytes)
+    unsigned SrcTySize = getLower()->getType()->getPrimitiveSize();
+    return ConstantRange(Constant::getNullValue(Ty),
+                         ConstantUInt::get(Ty, 1ULL << SrcTySize*8));
+  }
+
+  Constant *Lower = getLower();
+  Constant *Upper = getUpper();
+  if (Lower->getType()->isInteger() && !Lower->getType()->isUnsigned()) {
+    // Ensure we are doing a ZERO extension even if the input range is signed.
+    Lower = ConstantExpr::getCast(Lower, Ty->getUnsignedVersion());
+    Upper = ConstantExpr::getCast(Upper, Ty->getUnsignedVersion());
+  }
+
+  return ConstantRange(ConstantExpr::getCast(Lower, Ty),
+                       ConstantExpr::getCast(Upper, Ty));
+}
+
+/// truncate - Return a new range in the specified integer type, which must be
+/// strictly smaller than the current type.  The returned range will
+/// correspond to the possible range of values if the source range had been
+/// truncated to the specified type.
+ConstantRange ConstantRange::truncate(const Type *Ty) const {
+  assert(getLower()->getType()->getPrimitiveSize() > Ty->getPrimitiveSize() &&
+         "Not a value truncation");
+  uint64_t Size = 1ULL << Ty->getPrimitiveSize()*8;
+  if (isFullSet() || getSetSize() >= Size)
+    return ConstantRange(getType());
+
+  return ConstantRange(ConstantExpr::getCast(getLower(), Ty),
+                       ConstantExpr::getCast(getUpper(), Ty));
+}
+
+
 /// print - Print out the bounds to a stream...
 ///
 void ConstantRange::print(std::ostream &OS) const {
index d07e4af8b27d15fa019a7bc6a104710633cb0597..cb4be83bbd1c89a240601125931b7a8cb8001e09 100644 (file)
 #include "llvm/Type.h"
 using namespace llvm;
 
+static ConstantIntegral *Next(ConstantIntegral *CI) {
+  if (CI->getType() == Type::BoolTy)
+    return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
+      
+  Constant *Result = ConstantExpr::getAdd(CI,
+                                          ConstantInt::get(CI->getType(), 1));
+  return cast<ConstantIntegral>(Result);
+}
+
 static bool LT(ConstantIntegral *A, ConstantIntegral *B) {
-  Constant *C = ConstantExpr::get(Instruction::SetLT, A, B);
+  Constant *C = ConstantExpr::getSetLT(A, B);
   assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
   return cast<ConstantBool>(C)->getValue();
 }
 
-static bool GT(ConstantIntegral *A, ConstantIntegral *B) {
-  Constant *C = ConstantExpr::get(Instruction::SetGT, A, B);
+static bool LTE(ConstantIntegral *A, ConstantIntegral *B) {
+  Constant *C = ConstantExpr::getSetLE(A, B);
   assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
   return cast<ConstantBool>(C)->getValue();
 }
 
+static bool GT(ConstantIntegral *A, ConstantIntegral *B) { return LT(B, A); }
+
 static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
   return LT(A, B) ? A : B;
 }
@@ -46,7 +57,6 @@ static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
   return GT(A, B) ? A : B;
 }
 
-
 /// Initialize a full (the default) or empty set for the specified type.
 ///
 ConstantRange::ConstantRange(const Type *Ty, bool Full) {
@@ -58,6 +68,12 @@ ConstantRange::ConstantRange(const Type *Ty, bool Full) {
     Lower = Upper = ConstantIntegral::getMinValue(Ty);
 }
 
+/// Initialize a range to hold the single specified value.
+///
+ConstantRange::ConstantRange(Constant *V)
+  : Lower(cast<ConstantIntegral>(V)), Upper(Next(cast<ConstantIntegral>(V))) {
+}
+
 /// Initialize a range of values explicitly... this will assert out if
 /// Lower==Upper and Lower != Min or Max for its type (or if the two constants
 /// have different types)
@@ -73,15 +89,6 @@ ConstantRange::ConstantRange(Constant *L, Constant *U)
          "Lower == Upper, but they aren't min or max for type!");
 }
 
-static ConstantIntegral *Next(ConstantIntegral *CI) {
-  if (CI->getType() == Type::BoolTy)
-    return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
-      
-  Constant *Result = ConstantExpr::get(Instruction::Add, CI,
-                                       ConstantInt::get(CI->getType(), 1));
-  return cast<ConstantIntegral>(Result);
-}
-
 /// Initialize a set of values that all satisfy the condition with C.
 ///
 ConstantRange::ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C) {
@@ -151,14 +158,37 @@ uint64_t ConstantRange::getSetSize() const {
   }
   
   // Simply subtract the bounds...
-  Constant *Result =
-    ConstantExpr::get(Instruction::Sub, (Constant*)Upper, (Constant*)Lower);
+  Constant *Result = ConstantExpr::getSub(Upper, Lower);
   return cast<ConstantInt>(Result)->getRawValue();
 }
 
+/// contains - Return true if the specified value is in the set.
+///
+bool ConstantRange::contains(ConstantInt *Val) const {
+  if (Lower == Upper) {
+    if (isFullSet()) return true;
+    return false;
+  }
+
+  if (!isWrappedSet())
+    return LTE(Lower, Val) && LT(Val, Upper);
+  return LTE(Lower, Val) || LT(Val, Upper);
+}
 
 
 
+/// subtract - Subtract the specified constant from the endpoints of this
+/// constant range.
+ConstantRange ConstantRange::subtract(ConstantInt *CI) const {
+  assert(CI->getType() == getType() && getType()->isInteger() &&
+         "Cannot subtract from different type range or non-integer!");
+  // If the set is empty or full, don't modify the endpoints.
+  if (Lower == Upper) return *this;
+  return ConstantRange(ConstantExpr::getSub(Lower, CI),
+                       ConstantExpr::getSub(Upper, CI));
+}
+
+
 // intersect1Wrapped - This helper function is used to intersect two ranges when
 // it is known that LHS is wrapped and RHS isn't.
 //
@@ -245,6 +275,48 @@ ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
   return *this;
 }
 
+/// zeroExtend - Return a new range in the specified integer type, which must
+/// be strictly larger than the current type.  The returned range will
+/// correspond to the possible range of values if the source range had been
+/// zero extended.
+ConstantRange ConstantRange::zeroExtend(const Type *Ty) const {
+  assert(getLower()->getType()->getPrimitiveSize() < Ty->getPrimitiveSize() &&
+         "Not a value extension");
+  if (isFullSet()) {
+    // Change a source full set into [0, 1 << 8*numbytes)
+    unsigned SrcTySize = getLower()->getType()->getPrimitiveSize();
+    return ConstantRange(Constant::getNullValue(Ty),
+                         ConstantUInt::get(Ty, 1ULL << SrcTySize*8));
+  }
+
+  Constant *Lower = getLower();
+  Constant *Upper = getUpper();
+  if (Lower->getType()->isInteger() && !Lower->getType()->isUnsigned()) {
+    // Ensure we are doing a ZERO extension even if the input range is signed.
+    Lower = ConstantExpr::getCast(Lower, Ty->getUnsignedVersion());
+    Upper = ConstantExpr::getCast(Upper, Ty->getUnsignedVersion());
+  }
+
+  return ConstantRange(ConstantExpr::getCast(Lower, Ty),
+                       ConstantExpr::getCast(Upper, Ty));
+}
+
+/// truncate - Return a new range in the specified integer type, which must be
+/// strictly smaller than the current type.  The returned range will
+/// correspond to the possible range of values if the source range had been
+/// truncated to the specified type.
+ConstantRange ConstantRange::truncate(const Type *Ty) const {
+  assert(getLower()->getType()->getPrimitiveSize() > Ty->getPrimitiveSize() &&
+         "Not a value truncation");
+  uint64_t Size = 1ULL << Ty->getPrimitiveSize()*8;
+  if (isFullSet() || getSetSize() >= Size)
+    return ConstantRange(getType());
+
+  return ConstantRange(ConstantExpr::getCast(getLower(), Ty),
+                       ConstantExpr::getCast(getUpper(), Ty));
+}
+
+
 /// print - Print out the bounds to a stream...
 ///
 void ConstantRange::print(std::ostream &OS) const {
index d07e4af8b27d15fa019a7bc6a104710633cb0597..cb4be83bbd1c89a240601125931b7a8cb8001e09 100644 (file)
 #include "llvm/Type.h"
 using namespace llvm;
 
+static ConstantIntegral *Next(ConstantIntegral *CI) {
+  if (CI->getType() == Type::BoolTy)
+    return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
+      
+  Constant *Result = ConstantExpr::getAdd(CI,
+                                          ConstantInt::get(CI->getType(), 1));
+  return cast<ConstantIntegral>(Result);
+}
+
 static bool LT(ConstantIntegral *A, ConstantIntegral *B) {
-  Constant *C = ConstantExpr::get(Instruction::SetLT, A, B);
+  Constant *C = ConstantExpr::getSetLT(A, B);
   assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
   return cast<ConstantBool>(C)->getValue();
 }
 
-static bool GT(ConstantIntegral *A, ConstantIntegral *B) {
-  Constant *C = ConstantExpr::get(Instruction::SetGT, A, B);
+static bool LTE(ConstantIntegral *A, ConstantIntegral *B) {
+  Constant *C = ConstantExpr::getSetLE(A, B);
   assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
   return cast<ConstantBool>(C)->getValue();
 }
 
+static bool GT(ConstantIntegral *A, ConstantIntegral *B) { return LT(B, A); }
+
 static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
   return LT(A, B) ? A : B;
 }
@@ -46,7 +57,6 @@ static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
   return GT(A, B) ? A : B;
 }
 
-
 /// Initialize a full (the default) or empty set for the specified type.
 ///
 ConstantRange::ConstantRange(const Type *Ty, bool Full) {
@@ -58,6 +68,12 @@ ConstantRange::ConstantRange(const Type *Ty, bool Full) {
     Lower = Upper = ConstantIntegral::getMinValue(Ty);
 }
 
+/// Initialize a range to hold the single specified value.
+///
+ConstantRange::ConstantRange(Constant *V)
+  : Lower(cast<ConstantIntegral>(V)), Upper(Next(cast<ConstantIntegral>(V))) {
+}
+
 /// Initialize a range of values explicitly... this will assert out if
 /// Lower==Upper and Lower != Min or Max for its type (or if the two constants
 /// have different types)
@@ -73,15 +89,6 @@ ConstantRange::ConstantRange(Constant *L, Constant *U)
          "Lower == Upper, but they aren't min or max for type!");
 }
 
-static ConstantIntegral *Next(ConstantIntegral *CI) {
-  if (CI->getType() == Type::BoolTy)
-    return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
-      
-  Constant *Result = ConstantExpr::get(Instruction::Add, CI,
-                                       ConstantInt::get(CI->getType(), 1));
-  return cast<ConstantIntegral>(Result);
-}
-
 /// Initialize a set of values that all satisfy the condition with C.
 ///
 ConstantRange::ConstantRange(unsigned SetCCOpcode, ConstantIntegral *C) {
@@ -151,14 +158,37 @@ uint64_t ConstantRange::getSetSize() const {
   }
   
   // Simply subtract the bounds...
-  Constant *Result =
-    ConstantExpr::get(Instruction::Sub, (Constant*)Upper, (Constant*)Lower);
+  Constant *Result = ConstantExpr::getSub(Upper, Lower);
   return cast<ConstantInt>(Result)->getRawValue();
 }
 
+/// contains - Return true if the specified value is in the set.
+///
+bool ConstantRange::contains(ConstantInt *Val) const {
+  if (Lower == Upper) {
+    if (isFullSet()) return true;
+    return false;
+  }
+
+  if (!isWrappedSet())
+    return LTE(Lower, Val) && LT(Val, Upper);
+  return LTE(Lower, Val) || LT(Val, Upper);
+}
 
 
 
+/// subtract - Subtract the specified constant from the endpoints of this
+/// constant range.
+ConstantRange ConstantRange::subtract(ConstantInt *CI) const {
+  assert(CI->getType() == getType() && getType()->isInteger() &&
+         "Cannot subtract from different type range or non-integer!");
+  // If the set is empty or full, don't modify the endpoints.
+  if (Lower == Upper) return *this;
+  return ConstantRange(ConstantExpr::getSub(Lower, CI),
+                       ConstantExpr::getSub(Upper, CI));
+}
+
+
 // intersect1Wrapped - This helper function is used to intersect two ranges when
 // it is known that LHS is wrapped and RHS isn't.
 //
@@ -245,6 +275,48 @@ ConstantRange ConstantRange::unionWith(const ConstantRange &CR) const {
   return *this;
 }
 
+/// zeroExtend - Return a new range in the specified integer type, which must
+/// be strictly larger than the current type.  The returned range will
+/// correspond to the possible range of values if the source range had been
+/// zero extended.
+ConstantRange ConstantRange::zeroExtend(const Type *Ty) const {
+  assert(getLower()->getType()->getPrimitiveSize() < Ty->getPrimitiveSize() &&
+         "Not a value extension");
+  if (isFullSet()) {
+    // Change a source full set into [0, 1 << 8*numbytes)
+    unsigned SrcTySize = getLower()->getType()->getPrimitiveSize();
+    return ConstantRange(Constant::getNullValue(Ty),
+                         ConstantUInt::get(Ty, 1ULL << SrcTySize*8));
+  }
+
+  Constant *Lower = getLower();
+  Constant *Upper = getUpper();
+  if (Lower->getType()->isInteger() && !Lower->getType()->isUnsigned()) {
+    // Ensure we are doing a ZERO extension even if the input range is signed.
+    Lower = ConstantExpr::getCast(Lower, Ty->getUnsignedVersion());
+    Upper = ConstantExpr::getCast(Upper, Ty->getUnsignedVersion());
+  }
+
+  return ConstantRange(ConstantExpr::getCast(Lower, Ty),
+                       ConstantExpr::getCast(Upper, Ty));
+}
+
+/// truncate - Return a new range in the specified integer type, which must be
+/// strictly smaller than the current type.  The returned range will
+/// correspond to the possible range of values if the source range had been
+/// truncated to the specified type.
+ConstantRange ConstantRange::truncate(const Type *Ty) const {
+  assert(getLower()->getType()->getPrimitiveSize() > Ty->getPrimitiveSize() &&
+         "Not a value truncation");
+  uint64_t Size = 1ULL << Ty->getPrimitiveSize()*8;
+  if (isFullSet() || getSetSize() >= Size)
+    return ConstantRange(getType());
+
+  return ConstantRange(ConstantExpr::getCast(getLower(), Ty),
+                       ConstantExpr::getCast(getUpper(), Ty));
+}
+
+
 /// print - Print out the bounds to a stream...
 ///
 void ConstantRange::print(std::ostream &OS) const {