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