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