Extend the ValuesAtScope cache to cover all expressions, not just
[oota-llvm.git] / lib / Analysis / LoopVR.cpp
index 0a3d06bed7e897ca3aa34badc40bf29fb56d712c..573bd3ea01620ca6df2425e9a26a956eea88ffce 100644 (file)
@@ -15,6 +15,8 @@
 #include "llvm/Analysis/LoopVR.h"
 #include "llvm/Constants.h"
 #include "llvm/Instructions.h"
+#include "llvm/LLVMContext.h"
+#include "llvm/Analysis/LoopInfo.h"
 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
 #include "llvm/Assembly/Writer.h"
 #include "llvm/Support/CFG.h"
@@ -26,8 +28,8 @@ char LoopVR::ID = 0;
 static RegisterPass<LoopVR> X("loopvr", "Loop Value Ranges", false, true);
 
 /// getRange - determine the range for a particular SCEV within a given Loop
-ConstantRange LoopVR::getRange(SCEVHandle S, Loop *L, ScalarEvolution &SE) {
-  SCEVHandle T = SE.getBackedgeTakenCount(L);
+ConstantRange LoopVR::getRange(const SCEV *S, Loop *L, ScalarEvolution &SE) {
+  const SCEV *T = SE.getBackedgeTakenCount(L);
   if (isa<SCEVCouldNotCompute>(T))
     return ConstantRange(cast<IntegerType>(S->getType())->getBitWidth(), true);
 
@@ -36,11 +38,11 @@ ConstantRange LoopVR::getRange(SCEVHandle S, Loop *L, ScalarEvolution &SE) {
 }
 
 /// getRange - determine the range for a particular SCEV with a given trip count
-ConstantRange LoopVR::getRange(SCEVHandle S, SCEVHandle T, ScalarEvolution &SE){
+ConstantRange LoopVR::getRange(const SCEV *S, const SCEV *T, ScalarEvolution &SE){
 
   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
     return ConstantRange(C->getValue()->getValue());
-
+    
   ConstantRange FullSet(cast<IntegerType>(S->getType())->getBitWidth(), true);
 
   // {x,+,y,+,...z}. We detect overflow by checking the size of the set after
@@ -71,9 +73,9 @@ ConstantRange LoopVR::getRange(SCEVHandle S, SCEVHandle T, ScalarEvolution &SE){
     ConstantRange X = getRange(Mul->getOperand(0), T, SE);
     if (X.isFullSet()) return FullSet;
 
-    const IntegerType *Ty = IntegerType::get(X.getBitWidth());
-    const IntegerType *ExTy = IntegerType::get(X.getBitWidth() *
-                                               Mul->getNumOperands());
+    const IntegerType *Ty = IntegerType::get(SE.getContext(), X.getBitWidth());
+    const IntegerType *ExTy = IntegerType::get(SE.getContext(),
+                                      X.getBitWidth() * Mul->getNumOperands());
     ConstantRange XExt = X.zeroExtend(ExTy->getBitWidth());
 
     for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) {
@@ -140,14 +142,13 @@ ConstantRange LoopVR::getRange(SCEVHandle S, SCEVHandle T, ScalarEvolution &SE){
 
     if (R.getUnsignedMin() == 0) {
       // Just because it contains zero, doesn't mean it will also contain one.
-      // Use maximalIntersectWith to get the right behaviour.
       ConstantRange NotZero(APInt(L.getBitWidth(), 1),
                             APInt::getNullValue(L.getBitWidth()));
-      R = R.maximalIntersectWith(NotZero);
+      R = R.intersectWith(NotZero);
     }
  
-    // But, the maximal intersection might still include zero. If it does, then
-    // we know it also included one.
+    // But, the intersection might still include zero. If it does, then we know
+    // it also included one.
     if (R.contains(APInt::getNullValue(L.getBitWidth())))
       Upper = L.getUnsignedMax();
     else
@@ -182,8 +183,8 @@ ConstantRange LoopVR::getRange(SCEVHandle S, SCEVHandle T, ScalarEvolution &SE){
     if (!Trip) return FullSet;
 
     if (AddRec->isAffine()) {
-      SCEVHandle StartHandle = AddRec->getStart();
-      SCEVHandle StepHandle = AddRec->getOperand(1);
+      const SCEV *StartHandle = AddRec->getStart();
+      const SCEV *StepHandle = AddRec->getOperand(1);
 
       const SCEVConstant *Step = dyn_cast<SCEVConstant>(StepHandle);
       if (!Step) return FullSet;
@@ -194,7 +195,7 @@ ConstantRange LoopVR::getRange(SCEVHandle S, SCEVHandle T, ScalarEvolution &SE){
       if ((TripExt * StepExt).ugt(APInt::getLowBitsSet(ExWidth, ExWidth >> 1)))
         return FullSet;
 
-      SCEVHandle EndHandle = SE.getAddExpr(StartHandle,
+      const SCEV *EndHandle = SE.getAddExpr(StartHandle,
                                            SE.getMulExpr(T, StepHandle));
       const SCEVConstant *Start = dyn_cast<SCEVConstant>(StartHandle);
       const SCEVConstant *End = dyn_cast<SCEVConstant>(EndHandle);
@@ -219,10 +220,15 @@ ConstantRange LoopVR::getRange(SCEVHandle S, SCEVHandle T, ScalarEvolution &SE){
   return FullSet;
 }
 
+void LoopVR::getAnalysisUsage(AnalysisUsage &AU) const {
+  AU.addRequiredTransitive<LoopInfo>();
+  AU.addRequiredTransitive<ScalarEvolution>();
+  AU.setPreservesAll();
+}
+
 bool LoopVR::runOnFunction(Function &F) { Map.clear(); return false; }
 
-void LoopVR::print(std::ostream &os, const Module *) const {
-  raw_os_ostream OS(os);
+void LoopVR::print(raw_ostream &OS, const Module *) const {
   for (std::map<Value *, ConstantRange *>::const_iterator I = Map.begin(),
        E = Map.end(); I != E; ++I) {
     OS << *I->first << ": " << *I->second << '\n';
@@ -254,7 +260,7 @@ ConstantRange LoopVR::compute(Value *V) {
 
   ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
 
-  SCEVHandle S = SE.getSCEV(I);
+  const SCEV *S = SE.getSCEV(I);
   if (isa<SCEVUnknown>(S) || isa<SCEVCouldNotCompute>(S))
     return ConstantRange(cast<IntegerType>(V->getType())->getBitWidth(), false);
 
@@ -287,5 +293,5 @@ void LoopVR::narrow(Value *V, const ConstantRange &CR) {
   if (I == Map.end())
     Map[V] = new ConstantRange(CR);
   else
-    Map[V] = new ConstantRange(Map[V]->maximalIntersectWith(CR));
+    Map[V] = new ConstantRange(Map[V]->intersectWith(CR));
 }