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