LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ScalarEvolution.cpp

Go to the documentation of this file.
00001 //===- ScalarEvolution.cpp - Scalar Evolution Analysis ----------*- C++ -*-===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the implementation of the scalar evolution analysis
00011 // engine, which is used primarily to analyze expressions involving induction
00012 // variables in loops.
00013 //
00014 // There are several aspects to this library.  First is the representation of
00015 // scalar expressions, which are represented as subclasses of the SCEV class.
00016 // These classes are used to represent certain types of subexpressions that we
00017 // can handle.  These classes are reference counted, managed by the SCEVHandle
00018 // class.  We only create one SCEV of a particular shape, so pointer-comparisons
00019 // for equality are legal.
00020 //
00021 // One important aspect of the SCEV objects is that they are never cyclic, even
00022 // if there is a cycle in the dataflow for an expression (ie, a PHI node).  If
00023 // the PHI node is one of the idioms that we can represent (e.g., a polynomial
00024 // recurrence) then we represent it directly as a recurrence node, otherwise we
00025 // represent it as a SCEVUnknown node.
00026 //
00027 // In addition to being able to represent expressions of various types, we also
00028 // have folders that are used to build the *canonical* representation for a
00029 // particular expression.  These folders are capable of using a variety of
00030 // rewrite rules to simplify the expressions.
00031 // 
00032 // Once the folders are defined, we can implement the more interesting
00033 // higher-level code, such as the code that recognizes PHI nodes of various
00034 // types, computes the execution count of a loop, etc.
00035 //
00036 // TODO: We should use these routines and value representations to implement
00037 // dependence analysis!
00038 //
00039 //===----------------------------------------------------------------------===//
00040 //
00041 // There are several good references for the techniques used in this analysis.
00042 //
00043 //  Chains of recurrences -- a method to expedite the evaluation
00044 //  of closed-form functions
00045 //  Olaf Bachmann, Paul S. Wang, Eugene V. Zima
00046 //
00047 //  On computational properties of chains of recurrences
00048 //  Eugene V. Zima
00049 //
00050 //  Symbolic Evaluation of Chains of Recurrences for Loop Optimization
00051 //  Robert A. van Engelen
00052 //
00053 //  Efficient Symbolic Analysis for Optimizing Compilers
00054 //  Robert A. van Engelen
00055 //
00056 //  Using the chains of recurrences algebra for data dependence testing and
00057 //  induction variable substitution
00058 //  MS Thesis, Johnie Birch
00059 //
00060 //===----------------------------------------------------------------------===//
00061 
00062 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
00063 #include "llvm/Constants.h"
00064 #include "llvm/DerivedTypes.h"
00065 #include "llvm/GlobalVariable.h"
00066 #include "llvm/Instructions.h"
00067 #include "llvm/Analysis/LoopInfo.h"
00068 #include "llvm/Assembly/Writer.h"
00069 #include "llvm/Transforms/Scalar.h"
00070 #include "llvm/Transforms/Utils/Local.h"
00071 #include "llvm/Support/CFG.h"
00072 #include "llvm/Support/ConstantRange.h"
00073 #include "llvm/Support/InstIterator.h"
00074 #include "llvm/Support/CommandLine.h"
00075 #include "llvm/ADT/Statistic.h"
00076 #include <cmath>
00077 #include <algorithm>
00078 using namespace llvm;
00079 
00080 namespace {
00081   RegisterAnalysis<ScalarEvolution>
00082   R("scalar-evolution", "Scalar Evolution Analysis");
00083 
00084   Statistic<>
00085   NumBruteForceEvaluations("scalar-evolution",
00086                            "Number of brute force evaluations needed to "
00087                            "calculate high-order polynomial exit values");
00088   Statistic<>
00089   NumArrayLenItCounts("scalar-evolution",
00090                       "Number of trip counts computed with array length");
00091   Statistic<>
00092   NumTripCountsComputed("scalar-evolution",
00093                         "Number of loops with predictable loop counts");
00094   Statistic<>
00095   NumTripCountsNotComputed("scalar-evolution",
00096                            "Number of loops without predictable loop counts");
00097   Statistic<>
00098   NumBruteForceTripCountsComputed("scalar-evolution",
00099                         "Number of loops with trip counts computed by force");
00100 
00101   cl::opt<unsigned>
00102   MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden,
00103                           cl::desc("Maximum number of iterations SCEV will symbolically execute a constant derived loop"),
00104                           cl::init(100));
00105 }
00106 
00107 //===----------------------------------------------------------------------===//
00108 //                           SCEV class definitions
00109 //===----------------------------------------------------------------------===//
00110 
00111 //===----------------------------------------------------------------------===//
00112 // Implementation of the SCEV class.
00113 //
00114 SCEV::~SCEV() {}
00115 void SCEV::dump() const {
00116   print(std::cerr);
00117 }
00118 
00119 /// getValueRange - Return the tightest constant bounds that this value is
00120 /// known to have.  This method is only valid on integer SCEV objects.
00121 ConstantRange SCEV::getValueRange() const {
00122   const Type *Ty = getType();
00123   assert(Ty->isInteger() && "Can't get range for a non-integer SCEV!");
00124   Ty = Ty->getUnsignedVersion();
00125   // Default to a full range if no better information is available.
00126   return ConstantRange(getType());
00127 }
00128 
00129 
00130 SCEVCouldNotCompute::SCEVCouldNotCompute() : SCEV(scCouldNotCompute) {}
00131 
00132 bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const {
00133   assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
00134   return false;
00135 }
00136 
00137 const Type *SCEVCouldNotCompute::getType() const {
00138   assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
00139   return 0;
00140 }
00141 
00142 bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const {
00143   assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
00144   return false;
00145 }
00146 
00147 void SCEVCouldNotCompute::print(std::ostream &OS) const {
00148   OS << "***COULDNOTCOMPUTE***";
00149 }
00150 
00151 bool SCEVCouldNotCompute::classof(const SCEV *S) {
00152   return S->getSCEVType() == scCouldNotCompute;
00153 }
00154 
00155 
00156 // SCEVConstants - Only allow the creation of one SCEVConstant for any
00157 // particular value.  Don't use a SCEVHandle here, or else the object will
00158 // never be deleted!
00159 static std::map<ConstantInt*, SCEVConstant*> SCEVConstants;
00160   
00161 
00162 SCEVConstant::~SCEVConstant() {
00163   SCEVConstants.erase(V);
00164 }
00165 
00166 SCEVHandle SCEVConstant::get(ConstantInt *V) {
00167   // Make sure that SCEVConstant instances are all unsigned.
00168   if (V->getType()->isSigned()) {
00169     const Type *NewTy = V->getType()->getUnsignedVersion();
00170     V = cast<ConstantUInt>(ConstantExpr::getCast(V, NewTy));
00171   }
00172   
00173   SCEVConstant *&R = SCEVConstants[V];
00174   if (R == 0) R = new SCEVConstant(V);
00175   return R;
00176 }
00177 
00178 ConstantRange SCEVConstant::getValueRange() const {
00179   return ConstantRange(V);
00180 }
00181 
00182 const Type *SCEVConstant::getType() const { return V->getType(); }
00183 
00184 void SCEVConstant::print(std::ostream &OS) const {
00185   WriteAsOperand(OS, V, false);
00186 }
00187 
00188 // SCEVTruncates - Only allow the creation of one SCEVTruncateExpr for any
00189 // particular input.  Don't use a SCEVHandle here, or else the object will
00190 // never be deleted!
00191 static std::map<std::pair<SCEV*, const Type*>, SCEVTruncateExpr*> SCEVTruncates;
00192 
00193 SCEVTruncateExpr::SCEVTruncateExpr(const SCEVHandle &op, const Type *ty)
00194   : SCEV(scTruncate), Op(op), Ty(ty) {
00195   assert(Op->getType()->isInteger() && Ty->isInteger() &&
00196          Ty->isUnsigned() &&
00197          "Cannot truncate non-integer value!");
00198   assert(Op->getType()->getPrimitiveSize() > Ty->getPrimitiveSize() &&
00199          "This is not a truncating conversion!");
00200 }
00201 
00202 SCEVTruncateExpr::~SCEVTruncateExpr() {
00203   SCEVTruncates.erase(std::make_pair(Op, Ty));
00204 }
00205 
00206 ConstantRange SCEVTruncateExpr::getValueRange() const {
00207   return getOperand()->getValueRange().truncate(getType());
00208 }
00209 
00210 void SCEVTruncateExpr::print(std::ostream &OS) const {
00211   OS << "(truncate " << *Op << " to " << *Ty << ")";
00212 }
00213 
00214 // SCEVZeroExtends - Only allow the creation of one SCEVZeroExtendExpr for any
00215 // particular input.  Don't use a SCEVHandle here, or else the object will never
00216 // be deleted!
00217 static std::map<std::pair<SCEV*, const Type*>,
00218                 SCEVZeroExtendExpr*> SCEVZeroExtends;
00219 
00220 SCEVZeroExtendExpr::SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty)
00221   : SCEV(scTruncate), Op(Op), Ty(ty) {
00222   assert(Op->getType()->isInteger() && Ty->isInteger() &&
00223          Ty->isUnsigned() &&
00224          "Cannot zero extend non-integer value!");
00225   assert(Op->getType()->getPrimitiveSize() < Ty->getPrimitiveSize() &&
00226          "This is not an extending conversion!");
00227 }
00228 
00229 SCEVZeroExtendExpr::~SCEVZeroExtendExpr() {
00230   SCEVZeroExtends.erase(std::make_pair(Op, Ty));
00231 }
00232 
00233 ConstantRange SCEVZeroExtendExpr::getValueRange() const {
00234   return getOperand()->getValueRange().zeroExtend(getType());
00235 }
00236 
00237 void SCEVZeroExtendExpr::print(std::ostream &OS) const {
00238   OS << "(zeroextend " << *Op << " to " << *Ty << ")";
00239 }
00240 
00241 // SCEVCommExprs - Only allow the creation of one SCEVCommutativeExpr for any
00242 // particular input.  Don't use a SCEVHandle here, or else the object will never
00243 // be deleted!
00244 static std::map<std::pair<unsigned, std::vector<SCEV*> >,
00245                 SCEVCommutativeExpr*> SCEVCommExprs;
00246 
00247 SCEVCommutativeExpr::~SCEVCommutativeExpr() {
00248   SCEVCommExprs.erase(std::make_pair(getSCEVType(),
00249                                      std::vector<SCEV*>(Operands.begin(),
00250                                                         Operands.end())));
00251 }
00252 
00253 void SCEVCommutativeExpr::print(std::ostream &OS) const {
00254   assert(Operands.size() > 1 && "This plus expr shouldn't exist!");
00255   const char *OpStr = getOperationStr();
00256   OS << "(" << *Operands[0];
00257   for (unsigned i = 1, e = Operands.size(); i != e; ++i)
00258     OS << OpStr << *Operands[i];
00259   OS << ")";
00260 }
00261 
00262 // SCEVUDivs - Only allow the creation of one SCEVUDivExpr for any particular
00263 // input.  Don't use a SCEVHandle here, or else the object will never be
00264 // deleted!
00265 static std::map<std::pair<SCEV*, SCEV*>, SCEVUDivExpr*> SCEVUDivs;
00266 
00267 SCEVUDivExpr::~SCEVUDivExpr() {
00268   SCEVUDivs.erase(std::make_pair(LHS, RHS));
00269 }
00270 
00271 void SCEVUDivExpr::print(std::ostream &OS) const {
00272   OS << "(" << *LHS << " /u " << *RHS << ")";
00273 }
00274 
00275 const Type *SCEVUDivExpr::getType() const {
00276   const Type *Ty = LHS->getType();
00277   if (Ty->isSigned()) Ty = Ty->getUnsignedVersion();
00278   return Ty;
00279 }
00280 
00281 // SCEVAddRecExprs - Only allow the creation of one SCEVAddRecExpr for any
00282 // particular input.  Don't use a SCEVHandle here, or else the object will never
00283 // be deleted!
00284 static std::map<std::pair<const Loop *, std::vector<SCEV*> >,
00285                 SCEVAddRecExpr*> SCEVAddRecExprs;
00286 
00287 SCEVAddRecExpr::~SCEVAddRecExpr() {
00288   SCEVAddRecExprs.erase(std::make_pair(L,
00289                                        std::vector<SCEV*>(Operands.begin(),
00290                                                           Operands.end())));
00291 }
00292 
00293 bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const {
00294   // This recurrence is invariant w.r.t to QueryLoop iff QueryLoop doesn't
00295   // contain L.
00296   return !QueryLoop->contains(L->getHeader());
00297 }
00298 
00299 
00300 void SCEVAddRecExpr::print(std::ostream &OS) const {
00301   OS << "{" << *Operands[0];
00302   for (unsigned i = 1, e = Operands.size(); i != e; ++i)
00303     OS << ",+," << *Operands[i];
00304   OS << "}<" << L->getHeader()->getName() + ">";
00305 }
00306 
00307 // SCEVUnknowns - Only allow the creation of one SCEVUnknown for any particular
00308 // value.  Don't use a SCEVHandle here, or else the object will never be
00309 // deleted!
00310 static std::map<Value*, SCEVUnknown*> SCEVUnknowns;
00311 
00312 SCEVUnknown::~SCEVUnknown() { SCEVUnknowns.erase(V); }
00313 
00314 bool SCEVUnknown::isLoopInvariant(const Loop *L) const {
00315   // All non-instruction values are loop invariant.  All instructions are loop
00316   // invariant if they are not contained in the specified loop.
00317   if (Instruction *I = dyn_cast<Instruction>(V))
00318     return !L->contains(I->getParent());
00319   return true;
00320 }
00321 
00322 const Type *SCEVUnknown::getType() const {
00323   return V->getType();
00324 }
00325 
00326 void SCEVUnknown::print(std::ostream &OS) const {
00327   WriteAsOperand(OS, V, false);
00328 }
00329 
00330 //===----------------------------------------------------------------------===//
00331 //                               SCEV Utilities
00332 //===----------------------------------------------------------------------===//
00333 
00334 namespace {
00335   /// SCEVComplexityCompare - Return true if the complexity of the LHS is less
00336   /// than the complexity of the RHS.  This comparator is used to canonicalize
00337   /// expressions.
00338   struct SCEVComplexityCompare {
00339     bool operator()(SCEV *LHS, SCEV *RHS) {
00340       return LHS->getSCEVType() < RHS->getSCEVType();
00341     }
00342   };
00343 }
00344 
00345 /// GroupByComplexity - Given a list of SCEV objects, order them by their
00346 /// complexity, and group objects of the same complexity together by value.
00347 /// When this routine is finished, we know that any duplicates in the vector are
00348 /// consecutive and that complexity is monotonically increasing.
00349 ///
00350 /// Note that we go take special precautions to ensure that we get determinstic
00351 /// results from this routine.  In other words, we don't want the results of
00352 /// this to depend on where the addresses of various SCEV objects happened to
00353 /// land in memory.
00354 ///
00355 static void GroupByComplexity(std::vector<SCEVHandle> &Ops) {
00356   if (Ops.size() < 2) return;  // Noop
00357   if (Ops.size() == 2) {
00358     // This is the common case, which also happens to be trivially simple.
00359     // Special case it.
00360     if (Ops[0]->getSCEVType() > Ops[1]->getSCEVType())
00361       std::swap(Ops[0], Ops[1]);
00362     return;
00363   }
00364 
00365   // Do the rough sort by complexity.
00366   std::sort(Ops.begin(), Ops.end(), SCEVComplexityCompare());
00367 
00368   // Now that we are sorted by complexity, group elements of the same
00369   // complexity.  Note that this is, at worst, N^2, but the vector is likely to
00370   // be extremely short in practice.  Note that we take this approach because we
00371   // do not want to depend on the addresses of the objects we are grouping.
00372   for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
00373     SCEV *S = Ops[i];
00374     unsigned Complexity = S->getSCEVType();
00375 
00376     // If there are any objects of the same complexity and same value as this
00377     // one, group them.
00378     for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
00379       if (Ops[j] == S) { // Found a duplicate.
00380         // Move it to immediately after i'th element.
00381         std::swap(Ops[i+1], Ops[j]);
00382         ++i;   // no need to rescan it.
00383         if (i == e-2) return;  // Done!
00384       }
00385     }
00386   }
00387 }
00388 
00389 
00390 
00391 //===----------------------------------------------------------------------===//
00392 //                      Simple SCEV method implementations
00393 //===----------------------------------------------------------------------===//
00394 
00395 /// getIntegerSCEV - Given an integer or FP type, create a constant for the
00396 /// specified signed integer value and return a SCEV for the constant.
00397 SCEVHandle SCEVUnknown::getIntegerSCEV(int Val, const Type *Ty) {
00398   Constant *C;
00399   if (Val == 0) 
00400     C = Constant::getNullValue(Ty);
00401   else if (Ty->isFloatingPoint())
00402     C = ConstantFP::get(Ty, Val);
00403   else if (Ty->isSigned())
00404     C = ConstantSInt::get(Ty, Val);
00405   else {
00406     C = ConstantSInt::get(Ty->getSignedVersion(), Val);
00407     C = ConstantExpr::getCast(C, Ty);
00408   }
00409   return SCEVUnknown::get(C);
00410 }
00411 
00412 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the
00413 /// input value to the specified type.  If the type must be extended, it is zero
00414 /// extended.
00415 static SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty) {
00416   const Type *SrcTy = V->getType();
00417   assert(SrcTy->isInteger() && Ty->isInteger() &&
00418          "Cannot truncate or zero extend with non-integer arguments!");
00419   if (SrcTy->getPrimitiveSize() == Ty->getPrimitiveSize())
00420     return V;  // No conversion
00421   if (SrcTy->getPrimitiveSize() > Ty->getPrimitiveSize())
00422     return SCEVTruncateExpr::get(V, Ty);
00423   return SCEVZeroExtendExpr::get(V, Ty);
00424 }
00425 
00426 /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
00427 ///
00428 static SCEVHandle getNegativeSCEV(const SCEVHandle &V) {
00429   if (SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
00430     return SCEVUnknown::get(ConstantExpr::getNeg(VC->getValue()));
00431   
00432   return SCEVMulExpr::get(V, SCEVUnknown::getIntegerSCEV(-1, V->getType()));
00433 }
00434 
00435 /// getMinusSCEV - Return a SCEV corresponding to LHS - RHS.
00436 ///
00437 static SCEVHandle getMinusSCEV(const SCEVHandle &LHS, const SCEVHandle &RHS) {
00438   // X - Y --> X + -Y
00439   return SCEVAddExpr::get(LHS, getNegativeSCEV(RHS));
00440 }
00441 
00442 
00443 /// Binomial - Evaluate N!/((N-M)!*M!)  .  Note that N is often large and M is
00444 /// often very small, so we try to reduce the number of N! terms we need to
00445 /// evaluate by evaluating this as  (N!/(N-M)!)/M!
00446 static ConstantInt *Binomial(ConstantInt *N, unsigned M) {
00447   uint64_t NVal = N->getRawValue();
00448   uint64_t FirstTerm = 1;
00449   for (unsigned i = 0; i != M; ++i)
00450     FirstTerm *= NVal-i;
00451 
00452   unsigned MFactorial = 1;
00453   for (; M; --M)
00454     MFactorial *= M;
00455 
00456   Constant *Result = ConstantUInt::get(Type::ULongTy, FirstTerm/MFactorial);
00457   Result = ConstantExpr::getCast(Result, N->getType());
00458   assert(isa<ConstantInt>(Result) && "Cast of integer not folded??");
00459   return cast<ConstantInt>(Result);
00460 }
00461 
00462 /// PartialFact - Compute V!/(V-NumSteps)!
00463 static SCEVHandle PartialFact(SCEVHandle V, unsigned NumSteps) {
00464   // Handle this case efficiently, it is common to have constant iteration
00465   // counts while computing loop exit values.
00466   if (SCEVConstant *SC = dyn_cast<SCEVConstant>(V)) {
00467     uint64_t Val = SC->getValue()->getRawValue();
00468     uint64_t Result = 1;
00469     for (; NumSteps; --NumSteps)
00470       Result *= Val-(NumSteps-1);
00471     Constant *Res = ConstantUInt::get(Type::ULongTy, Result);
00472     return SCEVUnknown::get(ConstantExpr::getCast(Res, V->getType()));
00473   }
00474 
00475   const Type *Ty = V->getType();
00476   if (NumSteps == 0)
00477     return SCEVUnknown::getIntegerSCEV(1, Ty);
00478   
00479   SCEVHandle Result = V;
00480   for (unsigned i = 1; i != NumSteps; ++i)
00481     Result = SCEVMulExpr::get(Result, getMinusSCEV(V,
00482                                           SCEVUnknown::getIntegerSCEV(i, Ty)));
00483   return Result;
00484 }
00485 
00486 
00487 /// evaluateAtIteration - Return the value of this chain of recurrences at
00488 /// the specified iteration number.  We can evaluate this recurrence by
00489 /// multiplying each element in the chain by the binomial coefficient
00490 /// corresponding to it.  In other words, we can evaluate {A,+,B,+,C,+,D} as:
00491 ///
00492 ///   A*choose(It, 0) + B*choose(It, 1) + C*choose(It, 2) + D*choose(It, 3)
00493 ///
00494 /// FIXME/VERIFY: I don't trust that this is correct in the face of overflow.
00495 /// Is the binomial equation safe using modular arithmetic??
00496 ///
00497 SCEVHandle SCEVAddRecExpr::evaluateAtIteration(SCEVHandle It) const {
00498   SCEVHandle Result = getStart();
00499   int Divisor = 1;
00500   const Type *Ty = It->getType();
00501   for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
00502     SCEVHandle BC = PartialFact(It, i);
00503     Divisor *= i;
00504     SCEVHandle Val = SCEVUDivExpr::get(SCEVMulExpr::get(BC, getOperand(i)),
00505                                        SCEVUnknown::getIntegerSCEV(Divisor,Ty));
00506     Result = SCEVAddExpr::get(Result, Val);
00507   }
00508   return Result;
00509 }
00510 
00511 
00512 //===----------------------------------------------------------------------===//
00513 //                    SCEV Expression folder implementations
00514 //===----------------------------------------------------------------------===//
00515 
00516 SCEVHandle SCEVTruncateExpr::get(const SCEVHandle &Op, const Type *Ty) {
00517   if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
00518     return SCEVUnknown::get(ConstantExpr::getCast(SC->getValue(), Ty));
00519 
00520   // If the input value is a chrec scev made out of constants, truncate
00521   // all of the constants.
00522   if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
00523     std::vector<SCEVHandle> Operands;
00524     for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
00525       // FIXME: This should allow truncation of other expression types!
00526       if (isa<SCEVConstant>(AddRec->getOperand(i)))
00527         Operands.push_back(get(AddRec->getOperand(i), Ty));
00528       else
00529         break;
00530     if (Operands.size() == AddRec->getNumOperands())
00531       return SCEVAddRecExpr::get(Operands, AddRec->getLoop());
00532   }
00533 
00534   SCEVTruncateExpr *&Result = SCEVTruncates[std::make_pair(Op, Ty)];
00535   if (Result == 0) Result = new SCEVTruncateExpr(Op, Ty);
00536   return Result;
00537 }
00538 
00539 SCEVHandle SCEVZeroExtendExpr::get(const SCEVHandle &Op, const Type *Ty) {
00540   if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
00541     return SCEVUnknown::get(ConstantExpr::getCast(SC->getValue(), Ty));
00542 
00543   // FIXME: If the input value is a chrec scev, and we can prove that the value
00544   // did not overflow the old, smaller, value, we can zero extend all of the
00545   // operands (often constants).  This would allow analysis of something like
00546   // this:  for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
00547 
00548   SCEVZeroExtendExpr *&Result = SCEVZeroExtends[std::make_pair(Op, Ty)];
00549   if (Result == 0) Result = new SCEVZeroExtendExpr(Op, Ty);
00550   return Result;
00551 }
00552 
00553 // get - Get a canonical add expression, or something simpler if possible.
00554 SCEVHandle SCEVAddExpr::get(std::vector<SCEVHandle> &Ops) {
00555   assert(!Ops.empty() && "Cannot get empty add!");
00556   if (Ops.size() == 1) return Ops[0];
00557 
00558   // Sort by complexity, this groups all similar expression types together.
00559   GroupByComplexity(Ops);
00560 
00561   // If there are any constants, fold them together.
00562   unsigned Idx = 0;
00563   if (SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
00564     ++Idx;
00565     assert(Idx < Ops.size());
00566     while (SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
00567       // We found two constants, fold them together!
00568       Constant *Fold = ConstantExpr::getAdd(LHSC->getValue(), RHSC->getValue());
00569       if (ConstantInt *CI = dyn_cast<ConstantInt>(Fold)) {
00570         Ops[0] = SCEVConstant::get(CI);
00571         Ops.erase(Ops.begin()+1);  // Erase the folded element
00572         if (Ops.size() == 1) return Ops[0];
00573       } else {
00574         // If we couldn't fold the expression, move to the next constant.  Note
00575         // that this is impossible to happen in practice because we always
00576         // constant fold constant ints to constant ints.
00577         ++Idx;
00578       }
00579     }
00580 
00581     // If we are left with a constant zero being added, strip it off.
00582     if (cast<SCEVConstant>(Ops[0])->getValue()->isNullValue()) {
00583       Ops.erase(Ops.begin());
00584       --Idx;
00585     }
00586   }
00587 
00588   if (Ops.size() == 1) return Ops[0];
00589   
00590   // Okay, check to see if the same value occurs in the operand list twice.  If
00591   // so, merge them together into an multiply expression.  Since we sorted the
00592   // list, these values are required to be adjacent.
00593   const Type *Ty = Ops[0]->getType();
00594   for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
00595     if (Ops[i] == Ops[i+1]) {      //  X + Y + Y  -->  X + Y*2
00596       // Found a match, merge the two values into a multiply, and add any
00597       // remaining values to the result.
00598       SCEVHandle Two = SCEVUnknown::getIntegerSCEV(2, Ty);
00599       SCEVHandle Mul = SCEVMulExpr::get(Ops[i], Two);
00600       if (Ops.size() == 2)
00601         return Mul;
00602       Ops.erase(Ops.begin()+i, Ops.begin()+i+2);
00603       Ops.push_back(Mul);
00604       return SCEVAddExpr::get(Ops);
00605     }
00606 
00607   // Okay, now we know the first non-constant operand.  If there are add
00608   // operands they would be next.
00609   if (Idx < Ops.size()) {
00610     bool DeletedAdd = false;
00611     while (SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
00612       // If we have an add, expand the add operands onto the end of the operands
00613       // list.
00614       Ops.insert(Ops.end(), Add->op_begin(), Add->op_end());
00615       Ops.erase(Ops.begin()+Idx);
00616       DeletedAdd = true;
00617     }
00618 
00619     // If we deleted at least one add, we added operands to the end of the list,
00620     // and they are not necessarily sorted.  Recurse to resort and resimplify
00621     // any operands we just aquired.
00622     if (DeletedAdd)
00623       return get(Ops);
00624   }
00625 
00626   // Skip over the add expression until we get to a multiply.
00627   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
00628     ++Idx;
00629 
00630   // If we are adding something to a multiply expression, make sure the
00631   // something is not already an operand of the multiply.  If so, merge it into
00632   // the multiply.
00633   for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
00634     SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
00635     for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
00636       SCEV *MulOpSCEV = Mul->getOperand(MulOp);
00637       for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
00638         if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(MulOpSCEV)) {
00639           // Fold W + X + (X * Y * Z)  -->  W + (X * ((Y*Z)+1))
00640           SCEVHandle InnerMul = Mul->getOperand(MulOp == 0);
00641           if (Mul->getNumOperands() != 2) {
00642             // If the multiply has more than two operands, we must get the
00643             // Y*Z term.
00644             std::vector<SCEVHandle> MulOps(Mul->op_begin(), Mul->op_end());
00645             MulOps.erase(MulOps.begin()+MulOp);
00646             InnerMul = SCEVMulExpr::get(MulOps);
00647           }
00648           SCEVHandle One = SCEVUnknown::getIntegerSCEV(1, Ty);
00649           SCEVHandle AddOne = SCEVAddExpr::get(InnerMul, One);
00650           SCEVHandle OuterMul = SCEVMulExpr::get(AddOne, Ops[AddOp]);
00651           if (Ops.size() == 2) return OuterMul;
00652           if (AddOp < Idx) {
00653             Ops.erase(Ops.begin()+AddOp);
00654             Ops.erase(Ops.begin()+Idx-1);
00655           } else {
00656             Ops.erase(Ops.begin()+Idx);
00657             Ops.erase(Ops.begin()+AddOp-1);
00658           }
00659           Ops.push_back(OuterMul);
00660           return SCEVAddExpr::get(Ops);
00661         }
00662       
00663       // Check this multiply against other multiplies being added together.
00664       for (unsigned OtherMulIdx = Idx+1;
00665            OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
00666            ++OtherMulIdx) {
00667         SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
00668         // If MulOp occurs in OtherMul, we can fold the two multiplies
00669         // together.
00670         for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
00671              OMulOp != e; ++OMulOp)
00672           if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
00673             // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
00674             SCEVHandle InnerMul1 = Mul->getOperand(MulOp == 0);
00675             if (Mul->getNumOperands() != 2) {
00676               std::vector<SCEVHandle> MulOps(Mul->op_begin(), Mul->op_end());
00677               MulOps.erase(MulOps.begin()+MulOp);
00678               InnerMul1 = SCEVMulExpr::get(MulOps);
00679             }
00680             SCEVHandle InnerMul2 = OtherMul->getOperand(OMulOp == 0);
00681             if (OtherMul->getNumOperands() != 2) {
00682               std::vector<SCEVHandle> MulOps(OtherMul->op_begin(),
00683                                              OtherMul->op_end());
00684               MulOps.erase(MulOps.begin()+OMulOp);
00685               InnerMul2 = SCEVMulExpr::get(MulOps);
00686             }
00687             SCEVHandle InnerMulSum = SCEVAddExpr::get(InnerMul1,InnerMul2);
00688             SCEVHandle OuterMul = SCEVMulExpr::get(MulOpSCEV, InnerMulSum);
00689             if (Ops.size() == 2) return OuterMul;
00690             Ops.erase(Ops.begin()+Idx);
00691             Ops.erase(Ops.begin()+OtherMulIdx-1);
00692             Ops.push_back(OuterMul);
00693             return SCEVAddExpr::get(Ops);
00694           }
00695       }
00696     }
00697   }
00698 
00699   // If there are any add recurrences in the operands list, see if any other
00700   // added values are loop invariant.  If so, we can fold them into the
00701   // recurrence.
00702   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
00703     ++Idx;
00704 
00705   // Scan over all recurrences, trying to fold loop invariants into them.
00706   for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
00707     // Scan all of the other operands to this add and add them to the vector if
00708     // they are loop invariant w.r.t. the recurrence.
00709     std::vector<SCEVHandle> LIOps;
00710     SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
00711     for (unsigned i = 0, e = Ops.size(); i != e; ++i)
00712       if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
00713         LIOps.push_back(Ops[i]);
00714         Ops.erase(Ops.begin()+i);
00715         --i; --e;
00716       }
00717 
00718     // If we found some loop invariants, fold them into the recurrence.
00719     if (!LIOps.empty()) {
00720       //  NLI + LI + { Start,+,Step}  -->  NLI + { LI+Start,+,Step }
00721       LIOps.push_back(AddRec->getStart());
00722 
00723       std::vector<SCEVHandle> AddRecOps(AddRec->op_begin(), AddRec->op_end());
00724       AddRecOps[0] = SCEVAddExpr::get(LIOps);
00725 
00726       SCEVHandle NewRec = SCEVAddRecExpr::get(AddRecOps, AddRec->getLoop());
00727       // If all of the other operands were loop invariant, we are done.
00728       if (Ops.size() == 1) return NewRec;
00729 
00730       // Otherwise, add the folded AddRec by the non-liv parts.
00731       for (unsigned i = 0;; ++i)
00732         if (Ops[i] == AddRec) {
00733           Ops[i] = NewRec;
00734           break;
00735         }
00736       return SCEVAddExpr::get(Ops);
00737     }
00738 
00739     // Okay, if there weren't any loop invariants to be folded, check to see if
00740     // there are multiple AddRec's with the same loop induction variable being
00741     // added together.  If so, we can fold them.
00742     for (unsigned OtherIdx = Idx+1;
00743          OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
00744       if (OtherIdx != Idx) {
00745         SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
00746         if (AddRec->getLoop() == OtherAddRec->getLoop()) {
00747           // Other + {A,+,B} + {C,+,D}  -->  Other + {A+C,+,B+D}
00748           std::vector<SCEVHandle> NewOps(AddRec->op_begin(), AddRec->op_end());
00749           for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) {
00750             if (i >= NewOps.size()) {
00751               NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i,
00752                             OtherAddRec->op_end());
00753               break;
00754             }
00755             NewOps[i] = SCEVAddExpr::get(NewOps[i], OtherAddRec->getOperand(i));
00756           }
00757           SCEVHandle NewAddRec = SCEVAddRecExpr::get(NewOps, AddRec->getLoop());
00758 
00759           if (Ops.size() == 2) return NewAddRec;
00760 
00761           Ops.erase(Ops.begin()+Idx);
00762           Ops.erase(Ops.begin()+OtherIdx-1);
00763           Ops.push_back(NewAddRec);
00764           return SCEVAddExpr::get(Ops);
00765         }
00766       }
00767 
00768     // Otherwise couldn't fold anything into this recurrence.  Move onto the
00769     // next one.
00770   }
00771 
00772   // Okay, it looks like we really DO need an add expr.  Check to see if we
00773   // already have one, otherwise create a new one.
00774   std::vector<SCEV*> SCEVOps(Ops.begin(), Ops.end());
00775   SCEVCommutativeExpr *&Result = SCEVCommExprs[std::make_pair(scAddExpr,
00776                                                               SCEVOps)];
00777   if (Result == 0) Result = new SCEVAddExpr(Ops);
00778   return Result;
00779 }
00780 
00781 
00782 SCEVHandle SCEVMulExpr::get(std::vector<SCEVHandle> &Ops) {
00783   assert(!Ops.empty() && "Cannot get empty mul!");
00784 
00785   // Sort by complexity, this groups all similar expression types together.
00786   GroupByComplexity(Ops);
00787 
00788   // If there are any constants, fold them together.
00789   unsigned Idx = 0;
00790   if (SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
00791 
00792     // C1*(C2+V) -> C1*C2 + C1*V
00793     if (Ops.size() == 2)
00794       if (SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
00795         if (Add->getNumOperands() == 2 &&
00796             isa<SCEVConstant>(Add->getOperand(0)))
00797           return SCEVAddExpr::get(SCEVMulExpr::get(LHSC, Add->getOperand(0)),
00798                                   SCEVMulExpr::get(LHSC, Add->getOperand(1)));
00799 
00800 
00801     ++Idx;
00802     while (SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
00803       // We found two constants, fold them together!
00804       Constant *Fold = ConstantExpr::getMul(LHSC->getValue(), RHSC->getValue());
00805       if (ConstantInt *CI = dyn_cast<ConstantInt>(Fold)) {
00806         Ops[0] = SCEVConstant::get(CI);
00807         Ops.erase(Ops.begin()+1);  // Erase the folded element
00808         if (Ops.size() == 1) return Ops[0];
00809       } else {
00810         // If we couldn't fold the expression, move to the next constant.  Note
00811         // that this is impossible to happen in practice because we always
00812         // constant fold constant ints to constant ints.
00813         ++Idx;
00814       }
00815     }
00816 
00817     // If we are left with a constant one being multiplied, strip it off.
00818     if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) {
00819       Ops.erase(Ops.begin());
00820       --Idx;
00821     } else if (cast<SCEVConstant>(Ops[0])->getValue()->isNullValue()) {
00822       // If we have a multiply of zero, it will always be zero.
00823       return Ops[0];
00824     }
00825   }
00826 
00827   // Skip over the add expression until we get to a multiply.
00828   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
00829     ++Idx;
00830 
00831   if (Ops.size() == 1)
00832     return Ops[0];
00833   
00834   // If there are mul operands inline them all into this expression.
00835   if (Idx < Ops.size()) {
00836     bool DeletedMul = false;
00837     while (SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
00838       // If we have an mul, expand the mul operands onto the end of the operands
00839       // list.
00840       Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end());
00841       Ops.erase(Ops.begin()+Idx);
00842       DeletedMul = true;
00843     }
00844 
00845     // If we deleted at least one mul, we added operands to the end of the list,
00846     // and they are not necessarily sorted.  Recurse to resort and resimplify
00847     // any operands we just aquired.
00848     if (DeletedMul)
00849       return get(Ops);
00850   }
00851 
00852   // If there are any add recurrences in the operands list, see if any other
00853   // added values are loop invariant.  If so, we can fold them into the
00854   // recurrence.
00855   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
00856     ++Idx;
00857 
00858   // Scan over all recurrences, trying to fold loop invariants into them.
00859   for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
00860     // Scan all of the other operands to this mul and add them to the vector if
00861     // they are loop invariant w.r.t. the recurrence.
00862     std::vector<SCEVHandle> LIOps;
00863     SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
00864     for (unsigned i = 0, e = Ops.size(); i != e; ++i)
00865       if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
00866         LIOps.push_back(Ops[i]);
00867         Ops.erase(Ops.begin()+i);
00868         --i; --e;
00869       }
00870 
00871     // If we found some loop invariants, fold them into the recurrence.
00872     if (!LIOps.empty()) {
00873       //  NLI * LI * { Start,+,Step}  -->  NLI * { LI*Start,+,LI*Step }
00874       std::vector<SCEVHandle> NewOps;
00875       NewOps.reserve(AddRec->getNumOperands());
00876       if (LIOps.size() == 1) {
00877         SCEV *Scale = LIOps[0];
00878         for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
00879           NewOps.push_back(SCEVMulExpr::get(Scale, AddRec->getOperand(i)));
00880       } else {
00881         for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
00882           std::vector<SCEVHandle> MulOps(LIOps);
00883           MulOps.push_back(AddRec->getOperand(i));
00884           NewOps.push_back(SCEVMulExpr::get(MulOps));
00885         }
00886       }
00887 
00888       SCEVHandle NewRec = SCEVAddRecExpr::get(NewOps, AddRec->getLoop());
00889 
00890       // If all of the other operands were loop invariant, we are done.
00891       if (Ops.size() == 1) return NewRec;
00892 
00893       // Otherwise, multiply the folded AddRec by the non-liv parts.
00894       for (unsigned i = 0;; ++i)
00895         if (Ops[i] == AddRec) {
00896           Ops[i] = NewRec;
00897           break;
00898         }
00899       return SCEVMulExpr::get(Ops);
00900     }
00901 
00902     // Okay, if there weren't any loop invariants to be folded, check to see if
00903     // there are multiple AddRec's with the same loop induction variable being
00904     // multiplied together.  If so, we can fold them.
00905     for (unsigned OtherIdx = Idx+1;
00906          OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
00907       if (OtherIdx != Idx) {
00908         SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
00909         if (AddRec->getLoop() == OtherAddRec->getLoop()) {
00910           // F * G  -->  {A,+,B} * {C,+,D}  -->  {A*C,+,F*D + G*B + B*D}
00911           SCEVAddRecExpr *F = AddRec, *G = OtherAddRec;
00912           SCEVHandle NewStart = SCEVMulExpr::get(F->getStart(),
00913                                                  G->getStart());
00914           SCEVHandle B = F->getStepRecurrence();
00915           SCEVHandle D = G->getStepRecurrence();
00916           SCEVHandle NewStep = SCEVAddExpr::get(SCEVMulExpr::get(F, D),
00917                                                 SCEVMulExpr::get(G, B),
00918                                                 SCEVMulExpr::get(B, D));
00919           SCEVHandle NewAddRec = SCEVAddRecExpr::get(NewStart, NewStep,
00920                                                      F->getLoop());
00921           if (Ops.size() == 2) return NewAddRec;
00922 
00923           Ops.erase(Ops.begin()+Idx);
00924           Ops.erase(Ops.begin()+OtherIdx-1);
00925           Ops.push_back(NewAddRec);
00926           return SCEVMulExpr::get(Ops);
00927         }
00928       }
00929 
00930     // Otherwise couldn't fold anything into this recurrence.  Move onto the
00931     // next one.
00932   }
00933 
00934   // Okay, it looks like we really DO need an mul expr.  Check to see if we
00935   // already have one, otherwise create a new one.
00936   std::vector<SCEV*> SCEVOps(Ops.begin(), Ops.end());
00937   SCEVCommutativeExpr *&Result = SCEVCommExprs[std::make_pair(scMulExpr,
00938                                                               SCEVOps)];
00939   if (Result == 0)
00940     Result = new SCEVMulExpr(Ops);
00941   return Result;
00942 }
00943 
00944 SCEVHandle SCEVUDivExpr::get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
00945   if (SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
00946     if (RHSC->getValue()->equalsInt(1))
00947       return LHS;                            // X /u 1 --> x
00948     if (RHSC->getValue()->isAllOnesValue())
00949       return getNegativeSCEV(LHS);           // X /u -1  -->  -x
00950 
00951     if (SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
00952       Constant *LHSCV = LHSC->getValue();
00953       Constant *RHSCV = RHSC->getValue();
00954       if (LHSCV->getType()->isSigned())
00955         LHSCV = ConstantExpr::getCast(LHSCV,
00956                                       LHSCV->getType()->getUnsignedVersion());
00957       if (RHSCV->getType()->isSigned())
00958         RHSCV = ConstantExpr::getCast(RHSCV, LHSCV->getType());
00959       return SCEVUnknown::get(ConstantExpr::getDiv(LHSCV, RHSCV));
00960     }
00961   }
00962 
00963   // FIXME: implement folding of (X*4)/4 when we know X*4 doesn't overflow.
00964 
00965   SCEVUDivExpr *&Result = SCEVUDivs[std::make_pair(LHS, RHS)];
00966   if (Result == 0) Result = new SCEVUDivExpr(LHS, RHS);
00967   return Result;
00968 }
00969 
00970 
00971 /// SCEVAddRecExpr::get - Get a add recurrence expression for the
00972 /// specified loop.  Simplify the expression as much as possible.
00973 SCEVHandle SCEVAddRecExpr::get(const SCEVHandle &Start,
00974                                const SCEVHandle &Step, const Loop *L) {
00975   std::vector<SCEVHandle> Operands;
00976   Operands.push_back(Start);
00977   if (SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
00978     if (StepChrec->getLoop() == L) {
00979       Operands.insert(Operands.end(), StepChrec->op_begin(),
00980                       StepChrec->op_end());
00981       return get(Operands, L);
00982     }
00983 
00984   Operands.push_back(Step);
00985   return get(Operands, L);
00986 }
00987 
00988 /// SCEVAddRecExpr::get - Get a add recurrence expression for the
00989 /// specified loop.  Simplify the expression as much as possible.
00990 SCEVHandle SCEVAddRecExpr::get(std::vector<SCEVHandle> &Operands,
00991                                const Loop *L) {
00992   if (Operands.size() == 1) return Operands[0];
00993 
00994   if (SCEVConstant *StepC = dyn_cast<SCEVConstant>(Operands.back()))
00995     if (StepC->getValue()->isNullValue()) {
00996       Operands.pop_back();
00997       return get(Operands, L);             // { X,+,0 }  -->  X
00998     }
00999 
01000   SCEVAddRecExpr *&Result =
01001     SCEVAddRecExprs[std::make_pair(L, std::vector<SCEV*>(Operands.begin(),
01002                                                          Operands.end()))];
01003   if (Result == 0) Result = new SCEVAddRecExpr(Operands, L);
01004   return Result;
01005 }
01006 
01007 SCEVHandle SCEVUnknown::get(Value *V) {
01008   if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
01009     return SCEVConstant::get(CI);
01010   SCEVUnknown *&Result = SCEVUnknowns[V];
01011   if (Result == 0) Result = new SCEVUnknown(V);
01012   return Result;
01013 }
01014 
01015 
01016 //===----------------------------------------------------------------------===//
01017 //             ScalarEvolutionsImpl Definition and Implementation
01018 //===----------------------------------------------------------------------===//
01019 //
01020 /// ScalarEvolutionsImpl - This class implements the main driver for the scalar
01021 /// evolution code.
01022 ///
01023 namespace {
01024   struct ScalarEvolutionsImpl {
01025     /// F - The function we are analyzing.
01026     ///
01027     Function &F;
01028 
01029     /// LI - The loop information for the function we are currently analyzing.
01030     ///
01031     LoopInfo &LI;
01032 
01033     /// UnknownValue - This SCEV is used to represent unknown trip counts and
01034     /// things.
01035     SCEVHandle UnknownValue;
01036 
01037     /// Scalars - This is a cache of the scalars we have analyzed so far.
01038     ///
01039     std::map<Value*, SCEVHandle> Scalars;
01040 
01041     /// IterationCounts - Cache the iteration count of the loops for this
01042     /// function as they are computed.
01043     std::map<const Loop*, SCEVHandle> IterationCounts;
01044 
01045     /// ConstantEvolutionLoopExitValue - This map contains entries for all of
01046     /// the PHI instructions that we attempt to compute constant evolutions for.
01047     /// This allows us to avoid potentially expensive recomputation of these
01048     /// properties.  An instruction maps to null if we are unable to compute its
01049     /// exit value.
01050     std::map<PHINode*, Constant*> ConstantEvolutionLoopExitValue;
01051     
01052   public:
01053     ScalarEvolutionsImpl(Function &f, LoopInfo &li)
01054       : F(f), LI(li), UnknownValue(new SCEVCouldNotCompute()) {}
01055 
01056     /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the
01057     /// expression and create a new one.
01058     SCEVHandle getSCEV(Value *V);
01059 
01060     /// getSCEVAtScope - Compute the value of the specified expression within
01061     /// the indicated loop (which may be null to indicate in no loop).  If the
01062     /// expression cannot be evaluated, return UnknownValue itself.
01063     SCEVHandle getSCEVAtScope(SCEV *V, const Loop *L);
01064 
01065 
01066     /// hasLoopInvariantIterationCount - Return true if the specified loop has
01067     /// an analyzable loop-invariant iteration count.
01068     bool hasLoopInvariantIterationCount(const Loop *L);
01069 
01070     /// getIterationCount - If the specified loop has a predictable iteration
01071     /// count, return it.  Note that it is not valid to call this method on a
01072     /// loop without a loop-invariant iteration count.
01073     SCEVHandle getIterationCount(const Loop *L);
01074 
01075     /// deleteInstructionFromRecords - This method should be called by the
01076     /// client before it removes an instruction from the program, to make sure
01077     /// that no dangling references are left around.
01078     void deleteInstructionFromRecords(Instruction *I);
01079 
01080   private:
01081     /// createSCEV - We know that there is no SCEV for the specified value.
01082     /// Analyze the expression.
01083     SCEVHandle createSCEV(Value *V);
01084     SCEVHandle createNodeForCast(CastInst *CI);
01085 
01086     /// createNodeForPHI - Provide the special handling we need to analyze PHI
01087     /// SCEVs.
01088     SCEVHandle createNodeForPHI(PHINode *PN);
01089     void UpdatePHIUserScalarEntries(Instruction *I, PHINode *PN,
01090                                     std::set<Instruction*> &UpdatedInsts);
01091 
01092     /// ComputeIterationCount - Compute the number of times the specified loop
01093     /// will iterate.
01094     SCEVHandle ComputeIterationCount(const Loop *L);
01095 
01096     /// ComputeLoadConstantCompareIterationCount - Given an exit condition of
01097     /// 'setcc load X, cst', try to se if we can compute the trip count.
01098     SCEVHandle ComputeLoadConstantCompareIterationCount(LoadInst *LI,
01099                                                         Constant *RHS,
01100                                                         const Loop *L,
01101                                                         unsigned SetCCOpcode);
01102 
01103     /// ComputeIterationCountExhaustively - If the trip is known to execute a
01104     /// constant number of times (the condition evolves only from constants),
01105     /// try to evaluate a few iterations of the loop until we get the exit
01106     /// condition gets a value of ExitWhen (true or false).  If we cannot
01107     /// evaluate the trip count of the loop, return UnknownValue.
01108     SCEVHandle ComputeIterationCountExhaustively(const Loop *L, Value *Cond,
01109                                                  bool ExitWhen);
01110 
01111     /// HowFarToZero - Return the number of times a backedge comparing the
01112     /// specified value to zero will execute.  If not computable, return
01113     /// UnknownValue
01114     SCEVHandle HowFarToZero(SCEV *V, const Loop *L);
01115 
01116     /// HowFarToNonZero - Return the number of times a backedge checking the
01117     /// specified value for nonzero will execute.  If not computable, return
01118     /// UnknownValue
01119     SCEVHandle HowFarToNonZero(SCEV *V, const Loop *L);
01120 
01121     /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
01122     /// in the header of its containing loop, we know the loop executes a
01123     /// constant number of times, and the PHI node is just a recurrence
01124     /// involving constants, fold it.
01125     Constant *getConstantEvolutionLoopExitValue(PHINode *PN, uint64_t Its,
01126                                                 const Loop *L);
01127   };
01128 }
01129 
01130 //===----------------------------------------------------------------------===//
01131 //            Basic SCEV Analysis and PHI Idiom Recognition Code
01132 //
01133 
01134 /// deleteInstructionFromRecords - This method should be called by the
01135 /// client before it removes an instruction from the program, to make sure
01136 /// that no dangling references are left around.
01137 void ScalarEvolutionsImpl::deleteInstructionFromRecords(Instruction *I) {
01138   Scalars.erase(I);
01139   if (PHINode *PN = dyn_cast<PHINode>(I))
01140     ConstantEvolutionLoopExitValue.erase(PN);
01141 }
01142 
01143 
01144 /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the
01145 /// expression and create a new one.
01146 SCEVHandle ScalarEvolutionsImpl::getSCEV(Value *V) {
01147   assert(V->getType() != Type::VoidTy && "Can't analyze void expressions!");
01148 
01149   std::map<Value*, SCEVHandle>::iterator I = Scalars.find(V);
01150   if (I != Scalars.end()) return I->second;
01151   SCEVHandle S = createSCEV(V);
01152   Scalars.insert(std::make_pair(V, S));
01153   return S;
01154 }
01155 
01156 
01157 /// UpdatePHIUserScalarEntries - After PHI node analysis, we have a bunch of
01158 /// entries in the scalar map that refer to the "symbolic" PHI value instead of
01159 /// the recurrence value.  After we resolve the PHI we must loop over all of the
01160 /// using instructions that have scalar map entries and update them.
01161 void ScalarEvolutionsImpl::UpdatePHIUserScalarEntries(Instruction *I,
01162                                                       PHINode *PN,
01163                                         std::set<Instruction*> &UpdatedInsts) {
01164   std::map<Value*, SCEVHandle>::iterator SI = Scalars.find(I);
01165   if (SI == Scalars.end()) return;   // This scalar wasn't previous processed.
01166   if (UpdatedInsts.insert(I).second) {
01167     Scalars.erase(SI);                 // Remove the old entry
01168     getSCEV(I);                        // Calculate the new entry
01169     
01170     for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
01171          UI != E; ++UI)
01172       UpdatePHIUserScalarEntries(cast<Instruction>(*UI), PN, UpdatedInsts);
01173   }
01174 }
01175 
01176 
01177 /// createNodeForPHI - PHI nodes have two cases.  Either the PHI node exists in
01178 /// a loop header, making it a potential recurrence, or it doesn't.
01179 ///
01180 SCEVHandle ScalarEvolutionsImpl::createNodeForPHI(PHINode *PN) {
01181   if (PN->getNumIncomingValues() == 2)  // The loops have been canonicalized.
01182     if (const Loop *L = LI.getLoopFor(PN->getParent()))
01183       if (L->getHeader() == PN->getParent()) {
01184         // If it lives in the loop header, it has two incoming values, one
01185         // from outside the loop, and one from inside.
01186         unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0));
01187         unsigned BackEdge     = IncomingEdge^1;
01188         
01189         // While we are analyzing this PHI node, handle its value symbolically.
01190         SCEVHandle SymbolicName = SCEVUnknown::get(PN);
01191         assert(Scalars.find(PN) == Scalars.end() &&
01192                "PHI node already processed?");
01193         Scalars.insert(std::make_pair(PN, SymbolicName));
01194 
01195         // Using this symbolic name for the PHI, analyze the value coming around
01196         // the back-edge.
01197         SCEVHandle BEValue = getSCEV(PN->getIncomingValue(BackEdge));
01198 
01199         // NOTE: If BEValue is loop invariant, we know that the PHI node just
01200         // has a special value for the first iteration of the loop.
01201 
01202         // If the value coming around the backedge is an add with the symbolic
01203         // value we just inserted, then we found a simple induction variable!
01204         if (SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
01205           // If there is a single occurrence of the symbolic value, replace it
01206           // with a recurrence.
01207           unsigned FoundIndex = Add->getNumOperands();
01208           for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
01209             if (Add->getOperand(i) == SymbolicName)
01210               if (FoundIndex == e) {
01211                 FoundIndex = i;
01212                 break;
01213               }
01214 
01215           if (FoundIndex != Add->getNumOperands()) {
01216             // Create an add with everything but the specified operand.
01217             std::vector<SCEVHandle> Ops;
01218             for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
01219               if (i != FoundIndex)
01220                 Ops.push_back(Add->getOperand(i));
01221             SCEVHandle Accum = SCEVAddExpr::get(Ops);
01222 
01223             // This is not a valid addrec if the step amount is varying each
01224             // loop iteration, but is not itself an addrec in this loop.
01225             if (Accum->isLoopInvariant(L) ||
01226                 (isa<SCEVAddRecExpr>(Accum) &&
01227                  cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
01228               SCEVHandle StartVal = getSCEV(PN->getIncomingValue(IncomingEdge));
01229               SCEVHandle PHISCEV  = SCEVAddRecExpr::get(StartVal, Accum, L);
01230 
01231               // Okay, for the entire analysis of this edge we assumed the PHI
01232               // to be symbolic.  We now need to go back and update all of the
01233               // entries for the scalars that use the PHI (except for the PHI
01234               // itself) to use the new analyzed value instead of the "symbolic"
01235               // value.
01236               Scalars.find(PN)->second = PHISCEV;       // Update the PHI value
01237               std::set<Instruction*> UpdatedInsts;
01238               UpdatedInsts.insert(PN);
01239               for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end();
01240                    UI != E; ++UI)
01241                 UpdatePHIUserScalarEntries(cast<Instruction>(*UI), PN,
01242                                            UpdatedInsts);
01243               return PHISCEV;
01244             }
01245           }
01246         }
01247 
01248         return SymbolicName;
01249       }
01250   
01251   // If it's not a loop phi, we can't handle it yet.
01252   return SCEVUnknown::get(PN);
01253 }
01254 
01255 /// createNodeForCast - Handle the various forms of casts that we support.
01256 ///
01257 SCEVHandle ScalarEvolutionsImpl::createNodeForCast(CastInst *CI) {
01258   const Type *SrcTy = CI->getOperand(0)->getType();
01259   const Type *DestTy = CI->getType();
01260   
01261   // If this is a noop cast (ie, conversion from int to uint), ignore it.
01262   if (SrcTy->isLosslesslyConvertibleTo(DestTy))
01263     return getSCEV(CI->getOperand(0));
01264   
01265   if (SrcTy->isInteger() && DestTy->isInteger()) {
01266     // Otherwise, if this is a truncating integer cast, we can represent this
01267     // cast.
01268     if (SrcTy->getPrimitiveSize() > DestTy->getPrimitiveSize())
01269       return SCEVTruncateExpr::get(getSCEV(CI->getOperand(0)),
01270                                    CI->getType()->getUnsignedVersion());
01271     if (SrcTy->isUnsigned() &&
01272         SrcTy->getPrimitiveSize() > DestTy->getPrimitiveSize())
01273       return SCEVZeroExtendExpr::get(getSCEV(CI->getOperand(0)),
01274                                      CI->getType()->getUnsignedVersion());
01275   }
01276 
01277   // If this is an sign or zero extending cast and we can prove that the value
01278   // will never overflow, we could do similar transformations.
01279 
01280   // Otherwise, we can't handle this cast!
01281   return SCEVUnknown::get(CI);
01282 }
01283 
01284 
01285 /// createSCEV - We know that there is no SCEV for the specified value.
01286 /// Analyze the expression.
01287 ///
01288 SCEVHandle ScalarEvolutionsImpl::createSCEV(Value *V) {
01289   if (Instruction *I = dyn_cast<Instruction>(V)) {
01290     switch (I->getOpcode()) {
01291     case Instruction::Add:
01292       return SCEVAddExpr::get(getSCEV(I->getOperand(0)),
01293                               getSCEV(I->getOperand(1)));
01294     case Instruction::Mul:
01295       return SCEVMulExpr::get(getSCEV(I->getOperand(0)),
01296                               getSCEV(I->getOperand(1)));
01297     case Instruction::Div:
01298       if (V->getType()->isInteger() && V->getType()->isUnsigned())
01299         return SCEVUDivExpr::get(getSCEV(I->getOperand(0)),
01300                                  getSCEV(I->getOperand(1)));
01301       break;
01302 
01303     case Instruction::Sub:
01304       return getMinusSCEV(getSCEV(I->getOperand(0)), getSCEV(I->getOperand(1)));
01305 
01306     case Instruction::Shl:
01307       // Turn shift left of a constant amount into a multiply.
01308       if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
01309         Constant *X = ConstantInt::get(V->getType(), 1);
01310         X = ConstantExpr::getShl(X, SA);
01311         return SCEVMulExpr::get(getSCEV(I->getOperand(0)), getSCEV(X));
01312       }
01313       break;
01314 
01315     case Instruction::Shr:
01316       if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1)))
01317         if (V->getType()->isUnsigned()) {
01318           Constant *X = ConstantInt::get(V->getType(), 1);
01319           X = ConstantExpr::getShl(X, SA);
01320           return SCEVUDivExpr::get(getSCEV(I->getOperand(0)), getSCEV(X));
01321         }
01322       break;
01323 
01324     case Instruction::Cast:
01325       return createNodeForCast(cast<CastInst>(I));
01326 
01327     case Instruction::PHI:
01328       return createNodeForPHI(cast<PHINode>(I));
01329 
01330     default: // We cannot analyze this expression.
01331       break;
01332     }
01333   }
01334 
01335   return SCEVUnknown::get(V);
01336 }
01337 
01338 
01339 
01340 //===----------------------------------------------------------------------===//
01341 //                   Iteration Count Computation Code
01342 //
01343 
01344 /// getIterationCount - If the specified loop has a predictable iteration
01345 /// count, return it.  Note that it is not valid to call this method on a
01346 /// loop without a loop-invariant iteration count.
01347 SCEVHandle ScalarEvolutionsImpl::getIterationCount(const Loop *L) {
01348   std::map<const Loop*, SCEVHandle>::iterator I = IterationCounts.find(L);
01349   if (I == IterationCounts.end()) {
01350     SCEVHandle ItCount = ComputeIterationCount(L);
01351     I = IterationCounts.insert(std::make_pair(L, ItCount)).first;
01352     if (ItCount != UnknownValue) {
01353       assert(ItCount->isLoopInvariant(L) &&
01354              "Computed trip count isn't loop invariant for loop!");
01355       ++NumTripCountsComputed;
01356     } else if (isa<PHINode>(L->getHeader()->begin())) {
01357       // Only count loops that have phi nodes as not being computable.
01358       ++NumTripCountsNotComputed;
01359     }
01360   }
01361   return I->second;
01362 }
01363 
01364 /// ComputeIterationCount - Compute the number of times the specified loop
01365 /// will iterate.
01366 SCEVHandle ScalarEvolutionsImpl::ComputeIterationCount(const Loop *L) {
01367   // If the loop has a non-one exit block count, we can't analyze it.
01368   std::vector<BasicBlock*> ExitBlocks;
01369   L->getExitBlocks(ExitBlocks);
01370   if (ExitBlocks.size() != 1) return UnknownValue;
01371 
01372   // Okay, there is one exit block.  Try to find the condition that causes the
01373   // loop to be exited.
01374   BasicBlock *ExitBlock = ExitBlocks[0];
01375 
01376   BasicBlock *ExitingBlock = 0;
01377   for (pred_iterator PI = pred_begin(ExitBlock), E = pred_end(ExitBlock);
01378        PI != E; ++PI)
01379     if (L->contains(*PI)) {
01380       if (ExitingBlock == 0)
01381         ExitingBlock = *PI;
01382       else
01383         return UnknownValue;   // More than one block exiting!
01384     }
01385   assert(ExitingBlock && "No exits from loop, something is broken!");
01386 
01387   // Okay, we've computed the exiting block.  See what condition causes us to
01388   // exit.
01389   //
01390   // FIXME: we should be able to handle switch instructions (with a single exit)
01391   // FIXME: We should handle cast of int to bool as well
01392   BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
01393   if (ExitBr == 0) return UnknownValue;
01394   assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!");
01395   SetCondInst *ExitCond = dyn_cast<SetCondInst>(ExitBr->getCondition());
01396   if (ExitCond == 0)  // Not a setcc
01397     return ComputeIterationCountExhaustively(L, ExitBr->getCondition(),
01398                                           ExitBr->getSuccessor(0) == ExitBlock);
01399 
01400   // If the condition was exit on true, convert the condition to exit on false.
01401   Instruction::BinaryOps Cond;
01402   if (ExitBr->getSuccessor(1) == ExitBlock)
01403     Cond = ExitCond->getOpcode();
01404   else
01405     Cond = ExitCond->getInverseCondition();
01406 
01407   // Handle common loops like: for (X = "string"; *X; ++X)
01408   if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0)))
01409     if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) {
01410       SCEVHandle ItCnt =
01411         ComputeLoadConstantCompareIterationCount(LI, RHS, L, Cond);
01412       if (!isa<SCEVCouldNotCompute>(ItCnt)) return ItCnt;
01413     }
01414 
01415   SCEVHandle LHS = getSCEV(ExitCond->getOperand(0));
01416   SCEVHandle RHS = getSCEV(ExitCond->getOperand(1));
01417 
01418   // Try to evaluate any dependencies out of the loop.
01419   SCEVHandle Tmp = getSCEVAtScope(LHS, L);
01420   if (!isa<SCEVCouldNotCompute>(Tmp)) LHS = Tmp;
01421   Tmp = getSCEVAtScope(RHS, L);
01422   if (!isa<SCEVCouldNotCompute>(Tmp)) RHS = Tmp;
01423 
01424   // At this point, we would like to compute how many iterations of the loop the
01425   // predicate will return true for these inputs.
01426   if (isa<SCEVConstant>(LHS) && !isa<SCEVConstant>(RHS)) {
01427     // If there is a constant, force it into the RHS.
01428     std::swap(LHS, RHS);
01429     Cond = SetCondInst::getSwappedCondition(Cond);
01430   }
01431 
01432   // FIXME: think about handling pointer comparisons!  i.e.:
01433   // while (P != P+100) ++P;
01434 
01435   // If we have a comparison of a chrec against a constant, try to use value
01436   // ranges to answer this query.
01437   if (SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
01438     if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
01439       if (AddRec->getLoop() == L) {
01440         // Form the comparison range using the constant of the correct type so
01441         // that the ConstantRange class knows to do a signed or unsigned
01442         // comparison.
01443         ConstantInt *CompVal = RHSC->getValue();
01444         const Type *RealTy = ExitCond->getOperand(0)->getType();
01445         CompVal = dyn_cast<ConstantInt>(ConstantExpr::getCast(CompVal, RealTy));
01446         if (CompVal) {
01447           // Form the constant range.
01448           ConstantRange CompRange(Cond, CompVal);
01449           
01450           // Now that we have it, if it's signed, convert it to an unsigned
01451           // range.
01452           if (CompRange.getLower()->getType()->isSigned()) {
01453             const Type *NewTy = RHSC->getValue()->getType();
01454             Constant *NewL = ConstantExpr::getCast(CompRange.getLower(), NewTy);
01455             Constant *NewU = ConstantExpr::getCast(CompRange.getUpper(), NewTy);
01456             CompRange = ConstantRange(NewL, NewU);
01457           }
01458           
01459           SCEVHandle Ret = AddRec->getNumIterationsInRange(CompRange);
01460           if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
01461         }
01462       }
01463   
01464   switch (Cond) {
01465   case Instruction::SetNE:                     // while (X != Y)
01466     // Convert to: while (X-Y != 0)
01467     if (LHS->getType()->isInteger()) {
01468       SCEVHandle TC = HowFarToZero(getMinusSCEV(LHS, RHS), L);
01469       if (!isa<SCEVCouldNotCompute>(TC)) return TC;
01470     }
01471     break;
01472   case Instruction::SetEQ:
01473     // Convert to: while (X-Y == 0)           // while (X == Y)
01474     if (LHS->getType()->isInteger()) {
01475       SCEVHandle TC = HowFarToNonZero(getMinusSCEV(LHS, RHS), L);
01476       if (!isa<SCEVCouldNotCompute>(TC)) return TC;
01477     }
01478     break;
01479   default:
01480 #if 0
01481     std::cerr << "ComputeIterationCount ";
01482     if (ExitCond->getOperand(0)->getType()->isUnsigned())
01483       std::cerr << "[unsigned] ";
01484     std::cerr << *LHS << "   "
01485               << Instruction::getOpcodeName(Cond) << "   " << *RHS << "\n";
01486 #endif
01487     break;
01488   }
01489 
01490   return ComputeIterationCountExhaustively(L, ExitCond,
01491                                          ExitBr->getSuccessor(0) == ExitBlock);
01492 }
01493 
01494 static ConstantInt *
01495 EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, Constant *C) {
01496   SCEVHandle InVal = SCEVConstant::get(cast<ConstantInt>(C));
01497   SCEVHandle Val = AddRec->evaluateAtIteration(InVal);
01498   assert(isa<SCEVConstant>(Val) &&
01499          "Evaluation of SCEV at constant didn't fold correctly?");
01500   return cast<SCEVConstant>(Val)->getValue();
01501 }
01502 
01503 /// GetAddressedElementFromGlobal - Given a global variable with an initializer
01504 /// and a GEP expression (missing the pointer index) indexing into it, return
01505 /// the addressed element of the initializer or null if the index expression is
01506 /// invalid.
01507 static Constant *
01508 GetAddressedElementFromGlobal(GlobalVariable *GV, 
01509                               const std::vector<ConstantInt*> &Indices) {
01510   Constant *Init = GV->getInitializer();
01511   for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
01512     uint64_t Idx = Indices[i]->getRawValue();
01513     if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) {
01514       assert(Idx < CS->getNumOperands() && "Bad struct index!");
01515       Init = cast<Constant>(CS->getOperand(Idx));
01516     } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) {
01517       if (Idx >= CA->getNumOperands()) return 0;  // Bogus program
01518       Init = cast<Constant>(CA->getOperand(Idx));
01519     } else if (isa<ConstantAggregateZero>(Init)) {
01520       if (const StructType *STy = dyn_cast<StructType>(Init->getType())) {
01521         assert(Idx < STy->getNumElements() && "Bad struct index!");
01522         Init = Constant::getNullValue(STy->getElementType(Idx));
01523       } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) {
01524         if (Idx >= ATy->getNumElements()) return 0;  // Bogus program
01525         Init = Constant::getNullValue(ATy->getElementType());
01526       } else {
01527         assert(0 && "Unknown constant aggregate type!");
01528       }
01529       return 0;
01530     } else {
01531       return 0; // Unknown initializer type
01532     }
01533   }
01534   return Init;
01535 }
01536 
01537 /// ComputeLoadConstantCompareIterationCount - Given an exit condition of
01538 /// 'setcc load X, cst', try to se if we can compute the trip count.
01539 SCEVHandle ScalarEvolutionsImpl::
01540 ComputeLoadConstantCompareIterationCount(LoadInst *LI, Constant *RHS, 
01541                                          const Loop *L, unsigned SetCCOpcode) {
01542   if (LI->isVolatile()) return UnknownValue;
01543 
01544   // Check to see if the loaded pointer is a getelementptr of a global.
01545   GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0));
01546   if (!GEP) return UnknownValue;
01547 
01548   // Make sure that it is really a constant global we are gepping, with an
01549   // initializer, and make sure the first IDX is really 0.
01550   GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
01551   if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
01552       GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) ||
01553       !cast<Constant>(GEP->getOperand(1))->isNullValue())
01554     return UnknownValue;
01555 
01556   // Okay, we allow one non-constant index into the GEP instruction.
01557   Value *VarIdx = 0;
01558   std::vector<ConstantInt*> Indexes;
01559   unsigned VarIdxNum = 0;
01560   for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i)
01561     if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
01562       Indexes.push_back(CI);
01563     } else if (!isa<ConstantInt>(GEP->getOperand(i))) {
01564       if (VarIdx) return UnknownValue;  // Multiple non-constant idx's.
01565       VarIdx = GEP->getOperand(i);
01566       VarIdxNum = i-2;
01567       Indexes.push_back(0);
01568     }
01569 
01570   // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant.
01571   // Check to see if X is a loop variant variable value now.
01572   SCEVHandle Idx = getSCEV(VarIdx);
01573   SCEVHandle Tmp = getSCEVAtScope(Idx, L);
01574   if (!isa<SCEVCouldNotCompute>(Tmp)) Idx = Tmp;
01575 
01576   // We can only recognize very limited forms of loop index expressions, in
01577   // particular, only affine AddRec's like {C1,+,C2}.
01578   SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx);
01579   if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) ||
01580       !isa<SCEVConstant>(IdxExpr->getOperand(0)) ||
01581       !isa<SCEVConstant>(IdxExpr->getOperand(1)))
01582     return UnknownValue;
01583 
01584   unsigned MaxSteps = MaxBruteForceIterations;
01585   for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
01586     ConstantUInt *ItCst =
01587       ConstantUInt::get(IdxExpr->getType()->getUnsignedVersion(), IterationNum);
01588     ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst);
01589 
01590     // Form the GEP offset.
01591     Indexes[VarIdxNum] = Val;
01592 
01593     Constant *Result = GetAddressedElementFromGlobal(GV, Indexes);
01594     if (Result == 0) break;  // Cannot compute!
01595 
01596     // Evaluate the condition for this iteration.
01597     Result = ConstantExpr::get(SetCCOpcode, Result, RHS);
01598     if (!isa<ConstantBool>(Result)) break;  // Couldn't decide for sure
01599     if (Result == ConstantBool::False) {
01600 #if 0
01601       std::cerr << "\n***\n*** Computed loop count " << *ItCst
01602                 << "\n*** From global " << *GV << "*** BB: " << *L->getHeader()
01603                 << "***\n";
01604 #endif
01605       ++NumArrayLenItCounts;
01606       return SCEVConstant::get(ItCst);   // Found terminating iteration!
01607     }
01608   }
01609   return UnknownValue;
01610 }
01611 
01612 
01613 /// CanConstantFold - Return true if we can constant fold an instruction of the
01614 /// specified type, assuming that all operands were constants.
01615 static bool CanConstantFold(const Instruction *I) {
01616   if (isa<BinaryOperator>(I) || isa<ShiftInst>(I) ||
01617       isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I))
01618     return true;
01619   
01620   if (const CallInst *CI = dyn_cast<CallInst>(I))
01621     if (const Function *F = CI->getCalledFunction())
01622       return canConstantFoldCallTo((Function*)F);  // FIXME: elim cast
01623   return false;
01624 }
01625 
01626 /// ConstantFold - Constant fold an instruction of the specified type with the
01627 /// specified constant operands.  This function may modify the operands vector.
01628 static Constant *ConstantFold(const Instruction *I,
01629                               std::vector<Constant*> &Operands) {
01630   if (isa<BinaryOperator>(I) || isa<ShiftInst>(I))
01631     return ConstantExpr::get(I->getOpcode(), Operands[0], Operands[1]);
01632 
01633   switch (I->getOpcode()) {
01634   case Instruction::Cast:
01635     return ConstantExpr::getCast(Operands[0], I->getType());
01636   case Instruction::Select:
01637     return ConstantExpr::getSelect(Operands[0], Operands[1], Operands[2]);
01638   case Instruction::Call:
01639     if (Function *GV = dyn_cast<Function>(Operands[0])) {
01640       Operands.erase(Operands.begin());
01641       return ConstantFoldCall(cast<Function>(GV), Operands);
01642     }
01643 
01644     return 0;
01645   case Instruction::GetElementPtr:
01646     Constant *Base = Operands[0];
01647     Operands.erase(Operands.begin());
01648     return ConstantExpr::getGetElementPtr(Base, Operands);
01649   }
01650   return 0;
01651 }
01652 
01653 
01654 /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node
01655 /// in the loop that V is derived from.  We allow arbitrary operations along the
01656 /// way, but the operands of an operation must either be constants or a value
01657 /// derived from a constant PHI.  If this expression does not fit with these
01658 /// constraints, return null.
01659 static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) {
01660   // If this is not an instruction, or if this is an instruction outside of the
01661   // loop, it can't be derived from a loop PHI.
01662   Instruction *I = dyn_cast<Instruction>(V);
01663   if (I == 0 || !L->contains(I->getParent())) return 0;
01664 
01665   if (PHINode *PN = dyn_cast<PHINode>(I))
01666     if (L->getHeader() == I->getParent())
01667       return PN;
01668     else
01669       // We don't currently keep track of the control flow needed to evaluate
01670       // PHIs, so we cannot handle PHIs inside of loops.
01671       return 0;
01672 
01673   // If we won't be able to constant fold this expression even if the operands
01674   // are constants, return early.
01675   if (!CanConstantFold(I)) return 0;
01676   
01677   // Otherwise, we can evaluate this instruction if all of its operands are
01678   // constant or derived from a PHI node themselves.
01679   PHINode *PHI = 0;
01680   for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op)
01681     if (!(isa<Constant>(I->getOperand(Op)) ||
01682           isa<GlobalValue>(I->getOperand(Op)))) {
01683       PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L);
01684       if (P == 0) return 0;  // Not evolving from PHI
01685       if (PHI == 0)
01686         PHI = P;
01687       else if (PHI != P)
01688         return 0;  // Evolving from multiple different PHIs.
01689     }
01690 
01691   // This is a expression evolving from a constant PHI!
01692   return PHI;
01693 }
01694 
01695 /// EvaluateExpression - Given an expression that passes the
01696 /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node
01697 /// in the loop has the value PHIVal.  If we can't fold this expression for some
01698 /// reason, return null.
01699 static Constant *EvaluateExpression(Value *V, Constant *PHIVal) {
01700   if (isa<PHINode>(V)) return PHIVal;
01701   if (GlobalValue *GV = dyn_cast<GlobalValue>(V))
01702     return GV;
01703   if (Constant *C = dyn_cast<Constant>(V)) return C;
01704   Instruction *I = cast<Instruction>(V);
01705 
01706   std::vector<Constant*> Operands;
01707   Operands.resize(I->getNumOperands());
01708 
01709   for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
01710     Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal);
01711     if (Operands[i] == 0) return 0;
01712   }
01713 
01714   return ConstantFold(I, Operands);
01715 }
01716 
01717 /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
01718 /// in the header of its containing loop, we know the loop executes a
01719 /// constant number of times, and the PHI node is just a recurrence
01720 /// involving constants, fold it.
01721 Constant *ScalarEvolutionsImpl::
01722 getConstantEvolutionLoopExitValue(PHINode *PN, uint64_t Its, const Loop *L) {
01723   std::map<PHINode*, Constant*>::iterator I =
01724     ConstantEvolutionLoopExitValue.find(PN);
01725   if (I != ConstantEvolutionLoopExitValue.end())
01726     return I->second;
01727 
01728   if (Its > MaxBruteForceIterations) 
01729     return ConstantEvolutionLoopExitValue[PN] = 0;  // Not going to evaluate it.
01730 
01731   Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
01732 
01733   // Since the loop is canonicalized, the PHI node must have two entries.  One
01734   // entry must be a constant (coming in from outside of the loop), and the
01735   // second must be derived from the same PHI.
01736   bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
01737   Constant *StartCST =
01738     dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge));
01739   if (StartCST == 0)
01740     return RetVal = 0;  // Must be a constant.
01741 
01742   Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
01743   PHINode *PN2 = getConstantEvolvingPHI(BEValue, L);
01744   if (PN2 != PN)
01745     return RetVal = 0;  // Not derived from same PHI.
01746 
01747   // Execute the loop symbolically to determine the exit value.
01748   unsigned IterationNum = 0;
01749   unsigned NumIterations = Its;
01750   if (NumIterations != Its)
01751     return RetVal = 0;  // More than 2^32 iterations??
01752 
01753   for (Constant *PHIVal = StartCST; ; ++IterationNum) {
01754     if (IterationNum == NumIterations)
01755       return RetVal = PHIVal;  // Got exit value!
01756 
01757     // Compute the value of the PHI node for the next iteration.
01758     Constant *NextPHI = EvaluateExpression(BEValue, PHIVal);
01759     if (NextPHI == PHIVal)
01760       return RetVal = NextPHI;  // Stopped evolving!
01761     if (NextPHI == 0)
01762       return 0;        // Couldn't evaluate!
01763     PHIVal = NextPHI;
01764   }
01765 }
01766 
01767 /// ComputeIterationCountExhaustively - If the trip is known to execute a
01768 /// constant number of times (the condition evolves only from constants),
01769 /// try to evaluate a few iterations of the loop until we get the exit
01770 /// condition gets a value of ExitWhen (true or false).  If we cannot
01771 /// evaluate the trip count of the loop, return UnknownValue.
01772 SCEVHandle ScalarEvolutionsImpl::
01773 ComputeIterationCountExhaustively(const Loop *L, Value *Cond, bool ExitWhen) {
01774   PHINode *PN = getConstantEvolvingPHI(Cond, L);
01775   if (PN == 0) return UnknownValue;
01776 
01777   // Since the loop is canonicalized, the PHI node must have two entries.  One
01778   // entry must be a constant (coming in from outside of the loop), and the
01779   // second must be derived from the same PHI.
01780   bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
01781   Constant *StartCST =
01782     dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge));
01783   if (StartCST == 0) return UnknownValue;  // Must be a constant.
01784 
01785   Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
01786   PHINode *PN2 = getConstantEvolvingPHI(BEValue, L);
01787   if (PN2 != PN) return UnknownValue;  // Not derived from same PHI.
01788 
01789   // Okay, we find a PHI node that defines the trip count of this loop.  Execute
01790   // the loop symbolically to determine when the condition gets a value of
01791   // "ExitWhen".
01792   unsigned IterationNum = 0;
01793   unsigned MaxIterations = MaxBruteForceIterations;   // Limit analysis.
01794   for (Constant *PHIVal = StartCST;
01795        IterationNum != MaxIterations; ++IterationNum) {
01796     ConstantBool *CondVal =
01797       dyn_cast_or_null<ConstantBool>(EvaluateExpression(Cond, PHIVal));
01798     if (!CondVal) return UnknownValue;     // Couldn't symbolically evaluate.
01799 
01800     if (CondVal->getValue() == ExitWhen) {
01801       ConstantEvolutionLoopExitValue[PN] = PHIVal;
01802       ++NumBruteForceTripCountsComputed;
01803       return SCEVConstant::get(ConstantUInt::get(Type::UIntTy, IterationNum));
01804     }
01805     
01806     // Compute the value of the PHI node for the next iteration.
01807     Constant *NextPHI = EvaluateExpression(BEValue, PHIVal);
01808     if (NextPHI == 0 || NextPHI == PHIVal)
01809       return UnknownValue;  // Couldn't evaluate or not making progress...
01810     PHIVal = NextPHI;
01811   }
01812 
01813   // Too many iterations were needed to evaluate.
01814   return UnknownValue;
01815 }
01816 
01817 /// getSCEVAtScope - Compute the value of the specified expression within the
01818 /// indicated loop (which may be null to indicate in no loop).  If the
01819 /// expression cannot be evaluated, return UnknownValue.
01820 SCEVHandle ScalarEvolutionsImpl::getSCEVAtScope(SCEV *V, const Loop *L) {
01821   // FIXME: this should be turned into a virtual method on SCEV!
01822 
01823   if (isa<SCEVConstant>(V)) return V;
01824   
01825   // If this instruction is evolves from a constant-evolving PHI, compute the
01826   // exit value from the loop without using SCEVs.
01827   if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) {
01828     if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) {
01829       const Loop *LI = this->LI[I->getParent()];
01830       if (LI && LI->getParentLoop() == L)  // Looking for loop exit value.
01831         if (PHINode *PN = dyn_cast<PHINode>(I))
01832           if (PN->getParent() == LI->getHeader()) {
01833             // Okay, there is no closed form solution for the PHI node.  Check
01834             // to see if the loop that contains it has a known iteration count.
01835             // If so, we may be able to force computation of the exit value.
01836             SCEVHandle IterationCount = getIterationCount(LI);
01837             if (SCEVConstant *ICC = dyn_cast<SCEVConstant>(IterationCount)) {
01838               // Okay, we know how many times the containing loop executes.  If
01839               // this is a constant evolving PHI node, get the final value at
01840               // the specified iteration number.
01841               Constant *RV = getConstantEvolutionLoopExitValue(PN,
01842                                                ICC->getValue()->getRawValue(),
01843                                                                LI);
01844               if (RV) return SCEVUnknown::get(RV);
01845             }
01846           }
01847 
01848       // Okay, this is a some expression that we cannot symbolically evaluate
01849       // into a SCEV.  Check to see if it's possible to symbolically evaluate
01850       // the arguments into constants, and if see, try to constant propagate the
01851       // result.  This is particularly useful for computing loop exit values.
01852       if (CanConstantFold(I)) {
01853         std::vector<Constant*> Operands;
01854         Operands.reserve(I->getNumOperands());
01855         for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
01856           Value *Op = I->getOperand(i);
01857           if (Constant *C = dyn_cast<Constant>(Op)) {
01858             Operands.push_back(C);
01859           } else {
01860             SCEVHandle OpV = getSCEVAtScope(getSCEV(Op), L);
01861             if (SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV))
01862               Operands.push_back(ConstantExpr::getCast(SC->getValue(),
01863                                                        Op->getType()));
01864             else if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) {
01865               if (Constant *C = dyn_cast<Constant>(SU->getValue()))
01866                 Operands.push_back(ConstantExpr::getCast(C, Op->getType()));
01867               else
01868                 return V;
01869             } else {
01870               return V;
01871             }
01872           }
01873         }
01874         return SCEVUnknown::get(ConstantFold(I, Operands));
01875       }
01876     }
01877 
01878     // This is some other type of SCEVUnknown, just return it.
01879     return V;
01880   }
01881 
01882   if (SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) {
01883     // Avoid performing the look-up in the common case where the specified
01884     // expression has no loop-variant portions.
01885     for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) {
01886       SCEVHandle OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
01887       if (OpAtScope != Comm->getOperand(i)) {
01888         if (OpAtScope == UnknownValue) return UnknownValue;
01889         // Okay, at least one of these operands is loop variant but might be
01890         // foldable.  Build a new instance of the folded commutative expression.
01891         std::vector<SCEVHandle> NewOps(Comm->op_begin(), Comm->op_begin()+i);
01892         NewOps.push_back(OpAtScope);
01893 
01894         for (++i; i != e; ++i) {
01895           OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
01896           if (OpAtScope == UnknownValue) return UnknownValue;
01897           NewOps.push_back(OpAtScope);
01898         }
01899         if (isa<SCEVAddExpr>(Comm))
01900           return SCEVAddExpr::get(NewOps);
01901         assert(isa<SCEVMulExpr>(Comm) && "Only know about add and mul!");
01902         return SCEVMulExpr::get(NewOps);
01903       }
01904     }
01905     // If we got here, all operands are loop invariant.
01906     return Comm;
01907   }
01908 
01909   if (SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(V)) {
01910     SCEVHandle LHS = getSCEVAtScope(UDiv->getLHS(), L);
01911     if (LHS == UnknownValue) return LHS;
01912     SCEVHandle RHS = getSCEVAtScope(UDiv->getRHS(), L);
01913     if (RHS == UnknownValue) return RHS;
01914     if (LHS == UDiv->getLHS() && RHS == UDiv->getRHS())
01915       return UDiv;   // must be loop invariant
01916     return SCEVUDivExpr::get(LHS, RHS);
01917   }
01918 
01919   // If this is a loop recurrence for a loop that does not contain L, then we
01920   // are dealing with the final value computed by the loop.
01921   if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
01922     if (!L || !AddRec->getLoop()->contains(L->getHeader())) {
01923       // To evaluate this recurrence, we need to know how many times the AddRec
01924       // loop iterates.  Compute this now.
01925       SCEVHandle IterationCount = getIterationCount(AddRec->getLoop());
01926       if (IterationCount == UnknownValue) return UnknownValue;
01927       IterationCount = getTruncateOrZeroExtend(IterationCount,
01928                                                AddRec->getType());
01929       
01930       // If the value is affine, simplify the expression evaluation to just
01931       // Start + Step*IterationCount.
01932       if (AddRec->isAffine())
01933         return SCEVAddExpr::get(AddRec->getStart(),
01934                                 SCEVMulExpr::get(IterationCount,
01935                                                  AddRec->getOperand(1)));
01936 
01937       // Otherwise, evaluate it the hard way.
01938       return AddRec->evaluateAtIteration(IterationCount);
01939     }
01940     return UnknownValue;
01941   }
01942 
01943   //assert(0 && "Unknown SCEV type!");
01944   return UnknownValue;
01945 }
01946 
01947 
01948 /// SolveQuadraticEquation - Find the roots of the quadratic equation for the
01949 /// given quadratic chrec {L,+,M,+,N}.  This returns either the two roots (which
01950 /// might be the same) or two SCEVCouldNotCompute objects.
01951 ///
01952 static std::pair<SCEVHandle,SCEVHandle>
01953 SolveQuadraticEquation(const SCEVAddRecExpr *AddRec) {
01954   assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
01955   SCEVConstant *L = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
01956   SCEVConstant *M = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
01957   SCEVConstant *N = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
01958   
01959   // We currently can only solve this if the coefficients are constants.
01960   if (!L || !M || !N) {
01961     SCEV *CNC = new SCEVCouldNotCompute();
01962     return std::make_pair(CNC, CNC);
01963   }
01964 
01965   Constant *Two = ConstantInt::get(L->getValue()->getType(), 2);
01966   
01967   // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C
01968   Constant *C = L->getValue();
01969   // The B coefficient is M-N/2
01970   Constant *B = ConstantExpr::getSub(M->getValue(),
01971                                      ConstantExpr::getDiv(N->getValue(),
01972                                                           Two));
01973   // The A coefficient is N/2
01974   Constant *A = ConstantExpr::getDiv(N->getValue(), Two);
01975         
01976   // Compute the B^2-4ac term.
01977   Constant *SqrtTerm =
01978     ConstantExpr::getMul(ConstantInt::get(C->getType(), 4),
01979                          ConstantExpr::getMul(A, C));
01980   SqrtTerm = ConstantExpr::getSub(ConstantExpr::getMul(B, B), SqrtTerm);
01981 
01982   // Compute floor(sqrt(B^2-4ac))
01983   ConstantUInt *SqrtVal =
01984     cast<ConstantUInt>(ConstantExpr::getCast(SqrtTerm,
01985                                    SqrtTerm->getType()->getUnsignedVersion()));
01986   uint64_t SqrtValV = SqrtVal->getValue();
01987   uint64_t SqrtValV2 = (uint64_t)sqrt((double)SqrtValV);
01988   // The square root might not be precise for arbitrary 64-bit integer
01989   // values.  Do some sanity checks to ensure it's correct.
01990   if (SqrtValV2*SqrtValV2 > SqrtValV ||
01991       (SqrtValV2+1)*(SqrtValV2+1) <= SqrtValV) {
01992     SCEV *CNC = new SCEVCouldNotCompute();
01993     return std::make_pair(CNC, CNC);
01994   }
01995 
01996   SqrtVal = ConstantUInt::get(Type::ULongTy, SqrtValV2);
01997   SqrtTerm = ConstantExpr::getCast(SqrtVal, SqrtTerm->getType());
01998   
01999   Constant *NegB = ConstantExpr::getNeg(B);
02000   Constant *TwoA = ConstantExpr::getMul(A, Two);
02001   
02002   // The divisions must be performed as signed divisions.
02003   const Type *SignedTy = NegB->getType()->getSignedVersion();
02004   NegB = ConstantExpr::getCast(NegB, SignedTy);
02005   TwoA = ConstantExpr::getCast(TwoA, SignedTy);
02006   SqrtTerm = ConstantExpr::getCast(SqrtTerm, SignedTy);
02007   
02008   Constant *Solution1 =
02009     ConstantExpr::getDiv(ConstantExpr::getAdd(NegB, SqrtTerm), TwoA);
02010   Constant *Solution2 =
02011     ConstantExpr::getDiv(ConstantExpr::getSub(NegB, SqrtTerm), TwoA);
02012   return std::make_pair(SCEVUnknown::get(Solution1),
02013                         SCEVUnknown::get(Solution2));
02014 }
02015 
02016 /// HowFarToZero - Return the number of times a backedge comparing the specified
02017 /// value to zero will execute.  If not computable, return UnknownValue
02018 SCEVHandle ScalarEvolutionsImpl::HowFarToZero(SCEV *V, const Loop *L) {
02019   // If the value is a constant
02020   if (SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
02021     // If the value is already zero, the branch will execute zero times.
02022     if (C->getValue()->isNullValue()) return C;
02023     return UnknownValue;  // Otherwise it will loop infinitely.
02024   }
02025 
02026   SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V);
02027   if (!AddRec || AddRec->getLoop() != L)
02028     return UnknownValue;
02029 
02030   if (AddRec->isAffine()) {
02031     // If this is an affine expression the execution count of this branch is
02032     // equal to:
02033     //
02034     //     (0 - Start/Step)    iff   Start % Step == 0
02035     //
02036     // Get the initial value for the loop.
02037     SCEVHandle Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop());
02038     if (isa<SCEVCouldNotCompute>(Start)) return UnknownValue;
02039     SCEVHandle Step = AddRec->getOperand(1);
02040 
02041     Step = getSCEVAtScope(Step, L->getParentLoop());
02042 
02043     // Figure out if Start % Step == 0.
02044     // FIXME: We should add DivExpr and RemExpr operations to our AST.
02045     if (SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) {
02046       if (StepC->getValue()->equalsInt(1))      // N % 1 == 0
02047         return getNegativeSCEV(Start);  // 0 - Start/1 == -Start
02048       if (StepC->getValue()->isAllOnesValue())  // N % -1 == 0
02049         return Start;                   // 0 - Start/-1 == Start
02050 
02051       // Check to see if Start is divisible by SC with no remainder.
02052       if (SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) {
02053         ConstantInt *StartCC = StartC->getValue();
02054         Constant *StartNegC = ConstantExpr::getNeg(StartCC);
02055         Constant *Rem = ConstantExpr::getRem(StartNegC, StepC->getValue());
02056         if (Rem->isNullValue()) {
02057           Constant *Result =ConstantExpr::getDiv(StartNegC,StepC->getValue());
02058           return SCEVUnknown::get(Result);
02059         }
02060       }
02061     }
02062   } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) {
02063     // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
02064     // the quadratic equation to solve it.
02065     std::pair<SCEVHandle,SCEVHandle> Roots = SolveQuadraticEquation(AddRec);
02066     SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
02067     SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
02068     if (R1) {
02069 #if 0
02070       std::cerr << "HFTZ: " << *V << " - sol#1: " << *R1
02071                 << "  sol#2: " << *R2 << "\n";
02072 #endif
02073       // Pick the smallest positive root value.
02074       assert(R1->getType()->isUnsigned()&&"Didn't canonicalize to unsigned?");
02075       if (ConstantBool *CB =
02076           dyn_cast<ConstantBool>(ConstantExpr::getSetLT(R1->getValue(),
02077                                                         R2->getValue()))) {
02078         if (CB != ConstantBool::True)
02079           std::swap(R1, R2);   // R1 is the minimum root now.
02080           
02081         // We can only use this value if the chrec ends up with an exact zero
02082         // value at this index.  When solving for "X*X != 5", for example, we
02083         // should not accept a root of 2.
02084         SCEVHandle Val = AddRec->evaluateAtIteration(R1);
02085         if (SCEVConstant *EvalVal = dyn_cast<SCEVConstant>(Val))
02086           if (EvalVal->getValue()->isNullValue())
02087             return R1;  // We found a quadratic root!
02088       }
02089     }
02090   }
02091   
02092   return UnknownValue;
02093 }
02094 
02095 /// HowFarToNonZero - Return the number of times a backedge checking the
02096 /// specified value for nonzero will execute.  If not computable, return
02097 /// UnknownValue
02098 SCEVHandle ScalarEvolutionsImpl::HowFarToNonZero(SCEV *V, const Loop *L) {
02099   // Loops that look like: while (X == 0) are very strange indeed.  We don't
02100   // handle them yet except for the trivial case.  This could be expanded in the
02101   // future as needed.
02102  
02103   // If the value is a constant, check to see if it is known to be non-zero
02104   // already.  If so, the backedge will execute zero times.
02105   if (SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
02106     Constant *Zero = Constant::getNullValue(C->getValue()->getType());
02107     Constant *NonZero = ConstantExpr::getSetNE(C->getValue(), Zero);
02108     if (NonZero == ConstantBool::True)
02109       return getSCEV(Zero);
02110     return UnknownValue;  // Otherwise it will loop infinitely.
02111   }
02112   
02113   // We could implement others, but I really doubt anyone writes loops like
02114   // this, and if they did, they would already be constant folded.
02115   return UnknownValue;
02116 }
02117 
02118 /// getNumIterationsInRange - Return the number of iterations of this loop that
02119 /// produce values in the specified constant range.  Another way of looking at
02120 /// this is that it returns the first iteration number where the value is not in
02121 /// the condition, thus computing the exit count. If the iteration count can't
02122 /// be computed, an instance of SCEVCouldNotCompute is returned.
02123 SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range) const {
02124   if (Range.isFullSet())  // Infinite loop.
02125     return new SCEVCouldNotCompute();
02126 
02127   // If the start is a non-zero constant, shift the range to simplify things.
02128   if (SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart()))
02129     if (!SC->getValue()->isNullValue()) {
02130       std::vector<SCEVHandle> Operands(op_begin(), op_end());
02131       Operands[0] = SCEVUnknown::getIntegerSCEV(0, SC->getType());
02132       SCEVHandle Shifted = SCEVAddRecExpr::get(Operands, getLoop());
02133       if (SCEVAddRecExpr *ShiftedAddRec = dyn_cast<SCEVAddRecExpr>(Shifted))
02134         return ShiftedAddRec->getNumIterationsInRange(
02135                                               Range.subtract(SC->getValue()));
02136       // This is strange and shouldn't happen.
02137       return new SCEVCouldNotCompute();
02138     }
02139 
02140   // The only time we can solve this is when we have all constant indices.
02141   // Otherwise, we cannot determine the overflow conditions.
02142   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
02143     if (!isa<SCEVConstant>(getOperand(i)))
02144       return new SCEVCouldNotCompute();
02145 
02146 
02147   // Okay at this point we know that all elements of the chrec are constants and
02148   // that the start element is zero.
02149 
02150   // First check to see if the range contains zero.  If not, the first
02151   // iteration exits.
02152   ConstantInt *Zero = ConstantInt::get(getType(), 0);
02153   if (!Range.contains(Zero)) return SCEVConstant::get(Zero);
02154   
02155   if (isAffine()) {
02156     // If this is an affine expression then we have this situation:
02157     //   Solve {0,+,A} in Range  ===  Ax in Range
02158 
02159     // Since we know that zero is in the range, we know that the upper value of
02160     // the range must be the first possible exit value.  Also note that we
02161     // already checked for a full range.
02162     ConstantInt *Upper = cast<ConstantInt>(Range.getUpper());
02163     ConstantInt *A     = cast<SCEVConstant>(getOperand(1))->getValue();
02164     ConstantInt *One   = ConstantInt::get(getType(), 1);
02165 
02166     // The exit value should be (Upper+A-1)/A.
02167     Constant *ExitValue = Upper;
02168     if (A != One) {
02169       ExitValue = ConstantExpr::getSub(ConstantExpr::getAdd(Upper, A), One);
02170       ExitValue = ConstantExpr::getDiv(ExitValue, A);
02171     }
02172     assert(isa<ConstantInt>(ExitValue) &&
02173            "Constant folding of integers not implemented?");
02174 
02175     // Evaluate at the exit value.  If we really did fall out of the valid
02176     // range, then we computed our trip count, otherwise wrap around or other
02177     // things must have happened.
02178     ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue);
02179     if (Range.contains(Val))
02180       return new SCEVCouldNotCompute();  // Something strange happened
02181 
02182     // Ensure that the previous value is in the range.  This is a sanity check.
02183     assert(Range.contains(EvaluateConstantChrecAtConstant(this,
02184                               ConstantExpr::getSub(ExitValue, One))) &&
02185            "Linear scev computation is off in a bad way!");
02186     return SCEVConstant::get(cast<ConstantInt>(ExitValue));
02187   } else if (isQuadratic()) {
02188     // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the
02189     // quadratic equation to solve it.  To do this, we must frame our problem in
02190     // terms of figuring out when zero is crossed, instead of when
02191     // Range.getUpper() is crossed.
02192     std::vector<SCEVHandle> NewOps(op_begin(), op_end());
02193     NewOps[0] = getNegativeSCEV(SCEVUnknown::get(Range.getUpper()));
02194     SCEVHandle NewAddRec = SCEVAddRecExpr::get(NewOps, getLoop());
02195 
02196     // Next, solve the constructed addrec
02197     std::pair<SCEVHandle,SCEVHandle> Roots =
02198       SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec));
02199     SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
02200     SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
02201     if (R1) {
02202       // Pick the smallest positive root value.
02203       assert(R1->getType()->isUnsigned() && "Didn't canonicalize to unsigned?");
02204       if (ConstantBool *CB =
02205           dyn_cast<ConstantBool>(ConstantExpr::getSetLT(R1->getValue(),
02206                                                         R2->getValue()))) {
02207         if (CB != ConstantBool::True)
02208           std::swap(R1, R2);   // R1 is the minimum root now.
02209           
02210         // Make sure the root is not off by one.  The returned iteration should
02211         // not be in the range, but the previous one should be.  When solving
02212         // for "X*X < 5", for example, we should not return a root of 2.
02213         ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this,
02214                                                              R1->getValue());
02215         if (Range.contains(R1Val)) {
02216           // The next iteration must be out of the range...
02217           Constant *NextVal =
02218             ConstantExpr::getAdd(R1->getValue(),
02219                                  ConstantInt::get(R1->getType(), 1));
02220           
02221           R1Val = EvaluateConstantChrecAtConstant(this, NextVal);
02222           if (!Range.contains(R1Val))
02223             return SCEVUnknown::get(NextVal);
02224           return new SCEVCouldNotCompute();  // Something strange happened
02225         }
02226    
02227         // If R1 was not in the range, then it is a good return value.  Make
02228         // sure that R1-1 WAS in the range though, just in case.
02229         Constant *NextVal =
02230           ConstantExpr::getSub(R1->getValue(),
02231                                ConstantInt::get(R1->getType(), 1));
02232         R1Val = EvaluateConstantChrecAtConstant(this, NextVal);
02233         if (Range.contains(R1Val))
02234           return R1;
02235         return new SCEVCouldNotCompute();  // Something strange happened
02236       }
02237     }
02238   }
02239 
02240   // Fallback, if this is a general polynomial, figure out the progression
02241   // through brute force: evaluate until we find an iteration that fails the
02242   // test.  This is likely to be slow, but getting an accurate trip count is
02243   // incredibly important, we will be able to simplify the exit test a lot, and
02244   // we are almost guaranteed to get a trip count in this case.
02245   ConstantInt *TestVal = ConstantInt::get(getType(), 0);
02246   ConstantInt *One     = ConstantInt::get(getType(), 1);
02247   ConstantInt *EndVal  = TestVal;  // Stop when we wrap around.
02248   do {
02249     ++NumBruteForceEvaluations;
02250     SCEVHandle Val = evaluateAtIteration(SCEVConstant::get(TestVal));
02251     if (!isa<SCEVConstant>(Val))  // This shouldn't happen.
02252       return new SCEVCouldNotCompute();
02253 
02254     // Check to see if we found the value!
02255     if (!Range.contains(cast<SCEVConstant>(Val)->getValue()))
02256       return SCEVConstant::get(TestVal);
02257 
02258     // Increment to test the next index.
02259     TestVal = cast<ConstantInt>(ConstantExpr::getAdd(TestVal, One));
02260   } while (TestVal != EndVal);
02261   
02262   return new SCEVCouldNotCompute();
02263 }
02264 
02265 
02266 
02267 //===----------------------------------------------------------------------===//
02268 //                   ScalarEvolution Class Implementation
02269 //===----------------------------------------------------------------------===//
02270 
02271 bool ScalarEvolution::runOnFunction(Function &F) {
02272   Impl = new ScalarEvolutionsImpl(F, getAnalysis<LoopInfo>());
02273   return false;
02274 }
02275 
02276 void ScalarEvolution::releaseMemory() {
02277   delete (ScalarEvolutionsImpl*)Impl;
02278   Impl = 0;
02279 }
02280 
02281 void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const {
02282   AU.setPreservesAll();
02283   AU.addRequiredID(LoopSimplifyID);
02284   AU.addRequiredTransitive<LoopInfo>();
02285 }
02286 
02287 SCEVHandle ScalarEvolution::getSCEV(Value *V) const {
02288   return ((ScalarEvolutionsImpl*)Impl)->getSCEV(V);
02289 }
02290 
02291 SCEVHandle ScalarEvolution::getIterationCount(const Loop *L) const {
02292   return ((ScalarEvolutionsImpl*)Impl)->getIterationCount(L);
02293 }
02294 
02295 bool ScalarEvolution::hasLoopInvariantIterationCount(const Loop *L) const {
02296   return !isa<SCEVCouldNotCompute>(getIterationCount(L));
02297 }
02298 
02299 SCEVHandle ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) const {
02300   return ((ScalarEvolutionsImpl*)Impl)->getSCEVAtScope(getSCEV(V), L);
02301 }
02302 
02303 void ScalarEvolution::deleteInstructionFromRecords(Instruction *I) const {
02304   return ((ScalarEvolutionsImpl*)Impl)->deleteInstructionFromRecords(I);
02305 }
02306 
02307 static void PrintLoopInfo(std::ostream &OS, const ScalarEvolution *SE, 
02308                           const Loop *L) {
02309   // Print all inner loops first
02310   for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
02311     PrintLoopInfo(OS, SE, *I);
02312   
02313   std::cerr << "Loop " << L->getHeader()->getName() << ": ";
02314 
02315   std::vector<BasicBlock*> ExitBlocks;
02316   L->getExitBlocks(ExitBlocks);
02317   if (ExitBlocks.size() != 1)
02318     std::cerr << "<multiple exits> ";
02319 
02320   if (SE->hasLoopInvariantIterationCount(L)) {
02321     std::cerr << *SE->getIterationCount(L) << " iterations! ";
02322   } else {
02323     std::cerr << "Unpredictable iteration count. ";
02324   }
02325 
02326   std::cerr << "\n";
02327 }
02328 
02329 void ScalarEvolution::print(std::ostream &OS) const {
02330   Function &F = ((ScalarEvolutionsImpl*)Impl)->F;
02331   LoopInfo &LI = ((ScalarEvolutionsImpl*)Impl)->LI;
02332 
02333   OS << "Classifying expressions for: " << F.getName() << "\n";
02334   for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
02335     if (I->getType()->isInteger()) {
02336       OS << *I;
02337       OS << "  --> ";
02338       SCEVHandle SV = getSCEV(&*I);
02339       SV->print(OS);
02340       OS << "\t\t";
02341       
02342       if ((*I).getType()->isIntegral()) {
02343         ConstantRange Bounds = SV->getValueRange();
02344         if (!Bounds.isFullSet())
02345           OS << "Bounds: " << Bounds << " ";
02346       }
02347 
02348       if (const Loop *L = LI.getLoopFor((*I).getParent())) {
02349         OS << "Exits: ";
02350         SCEVHandle ExitValue = getSCEVAtScope(&*I, L->getParentLoop());
02351         if (isa<SCEVCouldNotCompute>(ExitValue)) {
02352           OS << "<<Unknown>>";
02353         } else {
02354           OS << *ExitValue;
02355         }
02356       }
02357 
02358 
02359       OS << "\n";
02360     }
02361 
02362   OS << "Determining loop execution counts for: " << F.getName() << "\n";
02363   for (LoopInfo::iterator I = LI.begin(), E = LI.end(); I != E; ++I)
02364     PrintLoopInfo(OS, this, *I);
02365 }
02366