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 |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +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" |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 68 | #include "llvm/LLVMContext.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | #include "llvm/Analysis/ConstantFolding.h" |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 70 | #include "llvm/Analysis/Dominators.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 71 | #include "llvm/Analysis/LoopInfo.h" |
Dan Gohman | a7726c3 | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 72 | #include "llvm/Analysis/ValueTracking.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 73 | #include "llvm/Assembly/Writer.h" |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 74 | #include "llvm/Target/TargetData.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 75 | #include "llvm/Support/CommandLine.h" |
| 76 | #include "llvm/Support/Compiler.h" |
| 77 | #include "llvm/Support/ConstantRange.h" |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 78 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 79 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 80 | #include "llvm/Support/InstIterator.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 81 | #include "llvm/Support/MathExtras.h" |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 82 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 83 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 84 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 85 | #include "llvm/ADT/SmallPtrSet.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 86 | #include <algorithm> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 87 | using namespace llvm; |
| 88 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 89 | STATISTIC(NumArrayLenItCounts, |
| 90 | "Number of trip counts computed with array length"); |
| 91 | STATISTIC(NumTripCountsComputed, |
| 92 | "Number of loops with predictable loop counts"); |
| 93 | STATISTIC(NumTripCountsNotComputed, |
| 94 | "Number of loops without predictable loop counts"); |
| 95 | STATISTIC(NumBruteForceTripCountsComputed, |
| 96 | "Number of loops with trip counts computed by force"); |
| 97 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 98 | static cl::opt<unsigned> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 99 | MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, |
| 100 | cl::desc("Maximum number of iterations SCEV will " |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 101 | "symbolically execute a constant " |
| 102 | "derived loop"), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 103 | cl::init(100)); |
| 104 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 105 | static RegisterPass<ScalarEvolution> |
| 106 | R("scalar-evolution", "Scalar Evolution Analysis", false, true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 107 | char ScalarEvolution::ID = 0; |
| 108 | |
| 109 | //===----------------------------------------------------------------------===// |
| 110 | // SCEV class definitions |
| 111 | //===----------------------------------------------------------------------===// |
| 112 | |
| 113 | //===----------------------------------------------------------------------===// |
| 114 | // Implementation of the SCEV class. |
| 115 | // |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 116 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 117 | SCEV::~SCEV() {} |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 118 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 119 | void SCEV::dump() const { |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 120 | print(errs()); |
| 121 | errs() << '\n'; |
| 122 | } |
| 123 | |
| 124 | void SCEV::print(std::ostream &o) const { |
| 125 | raw_os_ostream OS(o); |
| 126 | print(OS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 129 | bool SCEV::isZero() const { |
| 130 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 131 | return SC->getValue()->isZero(); |
| 132 | return false; |
| 133 | } |
| 134 | |
Dan Gohman | f8bc8e8 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 135 | bool SCEV::isOne() const { |
| 136 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 137 | return SC->getValue()->isOne(); |
| 138 | return false; |
| 139 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 140 | |
Dan Gohman | f05118e | 2009-06-24 00:30:26 +0000 | [diff] [blame] | 141 | bool SCEV::isAllOnesValue() const { |
| 142 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 143 | return SC->getValue()->isAllOnesValue(); |
| 144 | return false; |
| 145 | } |
| 146 | |
Owen Anderson | b70139d | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 147 | SCEVCouldNotCompute::SCEVCouldNotCompute() : |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 148 | SCEV(FoldingSetNodeID(), scCouldNotCompute) {} |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 149 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 150 | bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const { |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 151 | LLVM_UNREACHABLE("Attempt to use a SCEVCouldNotCompute object!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | |
| 155 | const Type *SCEVCouldNotCompute::getType() const { |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 156 | LLVM_UNREACHABLE("Attempt to use a SCEVCouldNotCompute object!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const { |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 161 | LLVM_UNREACHABLE("Attempt to use a SCEVCouldNotCompute object!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 165 | const SCEV * |
| 166 | SCEVCouldNotCompute::replaceSymbolicValuesWithConcrete( |
| 167 | const SCEV *Sym, |
| 168 | const SCEV *Conc, |
| 169 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 170 | return this; |
| 171 | } |
| 172 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 173 | void SCEVCouldNotCompute::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 174 | OS << "***COULDNOTCOMPUTE***"; |
| 175 | } |
| 176 | |
| 177 | bool SCEVCouldNotCompute::classof(const SCEV *S) { |
| 178 | return S->getSCEVType() == scCouldNotCompute; |
| 179 | } |
| 180 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 181 | const SCEV *ScalarEvolution::getConstant(ConstantInt *V) { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 182 | FoldingSetNodeID ID; |
| 183 | ID.AddInteger(scConstant); |
| 184 | ID.AddPointer(V); |
| 185 | void *IP = 0; |
| 186 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 187 | SCEV *S = SCEVAllocator.Allocate<SCEVConstant>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 188 | new (S) SCEVConstant(ID, V); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 189 | UniqueSCEVs.InsertNode(S, IP); |
| 190 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 193 | const SCEV *ScalarEvolution::getConstant(const APInt& Val) { |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 194 | return getConstant(ConstantInt::get(Val)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 197 | const SCEV * |
Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 198 | ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) { |
| 199 | return getConstant(ConstantInt::get(cast<IntegerType>(Ty), V, isSigned)); |
| 200 | } |
| 201 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 202 | const Type *SCEVConstant::getType() const { return V->getType(); } |
| 203 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 204 | void SCEVConstant::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 205 | WriteAsOperand(OS, V, false); |
| 206 | } |
| 207 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 208 | SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeID &ID, |
| 209 | unsigned SCEVTy, const SCEV *op, const Type *ty) |
| 210 | : SCEV(ID, SCEVTy), Op(op), Ty(ty) {} |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 211 | |
Dan Gohman | 2a38153 | 2009-04-21 01:25:57 +0000 | [diff] [blame] | 212 | bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 213 | return Op->dominates(BB, DT); |
| 214 | } |
| 215 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 216 | SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeID &ID, |
| 217 | const SCEV *op, const Type *ty) |
| 218 | : SCEVCastExpr(ID, scTruncate, op, ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 219 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 220 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 221 | "Cannot truncate non-integer value!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 224 | void SCEVTruncateExpr::print(raw_ostream &OS) const { |
Dan Gohman | c911922 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 225 | OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 228 | SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeID &ID, |
| 229 | const SCEV *op, const Type *ty) |
| 230 | : SCEVCastExpr(ID, scZeroExtend, op, ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 231 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 232 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 233 | "Cannot zero extend non-integer value!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 236 | void SCEVZeroExtendExpr::print(raw_ostream &OS) const { |
Dan Gohman | c911922 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 237 | OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 240 | SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeID &ID, |
| 241 | const SCEV *op, const Type *ty) |
| 242 | : SCEVCastExpr(ID, scSignExtend, op, ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 243 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 244 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 245 | "Cannot sign extend non-integer value!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 248 | void SCEVSignExtendExpr::print(raw_ostream &OS) const { |
Dan Gohman | c911922 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 249 | OS << "(sext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 252 | void SCEVCommutativeExpr::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 253 | assert(Operands.size() > 1 && "This plus expr shouldn't exist!"); |
| 254 | const char *OpStr = getOperationStr(); |
| 255 | OS << "(" << *Operands[0]; |
| 256 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 257 | OS << OpStr << *Operands[i]; |
| 258 | OS << ")"; |
| 259 | } |
| 260 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 261 | const SCEV * |
| 262 | SCEVCommutativeExpr::replaceSymbolicValuesWithConcrete( |
| 263 | const SCEV *Sym, |
| 264 | const SCEV *Conc, |
| 265 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 266 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 267 | const SCEV *H = |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 268 | getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 269 | if (H != getOperand(i)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 270 | SmallVector<const SCEV *, 8> NewOps; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 271 | NewOps.reserve(getNumOperands()); |
| 272 | for (unsigned j = 0; j != i; ++j) |
| 273 | NewOps.push_back(getOperand(j)); |
| 274 | NewOps.push_back(H); |
| 275 | for (++i; i != e; ++i) |
| 276 | NewOps.push_back(getOperand(i)-> |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 277 | replaceSymbolicValuesWithConcrete(Sym, Conc, SE)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 278 | |
| 279 | if (isa<SCEVAddExpr>(this)) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 280 | return SE.getAddExpr(NewOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 281 | else if (isa<SCEVMulExpr>(this)) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 282 | return SE.getMulExpr(NewOps); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 283 | else if (isa<SCEVSMaxExpr>(this)) |
| 284 | return SE.getSMaxExpr(NewOps); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 285 | else if (isa<SCEVUMaxExpr>(this)) |
| 286 | return SE.getUMaxExpr(NewOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 287 | else |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 288 | LLVM_UNREACHABLE("Unknown commutative expr!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | return this; |
| 292 | } |
| 293 | |
Dan Gohman | 72a8a02 | 2009-05-07 14:00:19 +0000 | [diff] [blame] | 294 | bool SCEVNAryExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 295 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 296 | if (!getOperand(i)->dominates(BB, DT)) |
| 297 | return false; |
| 298 | } |
| 299 | return true; |
| 300 | } |
| 301 | |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 302 | bool SCEVUDivExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 303 | return LHS->dominates(BB, DT) && RHS->dominates(BB, DT); |
| 304 | } |
| 305 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 306 | void SCEVUDivExpr::print(raw_ostream &OS) const { |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 307 | OS << "(" << *LHS << " /u " << *RHS << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 310 | const Type *SCEVUDivExpr::getType() const { |
Dan Gohman | 140f08f | 2009-05-26 17:44:05 +0000 | [diff] [blame] | 311 | // In most cases the types of LHS and RHS will be the same, but in some |
| 312 | // crazy cases one or the other may be a pointer. ScalarEvolution doesn't |
| 313 | // depend on the type for correctness, but handling types carefully can |
| 314 | // avoid extra casts in the SCEVExpander. The LHS is more likely to be |
| 315 | // a pointer type than the RHS, so use the RHS' type here. |
| 316 | return RHS->getType(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 319 | const SCEV * |
| 320 | SCEVAddRecExpr::replaceSymbolicValuesWithConcrete(const SCEV *Sym, |
| 321 | const SCEV *Conc, |
| 322 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 323 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 324 | const SCEV *H = |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 325 | getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 326 | if (H != getOperand(i)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 327 | SmallVector<const SCEV *, 8> NewOps; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 328 | NewOps.reserve(getNumOperands()); |
| 329 | for (unsigned j = 0; j != i; ++j) |
| 330 | NewOps.push_back(getOperand(j)); |
| 331 | NewOps.push_back(H); |
| 332 | for (++i; i != e; ++i) |
| 333 | NewOps.push_back(getOperand(i)-> |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 334 | replaceSymbolicValuesWithConcrete(Sym, Conc, SE)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 335 | |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 336 | return SE.getAddRecExpr(NewOps, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | return this; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const { |
Dan Gohman | ae1eaae | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 344 | // Add recurrences are never invariant in the function-body (null loop). |
Dan Gohman | 2d888d8 | 2009-06-26 22:17:21 +0000 | [diff] [blame] | 345 | if (!QueryLoop) |
| 346 | return false; |
| 347 | |
| 348 | // This recurrence is variant w.r.t. QueryLoop if QueryLoop contains L. |
| 349 | if (QueryLoop->contains(L->getHeader())) |
| 350 | return false; |
| 351 | |
| 352 | // This recurrence is variant w.r.t. QueryLoop if any of its operands |
| 353 | // are variant. |
| 354 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 355 | if (!getOperand(i)->isLoopInvariant(QueryLoop)) |
| 356 | return false; |
| 357 | |
| 358 | // Otherwise it's loop-invariant. |
| 359 | return true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 362 | void SCEVAddRecExpr::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 363 | OS << "{" << *Operands[0]; |
| 364 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 365 | OS << ",+," << *Operands[i]; |
| 366 | OS << "}<" << L->getHeader()->getName() + ">"; |
| 367 | } |
| 368 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 369 | bool SCEVUnknown::isLoopInvariant(const Loop *L) const { |
| 370 | // All non-instruction values are loop invariant. All instructions are loop |
| 371 | // invariant if they are not contained in the specified loop. |
Dan Gohman | ae1eaae | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 372 | // Instructions are never considered invariant in the function body |
| 373 | // (null loop) because they are defined within the "loop". |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 374 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Dan Gohman | ae1eaae | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 375 | return L && !L->contains(I->getParent()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 376 | return true; |
| 377 | } |
| 378 | |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 379 | bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 380 | if (Instruction *I = dyn_cast<Instruction>(getValue())) |
| 381 | return DT->dominates(I->getParent(), BB); |
| 382 | return true; |
| 383 | } |
| 384 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 385 | const Type *SCEVUnknown::getType() const { |
| 386 | return V->getType(); |
| 387 | } |
| 388 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 389 | void SCEVUnknown::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 390 | WriteAsOperand(OS, V, false); |
| 391 | } |
| 392 | |
| 393 | //===----------------------------------------------------------------------===// |
| 394 | // SCEV Utilities |
| 395 | //===----------------------------------------------------------------------===// |
| 396 | |
| 397 | namespace { |
| 398 | /// SCEVComplexityCompare - Return true if the complexity of the LHS is less |
| 399 | /// than the complexity of the RHS. This comparator is used to canonicalize |
| 400 | /// expressions. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 401 | class VISIBILITY_HIDDEN SCEVComplexityCompare { |
| 402 | LoopInfo *LI; |
| 403 | public: |
| 404 | explicit SCEVComplexityCompare(LoopInfo *li) : LI(li) {} |
| 405 | |
Dan Gohman | c0c69cf | 2008-04-14 18:23:56 +0000 | [diff] [blame] | 406 | bool operator()(const SCEV *LHS, const SCEV *RHS) const { |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 407 | // Primarily, sort the SCEVs by their getSCEVType(). |
| 408 | if (LHS->getSCEVType() != RHS->getSCEVType()) |
| 409 | return LHS->getSCEVType() < RHS->getSCEVType(); |
| 410 | |
| 411 | // Aside from the getSCEVType() ordering, the particular ordering |
| 412 | // isn't very important except that it's beneficial to be consistent, |
| 413 | // so that (a + b) and (b + a) don't end up as different expressions. |
| 414 | |
| 415 | // Sort SCEVUnknown values with some loose heuristics. TODO: This is |
| 416 | // not as complete as it could be. |
| 417 | if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) { |
| 418 | const SCEVUnknown *RU = cast<SCEVUnknown>(RHS); |
| 419 | |
Dan Gohman | d0c0123 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 420 | // Order pointer values after integer values. This helps SCEVExpander |
| 421 | // form GEPs. |
| 422 | if (isa<PointerType>(LU->getType()) && !isa<PointerType>(RU->getType())) |
| 423 | return false; |
| 424 | if (isa<PointerType>(RU->getType()) && !isa<PointerType>(LU->getType())) |
| 425 | return true; |
| 426 | |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 427 | // Compare getValueID values. |
| 428 | if (LU->getValue()->getValueID() != RU->getValue()->getValueID()) |
| 429 | return LU->getValue()->getValueID() < RU->getValue()->getValueID(); |
| 430 | |
| 431 | // Sort arguments by their position. |
| 432 | if (const Argument *LA = dyn_cast<Argument>(LU->getValue())) { |
| 433 | const Argument *RA = cast<Argument>(RU->getValue()); |
| 434 | return LA->getArgNo() < RA->getArgNo(); |
| 435 | } |
| 436 | |
| 437 | // For instructions, compare their loop depth, and their opcode. |
| 438 | // This is pretty loose. |
| 439 | if (Instruction *LV = dyn_cast<Instruction>(LU->getValue())) { |
| 440 | Instruction *RV = cast<Instruction>(RU->getValue()); |
| 441 | |
| 442 | // Compare loop depths. |
| 443 | if (LI->getLoopDepth(LV->getParent()) != |
| 444 | LI->getLoopDepth(RV->getParent())) |
| 445 | return LI->getLoopDepth(LV->getParent()) < |
| 446 | LI->getLoopDepth(RV->getParent()); |
| 447 | |
| 448 | // Compare opcodes. |
| 449 | if (LV->getOpcode() != RV->getOpcode()) |
| 450 | return LV->getOpcode() < RV->getOpcode(); |
| 451 | |
| 452 | // Compare the number of operands. |
| 453 | if (LV->getNumOperands() != RV->getNumOperands()) |
| 454 | return LV->getNumOperands() < RV->getNumOperands(); |
| 455 | } |
| 456 | |
| 457 | return false; |
| 458 | } |
| 459 | |
Dan Gohman | 56fc8f1 | 2009-06-14 22:51:25 +0000 | [diff] [blame] | 460 | // Compare constant values. |
| 461 | if (const SCEVConstant *LC = dyn_cast<SCEVConstant>(LHS)) { |
| 462 | const SCEVConstant *RC = cast<SCEVConstant>(RHS); |
Nick Lewycky | 9bb1405 | 2009-07-04 17:24:52 +0000 | [diff] [blame] | 463 | if (LC->getValue()->getBitWidth() != RC->getValue()->getBitWidth()) |
| 464 | return LC->getValue()->getBitWidth() < RC->getValue()->getBitWidth(); |
Dan Gohman | 56fc8f1 | 2009-06-14 22:51:25 +0000 | [diff] [blame] | 465 | return LC->getValue()->getValue().ult(RC->getValue()->getValue()); |
| 466 | } |
| 467 | |
| 468 | // Compare addrec loop depths. |
| 469 | if (const SCEVAddRecExpr *LA = dyn_cast<SCEVAddRecExpr>(LHS)) { |
| 470 | const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS); |
| 471 | if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth()) |
| 472 | return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth(); |
| 473 | } |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 474 | |
| 475 | // Lexicographically compare n-ary expressions. |
| 476 | if (const SCEVNAryExpr *LC = dyn_cast<SCEVNAryExpr>(LHS)) { |
| 477 | const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS); |
| 478 | for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) { |
| 479 | if (i >= RC->getNumOperands()) |
| 480 | return false; |
| 481 | if (operator()(LC->getOperand(i), RC->getOperand(i))) |
| 482 | return true; |
| 483 | if (operator()(RC->getOperand(i), LC->getOperand(i))) |
| 484 | return false; |
| 485 | } |
| 486 | return LC->getNumOperands() < RC->getNumOperands(); |
| 487 | } |
| 488 | |
Dan Gohman | 6e10db1 | 2009-05-07 19:23:21 +0000 | [diff] [blame] | 489 | // Lexicographically compare udiv expressions. |
| 490 | if (const SCEVUDivExpr *LC = dyn_cast<SCEVUDivExpr>(LHS)) { |
| 491 | const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS); |
| 492 | if (operator()(LC->getLHS(), RC->getLHS())) |
| 493 | return true; |
| 494 | if (operator()(RC->getLHS(), LC->getLHS())) |
| 495 | return false; |
| 496 | if (operator()(LC->getRHS(), RC->getRHS())) |
| 497 | return true; |
| 498 | if (operator()(RC->getRHS(), LC->getRHS())) |
| 499 | return false; |
| 500 | return false; |
| 501 | } |
| 502 | |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 503 | // Compare cast expressions by operand. |
| 504 | if (const SCEVCastExpr *LC = dyn_cast<SCEVCastExpr>(LHS)) { |
| 505 | const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS); |
| 506 | return operator()(LC->getOperand(), RC->getOperand()); |
| 507 | } |
| 508 | |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 509 | LLVM_UNREACHABLE("Unknown SCEV kind!"); |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 510 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 511 | } |
| 512 | }; |
| 513 | } |
| 514 | |
| 515 | /// GroupByComplexity - Given a list of SCEV objects, order them by their |
| 516 | /// complexity, and group objects of the same complexity together by value. |
| 517 | /// When this routine is finished, we know that any duplicates in the vector are |
| 518 | /// consecutive and that complexity is monotonically increasing. |
| 519 | /// |
| 520 | /// Note that we go take special precautions to ensure that we get determinstic |
| 521 | /// results from this routine. In other words, we don't want the results of |
| 522 | /// this to depend on where the addresses of various SCEV objects happened to |
| 523 | /// land in memory. |
| 524 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 525 | static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops, |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 526 | LoopInfo *LI) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 527 | if (Ops.size() < 2) return; // Noop |
| 528 | if (Ops.size() == 2) { |
| 529 | // This is the common case, which also happens to be trivially simple. |
| 530 | // Special case it. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 531 | if (SCEVComplexityCompare(LI)(Ops[1], Ops[0])) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 532 | std::swap(Ops[0], Ops[1]); |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | // Do the rough sort by complexity. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 537 | std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 538 | |
| 539 | // Now that we are sorted by complexity, group elements of the same |
| 540 | // complexity. Note that this is, at worst, N^2, but the vector is likely to |
| 541 | // be extremely short in practice. Note that we take this approach because we |
| 542 | // do not want to depend on the addresses of the objects we are grouping. |
| 543 | for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 544 | const SCEV *S = Ops[i]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 545 | unsigned Complexity = S->getSCEVType(); |
| 546 | |
| 547 | // If there are any objects of the same complexity and same value as this |
| 548 | // one, group them. |
| 549 | for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { |
| 550 | if (Ops[j] == S) { // Found a duplicate. |
| 551 | // Move it to immediately after i'th element. |
| 552 | std::swap(Ops[i+1], Ops[j]); |
| 553 | ++i; // no need to rescan it. |
| 554 | if (i == e-2) return; // Done! |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | |
| 561 | |
| 562 | //===----------------------------------------------------------------------===// |
| 563 | // Simple SCEV method implementations |
| 564 | //===----------------------------------------------------------------------===// |
| 565 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 566 | /// BinomialCoefficient - Compute BC(It, K). The result has width W. |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 567 | /// Assume, K > 0. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 568 | static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K, |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 569 | ScalarEvolution &SE, |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 570 | const Type* ResultTy) { |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 571 | // Handle the simplest case efficiently. |
| 572 | if (K == 1) |
| 573 | return SE.getTruncateOrZeroExtend(It, ResultTy); |
| 574 | |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 575 | // We are using the following formula for BC(It, K): |
| 576 | // |
| 577 | // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! |
| 578 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 579 | // Suppose, W is the bitwidth of the return value. We must be prepared for |
| 580 | // overflow. Hence, we must assure that the result of our computation is |
| 581 | // equal to the accurate one modulo 2^W. Unfortunately, division isn't |
| 582 | // safe in modular arithmetic. |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 583 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 584 | // 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] | 585 | // 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] | 586 | // K! (i.e. trailing zeros in the binary representation of K!), and ^ is |
| 587 | // exponentiation: |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 588 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 589 | // 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] | 590 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 591 | // This formula is trivially equivalent to the previous formula. However, |
| 592 | // this formula can be implemented much more efficiently. The trick is that |
| 593 | // K! / 2^T is odd, and exact division by an odd number *is* safe in modular |
| 594 | // arithmetic. To do exact division in modular arithmetic, all we have |
| 595 | // to do is multiply by the inverse. Therefore, this step can be done at |
| 596 | // width W. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 597 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 598 | // The next issue is how to safely do the division by 2^T. The way this |
| 599 | // is done is by doing the multiplication step at a width of at least W + T |
| 600 | // bits. This way, the bottom W+T bits of the product are accurate. Then, |
| 601 | // when we perform the division by 2^T (which is equivalent to a right shift |
| 602 | // by T), the bottom W bits are accurate. Extra bits are okay; they'll get |
| 603 | // truncated out after the division by 2^T. |
| 604 | // |
| 605 | // In comparison to just directly using the first formula, this technique |
| 606 | // is much more efficient; using the first formula requires W * K bits, |
| 607 | // but this formula less than W + K bits. Also, the first formula requires |
| 608 | // a division step, whereas this formula only requires multiplies and shifts. |
| 609 | // |
| 610 | // It doesn't matter whether the subtraction step is done in the calculation |
| 611 | // width or the input iteration count's width; if the subtraction overflows, |
| 612 | // the result must be zero anyway. We prefer here to do it in the width of |
| 613 | // the induction variable because it helps a lot for certain cases; CodeGen |
| 614 | // isn't smart enough to ignore the overflow, which leads to much less |
| 615 | // efficient code if the width of the subtraction is wider than the native |
| 616 | // register width. |
| 617 | // |
| 618 | // (It's possible to not widen at all by pulling out factors of 2 before |
| 619 | // the multiplication; for example, K=2 can be calculated as |
| 620 | // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires |
| 621 | // extra arithmetic, so it's not an obvious win, and it gets |
| 622 | // much more complicated for K > 3.) |
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 | // Protection from insane SCEVs; this bound is conservative, |
| 625 | // but it probably doesn't matter. |
| 626 | if (K > 1000) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 627 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 628 | |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 629 | unsigned W = SE.getTypeSizeInBits(ResultTy); |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 630 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 631 | // Calculate K! / 2^T and T; we divide out the factors of two before |
| 632 | // multiplying for calculating K! / 2^T to avoid overflow. |
| 633 | // Other overflow doesn't matter because we only care about the bottom |
| 634 | // W bits of the result. |
| 635 | APInt OddFactorial(W, 1); |
| 636 | unsigned T = 1; |
| 637 | for (unsigned i = 3; i <= K; ++i) { |
| 638 | APInt Mult(W, i); |
| 639 | unsigned TwoFactors = Mult.countTrailingZeros(); |
| 640 | T += TwoFactors; |
| 641 | Mult = Mult.lshr(TwoFactors); |
| 642 | OddFactorial *= Mult; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 643 | } |
Nick Lewycky | dbaa60a | 2008-06-13 04:38:55 +0000 | [diff] [blame] | 644 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 645 | // We need at least W + T bits for the multiplication step |
nicholas | 9e3e5fd | 2009-01-25 08:16:27 +0000 | [diff] [blame] | 646 | unsigned CalculationBits = W + T; |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 647 | |
| 648 | // Calcuate 2^T, at width T+W. |
| 649 | APInt DivFactor = APInt(CalculationBits, 1).shl(T); |
| 650 | |
| 651 | // Calculate the multiplicative inverse of K! / 2^T; |
| 652 | // this multiplication factor will perform the exact division by |
| 653 | // K! / 2^T. |
| 654 | APInt Mod = APInt::getSignedMinValue(W+1); |
| 655 | APInt MultiplyFactor = OddFactorial.zext(W+1); |
| 656 | MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); |
| 657 | MultiplyFactor = MultiplyFactor.trunc(W); |
| 658 | |
| 659 | // Calculate the product, at width T+W |
| 660 | const IntegerType *CalculationTy = IntegerType::get(CalculationBits); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 661 | const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 662 | for (unsigned i = 1; i != K; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 663 | const SCEV *S = SE.getMinusSCEV(It, SE.getIntegerSCEV(i, It->getType())); |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 664 | Dividend = SE.getMulExpr(Dividend, |
| 665 | SE.getTruncateOrZeroExtend(S, CalculationTy)); |
| 666 | } |
| 667 | |
| 668 | // Divide by 2^T |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 669 | const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 670 | |
| 671 | // Truncate the result, and divide by K! / 2^T. |
| 672 | |
| 673 | return SE.getMulExpr(SE.getConstant(MultiplyFactor), |
| 674 | SE.getTruncateOrZeroExtend(DivResult, ResultTy)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 677 | /// evaluateAtIteration - Return the value of this chain of recurrences at |
| 678 | /// the specified iteration number. We can evaluate this recurrence by |
| 679 | /// multiplying each element in the chain by the binomial coefficient |
| 680 | /// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as: |
| 681 | /// |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 682 | /// 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] | 683 | /// |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 684 | /// where BC(It, k) stands for binomial coefficient. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 685 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 686 | const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It, |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 687 | ScalarEvolution &SE) const { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 688 | const SCEV *Result = getStart(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 689 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 690 | // The computation is correct in the face of overflow provided that the |
| 691 | // multiplication is performed _after_ the evaluation of the binomial |
| 692 | // coefficient. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 693 | const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType()); |
Nick Lewycky | b6218e0 | 2008-10-13 03:58:02 +0000 | [diff] [blame] | 694 | if (isa<SCEVCouldNotCompute>(Coeff)) |
| 695 | return Coeff; |
| 696 | |
| 697 | Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 698 | } |
| 699 | return Result; |
| 700 | } |
| 701 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 702 | //===----------------------------------------------------------------------===// |
| 703 | // SCEV Expression folder implementations |
| 704 | //===----------------------------------------------------------------------===// |
| 705 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 706 | const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, |
Dan Gohman | 9c8abcc | 2009-05-01 16:44:56 +0000 | [diff] [blame] | 707 | const Type *Ty) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 708 | assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 709 | "This is not a truncating conversion!"); |
Dan Gohman | 13a51e2 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 710 | assert(isSCEVable(Ty) && |
| 711 | "This is not a conversion to a SCEVable type!"); |
| 712 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 713 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 714 | FoldingSetNodeID ID; |
| 715 | ID.AddInteger(scTruncate); |
| 716 | ID.AddPointer(Op); |
| 717 | ID.AddPointer(Ty); |
| 718 | void *IP = 0; |
| 719 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 720 | |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 721 | // Fold if the operand is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 722 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 723 | return getConstant( |
| 724 | cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 725 | |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 726 | // trunc(trunc(x)) --> trunc(x) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 727 | if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 728 | return getTruncateExpr(ST->getOperand(), Ty); |
| 729 | |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 730 | // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 731 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 732 | return getTruncateOrSignExtend(SS->getOperand(), Ty); |
| 733 | |
| 734 | // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 735 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 736 | return getTruncateOrZeroExtend(SZ->getOperand(), Ty); |
| 737 | |
Dan Gohman | 1c0aa2c | 2009-06-18 16:24:47 +0000 | [diff] [blame] | 738 | // 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] | 739 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 740 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 741 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 742 | Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty)); |
| 743 | return getAddRecExpr(Operands, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 746 | // The cast wasn't folded; create an explicit cast node. |
| 747 | // Recompute the insert position, as it may have been invalidated. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 748 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 749 | SCEV *S = SCEVAllocator.Allocate<SCEVTruncateExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 750 | new (S) SCEVTruncateExpr(ID, Op, Ty); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 751 | UniqueSCEVs.InsertNode(S, IP); |
| 752 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 755 | const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op, |
Dan Gohman | 36d4092 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 756 | const Type *Ty) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 757 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | 36d4092 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 758 | "This is not an extending conversion!"); |
Dan Gohman | 13a51e2 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 759 | assert(isSCEVable(Ty) && |
| 760 | "This is not a conversion to a SCEVable type!"); |
| 761 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | 36d4092 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 762 | |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 763 | // Fold if the operand is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 764 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 765 | const Type *IntTy = getEffectiveSCEVType(Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 766 | Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy); |
| 767 | if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 768 | return getConstant(cast<ConstantInt>(C)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 769 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 770 | |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 771 | // zext(zext(x)) --> zext(x) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 772 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 773 | return getZeroExtendExpr(SZ->getOperand(), Ty); |
| 774 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 775 | // 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] | 776 | // 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] | 777 | // operands (often constants). This allows analysis of something like |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 778 | // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 779 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 780 | if (AR->isAffine()) { |
| 781 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 782 | // Note that this serves two purposes: It filters out loops that are |
| 783 | // simply not analyzable, and it covers the case where this code is |
| 784 | // being called from within backedge-taken count analysis, such that |
| 785 | // attempting to ask for the backedge-taken count would likely result |
| 786 | // in infinite recursion. In the later case, the analysis code will |
| 787 | // cope with a conservative value, and it will take care to purge |
| 788 | // that value once it has finished. |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 789 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(AR->getLoop()); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 790 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | 4ada77f | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 791 | // Manually compute the final value for AR, checking for |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 792 | // overflow. |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 793 | const SCEV *Start = AR->getStart(); |
| 794 | const SCEV *Step = AR->getStepRecurrence(*this); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 795 | |
| 796 | // Check whether the backedge-taken count can be losslessly casted to |
| 797 | // the addrec's type. The count is always unsigned. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 798 | const SCEV *CastedMaxBECount = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 799 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 800 | const SCEV *RecastedMaxBECount = |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 801 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 802 | if (MaxBECount == RecastedMaxBECount) { |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 803 | const Type *WideTy = |
| 804 | IntegerType::get(getTypeSizeInBits(Start->getType()) * 2); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 805 | // Check whether Start+Step*MaxBECount has no unsigned overflow. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 806 | const SCEV *ZMul = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 807 | getMulExpr(CastedMaxBECount, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 808 | getTruncateOrZeroExtend(Step, Start->getType())); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 809 | const SCEV *Add = getAddExpr(Start, ZMul); |
| 810 | const SCEV *OperandExtendedAdd = |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 811 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 812 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 813 | getZeroExtendExpr(Step, WideTy))); |
| 814 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 815 | // Return the expression with the addrec on the outside. |
| 816 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 817 | getZeroExtendExpr(Step, Ty), |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 818 | AR->getLoop()); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 819 | |
| 820 | // Similar to above, only this time treat the step value as signed. |
| 821 | // This covers loops that count down. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 822 | const SCEV *SMul = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 823 | getMulExpr(CastedMaxBECount, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 824 | getTruncateOrSignExtend(Step, Start->getType())); |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 825 | Add = getAddExpr(Start, SMul); |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 826 | OperandExtendedAdd = |
| 827 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 828 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 829 | getSignExtendExpr(Step, WideTy))); |
| 830 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 831 | // Return the expression with the addrec on the outside. |
| 832 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 833 | getSignExtendExpr(Step, Ty), |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 834 | AR->getLoop()); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 838 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 839 | FoldingSetNodeID ID; |
| 840 | ID.AddInteger(scZeroExtend); |
| 841 | ID.AddPointer(Op); |
| 842 | ID.AddPointer(Ty); |
| 843 | void *IP = 0; |
| 844 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 845 | SCEV *S = SCEVAllocator.Allocate<SCEVZeroExtendExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 846 | new (S) SCEVZeroExtendExpr(ID, Op, Ty); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 847 | UniqueSCEVs.InsertNode(S, IP); |
| 848 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 851 | const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 852 | const Type *Ty) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 853 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 854 | "This is not an extending conversion!"); |
Dan Gohman | 13a51e2 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 855 | assert(isSCEVable(Ty) && |
| 856 | "This is not a conversion to a SCEVable type!"); |
| 857 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 858 | |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 859 | // Fold if the operand is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 860 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 861 | const Type *IntTy = getEffectiveSCEVType(Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 862 | Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy); |
| 863 | if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 864 | return getConstant(cast<ConstantInt>(C)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 865 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 866 | |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 867 | // sext(sext(x)) --> sext(x) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 868 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 869 | return getSignExtendExpr(SS->getOperand(), Ty); |
| 870 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 871 | // 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] | 872 | // 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] | 873 | // operands (often constants). This allows analysis of something like |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 874 | // this: for (signed char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 875 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 876 | if (AR->isAffine()) { |
| 877 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 878 | // Note that this serves two purposes: It filters out loops that are |
| 879 | // simply not analyzable, and it covers the case where this code is |
| 880 | // being called from within backedge-taken count analysis, such that |
| 881 | // attempting to ask for the backedge-taken count would likely result |
| 882 | // in infinite recursion. In the later case, the analysis code will |
| 883 | // cope with a conservative value, and it will take care to purge |
| 884 | // that value once it has finished. |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 885 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(AR->getLoop()); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 886 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | 4ada77f | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 887 | // Manually compute the final value for AR, checking for |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 888 | // overflow. |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 889 | const SCEV *Start = AR->getStart(); |
| 890 | const SCEV *Step = AR->getStepRecurrence(*this); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 891 | |
| 892 | // Check whether the backedge-taken count can be losslessly casted to |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 893 | // the addrec's type. The count is always unsigned. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 894 | const SCEV *CastedMaxBECount = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 895 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 896 | const SCEV *RecastedMaxBECount = |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 897 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 898 | if (MaxBECount == RecastedMaxBECount) { |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 899 | const Type *WideTy = |
| 900 | IntegerType::get(getTypeSizeInBits(Start->getType()) * 2); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 901 | // Check whether Start+Step*MaxBECount has no signed overflow. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 902 | const SCEV *SMul = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 903 | getMulExpr(CastedMaxBECount, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 904 | getTruncateOrSignExtend(Step, Start->getType())); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 905 | const SCEV *Add = getAddExpr(Start, SMul); |
| 906 | const SCEV *OperandExtendedAdd = |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 907 | getAddExpr(getSignExtendExpr(Start, WideTy), |
| 908 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 909 | getSignExtendExpr(Step, WideTy))); |
| 910 | if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 911 | // Return the expression with the addrec on the outside. |
| 912 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 913 | getSignExtendExpr(Step, Ty), |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 914 | AR->getLoop()); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 915 | } |
| 916 | } |
| 917 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 918 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 919 | FoldingSetNodeID ID; |
| 920 | ID.AddInteger(scSignExtend); |
| 921 | ID.AddPointer(Op); |
| 922 | ID.AddPointer(Ty); |
| 923 | void *IP = 0; |
| 924 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 925 | SCEV *S = SCEVAllocator.Allocate<SCEVSignExtendExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 926 | new (S) SCEVSignExtendExpr(ID, Op, Ty); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 927 | UniqueSCEVs.InsertNode(S, IP); |
| 928 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 931 | /// getAnyExtendExpr - Return a SCEV for the given operand extended with |
| 932 | /// unspecified bits out to the given type. |
| 933 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 934 | const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op, |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 935 | const Type *Ty) { |
| 936 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
| 937 | "This is not an extending conversion!"); |
| 938 | assert(isSCEVable(Ty) && |
| 939 | "This is not a conversion to a SCEVable type!"); |
| 940 | Ty = getEffectiveSCEVType(Ty); |
| 941 | |
| 942 | // Sign-extend negative constants. |
| 943 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| 944 | if (SC->getValue()->getValue().isNegative()) |
| 945 | return getSignExtendExpr(Op, Ty); |
| 946 | |
| 947 | // Peel off a truncate cast. |
| 948 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 949 | const SCEV *NewOp = T->getOperand(); |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 950 | if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty)) |
| 951 | return getAnyExtendExpr(NewOp, Ty); |
| 952 | return getTruncateOrNoop(NewOp, Ty); |
| 953 | } |
| 954 | |
| 955 | // Next try a zext cast. If the cast is folded, use it. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 956 | const SCEV *ZExt = getZeroExtendExpr(Op, Ty); |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 957 | if (!isa<SCEVZeroExtendExpr>(ZExt)) |
| 958 | return ZExt; |
| 959 | |
| 960 | // Next try a sext cast. If the cast is folded, use it. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 961 | const SCEV *SExt = getSignExtendExpr(Op, Ty); |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 962 | if (!isa<SCEVSignExtendExpr>(SExt)) |
| 963 | return SExt; |
| 964 | |
| 965 | // If the expression is obviously signed, use the sext cast value. |
| 966 | if (isa<SCEVSMaxExpr>(Op)) |
| 967 | return SExt; |
| 968 | |
| 969 | // Absent any other information, use the zext cast value. |
| 970 | return ZExt; |
| 971 | } |
| 972 | |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 973 | /// CollectAddOperandsWithScales - Process the given Ops list, which is |
| 974 | /// a list of operands to be added under the given scale, update the given |
| 975 | /// map. This is a helper function for getAddRecExpr. As an example of |
| 976 | /// what it does, given a sequence of operands that would form an add |
| 977 | /// expression like this: |
| 978 | /// |
| 979 | /// m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r) |
| 980 | /// |
| 981 | /// where A and B are constants, update the map with these values: |
| 982 | /// |
| 983 | /// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0) |
| 984 | /// |
| 985 | /// and add 13 + A*B*29 to AccumulatedConstant. |
| 986 | /// This will allow getAddRecExpr to produce this: |
| 987 | /// |
| 988 | /// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B) |
| 989 | /// |
| 990 | /// This form often exposes folding opportunities that are hidden in |
| 991 | /// the original operand list. |
| 992 | /// |
| 993 | /// Return true iff it appears that any interesting folding opportunities |
| 994 | /// may be exposed. This helps getAddRecExpr short-circuit extra work in |
| 995 | /// the common case where no interesting opportunities are present, and |
| 996 | /// is also used as a check to avoid infinite recursion. |
| 997 | /// |
| 998 | static bool |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 999 | CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M, |
| 1000 | SmallVector<const SCEV *, 8> &NewOps, |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1001 | APInt &AccumulatedConstant, |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1002 | const SmallVectorImpl<const SCEV *> &Ops, |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1003 | const APInt &Scale, |
| 1004 | ScalarEvolution &SE) { |
| 1005 | bool Interesting = false; |
| 1006 | |
| 1007 | // Iterate over the add operands. |
| 1008 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 1009 | const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]); |
| 1010 | if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) { |
| 1011 | APInt NewScale = |
| 1012 | Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue(); |
| 1013 | if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) { |
| 1014 | // A multiplication of a constant with another add; recurse. |
| 1015 | Interesting |= |
| 1016 | CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
| 1017 | cast<SCEVAddExpr>(Mul->getOperand(1)) |
| 1018 | ->getOperands(), |
| 1019 | NewScale, SE); |
| 1020 | } else { |
| 1021 | // A multiplication of a constant with some other value. Update |
| 1022 | // the map. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1023 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end()); |
| 1024 | const SCEV *Key = SE.getMulExpr(MulOps); |
| 1025 | std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = |
Dan Gohman | 3bf01f0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1026 | M.insert(std::make_pair(Key, NewScale)); |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1027 | if (Pair.second) { |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1028 | NewOps.push_back(Pair.first->first); |
| 1029 | } else { |
| 1030 | Pair.first->second += NewScale; |
| 1031 | // The map already had an entry for this value, which may indicate |
| 1032 | // a folding opportunity. |
| 1033 | Interesting = true; |
| 1034 | } |
| 1035 | } |
| 1036 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| 1037 | // Pull a buried constant out to the outside. |
| 1038 | if (Scale != 1 || AccumulatedConstant != 0 || C->isZero()) |
| 1039 | Interesting = true; |
| 1040 | AccumulatedConstant += Scale * C->getValue()->getValue(); |
| 1041 | } else { |
| 1042 | // An ordinary operand. Update the map. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1043 | std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = |
Dan Gohman | 3bf01f0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1044 | M.insert(std::make_pair(Ops[i], Scale)); |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1045 | if (Pair.second) { |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1046 | NewOps.push_back(Pair.first->first); |
| 1047 | } else { |
| 1048 | Pair.first->second += Scale; |
| 1049 | // The map already had an entry for this value, which may indicate |
| 1050 | // a folding opportunity. |
| 1051 | Interesting = true; |
| 1052 | } |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | return Interesting; |
| 1057 | } |
| 1058 | |
| 1059 | namespace { |
| 1060 | struct APIntCompare { |
| 1061 | bool operator()(const APInt &LHS, const APInt &RHS) const { |
| 1062 | return LHS.ult(RHS); |
| 1063 | } |
| 1064 | }; |
| 1065 | } |
| 1066 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1067 | /// getAddExpr - Get a canonical add expression, or something simpler if |
| 1068 | /// possible. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1069 | const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1070 | assert(!Ops.empty() && "Cannot get empty add!"); |
| 1071 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1072 | #ifndef NDEBUG |
| 1073 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1074 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1075 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1076 | "SCEVAddExpr operand types don't match!"); |
| 1077 | #endif |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1078 | |
| 1079 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1080 | GroupByComplexity(Ops, LI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1081 | |
| 1082 | // If there are any constants, fold them together. |
| 1083 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1084 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1085 | ++Idx; |
| 1086 | assert(Idx < Ops.size()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1087 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1088 | // We found two constants, fold them together! |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1089 | Ops[0] = getConstant(LHSC->getValue()->getValue() + |
| 1090 | RHSC->getValue()->getValue()); |
Dan Gohman | 68f23e8 | 2009-06-14 22:53:57 +0000 | [diff] [blame] | 1091 | if (Ops.size() == 2) return Ops[0]; |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1092 | Ops.erase(Ops.begin()+1); // Erase the folded element |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1093 | LHSC = cast<SCEVConstant>(Ops[0]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | // If we are left with a constant zero being added, strip it off. |
| 1097 | if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
| 1098 | Ops.erase(Ops.begin()); |
| 1099 | --Idx; |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | if (Ops.size() == 1) return Ops[0]; |
| 1104 | |
| 1105 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1106 | // so, merge them together into an multiply expression. Since we sorted the |
| 1107 | // list, these values are required to be adjacent. |
| 1108 | const Type *Ty = Ops[0]->getType(); |
| 1109 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1110 | if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2 |
| 1111 | // Found a match, merge the two values into a multiply, and add any |
| 1112 | // remaining values to the result. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1113 | const SCEV *Two = getIntegerSCEV(2, Ty); |
| 1114 | const SCEV *Mul = getMulExpr(Ops[i], Two); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1115 | if (Ops.size() == 2) |
| 1116 | return Mul; |
| 1117 | Ops.erase(Ops.begin()+i, Ops.begin()+i+2); |
| 1118 | Ops.push_back(Mul); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1119 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1122 | // Check for truncates. If all the operands are truncated from the same |
| 1123 | // type, see if factoring out the truncate would permit the result to be |
| 1124 | // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n) |
| 1125 | // if the contents of the resulting outer trunc fold to something simple. |
| 1126 | for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) { |
| 1127 | const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]); |
| 1128 | const Type *DstType = Trunc->getType(); |
| 1129 | const Type *SrcType = Trunc->getOperand()->getType(); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1130 | SmallVector<const SCEV *, 8> LargeOps; |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1131 | bool Ok = true; |
| 1132 | // Check all the operands to see if they can be represented in the |
| 1133 | // source type of the truncate. |
| 1134 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 1135 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) { |
| 1136 | if (T->getOperand()->getType() != SrcType) { |
| 1137 | Ok = false; |
| 1138 | break; |
| 1139 | } |
| 1140 | LargeOps.push_back(T->getOperand()); |
| 1141 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| 1142 | // This could be either sign or zero extension, but sign extension |
| 1143 | // is much more likely to be foldable here. |
| 1144 | LargeOps.push_back(getSignExtendExpr(C, SrcType)); |
| 1145 | } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1146 | SmallVector<const SCEV *, 8> LargeMulOps; |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1147 | for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { |
| 1148 | if (const SCEVTruncateExpr *T = |
| 1149 | dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) { |
| 1150 | if (T->getOperand()->getType() != SrcType) { |
| 1151 | Ok = false; |
| 1152 | break; |
| 1153 | } |
| 1154 | LargeMulOps.push_back(T->getOperand()); |
| 1155 | } else if (const SCEVConstant *C = |
| 1156 | dyn_cast<SCEVConstant>(M->getOperand(j))) { |
| 1157 | // This could be either sign or zero extension, but sign extension |
| 1158 | // is much more likely to be foldable here. |
| 1159 | LargeMulOps.push_back(getSignExtendExpr(C, SrcType)); |
| 1160 | } else { |
| 1161 | Ok = false; |
| 1162 | break; |
| 1163 | } |
| 1164 | } |
| 1165 | if (Ok) |
| 1166 | LargeOps.push_back(getMulExpr(LargeMulOps)); |
| 1167 | } else { |
| 1168 | Ok = false; |
| 1169 | break; |
| 1170 | } |
| 1171 | } |
| 1172 | if (Ok) { |
| 1173 | // Evaluate the expression in the larger type. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1174 | const SCEV *Fold = getAddExpr(LargeOps); |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1175 | // If it folds to something simple, use it. Otherwise, don't. |
| 1176 | if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold)) |
| 1177 | return getTruncateExpr(Fold, DstType); |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | // Skip past any other cast SCEVs. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1182 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) |
| 1183 | ++Idx; |
| 1184 | |
| 1185 | // If there are add operands they would be next. |
| 1186 | if (Idx < Ops.size()) { |
| 1187 | bool DeletedAdd = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1188 | while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1189 | // If we have an add, expand the add operands onto the end of the operands |
| 1190 | // list. |
| 1191 | Ops.insert(Ops.end(), Add->op_begin(), Add->op_end()); |
| 1192 | Ops.erase(Ops.begin()+Idx); |
| 1193 | DeletedAdd = true; |
| 1194 | } |
| 1195 | |
| 1196 | // If we deleted at least one add, we added operands to the end of the list, |
| 1197 | // and they are not necessarily sorted. Recurse to resort and resimplify |
| 1198 | // any operands we just aquired. |
| 1199 | if (DeletedAdd) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1200 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | // Skip over the add expression until we get to a multiply. |
| 1204 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1205 | ++Idx; |
| 1206 | |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1207 | // Check to see if there are any folding opportunities present with |
| 1208 | // operands multiplied by constant values. |
| 1209 | if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) { |
| 1210 | uint64_t BitWidth = getTypeSizeInBits(Ty); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1211 | DenseMap<const SCEV *, APInt> M; |
| 1212 | SmallVector<const SCEV *, 8> NewOps; |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1213 | APInt AccumulatedConstant(BitWidth, 0); |
| 1214 | if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
| 1215 | Ops, APInt(BitWidth, 1), *this)) { |
| 1216 | // Some interesting folding opportunity is present, so its worthwhile to |
| 1217 | // re-generate the operands list. Group the operands by constant scale, |
| 1218 | // to avoid multiplying by the same constant scale multiple times. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1219 | std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists; |
| 1220 | for (SmallVector<const SCEV *, 8>::iterator I = NewOps.begin(), |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1221 | E = NewOps.end(); I != E; ++I) |
| 1222 | MulOpLists[M.find(*I)->second].push_back(*I); |
| 1223 | // Re-generate the operands list. |
| 1224 | Ops.clear(); |
| 1225 | if (AccumulatedConstant != 0) |
| 1226 | Ops.push_back(getConstant(AccumulatedConstant)); |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1227 | for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator |
| 1228 | I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I) |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1229 | if (I->first != 0) |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1230 | Ops.push_back(getMulExpr(getConstant(I->first), |
| 1231 | getAddExpr(I->second))); |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1232 | if (Ops.empty()) |
| 1233 | return getIntegerSCEV(0, Ty); |
| 1234 | if (Ops.size() == 1) |
| 1235 | return Ops[0]; |
| 1236 | return getAddExpr(Ops); |
| 1237 | } |
| 1238 | } |
| 1239 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1240 | // If we are adding something to a multiply expression, make sure the |
| 1241 | // something is not already an operand of the multiply. If so, merge it into |
| 1242 | // the multiply. |
| 1243 | for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1244 | const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1245 | for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1246 | const SCEV *MulOpSCEV = Mul->getOperand(MulOp); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1247 | for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1248 | if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(Ops[AddOp])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1249 | // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1250 | const SCEV *InnerMul = Mul->getOperand(MulOp == 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1251 | if (Mul->getNumOperands() != 2) { |
| 1252 | // If the multiply has more than two operands, we must get the |
| 1253 | // Y*Z term. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1254 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), Mul->op_end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1255 | MulOps.erase(MulOps.begin()+MulOp); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1256 | InnerMul = getMulExpr(MulOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1257 | } |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1258 | const SCEV *One = getIntegerSCEV(1, Ty); |
| 1259 | const SCEV *AddOne = getAddExpr(InnerMul, One); |
| 1260 | const SCEV *OuterMul = getMulExpr(AddOne, Ops[AddOp]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1261 | if (Ops.size() == 2) return OuterMul; |
| 1262 | if (AddOp < Idx) { |
| 1263 | Ops.erase(Ops.begin()+AddOp); |
| 1264 | Ops.erase(Ops.begin()+Idx-1); |
| 1265 | } else { |
| 1266 | Ops.erase(Ops.begin()+Idx); |
| 1267 | Ops.erase(Ops.begin()+AddOp-1); |
| 1268 | } |
| 1269 | Ops.push_back(OuterMul); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1270 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | // Check this multiply against other multiplies being added together. |
| 1274 | for (unsigned OtherMulIdx = Idx+1; |
| 1275 | OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]); |
| 1276 | ++OtherMulIdx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1277 | const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1278 | // If MulOp occurs in OtherMul, we can fold the two multiplies |
| 1279 | // together. |
| 1280 | for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); |
| 1281 | OMulOp != e; ++OMulOp) |
| 1282 | if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { |
| 1283 | // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E)) |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1284 | const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1285 | if (Mul->getNumOperands() != 2) { |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1286 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), |
| 1287 | Mul->op_end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1288 | MulOps.erase(MulOps.begin()+MulOp); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1289 | InnerMul1 = getMulExpr(MulOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1290 | } |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1291 | const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1292 | if (OtherMul->getNumOperands() != 2) { |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1293 | SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(), |
| 1294 | OtherMul->op_end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1295 | MulOps.erase(MulOps.begin()+OMulOp); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1296 | InnerMul2 = getMulExpr(MulOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1297 | } |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1298 | const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2); |
| 1299 | const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1300 | if (Ops.size() == 2) return OuterMul; |
| 1301 | Ops.erase(Ops.begin()+Idx); |
| 1302 | Ops.erase(Ops.begin()+OtherMulIdx-1); |
| 1303 | Ops.push_back(OuterMul); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1304 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1305 | } |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | // If there are any add recurrences in the operands list, see if any other |
| 1311 | // added values are loop invariant. If so, we can fold them into the |
| 1312 | // recurrence. |
| 1313 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1314 | ++Idx; |
| 1315 | |
| 1316 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1317 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1318 | // Scan all of the other operands to this add and add them to the vector if |
| 1319 | // they are loop invariant w.r.t. the recurrence. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1320 | SmallVector<const SCEV *, 8> LIOps; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1321 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1322 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1323 | if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { |
| 1324 | LIOps.push_back(Ops[i]); |
| 1325 | Ops.erase(Ops.begin()+i); |
| 1326 | --i; --e; |
| 1327 | } |
| 1328 | |
| 1329 | // If we found some loop invariants, fold them into the recurrence. |
| 1330 | if (!LIOps.empty()) { |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1331 | // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1332 | LIOps.push_back(AddRec->getStart()); |
| 1333 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1334 | SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(), |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1335 | AddRec->op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1336 | AddRecOps[0] = getAddExpr(LIOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1337 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1338 | const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1339 | // If all of the other operands were loop invariant, we are done. |
| 1340 | if (Ops.size() == 1) return NewRec; |
| 1341 | |
| 1342 | // Otherwise, add the folded AddRec by the non-liv parts. |
| 1343 | for (unsigned i = 0;; ++i) |
| 1344 | if (Ops[i] == AddRec) { |
| 1345 | Ops[i] = NewRec; |
| 1346 | break; |
| 1347 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1348 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1352 | // there are multiple AddRec's with the same loop induction variable being |
| 1353 | // added together. If so, we can fold them. |
| 1354 | for (unsigned OtherIdx = Idx+1; |
| 1355 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) |
| 1356 | if (OtherIdx != Idx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1357 | const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1358 | if (AddRec->getLoop() == OtherAddRec->getLoop()) { |
| 1359 | // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D} |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1360 | SmallVector<const SCEV *, 4> NewOps(AddRec->op_begin(), |
| 1361 | AddRec->op_end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1362 | for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) { |
| 1363 | if (i >= NewOps.size()) { |
| 1364 | NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i, |
| 1365 | OtherAddRec->op_end()); |
| 1366 | break; |
| 1367 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1368 | NewOps[i] = getAddExpr(NewOps[i], OtherAddRec->getOperand(i)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1369 | } |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1370 | const SCEV *NewAddRec = getAddRecExpr(NewOps, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1371 | |
| 1372 | if (Ops.size() == 2) return NewAddRec; |
| 1373 | |
| 1374 | Ops.erase(Ops.begin()+Idx); |
| 1375 | Ops.erase(Ops.begin()+OtherIdx-1); |
| 1376 | Ops.push_back(NewAddRec); |
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 | |
| 1381 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1382 | // next one. |
| 1383 | } |
| 1384 | |
| 1385 | // Okay, it looks like we really DO need an add expr. Check to see if we |
| 1386 | // already have one, otherwise create a new one. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1387 | FoldingSetNodeID ID; |
| 1388 | ID.AddInteger(scAddExpr); |
| 1389 | ID.AddInteger(Ops.size()); |
| 1390 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1391 | ID.AddPointer(Ops[i]); |
| 1392 | void *IP = 0; |
| 1393 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1394 | SCEV *S = SCEVAllocator.Allocate<SCEVAddExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1395 | new (S) SCEVAddExpr(ID, Ops); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1396 | UniqueSCEVs.InsertNode(S, IP); |
| 1397 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1401 | /// getMulExpr - Get a canonical multiply expression, or something simpler if |
| 1402 | /// possible. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1403 | const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1404 | assert(!Ops.empty() && "Cannot get empty mul!"); |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1405 | #ifndef NDEBUG |
| 1406 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1407 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1408 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1409 | "SCEVMulExpr operand types don't match!"); |
| 1410 | #endif |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1411 | |
| 1412 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1413 | GroupByComplexity(Ops, LI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1414 | |
| 1415 | // If there are any constants, fold them together. |
| 1416 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1417 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1418 | |
| 1419 | // C1*(C2+V) -> C1*C2 + C1*V |
| 1420 | if (Ops.size() == 2) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1421 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1422 | if (Add->getNumOperands() == 2 && |
| 1423 | isa<SCEVConstant>(Add->getOperand(0))) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1424 | return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)), |
| 1425 | getMulExpr(LHSC, Add->getOperand(1))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1426 | |
| 1427 | |
| 1428 | ++Idx; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1429 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1430 | // We found two constants, fold them together! |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1431 | ConstantInt *Fold = ConstantInt::get(LHSC->getValue()->getValue() * |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1432 | RHSC->getValue()->getValue()); |
| 1433 | Ops[0] = getConstant(Fold); |
| 1434 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1435 | if (Ops.size() == 1) return Ops[0]; |
| 1436 | LHSC = cast<SCEVConstant>(Ops[0]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | // If we are left with a constant one being multiplied, strip it off. |
| 1440 | if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) { |
| 1441 | Ops.erase(Ops.begin()); |
| 1442 | --Idx; |
| 1443 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
| 1444 | // If we have a multiply of zero, it will always be zero. |
| 1445 | return Ops[0]; |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | // Skip over the add expression until we get to a multiply. |
| 1450 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1451 | ++Idx; |
| 1452 | |
| 1453 | if (Ops.size() == 1) |
| 1454 | return Ops[0]; |
| 1455 | |
| 1456 | // If there are mul operands inline them all into this expression. |
| 1457 | if (Idx < Ops.size()) { |
| 1458 | bool DeletedMul = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1459 | while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1460 | // If we have an mul, expand the mul operands onto the end of the operands |
| 1461 | // list. |
| 1462 | Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end()); |
| 1463 | Ops.erase(Ops.begin()+Idx); |
| 1464 | DeletedMul = true; |
| 1465 | } |
| 1466 | |
| 1467 | // If we deleted at least one mul, we added operands to the end of the list, |
| 1468 | // and they are not necessarily sorted. Recurse to resort and resimplify |
| 1469 | // any operands we just aquired. |
| 1470 | if (DeletedMul) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1471 | return getMulExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | // If there are any add recurrences in the operands list, see if any other |
| 1475 | // added values are loop invariant. If so, we can fold them into the |
| 1476 | // recurrence. |
| 1477 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1478 | ++Idx; |
| 1479 | |
| 1480 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1481 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1482 | // Scan all of the other operands to this mul and add them to the vector if |
| 1483 | // they are loop invariant w.r.t. the recurrence. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1484 | SmallVector<const SCEV *, 8> LIOps; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1485 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1486 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1487 | if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { |
| 1488 | LIOps.push_back(Ops[i]); |
| 1489 | Ops.erase(Ops.begin()+i); |
| 1490 | --i; --e; |
| 1491 | } |
| 1492 | |
| 1493 | // If we found some loop invariants, fold them into the recurrence. |
| 1494 | if (!LIOps.empty()) { |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1495 | // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step} |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1496 | SmallVector<const SCEV *, 4> NewOps; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1497 | NewOps.reserve(AddRec->getNumOperands()); |
| 1498 | if (LIOps.size() == 1) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1499 | const SCEV *Scale = LIOps[0]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1500 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1501 | NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1502 | } else { |
| 1503 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1504 | SmallVector<const SCEV *, 4> MulOps(LIOps.begin(), LIOps.end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1505 | MulOps.push_back(AddRec->getOperand(i)); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1506 | NewOps.push_back(getMulExpr(MulOps)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1507 | } |
| 1508 | } |
| 1509 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1510 | const SCEV *NewRec = getAddRecExpr(NewOps, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1511 | |
| 1512 | // If all of the other operands were loop invariant, we are done. |
| 1513 | if (Ops.size() == 1) return NewRec; |
| 1514 | |
| 1515 | // Otherwise, multiply the folded AddRec by the non-liv parts. |
| 1516 | for (unsigned i = 0;; ++i) |
| 1517 | if (Ops[i] == AddRec) { |
| 1518 | Ops[i] = NewRec; |
| 1519 | break; |
| 1520 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1521 | return getMulExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1525 | // there are multiple AddRec's with the same loop induction variable being |
| 1526 | // multiplied together. If so, we can fold them. |
| 1527 | for (unsigned OtherIdx = Idx+1; |
| 1528 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) |
| 1529 | if (OtherIdx != Idx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1530 | const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1531 | if (AddRec->getLoop() == OtherAddRec->getLoop()) { |
| 1532 | // 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] | 1533 | const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec; |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1534 | const SCEV *NewStart = getMulExpr(F->getStart(), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1535 | G->getStart()); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1536 | const SCEV *B = F->getStepRecurrence(*this); |
| 1537 | const SCEV *D = G->getStepRecurrence(*this); |
| 1538 | const SCEV *NewStep = getAddExpr(getMulExpr(F, D), |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1539 | getMulExpr(G, B), |
| 1540 | getMulExpr(B, D)); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1541 | const SCEV *NewAddRec = getAddRecExpr(NewStart, NewStep, |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1542 | F->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1543 | if (Ops.size() == 2) return NewAddRec; |
| 1544 | |
| 1545 | Ops.erase(Ops.begin()+Idx); |
| 1546 | Ops.erase(Ops.begin()+OtherIdx-1); |
| 1547 | Ops.push_back(NewAddRec); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1548 | return getMulExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1553 | // next one. |
| 1554 | } |
| 1555 | |
| 1556 | // Okay, it looks like we really DO need an mul expr. Check to see if we |
| 1557 | // already have one, otherwise create a new one. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1558 | FoldingSetNodeID ID; |
| 1559 | ID.AddInteger(scMulExpr); |
| 1560 | ID.AddInteger(Ops.size()); |
| 1561 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1562 | ID.AddPointer(Ops[i]); |
| 1563 | void *IP = 0; |
| 1564 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1565 | SCEV *S = SCEVAllocator.Allocate<SCEVMulExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1566 | new (S) SCEVMulExpr(ID, Ops); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1567 | UniqueSCEVs.InsertNode(S, IP); |
| 1568 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1571 | /// getUDivExpr - Get a canonical multiply expression, or something simpler if |
| 1572 | /// possible. |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1573 | const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS, |
| 1574 | const SCEV *RHS) { |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1575 | assert(getEffectiveSCEVType(LHS->getType()) == |
| 1576 | getEffectiveSCEVType(RHS->getType()) && |
| 1577 | "SCEVUDivExpr operand types don't match!"); |
| 1578 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1579 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1580 | if (RHSC->getValue()->equalsInt(1)) |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 1581 | return LHS; // X udiv 1 --> x |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1582 | if (RHSC->isZero()) |
| 1583 | return getIntegerSCEV(0, LHS->getType()); // value is undefined |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1584 | |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1585 | // Determine if the division can be folded into the operands of |
| 1586 | // its operands. |
| 1587 | // TODO: Generalize this to non-constants by using known-bits information. |
| 1588 | const Type *Ty = LHS->getType(); |
| 1589 | unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros(); |
| 1590 | unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ; |
| 1591 | // For non-power-of-two values, effectively round the value up to the |
| 1592 | // nearest power of two. |
| 1593 | if (!RHSC->getValue()->getValue().isPowerOf2()) |
| 1594 | ++MaxShiftAmt; |
| 1595 | const IntegerType *ExtTy = |
| 1596 | IntegerType::get(getTypeSizeInBits(Ty) + MaxShiftAmt); |
| 1597 | // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. |
| 1598 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) |
| 1599 | if (const SCEVConstant *Step = |
| 1600 | dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) |
| 1601 | if (!Step->getValue()->getValue() |
| 1602 | .urem(RHSC->getValue()->getValue()) && |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1603 | getZeroExtendExpr(AR, ExtTy) == |
| 1604 | getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), |
| 1605 | getZeroExtendExpr(Step, ExtTy), |
| 1606 | AR->getLoop())) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1607 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1608 | for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i) |
| 1609 | Operands.push_back(getUDivExpr(AR->getOperand(i), RHS)); |
| 1610 | return getAddRecExpr(Operands, AR->getLoop()); |
| 1611 | } |
| 1612 | // (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] | 1613 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1614 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1615 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) |
| 1616 | Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy)); |
| 1617 | if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1618 | // Find an operand that's safely divisible. |
| 1619 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1620 | const SCEV *Op = M->getOperand(i); |
| 1621 | const SCEV *Div = getUDivExpr(Op, RHSC); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1622 | if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1623 | const SmallVectorImpl<const SCEV *> &MOperands = M->getOperands(); |
| 1624 | Operands = SmallVector<const SCEV *, 4>(MOperands.begin(), |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1625 | MOperands.end()); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1626 | Operands[i] = Div; |
| 1627 | return getMulExpr(Operands); |
| 1628 | } |
| 1629 | } |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1630 | } |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1631 | // (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] | 1632 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1633 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1634 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) |
| 1635 | Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy)); |
| 1636 | if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { |
| 1637 | Operands.clear(); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1638 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1639 | const SCEV *Op = getUDivExpr(A->getOperand(i), RHS); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1640 | if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i)) |
| 1641 | break; |
| 1642 | Operands.push_back(Op); |
| 1643 | } |
| 1644 | if (Operands.size() == A->getNumOperands()) |
| 1645 | return getAddExpr(Operands); |
| 1646 | } |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1647 | } |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1648 | |
| 1649 | // Fold if both operands are constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1650 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1651 | Constant *LHSCV = LHSC->getValue(); |
| 1652 | Constant *RHSCV = RHSC->getValue(); |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 1653 | return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV, |
| 1654 | RHSCV))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1655 | } |
| 1656 | } |
| 1657 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1658 | FoldingSetNodeID ID; |
| 1659 | ID.AddInteger(scUDivExpr); |
| 1660 | ID.AddPointer(LHS); |
| 1661 | ID.AddPointer(RHS); |
| 1662 | void *IP = 0; |
| 1663 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1664 | SCEV *S = SCEVAllocator.Allocate<SCEVUDivExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1665 | new (S) SCEVUDivExpr(ID, LHS, RHS); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1666 | UniqueSCEVs.InsertNode(S, IP); |
| 1667 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1671 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 1672 | /// Simplify the expression as much as possible. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1673 | const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, |
| 1674 | const SCEV *Step, const Loop *L) { |
| 1675 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1676 | Operands.push_back(Start); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1677 | if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1678 | if (StepChrec->getLoop() == L) { |
| 1679 | Operands.insert(Operands.end(), StepChrec->op_begin(), |
| 1680 | StepChrec->op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1681 | return getAddRecExpr(Operands, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | Operands.push_back(Step); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1685 | return getAddRecExpr(Operands, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1688 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 1689 | /// Simplify the expression as much as possible. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1690 | const SCEV * |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1691 | ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands, |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1692 | const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1693 | if (Operands.size() == 1) return Operands[0]; |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1694 | #ifndef NDEBUG |
| 1695 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 1696 | assert(getEffectiveSCEVType(Operands[i]->getType()) == |
| 1697 | getEffectiveSCEVType(Operands[0]->getType()) && |
| 1698 | "SCEVAddRecExpr operand types don't match!"); |
| 1699 | #endif |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1700 | |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1701 | if (Operands.back()->isZero()) { |
| 1702 | Operands.pop_back(); |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1703 | return getAddRecExpr(Operands, L); // {X,+,0} --> X |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1704 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1705 | |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1706 | // Canonicalize nested AddRecs in by nesting them in order of loop depth. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1707 | if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) { |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1708 | const Loop* NestedLoop = NestedAR->getLoop(); |
| 1709 | if (L->getLoopDepth() < NestedLoop->getLoopDepth()) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1710 | SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(), |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1711 | NestedAR->op_end()); |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1712 | Operands[0] = NestedAR->getStart(); |
Dan Gohman | 08c4c07 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 1713 | // AddRecs require their operands be loop-invariant with respect to their |
| 1714 | // loops. Don't perform this transformation if it would break this |
| 1715 | // requirement. |
| 1716 | bool AllInvariant = true; |
| 1717 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 1718 | if (!Operands[i]->isLoopInvariant(L)) { |
| 1719 | AllInvariant = false; |
| 1720 | break; |
| 1721 | } |
| 1722 | if (AllInvariant) { |
| 1723 | NestedOperands[0] = getAddRecExpr(Operands, L); |
| 1724 | AllInvariant = true; |
| 1725 | for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i) |
| 1726 | if (!NestedOperands[i]->isLoopInvariant(NestedLoop)) { |
| 1727 | AllInvariant = false; |
| 1728 | break; |
| 1729 | } |
| 1730 | if (AllInvariant) |
| 1731 | // Ok, both add recurrences are valid after the transformation. |
| 1732 | return getAddRecExpr(NestedOperands, NestedLoop); |
| 1733 | } |
| 1734 | // Reset Operands to its original state. |
| 1735 | Operands[0] = NestedAR; |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1736 | } |
| 1737 | } |
| 1738 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1739 | FoldingSetNodeID ID; |
| 1740 | ID.AddInteger(scAddRecExpr); |
| 1741 | ID.AddInteger(Operands.size()); |
| 1742 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 1743 | ID.AddPointer(Operands[i]); |
| 1744 | ID.AddPointer(L); |
| 1745 | void *IP = 0; |
| 1746 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1747 | SCEV *S = SCEVAllocator.Allocate<SCEVAddRecExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1748 | new (S) SCEVAddRecExpr(ID, Operands, L); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1749 | UniqueSCEVs.InsertNode(S, IP); |
| 1750 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1753 | const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, |
| 1754 | const SCEV *RHS) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1755 | SmallVector<const SCEV *, 2> Ops; |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1756 | Ops.push_back(LHS); |
| 1757 | Ops.push_back(RHS); |
| 1758 | return getSMaxExpr(Ops); |
| 1759 | } |
| 1760 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1761 | const SCEV * |
| 1762 | ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1763 | assert(!Ops.empty() && "Cannot get empty smax!"); |
| 1764 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1765 | #ifndef NDEBUG |
| 1766 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1767 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1768 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1769 | "SCEVSMaxExpr operand types don't match!"); |
| 1770 | #endif |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1771 | |
| 1772 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1773 | GroupByComplexity(Ops, LI); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1774 | |
| 1775 | // If there are any constants, fold them together. |
| 1776 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1777 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1778 | ++Idx; |
| 1779 | assert(Idx < Ops.size()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1780 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1781 | // We found two constants, fold them together! |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1782 | ConstantInt *Fold = ConstantInt::get( |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1783 | APIntOps::smax(LHSC->getValue()->getValue(), |
| 1784 | RHSC->getValue()->getValue())); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1785 | Ops[0] = getConstant(Fold); |
| 1786 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1787 | if (Ops.size() == 1) return Ops[0]; |
| 1788 | LHSC = cast<SCEVConstant>(Ops[0]); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
Dan Gohman | d156c09 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1791 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1792 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) { |
| 1793 | Ops.erase(Ops.begin()); |
| 1794 | --Idx; |
Dan Gohman | d156c09 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1795 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) { |
| 1796 | // If we have an smax with a constant maximum-int, it will always be |
| 1797 | // maximum-int. |
| 1798 | return Ops[0]; |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | if (Ops.size() == 1) return Ops[0]; |
| 1803 | |
| 1804 | // Find the first SMax |
| 1805 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr) |
| 1806 | ++Idx; |
| 1807 | |
| 1808 | // Check to see if one of the operands is an SMax. If so, expand its operands |
| 1809 | // onto our operand list, and recurse to simplify. |
| 1810 | if (Idx < Ops.size()) { |
| 1811 | bool DeletedSMax = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1812 | while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1813 | Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end()); |
| 1814 | Ops.erase(Ops.begin()+Idx); |
| 1815 | DeletedSMax = true; |
| 1816 | } |
| 1817 | |
| 1818 | if (DeletedSMax) |
| 1819 | return getSMaxExpr(Ops); |
| 1820 | } |
| 1821 | |
| 1822 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1823 | // so, delete one. Since we sorted the list, these values are required to |
| 1824 | // be adjacent. |
| 1825 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1826 | if (Ops[i] == Ops[i+1]) { // X smax Y smax Y --> X smax Y |
| 1827 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 1828 | --i; --e; |
| 1829 | } |
| 1830 | |
| 1831 | if (Ops.size() == 1) return Ops[0]; |
| 1832 | |
| 1833 | assert(!Ops.empty() && "Reduced smax down to nothing!"); |
| 1834 | |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1835 | // 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] | 1836 | // already have one, otherwise create a new one. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1837 | FoldingSetNodeID ID; |
| 1838 | ID.AddInteger(scSMaxExpr); |
| 1839 | ID.AddInteger(Ops.size()); |
| 1840 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1841 | ID.AddPointer(Ops[i]); |
| 1842 | void *IP = 0; |
| 1843 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1844 | SCEV *S = SCEVAllocator.Allocate<SCEVSMaxExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1845 | new (S) SCEVSMaxExpr(ID, Ops); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1846 | UniqueSCEVs.InsertNode(S, IP); |
| 1847 | return S; |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1850 | const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, |
| 1851 | const SCEV *RHS) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1852 | SmallVector<const SCEV *, 2> Ops; |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1853 | Ops.push_back(LHS); |
| 1854 | Ops.push_back(RHS); |
| 1855 | return getUMaxExpr(Ops); |
| 1856 | } |
| 1857 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1858 | const SCEV * |
| 1859 | ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1860 | assert(!Ops.empty() && "Cannot get empty umax!"); |
| 1861 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1862 | #ifndef NDEBUG |
| 1863 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1864 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1865 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1866 | "SCEVUMaxExpr operand types don't match!"); |
| 1867 | #endif |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1868 | |
| 1869 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1870 | GroupByComplexity(Ops, LI); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1871 | |
| 1872 | // If there are any constants, fold them together. |
| 1873 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1874 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1875 | ++Idx; |
| 1876 | assert(Idx < Ops.size()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1877 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1878 | // We found two constants, fold them together! |
| 1879 | ConstantInt *Fold = ConstantInt::get( |
| 1880 | APIntOps::umax(LHSC->getValue()->getValue(), |
| 1881 | RHSC->getValue()->getValue())); |
| 1882 | Ops[0] = getConstant(Fold); |
| 1883 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1884 | if (Ops.size() == 1) return Ops[0]; |
| 1885 | LHSC = cast<SCEVConstant>(Ops[0]); |
| 1886 | } |
| 1887 | |
Dan Gohman | d156c09 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1888 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1889 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) { |
| 1890 | Ops.erase(Ops.begin()); |
| 1891 | --Idx; |
Dan Gohman | d156c09 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1892 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) { |
| 1893 | // If we have an umax with a constant maximum-int, it will always be |
| 1894 | // maximum-int. |
| 1895 | return Ops[0]; |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | if (Ops.size() == 1) return Ops[0]; |
| 1900 | |
| 1901 | // Find the first UMax |
| 1902 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr) |
| 1903 | ++Idx; |
| 1904 | |
| 1905 | // Check to see if one of the operands is a UMax. If so, expand its operands |
| 1906 | // onto our operand list, and recurse to simplify. |
| 1907 | if (Idx < Ops.size()) { |
| 1908 | bool DeletedUMax = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1909 | while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1910 | Ops.insert(Ops.end(), UMax->op_begin(), UMax->op_end()); |
| 1911 | Ops.erase(Ops.begin()+Idx); |
| 1912 | DeletedUMax = true; |
| 1913 | } |
| 1914 | |
| 1915 | if (DeletedUMax) |
| 1916 | return getUMaxExpr(Ops); |
| 1917 | } |
| 1918 | |
| 1919 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1920 | // so, delete one. Since we sorted the list, these values are required to |
| 1921 | // be adjacent. |
| 1922 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1923 | if (Ops[i] == Ops[i+1]) { // X umax Y umax Y --> X umax Y |
| 1924 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 1925 | --i; --e; |
| 1926 | } |
| 1927 | |
| 1928 | if (Ops.size() == 1) return Ops[0]; |
| 1929 | |
| 1930 | assert(!Ops.empty() && "Reduced umax down to nothing!"); |
| 1931 | |
| 1932 | // Okay, it looks like we really DO need a umax expr. Check to see if we |
| 1933 | // already have one, otherwise create a new one. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1934 | FoldingSetNodeID ID; |
| 1935 | ID.AddInteger(scUMaxExpr); |
| 1936 | ID.AddInteger(Ops.size()); |
| 1937 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1938 | ID.AddPointer(Ops[i]); |
| 1939 | void *IP = 0; |
| 1940 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1941 | SCEV *S = SCEVAllocator.Allocate<SCEVUMaxExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1942 | new (S) SCEVUMaxExpr(ID, Ops); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1943 | UniqueSCEVs.InsertNode(S, IP); |
| 1944 | return S; |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1947 | const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS, |
| 1948 | const SCEV *RHS) { |
Dan Gohman | d01fff8 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 1949 | // ~smax(~x, ~y) == smin(x, y). |
| 1950 | return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 1951 | } |
| 1952 | |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1953 | const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS, |
| 1954 | const SCEV *RHS) { |
Dan Gohman | d01fff8 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 1955 | // ~umax(~x, ~y) == umin(x, y) |
| 1956 | return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 1957 | } |
| 1958 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1959 | const SCEV *ScalarEvolution::getUnknown(Value *V) { |
Dan Gohman | 984c78a | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 1960 | // Don't attempt to do anything other than create a SCEVUnknown object |
| 1961 | // here. createSCEV only calls getUnknown after checking for all other |
| 1962 | // interesting possibilities, and any other code that calls getUnknown |
| 1963 | // is doing so in order to hide a value from SCEV canonicalization. |
| 1964 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1965 | FoldingSetNodeID ID; |
| 1966 | ID.AddInteger(scUnknown); |
| 1967 | ID.AddPointer(V); |
| 1968 | void *IP = 0; |
| 1969 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1970 | SCEV *S = SCEVAllocator.Allocate<SCEVUnknown>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1971 | new (S) SCEVUnknown(ID, V); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1972 | UniqueSCEVs.InsertNode(S, IP); |
| 1973 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1976 | //===----------------------------------------------------------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1977 | // Basic SCEV Analysis and PHI Idiom Recognition Code |
| 1978 | // |
| 1979 | |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 1980 | /// isSCEVable - Test if values of the given type are analyzable within |
| 1981 | /// the SCEV framework. This primarily includes integer types, and it |
| 1982 | /// can optionally include pointer types if the ScalarEvolution class |
| 1983 | /// has access to target-specific information. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 1984 | bool ScalarEvolution::isSCEVable(const Type *Ty) const { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 1985 | // Integers are always SCEVable. |
| 1986 | if (Ty->isInteger()) |
| 1987 | return true; |
| 1988 | |
| 1989 | // Pointers are SCEVable if TargetData information is available |
| 1990 | // to provide pointer size information. |
| 1991 | if (isa<PointerType>(Ty)) |
| 1992 | return TD != NULL; |
| 1993 | |
| 1994 | // Otherwise it's not SCEVable. |
| 1995 | return false; |
| 1996 | } |
| 1997 | |
| 1998 | /// getTypeSizeInBits - Return the size in bits of the specified type, |
| 1999 | /// for which isSCEVable must return true. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2000 | uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2001 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2002 | |
| 2003 | // If we have a TargetData, use it! |
| 2004 | if (TD) |
| 2005 | return TD->getTypeSizeInBits(Ty); |
| 2006 | |
| 2007 | // Otherwise, we support only integer types. |
| 2008 | assert(Ty->isInteger() && "isSCEVable permitted a non-SCEVable type!"); |
| 2009 | return Ty->getPrimitiveSizeInBits(); |
| 2010 | } |
| 2011 | |
| 2012 | /// getEffectiveSCEVType - Return a type with the same bitwidth as |
| 2013 | /// the given type and which represents how SCEV will treat the given |
| 2014 | /// type, for which isSCEVable must return true. For pointer types, |
| 2015 | /// this is the pointer-sized integer type. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2016 | const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2017 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2018 | |
| 2019 | if (Ty->isInteger()) |
| 2020 | return Ty; |
| 2021 | |
| 2022 | assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!"); |
| 2023 | return TD->getIntPtrType(); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2024 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2025 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2026 | const SCEV *ScalarEvolution::getCouldNotCompute() { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2027 | return &CouldNotCompute; |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2030 | /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the |
| 2031 | /// expression and create a new one. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2032 | const SCEV *ScalarEvolution::getSCEV(Value *V) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2033 | assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2034 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2035 | std::map<SCEVCallbackVH, const SCEV *>::iterator I = Scalars.find(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2036 | if (I != Scalars.end()) return I->second; |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2037 | const SCEV *S = createSCEV(V); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2038 | Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2039 | return S; |
| 2040 | } |
| 2041 | |
Dan Gohman | 984c78a | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2042 | /// getIntegerSCEV - Given a SCEVable type, create a constant for the |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2043 | /// specified signed integer value and return a SCEV for the constant. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2044 | const SCEV *ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) { |
Dan Gohman | 984c78a | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2045 | const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty)); |
| 2046 | return getConstant(ConstantInt::get(ITy, Val)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V |
| 2050 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2051 | const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) { |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2052 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2053 | return getConstant( |
| 2054 | cast<ConstantInt>(Context->getConstantExprNeg(VC->getValue()))); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2055 | |
| 2056 | const Type *Ty = V->getType(); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2057 | Ty = getEffectiveSCEVType(Ty); |
| 2058 | return getMulExpr(V, getConstant(ConstantInt::getAllOnesValue(Ty))); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2062 | const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) { |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2063 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 2064 | return getConstant(cast<ConstantInt>(ConstantExpr::getNot(VC->getValue()))); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2065 | |
| 2066 | const Type *Ty = V->getType(); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2067 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2068 | const SCEV *AllOnes = getConstant(ConstantInt::getAllOnesValue(Ty)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2069 | return getMinusSCEV(AllOnes, V); |
| 2070 | } |
| 2071 | |
| 2072 | /// getMinusSCEV - Return a SCEV corresponding to LHS - RHS. |
| 2073 | /// |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2074 | const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, |
| 2075 | const SCEV *RHS) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2076 | // X - Y --> X + -Y |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2077 | return getAddExpr(LHS, getNegativeSCEV(RHS)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2081 | /// input value to the specified type. If the type must be extended, it is zero |
| 2082 | /// extended. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2083 | const SCEV * |
| 2084 | ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2085 | const Type *Ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2086 | const Type *SrcTy = V->getType(); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2087 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2088 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2089 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2090 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2091 | return V; // No conversion |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2092 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2093 | return getTruncateExpr(V, Ty); |
| 2094 | return getZeroExtendExpr(V, Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2098 | /// input value to the specified type. If the type must be extended, it is sign |
| 2099 | /// extended. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2100 | const SCEV * |
| 2101 | ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2102 | const Type *Ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2103 | const Type *SrcTy = V->getType(); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2104 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2105 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2106 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2107 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2108 | return V; // No conversion |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2109 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2110 | return getTruncateExpr(V, Ty); |
| 2111 | return getSignExtendExpr(V, Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2114 | /// getNoopOrZeroExtend - 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. The conversion must not be narrowing. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2117 | const SCEV * |
| 2118 | ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2119 | const Type *SrcTy = V->getType(); |
| 2120 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2121 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 2122 | "Cannot noop or zero extend with non-integer arguments!"); |
| 2123 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2124 | "getNoopOrZeroExtend cannot truncate!"); |
| 2125 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2126 | return V; // No conversion |
| 2127 | return getZeroExtendExpr(V, Ty); |
| 2128 | } |
| 2129 | |
| 2130 | /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2131 | /// input value to the specified type. If the type must be extended, it is sign |
| 2132 | /// extended. The conversion must not be narrowing. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2133 | const SCEV * |
| 2134 | ScalarEvolution::getNoopOrSignExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2135 | const Type *SrcTy = V->getType(); |
| 2136 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2137 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 2138 | "Cannot noop or sign extend with non-integer arguments!"); |
| 2139 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2140 | "getNoopOrSignExtend cannot truncate!"); |
| 2141 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2142 | return V; // No conversion |
| 2143 | return getSignExtendExpr(V, Ty); |
| 2144 | } |
| 2145 | |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2146 | /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of |
| 2147 | /// the input value to the specified type. If the type must be extended, |
| 2148 | /// it is extended with unspecified bits. The conversion must not be |
| 2149 | /// narrowing. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2150 | const SCEV * |
| 2151 | ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2152 | const Type *SrcTy = V->getType(); |
| 2153 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2154 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 2155 | "Cannot noop or any extend with non-integer arguments!"); |
| 2156 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2157 | "getNoopOrAnyExtend cannot truncate!"); |
| 2158 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2159 | return V; // No conversion |
| 2160 | return getAnyExtendExpr(V, Ty); |
| 2161 | } |
| 2162 | |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2163 | /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the |
| 2164 | /// input value to the specified type. The conversion must not be widening. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2165 | const SCEV * |
| 2166 | ScalarEvolution::getTruncateOrNoop(const SCEV *V, const Type *Ty) { |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2167 | const Type *SrcTy = V->getType(); |
| 2168 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2169 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 2170 | "Cannot truncate or noop with non-integer arguments!"); |
| 2171 | assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) && |
| 2172 | "getTruncateOrNoop cannot extend!"); |
| 2173 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2174 | return V; // No conversion |
| 2175 | return getTruncateExpr(V, Ty); |
| 2176 | } |
| 2177 | |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2178 | /// getUMaxFromMismatchedTypes - Promote the operands to the wider of |
| 2179 | /// the types using zero-extension, and then perform a umax operation |
| 2180 | /// with them. |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2181 | const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS, |
| 2182 | const SCEV *RHS) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2183 | const SCEV *PromotedLHS = LHS; |
| 2184 | const SCEV *PromotedRHS = RHS; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2185 | |
| 2186 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2187 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2188 | else |
| 2189 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2190 | |
| 2191 | return getUMaxExpr(PromotedLHS, PromotedRHS); |
| 2192 | } |
| 2193 | |
Dan Gohman | 9e62bb0 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2194 | /// getUMinFromMismatchedTypes - Promote the operands to the wider of |
| 2195 | /// the types using zero-extension, and then perform a umin operation |
| 2196 | /// with them. |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2197 | const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS, |
| 2198 | const SCEV *RHS) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2199 | const SCEV *PromotedLHS = LHS; |
| 2200 | const SCEV *PromotedRHS = RHS; |
Dan Gohman | 9e62bb0 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2201 | |
| 2202 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2203 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2204 | else |
| 2205 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2206 | |
| 2207 | return getUMinExpr(PromotedLHS, PromotedRHS); |
| 2208 | } |
| 2209 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2210 | /// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value for |
| 2211 | /// the specified instruction and replaces any references to the symbolic value |
| 2212 | /// SymName with the specified value. This is used during PHI resolution. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2213 | void |
| 2214 | ScalarEvolution::ReplaceSymbolicValueWithConcrete(Instruction *I, |
| 2215 | const SCEV *SymName, |
| 2216 | const SCEV *NewVal) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2217 | std::map<SCEVCallbackVH, const SCEV *>::iterator SI = |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2218 | Scalars.find(SCEVCallbackVH(I, this)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2219 | if (SI == Scalars.end()) return; |
| 2220 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2221 | const SCEV *NV = |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2222 | SI->second->replaceSymbolicValuesWithConcrete(SymName, NewVal, *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2223 | if (NV == SI->second) return; // No change. |
| 2224 | |
| 2225 | SI->second = NV; // Update the scalars map! |
| 2226 | |
| 2227 | // Any instruction values that use this instruction might also need to be |
| 2228 | // updated! |
| 2229 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 2230 | UI != E; ++UI) |
| 2231 | ReplaceSymbolicValueWithConcrete(cast<Instruction>(*UI), SymName, NewVal); |
| 2232 | } |
| 2233 | |
| 2234 | /// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in |
| 2235 | /// a loop header, making it a potential recurrence, or it doesn't. |
| 2236 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2237 | const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2238 | if (PN->getNumIncomingValues() == 2) // The loops have been canonicalized. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2239 | if (const Loop *L = LI->getLoopFor(PN->getParent())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2240 | if (L->getHeader() == PN->getParent()) { |
| 2241 | // If it lives in the loop header, it has two incoming values, one |
| 2242 | // from outside the loop, and one from inside. |
| 2243 | unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0)); |
| 2244 | unsigned BackEdge = IncomingEdge^1; |
| 2245 | |
| 2246 | // While we are analyzing this PHI node, handle its value symbolically. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2247 | const SCEV *SymbolicName = getUnknown(PN); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2248 | assert(Scalars.find(PN) == Scalars.end() && |
| 2249 | "PHI node already processed?"); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2250 | Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2251 | |
| 2252 | // Using this symbolic name for the PHI, analyze the value coming around |
| 2253 | // the back-edge. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2254 | const SCEV *BEValue = getSCEV(PN->getIncomingValue(BackEdge)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2255 | |
| 2256 | // NOTE: If BEValue is loop invariant, we know that the PHI node just |
| 2257 | // has a special value for the first iteration of the loop. |
| 2258 | |
| 2259 | // If the value coming around the backedge is an add with the symbolic |
| 2260 | // value we just inserted, then we found a simple induction variable! |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2261 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2262 | // If there is a single occurrence of the symbolic value, replace it |
| 2263 | // with a recurrence. |
| 2264 | unsigned FoundIndex = Add->getNumOperands(); |
| 2265 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2266 | if (Add->getOperand(i) == SymbolicName) |
| 2267 | if (FoundIndex == e) { |
| 2268 | FoundIndex = i; |
| 2269 | break; |
| 2270 | } |
| 2271 | |
| 2272 | if (FoundIndex != Add->getNumOperands()) { |
| 2273 | // Create an add with everything but the specified operand. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2274 | SmallVector<const SCEV *, 8> Ops; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2275 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2276 | if (i != FoundIndex) |
| 2277 | Ops.push_back(Add->getOperand(i)); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2278 | const SCEV *Accum = getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2279 | |
| 2280 | // This is not a valid addrec if the step amount is varying each |
| 2281 | // loop iteration, but is not itself an addrec in this loop. |
| 2282 | if (Accum->isLoopInvariant(L) || |
| 2283 | (isa<SCEVAddRecExpr>(Accum) && |
| 2284 | cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) { |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2285 | const SCEV *StartVal = |
| 2286 | getSCEV(PN->getIncomingValue(IncomingEdge)); |
| 2287 | const SCEV *PHISCEV = |
| 2288 | getAddRecExpr(StartVal, Accum, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2289 | |
| 2290 | // Okay, for the entire analysis of this edge we assumed the PHI |
| 2291 | // to be symbolic. We now need to go back and update all of the |
| 2292 | // entries for the scalars that use the PHI (except for the PHI |
| 2293 | // itself) to use the new analyzed value instead of the "symbolic" |
| 2294 | // value. |
| 2295 | ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV); |
| 2296 | return PHISCEV; |
| 2297 | } |
| 2298 | } |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2299 | } else if (const SCEVAddRecExpr *AddRec = |
| 2300 | dyn_cast<SCEVAddRecExpr>(BEValue)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2301 | // Otherwise, this could be a loop like this: |
| 2302 | // i = 0; for (j = 1; ..; ++j) { .... i = j; } |
| 2303 | // In this case, j = {1,+,1} and BEValue is j. |
| 2304 | // Because the other in-value of i (0) fits the evolution of BEValue |
| 2305 | // i really is an addrec evolution. |
| 2306 | if (AddRec->getLoop() == L && AddRec->isAffine()) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2307 | const SCEV *StartVal = getSCEV(PN->getIncomingValue(IncomingEdge)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2308 | |
| 2309 | // If StartVal = j.start - j.stride, we can use StartVal as the |
| 2310 | // initial step of the addrec evolution. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2311 | if (StartVal == getMinusSCEV(AddRec->getOperand(0), |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 2312 | AddRec->getOperand(1))) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2313 | const SCEV *PHISCEV = |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2314 | getAddRecExpr(StartVal, AddRec->getOperand(1), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2315 | |
| 2316 | // Okay, for the entire analysis of this edge we assumed the PHI |
| 2317 | // to be symbolic. We now need to go back and update all of the |
| 2318 | // entries for the scalars that use the PHI (except for the PHI |
| 2319 | // itself) to use the new analyzed value instead of the "symbolic" |
| 2320 | // value. |
| 2321 | ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV); |
| 2322 | return PHISCEV; |
| 2323 | } |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | return SymbolicName; |
| 2328 | } |
| 2329 | |
| 2330 | // 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] | 2331 | return getUnknown(PN); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2334 | /// createNodeForGEP - Expand GEP instructions into add and multiply |
| 2335 | /// operations. This allows them to be analyzed by regular SCEV code. |
| 2336 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2337 | const SCEV *ScalarEvolution::createNodeForGEP(User *GEP) { |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2338 | |
| 2339 | const Type *IntPtrTy = TD->getIntPtrType(); |
Dan Gohman | c7034fa | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2340 | Value *Base = GEP->getOperand(0); |
Dan Gohman | d586a4f | 2009-05-09 00:14:52 +0000 | [diff] [blame] | 2341 | // Don't attempt to analyze GEPs over unsized objects. |
| 2342 | if (!cast<PointerType>(Base->getType())->getElementType()->isSized()) |
| 2343 | return getUnknown(GEP); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2344 | const SCEV *TotalOffset = getIntegerSCEV(0, IntPtrTy); |
Dan Gohman | c7034fa | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2345 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 2346 | for (GetElementPtrInst::op_iterator I = next(GEP->op_begin()), |
| 2347 | E = GEP->op_end(); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2348 | I != E; ++I) { |
| 2349 | Value *Index = *I; |
| 2350 | // Compute the (potentially symbolic) offset in bytes for this index. |
| 2351 | if (const StructType *STy = dyn_cast<StructType>(*GTI++)) { |
| 2352 | // For a struct, add the member offset. |
| 2353 | const StructLayout &SL = *TD->getStructLayout(STy); |
| 2354 | unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue(); |
| 2355 | uint64_t Offset = SL.getElementOffset(FieldNo); |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2356 | TotalOffset = getAddExpr(TotalOffset, |
| 2357 | getIntegerSCEV(Offset, IntPtrTy)); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2358 | } else { |
| 2359 | // For an array, add the element offset, explicitly scaled. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2360 | const SCEV *LocalOffset = getSCEV(Index); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2361 | if (!isa<PointerType>(LocalOffset->getType())) |
| 2362 | // Getelementptr indicies are signed. |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2363 | LocalOffset = getTruncateOrSignExtend(LocalOffset, |
| 2364 | IntPtrTy); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2365 | LocalOffset = |
| 2366 | getMulExpr(LocalOffset, |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2367 | getIntegerSCEV(TD->getTypeAllocSize(*GTI), |
| 2368 | IntPtrTy)); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2369 | TotalOffset = getAddExpr(TotalOffset, LocalOffset); |
| 2370 | } |
| 2371 | } |
| 2372 | return getAddExpr(getSCEV(Base), TotalOffset); |
| 2373 | } |
| 2374 | |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2375 | /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is |
| 2376 | /// guaranteed to end in (at every loop iteration). It is, at the same time, |
| 2377 | /// the minimum number of times S is divisible by 2. For example, given {4,+,8} |
| 2378 | /// 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] | 2379 | uint32_t |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2380 | ScalarEvolution::GetMinTrailingZeros(const SCEV *S) { |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2381 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Chris Lattner | 6ecce2a | 2007-11-23 22:36:49 +0000 | [diff] [blame] | 2382 | return C->getValue()->getValue().countTrailingZeros(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2383 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2384 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S)) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2385 | return std::min(GetMinTrailingZeros(T->getOperand()), |
| 2386 | (uint32_t)getTypeSizeInBits(T->getType())); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2387 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2388 | if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) { |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2389 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 2390 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 2391 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2394 | if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) { |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2395 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 2396 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 2397 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2400 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2401 | // The result is the min of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2402 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2403 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2404 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2405 | return MinOpRes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2408 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2409 | // The result is the sum of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2410 | uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0)); |
| 2411 | uint32_t BitWidth = getTypeSizeInBits(M->getType()); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2412 | for (unsigned i = 1, e = M->getNumOperands(); |
| 2413 | SumOpRes != BitWidth && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2414 | SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2415 | BitWidth); |
| 2416 | return SumOpRes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2417 | } |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2418 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2419 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2420 | // The result is the min of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2421 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2422 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2423 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2424 | return MinOpRes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2425 | } |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2426 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2427 | if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2428 | // The result is the min of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2429 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2430 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2431 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2432 | return MinOpRes; |
| 2433 | } |
| 2434 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2435 | if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2436 | // The result is the min of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2437 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2438 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2439 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2440 | return MinOpRes; |
| 2441 | } |
| 2442 | |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2443 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2444 | // For a SCEVUnknown, ask ValueTracking. |
| 2445 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2446 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 2447 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 2448 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones); |
| 2449 | return Zeros.countTrailingOnes(); |
| 2450 | } |
| 2451 | |
| 2452 | // SCEVUDivExpr |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2453 | return 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2456 | uint32_t |
| 2457 | ScalarEvolution::GetMinLeadingZeros(const SCEV *S) { |
| 2458 | // TODO: Handle other SCEV expression types here. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2459 | |
| 2460 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2461 | return C->getValue()->getValue().countLeadingZeros(); |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2462 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2463 | if (const SCEVZeroExtendExpr *C = dyn_cast<SCEVZeroExtendExpr>(S)) { |
| 2464 | // A zero-extension cast adds zero bits. |
| 2465 | return GetMinLeadingZeros(C->getOperand()) + |
| 2466 | (getTypeSizeInBits(C->getType()) - |
| 2467 | getTypeSizeInBits(C->getOperand()->getType())); |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2471 | // For a SCEVUnknown, ask ValueTracking. |
| 2472 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2473 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 2474 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 2475 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD); |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2476 | return Zeros.countLeadingOnes(); |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2479 | return 1; |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2482 | uint32_t |
| 2483 | ScalarEvolution::GetMinSignBits(const SCEV *S) { |
| 2484 | // TODO: Handle other SCEV expression types here. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2485 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2486 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) { |
| 2487 | const APInt &A = C->getValue()->getValue(); |
| 2488 | return A.isNegative() ? A.countLeadingOnes() : |
| 2489 | A.countLeadingZeros(); |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2492 | if (const SCEVSignExtendExpr *C = dyn_cast<SCEVSignExtendExpr>(S)) { |
| 2493 | // A sign-extension cast adds sign bits. |
| 2494 | return GetMinSignBits(C->getOperand()) + |
| 2495 | (getTypeSizeInBits(C->getType()) - |
| 2496 | getTypeSizeInBits(C->getOperand()->getType())); |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2499 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { |
| 2500 | unsigned BitWidth = getTypeSizeInBits(A->getType()); |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2501 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2502 | // Special case decrementing a value (ADD X, -1): |
| 2503 | if (const SCEVConstant *CRHS = dyn_cast<SCEVConstant>(A->getOperand(0))) |
| 2504 | if (CRHS->isAllOnesValue()) { |
| 2505 | SmallVector<const SCEV *, 4> OtherOps(A->op_begin() + 1, A->op_end()); |
| 2506 | const SCEV *OtherOpsAdd = getAddExpr(OtherOps); |
| 2507 | unsigned LZ = GetMinLeadingZeros(OtherOpsAdd); |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2508 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2509 | // If the input is known to be 0 or 1, the output is 0/-1, which is all |
| 2510 | // sign bits set. |
| 2511 | if (LZ == BitWidth - 1) |
| 2512 | return BitWidth; |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2513 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2514 | // If we are subtracting one from a positive number, there is no carry |
| 2515 | // out of the result. |
| 2516 | if (LZ > 0) |
| 2517 | return GetMinSignBits(OtherOpsAdd); |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2518 | } |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2519 | |
| 2520 | // Add can have at most one carry bit. Thus we know that the output |
| 2521 | // is, at worst, one more bit than the inputs. |
| 2522 | unsigned Min = BitWidth; |
| 2523 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { |
| 2524 | unsigned N = GetMinSignBits(A->getOperand(i)); |
| 2525 | Min = std::min(Min, N) - 1; |
| 2526 | if (Min == 0) return 1; |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2527 | } |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2528 | return 1; |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2529 | } |
| 2530 | |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2531 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2532 | // For a SCEVUnknown, ask ValueTracking. |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2533 | return ComputeNumSignBits(U->getValue(), TD); |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 2536 | return 1; |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2539 | /// createSCEV - We know that there is no SCEV for the specified value. |
| 2540 | /// Analyze the expression. |
| 2541 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2542 | const SCEV *ScalarEvolution::createSCEV(Value *V) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2543 | if (!isSCEVable(V->getType())) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2544 | return getUnknown(V); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2545 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2546 | unsigned Opcode = Instruction::UserOp1; |
| 2547 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 2548 | Opcode = I->getOpcode(); |
| 2549 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 2550 | Opcode = CE->getOpcode(); |
Dan Gohman | 984c78a | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2551 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 2552 | return getConstant(CI); |
| 2553 | else if (isa<ConstantPointerNull>(V)) |
| 2554 | return getIntegerSCEV(0, V->getType()); |
| 2555 | else if (isa<UndefValue>(V)) |
| 2556 | return getIntegerSCEV(0, V->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2557 | else |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2558 | return getUnknown(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2559 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2560 | User *U = cast<User>(V); |
| 2561 | switch (Opcode) { |
| 2562 | case Instruction::Add: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2563 | return getAddExpr(getSCEV(U->getOperand(0)), |
| 2564 | getSCEV(U->getOperand(1))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2565 | case Instruction::Mul: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2566 | return getMulExpr(getSCEV(U->getOperand(0)), |
| 2567 | getSCEV(U->getOperand(1))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2568 | case Instruction::UDiv: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2569 | return getUDivExpr(getSCEV(U->getOperand(0)), |
| 2570 | getSCEV(U->getOperand(1))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2571 | case Instruction::Sub: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2572 | return getMinusSCEV(getSCEV(U->getOperand(0)), |
| 2573 | getSCEV(U->getOperand(1))); |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2574 | case Instruction::And: |
| 2575 | // For an expression like x&255 that merely masks off the high bits, |
| 2576 | // use zext(trunc(x)) as the SCEV expression. |
| 2577 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 91ae1e7 | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2578 | if (CI->isNullValue()) |
| 2579 | return getSCEV(U->getOperand(1)); |
Dan Gohman | c7ebba1 | 2009-04-27 01:41:10 +0000 | [diff] [blame] | 2580 | if (CI->isAllOnesValue()) |
| 2581 | return getSCEV(U->getOperand(0)); |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2582 | const APInt &A = CI->getValue(); |
Dan Gohman | a7726c3 | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2583 | |
| 2584 | // Instcombine's ShrinkDemandedConstant may strip bits out of |
| 2585 | // constants, obscuring what would otherwise be a low-bits mask. |
| 2586 | // Use ComputeMaskedBits to compute what ShrinkDemandedConstant |
| 2587 | // knew about to reconstruct a low-bits mask value. |
| 2588 | unsigned LZ = A.countLeadingZeros(); |
| 2589 | unsigned BitWidth = A.getBitWidth(); |
| 2590 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
| 2591 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 2592 | ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD); |
| 2593 | |
| 2594 | APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ); |
| 2595 | |
Dan Gohman | ae1d7dd | 2009-06-17 23:54:37 +0000 | [diff] [blame] | 2596 | if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask)) |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2597 | return |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2598 | getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)), |
Dan Gohman | a7726c3 | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2599 | IntegerType::get(BitWidth - LZ)), |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2600 | U->getType()); |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2601 | } |
| 2602 | break; |
Dan Gohman | a7726c3 | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2603 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2604 | case Instruction::Or: |
| 2605 | // If the RHS of the Or is a constant, we may have something like: |
| 2606 | // X*4+1 which got turned into X*4|1. Handle this as an Add so loop |
| 2607 | // optimizations will transparently handle this case. |
| 2608 | // |
| 2609 | // In order for this transformation to be safe, the LHS must be of the |
| 2610 | // form X*(2^n) and the Or constant must be less than 2^n. |
| 2611 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2612 | const SCEV *LHS = getSCEV(U->getOperand(0)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2613 | const APInt &CIVal = CI->getValue(); |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2614 | if (GetMinTrailingZeros(LHS) >= |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2615 | (CIVal.getBitWidth() - CIVal.countLeadingZeros())) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2616 | return getAddExpr(LHS, getSCEV(U->getOperand(1))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2617 | } |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2618 | break; |
| 2619 | case Instruction::Xor: |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2620 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2621 | // If the RHS of the xor is a signbit, then this is just an add. |
| 2622 | // Instcombine turns add of signbit into xor as a strength reduction step. |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2623 | if (CI->getValue().isSignBit()) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2624 | return getAddExpr(getSCEV(U->getOperand(0)), |
| 2625 | getSCEV(U->getOperand(1))); |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2626 | |
| 2627 | // 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] | 2628 | if (CI->isAllOnesValue()) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2629 | return getNotSCEV(getSCEV(U->getOperand(0))); |
Dan Gohman | fc78cff | 2009-05-18 16:29:04 +0000 | [diff] [blame] | 2630 | |
| 2631 | // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask. |
| 2632 | // This is a variant of the check for xor with -1, and it handles |
| 2633 | // the case where instcombine has trimmed non-demanded bits out |
| 2634 | // of an xor with -1. |
| 2635 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0))) |
| 2636 | if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1))) |
| 2637 | if (BO->getOpcode() == Instruction::And && |
| 2638 | LCI->getValue() == CI->getValue()) |
| 2639 | if (const SCEVZeroExtendExpr *Z = |
Dan Gohman | e49ae43 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 2640 | dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) { |
Dan Gohman | ed1d8bb | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 2641 | const Type *UTy = U->getType(); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2642 | const SCEV *Z0 = Z->getOperand(); |
Dan Gohman | ed1d8bb | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 2643 | const Type *Z0Ty = Z0->getType(); |
| 2644 | unsigned Z0TySize = getTypeSizeInBits(Z0Ty); |
| 2645 | |
| 2646 | // If C is a low-bits mask, the zero extend is zerving to |
| 2647 | // mask off the high bits. Complement the operand and |
| 2648 | // re-apply the zext. |
| 2649 | if (APIntOps::isMask(Z0TySize, CI->getValue())) |
| 2650 | return getZeroExtendExpr(getNotSCEV(Z0), UTy); |
| 2651 | |
| 2652 | // If C is a single bit, it may be in the sign-bit position |
| 2653 | // before the zero-extend. In this case, represent the xor |
| 2654 | // using an add, which is equivalent, and re-apply the zext. |
| 2655 | APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize); |
| 2656 | if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() && |
| 2657 | Trunc.isSignBit()) |
| 2658 | return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)), |
| 2659 | UTy); |
Dan Gohman | e49ae43 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 2660 | } |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2661 | } |
| 2662 | break; |
| 2663 | |
| 2664 | case Instruction::Shl: |
| 2665 | // Turn shift left of a constant amount into a multiply. |
| 2666 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 2667 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
| 2668 | Constant *X = ConstantInt::get( |
| 2669 | APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2670 | return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2671 | } |
| 2672 | break; |
| 2673 | |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2674 | case Instruction::LShr: |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 2675 | // Turn logical shift right of a constant into a unsigned divide. |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2676 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 2677 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
| 2678 | Constant *X = ConstantInt::get( |
| 2679 | APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2680 | return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2681 | } |
| 2682 | break; |
| 2683 | |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2684 | case Instruction::AShr: |
| 2685 | // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression. |
| 2686 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) |
| 2687 | if (Instruction *L = dyn_cast<Instruction>(U->getOperand(0))) |
| 2688 | if (L->getOpcode() == Instruction::Shl && |
| 2689 | L->getOperand(1) == U->getOperand(1)) { |
Dan Gohman | 91ae1e7 | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2690 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2691 | uint64_t Amt = BitWidth - CI->getZExtValue(); |
| 2692 | if (Amt == BitWidth) |
| 2693 | return getSCEV(L->getOperand(0)); // shift by zero --> noop |
| 2694 | if (Amt > BitWidth) |
| 2695 | return getIntegerSCEV(0, U->getType()); // value is undefined |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2696 | return |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2697 | getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)), |
Dan Gohman | 91ae1e7 | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2698 | IntegerType::get(Amt)), |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2699 | U->getType()); |
| 2700 | } |
| 2701 | break; |
| 2702 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2703 | case Instruction::Trunc: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2704 | return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2705 | |
| 2706 | case Instruction::ZExt: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2707 | return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2708 | |
| 2709 | case Instruction::SExt: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2710 | return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2711 | |
| 2712 | case Instruction::BitCast: |
| 2713 | // BitCasts are no-op casts so we just eliminate the cast. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2714 | if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2715 | return getSCEV(U->getOperand(0)); |
| 2716 | break; |
| 2717 | |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2718 | case Instruction::IntToPtr: |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2719 | if (!TD) break; // Without TD we can't analyze pointers. |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2720 | return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)), |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2721 | TD->getIntPtrType()); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2722 | |
| 2723 | case Instruction::PtrToInt: |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2724 | if (!TD) break; // Without TD we can't analyze pointers. |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2725 | return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)), |
| 2726 | U->getType()); |
| 2727 | |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2728 | case Instruction::GetElementPtr: |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2729 | if (!TD) break; // Without TD we can't analyze pointers. |
Dan Gohman | ca5a39e | 2009-05-08 20:58:38 +0000 | [diff] [blame] | 2730 | return createNodeForGEP(U); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2731 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2732 | case Instruction::PHI: |
| 2733 | return createNodeForPHI(cast<PHINode>(U)); |
| 2734 | |
| 2735 | case Instruction::Select: |
| 2736 | // This could be a smax or umax that was lowered earlier. |
| 2737 | // Try to recover it. |
| 2738 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) { |
| 2739 | Value *LHS = ICI->getOperand(0); |
| 2740 | Value *RHS = ICI->getOperand(1); |
| 2741 | switch (ICI->getPredicate()) { |
| 2742 | case ICmpInst::ICMP_SLT: |
| 2743 | case ICmpInst::ICMP_SLE: |
| 2744 | std::swap(LHS, RHS); |
| 2745 | // fall through |
| 2746 | case ICmpInst::ICMP_SGT: |
| 2747 | case ICmpInst::ICMP_SGE: |
| 2748 | if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2749 | return getSMaxExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2750 | else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) |
Dan Gohman | d01fff8 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2751 | return getSMinExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2752 | break; |
| 2753 | case ICmpInst::ICMP_ULT: |
| 2754 | case ICmpInst::ICMP_ULE: |
| 2755 | std::swap(LHS, RHS); |
| 2756 | // fall through |
| 2757 | case ICmpInst::ICMP_UGT: |
| 2758 | case ICmpInst::ICMP_UGE: |
| 2759 | if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2760 | return getUMaxExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2761 | else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) |
Dan Gohman | d01fff8 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2762 | return getUMinExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2763 | break; |
Dan Gohman | f27dc69 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 2764 | case ICmpInst::ICMP_NE: |
| 2765 | // n != 0 ? n : 1 -> umax(n, 1) |
| 2766 | if (LHS == U->getOperand(1) && |
| 2767 | isa<ConstantInt>(U->getOperand(2)) && |
| 2768 | cast<ConstantInt>(U->getOperand(2))->isOne() && |
| 2769 | isa<ConstantInt>(RHS) && |
| 2770 | cast<ConstantInt>(RHS)->isZero()) |
| 2771 | return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(2))); |
| 2772 | break; |
| 2773 | case ICmpInst::ICMP_EQ: |
| 2774 | // n == 0 ? 1 : n -> umax(n, 1) |
| 2775 | if (LHS == U->getOperand(2) && |
| 2776 | isa<ConstantInt>(U->getOperand(1)) && |
| 2777 | cast<ConstantInt>(U->getOperand(1))->isOne() && |
| 2778 | isa<ConstantInt>(RHS) && |
| 2779 | cast<ConstantInt>(RHS)->isZero()) |
| 2780 | return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(1))); |
| 2781 | break; |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2782 | default: |
| 2783 | break; |
| 2784 | } |
| 2785 | } |
| 2786 | |
| 2787 | default: // We cannot analyze this expression. |
| 2788 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2791 | return getUnknown(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | |
| 2795 | |
| 2796 | //===----------------------------------------------------------------------===// |
| 2797 | // Iteration Count Computation Code |
| 2798 | // |
| 2799 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2800 | /// getBackedgeTakenCount - If the specified loop has a predictable |
| 2801 | /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute |
| 2802 | /// object. The backedge-taken count is the number of times the loop header |
| 2803 | /// will be branched to from within the loop. This is one less than the |
| 2804 | /// trip count of the loop, since it doesn't count the first iteration, |
| 2805 | /// when the header is branched to from outside the loop. |
| 2806 | /// |
| 2807 | /// Note that it is not valid to call this method on a loop without a |
| 2808 | /// loop-invariant backedge-taken count (see |
| 2809 | /// hasLoopInvariantBackedgeTakenCount). |
| 2810 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2811 | const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2812 | return getBackedgeTakenInfo(L).Exact; |
| 2813 | } |
| 2814 | |
| 2815 | /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except |
| 2816 | /// return the least SCEV value that is known never to be less than the |
| 2817 | /// actual backedge taken count. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2818 | const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2819 | return getBackedgeTakenInfo(L).Max; |
| 2820 | } |
| 2821 | |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 2822 | /// PushLoopPHIs - Push PHI nodes in the header of the given loop |
| 2823 | /// onto the given Worklist. |
| 2824 | static void |
| 2825 | PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) { |
| 2826 | BasicBlock *Header = L->getHeader(); |
| 2827 | |
| 2828 | // Push all Loop-header PHIs onto the Worklist stack. |
| 2829 | for (BasicBlock::iterator I = Header->begin(); |
| 2830 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 2831 | Worklist.push_back(PN); |
| 2832 | } |
| 2833 | |
| 2834 | /// PushDefUseChildren - Push users of the given Instruction |
| 2835 | /// onto the given Worklist. |
| 2836 | static void |
| 2837 | PushDefUseChildren(Instruction *I, |
| 2838 | SmallVectorImpl<Instruction *> &Worklist) { |
| 2839 | // Push the def-use children onto the Worklist stack. |
| 2840 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 2841 | UI != UE; ++UI) |
| 2842 | Worklist.push_back(cast<Instruction>(UI)); |
| 2843 | } |
| 2844 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2845 | const ScalarEvolution::BackedgeTakenInfo & |
| 2846 | ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2847 | // Initially insert a CouldNotCompute for this loop. If the insertion |
| 2848 | // succeeds, procede to actually compute a backedge-taken count and |
| 2849 | // update the value. The temporary CouldNotCompute value tells SCEV |
| 2850 | // code elsewhere that it shouldn't attempt to request a new |
| 2851 | // backedge-taken count, which could result in infinite recursion. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2852 | std::pair<std::map<const Loop*, BackedgeTakenInfo>::iterator, bool> Pair = |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2853 | BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute())); |
| 2854 | if (Pair.second) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2855 | BackedgeTakenInfo ItCount = ComputeBackedgeTakenCount(L); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2856 | if (ItCount.Exact != getCouldNotCompute()) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2857 | assert(ItCount.Exact->isLoopInvariant(L) && |
| 2858 | ItCount.Max->isLoopInvariant(L) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2859 | "Computed trip count isn't loop invariant for loop!"); |
| 2860 | ++NumTripCountsComputed; |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2861 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2862 | // Update the value in the map. |
| 2863 | Pair.first->second = ItCount; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2864 | } else { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2865 | if (ItCount.Max != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2866 | // Update the value in the map. |
| 2867 | Pair.first->second = ItCount; |
| 2868 | if (isa<PHINode>(L->getHeader()->begin())) |
| 2869 | // Only count loops that have phi nodes as not being computable. |
| 2870 | ++NumTripCountsNotComputed; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2871 | } |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2872 | |
| 2873 | // Now that we know more about the trip count for this loop, forget any |
| 2874 | // existing SCEV values for PHI nodes in this loop since they are only |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 2875 | // conservative estimates made without the benefit of trip count |
| 2876 | // information. This is similar to the code in |
| 2877 | // forgetLoopBackedgeTakenCount, except that it handles SCEVUnknown PHI |
| 2878 | // nodes specially. |
| 2879 | if (ItCount.hasAnyInfo()) { |
| 2880 | SmallVector<Instruction *, 16> Worklist; |
| 2881 | PushLoopPHIs(L, Worklist); |
| 2882 | |
| 2883 | SmallPtrSet<Instruction *, 8> Visited; |
| 2884 | while (!Worklist.empty()) { |
| 2885 | Instruction *I = Worklist.pop_back_val(); |
| 2886 | if (!Visited.insert(I)) continue; |
| 2887 | |
| 2888 | std::map<SCEVCallbackVH, const SCEV*>::iterator It = |
| 2889 | Scalars.find(static_cast<Value *>(I)); |
| 2890 | if (It != Scalars.end()) { |
| 2891 | // SCEVUnknown for a PHI either means that it has an unrecognized |
| 2892 | // structure, or it's a PHI that's in the progress of being computed |
| 2893 | // by createNodeForPHI. In the former case, additional loop trip count |
| 2894 | // information isn't going to change anything. In the later case, |
| 2895 | // createNodeForPHI will perform the necessary updates on its own when |
| 2896 | // it gets to that point. |
| 2897 | if (!isa<PHINode>(I) || !isa<SCEVUnknown>(It->second)) |
| 2898 | Scalars.erase(It); |
| 2899 | ValuesAtScopes.erase(I); |
| 2900 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 2901 | ConstantEvolutionLoopExitValue.erase(PN); |
| 2902 | } |
| 2903 | |
| 2904 | PushDefUseChildren(I, Worklist); |
| 2905 | } |
| 2906 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2907 | } |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2908 | return Pair.first->second; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2911 | /// forgetLoopBackedgeTakenCount - This method should be called by the |
Dan Gohman | f3a060a | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 2912 | /// 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] | 2913 | /// ScalarEvolution's ability to compute a trip count, or if the loop |
| 2914 | /// is deleted. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2915 | void ScalarEvolution::forgetLoopBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2916 | BackedgeTakenCounts.erase(L); |
Dan Gohman | 9462302 | 2009-05-02 17:43:35 +0000 | [diff] [blame] | 2917 | |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2918 | SmallVector<Instruction *, 16> Worklist; |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 2919 | PushLoopPHIs(L, Worklist); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2920 | |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 2921 | SmallPtrSet<Instruction *, 8> Visited; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2922 | while (!Worklist.empty()) { |
| 2923 | Instruction *I = Worklist.pop_back_val(); |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 2924 | if (!Visited.insert(I)) continue; |
| 2925 | |
| 2926 | std::map<SCEVCallbackVH, const SCEV*>::iterator It = |
| 2927 | Scalars.find(static_cast<Value *>(I)); |
| 2928 | if (It != Scalars.end()) { |
| 2929 | Scalars.erase(It); |
| 2930 | ValuesAtScopes.erase(I); |
| 2931 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 2932 | ConstantEvolutionLoopExitValue.erase(PN); |
| 2933 | } |
| 2934 | |
| 2935 | PushDefUseChildren(I, Worklist); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2936 | } |
Dan Gohman | f3a060a | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2939 | /// ComputeBackedgeTakenCount - Compute the number of times the backedge |
| 2940 | /// of the specified loop will execute. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2941 | ScalarEvolution::BackedgeTakenInfo |
| 2942 | ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2943 | SmallVector<BasicBlock*, 8> ExitingBlocks; |
| 2944 | L->getExitingBlocks(ExitingBlocks); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2945 | |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2946 | // Examine all exits and pick the most conservative values. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2947 | const SCEV *BECount = getCouldNotCompute(); |
| 2948 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2949 | bool CouldNotComputeBECount = false; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2950 | for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { |
| 2951 | BackedgeTakenInfo NewBTI = |
| 2952 | ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2953 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2954 | if (NewBTI.Exact == getCouldNotCompute()) { |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2955 | // We couldn't compute an exact value for this exit, so |
Dan Gohman | c6e8c83 | 2009-06-22 21:10:22 +0000 | [diff] [blame] | 2956 | // 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] | 2957 | CouldNotComputeBECount = true; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2958 | BECount = getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2959 | } else if (!CouldNotComputeBECount) { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2960 | if (BECount == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2961 | BECount = NewBTI.Exact; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2962 | else |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 2963 | BECount = getUMinFromMismatchedTypes(BECount, NewBTI.Exact); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2964 | } |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2965 | if (MaxBECount == getCouldNotCompute()) |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 2966 | MaxBECount = NewBTI.Max; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2967 | else if (NewBTI.Max != getCouldNotCompute()) |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 2968 | MaxBECount = getUMinFromMismatchedTypes(MaxBECount, NewBTI.Max); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 2972 | } |
| 2973 | |
| 2974 | /// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge |
| 2975 | /// of the specified loop will execute if it exits via the specified block. |
| 2976 | ScalarEvolution::BackedgeTakenInfo |
| 2977 | ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L, |
| 2978 | BasicBlock *ExitingBlock) { |
| 2979 | |
| 2980 | // Okay, we've chosen an exiting block. See what condition causes us to |
| 2981 | // exit at this block. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2982 | // |
| 2983 | // FIXME: we should be able to handle switch instructions (with a single exit) |
| 2984 | BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2985 | if (ExitBr == 0) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2986 | assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!"); |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2987 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2988 | // At this point, we know we have a conditional branch that determines whether |
| 2989 | // the loop is exited. However, we don't know if the branch is executed each |
| 2990 | // time through the loop. If not, then the execution count of the branch will |
| 2991 | // not be equal to the trip count of the loop. |
| 2992 | // |
| 2993 | // Currently we check for this by checking to see if the Exit branch goes to |
| 2994 | // the loop header. If so, we know it will always execute the same number of |
| 2995 | // 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] | 2996 | // loop header. This is common for un-rotated loops. |
| 2997 | // |
| 2998 | // If both of those tests fail, walk up the unique predecessor chain to the |
| 2999 | // header, stopping if there is an edge that doesn't exit the loop. If the |
| 3000 | // header is reached, the execution count of the branch will be equal to the |
| 3001 | // trip count of the loop. |
| 3002 | // |
| 3003 | // More extensive analysis could be done to handle more cases here. |
| 3004 | // |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3005 | if (ExitBr->getSuccessor(0) != L->getHeader() && |
| 3006 | ExitBr->getSuccessor(1) != L->getHeader() && |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3007 | ExitBr->getParent() != L->getHeader()) { |
| 3008 | // The simple checks failed, try climbing the unique predecessor chain |
| 3009 | // up to the header. |
| 3010 | bool Ok = false; |
| 3011 | for (BasicBlock *BB = ExitBr->getParent(); BB; ) { |
| 3012 | BasicBlock *Pred = BB->getUniquePredecessor(); |
| 3013 | if (!Pred) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3014 | return getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3015 | TerminatorInst *PredTerm = Pred->getTerminator(); |
| 3016 | for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) { |
| 3017 | BasicBlock *PredSucc = PredTerm->getSuccessor(i); |
| 3018 | if (PredSucc == BB) |
| 3019 | continue; |
| 3020 | // If the predecessor has a successor that isn't BB and isn't |
| 3021 | // outside the loop, assume the worst. |
| 3022 | if (L->contains(PredSucc)) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3023 | return getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3024 | } |
| 3025 | if (Pred == L->getHeader()) { |
| 3026 | Ok = true; |
| 3027 | break; |
| 3028 | } |
| 3029 | BB = Pred; |
| 3030 | } |
| 3031 | if (!Ok) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3032 | return getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
| 3035 | // Procede to the next level to examine the exit condition expression. |
| 3036 | return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(), |
| 3037 | ExitBr->getSuccessor(0), |
| 3038 | ExitBr->getSuccessor(1)); |
| 3039 | } |
| 3040 | |
| 3041 | /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the |
| 3042 | /// backedge of the specified loop will execute if its exit condition |
| 3043 | /// were a conditional branch of ExitCond, TBB, and FBB. |
| 3044 | ScalarEvolution::BackedgeTakenInfo |
| 3045 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L, |
| 3046 | Value *ExitCond, |
| 3047 | BasicBlock *TBB, |
| 3048 | BasicBlock *FBB) { |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3049 | // 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] | 3050 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) { |
| 3051 | if (BO->getOpcode() == Instruction::And) { |
| 3052 | // Recurse on the operands of the and. |
| 3053 | BackedgeTakenInfo BTI0 = |
| 3054 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 3055 | BackedgeTakenInfo BTI1 = |
| 3056 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3057 | const SCEV *BECount = getCouldNotCompute(); |
| 3058 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3059 | if (L->contains(TBB)) { |
| 3060 | // Both conditions must be true for the loop to continue executing. |
| 3061 | // Choose the less conservative count. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3062 | if (BTI0.Exact == getCouldNotCompute() || |
| 3063 | BTI1.Exact == getCouldNotCompute()) |
| 3064 | BECount = getCouldNotCompute(); |
Dan Gohman | ac958b3 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3065 | else |
| 3066 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3067 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3068 | MaxBECount = BTI1.Max; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3069 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3070 | MaxBECount = BTI0.Max; |
Dan Gohman | ac958b3 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3071 | else |
| 3072 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3073 | } else { |
| 3074 | // Both conditions must be true for the loop to exit. |
| 3075 | assert(L->contains(FBB) && "Loop block has no successor in loop!"); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3076 | if (BTI0.Exact != getCouldNotCompute() && |
| 3077 | BTI1.Exact != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3078 | BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3079 | if (BTI0.Max != getCouldNotCompute() && |
| 3080 | BTI1.Max != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3081 | MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max); |
| 3082 | } |
| 3083 | |
| 3084 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3085 | } |
| 3086 | if (BO->getOpcode() == Instruction::Or) { |
| 3087 | // Recurse on the operands of the or. |
| 3088 | BackedgeTakenInfo BTI0 = |
| 3089 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 3090 | BackedgeTakenInfo BTI1 = |
| 3091 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3092 | const SCEV *BECount = getCouldNotCompute(); |
| 3093 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3094 | if (L->contains(FBB)) { |
| 3095 | // Both conditions must be false for the loop to continue executing. |
| 3096 | // Choose the less conservative count. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3097 | if (BTI0.Exact == getCouldNotCompute() || |
| 3098 | BTI1.Exact == getCouldNotCompute()) |
| 3099 | BECount = getCouldNotCompute(); |
Dan Gohman | ac958b3 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3100 | else |
| 3101 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3102 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3103 | MaxBECount = BTI1.Max; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3104 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3105 | MaxBECount = BTI0.Max; |
Dan Gohman | ac958b3 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3106 | else |
| 3107 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3108 | } else { |
| 3109 | // Both conditions must be false for the loop to exit. |
| 3110 | assert(L->contains(TBB) && "Loop block has no successor in loop!"); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3111 | if (BTI0.Exact != getCouldNotCompute() && |
| 3112 | BTI1.Exact != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3113 | BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3114 | if (BTI0.Max != getCouldNotCompute() && |
| 3115 | BTI1.Max != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3116 | MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max); |
| 3117 | } |
| 3118 | |
| 3119 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3120 | } |
| 3121 | } |
| 3122 | |
| 3123 | // With an icmp, it may be feasible to compute an exact backedge-taken count. |
| 3124 | // Procede to the next level to examine the icmp. |
| 3125 | if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) |
| 3126 | return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3127 | |
Eli Friedman | 459d729 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3128 | // 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] | 3129 | return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
| 3130 | } |
| 3131 | |
| 3132 | /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the |
| 3133 | /// backedge of the specified loop will execute if its exit condition |
| 3134 | /// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB. |
| 3135 | ScalarEvolution::BackedgeTakenInfo |
| 3136 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L, |
| 3137 | ICmpInst *ExitCond, |
| 3138 | BasicBlock *TBB, |
| 3139 | BasicBlock *FBB) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3140 | |
| 3141 | // If the condition was exit on true, convert the condition to exit on false |
| 3142 | ICmpInst::Predicate Cond; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3143 | if (!L->contains(FBB)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3144 | Cond = ExitCond->getPredicate(); |
| 3145 | else |
| 3146 | Cond = ExitCond->getInversePredicate(); |
| 3147 | |
| 3148 | // Handle common loops like: for (X = "string"; *X; ++X) |
| 3149 | if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0))) |
| 3150 | if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3151 | const SCEV *ItCnt = |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3152 | ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3153 | if (!isa<SCEVCouldNotCompute>(ItCnt)) { |
| 3154 | unsigned BitWidth = getTypeSizeInBits(ItCnt->getType()); |
| 3155 | return BackedgeTakenInfo(ItCnt, |
| 3156 | isa<SCEVConstant>(ItCnt) ? ItCnt : |
| 3157 | getConstant(APInt::getMaxValue(BitWidth)-1)); |
| 3158 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3161 | const SCEV *LHS = getSCEV(ExitCond->getOperand(0)); |
| 3162 | const SCEV *RHS = getSCEV(ExitCond->getOperand(1)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3163 | |
| 3164 | // Try to evaluate any dependencies out of the loop. |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3165 | LHS = getSCEVAtScope(LHS, L); |
| 3166 | RHS = getSCEVAtScope(RHS, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3167 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3168 | // 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] | 3169 | // loop the predicate will return true for these inputs. |
Dan Gohman | 2d96e35 | 2008-09-16 18:52:57 +0000 | [diff] [blame] | 3170 | if (LHS->isLoopInvariant(L) && !RHS->isLoopInvariant(L)) { |
| 3171 | // If there is a loop-invariant, force it into the RHS. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3172 | std::swap(LHS, RHS); |
| 3173 | Cond = ICmpInst::getSwappedPredicate(Cond); |
| 3174 | } |
| 3175 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3176 | // If we have a comparison of a chrec against a constant, try to use value |
| 3177 | // ranges to answer this query. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3178 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) |
| 3179 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3180 | if (AddRec->getLoop() == L) { |
Eli Friedman | 459d729 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3181 | // Form the constant range. |
| 3182 | ConstantRange CompRange( |
| 3183 | ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue())); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3184 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3185 | const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this); |
Eli Friedman | 459d729 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3186 | if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | switch (Cond) { |
| 3190 | case ICmpInst::ICMP_NE: { // while (X != Y) |
| 3191 | // Convert to: while (X-Y != 0) |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3192 | const SCEV *TC = HowFarToZero(getMinusSCEV(LHS, RHS), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3193 | if (!isa<SCEVCouldNotCompute>(TC)) return TC; |
| 3194 | break; |
| 3195 | } |
| 3196 | case ICmpInst::ICMP_EQ: { |
| 3197 | // Convert to: while (X-Y == 0) // while (X == Y) |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3198 | const SCEV *TC = HowFarToNonZero(getMinusSCEV(LHS, RHS), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3199 | if (!isa<SCEVCouldNotCompute>(TC)) return TC; |
| 3200 | break; |
| 3201 | } |
| 3202 | case ICmpInst::ICMP_SLT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3203 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true); |
| 3204 | if (BTI.hasAnyInfo()) return BTI; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3205 | break; |
| 3206 | } |
| 3207 | case ICmpInst::ICMP_SGT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3208 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 3209 | getNotSCEV(RHS), L, true); |
| 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_ULT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3214 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false); |
| 3215 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | b7c2894 | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 3216 | break; |
| 3217 | } |
| 3218 | case ICmpInst::ICMP_UGT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3219 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 3220 | getNotSCEV(RHS), L, false); |
| 3221 | if (BTI.hasAnyInfo()) return BTI; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3222 | break; |
| 3223 | } |
| 3224 | default: |
| 3225 | #if 0 |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3226 | errs() << "ComputeBackedgeTakenCount "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3227 | if (ExitCond->getOperand(0)->getType()->isUnsigned()) |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3228 | errs() << "[unsigned] "; |
| 3229 | errs() << *LHS << " " |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3230 | << Instruction::getOpcodeName(Instruction::ICmp) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3231 | << " " << *RHS << "\n"; |
| 3232 | #endif |
| 3233 | break; |
| 3234 | } |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3235 | return |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3236 | ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | static ConstantInt * |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3240 | EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, |
| 3241 | ScalarEvolution &SE) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3242 | const SCEV *InVal = SE.getConstant(C); |
| 3243 | const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3244 | assert(isa<SCEVConstant>(Val) && |
| 3245 | "Evaluation of SCEV at constant didn't fold correctly?"); |
| 3246 | return cast<SCEVConstant>(Val)->getValue(); |
| 3247 | } |
| 3248 | |
| 3249 | /// GetAddressedElementFromGlobal - Given a global variable with an initializer |
| 3250 | /// and a GEP expression (missing the pointer index) indexing into it, return |
| 3251 | /// the addressed element of the initializer or null if the index expression is |
| 3252 | /// invalid. |
| 3253 | static Constant * |
Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 3254 | GetAddressedElementFromGlobal(LLVMContext *Context, GlobalVariable *GV, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3255 | const std::vector<ConstantInt*> &Indices) { |
| 3256 | Constant *Init = GV->getInitializer(); |
| 3257 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) { |
| 3258 | uint64_t Idx = Indices[i]->getZExtValue(); |
| 3259 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) { |
| 3260 | assert(Idx < CS->getNumOperands() && "Bad struct index!"); |
| 3261 | Init = cast<Constant>(CS->getOperand(Idx)); |
| 3262 | } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) { |
| 3263 | if (Idx >= CA->getNumOperands()) return 0; // Bogus program |
| 3264 | Init = cast<Constant>(CA->getOperand(Idx)); |
| 3265 | } else if (isa<ConstantAggregateZero>(Init)) { |
| 3266 | if (const StructType *STy = dyn_cast<StructType>(Init->getType())) { |
| 3267 | assert(Idx < STy->getNumElements() && "Bad struct index!"); |
Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 3268 | Init = Context->getNullValue(STy->getElementType(Idx)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3269 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) { |
| 3270 | if (Idx >= ATy->getNumElements()) return 0; // Bogus program |
Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 3271 | Init = Context->getNullValue(ATy->getElementType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3272 | } else { |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 3273 | LLVM_UNREACHABLE("Unknown constant aggregate type!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3274 | } |
| 3275 | return 0; |
| 3276 | } else { |
| 3277 | return 0; // Unknown initializer type |
| 3278 | } |
| 3279 | } |
| 3280 | return Init; |
| 3281 | } |
| 3282 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3283 | /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of |
| 3284 | /// 'icmp op load X, cst', try to see if we can compute the backedge |
| 3285 | /// execution count. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3286 | const SCEV * |
| 3287 | ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount( |
| 3288 | LoadInst *LI, |
| 3289 | Constant *RHS, |
| 3290 | const Loop *L, |
| 3291 | ICmpInst::Predicate predicate) { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3292 | if (LI->isVolatile()) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3293 | |
| 3294 | // Check to see if the loaded pointer is a getelementptr of a global. |
| 3295 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3296 | if (!GEP) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3297 | |
| 3298 | // Make sure that it is really a constant global we are gepping, with an |
| 3299 | // initializer, and make sure the first IDX is really 0. |
| 3300 | GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); |
| 3301 | if (!GV || !GV->isConstant() || !GV->hasInitializer() || |
| 3302 | GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) || |
| 3303 | !cast<Constant>(GEP->getOperand(1))->isNullValue()) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3304 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3305 | |
| 3306 | // Okay, we allow one non-constant index into the GEP instruction. |
| 3307 | Value *VarIdx = 0; |
| 3308 | std::vector<ConstantInt*> Indexes; |
| 3309 | unsigned VarIdxNum = 0; |
| 3310 | for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) |
| 3311 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 3312 | Indexes.push_back(CI); |
| 3313 | } else if (!isa<ConstantInt>(GEP->getOperand(i))) { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3314 | if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3315 | VarIdx = GEP->getOperand(i); |
| 3316 | VarIdxNum = i-2; |
| 3317 | Indexes.push_back(0); |
| 3318 | } |
| 3319 | |
| 3320 | // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant. |
| 3321 | // Check to see if X is a loop variant variable value now. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3322 | const SCEV *Idx = getSCEV(VarIdx); |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3323 | Idx = getSCEVAtScope(Idx, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3324 | |
| 3325 | // We can only recognize very limited forms of loop index expressions, in |
| 3326 | // particular, only affine AddRec's like {C1,+,C2}. |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3327 | const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3328 | if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) || |
| 3329 | !isa<SCEVConstant>(IdxExpr->getOperand(0)) || |
| 3330 | !isa<SCEVConstant>(IdxExpr->getOperand(1))) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3331 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3332 | |
| 3333 | unsigned MaxSteps = MaxBruteForceIterations; |
| 3334 | for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) { |
| 3335 | ConstantInt *ItCst = |
Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 3336 | ConstantInt::get(cast<IntegerType>(IdxExpr->getType()), IterationNum); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3337 | ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3338 | |
| 3339 | // Form the GEP offset. |
| 3340 | Indexes[VarIdxNum] = Val; |
| 3341 | |
Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 3342 | Constant *Result = GetAddressedElementFromGlobal(Context, GV, Indexes); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3343 | if (Result == 0) break; // Cannot compute! |
| 3344 | |
| 3345 | // Evaluate the condition for this iteration. |
| 3346 | Result = ConstantExpr::getICmp(predicate, Result, RHS); |
| 3347 | if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure |
| 3348 | if (cast<ConstantInt>(Result)->getValue().isMinValue()) { |
| 3349 | #if 0 |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3350 | errs() << "\n***\n*** Computed loop count " << *ItCst |
| 3351 | << "\n*** From global " << *GV << "*** BB: " << *L->getHeader() |
| 3352 | << "***\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3353 | #endif |
| 3354 | ++NumArrayLenItCounts; |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3355 | return getConstant(ItCst); // Found terminating iteration! |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3356 | } |
| 3357 | } |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3358 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
| 3361 | |
| 3362 | /// CanConstantFold - Return true if we can constant fold an instruction of the |
| 3363 | /// specified type, assuming that all operands were constants. |
| 3364 | static bool CanConstantFold(const Instruction *I) { |
| 3365 | if (isa<BinaryOperator>(I) || isa<CmpInst>(I) || |
| 3366 | isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I)) |
| 3367 | return true; |
| 3368 | |
| 3369 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 3370 | if (const Function *F = CI->getCalledFunction()) |
Dan Gohman | e6e001f | 2008-01-31 01:05:10 +0000 | [diff] [blame] | 3371 | return canConstantFoldCallTo(F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3372 | return false; |
| 3373 | } |
| 3374 | |
| 3375 | /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node |
| 3376 | /// in the loop that V is derived from. We allow arbitrary operations along the |
| 3377 | /// way, but the operands of an operation must either be constants or a value |
| 3378 | /// derived from a constant PHI. If this expression does not fit with these |
| 3379 | /// constraints, return null. |
| 3380 | static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) { |
| 3381 | // If this is not an instruction, or if this is an instruction outside of the |
| 3382 | // loop, it can't be derived from a loop PHI. |
| 3383 | Instruction *I = dyn_cast<Instruction>(V); |
| 3384 | if (I == 0 || !L->contains(I->getParent())) return 0; |
| 3385 | |
Anton Korobeynikov | 357a27d | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 3386 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3387 | if (L->getHeader() == I->getParent()) |
| 3388 | return PN; |
| 3389 | else |
| 3390 | // We don't currently keep track of the control flow needed to evaluate |
| 3391 | // PHIs, so we cannot handle PHIs inside of loops. |
| 3392 | return 0; |
Anton Korobeynikov | 357a27d | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 3393 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3394 | |
| 3395 | // If we won't be able to constant fold this expression even if the operands |
| 3396 | // are constants, return early. |
| 3397 | if (!CanConstantFold(I)) return 0; |
| 3398 | |
| 3399 | // Otherwise, we can evaluate this instruction if all of its operands are |
| 3400 | // constant or derived from a PHI node themselves. |
| 3401 | PHINode *PHI = 0; |
| 3402 | for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op) |
| 3403 | if (!(isa<Constant>(I->getOperand(Op)) || |
| 3404 | isa<GlobalValue>(I->getOperand(Op)))) { |
| 3405 | PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L); |
| 3406 | if (P == 0) return 0; // Not evolving from PHI |
| 3407 | if (PHI == 0) |
| 3408 | PHI = P; |
| 3409 | else if (PHI != P) |
| 3410 | return 0; // Evolving from multiple different PHIs. |
| 3411 | } |
| 3412 | |
| 3413 | // This is a expression evolving from a constant PHI! |
| 3414 | return PHI; |
| 3415 | } |
| 3416 | |
| 3417 | /// EvaluateExpression - Given an expression that passes the |
| 3418 | /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node |
| 3419 | /// in the loop has the value PHIVal. If we can't fold this expression for some |
| 3420 | /// reason, return null. |
| 3421 | static Constant *EvaluateExpression(Value *V, Constant *PHIVal) { |
| 3422 | if (isa<PHINode>(V)) return PHIVal; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3423 | if (Constant *C = dyn_cast<Constant>(V)) return C; |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3424 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3425 | Instruction *I = cast<Instruction>(V); |
Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 3426 | LLVMContext *Context = I->getParent()->getContext(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3427 | |
| 3428 | std::vector<Constant*> Operands; |
| 3429 | Operands.resize(I->getNumOperands()); |
| 3430 | |
| 3431 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 3432 | Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal); |
| 3433 | if (Operands[i] == 0) return 0; |
| 3434 | } |
| 3435 | |
Chris Lattner | d6e5691 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3436 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 3437 | return ConstantFoldCompareInstOperands(CI->getPredicate(), |
Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 3438 | &Operands[0], Operands.size(), |
| 3439 | Context); |
Chris Lattner | d6e5691 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3440 | else |
| 3441 | return ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 3442 | &Operands[0], Operands.size(), |
| 3443 | Context); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
| 3446 | /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is |
| 3447 | /// in the header of its containing loop, we know the loop executes a |
| 3448 | /// constant number of times, and the PHI node is just a recurrence |
| 3449 | /// involving constants, fold it. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3450 | Constant * |
| 3451 | ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN, |
| 3452 | const APInt& BEs, |
| 3453 | const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3454 | std::map<PHINode*, Constant*>::iterator I = |
| 3455 | ConstantEvolutionLoopExitValue.find(PN); |
| 3456 | if (I != ConstantEvolutionLoopExitValue.end()) |
| 3457 | return I->second; |
| 3458 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3459 | if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations))) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3460 | return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it. |
| 3461 | |
| 3462 | Constant *&RetVal = ConstantEvolutionLoopExitValue[PN]; |
| 3463 | |
| 3464 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 3465 | // entry must be a constant (coming in from outside of the loop), and the |
| 3466 | // second must be derived from the same PHI. |
| 3467 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 3468 | Constant *StartCST = |
| 3469 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
| 3470 | if (StartCST == 0) |
| 3471 | return RetVal = 0; // Must be a constant. |
| 3472 | |
| 3473 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
| 3474 | PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); |
| 3475 | if (PN2 != PN) |
| 3476 | return RetVal = 0; // Not derived from same PHI. |
| 3477 | |
| 3478 | // Execute the loop symbolically to determine the exit value. |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3479 | if (BEs.getActiveBits() >= 32) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3480 | return RetVal = 0; // More than 2^32-1 iterations?? Not doing it! |
| 3481 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3482 | unsigned NumIterations = BEs.getZExtValue(); // must be in range |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3483 | unsigned IterationNum = 0; |
| 3484 | for (Constant *PHIVal = StartCST; ; ++IterationNum) { |
| 3485 | if (IterationNum == NumIterations) |
| 3486 | return RetVal = PHIVal; // Got exit value! |
| 3487 | |
| 3488 | // Compute the value of the PHI node for the next iteration. |
| 3489 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal); |
| 3490 | if (NextPHI == PHIVal) |
| 3491 | return RetVal = NextPHI; // Stopped evolving! |
| 3492 | if (NextPHI == 0) |
| 3493 | return 0; // Couldn't evaluate! |
| 3494 | PHIVal = NextPHI; |
| 3495 | } |
| 3496 | } |
| 3497 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3498 | /// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute a |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3499 | /// constant number of times (the condition evolves only from constants), |
| 3500 | /// try to evaluate a few iterations of the loop until we get the exit |
| 3501 | /// condition gets a value of ExitWhen (true or false). If we cannot |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3502 | /// evaluate the trip count of the loop, return getCouldNotCompute(). |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3503 | const SCEV * |
| 3504 | ScalarEvolution::ComputeBackedgeTakenCountExhaustively(const Loop *L, |
| 3505 | Value *Cond, |
| 3506 | bool ExitWhen) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3507 | PHINode *PN = getConstantEvolvingPHI(Cond, L); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3508 | if (PN == 0) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3509 | |
| 3510 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 3511 | // entry must be a constant (coming in from outside of the loop), and the |
| 3512 | // second must be derived from the same PHI. |
| 3513 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 3514 | Constant *StartCST = |
| 3515 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3516 | if (StartCST == 0) return getCouldNotCompute(); // Must be a constant. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3517 | |
| 3518 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
| 3519 | PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3520 | if (PN2 != PN) return getCouldNotCompute(); // Not derived from same PHI. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3521 | |
| 3522 | // Okay, we find a PHI node that defines the trip count of this loop. Execute |
| 3523 | // the loop symbolically to determine when the condition gets a value of |
| 3524 | // "ExitWhen". |
| 3525 | unsigned IterationNum = 0; |
| 3526 | unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis. |
| 3527 | for (Constant *PHIVal = StartCST; |
| 3528 | IterationNum != MaxIterations; ++IterationNum) { |
| 3529 | ConstantInt *CondVal = |
| 3530 | dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal)); |
| 3531 | |
| 3532 | // Couldn't symbolically evaluate. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3533 | if (!CondVal) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3534 | |
| 3535 | if (CondVal->getValue() == uint64_t(ExitWhen)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3536 | ++NumBruteForceTripCountsComputed; |
Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 3537 | return getConstant(Type::Int32Ty, IterationNum); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | // Compute the value of the PHI node for the next iteration. |
| 3541 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal); |
| 3542 | if (NextPHI == 0 || NextPHI == PHIVal) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3543 | return getCouldNotCompute();// Couldn't evaluate or not making progress... |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3544 | PHIVal = NextPHI; |
| 3545 | } |
| 3546 | |
| 3547 | // Too many iterations were needed to evaluate. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3548 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3549 | } |
| 3550 | |
Dan Gohman | dd40e9a | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 3551 | /// getSCEVAtScope - Return a SCEV expression handle for the specified value |
| 3552 | /// at the specified scope in the program. The L value specifies a loop |
| 3553 | /// nest to evaluate the expression at, where null is the top-level or a |
| 3554 | /// specified loop is immediately inside of the loop. |
| 3555 | /// |
| 3556 | /// This method can be used to compute the exit value for a variable defined |
| 3557 | /// in a loop by querying what the value will hold in the parent loop. |
| 3558 | /// |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3559 | /// In the case that a relevant loop exit value cannot be computed, the |
| 3560 | /// original value V is returned. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3561 | const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3562 | // FIXME: this should be turned into a virtual method on SCEV! |
| 3563 | |
| 3564 | if (isa<SCEVConstant>(V)) return V; |
| 3565 | |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 3566 | // If this instruction is evolved from a constant-evolving PHI, compute the |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3567 | // exit value from the loop without using SCEVs. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3568 | if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3569 | if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3570 | const Loop *LI = (*this->LI)[I->getParent()]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3571 | if (LI && LI->getParentLoop() == L) // Looking for loop exit value. |
| 3572 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3573 | if (PN->getParent() == LI->getHeader()) { |
| 3574 | // Okay, there is no closed form solution for the PHI node. Check |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3575 | // to see if the loop that contains it has a known backedge-taken |
| 3576 | // count. If so, we may be able to force computation of the exit |
| 3577 | // value. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3578 | const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3579 | if (const SCEVConstant *BTCC = |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3580 | dyn_cast<SCEVConstant>(BackedgeTakenCount)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3581 | // Okay, we know how many times the containing loop executes. If |
| 3582 | // this is a constant evolving PHI node, get the final value at |
| 3583 | // the specified iteration number. |
| 3584 | Constant *RV = getConstantEvolutionLoopExitValue(PN, |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3585 | BTCC->getValue()->getValue(), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3586 | LI); |
Dan Gohman | 652caf1 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3587 | if (RV) return getSCEV(RV); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3588 | } |
| 3589 | } |
| 3590 | |
| 3591 | // Okay, this is an expression that we cannot symbolically evaluate |
| 3592 | // into a SCEV. Check to see if it's possible to symbolically evaluate |
| 3593 | // the arguments into constants, and if so, try to constant propagate the |
| 3594 | // result. This is particularly useful for computing loop exit values. |
| 3595 | if (CanConstantFold(I)) { |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3596 | // Check to see if we've folded this instruction at this loop before. |
| 3597 | std::map<const Loop *, Constant *> &Values = ValuesAtScopes[I]; |
| 3598 | std::pair<std::map<const Loop *, Constant *>::iterator, bool> Pair = |
| 3599 | Values.insert(std::make_pair(L, static_cast<Constant *>(0))); |
| 3600 | if (!Pair.second) |
Dan Gohman | 652caf1 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3601 | return Pair.first->second ? &*getSCEV(Pair.first->second) : V; |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3602 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3603 | std::vector<Constant*> Operands; |
| 3604 | Operands.reserve(I->getNumOperands()); |
| 3605 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 3606 | Value *Op = I->getOperand(i); |
| 3607 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| 3608 | Operands.push_back(C); |
| 3609 | } else { |
Chris Lattner | 3fff464 | 2007-11-23 08:46:22 +0000 | [diff] [blame] | 3610 | // If any of the operands is non-constant and if they are |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3611 | // non-integer and non-pointer, don't even try to analyze them |
| 3612 | // with scev techniques. |
Dan Gohman | 5e4eb76 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3613 | if (!isSCEVable(Op->getType())) |
Chris Lattner | 3fff464 | 2007-11-23 08:46:22 +0000 | [diff] [blame] | 3614 | return V; |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3615 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 3616 | const SCEV *OpV = getSCEVAtScope(getSCEV(Op), L); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3617 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) { |
Dan Gohman | 5e4eb76 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3618 | Constant *C = SC->getValue(); |
| 3619 | if (C->getType() != Op->getType()) |
| 3620 | C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 3621 | Op->getType(), |
| 3622 | false), |
| 3623 | C, Op->getType()); |
| 3624 | Operands.push_back(C); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3625 | } else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) { |
Dan Gohman | 5e4eb76 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3626 | if (Constant *C = dyn_cast<Constant>(SU->getValue())) { |
| 3627 | if (C->getType() != Op->getType()) |
| 3628 | C = |
| 3629 | ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 3630 | Op->getType(), |
| 3631 | false), |
| 3632 | C, Op->getType()); |
| 3633 | Operands.push_back(C); |
| 3634 | } else |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3635 | return V; |
| 3636 | } else { |
| 3637 | return V; |
| 3638 | } |
| 3639 | } |
| 3640 | } |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3641 | |
Chris Lattner | d6e5691 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3642 | Constant *C; |
| 3643 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 3644 | C = ConstantFoldCompareInstOperands(CI->getPredicate(), |
Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 3645 | &Operands[0], Operands.size(), |
| 3646 | Context); |
Chris Lattner | d6e5691 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3647 | else |
| 3648 | C = ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 3649 | &Operands[0], Operands.size(), Context); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3650 | Pair.first->second = C; |
Dan Gohman | 652caf1 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3651 | return getSCEV(C); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3652 | } |
| 3653 | } |
| 3654 | |
| 3655 | // This is some other type of SCEVUnknown, just return it. |
| 3656 | return V; |
| 3657 | } |
| 3658 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3659 | if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3660 | // Avoid performing the look-up in the common case where the specified |
| 3661 | // expression has no loop-variant portions. |
| 3662 | for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3663 | const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3664 | if (OpAtScope != Comm->getOperand(i)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3665 | // Okay, at least one of these operands is loop variant but might be |
| 3666 | // foldable. Build a new instance of the folded commutative expression. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3667 | SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(), |
| 3668 | Comm->op_begin()+i); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3669 | NewOps.push_back(OpAtScope); |
| 3670 | |
| 3671 | for (++i; i != e; ++i) { |
| 3672 | OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3673 | NewOps.push_back(OpAtScope); |
| 3674 | } |
| 3675 | if (isa<SCEVAddExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3676 | return getAddExpr(NewOps); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 3677 | if (isa<SCEVMulExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3678 | return getMulExpr(NewOps); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 3679 | if (isa<SCEVSMaxExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3680 | return getSMaxExpr(NewOps); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 3681 | if (isa<SCEVUMaxExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3682 | return getUMaxExpr(NewOps); |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 3683 | LLVM_UNREACHABLE("Unknown commutative SCEV type!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3684 | } |
| 3685 | } |
| 3686 | // If we got here, all operands are loop invariant. |
| 3687 | return Comm; |
| 3688 | } |
| 3689 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3690 | if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3691 | const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L); |
| 3692 | const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L); |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 3693 | if (LHS == Div->getLHS() && RHS == Div->getRHS()) |
| 3694 | return Div; // must be loop invariant |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3695 | return getUDivExpr(LHS, RHS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | // If this is a loop recurrence for a loop that does not contain L, then we |
| 3699 | // are dealing with the final value computed by the loop. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3700 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3701 | if (!L || !AddRec->getLoop()->contains(L->getHeader())) { |
| 3702 | // To evaluate this recurrence, we need to know how many times the AddRec |
| 3703 | // loop iterates. Compute this now. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3704 | const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop()); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3705 | if (BackedgeTakenCount == getCouldNotCompute()) return AddRec; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3706 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 3707 | // Then, evaluate the AddRec. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3708 | return AddRec->evaluateAtIteration(BackedgeTakenCount, *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3709 | } |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3710 | return AddRec; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3711 | } |
| 3712 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3713 | if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3714 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | 78d63c8 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 3715 | if (Op == Cast->getOperand()) |
| 3716 | return Cast; // must be loop invariant |
| 3717 | return getZeroExtendExpr(Op, Cast->getType()); |
| 3718 | } |
| 3719 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3720 | if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3721 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | 78d63c8 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 3722 | if (Op == Cast->getOperand()) |
| 3723 | return Cast; // must be loop invariant |
| 3724 | return getSignExtendExpr(Op, Cast->getType()); |
| 3725 | } |
| 3726 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3727 | if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3728 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | 78d63c8 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 3729 | if (Op == Cast->getOperand()) |
| 3730 | return Cast; // must be loop invariant |
| 3731 | return getTruncateExpr(Op, Cast->getType()); |
| 3732 | } |
| 3733 | |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 3734 | LLVM_UNREACHABLE("Unknown SCEV type!"); |
Daniel Dunbar | a95d96c | 2009-05-18 16:43:04 +0000 | [diff] [blame] | 3735 | return 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3736 | } |
| 3737 | |
Dan Gohman | dd40e9a | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 3738 | /// getSCEVAtScope - This is a convenience function which does |
| 3739 | /// getSCEVAtScope(getSCEV(V), L). |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3740 | const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3741 | return getSCEVAtScope(getSCEV(V), L); |
| 3742 | } |
| 3743 | |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3744 | /// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the |
| 3745 | /// following equation: |
| 3746 | /// |
| 3747 | /// A * X = B (mod N) |
| 3748 | /// |
| 3749 | /// where N = 2^BW and BW is the common bit width of A and B. The signedness of |
| 3750 | /// A and B isn't important. |
| 3751 | /// |
| 3752 | /// If the equation does not have a solution, SCEVCouldNotCompute is returned. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3753 | static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const APInt &B, |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3754 | ScalarEvolution &SE) { |
| 3755 | uint32_t BW = A.getBitWidth(); |
| 3756 | assert(BW == B.getBitWidth() && "Bit widths must be the same."); |
| 3757 | assert(A != 0 && "A must be non-zero."); |
| 3758 | |
| 3759 | // 1. D = gcd(A, N) |
| 3760 | // |
| 3761 | // The gcd of A and N may have only one prime factor: 2. The number of |
| 3762 | // trailing zeros in A is its multiplicity |
| 3763 | uint32_t Mult2 = A.countTrailingZeros(); |
| 3764 | // D = 2^Mult2 |
| 3765 | |
| 3766 | // 2. Check if B is divisible by D. |
| 3767 | // |
| 3768 | // B is divisible by D if and only if the multiplicity of prime factor 2 for B |
| 3769 | // is not less than multiplicity of this prime factor for D. |
| 3770 | if (B.countTrailingZeros() < Mult2) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 3771 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3772 | |
| 3773 | // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic |
| 3774 | // modulo (N / D). |
| 3775 | // |
| 3776 | // (N / D) may need BW+1 bits in its representation. Hence, we'll use this |
| 3777 | // bit width during computations. |
| 3778 | APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D |
| 3779 | APInt Mod(BW + 1, 0); |
| 3780 | Mod.set(BW - Mult2); // Mod = N / D |
| 3781 | APInt I = AD.multiplicativeInverse(Mod); |
| 3782 | |
| 3783 | // 4. Compute the minimum unsigned root of the equation: |
| 3784 | // I * (B / D) mod (N / D) |
| 3785 | APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod); |
| 3786 | |
| 3787 | // The result is guaranteed to be less than 2^BW so we may truncate it to BW |
| 3788 | // bits. |
| 3789 | return SE.getConstant(Result.trunc(BW)); |
| 3790 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3791 | |
| 3792 | /// SolveQuadraticEquation - Find the roots of the quadratic equation for the |
| 3793 | /// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which |
| 3794 | /// might be the same) or two SCEVCouldNotCompute objects. |
| 3795 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3796 | static std::pair<const SCEV *,const SCEV *> |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3797 | SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3798 | assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3799 | const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0)); |
| 3800 | const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1)); |
| 3801 | const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3802 | |
| 3803 | // We currently can only solve this if the coefficients are constants. |
| 3804 | if (!LC || !MC || !NC) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3805 | const SCEV *CNC = SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3806 | return std::make_pair(CNC, CNC); |
| 3807 | } |
| 3808 | |
| 3809 | uint32_t BitWidth = LC->getValue()->getValue().getBitWidth(); |
| 3810 | const APInt &L = LC->getValue()->getValue(); |
| 3811 | const APInt &M = MC->getValue()->getValue(); |
| 3812 | const APInt &N = NC->getValue()->getValue(); |
| 3813 | APInt Two(BitWidth, 2); |
| 3814 | APInt Four(BitWidth, 4); |
| 3815 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3816 | { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3817 | using namespace APIntOps; |
| 3818 | const APInt& C = L; |
| 3819 | // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C |
| 3820 | // The B coefficient is M-N/2 |
| 3821 | APInt B(M); |
| 3822 | B -= sdiv(N,Two); |
| 3823 | |
| 3824 | // The A coefficient is N/2 |
| 3825 | APInt A(N.sdiv(Two)); |
| 3826 | |
| 3827 | // Compute the B^2-4ac term. |
| 3828 | APInt SqrtTerm(B); |
| 3829 | SqrtTerm *= B; |
| 3830 | SqrtTerm -= Four * (A * C); |
| 3831 | |
| 3832 | // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest |
| 3833 | // integer value or else APInt::sqrt() will assert. |
| 3834 | APInt SqrtVal(SqrtTerm.sqrt()); |
| 3835 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3836 | // Compute the two solutions for the quadratic formula. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3837 | // The divisions must be performed as signed divisions. |
| 3838 | APInt NegB(-B); |
| 3839 | APInt TwoA( A << 1 ); |
Nick Lewycky | 3577669 | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 3840 | if (TwoA.isMinValue()) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3841 | const SCEV *CNC = SE.getCouldNotCompute(); |
Nick Lewycky | 3577669 | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 3842 | return std::make_pair(CNC, CNC); |
| 3843 | } |
| 3844 | |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 3845 | LLVMContext *Context = SE.getContext(); |
| 3846 | |
| 3847 | ConstantInt *Solution1 = |
| 3848 | Context->getConstantInt((NegB + SqrtVal).sdiv(TwoA)); |
| 3849 | ConstantInt *Solution2 = |
| 3850 | Context->getConstantInt((NegB - SqrtVal).sdiv(TwoA)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3851 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3852 | return std::make_pair(SE.getConstant(Solution1), |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3853 | SE.getConstant(Solution2)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3854 | } // end APIntOps namespace |
| 3855 | } |
| 3856 | |
| 3857 | /// HowFarToZero - Return the number of times a backedge comparing the specified |
Dan Gohman | 0c85091 | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 3858 | /// value to zero will execute. If not computable, return CouldNotCompute. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3859 | const SCEV *ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3860 | // If the value is a constant |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3861 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3862 | // If the value is already zero, the branch will execute zero times. |
| 3863 | if (C->getValue()->isZero()) return C; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3864 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3867 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3868 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3869 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3870 | |
| 3871 | if (AddRec->isAffine()) { |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3872 | // If this is an affine expression, the execution count of this branch is |
| 3873 | // the minimum unsigned root of the following equation: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3874 | // |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3875 | // Start + Step*N = 0 (mod 2^BW) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3876 | // |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3877 | // equivalent to: |
| 3878 | // |
| 3879 | // Step*N = -Start (mod 2^BW) |
| 3880 | // |
| 3881 | // where BW is the common bit width of Start and Step. |
| 3882 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3883 | // Get the initial value for the loop. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3884 | const SCEV *Start = getSCEVAtScope(AddRec->getStart(), |
| 3885 | L->getParentLoop()); |
| 3886 | const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), |
| 3887 | L->getParentLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3888 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3889 | if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) { |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3890 | // For now we handle only constant steps. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3891 | |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3892 | // First, handle unitary steps. |
| 3893 | if (StepC->getValue()->equalsInt(1)) // 1*N = -Start (mod 2^BW), so: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3894 | return getNegativeSCEV(Start); // N = -Start (as unsigned) |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3895 | if (StepC->getValue()->isAllOnesValue()) // -1*N = -Start (mod 2^BW), so: |
| 3896 | return Start; // N = Start (as unsigned) |
| 3897 | |
| 3898 | // Then, try to solve the above equation provided that Start is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3899 | if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3900 | return SolveLinEquationWithOverflow(StepC->getValue()->getValue(), |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3901 | -StartC->getValue()->getValue(), |
| 3902 | *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3903 | } |
| 3904 | } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) { |
| 3905 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of |
| 3906 | // the quadratic equation to solve it. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3907 | std::pair<const SCEV *,const SCEV *> Roots = SolveQuadraticEquation(AddRec, |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3908 | *this); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3909 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 3910 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3911 | if (R1) { |
| 3912 | #if 0 |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3913 | errs() << "HFTZ: " << *V << " - sol#1: " << *R1 |
| 3914 | << " sol#2: " << *R2 << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3915 | #endif |
| 3916 | // Pick the smallest positive root value. |
| 3917 | if (ConstantInt *CB = |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 3918 | dyn_cast<ConstantInt>(Context->getConstantExprICmp(ICmpInst::ICMP_ULT, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3919 | R1->getValue(), R2->getValue()))) { |
| 3920 | if (CB->getZExtValue() == false) |
| 3921 | std::swap(R1, R2); // R1 is the minimum root now. |
| 3922 | |
| 3923 | // We can only use this value if the chrec ends up with an exact zero |
| 3924 | // value at this index. When solving for "X*X != 5", for example, we |
| 3925 | // should not accept a root of 2. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3926 | const SCEV *Val = AddRec->evaluateAtIteration(R1, *this); |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 3927 | if (Val->isZero()) |
| 3928 | return R1; // We found a quadratic root! |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3929 | } |
| 3930 | } |
| 3931 | } |
| 3932 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3933 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | /// HowFarToNonZero - Return the number of times a backedge checking the |
| 3937 | /// specified value for nonzero will execute. If not computable, return |
Dan Gohman | 0c85091 | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 3938 | /// CouldNotCompute |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3939 | const SCEV *ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3940 | // Loops that look like: while (X == 0) are very strange indeed. We don't |
| 3941 | // handle them yet except for the trivial case. This could be expanded in the |
| 3942 | // future as needed. |
| 3943 | |
| 3944 | // If the value is a constant, check to see if it is known to be non-zero |
| 3945 | // already. If so, the backedge will execute zero times. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3946 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Nick Lewycky | f680518 | 2008-02-21 09:14:53 +0000 | [diff] [blame] | 3947 | if (!C->getValue()->isNullValue()) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3948 | return getIntegerSCEV(0, C->getType()); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3949 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | // We could implement others, but I really doubt anyone writes loops like |
| 3953 | // this, and if they did, they would already be constant folded. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3954 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3955 | } |
| 3956 | |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 3957 | /// getLoopPredecessor - If the given loop's header has exactly one unique |
| 3958 | /// predecessor outside the loop, return it. Otherwise return null. |
| 3959 | /// |
| 3960 | BasicBlock *ScalarEvolution::getLoopPredecessor(const Loop *L) { |
| 3961 | BasicBlock *Header = L->getHeader(); |
| 3962 | BasicBlock *Pred = 0; |
| 3963 | for (pred_iterator PI = pred_begin(Header), E = pred_end(Header); |
| 3964 | PI != E; ++PI) |
| 3965 | if (!L->contains(*PI)) { |
| 3966 | if (Pred && Pred != *PI) return 0; // Multiple predecessors. |
| 3967 | Pred = *PI; |
| 3968 | } |
| 3969 | return Pred; |
| 3970 | } |
| 3971 | |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 3972 | /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB |
| 3973 | /// (which may not be an immediate predecessor) which has exactly one |
| 3974 | /// successor from which BB is reachable, or null if no such block is |
| 3975 | /// found. |
| 3976 | /// |
| 3977 | BasicBlock * |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3978 | ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) { |
Dan Gohman | 1116ea7 | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 3979 | // If the block has a unique predecessor, then there is no path from the |
| 3980 | // predecessor to the block that does not go through the direct edge |
| 3981 | // from the predecessor to the block. |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 3982 | if (BasicBlock *Pred = BB->getSinglePredecessor()) |
| 3983 | return Pred; |
| 3984 | |
| 3985 | // 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] | 3986 | // If the header has a unique predecessor outside the loop, it must be |
| 3987 | // a block that has exactly one successor that can reach the loop. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3988 | if (Loop *L = LI->getLoopFor(BB)) |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 3989 | return getLoopPredecessor(L); |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 3990 | |
| 3991 | return 0; |
| 3992 | } |
| 3993 | |
Dan Gohman | bc1e347 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 3994 | /// HasSameValue - SCEV structural equivalence is usually sufficient for |
| 3995 | /// testing whether two expressions are equal, however for the purposes of |
| 3996 | /// looking for a condition guarding a loop, it can be useful to be a little |
| 3997 | /// more general, since a front-end may have replicated the controlling |
| 3998 | /// expression. |
| 3999 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4000 | static bool HasSameValue(const SCEV *A, const SCEV *B) { |
Dan Gohman | bc1e347 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4001 | // Quick check to see if they are the same SCEV. |
| 4002 | if (A == B) return true; |
| 4003 | |
| 4004 | // Otherwise, if they're both SCEVUnknown, it's possible that they hold |
| 4005 | // two different instructions with the same value. Check for this case. |
| 4006 | if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A)) |
| 4007 | if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B)) |
| 4008 | if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue())) |
| 4009 | if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue())) |
| 4010 | if (AI->isIdenticalTo(BI)) |
| 4011 | return true; |
| 4012 | |
| 4013 | // Otherwise assume they may have a different value. |
| 4014 | return false; |
| 4015 | } |
| 4016 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4017 | /// isLoopGuardedByCond - Test whether entry to the loop is protected by |
| 4018 | /// a conditional between LHS and RHS. This is used to help avoid max |
| 4019 | /// expressions in loop trip counts. |
| 4020 | bool ScalarEvolution::isLoopGuardedByCond(const Loop *L, |
| 4021 | ICmpInst::Predicate Pred, |
| 4022 | const SCEV *LHS, const SCEV *RHS) { |
Dan Gohman | 8b93818 | 2009-05-18 16:03:58 +0000 | [diff] [blame] | 4023 | // Interpret a null as meaning no loop, where there is obviously no guard |
| 4024 | // (interprocedural conditions notwithstanding). |
| 4025 | if (!L) return false; |
| 4026 | |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4027 | BasicBlock *Predecessor = getLoopPredecessor(L); |
| 4028 | BasicBlock *PredecessorDest = L->getHeader(); |
Nick Lewycky | 1b020bf | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4029 | |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4030 | // Starting at the loop predecessor, climb up the predecessor chain, as long |
| 4031 | // as there are predecessors that can be found that have unique successors |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4032 | // leading to the original header. |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4033 | for (; Predecessor; |
| 4034 | PredecessorDest = Predecessor, |
| 4035 | Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) { |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4036 | |
| 4037 | BranchInst *LoopEntryPredicate = |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4038 | dyn_cast<BranchInst>(Predecessor->getTerminator()); |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4039 | if (!LoopEntryPredicate || |
| 4040 | LoopEntryPredicate->isUnconditional()) |
| 4041 | continue; |
| 4042 | |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4043 | if (isNecessaryCond(LoopEntryPredicate->getCondition(), Pred, LHS, RHS, |
| 4044 | LoopEntryPredicate->getSuccessor(0) != PredecessorDest)) |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4045 | return true; |
Nick Lewycky | 1b020bf | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4046 | } |
| 4047 | |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4048 | return false; |
Nick Lewycky | 1b020bf | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4049 | } |
| 4050 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4051 | /// isNecessaryCond - Test whether the given CondValue value is a condition |
| 4052 | /// which is at least as strict as the one described by Pred, LHS, and RHS. |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4053 | bool ScalarEvolution::isNecessaryCond(Value *CondValue, |
| 4054 | ICmpInst::Predicate Pred, |
| 4055 | const SCEV *LHS, const SCEV *RHS, |
| 4056 | bool Inverse) { |
| 4057 | // Recursivly handle And and Or conditions. |
| 4058 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CondValue)) { |
| 4059 | if (BO->getOpcode() == Instruction::And) { |
| 4060 | if (!Inverse) |
| 4061 | return isNecessaryCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) || |
| 4062 | isNecessaryCond(BO->getOperand(1), Pred, LHS, RHS, Inverse); |
| 4063 | } else if (BO->getOpcode() == Instruction::Or) { |
| 4064 | if (Inverse) |
| 4065 | return isNecessaryCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) || |
| 4066 | isNecessaryCond(BO->getOperand(1), Pred, LHS, RHS, Inverse); |
| 4067 | } |
| 4068 | } |
| 4069 | |
| 4070 | ICmpInst *ICI = dyn_cast<ICmpInst>(CondValue); |
| 4071 | if (!ICI) return false; |
| 4072 | |
| 4073 | // Now that we found a conditional branch that dominates the loop, check to |
| 4074 | // see if it is the comparison we are looking for. |
| 4075 | Value *PreCondLHS = ICI->getOperand(0); |
| 4076 | Value *PreCondRHS = ICI->getOperand(1); |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4077 | ICmpInst::Predicate Cond; |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4078 | if (Inverse) |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4079 | Cond = ICI->getInversePredicate(); |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4080 | else |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4081 | Cond = ICI->getPredicate(); |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4082 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4083 | if (Cond == Pred) |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4084 | ; // An exact match. |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4085 | else if (!ICmpInst::isTrueWhenEqual(Cond) && Pred == ICmpInst::ICMP_NE) |
| 4086 | ; // The actual condition is beyond sufficient. |
| 4087 | else |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4088 | // Check a few special cases. |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4089 | switch (Cond) { |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4090 | case ICmpInst::ICMP_UGT: |
| 4091 | if (Pred == ICmpInst::ICMP_ULT) { |
| 4092 | std::swap(PreCondLHS, PreCondRHS); |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4093 | Cond = ICmpInst::ICMP_ULT; |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4094 | break; |
| 4095 | } |
| 4096 | return false; |
| 4097 | case ICmpInst::ICMP_SGT: |
| 4098 | if (Pred == ICmpInst::ICMP_SLT) { |
| 4099 | std::swap(PreCondLHS, PreCondRHS); |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4100 | Cond = ICmpInst::ICMP_SLT; |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4101 | break; |
| 4102 | } |
| 4103 | return false; |
| 4104 | case ICmpInst::ICMP_NE: |
| 4105 | // Expressions like (x >u 0) are often canonicalized to (x != 0), |
| 4106 | // so check for this case by checking if the NE is comparing against |
| 4107 | // a minimum or maximum constant. |
| 4108 | if (!ICmpInst::isTrueWhenEqual(Pred)) |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4109 | if (ConstantInt *CI = dyn_cast<ConstantInt>(PreCondRHS)) { |
| 4110 | const APInt &A = CI->getValue(); |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4111 | switch (Pred) { |
| 4112 | case ICmpInst::ICMP_SLT: |
| 4113 | if (A.isMaxSignedValue()) break; |
| 4114 | return false; |
| 4115 | case ICmpInst::ICMP_SGT: |
| 4116 | if (A.isMinSignedValue()) break; |
| 4117 | return false; |
| 4118 | case ICmpInst::ICMP_ULT: |
| 4119 | if (A.isMaxValue()) break; |
| 4120 | return false; |
| 4121 | case ICmpInst::ICMP_UGT: |
| 4122 | if (A.isMinValue()) break; |
| 4123 | return false; |
| 4124 | default: |
| 4125 | return false; |
| 4126 | } |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4127 | Cond = ICmpInst::ICMP_NE; |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4128 | // NE is symmetric but the original comparison may not be. Swap |
| 4129 | // the operands if necessary so that they match below. |
| 4130 | if (isa<SCEVConstant>(LHS)) |
| 4131 | std::swap(PreCondLHS, PreCondRHS); |
| 4132 | break; |
| 4133 | } |
| 4134 | return false; |
| 4135 | default: |
| 4136 | // We weren't able to reconcile the condition. |
| 4137 | return false; |
| 4138 | } |
| 4139 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4140 | if (!PreCondLHS->getType()->isInteger()) return false; |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4141 | |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4142 | const SCEV *PreCondLHSSCEV = getSCEV(PreCondLHS); |
| 4143 | const SCEV *PreCondRHSSCEV = getSCEV(PreCondRHS); |
| 4144 | return (HasSameValue(LHS, PreCondLHSSCEV) && |
| 4145 | HasSameValue(RHS, PreCondRHSSCEV)) || |
| 4146 | (HasSameValue(LHS, getNotSCEV(PreCondRHSSCEV)) && |
| 4147 | HasSameValue(RHS, getNotSCEV(PreCondLHSSCEV))); |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4148 | } |
| 4149 | |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4150 | /// getBECount - Subtract the end and start values and divide by the step, |
| 4151 | /// rounding up, to get the number of times the backedge is executed. Return |
| 4152 | /// CouldNotCompute if an intermediate computation overflows. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4153 | const SCEV *ScalarEvolution::getBECount(const SCEV *Start, |
| 4154 | const SCEV *End, |
| 4155 | const SCEV *Step) { |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4156 | const Type *Ty = Start->getType(); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4157 | const SCEV *NegOne = getIntegerSCEV(-1, Ty); |
| 4158 | const SCEV *Diff = getMinusSCEV(End, Start); |
| 4159 | const SCEV *RoundUp = getAddExpr(Step, NegOne); |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4160 | |
| 4161 | // Add an adjustment to the difference between End and Start so that |
| 4162 | // the division will effectively round up. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4163 | const SCEV *Add = getAddExpr(Diff, RoundUp); |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4164 | |
| 4165 | // Check Add for unsigned overflow. |
| 4166 | // TODO: More sophisticated things could be done here. |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4167 | const Type *WideTy = Context->getIntegerType(getTypeSizeInBits(Ty) + 1); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4168 | const SCEV *OperandExtendedAdd = |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4169 | getAddExpr(getZeroExtendExpr(Diff, WideTy), |
| 4170 | getZeroExtendExpr(RoundUp, WideTy)); |
| 4171 | if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4172 | return getCouldNotCompute(); |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4173 | |
| 4174 | return getUDivExpr(Add, Step); |
| 4175 | } |
| 4176 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4177 | /// HowManyLessThans - Return the number of times a backedge containing the |
| 4178 | /// specified less-than comparison will execute. If not computable, return |
Dan Gohman | 0c85091 | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4179 | /// CouldNotCompute. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4180 | ScalarEvolution::BackedgeTakenInfo |
| 4181 | ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS, |
| 4182 | const Loop *L, bool isSigned) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4183 | // Only handle: "ADDREC < LoopInvariant". |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4184 | if (!RHS->isLoopInvariant(L)) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4185 | |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4186 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4187 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4188 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4189 | |
| 4190 | if (AddRec->isAffine()) { |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 4191 | // FORNOW: We only support unit strides. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4192 | unsigned BitWidth = getTypeSizeInBits(AddRec->getType()); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4193 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4194 | |
| 4195 | // TODO: handle non-constant strides. |
| 4196 | const SCEVConstant *CStep = dyn_cast<SCEVConstant>(Step); |
| 4197 | if (!CStep || CStep->isZero()) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4198 | return getCouldNotCompute(); |
Dan Gohman | f8bc8e8 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 4199 | if (CStep->isOne()) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4200 | // With unit stride, the iteration never steps past the limit value. |
| 4201 | } else if (CStep->getValue()->getValue().isStrictlyPositive()) { |
| 4202 | if (const SCEVConstant *CLimit = dyn_cast<SCEVConstant>(RHS)) { |
| 4203 | // Test whether a positive iteration iteration can step past the limit |
| 4204 | // value and past the maximum value for its type in a single step. |
| 4205 | if (isSigned) { |
| 4206 | APInt Max = APInt::getSignedMaxValue(BitWidth); |
| 4207 | if ((Max - CStep->getValue()->getValue()) |
| 4208 | .slt(CLimit->getValue()->getValue())) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4209 | return getCouldNotCompute(); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4210 | } else { |
| 4211 | APInt Max = APInt::getMaxValue(BitWidth); |
| 4212 | if ((Max - CStep->getValue()->getValue()) |
| 4213 | .ult(CLimit->getValue()->getValue())) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4214 | return getCouldNotCompute(); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4215 | } |
| 4216 | } else |
| 4217 | // TODO: handle non-constant limit values below. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4218 | return getCouldNotCompute(); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4219 | } else |
| 4220 | // TODO: handle negative strides below. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4221 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4222 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4223 | // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant |
| 4224 | // m. So, we count the number of iterations in which {n,+,s} < m is true. |
| 4225 | // 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] | 4226 | // treat m-n as signed nor unsigned due to overflow possibility. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4227 | |
Wojciech Matyjewicz | ebc77b1 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4228 | // First, we get the value of the LHS in the first iteration: n |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4229 | const SCEV *Start = AddRec->getOperand(0); |
Wojciech Matyjewicz | ebc77b1 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4230 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4231 | // Determine the minimum constant start value. |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4232 | const SCEV *MinStart = isa<SCEVConstant>(Start) ? Start : |
| 4233 | getConstant(isSigned ? APInt::getSignedMinValue(BitWidth) : |
| 4234 | APInt::getMinValue(BitWidth)); |
Wojciech Matyjewicz | ebc77b1 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4235 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4236 | // If we know that the condition is true in order to enter the loop, |
| 4237 | // 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] | 4238 | // only know that it will execute (max(m,n)-n)/s times. In both cases, |
| 4239 | // the division must round up. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4240 | const SCEV *End = RHS; |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4241 | if (!isLoopGuardedByCond(L, |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4242 | isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4243 | getMinusSCEV(Start, Step), RHS)) |
| 4244 | End = isSigned ? getSMaxExpr(RHS, Start) |
| 4245 | : getUMaxExpr(RHS, Start); |
| 4246 | |
| 4247 | // Determine the maximum constant end value. |
Nick Lewycky | 9425be9 | 2009-07-11 20:38:25 +0000 | [diff] [blame] | 4248 | const SCEV *MaxEnd = |
| 4249 | isa<SCEVConstant>(End) ? End : |
| 4250 | getConstant(isSigned ? APInt::getSignedMaxValue(BitWidth) |
| 4251 | .ashr(GetMinSignBits(End) - 1) : |
| 4252 | APInt::getMaxValue(BitWidth) |
| 4253 | .lshr(GetMinLeadingZeros(End))); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4254 | |
| 4255 | // Finally, we subtract these two values and divide, rounding up, to get |
| 4256 | // the number of times the backedge is executed. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4257 | const SCEV *BECount = getBECount(Start, End, Step); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4258 | |
| 4259 | // The maximum backedge count is similar, except using the minimum start |
| 4260 | // value and the maximum end value. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4261 | const SCEV *MaxBECount = getBECount(MinStart, MaxEnd, Step); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4262 | |
| 4263 | return BackedgeTakenInfo(BECount, MaxBECount); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4266 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | /// getNumIterationsInRange - Return the number of iterations of this loop that |
| 4270 | /// produce values in the specified constant range. Another way of looking at |
| 4271 | /// this is that it returns the first iteration number where the value is not in |
| 4272 | /// the condition, thus computing the exit count. If the iteration count can't |
| 4273 | /// be computed, an instance of SCEVCouldNotCompute is returned. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4274 | const SCEV *SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4275 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4276 | if (Range.isFullSet()) // Infinite loop. |
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 | // 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] | 4280 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4281 | if (!SC->getValue()->isZero()) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4282 | SmallVector<const SCEV *, 4> Operands(op_begin(), op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4283 | Operands[0] = SE.getIntegerSCEV(0, SC->getType()); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4284 | const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4285 | if (const SCEVAddRecExpr *ShiftedAddRec = |
| 4286 | dyn_cast<SCEVAddRecExpr>(Shifted)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4287 | return ShiftedAddRec->getNumIterationsInRange( |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4288 | Range.subtract(SC->getValue()->getValue()), SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4289 | // This is strange and shouldn't happen. |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4290 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4291 | } |
| 4292 | |
| 4293 | // The only time we can solve this is when we have all constant indices. |
| 4294 | // Otherwise, we cannot determine the overflow conditions. |
| 4295 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 4296 | if (!isa<SCEVConstant>(getOperand(i))) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4297 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4298 | |
| 4299 | |
| 4300 | // Okay at this point we know that all elements of the chrec are constants and |
| 4301 | // that the start element is zero. |
| 4302 | |
| 4303 | // First check to see if the range contains zero. If not, the first |
| 4304 | // iteration exits. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 4305 | unsigned BitWidth = SE.getTypeSizeInBits(getType()); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4306 | if (!Range.contains(APInt(BitWidth, 0))) |
Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 4307 | return SE.getIntegerSCEV(0, getType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4308 | |
| 4309 | if (isAffine()) { |
| 4310 | // If this is an affine expression then we have this situation: |
| 4311 | // Solve {0,+,A} in Range === Ax in Range |
| 4312 | |
| 4313 | // We know that zero is in the range. If A is positive then we know that |
| 4314 | // the upper value of the range must be the first possible exit value. |
| 4315 | // If A is negative then the lower of the range is the last possible loop |
| 4316 | // value. Also note that we already checked for a full range. |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4317 | APInt One(BitWidth,1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4318 | APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue(); |
| 4319 | APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower(); |
| 4320 | |
| 4321 | // The exit value should be (End+A)/A. |
Nick Lewycky | a0facae | 2007-09-27 14:12:54 +0000 | [diff] [blame] | 4322 | APInt ExitVal = (End + A).udiv(A); |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4323 | ConstantInt *ExitValue = SE.getContext()->getConstantInt(ExitVal); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4324 | |
| 4325 | // Evaluate at the exit value. If we really did fall out of the valid |
| 4326 | // range, then we computed our trip count, otherwise wrap around or other |
| 4327 | // things must have happened. |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4328 | ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4329 | if (Range.contains(Val->getValue())) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4330 | return SE.getCouldNotCompute(); // Something strange happened |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4331 | |
| 4332 | // Ensure that the previous value is in the range. This is a sanity check. |
| 4333 | assert(Range.contains( |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4334 | EvaluateConstantChrecAtConstant(this, |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4335 | SE.getContext()->getConstantInt(ExitVal - One), SE)->getValue()) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4336 | "Linear scev computation is off in a bad way!"); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4337 | return SE.getConstant(ExitValue); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4338 | } else if (isQuadratic()) { |
| 4339 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the |
| 4340 | // quadratic equation to solve it. To do this, we must frame our problem in |
| 4341 | // terms of figuring out when zero is crossed, instead of when |
| 4342 | // Range.getUpper() is crossed. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4343 | SmallVector<const SCEV *, 4> NewOps(op_begin(), op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4344 | NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper())); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4345 | const SCEV *NewAddRec = SE.getAddRecExpr(NewOps, getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4346 | |
| 4347 | // Next, solve the constructed addrec |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4348 | std::pair<const SCEV *,const SCEV *> Roots = |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4349 | SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4350 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 4351 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4352 | if (R1) { |
| 4353 | // Pick the smallest positive root value. |
| 4354 | if (ConstantInt *CB = |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4355 | dyn_cast<ConstantInt>( |
| 4356 | SE.getContext()->getConstantExprICmp(ICmpInst::ICMP_ULT, |
| 4357 | R1->getValue(), R2->getValue()))) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4358 | if (CB->getZExtValue() == false) |
| 4359 | std::swap(R1, R2); // R1 is the minimum root now. |
| 4360 | |
| 4361 | // Make sure the root is not off by one. The returned iteration should |
| 4362 | // not be in the range, but the previous one should be. When solving |
| 4363 | // for "X*X < 5", for example, we should not return a root of 2. |
| 4364 | ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this, |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4365 | R1->getValue(), |
| 4366 | SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4367 | if (Range.contains(R1Val->getValue())) { |
| 4368 | // The next iteration must be out of the range... |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4369 | ConstantInt *NextVal = |
| 4370 | SE.getContext()->getConstantInt(R1->getValue()->getValue()+1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4371 | |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4372 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4373 | if (!Range.contains(R1Val->getValue())) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4374 | return SE.getConstant(NextVal); |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4375 | return SE.getCouldNotCompute(); // Something strange happened |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4376 | } |
| 4377 | |
| 4378 | // If R1 was not in the range, then it is a good return value. Make |
| 4379 | // sure that R1-1 WAS in the range though, just in case. |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4380 | ConstantInt *NextVal = |
| 4381 | SE.getContext()->getConstantInt(R1->getValue()->getValue()-1); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4382 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4383 | if (Range.contains(R1Val->getValue())) |
| 4384 | return R1; |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4385 | return SE.getCouldNotCompute(); // Something strange happened |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4386 | } |
| 4387 | } |
| 4388 | } |
| 4389 | |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4390 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
| 4393 | |
| 4394 | |
| 4395 | //===----------------------------------------------------------------------===// |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4396 | // SCEVCallbackVH Class Implementation |
| 4397 | //===----------------------------------------------------------------------===// |
| 4398 | |
Dan Gohman | 999d14e | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 4399 | void ScalarEvolution::SCEVCallbackVH::deleted() { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4400 | assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!"); |
| 4401 | if (PHINode *PN = dyn_cast<PHINode>(getValPtr())) |
| 4402 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 4403 | if (Instruction *I = dyn_cast<Instruction>(getValPtr())) |
| 4404 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4405 | SE->Scalars.erase(getValPtr()); |
| 4406 | // this now dangles! |
| 4407 | } |
| 4408 | |
Dan Gohman | 999d14e | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 4409 | void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4410 | assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!"); |
| 4411 | |
| 4412 | // Forget all the expressions associated with users of the old value, |
| 4413 | // so that future queries will recompute the expressions using the new |
| 4414 | // value. |
| 4415 | SmallVector<User *, 16> Worklist; |
| 4416 | Value *Old = getValPtr(); |
| 4417 | bool DeleteOld = false; |
| 4418 | for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end(); |
| 4419 | UI != UE; ++UI) |
| 4420 | Worklist.push_back(*UI); |
| 4421 | while (!Worklist.empty()) { |
| 4422 | User *U = Worklist.pop_back_val(); |
| 4423 | // Deleting the Old value will cause this to dangle. Postpone |
| 4424 | // that until everything else is done. |
| 4425 | if (U == Old) { |
| 4426 | DeleteOld = true; |
| 4427 | continue; |
| 4428 | } |
| 4429 | if (PHINode *PN = dyn_cast<PHINode>(U)) |
| 4430 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 4431 | if (Instruction *I = dyn_cast<Instruction>(U)) |
| 4432 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4433 | if (SE->Scalars.erase(U)) |
| 4434 | for (Value::use_iterator UI = U->use_begin(), UE = U->use_end(); |
| 4435 | UI != UE; ++UI) |
| 4436 | Worklist.push_back(*UI); |
| 4437 | } |
| 4438 | if (DeleteOld) { |
| 4439 | if (PHINode *PN = dyn_cast<PHINode>(Old)) |
| 4440 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 4441 | if (Instruction *I = dyn_cast<Instruction>(Old)) |
| 4442 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4443 | SE->Scalars.erase(Old); |
| 4444 | // this now dangles! |
| 4445 | } |
| 4446 | // this may dangle! |
| 4447 | } |
| 4448 | |
Dan Gohman | 999d14e | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 4449 | ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4450 | : CallbackVH(V), SE(se) {} |
| 4451 | |
| 4452 | //===----------------------------------------------------------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4453 | // ScalarEvolution Class Implementation |
| 4454 | //===----------------------------------------------------------------------===// |
| 4455 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4456 | ScalarEvolution::ScalarEvolution() |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4457 | : FunctionPass(&ID) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4458 | } |
| 4459 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4460 | bool ScalarEvolution::runOnFunction(Function &F) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4461 | this->F = &F; |
| 4462 | LI = &getAnalysis<LoopInfo>(); |
| 4463 | TD = getAnalysisIfAvailable<TargetData>(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4464 | return false; |
| 4465 | } |
| 4466 | |
| 4467 | void ScalarEvolution::releaseMemory() { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4468 | Scalars.clear(); |
| 4469 | BackedgeTakenCounts.clear(); |
| 4470 | ConstantEvolutionLoopExitValue.clear(); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 4471 | ValuesAtScopes.clear(); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4472 | UniqueSCEVs.clear(); |
| 4473 | SCEVAllocator.Reset(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
| 4476 | void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const { |
| 4477 | AU.setPreservesAll(); |
| 4478 | AU.addRequiredTransitive<LoopInfo>(); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4481 | bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4482 | return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4483 | } |
| 4484 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4485 | static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4486 | const Loop *L) { |
| 4487 | // Print all inner loops first |
| 4488 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 4489 | PrintLoopInfo(OS, SE, *I); |
| 4490 | |
Nick Lewycky | e5da191 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 4491 | OS << "Loop " << L->getHeader()->getName() << ": "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4492 | |
Devang Patel | 02451fa | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 4493 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4494 | L->getExitBlocks(ExitBlocks); |
| 4495 | if (ExitBlocks.size() != 1) |
Nick Lewycky | e5da191 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 4496 | OS << "<multiple exits> "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4497 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4498 | if (SE->hasLoopInvariantBackedgeTakenCount(L)) { |
| 4499 | OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4500 | } else { |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4501 | OS << "Unpredictable backedge-taken count. "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4502 | } |
| 4503 | |
Nick Lewycky | e5da191 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 4504 | OS << "\n"; |
Dan Gohman | b6b9e9e | 2009-06-24 00:33:16 +0000 | [diff] [blame] | 4505 | OS << "Loop " << L->getHeader()->getName() << ": "; |
| 4506 | |
| 4507 | if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) { |
| 4508 | OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L); |
| 4509 | } else { |
| 4510 | OS << "Unpredictable max backedge-taken count. "; |
| 4511 | } |
| 4512 | |
| 4513 | OS << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4514 | } |
| 4515 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 4516 | void ScalarEvolution::print(raw_ostream &OS, const Module* ) const { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4517 | // ScalarEvolution's implementaiton of the print method is to print |
| 4518 | // out SCEV values of all instructions that are interesting. Doing |
| 4519 | // this potentially causes it to create new SCEV objects though, |
| 4520 | // which technically conflicts with the const qualifier. This isn't |
Dan Gohman | ac2a9d6 | 2009-07-10 20:25:29 +0000 | [diff] [blame] | 4521 | // observable from outside the class though, so casting away the |
| 4522 | // const isn't dangerous. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4523 | ScalarEvolution &SE = *const_cast<ScalarEvolution*>(this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4524 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4525 | OS << "Classifying expressions for: " << F->getName() << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4526 | 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] | 4527 | if (isSCEVable(I->getType())) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4528 | OS << *I; |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 4529 | OS << " --> "; |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4530 | const SCEV *SV = SE.getSCEV(&*I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4531 | SV->print(OS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4532 | |
Dan Gohman | 8db598a | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 4533 | const Loop *L = LI->getLoopFor((*I).getParent()); |
| 4534 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4535 | const SCEV *AtUse = SE.getSCEVAtScope(SV, L); |
Dan Gohman | 8db598a | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 4536 | if (AtUse != SV) { |
| 4537 | OS << " --> "; |
| 4538 | AtUse->print(OS); |
| 4539 | } |
| 4540 | |
| 4541 | if (L) { |
Dan Gohman | e5b6084 | 2009-06-18 00:37:45 +0000 | [diff] [blame] | 4542 | OS << "\t\t" "Exits: "; |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4543 | const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop()); |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 4544 | if (!ExitValue->isLoopInvariant(L)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4545 | OS << "<<Unknown>>"; |
| 4546 | } else { |
| 4547 | OS << *ExitValue; |
| 4548 | } |
| 4549 | } |
| 4550 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4551 | OS << "\n"; |
| 4552 | } |
| 4553 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4554 | OS << "Determining loop execution counts for: " << F->getName() << "\n"; |
| 4555 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
| 4556 | PrintLoopInfo(OS, &SE, *I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4557 | } |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 4558 | |
| 4559 | void ScalarEvolution::print(std::ostream &o, const Module *M) const { |
| 4560 | raw_os_ostream OS(o); |
| 4561 | print(OS, M); |
| 4562 | } |