LLVM API Documentation

LoopVR.cpp

Go to the documentation of this file.
00001 //===- LoopVR.cpp - Value Range analysis driven by loop information -------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // FIXME: What does this do?
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #define DEBUG_TYPE "loopvr"
00015 #include "llvm/Analysis/LoopVR.h"
00016 #include "llvm/Constants.h"
00017 #include "llvm/Instructions.h"
00018 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
00019 #include "llvm/Assembly/Writer.h"
00020 #include "llvm/Support/CFG.h"
00021 #include "llvm/Support/Debug.h"
00022 #include "llvm/Support/raw_ostream.h"
00023 using namespace llvm;
00024 
00025 char LoopVR::ID = 0;
00026 static RegisterPass<LoopVR> X("loopvr", "Loop Value Ranges", true, true);
00027 
00028 /// getRange - determine the range for a particular SCEV within a given Loop
00029 ConstantRange LoopVR::getRange(SCEVHandle S, Loop *L, ScalarEvolution &SE) {
00030   SCEVHandle T = SE.getIterationCount(L);
00031   if (isa<SCEVCouldNotCompute>(T))
00032     return ConstantRange(cast<IntegerType>(S->getType())->getBitWidth(), true);
00033 
00034   T = SE.getTruncateOrZeroExtend(T, S->getType());
00035   return getRange(S, T, SE);
00036 }
00037 
00038 /// getRange - determine the range for a particular SCEV with a given trip count
00039 ConstantRange LoopVR::getRange(SCEVHandle S, SCEVHandle T, ScalarEvolution &SE){
00040 
00041   if (SCEVConstant *C = dyn_cast<SCEVConstant>(S))
00042     return ConstantRange(C->getValue()->getValue());
00043 
00044   ConstantRange FullSet(cast<IntegerType>(S->getType())->getBitWidth(), true);
00045 
00046   // {x,+,y,+,...z}. We detect overflow by checking the size of the set after
00047   // summing the upper and lower.
00048   if (SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
00049     ConstantRange X = getRange(Add->getOperand(0), T, SE);
00050     if (X.isFullSet()) return FullSet;
00051     for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) {
00052       ConstantRange Y = getRange(Add->getOperand(i), T, SE);
00053       if (Y.isFullSet()) return FullSet;
00054 
00055       APInt Spread_X = X.getSetSize(), Spread_Y = Y.getSetSize();
00056       APInt NewLower = X.getLower() + Y.getLower();
00057       APInt NewUpper = X.getUpper() + Y.getUpper() - 1;
00058       if (NewLower == NewUpper)
00059         return FullSet;
00060 
00061       X = ConstantRange(NewLower, NewUpper);
00062       if (X.getSetSize().ult(Spread_X) || X.getSetSize().ult(Spread_Y))
00063         return FullSet; // we've wrapped, therefore, full set.
00064     }
00065     return X;
00066   }
00067 
00068   // {x,*,y,*,...,z}. In order to detect overflow, we use k*bitwidth where
00069   // k is the number of terms being multiplied.
00070   if (SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
00071     ConstantRange X = getRange(Mul->getOperand(0), T, SE);
00072     if (X.isFullSet()) return FullSet;
00073 
00074     const IntegerType *Ty = IntegerType::get(X.getBitWidth());
00075     const IntegerType *ExTy = IntegerType::get(X.getBitWidth() *
00076                                                Mul->getNumOperands());
00077     ConstantRange XExt = X.zeroExtend(ExTy->getBitWidth());
00078 
00079     for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) {
00080       ConstantRange Y = getRange(Mul->getOperand(i), T, SE);
00081       if (Y.isFullSet()) return FullSet;
00082 
00083       ConstantRange YExt = Y.zeroExtend(ExTy->getBitWidth());
00084       XExt = ConstantRange(XExt.getLower() * YExt.getLower(),
00085                            ((XExt.getUpper()-1) * (YExt.getUpper()-1)) + 1);
00086     }
00087     return XExt.truncate(Ty->getBitWidth());
00088   }
00089 
00090   // X smax Y smax ... Z is: range(smax(X_smin, Y_smin, ..., Z_smin),
00091   //                               smax(X_smax, Y_smax, ..., Z_smax))
00092   // It doesn't matter if one of the SCEVs has FullSet because we're taking
00093   // a maximum of the minimums across all of them.
00094   if (SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
00095     ConstantRange X = getRange(SMax->getOperand(0), T, SE);
00096     if (X.isFullSet()) return FullSet;
00097 
00098     APInt smin = X.getSignedMin(), smax = X.getSignedMax();
00099     for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) {
00100       ConstantRange Y = getRange(SMax->getOperand(i), T, SE);
00101       smin = APIntOps::smax(smin, Y.getSignedMin());
00102       smax = APIntOps::smax(smax, Y.getSignedMax());
00103     }
00104     if (smax + 1 == smin) return FullSet;
00105     return ConstantRange(smin, smax + 1);
00106   }
00107 
00108   // X umax Y umax ... Z is: range(umax(X_umin, Y_umin, ..., Z_umin),
00109   //                               umax(X_umax, Y_umax, ..., Z_umax))
00110   // It doesn't matter if one of the SCEVs has FullSet because we're taking
00111   // a maximum of the minimums across all of them.
00112   if (SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
00113     ConstantRange X = getRange(UMax->getOperand(0), T, SE);
00114     if (X.isFullSet()) return FullSet;
00115 
00116     APInt umin = X.getUnsignedMin(), umax = X.getUnsignedMax();
00117     for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) {
00118       ConstantRange Y = getRange(UMax->getOperand(i), T, SE);
00119       umin = APIntOps::umax(umin, Y.getUnsignedMin());
00120       umax = APIntOps::umax(umax, Y.getUnsignedMax());
00121     }
00122     if (umax + 1 == umin) return FullSet;
00123     return ConstantRange(umin, umax + 1);
00124   }
00125 
00126   // L udiv R. Luckily, there's only ever 2 sides to a udiv.
00127   if (SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
00128     ConstantRange L = getRange(UDiv->getLHS(), T, SE);
00129     ConstantRange R = getRange(UDiv->getRHS(), T, SE);
00130     if (L.isFullSet() && R.isFullSet()) return FullSet;
00131 
00132     if (R.getUnsignedMax() == 0) {
00133       // RHS must be single-element zero. Return an empty set.
00134       return ConstantRange(R.getBitWidth(), false);
00135     }
00136 
00137     APInt Lower = L.getUnsignedMin().udiv(R.getUnsignedMax());
00138 
00139     APInt Upper;
00140 
00141     if (R.getUnsignedMin() == 0) {
00142       // Just because it contains zero, doesn't mean it will also contain one.
00143       // Use maximalIntersectWith to get the right behaviour.
00144       ConstantRange NotZero(APInt(L.getBitWidth(), 1),
00145                             APInt::getNullValue(L.getBitWidth()));
00146       R = R.maximalIntersectWith(NotZero);
00147     }
00148  
00149     // But, the maximal intersection might still include zero. If it does, then
00150     // we know it also included one.
00151     if (R.contains(APInt::getNullValue(L.getBitWidth())))
00152       Upper = L.getUnsignedMax();
00153     else
00154       Upper = L.getUnsignedMax().udiv(R.getUnsignedMin());
00155 
00156     return ConstantRange(Lower, Upper);
00157   }
00158 
00159   // ConstantRange already implements the cast operators.
00160 
00161   if (SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
00162     T = SE.getTruncateOrZeroExtend(T, ZExt->getOperand()->getType());
00163     ConstantRange X = getRange(ZExt->getOperand(), T, SE);
00164     return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth());
00165   }
00166 
00167   if (SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
00168     T = SE.getTruncateOrZeroExtend(T, SExt->getOperand()->getType());
00169     ConstantRange X = getRange(SExt->getOperand(), T, SE);
00170     return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth());
00171   }
00172 
00173   if (SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
00174     T = SE.getTruncateOrZeroExtend(T, Trunc->getOperand()->getType());
00175     ConstantRange X = getRange(Trunc->getOperand(), T, SE);
00176     if (X.isFullSet()) return FullSet;
00177     return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth());
00178   }
00179 
00180   if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
00181     SCEVConstant *Trip = dyn_cast<SCEVConstant>(T);
00182     if (!Trip) return FullSet;
00183 
00184     if (AddRec->isAffine()) {
00185       SCEVHandle StartHandle = AddRec->getStart();
00186       SCEVHandle StepHandle = AddRec->getOperand(1);
00187 
00188       SCEVConstant *Step = dyn_cast<SCEVConstant>(StepHandle);
00189       if (!Step) return FullSet;
00190 
00191       uint32_t ExWidth = 2 * Trip->getValue()->getBitWidth();
00192       APInt TripExt = Trip->getValue()->getValue(); TripExt.zext(ExWidth);
00193       APInt StepExt = Step->getValue()->getValue(); StepExt.zext(ExWidth);
00194       if ((TripExt * StepExt).ugt(APInt::getLowBitsSet(ExWidth, ExWidth >> 1)))
00195         return FullSet;
00196 
00197       SCEVHandle EndHandle = SE.getAddExpr(StartHandle,
00198                                            SE.getMulExpr(T, StepHandle));
00199       SCEVConstant *Start = dyn_cast<SCEVConstant>(StartHandle);
00200       SCEVConstant *End = dyn_cast<SCEVConstant>(EndHandle);
00201       if (!Start || !End) return FullSet;
00202 
00203       const APInt &StartInt = Start->getValue()->getValue();
00204       const APInt &EndInt = End->getValue()->getValue();
00205       const APInt &StepInt = Step->getValue()->getValue();
00206 
00207       if (StepInt.isNegative()) {
00208         if (EndInt == StartInt + 1) return FullSet;
00209         return ConstantRange(EndInt, StartInt + 1);
00210       } else {
00211         if (StartInt == EndInt + 1) return FullSet;
00212         return ConstantRange(StartInt, EndInt + 1);
00213       }
00214     }
00215   }
00216 
00217   // TODO: non-affine addrec, udiv, SCEVUnknown (narrowed from elsewhere)?
00218 
00219   return FullSet;
00220 }
00221 
00222 bool LoopVR::runOnFunction(Function &F) { Map.clear(); return false; }
00223 
00224 void LoopVR::print(std::ostream &os, const Module *) const {
00225   raw_os_ostream OS(os);
00226   for (std::map<Value *, ConstantRange *>::const_iterator I = Map.begin(),
00227        E = Map.end(); I != E; ++I) {
00228     OS << *I->first << ": " << *I->second << '\n';
00229   }
00230 }
00231 
00232 void LoopVR::releaseMemory() {
00233   for (std::map<Value *, ConstantRange *>::iterator I = Map.begin(),
00234        E = Map.end(); I != E; ++I) {
00235     delete I->second;
00236   }
00237 
00238   Map.clear();  
00239 }
00240 
00241 ConstantRange LoopVR::compute(Value *V) {
00242   if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
00243     return ConstantRange(CI->getValue());
00244 
00245   Instruction *I = dyn_cast<Instruction>(V);
00246   if (!I)
00247     return ConstantRange(cast<IntegerType>(V->getType())->getBitWidth(), false);
00248 
00249   LoopInfo &LI = getAnalysis<LoopInfo>();
00250 
00251   Loop *L = LI.getLoopFor(I->getParent());
00252   if (!L || L->isLoopInvariant(I))
00253     return ConstantRange(cast<IntegerType>(V->getType())->getBitWidth(), false);
00254 
00255   ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
00256 
00257   SCEVHandle S = SE.getSCEV(I);
00258   if (isa<SCEVUnknown>(S) || isa<SCEVCouldNotCompute>(S))
00259     return ConstantRange(cast<IntegerType>(V->getType())->getBitWidth(), false);
00260 
00261   return ConstantRange(getRange(S, L, SE));
00262 }
00263 
00264 ConstantRange LoopVR::get(Value *V) {
00265   std::map<Value *, ConstantRange *>::iterator I = Map.find(V);
00266   if (I == Map.end()) {
00267     ConstantRange *CR = new ConstantRange(compute(V));
00268     Map[V] = CR;
00269     return *CR;
00270   }
00271 
00272   return *I->second;
00273 }
00274 
00275 void LoopVR::remove(Value *V) {
00276   std::map<Value *, ConstantRange *>::iterator I = Map.find(V);
00277   if (I != Map.end()) {
00278     delete I->second;
00279     Map.erase(I);
00280   }
00281 }
00282 
00283 void LoopVR::narrow(Value *V, const ConstantRange &CR) {
00284   if (CR.isFullSet()) return;
00285 
00286   std::map<Value *, ConstantRange *>::iterator I = Map.find(V);
00287   if (I == Map.end())
00288     Map[V] = new ConstantRange(CR);
00289   else
00290     Map[V] = new ConstantRange(Map[V]->maximalIntersectWith(CR));
00291 }