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