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