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