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