Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- ScalarEvolution.cpp - Scalar Evolution Analysis ----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the implementation of the scalar evolution analysis |
| 11 | // engine, which is used primarily to analyze expressions involving induction |
| 12 | // variables in loops. |
| 13 | // |
| 14 | // There are several aspects to this library. First is the representation of |
| 15 | // scalar expressions, which are represented as subclasses of the SCEV class. |
| 16 | // These classes are used to represent certain types of subexpressions that we |
| 17 | // can handle. These classes are reference counted, managed by the SCEVHandle |
| 18 | // class. We only create one SCEV of a particular shape, so pointer-comparisons |
| 19 | // for equality are legal. |
| 20 | // |
| 21 | // One important aspect of the SCEV objects is that they are never cyclic, even |
| 22 | // if there is a cycle in the dataflow for an expression (ie, a PHI node). If |
| 23 | // the PHI node is one of the idioms that we can represent (e.g., a polynomial |
| 24 | // recurrence) then we represent it directly as a recurrence node, otherwise we |
| 25 | // represent it as a SCEVUnknown node. |
| 26 | // |
| 27 | // In addition to being able to represent expressions of various types, we also |
| 28 | // have folders that are used to build the *canonical* representation for a |
| 29 | // particular expression. These folders are capable of using a variety of |
| 30 | // rewrite rules to simplify the expressions. |
| 31 | // |
| 32 | // Once the folders are defined, we can implement the more interesting |
| 33 | // higher-level code, such as the code that recognizes PHI nodes of various |
| 34 | // types, computes the execution count of a loop, etc. |
| 35 | // |
| 36 | // TODO: We should use these routines and value representations to implement |
| 37 | // dependence analysis! |
| 38 | // |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | // |
| 41 | // There are several good references for the techniques used in this analysis. |
| 42 | // |
| 43 | // Chains of recurrences -- a method to expedite the evaluation |
| 44 | // of closed-form functions |
| 45 | // Olaf Bachmann, Paul S. Wang, Eugene V. Zima |
| 46 | // |
| 47 | // On computational properties of chains of recurrences |
| 48 | // Eugene V. Zima |
| 49 | // |
| 50 | // Symbolic Evaluation of Chains of Recurrences for Loop Optimization |
| 51 | // Robert A. van Engelen |
| 52 | // |
| 53 | // Efficient Symbolic Analysis for Optimizing Compilers |
| 54 | // Robert A. van Engelen |
| 55 | // |
| 56 | // Using the chains of recurrences algebra for data dependence testing and |
| 57 | // induction variable substitution |
| 58 | // MS Thesis, Johnie Birch |
| 59 | // |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | |
| 62 | #define DEBUG_TYPE "scalar-evolution" |
| 63 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 64 | #include "llvm/Constants.h" |
| 65 | #include "llvm/DerivedTypes.h" |
| 66 | #include "llvm/GlobalVariable.h" |
| 67 | #include "llvm/Instructions.h" |
| 68 | #include "llvm/Analysis/ConstantFolding.h" |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 69 | #include "llvm/Analysis/Dominators.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 70 | #include "llvm/Analysis/LoopInfo.h" |
| 71 | #include "llvm/Assembly/Writer.h" |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 72 | #include "llvm/Target/TargetData.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 73 | #include "llvm/Support/CommandLine.h" |
| 74 | #include "llvm/Support/Compiler.h" |
| 75 | #include "llvm/Support/ConstantRange.h" |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 76 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 77 | #include "llvm/Support/InstIterator.h" |
| 78 | #include "llvm/Support/ManagedStatic.h" |
| 79 | #include "llvm/Support/MathExtras.h" |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 80 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 81 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 82 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 83 | #include <ostream> |
| 84 | #include <algorithm> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 85 | using namespace llvm; |
| 86 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 87 | STATISTIC(NumArrayLenItCounts, |
| 88 | "Number of trip counts computed with array length"); |
| 89 | STATISTIC(NumTripCountsComputed, |
| 90 | "Number of loops with predictable loop counts"); |
| 91 | STATISTIC(NumTripCountsNotComputed, |
| 92 | "Number of loops without predictable loop counts"); |
| 93 | STATISTIC(NumBruteForceTripCountsComputed, |
| 94 | "Number of loops with trip counts computed by force"); |
| 95 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 96 | static cl::opt<unsigned> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 97 | MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, |
| 98 | cl::desc("Maximum number of iterations SCEV will " |
| 99 | "symbolically execute a constant derived loop"), |
| 100 | cl::init(100)); |
| 101 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 102 | static RegisterPass<ScalarEvolution> |
| 103 | R("scalar-evolution", "Scalar Evolution Analysis", false, true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 104 | char ScalarEvolution::ID = 0; |
| 105 | |
| 106 | //===----------------------------------------------------------------------===// |
| 107 | // SCEV class definitions |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | |
| 110 | //===----------------------------------------------------------------------===// |
| 111 | // Implementation of the SCEV class. |
| 112 | // |
| 113 | SCEV::~SCEV() {} |
| 114 | void SCEV::dump() const { |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 115 | print(errs()); |
| 116 | errs() << '\n'; |
| 117 | } |
| 118 | |
| 119 | void SCEV::print(std::ostream &o) const { |
| 120 | raw_os_ostream OS(o); |
| 121 | print(OS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 124 | bool SCEV::isZero() const { |
| 125 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 126 | return SC->getValue()->isZero(); |
| 127 | return false; |
| 128 | } |
| 129 | |
Dan Gohman | f8bc8e8 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 130 | bool SCEV::isOne() const { |
| 131 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 132 | return SC->getValue()->isOne(); |
| 133 | return false; |
| 134 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 135 | |
| 136 | SCEVCouldNotCompute::SCEVCouldNotCompute() : SCEV(scCouldNotCompute) {} |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 137 | SCEVCouldNotCompute::~SCEVCouldNotCompute() {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 138 | |
| 139 | bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const { |
| 140 | assert(0 && "Attempt to use a SCEVCouldNotCompute object!"); |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | const Type *SCEVCouldNotCompute::getType() const { |
| 145 | assert(0 && "Attempt to use a SCEVCouldNotCompute object!"); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const { |
| 150 | assert(0 && "Attempt to use a SCEVCouldNotCompute object!"); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | SCEVHandle SCEVCouldNotCompute:: |
| 155 | replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym, |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 156 | const SCEVHandle &Conc, |
| 157 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 158 | return this; |
| 159 | } |
| 160 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 161 | void SCEVCouldNotCompute::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 162 | OS << "***COULDNOTCOMPUTE***"; |
| 163 | } |
| 164 | |
| 165 | bool SCEVCouldNotCompute::classof(const SCEV *S) { |
| 166 | return S->getSCEVType() == scCouldNotCompute; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | // SCEVConstants - Only allow the creation of one SCEVConstant for any |
| 171 | // particular value. Don't use a SCEVHandle here, or else the object will |
| 172 | // never be deleted! |
| 173 | static ManagedStatic<std::map<ConstantInt*, SCEVConstant*> > SCEVConstants; |
| 174 | |
| 175 | |
| 176 | SCEVConstant::~SCEVConstant() { |
| 177 | SCEVConstants->erase(V); |
| 178 | } |
| 179 | |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 180 | SCEVHandle ScalarEvolution::getConstant(ConstantInt *V) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 181 | SCEVConstant *&R = (*SCEVConstants)[V]; |
| 182 | if (R == 0) R = new SCEVConstant(V); |
| 183 | return R; |
| 184 | } |
| 185 | |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 186 | SCEVHandle ScalarEvolution::getConstant(const APInt& Val) { |
| 187 | return getConstant(ConstantInt::get(Val)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 190 | const Type *SCEVConstant::getType() const { return V->getType(); } |
| 191 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 192 | void SCEVConstant::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 193 | WriteAsOperand(OS, V, false); |
| 194 | } |
| 195 | |
Dan Gohman | 2a38153 | 2009-04-21 01:25:57 +0000 | [diff] [blame] | 196 | SCEVCastExpr::SCEVCastExpr(unsigned SCEVTy, |
| 197 | const SCEVHandle &op, const Type *ty) |
| 198 | : SCEV(SCEVTy), Op(op), Ty(ty) {} |
| 199 | |
| 200 | SCEVCastExpr::~SCEVCastExpr() {} |
| 201 | |
| 202 | bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 203 | return Op->dominates(BB, DT); |
| 204 | } |
| 205 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 206 | // SCEVTruncates - Only allow the creation of one SCEVTruncateExpr for any |
| 207 | // particular input. Don't use a SCEVHandle here, or else the object will |
| 208 | // never be deleted! |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 209 | static ManagedStatic<std::map<std::pair<const SCEV*, const Type*>, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 210 | SCEVTruncateExpr*> > SCEVTruncates; |
| 211 | |
| 212 | SCEVTruncateExpr::SCEVTruncateExpr(const SCEVHandle &op, const Type *ty) |
Dan Gohman | 2a38153 | 2009-04-21 01:25:57 +0000 | [diff] [blame] | 213 | : SCEVCastExpr(scTruncate, op, ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 214 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 215 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 216 | "Cannot truncate non-integer value!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | SCEVTruncateExpr::~SCEVTruncateExpr() { |
| 220 | SCEVTruncates->erase(std::make_pair(Op, Ty)); |
| 221 | } |
| 222 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 223 | void SCEVTruncateExpr::print(raw_ostream &OS) const { |
Dan Gohman | c911922 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 224 | OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // SCEVZeroExtends - Only allow the creation of one SCEVZeroExtendExpr for any |
| 228 | // particular input. Don't use a SCEVHandle here, or else the object will never |
| 229 | // be deleted! |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 230 | static ManagedStatic<std::map<std::pair<const SCEV*, const Type*>, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 231 | SCEVZeroExtendExpr*> > SCEVZeroExtends; |
| 232 | |
| 233 | SCEVZeroExtendExpr::SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty) |
Dan Gohman | 2a38153 | 2009-04-21 01:25:57 +0000 | [diff] [blame] | 234 | : SCEVCastExpr(scZeroExtend, op, ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 235 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 236 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 237 | "Cannot zero extend non-integer value!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | SCEVZeroExtendExpr::~SCEVZeroExtendExpr() { |
| 241 | SCEVZeroExtends->erase(std::make_pair(Op, Ty)); |
| 242 | } |
| 243 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 244 | void SCEVZeroExtendExpr::print(raw_ostream &OS) const { |
Dan Gohman | c911922 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 245 | OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | // SCEVSignExtends - Only allow the creation of one SCEVSignExtendExpr for any |
| 249 | // particular input. Don't use a SCEVHandle here, or else the object will never |
| 250 | // be deleted! |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 251 | static ManagedStatic<std::map<std::pair<const SCEV*, const Type*>, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 252 | SCEVSignExtendExpr*> > SCEVSignExtends; |
| 253 | |
| 254 | SCEVSignExtendExpr::SCEVSignExtendExpr(const SCEVHandle &op, const Type *ty) |
Dan Gohman | 2a38153 | 2009-04-21 01:25:57 +0000 | [diff] [blame] | 255 | : SCEVCastExpr(scSignExtend, op, ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 256 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 257 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 258 | "Cannot sign extend non-integer value!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | SCEVSignExtendExpr::~SCEVSignExtendExpr() { |
| 262 | SCEVSignExtends->erase(std::make_pair(Op, Ty)); |
| 263 | } |
| 264 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 265 | void SCEVSignExtendExpr::print(raw_ostream &OS) const { |
Dan Gohman | c911922 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 266 | OS << "(sext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // SCEVCommExprs - Only allow the creation of one SCEVCommutativeExpr for any |
| 270 | // particular input. Don't use a SCEVHandle here, or else the object will never |
| 271 | // be deleted! |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 272 | static ManagedStatic<std::map<std::pair<unsigned, std::vector<const SCEV*> >, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 273 | SCEVCommutativeExpr*> > SCEVCommExprs; |
| 274 | |
| 275 | SCEVCommutativeExpr::~SCEVCommutativeExpr() { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 276 | std::vector<const SCEV*> SCEVOps(Operands.begin(), Operands.end()); |
| 277 | SCEVCommExprs->erase(std::make_pair(getSCEVType(), SCEVOps)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 280 | void SCEVCommutativeExpr::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 281 | assert(Operands.size() > 1 && "This plus expr shouldn't exist!"); |
| 282 | const char *OpStr = getOperationStr(); |
| 283 | OS << "(" << *Operands[0]; |
| 284 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 285 | OS << OpStr << *Operands[i]; |
| 286 | OS << ")"; |
| 287 | } |
| 288 | |
| 289 | SCEVHandle SCEVCommutativeExpr:: |
| 290 | replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym, |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 291 | const SCEVHandle &Conc, |
| 292 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 293 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 294 | SCEVHandle H = |
| 295 | getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 296 | if (H != getOperand(i)) { |
| 297 | std::vector<SCEVHandle> NewOps; |
| 298 | NewOps.reserve(getNumOperands()); |
| 299 | for (unsigned j = 0; j != i; ++j) |
| 300 | NewOps.push_back(getOperand(j)); |
| 301 | NewOps.push_back(H); |
| 302 | for (++i; i != e; ++i) |
| 303 | NewOps.push_back(getOperand(i)-> |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 304 | replaceSymbolicValuesWithConcrete(Sym, Conc, SE)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 305 | |
| 306 | if (isa<SCEVAddExpr>(this)) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 307 | return SE.getAddExpr(NewOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 308 | else if (isa<SCEVMulExpr>(this)) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 309 | return SE.getMulExpr(NewOps); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 310 | else if (isa<SCEVSMaxExpr>(this)) |
| 311 | return SE.getSMaxExpr(NewOps); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 312 | else if (isa<SCEVUMaxExpr>(this)) |
| 313 | return SE.getUMaxExpr(NewOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 314 | else |
| 315 | assert(0 && "Unknown commutative expr!"); |
| 316 | } |
| 317 | } |
| 318 | return this; |
| 319 | } |
| 320 | |
Dan Gohman | 72a8a02 | 2009-05-07 14:00:19 +0000 | [diff] [blame] | 321 | bool SCEVNAryExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 322 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 323 | if (!getOperand(i)->dominates(BB, DT)) |
| 324 | return false; |
| 325 | } |
| 326 | return true; |
| 327 | } |
| 328 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 329 | |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 330 | // SCEVUDivs - Only allow the creation of one SCEVUDivExpr for any particular |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 331 | // input. Don't use a SCEVHandle here, or else the object will never be |
| 332 | // deleted! |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 333 | static ManagedStatic<std::map<std::pair<const SCEV*, const SCEV*>, |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 334 | SCEVUDivExpr*> > SCEVUDivs; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 335 | |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 336 | SCEVUDivExpr::~SCEVUDivExpr() { |
| 337 | SCEVUDivs->erase(std::make_pair(LHS, RHS)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 340 | bool SCEVUDivExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 341 | return LHS->dominates(BB, DT) && RHS->dominates(BB, DT); |
| 342 | } |
| 343 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 344 | void SCEVUDivExpr::print(raw_ostream &OS) const { |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 345 | OS << "(" << *LHS << " /u " << *RHS << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 348 | const Type *SCEVUDivExpr::getType() const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 349 | return LHS->getType(); |
| 350 | } |
| 351 | |
| 352 | // SCEVAddRecExprs - Only allow the creation of one SCEVAddRecExpr for any |
| 353 | // particular input. Don't use a SCEVHandle here, or else the object will never |
| 354 | // be deleted! |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 355 | static ManagedStatic<std::map<std::pair<const Loop *, |
| 356 | std::vector<const SCEV*> >, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 357 | SCEVAddRecExpr*> > SCEVAddRecExprs; |
| 358 | |
| 359 | SCEVAddRecExpr::~SCEVAddRecExpr() { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 360 | std::vector<const SCEV*> SCEVOps(Operands.begin(), Operands.end()); |
| 361 | SCEVAddRecExprs->erase(std::make_pair(L, SCEVOps)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | SCEVHandle SCEVAddRecExpr:: |
| 365 | replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym, |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 366 | const SCEVHandle &Conc, |
| 367 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 368 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 369 | SCEVHandle H = |
| 370 | getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 371 | if (H != getOperand(i)) { |
| 372 | std::vector<SCEVHandle> NewOps; |
| 373 | NewOps.reserve(getNumOperands()); |
| 374 | for (unsigned j = 0; j != i; ++j) |
| 375 | NewOps.push_back(getOperand(j)); |
| 376 | NewOps.push_back(H); |
| 377 | for (++i; i != e; ++i) |
| 378 | NewOps.push_back(getOperand(i)-> |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 379 | replaceSymbolicValuesWithConcrete(Sym, Conc, SE)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 380 | |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 381 | return SE.getAddRecExpr(NewOps, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | return this; |
| 385 | } |
| 386 | |
| 387 | |
| 388 | bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const { |
| 389 | // This recurrence is invariant w.r.t to QueryLoop iff QueryLoop doesn't |
| 390 | // contain L and if the start is invariant. |
| 391 | return !QueryLoop->contains(L->getHeader()) && |
| 392 | getOperand(0)->isLoopInvariant(QueryLoop); |
| 393 | } |
| 394 | |
| 395 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 396 | void SCEVAddRecExpr::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 397 | OS << "{" << *Operands[0]; |
| 398 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 399 | OS << ",+," << *Operands[i]; |
| 400 | OS << "}<" << L->getHeader()->getName() + ">"; |
| 401 | } |
| 402 | |
| 403 | // SCEVUnknowns - Only allow the creation of one SCEVUnknown for any particular |
| 404 | // value. Don't use a SCEVHandle here, or else the object will never be |
| 405 | // deleted! |
| 406 | static ManagedStatic<std::map<Value*, SCEVUnknown*> > SCEVUnknowns; |
| 407 | |
| 408 | SCEVUnknown::~SCEVUnknown() { SCEVUnknowns->erase(V); } |
| 409 | |
| 410 | bool SCEVUnknown::isLoopInvariant(const Loop *L) const { |
| 411 | // All non-instruction values are loop invariant. All instructions are loop |
| 412 | // invariant if they are not contained in the specified loop. |
| 413 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 414 | return !L->contains(I->getParent()); |
| 415 | return true; |
| 416 | } |
| 417 | |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 418 | bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 419 | if (Instruction *I = dyn_cast<Instruction>(getValue())) |
| 420 | return DT->dominates(I->getParent(), BB); |
| 421 | return true; |
| 422 | } |
| 423 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 424 | const Type *SCEVUnknown::getType() const { |
| 425 | return V->getType(); |
| 426 | } |
| 427 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 428 | void SCEVUnknown::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 429 | WriteAsOperand(OS, V, false); |
| 430 | } |
| 431 | |
| 432 | //===----------------------------------------------------------------------===// |
| 433 | // SCEV Utilities |
| 434 | //===----------------------------------------------------------------------===// |
| 435 | |
| 436 | namespace { |
| 437 | /// SCEVComplexityCompare - Return true if the complexity of the LHS is less |
| 438 | /// than the complexity of the RHS. This comparator is used to canonicalize |
| 439 | /// expressions. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 440 | class VISIBILITY_HIDDEN SCEVComplexityCompare { |
| 441 | LoopInfo *LI; |
| 442 | public: |
| 443 | explicit SCEVComplexityCompare(LoopInfo *li) : LI(li) {} |
| 444 | |
Dan Gohman | c0c69cf | 2008-04-14 18:23:56 +0000 | [diff] [blame] | 445 | bool operator()(const SCEV *LHS, const SCEV *RHS) const { |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 446 | // Primarily, sort the SCEVs by their getSCEVType(). |
| 447 | if (LHS->getSCEVType() != RHS->getSCEVType()) |
| 448 | return LHS->getSCEVType() < RHS->getSCEVType(); |
| 449 | |
| 450 | // Aside from the getSCEVType() ordering, the particular ordering |
| 451 | // isn't very important except that it's beneficial to be consistent, |
| 452 | // so that (a + b) and (b + a) don't end up as different expressions. |
| 453 | |
| 454 | // Sort SCEVUnknown values with some loose heuristics. TODO: This is |
| 455 | // not as complete as it could be. |
| 456 | if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) { |
| 457 | const SCEVUnknown *RU = cast<SCEVUnknown>(RHS); |
| 458 | |
| 459 | // Compare getValueID values. |
| 460 | if (LU->getValue()->getValueID() != RU->getValue()->getValueID()) |
| 461 | return LU->getValue()->getValueID() < RU->getValue()->getValueID(); |
| 462 | |
| 463 | // Sort arguments by their position. |
| 464 | if (const Argument *LA = dyn_cast<Argument>(LU->getValue())) { |
| 465 | const Argument *RA = cast<Argument>(RU->getValue()); |
| 466 | return LA->getArgNo() < RA->getArgNo(); |
| 467 | } |
| 468 | |
| 469 | // For instructions, compare their loop depth, and their opcode. |
| 470 | // This is pretty loose. |
| 471 | if (Instruction *LV = dyn_cast<Instruction>(LU->getValue())) { |
| 472 | Instruction *RV = cast<Instruction>(RU->getValue()); |
| 473 | |
| 474 | // Compare loop depths. |
| 475 | if (LI->getLoopDepth(LV->getParent()) != |
| 476 | LI->getLoopDepth(RV->getParent())) |
| 477 | return LI->getLoopDepth(LV->getParent()) < |
| 478 | LI->getLoopDepth(RV->getParent()); |
| 479 | |
| 480 | // Compare opcodes. |
| 481 | if (LV->getOpcode() != RV->getOpcode()) |
| 482 | return LV->getOpcode() < RV->getOpcode(); |
| 483 | |
| 484 | // Compare the number of operands. |
| 485 | if (LV->getNumOperands() != RV->getNumOperands()) |
| 486 | return LV->getNumOperands() < RV->getNumOperands(); |
| 487 | } |
| 488 | |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | // Constant sorting doesn't matter since they'll be folded. |
| 493 | if (isa<SCEVConstant>(LHS)) |
| 494 | return false; |
| 495 | |
| 496 | // Lexicographically compare n-ary expressions. |
| 497 | if (const SCEVNAryExpr *LC = dyn_cast<SCEVNAryExpr>(LHS)) { |
| 498 | const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS); |
| 499 | for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) { |
| 500 | if (i >= RC->getNumOperands()) |
| 501 | return false; |
| 502 | if (operator()(LC->getOperand(i), RC->getOperand(i))) |
| 503 | return true; |
| 504 | if (operator()(RC->getOperand(i), LC->getOperand(i))) |
| 505 | return false; |
| 506 | } |
| 507 | return LC->getNumOperands() < RC->getNumOperands(); |
| 508 | } |
| 509 | |
Dan Gohman | 6e10db1 | 2009-05-07 19:23:21 +0000 | [diff] [blame] | 510 | // Lexicographically compare udiv expressions. |
| 511 | if (const SCEVUDivExpr *LC = dyn_cast<SCEVUDivExpr>(LHS)) { |
| 512 | const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS); |
| 513 | if (operator()(LC->getLHS(), RC->getLHS())) |
| 514 | return true; |
| 515 | if (operator()(RC->getLHS(), LC->getLHS())) |
| 516 | return false; |
| 517 | if (operator()(LC->getRHS(), RC->getRHS())) |
| 518 | return true; |
| 519 | if (operator()(RC->getRHS(), LC->getRHS())) |
| 520 | return false; |
| 521 | return false; |
| 522 | } |
| 523 | |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 524 | // Compare cast expressions by operand. |
| 525 | if (const SCEVCastExpr *LC = dyn_cast<SCEVCastExpr>(LHS)) { |
| 526 | const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS); |
| 527 | return operator()(LC->getOperand(), RC->getOperand()); |
| 528 | } |
| 529 | |
| 530 | assert(0 && "Unknown SCEV kind!"); |
| 531 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 532 | } |
| 533 | }; |
| 534 | } |
| 535 | |
| 536 | /// GroupByComplexity - Given a list of SCEV objects, order them by their |
| 537 | /// complexity, and group objects of the same complexity together by value. |
| 538 | /// When this routine is finished, we know that any duplicates in the vector are |
| 539 | /// consecutive and that complexity is monotonically increasing. |
| 540 | /// |
| 541 | /// Note that we go take special precautions to ensure that we get determinstic |
| 542 | /// results from this routine. In other words, we don't want the results of |
| 543 | /// this to depend on where the addresses of various SCEV objects happened to |
| 544 | /// land in memory. |
| 545 | /// |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 546 | static void GroupByComplexity(std::vector<SCEVHandle> &Ops, |
| 547 | LoopInfo *LI) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 548 | if (Ops.size() < 2) return; // Noop |
| 549 | if (Ops.size() == 2) { |
| 550 | // This is the common case, which also happens to be trivially simple. |
| 551 | // Special case it. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 552 | if (SCEVComplexityCompare(LI)(Ops[1], Ops[0])) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 553 | std::swap(Ops[0], Ops[1]); |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | // Do the rough sort by complexity. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 558 | std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 559 | |
| 560 | // Now that we are sorted by complexity, group elements of the same |
| 561 | // complexity. Note that this is, at worst, N^2, but the vector is likely to |
| 562 | // be extremely short in practice. Note that we take this approach because we |
| 563 | // do not want to depend on the addresses of the objects we are grouping. |
| 564 | for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 565 | const SCEV *S = Ops[i]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 566 | unsigned Complexity = S->getSCEVType(); |
| 567 | |
| 568 | // If there are any objects of the same complexity and same value as this |
| 569 | // one, group them. |
| 570 | for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { |
| 571 | if (Ops[j] == S) { // Found a duplicate. |
| 572 | // Move it to immediately after i'th element. |
| 573 | std::swap(Ops[i+1], Ops[j]); |
| 574 | ++i; // no need to rescan it. |
| 575 | if (i == e-2) return; // Done! |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | |
| 582 | |
| 583 | //===----------------------------------------------------------------------===// |
| 584 | // Simple SCEV method implementations |
| 585 | //===----------------------------------------------------------------------===// |
| 586 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 587 | /// BinomialCoefficient - Compute BC(It, K). The result has width W. |
| 588 | // Assume, K > 0. |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 589 | static SCEVHandle BinomialCoefficient(SCEVHandle It, unsigned K, |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 590 | ScalarEvolution &SE, |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 591 | const Type* ResultTy) { |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 592 | // Handle the simplest case efficiently. |
| 593 | if (K == 1) |
| 594 | return SE.getTruncateOrZeroExtend(It, ResultTy); |
| 595 | |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 596 | // We are using the following formula for BC(It, K): |
| 597 | // |
| 598 | // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! |
| 599 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 600 | // Suppose, W is the bitwidth of the return value. We must be prepared for |
| 601 | // overflow. Hence, we must assure that the result of our computation is |
| 602 | // equal to the accurate one modulo 2^W. Unfortunately, division isn't |
| 603 | // safe in modular arithmetic. |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 604 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 605 | // However, this code doesn't use exactly that formula; the formula it uses |
| 606 | // is something like the following, where T is the number of factors of 2 in |
| 607 | // K! (i.e. trailing zeros in the binary representation of K!), and ^ is |
| 608 | // exponentiation: |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 609 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 610 | // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T) |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 611 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 612 | // This formula is trivially equivalent to the previous formula. However, |
| 613 | // this formula can be implemented much more efficiently. The trick is that |
| 614 | // K! / 2^T is odd, and exact division by an odd number *is* safe in modular |
| 615 | // arithmetic. To do exact division in modular arithmetic, all we have |
| 616 | // to do is multiply by the inverse. Therefore, this step can be done at |
| 617 | // width W. |
| 618 | // |
| 619 | // The next issue is how to safely do the division by 2^T. The way this |
| 620 | // is done is by doing the multiplication step at a width of at least W + T |
| 621 | // bits. This way, the bottom W+T bits of the product are accurate. Then, |
| 622 | // when we perform the division by 2^T (which is equivalent to a right shift |
| 623 | // by T), the bottom W bits are accurate. Extra bits are okay; they'll get |
| 624 | // truncated out after the division by 2^T. |
| 625 | // |
| 626 | // In comparison to just directly using the first formula, this technique |
| 627 | // is much more efficient; using the first formula requires W * K bits, |
| 628 | // but this formula less than W + K bits. Also, the first formula requires |
| 629 | // a division step, whereas this formula only requires multiplies and shifts. |
| 630 | // |
| 631 | // It doesn't matter whether the subtraction step is done in the calculation |
| 632 | // width or the input iteration count's width; if the subtraction overflows, |
| 633 | // the result must be zero anyway. We prefer here to do it in the width of |
| 634 | // the induction variable because it helps a lot for certain cases; CodeGen |
| 635 | // isn't smart enough to ignore the overflow, which leads to much less |
| 636 | // efficient code if the width of the subtraction is wider than the native |
| 637 | // register width. |
| 638 | // |
| 639 | // (It's possible to not widen at all by pulling out factors of 2 before |
| 640 | // the multiplication; for example, K=2 can be calculated as |
| 641 | // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires |
| 642 | // extra arithmetic, so it's not an obvious win, and it gets |
| 643 | // much more complicated for K > 3.) |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 644 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 645 | // Protection from insane SCEVs; this bound is conservative, |
| 646 | // but it probably doesn't matter. |
| 647 | if (K > 1000) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 648 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 649 | |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 650 | unsigned W = SE.getTypeSizeInBits(ResultTy); |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 651 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 652 | // Calculate K! / 2^T and T; we divide out the factors of two before |
| 653 | // multiplying for calculating K! / 2^T to avoid overflow. |
| 654 | // Other overflow doesn't matter because we only care about the bottom |
| 655 | // W bits of the result. |
| 656 | APInt OddFactorial(W, 1); |
| 657 | unsigned T = 1; |
| 658 | for (unsigned i = 3; i <= K; ++i) { |
| 659 | APInt Mult(W, i); |
| 660 | unsigned TwoFactors = Mult.countTrailingZeros(); |
| 661 | T += TwoFactors; |
| 662 | Mult = Mult.lshr(TwoFactors); |
| 663 | OddFactorial *= Mult; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 664 | } |
Nick Lewycky | dbaa60a | 2008-06-13 04:38:55 +0000 | [diff] [blame] | 665 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 666 | // We need at least W + T bits for the multiplication step |
nicholas | 9e3e5fd | 2009-01-25 08:16:27 +0000 | [diff] [blame] | 667 | unsigned CalculationBits = W + T; |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 668 | |
| 669 | // Calcuate 2^T, at width T+W. |
| 670 | APInt DivFactor = APInt(CalculationBits, 1).shl(T); |
| 671 | |
| 672 | // Calculate the multiplicative inverse of K! / 2^T; |
| 673 | // this multiplication factor will perform the exact division by |
| 674 | // K! / 2^T. |
| 675 | APInt Mod = APInt::getSignedMinValue(W+1); |
| 676 | APInt MultiplyFactor = OddFactorial.zext(W+1); |
| 677 | MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); |
| 678 | MultiplyFactor = MultiplyFactor.trunc(W); |
| 679 | |
| 680 | // Calculate the product, at width T+W |
| 681 | const IntegerType *CalculationTy = IntegerType::get(CalculationBits); |
| 682 | SCEVHandle Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); |
| 683 | for (unsigned i = 1; i != K; ++i) { |
| 684 | SCEVHandle S = SE.getMinusSCEV(It, SE.getIntegerSCEV(i, It->getType())); |
| 685 | Dividend = SE.getMulExpr(Dividend, |
| 686 | SE.getTruncateOrZeroExtend(S, CalculationTy)); |
| 687 | } |
| 688 | |
| 689 | // Divide by 2^T |
| 690 | SCEVHandle DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); |
| 691 | |
| 692 | // Truncate the result, and divide by K! / 2^T. |
| 693 | |
| 694 | return SE.getMulExpr(SE.getConstant(MultiplyFactor), |
| 695 | SE.getTruncateOrZeroExtend(DivResult, ResultTy)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 698 | /// evaluateAtIteration - Return the value of this chain of recurrences at |
| 699 | /// the specified iteration number. We can evaluate this recurrence by |
| 700 | /// multiplying each element in the chain by the binomial coefficient |
| 701 | /// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as: |
| 702 | /// |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 703 | /// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 704 | /// |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 705 | /// where BC(It, k) stands for binomial coefficient. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 706 | /// |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 707 | SCEVHandle SCEVAddRecExpr::evaluateAtIteration(SCEVHandle It, |
| 708 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 709 | SCEVHandle Result = getStart(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 710 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 711 | // The computation is correct in the face of overflow provided that the |
| 712 | // multiplication is performed _after_ the evaluation of the binomial |
| 713 | // coefficient. |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 714 | SCEVHandle Coeff = BinomialCoefficient(It, i, SE, getType()); |
Nick Lewycky | b6218e0 | 2008-10-13 03:58:02 +0000 | [diff] [blame] | 715 | if (isa<SCEVCouldNotCompute>(Coeff)) |
| 716 | return Coeff; |
| 717 | |
| 718 | Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 719 | } |
| 720 | return Result; |
| 721 | } |
| 722 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 723 | //===----------------------------------------------------------------------===// |
| 724 | // SCEV Expression folder implementations |
| 725 | //===----------------------------------------------------------------------===// |
| 726 | |
Dan Gohman | 9c8abcc | 2009-05-01 16:44:56 +0000 | [diff] [blame] | 727 | SCEVHandle ScalarEvolution::getTruncateExpr(const SCEVHandle &Op, |
| 728 | const Type *Ty) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 729 | assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 730 | "This is not a truncating conversion!"); |
Dan Gohman | 13a51e2 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 731 | assert(isSCEVable(Ty) && |
| 732 | "This is not a conversion to a SCEVable type!"); |
| 733 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 734 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 735 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 736 | return getUnknown( |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 737 | ConstantExpr::getTrunc(SC->getValue(), Ty)); |
| 738 | |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 739 | // trunc(trunc(x)) --> trunc(x) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 740 | if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 741 | return getTruncateExpr(ST->getOperand(), Ty); |
| 742 | |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 743 | // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 744 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 745 | return getTruncateOrSignExtend(SS->getOperand(), Ty); |
| 746 | |
| 747 | // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 748 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 749 | return getTruncateOrZeroExtend(SZ->getOperand(), Ty); |
| 750 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 751 | // If the input value is a chrec scev made out of constants, truncate |
| 752 | // all of the constants. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 753 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 754 | std::vector<SCEVHandle> Operands; |
| 755 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 756 | Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty)); |
| 757 | return getAddRecExpr(Operands, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | SCEVTruncateExpr *&Result = (*SCEVTruncates)[std::make_pair(Op, Ty)]; |
| 761 | if (Result == 0) Result = new SCEVTruncateExpr(Op, Ty); |
| 762 | return Result; |
| 763 | } |
| 764 | |
Dan Gohman | 36d4092 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 765 | SCEVHandle ScalarEvolution::getZeroExtendExpr(const SCEVHandle &Op, |
| 766 | const Type *Ty) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 767 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | 36d4092 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 768 | "This is not an extending conversion!"); |
Dan Gohman | 13a51e2 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 769 | assert(isSCEVable(Ty) && |
| 770 | "This is not a conversion to a SCEVable type!"); |
| 771 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | 36d4092 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 772 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 773 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 774 | const Type *IntTy = getEffectiveSCEVType(Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 775 | Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy); |
| 776 | if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); |
| 777 | return getUnknown(C); |
| 778 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 779 | |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 780 | // zext(zext(x)) --> zext(x) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 781 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 782 | return getZeroExtendExpr(SZ->getOperand(), Ty); |
| 783 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 784 | // If the input value is a chrec scev, and we can prove that the value |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 785 | // did not overflow the old, smaller, value, we can zero extend all of the |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 786 | // operands (often constants). This allows analysis of something like |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 787 | // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 788 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 789 | if (AR->isAffine()) { |
| 790 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 791 | // Note that this serves two purposes: It filters out loops that are |
| 792 | // simply not analyzable, and it covers the case where this code is |
| 793 | // being called from within backedge-taken count analysis, such that |
| 794 | // attempting to ask for the backedge-taken count would likely result |
| 795 | // in infinite recursion. In the later case, the analysis code will |
| 796 | // cope with a conservative value, and it will take care to purge |
| 797 | // that value once it has finished. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 798 | SCEVHandle MaxBECount = getMaxBackedgeTakenCount(AR->getLoop()); |
| 799 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | 4ada77f | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 800 | // Manually compute the final value for AR, checking for |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 801 | // overflow. |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 802 | SCEVHandle Start = AR->getStart(); |
| 803 | SCEVHandle Step = AR->getStepRecurrence(*this); |
| 804 | |
| 805 | // Check whether the backedge-taken count can be losslessly casted to |
| 806 | // the addrec's type. The count is always unsigned. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 807 | SCEVHandle CastedMaxBECount = |
| 808 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
| 809 | if (MaxBECount == |
| 810 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType())) { |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 811 | const Type *WideTy = |
| 812 | IntegerType::get(getTypeSizeInBits(Start->getType()) * 2); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 813 | // Check whether Start+Step*MaxBECount has no unsigned overflow. |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 814 | SCEVHandle ZMul = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 815 | getMulExpr(CastedMaxBECount, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 816 | getTruncateOrZeroExtend(Step, Start->getType())); |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 817 | SCEVHandle Add = getAddExpr(Start, ZMul); |
| 818 | if (getZeroExtendExpr(Add, WideTy) == |
| 819 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 820 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 821 | getZeroExtendExpr(Step, WideTy)))) |
| 822 | // Return the expression with the addrec on the outside. |
| 823 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 824 | getZeroExtendExpr(Step, Ty), |
| 825 | AR->getLoop()); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 826 | |
| 827 | // Similar to above, only this time treat the step value as signed. |
| 828 | // This covers loops that count down. |
| 829 | SCEVHandle SMul = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 830 | getMulExpr(CastedMaxBECount, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 831 | getTruncateOrSignExtend(Step, Start->getType())); |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 832 | Add = getAddExpr(Start, SMul); |
| 833 | if (getZeroExtendExpr(Add, WideTy) == |
| 834 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 835 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 836 | getSignExtendExpr(Step, WideTy)))) |
| 837 | // Return the expression with the addrec on the outside. |
| 838 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 839 | getSignExtendExpr(Step, Ty), |
| 840 | AR->getLoop()); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 844 | |
| 845 | SCEVZeroExtendExpr *&Result = (*SCEVZeroExtends)[std::make_pair(Op, Ty)]; |
| 846 | if (Result == 0) Result = new SCEVZeroExtendExpr(Op, Ty); |
| 847 | return Result; |
| 848 | } |
| 849 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 850 | SCEVHandle ScalarEvolution::getSignExtendExpr(const SCEVHandle &Op, |
| 851 | const Type *Ty) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 852 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 853 | "This is not an extending conversion!"); |
Dan Gohman | 13a51e2 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 854 | assert(isSCEVable(Ty) && |
| 855 | "This is not a conversion to a SCEVable type!"); |
| 856 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 857 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 858 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 859 | const Type *IntTy = getEffectiveSCEVType(Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 860 | Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy); |
| 861 | if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); |
| 862 | return getUnknown(C); |
| 863 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 864 | |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 865 | // sext(sext(x)) --> sext(x) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 866 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 867 | return getSignExtendExpr(SS->getOperand(), Ty); |
| 868 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 869 | // If the input value is a chrec scev, and we can prove that the value |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 870 | // did not overflow the old, smaller, value, we can sign extend all of the |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 871 | // operands (often constants). This allows analysis of something like |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 872 | // this: for (signed char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 873 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 874 | if (AR->isAffine()) { |
| 875 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 876 | // Note that this serves two purposes: It filters out loops that are |
| 877 | // simply not analyzable, and it covers the case where this code is |
| 878 | // being called from within backedge-taken count analysis, such that |
| 879 | // attempting to ask for the backedge-taken count would likely result |
| 880 | // in infinite recursion. In the later case, the analysis code will |
| 881 | // cope with a conservative value, and it will take care to purge |
| 882 | // that value once it has finished. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 883 | SCEVHandle MaxBECount = getMaxBackedgeTakenCount(AR->getLoop()); |
| 884 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | 4ada77f | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 885 | // Manually compute the final value for AR, checking for |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 886 | // overflow. |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 887 | SCEVHandle Start = AR->getStart(); |
| 888 | SCEVHandle Step = AR->getStepRecurrence(*this); |
| 889 | |
| 890 | // Check whether the backedge-taken count can be losslessly casted to |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 891 | // the addrec's type. The count is always unsigned. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 892 | SCEVHandle CastedMaxBECount = |
| 893 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
| 894 | if (MaxBECount == |
| 895 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType())) { |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 896 | const Type *WideTy = |
| 897 | IntegerType::get(getTypeSizeInBits(Start->getType()) * 2); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 898 | // Check whether Start+Step*MaxBECount has no signed overflow. |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 899 | SCEVHandle SMul = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 900 | getMulExpr(CastedMaxBECount, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 901 | getTruncateOrSignExtend(Step, Start->getType())); |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 902 | SCEVHandle Add = getAddExpr(Start, SMul); |
| 903 | if (getSignExtendExpr(Add, WideTy) == |
| 904 | getAddExpr(getSignExtendExpr(Start, WideTy), |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 905 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 906 | getSignExtendExpr(Step, WideTy)))) |
| 907 | // Return the expression with the addrec on the outside. |
| 908 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 909 | getSignExtendExpr(Step, Ty), |
| 910 | AR->getLoop()); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 914 | |
| 915 | SCEVSignExtendExpr *&Result = (*SCEVSignExtends)[std::make_pair(Op, Ty)]; |
| 916 | if (Result == 0) Result = new SCEVSignExtendExpr(Op, Ty); |
| 917 | return Result; |
| 918 | } |
| 919 | |
| 920 | // get - Get a canonical add expression, or something simpler if possible. |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 921 | SCEVHandle ScalarEvolution::getAddExpr(std::vector<SCEVHandle> &Ops) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 922 | assert(!Ops.empty() && "Cannot get empty add!"); |
| 923 | if (Ops.size() == 1) return Ops[0]; |
| 924 | |
| 925 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 926 | GroupByComplexity(Ops, LI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 927 | |
| 928 | // If there are any constants, fold them together. |
| 929 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 930 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 931 | ++Idx; |
| 932 | assert(Idx < Ops.size()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 933 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 934 | // We found two constants, fold them together! |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 935 | ConstantInt *Fold = ConstantInt::get(LHSC->getValue()->getValue() + |
| 936 | RHSC->getValue()->getValue()); |
| 937 | Ops[0] = getConstant(Fold); |
| 938 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 939 | if (Ops.size() == 1) return Ops[0]; |
| 940 | LHSC = cast<SCEVConstant>(Ops[0]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | // If we are left with a constant zero being added, strip it off. |
| 944 | if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
| 945 | Ops.erase(Ops.begin()); |
| 946 | --Idx; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | if (Ops.size() == 1) return Ops[0]; |
| 951 | |
| 952 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 953 | // so, merge them together into an multiply expression. Since we sorted the |
| 954 | // list, these values are required to be adjacent. |
| 955 | const Type *Ty = Ops[0]->getType(); |
| 956 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 957 | if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2 |
| 958 | // Found a match, merge the two values into a multiply, and add any |
| 959 | // remaining values to the result. |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 960 | SCEVHandle Two = getIntegerSCEV(2, Ty); |
| 961 | SCEVHandle Mul = getMulExpr(Ops[i], Two); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 962 | if (Ops.size() == 2) |
| 963 | return Mul; |
| 964 | Ops.erase(Ops.begin()+i, Ops.begin()+i+2); |
| 965 | Ops.push_back(Mul); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 966 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 969 | // Check for truncates. If all the operands are truncated from the same |
| 970 | // type, see if factoring out the truncate would permit the result to be |
| 971 | // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n) |
| 972 | // if the contents of the resulting outer trunc fold to something simple. |
| 973 | for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) { |
| 974 | const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]); |
| 975 | const Type *DstType = Trunc->getType(); |
| 976 | const Type *SrcType = Trunc->getOperand()->getType(); |
| 977 | std::vector<SCEVHandle> LargeOps; |
| 978 | bool Ok = true; |
| 979 | // Check all the operands to see if they can be represented in the |
| 980 | // source type of the truncate. |
| 981 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 982 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) { |
| 983 | if (T->getOperand()->getType() != SrcType) { |
| 984 | Ok = false; |
| 985 | break; |
| 986 | } |
| 987 | LargeOps.push_back(T->getOperand()); |
| 988 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| 989 | // This could be either sign or zero extension, but sign extension |
| 990 | // is much more likely to be foldable here. |
| 991 | LargeOps.push_back(getSignExtendExpr(C, SrcType)); |
| 992 | } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) { |
| 993 | std::vector<SCEVHandle> LargeMulOps; |
| 994 | for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { |
| 995 | if (const SCEVTruncateExpr *T = |
| 996 | dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) { |
| 997 | if (T->getOperand()->getType() != SrcType) { |
| 998 | Ok = false; |
| 999 | break; |
| 1000 | } |
| 1001 | LargeMulOps.push_back(T->getOperand()); |
| 1002 | } else if (const SCEVConstant *C = |
| 1003 | dyn_cast<SCEVConstant>(M->getOperand(j))) { |
| 1004 | // This could be either sign or zero extension, but sign extension |
| 1005 | // is much more likely to be foldable here. |
| 1006 | LargeMulOps.push_back(getSignExtendExpr(C, SrcType)); |
| 1007 | } else { |
| 1008 | Ok = false; |
| 1009 | break; |
| 1010 | } |
| 1011 | } |
| 1012 | if (Ok) |
| 1013 | LargeOps.push_back(getMulExpr(LargeMulOps)); |
| 1014 | } else { |
| 1015 | Ok = false; |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
| 1019 | if (Ok) { |
| 1020 | // Evaluate the expression in the larger type. |
| 1021 | SCEVHandle Fold = getAddExpr(LargeOps); |
| 1022 | // If it folds to something simple, use it. Otherwise, don't. |
| 1023 | if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold)) |
| 1024 | return getTruncateExpr(Fold, DstType); |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | // Skip past any other cast SCEVs. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1029 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) |
| 1030 | ++Idx; |
| 1031 | |
| 1032 | // If there are add operands they would be next. |
| 1033 | if (Idx < Ops.size()) { |
| 1034 | bool DeletedAdd = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1035 | while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1036 | // If we have an add, expand the add operands onto the end of the operands |
| 1037 | // list. |
| 1038 | Ops.insert(Ops.end(), Add->op_begin(), Add->op_end()); |
| 1039 | Ops.erase(Ops.begin()+Idx); |
| 1040 | DeletedAdd = true; |
| 1041 | } |
| 1042 | |
| 1043 | // If we deleted at least one add, we added operands to the end of the list, |
| 1044 | // and they are not necessarily sorted. Recurse to resort and resimplify |
| 1045 | // any operands we just aquired. |
| 1046 | if (DeletedAdd) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1047 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | // Skip over the add expression until we get to a multiply. |
| 1051 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1052 | ++Idx; |
| 1053 | |
| 1054 | // If we are adding something to a multiply expression, make sure the |
| 1055 | // something is not already an operand of the multiply. If so, merge it into |
| 1056 | // the multiply. |
| 1057 | for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1058 | const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1059 | for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1060 | const SCEV *MulOpSCEV = Mul->getOperand(MulOp); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1061 | for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) |
| 1062 | if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(MulOpSCEV)) { |
| 1063 | // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) |
| 1064 | SCEVHandle InnerMul = Mul->getOperand(MulOp == 0); |
| 1065 | if (Mul->getNumOperands() != 2) { |
| 1066 | // If the multiply has more than two operands, we must get the |
| 1067 | // Y*Z term. |
| 1068 | std::vector<SCEVHandle> MulOps(Mul->op_begin(), Mul->op_end()); |
| 1069 | MulOps.erase(MulOps.begin()+MulOp); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1070 | InnerMul = getMulExpr(MulOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1071 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1072 | SCEVHandle One = getIntegerSCEV(1, Ty); |
| 1073 | SCEVHandle AddOne = getAddExpr(InnerMul, One); |
| 1074 | SCEVHandle OuterMul = getMulExpr(AddOne, Ops[AddOp]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1075 | if (Ops.size() == 2) return OuterMul; |
| 1076 | if (AddOp < Idx) { |
| 1077 | Ops.erase(Ops.begin()+AddOp); |
| 1078 | Ops.erase(Ops.begin()+Idx-1); |
| 1079 | } else { |
| 1080 | Ops.erase(Ops.begin()+Idx); |
| 1081 | Ops.erase(Ops.begin()+AddOp-1); |
| 1082 | } |
| 1083 | Ops.push_back(OuterMul); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1084 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | // Check this multiply against other multiplies being added together. |
| 1088 | for (unsigned OtherMulIdx = Idx+1; |
| 1089 | OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]); |
| 1090 | ++OtherMulIdx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1091 | const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1092 | // If MulOp occurs in OtherMul, we can fold the two multiplies |
| 1093 | // together. |
| 1094 | for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); |
| 1095 | OMulOp != e; ++OMulOp) |
| 1096 | if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { |
| 1097 | // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E)) |
| 1098 | SCEVHandle InnerMul1 = Mul->getOperand(MulOp == 0); |
| 1099 | if (Mul->getNumOperands() != 2) { |
| 1100 | std::vector<SCEVHandle> MulOps(Mul->op_begin(), Mul->op_end()); |
| 1101 | MulOps.erase(MulOps.begin()+MulOp); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1102 | InnerMul1 = getMulExpr(MulOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1103 | } |
| 1104 | SCEVHandle InnerMul2 = OtherMul->getOperand(OMulOp == 0); |
| 1105 | if (OtherMul->getNumOperands() != 2) { |
| 1106 | std::vector<SCEVHandle> MulOps(OtherMul->op_begin(), |
| 1107 | OtherMul->op_end()); |
| 1108 | MulOps.erase(MulOps.begin()+OMulOp); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1109 | InnerMul2 = getMulExpr(MulOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1110 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1111 | SCEVHandle InnerMulSum = getAddExpr(InnerMul1,InnerMul2); |
| 1112 | SCEVHandle OuterMul = getMulExpr(MulOpSCEV, InnerMulSum); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1113 | if (Ops.size() == 2) return OuterMul; |
| 1114 | Ops.erase(Ops.begin()+Idx); |
| 1115 | Ops.erase(Ops.begin()+OtherMulIdx-1); |
| 1116 | Ops.push_back(OuterMul); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1117 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | // If there are any add recurrences in the operands list, see if any other |
| 1124 | // added values are loop invariant. If so, we can fold them into the |
| 1125 | // recurrence. |
| 1126 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1127 | ++Idx; |
| 1128 | |
| 1129 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1130 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1131 | // Scan all of the other operands to this add and add them to the vector if |
| 1132 | // they are loop invariant w.r.t. the recurrence. |
| 1133 | std::vector<SCEVHandle> LIOps; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1134 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1135 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1136 | if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { |
| 1137 | LIOps.push_back(Ops[i]); |
| 1138 | Ops.erase(Ops.begin()+i); |
| 1139 | --i; --e; |
| 1140 | } |
| 1141 | |
| 1142 | // If we found some loop invariants, fold them into the recurrence. |
| 1143 | if (!LIOps.empty()) { |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1144 | // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1145 | LIOps.push_back(AddRec->getStart()); |
| 1146 | |
| 1147 | std::vector<SCEVHandle> AddRecOps(AddRec->op_begin(), AddRec->op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1148 | AddRecOps[0] = getAddExpr(LIOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1149 | |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1150 | SCEVHandle NewRec = getAddRecExpr(AddRecOps, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1151 | // If all of the other operands were loop invariant, we are done. |
| 1152 | if (Ops.size() == 1) return NewRec; |
| 1153 | |
| 1154 | // Otherwise, add the folded AddRec by the non-liv parts. |
| 1155 | for (unsigned i = 0;; ++i) |
| 1156 | if (Ops[i] == AddRec) { |
| 1157 | Ops[i] = NewRec; |
| 1158 | break; |
| 1159 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1160 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1164 | // there are multiple AddRec's with the same loop induction variable being |
| 1165 | // added together. If so, we can fold them. |
| 1166 | for (unsigned OtherIdx = Idx+1; |
| 1167 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) |
| 1168 | if (OtherIdx != Idx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1169 | const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1170 | if (AddRec->getLoop() == OtherAddRec->getLoop()) { |
| 1171 | // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D} |
| 1172 | std::vector<SCEVHandle> NewOps(AddRec->op_begin(), AddRec->op_end()); |
| 1173 | for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) { |
| 1174 | if (i >= NewOps.size()) { |
| 1175 | NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i, |
| 1176 | OtherAddRec->op_end()); |
| 1177 | break; |
| 1178 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1179 | NewOps[i] = getAddExpr(NewOps[i], OtherAddRec->getOperand(i)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1180 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1181 | SCEVHandle NewAddRec = getAddRecExpr(NewOps, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1182 | |
| 1183 | if (Ops.size() == 2) return NewAddRec; |
| 1184 | |
| 1185 | Ops.erase(Ops.begin()+Idx); |
| 1186 | Ops.erase(Ops.begin()+OtherIdx-1); |
| 1187 | Ops.push_back(NewAddRec); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1188 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1193 | // next one. |
| 1194 | } |
| 1195 | |
| 1196 | // Okay, it looks like we really DO need an add expr. Check to see if we |
| 1197 | // already have one, otherwise create a new one. |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1198 | std::vector<const SCEV*> SCEVOps(Ops.begin(), Ops.end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1199 | SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scAddExpr, |
| 1200 | SCEVOps)]; |
| 1201 | if (Result == 0) Result = new SCEVAddExpr(Ops); |
| 1202 | return Result; |
| 1203 | } |
| 1204 | |
| 1205 | |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1206 | SCEVHandle ScalarEvolution::getMulExpr(std::vector<SCEVHandle> &Ops) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1207 | assert(!Ops.empty() && "Cannot get empty mul!"); |
| 1208 | |
| 1209 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1210 | GroupByComplexity(Ops, LI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1211 | |
| 1212 | // If there are any constants, fold them together. |
| 1213 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1214 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1215 | |
| 1216 | // C1*(C2+V) -> C1*C2 + C1*V |
| 1217 | if (Ops.size() == 2) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1218 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1219 | if (Add->getNumOperands() == 2 && |
| 1220 | isa<SCEVConstant>(Add->getOperand(0))) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1221 | return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)), |
| 1222 | getMulExpr(LHSC, Add->getOperand(1))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1223 | |
| 1224 | |
| 1225 | ++Idx; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1226 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1227 | // We found two constants, fold them together! |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1228 | ConstantInt *Fold = ConstantInt::get(LHSC->getValue()->getValue() * |
| 1229 | RHSC->getValue()->getValue()); |
| 1230 | Ops[0] = getConstant(Fold); |
| 1231 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1232 | if (Ops.size() == 1) return Ops[0]; |
| 1233 | LHSC = cast<SCEVConstant>(Ops[0]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | // If we are left with a constant one being multiplied, strip it off. |
| 1237 | if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) { |
| 1238 | Ops.erase(Ops.begin()); |
| 1239 | --Idx; |
| 1240 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
| 1241 | // If we have a multiply of zero, it will always be zero. |
| 1242 | return Ops[0]; |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | // Skip over the add expression until we get to a multiply. |
| 1247 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1248 | ++Idx; |
| 1249 | |
| 1250 | if (Ops.size() == 1) |
| 1251 | return Ops[0]; |
| 1252 | |
| 1253 | // If there are mul operands inline them all into this expression. |
| 1254 | if (Idx < Ops.size()) { |
| 1255 | bool DeletedMul = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1256 | while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1257 | // If we have an mul, expand the mul operands onto the end of the operands |
| 1258 | // list. |
| 1259 | Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end()); |
| 1260 | Ops.erase(Ops.begin()+Idx); |
| 1261 | DeletedMul = true; |
| 1262 | } |
| 1263 | |
| 1264 | // If we deleted at least one mul, we added operands to the end of the list, |
| 1265 | // and they are not necessarily sorted. Recurse to resort and resimplify |
| 1266 | // any operands we just aquired. |
| 1267 | if (DeletedMul) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1268 | return getMulExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | // If there are any add recurrences in the operands list, see if any other |
| 1272 | // added values are loop invariant. If so, we can fold them into the |
| 1273 | // recurrence. |
| 1274 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1275 | ++Idx; |
| 1276 | |
| 1277 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1278 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1279 | // Scan all of the other operands to this mul and add them to the vector if |
| 1280 | // they are loop invariant w.r.t. the recurrence. |
| 1281 | std::vector<SCEVHandle> LIOps; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1282 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1283 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1284 | if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { |
| 1285 | LIOps.push_back(Ops[i]); |
| 1286 | Ops.erase(Ops.begin()+i); |
| 1287 | --i; --e; |
| 1288 | } |
| 1289 | |
| 1290 | // If we found some loop invariants, fold them into the recurrence. |
| 1291 | if (!LIOps.empty()) { |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1292 | // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1293 | std::vector<SCEVHandle> NewOps; |
| 1294 | NewOps.reserve(AddRec->getNumOperands()); |
| 1295 | if (LIOps.size() == 1) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1296 | const SCEV *Scale = LIOps[0]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1297 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1298 | NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1299 | } else { |
| 1300 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { |
| 1301 | std::vector<SCEVHandle> MulOps(LIOps); |
| 1302 | MulOps.push_back(AddRec->getOperand(i)); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1303 | NewOps.push_back(getMulExpr(MulOps)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1304 | } |
| 1305 | } |
| 1306 | |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1307 | SCEVHandle NewRec = getAddRecExpr(NewOps, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1308 | |
| 1309 | // If all of the other operands were loop invariant, we are done. |
| 1310 | if (Ops.size() == 1) return NewRec; |
| 1311 | |
| 1312 | // Otherwise, multiply the folded AddRec by the non-liv parts. |
| 1313 | for (unsigned i = 0;; ++i) |
| 1314 | if (Ops[i] == AddRec) { |
| 1315 | Ops[i] = NewRec; |
| 1316 | break; |
| 1317 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1318 | return getMulExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1322 | // there are multiple AddRec's with the same loop induction variable being |
| 1323 | // multiplied together. If so, we can fold them. |
| 1324 | for (unsigned OtherIdx = Idx+1; |
| 1325 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) |
| 1326 | if (OtherIdx != Idx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1327 | const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1328 | if (AddRec->getLoop() == OtherAddRec->getLoop()) { |
| 1329 | // F * G --> {A,+,B} * {C,+,D} --> {A*C,+,F*D + G*B + B*D} |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1330 | const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec; |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1331 | SCEVHandle NewStart = getMulExpr(F->getStart(), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1332 | G->getStart()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1333 | SCEVHandle B = F->getStepRecurrence(*this); |
| 1334 | SCEVHandle D = G->getStepRecurrence(*this); |
| 1335 | SCEVHandle NewStep = getAddExpr(getMulExpr(F, D), |
| 1336 | getMulExpr(G, B), |
| 1337 | getMulExpr(B, D)); |
| 1338 | SCEVHandle NewAddRec = getAddRecExpr(NewStart, NewStep, |
| 1339 | F->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1340 | if (Ops.size() == 2) return NewAddRec; |
| 1341 | |
| 1342 | Ops.erase(Ops.begin()+Idx); |
| 1343 | Ops.erase(Ops.begin()+OtherIdx-1); |
| 1344 | Ops.push_back(NewAddRec); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1345 | return getMulExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1350 | // next one. |
| 1351 | } |
| 1352 | |
| 1353 | // Okay, it looks like we really DO need an mul expr. Check to see if we |
| 1354 | // already have one, otherwise create a new one. |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1355 | std::vector<const SCEV*> SCEVOps(Ops.begin(), Ops.end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1356 | SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scMulExpr, |
| 1357 | SCEVOps)]; |
| 1358 | if (Result == 0) |
| 1359 | Result = new SCEVMulExpr(Ops); |
| 1360 | return Result; |
| 1361 | } |
| 1362 | |
Dan Gohman | 77841cd | 2009-05-04 22:23:18 +0000 | [diff] [blame] | 1363 | SCEVHandle ScalarEvolution::getUDivExpr(const SCEVHandle &LHS, |
| 1364 | const SCEVHandle &RHS) { |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1365 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1366 | if (RHSC->getValue()->equalsInt(1)) |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 1367 | return LHS; // X udiv 1 --> x |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1368 | if (RHSC->isZero()) |
| 1369 | return getIntegerSCEV(0, LHS->getType()); // value is undefined |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1370 | |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1371 | // Determine if the division can be folded into the operands of |
| 1372 | // its operands. |
| 1373 | // TODO: Generalize this to non-constants by using known-bits information. |
| 1374 | const Type *Ty = LHS->getType(); |
| 1375 | unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros(); |
| 1376 | unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ; |
| 1377 | // For non-power-of-two values, effectively round the value up to the |
| 1378 | // nearest power of two. |
| 1379 | if (!RHSC->getValue()->getValue().isPowerOf2()) |
| 1380 | ++MaxShiftAmt; |
| 1381 | const IntegerType *ExtTy = |
| 1382 | IntegerType::get(getTypeSizeInBits(Ty) + MaxShiftAmt); |
| 1383 | // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. |
| 1384 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) |
| 1385 | if (const SCEVConstant *Step = |
| 1386 | dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) |
| 1387 | if (!Step->getValue()->getValue() |
| 1388 | .urem(RHSC->getValue()->getValue()) && |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1389 | getZeroExtendExpr(AR, ExtTy) == |
| 1390 | getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), |
| 1391 | getZeroExtendExpr(Step, ExtTy), |
| 1392 | AR->getLoop())) { |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1393 | std::vector<SCEVHandle> Operands; |
| 1394 | for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i) |
| 1395 | Operands.push_back(getUDivExpr(AR->getOperand(i), RHS)); |
| 1396 | return getAddRecExpr(Operands, AR->getLoop()); |
| 1397 | } |
| 1398 | // (A*B)/C --> A*(B/C) if safe and B/C can be folded. |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1399 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) { |
| 1400 | std::vector<SCEVHandle> Operands; |
| 1401 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) |
| 1402 | Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy)); |
| 1403 | if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1404 | // Find an operand that's safely divisible. |
| 1405 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { |
| 1406 | SCEVHandle Op = M->getOperand(i); |
| 1407 | SCEVHandle Div = getUDivExpr(Op, RHSC); |
| 1408 | if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) { |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1409 | Operands = M->getOperands(); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1410 | Operands[i] = Div; |
| 1411 | return getMulExpr(Operands); |
| 1412 | } |
| 1413 | } |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1414 | } |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1415 | // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded. |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1416 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) { |
| 1417 | std::vector<SCEVHandle> Operands; |
| 1418 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) |
| 1419 | Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy)); |
| 1420 | if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { |
| 1421 | Operands.clear(); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1422 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { |
| 1423 | SCEVHandle Op = getUDivExpr(A->getOperand(i), RHS); |
| 1424 | if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i)) |
| 1425 | break; |
| 1426 | Operands.push_back(Op); |
| 1427 | } |
| 1428 | if (Operands.size() == A->getNumOperands()) |
| 1429 | return getAddExpr(Operands); |
| 1430 | } |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1431 | } |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1432 | |
| 1433 | // Fold if both operands are constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1434 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1435 | Constant *LHSCV = LHSC->getValue(); |
| 1436 | Constant *RHSCV = RHSC->getValue(); |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 1437 | return getUnknown(ConstantExpr::getUDiv(LHSCV, RHSCV)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 1441 | SCEVUDivExpr *&Result = (*SCEVUDivs)[std::make_pair(LHS, RHS)]; |
| 1442 | if (Result == 0) Result = new SCEVUDivExpr(LHS, RHS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1443 | return Result; |
| 1444 | } |
| 1445 | |
| 1446 | |
| 1447 | /// SCEVAddRecExpr::get - Get a add recurrence expression for the |
| 1448 | /// specified loop. Simplify the expression as much as possible. |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1449 | SCEVHandle ScalarEvolution::getAddRecExpr(const SCEVHandle &Start, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1450 | const SCEVHandle &Step, const Loop *L) { |
| 1451 | std::vector<SCEVHandle> Operands; |
| 1452 | Operands.push_back(Start); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1453 | if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1454 | if (StepChrec->getLoop() == L) { |
| 1455 | Operands.insert(Operands.end(), StepChrec->op_begin(), |
| 1456 | StepChrec->op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1457 | return getAddRecExpr(Operands, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | Operands.push_back(Step); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1461 | return getAddRecExpr(Operands, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | /// SCEVAddRecExpr::get - Get a add recurrence expression for the |
| 1465 | /// specified loop. Simplify the expression as much as possible. |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1466 | SCEVHandle ScalarEvolution::getAddRecExpr(std::vector<SCEVHandle> &Operands, |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 1467 | const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1468 | if (Operands.size() == 1) return Operands[0]; |
| 1469 | |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1470 | if (Operands.back()->isZero()) { |
| 1471 | Operands.pop_back(); |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1472 | return getAddRecExpr(Operands, L); // {X,+,0} --> X |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1473 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1474 | |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1475 | // Canonicalize nested AddRecs in by nesting them in order of loop depth. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1476 | if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) { |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1477 | const Loop* NestedLoop = NestedAR->getLoop(); |
| 1478 | if (L->getLoopDepth() < NestedLoop->getLoopDepth()) { |
| 1479 | std::vector<SCEVHandle> NestedOperands(NestedAR->op_begin(), |
| 1480 | NestedAR->op_end()); |
| 1481 | SCEVHandle NestedARHandle(NestedAR); |
| 1482 | Operands[0] = NestedAR->getStart(); |
| 1483 | NestedOperands[0] = getAddRecExpr(Operands, L); |
| 1484 | return getAddRecExpr(NestedOperands, NestedLoop); |
| 1485 | } |
| 1486 | } |
| 1487 | |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1488 | std::vector<const SCEV*> SCEVOps(Operands.begin(), Operands.end()); |
| 1489 | SCEVAddRecExpr *&Result = (*SCEVAddRecExprs)[std::make_pair(L, SCEVOps)]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1490 | if (Result == 0) Result = new SCEVAddRecExpr(Operands, L); |
| 1491 | return Result; |
| 1492 | } |
| 1493 | |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1494 | SCEVHandle ScalarEvolution::getSMaxExpr(const SCEVHandle &LHS, |
| 1495 | const SCEVHandle &RHS) { |
| 1496 | std::vector<SCEVHandle> Ops; |
| 1497 | Ops.push_back(LHS); |
| 1498 | Ops.push_back(RHS); |
| 1499 | return getSMaxExpr(Ops); |
| 1500 | } |
| 1501 | |
| 1502 | SCEVHandle ScalarEvolution::getSMaxExpr(std::vector<SCEVHandle> Ops) { |
| 1503 | assert(!Ops.empty() && "Cannot get empty smax!"); |
| 1504 | if (Ops.size() == 1) return Ops[0]; |
| 1505 | |
| 1506 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1507 | GroupByComplexity(Ops, LI); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1508 | |
| 1509 | // If there are any constants, fold them together. |
| 1510 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1511 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1512 | ++Idx; |
| 1513 | assert(Idx < Ops.size()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1514 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1515 | // We found two constants, fold them together! |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1516 | ConstantInt *Fold = ConstantInt::get( |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1517 | APIntOps::smax(LHSC->getValue()->getValue(), |
| 1518 | RHSC->getValue()->getValue())); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1519 | Ops[0] = getConstant(Fold); |
| 1520 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1521 | if (Ops.size() == 1) return Ops[0]; |
| 1522 | LHSC = cast<SCEVConstant>(Ops[0]); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | // If we are left with a constant -inf, strip it off. |
| 1526 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) { |
| 1527 | Ops.erase(Ops.begin()); |
| 1528 | --Idx; |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | if (Ops.size() == 1) return Ops[0]; |
| 1533 | |
| 1534 | // Find the first SMax |
| 1535 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr) |
| 1536 | ++Idx; |
| 1537 | |
| 1538 | // Check to see if one of the operands is an SMax. If so, expand its operands |
| 1539 | // onto our operand list, and recurse to simplify. |
| 1540 | if (Idx < Ops.size()) { |
| 1541 | bool DeletedSMax = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1542 | while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1543 | Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end()); |
| 1544 | Ops.erase(Ops.begin()+Idx); |
| 1545 | DeletedSMax = true; |
| 1546 | } |
| 1547 | |
| 1548 | if (DeletedSMax) |
| 1549 | return getSMaxExpr(Ops); |
| 1550 | } |
| 1551 | |
| 1552 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1553 | // so, delete one. Since we sorted the list, these values are required to |
| 1554 | // be adjacent. |
| 1555 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1556 | if (Ops[i] == Ops[i+1]) { // X smax Y smax Y --> X smax Y |
| 1557 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 1558 | --i; --e; |
| 1559 | } |
| 1560 | |
| 1561 | if (Ops.size() == 1) return Ops[0]; |
| 1562 | |
| 1563 | assert(!Ops.empty() && "Reduced smax down to nothing!"); |
| 1564 | |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1565 | // Okay, it looks like we really DO need an smax expr. Check to see if we |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1566 | // already have one, otherwise create a new one. |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1567 | std::vector<const SCEV*> SCEVOps(Ops.begin(), Ops.end()); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1568 | SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scSMaxExpr, |
| 1569 | SCEVOps)]; |
| 1570 | if (Result == 0) Result = new SCEVSMaxExpr(Ops); |
| 1571 | return Result; |
| 1572 | } |
| 1573 | |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1574 | SCEVHandle ScalarEvolution::getUMaxExpr(const SCEVHandle &LHS, |
| 1575 | const SCEVHandle &RHS) { |
| 1576 | std::vector<SCEVHandle> Ops; |
| 1577 | Ops.push_back(LHS); |
| 1578 | Ops.push_back(RHS); |
| 1579 | return getUMaxExpr(Ops); |
| 1580 | } |
| 1581 | |
| 1582 | SCEVHandle ScalarEvolution::getUMaxExpr(std::vector<SCEVHandle> Ops) { |
| 1583 | assert(!Ops.empty() && "Cannot get empty umax!"); |
| 1584 | if (Ops.size() == 1) return Ops[0]; |
| 1585 | |
| 1586 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1587 | GroupByComplexity(Ops, LI); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1588 | |
| 1589 | // If there are any constants, fold them together. |
| 1590 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1591 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1592 | ++Idx; |
| 1593 | assert(Idx < Ops.size()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1594 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1595 | // We found two constants, fold them together! |
| 1596 | ConstantInt *Fold = ConstantInt::get( |
| 1597 | APIntOps::umax(LHSC->getValue()->getValue(), |
| 1598 | RHSC->getValue()->getValue())); |
| 1599 | Ops[0] = getConstant(Fold); |
| 1600 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1601 | if (Ops.size() == 1) return Ops[0]; |
| 1602 | LHSC = cast<SCEVConstant>(Ops[0]); |
| 1603 | } |
| 1604 | |
| 1605 | // If we are left with a constant zero, strip it off. |
| 1606 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) { |
| 1607 | Ops.erase(Ops.begin()); |
| 1608 | --Idx; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | if (Ops.size() == 1) return Ops[0]; |
| 1613 | |
| 1614 | // Find the first UMax |
| 1615 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr) |
| 1616 | ++Idx; |
| 1617 | |
| 1618 | // Check to see if one of the operands is a UMax. If so, expand its operands |
| 1619 | // onto our operand list, and recurse to simplify. |
| 1620 | if (Idx < Ops.size()) { |
| 1621 | bool DeletedUMax = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1622 | while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1623 | Ops.insert(Ops.end(), UMax->op_begin(), UMax->op_end()); |
| 1624 | Ops.erase(Ops.begin()+Idx); |
| 1625 | DeletedUMax = true; |
| 1626 | } |
| 1627 | |
| 1628 | if (DeletedUMax) |
| 1629 | return getUMaxExpr(Ops); |
| 1630 | } |
| 1631 | |
| 1632 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1633 | // so, delete one. Since we sorted the list, these values are required to |
| 1634 | // be adjacent. |
| 1635 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1636 | if (Ops[i] == Ops[i+1]) { // X umax Y umax Y --> X umax Y |
| 1637 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 1638 | --i; --e; |
| 1639 | } |
| 1640 | |
| 1641 | if (Ops.size() == 1) return Ops[0]; |
| 1642 | |
| 1643 | assert(!Ops.empty() && "Reduced umax down to nothing!"); |
| 1644 | |
| 1645 | // Okay, it looks like we really DO need a umax expr. Check to see if we |
| 1646 | // already have one, otherwise create a new one. |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1647 | std::vector<const SCEV*> SCEVOps(Ops.begin(), Ops.end()); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1648 | SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scUMaxExpr, |
| 1649 | SCEVOps)]; |
| 1650 | if (Result == 0) Result = new SCEVUMaxExpr(Ops); |
| 1651 | return Result; |
| 1652 | } |
| 1653 | |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1654 | SCEVHandle ScalarEvolution::getUnknown(Value *V) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1655 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1656 | return getConstant(CI); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1657 | if (isa<ConstantPointerNull>(V)) |
| 1658 | return getIntegerSCEV(0, V->getType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1659 | SCEVUnknown *&Result = (*SCEVUnknowns)[V]; |
| 1660 | if (Result == 0) Result = new SCEVUnknown(V); |
| 1661 | return Result; |
| 1662 | } |
| 1663 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1664 | //===----------------------------------------------------------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1665 | // Basic SCEV Analysis and PHI Idiom Recognition Code |
| 1666 | // |
| 1667 | |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 1668 | /// isSCEVable - Test if values of the given type are analyzable within |
| 1669 | /// the SCEV framework. This primarily includes integer types, and it |
| 1670 | /// can optionally include pointer types if the ScalarEvolution class |
| 1671 | /// has access to target-specific information. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1672 | bool ScalarEvolution::isSCEVable(const Type *Ty) const { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 1673 | // Integers are always SCEVable. |
| 1674 | if (Ty->isInteger()) |
| 1675 | return true; |
| 1676 | |
| 1677 | // Pointers are SCEVable if TargetData information is available |
| 1678 | // to provide pointer size information. |
| 1679 | if (isa<PointerType>(Ty)) |
| 1680 | return TD != NULL; |
| 1681 | |
| 1682 | // Otherwise it's not SCEVable. |
| 1683 | return false; |
| 1684 | } |
| 1685 | |
| 1686 | /// getTypeSizeInBits - Return the size in bits of the specified type, |
| 1687 | /// for which isSCEVable must return true. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1688 | uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 1689 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 1690 | |
| 1691 | // If we have a TargetData, use it! |
| 1692 | if (TD) |
| 1693 | return TD->getTypeSizeInBits(Ty); |
| 1694 | |
| 1695 | // Otherwise, we support only integer types. |
| 1696 | assert(Ty->isInteger() && "isSCEVable permitted a non-SCEVable type!"); |
| 1697 | return Ty->getPrimitiveSizeInBits(); |
| 1698 | } |
| 1699 | |
| 1700 | /// getEffectiveSCEVType - Return a type with the same bitwidth as |
| 1701 | /// the given type and which represents how SCEV will treat the given |
| 1702 | /// type, for which isSCEVable must return true. For pointer types, |
| 1703 | /// this is the pointer-sized integer type. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1704 | const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 1705 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 1706 | |
| 1707 | if (Ty->isInteger()) |
| 1708 | return Ty; |
| 1709 | |
| 1710 | assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!"); |
| 1711 | return TD->getIntPtrType(); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1712 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1713 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1714 | SCEVHandle ScalarEvolution::getCouldNotCompute() { |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 1715 | return UnknownValue; |
| 1716 | } |
| 1717 | |
Dan Gohman | d83d4af | 2009-05-04 22:20:30 +0000 | [diff] [blame] | 1718 | /// hasSCEV - Return true if the SCEV for this value has already been |
Edwin Török | 0e828d6 | 2009-05-01 08:33:47 +0000 | [diff] [blame] | 1719 | /// computed. |
| 1720 | bool ScalarEvolution::hasSCEV(Value *V) const { |
| 1721 | return Scalars.count(V); |
| 1722 | } |
| 1723 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1724 | /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the |
| 1725 | /// expression and create a new one. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1726 | SCEVHandle ScalarEvolution::getSCEV(Value *V) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 1727 | assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1728 | |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1729 | std::map<SCEVCallbackVH, SCEVHandle>::iterator I = Scalars.find(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1730 | if (I != Scalars.end()) return I->second; |
| 1731 | SCEVHandle S = createSCEV(V); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1732 | Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1733 | return S; |
| 1734 | } |
| 1735 | |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1736 | /// getIntegerSCEV - Given an integer or FP type, create a constant for the |
| 1737 | /// specified signed integer value and return a SCEV for the constant. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1738 | SCEVHandle ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) { |
| 1739 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1740 | Constant *C; |
| 1741 | if (Val == 0) |
| 1742 | C = Constant::getNullValue(Ty); |
| 1743 | else if (Ty->isFloatingPoint()) |
| 1744 | C = ConstantFP::get(APFloat(Ty==Type::FloatTy ? APFloat::IEEEsingle : |
| 1745 | APFloat::IEEEdouble, Val)); |
| 1746 | else |
| 1747 | C = ConstantInt::get(Ty, Val); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1748 | return getUnknown(C); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V |
| 1752 | /// |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1753 | SCEVHandle ScalarEvolution::getNegativeSCEV(const SCEVHandle &V) { |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1754 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1755 | return getUnknown(ConstantExpr::getNeg(VC->getValue())); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1756 | |
| 1757 | const Type *Ty = V->getType(); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1758 | Ty = getEffectiveSCEVType(Ty); |
| 1759 | return getMulExpr(V, getConstant(ConstantInt::getAllOnesValue(Ty))); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1763 | SCEVHandle ScalarEvolution::getNotSCEV(const SCEVHandle &V) { |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1764 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1765 | return getUnknown(ConstantExpr::getNot(VC->getValue())); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1766 | |
| 1767 | const Type *Ty = V->getType(); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1768 | Ty = getEffectiveSCEVType(Ty); |
| 1769 | SCEVHandle AllOnes = getConstant(ConstantInt::getAllOnesValue(Ty)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1770 | return getMinusSCEV(AllOnes, V); |
| 1771 | } |
| 1772 | |
| 1773 | /// getMinusSCEV - Return a SCEV corresponding to LHS - RHS. |
| 1774 | /// |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1775 | SCEVHandle ScalarEvolution::getMinusSCEV(const SCEVHandle &LHS, |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 1776 | const SCEVHandle &RHS) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1777 | // X - Y --> X + -Y |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1778 | return getAddExpr(LHS, getNegativeSCEV(RHS)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 1782 | /// input value to the specified type. If the type must be extended, it is zero |
| 1783 | /// extended. |
| 1784 | SCEVHandle |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1785 | ScalarEvolution::getTruncateOrZeroExtend(const SCEVHandle &V, |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 1786 | const Type *Ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1787 | const Type *SrcTy = V->getType(); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 1788 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 1789 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1790 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 1791 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1792 | return V; // No conversion |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 1793 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1794 | return getTruncateExpr(V, Ty); |
| 1795 | return getZeroExtendExpr(V, Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 1799 | /// input value to the specified type. If the type must be extended, it is sign |
| 1800 | /// extended. |
| 1801 | SCEVHandle |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1802 | ScalarEvolution::getTruncateOrSignExtend(const SCEVHandle &V, |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 1803 | const Type *Ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1804 | const Type *SrcTy = V->getType(); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 1805 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 1806 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1807 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 1808 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1809 | return V; // No conversion |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 1810 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1811 | return getTruncateExpr(V, Ty); |
| 1812 | return getSignExtendExpr(V, Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 1815 | /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 1816 | /// input value to the specified type. If the type must be extended, it is zero |
| 1817 | /// extended. The conversion must not be narrowing. |
| 1818 | SCEVHandle |
| 1819 | ScalarEvolution::getNoopOrZeroExtend(const SCEVHandle &V, const Type *Ty) { |
| 1820 | const Type *SrcTy = V->getType(); |
| 1821 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 1822 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 1823 | "Cannot noop or zero extend with non-integer arguments!"); |
| 1824 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 1825 | "getNoopOrZeroExtend cannot truncate!"); |
| 1826 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 1827 | return V; // No conversion |
| 1828 | return getZeroExtendExpr(V, Ty); |
| 1829 | } |
| 1830 | |
| 1831 | /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 1832 | /// input value to the specified type. If the type must be extended, it is sign |
| 1833 | /// extended. The conversion must not be narrowing. |
| 1834 | SCEVHandle |
| 1835 | ScalarEvolution::getNoopOrSignExtend(const SCEVHandle &V, const Type *Ty) { |
| 1836 | const Type *SrcTy = V->getType(); |
| 1837 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 1838 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 1839 | "Cannot noop or sign extend with non-integer arguments!"); |
| 1840 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 1841 | "getNoopOrSignExtend cannot truncate!"); |
| 1842 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 1843 | return V; // No conversion |
| 1844 | return getSignExtendExpr(V, Ty); |
| 1845 | } |
| 1846 | |
| 1847 | /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the |
| 1848 | /// input value to the specified type. The conversion must not be widening. |
| 1849 | SCEVHandle |
| 1850 | ScalarEvolution::getTruncateOrNoop(const SCEVHandle &V, const Type *Ty) { |
| 1851 | const Type *SrcTy = V->getType(); |
| 1852 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 1853 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 1854 | "Cannot truncate or noop with non-integer arguments!"); |
| 1855 | assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) && |
| 1856 | "getTruncateOrNoop cannot extend!"); |
| 1857 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 1858 | return V; // No conversion |
| 1859 | return getTruncateExpr(V, Ty); |
| 1860 | } |
| 1861 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1862 | /// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value for |
| 1863 | /// the specified instruction and replaces any references to the symbolic value |
| 1864 | /// SymName with the specified value. This is used during PHI resolution. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1865 | void ScalarEvolution:: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1866 | ReplaceSymbolicValueWithConcrete(Instruction *I, const SCEVHandle &SymName, |
| 1867 | const SCEVHandle &NewVal) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1868 | std::map<SCEVCallbackVH, SCEVHandle>::iterator SI = |
| 1869 | Scalars.find(SCEVCallbackVH(I, this)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1870 | if (SI == Scalars.end()) return; |
| 1871 | |
| 1872 | SCEVHandle NV = |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1873 | SI->second->replaceSymbolicValuesWithConcrete(SymName, NewVal, *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1874 | if (NV == SI->second) return; // No change. |
| 1875 | |
| 1876 | SI->second = NV; // Update the scalars map! |
| 1877 | |
| 1878 | // Any instruction values that use this instruction might also need to be |
| 1879 | // updated! |
| 1880 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 1881 | UI != E; ++UI) |
| 1882 | ReplaceSymbolicValueWithConcrete(cast<Instruction>(*UI), SymName, NewVal); |
| 1883 | } |
| 1884 | |
| 1885 | /// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in |
| 1886 | /// a loop header, making it a potential recurrence, or it doesn't. |
| 1887 | /// |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1888 | SCEVHandle ScalarEvolution::createNodeForPHI(PHINode *PN) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1889 | if (PN->getNumIncomingValues() == 2) // The loops have been canonicalized. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1890 | if (const Loop *L = LI->getLoopFor(PN->getParent())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1891 | if (L->getHeader() == PN->getParent()) { |
| 1892 | // If it lives in the loop header, it has two incoming values, one |
| 1893 | // from outside the loop, and one from inside. |
| 1894 | unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0)); |
| 1895 | unsigned BackEdge = IncomingEdge^1; |
| 1896 | |
| 1897 | // While we are analyzing this PHI node, handle its value symbolically. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1898 | SCEVHandle SymbolicName = getUnknown(PN); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1899 | assert(Scalars.find(PN) == Scalars.end() && |
| 1900 | "PHI node already processed?"); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1901 | Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1902 | |
| 1903 | // Using this symbolic name for the PHI, analyze the value coming around |
| 1904 | // the back-edge. |
| 1905 | SCEVHandle BEValue = getSCEV(PN->getIncomingValue(BackEdge)); |
| 1906 | |
| 1907 | // NOTE: If BEValue is loop invariant, we know that the PHI node just |
| 1908 | // has a special value for the first iteration of the loop. |
| 1909 | |
| 1910 | // If the value coming around the backedge is an add with the symbolic |
| 1911 | // value we just inserted, then we found a simple induction variable! |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1912 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1913 | // If there is a single occurrence of the symbolic value, replace it |
| 1914 | // with a recurrence. |
| 1915 | unsigned FoundIndex = Add->getNumOperands(); |
| 1916 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 1917 | if (Add->getOperand(i) == SymbolicName) |
| 1918 | if (FoundIndex == e) { |
| 1919 | FoundIndex = i; |
| 1920 | break; |
| 1921 | } |
| 1922 | |
| 1923 | if (FoundIndex != Add->getNumOperands()) { |
| 1924 | // Create an add with everything but the specified operand. |
| 1925 | std::vector<SCEVHandle> Ops; |
| 1926 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 1927 | if (i != FoundIndex) |
| 1928 | Ops.push_back(Add->getOperand(i)); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1929 | SCEVHandle Accum = getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1930 | |
| 1931 | // This is not a valid addrec if the step amount is varying each |
| 1932 | // loop iteration, but is not itself an addrec in this loop. |
| 1933 | if (Accum->isLoopInvariant(L) || |
| 1934 | (isa<SCEVAddRecExpr>(Accum) && |
| 1935 | cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) { |
| 1936 | SCEVHandle StartVal = getSCEV(PN->getIncomingValue(IncomingEdge)); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1937 | SCEVHandle PHISCEV = getAddRecExpr(StartVal, Accum, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1938 | |
| 1939 | // Okay, for the entire analysis of this edge we assumed the PHI |
| 1940 | // to be symbolic. We now need to go back and update all of the |
| 1941 | // entries for the scalars that use the PHI (except for the PHI |
| 1942 | // itself) to use the new analyzed value instead of the "symbolic" |
| 1943 | // value. |
| 1944 | ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV); |
| 1945 | return PHISCEV; |
| 1946 | } |
| 1947 | } |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1948 | } else if (const SCEVAddRecExpr *AddRec = |
| 1949 | dyn_cast<SCEVAddRecExpr>(BEValue)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1950 | // Otherwise, this could be a loop like this: |
| 1951 | // i = 0; for (j = 1; ..; ++j) { .... i = j; } |
| 1952 | // In this case, j = {1,+,1} and BEValue is j. |
| 1953 | // Because the other in-value of i (0) fits the evolution of BEValue |
| 1954 | // i really is an addrec evolution. |
| 1955 | if (AddRec->getLoop() == L && AddRec->isAffine()) { |
| 1956 | SCEVHandle StartVal = getSCEV(PN->getIncomingValue(IncomingEdge)); |
| 1957 | |
| 1958 | // If StartVal = j.start - j.stride, we can use StartVal as the |
| 1959 | // initial step of the addrec evolution. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1960 | if (StartVal == getMinusSCEV(AddRec->getOperand(0), |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1961 | AddRec->getOperand(1))) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1962 | SCEVHandle PHISCEV = |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1963 | getAddRecExpr(StartVal, AddRec->getOperand(1), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1964 | |
| 1965 | // Okay, for the entire analysis of this edge we assumed the PHI |
| 1966 | // to be symbolic. We now need to go back and update all of the |
| 1967 | // entries for the scalars that use the PHI (except for the PHI |
| 1968 | // itself) to use the new analyzed value instead of the "symbolic" |
| 1969 | // value. |
| 1970 | ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV); |
| 1971 | return PHISCEV; |
| 1972 | } |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | return SymbolicName; |
| 1977 | } |
| 1978 | |
| 1979 | // If it's not a loop phi, we can't handle it yet. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1980 | return getUnknown(PN); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 1983 | /// createNodeForGEP - Expand GEP instructions into add and multiply |
| 1984 | /// operations. This allows them to be analyzed by regular SCEV code. |
| 1985 | /// |
Dan Gohman | ca5a39e | 2009-05-08 20:58:38 +0000 | [diff] [blame] | 1986 | SCEVHandle ScalarEvolution::createNodeForGEP(User *GEP) { |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 1987 | |
| 1988 | const Type *IntPtrTy = TD->getIntPtrType(); |
Dan Gohman | c7034fa | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 1989 | Value *Base = GEP->getOperand(0); |
Dan Gohman | d586a4f | 2009-05-09 00:14:52 +0000 | [diff] [blame] | 1990 | // Don't attempt to analyze GEPs over unsized objects. |
| 1991 | if (!cast<PointerType>(Base->getType())->getElementType()->isSized()) |
| 1992 | return getUnknown(GEP); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 1993 | SCEVHandle TotalOffset = getIntegerSCEV(0, IntPtrTy); |
Dan Gohman | c7034fa | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 1994 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 1995 | for (GetElementPtrInst::op_iterator I = next(GEP->op_begin()), |
| 1996 | E = GEP->op_end(); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 1997 | I != E; ++I) { |
| 1998 | Value *Index = *I; |
| 1999 | // Compute the (potentially symbolic) offset in bytes for this index. |
| 2000 | if (const StructType *STy = dyn_cast<StructType>(*GTI++)) { |
| 2001 | // For a struct, add the member offset. |
| 2002 | const StructLayout &SL = *TD->getStructLayout(STy); |
| 2003 | unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue(); |
| 2004 | uint64_t Offset = SL.getElementOffset(FieldNo); |
| 2005 | TotalOffset = getAddExpr(TotalOffset, |
| 2006 | getIntegerSCEV(Offset, IntPtrTy)); |
| 2007 | } else { |
| 2008 | // For an array, add the element offset, explicitly scaled. |
| 2009 | SCEVHandle LocalOffset = getSCEV(Index); |
| 2010 | if (!isa<PointerType>(LocalOffset->getType())) |
| 2011 | // Getelementptr indicies are signed. |
| 2012 | LocalOffset = getTruncateOrSignExtend(LocalOffset, |
| 2013 | IntPtrTy); |
| 2014 | LocalOffset = |
| 2015 | getMulExpr(LocalOffset, |
Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2016 | getIntegerSCEV(TD->getTypeAllocSize(*GTI), |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2017 | IntPtrTy)); |
| 2018 | TotalOffset = getAddExpr(TotalOffset, LocalOffset); |
| 2019 | } |
| 2020 | } |
| 2021 | return getAddExpr(getSCEV(Base), TotalOffset); |
| 2022 | } |
| 2023 | |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2024 | /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is |
| 2025 | /// guaranteed to end in (at every loop iteration). It is, at the same time, |
| 2026 | /// the minimum number of times S is divisible by 2. For example, given {4,+,8} |
| 2027 | /// it returns 2. If S is guaranteed to be 0, it returns the bitwidth of S. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2028 | static uint32_t GetMinTrailingZeros(SCEVHandle S, const ScalarEvolution &SE) { |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2029 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Chris Lattner | 6ecce2a | 2007-11-23 22:36:49 +0000 | [diff] [blame] | 2030 | return C->getValue()->getValue().countTrailingZeros(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2031 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2032 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S)) |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2033 | return std::min(GetMinTrailingZeros(T->getOperand(), SE), |
| 2034 | (uint32_t)SE.getTypeSizeInBits(T->getType())); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2035 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2036 | if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2037 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand(), SE); |
| 2038 | return OpRes == SE.getTypeSizeInBits(E->getOperand()->getType()) ? |
Dan Gohman | bfd51da | 2009-05-12 01:23:18 +0000 | [diff] [blame] | 2039 | SE.getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2042 | if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2043 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand(), SE); |
| 2044 | return OpRes == SE.getTypeSizeInBits(E->getOperand()->getType()) ? |
Dan Gohman | bfd51da | 2009-05-12 01:23:18 +0000 | [diff] [blame] | 2045 | SE.getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2048 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2049 | // The result is the min of all operands results. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2050 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0), SE); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2051 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2052 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i), SE)); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2053 | return MinOpRes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2056 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2057 | // The result is the sum of all operands results. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2058 | uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0), SE); |
| 2059 | uint32_t BitWidth = SE.getTypeSizeInBits(M->getType()); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2060 | for (unsigned i = 1, e = M->getNumOperands(); |
| 2061 | SumOpRes != BitWidth && i != e; ++i) |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2062 | SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i), SE), |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2063 | BitWidth); |
| 2064 | return SumOpRes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2065 | } |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2066 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2067 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2068 | // The result is the min of all operands results. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2069 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0), SE); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2070 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2071 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i), SE)); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2072 | return MinOpRes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2073 | } |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2074 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2075 | if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2076 | // The result is the min of all operands results. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2077 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0), SE); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2078 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2079 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i), SE)); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2080 | return MinOpRes; |
| 2081 | } |
| 2082 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2083 | if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2084 | // The result is the min of all operands results. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2085 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0), SE); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2086 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2087 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i), SE)); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2088 | return MinOpRes; |
| 2089 | } |
| 2090 | |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 2091 | // SCEVUDivExpr, SCEVUnknown |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2092 | return 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
| 2095 | /// createSCEV - We know that there is no SCEV for the specified value. |
| 2096 | /// Analyze the expression. |
| 2097 | /// |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2098 | SCEVHandle ScalarEvolution::createSCEV(Value *V) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2099 | if (!isSCEVable(V->getType())) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2100 | return getUnknown(V); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2101 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2102 | unsigned Opcode = Instruction::UserOp1; |
| 2103 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 2104 | Opcode = I->getOpcode(); |
| 2105 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 2106 | Opcode = CE->getOpcode(); |
| 2107 | else |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2108 | return getUnknown(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2109 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2110 | User *U = cast<User>(V); |
| 2111 | switch (Opcode) { |
| 2112 | case Instruction::Add: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2113 | return getAddExpr(getSCEV(U->getOperand(0)), |
| 2114 | getSCEV(U->getOperand(1))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2115 | case Instruction::Mul: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2116 | return getMulExpr(getSCEV(U->getOperand(0)), |
| 2117 | getSCEV(U->getOperand(1))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2118 | case Instruction::UDiv: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2119 | return getUDivExpr(getSCEV(U->getOperand(0)), |
| 2120 | getSCEV(U->getOperand(1))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2121 | case Instruction::Sub: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2122 | return getMinusSCEV(getSCEV(U->getOperand(0)), |
| 2123 | getSCEV(U->getOperand(1))); |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2124 | case Instruction::And: |
| 2125 | // For an expression like x&255 that merely masks off the high bits, |
| 2126 | // use zext(trunc(x)) as the SCEV expression. |
| 2127 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 91ae1e7 | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2128 | if (CI->isNullValue()) |
| 2129 | return getSCEV(U->getOperand(1)); |
Dan Gohman | c7ebba1 | 2009-04-27 01:41:10 +0000 | [diff] [blame] | 2130 | if (CI->isAllOnesValue()) |
| 2131 | return getSCEV(U->getOperand(0)); |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2132 | const APInt &A = CI->getValue(); |
| 2133 | unsigned Ones = A.countTrailingOnes(); |
| 2134 | if (APIntOps::isMask(Ones, A)) |
| 2135 | return |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2136 | getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)), |
| 2137 | IntegerType::get(Ones)), |
| 2138 | U->getType()); |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2139 | } |
| 2140 | break; |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2141 | case Instruction::Or: |
| 2142 | // If the RHS of the Or is a constant, we may have something like: |
| 2143 | // X*4+1 which got turned into X*4|1. Handle this as an Add so loop |
| 2144 | // optimizations will transparently handle this case. |
| 2145 | // |
| 2146 | // In order for this transformation to be safe, the LHS must be of the |
| 2147 | // form X*(2^n) and the Or constant must be less than 2^n. |
| 2148 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 2149 | SCEVHandle LHS = getSCEV(U->getOperand(0)); |
| 2150 | const APInt &CIVal = CI->getValue(); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2151 | if (GetMinTrailingZeros(LHS, *this) >= |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2152 | (CIVal.getBitWidth() - CIVal.countLeadingZeros())) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2153 | return getAddExpr(LHS, getSCEV(U->getOperand(1))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2154 | } |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2155 | break; |
| 2156 | case Instruction::Xor: |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2157 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2158 | // If the RHS of the xor is a signbit, then this is just an add. |
| 2159 | // Instcombine turns add of signbit into xor as a strength reduction step. |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2160 | if (CI->getValue().isSignBit()) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2161 | return getAddExpr(getSCEV(U->getOperand(0)), |
| 2162 | getSCEV(U->getOperand(1))); |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2163 | |
| 2164 | // If the RHS of xor is -1, then this is a not operation. |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2165 | else if (CI->isAllOnesValue()) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2166 | return getNotSCEV(getSCEV(U->getOperand(0))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2167 | } |
| 2168 | break; |
| 2169 | |
| 2170 | case Instruction::Shl: |
| 2171 | // Turn shift left of a constant amount into a multiply. |
| 2172 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 2173 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
| 2174 | Constant *X = ConstantInt::get( |
| 2175 | APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2176 | return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2177 | } |
| 2178 | break; |
| 2179 | |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2180 | case Instruction::LShr: |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 2181 | // Turn logical shift right of a constant into a unsigned divide. |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2182 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 2183 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
| 2184 | Constant *X = ConstantInt::get( |
| 2185 | APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2186 | return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2187 | } |
| 2188 | break; |
| 2189 | |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2190 | case Instruction::AShr: |
| 2191 | // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression. |
| 2192 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) |
| 2193 | if (Instruction *L = dyn_cast<Instruction>(U->getOperand(0))) |
| 2194 | if (L->getOpcode() == Instruction::Shl && |
| 2195 | L->getOperand(1) == U->getOperand(1)) { |
Dan Gohman | 91ae1e7 | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2196 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2197 | uint64_t Amt = BitWidth - CI->getZExtValue(); |
| 2198 | if (Amt == BitWidth) |
| 2199 | return getSCEV(L->getOperand(0)); // shift by zero --> noop |
| 2200 | if (Amt > BitWidth) |
| 2201 | return getIntegerSCEV(0, U->getType()); // value is undefined |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2202 | return |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2203 | getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)), |
Dan Gohman | 91ae1e7 | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2204 | IntegerType::get(Amt)), |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2205 | U->getType()); |
| 2206 | } |
| 2207 | break; |
| 2208 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2209 | case Instruction::Trunc: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2210 | return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2211 | |
| 2212 | case Instruction::ZExt: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2213 | return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2214 | |
| 2215 | case Instruction::SExt: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2216 | return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2217 | |
| 2218 | case Instruction::BitCast: |
| 2219 | // BitCasts are no-op casts so we just eliminate the cast. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2220 | if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2221 | return getSCEV(U->getOperand(0)); |
| 2222 | break; |
| 2223 | |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2224 | case Instruction::IntToPtr: |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2225 | if (!TD) break; // Without TD we can't analyze pointers. |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2226 | return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)), |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2227 | TD->getIntPtrType()); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2228 | |
| 2229 | case Instruction::PtrToInt: |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2230 | if (!TD) break; // Without TD we can't analyze pointers. |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2231 | return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)), |
| 2232 | U->getType()); |
| 2233 | |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2234 | case Instruction::GetElementPtr: |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2235 | if (!TD) break; // Without TD we can't analyze pointers. |
Dan Gohman | ca5a39e | 2009-05-08 20:58:38 +0000 | [diff] [blame] | 2236 | return createNodeForGEP(U); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2237 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2238 | case Instruction::PHI: |
| 2239 | return createNodeForPHI(cast<PHINode>(U)); |
| 2240 | |
| 2241 | case Instruction::Select: |
| 2242 | // This could be a smax or umax that was lowered earlier. |
| 2243 | // Try to recover it. |
| 2244 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) { |
| 2245 | Value *LHS = ICI->getOperand(0); |
| 2246 | Value *RHS = ICI->getOperand(1); |
| 2247 | switch (ICI->getPredicate()) { |
| 2248 | case ICmpInst::ICMP_SLT: |
| 2249 | case ICmpInst::ICMP_SLE: |
| 2250 | std::swap(LHS, RHS); |
| 2251 | // fall through |
| 2252 | case ICmpInst::ICMP_SGT: |
| 2253 | case ICmpInst::ICMP_SGE: |
| 2254 | if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2255 | return getSMaxExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2256 | else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) |
Eli Friedman | 8e2fd03 | 2008-07-30 04:36:32 +0000 | [diff] [blame] | 2257 | // ~smax(~x, ~y) == smin(x, y). |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2258 | return getNotSCEV(getSMaxExpr( |
| 2259 | getNotSCEV(getSCEV(LHS)), |
| 2260 | getNotSCEV(getSCEV(RHS)))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2261 | break; |
| 2262 | case ICmpInst::ICMP_ULT: |
| 2263 | case ICmpInst::ICMP_ULE: |
| 2264 | std::swap(LHS, RHS); |
| 2265 | // fall through |
| 2266 | case ICmpInst::ICMP_UGT: |
| 2267 | case ICmpInst::ICMP_UGE: |
| 2268 | if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2269 | return getUMaxExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2270 | else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) |
| 2271 | // ~umax(~x, ~y) == umin(x, y) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2272 | return getNotSCEV(getUMaxExpr(getNotSCEV(getSCEV(LHS)), |
| 2273 | getNotSCEV(getSCEV(RHS)))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2274 | break; |
| 2275 | default: |
| 2276 | break; |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | default: // We cannot analyze this expression. |
| 2281 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2284 | return getUnknown(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | |
| 2288 | |
| 2289 | //===----------------------------------------------------------------------===// |
| 2290 | // Iteration Count Computation Code |
| 2291 | // |
| 2292 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2293 | /// getBackedgeTakenCount - If the specified loop has a predictable |
| 2294 | /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute |
| 2295 | /// object. The backedge-taken count is the number of times the loop header |
| 2296 | /// will be branched to from within the loop. This is one less than the |
| 2297 | /// trip count of the loop, since it doesn't count the first iteration, |
| 2298 | /// when the header is branched to from outside the loop. |
| 2299 | /// |
| 2300 | /// Note that it is not valid to call this method on a loop without a |
| 2301 | /// loop-invariant backedge-taken count (see |
| 2302 | /// hasLoopInvariantBackedgeTakenCount). |
| 2303 | /// |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2304 | SCEVHandle ScalarEvolution::getBackedgeTakenCount(const Loop *L) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2305 | return getBackedgeTakenInfo(L).Exact; |
| 2306 | } |
| 2307 | |
| 2308 | /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except |
| 2309 | /// return the least SCEV value that is known never to be less than the |
| 2310 | /// actual backedge taken count. |
| 2311 | SCEVHandle ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) { |
| 2312 | return getBackedgeTakenInfo(L).Max; |
| 2313 | } |
| 2314 | |
| 2315 | const ScalarEvolution::BackedgeTakenInfo & |
| 2316 | ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2317 | // Initially insert a CouldNotCompute for this loop. If the insertion |
| 2318 | // succeeds, procede to actually compute a backedge-taken count and |
| 2319 | // update the value. The temporary CouldNotCompute value tells SCEV |
| 2320 | // code elsewhere that it shouldn't attempt to request a new |
| 2321 | // backedge-taken count, which could result in infinite recursion. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2322 | std::pair<std::map<const Loop*, BackedgeTakenInfo>::iterator, bool> Pair = |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2323 | BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute())); |
| 2324 | if (Pair.second) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2325 | BackedgeTakenInfo ItCount = ComputeBackedgeTakenCount(L); |
| 2326 | if (ItCount.Exact != UnknownValue) { |
| 2327 | assert(ItCount.Exact->isLoopInvariant(L) && |
| 2328 | ItCount.Max->isLoopInvariant(L) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2329 | "Computed trip count isn't loop invariant for loop!"); |
| 2330 | ++NumTripCountsComputed; |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2331 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2332 | // Update the value in the map. |
| 2333 | Pair.first->second = ItCount; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2334 | } else if (isa<PHINode>(L->getHeader()->begin())) { |
| 2335 | // Only count loops that have phi nodes as not being computable. |
| 2336 | ++NumTripCountsNotComputed; |
| 2337 | } |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2338 | |
| 2339 | // Now that we know more about the trip count for this loop, forget any |
| 2340 | // existing SCEV values for PHI nodes in this loop since they are only |
| 2341 | // conservative estimates made without the benefit |
| 2342 | // of trip count information. |
| 2343 | if (ItCount.hasAnyInfo()) |
Dan Gohman | 9462302 | 2009-05-02 17:43:35 +0000 | [diff] [blame] | 2344 | forgetLoopPHIs(L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2345 | } |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2346 | return Pair.first->second; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2349 | /// forgetLoopBackedgeTakenCount - This method should be called by the |
Dan Gohman | f3a060a | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 2350 | /// client when it has changed a loop in a way that may effect |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2351 | /// ScalarEvolution's ability to compute a trip count, or if the loop |
| 2352 | /// is deleted. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2353 | void ScalarEvolution::forgetLoopBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2354 | BackedgeTakenCounts.erase(L); |
Dan Gohman | 9462302 | 2009-05-02 17:43:35 +0000 | [diff] [blame] | 2355 | forgetLoopPHIs(L); |
| 2356 | } |
| 2357 | |
| 2358 | /// forgetLoopPHIs - Delete the memoized SCEVs associated with the |
| 2359 | /// PHI nodes in the given loop. This is used when the trip count of |
| 2360 | /// the loop may have changed. |
| 2361 | void ScalarEvolution::forgetLoopPHIs(const Loop *L) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2362 | BasicBlock *Header = L->getHeader(); |
| 2363 | |
Dan Gohman | 9fd4a00 | 2009-05-12 01:27:58 +0000 | [diff] [blame] | 2364 | // Push all Loop-header PHIs onto the Worklist stack, except those |
| 2365 | // that are presently represented via a SCEVUnknown. SCEVUnknown for |
| 2366 | // a PHI either means that it has an unrecognized structure, or it's |
| 2367 | // a PHI that's in the progress of being computed by createNodeForPHI. |
| 2368 | // In the former case, additional loop trip count information isn't |
| 2369 | // going to change anything. In the later case, createNodeForPHI will |
| 2370 | // perform the necessary updates on its own when it gets to that point. |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2371 | SmallVector<Instruction *, 16> Worklist; |
| 2372 | for (BasicBlock::iterator I = Header->begin(); |
Dan Gohman | 9fd4a00 | 2009-05-12 01:27:58 +0000 | [diff] [blame] | 2373 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 2374 | std::map<SCEVCallbackVH, SCEVHandle>::iterator It = Scalars.find((Value*)I); |
| 2375 | if (It != Scalars.end() && !isa<SCEVUnknown>(It->second)) |
| 2376 | Worklist.push_back(PN); |
| 2377 | } |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2378 | |
| 2379 | while (!Worklist.empty()) { |
| 2380 | Instruction *I = Worklist.pop_back_val(); |
| 2381 | if (Scalars.erase(I)) |
| 2382 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 2383 | UI != UE; ++UI) |
| 2384 | Worklist.push_back(cast<Instruction>(UI)); |
| 2385 | } |
Dan Gohman | f3a060a | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2388 | /// ComputeBackedgeTakenCount - Compute the number of times the backedge |
| 2389 | /// of the specified loop will execute. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2390 | ScalarEvolution::BackedgeTakenInfo |
| 2391 | ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2392 | // If the loop has a non-one exit block count, we can't analyze it. |
Devang Patel | 02451fa | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 2393 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2394 | L->getExitBlocks(ExitBlocks); |
| 2395 | if (ExitBlocks.size() != 1) return UnknownValue; |
| 2396 | |
| 2397 | // Okay, there is one exit block. Try to find the condition that causes the |
| 2398 | // loop to be exited. |
| 2399 | BasicBlock *ExitBlock = ExitBlocks[0]; |
| 2400 | |
| 2401 | BasicBlock *ExitingBlock = 0; |
| 2402 | for (pred_iterator PI = pred_begin(ExitBlock), E = pred_end(ExitBlock); |
| 2403 | PI != E; ++PI) |
| 2404 | if (L->contains(*PI)) { |
| 2405 | if (ExitingBlock == 0) |
| 2406 | ExitingBlock = *PI; |
| 2407 | else |
| 2408 | return UnknownValue; // More than one block exiting! |
| 2409 | } |
| 2410 | assert(ExitingBlock && "No exits from loop, something is broken!"); |
| 2411 | |
| 2412 | // Okay, we've computed the exiting block. See what condition causes us to |
| 2413 | // exit. |
| 2414 | // |
| 2415 | // FIXME: we should be able to handle switch instructions (with a single exit) |
| 2416 | BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); |
| 2417 | if (ExitBr == 0) return UnknownValue; |
| 2418 | assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!"); |
| 2419 | |
| 2420 | // At this point, we know we have a conditional branch that determines whether |
| 2421 | // the loop is exited. However, we don't know if the branch is executed each |
| 2422 | // time through the loop. If not, then the execution count of the branch will |
| 2423 | // not be equal to the trip count of the loop. |
| 2424 | // |
| 2425 | // Currently we check for this by checking to see if the Exit branch goes to |
| 2426 | // the loop header. If so, we know it will always execute the same number of |
| 2427 | // times as the loop. We also handle the case where the exit block *is* the |
| 2428 | // loop header. This is common for un-rotated loops. More extensive analysis |
| 2429 | // could be done to handle more cases here. |
| 2430 | if (ExitBr->getSuccessor(0) != L->getHeader() && |
| 2431 | ExitBr->getSuccessor(1) != L->getHeader() && |
| 2432 | ExitBr->getParent() != L->getHeader()) |
| 2433 | return UnknownValue; |
| 2434 | |
| 2435 | ICmpInst *ExitCond = dyn_cast<ICmpInst>(ExitBr->getCondition()); |
| 2436 | |
Eli Friedman | 459d729 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 2437 | // If it's not an integer or pointer comparison then compute it the hard way. |
| 2438 | if (ExitCond == 0) |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2439 | return ComputeBackedgeTakenCountExhaustively(L, ExitBr->getCondition(), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2440 | ExitBr->getSuccessor(0) == ExitBlock); |
| 2441 | |
| 2442 | // If the condition was exit on true, convert the condition to exit on false |
| 2443 | ICmpInst::Predicate Cond; |
| 2444 | if (ExitBr->getSuccessor(1) == ExitBlock) |
| 2445 | Cond = ExitCond->getPredicate(); |
| 2446 | else |
| 2447 | Cond = ExitCond->getInversePredicate(); |
| 2448 | |
| 2449 | // Handle common loops like: for (X = "string"; *X; ++X) |
| 2450 | if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0))) |
| 2451 | if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) { |
| 2452 | SCEVHandle ItCnt = |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2453 | ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2454 | if (!isa<SCEVCouldNotCompute>(ItCnt)) return ItCnt; |
| 2455 | } |
| 2456 | |
| 2457 | SCEVHandle LHS = getSCEV(ExitCond->getOperand(0)); |
| 2458 | SCEVHandle RHS = getSCEV(ExitCond->getOperand(1)); |
| 2459 | |
| 2460 | // Try to evaluate any dependencies out of the loop. |
| 2461 | SCEVHandle Tmp = getSCEVAtScope(LHS, L); |
| 2462 | if (!isa<SCEVCouldNotCompute>(Tmp)) LHS = Tmp; |
| 2463 | Tmp = getSCEVAtScope(RHS, L); |
| 2464 | if (!isa<SCEVCouldNotCompute>(Tmp)) RHS = Tmp; |
| 2465 | |
| 2466 | // At this point, we would like to compute how many iterations of the |
| 2467 | // loop the predicate will return true for these inputs. |
Dan Gohman | 2d96e35 | 2008-09-16 18:52:57 +0000 | [diff] [blame] | 2468 | if (LHS->isLoopInvariant(L) && !RHS->isLoopInvariant(L)) { |
| 2469 | // If there is a loop-invariant, force it into the RHS. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2470 | std::swap(LHS, RHS); |
| 2471 | Cond = ICmpInst::getSwappedPredicate(Cond); |
| 2472 | } |
| 2473 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2474 | // If we have a comparison of a chrec against a constant, try to use value |
| 2475 | // ranges to answer this query. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2476 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) |
| 2477 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2478 | if (AddRec->getLoop() == L) { |
Eli Friedman | 459d729 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 2479 | // Form the constant range. |
| 2480 | ConstantRange CompRange( |
| 2481 | ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue())); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2482 | |
Eli Friedman | 459d729 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 2483 | SCEVHandle Ret = AddRec->getNumIterationsInRange(CompRange, *this); |
| 2484 | if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | switch (Cond) { |
| 2488 | case ICmpInst::ICMP_NE: { // while (X != Y) |
| 2489 | // Convert to: while (X-Y != 0) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2490 | SCEVHandle TC = HowFarToZero(getMinusSCEV(LHS, RHS), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2491 | if (!isa<SCEVCouldNotCompute>(TC)) return TC; |
| 2492 | break; |
| 2493 | } |
| 2494 | case ICmpInst::ICMP_EQ: { |
| 2495 | // Convert to: while (X-Y == 0) // while (X == Y) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2496 | SCEVHandle TC = HowFarToNonZero(getMinusSCEV(LHS, RHS), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2497 | if (!isa<SCEVCouldNotCompute>(TC)) return TC; |
| 2498 | break; |
| 2499 | } |
| 2500 | case ICmpInst::ICMP_SLT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2501 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true); |
| 2502 | if (BTI.hasAnyInfo()) return BTI; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2503 | break; |
| 2504 | } |
| 2505 | case ICmpInst::ICMP_SGT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2506 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 2507 | getNotSCEV(RHS), L, true); |
| 2508 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | b7c2894 | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 2509 | break; |
| 2510 | } |
| 2511 | case ICmpInst::ICMP_ULT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2512 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false); |
| 2513 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | b7c2894 | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 2514 | break; |
| 2515 | } |
| 2516 | case ICmpInst::ICMP_UGT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2517 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 2518 | getNotSCEV(RHS), L, false); |
| 2519 | if (BTI.hasAnyInfo()) return BTI; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2520 | break; |
| 2521 | } |
| 2522 | default: |
| 2523 | #if 0 |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 2524 | errs() << "ComputeBackedgeTakenCount "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2525 | if (ExitCond->getOperand(0)->getType()->isUnsigned()) |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 2526 | errs() << "[unsigned] "; |
| 2527 | errs() << *LHS << " " |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2528 | << Instruction::getOpcodeName(Instruction::ICmp) |
| 2529 | << " " << *RHS << "\n"; |
| 2530 | #endif |
| 2531 | break; |
| 2532 | } |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2533 | return |
| 2534 | ComputeBackedgeTakenCountExhaustively(L, ExitCond, |
| 2535 | ExitBr->getSuccessor(0) == ExitBlock); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2536 | } |
| 2537 | |
| 2538 | static ConstantInt * |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 2539 | EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, |
| 2540 | ScalarEvolution &SE) { |
| 2541 | SCEVHandle InVal = SE.getConstant(C); |
| 2542 | SCEVHandle Val = AddRec->evaluateAtIteration(InVal, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2543 | assert(isa<SCEVConstant>(Val) && |
| 2544 | "Evaluation of SCEV at constant didn't fold correctly?"); |
| 2545 | return cast<SCEVConstant>(Val)->getValue(); |
| 2546 | } |
| 2547 | |
| 2548 | /// GetAddressedElementFromGlobal - Given a global variable with an initializer |
| 2549 | /// and a GEP expression (missing the pointer index) indexing into it, return |
| 2550 | /// the addressed element of the initializer or null if the index expression is |
| 2551 | /// invalid. |
| 2552 | static Constant * |
| 2553 | GetAddressedElementFromGlobal(GlobalVariable *GV, |
| 2554 | const std::vector<ConstantInt*> &Indices) { |
| 2555 | Constant *Init = GV->getInitializer(); |
| 2556 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) { |
| 2557 | uint64_t Idx = Indices[i]->getZExtValue(); |
| 2558 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) { |
| 2559 | assert(Idx < CS->getNumOperands() && "Bad struct index!"); |
| 2560 | Init = cast<Constant>(CS->getOperand(Idx)); |
| 2561 | } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) { |
| 2562 | if (Idx >= CA->getNumOperands()) return 0; // Bogus program |
| 2563 | Init = cast<Constant>(CA->getOperand(Idx)); |
| 2564 | } else if (isa<ConstantAggregateZero>(Init)) { |
| 2565 | if (const StructType *STy = dyn_cast<StructType>(Init->getType())) { |
| 2566 | assert(Idx < STy->getNumElements() && "Bad struct index!"); |
| 2567 | Init = Constant::getNullValue(STy->getElementType(Idx)); |
| 2568 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) { |
| 2569 | if (Idx >= ATy->getNumElements()) return 0; // Bogus program |
| 2570 | Init = Constant::getNullValue(ATy->getElementType()); |
| 2571 | } else { |
| 2572 | assert(0 && "Unknown constant aggregate type!"); |
| 2573 | } |
| 2574 | return 0; |
| 2575 | } else { |
| 2576 | return 0; // Unknown initializer type |
| 2577 | } |
| 2578 | } |
| 2579 | return Init; |
| 2580 | } |
| 2581 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2582 | /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of |
| 2583 | /// 'icmp op load X, cst', try to see if we can compute the backedge |
| 2584 | /// execution count. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2585 | SCEVHandle ScalarEvolution:: |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2586 | ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI, Constant *RHS, |
| 2587 | const Loop *L, |
| 2588 | ICmpInst::Predicate predicate) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2589 | if (LI->isVolatile()) return UnknownValue; |
| 2590 | |
| 2591 | // Check to see if the loaded pointer is a getelementptr of a global. |
| 2592 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)); |
| 2593 | if (!GEP) return UnknownValue; |
| 2594 | |
| 2595 | // Make sure that it is really a constant global we are gepping, with an |
| 2596 | // initializer, and make sure the first IDX is really 0. |
| 2597 | GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); |
| 2598 | if (!GV || !GV->isConstant() || !GV->hasInitializer() || |
| 2599 | GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) || |
| 2600 | !cast<Constant>(GEP->getOperand(1))->isNullValue()) |
| 2601 | return UnknownValue; |
| 2602 | |
| 2603 | // Okay, we allow one non-constant index into the GEP instruction. |
| 2604 | Value *VarIdx = 0; |
| 2605 | std::vector<ConstantInt*> Indexes; |
| 2606 | unsigned VarIdxNum = 0; |
| 2607 | for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) |
| 2608 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 2609 | Indexes.push_back(CI); |
| 2610 | } else if (!isa<ConstantInt>(GEP->getOperand(i))) { |
| 2611 | if (VarIdx) return UnknownValue; // Multiple non-constant idx's. |
| 2612 | VarIdx = GEP->getOperand(i); |
| 2613 | VarIdxNum = i-2; |
| 2614 | Indexes.push_back(0); |
| 2615 | } |
| 2616 | |
| 2617 | // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant. |
| 2618 | // Check to see if X is a loop variant variable value now. |
| 2619 | SCEVHandle Idx = getSCEV(VarIdx); |
| 2620 | SCEVHandle Tmp = getSCEVAtScope(Idx, L); |
| 2621 | if (!isa<SCEVCouldNotCompute>(Tmp)) Idx = Tmp; |
| 2622 | |
| 2623 | // We can only recognize very limited forms of loop index expressions, in |
| 2624 | // particular, only affine AddRec's like {C1,+,C2}. |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2625 | const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2626 | if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) || |
| 2627 | !isa<SCEVConstant>(IdxExpr->getOperand(0)) || |
| 2628 | !isa<SCEVConstant>(IdxExpr->getOperand(1))) |
| 2629 | return UnknownValue; |
| 2630 | |
| 2631 | unsigned MaxSteps = MaxBruteForceIterations; |
| 2632 | for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) { |
| 2633 | ConstantInt *ItCst = |
| 2634 | ConstantInt::get(IdxExpr->getType(), IterationNum); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2635 | ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2636 | |
| 2637 | // Form the GEP offset. |
| 2638 | Indexes[VarIdxNum] = Val; |
| 2639 | |
| 2640 | Constant *Result = GetAddressedElementFromGlobal(GV, Indexes); |
| 2641 | if (Result == 0) break; // Cannot compute! |
| 2642 | |
| 2643 | // Evaluate the condition for this iteration. |
| 2644 | Result = ConstantExpr::getICmp(predicate, Result, RHS); |
| 2645 | if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure |
| 2646 | if (cast<ConstantInt>(Result)->getValue().isMinValue()) { |
| 2647 | #if 0 |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 2648 | errs() << "\n***\n*** Computed loop count " << *ItCst |
| 2649 | << "\n*** From global " << *GV << "*** BB: " << *L->getHeader() |
| 2650 | << "***\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2651 | #endif |
| 2652 | ++NumArrayLenItCounts; |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2653 | return getConstant(ItCst); // Found terminating iteration! |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2654 | } |
| 2655 | } |
| 2656 | return UnknownValue; |
| 2657 | } |
| 2658 | |
| 2659 | |
| 2660 | /// CanConstantFold - Return true if we can constant fold an instruction of the |
| 2661 | /// specified type, assuming that all operands were constants. |
| 2662 | static bool CanConstantFold(const Instruction *I) { |
| 2663 | if (isa<BinaryOperator>(I) || isa<CmpInst>(I) || |
| 2664 | isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I)) |
| 2665 | return true; |
| 2666 | |
| 2667 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 2668 | if (const Function *F = CI->getCalledFunction()) |
Dan Gohman | e6e001f | 2008-01-31 01:05:10 +0000 | [diff] [blame] | 2669 | return canConstantFoldCallTo(F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2670 | return false; |
| 2671 | } |
| 2672 | |
| 2673 | /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node |
| 2674 | /// in the loop that V is derived from. We allow arbitrary operations along the |
| 2675 | /// way, but the operands of an operation must either be constants or a value |
| 2676 | /// derived from a constant PHI. If this expression does not fit with these |
| 2677 | /// constraints, return null. |
| 2678 | static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) { |
| 2679 | // If this is not an instruction, or if this is an instruction outside of the |
| 2680 | // loop, it can't be derived from a loop PHI. |
| 2681 | Instruction *I = dyn_cast<Instruction>(V); |
| 2682 | if (I == 0 || !L->contains(I->getParent())) return 0; |
| 2683 | |
Anton Korobeynikov | 357a27d | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 2684 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2685 | if (L->getHeader() == I->getParent()) |
| 2686 | return PN; |
| 2687 | else |
| 2688 | // We don't currently keep track of the control flow needed to evaluate |
| 2689 | // PHIs, so we cannot handle PHIs inside of loops. |
| 2690 | return 0; |
Anton Korobeynikov | 357a27d | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 2691 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2692 | |
| 2693 | // If we won't be able to constant fold this expression even if the operands |
| 2694 | // are constants, return early. |
| 2695 | if (!CanConstantFold(I)) return 0; |
| 2696 | |
| 2697 | // Otherwise, we can evaluate this instruction if all of its operands are |
| 2698 | // constant or derived from a PHI node themselves. |
| 2699 | PHINode *PHI = 0; |
| 2700 | for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op) |
| 2701 | if (!(isa<Constant>(I->getOperand(Op)) || |
| 2702 | isa<GlobalValue>(I->getOperand(Op)))) { |
| 2703 | PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L); |
| 2704 | if (P == 0) return 0; // Not evolving from PHI |
| 2705 | if (PHI == 0) |
| 2706 | PHI = P; |
| 2707 | else if (PHI != P) |
| 2708 | return 0; // Evolving from multiple different PHIs. |
| 2709 | } |
| 2710 | |
| 2711 | // This is a expression evolving from a constant PHI! |
| 2712 | return PHI; |
| 2713 | } |
| 2714 | |
| 2715 | /// EvaluateExpression - Given an expression that passes the |
| 2716 | /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node |
| 2717 | /// in the loop has the value PHIVal. If we can't fold this expression for some |
| 2718 | /// reason, return null. |
| 2719 | static Constant *EvaluateExpression(Value *V, Constant *PHIVal) { |
| 2720 | if (isa<PHINode>(V)) return PHIVal; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2721 | if (Constant *C = dyn_cast<Constant>(V)) return C; |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2722 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2723 | Instruction *I = cast<Instruction>(V); |
| 2724 | |
| 2725 | std::vector<Constant*> Operands; |
| 2726 | Operands.resize(I->getNumOperands()); |
| 2727 | |
| 2728 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 2729 | Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal); |
| 2730 | if (Operands[i] == 0) return 0; |
| 2731 | } |
| 2732 | |
Chris Lattner | d6e5691 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 2733 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 2734 | return ConstantFoldCompareInstOperands(CI->getPredicate(), |
| 2735 | &Operands[0], Operands.size()); |
| 2736 | else |
| 2737 | return ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
| 2738 | &Operands[0], Operands.size()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
| 2741 | /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is |
| 2742 | /// in the header of its containing loop, we know the loop executes a |
| 2743 | /// constant number of times, and the PHI node is just a recurrence |
| 2744 | /// involving constants, fold it. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2745 | Constant *ScalarEvolution:: |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2746 | getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs, const Loop *L){ |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2747 | std::map<PHINode*, Constant*>::iterator I = |
| 2748 | ConstantEvolutionLoopExitValue.find(PN); |
| 2749 | if (I != ConstantEvolutionLoopExitValue.end()) |
| 2750 | return I->second; |
| 2751 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2752 | if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations))) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2753 | return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it. |
| 2754 | |
| 2755 | Constant *&RetVal = ConstantEvolutionLoopExitValue[PN]; |
| 2756 | |
| 2757 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 2758 | // entry must be a constant (coming in from outside of the loop), and the |
| 2759 | // second must be derived from the same PHI. |
| 2760 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 2761 | Constant *StartCST = |
| 2762 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
| 2763 | if (StartCST == 0) |
| 2764 | return RetVal = 0; // Must be a constant. |
| 2765 | |
| 2766 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
| 2767 | PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); |
| 2768 | if (PN2 != PN) |
| 2769 | return RetVal = 0; // Not derived from same PHI. |
| 2770 | |
| 2771 | // Execute the loop symbolically to determine the exit value. |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2772 | if (BEs.getActiveBits() >= 32) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2773 | return RetVal = 0; // More than 2^32-1 iterations?? Not doing it! |
| 2774 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2775 | unsigned NumIterations = BEs.getZExtValue(); // must be in range |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2776 | unsigned IterationNum = 0; |
| 2777 | for (Constant *PHIVal = StartCST; ; ++IterationNum) { |
| 2778 | if (IterationNum == NumIterations) |
| 2779 | return RetVal = PHIVal; // Got exit value! |
| 2780 | |
| 2781 | // Compute the value of the PHI node for the next iteration. |
| 2782 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal); |
| 2783 | if (NextPHI == PHIVal) |
| 2784 | return RetVal = NextPHI; // Stopped evolving! |
| 2785 | if (NextPHI == 0) |
| 2786 | return 0; // Couldn't evaluate! |
| 2787 | PHIVal = NextPHI; |
| 2788 | } |
| 2789 | } |
| 2790 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2791 | /// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute a |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2792 | /// constant number of times (the condition evolves only from constants), |
| 2793 | /// try to evaluate a few iterations of the loop until we get the exit |
| 2794 | /// condition gets a value of ExitWhen (true or false). If we cannot |
| 2795 | /// evaluate the trip count of the loop, return UnknownValue. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2796 | SCEVHandle ScalarEvolution:: |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2797 | ComputeBackedgeTakenCountExhaustively(const Loop *L, Value *Cond, bool ExitWhen) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2798 | PHINode *PN = getConstantEvolvingPHI(Cond, L); |
| 2799 | if (PN == 0) return UnknownValue; |
| 2800 | |
| 2801 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 2802 | // entry must be a constant (coming in from outside of the loop), and the |
| 2803 | // second must be derived from the same PHI. |
| 2804 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 2805 | Constant *StartCST = |
| 2806 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
| 2807 | if (StartCST == 0) return UnknownValue; // Must be a constant. |
| 2808 | |
| 2809 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
| 2810 | PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); |
| 2811 | if (PN2 != PN) return UnknownValue; // Not derived from same PHI. |
| 2812 | |
| 2813 | // Okay, we find a PHI node that defines the trip count of this loop. Execute |
| 2814 | // the loop symbolically to determine when the condition gets a value of |
| 2815 | // "ExitWhen". |
| 2816 | unsigned IterationNum = 0; |
| 2817 | unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis. |
| 2818 | for (Constant *PHIVal = StartCST; |
| 2819 | IterationNum != MaxIterations; ++IterationNum) { |
| 2820 | ConstantInt *CondVal = |
| 2821 | dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal)); |
| 2822 | |
| 2823 | // Couldn't symbolically evaluate. |
| 2824 | if (!CondVal) return UnknownValue; |
| 2825 | |
| 2826 | if (CondVal->getValue() == uint64_t(ExitWhen)) { |
| 2827 | ConstantEvolutionLoopExitValue[PN] = PHIVal; |
| 2828 | ++NumBruteForceTripCountsComputed; |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2829 | return getConstant(ConstantInt::get(Type::Int32Ty, IterationNum)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
| 2832 | // Compute the value of the PHI node for the next iteration. |
| 2833 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal); |
| 2834 | if (NextPHI == 0 || NextPHI == PHIVal) |
| 2835 | return UnknownValue; // Couldn't evaluate or not making progress... |
| 2836 | PHIVal = NextPHI; |
| 2837 | } |
| 2838 | |
| 2839 | // Too many iterations were needed to evaluate. |
| 2840 | return UnknownValue; |
| 2841 | } |
| 2842 | |
Dan Gohman | dd40e9a | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 2843 | /// getSCEVAtScope - Return a SCEV expression handle for the specified value |
| 2844 | /// at the specified scope in the program. The L value specifies a loop |
| 2845 | /// nest to evaluate the expression at, where null is the top-level or a |
| 2846 | /// specified loop is immediately inside of the loop. |
| 2847 | /// |
| 2848 | /// This method can be used to compute the exit value for a variable defined |
| 2849 | /// in a loop by querying what the value will hold in the parent loop. |
| 2850 | /// |
| 2851 | /// If this value is not computable at this scope, a SCEVCouldNotCompute |
| 2852 | /// object is returned. |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2853 | SCEVHandle ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2854 | // FIXME: this should be turned into a virtual method on SCEV! |
| 2855 | |
| 2856 | if (isa<SCEVConstant>(V)) return V; |
| 2857 | |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2858 | // If this instruction is evolved from a constant-evolving PHI, compute the |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2859 | // exit value from the loop without using SCEVs. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2860 | if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2861 | if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2862 | const Loop *LI = (*this->LI)[I->getParent()]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2863 | if (LI && LI->getParentLoop() == L) // Looking for loop exit value. |
| 2864 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 2865 | if (PN->getParent() == LI->getHeader()) { |
| 2866 | // Okay, there is no closed form solution for the PHI node. Check |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2867 | // to see if the loop that contains it has a known backedge-taken |
| 2868 | // count. If so, we may be able to force computation of the exit |
| 2869 | // value. |
| 2870 | SCEVHandle BackedgeTakenCount = getBackedgeTakenCount(LI); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2871 | if (const SCEVConstant *BTCC = |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2872 | dyn_cast<SCEVConstant>(BackedgeTakenCount)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2873 | // Okay, we know how many times the containing loop executes. If |
| 2874 | // this is a constant evolving PHI node, get the final value at |
| 2875 | // the specified iteration number. |
| 2876 | Constant *RV = getConstantEvolutionLoopExitValue(PN, |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2877 | BTCC->getValue()->getValue(), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2878 | LI); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2879 | if (RV) return getUnknown(RV); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2880 | } |
| 2881 | } |
| 2882 | |
| 2883 | // Okay, this is an expression that we cannot symbolically evaluate |
| 2884 | // into a SCEV. Check to see if it's possible to symbolically evaluate |
| 2885 | // the arguments into constants, and if so, try to constant propagate the |
| 2886 | // result. This is particularly useful for computing loop exit values. |
| 2887 | if (CanConstantFold(I)) { |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 2888 | // Check to see if we've folded this instruction at this loop before. |
| 2889 | std::map<const Loop *, Constant *> &Values = ValuesAtScopes[I]; |
| 2890 | std::pair<std::map<const Loop *, Constant *>::iterator, bool> Pair = |
| 2891 | Values.insert(std::make_pair(L, static_cast<Constant *>(0))); |
| 2892 | if (!Pair.second) |
| 2893 | return Pair.first->second ? &*getUnknown(Pair.first->second) : V; |
| 2894 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2895 | std::vector<Constant*> Operands; |
| 2896 | Operands.reserve(I->getNumOperands()); |
| 2897 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 2898 | Value *Op = I->getOperand(i); |
| 2899 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| 2900 | Operands.push_back(C); |
| 2901 | } else { |
Chris Lattner | 3fff464 | 2007-11-23 08:46:22 +0000 | [diff] [blame] | 2902 | // If any of the operands is non-constant and if they are |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2903 | // non-integer and non-pointer, don't even try to analyze them |
| 2904 | // with scev techniques. |
Dan Gohman | 5e4eb76 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 2905 | if (!isSCEVable(Op->getType())) |
Chris Lattner | 3fff464 | 2007-11-23 08:46:22 +0000 | [diff] [blame] | 2906 | return V; |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2907 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2908 | SCEVHandle OpV = getSCEVAtScope(getSCEV(Op), L); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2909 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) { |
Dan Gohman | 5e4eb76 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 2910 | Constant *C = SC->getValue(); |
| 2911 | if (C->getType() != Op->getType()) |
| 2912 | C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 2913 | Op->getType(), |
| 2914 | false), |
| 2915 | C, Op->getType()); |
| 2916 | Operands.push_back(C); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2917 | } else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) { |
Dan Gohman | 5e4eb76 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 2918 | if (Constant *C = dyn_cast<Constant>(SU->getValue())) { |
| 2919 | if (C->getType() != Op->getType()) |
| 2920 | C = |
| 2921 | ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 2922 | Op->getType(), |
| 2923 | false), |
| 2924 | C, Op->getType()); |
| 2925 | Operands.push_back(C); |
| 2926 | } else |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2927 | return V; |
| 2928 | } else { |
| 2929 | return V; |
| 2930 | } |
| 2931 | } |
| 2932 | } |
Chris Lattner | d6e5691 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 2933 | |
| 2934 | Constant *C; |
| 2935 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 2936 | C = ConstantFoldCompareInstOperands(CI->getPredicate(), |
| 2937 | &Operands[0], Operands.size()); |
| 2938 | else |
| 2939 | C = ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
| 2940 | &Operands[0], Operands.size()); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 2941 | Pair.first->second = C; |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2942 | return getUnknown(C); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2943 | } |
| 2944 | } |
| 2945 | |
| 2946 | // This is some other type of SCEVUnknown, just return it. |
| 2947 | return V; |
| 2948 | } |
| 2949 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2950 | if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2951 | // Avoid performing the look-up in the common case where the specified |
| 2952 | // expression has no loop-variant portions. |
| 2953 | for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) { |
| 2954 | SCEVHandle OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
| 2955 | if (OpAtScope != Comm->getOperand(i)) { |
| 2956 | if (OpAtScope == UnknownValue) return UnknownValue; |
| 2957 | // Okay, at least one of these operands is loop variant but might be |
| 2958 | // foldable. Build a new instance of the folded commutative expression. |
| 2959 | std::vector<SCEVHandle> NewOps(Comm->op_begin(), Comm->op_begin()+i); |
| 2960 | NewOps.push_back(OpAtScope); |
| 2961 | |
| 2962 | for (++i; i != e; ++i) { |
| 2963 | OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
| 2964 | if (OpAtScope == UnknownValue) return UnknownValue; |
| 2965 | NewOps.push_back(OpAtScope); |
| 2966 | } |
| 2967 | if (isa<SCEVAddExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2968 | return getAddExpr(NewOps); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2969 | if (isa<SCEVMulExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2970 | return getMulExpr(NewOps); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2971 | if (isa<SCEVSMaxExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2972 | return getSMaxExpr(NewOps); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2973 | if (isa<SCEVUMaxExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2974 | return getUMaxExpr(NewOps); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2975 | assert(0 && "Unknown commutative SCEV type!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2976 | } |
| 2977 | } |
| 2978 | // If we got here, all operands are loop invariant. |
| 2979 | return Comm; |
| 2980 | } |
| 2981 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2982 | if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) { |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 2983 | SCEVHandle LHS = getSCEVAtScope(Div->getLHS(), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2984 | if (LHS == UnknownValue) return LHS; |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 2985 | SCEVHandle RHS = getSCEVAtScope(Div->getRHS(), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2986 | if (RHS == UnknownValue) return RHS; |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 2987 | if (LHS == Div->getLHS() && RHS == Div->getRHS()) |
| 2988 | return Div; // must be loop invariant |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2989 | return getUDivExpr(LHS, RHS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
| 2992 | // If this is a loop recurrence for a loop that does not contain L, then we |
| 2993 | // are dealing with the final value computed by the loop. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2994 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2995 | if (!L || !AddRec->getLoop()->contains(L->getHeader())) { |
| 2996 | // To evaluate this recurrence, we need to know how many times the AddRec |
| 2997 | // loop iterates. Compute this now. |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2998 | SCEVHandle BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop()); |
| 2999 | if (BackedgeTakenCount == UnknownValue) return UnknownValue; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3000 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 3001 | // Then, evaluate the AddRec. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3002 | return AddRec->evaluateAtIteration(BackedgeTakenCount, *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3003 | } |
| 3004 | return UnknownValue; |
| 3005 | } |
| 3006 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3007 | if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) { |
Dan Gohman | 78d63c8 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 3008 | SCEVHandle Op = getSCEVAtScope(Cast->getOperand(), L); |
| 3009 | if (Op == UnknownValue) return Op; |
| 3010 | if (Op == Cast->getOperand()) |
| 3011 | return Cast; // must be loop invariant |
| 3012 | return getZeroExtendExpr(Op, Cast->getType()); |
| 3013 | } |
| 3014 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3015 | if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) { |
Dan Gohman | 78d63c8 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 3016 | SCEVHandle Op = getSCEVAtScope(Cast->getOperand(), L); |
| 3017 | if (Op == UnknownValue) return Op; |
| 3018 | if (Op == Cast->getOperand()) |
| 3019 | return Cast; // must be loop invariant |
| 3020 | return getSignExtendExpr(Op, Cast->getType()); |
| 3021 | } |
| 3022 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3023 | if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) { |
Dan Gohman | 78d63c8 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 3024 | SCEVHandle Op = getSCEVAtScope(Cast->getOperand(), L); |
| 3025 | if (Op == UnknownValue) return Op; |
| 3026 | if (Op == Cast->getOperand()) |
| 3027 | return Cast; // must be loop invariant |
| 3028 | return getTruncateExpr(Op, Cast->getType()); |
| 3029 | } |
| 3030 | |
| 3031 | assert(0 && "Unknown SCEV type!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
Dan Gohman | dd40e9a | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 3034 | /// getSCEVAtScope - This is a convenience function which does |
| 3035 | /// getSCEVAtScope(getSCEV(V), L). |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3036 | SCEVHandle ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) { |
| 3037 | return getSCEVAtScope(getSCEV(V), L); |
| 3038 | } |
| 3039 | |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3040 | /// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the |
| 3041 | /// following equation: |
| 3042 | /// |
| 3043 | /// A * X = B (mod N) |
| 3044 | /// |
| 3045 | /// where N = 2^BW and BW is the common bit width of A and B. The signedness of |
| 3046 | /// A and B isn't important. |
| 3047 | /// |
| 3048 | /// If the equation does not have a solution, SCEVCouldNotCompute is returned. |
| 3049 | static SCEVHandle SolveLinEquationWithOverflow(const APInt &A, const APInt &B, |
| 3050 | ScalarEvolution &SE) { |
| 3051 | uint32_t BW = A.getBitWidth(); |
| 3052 | assert(BW == B.getBitWidth() && "Bit widths must be the same."); |
| 3053 | assert(A != 0 && "A must be non-zero."); |
| 3054 | |
| 3055 | // 1. D = gcd(A, N) |
| 3056 | // |
| 3057 | // The gcd of A and N may have only one prime factor: 2. The number of |
| 3058 | // trailing zeros in A is its multiplicity |
| 3059 | uint32_t Mult2 = A.countTrailingZeros(); |
| 3060 | // D = 2^Mult2 |
| 3061 | |
| 3062 | // 2. Check if B is divisible by D. |
| 3063 | // |
| 3064 | // B is divisible by D if and only if the multiplicity of prime factor 2 for B |
| 3065 | // is not less than multiplicity of this prime factor for D. |
| 3066 | if (B.countTrailingZeros() < Mult2) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 3067 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3068 | |
| 3069 | // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic |
| 3070 | // modulo (N / D). |
| 3071 | // |
| 3072 | // (N / D) may need BW+1 bits in its representation. Hence, we'll use this |
| 3073 | // bit width during computations. |
| 3074 | APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D |
| 3075 | APInt Mod(BW + 1, 0); |
| 3076 | Mod.set(BW - Mult2); // Mod = N / D |
| 3077 | APInt I = AD.multiplicativeInverse(Mod); |
| 3078 | |
| 3079 | // 4. Compute the minimum unsigned root of the equation: |
| 3080 | // I * (B / D) mod (N / D) |
| 3081 | APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod); |
| 3082 | |
| 3083 | // The result is guaranteed to be less than 2^BW so we may truncate it to BW |
| 3084 | // bits. |
| 3085 | return SE.getConstant(Result.trunc(BW)); |
| 3086 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3087 | |
| 3088 | /// SolveQuadraticEquation - Find the roots of the quadratic equation for the |
| 3089 | /// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which |
| 3090 | /// might be the same) or two SCEVCouldNotCompute objects. |
| 3091 | /// |
| 3092 | static std::pair<SCEVHandle,SCEVHandle> |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3093 | SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3094 | assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3095 | const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0)); |
| 3096 | const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1)); |
| 3097 | const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3098 | |
| 3099 | // We currently can only solve this if the coefficients are constants. |
| 3100 | if (!LC || !MC || !NC) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3101 | const SCEV *CNC = SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3102 | return std::make_pair(CNC, CNC); |
| 3103 | } |
| 3104 | |
| 3105 | uint32_t BitWidth = LC->getValue()->getValue().getBitWidth(); |
| 3106 | const APInt &L = LC->getValue()->getValue(); |
| 3107 | const APInt &M = MC->getValue()->getValue(); |
| 3108 | const APInt &N = NC->getValue()->getValue(); |
| 3109 | APInt Two(BitWidth, 2); |
| 3110 | APInt Four(BitWidth, 4); |
| 3111 | |
| 3112 | { |
| 3113 | using namespace APIntOps; |
| 3114 | const APInt& C = L; |
| 3115 | // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C |
| 3116 | // The B coefficient is M-N/2 |
| 3117 | APInt B(M); |
| 3118 | B -= sdiv(N,Two); |
| 3119 | |
| 3120 | // The A coefficient is N/2 |
| 3121 | APInt A(N.sdiv(Two)); |
| 3122 | |
| 3123 | // Compute the B^2-4ac term. |
| 3124 | APInt SqrtTerm(B); |
| 3125 | SqrtTerm *= B; |
| 3126 | SqrtTerm -= Four * (A * C); |
| 3127 | |
| 3128 | // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest |
| 3129 | // integer value or else APInt::sqrt() will assert. |
| 3130 | APInt SqrtVal(SqrtTerm.sqrt()); |
| 3131 | |
| 3132 | // Compute the two solutions for the quadratic formula. |
| 3133 | // The divisions must be performed as signed divisions. |
| 3134 | APInt NegB(-B); |
| 3135 | APInt TwoA( A << 1 ); |
Nick Lewycky | 3577669 | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 3136 | if (TwoA.isMinValue()) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3137 | const SCEV *CNC = SE.getCouldNotCompute(); |
Nick Lewycky | 3577669 | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 3138 | return std::make_pair(CNC, CNC); |
| 3139 | } |
| 3140 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3141 | ConstantInt *Solution1 = ConstantInt::get((NegB + SqrtVal).sdiv(TwoA)); |
| 3142 | ConstantInt *Solution2 = ConstantInt::get((NegB - SqrtVal).sdiv(TwoA)); |
| 3143 | |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3144 | return std::make_pair(SE.getConstant(Solution1), |
| 3145 | SE.getConstant(Solution2)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3146 | } // end APIntOps namespace |
| 3147 | } |
| 3148 | |
| 3149 | /// HowFarToZero - Return the number of times a backedge comparing the specified |
| 3150 | /// value to zero will execute. If not computable, return UnknownValue |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3151 | SCEVHandle ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3152 | // If the value is a constant |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3153 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3154 | // If the value is already zero, the branch will execute zero times. |
| 3155 | if (C->getValue()->isZero()) return C; |
| 3156 | return UnknownValue; // Otherwise it will loop infinitely. |
| 3157 | } |
| 3158 | |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3159 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3160 | if (!AddRec || AddRec->getLoop() != L) |
| 3161 | return UnknownValue; |
| 3162 | |
| 3163 | if (AddRec->isAffine()) { |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3164 | // If this is an affine expression, the execution count of this branch is |
| 3165 | // the minimum unsigned root of the following equation: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3166 | // |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3167 | // Start + Step*N = 0 (mod 2^BW) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3168 | // |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3169 | // equivalent to: |
| 3170 | // |
| 3171 | // Step*N = -Start (mod 2^BW) |
| 3172 | // |
| 3173 | // where BW is the common bit width of Start and Step. |
| 3174 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3175 | // Get the initial value for the loop. |
| 3176 | SCEVHandle Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop()); |
| 3177 | if (isa<SCEVCouldNotCompute>(Start)) return UnknownValue; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3178 | |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3179 | SCEVHandle Step = getSCEVAtScope(AddRec->getOperand(1), L->getParentLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3180 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3181 | if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) { |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3182 | // For now we handle only constant steps. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3183 | |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3184 | // First, handle unitary steps. |
| 3185 | if (StepC->getValue()->equalsInt(1)) // 1*N = -Start (mod 2^BW), so: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3186 | return getNegativeSCEV(Start); // N = -Start (as unsigned) |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3187 | if (StepC->getValue()->isAllOnesValue()) // -1*N = -Start (mod 2^BW), so: |
| 3188 | return Start; // N = Start (as unsigned) |
| 3189 | |
| 3190 | // Then, try to solve the above equation provided that Start is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3191 | if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3192 | return SolveLinEquationWithOverflow(StepC->getValue()->getValue(), |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3193 | -StartC->getValue()->getValue(), |
| 3194 | *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3195 | } |
| 3196 | } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) { |
| 3197 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of |
| 3198 | // the quadratic equation to solve it. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3199 | std::pair<SCEVHandle,SCEVHandle> Roots = SolveQuadraticEquation(AddRec, |
| 3200 | *this); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3201 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 3202 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3203 | if (R1) { |
| 3204 | #if 0 |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3205 | errs() << "HFTZ: " << *V << " - sol#1: " << *R1 |
| 3206 | << " sol#2: " << *R2 << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3207 | #endif |
| 3208 | // Pick the smallest positive root value. |
| 3209 | if (ConstantInt *CB = |
| 3210 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, |
| 3211 | R1->getValue(), R2->getValue()))) { |
| 3212 | if (CB->getZExtValue() == false) |
| 3213 | std::swap(R1, R2); // R1 is the minimum root now. |
| 3214 | |
| 3215 | // We can only use this value if the chrec ends up with an exact zero |
| 3216 | // value at this index. When solving for "X*X != 5", for example, we |
| 3217 | // should not accept a root of 2. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3218 | SCEVHandle Val = AddRec->evaluateAtIteration(R1, *this); |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 3219 | if (Val->isZero()) |
| 3220 | return R1; // We found a quadratic root! |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3221 | } |
| 3222 | } |
| 3223 | } |
| 3224 | |
| 3225 | return UnknownValue; |
| 3226 | } |
| 3227 | |
| 3228 | /// HowFarToNonZero - Return the number of times a backedge checking the |
| 3229 | /// specified value for nonzero will execute. If not computable, return |
| 3230 | /// UnknownValue |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3231 | SCEVHandle ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3232 | // Loops that look like: while (X == 0) are very strange indeed. We don't |
| 3233 | // handle them yet except for the trivial case. This could be expanded in the |
| 3234 | // future as needed. |
| 3235 | |
| 3236 | // If the value is a constant, check to see if it is known to be non-zero |
| 3237 | // already. If so, the backedge will execute zero times. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3238 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Nick Lewycky | f680518 | 2008-02-21 09:14:53 +0000 | [diff] [blame] | 3239 | if (!C->getValue()->isNullValue()) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3240 | return getIntegerSCEV(0, C->getType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3241 | return UnknownValue; // Otherwise it will loop infinitely. |
| 3242 | } |
| 3243 | |
| 3244 | // We could implement others, but I really doubt anyone writes loops like |
| 3245 | // this, and if they did, they would already be constant folded. |
| 3246 | return UnknownValue; |
| 3247 | } |
| 3248 | |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame^] | 3249 | /// getLoopPredecessor - If the given loop's header has exactly one unique |
| 3250 | /// predecessor outside the loop, return it. Otherwise return null. |
| 3251 | /// |
| 3252 | BasicBlock *ScalarEvolution::getLoopPredecessor(const Loop *L) { |
| 3253 | BasicBlock *Header = L->getHeader(); |
| 3254 | BasicBlock *Pred = 0; |
| 3255 | for (pred_iterator PI = pred_begin(Header), E = pred_end(Header); |
| 3256 | PI != E; ++PI) |
| 3257 | if (!L->contains(*PI)) { |
| 3258 | if (Pred && Pred != *PI) return 0; // Multiple predecessors. |
| 3259 | Pred = *PI; |
| 3260 | } |
| 3261 | return Pred; |
| 3262 | } |
| 3263 | |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 3264 | /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB |
| 3265 | /// (which may not be an immediate predecessor) which has exactly one |
| 3266 | /// successor from which BB is reachable, or null if no such block is |
| 3267 | /// found. |
| 3268 | /// |
| 3269 | BasicBlock * |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3270 | ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) { |
Dan Gohman | 1116ea7 | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 3271 | // If the block has a unique predecessor, then there is no path from the |
| 3272 | // predecessor to the block that does not go through the direct edge |
| 3273 | // from the predecessor to the block. |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 3274 | if (BasicBlock *Pred = BB->getSinglePredecessor()) |
| 3275 | return Pred; |
| 3276 | |
| 3277 | // A loop's header is defined to be a block that dominates the loop. |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame^] | 3278 | // If the header has a unique predecessor outside the loop, it must be |
| 3279 | // a block that has exactly one successor that can reach the loop. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3280 | if (Loop *L = LI->getLoopFor(BB)) |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame^] | 3281 | return getLoopPredecessor(L); |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 3282 | |
| 3283 | return 0; |
| 3284 | } |
| 3285 | |
Dan Gohman | cacd201 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 3286 | /// isLoopGuardedByCond - Test whether entry to the loop is protected by |
Dan Gohman | 1116ea7 | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 3287 | /// a conditional between LHS and RHS. This is used to help avoid max |
| 3288 | /// expressions in loop trip counts. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3289 | bool ScalarEvolution::isLoopGuardedByCond(const Loop *L, |
Dan Gohman | 1116ea7 | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 3290 | ICmpInst::Predicate Pred, |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3291 | const SCEV *LHS, const SCEV *RHS) { |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame^] | 3292 | BasicBlock *Predecessor = getLoopPredecessor(L); |
| 3293 | BasicBlock *PredecessorDest = L->getHeader(); |
Nick Lewycky | 1b020bf | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 3294 | |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame^] | 3295 | // Starting at the loop predecessor, climb up the predecessor chain, as long |
| 3296 | // as there are predecessors that can be found that have unique successors |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 3297 | // leading to the original header. |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame^] | 3298 | for (; Predecessor; |
| 3299 | PredecessorDest = Predecessor, |
| 3300 | Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) { |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 3301 | |
| 3302 | BranchInst *LoopEntryPredicate = |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame^] | 3303 | dyn_cast<BranchInst>(Predecessor->getTerminator()); |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 3304 | if (!LoopEntryPredicate || |
| 3305 | LoopEntryPredicate->isUnconditional()) |
| 3306 | continue; |
| 3307 | |
| 3308 | ICmpInst *ICI = dyn_cast<ICmpInst>(LoopEntryPredicate->getCondition()); |
| 3309 | if (!ICI) continue; |
| 3310 | |
| 3311 | // Now that we found a conditional branch that dominates the loop, check to |
| 3312 | // see if it is the comparison we are looking for. |
| 3313 | Value *PreCondLHS = ICI->getOperand(0); |
| 3314 | Value *PreCondRHS = ICI->getOperand(1); |
| 3315 | ICmpInst::Predicate Cond; |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame^] | 3316 | if (LoopEntryPredicate->getSuccessor(0) == PredecessorDest) |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 3317 | Cond = ICI->getPredicate(); |
| 3318 | else |
| 3319 | Cond = ICI->getInversePredicate(); |
| 3320 | |
Dan Gohman | cacd201 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 3321 | if (Cond == Pred) |
| 3322 | ; // An exact match. |
| 3323 | else if (!ICmpInst::isTrueWhenEqual(Cond) && Pred == ICmpInst::ICMP_NE) |
| 3324 | ; // The actual condition is beyond sufficient. |
| 3325 | else |
| 3326 | // Check a few special cases. |
| 3327 | switch (Cond) { |
| 3328 | case ICmpInst::ICMP_UGT: |
| 3329 | if (Pred == ICmpInst::ICMP_ULT) { |
| 3330 | std::swap(PreCondLHS, PreCondRHS); |
| 3331 | Cond = ICmpInst::ICMP_ULT; |
| 3332 | break; |
| 3333 | } |
| 3334 | continue; |
| 3335 | case ICmpInst::ICMP_SGT: |
| 3336 | if (Pred == ICmpInst::ICMP_SLT) { |
| 3337 | std::swap(PreCondLHS, PreCondRHS); |
| 3338 | Cond = ICmpInst::ICMP_SLT; |
| 3339 | break; |
| 3340 | } |
| 3341 | continue; |
| 3342 | case ICmpInst::ICMP_NE: |
| 3343 | // Expressions like (x >u 0) are often canonicalized to (x != 0), |
| 3344 | // so check for this case by checking if the NE is comparing against |
| 3345 | // a minimum or maximum constant. |
| 3346 | if (!ICmpInst::isTrueWhenEqual(Pred)) |
| 3347 | if (ConstantInt *CI = dyn_cast<ConstantInt>(PreCondRHS)) { |
| 3348 | const APInt &A = CI->getValue(); |
| 3349 | switch (Pred) { |
| 3350 | case ICmpInst::ICMP_SLT: |
| 3351 | if (A.isMaxSignedValue()) break; |
| 3352 | continue; |
| 3353 | case ICmpInst::ICMP_SGT: |
| 3354 | if (A.isMinSignedValue()) break; |
| 3355 | continue; |
| 3356 | case ICmpInst::ICMP_ULT: |
| 3357 | if (A.isMaxValue()) break; |
| 3358 | continue; |
| 3359 | case ICmpInst::ICMP_UGT: |
| 3360 | if (A.isMinValue()) break; |
| 3361 | continue; |
| 3362 | default: |
| 3363 | continue; |
| 3364 | } |
| 3365 | Cond = ICmpInst::ICMP_NE; |
| 3366 | // NE is symmetric but the original comparison may not be. Swap |
| 3367 | // the operands if necessary so that they match below. |
| 3368 | if (isa<SCEVConstant>(LHS)) |
| 3369 | std::swap(PreCondLHS, PreCondRHS); |
| 3370 | break; |
| 3371 | } |
| 3372 | continue; |
| 3373 | default: |
| 3374 | // We weren't able to reconcile the condition. |
| 3375 | continue; |
| 3376 | } |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 3377 | |
| 3378 | if (!PreCondLHS->getType()->isInteger()) continue; |
| 3379 | |
| 3380 | SCEVHandle PreCondLHSSCEV = getSCEV(PreCondLHS); |
| 3381 | SCEVHandle PreCondRHSSCEV = getSCEV(PreCondRHS); |
| 3382 | if ((LHS == PreCondLHSSCEV && RHS == PreCondRHSSCEV) || |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3383 | (LHS == getNotSCEV(PreCondRHSSCEV) && |
| 3384 | RHS == getNotSCEV(PreCondLHSSCEV))) |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 3385 | return true; |
Nick Lewycky | 1b020bf | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 3388 | return false; |
Nick Lewycky | 1b020bf | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3391 | /// HowManyLessThans - Return the number of times a backedge containing the |
| 3392 | /// specified less-than comparison will execute. If not computable, return |
| 3393 | /// UnknownValue. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3394 | ScalarEvolution::BackedgeTakenInfo ScalarEvolution:: |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3395 | HowManyLessThans(const SCEV *LHS, const SCEV *RHS, |
| 3396 | const Loop *L, bool isSigned) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3397 | // Only handle: "ADDREC < LoopInvariant". |
| 3398 | if (!RHS->isLoopInvariant(L)) return UnknownValue; |
| 3399 | |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3400 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3401 | if (!AddRec || AddRec->getLoop() != L) |
| 3402 | return UnknownValue; |
| 3403 | |
| 3404 | if (AddRec->isAffine()) { |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 3405 | // FORNOW: We only support unit strides. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3406 | unsigned BitWidth = getTypeSizeInBits(AddRec->getType()); |
| 3407 | SCEVHandle Step = AddRec->getStepRecurrence(*this); |
| 3408 | SCEVHandle NegOne = getIntegerSCEV(-1, AddRec->getType()); |
| 3409 | |
| 3410 | // TODO: handle non-constant strides. |
| 3411 | const SCEVConstant *CStep = dyn_cast<SCEVConstant>(Step); |
| 3412 | if (!CStep || CStep->isZero()) |
| 3413 | return UnknownValue; |
Dan Gohman | f8bc8e8 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 3414 | if (CStep->isOne()) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3415 | // With unit stride, the iteration never steps past the limit value. |
| 3416 | } else if (CStep->getValue()->getValue().isStrictlyPositive()) { |
| 3417 | if (const SCEVConstant *CLimit = dyn_cast<SCEVConstant>(RHS)) { |
| 3418 | // Test whether a positive iteration iteration can step past the limit |
| 3419 | // value and past the maximum value for its type in a single step. |
| 3420 | if (isSigned) { |
| 3421 | APInt Max = APInt::getSignedMaxValue(BitWidth); |
| 3422 | if ((Max - CStep->getValue()->getValue()) |
| 3423 | .slt(CLimit->getValue()->getValue())) |
| 3424 | return UnknownValue; |
| 3425 | } else { |
| 3426 | APInt Max = APInt::getMaxValue(BitWidth); |
| 3427 | if ((Max - CStep->getValue()->getValue()) |
| 3428 | .ult(CLimit->getValue()->getValue())) |
| 3429 | return UnknownValue; |
| 3430 | } |
| 3431 | } else |
| 3432 | // TODO: handle non-constant limit values below. |
| 3433 | return UnknownValue; |
| 3434 | } else |
| 3435 | // TODO: handle negative strides below. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3436 | return UnknownValue; |
| 3437 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3438 | // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant |
| 3439 | // m. So, we count the number of iterations in which {n,+,s} < m is true. |
| 3440 | // Note that we cannot simply return max(m-n,0)/s because it's not safe to |
Wojciech Matyjewicz | 1377a54 | 2008-02-13 12:21:32 +0000 | [diff] [blame] | 3441 | // treat m-n as signed nor unsigned due to overflow possibility. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3442 | |
Wojciech Matyjewicz | ebc77b1 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 3443 | // First, we get the value of the LHS in the first iteration: n |
| 3444 | SCEVHandle Start = AddRec->getOperand(0); |
| 3445 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3446 | // Determine the minimum constant start value. |
| 3447 | SCEVHandle MinStart = isa<SCEVConstant>(Start) ? Start : |
| 3448 | getConstant(isSigned ? APInt::getSignedMinValue(BitWidth) : |
| 3449 | APInt::getMinValue(BitWidth)); |
Wojciech Matyjewicz | ebc77b1 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 3450 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3451 | // If we know that the condition is true in order to enter the loop, |
| 3452 | // then we know that it will run exactly (m-n)/s times. Otherwise, we |
| 3453 | // only know if will execute (max(m,n)-n)/s times. In both cases, the |
| 3454 | // division must round up. |
| 3455 | SCEVHandle End = RHS; |
| 3456 | if (!isLoopGuardedByCond(L, |
| 3457 | isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
| 3458 | getMinusSCEV(Start, Step), RHS)) |
| 3459 | End = isSigned ? getSMaxExpr(RHS, Start) |
| 3460 | : getUMaxExpr(RHS, Start); |
| 3461 | |
| 3462 | // Determine the maximum constant end value. |
| 3463 | SCEVHandle MaxEnd = isa<SCEVConstant>(End) ? End : |
| 3464 | getConstant(isSigned ? APInt::getSignedMaxValue(BitWidth) : |
| 3465 | APInt::getMaxValue(BitWidth)); |
| 3466 | |
| 3467 | // Finally, we subtract these two values and divide, rounding up, to get |
| 3468 | // the number of times the backedge is executed. |
| 3469 | SCEVHandle BECount = getUDivExpr(getAddExpr(getMinusSCEV(End, Start), |
| 3470 | getAddExpr(Step, NegOne)), |
| 3471 | Step); |
| 3472 | |
| 3473 | // The maximum backedge count is similar, except using the minimum start |
| 3474 | // value and the maximum end value. |
| 3475 | SCEVHandle MaxBECount = getUDivExpr(getAddExpr(getMinusSCEV(MaxEnd, |
| 3476 | MinStart), |
| 3477 | getAddExpr(Step, NegOne)), |
| 3478 | Step); |
| 3479 | |
| 3480 | return BackedgeTakenInfo(BECount, MaxBECount); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
| 3483 | return UnknownValue; |
| 3484 | } |
| 3485 | |
| 3486 | /// getNumIterationsInRange - Return the number of iterations of this loop that |
| 3487 | /// produce values in the specified constant range. Another way of looking at |
| 3488 | /// this is that it returns the first iteration number where the value is not in |
| 3489 | /// the condition, thus computing the exit count. If the iteration count can't |
| 3490 | /// be computed, an instance of SCEVCouldNotCompute is returned. |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3491 | SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, |
| 3492 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3493 | if (Range.isFullSet()) // Infinite loop. |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 3494 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3495 | |
| 3496 | // If the start is a non-zero constant, shift the range to simplify things. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3497 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3498 | if (!SC->getValue()->isZero()) { |
| 3499 | std::vector<SCEVHandle> Operands(op_begin(), op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3500 | Operands[0] = SE.getIntegerSCEV(0, SC->getType()); |
| 3501 | SCEVHandle Shifted = SE.getAddRecExpr(Operands, getLoop()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3502 | if (const SCEVAddRecExpr *ShiftedAddRec = |
| 3503 | dyn_cast<SCEVAddRecExpr>(Shifted)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3504 | return ShiftedAddRec->getNumIterationsInRange( |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3505 | Range.subtract(SC->getValue()->getValue()), SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3506 | // This is strange and shouldn't happen. |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 3507 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | // The only time we can solve this is when we have all constant indices. |
| 3511 | // Otherwise, we cannot determine the overflow conditions. |
| 3512 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 3513 | if (!isa<SCEVConstant>(getOperand(i))) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 3514 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3515 | |
| 3516 | |
| 3517 | // Okay at this point we know that all elements of the chrec are constants and |
| 3518 | // that the start element is zero. |
| 3519 | |
| 3520 | // First check to see if the range contains zero. If not, the first |
| 3521 | // iteration exits. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 3522 | unsigned BitWidth = SE.getTypeSizeInBits(getType()); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3523 | if (!Range.contains(APInt(BitWidth, 0))) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3524 | return SE.getConstant(ConstantInt::get(getType(),0)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3525 | |
| 3526 | if (isAffine()) { |
| 3527 | // If this is an affine expression then we have this situation: |
| 3528 | // Solve {0,+,A} in Range === Ax in Range |
| 3529 | |
| 3530 | // We know that zero is in the range. If A is positive then we know that |
| 3531 | // the upper value of the range must be the first possible exit value. |
| 3532 | // If A is negative then the lower of the range is the last possible loop |
| 3533 | // value. Also note that we already checked for a full range. |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3534 | APInt One(BitWidth,1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3535 | APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue(); |
| 3536 | APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower(); |
| 3537 | |
| 3538 | // The exit value should be (End+A)/A. |
Nick Lewycky | a0facae | 2007-09-27 14:12:54 +0000 | [diff] [blame] | 3539 | APInt ExitVal = (End + A).udiv(A); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3540 | ConstantInt *ExitValue = ConstantInt::get(ExitVal); |
| 3541 | |
| 3542 | // Evaluate at the exit value. If we really did fall out of the valid |
| 3543 | // range, then we computed our trip count, otherwise wrap around or other |
| 3544 | // things must have happened. |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3545 | ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3546 | if (Range.contains(Val->getValue())) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 3547 | return SE.getCouldNotCompute(); // Something strange happened |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3548 | |
| 3549 | // Ensure that the previous value is in the range. This is a sanity check. |
| 3550 | assert(Range.contains( |
| 3551 | EvaluateConstantChrecAtConstant(this, |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3552 | ConstantInt::get(ExitVal - One), SE)->getValue()) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3553 | "Linear scev computation is off in a bad way!"); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3554 | return SE.getConstant(ExitValue); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3555 | } else if (isQuadratic()) { |
| 3556 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the |
| 3557 | // quadratic equation to solve it. To do this, we must frame our problem in |
| 3558 | // terms of figuring out when zero is crossed, instead of when |
| 3559 | // Range.getUpper() is crossed. |
| 3560 | std::vector<SCEVHandle> NewOps(op_begin(), op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3561 | NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper())); |
| 3562 | SCEVHandle NewAddRec = SE.getAddRecExpr(NewOps, getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3563 | |
| 3564 | // Next, solve the constructed addrec |
| 3565 | std::pair<SCEVHandle,SCEVHandle> Roots = |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3566 | SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3567 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 3568 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3569 | if (R1) { |
| 3570 | // Pick the smallest positive root value. |
| 3571 | if (ConstantInt *CB = |
| 3572 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, |
| 3573 | R1->getValue(), R2->getValue()))) { |
| 3574 | if (CB->getZExtValue() == false) |
| 3575 | std::swap(R1, R2); // R1 is the minimum root now. |
| 3576 | |
| 3577 | // Make sure the root is not off by one. The returned iteration should |
| 3578 | // not be in the range, but the previous one should be. When solving |
| 3579 | // for "X*X < 5", for example, we should not return a root of 2. |
| 3580 | ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this, |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3581 | R1->getValue(), |
| 3582 | SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3583 | if (Range.contains(R1Val->getValue())) { |
| 3584 | // The next iteration must be out of the range... |
| 3585 | ConstantInt *NextVal = ConstantInt::get(R1->getValue()->getValue()+1); |
| 3586 | |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3587 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3588 | if (!Range.contains(R1Val->getValue())) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3589 | return SE.getConstant(NextVal); |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 3590 | return SE.getCouldNotCompute(); // Something strange happened |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | // If R1 was not in the range, then it is a good return value. Make |
| 3594 | // sure that R1-1 WAS in the range though, just in case. |
| 3595 | ConstantInt *NextVal = ConstantInt::get(R1->getValue()->getValue()-1); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3596 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3597 | if (Range.contains(R1Val->getValue())) |
| 3598 | return R1; |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 3599 | return SE.getCouldNotCompute(); // Something strange happened |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3600 | } |
| 3601 | } |
| 3602 | } |
| 3603 | |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 3604 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | |
| 3608 | |
| 3609 | //===----------------------------------------------------------------------===// |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3610 | // SCEVCallbackVH Class Implementation |
| 3611 | //===----------------------------------------------------------------------===// |
| 3612 | |
| 3613 | void SCEVCallbackVH::deleted() { |
| 3614 | assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!"); |
| 3615 | if (PHINode *PN = dyn_cast<PHINode>(getValPtr())) |
| 3616 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3617 | if (Instruction *I = dyn_cast<Instruction>(getValPtr())) |
| 3618 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3619 | SE->Scalars.erase(getValPtr()); |
| 3620 | // this now dangles! |
| 3621 | } |
| 3622 | |
| 3623 | void SCEVCallbackVH::allUsesReplacedWith(Value *) { |
| 3624 | assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!"); |
| 3625 | |
| 3626 | // Forget all the expressions associated with users of the old value, |
| 3627 | // so that future queries will recompute the expressions using the new |
| 3628 | // value. |
| 3629 | SmallVector<User *, 16> Worklist; |
| 3630 | Value *Old = getValPtr(); |
| 3631 | bool DeleteOld = false; |
| 3632 | for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end(); |
| 3633 | UI != UE; ++UI) |
| 3634 | Worklist.push_back(*UI); |
| 3635 | while (!Worklist.empty()) { |
| 3636 | User *U = Worklist.pop_back_val(); |
| 3637 | // Deleting the Old value will cause this to dangle. Postpone |
| 3638 | // that until everything else is done. |
| 3639 | if (U == Old) { |
| 3640 | DeleteOld = true; |
| 3641 | continue; |
| 3642 | } |
| 3643 | if (PHINode *PN = dyn_cast<PHINode>(U)) |
| 3644 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3645 | if (Instruction *I = dyn_cast<Instruction>(U)) |
| 3646 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3647 | if (SE->Scalars.erase(U)) |
| 3648 | for (Value::use_iterator UI = U->use_begin(), UE = U->use_end(); |
| 3649 | UI != UE; ++UI) |
| 3650 | Worklist.push_back(*UI); |
| 3651 | } |
| 3652 | if (DeleteOld) { |
| 3653 | if (PHINode *PN = dyn_cast<PHINode>(Old)) |
| 3654 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3655 | if (Instruction *I = dyn_cast<Instruction>(Old)) |
| 3656 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3657 | SE->Scalars.erase(Old); |
| 3658 | // this now dangles! |
| 3659 | } |
| 3660 | // this may dangle! |
| 3661 | } |
| 3662 | |
| 3663 | SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) |
| 3664 | : CallbackVH(V), SE(se) {} |
| 3665 | |
| 3666 | //===----------------------------------------------------------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3667 | // ScalarEvolution Class Implementation |
| 3668 | //===----------------------------------------------------------------------===// |
| 3669 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3670 | ScalarEvolution::ScalarEvolution() |
| 3671 | : FunctionPass(&ID), UnknownValue(new SCEVCouldNotCompute()) { |
| 3672 | } |
| 3673 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3674 | bool ScalarEvolution::runOnFunction(Function &F) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3675 | this->F = &F; |
| 3676 | LI = &getAnalysis<LoopInfo>(); |
| 3677 | TD = getAnalysisIfAvailable<TargetData>(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3678 | return false; |
| 3679 | } |
| 3680 | |
| 3681 | void ScalarEvolution::releaseMemory() { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3682 | Scalars.clear(); |
| 3683 | BackedgeTakenCounts.clear(); |
| 3684 | ConstantEvolutionLoopExitValue.clear(); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3685 | ValuesAtScopes.clear(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3686 | } |
| 3687 | |
| 3688 | void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const { |
| 3689 | AU.setPreservesAll(); |
| 3690 | AU.addRequiredTransitive<LoopInfo>(); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3691 | } |
| 3692 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3693 | bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3694 | return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3695 | } |
| 3696 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3697 | static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3698 | const Loop *L) { |
| 3699 | // Print all inner loops first |
| 3700 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 3701 | PrintLoopInfo(OS, SE, *I); |
| 3702 | |
Nick Lewycky | e5da191 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 3703 | OS << "Loop " << L->getHeader()->getName() << ": "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3704 | |
Devang Patel | 02451fa | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 3705 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3706 | L->getExitBlocks(ExitBlocks); |
| 3707 | if (ExitBlocks.size() != 1) |
Nick Lewycky | e5da191 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 3708 | OS << "<multiple exits> "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3709 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3710 | if (SE->hasLoopInvariantBackedgeTakenCount(L)) { |
| 3711 | OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3712 | } else { |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3713 | OS << "Unpredictable backedge-taken count. "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3714 | } |
| 3715 | |
Nick Lewycky | e5da191 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 3716 | OS << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3717 | } |
| 3718 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3719 | void ScalarEvolution::print(raw_ostream &OS, const Module* ) const { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3720 | // ScalarEvolution's implementaiton of the print method is to print |
| 3721 | // out SCEV values of all instructions that are interesting. Doing |
| 3722 | // this potentially causes it to create new SCEV objects though, |
| 3723 | // which technically conflicts with the const qualifier. This isn't |
| 3724 | // observable from outside the class though (the hasSCEV function |
| 3725 | // notwithstanding), so casting away the const isn't dangerous. |
| 3726 | ScalarEvolution &SE = *const_cast<ScalarEvolution*>(this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3727 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3728 | OS << "Classifying expressions for: " << F->getName() << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3729 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
Dan Gohman | 43d37e9 | 2009-04-30 01:30:18 +0000 | [diff] [blame] | 3730 | if (isSCEVable(I->getType())) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3731 | OS << *I; |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 3732 | OS << " --> "; |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3733 | SCEVHandle SV = SE.getSCEV(&*I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3734 | SV->print(OS); |
| 3735 | OS << "\t\t"; |
| 3736 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3737 | if (const Loop *L = LI->getLoopFor((*I).getParent())) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3738 | OS << "Exits: "; |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3739 | SCEVHandle ExitValue = SE.getSCEVAtScope(&*I, L->getParentLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3740 | if (isa<SCEVCouldNotCompute>(ExitValue)) { |
| 3741 | OS << "<<Unknown>>"; |
| 3742 | } else { |
| 3743 | OS << *ExitValue; |
| 3744 | } |
| 3745 | } |
| 3746 | |
| 3747 | |
| 3748 | OS << "\n"; |
| 3749 | } |
| 3750 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3751 | OS << "Determining loop execution counts for: " << F->getName() << "\n"; |
| 3752 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
| 3753 | PrintLoopInfo(OS, &SE, *I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3754 | } |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3755 | |
| 3756 | void ScalarEvolution::print(std::ostream &o, const Module *M) const { |
| 3757 | raw_os_ostream OS(o); |
| 3758 | print(OS, M); |
| 3759 | } |