Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- ScalarEvolution.cpp - Scalar Evolution Analysis ----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 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 |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 17 | // can handle. These classes are reference counted, managed by the const SCEV* |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
| 31 | // |
| 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 | // |
| 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 | |
| 62 | #define DEBUG_TYPE "scalar-evolution" |
| 63 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 64 | #include "llvm/Constants.h" |
| 65 | #include "llvm/DerivedTypes.h" |
| 66 | #include "llvm/GlobalVariable.h" |
| 67 | #include "llvm/Instructions.h" |
| 68 | #include "llvm/Analysis/ConstantFolding.h" |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 69 | #include "llvm/Analysis/Dominators.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 70 | #include "llvm/Analysis/LoopInfo.h" |
Dan Gohman | a7726c3 | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 71 | #include "llvm/Analysis/ValueTracking.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 72 | #include "llvm/Assembly/Writer.h" |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 73 | #include "llvm/Target/TargetData.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 74 | #include "llvm/Support/CommandLine.h" |
| 75 | #include "llvm/Support/Compiler.h" |
| 76 | #include "llvm/Support/ConstantRange.h" |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 77 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 78 | #include "llvm/Support/InstIterator.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 79 | #include "llvm/Support/MathExtras.h" |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 80 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 81 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 82 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 83 | #include <algorithm> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 84 | using namespace llvm; |
| 85 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 86 | STATISTIC(NumArrayLenItCounts, |
| 87 | "Number of trip counts computed with array length"); |
| 88 | STATISTIC(NumTripCountsComputed, |
| 89 | "Number of loops with predictable loop counts"); |
| 90 | STATISTIC(NumTripCountsNotComputed, |
| 91 | "Number of loops without predictable loop counts"); |
| 92 | STATISTIC(NumBruteForceTripCountsComputed, |
| 93 | "Number of loops with trip counts computed by force"); |
| 94 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 95 | static cl::opt<unsigned> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 96 | MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, |
| 97 | cl::desc("Maximum number of iterations SCEV will " |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 98 | "symbolically execute a constant " |
| 99 | "derived loop"), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 100 | cl::init(100)); |
| 101 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 102 | static RegisterPass<ScalarEvolution> |
| 103 | R("scalar-evolution", "Scalar Evolution Analysis", false, true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 104 | char ScalarEvolution::ID = 0; |
| 105 | |
| 106 | //===----------------------------------------------------------------------===// |
| 107 | // SCEV class definitions |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | |
| 110 | //===----------------------------------------------------------------------===// |
| 111 | // Implementation of the SCEV class. |
| 112 | // |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame^] | 113 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 114 | SCEV::~SCEV() {} |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame^] | 115 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 116 | void SCEV::dump() const { |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 117 | print(errs()); |
| 118 | errs() << '\n'; |
| 119 | } |
| 120 | |
| 121 | void SCEV::print(std::ostream &o) const { |
| 122 | raw_os_ostream OS(o); |
| 123 | print(OS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 126 | bool SCEV::isZero() const { |
| 127 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 128 | return SC->getValue()->isZero(); |
| 129 | return false; |
| 130 | } |
| 131 | |
Dan Gohman | f8bc8e8 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 132 | bool SCEV::isOne() const { |
| 133 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 134 | return SC->getValue()->isOne(); |
| 135 | return false; |
| 136 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 137 | |
Dan Gohman | f05118e | 2009-06-24 00:30:26 +0000 | [diff] [blame] | 138 | bool SCEV::isAllOnesValue() const { |
| 139 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 140 | return SC->getValue()->isAllOnesValue(); |
| 141 | return false; |
| 142 | } |
| 143 | |
Owen Anderson | b70139d | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 144 | SCEVCouldNotCompute::SCEVCouldNotCompute() : |
| 145 | SCEV(scCouldNotCompute) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 146 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 147 | void SCEVCouldNotCompute::Profile(FoldingSetNodeID &ID) const { |
| 148 | assert(0 && "Attempt to use a SCEVCouldNotCompute object!"); |
| 149 | } |
| 150 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 151 | bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const { |
| 152 | assert(0 && "Attempt to use a SCEVCouldNotCompute object!"); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | const Type *SCEVCouldNotCompute::getType() const { |
| 157 | assert(0 && "Attempt to use a SCEVCouldNotCompute object!"); |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const { |
| 162 | assert(0 && "Attempt to use a SCEVCouldNotCompute object!"); |
| 163 | return false; |
| 164 | } |
| 165 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 166 | const SCEV * |
| 167 | SCEVCouldNotCompute::replaceSymbolicValuesWithConcrete( |
| 168 | const SCEV *Sym, |
| 169 | const SCEV *Conc, |
| 170 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 171 | return this; |
| 172 | } |
| 173 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 174 | void SCEVCouldNotCompute::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 175 | OS << "***COULDNOTCOMPUTE***"; |
| 176 | } |
| 177 | |
| 178 | bool SCEVCouldNotCompute::classof(const SCEV *S) { |
| 179 | return S->getSCEVType() == scCouldNotCompute; |
| 180 | } |
| 181 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 182 | const SCEV* ScalarEvolution::getConstant(ConstantInt *V) { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 183 | FoldingSetNodeID ID; |
| 184 | ID.AddInteger(scConstant); |
| 185 | ID.AddPointer(V); |
| 186 | void *IP = 0; |
| 187 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 188 | SCEV *S = SCEVAllocator.Allocate<SCEVConstant>(); |
| 189 | new (S) SCEVConstant(V); |
| 190 | UniqueSCEVs.InsertNode(S, IP); |
| 191 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 194 | const SCEV* ScalarEvolution::getConstant(const APInt& Val) { |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 195 | return getConstant(ConstantInt::get(Val)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 198 | const SCEV* |
Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 199 | ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) { |
| 200 | return getConstant(ConstantInt::get(cast<IntegerType>(Ty), V, isSigned)); |
| 201 | } |
| 202 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 203 | void SCEVConstant::Profile(FoldingSetNodeID &ID) const { |
| 204 | ID.AddInteger(scConstant); |
| 205 | ID.AddPointer(V); |
| 206 | } |
| 207 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 208 | const Type *SCEVConstant::getType() const { return V->getType(); } |
| 209 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 210 | void SCEVConstant::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 211 | WriteAsOperand(OS, V, false); |
| 212 | } |
| 213 | |
Dan Gohman | 2a38153 | 2009-04-21 01:25:57 +0000 | [diff] [blame] | 214 | SCEVCastExpr::SCEVCastExpr(unsigned SCEVTy, |
Owen Anderson | b70139d | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 215 | const SCEV* op, const Type *ty) |
| 216 | : SCEV(SCEVTy), Op(op), Ty(ty) {} |
Dan Gohman | 2a38153 | 2009-04-21 01:25:57 +0000 | [diff] [blame] | 217 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 218 | void SCEVCastExpr::Profile(FoldingSetNodeID &ID) const { |
| 219 | ID.AddInteger(getSCEVType()); |
| 220 | ID.AddPointer(Op); |
| 221 | ID.AddPointer(Ty); |
| 222 | } |
| 223 | |
Dan Gohman | 2a38153 | 2009-04-21 01:25:57 +0000 | [diff] [blame] | 224 | bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 225 | return Op->dominates(BB, DT); |
| 226 | } |
| 227 | |
Owen Anderson | b70139d | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 228 | SCEVTruncateExpr::SCEVTruncateExpr(const SCEV* op, const Type *ty) |
| 229 | : SCEVCastExpr(scTruncate, op, ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 230 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 231 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 232 | "Cannot truncate non-integer value!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 235 | void SCEVTruncateExpr::print(raw_ostream &OS) const { |
Dan Gohman | c911922 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 236 | OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Owen Anderson | b70139d | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 239 | SCEVZeroExtendExpr::SCEVZeroExtendExpr(const SCEV* op, const Type *ty) |
| 240 | : SCEVCastExpr(scZeroExtend, op, ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 241 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 242 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 243 | "Cannot zero extend non-integer value!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 246 | void SCEVZeroExtendExpr::print(raw_ostream &OS) const { |
Dan Gohman | c911922 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 247 | OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Owen Anderson | b70139d | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 250 | SCEVSignExtendExpr::SCEVSignExtendExpr(const SCEV* op, const Type *ty) |
| 251 | : SCEVCastExpr(scSignExtend, op, ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 252 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 253 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 254 | "Cannot sign extend non-integer value!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 257 | void SCEVSignExtendExpr::print(raw_ostream &OS) const { |
Dan Gohman | c911922 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 258 | OS << "(sext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 261 | void SCEVCommutativeExpr::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 262 | assert(Operands.size() > 1 && "This plus expr shouldn't exist!"); |
| 263 | const char *OpStr = getOperationStr(); |
| 264 | OS << "(" << *Operands[0]; |
| 265 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 266 | OS << OpStr << *Operands[i]; |
| 267 | OS << ")"; |
| 268 | } |
| 269 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 270 | const SCEV * |
| 271 | SCEVCommutativeExpr::replaceSymbolicValuesWithConcrete( |
| 272 | const SCEV *Sym, |
| 273 | const SCEV *Conc, |
| 274 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 275 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 276 | const SCEV* H = |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 277 | getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 278 | if (H != getOperand(i)) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 279 | SmallVector<const SCEV*, 8> NewOps; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 280 | NewOps.reserve(getNumOperands()); |
| 281 | for (unsigned j = 0; j != i; ++j) |
| 282 | NewOps.push_back(getOperand(j)); |
| 283 | NewOps.push_back(H); |
| 284 | for (++i; i != e; ++i) |
| 285 | NewOps.push_back(getOperand(i)-> |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 286 | replaceSymbolicValuesWithConcrete(Sym, Conc, SE)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 287 | |
| 288 | if (isa<SCEVAddExpr>(this)) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 289 | return SE.getAddExpr(NewOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 290 | else if (isa<SCEVMulExpr>(this)) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 291 | return SE.getMulExpr(NewOps); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 292 | else if (isa<SCEVSMaxExpr>(this)) |
| 293 | return SE.getSMaxExpr(NewOps); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 294 | else if (isa<SCEVUMaxExpr>(this)) |
| 295 | return SE.getUMaxExpr(NewOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 296 | else |
| 297 | assert(0 && "Unknown commutative expr!"); |
| 298 | } |
| 299 | } |
| 300 | return this; |
| 301 | } |
| 302 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 303 | void SCEVNAryExpr::Profile(FoldingSetNodeID &ID) const { |
| 304 | ID.AddInteger(getSCEVType()); |
| 305 | ID.AddInteger(Operands.size()); |
| 306 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 307 | ID.AddPointer(Operands[i]); |
| 308 | } |
| 309 | |
Dan Gohman | 72a8a02 | 2009-05-07 14:00:19 +0000 | [diff] [blame] | 310 | bool SCEVNAryExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 311 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 312 | if (!getOperand(i)->dominates(BB, DT)) |
| 313 | return false; |
| 314 | } |
| 315 | return true; |
| 316 | } |
| 317 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 318 | void SCEVUDivExpr::Profile(FoldingSetNodeID &ID) const { |
| 319 | ID.AddInteger(scUDivExpr); |
| 320 | ID.AddPointer(LHS); |
| 321 | ID.AddPointer(RHS); |
| 322 | } |
| 323 | |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 324 | bool SCEVUDivExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 325 | return LHS->dominates(BB, DT) && RHS->dominates(BB, DT); |
| 326 | } |
| 327 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 328 | void SCEVUDivExpr::print(raw_ostream &OS) const { |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 329 | OS << "(" << *LHS << " /u " << *RHS << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 332 | const Type *SCEVUDivExpr::getType() const { |
Dan Gohman | 140f08f | 2009-05-26 17:44:05 +0000 | [diff] [blame] | 333 | // In most cases the types of LHS and RHS will be the same, but in some |
| 334 | // crazy cases one or the other may be a pointer. ScalarEvolution doesn't |
| 335 | // depend on the type for correctness, but handling types carefully can |
| 336 | // avoid extra casts in the SCEVExpander. The LHS is more likely to be |
| 337 | // a pointer type than the RHS, so use the RHS' type here. |
| 338 | return RHS->getType(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 341 | void SCEVAddRecExpr::Profile(FoldingSetNodeID &ID) const { |
| 342 | ID.AddInteger(scAddRecExpr); |
| 343 | ID.AddInteger(Operands.size()); |
| 344 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 345 | ID.AddPointer(Operands[i]); |
| 346 | ID.AddPointer(L); |
| 347 | } |
| 348 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 349 | const SCEV * |
| 350 | SCEVAddRecExpr::replaceSymbolicValuesWithConcrete(const SCEV *Sym, |
| 351 | const SCEV *Conc, |
| 352 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 353 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 354 | const SCEV* H = |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 355 | getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 356 | if (H != getOperand(i)) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 357 | SmallVector<const SCEV*, 8> NewOps; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 358 | NewOps.reserve(getNumOperands()); |
| 359 | for (unsigned j = 0; j != i; ++j) |
| 360 | NewOps.push_back(getOperand(j)); |
| 361 | NewOps.push_back(H); |
| 362 | for (++i; i != e; ++i) |
| 363 | NewOps.push_back(getOperand(i)-> |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 364 | replaceSymbolicValuesWithConcrete(Sym, Conc, SE)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 365 | |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 366 | return SE.getAddRecExpr(NewOps, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | return this; |
| 370 | } |
| 371 | |
| 372 | |
| 373 | bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const { |
Dan Gohman | ae1eaae | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 374 | // Add recurrences are never invariant in the function-body (null loop). |
Dan Gohman | 2d888d8 | 2009-06-26 22:17:21 +0000 | [diff] [blame] | 375 | if (!QueryLoop) |
| 376 | return false; |
| 377 | |
| 378 | // This recurrence is variant w.r.t. QueryLoop if QueryLoop contains L. |
| 379 | if (QueryLoop->contains(L->getHeader())) |
| 380 | return false; |
| 381 | |
| 382 | // This recurrence is variant w.r.t. QueryLoop if any of its operands |
| 383 | // are variant. |
| 384 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 385 | if (!getOperand(i)->isLoopInvariant(QueryLoop)) |
| 386 | return false; |
| 387 | |
| 388 | // Otherwise it's loop-invariant. |
| 389 | return true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 392 | void SCEVAddRecExpr::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 393 | OS << "{" << *Operands[0]; |
| 394 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 395 | OS << ",+," << *Operands[i]; |
| 396 | OS << "}<" << L->getHeader()->getName() + ">"; |
| 397 | } |
| 398 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 399 | void SCEVUnknown::Profile(FoldingSetNodeID &ID) const { |
| 400 | ID.AddInteger(scUnknown); |
| 401 | ID.AddPointer(V); |
| 402 | } |
| 403 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 404 | bool SCEVUnknown::isLoopInvariant(const Loop *L) const { |
| 405 | // All non-instruction values are loop invariant. All instructions are loop |
| 406 | // invariant if they are not contained in the specified loop. |
Dan Gohman | ae1eaae | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 407 | // Instructions are never considered invariant in the function body |
| 408 | // (null loop) because they are defined within the "loop". |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 409 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Dan Gohman | ae1eaae | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 410 | return L && !L->contains(I->getParent()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 411 | return true; |
| 412 | } |
| 413 | |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 414 | bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 415 | if (Instruction *I = dyn_cast<Instruction>(getValue())) |
| 416 | return DT->dominates(I->getParent(), BB); |
| 417 | return true; |
| 418 | } |
| 419 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 420 | const Type *SCEVUnknown::getType() const { |
| 421 | return V->getType(); |
| 422 | } |
| 423 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 424 | void SCEVUnknown::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 425 | WriteAsOperand(OS, V, false); |
| 426 | } |
| 427 | |
| 428 | //===----------------------------------------------------------------------===// |
| 429 | // SCEV Utilities |
| 430 | //===----------------------------------------------------------------------===// |
| 431 | |
| 432 | namespace { |
| 433 | /// SCEVComplexityCompare - Return true if the complexity of the LHS is less |
| 434 | /// than the complexity of the RHS. This comparator is used to canonicalize |
| 435 | /// expressions. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 436 | class VISIBILITY_HIDDEN SCEVComplexityCompare { |
| 437 | LoopInfo *LI; |
| 438 | public: |
| 439 | explicit SCEVComplexityCompare(LoopInfo *li) : LI(li) {} |
| 440 | |
Dan Gohman | c0c69cf | 2008-04-14 18:23:56 +0000 | [diff] [blame] | 441 | bool operator()(const SCEV *LHS, const SCEV *RHS) const { |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 442 | // Primarily, sort the SCEVs by their getSCEVType(). |
| 443 | if (LHS->getSCEVType() != RHS->getSCEVType()) |
| 444 | return LHS->getSCEVType() < RHS->getSCEVType(); |
| 445 | |
| 446 | // Aside from the getSCEVType() ordering, the particular ordering |
| 447 | // isn't very important except that it's beneficial to be consistent, |
| 448 | // so that (a + b) and (b + a) don't end up as different expressions. |
| 449 | |
| 450 | // Sort SCEVUnknown values with some loose heuristics. TODO: This is |
| 451 | // not as complete as it could be. |
| 452 | if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) { |
| 453 | const SCEVUnknown *RU = cast<SCEVUnknown>(RHS); |
| 454 | |
Dan Gohman | d0c0123 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 455 | // Order pointer values after integer values. This helps SCEVExpander |
| 456 | // form GEPs. |
| 457 | if (isa<PointerType>(LU->getType()) && !isa<PointerType>(RU->getType())) |
| 458 | return false; |
| 459 | if (isa<PointerType>(RU->getType()) && !isa<PointerType>(LU->getType())) |
| 460 | return true; |
| 461 | |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 462 | // Compare getValueID values. |
| 463 | if (LU->getValue()->getValueID() != RU->getValue()->getValueID()) |
| 464 | return LU->getValue()->getValueID() < RU->getValue()->getValueID(); |
| 465 | |
| 466 | // Sort arguments by their position. |
| 467 | if (const Argument *LA = dyn_cast<Argument>(LU->getValue())) { |
| 468 | const Argument *RA = cast<Argument>(RU->getValue()); |
| 469 | return LA->getArgNo() < RA->getArgNo(); |
| 470 | } |
| 471 | |
| 472 | // For instructions, compare their loop depth, and their opcode. |
| 473 | // This is pretty loose. |
| 474 | if (Instruction *LV = dyn_cast<Instruction>(LU->getValue())) { |
| 475 | Instruction *RV = cast<Instruction>(RU->getValue()); |
| 476 | |
| 477 | // Compare loop depths. |
| 478 | if (LI->getLoopDepth(LV->getParent()) != |
| 479 | LI->getLoopDepth(RV->getParent())) |
| 480 | return LI->getLoopDepth(LV->getParent()) < |
| 481 | LI->getLoopDepth(RV->getParent()); |
| 482 | |
| 483 | // Compare opcodes. |
| 484 | if (LV->getOpcode() != RV->getOpcode()) |
| 485 | return LV->getOpcode() < RV->getOpcode(); |
| 486 | |
| 487 | // Compare the number of operands. |
| 488 | if (LV->getNumOperands() != RV->getNumOperands()) |
| 489 | return LV->getNumOperands() < RV->getNumOperands(); |
| 490 | } |
| 491 | |
| 492 | return false; |
| 493 | } |
| 494 | |
Dan Gohman | 56fc8f1 | 2009-06-14 22:51:25 +0000 | [diff] [blame] | 495 | // Compare constant values. |
| 496 | if (const SCEVConstant *LC = dyn_cast<SCEVConstant>(LHS)) { |
| 497 | const SCEVConstant *RC = cast<SCEVConstant>(RHS); |
| 498 | return LC->getValue()->getValue().ult(RC->getValue()->getValue()); |
| 499 | } |
| 500 | |
| 501 | // Compare addrec loop depths. |
| 502 | if (const SCEVAddRecExpr *LA = dyn_cast<SCEVAddRecExpr>(LHS)) { |
| 503 | const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS); |
| 504 | if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth()) |
| 505 | return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth(); |
| 506 | } |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 507 | |
| 508 | // Lexicographically compare n-ary expressions. |
| 509 | if (const SCEVNAryExpr *LC = dyn_cast<SCEVNAryExpr>(LHS)) { |
| 510 | const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS); |
| 511 | for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) { |
| 512 | if (i >= RC->getNumOperands()) |
| 513 | return false; |
| 514 | if (operator()(LC->getOperand(i), RC->getOperand(i))) |
| 515 | return true; |
| 516 | if (operator()(RC->getOperand(i), LC->getOperand(i))) |
| 517 | return false; |
| 518 | } |
| 519 | return LC->getNumOperands() < RC->getNumOperands(); |
| 520 | } |
| 521 | |
Dan Gohman | 6e10db1 | 2009-05-07 19:23:21 +0000 | [diff] [blame] | 522 | // Lexicographically compare udiv expressions. |
| 523 | if (const SCEVUDivExpr *LC = dyn_cast<SCEVUDivExpr>(LHS)) { |
| 524 | const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS); |
| 525 | if (operator()(LC->getLHS(), RC->getLHS())) |
| 526 | return true; |
| 527 | if (operator()(RC->getLHS(), LC->getLHS())) |
| 528 | return false; |
| 529 | if (operator()(LC->getRHS(), RC->getRHS())) |
| 530 | return true; |
| 531 | if (operator()(RC->getRHS(), LC->getRHS())) |
| 532 | return false; |
| 533 | return false; |
| 534 | } |
| 535 | |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 536 | // Compare cast expressions by operand. |
| 537 | if (const SCEVCastExpr *LC = dyn_cast<SCEVCastExpr>(LHS)) { |
| 538 | const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS); |
| 539 | return operator()(LC->getOperand(), RC->getOperand()); |
| 540 | } |
| 541 | |
| 542 | assert(0 && "Unknown SCEV kind!"); |
| 543 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 544 | } |
| 545 | }; |
| 546 | } |
| 547 | |
| 548 | /// GroupByComplexity - Given a list of SCEV objects, order them by their |
| 549 | /// complexity, and group objects of the same complexity together by value. |
| 550 | /// When this routine is finished, we know that any duplicates in the vector are |
| 551 | /// consecutive and that complexity is monotonically increasing. |
| 552 | /// |
| 553 | /// Note that we go take special precautions to ensure that we get determinstic |
| 554 | /// results from this routine. In other words, we don't want the results of |
| 555 | /// this to depend on where the addresses of various SCEV objects happened to |
| 556 | /// land in memory. |
| 557 | /// |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 558 | static void GroupByComplexity(SmallVectorImpl<const SCEV*> &Ops, |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 559 | LoopInfo *LI) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 560 | if (Ops.size() < 2) return; // Noop |
| 561 | if (Ops.size() == 2) { |
| 562 | // This is the common case, which also happens to be trivially simple. |
| 563 | // Special case it. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 564 | if (SCEVComplexityCompare(LI)(Ops[1], Ops[0])) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 565 | std::swap(Ops[0], Ops[1]); |
| 566 | return; |
| 567 | } |
| 568 | |
| 569 | // Do the rough sort by complexity. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 570 | std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 571 | |
| 572 | // Now that we are sorted by complexity, group elements of the same |
| 573 | // complexity. Note that this is, at worst, N^2, but the vector is likely to |
| 574 | // be extremely short in practice. Note that we take this approach because we |
| 575 | // do not want to depend on the addresses of the objects we are grouping. |
| 576 | for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 577 | const SCEV *S = Ops[i]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 578 | unsigned Complexity = S->getSCEVType(); |
| 579 | |
| 580 | // If there are any objects of the same complexity and same value as this |
| 581 | // one, group them. |
| 582 | for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { |
| 583 | if (Ops[j] == S) { // Found a duplicate. |
| 584 | // Move it to immediately after i'th element. |
| 585 | std::swap(Ops[i+1], Ops[j]); |
| 586 | ++i; // no need to rescan it. |
| 587 | if (i == e-2) return; // Done! |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | |
| 594 | |
| 595 | //===----------------------------------------------------------------------===// |
| 596 | // Simple SCEV method implementations |
| 597 | //===----------------------------------------------------------------------===// |
| 598 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 599 | /// BinomialCoefficient - Compute BC(It, K). The result has width W. |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 600 | /// Assume, K > 0. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 601 | static const SCEV* BinomialCoefficient(const SCEV* It, unsigned K, |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 602 | ScalarEvolution &SE, |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 603 | const Type* ResultTy) { |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 604 | // Handle the simplest case efficiently. |
| 605 | if (K == 1) |
| 606 | return SE.getTruncateOrZeroExtend(It, ResultTy); |
| 607 | |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 608 | // We are using the following formula for BC(It, K): |
| 609 | // |
| 610 | // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! |
| 611 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 612 | // Suppose, W is the bitwidth of the return value. We must be prepared for |
| 613 | // overflow. Hence, we must assure that the result of our computation is |
| 614 | // equal to the accurate one modulo 2^W. Unfortunately, division isn't |
| 615 | // safe in modular arithmetic. |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 616 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 617 | // However, this code doesn't use exactly that formula; the formula it uses |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 618 | // is something like the following, where T is the number of factors of 2 in |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 619 | // K! (i.e. trailing zeros in the binary representation of K!), and ^ is |
| 620 | // exponentiation: |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 621 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 622 | // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T) |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 623 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 624 | // This formula is trivially equivalent to the previous formula. However, |
| 625 | // this formula can be implemented much more efficiently. The trick is that |
| 626 | // K! / 2^T is odd, and exact division by an odd number *is* safe in modular |
| 627 | // arithmetic. To do exact division in modular arithmetic, all we have |
| 628 | // to do is multiply by the inverse. Therefore, this step can be done at |
| 629 | // width W. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 630 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 631 | // The next issue is how to safely do the division by 2^T. The way this |
| 632 | // is done is by doing the multiplication step at a width of at least W + T |
| 633 | // bits. This way, the bottom W+T bits of the product are accurate. Then, |
| 634 | // when we perform the division by 2^T (which is equivalent to a right shift |
| 635 | // by T), the bottom W bits are accurate. Extra bits are okay; they'll get |
| 636 | // truncated out after the division by 2^T. |
| 637 | // |
| 638 | // In comparison to just directly using the first formula, this technique |
| 639 | // is much more efficient; using the first formula requires W * K bits, |
| 640 | // but this formula less than W + K bits. Also, the first formula requires |
| 641 | // a division step, whereas this formula only requires multiplies and shifts. |
| 642 | // |
| 643 | // It doesn't matter whether the subtraction step is done in the calculation |
| 644 | // width or the input iteration count's width; if the subtraction overflows, |
| 645 | // the result must be zero anyway. We prefer here to do it in the width of |
| 646 | // the induction variable because it helps a lot for certain cases; CodeGen |
| 647 | // isn't smart enough to ignore the overflow, which leads to much less |
| 648 | // efficient code if the width of the subtraction is wider than the native |
| 649 | // register width. |
| 650 | // |
| 651 | // (It's possible to not widen at all by pulling out factors of 2 before |
| 652 | // the multiplication; for example, K=2 can be calculated as |
| 653 | // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires |
| 654 | // extra arithmetic, so it's not an obvious win, and it gets |
| 655 | // much more complicated for K > 3.) |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 656 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 657 | // Protection from insane SCEVs; this bound is conservative, |
| 658 | // but it probably doesn't matter. |
| 659 | if (K > 1000) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 660 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 661 | |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 662 | unsigned W = SE.getTypeSizeInBits(ResultTy); |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 663 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 664 | // Calculate K! / 2^T and T; we divide out the factors of two before |
| 665 | // multiplying for calculating K! / 2^T to avoid overflow. |
| 666 | // Other overflow doesn't matter because we only care about the bottom |
| 667 | // W bits of the result. |
| 668 | APInt OddFactorial(W, 1); |
| 669 | unsigned T = 1; |
| 670 | for (unsigned i = 3; i <= K; ++i) { |
| 671 | APInt Mult(W, i); |
| 672 | unsigned TwoFactors = Mult.countTrailingZeros(); |
| 673 | T += TwoFactors; |
| 674 | Mult = Mult.lshr(TwoFactors); |
| 675 | OddFactorial *= Mult; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 676 | } |
Nick Lewycky | dbaa60a | 2008-06-13 04:38:55 +0000 | [diff] [blame] | 677 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 678 | // We need at least W + T bits for the multiplication step |
nicholas | 9e3e5fd | 2009-01-25 08:16:27 +0000 | [diff] [blame] | 679 | unsigned CalculationBits = W + T; |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 680 | |
| 681 | // Calcuate 2^T, at width T+W. |
| 682 | APInt DivFactor = APInt(CalculationBits, 1).shl(T); |
| 683 | |
| 684 | // Calculate the multiplicative inverse of K! / 2^T; |
| 685 | // this multiplication factor will perform the exact division by |
| 686 | // K! / 2^T. |
| 687 | APInt Mod = APInt::getSignedMinValue(W+1); |
| 688 | APInt MultiplyFactor = OddFactorial.zext(W+1); |
| 689 | MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); |
| 690 | MultiplyFactor = MultiplyFactor.trunc(W); |
| 691 | |
| 692 | // Calculate the product, at width T+W |
| 693 | const IntegerType *CalculationTy = IntegerType::get(CalculationBits); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 694 | const SCEV* Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 695 | for (unsigned i = 1; i != K; ++i) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 696 | const SCEV* S = SE.getMinusSCEV(It, SE.getIntegerSCEV(i, It->getType())); |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 697 | Dividend = SE.getMulExpr(Dividend, |
| 698 | SE.getTruncateOrZeroExtend(S, CalculationTy)); |
| 699 | } |
| 700 | |
| 701 | // Divide by 2^T |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 702 | const SCEV* DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 703 | |
| 704 | // Truncate the result, and divide by K! / 2^T. |
| 705 | |
| 706 | return SE.getMulExpr(SE.getConstant(MultiplyFactor), |
| 707 | SE.getTruncateOrZeroExtend(DivResult, ResultTy)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 710 | /// evaluateAtIteration - Return the value of this chain of recurrences at |
| 711 | /// the specified iteration number. We can evaluate this recurrence by |
| 712 | /// multiplying each element in the chain by the binomial coefficient |
| 713 | /// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as: |
| 714 | /// |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 715 | /// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 716 | /// |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 717 | /// where BC(It, k) stands for binomial coefficient. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 718 | /// |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 719 | const SCEV* SCEVAddRecExpr::evaluateAtIteration(const SCEV* It, |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 720 | ScalarEvolution &SE) const { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 721 | const SCEV* Result = getStart(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 722 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 723 | // The computation is correct in the face of overflow provided that the |
| 724 | // multiplication is performed _after_ the evaluation of the binomial |
| 725 | // coefficient. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 726 | const SCEV* Coeff = BinomialCoefficient(It, i, SE, getType()); |
Nick Lewycky | b6218e0 | 2008-10-13 03:58:02 +0000 | [diff] [blame] | 727 | if (isa<SCEVCouldNotCompute>(Coeff)) |
| 728 | return Coeff; |
| 729 | |
| 730 | Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 731 | } |
| 732 | return Result; |
| 733 | } |
| 734 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 735 | //===----------------------------------------------------------------------===// |
| 736 | // SCEV Expression folder implementations |
| 737 | //===----------------------------------------------------------------------===// |
| 738 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 739 | const SCEV* ScalarEvolution::getTruncateExpr(const SCEV* Op, |
Dan Gohman | 9c8abcc | 2009-05-01 16:44:56 +0000 | [diff] [blame] | 740 | const Type *Ty) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 741 | assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 742 | "This is not a truncating conversion!"); |
Dan Gohman | 13a51e2 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 743 | assert(isSCEVable(Ty) && |
| 744 | "This is not a conversion to a SCEVable type!"); |
| 745 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 746 | |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame^] | 747 | // Fold if the operand is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 748 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 749 | return getConstant( |
| 750 | cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 751 | |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 752 | // trunc(trunc(x)) --> trunc(x) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 753 | if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 754 | return getTruncateExpr(ST->getOperand(), Ty); |
| 755 | |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 756 | // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 757 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 758 | return getTruncateOrSignExtend(SS->getOperand(), Ty); |
| 759 | |
| 760 | // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 761 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 762 | return getTruncateOrZeroExtend(SZ->getOperand(), Ty); |
| 763 | |
Dan Gohman | 1c0aa2c | 2009-06-18 16:24:47 +0000 | [diff] [blame] | 764 | // If the input value is a chrec scev, truncate the chrec's operands. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 765 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 766 | SmallVector<const SCEV*, 4> Operands; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 767 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 768 | Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty)); |
| 769 | return getAddRecExpr(Operands, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 772 | FoldingSetNodeID ID; |
| 773 | ID.AddInteger(scTruncate); |
| 774 | ID.AddPointer(Op); |
| 775 | ID.AddPointer(Ty); |
| 776 | void *IP = 0; |
| 777 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 778 | SCEV *S = SCEVAllocator.Allocate<SCEVTruncateExpr>(); |
| 779 | new (S) SCEVTruncateExpr(Op, Ty); |
| 780 | UniqueSCEVs.InsertNode(S, IP); |
| 781 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 784 | const SCEV* ScalarEvolution::getZeroExtendExpr(const SCEV* Op, |
Dan Gohman | 36d4092 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 785 | const Type *Ty) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 786 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | 36d4092 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 787 | "This is not an extending conversion!"); |
Dan Gohman | 13a51e2 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 788 | assert(isSCEVable(Ty) && |
| 789 | "This is not a conversion to a SCEVable type!"); |
| 790 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | 36d4092 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 791 | |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame^] | 792 | // Fold if the operand is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 793 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 794 | const Type *IntTy = getEffectiveSCEVType(Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 795 | Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy); |
| 796 | if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 797 | return getConstant(cast<ConstantInt>(C)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 798 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 799 | |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 800 | // zext(zext(x)) --> zext(x) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 801 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 802 | return getZeroExtendExpr(SZ->getOperand(), Ty); |
| 803 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 804 | // If the input value is a chrec scev, and we can prove that the value |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 805 | // did not overflow the old, smaller, value, we can zero extend all of the |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 806 | // operands (often constants). This allows analysis of something like |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 807 | // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 808 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 809 | if (AR->isAffine()) { |
| 810 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 811 | // Note that this serves two purposes: It filters out loops that are |
| 812 | // simply not analyzable, and it covers the case where this code is |
| 813 | // being called from within backedge-taken count analysis, such that |
| 814 | // attempting to ask for the backedge-taken count would likely result |
| 815 | // in infinite recursion. In the later case, the analysis code will |
| 816 | // cope with a conservative value, and it will take care to purge |
| 817 | // that value once it has finished. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 818 | const SCEV* MaxBECount = getMaxBackedgeTakenCount(AR->getLoop()); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 819 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | 4ada77f | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 820 | // Manually compute the final value for AR, checking for |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 821 | // overflow. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 822 | const SCEV* Start = AR->getStart(); |
| 823 | const SCEV* Step = AR->getStepRecurrence(*this); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 824 | |
| 825 | // Check whether the backedge-taken count can be losslessly casted to |
| 826 | // the addrec's type. The count is always unsigned. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 827 | const SCEV* CastedMaxBECount = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 828 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 829 | const SCEV* RecastedMaxBECount = |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 830 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 831 | if (MaxBECount == RecastedMaxBECount) { |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 832 | const Type *WideTy = |
| 833 | IntegerType::get(getTypeSizeInBits(Start->getType()) * 2); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 834 | // Check whether Start+Step*MaxBECount has no unsigned overflow. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 835 | const SCEV* ZMul = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 836 | getMulExpr(CastedMaxBECount, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 837 | getTruncateOrZeroExtend(Step, Start->getType())); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 838 | const SCEV* Add = getAddExpr(Start, ZMul); |
| 839 | const SCEV* OperandExtendedAdd = |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 840 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 841 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 842 | getZeroExtendExpr(Step, WideTy))); |
| 843 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 844 | // Return the expression with the addrec on the outside. |
| 845 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 846 | getZeroExtendExpr(Step, Ty), |
| 847 | AR->getLoop()); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 848 | |
| 849 | // Similar to above, only this time treat the step value as signed. |
| 850 | // This covers loops that count down. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 851 | const SCEV* SMul = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 852 | getMulExpr(CastedMaxBECount, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 853 | getTruncateOrSignExtend(Step, Start->getType())); |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 854 | Add = getAddExpr(Start, SMul); |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 855 | OperandExtendedAdd = |
| 856 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 857 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 858 | getSignExtendExpr(Step, WideTy))); |
| 859 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 860 | // Return the expression with the addrec on the outside. |
| 861 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 862 | getSignExtendExpr(Step, Ty), |
| 863 | AR->getLoop()); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 867 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 868 | FoldingSetNodeID ID; |
| 869 | ID.AddInteger(scZeroExtend); |
| 870 | ID.AddPointer(Op); |
| 871 | ID.AddPointer(Ty); |
| 872 | void *IP = 0; |
| 873 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 874 | SCEV *S = SCEVAllocator.Allocate<SCEVZeroExtendExpr>(); |
| 875 | new (S) SCEVZeroExtendExpr(Op, Ty); |
| 876 | UniqueSCEVs.InsertNode(S, IP); |
| 877 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 880 | const SCEV* ScalarEvolution::getSignExtendExpr(const SCEV* Op, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 881 | const Type *Ty) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 882 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 883 | "This is not an extending conversion!"); |
Dan Gohman | 13a51e2 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 884 | assert(isSCEVable(Ty) && |
| 885 | "This is not a conversion to a SCEVable type!"); |
| 886 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 887 | |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame^] | 888 | // Fold if the operand is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 889 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 890 | const Type *IntTy = getEffectiveSCEVType(Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 891 | Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy); |
| 892 | if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 893 | return getConstant(cast<ConstantInt>(C)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 894 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 895 | |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 896 | // sext(sext(x)) --> sext(x) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 897 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 898 | return getSignExtendExpr(SS->getOperand(), Ty); |
| 899 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 900 | // If the input value is a chrec scev, and we can prove that the value |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 901 | // did not overflow the old, smaller, value, we can sign extend all of the |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 902 | // operands (often constants). This allows analysis of something like |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 903 | // this: for (signed char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 904 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 905 | if (AR->isAffine()) { |
| 906 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 907 | // Note that this serves two purposes: It filters out loops that are |
| 908 | // simply not analyzable, and it covers the case where this code is |
| 909 | // being called from within backedge-taken count analysis, such that |
| 910 | // attempting to ask for the backedge-taken count would likely result |
| 911 | // in infinite recursion. In the later case, the analysis code will |
| 912 | // cope with a conservative value, and it will take care to purge |
| 913 | // that value once it has finished. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 914 | const SCEV* MaxBECount = getMaxBackedgeTakenCount(AR->getLoop()); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 915 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | 4ada77f | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 916 | // Manually compute the final value for AR, checking for |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 917 | // overflow. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 918 | const SCEV* Start = AR->getStart(); |
| 919 | const SCEV* Step = AR->getStepRecurrence(*this); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 920 | |
| 921 | // Check whether the backedge-taken count can be losslessly casted to |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 922 | // the addrec's type. The count is always unsigned. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 923 | const SCEV* CastedMaxBECount = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 924 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 925 | const SCEV* RecastedMaxBECount = |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 926 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 927 | if (MaxBECount == RecastedMaxBECount) { |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 928 | const Type *WideTy = |
| 929 | IntegerType::get(getTypeSizeInBits(Start->getType()) * 2); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 930 | // Check whether Start+Step*MaxBECount has no signed overflow. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 931 | const SCEV* SMul = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 932 | getMulExpr(CastedMaxBECount, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 933 | getTruncateOrSignExtend(Step, Start->getType())); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 934 | const SCEV* Add = getAddExpr(Start, SMul); |
| 935 | const SCEV* OperandExtendedAdd = |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 936 | getAddExpr(getSignExtendExpr(Start, WideTy), |
| 937 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 938 | getSignExtendExpr(Step, WideTy))); |
| 939 | if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 940 | // Return the expression with the addrec on the outside. |
| 941 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 942 | getSignExtendExpr(Step, Ty), |
| 943 | AR->getLoop()); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 947 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 948 | FoldingSetNodeID ID; |
| 949 | ID.AddInteger(scSignExtend); |
| 950 | ID.AddPointer(Op); |
| 951 | ID.AddPointer(Ty); |
| 952 | void *IP = 0; |
| 953 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 954 | SCEV *S = SCEVAllocator.Allocate<SCEVSignExtendExpr>(); |
| 955 | new (S) SCEVSignExtendExpr(Op, Ty); |
| 956 | UniqueSCEVs.InsertNode(S, IP); |
| 957 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 960 | /// getAnyExtendExpr - Return a SCEV for the given operand extended with |
| 961 | /// unspecified bits out to the given type. |
| 962 | /// |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 963 | const SCEV* ScalarEvolution::getAnyExtendExpr(const SCEV* Op, |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 964 | const Type *Ty) { |
| 965 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
| 966 | "This is not an extending conversion!"); |
| 967 | assert(isSCEVable(Ty) && |
| 968 | "This is not a conversion to a SCEVable type!"); |
| 969 | Ty = getEffectiveSCEVType(Ty); |
| 970 | |
| 971 | // Sign-extend negative constants. |
| 972 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| 973 | if (SC->getValue()->getValue().isNegative()) |
| 974 | return getSignExtendExpr(Op, Ty); |
| 975 | |
| 976 | // Peel off a truncate cast. |
| 977 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 978 | const SCEV* NewOp = T->getOperand(); |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 979 | if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty)) |
| 980 | return getAnyExtendExpr(NewOp, Ty); |
| 981 | return getTruncateOrNoop(NewOp, Ty); |
| 982 | } |
| 983 | |
| 984 | // Next try a zext cast. If the cast is folded, use it. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 985 | const SCEV* ZExt = getZeroExtendExpr(Op, Ty); |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 986 | if (!isa<SCEVZeroExtendExpr>(ZExt)) |
| 987 | return ZExt; |
| 988 | |
| 989 | // Next try a sext cast. If the cast is folded, use it. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 990 | const SCEV* SExt = getSignExtendExpr(Op, Ty); |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 991 | if (!isa<SCEVSignExtendExpr>(SExt)) |
| 992 | return SExt; |
| 993 | |
| 994 | // If the expression is obviously signed, use the sext cast value. |
| 995 | if (isa<SCEVSMaxExpr>(Op)) |
| 996 | return SExt; |
| 997 | |
| 998 | // Absent any other information, use the zext cast value. |
| 999 | return ZExt; |
| 1000 | } |
| 1001 | |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1002 | /// CollectAddOperandsWithScales - Process the given Ops list, which is |
| 1003 | /// a list of operands to be added under the given scale, update the given |
| 1004 | /// map. This is a helper function for getAddRecExpr. As an example of |
| 1005 | /// what it does, given a sequence of operands that would form an add |
| 1006 | /// expression like this: |
| 1007 | /// |
| 1008 | /// m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r) |
| 1009 | /// |
| 1010 | /// where A and B are constants, update the map with these values: |
| 1011 | /// |
| 1012 | /// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0) |
| 1013 | /// |
| 1014 | /// and add 13 + A*B*29 to AccumulatedConstant. |
| 1015 | /// This will allow getAddRecExpr to produce this: |
| 1016 | /// |
| 1017 | /// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B) |
| 1018 | /// |
| 1019 | /// This form often exposes folding opportunities that are hidden in |
| 1020 | /// the original operand list. |
| 1021 | /// |
| 1022 | /// Return true iff it appears that any interesting folding opportunities |
| 1023 | /// may be exposed. This helps getAddRecExpr short-circuit extra work in |
| 1024 | /// the common case where no interesting opportunities are present, and |
| 1025 | /// is also used as a check to avoid infinite recursion. |
| 1026 | /// |
| 1027 | static bool |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1028 | CollectAddOperandsWithScales(DenseMap<const SCEV*, APInt> &M, |
| 1029 | SmallVector<const SCEV*, 8> &NewOps, |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1030 | APInt &AccumulatedConstant, |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1031 | const SmallVectorImpl<const SCEV*> &Ops, |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1032 | const APInt &Scale, |
| 1033 | ScalarEvolution &SE) { |
| 1034 | bool Interesting = false; |
| 1035 | |
| 1036 | // Iterate over the add operands. |
| 1037 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 1038 | const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]); |
| 1039 | if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) { |
| 1040 | APInt NewScale = |
| 1041 | Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue(); |
| 1042 | if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) { |
| 1043 | // A multiplication of a constant with another add; recurse. |
| 1044 | Interesting |= |
| 1045 | CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
| 1046 | cast<SCEVAddExpr>(Mul->getOperand(1)) |
| 1047 | ->getOperands(), |
| 1048 | NewScale, SE); |
| 1049 | } else { |
| 1050 | // A multiplication of a constant with some other value. Update |
| 1051 | // the map. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1052 | SmallVector<const SCEV*, 4> MulOps(Mul->op_begin()+1, Mul->op_end()); |
| 1053 | const SCEV* Key = SE.getMulExpr(MulOps); |
| 1054 | std::pair<DenseMap<const SCEV*, APInt>::iterator, bool> Pair = |
Dan Gohman | 3bf01f0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1055 | M.insert(std::make_pair(Key, NewScale)); |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1056 | if (Pair.second) { |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1057 | NewOps.push_back(Pair.first->first); |
| 1058 | } else { |
| 1059 | Pair.first->second += NewScale; |
| 1060 | // The map already had an entry for this value, which may indicate |
| 1061 | // a folding opportunity. |
| 1062 | Interesting = true; |
| 1063 | } |
| 1064 | } |
| 1065 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| 1066 | // Pull a buried constant out to the outside. |
| 1067 | if (Scale != 1 || AccumulatedConstant != 0 || C->isZero()) |
| 1068 | Interesting = true; |
| 1069 | AccumulatedConstant += Scale * C->getValue()->getValue(); |
| 1070 | } else { |
| 1071 | // An ordinary operand. Update the map. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1072 | std::pair<DenseMap<const SCEV*, APInt>::iterator, bool> Pair = |
Dan Gohman | 3bf01f0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1073 | M.insert(std::make_pair(Ops[i], Scale)); |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1074 | if (Pair.second) { |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1075 | NewOps.push_back(Pair.first->first); |
| 1076 | } else { |
| 1077 | Pair.first->second += Scale; |
| 1078 | // The map already had an entry for this value, which may indicate |
| 1079 | // a folding opportunity. |
| 1080 | Interesting = true; |
| 1081 | } |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | return Interesting; |
| 1086 | } |
| 1087 | |
| 1088 | namespace { |
| 1089 | struct APIntCompare { |
| 1090 | bool operator()(const APInt &LHS, const APInt &RHS) const { |
| 1091 | return LHS.ult(RHS); |
| 1092 | } |
| 1093 | }; |
| 1094 | } |
| 1095 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1096 | /// getAddExpr - Get a canonical add expression, or something simpler if |
| 1097 | /// possible. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1098 | const SCEV* ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV*> &Ops) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1099 | assert(!Ops.empty() && "Cannot get empty add!"); |
| 1100 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1101 | #ifndef NDEBUG |
| 1102 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1103 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1104 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1105 | "SCEVAddExpr operand types don't match!"); |
| 1106 | #endif |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1107 | |
| 1108 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1109 | GroupByComplexity(Ops, LI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1110 | |
| 1111 | // If there are any constants, fold them together. |
| 1112 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1113 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1114 | ++Idx; |
| 1115 | assert(Idx < Ops.size()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1116 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1117 | // We found two constants, fold them together! |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1118 | Ops[0] = getConstant(LHSC->getValue()->getValue() + |
| 1119 | RHSC->getValue()->getValue()); |
Dan Gohman | 68f23e8 | 2009-06-14 22:53:57 +0000 | [diff] [blame] | 1120 | if (Ops.size() == 2) return Ops[0]; |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1121 | Ops.erase(Ops.begin()+1); // Erase the folded element |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1122 | LHSC = cast<SCEVConstant>(Ops[0]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | // If we are left with a constant zero being added, strip it off. |
| 1126 | if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
| 1127 | Ops.erase(Ops.begin()); |
| 1128 | --Idx; |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | if (Ops.size() == 1) return Ops[0]; |
| 1133 | |
| 1134 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1135 | // so, merge them together into an multiply expression. Since we sorted the |
| 1136 | // list, these values are required to be adjacent. |
| 1137 | const Type *Ty = Ops[0]->getType(); |
| 1138 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1139 | if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2 |
| 1140 | // Found a match, merge the two values into a multiply, and add any |
| 1141 | // remaining values to the result. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1142 | const SCEV* Two = getIntegerSCEV(2, Ty); |
| 1143 | const SCEV* Mul = getMulExpr(Ops[i], Two); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1144 | if (Ops.size() == 2) |
| 1145 | return Mul; |
| 1146 | Ops.erase(Ops.begin()+i, Ops.begin()+i+2); |
| 1147 | Ops.push_back(Mul); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1148 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1151 | // Check for truncates. If all the operands are truncated from the same |
| 1152 | // type, see if factoring out the truncate would permit the result to be |
| 1153 | // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n) |
| 1154 | // if the contents of the resulting outer trunc fold to something simple. |
| 1155 | for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) { |
| 1156 | const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]); |
| 1157 | const Type *DstType = Trunc->getType(); |
| 1158 | const Type *SrcType = Trunc->getOperand()->getType(); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1159 | SmallVector<const SCEV*, 8> LargeOps; |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1160 | bool Ok = true; |
| 1161 | // Check all the operands to see if they can be represented in the |
| 1162 | // source type of the truncate. |
| 1163 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 1164 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) { |
| 1165 | if (T->getOperand()->getType() != SrcType) { |
| 1166 | Ok = false; |
| 1167 | break; |
| 1168 | } |
| 1169 | LargeOps.push_back(T->getOperand()); |
| 1170 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| 1171 | // This could be either sign or zero extension, but sign extension |
| 1172 | // is much more likely to be foldable here. |
| 1173 | LargeOps.push_back(getSignExtendExpr(C, SrcType)); |
| 1174 | } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1175 | SmallVector<const SCEV*, 8> LargeMulOps; |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1176 | for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { |
| 1177 | if (const SCEVTruncateExpr *T = |
| 1178 | dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) { |
| 1179 | if (T->getOperand()->getType() != SrcType) { |
| 1180 | Ok = false; |
| 1181 | break; |
| 1182 | } |
| 1183 | LargeMulOps.push_back(T->getOperand()); |
| 1184 | } else if (const SCEVConstant *C = |
| 1185 | dyn_cast<SCEVConstant>(M->getOperand(j))) { |
| 1186 | // This could be either sign or zero extension, but sign extension |
| 1187 | // is much more likely to be foldable here. |
| 1188 | LargeMulOps.push_back(getSignExtendExpr(C, SrcType)); |
| 1189 | } else { |
| 1190 | Ok = false; |
| 1191 | break; |
| 1192 | } |
| 1193 | } |
| 1194 | if (Ok) |
| 1195 | LargeOps.push_back(getMulExpr(LargeMulOps)); |
| 1196 | } else { |
| 1197 | Ok = false; |
| 1198 | break; |
| 1199 | } |
| 1200 | } |
| 1201 | if (Ok) { |
| 1202 | // Evaluate the expression in the larger type. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1203 | const SCEV* Fold = getAddExpr(LargeOps); |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1204 | // If it folds to something simple, use it. Otherwise, don't. |
| 1205 | if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold)) |
| 1206 | return getTruncateExpr(Fold, DstType); |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | // Skip past any other cast SCEVs. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1211 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) |
| 1212 | ++Idx; |
| 1213 | |
| 1214 | // If there are add operands they would be next. |
| 1215 | if (Idx < Ops.size()) { |
| 1216 | bool DeletedAdd = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1217 | while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1218 | // If we have an add, expand the add operands onto the end of the operands |
| 1219 | // list. |
| 1220 | Ops.insert(Ops.end(), Add->op_begin(), Add->op_end()); |
| 1221 | Ops.erase(Ops.begin()+Idx); |
| 1222 | DeletedAdd = true; |
| 1223 | } |
| 1224 | |
| 1225 | // If we deleted at least one add, we added operands to the end of the list, |
| 1226 | // and they are not necessarily sorted. Recurse to resort and resimplify |
| 1227 | // any operands we just aquired. |
| 1228 | if (DeletedAdd) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1229 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | // Skip over the add expression until we get to a multiply. |
| 1233 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1234 | ++Idx; |
| 1235 | |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1236 | // Check to see if there are any folding opportunities present with |
| 1237 | // operands multiplied by constant values. |
| 1238 | if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) { |
| 1239 | uint64_t BitWidth = getTypeSizeInBits(Ty); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1240 | DenseMap<const SCEV*, APInt> M; |
| 1241 | SmallVector<const SCEV*, 8> NewOps; |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1242 | APInt AccumulatedConstant(BitWidth, 0); |
| 1243 | if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
| 1244 | Ops, APInt(BitWidth, 1), *this)) { |
| 1245 | // Some interesting folding opportunity is present, so its worthwhile to |
| 1246 | // re-generate the operands list. Group the operands by constant scale, |
| 1247 | // to avoid multiplying by the same constant scale multiple times. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1248 | std::map<APInt, SmallVector<const SCEV*, 4>, APIntCompare> MulOpLists; |
| 1249 | for (SmallVector<const SCEV*, 8>::iterator I = NewOps.begin(), |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1250 | E = NewOps.end(); I != E; ++I) |
| 1251 | MulOpLists[M.find(*I)->second].push_back(*I); |
| 1252 | // Re-generate the operands list. |
| 1253 | Ops.clear(); |
| 1254 | if (AccumulatedConstant != 0) |
| 1255 | Ops.push_back(getConstant(AccumulatedConstant)); |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1256 | for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator |
| 1257 | I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I) |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1258 | if (I->first != 0) |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1259 | Ops.push_back(getMulExpr(getConstant(I->first), |
| 1260 | getAddExpr(I->second))); |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1261 | if (Ops.empty()) |
| 1262 | return getIntegerSCEV(0, Ty); |
| 1263 | if (Ops.size() == 1) |
| 1264 | return Ops[0]; |
| 1265 | return getAddExpr(Ops); |
| 1266 | } |
| 1267 | } |
| 1268 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1269 | // If we are adding something to a multiply expression, make sure the |
| 1270 | // something is not already an operand of the multiply. If so, merge it into |
| 1271 | // the multiply. |
| 1272 | for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1273 | const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1274 | for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1275 | const SCEV *MulOpSCEV = Mul->getOperand(MulOp); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1276 | for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1277 | if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(Ops[AddOp])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1278 | // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1279 | const SCEV* InnerMul = Mul->getOperand(MulOp == 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1280 | if (Mul->getNumOperands() != 2) { |
| 1281 | // If the multiply has more than two operands, we must get the |
| 1282 | // Y*Z term. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1283 | SmallVector<const SCEV*, 4> MulOps(Mul->op_begin(), Mul->op_end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1284 | MulOps.erase(MulOps.begin()+MulOp); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1285 | InnerMul = getMulExpr(MulOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1286 | } |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1287 | const SCEV* One = getIntegerSCEV(1, Ty); |
| 1288 | const SCEV* AddOne = getAddExpr(InnerMul, One); |
| 1289 | const SCEV* OuterMul = getMulExpr(AddOne, Ops[AddOp]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1290 | if (Ops.size() == 2) return OuterMul; |
| 1291 | if (AddOp < Idx) { |
| 1292 | Ops.erase(Ops.begin()+AddOp); |
| 1293 | Ops.erase(Ops.begin()+Idx-1); |
| 1294 | } else { |
| 1295 | Ops.erase(Ops.begin()+Idx); |
| 1296 | Ops.erase(Ops.begin()+AddOp-1); |
| 1297 | } |
| 1298 | Ops.push_back(OuterMul); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1299 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | // Check this multiply against other multiplies being added together. |
| 1303 | for (unsigned OtherMulIdx = Idx+1; |
| 1304 | OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]); |
| 1305 | ++OtherMulIdx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1306 | const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1307 | // If MulOp occurs in OtherMul, we can fold the two multiplies |
| 1308 | // together. |
| 1309 | for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); |
| 1310 | OMulOp != e; ++OMulOp) |
| 1311 | if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { |
| 1312 | // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E)) |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1313 | const SCEV* InnerMul1 = Mul->getOperand(MulOp == 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1314 | if (Mul->getNumOperands() != 2) { |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1315 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), |
| 1316 | Mul->op_end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1317 | MulOps.erase(MulOps.begin()+MulOp); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1318 | InnerMul1 = getMulExpr(MulOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1319 | } |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1320 | const SCEV* InnerMul2 = OtherMul->getOperand(OMulOp == 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1321 | if (OtherMul->getNumOperands() != 2) { |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1322 | SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(), |
| 1323 | OtherMul->op_end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1324 | MulOps.erase(MulOps.begin()+OMulOp); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1325 | InnerMul2 = getMulExpr(MulOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1326 | } |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1327 | const SCEV* InnerMulSum = getAddExpr(InnerMul1,InnerMul2); |
| 1328 | const SCEV* OuterMul = getMulExpr(MulOpSCEV, InnerMulSum); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1329 | if (Ops.size() == 2) return OuterMul; |
| 1330 | Ops.erase(Ops.begin()+Idx); |
| 1331 | Ops.erase(Ops.begin()+OtherMulIdx-1); |
| 1332 | Ops.push_back(OuterMul); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1333 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1334 | } |
| 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | // If there are any add recurrences in the operands list, see if any other |
| 1340 | // added values are loop invariant. If so, we can fold them into the |
| 1341 | // recurrence. |
| 1342 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1343 | ++Idx; |
| 1344 | |
| 1345 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1346 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1347 | // Scan all of the other operands to this add and add them to the vector if |
| 1348 | // they are loop invariant w.r.t. the recurrence. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1349 | SmallVector<const SCEV*, 8> LIOps; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1350 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1351 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1352 | if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { |
| 1353 | LIOps.push_back(Ops[i]); |
| 1354 | Ops.erase(Ops.begin()+i); |
| 1355 | --i; --e; |
| 1356 | } |
| 1357 | |
| 1358 | // If we found some loop invariants, fold them into the recurrence. |
| 1359 | if (!LIOps.empty()) { |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1360 | // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1361 | LIOps.push_back(AddRec->getStart()); |
| 1362 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1363 | SmallVector<const SCEV*, 4> AddRecOps(AddRec->op_begin(), |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1364 | AddRec->op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1365 | AddRecOps[0] = getAddExpr(LIOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1366 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1367 | const SCEV* NewRec = getAddRecExpr(AddRecOps, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1368 | // If all of the other operands were loop invariant, we are done. |
| 1369 | if (Ops.size() == 1) return NewRec; |
| 1370 | |
| 1371 | // Otherwise, add the folded AddRec by the non-liv parts. |
| 1372 | for (unsigned i = 0;; ++i) |
| 1373 | if (Ops[i] == AddRec) { |
| 1374 | Ops[i] = NewRec; |
| 1375 | break; |
| 1376 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1377 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1381 | // there are multiple AddRec's with the same loop induction variable being |
| 1382 | // added together. If so, we can fold them. |
| 1383 | for (unsigned OtherIdx = Idx+1; |
| 1384 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) |
| 1385 | if (OtherIdx != Idx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1386 | const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1387 | if (AddRec->getLoop() == OtherAddRec->getLoop()) { |
| 1388 | // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D} |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1389 | SmallVector<const SCEV *, 4> NewOps(AddRec->op_begin(), |
| 1390 | AddRec->op_end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1391 | for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) { |
| 1392 | if (i >= NewOps.size()) { |
| 1393 | NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i, |
| 1394 | OtherAddRec->op_end()); |
| 1395 | break; |
| 1396 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1397 | NewOps[i] = getAddExpr(NewOps[i], OtherAddRec->getOperand(i)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1398 | } |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1399 | const SCEV* NewAddRec = getAddRecExpr(NewOps, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1400 | |
| 1401 | if (Ops.size() == 2) return NewAddRec; |
| 1402 | |
| 1403 | Ops.erase(Ops.begin()+Idx); |
| 1404 | Ops.erase(Ops.begin()+OtherIdx-1); |
| 1405 | Ops.push_back(NewAddRec); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1406 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1411 | // next one. |
| 1412 | } |
| 1413 | |
| 1414 | // Okay, it looks like we really DO need an add expr. Check to see if we |
| 1415 | // already have one, otherwise create a new one. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1416 | FoldingSetNodeID ID; |
| 1417 | ID.AddInteger(scAddExpr); |
| 1418 | ID.AddInteger(Ops.size()); |
| 1419 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1420 | ID.AddPointer(Ops[i]); |
| 1421 | void *IP = 0; |
| 1422 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1423 | SCEV *S = SCEVAllocator.Allocate<SCEVAddExpr>(); |
| 1424 | new (S) SCEVAddExpr(Ops); |
| 1425 | UniqueSCEVs.InsertNode(S, IP); |
| 1426 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1430 | /// getMulExpr - Get a canonical multiply expression, or something simpler if |
| 1431 | /// possible. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1432 | const SCEV* ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV*> &Ops) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1433 | assert(!Ops.empty() && "Cannot get empty mul!"); |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1434 | #ifndef NDEBUG |
| 1435 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1436 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1437 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1438 | "SCEVMulExpr operand types don't match!"); |
| 1439 | #endif |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1440 | |
| 1441 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1442 | GroupByComplexity(Ops, LI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1443 | |
| 1444 | // If there are any constants, fold them together. |
| 1445 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1446 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1447 | |
| 1448 | // C1*(C2+V) -> C1*C2 + C1*V |
| 1449 | if (Ops.size() == 2) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1450 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1451 | if (Add->getNumOperands() == 2 && |
| 1452 | isa<SCEVConstant>(Add->getOperand(0))) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1453 | return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)), |
| 1454 | getMulExpr(LHSC, Add->getOperand(1))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1455 | |
| 1456 | |
| 1457 | ++Idx; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1458 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1459 | // We found two constants, fold them together! |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1460 | ConstantInt *Fold = ConstantInt::get(LHSC->getValue()->getValue() * |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1461 | RHSC->getValue()->getValue()); |
| 1462 | Ops[0] = getConstant(Fold); |
| 1463 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1464 | if (Ops.size() == 1) return Ops[0]; |
| 1465 | LHSC = cast<SCEVConstant>(Ops[0]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | // If we are left with a constant one being multiplied, strip it off. |
| 1469 | if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) { |
| 1470 | Ops.erase(Ops.begin()); |
| 1471 | --Idx; |
| 1472 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
| 1473 | // If we have a multiply of zero, it will always be zero. |
| 1474 | return Ops[0]; |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | // Skip over the add expression until we get to a multiply. |
| 1479 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1480 | ++Idx; |
| 1481 | |
| 1482 | if (Ops.size() == 1) |
| 1483 | return Ops[0]; |
| 1484 | |
| 1485 | // If there are mul operands inline them all into this expression. |
| 1486 | if (Idx < Ops.size()) { |
| 1487 | bool DeletedMul = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1488 | while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1489 | // If we have an mul, expand the mul operands onto the end of the operands |
| 1490 | // list. |
| 1491 | Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end()); |
| 1492 | Ops.erase(Ops.begin()+Idx); |
| 1493 | DeletedMul = true; |
| 1494 | } |
| 1495 | |
| 1496 | // If we deleted at least one mul, we added operands to the end of the list, |
| 1497 | // and they are not necessarily sorted. Recurse to resort and resimplify |
| 1498 | // any operands we just aquired. |
| 1499 | if (DeletedMul) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1500 | return getMulExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | // If there are any add recurrences in the operands list, see if any other |
| 1504 | // added values are loop invariant. If so, we can fold them into the |
| 1505 | // recurrence. |
| 1506 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1507 | ++Idx; |
| 1508 | |
| 1509 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1510 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1511 | // Scan all of the other operands to this mul and add them to the vector if |
| 1512 | // they are loop invariant w.r.t. the recurrence. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1513 | SmallVector<const SCEV*, 8> LIOps; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1514 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1515 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1516 | if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { |
| 1517 | LIOps.push_back(Ops[i]); |
| 1518 | Ops.erase(Ops.begin()+i); |
| 1519 | --i; --e; |
| 1520 | } |
| 1521 | |
| 1522 | // If we found some loop invariants, fold them into the recurrence. |
| 1523 | if (!LIOps.empty()) { |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1524 | // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step} |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1525 | SmallVector<const SCEV*, 4> NewOps; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1526 | NewOps.reserve(AddRec->getNumOperands()); |
| 1527 | if (LIOps.size() == 1) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1528 | const SCEV *Scale = LIOps[0]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1529 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1530 | NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1531 | } else { |
| 1532 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1533 | SmallVector<const SCEV*, 4> MulOps(LIOps.begin(), LIOps.end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1534 | MulOps.push_back(AddRec->getOperand(i)); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1535 | NewOps.push_back(getMulExpr(MulOps)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1536 | } |
| 1537 | } |
| 1538 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1539 | const SCEV* NewRec = getAddRecExpr(NewOps, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1540 | |
| 1541 | // If all of the other operands were loop invariant, we are done. |
| 1542 | if (Ops.size() == 1) return NewRec; |
| 1543 | |
| 1544 | // Otherwise, multiply the folded AddRec by the non-liv parts. |
| 1545 | for (unsigned i = 0;; ++i) |
| 1546 | if (Ops[i] == AddRec) { |
| 1547 | Ops[i] = NewRec; |
| 1548 | break; |
| 1549 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1550 | return getMulExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1554 | // there are multiple AddRec's with the same loop induction variable being |
| 1555 | // multiplied together. If so, we can fold them. |
| 1556 | for (unsigned OtherIdx = Idx+1; |
| 1557 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) |
| 1558 | if (OtherIdx != Idx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1559 | const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1560 | if (AddRec->getLoop() == OtherAddRec->getLoop()) { |
| 1561 | // F * G --> {A,+,B} * {C,+,D} --> {A*C,+,F*D + G*B + B*D} |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1562 | const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec; |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1563 | const SCEV* NewStart = getMulExpr(F->getStart(), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1564 | G->getStart()); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1565 | const SCEV* B = F->getStepRecurrence(*this); |
| 1566 | const SCEV* D = G->getStepRecurrence(*this); |
| 1567 | const SCEV* NewStep = getAddExpr(getMulExpr(F, D), |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1568 | getMulExpr(G, B), |
| 1569 | getMulExpr(B, D)); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1570 | const SCEV* NewAddRec = getAddRecExpr(NewStart, NewStep, |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1571 | F->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1572 | if (Ops.size() == 2) return NewAddRec; |
| 1573 | |
| 1574 | Ops.erase(Ops.begin()+Idx); |
| 1575 | Ops.erase(Ops.begin()+OtherIdx-1); |
| 1576 | Ops.push_back(NewAddRec); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1577 | return getMulExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1582 | // next one. |
| 1583 | } |
| 1584 | |
| 1585 | // Okay, it looks like we really DO need an mul expr. Check to see if we |
| 1586 | // already have one, otherwise create a new one. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1587 | FoldingSetNodeID ID; |
| 1588 | ID.AddInteger(scMulExpr); |
| 1589 | ID.AddInteger(Ops.size()); |
| 1590 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1591 | ID.AddPointer(Ops[i]); |
| 1592 | void *IP = 0; |
| 1593 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1594 | SCEV *S = SCEVAllocator.Allocate<SCEVMulExpr>(); |
| 1595 | new (S) SCEVMulExpr(Ops); |
| 1596 | UniqueSCEVs.InsertNode(S, IP); |
| 1597 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1600 | /// getUDivExpr - Get a canonical multiply expression, or something simpler if |
| 1601 | /// possible. |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1602 | const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS, |
| 1603 | const SCEV *RHS) { |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1604 | assert(getEffectiveSCEVType(LHS->getType()) == |
| 1605 | getEffectiveSCEVType(RHS->getType()) && |
| 1606 | "SCEVUDivExpr operand types don't match!"); |
| 1607 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1608 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1609 | if (RHSC->getValue()->equalsInt(1)) |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 1610 | return LHS; // X udiv 1 --> x |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1611 | if (RHSC->isZero()) |
| 1612 | return getIntegerSCEV(0, LHS->getType()); // value is undefined |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1613 | |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1614 | // Determine if the division can be folded into the operands of |
| 1615 | // its operands. |
| 1616 | // TODO: Generalize this to non-constants by using known-bits information. |
| 1617 | const Type *Ty = LHS->getType(); |
| 1618 | unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros(); |
| 1619 | unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ; |
| 1620 | // For non-power-of-two values, effectively round the value up to the |
| 1621 | // nearest power of two. |
| 1622 | if (!RHSC->getValue()->getValue().isPowerOf2()) |
| 1623 | ++MaxShiftAmt; |
| 1624 | const IntegerType *ExtTy = |
| 1625 | IntegerType::get(getTypeSizeInBits(Ty) + MaxShiftAmt); |
| 1626 | // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. |
| 1627 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) |
| 1628 | if (const SCEVConstant *Step = |
| 1629 | dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) |
| 1630 | if (!Step->getValue()->getValue() |
| 1631 | .urem(RHSC->getValue()->getValue()) && |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1632 | getZeroExtendExpr(AR, ExtTy) == |
| 1633 | getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), |
| 1634 | getZeroExtendExpr(Step, ExtTy), |
| 1635 | AR->getLoop())) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1636 | SmallVector<const SCEV*, 4> Operands; |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1637 | for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i) |
| 1638 | Operands.push_back(getUDivExpr(AR->getOperand(i), RHS)); |
| 1639 | return getAddRecExpr(Operands, AR->getLoop()); |
| 1640 | } |
| 1641 | // (A*B)/C --> A*(B/C) if safe and B/C can be folded. |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1642 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1643 | SmallVector<const SCEV*, 4> Operands; |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1644 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) |
| 1645 | Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy)); |
| 1646 | if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1647 | // Find an operand that's safely divisible. |
| 1648 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1649 | const SCEV* Op = M->getOperand(i); |
| 1650 | const SCEV* Div = getUDivExpr(Op, RHSC); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1651 | if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1652 | const SmallVectorImpl<const SCEV*> &MOperands = M->getOperands(); |
| 1653 | Operands = SmallVector<const SCEV*, 4>(MOperands.begin(), |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1654 | MOperands.end()); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1655 | Operands[i] = Div; |
| 1656 | return getMulExpr(Operands); |
| 1657 | } |
| 1658 | } |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1659 | } |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1660 | // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded. |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1661 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1662 | SmallVector<const SCEV*, 4> Operands; |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1663 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) |
| 1664 | Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy)); |
| 1665 | if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { |
| 1666 | Operands.clear(); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1667 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1668 | const SCEV* Op = getUDivExpr(A->getOperand(i), RHS); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1669 | if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i)) |
| 1670 | break; |
| 1671 | Operands.push_back(Op); |
| 1672 | } |
| 1673 | if (Operands.size() == A->getNumOperands()) |
| 1674 | return getAddExpr(Operands); |
| 1675 | } |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1676 | } |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1677 | |
| 1678 | // Fold if both operands are constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1679 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1680 | Constant *LHSCV = LHSC->getValue(); |
| 1681 | Constant *RHSCV = RHSC->getValue(); |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 1682 | return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV, |
| 1683 | RHSCV))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1684 | } |
| 1685 | } |
| 1686 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1687 | FoldingSetNodeID ID; |
| 1688 | ID.AddInteger(scUDivExpr); |
| 1689 | ID.AddPointer(LHS); |
| 1690 | ID.AddPointer(RHS); |
| 1691 | void *IP = 0; |
| 1692 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1693 | SCEV *S = SCEVAllocator.Allocate<SCEVUDivExpr>(); |
| 1694 | new (S) SCEVUDivExpr(LHS, RHS); |
| 1695 | UniqueSCEVs.InsertNode(S, IP); |
| 1696 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1700 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 1701 | /// Simplify the expression as much as possible. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1702 | const SCEV* ScalarEvolution::getAddRecExpr(const SCEV* Start, |
| 1703 | const SCEV* Step, const Loop *L) { |
| 1704 | SmallVector<const SCEV*, 4> Operands; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1705 | Operands.push_back(Start); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1706 | if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1707 | if (StepChrec->getLoop() == L) { |
| 1708 | Operands.insert(Operands.end(), StepChrec->op_begin(), |
| 1709 | StepChrec->op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1710 | return getAddRecExpr(Operands, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | Operands.push_back(Step); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1714 | return getAddRecExpr(Operands, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1717 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 1718 | /// Simplify the expression as much as possible. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1719 | const SCEV * |
| 1720 | ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV*> &Operands, |
| 1721 | const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1722 | if (Operands.size() == 1) return Operands[0]; |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1723 | #ifndef NDEBUG |
| 1724 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 1725 | assert(getEffectiveSCEVType(Operands[i]->getType()) == |
| 1726 | getEffectiveSCEVType(Operands[0]->getType()) && |
| 1727 | "SCEVAddRecExpr operand types don't match!"); |
| 1728 | #endif |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1729 | |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1730 | if (Operands.back()->isZero()) { |
| 1731 | Operands.pop_back(); |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1732 | return getAddRecExpr(Operands, L); // {X,+,0} --> X |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1733 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1734 | |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1735 | // Canonicalize nested AddRecs in by nesting them in order of loop depth. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1736 | if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) { |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1737 | const Loop* NestedLoop = NestedAR->getLoop(); |
| 1738 | if (L->getLoopDepth() < NestedLoop->getLoopDepth()) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1739 | SmallVector<const SCEV*, 4> NestedOperands(NestedAR->op_begin(), |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1740 | NestedAR->op_end()); |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1741 | Operands[0] = NestedAR->getStart(); |
Dan Gohman | 08c4c07 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 1742 | // AddRecs require their operands be loop-invariant with respect to their |
| 1743 | // loops. Don't perform this transformation if it would break this |
| 1744 | // requirement. |
| 1745 | bool AllInvariant = true; |
| 1746 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 1747 | if (!Operands[i]->isLoopInvariant(L)) { |
| 1748 | AllInvariant = false; |
| 1749 | break; |
| 1750 | } |
| 1751 | if (AllInvariant) { |
| 1752 | NestedOperands[0] = getAddRecExpr(Operands, L); |
| 1753 | AllInvariant = true; |
| 1754 | for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i) |
| 1755 | if (!NestedOperands[i]->isLoopInvariant(NestedLoop)) { |
| 1756 | AllInvariant = false; |
| 1757 | break; |
| 1758 | } |
| 1759 | if (AllInvariant) |
| 1760 | // Ok, both add recurrences are valid after the transformation. |
| 1761 | return getAddRecExpr(NestedOperands, NestedLoop); |
| 1762 | } |
| 1763 | // Reset Operands to its original state. |
| 1764 | Operands[0] = NestedAR; |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1765 | } |
| 1766 | } |
| 1767 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1768 | FoldingSetNodeID ID; |
| 1769 | ID.AddInteger(scAddRecExpr); |
| 1770 | ID.AddInteger(Operands.size()); |
| 1771 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 1772 | ID.AddPointer(Operands[i]); |
| 1773 | ID.AddPointer(L); |
| 1774 | void *IP = 0; |
| 1775 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1776 | SCEV *S = SCEVAllocator.Allocate<SCEVAddRecExpr>(); |
| 1777 | new (S) SCEVAddRecExpr(Operands, L); |
| 1778 | UniqueSCEVs.InsertNode(S, IP); |
| 1779 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1782 | const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, |
| 1783 | const SCEV *RHS) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1784 | SmallVector<const SCEV*, 2> Ops; |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1785 | Ops.push_back(LHS); |
| 1786 | Ops.push_back(RHS); |
| 1787 | return getSMaxExpr(Ops); |
| 1788 | } |
| 1789 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1790 | const SCEV* |
| 1791 | ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV*> &Ops) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1792 | assert(!Ops.empty() && "Cannot get empty smax!"); |
| 1793 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1794 | #ifndef NDEBUG |
| 1795 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1796 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1797 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1798 | "SCEVSMaxExpr operand types don't match!"); |
| 1799 | #endif |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1800 | |
| 1801 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1802 | GroupByComplexity(Ops, LI); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1803 | |
| 1804 | // If there are any constants, fold them together. |
| 1805 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1806 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1807 | ++Idx; |
| 1808 | assert(Idx < Ops.size()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1809 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1810 | // We found two constants, fold them together! |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1811 | ConstantInt *Fold = ConstantInt::get( |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1812 | APIntOps::smax(LHSC->getValue()->getValue(), |
| 1813 | RHSC->getValue()->getValue())); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1814 | Ops[0] = getConstant(Fold); |
| 1815 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1816 | if (Ops.size() == 1) return Ops[0]; |
| 1817 | LHSC = cast<SCEVConstant>(Ops[0]); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Dan Gohman | d156c09 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1820 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1821 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) { |
| 1822 | Ops.erase(Ops.begin()); |
| 1823 | --Idx; |
Dan Gohman | d156c09 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1824 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) { |
| 1825 | // If we have an smax with a constant maximum-int, it will always be |
| 1826 | // maximum-int. |
| 1827 | return Ops[0]; |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | if (Ops.size() == 1) return Ops[0]; |
| 1832 | |
| 1833 | // Find the first SMax |
| 1834 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr) |
| 1835 | ++Idx; |
| 1836 | |
| 1837 | // Check to see if one of the operands is an SMax. If so, expand its operands |
| 1838 | // onto our operand list, and recurse to simplify. |
| 1839 | if (Idx < Ops.size()) { |
| 1840 | bool DeletedSMax = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1841 | while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1842 | Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end()); |
| 1843 | Ops.erase(Ops.begin()+Idx); |
| 1844 | DeletedSMax = true; |
| 1845 | } |
| 1846 | |
| 1847 | if (DeletedSMax) |
| 1848 | return getSMaxExpr(Ops); |
| 1849 | } |
| 1850 | |
| 1851 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1852 | // so, delete one. Since we sorted the list, these values are required to |
| 1853 | // be adjacent. |
| 1854 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1855 | if (Ops[i] == Ops[i+1]) { // X smax Y smax Y --> X smax Y |
| 1856 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 1857 | --i; --e; |
| 1858 | } |
| 1859 | |
| 1860 | if (Ops.size() == 1) return Ops[0]; |
| 1861 | |
| 1862 | assert(!Ops.empty() && "Reduced smax down to nothing!"); |
| 1863 | |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1864 | // Okay, it looks like we really DO need an smax expr. Check to see if we |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1865 | // already have one, otherwise create a new one. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1866 | FoldingSetNodeID ID; |
| 1867 | ID.AddInteger(scSMaxExpr); |
| 1868 | ID.AddInteger(Ops.size()); |
| 1869 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1870 | ID.AddPointer(Ops[i]); |
| 1871 | void *IP = 0; |
| 1872 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1873 | SCEV *S = SCEVAllocator.Allocate<SCEVSMaxExpr>(); |
| 1874 | new (S) SCEVSMaxExpr(Ops); |
| 1875 | UniqueSCEVs.InsertNode(S, IP); |
| 1876 | return S; |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1879 | const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, |
| 1880 | const SCEV *RHS) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1881 | SmallVector<const SCEV*, 2> Ops; |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1882 | Ops.push_back(LHS); |
| 1883 | Ops.push_back(RHS); |
| 1884 | return getUMaxExpr(Ops); |
| 1885 | } |
| 1886 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1887 | const SCEV* |
| 1888 | ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV*> &Ops) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1889 | assert(!Ops.empty() && "Cannot get empty umax!"); |
| 1890 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1891 | #ifndef NDEBUG |
| 1892 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1893 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1894 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1895 | "SCEVUMaxExpr operand types don't match!"); |
| 1896 | #endif |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1897 | |
| 1898 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1899 | GroupByComplexity(Ops, LI); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1900 | |
| 1901 | // If there are any constants, fold them together. |
| 1902 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1903 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1904 | ++Idx; |
| 1905 | assert(Idx < Ops.size()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1906 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1907 | // We found two constants, fold them together! |
| 1908 | ConstantInt *Fold = ConstantInt::get( |
| 1909 | APIntOps::umax(LHSC->getValue()->getValue(), |
| 1910 | RHSC->getValue()->getValue())); |
| 1911 | Ops[0] = getConstant(Fold); |
| 1912 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1913 | if (Ops.size() == 1) return Ops[0]; |
| 1914 | LHSC = cast<SCEVConstant>(Ops[0]); |
| 1915 | } |
| 1916 | |
Dan Gohman | d156c09 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1917 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1918 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) { |
| 1919 | Ops.erase(Ops.begin()); |
| 1920 | --Idx; |
Dan Gohman | d156c09 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1921 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) { |
| 1922 | // If we have an umax with a constant maximum-int, it will always be |
| 1923 | // maximum-int. |
| 1924 | return Ops[0]; |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | if (Ops.size() == 1) return Ops[0]; |
| 1929 | |
| 1930 | // Find the first UMax |
| 1931 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr) |
| 1932 | ++Idx; |
| 1933 | |
| 1934 | // Check to see if one of the operands is a UMax. If so, expand its operands |
| 1935 | // onto our operand list, and recurse to simplify. |
| 1936 | if (Idx < Ops.size()) { |
| 1937 | bool DeletedUMax = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1938 | while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1939 | Ops.insert(Ops.end(), UMax->op_begin(), UMax->op_end()); |
| 1940 | Ops.erase(Ops.begin()+Idx); |
| 1941 | DeletedUMax = true; |
| 1942 | } |
| 1943 | |
| 1944 | if (DeletedUMax) |
| 1945 | return getUMaxExpr(Ops); |
| 1946 | } |
| 1947 | |
| 1948 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1949 | // so, delete one. Since we sorted the list, these values are required to |
| 1950 | // be adjacent. |
| 1951 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1952 | if (Ops[i] == Ops[i+1]) { // X umax Y umax Y --> X umax Y |
| 1953 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 1954 | --i; --e; |
| 1955 | } |
| 1956 | |
| 1957 | if (Ops.size() == 1) return Ops[0]; |
| 1958 | |
| 1959 | assert(!Ops.empty() && "Reduced umax down to nothing!"); |
| 1960 | |
| 1961 | // Okay, it looks like we really DO need a umax expr. Check to see if we |
| 1962 | // already have one, otherwise create a new one. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1963 | FoldingSetNodeID ID; |
| 1964 | ID.AddInteger(scUMaxExpr); |
| 1965 | ID.AddInteger(Ops.size()); |
| 1966 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1967 | ID.AddPointer(Ops[i]); |
| 1968 | void *IP = 0; |
| 1969 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1970 | SCEV *S = SCEVAllocator.Allocate<SCEVUMaxExpr>(); |
| 1971 | new (S) SCEVUMaxExpr(Ops); |
| 1972 | UniqueSCEVs.InsertNode(S, IP); |
| 1973 | return S; |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1976 | const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS, |
| 1977 | const SCEV *RHS) { |
Dan Gohman | d01fff8 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 1978 | // ~smax(~x, ~y) == smin(x, y). |
| 1979 | return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 1980 | } |
| 1981 | |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1982 | const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS, |
| 1983 | const SCEV *RHS) { |
Dan Gohman | d01fff8 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 1984 | // ~umax(~x, ~y) == umin(x, y) |
| 1985 | return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 1986 | } |
| 1987 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1988 | const SCEV* ScalarEvolution::getUnknown(Value *V) { |
Dan Gohman | 984c78a | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 1989 | // Don't attempt to do anything other than create a SCEVUnknown object |
| 1990 | // here. createSCEV only calls getUnknown after checking for all other |
| 1991 | // interesting possibilities, and any other code that calls getUnknown |
| 1992 | // is doing so in order to hide a value from SCEV canonicalization. |
| 1993 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1994 | FoldingSetNodeID ID; |
| 1995 | ID.AddInteger(scUnknown); |
| 1996 | ID.AddPointer(V); |
| 1997 | void *IP = 0; |
| 1998 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1999 | SCEV *S = SCEVAllocator.Allocate<SCEVUnknown>(); |
| 2000 | new (S) SCEVUnknown(V); |
| 2001 | UniqueSCEVs.InsertNode(S, IP); |
| 2002 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2005 | //===----------------------------------------------------------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2006 | // Basic SCEV Analysis and PHI Idiom Recognition Code |
| 2007 | // |
| 2008 | |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2009 | /// isSCEVable - Test if values of the given type are analyzable within |
| 2010 | /// the SCEV framework. This primarily includes integer types, and it |
| 2011 | /// can optionally include pointer types if the ScalarEvolution class |
| 2012 | /// has access to target-specific information. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2013 | bool ScalarEvolution::isSCEVable(const Type *Ty) const { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2014 | // Integers are always SCEVable. |
| 2015 | if (Ty->isInteger()) |
| 2016 | return true; |
| 2017 | |
| 2018 | // Pointers are SCEVable if TargetData information is available |
| 2019 | // to provide pointer size information. |
| 2020 | if (isa<PointerType>(Ty)) |
| 2021 | return TD != NULL; |
| 2022 | |
| 2023 | // Otherwise it's not SCEVable. |
| 2024 | return false; |
| 2025 | } |
| 2026 | |
| 2027 | /// getTypeSizeInBits - Return the size in bits of the specified type, |
| 2028 | /// for which isSCEVable must return true. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2029 | uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2030 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2031 | |
| 2032 | // If we have a TargetData, use it! |
| 2033 | if (TD) |
| 2034 | return TD->getTypeSizeInBits(Ty); |
| 2035 | |
| 2036 | // Otherwise, we support only integer types. |
| 2037 | assert(Ty->isInteger() && "isSCEVable permitted a non-SCEVable type!"); |
| 2038 | return Ty->getPrimitiveSizeInBits(); |
| 2039 | } |
| 2040 | |
| 2041 | /// getEffectiveSCEVType - Return a type with the same bitwidth as |
| 2042 | /// the given type and which represents how SCEV will treat the given |
| 2043 | /// type, for which isSCEVable must return true. For pointer types, |
| 2044 | /// this is the pointer-sized integer type. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2045 | const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2046 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2047 | |
| 2048 | if (Ty->isInteger()) |
| 2049 | return Ty; |
| 2050 | |
| 2051 | assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!"); |
| 2052 | return TD->getIntPtrType(); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2053 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2054 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2055 | const SCEV* ScalarEvolution::getCouldNotCompute() { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2056 | return &CouldNotCompute; |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
Dan Gohman | d83d4af | 2009-05-04 22:20:30 +0000 | [diff] [blame] | 2059 | /// hasSCEV - Return true if the SCEV for this value has already been |
Edwin Török | 0e828d6 | 2009-05-01 08:33:47 +0000 | [diff] [blame] | 2060 | /// computed. |
| 2061 | bool ScalarEvolution::hasSCEV(Value *V) const { |
| 2062 | return Scalars.count(V); |
| 2063 | } |
| 2064 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2065 | /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the |
| 2066 | /// expression and create a new one. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2067 | const SCEV* ScalarEvolution::getSCEV(Value *V) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2068 | assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2069 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2070 | std::map<SCEVCallbackVH, const SCEV*>::iterator I = Scalars.find(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2071 | if (I != Scalars.end()) return I->second; |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2072 | const SCEV* S = createSCEV(V); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2073 | Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2074 | return S; |
| 2075 | } |
| 2076 | |
Dan Gohman | 984c78a | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2077 | /// getIntegerSCEV - Given a SCEVable type, create a constant for the |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2078 | /// specified signed integer value and return a SCEV for the constant. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2079 | const SCEV* ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) { |
Dan Gohman | 984c78a | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2080 | const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty)); |
| 2081 | return getConstant(ConstantInt::get(ITy, Val)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V |
| 2085 | /// |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2086 | const SCEV* ScalarEvolution::getNegativeSCEV(const SCEV* V) { |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2087 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 2088 | return getConstant(cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue()))); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2089 | |
| 2090 | const Type *Ty = V->getType(); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2091 | Ty = getEffectiveSCEVType(Ty); |
| 2092 | return getMulExpr(V, getConstant(ConstantInt::getAllOnesValue(Ty))); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
| 2095 | /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2096 | const SCEV* ScalarEvolution::getNotSCEV(const SCEV* V) { |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2097 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 2098 | return getConstant(cast<ConstantInt>(ConstantExpr::getNot(VC->getValue()))); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2099 | |
| 2100 | const Type *Ty = V->getType(); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2101 | Ty = getEffectiveSCEVType(Ty); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2102 | const SCEV* AllOnes = getConstant(ConstantInt::getAllOnesValue(Ty)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2103 | return getMinusSCEV(AllOnes, V); |
| 2104 | } |
| 2105 | |
| 2106 | /// getMinusSCEV - Return a SCEV corresponding to LHS - RHS. |
| 2107 | /// |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2108 | const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, |
| 2109 | const SCEV *RHS) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2110 | // X - Y --> X + -Y |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2111 | return getAddExpr(LHS, getNegativeSCEV(RHS)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2115 | /// input value to the specified type. If the type must be extended, it is zero |
| 2116 | /// extended. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2117 | const SCEV* |
| 2118 | ScalarEvolution::getTruncateOrZeroExtend(const SCEV* V, |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2119 | const Type *Ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2120 | const Type *SrcTy = V->getType(); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2121 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2122 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2123 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2124 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2125 | return V; // No conversion |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2126 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2127 | return getTruncateExpr(V, Ty); |
| 2128 | return getZeroExtendExpr(V, Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2132 | /// input value to the specified type. If the type must be extended, it is sign |
| 2133 | /// extended. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2134 | const SCEV* |
| 2135 | ScalarEvolution::getTruncateOrSignExtend(const SCEV* V, |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2136 | const Type *Ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2137 | const Type *SrcTy = V->getType(); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2138 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2139 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2140 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2141 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2142 | return V; // No conversion |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2143 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2144 | return getTruncateExpr(V, Ty); |
| 2145 | return getSignExtendExpr(V, Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2148 | /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2149 | /// input value to the specified type. If the type must be extended, it is zero |
| 2150 | /// extended. The conversion must not be narrowing. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2151 | const SCEV* |
| 2152 | ScalarEvolution::getNoopOrZeroExtend(const SCEV* V, const Type *Ty) { |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2153 | const Type *SrcTy = V->getType(); |
| 2154 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2155 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 2156 | "Cannot noop or zero extend with non-integer arguments!"); |
| 2157 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2158 | "getNoopOrZeroExtend cannot truncate!"); |
| 2159 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2160 | return V; // No conversion |
| 2161 | return getZeroExtendExpr(V, Ty); |
| 2162 | } |
| 2163 | |
| 2164 | /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2165 | /// input value to the specified type. If the type must be extended, it is sign |
| 2166 | /// extended. The conversion must not be narrowing. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2167 | const SCEV* |
| 2168 | ScalarEvolution::getNoopOrSignExtend(const SCEV* V, const Type *Ty) { |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2169 | const Type *SrcTy = V->getType(); |
| 2170 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2171 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 2172 | "Cannot noop or sign extend with non-integer arguments!"); |
| 2173 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2174 | "getNoopOrSignExtend cannot truncate!"); |
| 2175 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2176 | return V; // No conversion |
| 2177 | return getSignExtendExpr(V, Ty); |
| 2178 | } |
| 2179 | |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2180 | /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of |
| 2181 | /// the input value to the specified type. If the type must be extended, |
| 2182 | /// it is extended with unspecified bits. The conversion must not be |
| 2183 | /// narrowing. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2184 | const SCEV* |
| 2185 | ScalarEvolution::getNoopOrAnyExtend(const SCEV* V, const Type *Ty) { |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2186 | const Type *SrcTy = V->getType(); |
| 2187 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2188 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 2189 | "Cannot noop or any extend with non-integer arguments!"); |
| 2190 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2191 | "getNoopOrAnyExtend cannot truncate!"); |
| 2192 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2193 | return V; // No conversion |
| 2194 | return getAnyExtendExpr(V, Ty); |
| 2195 | } |
| 2196 | |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2197 | /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the |
| 2198 | /// input value to the specified type. The conversion must not be widening. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2199 | const SCEV* |
| 2200 | ScalarEvolution::getTruncateOrNoop(const SCEV* V, const Type *Ty) { |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2201 | const Type *SrcTy = V->getType(); |
| 2202 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2203 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 2204 | "Cannot truncate or noop with non-integer arguments!"); |
| 2205 | assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) && |
| 2206 | "getTruncateOrNoop cannot extend!"); |
| 2207 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2208 | return V; // No conversion |
| 2209 | return getTruncateExpr(V, Ty); |
| 2210 | } |
| 2211 | |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2212 | /// getUMaxFromMismatchedTypes - Promote the operands to the wider of |
| 2213 | /// the types using zero-extension, and then perform a umax operation |
| 2214 | /// with them. |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2215 | const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS, |
| 2216 | const SCEV *RHS) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2217 | const SCEV* PromotedLHS = LHS; |
| 2218 | const SCEV* PromotedRHS = RHS; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2219 | |
| 2220 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2221 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2222 | else |
| 2223 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2224 | |
| 2225 | return getUMaxExpr(PromotedLHS, PromotedRHS); |
| 2226 | } |
| 2227 | |
Dan Gohman | 9e62bb0 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2228 | /// getUMinFromMismatchedTypes - Promote the operands to the wider of |
| 2229 | /// the types using zero-extension, and then perform a umin operation |
| 2230 | /// with them. |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2231 | const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS, |
| 2232 | const SCEV *RHS) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2233 | const SCEV* PromotedLHS = LHS; |
| 2234 | const SCEV* PromotedRHS = RHS; |
Dan Gohman | 9e62bb0 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2235 | |
| 2236 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2237 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2238 | else |
| 2239 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2240 | |
| 2241 | return getUMinExpr(PromotedLHS, PromotedRHS); |
| 2242 | } |
| 2243 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2244 | /// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value for |
| 2245 | /// the specified instruction and replaces any references to the symbolic value |
| 2246 | /// SymName with the specified value. This is used during PHI resolution. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2247 | void |
| 2248 | ScalarEvolution::ReplaceSymbolicValueWithConcrete(Instruction *I, |
| 2249 | const SCEV *SymName, |
| 2250 | const SCEV *NewVal) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2251 | std::map<SCEVCallbackVH, const SCEV*>::iterator SI = |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2252 | Scalars.find(SCEVCallbackVH(I, this)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2253 | if (SI == Scalars.end()) return; |
| 2254 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2255 | const SCEV* NV = |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2256 | SI->second->replaceSymbolicValuesWithConcrete(SymName, NewVal, *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2257 | if (NV == SI->second) return; // No change. |
| 2258 | |
| 2259 | SI->second = NV; // Update the scalars map! |
| 2260 | |
| 2261 | // Any instruction values that use this instruction might also need to be |
| 2262 | // updated! |
| 2263 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 2264 | UI != E; ++UI) |
| 2265 | ReplaceSymbolicValueWithConcrete(cast<Instruction>(*UI), SymName, NewVal); |
| 2266 | } |
| 2267 | |
| 2268 | /// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in |
| 2269 | /// a loop header, making it a potential recurrence, or it doesn't. |
| 2270 | /// |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2271 | const SCEV* ScalarEvolution::createNodeForPHI(PHINode *PN) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2272 | if (PN->getNumIncomingValues() == 2) // The loops have been canonicalized. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2273 | if (const Loop *L = LI->getLoopFor(PN->getParent())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2274 | if (L->getHeader() == PN->getParent()) { |
| 2275 | // If it lives in the loop header, it has two incoming values, one |
| 2276 | // from outside the loop, and one from inside. |
| 2277 | unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0)); |
| 2278 | unsigned BackEdge = IncomingEdge^1; |
| 2279 | |
| 2280 | // While we are analyzing this PHI node, handle its value symbolically. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2281 | const SCEV* SymbolicName = getUnknown(PN); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2282 | assert(Scalars.find(PN) == Scalars.end() && |
| 2283 | "PHI node already processed?"); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2284 | Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2285 | |
| 2286 | // Using this symbolic name for the PHI, analyze the value coming around |
| 2287 | // the back-edge. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2288 | const SCEV* BEValue = getSCEV(PN->getIncomingValue(BackEdge)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2289 | |
| 2290 | // NOTE: If BEValue is loop invariant, we know that the PHI node just |
| 2291 | // has a special value for the first iteration of the loop. |
| 2292 | |
| 2293 | // If the value coming around the backedge is an add with the symbolic |
| 2294 | // value we just inserted, then we found a simple induction variable! |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2295 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2296 | // If there is a single occurrence of the symbolic value, replace it |
| 2297 | // with a recurrence. |
| 2298 | unsigned FoundIndex = Add->getNumOperands(); |
| 2299 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2300 | if (Add->getOperand(i) == SymbolicName) |
| 2301 | if (FoundIndex == e) { |
| 2302 | FoundIndex = i; |
| 2303 | break; |
| 2304 | } |
| 2305 | |
| 2306 | if (FoundIndex != Add->getNumOperands()) { |
| 2307 | // Create an add with everything but the specified operand. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2308 | SmallVector<const SCEV*, 8> Ops; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2309 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2310 | if (i != FoundIndex) |
| 2311 | Ops.push_back(Add->getOperand(i)); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2312 | const SCEV* Accum = getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2313 | |
| 2314 | // This is not a valid addrec if the step amount is varying each |
| 2315 | // loop iteration, but is not itself an addrec in this loop. |
| 2316 | if (Accum->isLoopInvariant(L) || |
| 2317 | (isa<SCEVAddRecExpr>(Accum) && |
| 2318 | cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) { |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2319 | const SCEV *StartVal = |
| 2320 | getSCEV(PN->getIncomingValue(IncomingEdge)); |
| 2321 | const SCEV *PHISCEV = |
| 2322 | getAddRecExpr(StartVal, Accum, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2323 | |
| 2324 | // Okay, for the entire analysis of this edge we assumed the PHI |
| 2325 | // to be symbolic. We now need to go back and update all of the |
| 2326 | // entries for the scalars that use the PHI (except for the PHI |
| 2327 | // itself) to use the new analyzed value instead of the "symbolic" |
| 2328 | // value. |
| 2329 | ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV); |
| 2330 | return PHISCEV; |
| 2331 | } |
| 2332 | } |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2333 | } else if (const SCEVAddRecExpr *AddRec = |
| 2334 | dyn_cast<SCEVAddRecExpr>(BEValue)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2335 | // Otherwise, this could be a loop like this: |
| 2336 | // i = 0; for (j = 1; ..; ++j) { .... i = j; } |
| 2337 | // In this case, j = {1,+,1} and BEValue is j. |
| 2338 | // Because the other in-value of i (0) fits the evolution of BEValue |
| 2339 | // i really is an addrec evolution. |
| 2340 | if (AddRec->getLoop() == L && AddRec->isAffine()) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2341 | const SCEV* StartVal = getSCEV(PN->getIncomingValue(IncomingEdge)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2342 | |
| 2343 | // If StartVal = j.start - j.stride, we can use StartVal as the |
| 2344 | // initial step of the addrec evolution. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2345 | if (StartVal == getMinusSCEV(AddRec->getOperand(0), |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 2346 | AddRec->getOperand(1))) { |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2347 | const SCEV* PHISCEV = |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2348 | getAddRecExpr(StartVal, AddRec->getOperand(1), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2349 | |
| 2350 | // Okay, for the entire analysis of this edge we assumed the PHI |
| 2351 | // to be symbolic. We now need to go back and update all of the |
| 2352 | // entries for the scalars that use the PHI (except for the PHI |
| 2353 | // itself) to use the new analyzed value instead of the "symbolic" |
| 2354 | // value. |
| 2355 | ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV); |
| 2356 | return PHISCEV; |
| 2357 | } |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | return SymbolicName; |
| 2362 | } |
| 2363 | |
| 2364 | // If it's not a loop phi, we can't handle it yet. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2365 | return getUnknown(PN); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2368 | /// createNodeForGEP - Expand GEP instructions into add and multiply |
| 2369 | /// operations. This allows them to be analyzed by regular SCEV code. |
| 2370 | /// |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2371 | const SCEV* ScalarEvolution::createNodeForGEP(User *GEP) { |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2372 | |
| 2373 | const Type *IntPtrTy = TD->getIntPtrType(); |
Dan Gohman | c7034fa | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2374 | Value *Base = GEP->getOperand(0); |
Dan Gohman | d586a4f | 2009-05-09 00:14:52 +0000 | [diff] [blame] | 2375 | // Don't attempt to analyze GEPs over unsized objects. |
| 2376 | if (!cast<PointerType>(Base->getType())->getElementType()->isSized()) |
| 2377 | return getUnknown(GEP); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2378 | const SCEV* TotalOffset = getIntegerSCEV(0, IntPtrTy); |
Dan Gohman | c7034fa | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2379 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 2380 | for (GetElementPtrInst::op_iterator I = next(GEP->op_begin()), |
| 2381 | E = GEP->op_end(); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2382 | I != E; ++I) { |
| 2383 | Value *Index = *I; |
| 2384 | // Compute the (potentially symbolic) offset in bytes for this index. |
| 2385 | if (const StructType *STy = dyn_cast<StructType>(*GTI++)) { |
| 2386 | // For a struct, add the member offset. |
| 2387 | const StructLayout &SL = *TD->getStructLayout(STy); |
| 2388 | unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue(); |
| 2389 | uint64_t Offset = SL.getElementOffset(FieldNo); |
| 2390 | TotalOffset = getAddExpr(TotalOffset, |
| 2391 | getIntegerSCEV(Offset, IntPtrTy)); |
| 2392 | } else { |
| 2393 | // For an array, add the element offset, explicitly scaled. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2394 | const SCEV* LocalOffset = getSCEV(Index); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2395 | if (!isa<PointerType>(LocalOffset->getType())) |
| 2396 | // Getelementptr indicies are signed. |
| 2397 | LocalOffset = getTruncateOrSignExtend(LocalOffset, |
| 2398 | IntPtrTy); |
| 2399 | LocalOffset = |
| 2400 | getMulExpr(LocalOffset, |
Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2401 | getIntegerSCEV(TD->getTypeAllocSize(*GTI), |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2402 | IntPtrTy)); |
| 2403 | TotalOffset = getAddExpr(TotalOffset, LocalOffset); |
| 2404 | } |
| 2405 | } |
| 2406 | return getAddExpr(getSCEV(Base), TotalOffset); |
| 2407 | } |
| 2408 | |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2409 | /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is |
| 2410 | /// guaranteed to end in (at every loop iteration). It is, at the same time, |
| 2411 | /// the minimum number of times S is divisible by 2. For example, given {4,+,8} |
| 2412 | /// it returns 2. If S is guaranteed to be 0, it returns the bitwidth of S. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2413 | uint32_t |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2414 | ScalarEvolution::GetMinTrailingZeros(const SCEV* S) { |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2415 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Chris Lattner | 6ecce2a | 2007-11-23 22:36:49 +0000 | [diff] [blame] | 2416 | return C->getValue()->getValue().countTrailingZeros(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2417 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2418 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S)) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2419 | return std::min(GetMinTrailingZeros(T->getOperand()), |
| 2420 | (uint32_t)getTypeSizeInBits(T->getType())); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2421 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2422 | if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) { |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2423 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 2424 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 2425 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2428 | if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) { |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2429 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 2430 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 2431 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2434 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2435 | // The result is the min of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2436 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2437 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2438 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2439 | return MinOpRes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2442 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2443 | // The result is the sum of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2444 | uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0)); |
| 2445 | uint32_t BitWidth = getTypeSizeInBits(M->getType()); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2446 | for (unsigned i = 1, e = M->getNumOperands(); |
| 2447 | SumOpRes != BitWidth && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2448 | SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2449 | BitWidth); |
| 2450 | return SumOpRes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2451 | } |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2452 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2453 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2454 | // The result is the min of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2455 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2456 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2457 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2458 | return MinOpRes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2459 | } |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2460 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2461 | if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2462 | // The result is the min of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2463 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2464 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2465 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2466 | return MinOpRes; |
| 2467 | } |
| 2468 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2469 | if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2470 | // The result is the min of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2471 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2472 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2473 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2474 | return MinOpRes; |
| 2475 | } |
| 2476 | |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2477 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2478 | // For a SCEVUnknown, ask ValueTracking. |
| 2479 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2480 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 2481 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 2482 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones); |
| 2483 | return Zeros.countTrailingOnes(); |
| 2484 | } |
| 2485 | |
| 2486 | // SCEVUDivExpr |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2487 | return 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2490 | uint32_t |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2491 | ScalarEvolution::GetMinLeadingZeros(const SCEV* S) { |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2492 | // TODO: Handle other SCEV expression types here. |
| 2493 | |
| 2494 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
| 2495 | return C->getValue()->getValue().countLeadingZeros(); |
| 2496 | |
| 2497 | if (const SCEVZeroExtendExpr *C = dyn_cast<SCEVZeroExtendExpr>(S)) { |
| 2498 | // A zero-extension cast adds zero bits. |
| 2499 | return GetMinLeadingZeros(C->getOperand()) + |
| 2500 | (getTypeSizeInBits(C->getType()) - |
| 2501 | getTypeSizeInBits(C->getOperand()->getType())); |
| 2502 | } |
| 2503 | |
| 2504 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2505 | // For a SCEVUnknown, ask ValueTracking. |
| 2506 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2507 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 2508 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 2509 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD); |
| 2510 | return Zeros.countLeadingOnes(); |
| 2511 | } |
| 2512 | |
| 2513 | return 1; |
| 2514 | } |
| 2515 | |
| 2516 | uint32_t |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2517 | ScalarEvolution::GetMinSignBits(const SCEV* S) { |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2518 | // TODO: Handle other SCEV expression types here. |
| 2519 | |
| 2520 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) { |
| 2521 | const APInt &A = C->getValue()->getValue(); |
| 2522 | return A.isNegative() ? A.countLeadingOnes() : |
| 2523 | A.countLeadingZeros(); |
| 2524 | } |
| 2525 | |
| 2526 | if (const SCEVSignExtendExpr *C = dyn_cast<SCEVSignExtendExpr>(S)) { |
| 2527 | // A sign-extension cast adds sign bits. |
| 2528 | return GetMinSignBits(C->getOperand()) + |
| 2529 | (getTypeSizeInBits(C->getType()) - |
| 2530 | getTypeSizeInBits(C->getOperand()->getType())); |
| 2531 | } |
| 2532 | |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2533 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { |
| 2534 | unsigned BitWidth = getTypeSizeInBits(A->getType()); |
| 2535 | |
| 2536 | // Special case decrementing a value (ADD X, -1): |
| 2537 | if (const SCEVConstant *CRHS = dyn_cast<SCEVConstant>(A->getOperand(0))) |
| 2538 | if (CRHS->isAllOnesValue()) { |
| 2539 | SmallVector<const SCEV *, 4> OtherOps(A->op_begin() + 1, A->op_end()); |
| 2540 | const SCEV *OtherOpsAdd = getAddExpr(OtherOps); |
| 2541 | unsigned LZ = GetMinLeadingZeros(OtherOpsAdd); |
| 2542 | |
| 2543 | // If the input is known to be 0 or 1, the output is 0/-1, which is all |
| 2544 | // sign bits set. |
| 2545 | if (LZ == BitWidth - 1) |
| 2546 | return BitWidth; |
| 2547 | |
| 2548 | // If we are subtracting one from a positive number, there is no carry |
| 2549 | // out of the result. |
| 2550 | if (LZ > 0) |
| 2551 | return GetMinSignBits(OtherOpsAdd); |
| 2552 | } |
| 2553 | |
| 2554 | // Add can have at most one carry bit. Thus we know that the output |
| 2555 | // is, at worst, one more bit than the inputs. |
| 2556 | unsigned Min = BitWidth; |
| 2557 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { |
| 2558 | unsigned N = GetMinSignBits(A->getOperand(i)); |
| 2559 | Min = std::min(Min, N) - 1; |
| 2560 | if (Min == 0) return 1; |
| 2561 | } |
| 2562 | return 1; |
| 2563 | } |
| 2564 | |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2565 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2566 | // For a SCEVUnknown, ask ValueTracking. |
| 2567 | return ComputeNumSignBits(U->getValue(), TD); |
| 2568 | } |
| 2569 | |
| 2570 | return 1; |
| 2571 | } |
| 2572 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2573 | /// createSCEV - We know that there is no SCEV for the specified value. |
| 2574 | /// Analyze the expression. |
| 2575 | /// |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2576 | const SCEV* ScalarEvolution::createSCEV(Value *V) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2577 | if (!isSCEVable(V->getType())) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2578 | return getUnknown(V); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2579 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2580 | unsigned Opcode = Instruction::UserOp1; |
| 2581 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 2582 | Opcode = I->getOpcode(); |
| 2583 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 2584 | Opcode = CE->getOpcode(); |
Dan Gohman | 984c78a | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2585 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 2586 | return getConstant(CI); |
| 2587 | else if (isa<ConstantPointerNull>(V)) |
| 2588 | return getIntegerSCEV(0, V->getType()); |
| 2589 | else if (isa<UndefValue>(V)) |
| 2590 | return getIntegerSCEV(0, V->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2591 | else |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2592 | return getUnknown(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2593 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2594 | User *U = cast<User>(V); |
| 2595 | switch (Opcode) { |
| 2596 | case Instruction::Add: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2597 | return getAddExpr(getSCEV(U->getOperand(0)), |
| 2598 | getSCEV(U->getOperand(1))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2599 | case Instruction::Mul: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2600 | return getMulExpr(getSCEV(U->getOperand(0)), |
| 2601 | getSCEV(U->getOperand(1))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2602 | case Instruction::UDiv: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2603 | return getUDivExpr(getSCEV(U->getOperand(0)), |
| 2604 | getSCEV(U->getOperand(1))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2605 | case Instruction::Sub: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2606 | return getMinusSCEV(getSCEV(U->getOperand(0)), |
| 2607 | getSCEV(U->getOperand(1))); |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2608 | case Instruction::And: |
| 2609 | // For an expression like x&255 that merely masks off the high bits, |
| 2610 | // use zext(trunc(x)) as the SCEV expression. |
| 2611 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 91ae1e7 | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2612 | if (CI->isNullValue()) |
| 2613 | return getSCEV(U->getOperand(1)); |
Dan Gohman | c7ebba1 | 2009-04-27 01:41:10 +0000 | [diff] [blame] | 2614 | if (CI->isAllOnesValue()) |
| 2615 | return getSCEV(U->getOperand(0)); |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2616 | const APInt &A = CI->getValue(); |
Dan Gohman | a7726c3 | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2617 | |
| 2618 | // Instcombine's ShrinkDemandedConstant may strip bits out of |
| 2619 | // constants, obscuring what would otherwise be a low-bits mask. |
| 2620 | // Use ComputeMaskedBits to compute what ShrinkDemandedConstant |
| 2621 | // knew about to reconstruct a low-bits mask value. |
| 2622 | unsigned LZ = A.countLeadingZeros(); |
| 2623 | unsigned BitWidth = A.getBitWidth(); |
| 2624 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
| 2625 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 2626 | ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD); |
| 2627 | |
| 2628 | APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ); |
| 2629 | |
Dan Gohman | ae1d7dd | 2009-06-17 23:54:37 +0000 | [diff] [blame] | 2630 | if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask)) |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2631 | return |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2632 | getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)), |
Dan Gohman | a7726c3 | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2633 | IntegerType::get(BitWidth - LZ)), |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2634 | U->getType()); |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2635 | } |
| 2636 | break; |
Dan Gohman | a7726c3 | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2637 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2638 | case Instruction::Or: |
| 2639 | // If the RHS of the Or is a constant, we may have something like: |
| 2640 | // X*4+1 which got turned into X*4|1. Handle this as an Add so loop |
| 2641 | // optimizations will transparently handle this case. |
| 2642 | // |
| 2643 | // In order for this transformation to be safe, the LHS must be of the |
| 2644 | // form X*(2^n) and the Or constant must be less than 2^n. |
| 2645 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2646 | const SCEV* LHS = getSCEV(U->getOperand(0)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2647 | const APInt &CIVal = CI->getValue(); |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2648 | if (GetMinTrailingZeros(LHS) >= |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2649 | (CIVal.getBitWidth() - CIVal.countLeadingZeros())) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2650 | return getAddExpr(LHS, getSCEV(U->getOperand(1))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2651 | } |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2652 | break; |
| 2653 | case Instruction::Xor: |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2654 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2655 | // If the RHS of the xor is a signbit, then this is just an add. |
| 2656 | // Instcombine turns add of signbit into xor as a strength reduction step. |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2657 | if (CI->getValue().isSignBit()) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2658 | return getAddExpr(getSCEV(U->getOperand(0)), |
| 2659 | getSCEV(U->getOperand(1))); |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2660 | |
| 2661 | // If the RHS of xor is -1, then this is a not operation. |
Dan Gohman | c897f75 | 2009-05-18 16:17:44 +0000 | [diff] [blame] | 2662 | if (CI->isAllOnesValue()) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2663 | return getNotSCEV(getSCEV(U->getOperand(0))); |
Dan Gohman | fc78cff | 2009-05-18 16:29:04 +0000 | [diff] [blame] | 2664 | |
| 2665 | // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask. |
| 2666 | // This is a variant of the check for xor with -1, and it handles |
| 2667 | // the case where instcombine has trimmed non-demanded bits out |
| 2668 | // of an xor with -1. |
| 2669 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0))) |
| 2670 | if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1))) |
| 2671 | if (BO->getOpcode() == Instruction::And && |
| 2672 | LCI->getValue() == CI->getValue()) |
| 2673 | if (const SCEVZeroExtendExpr *Z = |
Dan Gohman | e49ae43 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 2674 | dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) { |
Dan Gohman | ed1d8bb | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 2675 | const Type *UTy = U->getType(); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2676 | const SCEV* Z0 = Z->getOperand(); |
Dan Gohman | ed1d8bb | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 2677 | const Type *Z0Ty = Z0->getType(); |
| 2678 | unsigned Z0TySize = getTypeSizeInBits(Z0Ty); |
| 2679 | |
| 2680 | // If C is a low-bits mask, the zero extend is zerving to |
| 2681 | // mask off the high bits. Complement the operand and |
| 2682 | // re-apply the zext. |
| 2683 | if (APIntOps::isMask(Z0TySize, CI->getValue())) |
| 2684 | return getZeroExtendExpr(getNotSCEV(Z0), UTy); |
| 2685 | |
| 2686 | // If C is a single bit, it may be in the sign-bit position |
| 2687 | // before the zero-extend. In this case, represent the xor |
| 2688 | // using an add, which is equivalent, and re-apply the zext. |
| 2689 | APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize); |
| 2690 | if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() && |
| 2691 | Trunc.isSignBit()) |
| 2692 | return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)), |
| 2693 | UTy); |
Dan Gohman | e49ae43 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 2694 | } |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2695 | } |
| 2696 | break; |
| 2697 | |
| 2698 | case Instruction::Shl: |
| 2699 | // Turn shift left of a constant amount into a multiply. |
| 2700 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 2701 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
| 2702 | Constant *X = ConstantInt::get( |
| 2703 | APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2704 | return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2705 | } |
| 2706 | break; |
| 2707 | |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2708 | case Instruction::LShr: |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 2709 | // Turn logical shift right of a constant into a unsigned divide. |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2710 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 2711 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
| 2712 | Constant *X = ConstantInt::get( |
| 2713 | APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2714 | return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2715 | } |
| 2716 | break; |
| 2717 | |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2718 | case Instruction::AShr: |
| 2719 | // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression. |
| 2720 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) |
| 2721 | if (Instruction *L = dyn_cast<Instruction>(U->getOperand(0))) |
| 2722 | if (L->getOpcode() == Instruction::Shl && |
| 2723 | L->getOperand(1) == U->getOperand(1)) { |
Dan Gohman | 91ae1e7 | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2724 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2725 | uint64_t Amt = BitWidth - CI->getZExtValue(); |
| 2726 | if (Amt == BitWidth) |
| 2727 | return getSCEV(L->getOperand(0)); // shift by zero --> noop |
| 2728 | if (Amt > BitWidth) |
| 2729 | return getIntegerSCEV(0, U->getType()); // value is undefined |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2730 | return |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2731 | getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)), |
Dan Gohman | 91ae1e7 | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2732 | IntegerType::get(Amt)), |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2733 | U->getType()); |
| 2734 | } |
| 2735 | break; |
| 2736 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2737 | case Instruction::Trunc: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2738 | return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2739 | |
| 2740 | case Instruction::ZExt: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2741 | return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2742 | |
| 2743 | case Instruction::SExt: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2744 | return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2745 | |
| 2746 | case Instruction::BitCast: |
| 2747 | // BitCasts are no-op casts so we just eliminate the cast. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2748 | if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2749 | return getSCEV(U->getOperand(0)); |
| 2750 | break; |
| 2751 | |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2752 | case Instruction::IntToPtr: |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2753 | if (!TD) break; // Without TD we can't analyze pointers. |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2754 | return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)), |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2755 | TD->getIntPtrType()); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2756 | |
| 2757 | case Instruction::PtrToInt: |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2758 | if (!TD) break; // Without TD we can't analyze pointers. |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2759 | return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)), |
| 2760 | U->getType()); |
| 2761 | |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2762 | case Instruction::GetElementPtr: |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2763 | if (!TD) break; // Without TD we can't analyze pointers. |
Dan Gohman | ca5a39e | 2009-05-08 20:58:38 +0000 | [diff] [blame] | 2764 | return createNodeForGEP(U); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2765 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2766 | case Instruction::PHI: |
| 2767 | return createNodeForPHI(cast<PHINode>(U)); |
| 2768 | |
| 2769 | case Instruction::Select: |
| 2770 | // This could be a smax or umax that was lowered earlier. |
| 2771 | // Try to recover it. |
| 2772 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) { |
| 2773 | Value *LHS = ICI->getOperand(0); |
| 2774 | Value *RHS = ICI->getOperand(1); |
| 2775 | switch (ICI->getPredicate()) { |
| 2776 | case ICmpInst::ICMP_SLT: |
| 2777 | case ICmpInst::ICMP_SLE: |
| 2778 | std::swap(LHS, RHS); |
| 2779 | // fall through |
| 2780 | case ICmpInst::ICMP_SGT: |
| 2781 | case ICmpInst::ICMP_SGE: |
| 2782 | if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2783 | return getSMaxExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2784 | else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) |
Dan Gohman | d01fff8 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2785 | return getSMinExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2786 | break; |
| 2787 | case ICmpInst::ICMP_ULT: |
| 2788 | case ICmpInst::ICMP_ULE: |
| 2789 | std::swap(LHS, RHS); |
| 2790 | // fall through |
| 2791 | case ICmpInst::ICMP_UGT: |
| 2792 | case ICmpInst::ICMP_UGE: |
| 2793 | if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2794 | return getUMaxExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2795 | else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) |
Dan Gohman | d01fff8 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2796 | return getUMinExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2797 | break; |
Dan Gohman | f27dc69 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 2798 | case ICmpInst::ICMP_NE: |
| 2799 | // n != 0 ? n : 1 -> umax(n, 1) |
| 2800 | if (LHS == U->getOperand(1) && |
| 2801 | isa<ConstantInt>(U->getOperand(2)) && |
| 2802 | cast<ConstantInt>(U->getOperand(2))->isOne() && |
| 2803 | isa<ConstantInt>(RHS) && |
| 2804 | cast<ConstantInt>(RHS)->isZero()) |
| 2805 | return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(2))); |
| 2806 | break; |
| 2807 | case ICmpInst::ICMP_EQ: |
| 2808 | // n == 0 ? 1 : n -> umax(n, 1) |
| 2809 | if (LHS == U->getOperand(2) && |
| 2810 | isa<ConstantInt>(U->getOperand(1)) && |
| 2811 | cast<ConstantInt>(U->getOperand(1))->isOne() && |
| 2812 | isa<ConstantInt>(RHS) && |
| 2813 | cast<ConstantInt>(RHS)->isZero()) |
| 2814 | return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(1))); |
| 2815 | break; |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2816 | default: |
| 2817 | break; |
| 2818 | } |
| 2819 | } |
| 2820 | |
| 2821 | default: // We cannot analyze this expression. |
| 2822 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2825 | return getUnknown(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | |
| 2829 | |
| 2830 | //===----------------------------------------------------------------------===// |
| 2831 | // Iteration Count Computation Code |
| 2832 | // |
| 2833 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2834 | /// getBackedgeTakenCount - If the specified loop has a predictable |
| 2835 | /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute |
| 2836 | /// object. The backedge-taken count is the number of times the loop header |
| 2837 | /// will be branched to from within the loop. This is one less than the |
| 2838 | /// trip count of the loop, since it doesn't count the first iteration, |
| 2839 | /// when the header is branched to from outside the loop. |
| 2840 | /// |
| 2841 | /// Note that it is not valid to call this method on a loop without a |
| 2842 | /// loop-invariant backedge-taken count (see |
| 2843 | /// hasLoopInvariantBackedgeTakenCount). |
| 2844 | /// |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2845 | const SCEV* ScalarEvolution::getBackedgeTakenCount(const Loop *L) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2846 | return getBackedgeTakenInfo(L).Exact; |
| 2847 | } |
| 2848 | |
| 2849 | /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except |
| 2850 | /// return the least SCEV value that is known never to be less than the |
| 2851 | /// actual backedge taken count. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2852 | const SCEV* ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2853 | return getBackedgeTakenInfo(L).Max; |
| 2854 | } |
| 2855 | |
| 2856 | const ScalarEvolution::BackedgeTakenInfo & |
| 2857 | ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2858 | // Initially insert a CouldNotCompute for this loop. If the insertion |
| 2859 | // succeeds, procede to actually compute a backedge-taken count and |
| 2860 | // update the value. The temporary CouldNotCompute value tells SCEV |
| 2861 | // code elsewhere that it shouldn't attempt to request a new |
| 2862 | // backedge-taken count, which could result in infinite recursion. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2863 | std::pair<std::map<const Loop*, BackedgeTakenInfo>::iterator, bool> Pair = |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2864 | BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute())); |
| 2865 | if (Pair.second) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2866 | BackedgeTakenInfo ItCount = ComputeBackedgeTakenCount(L); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2867 | if (ItCount.Exact != getCouldNotCompute()) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2868 | assert(ItCount.Exact->isLoopInvariant(L) && |
| 2869 | ItCount.Max->isLoopInvariant(L) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2870 | "Computed trip count isn't loop invariant for loop!"); |
| 2871 | ++NumTripCountsComputed; |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2872 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2873 | // Update the value in the map. |
| 2874 | Pair.first->second = ItCount; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2875 | } else { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2876 | if (ItCount.Max != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2877 | // Update the value in the map. |
| 2878 | Pair.first->second = ItCount; |
| 2879 | if (isa<PHINode>(L->getHeader()->begin())) |
| 2880 | // Only count loops that have phi nodes as not being computable. |
| 2881 | ++NumTripCountsNotComputed; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2882 | } |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2883 | |
| 2884 | // Now that we know more about the trip count for this loop, forget any |
| 2885 | // existing SCEV values for PHI nodes in this loop since they are only |
| 2886 | // conservative estimates made without the benefit |
| 2887 | // of trip count information. |
| 2888 | if (ItCount.hasAnyInfo()) |
Dan Gohman | 9462302 | 2009-05-02 17:43:35 +0000 | [diff] [blame] | 2889 | forgetLoopPHIs(L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2890 | } |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2891 | return Pair.first->second; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2894 | /// forgetLoopBackedgeTakenCount - This method should be called by the |
Dan Gohman | f3a060a | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 2895 | /// client when it has changed a loop in a way that may effect |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2896 | /// ScalarEvolution's ability to compute a trip count, or if the loop |
| 2897 | /// is deleted. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2898 | void ScalarEvolution::forgetLoopBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2899 | BackedgeTakenCounts.erase(L); |
Dan Gohman | 9462302 | 2009-05-02 17:43:35 +0000 | [diff] [blame] | 2900 | forgetLoopPHIs(L); |
| 2901 | } |
| 2902 | |
| 2903 | /// forgetLoopPHIs - Delete the memoized SCEVs associated with the |
| 2904 | /// PHI nodes in the given loop. This is used when the trip count of |
| 2905 | /// the loop may have changed. |
| 2906 | void ScalarEvolution::forgetLoopPHIs(const Loop *L) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2907 | BasicBlock *Header = L->getHeader(); |
| 2908 | |
Dan Gohman | 9fd4a00 | 2009-05-12 01:27:58 +0000 | [diff] [blame] | 2909 | // Push all Loop-header PHIs onto the Worklist stack, except those |
| 2910 | // that are presently represented via a SCEVUnknown. SCEVUnknown for |
| 2911 | // a PHI either means that it has an unrecognized structure, or it's |
| 2912 | // a PHI that's in the progress of being computed by createNodeForPHI. |
| 2913 | // In the former case, additional loop trip count information isn't |
| 2914 | // going to change anything. In the later case, createNodeForPHI will |
| 2915 | // perform the necessary updates on its own when it gets to that point. |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2916 | SmallVector<Instruction *, 16> Worklist; |
| 2917 | for (BasicBlock::iterator I = Header->begin(); |
Dan Gohman | 9fd4a00 | 2009-05-12 01:27:58 +0000 | [diff] [blame] | 2918 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2919 | std::map<SCEVCallbackVH, const SCEV*>::iterator It = |
| 2920 | Scalars.find((Value*)I); |
Dan Gohman | 9fd4a00 | 2009-05-12 01:27:58 +0000 | [diff] [blame] | 2921 | if (It != Scalars.end() && !isa<SCEVUnknown>(It->second)) |
| 2922 | Worklist.push_back(PN); |
| 2923 | } |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2924 | |
| 2925 | while (!Worklist.empty()) { |
| 2926 | Instruction *I = Worklist.pop_back_val(); |
| 2927 | if (Scalars.erase(I)) |
| 2928 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 2929 | UI != UE; ++UI) |
| 2930 | Worklist.push_back(cast<Instruction>(UI)); |
| 2931 | } |
Dan Gohman | f3a060a | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2934 | /// ComputeBackedgeTakenCount - Compute the number of times the backedge |
| 2935 | /// of the specified loop will execute. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2936 | ScalarEvolution::BackedgeTakenInfo |
| 2937 | ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2938 | SmallVector<BasicBlock*, 8> ExitingBlocks; |
| 2939 | L->getExitingBlocks(ExitingBlocks); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2940 | |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2941 | // Examine all exits and pick the most conservative values. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2942 | const SCEV* BECount = getCouldNotCompute(); |
| 2943 | const SCEV* MaxBECount = getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2944 | bool CouldNotComputeBECount = false; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2945 | for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { |
| 2946 | BackedgeTakenInfo NewBTI = |
| 2947 | ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2948 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2949 | if (NewBTI.Exact == getCouldNotCompute()) { |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2950 | // We couldn't compute an exact value for this exit, so |
Dan Gohman | c6e8c83 | 2009-06-22 21:10:22 +0000 | [diff] [blame] | 2951 | // we won't be able to compute an exact value for the loop. |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2952 | CouldNotComputeBECount = true; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2953 | BECount = getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2954 | } else if (!CouldNotComputeBECount) { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2955 | if (BECount == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2956 | BECount = NewBTI.Exact; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2957 | else |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 2958 | BECount = getUMinFromMismatchedTypes(BECount, NewBTI.Exact); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2959 | } |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2960 | if (MaxBECount == getCouldNotCompute()) |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 2961 | MaxBECount = NewBTI.Max; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2962 | else if (NewBTI.Max != getCouldNotCompute()) |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 2963 | MaxBECount = getUMinFromMismatchedTypes(MaxBECount, NewBTI.Max); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 2967 | } |
| 2968 | |
| 2969 | /// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge |
| 2970 | /// of the specified loop will execute if it exits via the specified block. |
| 2971 | ScalarEvolution::BackedgeTakenInfo |
| 2972 | ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L, |
| 2973 | BasicBlock *ExitingBlock) { |
| 2974 | |
| 2975 | // Okay, we've chosen an exiting block. See what condition causes us to |
| 2976 | // exit at this block. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2977 | // |
| 2978 | // FIXME: we should be able to handle switch instructions (with a single exit) |
| 2979 | BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2980 | if (ExitBr == 0) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2981 | assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!"); |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2982 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2983 | // At this point, we know we have a conditional branch that determines whether |
| 2984 | // the loop is exited. However, we don't know if the branch is executed each |
| 2985 | // time through the loop. If not, then the execution count of the branch will |
| 2986 | // not be equal to the trip count of the loop. |
| 2987 | // |
| 2988 | // Currently we check for this by checking to see if the Exit branch goes to |
| 2989 | // the loop header. If so, we know it will always execute the same number of |
| 2990 | // times as the loop. We also handle the case where the exit block *is* the |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2991 | // loop header. This is common for un-rotated loops. |
| 2992 | // |
| 2993 | // If both of those tests fail, walk up the unique predecessor chain to the |
| 2994 | // header, stopping if there is an edge that doesn't exit the loop. If the |
| 2995 | // header is reached, the execution count of the branch will be equal to the |
| 2996 | // trip count of the loop. |
| 2997 | // |
| 2998 | // More extensive analysis could be done to handle more cases here. |
| 2999 | // |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3000 | if (ExitBr->getSuccessor(0) != L->getHeader() && |
| 3001 | ExitBr->getSuccessor(1) != L->getHeader() && |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3002 | ExitBr->getParent() != L->getHeader()) { |
| 3003 | // The simple checks failed, try climbing the unique predecessor chain |
| 3004 | // up to the header. |
| 3005 | bool Ok = false; |
| 3006 | for (BasicBlock *BB = ExitBr->getParent(); BB; ) { |
| 3007 | BasicBlock *Pred = BB->getUniquePredecessor(); |
| 3008 | if (!Pred) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3009 | return getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3010 | TerminatorInst *PredTerm = Pred->getTerminator(); |
| 3011 | for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) { |
| 3012 | BasicBlock *PredSucc = PredTerm->getSuccessor(i); |
| 3013 | if (PredSucc == BB) |
| 3014 | continue; |
| 3015 | // If the predecessor has a successor that isn't BB and isn't |
| 3016 | // outside the loop, assume the worst. |
| 3017 | if (L->contains(PredSucc)) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3018 | return getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3019 | } |
| 3020 | if (Pred == L->getHeader()) { |
| 3021 | Ok = true; |
| 3022 | break; |
| 3023 | } |
| 3024 | BB = Pred; |
| 3025 | } |
| 3026 | if (!Ok) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3027 | return getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
| 3030 | // Procede to the next level to examine the exit condition expression. |
| 3031 | return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(), |
| 3032 | ExitBr->getSuccessor(0), |
| 3033 | ExitBr->getSuccessor(1)); |
| 3034 | } |
| 3035 | |
| 3036 | /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the |
| 3037 | /// backedge of the specified loop will execute if its exit condition |
| 3038 | /// were a conditional branch of ExitCond, TBB, and FBB. |
| 3039 | ScalarEvolution::BackedgeTakenInfo |
| 3040 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L, |
| 3041 | Value *ExitCond, |
| 3042 | BasicBlock *TBB, |
| 3043 | BasicBlock *FBB) { |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3044 | // Check if the controlling expression for this loop is an And or Or. |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3045 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) { |
| 3046 | if (BO->getOpcode() == Instruction::And) { |
| 3047 | // Recurse on the operands of the and. |
| 3048 | BackedgeTakenInfo BTI0 = |
| 3049 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 3050 | BackedgeTakenInfo BTI1 = |
| 3051 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3052 | const SCEV* BECount = getCouldNotCompute(); |
| 3053 | const SCEV* MaxBECount = getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3054 | if (L->contains(TBB)) { |
| 3055 | // Both conditions must be true for the loop to continue executing. |
| 3056 | // Choose the less conservative count. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3057 | if (BTI0.Exact == getCouldNotCompute() || |
| 3058 | BTI1.Exact == getCouldNotCompute()) |
| 3059 | BECount = getCouldNotCompute(); |
Dan Gohman | ac958b3 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3060 | else |
| 3061 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3062 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3063 | MaxBECount = BTI1.Max; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3064 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3065 | MaxBECount = BTI0.Max; |
Dan Gohman | ac958b3 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3066 | else |
| 3067 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3068 | } else { |
| 3069 | // Both conditions must be true for the loop to exit. |
| 3070 | assert(L->contains(FBB) && "Loop block has no successor in loop!"); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3071 | if (BTI0.Exact != getCouldNotCompute() && |
| 3072 | BTI1.Exact != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3073 | BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3074 | if (BTI0.Max != getCouldNotCompute() && |
| 3075 | BTI1.Max != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3076 | MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max); |
| 3077 | } |
| 3078 | |
| 3079 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3080 | } |
| 3081 | if (BO->getOpcode() == Instruction::Or) { |
| 3082 | // Recurse on the operands of the or. |
| 3083 | BackedgeTakenInfo BTI0 = |
| 3084 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 3085 | BackedgeTakenInfo BTI1 = |
| 3086 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3087 | const SCEV* BECount = getCouldNotCompute(); |
| 3088 | const SCEV* MaxBECount = getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3089 | if (L->contains(FBB)) { |
| 3090 | // Both conditions must be false for the loop to continue executing. |
| 3091 | // Choose the less conservative count. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3092 | if (BTI0.Exact == getCouldNotCompute() || |
| 3093 | BTI1.Exact == getCouldNotCompute()) |
| 3094 | BECount = getCouldNotCompute(); |
Dan Gohman | ac958b3 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3095 | else |
| 3096 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3097 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3098 | MaxBECount = BTI1.Max; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3099 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3100 | MaxBECount = BTI0.Max; |
Dan Gohman | ac958b3 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3101 | else |
| 3102 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3103 | } else { |
| 3104 | // Both conditions must be false for the loop to exit. |
| 3105 | assert(L->contains(TBB) && "Loop block has no successor in loop!"); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3106 | if (BTI0.Exact != getCouldNotCompute() && |
| 3107 | BTI1.Exact != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3108 | BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3109 | if (BTI0.Max != getCouldNotCompute() && |
| 3110 | BTI1.Max != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3111 | MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max); |
| 3112 | } |
| 3113 | |
| 3114 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3115 | } |
| 3116 | } |
| 3117 | |
| 3118 | // With an icmp, it may be feasible to compute an exact backedge-taken count. |
| 3119 | // Procede to the next level to examine the icmp. |
| 3120 | if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) |
| 3121 | return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3122 | |
Eli Friedman | 459d729 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3123 | // If it's not an integer or pointer comparison then compute it the hard way. |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3124 | return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
| 3125 | } |
| 3126 | |
| 3127 | /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the |
| 3128 | /// backedge of the specified loop will execute if its exit condition |
| 3129 | /// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB. |
| 3130 | ScalarEvolution::BackedgeTakenInfo |
| 3131 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L, |
| 3132 | ICmpInst *ExitCond, |
| 3133 | BasicBlock *TBB, |
| 3134 | BasicBlock *FBB) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3135 | |
| 3136 | // If the condition was exit on true, convert the condition to exit on false |
| 3137 | ICmpInst::Predicate Cond; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3138 | if (!L->contains(FBB)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3139 | Cond = ExitCond->getPredicate(); |
| 3140 | else |
| 3141 | Cond = ExitCond->getInversePredicate(); |
| 3142 | |
| 3143 | // Handle common loops like: for (X = "string"; *X; ++X) |
| 3144 | if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0))) |
| 3145 | if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3146 | const SCEV* ItCnt = |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3147 | ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3148 | if (!isa<SCEVCouldNotCompute>(ItCnt)) { |
| 3149 | unsigned BitWidth = getTypeSizeInBits(ItCnt->getType()); |
| 3150 | return BackedgeTakenInfo(ItCnt, |
| 3151 | isa<SCEVConstant>(ItCnt) ? ItCnt : |
| 3152 | getConstant(APInt::getMaxValue(BitWidth)-1)); |
| 3153 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3156 | const SCEV* LHS = getSCEV(ExitCond->getOperand(0)); |
| 3157 | const SCEV* RHS = getSCEV(ExitCond->getOperand(1)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3158 | |
| 3159 | // Try to evaluate any dependencies out of the loop. |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3160 | LHS = getSCEVAtScope(LHS, L); |
| 3161 | RHS = getSCEVAtScope(RHS, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3162 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3163 | // At this point, we would like to compute how many iterations of the |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3164 | // loop the predicate will return true for these inputs. |
Dan Gohman | 2d96e35 | 2008-09-16 18:52:57 +0000 | [diff] [blame] | 3165 | if (LHS->isLoopInvariant(L) && !RHS->isLoopInvariant(L)) { |
| 3166 | // If there is a loop-invariant, force it into the RHS. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3167 | std::swap(LHS, RHS); |
| 3168 | Cond = ICmpInst::getSwappedPredicate(Cond); |
| 3169 | } |
| 3170 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3171 | // If we have a comparison of a chrec against a constant, try to use value |
| 3172 | // ranges to answer this query. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3173 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) |
| 3174 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3175 | if (AddRec->getLoop() == L) { |
Eli Friedman | 459d729 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3176 | // Form the constant range. |
| 3177 | ConstantRange CompRange( |
| 3178 | ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue())); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3179 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3180 | const SCEV* Ret = AddRec->getNumIterationsInRange(CompRange, *this); |
Eli Friedman | 459d729 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3181 | if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | switch (Cond) { |
| 3185 | case ICmpInst::ICMP_NE: { // while (X != Y) |
| 3186 | // Convert to: while (X-Y != 0) |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3187 | const SCEV* TC = HowFarToZero(getMinusSCEV(LHS, RHS), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3188 | if (!isa<SCEVCouldNotCompute>(TC)) return TC; |
| 3189 | break; |
| 3190 | } |
| 3191 | case ICmpInst::ICMP_EQ: { |
| 3192 | // Convert to: while (X-Y == 0) // while (X == Y) |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3193 | const SCEV* TC = HowFarToNonZero(getMinusSCEV(LHS, RHS), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3194 | if (!isa<SCEVCouldNotCompute>(TC)) return TC; |
| 3195 | break; |
| 3196 | } |
| 3197 | case ICmpInst::ICMP_SLT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3198 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true); |
| 3199 | if (BTI.hasAnyInfo()) return BTI; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3200 | break; |
| 3201 | } |
| 3202 | case ICmpInst::ICMP_SGT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3203 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 3204 | getNotSCEV(RHS), L, true); |
| 3205 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | b7c2894 | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 3206 | break; |
| 3207 | } |
| 3208 | case ICmpInst::ICMP_ULT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3209 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false); |
| 3210 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | b7c2894 | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 3211 | break; |
| 3212 | } |
| 3213 | case ICmpInst::ICMP_UGT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3214 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 3215 | getNotSCEV(RHS), L, false); |
| 3216 | if (BTI.hasAnyInfo()) return BTI; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3217 | break; |
| 3218 | } |
| 3219 | default: |
| 3220 | #if 0 |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3221 | errs() << "ComputeBackedgeTakenCount "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3222 | if (ExitCond->getOperand(0)->getType()->isUnsigned()) |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3223 | errs() << "[unsigned] "; |
| 3224 | errs() << *LHS << " " |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3225 | << Instruction::getOpcodeName(Instruction::ICmp) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3226 | << " " << *RHS << "\n"; |
| 3227 | #endif |
| 3228 | break; |
| 3229 | } |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3230 | return |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3231 | ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
| 3234 | static ConstantInt * |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3235 | EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, |
| 3236 | ScalarEvolution &SE) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3237 | const SCEV* InVal = SE.getConstant(C); |
| 3238 | const SCEV* Val = AddRec->evaluateAtIteration(InVal, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3239 | assert(isa<SCEVConstant>(Val) && |
| 3240 | "Evaluation of SCEV at constant didn't fold correctly?"); |
| 3241 | return cast<SCEVConstant>(Val)->getValue(); |
| 3242 | } |
| 3243 | |
| 3244 | /// GetAddressedElementFromGlobal - Given a global variable with an initializer |
| 3245 | /// and a GEP expression (missing the pointer index) indexing into it, return |
| 3246 | /// the addressed element of the initializer or null if the index expression is |
| 3247 | /// invalid. |
| 3248 | static Constant * |
| 3249 | GetAddressedElementFromGlobal(GlobalVariable *GV, |
| 3250 | const std::vector<ConstantInt*> &Indices) { |
| 3251 | Constant *Init = GV->getInitializer(); |
| 3252 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) { |
| 3253 | uint64_t Idx = Indices[i]->getZExtValue(); |
| 3254 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) { |
| 3255 | assert(Idx < CS->getNumOperands() && "Bad struct index!"); |
| 3256 | Init = cast<Constant>(CS->getOperand(Idx)); |
| 3257 | } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) { |
| 3258 | if (Idx >= CA->getNumOperands()) return 0; // Bogus program |
| 3259 | Init = cast<Constant>(CA->getOperand(Idx)); |
| 3260 | } else if (isa<ConstantAggregateZero>(Init)) { |
| 3261 | if (const StructType *STy = dyn_cast<StructType>(Init->getType())) { |
| 3262 | assert(Idx < STy->getNumElements() && "Bad struct index!"); |
| 3263 | Init = Constant::getNullValue(STy->getElementType(Idx)); |
| 3264 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) { |
| 3265 | if (Idx >= ATy->getNumElements()) return 0; // Bogus program |
| 3266 | Init = Constant::getNullValue(ATy->getElementType()); |
| 3267 | } else { |
| 3268 | assert(0 && "Unknown constant aggregate type!"); |
| 3269 | } |
| 3270 | return 0; |
| 3271 | } else { |
| 3272 | return 0; // Unknown initializer type |
| 3273 | } |
| 3274 | } |
| 3275 | return Init; |
| 3276 | } |
| 3277 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3278 | /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of |
| 3279 | /// 'icmp op load X, cst', try to see if we can compute the backedge |
| 3280 | /// execution count. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3281 | const SCEV * |
| 3282 | ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount( |
| 3283 | LoadInst *LI, |
| 3284 | Constant *RHS, |
| 3285 | const Loop *L, |
| 3286 | ICmpInst::Predicate predicate) { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3287 | if (LI->isVolatile()) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3288 | |
| 3289 | // Check to see if the loaded pointer is a getelementptr of a global. |
| 3290 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3291 | if (!GEP) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3292 | |
| 3293 | // Make sure that it is really a constant global we are gepping, with an |
| 3294 | // initializer, and make sure the first IDX is really 0. |
| 3295 | GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); |
| 3296 | if (!GV || !GV->isConstant() || !GV->hasInitializer() || |
| 3297 | GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) || |
| 3298 | !cast<Constant>(GEP->getOperand(1))->isNullValue()) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3299 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3300 | |
| 3301 | // Okay, we allow one non-constant index into the GEP instruction. |
| 3302 | Value *VarIdx = 0; |
| 3303 | std::vector<ConstantInt*> Indexes; |
| 3304 | unsigned VarIdxNum = 0; |
| 3305 | for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) |
| 3306 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 3307 | Indexes.push_back(CI); |
| 3308 | } else if (!isa<ConstantInt>(GEP->getOperand(i))) { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3309 | if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3310 | VarIdx = GEP->getOperand(i); |
| 3311 | VarIdxNum = i-2; |
| 3312 | Indexes.push_back(0); |
| 3313 | } |
| 3314 | |
| 3315 | // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant. |
| 3316 | // Check to see if X is a loop variant variable value now. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3317 | const SCEV* Idx = getSCEV(VarIdx); |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3318 | Idx = getSCEVAtScope(Idx, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3319 | |
| 3320 | // We can only recognize very limited forms of loop index expressions, in |
| 3321 | // particular, only affine AddRec's like {C1,+,C2}. |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3322 | const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3323 | if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) || |
| 3324 | !isa<SCEVConstant>(IdxExpr->getOperand(0)) || |
| 3325 | !isa<SCEVConstant>(IdxExpr->getOperand(1))) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3326 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3327 | |
| 3328 | unsigned MaxSteps = MaxBruteForceIterations; |
| 3329 | for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) { |
| 3330 | ConstantInt *ItCst = |
Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 3331 | ConstantInt::get(cast<IntegerType>(IdxExpr->getType()), IterationNum); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3332 | ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3333 | |
| 3334 | // Form the GEP offset. |
| 3335 | Indexes[VarIdxNum] = Val; |
| 3336 | |
| 3337 | Constant *Result = GetAddressedElementFromGlobal(GV, Indexes); |
| 3338 | if (Result == 0) break; // Cannot compute! |
| 3339 | |
| 3340 | // Evaluate the condition for this iteration. |
| 3341 | Result = ConstantExpr::getICmp(predicate, Result, RHS); |
| 3342 | if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure |
| 3343 | if (cast<ConstantInt>(Result)->getValue().isMinValue()) { |
| 3344 | #if 0 |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3345 | errs() << "\n***\n*** Computed loop count " << *ItCst |
| 3346 | << "\n*** From global " << *GV << "*** BB: " << *L->getHeader() |
| 3347 | << "***\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3348 | #endif |
| 3349 | ++NumArrayLenItCounts; |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3350 | return getConstant(ItCst); // Found terminating iteration! |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3351 | } |
| 3352 | } |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3353 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
| 3356 | |
| 3357 | /// CanConstantFold - Return true if we can constant fold an instruction of the |
| 3358 | /// specified type, assuming that all operands were constants. |
| 3359 | static bool CanConstantFold(const Instruction *I) { |
| 3360 | if (isa<BinaryOperator>(I) || isa<CmpInst>(I) || |
| 3361 | isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I)) |
| 3362 | return true; |
| 3363 | |
| 3364 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 3365 | if (const Function *F = CI->getCalledFunction()) |
Dan Gohman | e6e001f | 2008-01-31 01:05:10 +0000 | [diff] [blame] | 3366 | return canConstantFoldCallTo(F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3367 | return false; |
| 3368 | } |
| 3369 | |
| 3370 | /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node |
| 3371 | /// in the loop that V is derived from. We allow arbitrary operations along the |
| 3372 | /// way, but the operands of an operation must either be constants or a value |
| 3373 | /// derived from a constant PHI. If this expression does not fit with these |
| 3374 | /// constraints, return null. |
| 3375 | static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) { |
| 3376 | // If this is not an instruction, or if this is an instruction outside of the |
| 3377 | // loop, it can't be derived from a loop PHI. |
| 3378 | Instruction *I = dyn_cast<Instruction>(V); |
| 3379 | if (I == 0 || !L->contains(I->getParent())) return 0; |
| 3380 | |
Anton Korobeynikov | 357a27d | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 3381 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3382 | if (L->getHeader() == I->getParent()) |
| 3383 | return PN; |
| 3384 | else |
| 3385 | // We don't currently keep track of the control flow needed to evaluate |
| 3386 | // PHIs, so we cannot handle PHIs inside of loops. |
| 3387 | return 0; |
Anton Korobeynikov | 357a27d | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 3388 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3389 | |
| 3390 | // If we won't be able to constant fold this expression even if the operands |
| 3391 | // are constants, return early. |
| 3392 | if (!CanConstantFold(I)) return 0; |
| 3393 | |
| 3394 | // Otherwise, we can evaluate this instruction if all of its operands are |
| 3395 | // constant or derived from a PHI node themselves. |
| 3396 | PHINode *PHI = 0; |
| 3397 | for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op) |
| 3398 | if (!(isa<Constant>(I->getOperand(Op)) || |
| 3399 | isa<GlobalValue>(I->getOperand(Op)))) { |
| 3400 | PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L); |
| 3401 | if (P == 0) return 0; // Not evolving from PHI |
| 3402 | if (PHI == 0) |
| 3403 | PHI = P; |
| 3404 | else if (PHI != P) |
| 3405 | return 0; // Evolving from multiple different PHIs. |
| 3406 | } |
| 3407 | |
| 3408 | // This is a expression evolving from a constant PHI! |
| 3409 | return PHI; |
| 3410 | } |
| 3411 | |
| 3412 | /// EvaluateExpression - Given an expression that passes the |
| 3413 | /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node |
| 3414 | /// in the loop has the value PHIVal. If we can't fold this expression for some |
| 3415 | /// reason, return null. |
| 3416 | static Constant *EvaluateExpression(Value *V, Constant *PHIVal) { |
| 3417 | if (isa<PHINode>(V)) return PHIVal; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3418 | if (Constant *C = dyn_cast<Constant>(V)) return C; |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3419 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3420 | Instruction *I = cast<Instruction>(V); |
| 3421 | |
| 3422 | std::vector<Constant*> Operands; |
| 3423 | Operands.resize(I->getNumOperands()); |
| 3424 | |
| 3425 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 3426 | Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal); |
| 3427 | if (Operands[i] == 0) return 0; |
| 3428 | } |
| 3429 | |
Chris Lattner | d6e5691 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3430 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 3431 | return ConstantFoldCompareInstOperands(CI->getPredicate(), |
| 3432 | &Operands[0], Operands.size()); |
| 3433 | else |
| 3434 | return ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
| 3435 | &Operands[0], Operands.size()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is |
| 3439 | /// in the header of its containing loop, we know the loop executes a |
| 3440 | /// constant number of times, and the PHI node is just a recurrence |
| 3441 | /// involving constants, fold it. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3442 | Constant * |
| 3443 | ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN, |
| 3444 | const APInt& BEs, |
| 3445 | const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3446 | std::map<PHINode*, Constant*>::iterator I = |
| 3447 | ConstantEvolutionLoopExitValue.find(PN); |
| 3448 | if (I != ConstantEvolutionLoopExitValue.end()) |
| 3449 | return I->second; |
| 3450 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3451 | if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations))) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3452 | return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it. |
| 3453 | |
| 3454 | Constant *&RetVal = ConstantEvolutionLoopExitValue[PN]; |
| 3455 | |
| 3456 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 3457 | // entry must be a constant (coming in from outside of the loop), and the |
| 3458 | // second must be derived from the same PHI. |
| 3459 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 3460 | Constant *StartCST = |
| 3461 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
| 3462 | if (StartCST == 0) |
| 3463 | return RetVal = 0; // Must be a constant. |
| 3464 | |
| 3465 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
| 3466 | PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); |
| 3467 | if (PN2 != PN) |
| 3468 | return RetVal = 0; // Not derived from same PHI. |
| 3469 | |
| 3470 | // Execute the loop symbolically to determine the exit value. |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3471 | if (BEs.getActiveBits() >= 32) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3472 | return RetVal = 0; // More than 2^32-1 iterations?? Not doing it! |
| 3473 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3474 | unsigned NumIterations = BEs.getZExtValue(); // must be in range |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3475 | unsigned IterationNum = 0; |
| 3476 | for (Constant *PHIVal = StartCST; ; ++IterationNum) { |
| 3477 | if (IterationNum == NumIterations) |
| 3478 | return RetVal = PHIVal; // Got exit value! |
| 3479 | |
| 3480 | // Compute the value of the PHI node for the next iteration. |
| 3481 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal); |
| 3482 | if (NextPHI == PHIVal) |
| 3483 | return RetVal = NextPHI; // Stopped evolving! |
| 3484 | if (NextPHI == 0) |
| 3485 | return 0; // Couldn't evaluate! |
| 3486 | PHIVal = NextPHI; |
| 3487 | } |
| 3488 | } |
| 3489 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3490 | /// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute a |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3491 | /// constant number of times (the condition evolves only from constants), |
| 3492 | /// try to evaluate a few iterations of the loop until we get the exit |
| 3493 | /// condition gets a value of ExitWhen (true or false). If we cannot |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3494 | /// evaluate the trip count of the loop, return getCouldNotCompute(). |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3495 | const SCEV * |
| 3496 | ScalarEvolution::ComputeBackedgeTakenCountExhaustively(const Loop *L, |
| 3497 | Value *Cond, |
| 3498 | bool ExitWhen) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3499 | PHINode *PN = getConstantEvolvingPHI(Cond, L); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3500 | if (PN == 0) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3501 | |
| 3502 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 3503 | // entry must be a constant (coming in from outside of the loop), and the |
| 3504 | // second must be derived from the same PHI. |
| 3505 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 3506 | Constant *StartCST = |
| 3507 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3508 | if (StartCST == 0) return getCouldNotCompute(); // Must be a constant. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3509 | |
| 3510 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
| 3511 | PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3512 | if (PN2 != PN) return getCouldNotCompute(); // Not derived from same PHI. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3513 | |
| 3514 | // Okay, we find a PHI node that defines the trip count of this loop. Execute |
| 3515 | // the loop symbolically to determine when the condition gets a value of |
| 3516 | // "ExitWhen". |
| 3517 | unsigned IterationNum = 0; |
| 3518 | unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis. |
| 3519 | for (Constant *PHIVal = StartCST; |
| 3520 | IterationNum != MaxIterations; ++IterationNum) { |
| 3521 | ConstantInt *CondVal = |
| 3522 | dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal)); |
| 3523 | |
| 3524 | // Couldn't symbolically evaluate. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3525 | if (!CondVal) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3526 | |
| 3527 | if (CondVal->getValue() == uint64_t(ExitWhen)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3528 | ++NumBruteForceTripCountsComputed; |
Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 3529 | return getConstant(Type::Int32Ty, IterationNum); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | // Compute the value of the PHI node for the next iteration. |
| 3533 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal); |
| 3534 | if (NextPHI == 0 || NextPHI == PHIVal) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3535 | return getCouldNotCompute();// Couldn't evaluate or not making progress... |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3536 | PHIVal = NextPHI; |
| 3537 | } |
| 3538 | |
| 3539 | // Too many iterations were needed to evaluate. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3540 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
Dan Gohman | dd40e9a | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 3543 | /// getSCEVAtScope - Return a SCEV expression handle for the specified value |
| 3544 | /// at the specified scope in the program. The L value specifies a loop |
| 3545 | /// nest to evaluate the expression at, where null is the top-level or a |
| 3546 | /// specified loop is immediately inside of the loop. |
| 3547 | /// |
| 3548 | /// This method can be used to compute the exit value for a variable defined |
| 3549 | /// in a loop by querying what the value will hold in the parent loop. |
| 3550 | /// |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3551 | /// In the case that a relevant loop exit value cannot be computed, the |
| 3552 | /// original value V is returned. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3553 | const SCEV* ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3554 | // FIXME: this should be turned into a virtual method on SCEV! |
| 3555 | |
| 3556 | if (isa<SCEVConstant>(V)) return V; |
| 3557 | |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 3558 | // If this instruction is evolved from a constant-evolving PHI, compute the |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3559 | // exit value from the loop without using SCEVs. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3560 | if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3561 | if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3562 | const Loop *LI = (*this->LI)[I->getParent()]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3563 | if (LI && LI->getParentLoop() == L) // Looking for loop exit value. |
| 3564 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3565 | if (PN->getParent() == LI->getHeader()) { |
| 3566 | // Okay, there is no closed form solution for the PHI node. Check |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3567 | // to see if the loop that contains it has a known backedge-taken |
| 3568 | // count. If so, we may be able to force computation of the exit |
| 3569 | // value. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3570 | const SCEV* BackedgeTakenCount = getBackedgeTakenCount(LI); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3571 | if (const SCEVConstant *BTCC = |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3572 | dyn_cast<SCEVConstant>(BackedgeTakenCount)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3573 | // Okay, we know how many times the containing loop executes. If |
| 3574 | // this is a constant evolving PHI node, get the final value at |
| 3575 | // the specified iteration number. |
| 3576 | Constant *RV = getConstantEvolutionLoopExitValue(PN, |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3577 | BTCC->getValue()->getValue(), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3578 | LI); |
Dan Gohman | 652caf1 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3579 | if (RV) return getSCEV(RV); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3580 | } |
| 3581 | } |
| 3582 | |
| 3583 | // Okay, this is an expression that we cannot symbolically evaluate |
| 3584 | // into a SCEV. Check to see if it's possible to symbolically evaluate |
| 3585 | // the arguments into constants, and if so, try to constant propagate the |
| 3586 | // result. This is particularly useful for computing loop exit values. |
| 3587 | if (CanConstantFold(I)) { |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3588 | // Check to see if we've folded this instruction at this loop before. |
| 3589 | std::map<const Loop *, Constant *> &Values = ValuesAtScopes[I]; |
| 3590 | std::pair<std::map<const Loop *, Constant *>::iterator, bool> Pair = |
| 3591 | Values.insert(std::make_pair(L, static_cast<Constant *>(0))); |
| 3592 | if (!Pair.second) |
Dan Gohman | 652caf1 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3593 | return Pair.first->second ? &*getSCEV(Pair.first->second) : V; |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3594 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3595 | std::vector<Constant*> Operands; |
| 3596 | Operands.reserve(I->getNumOperands()); |
| 3597 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 3598 | Value *Op = I->getOperand(i); |
| 3599 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| 3600 | Operands.push_back(C); |
| 3601 | } else { |
Chris Lattner | 3fff464 | 2007-11-23 08:46:22 +0000 | [diff] [blame] | 3602 | // If any of the operands is non-constant and if they are |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3603 | // non-integer and non-pointer, don't even try to analyze them |
| 3604 | // with scev techniques. |
Dan Gohman | 5e4eb76 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3605 | if (!isSCEVable(Op->getType())) |
Chris Lattner | 3fff464 | 2007-11-23 08:46:22 +0000 | [diff] [blame] | 3606 | return V; |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3607 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3608 | const SCEV* OpV = getSCEVAtScope(getSCEV(Op), L); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3609 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) { |
Dan Gohman | 5e4eb76 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3610 | Constant *C = SC->getValue(); |
| 3611 | if (C->getType() != Op->getType()) |
| 3612 | C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 3613 | Op->getType(), |
| 3614 | false), |
| 3615 | C, Op->getType()); |
| 3616 | Operands.push_back(C); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3617 | } else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) { |
Dan Gohman | 5e4eb76 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3618 | if (Constant *C = dyn_cast<Constant>(SU->getValue())) { |
| 3619 | if (C->getType() != Op->getType()) |
| 3620 | C = |
| 3621 | ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 3622 | Op->getType(), |
| 3623 | false), |
| 3624 | C, Op->getType()); |
| 3625 | Operands.push_back(C); |
| 3626 | } else |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3627 | return V; |
| 3628 | } else { |
| 3629 | return V; |
| 3630 | } |
| 3631 | } |
| 3632 | } |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3633 | |
Chris Lattner | d6e5691 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3634 | Constant *C; |
| 3635 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 3636 | C = ConstantFoldCompareInstOperands(CI->getPredicate(), |
| 3637 | &Operands[0], Operands.size()); |
| 3638 | else |
| 3639 | C = ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
| 3640 | &Operands[0], Operands.size()); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3641 | Pair.first->second = C; |
Dan Gohman | 652caf1 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3642 | return getSCEV(C); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | // This is some other type of SCEVUnknown, just return it. |
| 3647 | return V; |
| 3648 | } |
| 3649 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3650 | if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3651 | // Avoid performing the look-up in the common case where the specified |
| 3652 | // expression has no loop-variant portions. |
| 3653 | for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3654 | const SCEV* OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3655 | if (OpAtScope != Comm->getOperand(i)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3656 | // Okay, at least one of these operands is loop variant but might be |
| 3657 | // foldable. Build a new instance of the folded commutative expression. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3658 | SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(), |
| 3659 | Comm->op_begin()+i); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3660 | NewOps.push_back(OpAtScope); |
| 3661 | |
| 3662 | for (++i; i != e; ++i) { |
| 3663 | OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3664 | NewOps.push_back(OpAtScope); |
| 3665 | } |
| 3666 | if (isa<SCEVAddExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3667 | return getAddExpr(NewOps); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 3668 | if (isa<SCEVMulExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3669 | return getMulExpr(NewOps); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 3670 | if (isa<SCEVSMaxExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3671 | return getSMaxExpr(NewOps); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 3672 | if (isa<SCEVUMaxExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3673 | return getUMaxExpr(NewOps); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 3674 | assert(0 && "Unknown commutative SCEV type!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3675 | } |
| 3676 | } |
| 3677 | // If we got here, all operands are loop invariant. |
| 3678 | return Comm; |
| 3679 | } |
| 3680 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3681 | if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3682 | const SCEV* LHS = getSCEVAtScope(Div->getLHS(), L); |
| 3683 | const SCEV* RHS = getSCEVAtScope(Div->getRHS(), L); |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 3684 | if (LHS == Div->getLHS() && RHS == Div->getRHS()) |
| 3685 | return Div; // must be loop invariant |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3686 | return getUDivExpr(LHS, RHS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3687 | } |
| 3688 | |
| 3689 | // If this is a loop recurrence for a loop that does not contain L, then we |
| 3690 | // are dealing with the final value computed by the loop. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3691 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3692 | if (!L || !AddRec->getLoop()->contains(L->getHeader())) { |
| 3693 | // To evaluate this recurrence, we need to know how many times the AddRec |
| 3694 | // loop iterates. Compute this now. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3695 | const SCEV* BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop()); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3696 | if (BackedgeTakenCount == getCouldNotCompute()) return AddRec; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3697 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 3698 | // Then, evaluate the AddRec. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3699 | return AddRec->evaluateAtIteration(BackedgeTakenCount, *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3700 | } |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3701 | return AddRec; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3704 | if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3705 | const SCEV* Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | 78d63c8 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 3706 | if (Op == Cast->getOperand()) |
| 3707 | return Cast; // must be loop invariant |
| 3708 | return getZeroExtendExpr(Op, Cast->getType()); |
| 3709 | } |
| 3710 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3711 | if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3712 | const SCEV* Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | 78d63c8 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 3713 | if (Op == Cast->getOperand()) |
| 3714 | return Cast; // must be loop invariant |
| 3715 | return getSignExtendExpr(Op, Cast->getType()); |
| 3716 | } |
| 3717 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3718 | if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3719 | const SCEV* Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | 78d63c8 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 3720 | if (Op == Cast->getOperand()) |
| 3721 | return Cast; // must be loop invariant |
| 3722 | return getTruncateExpr(Op, Cast->getType()); |
| 3723 | } |
| 3724 | |
| 3725 | assert(0 && "Unknown SCEV type!"); |
Daniel Dunbar | a95d96c | 2009-05-18 16:43:04 +0000 | [diff] [blame] | 3726 | return 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
Dan Gohman | dd40e9a | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 3729 | /// getSCEVAtScope - This is a convenience function which does |
| 3730 | /// getSCEVAtScope(getSCEV(V), L). |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3731 | const SCEV* ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3732 | return getSCEVAtScope(getSCEV(V), L); |
| 3733 | } |
| 3734 | |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3735 | /// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the |
| 3736 | /// following equation: |
| 3737 | /// |
| 3738 | /// A * X = B (mod N) |
| 3739 | /// |
| 3740 | /// where N = 2^BW and BW is the common bit width of A and B. The signedness of |
| 3741 | /// A and B isn't important. |
| 3742 | /// |
| 3743 | /// If the equation does not have a solution, SCEVCouldNotCompute is returned. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3744 | static const SCEV* SolveLinEquationWithOverflow(const APInt &A, const APInt &B, |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3745 | ScalarEvolution &SE) { |
| 3746 | uint32_t BW = A.getBitWidth(); |
| 3747 | assert(BW == B.getBitWidth() && "Bit widths must be the same."); |
| 3748 | assert(A != 0 && "A must be non-zero."); |
| 3749 | |
| 3750 | // 1. D = gcd(A, N) |
| 3751 | // |
| 3752 | // The gcd of A and N may have only one prime factor: 2. The number of |
| 3753 | // trailing zeros in A is its multiplicity |
| 3754 | uint32_t Mult2 = A.countTrailingZeros(); |
| 3755 | // D = 2^Mult2 |
| 3756 | |
| 3757 | // 2. Check if B is divisible by D. |
| 3758 | // |
| 3759 | // B is divisible by D if and only if the multiplicity of prime factor 2 for B |
| 3760 | // is not less than multiplicity of this prime factor for D. |
| 3761 | if (B.countTrailingZeros() < Mult2) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 3762 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3763 | |
| 3764 | // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic |
| 3765 | // modulo (N / D). |
| 3766 | // |
| 3767 | // (N / D) may need BW+1 bits in its representation. Hence, we'll use this |
| 3768 | // bit width during computations. |
| 3769 | APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D |
| 3770 | APInt Mod(BW + 1, 0); |
| 3771 | Mod.set(BW - Mult2); // Mod = N / D |
| 3772 | APInt I = AD.multiplicativeInverse(Mod); |
| 3773 | |
| 3774 | // 4. Compute the minimum unsigned root of the equation: |
| 3775 | // I * (B / D) mod (N / D) |
| 3776 | APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod); |
| 3777 | |
| 3778 | // The result is guaranteed to be less than 2^BW so we may truncate it to BW |
| 3779 | // bits. |
| 3780 | return SE.getConstant(Result.trunc(BW)); |
| 3781 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3782 | |
| 3783 | /// SolveQuadraticEquation - Find the roots of the quadratic equation for the |
| 3784 | /// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which |
| 3785 | /// might be the same) or two SCEVCouldNotCompute objects. |
| 3786 | /// |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3787 | static std::pair<const SCEV*,const SCEV*> |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3788 | SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3789 | assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3790 | const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0)); |
| 3791 | const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1)); |
| 3792 | const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3793 | |
| 3794 | // We currently can only solve this if the coefficients are constants. |
| 3795 | if (!LC || !MC || !NC) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3796 | const SCEV *CNC = SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3797 | return std::make_pair(CNC, CNC); |
| 3798 | } |
| 3799 | |
| 3800 | uint32_t BitWidth = LC->getValue()->getValue().getBitWidth(); |
| 3801 | const APInt &L = LC->getValue()->getValue(); |
| 3802 | const APInt &M = MC->getValue()->getValue(); |
| 3803 | const APInt &N = NC->getValue()->getValue(); |
| 3804 | APInt Two(BitWidth, 2); |
| 3805 | APInt Four(BitWidth, 4); |
| 3806 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3807 | { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3808 | using namespace APIntOps; |
| 3809 | const APInt& C = L; |
| 3810 | // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C |
| 3811 | // The B coefficient is M-N/2 |
| 3812 | APInt B(M); |
| 3813 | B -= sdiv(N,Two); |
| 3814 | |
| 3815 | // The A coefficient is N/2 |
| 3816 | APInt A(N.sdiv(Two)); |
| 3817 | |
| 3818 | // Compute the B^2-4ac term. |
| 3819 | APInt SqrtTerm(B); |
| 3820 | SqrtTerm *= B; |
| 3821 | SqrtTerm -= Four * (A * C); |
| 3822 | |
| 3823 | // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest |
| 3824 | // integer value or else APInt::sqrt() will assert. |
| 3825 | APInt SqrtVal(SqrtTerm.sqrt()); |
| 3826 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3827 | // Compute the two solutions for the quadratic formula. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3828 | // The divisions must be performed as signed divisions. |
| 3829 | APInt NegB(-B); |
| 3830 | APInt TwoA( A << 1 ); |
Nick Lewycky | 3577669 | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 3831 | if (TwoA.isMinValue()) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3832 | const SCEV *CNC = SE.getCouldNotCompute(); |
Nick Lewycky | 3577669 | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 3833 | return std::make_pair(CNC, CNC); |
| 3834 | } |
| 3835 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3836 | ConstantInt *Solution1 = ConstantInt::get((NegB + SqrtVal).sdiv(TwoA)); |
| 3837 | ConstantInt *Solution2 = ConstantInt::get((NegB - SqrtVal).sdiv(TwoA)); |
| 3838 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3839 | return std::make_pair(SE.getConstant(Solution1), |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3840 | SE.getConstant(Solution2)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3841 | } // end APIntOps namespace |
| 3842 | } |
| 3843 | |
| 3844 | /// HowFarToZero - Return the number of times a backedge comparing the specified |
Dan Gohman | 0c85091 | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 3845 | /// value to zero will execute. If not computable, return CouldNotCompute. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3846 | const SCEV* ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3847 | // If the value is a constant |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3848 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3849 | // If the value is already zero, the branch will execute zero times. |
| 3850 | if (C->getValue()->isZero()) return C; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3851 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3854 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3855 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3856 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3857 | |
| 3858 | if (AddRec->isAffine()) { |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3859 | // If this is an affine expression, the execution count of this branch is |
| 3860 | // the minimum unsigned root of the following equation: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3861 | // |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3862 | // Start + Step*N = 0 (mod 2^BW) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3863 | // |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3864 | // equivalent to: |
| 3865 | // |
| 3866 | // Step*N = -Start (mod 2^BW) |
| 3867 | // |
| 3868 | // where BW is the common bit width of Start and Step. |
| 3869 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3870 | // Get the initial value for the loop. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3871 | const SCEV *Start = getSCEVAtScope(AddRec->getStart(), |
| 3872 | L->getParentLoop()); |
| 3873 | const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), |
| 3874 | L->getParentLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3875 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3876 | if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) { |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3877 | // For now we handle only constant steps. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3878 | |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3879 | // First, handle unitary steps. |
| 3880 | if (StepC->getValue()->equalsInt(1)) // 1*N = -Start (mod 2^BW), so: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3881 | return getNegativeSCEV(Start); // N = -Start (as unsigned) |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3882 | if (StepC->getValue()->isAllOnesValue()) // -1*N = -Start (mod 2^BW), so: |
| 3883 | return Start; // N = Start (as unsigned) |
| 3884 | |
| 3885 | // Then, try to solve the above equation provided that Start is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3886 | if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3887 | return SolveLinEquationWithOverflow(StepC->getValue()->getValue(), |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3888 | -StartC->getValue()->getValue(), |
| 3889 | *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3890 | } |
| 3891 | } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) { |
| 3892 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of |
| 3893 | // the quadratic equation to solve it. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3894 | std::pair<const SCEV*,const SCEV*> Roots = SolveQuadraticEquation(AddRec, |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3895 | *this); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3896 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 3897 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3898 | if (R1) { |
| 3899 | #if 0 |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3900 | errs() << "HFTZ: " << *V << " - sol#1: " << *R1 |
| 3901 | << " sol#2: " << *R2 << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3902 | #endif |
| 3903 | // Pick the smallest positive root value. |
| 3904 | if (ConstantInt *CB = |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3905 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3906 | R1->getValue(), R2->getValue()))) { |
| 3907 | if (CB->getZExtValue() == false) |
| 3908 | std::swap(R1, R2); // R1 is the minimum root now. |
| 3909 | |
| 3910 | // We can only use this value if the chrec ends up with an exact zero |
| 3911 | // value at this index. When solving for "X*X != 5", for example, we |
| 3912 | // should not accept a root of 2. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3913 | const SCEV* Val = AddRec->evaluateAtIteration(R1, *this); |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 3914 | if (Val->isZero()) |
| 3915 | return R1; // We found a quadratic root! |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3916 | } |
| 3917 | } |
| 3918 | } |
| 3919 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3920 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3921 | } |
| 3922 | |
| 3923 | /// HowFarToNonZero - Return the number of times a backedge checking the |
| 3924 | /// specified value for nonzero will execute. If not computable, return |
Dan Gohman | 0c85091 | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 3925 | /// CouldNotCompute |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3926 | const SCEV* ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3927 | // Loops that look like: while (X == 0) are very strange indeed. We don't |
| 3928 | // handle them yet except for the trivial case. This could be expanded in the |
| 3929 | // future as needed. |
| 3930 | |
| 3931 | // If the value is a constant, check to see if it is known to be non-zero |
| 3932 | // already. If so, the backedge will execute zero times. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3933 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Nick Lewycky | f680518 | 2008-02-21 09:14:53 +0000 | [diff] [blame] | 3934 | if (!C->getValue()->isNullValue()) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3935 | return getIntegerSCEV(0, C->getType()); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3936 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3937 | } |
| 3938 | |
| 3939 | // We could implement others, but I really doubt anyone writes loops like |
| 3940 | // this, and if they did, they would already be constant folded. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3941 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3942 | } |
| 3943 | |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 3944 | /// getLoopPredecessor - If the given loop's header has exactly one unique |
| 3945 | /// predecessor outside the loop, return it. Otherwise return null. |
| 3946 | /// |
| 3947 | BasicBlock *ScalarEvolution::getLoopPredecessor(const Loop *L) { |
| 3948 | BasicBlock *Header = L->getHeader(); |
| 3949 | BasicBlock *Pred = 0; |
| 3950 | for (pred_iterator PI = pred_begin(Header), E = pred_end(Header); |
| 3951 | PI != E; ++PI) |
| 3952 | if (!L->contains(*PI)) { |
| 3953 | if (Pred && Pred != *PI) return 0; // Multiple predecessors. |
| 3954 | Pred = *PI; |
| 3955 | } |
| 3956 | return Pred; |
| 3957 | } |
| 3958 | |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 3959 | /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB |
| 3960 | /// (which may not be an immediate predecessor) which has exactly one |
| 3961 | /// successor from which BB is reachable, or null if no such block is |
| 3962 | /// found. |
| 3963 | /// |
| 3964 | BasicBlock * |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3965 | ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) { |
Dan Gohman | 1116ea7 | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 3966 | // If the block has a unique predecessor, then there is no path from the |
| 3967 | // predecessor to the block that does not go through the direct edge |
| 3968 | // from the predecessor to the block. |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 3969 | if (BasicBlock *Pred = BB->getSinglePredecessor()) |
| 3970 | return Pred; |
| 3971 | |
| 3972 | // A loop's header is defined to be a block that dominates the loop. |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 3973 | // If the header has a unique predecessor outside the loop, it must be |
| 3974 | // a block that has exactly one successor that can reach the loop. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3975 | if (Loop *L = LI->getLoopFor(BB)) |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 3976 | return getLoopPredecessor(L); |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 3977 | |
| 3978 | return 0; |
| 3979 | } |
| 3980 | |
Dan Gohman | bc1e347 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 3981 | /// HasSameValue - SCEV structural equivalence is usually sufficient for |
| 3982 | /// testing whether two expressions are equal, however for the purposes of |
| 3983 | /// looking for a condition guarding a loop, it can be useful to be a little |
| 3984 | /// more general, since a front-end may have replicated the controlling |
| 3985 | /// expression. |
| 3986 | /// |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3987 | static bool HasSameValue(const SCEV* A, const SCEV* B) { |
Dan Gohman | bc1e347 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 3988 | // Quick check to see if they are the same SCEV. |
| 3989 | if (A == B) return true; |
| 3990 | |
| 3991 | // Otherwise, if they're both SCEVUnknown, it's possible that they hold |
| 3992 | // two different instructions with the same value. Check for this case. |
| 3993 | if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A)) |
| 3994 | if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B)) |
| 3995 | if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue())) |
| 3996 | if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue())) |
| 3997 | if (AI->isIdenticalTo(BI)) |
| 3998 | return true; |
| 3999 | |
| 4000 | // Otherwise assume they may have a different value. |
| 4001 | return false; |
| 4002 | } |
| 4003 | |
Dan Gohman | cacd201 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 4004 | /// isLoopGuardedByCond - Test whether entry to the loop is protected by |
Dan Gohman | 1116ea7 | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 4005 | /// a conditional between LHS and RHS. This is used to help avoid max |
| 4006 | /// expressions in loop trip counts. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4007 | bool ScalarEvolution::isLoopGuardedByCond(const Loop *L, |
Dan Gohman | 1116ea7 | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 4008 | ICmpInst::Predicate Pred, |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4009 | const SCEV *LHS, const SCEV *RHS) { |
Dan Gohman | 8b93818 | 2009-05-18 16:03:58 +0000 | [diff] [blame] | 4010 | // Interpret a null as meaning no loop, where there is obviously no guard |
| 4011 | // (interprocedural conditions notwithstanding). |
| 4012 | if (!L) return false; |
| 4013 | |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4014 | BasicBlock *Predecessor = getLoopPredecessor(L); |
| 4015 | BasicBlock *PredecessorDest = L->getHeader(); |
Nick Lewycky | 1b020bf | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4016 | |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4017 | // Starting at the loop predecessor, climb up the predecessor chain, as long |
| 4018 | // as there are predecessors that can be found that have unique successors |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4019 | // leading to the original header. |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4020 | for (; Predecessor; |
| 4021 | PredecessorDest = Predecessor, |
| 4022 | Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) { |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4023 | |
| 4024 | BranchInst *LoopEntryPredicate = |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4025 | dyn_cast<BranchInst>(Predecessor->getTerminator()); |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4026 | if (!LoopEntryPredicate || |
| 4027 | LoopEntryPredicate->isUnconditional()) |
| 4028 | continue; |
| 4029 | |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4030 | if (isNecessaryCond(LoopEntryPredicate->getCondition(), Pred, LHS, RHS, |
| 4031 | LoopEntryPredicate->getSuccessor(0) != PredecessorDest)) |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4032 | return true; |
Nick Lewycky | 1b020bf | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4033 | } |
| 4034 | |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4035 | return false; |
Nick Lewycky | 1b020bf | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4036 | } |
| 4037 | |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4038 | /// isNecessaryCond - Test whether the given CondValue value is a condition |
| 4039 | /// which is at least as strict as the one described by Pred, LHS, and RHS. |
| 4040 | bool ScalarEvolution::isNecessaryCond(Value *CondValue, |
| 4041 | ICmpInst::Predicate Pred, |
| 4042 | const SCEV *LHS, const SCEV *RHS, |
| 4043 | bool Inverse) { |
| 4044 | // Recursivly handle And and Or conditions. |
| 4045 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CondValue)) { |
| 4046 | if (BO->getOpcode() == Instruction::And) { |
| 4047 | if (!Inverse) |
| 4048 | return isNecessaryCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) || |
| 4049 | isNecessaryCond(BO->getOperand(1), Pred, LHS, RHS, Inverse); |
| 4050 | } else if (BO->getOpcode() == Instruction::Or) { |
| 4051 | if (Inverse) |
| 4052 | return isNecessaryCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) || |
| 4053 | isNecessaryCond(BO->getOperand(1), Pred, LHS, RHS, Inverse); |
| 4054 | } |
| 4055 | } |
| 4056 | |
| 4057 | ICmpInst *ICI = dyn_cast<ICmpInst>(CondValue); |
| 4058 | if (!ICI) return false; |
| 4059 | |
| 4060 | // Now that we found a conditional branch that dominates the loop, check to |
| 4061 | // see if it is the comparison we are looking for. |
| 4062 | Value *PreCondLHS = ICI->getOperand(0); |
| 4063 | Value *PreCondRHS = ICI->getOperand(1); |
| 4064 | ICmpInst::Predicate Cond; |
| 4065 | if (Inverse) |
| 4066 | Cond = ICI->getInversePredicate(); |
| 4067 | else |
| 4068 | Cond = ICI->getPredicate(); |
| 4069 | |
| 4070 | if (Cond == Pred) |
| 4071 | ; // An exact match. |
| 4072 | else if (!ICmpInst::isTrueWhenEqual(Cond) && Pred == ICmpInst::ICMP_NE) |
| 4073 | ; // The actual condition is beyond sufficient. |
| 4074 | else |
| 4075 | // Check a few special cases. |
| 4076 | switch (Cond) { |
| 4077 | case ICmpInst::ICMP_UGT: |
| 4078 | if (Pred == ICmpInst::ICMP_ULT) { |
| 4079 | std::swap(PreCondLHS, PreCondRHS); |
| 4080 | Cond = ICmpInst::ICMP_ULT; |
| 4081 | break; |
| 4082 | } |
| 4083 | return false; |
| 4084 | case ICmpInst::ICMP_SGT: |
| 4085 | if (Pred == ICmpInst::ICMP_SLT) { |
| 4086 | std::swap(PreCondLHS, PreCondRHS); |
| 4087 | Cond = ICmpInst::ICMP_SLT; |
| 4088 | break; |
| 4089 | } |
| 4090 | return false; |
| 4091 | case ICmpInst::ICMP_NE: |
| 4092 | // Expressions like (x >u 0) are often canonicalized to (x != 0), |
| 4093 | // so check for this case by checking if the NE is comparing against |
| 4094 | // a minimum or maximum constant. |
| 4095 | if (!ICmpInst::isTrueWhenEqual(Pred)) |
| 4096 | if (ConstantInt *CI = dyn_cast<ConstantInt>(PreCondRHS)) { |
| 4097 | const APInt &A = CI->getValue(); |
| 4098 | switch (Pred) { |
| 4099 | case ICmpInst::ICMP_SLT: |
| 4100 | if (A.isMaxSignedValue()) break; |
| 4101 | return false; |
| 4102 | case ICmpInst::ICMP_SGT: |
| 4103 | if (A.isMinSignedValue()) break; |
| 4104 | return false; |
| 4105 | case ICmpInst::ICMP_ULT: |
| 4106 | if (A.isMaxValue()) break; |
| 4107 | return false; |
| 4108 | case ICmpInst::ICMP_UGT: |
| 4109 | if (A.isMinValue()) break; |
| 4110 | return false; |
| 4111 | default: |
| 4112 | return false; |
| 4113 | } |
| 4114 | Cond = ICmpInst::ICMP_NE; |
| 4115 | // NE is symmetric but the original comparison may not be. Swap |
| 4116 | // the operands if necessary so that they match below. |
| 4117 | if (isa<SCEVConstant>(LHS)) |
| 4118 | std::swap(PreCondLHS, PreCondRHS); |
| 4119 | break; |
| 4120 | } |
| 4121 | return false; |
| 4122 | default: |
| 4123 | // We weren't able to reconcile the condition. |
| 4124 | return false; |
| 4125 | } |
| 4126 | |
| 4127 | if (!PreCondLHS->getType()->isInteger()) return false; |
| 4128 | |
| 4129 | const SCEV *PreCondLHSSCEV = getSCEV(PreCondLHS); |
| 4130 | const SCEV *PreCondRHSSCEV = getSCEV(PreCondRHS); |
| 4131 | return (HasSameValue(LHS, PreCondLHSSCEV) && |
| 4132 | HasSameValue(RHS, PreCondRHSSCEV)) || |
| 4133 | (HasSameValue(LHS, getNotSCEV(PreCondRHSSCEV)) && |
| 4134 | HasSameValue(RHS, getNotSCEV(PreCondLHSSCEV))); |
| 4135 | } |
| 4136 | |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4137 | /// getBECount - Subtract the end and start values and divide by the step, |
| 4138 | /// rounding up, to get the number of times the backedge is executed. Return |
| 4139 | /// CouldNotCompute if an intermediate computation overflows. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4140 | const SCEV* ScalarEvolution::getBECount(const SCEV* Start, |
| 4141 | const SCEV* End, |
| 4142 | const SCEV* Step) { |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4143 | const Type *Ty = Start->getType(); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4144 | const SCEV* NegOne = getIntegerSCEV(-1, Ty); |
| 4145 | const SCEV* Diff = getMinusSCEV(End, Start); |
| 4146 | const SCEV* RoundUp = getAddExpr(Step, NegOne); |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4147 | |
| 4148 | // Add an adjustment to the difference between End and Start so that |
| 4149 | // the division will effectively round up. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4150 | const SCEV* Add = getAddExpr(Diff, RoundUp); |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4151 | |
| 4152 | // Check Add for unsigned overflow. |
| 4153 | // TODO: More sophisticated things could be done here. |
| 4154 | const Type *WideTy = IntegerType::get(getTypeSizeInBits(Ty) + 1); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4155 | const SCEV* OperandExtendedAdd = |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4156 | getAddExpr(getZeroExtendExpr(Diff, WideTy), |
| 4157 | getZeroExtendExpr(RoundUp, WideTy)); |
| 4158 | if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4159 | return getCouldNotCompute(); |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4160 | |
| 4161 | return getUDivExpr(Add, Step); |
| 4162 | } |
| 4163 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4164 | /// HowManyLessThans - Return the number of times a backedge containing the |
| 4165 | /// specified less-than comparison will execute. If not computable, return |
Dan Gohman | 0c85091 | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4166 | /// CouldNotCompute. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4167 | ScalarEvolution::BackedgeTakenInfo |
| 4168 | ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS, |
| 4169 | const Loop *L, bool isSigned) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4170 | // Only handle: "ADDREC < LoopInvariant". |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4171 | if (!RHS->isLoopInvariant(L)) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4172 | |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4173 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4174 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4175 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4176 | |
| 4177 | if (AddRec->isAffine()) { |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 4178 | // FORNOW: We only support unit strides. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4179 | unsigned BitWidth = getTypeSizeInBits(AddRec->getType()); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4180 | const SCEV* Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4181 | |
| 4182 | // TODO: handle non-constant strides. |
| 4183 | const SCEVConstant *CStep = dyn_cast<SCEVConstant>(Step); |
| 4184 | if (!CStep || CStep->isZero()) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4185 | return getCouldNotCompute(); |
Dan Gohman | f8bc8e8 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 4186 | if (CStep->isOne()) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4187 | // With unit stride, the iteration never steps past the limit value. |
| 4188 | } else if (CStep->getValue()->getValue().isStrictlyPositive()) { |
| 4189 | if (const SCEVConstant *CLimit = dyn_cast<SCEVConstant>(RHS)) { |
| 4190 | // Test whether a positive iteration iteration can step past the limit |
| 4191 | // value and past the maximum value for its type in a single step. |
| 4192 | if (isSigned) { |
| 4193 | APInt Max = APInt::getSignedMaxValue(BitWidth); |
| 4194 | if ((Max - CStep->getValue()->getValue()) |
| 4195 | .slt(CLimit->getValue()->getValue())) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4196 | return getCouldNotCompute(); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4197 | } else { |
| 4198 | APInt Max = APInt::getMaxValue(BitWidth); |
| 4199 | if ((Max - CStep->getValue()->getValue()) |
| 4200 | .ult(CLimit->getValue()->getValue())) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4201 | return getCouldNotCompute(); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4202 | } |
| 4203 | } else |
| 4204 | // TODO: handle non-constant limit values below. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4205 | return getCouldNotCompute(); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4206 | } else |
| 4207 | // TODO: handle negative strides below. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4208 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4209 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4210 | // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant |
| 4211 | // m. So, we count the number of iterations in which {n,+,s} < m is true. |
| 4212 | // Note that we cannot simply return max(m-n,0)/s because it's not safe to |
Wojciech Matyjewicz | 1377a54 | 2008-02-13 12:21:32 +0000 | [diff] [blame] | 4213 | // treat m-n as signed nor unsigned due to overflow possibility. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4214 | |
Wojciech Matyjewicz | ebc77b1 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4215 | // First, we get the value of the LHS in the first iteration: n |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4216 | const SCEV* Start = AddRec->getOperand(0); |
Wojciech Matyjewicz | ebc77b1 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4217 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4218 | // Determine the minimum constant start value. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4219 | const SCEV *MinStart = isa<SCEVConstant>(Start) ? Start : |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4220 | getConstant(isSigned ? APInt::getSignedMinValue(BitWidth) : |
| 4221 | APInt::getMinValue(BitWidth)); |
Wojciech Matyjewicz | ebc77b1 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4222 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4223 | // If we know that the condition is true in order to enter the loop, |
| 4224 | // then we know that it will run exactly (m-n)/s times. Otherwise, we |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 4225 | // only know that it will execute (max(m,n)-n)/s times. In both cases, |
| 4226 | // the division must round up. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4227 | const SCEV* End = RHS; |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4228 | if (!isLoopGuardedByCond(L, |
| 4229 | isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
| 4230 | getMinusSCEV(Start, Step), RHS)) |
| 4231 | End = isSigned ? getSMaxExpr(RHS, Start) |
| 4232 | : getUMaxExpr(RHS, Start); |
| 4233 | |
| 4234 | // Determine the maximum constant end value. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4235 | const SCEV* MaxEnd = |
Dan Gohman | 92369c3 | 2009-06-20 00:32:22 +0000 | [diff] [blame] | 4236 | isa<SCEVConstant>(End) ? End : |
| 4237 | getConstant(isSigned ? APInt::getSignedMaxValue(BitWidth) |
| 4238 | .ashr(GetMinSignBits(End) - 1) : |
| 4239 | APInt::getMaxValue(BitWidth) |
| 4240 | .lshr(GetMinLeadingZeros(End))); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4241 | |
| 4242 | // Finally, we subtract these two values and divide, rounding up, to get |
| 4243 | // the number of times the backedge is executed. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4244 | const SCEV* BECount = getBECount(Start, End, Step); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4245 | |
| 4246 | // The maximum backedge count is similar, except using the minimum start |
| 4247 | // value and the maximum end value. |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame^] | 4248 | const SCEV* MaxBECount = getBECount(MinStart, MaxEnd, Step); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4249 | |
| 4250 | return BackedgeTakenInfo(BECount, MaxBECount); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4251 | } |
| 4252 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4253 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4254 | } |
| 4255 | |
| 4256 | /// getNumIterationsInRange - Return the number of iterations of this loop that |
| 4257 | /// produce values in the specified constant range. Another way of looking at |
| 4258 | /// this is that it returns the first iteration number where the value is not in |
| 4259 | /// the condition, thus computing the exit count. If the iteration count can't |
| 4260 | /// be computed, an instance of SCEVCouldNotCompute is returned. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4261 | const SCEV* SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4262 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4263 | if (Range.isFullSet()) // Infinite loop. |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4264 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4265 | |
| 4266 | // If the start is a non-zero constant, shift the range to simplify things. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4267 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4268 | if (!SC->getValue()->isZero()) { |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4269 | SmallVector<const SCEV*, 4> Operands(op_begin(), op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4270 | Operands[0] = SE.getIntegerSCEV(0, SC->getType()); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4271 | const SCEV* Shifted = SE.getAddRecExpr(Operands, getLoop()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4272 | if (const SCEVAddRecExpr *ShiftedAddRec = |
| 4273 | dyn_cast<SCEVAddRecExpr>(Shifted)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4274 | return ShiftedAddRec->getNumIterationsInRange( |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4275 | Range.subtract(SC->getValue()->getValue()), SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4276 | // This is strange and shouldn't happen. |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4277 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4278 | } |
| 4279 | |
| 4280 | // The only time we can solve this is when we have all constant indices. |
| 4281 | // Otherwise, we cannot determine the overflow conditions. |
| 4282 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 4283 | if (!isa<SCEVConstant>(getOperand(i))) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4284 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4285 | |
| 4286 | |
| 4287 | // Okay at this point we know that all elements of the chrec are constants and |
| 4288 | // that the start element is zero. |
| 4289 | |
| 4290 | // First check to see if the range contains zero. If not, the first |
| 4291 | // iteration exits. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 4292 | unsigned BitWidth = SE.getTypeSizeInBits(getType()); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4293 | if (!Range.contains(APInt(BitWidth, 0))) |
Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 4294 | return SE.getIntegerSCEV(0, getType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4295 | |
| 4296 | if (isAffine()) { |
| 4297 | // If this is an affine expression then we have this situation: |
| 4298 | // Solve {0,+,A} in Range === Ax in Range |
| 4299 | |
| 4300 | // We know that zero is in the range. If A is positive then we know that |
| 4301 | // the upper value of the range must be the first possible exit value. |
| 4302 | // If A is negative then the lower of the range is the last possible loop |
| 4303 | // value. Also note that we already checked for a full range. |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4304 | APInt One(BitWidth,1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4305 | APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue(); |
| 4306 | APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower(); |
| 4307 | |
| 4308 | // The exit value should be (End+A)/A. |
Nick Lewycky | a0facae | 2007-09-27 14:12:54 +0000 | [diff] [blame] | 4309 | APInt ExitVal = (End + A).udiv(A); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4310 | ConstantInt *ExitValue = ConstantInt::get(ExitVal); |
| 4311 | |
| 4312 | // Evaluate at the exit value. If we really did fall out of the valid |
| 4313 | // range, then we computed our trip count, otherwise wrap around or other |
| 4314 | // things must have happened. |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4315 | ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4316 | if (Range.contains(Val->getValue())) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4317 | return SE.getCouldNotCompute(); // Something strange happened |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4318 | |
| 4319 | // Ensure that the previous value is in the range. This is a sanity check. |
| 4320 | assert(Range.contains( |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4321 | EvaluateConstantChrecAtConstant(this, |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4322 | ConstantInt::get(ExitVal - One), SE)->getValue()) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4323 | "Linear scev computation is off in a bad way!"); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4324 | return SE.getConstant(ExitValue); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4325 | } else if (isQuadratic()) { |
| 4326 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the |
| 4327 | // quadratic equation to solve it. To do this, we must frame our problem in |
| 4328 | // terms of figuring out when zero is crossed, instead of when |
| 4329 | // Range.getUpper() is crossed. |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4330 | SmallVector<const SCEV*, 4> NewOps(op_begin(), op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4331 | NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper())); |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4332 | const SCEV* NewAddRec = SE.getAddRecExpr(NewOps, getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4333 | |
| 4334 | // Next, solve the constructed addrec |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4335 | std::pair<const SCEV*,const SCEV*> Roots = |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4336 | SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4337 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 4338 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4339 | if (R1) { |
| 4340 | // Pick the smallest positive root value. |
| 4341 | if (ConstantInt *CB = |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4342 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4343 | R1->getValue(), R2->getValue()))) { |
| 4344 | if (CB->getZExtValue() == false) |
| 4345 | std::swap(R1, R2); // R1 is the minimum root now. |
| 4346 | |
| 4347 | // Make sure the root is not off by one. The returned iteration should |
| 4348 | // not be in the range, but the previous one should be. When solving |
| 4349 | // for "X*X < 5", for example, we should not return a root of 2. |
| 4350 | ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this, |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4351 | R1->getValue(), |
| 4352 | SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4353 | if (Range.contains(R1Val->getValue())) { |
| 4354 | // The next iteration must be out of the range... |
| 4355 | ConstantInt *NextVal = ConstantInt::get(R1->getValue()->getValue()+1); |
| 4356 | |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4357 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4358 | if (!Range.contains(R1Val->getValue())) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4359 | return SE.getConstant(NextVal); |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4360 | return SE.getCouldNotCompute(); // Something strange happened |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
| 4363 | // If R1 was not in the range, then it is a good return value. Make |
| 4364 | // sure that R1-1 WAS in the range though, just in case. |
| 4365 | ConstantInt *NextVal = ConstantInt::get(R1->getValue()->getValue()-1); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4366 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4367 | if (Range.contains(R1Val->getValue())) |
| 4368 | return R1; |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4369 | return SE.getCouldNotCompute(); // Something strange happened |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4370 | } |
| 4371 | } |
| 4372 | } |
| 4373 | |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4374 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4375 | } |
| 4376 | |
| 4377 | |
| 4378 | |
| 4379 | //===----------------------------------------------------------------------===// |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4380 | // SCEVCallbackVH Class Implementation |
| 4381 | //===----------------------------------------------------------------------===// |
| 4382 | |
Dan Gohman | 999d14e | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 4383 | void ScalarEvolution::SCEVCallbackVH::deleted() { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4384 | assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!"); |
| 4385 | if (PHINode *PN = dyn_cast<PHINode>(getValPtr())) |
| 4386 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 4387 | if (Instruction *I = dyn_cast<Instruction>(getValPtr())) |
| 4388 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4389 | SE->Scalars.erase(getValPtr()); |
| 4390 | // this now dangles! |
| 4391 | } |
| 4392 | |
Dan Gohman | 999d14e | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 4393 | void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4394 | assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!"); |
| 4395 | |
| 4396 | // Forget all the expressions associated with users of the old value, |
| 4397 | // so that future queries will recompute the expressions using the new |
| 4398 | // value. |
| 4399 | SmallVector<User *, 16> Worklist; |
| 4400 | Value *Old = getValPtr(); |
| 4401 | bool DeleteOld = false; |
| 4402 | for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end(); |
| 4403 | UI != UE; ++UI) |
| 4404 | Worklist.push_back(*UI); |
| 4405 | while (!Worklist.empty()) { |
| 4406 | User *U = Worklist.pop_back_val(); |
| 4407 | // Deleting the Old value will cause this to dangle. Postpone |
| 4408 | // that until everything else is done. |
| 4409 | if (U == Old) { |
| 4410 | DeleteOld = true; |
| 4411 | continue; |
| 4412 | } |
| 4413 | if (PHINode *PN = dyn_cast<PHINode>(U)) |
| 4414 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 4415 | if (Instruction *I = dyn_cast<Instruction>(U)) |
| 4416 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4417 | if (SE->Scalars.erase(U)) |
| 4418 | for (Value::use_iterator UI = U->use_begin(), UE = U->use_end(); |
| 4419 | UI != UE; ++UI) |
| 4420 | Worklist.push_back(*UI); |
| 4421 | } |
| 4422 | if (DeleteOld) { |
| 4423 | if (PHINode *PN = dyn_cast<PHINode>(Old)) |
| 4424 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 4425 | if (Instruction *I = dyn_cast<Instruction>(Old)) |
| 4426 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4427 | SE->Scalars.erase(Old); |
| 4428 | // this now dangles! |
| 4429 | } |
| 4430 | // this may dangle! |
| 4431 | } |
| 4432 | |
Dan Gohman | 999d14e | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 4433 | ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4434 | : CallbackVH(V), SE(se) {} |
| 4435 | |
| 4436 | //===----------------------------------------------------------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4437 | // ScalarEvolution Class Implementation |
| 4438 | //===----------------------------------------------------------------------===// |
| 4439 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4440 | ScalarEvolution::ScalarEvolution() |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4441 | : FunctionPass(&ID) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4444 | bool ScalarEvolution::runOnFunction(Function &F) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4445 | this->F = &F; |
| 4446 | LI = &getAnalysis<LoopInfo>(); |
| 4447 | TD = getAnalysisIfAvailable<TargetData>(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4448 | return false; |
| 4449 | } |
| 4450 | |
| 4451 | void ScalarEvolution::releaseMemory() { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4452 | Scalars.clear(); |
| 4453 | BackedgeTakenCounts.clear(); |
| 4454 | ConstantEvolutionLoopExitValue.clear(); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 4455 | ValuesAtScopes.clear(); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4456 | UniqueSCEVs.clear(); |
| 4457 | SCEVAllocator.Reset(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4458 | } |
| 4459 | |
| 4460 | void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const { |
| 4461 | AU.setPreservesAll(); |
| 4462 | AU.addRequiredTransitive<LoopInfo>(); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4463 | } |
| 4464 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4465 | bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4466 | return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4467 | } |
| 4468 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4469 | static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4470 | const Loop *L) { |
| 4471 | // Print all inner loops first |
| 4472 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 4473 | PrintLoopInfo(OS, SE, *I); |
| 4474 | |
Nick Lewycky | e5da191 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 4475 | OS << "Loop " << L->getHeader()->getName() << ": "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4476 | |
Devang Patel | 02451fa | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 4477 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4478 | L->getExitBlocks(ExitBlocks); |
| 4479 | if (ExitBlocks.size() != 1) |
Nick Lewycky | e5da191 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 4480 | OS << "<multiple exits> "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4481 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4482 | if (SE->hasLoopInvariantBackedgeTakenCount(L)) { |
| 4483 | OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4484 | } else { |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4485 | OS << "Unpredictable backedge-taken count. "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
Nick Lewycky | e5da191 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 4488 | OS << "\n"; |
Dan Gohman | b6b9e9e | 2009-06-24 00:33:16 +0000 | [diff] [blame] | 4489 | OS << "Loop " << L->getHeader()->getName() << ": "; |
| 4490 | |
| 4491 | if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) { |
| 4492 | OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L); |
| 4493 | } else { |
| 4494 | OS << "Unpredictable max backedge-taken count. "; |
| 4495 | } |
| 4496 | |
| 4497 | OS << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4498 | } |
| 4499 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 4500 | void ScalarEvolution::print(raw_ostream &OS, const Module* ) const { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4501 | // ScalarEvolution's implementaiton of the print method is to print |
| 4502 | // out SCEV values of all instructions that are interesting. Doing |
| 4503 | // this potentially causes it to create new SCEV objects though, |
| 4504 | // which technically conflicts with the const qualifier. This isn't |
| 4505 | // observable from outside the class though (the hasSCEV function |
| 4506 | // notwithstanding), so casting away the const isn't dangerous. |
| 4507 | ScalarEvolution &SE = *const_cast<ScalarEvolution*>(this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4508 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4509 | OS << "Classifying expressions for: " << F->getName() << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4510 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
Dan Gohman | 43d37e9 | 2009-04-30 01:30:18 +0000 | [diff] [blame] | 4511 | if (isSCEVable(I->getType())) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4512 | OS << *I; |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 4513 | OS << " --> "; |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4514 | const SCEV* SV = SE.getSCEV(&*I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4515 | SV->print(OS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4516 | |
Dan Gohman | 8db598a | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 4517 | const Loop *L = LI->getLoopFor((*I).getParent()); |
| 4518 | |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4519 | const SCEV* AtUse = SE.getSCEVAtScope(SV, L); |
Dan Gohman | 8db598a | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 4520 | if (AtUse != SV) { |
| 4521 | OS << " --> "; |
| 4522 | AtUse->print(OS); |
| 4523 | } |
| 4524 | |
| 4525 | if (L) { |
Dan Gohman | e5b6084 | 2009-06-18 00:37:45 +0000 | [diff] [blame] | 4526 | OS << "\t\t" "Exits: "; |
Owen Anderson | ecd0cd7 | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4527 | const SCEV* ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop()); |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 4528 | if (!ExitValue->isLoopInvariant(L)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4529 | OS << "<<Unknown>>"; |
| 4530 | } else { |
| 4531 | OS << *ExitValue; |
| 4532 | } |
| 4533 | } |
| 4534 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4535 | OS << "\n"; |
| 4536 | } |
| 4537 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4538 | OS << "Determining loop execution counts for: " << F->getName() << "\n"; |
| 4539 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
| 4540 | PrintLoopInfo(OS, &SE, *I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4541 | } |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 4542 | |
| 4543 | void ScalarEvolution::print(std::ostream &o, const Module *M) const { |
| 4544 | raw_os_ostream OS(o); |
| 4545 | print(OS, M); |
| 4546 | } |