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