For PR1205:
authorReid Spencer <rspencer@reidspencer.com>
Wed, 28 Feb 2007 19:57:34 +0000 (19:57 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Wed, 28 Feb 2007 19:57:34 +0000 (19:57 +0000)
Remove ConstantInt from ConstantRange interface and adjust its users to
compensate.

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

include/llvm/Analysis/ScalarEvolution.h
include/llvm/Support/ConstantRange.h
lib/Analysis/ConstantRange.cpp
lib/Analysis/ScalarEvolution.cpp
lib/Support/ConstantRange.cpp
lib/Transforms/Scalar/CorrelatedExprs.cpp

index 4aac284ee006775b3a80435fc369541281dcbd73..b950ca48c6409ddd21683ac30124663567259b28 100644 (file)
@@ -85,6 +85,10 @@ namespace llvm {
     ///
     virtual const Type *getType() const = 0;
 
+    /// getBitWidth - Get the bit width of the type, if it has one, 0 otherwise.
+    /// 
+    uint32_t getBitWidth() const;
+
     /// replaceSymbolicValuesWithConcrete - If this SCEV internally references
     /// the symbolic value "Sym", construct and return a new SCEV that produces
     /// the same value, but which uses the concrete value Conc instead of the
index b7e98181b3e7907009f9cc260ac79fd454300cf7..ae36f42e12c9d32027ba40e7c7b725d0f6f75aa5 100644 (file)
@@ -37,7 +37,6 @@
 
 namespace llvm {
 class Constant;
-class ConstantInt;
 class Type;
 
 class ConstantRange {
@@ -66,11 +65,11 @@ class ConstantRange {
 
   /// getLower - Return the lower value for this range...
   ///
-  ConstantInt *getLower() const; 
+  const APInt &getLower() const { return Lower; }
 
   /// getUpper - Return the upper value for this range...
   ///
-  ConstantInt *getUpper() const; 
+  const APInt &getUpper() const { return Upper; } 
 
   /// getType - Return the LLVM data type of this range.
   ///
@@ -94,12 +93,16 @@ class ConstantRange {
   /// The isSigned parameter indicates whether the comparisons should be
   /// performed as if the values are signed or not.
   ///
-  bool contains(ConstantInt *Val, bool isSigned) const;
+  bool contains(const APInt &Val, bool isSigned) const;
 
   /// getSingleElement - If this set contains a single element, return it,
   /// otherwise return null.
   ///
-  ConstantInt *getSingleElement() const;
+  const APInt *getSingleElement() const {
+    if (Upper == Lower + 1)
+      return &Lower;
+    return 0;
+  }
 
   /// isSingleElement - Return true if this set contains exactly one member.
   ///
@@ -120,7 +123,7 @@ class ConstantRange {
 
   /// subtract - Subtract the specified constant from the endpoints of this
   /// constant range.
-  ConstantRange subtract(ConstantInt *CI) const;
+  ConstantRange subtract(const APInt &CI) const;
 
   /// intersectWith - Return the range that results from the intersection of
   /// this range with another range.  The resultant range is pruned as much as
index c000c73e8b2c9011b323f9fad47c9fb932f58a68..f419edcf9b94b94455441b56f1965f7d34dabc1c 100644 (file)
@@ -22,7 +22,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/ConstantRange.h"
-#include "llvm/Constants.h"
 #include "llvm/Instruction.h"
 #include "llvm/Instructions.h"
 #include "llvm/Type.h"
@@ -107,14 +106,6 @@ const Type *ConstantRange::getType() const {
   return IntegerType::get(Lower.getBitWidth()); 
 }
 
-ConstantInt *ConstantRange::getLower() const {
-  return ConstantInt::get(getType(), Lower);
-}
-
-ConstantInt *ConstantRange::getUpper() const {
-  return ConstantInt::get(getType(), Upper);
-}
-
 /// isFullSet - Return true if this set contains all of the elements possible
 /// for this data-type
 bool ConstantRange::isFullSet() const {
@@ -136,14 +127,6 @@ bool ConstantRange::isWrappedSet(bool isSigned) const {
   return Lower.ugt(Upper);
 }
 
-/// getSingleElement - If this set contains a single element, return it,
-/// otherwise return null.
-ConstantInt *ConstantRange::getSingleElement() const {
-  if (Upper == Lower + 1)  // Is it a single element range?
-    return ConstantInt::get(getType(), Lower);
-  return 0;
-}
-
 /// getSetSize - Return the number of elements in this set.
 ///
 APInt ConstantRange::getSetSize() const {
@@ -161,14 +144,13 @@ APInt ConstantRange::getSetSize() const {
 
 /// contains - Return true if the specified value is in the set.
 ///
-bool ConstantRange::contains(ConstantInt *Val, bool isSigned) const {
+bool ConstantRange::contains(const APInt &V, bool isSigned) const {
   if (Lower == Upper) {
     if (isFullSet()) 
       return true;
     return false;
   }
 
-  const APInt &V = Val->getValue();
   if (!isWrappedSet(isSigned))
     if (isSigned)
       return Lower.sle(V) && V.slt(Upper);
@@ -182,14 +164,11 @@ bool ConstantRange::contains(ConstantInt *Val, bool isSigned) const {
 
 /// subtract - Subtract the specified constant from the endpoints of this
 /// constant range.
-ConstantRange ConstantRange::subtract(ConstantInt *CI) const {
-  assert(CI->getType() == getType() && 
-         "Cannot subtract from different type range or non-integer!");
+ConstantRange ConstantRange::subtract(const APInt &Val) const {
+  assert(Val.getBitWidth() == Lower.getBitWidth() && "Wrong bit width");
   // If the set is empty or full, don't modify the endpoints.
   if (Lower == Upper) 
     return *this;
-  
-  const APInt &Val = CI->getValue();
   return ConstantRange(Lower - Val, Upper - Val);
 }
 
index 0507b39b7d9feefacbc98e013c6090b68c59cfa8..364e1223ba4492476feecb79e7ed3bef645a4a86 100644 (file)
@@ -127,6 +127,12 @@ ConstantRange SCEV::getValueRange() const {
   return ConstantRange(getType());
 }
 
+uint32_t SCEV::getBitWidth() const {
+  if (const IntegerType* ITy = dyn_cast<IntegerType>(getType()))
+    return ITy->getBitWidth();
+  return 0;
+}
+
 
 SCEVCouldNotCompute::SCEVCouldNotCompute() : SCEV(scCouldNotCompute) {}
 
@@ -2320,7 +2326,7 @@ SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
       SCEVHandle Shifted = SCEVAddRecExpr::get(Operands, getLoop());
       if (SCEVAddRecExpr *ShiftedAddRec = dyn_cast<SCEVAddRecExpr>(Shifted))
         return ShiftedAddRec->getNumIterationsInRange(
-                                      Range.subtract(SC->getValue()),isSigned);
+                           Range.subtract(SC->getValue()->getValue()),isSigned);
       // This is strange and shouldn't happen.
       return new SCEVCouldNotCompute();
     }
@@ -2337,8 +2343,8 @@ SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
 
   // First check to see if the range contains zero.  If not, the first
   // iteration exits.
-  ConstantInt *Zero = ConstantInt::get(getType(), 0);
-  if (!Range.contains(Zero, isSigned)) return SCEVConstant::get(Zero);
+  if (!Range.contains(APInt(getBitWidth(),0), isSigned)) 
+    return SCEVConstant::get(ConstantInt::get(getType(),0));
 
   if (isAffine()) {
     // If this is an affine expression then we have this situation:
@@ -2347,29 +2353,27 @@ SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
     // Since we know that zero is in the range, we know that the upper value of
     // the range must be the first possible exit value.  Also note that we
     // already checked for a full range.
-    ConstantInt *Upper = cast<ConstantInt>(Range.getUpper());
-    ConstantInt *A     = cast<SCEVConstant>(getOperand(1))->getValue();
-    ConstantInt *One   = ConstantInt::get(getType(), 1);
+    const APInt &Upper = Range.getUpper();
+    APInt A     = cast<SCEVConstant>(getOperand(1))->getValue()->getValue();
+    APInt One(getBitWidth(),1);
 
     // The exit value should be (Upper+A-1)/A.
-    Constant *ExitValue = Upper;
-    if (A != One) {
-      ExitValue = ConstantExpr::getSub(ConstantExpr::getAdd(Upper, A), One);
-      ExitValue = ConstantExpr::getSDiv(ExitValue, A);
-    }
-    assert(isa<ConstantInt>(ExitValue) &&
-           "Constant folding of integers not implemented?");
+    APInt ExitVal(Upper);
+    if (A != One)
+      ExitVal = (Upper + A - One).sdiv(A);
+    ConstantInt *ExitValue = ConstantInt::get(getType(), ExitVal);
 
     // Evaluate at the exit value.  If we really did fall out of the valid
     // range, then we computed our trip count, otherwise wrap around or other
     // things must have happened.
     ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue);
-    if (Range.contains(Val, isSigned))
+    if (Range.contains(Val->getValue(), isSigned))
       return new SCEVCouldNotCompute();  // Something strange happened
 
     // Ensure that the previous value is in the range.  This is a sanity check.
-    assert(Range.contains(EvaluateConstantChrecAtConstant(this,
-                          ConstantExpr::getSub(ExitValue, One)), isSigned) &&
+    assert(Range.contains(
+           EvaluateConstantChrecAtConstant(this, 
+           ConstantInt::get(getType(), ExitVal - One))->getValue(), isSigned) &&
            "Linear scev computation is off in a bad way!");
     return SCEVConstant::get(cast<ConstantInt>(ExitValue));
   } else if (isQuadratic()) {
@@ -2378,7 +2382,8 @@ SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
     // terms of figuring out when zero is crossed, instead of when
     // Range.getUpper() is crossed.
     std::vector<SCEVHandle> NewOps(op_begin(), op_end());
-    NewOps[0] = SCEV::getNegativeSCEV(SCEVUnknown::get(Range.getUpper()));
+    NewOps[0] = SCEV::getNegativeSCEV(SCEVUnknown::get(
+                                ConstantInt::get(getType(), Range.getUpper())));
     SCEVHandle NewAddRec = SCEVAddRecExpr::get(NewOps, getLoop());
 
     // Next, solve the constructed addrec
@@ -2399,14 +2404,14 @@ SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
         // for "X*X < 5", for example, we should not return a root of 2.
         ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this,
                                                              R1->getValue());
-        if (Range.contains(R1Val, isSigned)) {
+        if (Range.contains(R1Val->getValue(), isSigned)) {
           // The next iteration must be out of the range...
           Constant *NextVal =
             ConstantExpr::getAdd(R1->getValue(),
                                  ConstantInt::get(R1->getType(), 1));
 
           R1Val = EvaluateConstantChrecAtConstant(this, NextVal);
-          if (!Range.contains(R1Val, isSigned))
+          if (!Range.contains(R1Val->getValue(), isSigned))
             return SCEVUnknown::get(NextVal);
           return new SCEVCouldNotCompute();  // Something strange happened
         }
@@ -2417,7 +2422,7 @@ SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
           ConstantExpr::getSub(R1->getValue(),
                                ConstantInt::get(R1->getType(), 1));
         R1Val = EvaluateConstantChrecAtConstant(this, NextVal);
-        if (Range.contains(R1Val, isSigned))
+        if (Range.contains(R1Val->getValue(), isSigned))
           return R1;
         return new SCEVCouldNotCompute();  // Something strange happened
       }
@@ -2439,7 +2444,8 @@ SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
       return new SCEVCouldNotCompute();
 
     // Check to see if we found the value!
-    if (!Range.contains(cast<SCEVConstant>(Val)->getValue(), isSigned))
+    if (!Range.contains(cast<SCEVConstant>(Val)->getValue()->getValue(), 
+                        isSigned))
       return SCEVConstant::get(TestVal);
 
     // Increment to test the next index.
index c000c73e8b2c9011b323f9fad47c9fb932f58a68..f419edcf9b94b94455441b56f1965f7d34dabc1c 100644 (file)
@@ -22,7 +22,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/ConstantRange.h"
-#include "llvm/Constants.h"
 #include "llvm/Instruction.h"
 #include "llvm/Instructions.h"
 #include "llvm/Type.h"
@@ -107,14 +106,6 @@ const Type *ConstantRange::getType() const {
   return IntegerType::get(Lower.getBitWidth()); 
 }
 
-ConstantInt *ConstantRange::getLower() const {
-  return ConstantInt::get(getType(), Lower);
-}
-
-ConstantInt *ConstantRange::getUpper() const {
-  return ConstantInt::get(getType(), Upper);
-}
-
 /// isFullSet - Return true if this set contains all of the elements possible
 /// for this data-type
 bool ConstantRange::isFullSet() const {
@@ -136,14 +127,6 @@ bool ConstantRange::isWrappedSet(bool isSigned) const {
   return Lower.ugt(Upper);
 }
 
-/// getSingleElement - If this set contains a single element, return it,
-/// otherwise return null.
-ConstantInt *ConstantRange::getSingleElement() const {
-  if (Upper == Lower + 1)  // Is it a single element range?
-    return ConstantInt::get(getType(), Lower);
-  return 0;
-}
-
 /// getSetSize - Return the number of elements in this set.
 ///
 APInt ConstantRange::getSetSize() const {
@@ -161,14 +144,13 @@ APInt ConstantRange::getSetSize() const {
 
 /// contains - Return true if the specified value is in the set.
 ///
-bool ConstantRange::contains(ConstantInt *Val, bool isSigned) const {
+bool ConstantRange::contains(const APInt &V, bool isSigned) const {
   if (Lower == Upper) {
     if (isFullSet()) 
       return true;
     return false;
   }
 
-  const APInt &V = Val->getValue();
   if (!isWrappedSet(isSigned))
     if (isSigned)
       return Lower.sle(V) && V.slt(Upper);
@@ -182,14 +164,11 @@ bool ConstantRange::contains(ConstantInt *Val, bool isSigned) const {
 
 /// subtract - Subtract the specified constant from the endpoints of this
 /// constant range.
-ConstantRange ConstantRange::subtract(ConstantInt *CI) const {
-  assert(CI->getType() == getType() && 
-         "Cannot subtract from different type range or non-integer!");
+ConstantRange ConstantRange::subtract(const APInt &Val) const {
+  assert(Val.getBitWidth() == Lower.getBitWidth() && "Wrong bit width");
   // If the set is empty or full, don't modify the endpoints.
   if (Lower == Upper) 
     return *this;
-  
-  const APInt &Val = CI->getValue();
   return ConstantRange(Lower - Val, Upper - Val);
 }
 
index 7942636da7877cd9885944f35803b1d424b5902c..277a1284eacaeab8d880b145dd737b518aea212c 100644 (file)
@@ -1018,7 +1018,10 @@ void CEE::ComputeReplacements(RegionInfo &RI) {
 
     // If we know that this value is a particular constant, set Replacement to
     // the constant...
-    Value *Replacement = VI.getBounds().getSingleElement();
+    Value *Replacement = 0;
+    const APInt * Rplcmnt = VI.getBounds().getSingleElement();
+    if (Rplcmnt)
+      Replacement = ConstantInt::get(*Rplcmnt);
 
     // If this value is not known to be some constant, figure out the lowest
     // rank value that it is known to be equal to (if anything).