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