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