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