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