ScalarEvolution.cpp revision 9525528a7dc5462b6374d38c81ba5c07b11741fe
1//===- ScalarEvolution.cpp - Scalar Evolution Analysis ----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the implementation of the scalar evolution analysis
11// engine, which is used primarily to analyze expressions involving induction
12// variables in loops.
13//
14// There are several aspects to this library.  First is the representation of
15// scalar expressions, which are represented as subclasses of the SCEV class.
16// These classes are used to represent certain types of subexpressions that we
17// can handle.  These classes are reference counted, managed by the SCEVHandle
18// class.  We only create one SCEV of a particular shape, so pointer-comparisons
19// for equality are legal.
20//
21// One important aspect of the SCEV objects is that they are never cyclic, even
22// if there is a cycle in the dataflow for an expression (ie, a PHI node).  If
23// the PHI node is one of the idioms that we can represent (e.g., a polynomial
24// recurrence) then we represent it directly as a recurrence node, otherwise we
25// represent it as a SCEVUnknown node.
26//
27// In addition to being able to represent expressions of various types, we also
28// have folders that are used to build the *canonical* representation for a
29// particular expression.  These folders are capable of using a variety of
30// rewrite rules to simplify the expressions.
31//
32// Once the folders are defined, we can implement the more interesting
33// higher-level code, such as the code that recognizes PHI nodes of various
34// types, computes the execution count of a loop, etc.
35//
36// TODO: We should use these routines and value representations to implement
37// dependence analysis!
38//
39//===----------------------------------------------------------------------===//
40//
41// There are several good references for the techniques used in this analysis.
42//
43//  Chains of recurrences -- a method to expedite the evaluation
44//  of closed-form functions
45//  Olaf Bachmann, Paul S. Wang, Eugene V. Zima
46//
47//  On computational properties of chains of recurrences
48//  Eugene V. Zima
49//
50//  Symbolic Evaluation of Chains of Recurrences for Loop Optimization
51//  Robert A. van Engelen
52//
53//  Efficient Symbolic Analysis for Optimizing Compilers
54//  Robert A. van Engelen
55//
56//  Using the chains of recurrences algebra for data dependence testing and
57//  induction variable substitution
58//  MS Thesis, Johnie Birch
59//
60//===----------------------------------------------------------------------===//
61
62#include "llvm/Analysis/ScalarEvolutionExpressions.h"
63#include "llvm/Constants.h"
64#include "llvm/DerivedTypes.h"
65#include "llvm/GlobalVariable.h"
66#include "llvm/Instructions.h"
67#include "llvm/Analysis/ConstantFolding.h"
68#include "llvm/Analysis/LoopInfo.h"
69#include "llvm/Assembly/Writer.h"
70#include "llvm/Transforms/Scalar.h"
71#include "llvm/Support/CFG.h"
72#include "llvm/Support/CommandLine.h"
73#include "llvm/Support/ConstantRange.h"
74#include "llvm/Support/InstIterator.h"
75#include "llvm/Support/Visibility.h"
76#include "llvm/ADT/Statistic.h"
77#include <cmath>
78#include <iostream>
79#include <algorithm>
80using namespace llvm;
81
82namespace {
83  RegisterAnalysis<ScalarEvolution>
84  R("scalar-evolution", "Scalar Evolution Analysis");
85
86  Statistic<>
87  NumBruteForceEvaluations("scalar-evolution",
88                           "Number of brute force evaluations needed to "
89                           "calculate high-order polynomial exit values");
90  Statistic<>
91  NumArrayLenItCounts("scalar-evolution",
92                      "Number of trip counts computed with array length");
93  Statistic<>
94  NumTripCountsComputed("scalar-evolution",
95                        "Number of loops with predictable loop counts");
96  Statistic<>
97  NumTripCountsNotComputed("scalar-evolution",
98                           "Number of loops without predictable loop counts");
99  Statistic<>
100  NumBruteForceTripCountsComputed("scalar-evolution",
101                        "Number of loops with trip counts computed by force");
102
103  cl::opt<unsigned>
104  MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden,
105                          cl::desc("Maximum number of iterations SCEV will "
106                              "symbolically execute a constant derived loop"),
107                          cl::init(100));
108}
109
110//===----------------------------------------------------------------------===//
111//                           SCEV class definitions
112//===----------------------------------------------------------------------===//
113
114//===----------------------------------------------------------------------===//
115// Implementation of the SCEV class.
116//
117SCEV::~SCEV() {}
118void SCEV::dump() const {
119  print(std::cerr);
120}
121
122/// getValueRange - Return the tightest constant bounds that this value is
123/// known to have.  This method is only valid on integer SCEV objects.
124ConstantRange SCEV::getValueRange() const {
125  const Type *Ty = getType();
126  assert(Ty->isInteger() && "Can't get range for a non-integer SCEV!");
127  Ty = Ty->getUnsignedVersion();
128  // Default to a full range if no better information is available.
129  return ConstantRange(getType());
130}
131
132
133SCEVCouldNotCompute::SCEVCouldNotCompute() : SCEV(scCouldNotCompute) {}
134
135bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const {
136  assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
137  return false;
138}
139
140const Type *SCEVCouldNotCompute::getType() const {
141  assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
142  return 0;
143}
144
145bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const {
146  assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
147  return false;
148}
149
150SCEVHandle SCEVCouldNotCompute::
151replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
152                                  const SCEVHandle &Conc) const {
153  return this;
154}
155
156void SCEVCouldNotCompute::print(std::ostream &OS) const {
157  OS << "***COULDNOTCOMPUTE***";
158}
159
160bool SCEVCouldNotCompute::classof(const SCEV *S) {
161  return S->getSCEVType() == scCouldNotCompute;
162}
163
164
165// SCEVConstants - Only allow the creation of one SCEVConstant for any
166// particular value.  Don't use a SCEVHandle here, or else the object will
167// never be deleted!
168static std::map<ConstantInt*, SCEVConstant*> SCEVConstants;
169
170
171SCEVConstant::~SCEVConstant() {
172  SCEVConstants.erase(V);
173}
174
175SCEVHandle SCEVConstant::get(ConstantInt *V) {
176  // Make sure that SCEVConstant instances are all unsigned.
177  if (V->getType()->isSigned()) {
178    const Type *NewTy = V->getType()->getUnsignedVersion();
179    V = cast<ConstantUInt>(ConstantExpr::getCast(V, NewTy));
180  }
181
182  SCEVConstant *&R = SCEVConstants[V];
183  if (R == 0) R = new SCEVConstant(V);
184  return R;
185}
186
187ConstantRange SCEVConstant::getValueRange() const {
188  return ConstantRange(V);
189}
190
191const Type *SCEVConstant::getType() const { return V->getType(); }
192
193void SCEVConstant::print(std::ostream &OS) const {
194  WriteAsOperand(OS, V, false);
195}
196
197// SCEVTruncates - Only allow the creation of one SCEVTruncateExpr for any
198// particular input.  Don't use a SCEVHandle here, or else the object will
199// never be deleted!
200static std::map<std::pair<SCEV*, const Type*>, SCEVTruncateExpr*> SCEVTruncates;
201
202SCEVTruncateExpr::SCEVTruncateExpr(const SCEVHandle &op, const Type *ty)
203  : SCEV(scTruncate), Op(op), Ty(ty) {
204  assert(Op->getType()->isInteger() && Ty->isInteger() &&
205         Ty->isUnsigned() &&
206         "Cannot truncate non-integer value!");
207  assert(Op->getType()->getPrimitiveSize() > Ty->getPrimitiveSize() &&
208         "This is not a truncating conversion!");
209}
210
211SCEVTruncateExpr::~SCEVTruncateExpr() {
212  SCEVTruncates.erase(std::make_pair(Op, Ty));
213}
214
215ConstantRange SCEVTruncateExpr::getValueRange() const {
216  return getOperand()->getValueRange().truncate(getType());
217}
218
219void SCEVTruncateExpr::print(std::ostream &OS) const {
220  OS << "(truncate " << *Op << " to " << *Ty << ")";
221}
222
223// SCEVZeroExtends - Only allow the creation of one SCEVZeroExtendExpr for any
224// particular input.  Don't use a SCEVHandle here, or else the object will never
225// be deleted!
226static std::map<std::pair<SCEV*, const Type*>,
227                SCEVZeroExtendExpr*> SCEVZeroExtends;
228
229SCEVZeroExtendExpr::SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty)
230  : SCEV(scTruncate), Op(op), Ty(ty) {
231  assert(Op->getType()->isInteger() && Ty->isInteger() &&
232         Ty->isUnsigned() &&
233         "Cannot zero extend non-integer value!");
234  assert(Op->getType()->getPrimitiveSize() < Ty->getPrimitiveSize() &&
235         "This is not an extending conversion!");
236}
237
238SCEVZeroExtendExpr::~SCEVZeroExtendExpr() {
239  SCEVZeroExtends.erase(std::make_pair(Op, Ty));
240}
241
242ConstantRange SCEVZeroExtendExpr::getValueRange() const {
243  return getOperand()->getValueRange().zeroExtend(getType());
244}
245
246void SCEVZeroExtendExpr::print(std::ostream &OS) const {
247  OS << "(zeroextend " << *Op << " to " << *Ty << ")";
248}
249
250// SCEVCommExprs - Only allow the creation of one SCEVCommutativeExpr for any
251// particular input.  Don't use a SCEVHandle here, or else the object will never
252// be deleted!
253static std::map<std::pair<unsigned, std::vector<SCEV*> >,
254                SCEVCommutativeExpr*> SCEVCommExprs;
255
256SCEVCommutativeExpr::~SCEVCommutativeExpr() {
257  SCEVCommExprs.erase(std::make_pair(getSCEVType(),
258                                     std::vector<SCEV*>(Operands.begin(),
259                                                        Operands.end())));
260}
261
262void SCEVCommutativeExpr::print(std::ostream &OS) const {
263  assert(Operands.size() > 1 && "This plus expr shouldn't exist!");
264  const char *OpStr = getOperationStr();
265  OS << "(" << *Operands[0];
266  for (unsigned i = 1, e = Operands.size(); i != e; ++i)
267    OS << OpStr << *Operands[i];
268  OS << ")";
269}
270
271SCEVHandle SCEVCommutativeExpr::
272replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
273                                  const SCEVHandle &Conc) const {
274  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
275    SCEVHandle H = getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc);
276    if (H != getOperand(i)) {
277      std::vector<SCEVHandle> NewOps;
278      NewOps.reserve(getNumOperands());
279      for (unsigned j = 0; j != i; ++j)
280        NewOps.push_back(getOperand(j));
281      NewOps.push_back(H);
282      for (++i; i != e; ++i)
283        NewOps.push_back(getOperand(i)->
284                         replaceSymbolicValuesWithConcrete(Sym, Conc));
285
286      if (isa<SCEVAddExpr>(this))
287        return SCEVAddExpr::get(NewOps);
288      else if (isa<SCEVMulExpr>(this))
289        return SCEVMulExpr::get(NewOps);
290      else
291        assert(0 && "Unknown commutative expr!");
292    }
293  }
294  return this;
295}
296
297
298// SCEVSDivs - Only allow the creation of one SCEVSDivExpr for any particular
299// input.  Don't use a SCEVHandle here, or else the object will never be
300// deleted!
301static std::map<std::pair<SCEV*, SCEV*>, SCEVSDivExpr*> SCEVSDivs;
302
303SCEVSDivExpr::~SCEVSDivExpr() {
304  SCEVSDivs.erase(std::make_pair(LHS, RHS));
305}
306
307void SCEVSDivExpr::print(std::ostream &OS) const {
308  OS << "(" << *LHS << " /s " << *RHS << ")";
309}
310
311const Type *SCEVSDivExpr::getType() const {
312  const Type *Ty = LHS->getType();
313  if (Ty->isUnsigned()) Ty = Ty->getSignedVersion();
314  return Ty;
315}
316
317// SCEVAddRecExprs - Only allow the creation of one SCEVAddRecExpr for any
318// particular input.  Don't use a SCEVHandle here, or else the object will never
319// be deleted!
320static std::map<std::pair<const Loop *, std::vector<SCEV*> >,
321                SCEVAddRecExpr*> SCEVAddRecExprs;
322
323SCEVAddRecExpr::~SCEVAddRecExpr() {
324  SCEVAddRecExprs.erase(std::make_pair(L,
325                                       std::vector<SCEV*>(Operands.begin(),
326                                                          Operands.end())));
327}
328
329SCEVHandle SCEVAddRecExpr::
330replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
331                                  const SCEVHandle &Conc) const {
332  for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
333    SCEVHandle H = getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc);
334    if (H != getOperand(i)) {
335      std::vector<SCEVHandle> NewOps;
336      NewOps.reserve(getNumOperands());
337      for (unsigned j = 0; j != i; ++j)
338        NewOps.push_back(getOperand(j));
339      NewOps.push_back(H);
340      for (++i; i != e; ++i)
341        NewOps.push_back(getOperand(i)->
342                         replaceSymbolicValuesWithConcrete(Sym, Conc));
343
344      return get(NewOps, L);
345    }
346  }
347  return this;
348}
349
350
351bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const {
352  // This recurrence is invariant w.r.t to QueryLoop iff QueryLoop doesn't
353  // contain L and if the start is invariant.
354  return !QueryLoop->contains(L->getHeader()) &&
355         getOperand(0)->isLoopInvariant(QueryLoop);
356}
357
358
359void SCEVAddRecExpr::print(std::ostream &OS) const {
360  OS << "{" << *Operands[0];
361  for (unsigned i = 1, e = Operands.size(); i != e; ++i)
362    OS << ",+," << *Operands[i];
363  OS << "}<" << L->getHeader()->getName() + ">";
364}
365
366// SCEVUnknowns - Only allow the creation of one SCEVUnknown for any particular
367// value.  Don't use a SCEVHandle here, or else the object will never be
368// deleted!
369static std::map<Value*, SCEVUnknown*> SCEVUnknowns;
370
371SCEVUnknown::~SCEVUnknown() { SCEVUnknowns.erase(V); }
372
373bool SCEVUnknown::isLoopInvariant(const Loop *L) const {
374  // All non-instruction values are loop invariant.  All instructions are loop
375  // invariant if they are not contained in the specified loop.
376  if (Instruction *I = dyn_cast<Instruction>(V))
377    return !L->contains(I->getParent());
378  return true;
379}
380
381const Type *SCEVUnknown::getType() const {
382  return V->getType();
383}
384
385void SCEVUnknown::print(std::ostream &OS) const {
386  WriteAsOperand(OS, V, false);
387}
388
389//===----------------------------------------------------------------------===//
390//                               SCEV Utilities
391//===----------------------------------------------------------------------===//
392
393namespace {
394  /// SCEVComplexityCompare - Return true if the complexity of the LHS is less
395  /// than the complexity of the RHS.  This comparator is used to canonicalize
396  /// expressions.
397  struct VISIBILITY_HIDDEN SCEVComplexityCompare {
398    bool operator()(SCEV *LHS, SCEV *RHS) {
399      return LHS->getSCEVType() < RHS->getSCEVType();
400    }
401  };
402}
403
404/// GroupByComplexity - Given a list of SCEV objects, order them by their
405/// complexity, and group objects of the same complexity together by value.
406/// When this routine is finished, we know that any duplicates in the vector are
407/// consecutive and that complexity is monotonically increasing.
408///
409/// Note that we go take special precautions to ensure that we get determinstic
410/// results from this routine.  In other words, we don't want the results of
411/// this to depend on where the addresses of various SCEV objects happened to
412/// land in memory.
413///
414static void GroupByComplexity(std::vector<SCEVHandle> &Ops) {
415  if (Ops.size() < 2) return;  // Noop
416  if (Ops.size() == 2) {
417    // This is the common case, which also happens to be trivially simple.
418    // Special case it.
419    if (Ops[0]->getSCEVType() > Ops[1]->getSCEVType())
420      std::swap(Ops[0], Ops[1]);
421    return;
422  }
423
424  // Do the rough sort by complexity.
425  std::sort(Ops.begin(), Ops.end(), SCEVComplexityCompare());
426
427  // Now that we are sorted by complexity, group elements of the same
428  // complexity.  Note that this is, at worst, N^2, but the vector is likely to
429  // be extremely short in practice.  Note that we take this approach because we
430  // do not want to depend on the addresses of the objects we are grouping.
431  for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
432    SCEV *S = Ops[i];
433    unsigned Complexity = S->getSCEVType();
434
435    // If there are any objects of the same complexity and same value as this
436    // one, group them.
437    for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
438      if (Ops[j] == S) { // Found a duplicate.
439        // Move it to immediately after i'th element.
440        std::swap(Ops[i+1], Ops[j]);
441        ++i;   // no need to rescan it.
442        if (i == e-2) return;  // Done!
443      }
444    }
445  }
446}
447
448
449
450//===----------------------------------------------------------------------===//
451//                      Simple SCEV method implementations
452//===----------------------------------------------------------------------===//
453
454/// getIntegerSCEV - Given an integer or FP type, create a constant for the
455/// specified signed integer value and return a SCEV for the constant.
456SCEVHandle SCEVUnknown::getIntegerSCEV(int Val, const Type *Ty) {
457  Constant *C;
458  if (Val == 0)
459    C = Constant::getNullValue(Ty);
460  else if (Ty->isFloatingPoint())
461    C = ConstantFP::get(Ty, Val);
462  else if (Ty->isSigned())
463    C = ConstantSInt::get(Ty, Val);
464  else {
465    C = ConstantSInt::get(Ty->getSignedVersion(), Val);
466    C = ConstantExpr::getCast(C, Ty);
467  }
468  return SCEVUnknown::get(C);
469}
470
471/// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the
472/// input value to the specified type.  If the type must be extended, it is zero
473/// extended.
474static SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty) {
475  const Type *SrcTy = V->getType();
476  assert(SrcTy->isInteger() && Ty->isInteger() &&
477         "Cannot truncate or zero extend with non-integer arguments!");
478  if (SrcTy->getPrimitiveSize() == Ty->getPrimitiveSize())
479    return V;  // No conversion
480  if (SrcTy->getPrimitiveSize() > Ty->getPrimitiveSize())
481    return SCEVTruncateExpr::get(V, Ty);
482  return SCEVZeroExtendExpr::get(V, Ty);
483}
484
485/// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
486///
487SCEVHandle SCEV::getNegativeSCEV(const SCEVHandle &V) {
488  if (SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
489    return SCEVUnknown::get(ConstantExpr::getNeg(VC->getValue()));
490
491  return SCEVMulExpr::get(V, SCEVUnknown::getIntegerSCEV(-1, V->getType()));
492}
493
494/// getMinusSCEV - Return a SCEV corresponding to LHS - RHS.
495///
496SCEVHandle SCEV::getMinusSCEV(const SCEVHandle &LHS, const SCEVHandle &RHS) {
497  // X - Y --> X + -Y
498  return SCEVAddExpr::get(LHS, SCEV::getNegativeSCEV(RHS));
499}
500
501
502/// PartialFact - Compute V!/(V-NumSteps)!
503static SCEVHandle PartialFact(SCEVHandle V, unsigned NumSteps) {
504  // Handle this case efficiently, it is common to have constant iteration
505  // counts while computing loop exit values.
506  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(V)) {
507    uint64_t Val = SC->getValue()->getRawValue();
508    uint64_t Result = 1;
509    for (; NumSteps; --NumSteps)
510      Result *= Val-(NumSteps-1);
511    Constant *Res = ConstantUInt::get(Type::ULongTy, Result);
512    return SCEVUnknown::get(ConstantExpr::getCast(Res, V->getType()));
513  }
514
515  const Type *Ty = V->getType();
516  if (NumSteps == 0)
517    return SCEVUnknown::getIntegerSCEV(1, Ty);
518
519  SCEVHandle Result = V;
520  for (unsigned i = 1; i != NumSteps; ++i)
521    Result = SCEVMulExpr::get(Result, SCEV::getMinusSCEV(V,
522                                          SCEVUnknown::getIntegerSCEV(i, Ty)));
523  return Result;
524}
525
526
527/// evaluateAtIteration - Return the value of this chain of recurrences at
528/// the specified iteration number.  We can evaluate this recurrence by
529/// multiplying each element in the chain by the binomial coefficient
530/// corresponding to it.  In other words, we can evaluate {A,+,B,+,C,+,D} as:
531///
532///   A*choose(It, 0) + B*choose(It, 1) + C*choose(It, 2) + D*choose(It, 3)
533///
534/// FIXME/VERIFY: I don't trust that this is correct in the face of overflow.
535/// Is the binomial equation safe using modular arithmetic??
536///
537SCEVHandle SCEVAddRecExpr::evaluateAtIteration(SCEVHandle It) const {
538  SCEVHandle Result = getStart();
539  int Divisor = 1;
540  const Type *Ty = It->getType();
541  for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
542    SCEVHandle BC = PartialFact(It, i);
543    Divisor *= i;
544    SCEVHandle Val = SCEVSDivExpr::get(SCEVMulExpr::get(BC, getOperand(i)),
545                                       SCEVUnknown::getIntegerSCEV(Divisor,Ty));
546    Result = SCEVAddExpr::get(Result, Val);
547  }
548  return Result;
549}
550
551
552//===----------------------------------------------------------------------===//
553//                    SCEV Expression folder implementations
554//===----------------------------------------------------------------------===//
555
556SCEVHandle SCEVTruncateExpr::get(const SCEVHandle &Op, const Type *Ty) {
557  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
558    return SCEVUnknown::get(ConstantExpr::getCast(SC->getValue(), Ty));
559
560  // If the input value is a chrec scev made out of constants, truncate
561  // all of the constants.
562  if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
563    std::vector<SCEVHandle> Operands;
564    for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
565      // FIXME: This should allow truncation of other expression types!
566      if (isa<SCEVConstant>(AddRec->getOperand(i)))
567        Operands.push_back(get(AddRec->getOperand(i), Ty));
568      else
569        break;
570    if (Operands.size() == AddRec->getNumOperands())
571      return SCEVAddRecExpr::get(Operands, AddRec->getLoop());
572  }
573
574  SCEVTruncateExpr *&Result = SCEVTruncates[std::make_pair(Op, Ty)];
575  if (Result == 0) Result = new SCEVTruncateExpr(Op, Ty);
576  return Result;
577}
578
579SCEVHandle SCEVZeroExtendExpr::get(const SCEVHandle &Op, const Type *Ty) {
580  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
581    return SCEVUnknown::get(ConstantExpr::getCast(SC->getValue(), Ty));
582
583  // FIXME: If the input value is a chrec scev, and we can prove that the value
584  // did not overflow the old, smaller, value, we can zero extend all of the
585  // operands (often constants).  This would allow analysis of something like
586  // this:  for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
587
588  SCEVZeroExtendExpr *&Result = SCEVZeroExtends[std::make_pair(Op, Ty)];
589  if (Result == 0) Result = new SCEVZeroExtendExpr(Op, Ty);
590  return Result;
591}
592
593// get - Get a canonical add expression, or something simpler if possible.
594SCEVHandle SCEVAddExpr::get(std::vector<SCEVHandle> &Ops) {
595  assert(!Ops.empty() && "Cannot get empty add!");
596  if (Ops.size() == 1) return Ops[0];
597
598  // Sort by complexity, this groups all similar expression types together.
599  GroupByComplexity(Ops);
600
601  // If there are any constants, fold them together.
602  unsigned Idx = 0;
603  if (SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
604    ++Idx;
605    assert(Idx < Ops.size());
606    while (SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
607      // We found two constants, fold them together!
608      Constant *Fold = ConstantExpr::getAdd(LHSC->getValue(), RHSC->getValue());
609      if (ConstantInt *CI = dyn_cast<ConstantInt>(Fold)) {
610        Ops[0] = SCEVConstant::get(CI);
611        Ops.erase(Ops.begin()+1);  // Erase the folded element
612        if (Ops.size() == 1) return Ops[0];
613        LHSC = cast<SCEVConstant>(Ops[0]);
614      } else {
615        // If we couldn't fold the expression, move to the next constant.  Note
616        // that this is impossible to happen in practice because we always
617        // constant fold constant ints to constant ints.
618        ++Idx;
619      }
620    }
621
622    // If we are left with a constant zero being added, strip it off.
623    if (cast<SCEVConstant>(Ops[0])->getValue()->isNullValue()) {
624      Ops.erase(Ops.begin());
625      --Idx;
626    }
627  }
628
629  if (Ops.size() == 1) return Ops[0];
630
631  // Okay, check to see if the same value occurs in the operand list twice.  If
632  // so, merge them together into an multiply expression.  Since we sorted the
633  // list, these values are required to be adjacent.
634  const Type *Ty = Ops[0]->getType();
635  for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
636    if (Ops[i] == Ops[i+1]) {      //  X + Y + Y  -->  X + Y*2
637      // Found a match, merge the two values into a multiply, and add any
638      // remaining values to the result.
639      SCEVHandle Two = SCEVUnknown::getIntegerSCEV(2, Ty);
640      SCEVHandle Mul = SCEVMulExpr::get(Ops[i], Two);
641      if (Ops.size() == 2)
642        return Mul;
643      Ops.erase(Ops.begin()+i, Ops.begin()+i+2);
644      Ops.push_back(Mul);
645      return SCEVAddExpr::get(Ops);
646    }
647
648  // Okay, now we know the first non-constant operand.  If there are add
649  // operands they would be next.
650  if (Idx < Ops.size()) {
651    bool DeletedAdd = false;
652    while (SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
653      // If we have an add, expand the add operands onto the end of the operands
654      // list.
655      Ops.insert(Ops.end(), Add->op_begin(), Add->op_end());
656      Ops.erase(Ops.begin()+Idx);
657      DeletedAdd = true;
658    }
659
660    // If we deleted at least one add, we added operands to the end of the list,
661    // and they are not necessarily sorted.  Recurse to resort and resimplify
662    // any operands we just aquired.
663    if (DeletedAdd)
664      return get(Ops);
665  }
666
667  // Skip over the add expression until we get to a multiply.
668  while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
669    ++Idx;
670
671  // If we are adding something to a multiply expression, make sure the
672  // something is not already an operand of the multiply.  If so, merge it into
673  // the multiply.
674  for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
675    SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
676    for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
677      SCEV *MulOpSCEV = Mul->getOperand(MulOp);
678      for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
679        if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(MulOpSCEV)) {
680          // Fold W + X + (X * Y * Z)  -->  W + (X * ((Y*Z)+1))
681          SCEVHandle InnerMul = Mul->getOperand(MulOp == 0);
682          if (Mul->getNumOperands() != 2) {
683            // If the multiply has more than two operands, we must get the
684            // Y*Z term.
685            std::vector<SCEVHandle> MulOps(Mul->op_begin(), Mul->op_end());
686            MulOps.erase(MulOps.begin()+MulOp);
687            InnerMul = SCEVMulExpr::get(MulOps);
688          }
689          SCEVHandle One = SCEVUnknown::getIntegerSCEV(1, Ty);
690          SCEVHandle AddOne = SCEVAddExpr::get(InnerMul, One);
691          SCEVHandle OuterMul = SCEVMulExpr::get(AddOne, Ops[AddOp]);
692          if (Ops.size() == 2) return OuterMul;
693          if (AddOp < Idx) {
694            Ops.erase(Ops.begin()+AddOp);
695            Ops.erase(Ops.begin()+Idx-1);
696          } else {
697            Ops.erase(Ops.begin()+Idx);
698            Ops.erase(Ops.begin()+AddOp-1);
699          }
700          Ops.push_back(OuterMul);
701          return SCEVAddExpr::get(Ops);
702        }
703
704      // Check this multiply against other multiplies being added together.
705      for (unsigned OtherMulIdx = Idx+1;
706           OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
707           ++OtherMulIdx) {
708        SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
709        // If MulOp occurs in OtherMul, we can fold the two multiplies
710        // together.
711        for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
712             OMulOp != e; ++OMulOp)
713          if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
714            // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
715            SCEVHandle InnerMul1 = Mul->getOperand(MulOp == 0);
716            if (Mul->getNumOperands() != 2) {
717              std::vector<SCEVHandle> MulOps(Mul->op_begin(), Mul->op_end());
718              MulOps.erase(MulOps.begin()+MulOp);
719              InnerMul1 = SCEVMulExpr::get(MulOps);
720            }
721            SCEVHandle InnerMul2 = OtherMul->getOperand(OMulOp == 0);
722            if (OtherMul->getNumOperands() != 2) {
723              std::vector<SCEVHandle> MulOps(OtherMul->op_begin(),
724                                             OtherMul->op_end());
725              MulOps.erase(MulOps.begin()+OMulOp);
726              InnerMul2 = SCEVMulExpr::get(MulOps);
727            }
728            SCEVHandle InnerMulSum = SCEVAddExpr::get(InnerMul1,InnerMul2);
729            SCEVHandle OuterMul = SCEVMulExpr::get(MulOpSCEV, InnerMulSum);
730            if (Ops.size() == 2) return OuterMul;
731            Ops.erase(Ops.begin()+Idx);
732            Ops.erase(Ops.begin()+OtherMulIdx-1);
733            Ops.push_back(OuterMul);
734            return SCEVAddExpr::get(Ops);
735          }
736      }
737    }
738  }
739
740  // If there are any add recurrences in the operands list, see if any other
741  // added values are loop invariant.  If so, we can fold them into the
742  // recurrence.
743  while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
744    ++Idx;
745
746  // Scan over all recurrences, trying to fold loop invariants into them.
747  for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
748    // Scan all of the other operands to this add and add them to the vector if
749    // they are loop invariant w.r.t. the recurrence.
750    std::vector<SCEVHandle> LIOps;
751    SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
752    for (unsigned i = 0, e = Ops.size(); i != e; ++i)
753      if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
754        LIOps.push_back(Ops[i]);
755        Ops.erase(Ops.begin()+i);
756        --i; --e;
757      }
758
759    // If we found some loop invariants, fold them into the recurrence.
760    if (!LIOps.empty()) {
761      //  NLI + LI + { Start,+,Step}  -->  NLI + { LI+Start,+,Step }
762      LIOps.push_back(AddRec->getStart());
763
764      std::vector<SCEVHandle> AddRecOps(AddRec->op_begin(), AddRec->op_end());
765      AddRecOps[0] = SCEVAddExpr::get(LIOps);
766
767      SCEVHandle NewRec = SCEVAddRecExpr::get(AddRecOps, AddRec->getLoop());
768      // If all of the other operands were loop invariant, we are done.
769      if (Ops.size() == 1) return NewRec;
770
771      // Otherwise, add the folded AddRec by the non-liv parts.
772      for (unsigned i = 0;; ++i)
773        if (Ops[i] == AddRec) {
774          Ops[i] = NewRec;
775          break;
776        }
777      return SCEVAddExpr::get(Ops);
778    }
779
780    // Okay, if there weren't any loop invariants to be folded, check to see if
781    // there are multiple AddRec's with the same loop induction variable being
782    // added together.  If so, we can fold them.
783    for (unsigned OtherIdx = Idx+1;
784         OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
785      if (OtherIdx != Idx) {
786        SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
787        if (AddRec->getLoop() == OtherAddRec->getLoop()) {
788          // Other + {A,+,B} + {C,+,D}  -->  Other + {A+C,+,B+D}
789          std::vector<SCEVHandle> NewOps(AddRec->op_begin(), AddRec->op_end());
790          for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) {
791            if (i >= NewOps.size()) {
792              NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i,
793                            OtherAddRec->op_end());
794              break;
795            }
796            NewOps[i] = SCEVAddExpr::get(NewOps[i], OtherAddRec->getOperand(i));
797          }
798          SCEVHandle NewAddRec = SCEVAddRecExpr::get(NewOps, AddRec->getLoop());
799
800          if (Ops.size() == 2) return NewAddRec;
801
802          Ops.erase(Ops.begin()+Idx);
803          Ops.erase(Ops.begin()+OtherIdx-1);
804          Ops.push_back(NewAddRec);
805          return SCEVAddExpr::get(Ops);
806        }
807      }
808
809    // Otherwise couldn't fold anything into this recurrence.  Move onto the
810    // next one.
811  }
812
813  // Okay, it looks like we really DO need an add expr.  Check to see if we
814  // already have one, otherwise create a new one.
815  std::vector<SCEV*> SCEVOps(Ops.begin(), Ops.end());
816  SCEVCommutativeExpr *&Result = SCEVCommExprs[std::make_pair(scAddExpr,
817                                                              SCEVOps)];
818  if (Result == 0) Result = new SCEVAddExpr(Ops);
819  return Result;
820}
821
822
823SCEVHandle SCEVMulExpr::get(std::vector<SCEVHandle> &Ops) {
824  assert(!Ops.empty() && "Cannot get empty mul!");
825
826  // Sort by complexity, this groups all similar expression types together.
827  GroupByComplexity(Ops);
828
829  // If there are any constants, fold them together.
830  unsigned Idx = 0;
831  if (SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
832
833    // C1*(C2+V) -> C1*C2 + C1*V
834    if (Ops.size() == 2)
835      if (SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
836        if (Add->getNumOperands() == 2 &&
837            isa<SCEVConstant>(Add->getOperand(0)))
838          return SCEVAddExpr::get(SCEVMulExpr::get(LHSC, Add->getOperand(0)),
839                                  SCEVMulExpr::get(LHSC, Add->getOperand(1)));
840
841
842    ++Idx;
843    while (SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
844      // We found two constants, fold them together!
845      Constant *Fold = ConstantExpr::getMul(LHSC->getValue(), RHSC->getValue());
846      if (ConstantInt *CI = dyn_cast<ConstantInt>(Fold)) {
847        Ops[0] = SCEVConstant::get(CI);
848        Ops.erase(Ops.begin()+1);  // Erase the folded element
849        if (Ops.size() == 1) return Ops[0];
850        LHSC = cast<SCEVConstant>(Ops[0]);
851      } else {
852        // If we couldn't fold the expression, move to the next constant.  Note
853        // that this is impossible to happen in practice because we always
854        // constant fold constant ints to constant ints.
855        ++Idx;
856      }
857    }
858
859    // If we are left with a constant one being multiplied, strip it off.
860    if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) {
861      Ops.erase(Ops.begin());
862      --Idx;
863    } else if (cast<SCEVConstant>(Ops[0])->getValue()->isNullValue()) {
864      // If we have a multiply of zero, it will always be zero.
865      return Ops[0];
866    }
867  }
868
869  // Skip over the add expression until we get to a multiply.
870  while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
871    ++Idx;
872
873  if (Ops.size() == 1)
874    return Ops[0];
875
876  // If there are mul operands inline them all into this expression.
877  if (Idx < Ops.size()) {
878    bool DeletedMul = false;
879    while (SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
880      // If we have an mul, expand the mul operands onto the end of the operands
881      // list.
882      Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end());
883      Ops.erase(Ops.begin()+Idx);
884      DeletedMul = true;
885    }
886
887    // If we deleted at least one mul, we added operands to the end of the list,
888    // and they are not necessarily sorted.  Recurse to resort and resimplify
889    // any operands we just aquired.
890    if (DeletedMul)
891      return get(Ops);
892  }
893
894  // If there are any add recurrences in the operands list, see if any other
895  // added values are loop invariant.  If so, we can fold them into the
896  // recurrence.
897  while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
898    ++Idx;
899
900  // Scan over all recurrences, trying to fold loop invariants into them.
901  for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
902    // Scan all of the other operands to this mul and add them to the vector if
903    // they are loop invariant w.r.t. the recurrence.
904    std::vector<SCEVHandle> LIOps;
905    SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
906    for (unsigned i = 0, e = Ops.size(); i != e; ++i)
907      if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
908        LIOps.push_back(Ops[i]);
909        Ops.erase(Ops.begin()+i);
910        --i; --e;
911      }
912
913    // If we found some loop invariants, fold them into the recurrence.
914    if (!LIOps.empty()) {
915      //  NLI * LI * { Start,+,Step}  -->  NLI * { LI*Start,+,LI*Step }
916      std::vector<SCEVHandle> NewOps;
917      NewOps.reserve(AddRec->getNumOperands());
918      if (LIOps.size() == 1) {
919        SCEV *Scale = LIOps[0];
920        for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
921          NewOps.push_back(SCEVMulExpr::get(Scale, AddRec->getOperand(i)));
922      } else {
923        for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
924          std::vector<SCEVHandle> MulOps(LIOps);
925          MulOps.push_back(AddRec->getOperand(i));
926          NewOps.push_back(SCEVMulExpr::get(MulOps));
927        }
928      }
929
930      SCEVHandle NewRec = SCEVAddRecExpr::get(NewOps, AddRec->getLoop());
931
932      // If all of the other operands were loop invariant, we are done.
933      if (Ops.size() == 1) return NewRec;
934
935      // Otherwise, multiply the folded AddRec by the non-liv parts.
936      for (unsigned i = 0;; ++i)
937        if (Ops[i] == AddRec) {
938          Ops[i] = NewRec;
939          break;
940        }
941      return SCEVMulExpr::get(Ops);
942    }
943
944    // Okay, if there weren't any loop invariants to be folded, check to see if
945    // there are multiple AddRec's with the same loop induction variable being
946    // multiplied together.  If so, we can fold them.
947    for (unsigned OtherIdx = Idx+1;
948         OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
949      if (OtherIdx != Idx) {
950        SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
951        if (AddRec->getLoop() == OtherAddRec->getLoop()) {
952          // F * G  -->  {A,+,B} * {C,+,D}  -->  {A*C,+,F*D + G*B + B*D}
953          SCEVAddRecExpr *F = AddRec, *G = OtherAddRec;
954          SCEVHandle NewStart = SCEVMulExpr::get(F->getStart(),
955                                                 G->getStart());
956          SCEVHandle B = F->getStepRecurrence();
957          SCEVHandle D = G->getStepRecurrence();
958          SCEVHandle NewStep = SCEVAddExpr::get(SCEVMulExpr::get(F, D),
959                                                SCEVMulExpr::get(G, B),
960                                                SCEVMulExpr::get(B, D));
961          SCEVHandle NewAddRec = SCEVAddRecExpr::get(NewStart, NewStep,
962                                                     F->getLoop());
963          if (Ops.size() == 2) return NewAddRec;
964
965          Ops.erase(Ops.begin()+Idx);
966          Ops.erase(Ops.begin()+OtherIdx-1);
967          Ops.push_back(NewAddRec);
968          return SCEVMulExpr::get(Ops);
969        }
970      }
971
972    // Otherwise couldn't fold anything into this recurrence.  Move onto the
973    // next one.
974  }
975
976  // Okay, it looks like we really DO need an mul expr.  Check to see if we
977  // already have one, otherwise create a new one.
978  std::vector<SCEV*> SCEVOps(Ops.begin(), Ops.end());
979  SCEVCommutativeExpr *&Result = SCEVCommExprs[std::make_pair(scMulExpr,
980                                                              SCEVOps)];
981  if (Result == 0)
982    Result = new SCEVMulExpr(Ops);
983  return Result;
984}
985
986SCEVHandle SCEVSDivExpr::get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
987  if (SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
988    if (RHSC->getValue()->equalsInt(1))
989      return LHS;                            // X /s 1 --> x
990    if (RHSC->getValue()->isAllOnesValue())
991      return SCEV::getNegativeSCEV(LHS);           // X /s -1  -->  -x
992
993    if (SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
994      Constant *LHSCV = LHSC->getValue();
995      Constant *RHSCV = RHSC->getValue();
996      if (LHSCV->getType()->isUnsigned())
997        LHSCV = ConstantExpr::getCast(LHSCV,
998                                      LHSCV->getType()->getSignedVersion());
999      if (RHSCV->getType()->isUnsigned())
1000        RHSCV = ConstantExpr::getCast(RHSCV, LHSCV->getType());
1001      return SCEVUnknown::get(ConstantExpr::getDiv(LHSCV, RHSCV));
1002    }
1003  }
1004
1005  // FIXME: implement folding of (X*4)/4 when we know X*4 doesn't overflow.
1006
1007  SCEVSDivExpr *&Result = SCEVSDivs[std::make_pair(LHS, RHS)];
1008  if (Result == 0) Result = new SCEVSDivExpr(LHS, RHS);
1009  return Result;
1010}
1011
1012
1013/// SCEVAddRecExpr::get - Get a add recurrence expression for the
1014/// specified loop.  Simplify the expression as much as possible.
1015SCEVHandle SCEVAddRecExpr::get(const SCEVHandle &Start,
1016                               const SCEVHandle &Step, const Loop *L) {
1017  std::vector<SCEVHandle> Operands;
1018  Operands.push_back(Start);
1019  if (SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
1020    if (StepChrec->getLoop() == L) {
1021      Operands.insert(Operands.end(), StepChrec->op_begin(),
1022                      StepChrec->op_end());
1023      return get(Operands, L);
1024    }
1025
1026  Operands.push_back(Step);
1027  return get(Operands, L);
1028}
1029
1030/// SCEVAddRecExpr::get - Get a add recurrence expression for the
1031/// specified loop.  Simplify the expression as much as possible.
1032SCEVHandle SCEVAddRecExpr::get(std::vector<SCEVHandle> &Operands,
1033                               const Loop *L) {
1034  if (Operands.size() == 1) return Operands[0];
1035
1036  if (SCEVConstant *StepC = dyn_cast<SCEVConstant>(Operands.back()))
1037    if (StepC->getValue()->isNullValue()) {
1038      Operands.pop_back();
1039      return get(Operands, L);             // { X,+,0 }  -->  X
1040    }
1041
1042  SCEVAddRecExpr *&Result =
1043    SCEVAddRecExprs[std::make_pair(L, std::vector<SCEV*>(Operands.begin(),
1044                                                         Operands.end()))];
1045  if (Result == 0) Result = new SCEVAddRecExpr(Operands, L);
1046  return Result;
1047}
1048
1049SCEVHandle SCEVUnknown::get(Value *V) {
1050  if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
1051    return SCEVConstant::get(CI);
1052  SCEVUnknown *&Result = SCEVUnknowns[V];
1053  if (Result == 0) Result = new SCEVUnknown(V);
1054  return Result;
1055}
1056
1057
1058//===----------------------------------------------------------------------===//
1059//             ScalarEvolutionsImpl Definition and Implementation
1060//===----------------------------------------------------------------------===//
1061//
1062/// ScalarEvolutionsImpl - This class implements the main driver for the scalar
1063/// evolution code.
1064///
1065namespace {
1066  struct VISIBILITY_HIDDEN ScalarEvolutionsImpl {
1067    /// F - The function we are analyzing.
1068    ///
1069    Function &F;
1070
1071    /// LI - The loop information for the function we are currently analyzing.
1072    ///
1073    LoopInfo &LI;
1074
1075    /// UnknownValue - This SCEV is used to represent unknown trip counts and
1076    /// things.
1077    SCEVHandle UnknownValue;
1078
1079    /// Scalars - This is a cache of the scalars we have analyzed so far.
1080    ///
1081    std::map<Value*, SCEVHandle> Scalars;
1082
1083    /// IterationCounts - Cache the iteration count of the loops for this
1084    /// function as they are computed.
1085    std::map<const Loop*, SCEVHandle> IterationCounts;
1086
1087    /// ConstantEvolutionLoopExitValue - This map contains entries for all of
1088    /// the PHI instructions that we attempt to compute constant evolutions for.
1089    /// This allows us to avoid potentially expensive recomputation of these
1090    /// properties.  An instruction maps to null if we are unable to compute its
1091    /// exit value.
1092    std::map<PHINode*, Constant*> ConstantEvolutionLoopExitValue;
1093
1094  public:
1095    ScalarEvolutionsImpl(Function &f, LoopInfo &li)
1096      : F(f), LI(li), UnknownValue(new SCEVCouldNotCompute()) {}
1097
1098    /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the
1099    /// expression and create a new one.
1100    SCEVHandle getSCEV(Value *V);
1101
1102    /// hasSCEV - Return true if the SCEV for this value has already been
1103    /// computed.
1104    bool hasSCEV(Value *V) const {
1105      return Scalars.count(V);
1106    }
1107
1108    /// setSCEV - Insert the specified SCEV into the map of current SCEVs for
1109    /// the specified value.
1110    void setSCEV(Value *V, const SCEVHandle &H) {
1111      bool isNew = Scalars.insert(std::make_pair(V, H)).second;
1112      assert(isNew && "This entry already existed!");
1113    }
1114
1115
1116    /// getSCEVAtScope - Compute the value of the specified expression within
1117    /// the indicated loop (which may be null to indicate in no loop).  If the
1118    /// expression cannot be evaluated, return UnknownValue itself.
1119    SCEVHandle getSCEVAtScope(SCEV *V, const Loop *L);
1120
1121
1122    /// hasLoopInvariantIterationCount - Return true if the specified loop has
1123    /// an analyzable loop-invariant iteration count.
1124    bool hasLoopInvariantIterationCount(const Loop *L);
1125
1126    /// getIterationCount - If the specified loop has a predictable iteration
1127    /// count, return it.  Note that it is not valid to call this method on a
1128    /// loop without a loop-invariant iteration count.
1129    SCEVHandle getIterationCount(const Loop *L);
1130
1131    /// deleteInstructionFromRecords - This method should be called by the
1132    /// client before it removes an instruction from the program, to make sure
1133    /// that no dangling references are left around.
1134    void deleteInstructionFromRecords(Instruction *I);
1135
1136  private:
1137    /// createSCEV - We know that there is no SCEV for the specified value.
1138    /// Analyze the expression.
1139    SCEVHandle createSCEV(Value *V);
1140    SCEVHandle createNodeForCast(CastInst *CI);
1141
1142    /// createNodeForPHI - Provide the special handling we need to analyze PHI
1143    /// SCEVs.
1144    SCEVHandle createNodeForPHI(PHINode *PN);
1145
1146    /// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value
1147    /// for the specified instruction and replaces any references to the
1148    /// symbolic value SymName with the specified value.  This is used during
1149    /// PHI resolution.
1150    void ReplaceSymbolicValueWithConcrete(Instruction *I,
1151                                          const SCEVHandle &SymName,
1152                                          const SCEVHandle &NewVal);
1153
1154    /// ComputeIterationCount - Compute the number of times the specified loop
1155    /// will iterate.
1156    SCEVHandle ComputeIterationCount(const Loop *L);
1157
1158    /// ComputeLoadConstantCompareIterationCount - Given an exit condition of
1159    /// 'setcc load X, cst', try to se if we can compute the trip count.
1160    SCEVHandle ComputeLoadConstantCompareIterationCount(LoadInst *LI,
1161                                                        Constant *RHS,
1162                                                        const Loop *L,
1163                                                        unsigned SetCCOpcode);
1164
1165    /// ComputeIterationCountExhaustively - If the trip is known to execute a
1166    /// constant number of times (the condition evolves only from constants),
1167    /// try to evaluate a few iterations of the loop until we get the exit
1168    /// condition gets a value of ExitWhen (true or false).  If we cannot
1169    /// evaluate the trip count of the loop, return UnknownValue.
1170    SCEVHandle ComputeIterationCountExhaustively(const Loop *L, Value *Cond,
1171                                                 bool ExitWhen);
1172
1173    /// HowFarToZero - Return the number of times a backedge comparing the
1174    /// specified value to zero will execute.  If not computable, return
1175    /// UnknownValue.
1176    SCEVHandle HowFarToZero(SCEV *V, const Loop *L);
1177
1178    /// HowFarToNonZero - Return the number of times a backedge checking the
1179    /// specified value for nonzero will execute.  If not computable, return
1180    /// UnknownValue.
1181    SCEVHandle HowFarToNonZero(SCEV *V, const Loop *L);
1182
1183    /// HowManyLessThans - Return the number of times a backedge containing the
1184    /// specified less-than comparison will execute.  If not computable, return
1185    /// UnknownValue.
1186    SCEVHandle HowManyLessThans(SCEV *LHS, SCEV *RHS, const Loop *L);
1187
1188    /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
1189    /// in the header of its containing loop, we know the loop executes a
1190    /// constant number of times, and the PHI node is just a recurrence
1191    /// involving constants, fold it.
1192    Constant *getConstantEvolutionLoopExitValue(PHINode *PN, uint64_t Its,
1193                                                const Loop *L);
1194  };
1195}
1196
1197//===----------------------------------------------------------------------===//
1198//            Basic SCEV Analysis and PHI Idiom Recognition Code
1199//
1200
1201/// deleteInstructionFromRecords - This method should be called by the
1202/// client before it removes an instruction from the program, to make sure
1203/// that no dangling references are left around.
1204void ScalarEvolutionsImpl::deleteInstructionFromRecords(Instruction *I) {
1205  Scalars.erase(I);
1206  if (PHINode *PN = dyn_cast<PHINode>(I))
1207    ConstantEvolutionLoopExitValue.erase(PN);
1208}
1209
1210
1211/// getSCEV - Return an existing SCEV if it exists, otherwise analyze the
1212/// expression and create a new one.
1213SCEVHandle ScalarEvolutionsImpl::getSCEV(Value *V) {
1214  assert(V->getType() != Type::VoidTy && "Can't analyze void expressions!");
1215
1216  std::map<Value*, SCEVHandle>::iterator I = Scalars.find(V);
1217  if (I != Scalars.end()) return I->second;
1218  SCEVHandle S = createSCEV(V);
1219  Scalars.insert(std::make_pair(V, S));
1220  return S;
1221}
1222
1223/// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value for
1224/// the specified instruction and replaces any references to the symbolic value
1225/// SymName with the specified value.  This is used during PHI resolution.
1226void ScalarEvolutionsImpl::
1227ReplaceSymbolicValueWithConcrete(Instruction *I, const SCEVHandle &SymName,
1228                                 const SCEVHandle &NewVal) {
1229  std::map<Value*, SCEVHandle>::iterator SI = Scalars.find(I);
1230  if (SI == Scalars.end()) return;
1231
1232  SCEVHandle NV =
1233    SI->second->replaceSymbolicValuesWithConcrete(SymName, NewVal);
1234  if (NV == SI->second) return;  // No change.
1235
1236  SI->second = NV;       // Update the scalars map!
1237
1238  // Any instruction values that use this instruction might also need to be
1239  // updated!
1240  for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
1241       UI != E; ++UI)
1242    ReplaceSymbolicValueWithConcrete(cast<Instruction>(*UI), SymName, NewVal);
1243}
1244
1245/// createNodeForPHI - PHI nodes have two cases.  Either the PHI node exists in
1246/// a loop header, making it a potential recurrence, or it doesn't.
1247///
1248SCEVHandle ScalarEvolutionsImpl::createNodeForPHI(PHINode *PN) {
1249  if (PN->getNumIncomingValues() == 2)  // The loops have been canonicalized.
1250    if (const Loop *L = LI.getLoopFor(PN->getParent()))
1251      if (L->getHeader() == PN->getParent()) {
1252        // If it lives in the loop header, it has two incoming values, one
1253        // from outside the loop, and one from inside.
1254        unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0));
1255        unsigned BackEdge     = IncomingEdge^1;
1256
1257        // While we are analyzing this PHI node, handle its value symbolically.
1258        SCEVHandle SymbolicName = SCEVUnknown::get(PN);
1259        assert(Scalars.find(PN) == Scalars.end() &&
1260               "PHI node already processed?");
1261        Scalars.insert(std::make_pair(PN, SymbolicName));
1262
1263        // Using this symbolic name for the PHI, analyze the value coming around
1264        // the back-edge.
1265        SCEVHandle BEValue = getSCEV(PN->getIncomingValue(BackEdge));
1266
1267        // NOTE: If BEValue is loop invariant, we know that the PHI node just
1268        // has a special value for the first iteration of the loop.
1269
1270        // If the value coming around the backedge is an add with the symbolic
1271        // value we just inserted, then we found a simple induction variable!
1272        if (SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
1273          // If there is a single occurrence of the symbolic value, replace it
1274          // with a recurrence.
1275          unsigned FoundIndex = Add->getNumOperands();
1276          for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
1277            if (Add->getOperand(i) == SymbolicName)
1278              if (FoundIndex == e) {
1279                FoundIndex = i;
1280                break;
1281              }
1282
1283          if (FoundIndex != Add->getNumOperands()) {
1284            // Create an add with everything but the specified operand.
1285            std::vector<SCEVHandle> Ops;
1286            for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
1287              if (i != FoundIndex)
1288                Ops.push_back(Add->getOperand(i));
1289            SCEVHandle Accum = SCEVAddExpr::get(Ops);
1290
1291            // This is not a valid addrec if the step amount is varying each
1292            // loop iteration, but is not itself an addrec in this loop.
1293            if (Accum->isLoopInvariant(L) ||
1294                (isa<SCEVAddRecExpr>(Accum) &&
1295                 cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
1296              SCEVHandle StartVal = getSCEV(PN->getIncomingValue(IncomingEdge));
1297              SCEVHandle PHISCEV  = SCEVAddRecExpr::get(StartVal, Accum, L);
1298
1299              // Okay, for the entire analysis of this edge we assumed the PHI
1300              // to be symbolic.  We now need to go back and update all of the
1301              // entries for the scalars that use the PHI (except for the PHI
1302              // itself) to use the new analyzed value instead of the "symbolic"
1303              // value.
1304              ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV);
1305              return PHISCEV;
1306            }
1307          }
1308        } else if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(BEValue)) {
1309          // Otherwise, this could be a loop like this:
1310          //     i = 0;  for (j = 1; ..; ++j) { ....  i = j; }
1311          // In this case, j = {1,+,1}  and BEValue is j.
1312          // Because the other in-value of i (0) fits the evolution of BEValue
1313          // i really is an addrec evolution.
1314          if (AddRec->getLoop() == L && AddRec->isAffine()) {
1315            SCEVHandle StartVal = getSCEV(PN->getIncomingValue(IncomingEdge));
1316
1317            // If StartVal = j.start - j.stride, we can use StartVal as the
1318            // initial step of the addrec evolution.
1319            if (StartVal == SCEV::getMinusSCEV(AddRec->getOperand(0),
1320                                               AddRec->getOperand(1))) {
1321              SCEVHandle PHISCEV =
1322                 SCEVAddRecExpr::get(StartVal, AddRec->getOperand(1), L);
1323
1324              // Okay, for the entire analysis of this edge we assumed the PHI
1325              // to be symbolic.  We now need to go back and update all of the
1326              // entries for the scalars that use the PHI (except for the PHI
1327              // itself) to use the new analyzed value instead of the "symbolic"
1328              // value.
1329              ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV);
1330              return PHISCEV;
1331            }
1332          }
1333        }
1334
1335        return SymbolicName;
1336      }
1337
1338  // If it's not a loop phi, we can't handle it yet.
1339  return SCEVUnknown::get(PN);
1340}
1341
1342/// createNodeForCast - Handle the various forms of casts that we support.
1343///
1344SCEVHandle ScalarEvolutionsImpl::createNodeForCast(CastInst *CI) {
1345  const Type *SrcTy = CI->getOperand(0)->getType();
1346  const Type *DestTy = CI->getType();
1347
1348  // If this is a noop cast (ie, conversion from int to uint), ignore it.
1349  if (SrcTy->isLosslesslyConvertibleTo(DestTy))
1350    return getSCEV(CI->getOperand(0));
1351
1352  if (SrcTy->isInteger() && DestTy->isInteger()) {
1353    // Otherwise, if this is a truncating integer cast, we can represent this
1354    // cast.
1355    if (SrcTy->getPrimitiveSize() > DestTy->getPrimitiveSize())
1356      return SCEVTruncateExpr::get(getSCEV(CI->getOperand(0)),
1357                                   CI->getType()->getUnsignedVersion());
1358    if (SrcTy->isUnsigned() &&
1359        SrcTy->getPrimitiveSize() > DestTy->getPrimitiveSize())
1360      return SCEVZeroExtendExpr::get(getSCEV(CI->getOperand(0)),
1361                                     CI->getType()->getUnsignedVersion());
1362  }
1363
1364  // If this is an sign or zero extending cast and we can prove that the value
1365  // will never overflow, we could do similar transformations.
1366
1367  // Otherwise, we can't handle this cast!
1368  return SCEVUnknown::get(CI);
1369}
1370
1371
1372/// createSCEV - We know that there is no SCEV for the specified value.
1373/// Analyze the expression.
1374///
1375SCEVHandle ScalarEvolutionsImpl::createSCEV(Value *V) {
1376  if (Instruction *I = dyn_cast<Instruction>(V)) {
1377    switch (I->getOpcode()) {
1378    case Instruction::Add:
1379      return SCEVAddExpr::get(getSCEV(I->getOperand(0)),
1380                              getSCEV(I->getOperand(1)));
1381    case Instruction::Mul:
1382      return SCEVMulExpr::get(getSCEV(I->getOperand(0)),
1383                              getSCEV(I->getOperand(1)));
1384    case Instruction::Div:
1385      if (V->getType()->isInteger() && V->getType()->isSigned())
1386        return SCEVSDivExpr::get(getSCEV(I->getOperand(0)),
1387                                 getSCEV(I->getOperand(1)));
1388      break;
1389
1390    case Instruction::Sub:
1391      return SCEV::getMinusSCEV(getSCEV(I->getOperand(0)),
1392                                getSCEV(I->getOperand(1)));
1393
1394    case Instruction::Shl:
1395      // Turn shift left of a constant amount into a multiply.
1396      if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1397        Constant *X = ConstantInt::get(V->getType(), 1);
1398        X = ConstantExpr::getShl(X, SA);
1399        return SCEVMulExpr::get(getSCEV(I->getOperand(0)), getSCEV(X));
1400      }
1401      break;
1402
1403    case Instruction::Cast:
1404      return createNodeForCast(cast<CastInst>(I));
1405
1406    case Instruction::PHI:
1407      return createNodeForPHI(cast<PHINode>(I));
1408
1409    default: // We cannot analyze this expression.
1410      break;
1411    }
1412  }
1413
1414  return SCEVUnknown::get(V);
1415}
1416
1417
1418
1419//===----------------------------------------------------------------------===//
1420//                   Iteration Count Computation Code
1421//
1422
1423/// getIterationCount - If the specified loop has a predictable iteration
1424/// count, return it.  Note that it is not valid to call this method on a
1425/// loop without a loop-invariant iteration count.
1426SCEVHandle ScalarEvolutionsImpl::getIterationCount(const Loop *L) {
1427  std::map<const Loop*, SCEVHandle>::iterator I = IterationCounts.find(L);
1428  if (I == IterationCounts.end()) {
1429    SCEVHandle ItCount = ComputeIterationCount(L);
1430    I = IterationCounts.insert(std::make_pair(L, ItCount)).first;
1431    if (ItCount != UnknownValue) {
1432      assert(ItCount->isLoopInvariant(L) &&
1433             "Computed trip count isn't loop invariant for loop!");
1434      ++NumTripCountsComputed;
1435    } else if (isa<PHINode>(L->getHeader()->begin())) {
1436      // Only count loops that have phi nodes as not being computable.
1437      ++NumTripCountsNotComputed;
1438    }
1439  }
1440  return I->second;
1441}
1442
1443/// ComputeIterationCount - Compute the number of times the specified loop
1444/// will iterate.
1445SCEVHandle ScalarEvolutionsImpl::ComputeIterationCount(const Loop *L) {
1446  // If the loop has a non-one exit block count, we can't analyze it.
1447  std::vector<BasicBlock*> ExitBlocks;
1448  L->getExitBlocks(ExitBlocks);
1449  if (ExitBlocks.size() != 1) return UnknownValue;
1450
1451  // Okay, there is one exit block.  Try to find the condition that causes the
1452  // loop to be exited.
1453  BasicBlock *ExitBlock = ExitBlocks[0];
1454
1455  BasicBlock *ExitingBlock = 0;
1456  for (pred_iterator PI = pred_begin(ExitBlock), E = pred_end(ExitBlock);
1457       PI != E; ++PI)
1458    if (L->contains(*PI)) {
1459      if (ExitingBlock == 0)
1460        ExitingBlock = *PI;
1461      else
1462        return UnknownValue;   // More than one block exiting!
1463    }
1464  assert(ExitingBlock && "No exits from loop, something is broken!");
1465
1466  // Okay, we've computed the exiting block.  See what condition causes us to
1467  // exit.
1468  //
1469  // FIXME: we should be able to handle switch instructions (with a single exit)
1470  // FIXME: We should handle cast of int to bool as well
1471  BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
1472  if (ExitBr == 0) return UnknownValue;
1473  assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!");
1474  SetCondInst *ExitCond = dyn_cast<SetCondInst>(ExitBr->getCondition());
1475  if (ExitCond == 0)  // Not a setcc
1476    return ComputeIterationCountExhaustively(L, ExitBr->getCondition(),
1477                                          ExitBr->getSuccessor(0) == ExitBlock);
1478
1479  // If the condition was exit on true, convert the condition to exit on false.
1480  Instruction::BinaryOps Cond;
1481  if (ExitBr->getSuccessor(1) == ExitBlock)
1482    Cond = ExitCond->getOpcode();
1483  else
1484    Cond = ExitCond->getInverseCondition();
1485
1486  // Handle common loops like: for (X = "string"; *X; ++X)
1487  if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0)))
1488    if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) {
1489      SCEVHandle ItCnt =
1490        ComputeLoadConstantCompareIterationCount(LI, RHS, L, Cond);
1491      if (!isa<SCEVCouldNotCompute>(ItCnt)) return ItCnt;
1492    }
1493
1494  SCEVHandle LHS = getSCEV(ExitCond->getOperand(0));
1495  SCEVHandle RHS = getSCEV(ExitCond->getOperand(1));
1496
1497  // Try to evaluate any dependencies out of the loop.
1498  SCEVHandle Tmp = getSCEVAtScope(LHS, L);
1499  if (!isa<SCEVCouldNotCompute>(Tmp)) LHS = Tmp;
1500  Tmp = getSCEVAtScope(RHS, L);
1501  if (!isa<SCEVCouldNotCompute>(Tmp)) RHS = Tmp;
1502
1503  // At this point, we would like to compute how many iterations of the loop the
1504  // predicate will return true for these inputs.
1505  if (isa<SCEVConstant>(LHS) && !isa<SCEVConstant>(RHS)) {
1506    // If there is a constant, force it into the RHS.
1507    std::swap(LHS, RHS);
1508    Cond = SetCondInst::getSwappedCondition(Cond);
1509  }
1510
1511  // FIXME: think about handling pointer comparisons!  i.e.:
1512  // while (P != P+100) ++P;
1513
1514  // If we have a comparison of a chrec against a constant, try to use value
1515  // ranges to answer this query.
1516  if (SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
1517    if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
1518      if (AddRec->getLoop() == L) {
1519        // Form the comparison range using the constant of the correct type so
1520        // that the ConstantRange class knows to do a signed or unsigned
1521        // comparison.
1522        ConstantInt *CompVal = RHSC->getValue();
1523        const Type *RealTy = ExitCond->getOperand(0)->getType();
1524        CompVal = dyn_cast<ConstantInt>(ConstantExpr::getCast(CompVal, RealTy));
1525        if (CompVal) {
1526          // Form the constant range.
1527          ConstantRange CompRange(Cond, CompVal);
1528
1529          // Now that we have it, if it's signed, convert it to an unsigned
1530          // range.
1531          if (CompRange.getLower()->getType()->isSigned()) {
1532            const Type *NewTy = RHSC->getValue()->getType();
1533            Constant *NewL = ConstantExpr::getCast(CompRange.getLower(), NewTy);
1534            Constant *NewU = ConstantExpr::getCast(CompRange.getUpper(), NewTy);
1535            CompRange = ConstantRange(NewL, NewU);
1536          }
1537
1538          SCEVHandle Ret = AddRec->getNumIterationsInRange(CompRange);
1539          if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
1540        }
1541      }
1542
1543  switch (Cond) {
1544  case Instruction::SetNE:                     // while (X != Y)
1545    // Convert to: while (X-Y != 0)
1546    if (LHS->getType()->isInteger()) {
1547      SCEVHandle TC = HowFarToZero(SCEV::getMinusSCEV(LHS, RHS), L);
1548      if (!isa<SCEVCouldNotCompute>(TC)) return TC;
1549    }
1550    break;
1551  case Instruction::SetEQ:
1552    // Convert to: while (X-Y == 0)           // while (X == Y)
1553    if (LHS->getType()->isInteger()) {
1554      SCEVHandle TC = HowFarToNonZero(SCEV::getMinusSCEV(LHS, RHS), L);
1555      if (!isa<SCEVCouldNotCompute>(TC)) return TC;
1556    }
1557    break;
1558  case Instruction::SetLT:
1559    if (LHS->getType()->isInteger() &&
1560        ExitCond->getOperand(0)->getType()->isSigned()) {
1561      SCEVHandle TC = HowManyLessThans(LHS, RHS, L);
1562      if (!isa<SCEVCouldNotCompute>(TC)) return TC;
1563    }
1564    break;
1565  case Instruction::SetGT:
1566    if (LHS->getType()->isInteger() &&
1567        ExitCond->getOperand(0)->getType()->isSigned()) {
1568      SCEVHandle TC = HowManyLessThans(RHS, LHS, L);
1569      if (!isa<SCEVCouldNotCompute>(TC)) return TC;
1570    }
1571    break;
1572  default:
1573#if 0
1574    std::cerr << "ComputeIterationCount ";
1575    if (ExitCond->getOperand(0)->getType()->isUnsigned())
1576      std::cerr << "[unsigned] ";
1577    std::cerr << *LHS << "   "
1578              << Instruction::getOpcodeName(Cond) << "   " << *RHS << "\n";
1579#endif
1580    break;
1581  }
1582
1583  return ComputeIterationCountExhaustively(L, ExitCond,
1584                                         ExitBr->getSuccessor(0) == ExitBlock);
1585}
1586
1587static ConstantInt *
1588EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, Constant *C) {
1589  SCEVHandle InVal = SCEVConstant::get(cast<ConstantInt>(C));
1590  SCEVHandle Val = AddRec->evaluateAtIteration(InVal);
1591  assert(isa<SCEVConstant>(Val) &&
1592         "Evaluation of SCEV at constant didn't fold correctly?");
1593  return cast<SCEVConstant>(Val)->getValue();
1594}
1595
1596/// GetAddressedElementFromGlobal - Given a global variable with an initializer
1597/// and a GEP expression (missing the pointer index) indexing into it, return
1598/// the addressed element of the initializer or null if the index expression is
1599/// invalid.
1600static Constant *
1601GetAddressedElementFromGlobal(GlobalVariable *GV,
1602                              const std::vector<ConstantInt*> &Indices) {
1603  Constant *Init = GV->getInitializer();
1604  for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1605    uint64_t Idx = Indices[i]->getRawValue();
1606    if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) {
1607      assert(Idx < CS->getNumOperands() && "Bad struct index!");
1608      Init = cast<Constant>(CS->getOperand(Idx));
1609    } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) {
1610      if (Idx >= CA->getNumOperands()) return 0;  // Bogus program
1611      Init = cast<Constant>(CA->getOperand(Idx));
1612    } else if (isa<ConstantAggregateZero>(Init)) {
1613      if (const StructType *STy = dyn_cast<StructType>(Init->getType())) {
1614        assert(Idx < STy->getNumElements() && "Bad struct index!");
1615        Init = Constant::getNullValue(STy->getElementType(Idx));
1616      } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) {
1617        if (Idx >= ATy->getNumElements()) return 0;  // Bogus program
1618        Init = Constant::getNullValue(ATy->getElementType());
1619      } else {
1620        assert(0 && "Unknown constant aggregate type!");
1621      }
1622      return 0;
1623    } else {
1624      return 0; // Unknown initializer type
1625    }
1626  }
1627  return Init;
1628}
1629
1630/// ComputeLoadConstantCompareIterationCount - Given an exit condition of
1631/// 'setcc load X, cst', try to se if we can compute the trip count.
1632SCEVHandle ScalarEvolutionsImpl::
1633ComputeLoadConstantCompareIterationCount(LoadInst *LI, Constant *RHS,
1634                                         const Loop *L, unsigned SetCCOpcode) {
1635  if (LI->isVolatile()) return UnknownValue;
1636
1637  // Check to see if the loaded pointer is a getelementptr of a global.
1638  GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0));
1639  if (!GEP) return UnknownValue;
1640
1641  // Make sure that it is really a constant global we are gepping, with an
1642  // initializer, and make sure the first IDX is really 0.
1643  GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
1644  if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
1645      GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) ||
1646      !cast<Constant>(GEP->getOperand(1))->isNullValue())
1647    return UnknownValue;
1648
1649  // Okay, we allow one non-constant index into the GEP instruction.
1650  Value *VarIdx = 0;
1651  std::vector<ConstantInt*> Indexes;
1652  unsigned VarIdxNum = 0;
1653  for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i)
1654    if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
1655      Indexes.push_back(CI);
1656    } else if (!isa<ConstantInt>(GEP->getOperand(i))) {
1657      if (VarIdx) return UnknownValue;  // Multiple non-constant idx's.
1658      VarIdx = GEP->getOperand(i);
1659      VarIdxNum = i-2;
1660      Indexes.push_back(0);
1661    }
1662
1663  // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant.
1664  // Check to see if X is a loop variant variable value now.
1665  SCEVHandle Idx = getSCEV(VarIdx);
1666  SCEVHandle Tmp = getSCEVAtScope(Idx, L);
1667  if (!isa<SCEVCouldNotCompute>(Tmp)) Idx = Tmp;
1668
1669  // We can only recognize very limited forms of loop index expressions, in
1670  // particular, only affine AddRec's like {C1,+,C2}.
1671  SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx);
1672  if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) ||
1673      !isa<SCEVConstant>(IdxExpr->getOperand(0)) ||
1674      !isa<SCEVConstant>(IdxExpr->getOperand(1)))
1675    return UnknownValue;
1676
1677  unsigned MaxSteps = MaxBruteForceIterations;
1678  for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
1679    ConstantUInt *ItCst =
1680      ConstantUInt::get(IdxExpr->getType()->getUnsignedVersion(), IterationNum);
1681    ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst);
1682
1683    // Form the GEP offset.
1684    Indexes[VarIdxNum] = Val;
1685
1686    Constant *Result = GetAddressedElementFromGlobal(GV, Indexes);
1687    if (Result == 0) break;  // Cannot compute!
1688
1689    // Evaluate the condition for this iteration.
1690    Result = ConstantExpr::get(SetCCOpcode, Result, RHS);
1691    if (!isa<ConstantBool>(Result)) break;  // Couldn't decide for sure
1692    if (Result == ConstantBool::False) {
1693#if 0
1694      std::cerr << "\n***\n*** Computed loop count " << *ItCst
1695                << "\n*** From global " << *GV << "*** BB: " << *L->getHeader()
1696                << "***\n";
1697#endif
1698      ++NumArrayLenItCounts;
1699      return SCEVConstant::get(ItCst);   // Found terminating iteration!
1700    }
1701  }
1702  return UnknownValue;
1703}
1704
1705
1706/// CanConstantFold - Return true if we can constant fold an instruction of the
1707/// specified type, assuming that all operands were constants.
1708static bool CanConstantFold(const Instruction *I) {
1709  if (isa<BinaryOperator>(I) || isa<ShiftInst>(I) ||
1710      isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I))
1711    return true;
1712
1713  if (const CallInst *CI = dyn_cast<CallInst>(I))
1714    if (const Function *F = CI->getCalledFunction())
1715      return canConstantFoldCallTo((Function*)F);  // FIXME: elim cast
1716  return false;
1717}
1718
1719/// ConstantFold - Constant fold an instruction of the specified type with the
1720/// specified constant operands.  This function may modify the operands vector.
1721static Constant *ConstantFold(const Instruction *I,
1722                              std::vector<Constant*> &Operands) {
1723  if (isa<BinaryOperator>(I) || isa<ShiftInst>(I))
1724    return ConstantExpr::get(I->getOpcode(), Operands[0], Operands[1]);
1725
1726  switch (I->getOpcode()) {
1727  case Instruction::Cast:
1728    return ConstantExpr::getCast(Operands[0], I->getType());
1729  case Instruction::Select:
1730    return ConstantExpr::getSelect(Operands[0], Operands[1], Operands[2]);
1731  case Instruction::Call:
1732    if (Function *GV = dyn_cast<Function>(Operands[0])) {
1733      Operands.erase(Operands.begin());
1734      return ConstantFoldCall(cast<Function>(GV), Operands);
1735    }
1736
1737    return 0;
1738  case Instruction::GetElementPtr:
1739    Constant *Base = Operands[0];
1740    Operands.erase(Operands.begin());
1741    return ConstantExpr::getGetElementPtr(Base, Operands);
1742  }
1743  return 0;
1744}
1745
1746
1747/// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node
1748/// in the loop that V is derived from.  We allow arbitrary operations along the
1749/// way, but the operands of an operation must either be constants or a value
1750/// derived from a constant PHI.  If this expression does not fit with these
1751/// constraints, return null.
1752static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) {
1753  // If this is not an instruction, or if this is an instruction outside of the
1754  // loop, it can't be derived from a loop PHI.
1755  Instruction *I = dyn_cast<Instruction>(V);
1756  if (I == 0 || !L->contains(I->getParent())) return 0;
1757
1758  if (PHINode *PN = dyn_cast<PHINode>(I))
1759    if (L->getHeader() == I->getParent())
1760      return PN;
1761    else
1762      // We don't currently keep track of the control flow needed to evaluate
1763      // PHIs, so we cannot handle PHIs inside of loops.
1764      return 0;
1765
1766  // If we won't be able to constant fold this expression even if the operands
1767  // are constants, return early.
1768  if (!CanConstantFold(I)) return 0;
1769
1770  // Otherwise, we can evaluate this instruction if all of its operands are
1771  // constant or derived from a PHI node themselves.
1772  PHINode *PHI = 0;
1773  for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op)
1774    if (!(isa<Constant>(I->getOperand(Op)) ||
1775          isa<GlobalValue>(I->getOperand(Op)))) {
1776      PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L);
1777      if (P == 0) return 0;  // Not evolving from PHI
1778      if (PHI == 0)
1779        PHI = P;
1780      else if (PHI != P)
1781        return 0;  // Evolving from multiple different PHIs.
1782    }
1783
1784  // This is a expression evolving from a constant PHI!
1785  return PHI;
1786}
1787
1788/// EvaluateExpression - Given an expression that passes the
1789/// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node
1790/// in the loop has the value PHIVal.  If we can't fold this expression for some
1791/// reason, return null.
1792static Constant *EvaluateExpression(Value *V, Constant *PHIVal) {
1793  if (isa<PHINode>(V)) return PHIVal;
1794  if (GlobalValue *GV = dyn_cast<GlobalValue>(V))
1795    return GV;
1796  if (Constant *C = dyn_cast<Constant>(V)) return C;
1797  Instruction *I = cast<Instruction>(V);
1798
1799  std::vector<Constant*> Operands;
1800  Operands.resize(I->getNumOperands());
1801
1802  for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
1803    Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal);
1804    if (Operands[i] == 0) return 0;
1805  }
1806
1807  return ConstantFold(I, Operands);
1808}
1809
1810/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
1811/// in the header of its containing loop, we know the loop executes a
1812/// constant number of times, and the PHI node is just a recurrence
1813/// involving constants, fold it.
1814Constant *ScalarEvolutionsImpl::
1815getConstantEvolutionLoopExitValue(PHINode *PN, uint64_t Its, const Loop *L) {
1816  std::map<PHINode*, Constant*>::iterator I =
1817    ConstantEvolutionLoopExitValue.find(PN);
1818  if (I != ConstantEvolutionLoopExitValue.end())
1819    return I->second;
1820
1821  if (Its > MaxBruteForceIterations)
1822    return ConstantEvolutionLoopExitValue[PN] = 0;  // Not going to evaluate it.
1823
1824  Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
1825
1826  // Since the loop is canonicalized, the PHI node must have two entries.  One
1827  // entry must be a constant (coming in from outside of the loop), and the
1828  // second must be derived from the same PHI.
1829  bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
1830  Constant *StartCST =
1831    dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge));
1832  if (StartCST == 0)
1833    return RetVal = 0;  // Must be a constant.
1834
1835  Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
1836  PHINode *PN2 = getConstantEvolvingPHI(BEValue, L);
1837  if (PN2 != PN)
1838    return RetVal = 0;  // Not derived from same PHI.
1839
1840  // Execute the loop symbolically to determine the exit value.
1841  unsigned IterationNum = 0;
1842  unsigned NumIterations = Its;
1843  if (NumIterations != Its)
1844    return RetVal = 0;  // More than 2^32 iterations??
1845
1846  for (Constant *PHIVal = StartCST; ; ++IterationNum) {
1847    if (IterationNum == NumIterations)
1848      return RetVal = PHIVal;  // Got exit value!
1849
1850    // Compute the value of the PHI node for the next iteration.
1851    Constant *NextPHI = EvaluateExpression(BEValue, PHIVal);
1852    if (NextPHI == PHIVal)
1853      return RetVal = NextPHI;  // Stopped evolving!
1854    if (NextPHI == 0)
1855      return 0;        // Couldn't evaluate!
1856    PHIVal = NextPHI;
1857  }
1858}
1859
1860/// ComputeIterationCountExhaustively - If the trip is known to execute a
1861/// constant number of times (the condition evolves only from constants),
1862/// try to evaluate a few iterations of the loop until we get the exit
1863/// condition gets a value of ExitWhen (true or false).  If we cannot
1864/// evaluate the trip count of the loop, return UnknownValue.
1865SCEVHandle ScalarEvolutionsImpl::
1866ComputeIterationCountExhaustively(const Loop *L, Value *Cond, bool ExitWhen) {
1867  PHINode *PN = getConstantEvolvingPHI(Cond, L);
1868  if (PN == 0) return UnknownValue;
1869
1870  // Since the loop is canonicalized, the PHI node must have two entries.  One
1871  // entry must be a constant (coming in from outside of the loop), and the
1872  // second must be derived from the same PHI.
1873  bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
1874  Constant *StartCST =
1875    dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge));
1876  if (StartCST == 0) return UnknownValue;  // Must be a constant.
1877
1878  Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
1879  PHINode *PN2 = getConstantEvolvingPHI(BEValue, L);
1880  if (PN2 != PN) return UnknownValue;  // Not derived from same PHI.
1881
1882  // Okay, we find a PHI node that defines the trip count of this loop.  Execute
1883  // the loop symbolically to determine when the condition gets a value of
1884  // "ExitWhen".
1885  unsigned IterationNum = 0;
1886  unsigned MaxIterations = MaxBruteForceIterations;   // Limit analysis.
1887  for (Constant *PHIVal = StartCST;
1888       IterationNum != MaxIterations; ++IterationNum) {
1889    ConstantBool *CondVal =
1890      dyn_cast_or_null<ConstantBool>(EvaluateExpression(Cond, PHIVal));
1891    if (!CondVal) return UnknownValue;     // Couldn't symbolically evaluate.
1892
1893    if (CondVal->getValue() == ExitWhen) {
1894      ConstantEvolutionLoopExitValue[PN] = PHIVal;
1895      ++NumBruteForceTripCountsComputed;
1896      return SCEVConstant::get(ConstantUInt::get(Type::UIntTy, IterationNum));
1897    }
1898
1899    // Compute the value of the PHI node for the next iteration.
1900    Constant *NextPHI = EvaluateExpression(BEValue, PHIVal);
1901    if (NextPHI == 0 || NextPHI == PHIVal)
1902      return UnknownValue;  // Couldn't evaluate or not making progress...
1903    PHIVal = NextPHI;
1904  }
1905
1906  // Too many iterations were needed to evaluate.
1907  return UnknownValue;
1908}
1909
1910/// getSCEVAtScope - Compute the value of the specified expression within the
1911/// indicated loop (which may be null to indicate in no loop).  If the
1912/// expression cannot be evaluated, return UnknownValue.
1913SCEVHandle ScalarEvolutionsImpl::getSCEVAtScope(SCEV *V, const Loop *L) {
1914  // FIXME: this should be turned into a virtual method on SCEV!
1915
1916  if (isa<SCEVConstant>(V)) return V;
1917
1918  // If this instruction is evolves from a constant-evolving PHI, compute the
1919  // exit value from the loop without using SCEVs.
1920  if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) {
1921    if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) {
1922      const Loop *LI = this->LI[I->getParent()];
1923      if (LI && LI->getParentLoop() == L)  // Looking for loop exit value.
1924        if (PHINode *PN = dyn_cast<PHINode>(I))
1925          if (PN->getParent() == LI->getHeader()) {
1926            // Okay, there is no closed form solution for the PHI node.  Check
1927            // to see if the loop that contains it has a known iteration count.
1928            // If so, we may be able to force computation of the exit value.
1929            SCEVHandle IterationCount = getIterationCount(LI);
1930            if (SCEVConstant *ICC = dyn_cast<SCEVConstant>(IterationCount)) {
1931              // Okay, we know how many times the containing loop executes.  If
1932              // this is a constant evolving PHI node, get the final value at
1933              // the specified iteration number.
1934              Constant *RV = getConstantEvolutionLoopExitValue(PN,
1935                                               ICC->getValue()->getRawValue(),
1936                                                               LI);
1937              if (RV) return SCEVUnknown::get(RV);
1938            }
1939          }
1940
1941      // Okay, this is a some expression that we cannot symbolically evaluate
1942      // into a SCEV.  Check to see if it's possible to symbolically evaluate
1943      // the arguments into constants, and if see, try to constant propagate the
1944      // result.  This is particularly useful for computing loop exit values.
1945      if (CanConstantFold(I)) {
1946        std::vector<Constant*> Operands;
1947        Operands.reserve(I->getNumOperands());
1948        for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
1949          Value *Op = I->getOperand(i);
1950          if (Constant *C = dyn_cast<Constant>(Op)) {
1951            Operands.push_back(C);
1952          } else {
1953            SCEVHandle OpV = getSCEVAtScope(getSCEV(Op), L);
1954            if (SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV))
1955              Operands.push_back(ConstantExpr::getCast(SC->getValue(),
1956                                                       Op->getType()));
1957            else if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) {
1958              if (Constant *C = dyn_cast<Constant>(SU->getValue()))
1959                Operands.push_back(ConstantExpr::getCast(C, Op->getType()));
1960              else
1961                return V;
1962            } else {
1963              return V;
1964            }
1965          }
1966        }
1967        return SCEVUnknown::get(ConstantFold(I, Operands));
1968      }
1969    }
1970
1971    // This is some other type of SCEVUnknown, just return it.
1972    return V;
1973  }
1974
1975  if (SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) {
1976    // Avoid performing the look-up in the common case where the specified
1977    // expression has no loop-variant portions.
1978    for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) {
1979      SCEVHandle OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
1980      if (OpAtScope != Comm->getOperand(i)) {
1981        if (OpAtScope == UnknownValue) return UnknownValue;
1982        // Okay, at least one of these operands is loop variant but might be
1983        // foldable.  Build a new instance of the folded commutative expression.
1984        std::vector<SCEVHandle> NewOps(Comm->op_begin(), Comm->op_begin()+i);
1985        NewOps.push_back(OpAtScope);
1986
1987        for (++i; i != e; ++i) {
1988          OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
1989          if (OpAtScope == UnknownValue) return UnknownValue;
1990          NewOps.push_back(OpAtScope);
1991        }
1992        if (isa<SCEVAddExpr>(Comm))
1993          return SCEVAddExpr::get(NewOps);
1994        assert(isa<SCEVMulExpr>(Comm) && "Only know about add and mul!");
1995        return SCEVMulExpr::get(NewOps);
1996      }
1997    }
1998    // If we got here, all operands are loop invariant.
1999    return Comm;
2000  }
2001
2002  if (SCEVSDivExpr *Div = dyn_cast<SCEVSDivExpr>(V)) {
2003    SCEVHandle LHS = getSCEVAtScope(Div->getLHS(), L);
2004    if (LHS == UnknownValue) return LHS;
2005    SCEVHandle RHS = getSCEVAtScope(Div->getRHS(), L);
2006    if (RHS == UnknownValue) return RHS;
2007    if (LHS == Div->getLHS() && RHS == Div->getRHS())
2008      return Div;   // must be loop invariant
2009    return SCEVSDivExpr::get(LHS, RHS);
2010  }
2011
2012  // If this is a loop recurrence for a loop that does not contain L, then we
2013  // are dealing with the final value computed by the loop.
2014  if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
2015    if (!L || !AddRec->getLoop()->contains(L->getHeader())) {
2016      // To evaluate this recurrence, we need to know how many times the AddRec
2017      // loop iterates.  Compute this now.
2018      SCEVHandle IterationCount = getIterationCount(AddRec->getLoop());
2019      if (IterationCount == UnknownValue) return UnknownValue;
2020      IterationCount = getTruncateOrZeroExtend(IterationCount,
2021                                               AddRec->getType());
2022
2023      // If the value is affine, simplify the expression evaluation to just
2024      // Start + Step*IterationCount.
2025      if (AddRec->isAffine())
2026        return SCEVAddExpr::get(AddRec->getStart(),
2027                                SCEVMulExpr::get(IterationCount,
2028                                                 AddRec->getOperand(1)));
2029
2030      // Otherwise, evaluate it the hard way.
2031      return AddRec->evaluateAtIteration(IterationCount);
2032    }
2033    return UnknownValue;
2034  }
2035
2036  //assert(0 && "Unknown SCEV type!");
2037  return UnknownValue;
2038}
2039
2040
2041/// SolveQuadraticEquation - Find the roots of the quadratic equation for the
2042/// given quadratic chrec {L,+,M,+,N}.  This returns either the two roots (which
2043/// might be the same) or two SCEVCouldNotCompute objects.
2044///
2045static std::pair<SCEVHandle,SCEVHandle>
2046SolveQuadraticEquation(const SCEVAddRecExpr *AddRec) {
2047  assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
2048  SCEVConstant *L = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
2049  SCEVConstant *M = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
2050  SCEVConstant *N = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
2051
2052  // We currently can only solve this if the coefficients are constants.
2053  if (!L || !M || !N) {
2054    SCEV *CNC = new SCEVCouldNotCompute();
2055    return std::make_pair(CNC, CNC);
2056  }
2057
2058  Constant *Two = ConstantInt::get(L->getValue()->getType(), 2);
2059
2060  // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C
2061  Constant *C = L->getValue();
2062  // The B coefficient is M-N/2
2063  Constant *B = ConstantExpr::getSub(M->getValue(),
2064                                     ConstantExpr::getDiv(N->getValue(),
2065                                                          Two));
2066  // The A coefficient is N/2
2067  Constant *A = ConstantExpr::getDiv(N->getValue(), Two);
2068
2069  // Compute the B^2-4ac term.
2070  Constant *SqrtTerm =
2071    ConstantExpr::getMul(ConstantInt::get(C->getType(), 4),
2072                         ConstantExpr::getMul(A, C));
2073  SqrtTerm = ConstantExpr::getSub(ConstantExpr::getMul(B, B), SqrtTerm);
2074
2075  // Compute floor(sqrt(B^2-4ac))
2076  ConstantUInt *SqrtVal =
2077    cast<ConstantUInt>(ConstantExpr::getCast(SqrtTerm,
2078                                   SqrtTerm->getType()->getUnsignedVersion()));
2079  uint64_t SqrtValV = SqrtVal->getValue();
2080  uint64_t SqrtValV2 = (uint64_t)sqrt((double)SqrtValV);
2081  // The square root might not be precise for arbitrary 64-bit integer
2082  // values.  Do some sanity checks to ensure it's correct.
2083  if (SqrtValV2*SqrtValV2 > SqrtValV ||
2084      (SqrtValV2+1)*(SqrtValV2+1) <= SqrtValV) {
2085    SCEV *CNC = new SCEVCouldNotCompute();
2086    return std::make_pair(CNC, CNC);
2087  }
2088
2089  SqrtVal = ConstantUInt::get(Type::ULongTy, SqrtValV2);
2090  SqrtTerm = ConstantExpr::getCast(SqrtVal, SqrtTerm->getType());
2091
2092  Constant *NegB = ConstantExpr::getNeg(B);
2093  Constant *TwoA = ConstantExpr::getMul(A, Two);
2094
2095  // The divisions must be performed as signed divisions.
2096  const Type *SignedTy = NegB->getType()->getSignedVersion();
2097  NegB = ConstantExpr::getCast(NegB, SignedTy);
2098  TwoA = ConstantExpr::getCast(TwoA, SignedTy);
2099  SqrtTerm = ConstantExpr::getCast(SqrtTerm, SignedTy);
2100
2101  Constant *Solution1 =
2102    ConstantExpr::getDiv(ConstantExpr::getAdd(NegB, SqrtTerm), TwoA);
2103  Constant *Solution2 =
2104    ConstantExpr::getDiv(ConstantExpr::getSub(NegB, SqrtTerm), TwoA);
2105  return std::make_pair(SCEVUnknown::get(Solution1),
2106                        SCEVUnknown::get(Solution2));
2107}
2108
2109/// HowFarToZero - Return the number of times a backedge comparing the specified
2110/// value to zero will execute.  If not computable, return UnknownValue
2111SCEVHandle ScalarEvolutionsImpl::HowFarToZero(SCEV *V, const Loop *L) {
2112  // If the value is a constant
2113  if (SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
2114    // If the value is already zero, the branch will execute zero times.
2115    if (C->getValue()->isNullValue()) return C;
2116    return UnknownValue;  // Otherwise it will loop infinitely.
2117  }
2118
2119  SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V);
2120  if (!AddRec || AddRec->getLoop() != L)
2121    return UnknownValue;
2122
2123  if (AddRec->isAffine()) {
2124    // If this is an affine expression the execution count of this branch is
2125    // equal to:
2126    //
2127    //     (0 - Start/Step)    iff   Start % Step == 0
2128    //
2129    // Get the initial value for the loop.
2130    SCEVHandle Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop());
2131    if (isa<SCEVCouldNotCompute>(Start)) return UnknownValue;
2132    SCEVHandle Step = AddRec->getOperand(1);
2133
2134    Step = getSCEVAtScope(Step, L->getParentLoop());
2135
2136    // Figure out if Start % Step == 0.
2137    // FIXME: We should add DivExpr and RemExpr operations to our AST.
2138    if (SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) {
2139      if (StepC->getValue()->equalsInt(1))      // N % 1 == 0
2140        return SCEV::getNegativeSCEV(Start);  // 0 - Start/1 == -Start
2141      if (StepC->getValue()->isAllOnesValue())  // N % -1 == 0
2142        return Start;                   // 0 - Start/-1 == Start
2143
2144      // Check to see if Start is divisible by SC with no remainder.
2145      if (SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) {
2146        ConstantInt *StartCC = StartC->getValue();
2147        Constant *StartNegC = ConstantExpr::getNeg(StartCC);
2148        Constant *Rem = ConstantExpr::getRem(StartNegC, StepC->getValue());
2149        if (Rem->isNullValue()) {
2150          Constant *Result =ConstantExpr::getDiv(StartNegC,StepC->getValue());
2151          return SCEVUnknown::get(Result);
2152        }
2153      }
2154    }
2155  } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) {
2156    // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
2157    // the quadratic equation to solve it.
2158    std::pair<SCEVHandle,SCEVHandle> Roots = SolveQuadraticEquation(AddRec);
2159    SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
2160    SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
2161    if (R1) {
2162#if 0
2163      std::cerr << "HFTZ: " << *V << " - sol#1: " << *R1
2164                << "  sol#2: " << *R2 << "\n";
2165#endif
2166      // Pick the smallest positive root value.
2167      assert(R1->getType()->isUnsigned()&&"Didn't canonicalize to unsigned?");
2168      if (ConstantBool *CB =
2169          dyn_cast<ConstantBool>(ConstantExpr::getSetLT(R1->getValue(),
2170                                                        R2->getValue()))) {
2171        if (CB != ConstantBool::True)
2172          std::swap(R1, R2);   // R1 is the minimum root now.
2173
2174        // We can only use this value if the chrec ends up with an exact zero
2175        // value at this index.  When solving for "X*X != 5", for example, we
2176        // should not accept a root of 2.
2177        SCEVHandle Val = AddRec->evaluateAtIteration(R1);
2178        if (SCEVConstant *EvalVal = dyn_cast<SCEVConstant>(Val))
2179          if (EvalVal->getValue()->isNullValue())
2180            return R1;  // We found a quadratic root!
2181      }
2182    }
2183  }
2184
2185  return UnknownValue;
2186}
2187
2188/// HowFarToNonZero - Return the number of times a backedge checking the
2189/// specified value for nonzero will execute.  If not computable, return
2190/// UnknownValue
2191SCEVHandle ScalarEvolutionsImpl::HowFarToNonZero(SCEV *V, const Loop *L) {
2192  // Loops that look like: while (X == 0) are very strange indeed.  We don't
2193  // handle them yet except for the trivial case.  This could be expanded in the
2194  // future as needed.
2195
2196  // If the value is a constant, check to see if it is known to be non-zero
2197  // already.  If so, the backedge will execute zero times.
2198  if (SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
2199    Constant *Zero = Constant::getNullValue(C->getValue()->getType());
2200    Constant *NonZero = ConstantExpr::getSetNE(C->getValue(), Zero);
2201    if (NonZero == ConstantBool::True)
2202      return getSCEV(Zero);
2203    return UnknownValue;  // Otherwise it will loop infinitely.
2204  }
2205
2206  // We could implement others, but I really doubt anyone writes loops like
2207  // this, and if they did, they would already be constant folded.
2208  return UnknownValue;
2209}
2210
2211/// HowManyLessThans - Return the number of times a backedge containing the
2212/// specified less-than comparison will execute.  If not computable, return
2213/// UnknownValue.
2214SCEVHandle ScalarEvolutionsImpl::
2215HowManyLessThans(SCEV *LHS, SCEV *RHS, const Loop *L) {
2216  // Only handle:  "ADDREC < LoopInvariant".
2217  if (!RHS->isLoopInvariant(L)) return UnknownValue;
2218
2219  SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS);
2220  if (!AddRec || AddRec->getLoop() != L)
2221    return UnknownValue;
2222
2223  if (AddRec->isAffine()) {
2224    // FORNOW: We only support unit strides.
2225    SCEVHandle One = SCEVUnknown::getIntegerSCEV(1, RHS->getType());
2226    if (AddRec->getOperand(1) != One)
2227      return UnknownValue;
2228
2229    // The number of iterations for "[n,+,1] < m", is m-n.  However, we don't
2230    // know that m is >= n on input to the loop.  If it is, the condition return
2231    // true zero times.  What we really should return, for full generality, is
2232    // SMAX(0, m-n).  Since we cannot check this, we will instead check for a
2233    // canonical loop form: most do-loops will have a check that dominates the
2234    // loop, that only enters the loop if [n-1]<m.  If we can find this check,
2235    // we know that the SMAX will evaluate to m-n, because we know that m >= n.
2236
2237    // Search for the check.
2238    BasicBlock *Preheader = L->getLoopPreheader();
2239    BasicBlock *PreheaderDest = L->getHeader();
2240    if (Preheader == 0) return UnknownValue;
2241
2242    BranchInst *LoopEntryPredicate =
2243      dyn_cast<BranchInst>(Preheader->getTerminator());
2244    if (!LoopEntryPredicate) return UnknownValue;
2245
2246    // This might be a critical edge broken out.  If the loop preheader ends in
2247    // an unconditional branch to the loop, check to see if the preheader has a
2248    // single predecessor, and if so, look for its terminator.
2249    while (LoopEntryPredicate->isUnconditional()) {
2250      PreheaderDest = Preheader;
2251      Preheader = Preheader->getSinglePredecessor();
2252      if (!Preheader) return UnknownValue;  // Multiple preds.
2253
2254      LoopEntryPredicate =
2255        dyn_cast<BranchInst>(Preheader->getTerminator());
2256      if (!LoopEntryPredicate) return UnknownValue;
2257    }
2258
2259    // Now that we found a conditional branch that dominates the loop, check to
2260    // see if it is the comparison we are looking for.
2261    SetCondInst *SCI =dyn_cast<SetCondInst>(LoopEntryPredicate->getCondition());
2262    if (!SCI) return UnknownValue;
2263    Value *PreCondLHS = SCI->getOperand(0);
2264    Value *PreCondRHS = SCI->getOperand(1);
2265    Instruction::BinaryOps Cond;
2266    if (LoopEntryPredicate->getSuccessor(0) == PreheaderDest)
2267      Cond = SCI->getOpcode();
2268    else
2269      Cond = SCI->getInverseCondition();
2270
2271    switch (Cond) {
2272    case Instruction::SetGT:
2273      std::swap(PreCondLHS, PreCondRHS);
2274      Cond = Instruction::SetLT;
2275      // Fall Through.
2276    case Instruction::SetLT:
2277      if (PreCondLHS->getType()->isInteger() &&
2278          PreCondLHS->getType()->isSigned()) {
2279        if (RHS != getSCEV(PreCondRHS))
2280          return UnknownValue;  // Not a comparison against 'm'.
2281
2282        if (SCEV::getMinusSCEV(AddRec->getOperand(0), One)
2283                    != getSCEV(PreCondLHS))
2284          return UnknownValue;  // Not a comparison against 'n-1'.
2285        break;
2286      } else {
2287        return UnknownValue;
2288      }
2289    default: break;
2290    }
2291
2292    //std::cerr << "Computed Loop Trip Count as: " <<
2293    //  *SCEV::getMinusSCEV(RHS, AddRec->getOperand(0)) << "\n";
2294    return SCEV::getMinusSCEV(RHS, AddRec->getOperand(0));
2295  }
2296
2297  return UnknownValue;
2298}
2299
2300/// getNumIterationsInRange - Return the number of iterations of this loop that
2301/// produce values in the specified constant range.  Another way of looking at
2302/// this is that it returns the first iteration number where the value is not in
2303/// the condition, thus computing the exit count. If the iteration count can't
2304/// be computed, an instance of SCEVCouldNotCompute is returned.
2305SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range) const {
2306  if (Range.isFullSet())  // Infinite loop.
2307    return new SCEVCouldNotCompute();
2308
2309  // If the start is a non-zero constant, shift the range to simplify things.
2310  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart()))
2311    if (!SC->getValue()->isNullValue()) {
2312      std::vector<SCEVHandle> Operands(op_begin(), op_end());
2313      Operands[0] = SCEVUnknown::getIntegerSCEV(0, SC->getType());
2314      SCEVHandle Shifted = SCEVAddRecExpr::get(Operands, getLoop());
2315      if (SCEVAddRecExpr *ShiftedAddRec = dyn_cast<SCEVAddRecExpr>(Shifted))
2316        return ShiftedAddRec->getNumIterationsInRange(
2317                                              Range.subtract(SC->getValue()));
2318      // This is strange and shouldn't happen.
2319      return new SCEVCouldNotCompute();
2320    }
2321
2322  // The only time we can solve this is when we have all constant indices.
2323  // Otherwise, we cannot determine the overflow conditions.
2324  for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2325    if (!isa<SCEVConstant>(getOperand(i)))
2326      return new SCEVCouldNotCompute();
2327
2328
2329  // Okay at this point we know that all elements of the chrec are constants and
2330  // that the start element is zero.
2331
2332  // First check to see if the range contains zero.  If not, the first
2333  // iteration exits.
2334  ConstantInt *Zero = ConstantInt::get(getType(), 0);
2335  if (!Range.contains(Zero)) return SCEVConstant::get(Zero);
2336
2337  if (isAffine()) {
2338    // If this is an affine expression then we have this situation:
2339    //   Solve {0,+,A} in Range  ===  Ax in Range
2340
2341    // Since we know that zero is in the range, we know that the upper value of
2342    // the range must be the first possible exit value.  Also note that we
2343    // already checked for a full range.
2344    ConstantInt *Upper = cast<ConstantInt>(Range.getUpper());
2345    ConstantInt *A     = cast<SCEVConstant>(getOperand(1))->getValue();
2346    ConstantInt *One   = ConstantInt::get(getType(), 1);
2347
2348    // The exit value should be (Upper+A-1)/A.
2349    Constant *ExitValue = Upper;
2350    if (A != One) {
2351      ExitValue = ConstantExpr::getSub(ConstantExpr::getAdd(Upper, A), One);
2352      ExitValue = ConstantExpr::getDiv(ExitValue, A);
2353    }
2354    assert(isa<ConstantInt>(ExitValue) &&
2355           "Constant folding of integers not implemented?");
2356
2357    // Evaluate at the exit value.  If we really did fall out of the valid
2358    // range, then we computed our trip count, otherwise wrap around or other
2359    // things must have happened.
2360    ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue);
2361    if (Range.contains(Val))
2362      return new SCEVCouldNotCompute();  // Something strange happened
2363
2364    // Ensure that the previous value is in the range.  This is a sanity check.
2365    assert(Range.contains(EvaluateConstantChrecAtConstant(this,
2366                              ConstantExpr::getSub(ExitValue, One))) &&
2367           "Linear scev computation is off in a bad way!");
2368    return SCEVConstant::get(cast<ConstantInt>(ExitValue));
2369  } else if (isQuadratic()) {
2370    // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the
2371    // quadratic equation to solve it.  To do this, we must frame our problem in
2372    // terms of figuring out when zero is crossed, instead of when
2373    // Range.getUpper() is crossed.
2374    std::vector<SCEVHandle> NewOps(op_begin(), op_end());
2375    NewOps[0] = SCEV::getNegativeSCEV(SCEVUnknown::get(Range.getUpper()));
2376    SCEVHandle NewAddRec = SCEVAddRecExpr::get(NewOps, getLoop());
2377
2378    // Next, solve the constructed addrec
2379    std::pair<SCEVHandle,SCEVHandle> Roots =
2380      SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec));
2381    SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
2382    SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
2383    if (R1) {
2384      // Pick the smallest positive root value.
2385      assert(R1->getType()->isUnsigned() && "Didn't canonicalize to unsigned?");
2386      if (ConstantBool *CB =
2387          dyn_cast<ConstantBool>(ConstantExpr::getSetLT(R1->getValue(),
2388                                                        R2->getValue()))) {
2389        if (CB != ConstantBool::True)
2390          std::swap(R1, R2);   // R1 is the minimum root now.
2391
2392        // Make sure the root is not off by one.  The returned iteration should
2393        // not be in the range, but the previous one should be.  When solving
2394        // for "X*X < 5", for example, we should not return a root of 2.
2395        ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this,
2396                                                             R1->getValue());
2397        if (Range.contains(R1Val)) {
2398          // The next iteration must be out of the range...
2399          Constant *NextVal =
2400            ConstantExpr::getAdd(R1->getValue(),
2401                                 ConstantInt::get(R1->getType(), 1));
2402
2403          R1Val = EvaluateConstantChrecAtConstant(this, NextVal);
2404          if (!Range.contains(R1Val))
2405            return SCEVUnknown::get(NextVal);
2406          return new SCEVCouldNotCompute();  // Something strange happened
2407        }
2408
2409        // If R1 was not in the range, then it is a good return value.  Make
2410        // sure that R1-1 WAS in the range though, just in case.
2411        Constant *NextVal =
2412          ConstantExpr::getSub(R1->getValue(),
2413                               ConstantInt::get(R1->getType(), 1));
2414        R1Val = EvaluateConstantChrecAtConstant(this, NextVal);
2415        if (Range.contains(R1Val))
2416          return R1;
2417        return new SCEVCouldNotCompute();  // Something strange happened
2418      }
2419    }
2420  }
2421
2422  // Fallback, if this is a general polynomial, figure out the progression
2423  // through brute force: evaluate until we find an iteration that fails the
2424  // test.  This is likely to be slow, but getting an accurate trip count is
2425  // incredibly important, we will be able to simplify the exit test a lot, and
2426  // we are almost guaranteed to get a trip count in this case.
2427  ConstantInt *TestVal = ConstantInt::get(getType(), 0);
2428  ConstantInt *One     = ConstantInt::get(getType(), 1);
2429  ConstantInt *EndVal  = TestVal;  // Stop when we wrap around.
2430  do {
2431    ++NumBruteForceEvaluations;
2432    SCEVHandle Val = evaluateAtIteration(SCEVConstant::get(TestVal));
2433    if (!isa<SCEVConstant>(Val))  // This shouldn't happen.
2434      return new SCEVCouldNotCompute();
2435
2436    // Check to see if we found the value!
2437    if (!Range.contains(cast<SCEVConstant>(Val)->getValue()))
2438      return SCEVConstant::get(TestVal);
2439
2440    // Increment to test the next index.
2441    TestVal = cast<ConstantInt>(ConstantExpr::getAdd(TestVal, One));
2442  } while (TestVal != EndVal);
2443
2444  return new SCEVCouldNotCompute();
2445}
2446
2447
2448
2449//===----------------------------------------------------------------------===//
2450//                   ScalarEvolution Class Implementation
2451//===----------------------------------------------------------------------===//
2452
2453bool ScalarEvolution::runOnFunction(Function &F) {
2454  Impl = new ScalarEvolutionsImpl(F, getAnalysis<LoopInfo>());
2455  return false;
2456}
2457
2458void ScalarEvolution::releaseMemory() {
2459  delete (ScalarEvolutionsImpl*)Impl;
2460  Impl = 0;
2461}
2462
2463void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const {
2464  AU.setPreservesAll();
2465  AU.addRequiredTransitive<LoopInfo>();
2466}
2467
2468SCEVHandle ScalarEvolution::getSCEV(Value *V) const {
2469  return ((ScalarEvolutionsImpl*)Impl)->getSCEV(V);
2470}
2471
2472/// hasSCEV - Return true if the SCEV for this value has already been
2473/// computed.
2474bool ScalarEvolution::hasSCEV(Value *V) const {
2475  return ((ScalarEvolutionsImpl*)Impl)->hasSCEV(V);
2476}
2477
2478
2479/// setSCEV - Insert the specified SCEV into the map of current SCEVs for
2480/// the specified value.
2481void ScalarEvolution::setSCEV(Value *V, const SCEVHandle &H) {
2482  ((ScalarEvolutionsImpl*)Impl)->setSCEV(V, H);
2483}
2484
2485
2486SCEVHandle ScalarEvolution::getIterationCount(const Loop *L) const {
2487  return ((ScalarEvolutionsImpl*)Impl)->getIterationCount(L);
2488}
2489
2490bool ScalarEvolution::hasLoopInvariantIterationCount(const Loop *L) const {
2491  return !isa<SCEVCouldNotCompute>(getIterationCount(L));
2492}
2493
2494SCEVHandle ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) const {
2495  return ((ScalarEvolutionsImpl*)Impl)->getSCEVAtScope(getSCEV(V), L);
2496}
2497
2498void ScalarEvolution::deleteInstructionFromRecords(Instruction *I) const {
2499  return ((ScalarEvolutionsImpl*)Impl)->deleteInstructionFromRecords(I);
2500}
2501
2502static void PrintLoopInfo(std::ostream &OS, const ScalarEvolution *SE,
2503                          const Loop *L) {
2504  // Print all inner loops first
2505  for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
2506    PrintLoopInfo(OS, SE, *I);
2507
2508  std::cerr << "Loop " << L->getHeader()->getName() << ": ";
2509
2510  std::vector<BasicBlock*> ExitBlocks;
2511  L->getExitBlocks(ExitBlocks);
2512  if (ExitBlocks.size() != 1)
2513    std::cerr << "<multiple exits> ";
2514
2515  if (SE->hasLoopInvariantIterationCount(L)) {
2516    std::cerr << *SE->getIterationCount(L) << " iterations! ";
2517  } else {
2518    std::cerr << "Unpredictable iteration count. ";
2519  }
2520
2521  std::cerr << "\n";
2522}
2523
2524void ScalarEvolution::print(std::ostream &OS, const Module* ) const {
2525  Function &F = ((ScalarEvolutionsImpl*)Impl)->F;
2526  LoopInfo &LI = ((ScalarEvolutionsImpl*)Impl)->LI;
2527
2528  OS << "Classifying expressions for: " << F.getName() << "\n";
2529  for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
2530    if (I->getType()->isInteger()) {
2531      OS << *I;
2532      OS << "  --> ";
2533      SCEVHandle SV = getSCEV(&*I);
2534      SV->print(OS);
2535      OS << "\t\t";
2536
2537      if ((*I).getType()->isIntegral()) {
2538        ConstantRange Bounds = SV->getValueRange();
2539        if (!Bounds.isFullSet())
2540          OS << "Bounds: " << Bounds << " ";
2541      }
2542
2543      if (const Loop *L = LI.getLoopFor((*I).getParent())) {
2544        OS << "Exits: ";
2545        SCEVHandle ExitValue = getSCEVAtScope(&*I, L->getParentLoop());
2546        if (isa<SCEVCouldNotCompute>(ExitValue)) {
2547          OS << "<<Unknown>>";
2548        } else {
2549          OS << *ExitValue;
2550        }
2551      }
2552
2553
2554      OS << "\n";
2555    }
2556
2557  OS << "Determining loop execution counts for: " << F.getName() << "\n";
2558  for (LoopInfo::iterator I = LI.begin(), E = LI.end(); I != E; ++I)
2559    PrintLoopInfo(OS, this, *I);
2560}
2561
2562