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 | 9545fb0 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 69 | #include "llvm/Operator.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 70 | #include "llvm/Analysis/ConstantFolding.h" |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 71 | #include "llvm/Analysis/Dominators.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 72 | #include "llvm/Analysis/LoopInfo.h" |
Dan Gohman | a7726c3 | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 73 | #include "llvm/Analysis/ValueTracking.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 74 | #include "llvm/Assembly/Writer.h" |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 75 | #include "llvm/Target/TargetData.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 76 | #include "llvm/Support/CommandLine.h" |
| 77 | #include "llvm/Support/Compiler.h" |
| 78 | #include "llvm/Support/ConstantRange.h" |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 79 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 80 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 81 | #include "llvm/Support/InstIterator.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 82 | #include "llvm/Support/MathExtras.h" |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 83 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 84 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 85 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 86 | #include "llvm/ADT/SmallPtrSet.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 87 | #include <algorithm> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 88 | using namespace llvm; |
| 89 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 90 | STATISTIC(NumArrayLenItCounts, |
| 91 | "Number of trip counts computed with array length"); |
| 92 | STATISTIC(NumTripCountsComputed, |
| 93 | "Number of loops with predictable loop counts"); |
| 94 | STATISTIC(NumTripCountsNotComputed, |
| 95 | "Number of loops without predictable loop counts"); |
| 96 | STATISTIC(NumBruteForceTripCountsComputed, |
| 97 | "Number of loops with trip counts computed by force"); |
| 98 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 99 | static cl::opt<unsigned> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 100 | MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, |
| 101 | cl::desc("Maximum number of iterations SCEV will " |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 102 | "symbolically execute a constant " |
| 103 | "derived loop"), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 104 | cl::init(100)); |
| 105 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 106 | static RegisterPass<ScalarEvolution> |
| 107 | R("scalar-evolution", "Scalar Evolution Analysis", false, true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 108 | char ScalarEvolution::ID = 0; |
| 109 | |
| 110 | //===----------------------------------------------------------------------===// |
| 111 | // SCEV class definitions |
| 112 | //===----------------------------------------------------------------------===// |
| 113 | |
| 114 | //===----------------------------------------------------------------------===// |
| 115 | // Implementation of the SCEV class. |
| 116 | // |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 117 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 118 | SCEV::~SCEV() {} |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 119 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 120 | void SCEV::dump() const { |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 121 | print(errs()); |
| 122 | errs() << '\n'; |
| 123 | } |
| 124 | |
| 125 | void SCEV::print(std::ostream &o) const { |
| 126 | raw_os_ostream OS(o); |
| 127 | print(OS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 130 | bool SCEV::isZero() const { |
| 131 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 132 | return SC->getValue()->isZero(); |
| 133 | return false; |
| 134 | } |
| 135 | |
Dan Gohman | f8bc8e8 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 136 | bool SCEV::isOne() const { |
| 137 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 138 | return SC->getValue()->isOne(); |
| 139 | return false; |
| 140 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 141 | |
Dan Gohman | f05118e | 2009-06-24 00:30:26 +0000 | [diff] [blame] | 142 | bool SCEV::isAllOnesValue() const { |
| 143 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 144 | return SC->getValue()->isAllOnesValue(); |
| 145 | return false; |
| 146 | } |
| 147 | |
Owen Anderson | b70139d | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 148 | SCEVCouldNotCompute::SCEVCouldNotCompute() : |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 149 | SCEV(FoldingSetNodeID(), scCouldNotCompute) {} |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 150 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 151 | bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 152 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 153 | return false; |
| 154 | } |
| 155 | |
| 156 | const Type *SCEVCouldNotCompute::getType() const { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 157 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 162 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 163 | return false; |
| 164 | } |
| 165 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 166 | const SCEV * |
| 167 | SCEVCouldNotCompute::replaceSymbolicValuesWithConcrete( |
| 168 | const SCEV *Sym, |
| 169 | const SCEV *Conc, |
| 170 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 171 | return this; |
| 172 | } |
| 173 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 174 | void SCEVCouldNotCompute::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 175 | OS << "***COULDNOTCOMPUTE***"; |
| 176 | } |
| 177 | |
| 178 | bool SCEVCouldNotCompute::classof(const SCEV *S) { |
| 179 | return S->getSCEVType() == scCouldNotCompute; |
| 180 | } |
| 181 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 182 | const SCEV *ScalarEvolution::getConstant(ConstantInt *V) { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 183 | FoldingSetNodeID ID; |
| 184 | ID.AddInteger(scConstant); |
| 185 | ID.AddPointer(V); |
| 186 | void *IP = 0; |
| 187 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 188 | SCEV *S = SCEVAllocator.Allocate<SCEVConstant>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 189 | new (S) SCEVConstant(ID, V); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 190 | UniqueSCEVs.InsertNode(S, IP); |
| 191 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 194 | const SCEV *ScalarEvolution::getConstant(const APInt& Val) { |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame^] | 195 | return getConstant(ConstantInt::get(getContext(), Val)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 198 | const SCEV * |
Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 199 | ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) { |
Owen Anderson | 9f5b2aa | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 200 | return getConstant( |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame^] | 201 | ConstantInt::get(cast<IntegerType>(Ty), V, isSigned)); |
Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 204 | const Type *SCEVConstant::getType() const { return V->getType(); } |
| 205 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 206 | void SCEVConstant::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 207 | WriteAsOperand(OS, V, false); |
| 208 | } |
| 209 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 210 | SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeID &ID, |
| 211 | unsigned SCEVTy, const SCEV *op, const Type *ty) |
| 212 | : SCEV(ID, SCEVTy), Op(op), Ty(ty) {} |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 213 | |
Dan Gohman | 2a38153 | 2009-04-21 01:25:57 +0000 | [diff] [blame] | 214 | bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 215 | return Op->dominates(BB, DT); |
| 216 | } |
| 217 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 218 | SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeID &ID, |
| 219 | const SCEV *op, const Type *ty) |
| 220 | : SCEVCastExpr(ID, scTruncate, op, ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 221 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 222 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 223 | "Cannot truncate non-integer value!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 226 | void SCEVTruncateExpr::print(raw_ostream &OS) const { |
Dan Gohman | c911922 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 227 | OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 230 | SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeID &ID, |
| 231 | const SCEV *op, const Type *ty) |
| 232 | : SCEVCastExpr(ID, scZeroExtend, op, ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 233 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 234 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 235 | "Cannot zero extend non-integer value!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 238 | void SCEVZeroExtendExpr::print(raw_ostream &OS) const { |
Dan Gohman | c911922 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 239 | OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 242 | SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeID &ID, |
| 243 | const SCEV *op, const Type *ty) |
| 244 | : SCEVCastExpr(ID, scSignExtend, op, ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 245 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 246 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 247 | "Cannot sign extend non-integer value!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 250 | void SCEVSignExtendExpr::print(raw_ostream &OS) const { |
Dan Gohman | c911922 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 251 | OS << "(sext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 254 | void SCEVCommutativeExpr::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 255 | assert(Operands.size() > 1 && "This plus expr shouldn't exist!"); |
| 256 | const char *OpStr = getOperationStr(); |
| 257 | OS << "(" << *Operands[0]; |
| 258 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 259 | OS << OpStr << *Operands[i]; |
| 260 | OS << ")"; |
| 261 | } |
| 262 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 263 | const SCEV * |
| 264 | SCEVCommutativeExpr::replaceSymbolicValuesWithConcrete( |
| 265 | const SCEV *Sym, |
| 266 | const SCEV *Conc, |
| 267 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 268 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 269 | const SCEV *H = |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 270 | getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 271 | if (H != getOperand(i)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 272 | SmallVector<const SCEV *, 8> NewOps; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 273 | NewOps.reserve(getNumOperands()); |
| 274 | for (unsigned j = 0; j != i; ++j) |
| 275 | NewOps.push_back(getOperand(j)); |
| 276 | NewOps.push_back(H); |
| 277 | for (++i; i != e; ++i) |
| 278 | NewOps.push_back(getOperand(i)-> |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 279 | replaceSymbolicValuesWithConcrete(Sym, Conc, SE)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 280 | |
| 281 | if (isa<SCEVAddExpr>(this)) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 282 | return SE.getAddExpr(NewOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 283 | else if (isa<SCEVMulExpr>(this)) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 284 | return SE.getMulExpr(NewOps); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 285 | else if (isa<SCEVSMaxExpr>(this)) |
| 286 | return SE.getSMaxExpr(NewOps); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 287 | else if (isa<SCEVUMaxExpr>(this)) |
| 288 | return SE.getUMaxExpr(NewOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 289 | else |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 290 | llvm_unreachable("Unknown commutative expr!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | return this; |
| 294 | } |
| 295 | |
Dan Gohman | 72a8a02 | 2009-05-07 14:00:19 +0000 | [diff] [blame] | 296 | bool SCEVNAryExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 297 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 298 | if (!getOperand(i)->dominates(BB, DT)) |
| 299 | return false; |
| 300 | } |
| 301 | return true; |
| 302 | } |
| 303 | |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 304 | bool SCEVUDivExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 305 | return LHS->dominates(BB, DT) && RHS->dominates(BB, DT); |
| 306 | } |
| 307 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 308 | void SCEVUDivExpr::print(raw_ostream &OS) const { |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 309 | OS << "(" << *LHS << " /u " << *RHS << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 312 | const Type *SCEVUDivExpr::getType() const { |
Dan Gohman | 140f08f | 2009-05-26 17:44:05 +0000 | [diff] [blame] | 313 | // In most cases the types of LHS and RHS will be the same, but in some |
| 314 | // crazy cases one or the other may be a pointer. ScalarEvolution doesn't |
| 315 | // depend on the type for correctness, but handling types carefully can |
| 316 | // avoid extra casts in the SCEVExpander. The LHS is more likely to be |
| 317 | // a pointer type than the RHS, so use the RHS' type here. |
| 318 | return RHS->getType(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 321 | const SCEV * |
| 322 | SCEVAddRecExpr::replaceSymbolicValuesWithConcrete(const SCEV *Sym, |
| 323 | const SCEV *Conc, |
| 324 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 325 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 326 | const SCEV *H = |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 327 | getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 328 | if (H != getOperand(i)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 329 | SmallVector<const SCEV *, 8> NewOps; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 330 | NewOps.reserve(getNumOperands()); |
| 331 | for (unsigned j = 0; j != i; ++j) |
| 332 | NewOps.push_back(getOperand(j)); |
| 333 | NewOps.push_back(H); |
| 334 | for (++i; i != e; ++i) |
| 335 | NewOps.push_back(getOperand(i)-> |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 336 | replaceSymbolicValuesWithConcrete(Sym, Conc, SE)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 337 | |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 338 | return SE.getAddRecExpr(NewOps, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | return this; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const { |
Dan Gohman | ae1eaae | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 346 | // Add recurrences are never invariant in the function-body (null loop). |
Dan Gohman | 2d888d8 | 2009-06-26 22:17:21 +0000 | [diff] [blame] | 347 | if (!QueryLoop) |
| 348 | return false; |
| 349 | |
| 350 | // This recurrence is variant w.r.t. QueryLoop if QueryLoop contains L. |
| 351 | if (QueryLoop->contains(L->getHeader())) |
| 352 | return false; |
| 353 | |
| 354 | // This recurrence is variant w.r.t. QueryLoop if any of its operands |
| 355 | // are variant. |
| 356 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 357 | if (!getOperand(i)->isLoopInvariant(QueryLoop)) |
| 358 | return false; |
| 359 | |
| 360 | // Otherwise it's loop-invariant. |
| 361 | return true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 364 | void SCEVAddRecExpr::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 365 | OS << "{" << *Operands[0]; |
| 366 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 367 | OS << ",+," << *Operands[i]; |
| 368 | OS << "}<" << L->getHeader()->getName() + ">"; |
| 369 | } |
| 370 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 371 | bool SCEVUnknown::isLoopInvariant(const Loop *L) const { |
| 372 | // All non-instruction values are loop invariant. All instructions are loop |
| 373 | // invariant if they are not contained in the specified loop. |
Dan Gohman | ae1eaae | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 374 | // Instructions are never considered invariant in the function body |
| 375 | // (null loop) because they are defined within the "loop". |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 376 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Dan Gohman | ae1eaae | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 377 | return L && !L->contains(I->getParent()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 378 | return true; |
| 379 | } |
| 380 | |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 381 | bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 382 | if (Instruction *I = dyn_cast<Instruction>(getValue())) |
| 383 | return DT->dominates(I->getParent(), BB); |
| 384 | return true; |
| 385 | } |
| 386 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 387 | const Type *SCEVUnknown::getType() const { |
| 388 | return V->getType(); |
| 389 | } |
| 390 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 391 | void SCEVUnknown::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 392 | WriteAsOperand(OS, V, false); |
| 393 | } |
| 394 | |
| 395 | //===----------------------------------------------------------------------===// |
| 396 | // SCEV Utilities |
| 397 | //===----------------------------------------------------------------------===// |
| 398 | |
| 399 | namespace { |
| 400 | /// SCEVComplexityCompare - Return true if the complexity of the LHS is less |
| 401 | /// than the complexity of the RHS. This comparator is used to canonicalize |
| 402 | /// expressions. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 403 | class VISIBILITY_HIDDEN SCEVComplexityCompare { |
| 404 | LoopInfo *LI; |
| 405 | public: |
| 406 | explicit SCEVComplexityCompare(LoopInfo *li) : LI(li) {} |
| 407 | |
Dan Gohman | c0c69cf | 2008-04-14 18:23:56 +0000 | [diff] [blame] | 408 | bool operator()(const SCEV *LHS, const SCEV *RHS) const { |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 409 | // Primarily, sort the SCEVs by their getSCEVType(). |
| 410 | if (LHS->getSCEVType() != RHS->getSCEVType()) |
| 411 | return LHS->getSCEVType() < RHS->getSCEVType(); |
| 412 | |
| 413 | // Aside from the getSCEVType() ordering, the particular ordering |
| 414 | // isn't very important except that it's beneficial to be consistent, |
| 415 | // so that (a + b) and (b + a) don't end up as different expressions. |
| 416 | |
| 417 | // Sort SCEVUnknown values with some loose heuristics. TODO: This is |
| 418 | // not as complete as it could be. |
| 419 | if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) { |
| 420 | const SCEVUnknown *RU = cast<SCEVUnknown>(RHS); |
| 421 | |
Dan Gohman | d0c0123 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 422 | // Order pointer values after integer values. This helps SCEVExpander |
| 423 | // form GEPs. |
| 424 | if (isa<PointerType>(LU->getType()) && !isa<PointerType>(RU->getType())) |
| 425 | return false; |
| 426 | if (isa<PointerType>(RU->getType()) && !isa<PointerType>(LU->getType())) |
| 427 | return true; |
| 428 | |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 429 | // Compare getValueID values. |
| 430 | if (LU->getValue()->getValueID() != RU->getValue()->getValueID()) |
| 431 | return LU->getValue()->getValueID() < RU->getValue()->getValueID(); |
| 432 | |
| 433 | // Sort arguments by their position. |
| 434 | if (const Argument *LA = dyn_cast<Argument>(LU->getValue())) { |
| 435 | const Argument *RA = cast<Argument>(RU->getValue()); |
| 436 | return LA->getArgNo() < RA->getArgNo(); |
| 437 | } |
| 438 | |
| 439 | // For instructions, compare their loop depth, and their opcode. |
| 440 | // This is pretty loose. |
| 441 | if (Instruction *LV = dyn_cast<Instruction>(LU->getValue())) { |
| 442 | Instruction *RV = cast<Instruction>(RU->getValue()); |
| 443 | |
| 444 | // Compare loop depths. |
| 445 | if (LI->getLoopDepth(LV->getParent()) != |
| 446 | LI->getLoopDepth(RV->getParent())) |
| 447 | return LI->getLoopDepth(LV->getParent()) < |
| 448 | LI->getLoopDepth(RV->getParent()); |
| 449 | |
| 450 | // Compare opcodes. |
| 451 | if (LV->getOpcode() != RV->getOpcode()) |
| 452 | return LV->getOpcode() < RV->getOpcode(); |
| 453 | |
| 454 | // Compare the number of operands. |
| 455 | if (LV->getNumOperands() != RV->getNumOperands()) |
| 456 | return LV->getNumOperands() < RV->getNumOperands(); |
| 457 | } |
| 458 | |
| 459 | return false; |
| 460 | } |
| 461 | |
Dan Gohman | 56fc8f1 | 2009-06-14 22:51:25 +0000 | [diff] [blame] | 462 | // Compare constant values. |
| 463 | if (const SCEVConstant *LC = dyn_cast<SCEVConstant>(LHS)) { |
| 464 | const SCEVConstant *RC = cast<SCEVConstant>(RHS); |
Nick Lewycky | 9bb1405 | 2009-07-04 17:24:52 +0000 | [diff] [blame] | 465 | if (LC->getValue()->getBitWidth() != RC->getValue()->getBitWidth()) |
| 466 | return LC->getValue()->getBitWidth() < RC->getValue()->getBitWidth(); |
Dan Gohman | 56fc8f1 | 2009-06-14 22:51:25 +0000 | [diff] [blame] | 467 | return LC->getValue()->getValue().ult(RC->getValue()->getValue()); |
| 468 | } |
| 469 | |
| 470 | // Compare addrec loop depths. |
| 471 | if (const SCEVAddRecExpr *LA = dyn_cast<SCEVAddRecExpr>(LHS)) { |
| 472 | const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS); |
| 473 | if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth()) |
| 474 | return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth(); |
| 475 | } |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 476 | |
| 477 | // Lexicographically compare n-ary expressions. |
| 478 | if (const SCEVNAryExpr *LC = dyn_cast<SCEVNAryExpr>(LHS)) { |
| 479 | const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS); |
| 480 | for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) { |
| 481 | if (i >= RC->getNumOperands()) |
| 482 | return false; |
| 483 | if (operator()(LC->getOperand(i), RC->getOperand(i))) |
| 484 | return true; |
| 485 | if (operator()(RC->getOperand(i), LC->getOperand(i))) |
| 486 | return false; |
| 487 | } |
| 488 | return LC->getNumOperands() < RC->getNumOperands(); |
| 489 | } |
| 490 | |
Dan Gohman | 6e10db1 | 2009-05-07 19:23:21 +0000 | [diff] [blame] | 491 | // Lexicographically compare udiv expressions. |
| 492 | if (const SCEVUDivExpr *LC = dyn_cast<SCEVUDivExpr>(LHS)) { |
| 493 | const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS); |
| 494 | if (operator()(LC->getLHS(), RC->getLHS())) |
| 495 | return true; |
| 496 | if (operator()(RC->getLHS(), LC->getLHS())) |
| 497 | return false; |
| 498 | if (operator()(LC->getRHS(), RC->getRHS())) |
| 499 | return true; |
| 500 | if (operator()(RC->getRHS(), LC->getRHS())) |
| 501 | return false; |
| 502 | return false; |
| 503 | } |
| 504 | |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 505 | // Compare cast expressions by operand. |
| 506 | if (const SCEVCastExpr *LC = dyn_cast<SCEVCastExpr>(LHS)) { |
| 507 | const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS); |
| 508 | return operator()(LC->getOperand(), RC->getOperand()); |
| 509 | } |
| 510 | |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 511 | llvm_unreachable("Unknown SCEV kind!"); |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 512 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 513 | } |
| 514 | }; |
| 515 | } |
| 516 | |
| 517 | /// GroupByComplexity - Given a list of SCEV objects, order them by their |
| 518 | /// complexity, and group objects of the same complexity together by value. |
| 519 | /// When this routine is finished, we know that any duplicates in the vector are |
| 520 | /// consecutive and that complexity is monotonically increasing. |
| 521 | /// |
| 522 | /// Note that we go take special precautions to ensure that we get determinstic |
| 523 | /// results from this routine. In other words, we don't want the results of |
| 524 | /// this to depend on where the addresses of various SCEV objects happened to |
| 525 | /// land in memory. |
| 526 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 527 | static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops, |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 528 | LoopInfo *LI) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 529 | if (Ops.size() < 2) return; // Noop |
| 530 | if (Ops.size() == 2) { |
| 531 | // This is the common case, which also happens to be trivially simple. |
| 532 | // Special case it. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 533 | if (SCEVComplexityCompare(LI)(Ops[1], Ops[0])) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 534 | std::swap(Ops[0], Ops[1]); |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | // Do the rough sort by complexity. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 539 | std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 540 | |
| 541 | // Now that we are sorted by complexity, group elements of the same |
| 542 | // complexity. Note that this is, at worst, N^2, but the vector is likely to |
| 543 | // be extremely short in practice. Note that we take this approach because we |
| 544 | // do not want to depend on the addresses of the objects we are grouping. |
| 545 | for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 546 | const SCEV *S = Ops[i]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 547 | unsigned Complexity = S->getSCEVType(); |
| 548 | |
| 549 | // If there are any objects of the same complexity and same value as this |
| 550 | // one, group them. |
| 551 | for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { |
| 552 | if (Ops[j] == S) { // Found a duplicate. |
| 553 | // Move it to immediately after i'th element. |
| 554 | std::swap(Ops[i+1], Ops[j]); |
| 555 | ++i; // no need to rescan it. |
| 556 | if (i == e-2) return; // Done! |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | |
| 563 | |
| 564 | //===----------------------------------------------------------------------===// |
| 565 | // Simple SCEV method implementations |
| 566 | //===----------------------------------------------------------------------===// |
| 567 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 568 | /// BinomialCoefficient - Compute BC(It, K). The result has width W. |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 569 | /// Assume, K > 0. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 570 | static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K, |
Dan Gohman | f5606fd | 2009-07-21 00:38:55 +0000 | [diff] [blame] | 571 | ScalarEvolution &SE, |
| 572 | const Type* ResultTy) { |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 573 | // Handle the simplest case efficiently. |
| 574 | if (K == 1) |
| 575 | return SE.getTruncateOrZeroExtend(It, ResultTy); |
| 576 | |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 577 | // We are using the following formula for BC(It, K): |
| 578 | // |
| 579 | // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! |
| 580 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 581 | // Suppose, W is the bitwidth of the return value. We must be prepared for |
| 582 | // overflow. Hence, we must assure that the result of our computation is |
| 583 | // equal to the accurate one modulo 2^W. Unfortunately, division isn't |
| 584 | // safe in modular arithmetic. |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 585 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 586 | // 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] | 587 | // 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] | 588 | // K! (i.e. trailing zeros in the binary representation of K!), and ^ is |
| 589 | // exponentiation: |
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 | // 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] | 592 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 593 | // This formula is trivially equivalent to the previous formula. However, |
| 594 | // this formula can be implemented much more efficiently. The trick is that |
| 595 | // K! / 2^T is odd, and exact division by an odd number *is* safe in modular |
| 596 | // arithmetic. To do exact division in modular arithmetic, all we have |
| 597 | // to do is multiply by the inverse. Therefore, this step can be done at |
| 598 | // width W. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 599 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 600 | // The next issue is how to safely do the division by 2^T. The way this |
| 601 | // is done is by doing the multiplication step at a width of at least W + T |
| 602 | // bits. This way, the bottom W+T bits of the product are accurate. Then, |
| 603 | // when we perform the division by 2^T (which is equivalent to a right shift |
| 604 | // by T), the bottom W bits are accurate. Extra bits are okay; they'll get |
| 605 | // truncated out after the division by 2^T. |
| 606 | // |
| 607 | // In comparison to just directly using the first formula, this technique |
| 608 | // is much more efficient; using the first formula requires W * K bits, |
| 609 | // but this formula less than W + K bits. Also, the first formula requires |
| 610 | // a division step, whereas this formula only requires multiplies and shifts. |
| 611 | // |
| 612 | // It doesn't matter whether the subtraction step is done in the calculation |
| 613 | // width or the input iteration count's width; if the subtraction overflows, |
| 614 | // the result must be zero anyway. We prefer here to do it in the width of |
| 615 | // the induction variable because it helps a lot for certain cases; CodeGen |
| 616 | // isn't smart enough to ignore the overflow, which leads to much less |
| 617 | // efficient code if the width of the subtraction is wider than the native |
| 618 | // register width. |
| 619 | // |
| 620 | // (It's possible to not widen at all by pulling out factors of 2 before |
| 621 | // the multiplication; for example, K=2 can be calculated as |
| 622 | // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires |
| 623 | // extra arithmetic, so it's not an obvious win, and it gets |
| 624 | // much more complicated for K > 3.) |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 625 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 626 | // Protection from insane SCEVs; this bound is conservative, |
| 627 | // but it probably doesn't matter. |
| 628 | if (K > 1000) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 629 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 630 | |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 631 | unsigned W = SE.getTypeSizeInBits(ResultTy); |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 632 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 633 | // Calculate K! / 2^T and T; we divide out the factors of two before |
| 634 | // multiplying for calculating K! / 2^T to avoid overflow. |
| 635 | // Other overflow doesn't matter because we only care about the bottom |
| 636 | // W bits of the result. |
| 637 | APInt OddFactorial(W, 1); |
| 638 | unsigned T = 1; |
| 639 | for (unsigned i = 3; i <= K; ++i) { |
| 640 | APInt Mult(W, i); |
| 641 | unsigned TwoFactors = Mult.countTrailingZeros(); |
| 642 | T += TwoFactors; |
| 643 | Mult = Mult.lshr(TwoFactors); |
| 644 | OddFactorial *= Mult; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 645 | } |
Nick Lewycky | dbaa60a | 2008-06-13 04:38:55 +0000 | [diff] [blame] | 646 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 647 | // We need at least W + T bits for the multiplication step |
nicholas | 9e3e5fd | 2009-01-25 08:16:27 +0000 | [diff] [blame] | 648 | unsigned CalculationBits = W + T; |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 649 | |
| 650 | // Calcuate 2^T, at width T+W. |
| 651 | APInt DivFactor = APInt(CalculationBits, 1).shl(T); |
| 652 | |
| 653 | // Calculate the multiplicative inverse of K! / 2^T; |
| 654 | // this multiplication factor will perform the exact division by |
| 655 | // K! / 2^T. |
| 656 | APInt Mod = APInt::getSignedMinValue(W+1); |
| 657 | APInt MultiplyFactor = OddFactorial.zext(W+1); |
| 658 | MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); |
| 659 | MultiplyFactor = MultiplyFactor.trunc(W); |
| 660 | |
| 661 | // Calculate the product, at width T+W |
| 662 | const IntegerType *CalculationTy = IntegerType::get(CalculationBits); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 663 | const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 664 | for (unsigned i = 1; i != K; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 665 | const SCEV *S = SE.getMinusSCEV(It, SE.getIntegerSCEV(i, It->getType())); |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 666 | Dividend = SE.getMulExpr(Dividend, |
| 667 | SE.getTruncateOrZeroExtend(S, CalculationTy)); |
| 668 | } |
| 669 | |
| 670 | // Divide by 2^T |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 671 | const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 672 | |
| 673 | // Truncate the result, and divide by K! / 2^T. |
| 674 | |
| 675 | return SE.getMulExpr(SE.getConstant(MultiplyFactor), |
| 676 | SE.getTruncateOrZeroExtend(DivResult, ResultTy)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 679 | /// evaluateAtIteration - Return the value of this chain of recurrences at |
| 680 | /// the specified iteration number. We can evaluate this recurrence by |
| 681 | /// multiplying each element in the chain by the binomial coefficient |
| 682 | /// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as: |
| 683 | /// |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 684 | /// 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] | 685 | /// |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 686 | /// where BC(It, k) stands for binomial coefficient. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 687 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 688 | const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It, |
Dan Gohman | f5606fd | 2009-07-21 00:38:55 +0000 | [diff] [blame] | 689 | ScalarEvolution &SE) const { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 690 | const SCEV *Result = getStart(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 691 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 692 | // The computation is correct in the face of overflow provided that the |
| 693 | // multiplication is performed _after_ the evaluation of the binomial |
| 694 | // coefficient. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 695 | const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType()); |
Nick Lewycky | b6218e0 | 2008-10-13 03:58:02 +0000 | [diff] [blame] | 696 | if (isa<SCEVCouldNotCompute>(Coeff)) |
| 697 | return Coeff; |
| 698 | |
| 699 | Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 700 | } |
| 701 | return Result; |
| 702 | } |
| 703 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 704 | //===----------------------------------------------------------------------===// |
| 705 | // SCEV Expression folder implementations |
| 706 | //===----------------------------------------------------------------------===// |
| 707 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 708 | const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, |
Dan Gohman | 69eacc7 | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 709 | const Type *Ty) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 710 | assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 711 | "This is not a truncating conversion!"); |
Dan Gohman | 13a51e2 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 712 | assert(isSCEVable(Ty) && |
| 713 | "This is not a conversion to a SCEVable type!"); |
| 714 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 715 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 716 | FoldingSetNodeID ID; |
| 717 | ID.AddInteger(scTruncate); |
| 718 | ID.AddPointer(Op); |
| 719 | ID.AddPointer(Ty); |
| 720 | void *IP = 0; |
| 721 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 722 | |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 723 | // Fold if the operand is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 724 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 725 | return getConstant( |
| 726 | cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 727 | |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 728 | // trunc(trunc(x)) --> trunc(x) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 729 | if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 730 | return getTruncateExpr(ST->getOperand(), Ty); |
| 731 | |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 732 | // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 733 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 734 | return getTruncateOrSignExtend(SS->getOperand(), Ty); |
| 735 | |
| 736 | // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 737 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 738 | return getTruncateOrZeroExtend(SZ->getOperand(), Ty); |
| 739 | |
Dan Gohman | 1c0aa2c | 2009-06-18 16:24:47 +0000 | [diff] [blame] | 740 | // 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] | 741 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 742 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 743 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 744 | Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty)); |
| 745 | return getAddRecExpr(Operands, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 748 | // The cast wasn't folded; create an explicit cast node. |
| 749 | // Recompute the insert position, as it may have been invalidated. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 750 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 751 | SCEV *S = SCEVAllocator.Allocate<SCEVTruncateExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 752 | new (S) SCEVTruncateExpr(ID, Op, Ty); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 753 | UniqueSCEVs.InsertNode(S, IP); |
| 754 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 757 | const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op, |
Dan Gohman | 69eacc7 | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 758 | const Type *Ty) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 759 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | 36d4092 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 760 | "This is not an extending conversion!"); |
Dan Gohman | 13a51e2 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 761 | assert(isSCEVable(Ty) && |
| 762 | "This is not a conversion to a SCEVable type!"); |
| 763 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | 36d4092 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 764 | |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 765 | // Fold if the operand is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 766 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 767 | const Type *IntTy = getEffectiveSCEVType(Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 768 | Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy); |
| 769 | if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 770 | return getConstant(cast<ConstantInt>(C)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 771 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 772 | |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 773 | // zext(zext(x)) --> zext(x) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 774 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 775 | return getZeroExtendExpr(SZ->getOperand(), Ty); |
| 776 | |
Dan Gohman | db88842 | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 777 | // Before doing any expensive analysis, check to see if we've already |
| 778 | // computed a SCEV for this Op and Ty. |
| 779 | FoldingSetNodeID ID; |
| 780 | ID.AddInteger(scZeroExtend); |
| 781 | ID.AddPointer(Op); |
| 782 | ID.AddPointer(Ty); |
| 783 | void *IP = 0; |
| 784 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 785 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 786 | // 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] | 787 | // 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] | 788 | // operands (often constants). This allows analysis of something like |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 789 | // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 790 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 791 | if (AR->isAffine()) { |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 792 | const SCEV *Start = AR->getStart(); |
| 793 | const SCEV *Step = AR->getStepRecurrence(*this); |
| 794 | unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
| 795 | const Loop *L = AR->getLoop(); |
| 796 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 797 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 798 | // Note that this serves two purposes: It filters out loops that are |
| 799 | // simply not analyzable, and it covers the case where this code is |
| 800 | // being called from within backedge-taken count analysis, such that |
| 801 | // attempting to ask for the backedge-taken count would likely result |
| 802 | // in infinite recursion. In the later case, the analysis code will |
| 803 | // cope with a conservative value, and it will take care to purge |
| 804 | // that value once it has finished. |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 805 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 806 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | 4ada77f | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 807 | // Manually compute the final value for AR, checking for |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 808 | // overflow. |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 809 | |
| 810 | // Check whether the backedge-taken count can be losslessly casted to |
| 811 | // the addrec's type. The count is always unsigned. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 812 | const SCEV *CastedMaxBECount = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 813 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 814 | const SCEV *RecastedMaxBECount = |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 815 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 816 | if (MaxBECount == RecastedMaxBECount) { |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 817 | const Type *WideTy = IntegerType::get(BitWidth * 2); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 818 | // Check whether Start+Step*MaxBECount has no unsigned overflow. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 819 | const SCEV *ZMul = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 820 | getMulExpr(CastedMaxBECount, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 821 | getTruncateOrZeroExtend(Step, Start->getType())); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 822 | const SCEV *Add = getAddExpr(Start, ZMul); |
| 823 | const SCEV *OperandExtendedAdd = |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 824 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 825 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 826 | getZeroExtendExpr(Step, WideTy))); |
| 827 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 828 | // Return the expression with the addrec on the outside. |
| 829 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 830 | getZeroExtendExpr(Step, Ty), |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 831 | L); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 832 | |
| 833 | // Similar to above, only this time treat the step value as signed. |
| 834 | // This covers loops that count down. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 835 | const SCEV *SMul = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 836 | getMulExpr(CastedMaxBECount, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 837 | getTruncateOrSignExtend(Step, Start->getType())); |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 838 | Add = getAddExpr(Start, SMul); |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 839 | OperandExtendedAdd = |
| 840 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 841 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 842 | getSignExtendExpr(Step, WideTy))); |
| 843 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 844 | // Return the expression with the addrec on the outside. |
| 845 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 846 | getSignExtendExpr(Step, Ty), |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 847 | L); |
| 848 | } |
| 849 | |
| 850 | // If the backedge is guarded by a comparison with the pre-inc value |
| 851 | // the addrec is safe. Also, if the entry is guarded by a comparison |
| 852 | // with the start value and the backedge is guarded by a comparison |
| 853 | // with the post-inc value, the addrec is safe. |
| 854 | if (isKnownPositive(Step)) { |
| 855 | const SCEV *N = getConstant(APInt::getMinValue(BitWidth) - |
| 856 | getUnsignedRange(Step).getUnsignedMax()); |
| 857 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) || |
| 858 | (isLoopGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) && |
| 859 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, |
| 860 | AR->getPostIncExpr(*this), N))) |
| 861 | // Return the expression with the addrec on the outside. |
| 862 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 863 | getZeroExtendExpr(Step, Ty), |
| 864 | L); |
| 865 | } else if (isKnownNegative(Step)) { |
| 866 | const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) - |
| 867 | getSignedRange(Step).getSignedMin()); |
| 868 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) && |
| 869 | (isLoopGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) || |
| 870 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, |
| 871 | AR->getPostIncExpr(*this), N))) |
| 872 | // Return the expression with the addrec on the outside. |
| 873 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 874 | getSignExtendExpr(Step, Ty), |
| 875 | L); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 879 | |
Dan Gohman | db88842 | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 880 | // The cast wasn't folded; create an explicit cast node. |
| 881 | // Recompute the insert position, as it may have been invalidated. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 882 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 883 | SCEV *S = SCEVAllocator.Allocate<SCEVZeroExtendExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 884 | new (S) SCEVZeroExtendExpr(ID, Op, Ty); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 885 | UniqueSCEVs.InsertNode(S, IP); |
| 886 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 889 | const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op, |
Dan Gohman | 69eacc7 | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 890 | const Type *Ty) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 891 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 892 | "This is not an extending conversion!"); |
Dan Gohman | 13a51e2 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 893 | assert(isSCEVable(Ty) && |
| 894 | "This is not a conversion to a SCEVable type!"); |
| 895 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 896 | |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 897 | // Fold if the operand is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 898 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 899 | const Type *IntTy = getEffectiveSCEVType(Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 900 | Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy); |
| 901 | if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 902 | return getConstant(cast<ConstantInt>(C)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 903 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 904 | |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 905 | // sext(sext(x)) --> sext(x) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 906 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 907 | return getSignExtendExpr(SS->getOperand(), Ty); |
| 908 | |
Dan Gohman | db88842 | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 909 | // Before doing any expensive analysis, check to see if we've already |
| 910 | // computed a SCEV for this Op and Ty. |
| 911 | FoldingSetNodeID ID; |
| 912 | ID.AddInteger(scSignExtend); |
| 913 | ID.AddPointer(Op); |
| 914 | ID.AddPointer(Ty); |
| 915 | void *IP = 0; |
| 916 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 917 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 918 | // 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] | 919 | // 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] | 920 | // operands (often constants). This allows analysis of something like |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 921 | // this: for (signed char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 922 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 923 | if (AR->isAffine()) { |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 924 | const SCEV *Start = AR->getStart(); |
| 925 | const SCEV *Step = AR->getStepRecurrence(*this); |
| 926 | unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
| 927 | const Loop *L = AR->getLoop(); |
| 928 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 929 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 930 | // Note that this serves two purposes: It filters out loops that are |
| 931 | // simply not analyzable, and it covers the case where this code is |
| 932 | // being called from within backedge-taken count analysis, such that |
| 933 | // attempting to ask for the backedge-taken count would likely result |
| 934 | // in infinite recursion. In the later case, the analysis code will |
| 935 | // cope with a conservative value, and it will take care to purge |
| 936 | // that value once it has finished. |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 937 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 938 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | 4ada77f | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 939 | // Manually compute the final value for AR, checking for |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 940 | // overflow. |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 941 | |
| 942 | // Check whether the backedge-taken count can be losslessly casted to |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 943 | // the addrec's type. The count is always unsigned. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 944 | const SCEV *CastedMaxBECount = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 945 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 946 | const SCEV *RecastedMaxBECount = |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 947 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 948 | if (MaxBECount == RecastedMaxBECount) { |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 949 | const Type *WideTy = IntegerType::get(BitWidth * 2); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 950 | // Check whether Start+Step*MaxBECount has no signed overflow. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 951 | const SCEV *SMul = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 952 | getMulExpr(CastedMaxBECount, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 953 | getTruncateOrSignExtend(Step, Start->getType())); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 954 | const SCEV *Add = getAddExpr(Start, SMul); |
| 955 | const SCEV *OperandExtendedAdd = |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 956 | getAddExpr(getSignExtendExpr(Start, WideTy), |
| 957 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 958 | getSignExtendExpr(Step, WideTy))); |
| 959 | if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 960 | // Return the expression with the addrec on the outside. |
| 961 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 962 | getSignExtendExpr(Step, Ty), |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 963 | L); |
Dan Gohman | 2d4f5b1 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 964 | |
| 965 | // Similar to above, only this time treat the step value as unsigned. |
| 966 | // This covers loops that count up with an unsigned step. |
| 967 | const SCEV *UMul = |
| 968 | getMulExpr(CastedMaxBECount, |
| 969 | getTruncateOrZeroExtend(Step, Start->getType())); |
| 970 | Add = getAddExpr(Start, UMul); |
| 971 | OperandExtendedAdd = |
| 972 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 973 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 974 | getZeroExtendExpr(Step, WideTy))); |
| 975 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
| 976 | // Return the expression with the addrec on the outside. |
| 977 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 978 | getZeroExtendExpr(Step, Ty), |
| 979 | L); |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | // If the backedge is guarded by a comparison with the pre-inc value |
| 983 | // the addrec is safe. Also, if the entry is guarded by a comparison |
| 984 | // with the start value and the backedge is guarded by a comparison |
| 985 | // with the post-inc value, the addrec is safe. |
| 986 | if (isKnownPositive(Step)) { |
| 987 | const SCEV *N = getConstant(APInt::getSignedMinValue(BitWidth) - |
| 988 | getSignedRange(Step).getSignedMax()); |
| 989 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, AR, N) || |
| 990 | (isLoopGuardedByCond(L, ICmpInst::ICMP_SLT, Start, N) && |
| 991 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, |
| 992 | AR->getPostIncExpr(*this), N))) |
| 993 | // Return the expression with the addrec on the outside. |
| 994 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 995 | getSignExtendExpr(Step, Ty), |
| 996 | L); |
| 997 | } else if (isKnownNegative(Step)) { |
| 998 | const SCEV *N = getConstant(APInt::getSignedMaxValue(BitWidth) - |
| 999 | getSignedRange(Step).getSignedMin()); |
| 1000 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, AR, N) || |
| 1001 | (isLoopGuardedByCond(L, ICmpInst::ICMP_SGT, Start, N) && |
| 1002 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, |
| 1003 | AR->getPostIncExpr(*this), N))) |
| 1004 | // Return the expression with the addrec on the outside. |
| 1005 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 1006 | getSignExtendExpr(Step, Ty), |
| 1007 | L); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1011 | |
Dan Gohman | db88842 | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 1012 | // The cast wasn't folded; create an explicit cast node. |
| 1013 | // Recompute the insert position, as it may have been invalidated. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1014 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1015 | SCEV *S = SCEVAllocator.Allocate<SCEVSignExtendExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1016 | new (S) SCEVSignExtendExpr(ID, Op, Ty); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1017 | UniqueSCEVs.InsertNode(S, IP); |
| 1018 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1021 | /// getAnyExtendExpr - Return a SCEV for the given operand extended with |
| 1022 | /// unspecified bits out to the given type. |
| 1023 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1024 | const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op, |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1025 | const Type *Ty) { |
| 1026 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
| 1027 | "This is not an extending conversion!"); |
| 1028 | assert(isSCEVable(Ty) && |
| 1029 | "This is not a conversion to a SCEVable type!"); |
| 1030 | Ty = getEffectiveSCEVType(Ty); |
| 1031 | |
| 1032 | // Sign-extend negative constants. |
| 1033 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| 1034 | if (SC->getValue()->getValue().isNegative()) |
| 1035 | return getSignExtendExpr(Op, Ty); |
| 1036 | |
| 1037 | // Peel off a truncate cast. |
| 1038 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1039 | const SCEV *NewOp = T->getOperand(); |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1040 | if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty)) |
| 1041 | return getAnyExtendExpr(NewOp, Ty); |
| 1042 | return getTruncateOrNoop(NewOp, Ty); |
| 1043 | } |
| 1044 | |
| 1045 | // Next try a zext cast. If the cast is folded, use it. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1046 | const SCEV *ZExt = getZeroExtendExpr(Op, Ty); |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1047 | if (!isa<SCEVZeroExtendExpr>(ZExt)) |
| 1048 | return ZExt; |
| 1049 | |
| 1050 | // Next try a sext cast. If the cast is folded, use it. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1051 | const SCEV *SExt = getSignExtendExpr(Op, Ty); |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1052 | if (!isa<SCEVSignExtendExpr>(SExt)) |
| 1053 | return SExt; |
| 1054 | |
| 1055 | // If the expression is obviously signed, use the sext cast value. |
| 1056 | if (isa<SCEVSMaxExpr>(Op)) |
| 1057 | return SExt; |
| 1058 | |
| 1059 | // Absent any other information, use the zext cast value. |
| 1060 | return ZExt; |
| 1061 | } |
| 1062 | |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1063 | /// CollectAddOperandsWithScales - Process the given Ops list, which is |
| 1064 | /// a list of operands to be added under the given scale, update the given |
| 1065 | /// map. This is a helper function for getAddRecExpr. As an example of |
| 1066 | /// what it does, given a sequence of operands that would form an add |
| 1067 | /// expression like this: |
| 1068 | /// |
| 1069 | /// m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r) |
| 1070 | /// |
| 1071 | /// where A and B are constants, update the map with these values: |
| 1072 | /// |
| 1073 | /// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0) |
| 1074 | /// |
| 1075 | /// and add 13 + A*B*29 to AccumulatedConstant. |
| 1076 | /// This will allow getAddRecExpr to produce this: |
| 1077 | /// |
| 1078 | /// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B) |
| 1079 | /// |
| 1080 | /// This form often exposes folding opportunities that are hidden in |
| 1081 | /// the original operand list. |
| 1082 | /// |
| 1083 | /// Return true iff it appears that any interesting folding opportunities |
| 1084 | /// may be exposed. This helps getAddRecExpr short-circuit extra work in |
| 1085 | /// the common case where no interesting opportunities are present, and |
| 1086 | /// is also used as a check to avoid infinite recursion. |
| 1087 | /// |
| 1088 | static bool |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1089 | CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M, |
| 1090 | SmallVector<const SCEV *, 8> &NewOps, |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1091 | APInt &AccumulatedConstant, |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1092 | const SmallVectorImpl<const SCEV *> &Ops, |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1093 | const APInt &Scale, |
| 1094 | ScalarEvolution &SE) { |
| 1095 | bool Interesting = false; |
| 1096 | |
| 1097 | // Iterate over the add operands. |
| 1098 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 1099 | const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]); |
| 1100 | if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) { |
| 1101 | APInt NewScale = |
| 1102 | Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue(); |
| 1103 | if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) { |
| 1104 | // A multiplication of a constant with another add; recurse. |
| 1105 | Interesting |= |
| 1106 | CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
| 1107 | cast<SCEVAddExpr>(Mul->getOperand(1)) |
| 1108 | ->getOperands(), |
| 1109 | NewScale, SE); |
| 1110 | } else { |
| 1111 | // A multiplication of a constant with some other value. Update |
| 1112 | // the map. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1113 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end()); |
| 1114 | const SCEV *Key = SE.getMulExpr(MulOps); |
| 1115 | std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = |
Dan Gohman | 3bf01f0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1116 | M.insert(std::make_pair(Key, NewScale)); |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1117 | if (Pair.second) { |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1118 | NewOps.push_back(Pair.first->first); |
| 1119 | } else { |
| 1120 | Pair.first->second += NewScale; |
| 1121 | // The map already had an entry for this value, which may indicate |
| 1122 | // a folding opportunity. |
| 1123 | Interesting = true; |
| 1124 | } |
| 1125 | } |
| 1126 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| 1127 | // Pull a buried constant out to the outside. |
| 1128 | if (Scale != 1 || AccumulatedConstant != 0 || C->isZero()) |
| 1129 | Interesting = true; |
| 1130 | AccumulatedConstant += Scale * C->getValue()->getValue(); |
| 1131 | } else { |
| 1132 | // An ordinary operand. Update the map. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1133 | std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = |
Dan Gohman | 3bf01f0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1134 | M.insert(std::make_pair(Ops[i], Scale)); |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1135 | if (Pair.second) { |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1136 | NewOps.push_back(Pair.first->first); |
| 1137 | } else { |
| 1138 | Pair.first->second += Scale; |
| 1139 | // The map already had an entry for this value, which may indicate |
| 1140 | // a folding opportunity. |
| 1141 | Interesting = true; |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | return Interesting; |
| 1147 | } |
| 1148 | |
| 1149 | namespace { |
| 1150 | struct APIntCompare { |
| 1151 | bool operator()(const APInt &LHS, const APInt &RHS) const { |
| 1152 | return LHS.ult(RHS); |
| 1153 | } |
| 1154 | }; |
| 1155 | } |
| 1156 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1157 | /// getAddExpr - Get a canonical add expression, or something simpler if |
| 1158 | /// possible. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1159 | const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1160 | assert(!Ops.empty() && "Cannot get empty add!"); |
| 1161 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1162 | #ifndef NDEBUG |
| 1163 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1164 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1165 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1166 | "SCEVAddExpr operand types don't match!"); |
| 1167 | #endif |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1168 | |
| 1169 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1170 | GroupByComplexity(Ops, LI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1171 | |
| 1172 | // If there are any constants, fold them together. |
| 1173 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1174 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1175 | ++Idx; |
| 1176 | assert(Idx < Ops.size()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1177 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1178 | // We found two constants, fold them together! |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1179 | Ops[0] = getConstant(LHSC->getValue()->getValue() + |
| 1180 | RHSC->getValue()->getValue()); |
Dan Gohman | 68f23e8 | 2009-06-14 22:53:57 +0000 | [diff] [blame] | 1181 | if (Ops.size() == 2) return Ops[0]; |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1182 | Ops.erase(Ops.begin()+1); // Erase the folded element |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1183 | LHSC = cast<SCEVConstant>(Ops[0]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | // If we are left with a constant zero being added, strip it off. |
| 1187 | if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
| 1188 | Ops.erase(Ops.begin()); |
| 1189 | --Idx; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | if (Ops.size() == 1) return Ops[0]; |
| 1194 | |
| 1195 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1196 | // so, merge them together into an multiply expression. Since we sorted the |
| 1197 | // list, these values are required to be adjacent. |
| 1198 | const Type *Ty = Ops[0]->getType(); |
| 1199 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1200 | if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2 |
| 1201 | // Found a match, merge the two values into a multiply, and add any |
| 1202 | // remaining values to the result. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1203 | const SCEV *Two = getIntegerSCEV(2, Ty); |
| 1204 | const SCEV *Mul = getMulExpr(Ops[i], Two); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1205 | if (Ops.size() == 2) |
| 1206 | return Mul; |
| 1207 | Ops.erase(Ops.begin()+i, Ops.begin()+i+2); |
| 1208 | Ops.push_back(Mul); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1209 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1212 | // Check for truncates. If all the operands are truncated from the same |
| 1213 | // type, see if factoring out the truncate would permit the result to be |
| 1214 | // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n) |
| 1215 | // if the contents of the resulting outer trunc fold to something simple. |
| 1216 | for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) { |
| 1217 | const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]); |
| 1218 | const Type *DstType = Trunc->getType(); |
| 1219 | const Type *SrcType = Trunc->getOperand()->getType(); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1220 | SmallVector<const SCEV *, 8> LargeOps; |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1221 | bool Ok = true; |
| 1222 | // Check all the operands to see if they can be represented in the |
| 1223 | // source type of the truncate. |
| 1224 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 1225 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) { |
| 1226 | if (T->getOperand()->getType() != SrcType) { |
| 1227 | Ok = false; |
| 1228 | break; |
| 1229 | } |
| 1230 | LargeOps.push_back(T->getOperand()); |
| 1231 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| 1232 | // This could be either sign or zero extension, but sign extension |
| 1233 | // is much more likely to be foldable here. |
| 1234 | LargeOps.push_back(getSignExtendExpr(C, SrcType)); |
| 1235 | } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1236 | SmallVector<const SCEV *, 8> LargeMulOps; |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1237 | for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { |
| 1238 | if (const SCEVTruncateExpr *T = |
| 1239 | dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) { |
| 1240 | if (T->getOperand()->getType() != SrcType) { |
| 1241 | Ok = false; |
| 1242 | break; |
| 1243 | } |
| 1244 | LargeMulOps.push_back(T->getOperand()); |
| 1245 | } else if (const SCEVConstant *C = |
| 1246 | dyn_cast<SCEVConstant>(M->getOperand(j))) { |
| 1247 | // This could be either sign or zero extension, but sign extension |
| 1248 | // is much more likely to be foldable here. |
| 1249 | LargeMulOps.push_back(getSignExtendExpr(C, SrcType)); |
| 1250 | } else { |
| 1251 | Ok = false; |
| 1252 | break; |
| 1253 | } |
| 1254 | } |
| 1255 | if (Ok) |
| 1256 | LargeOps.push_back(getMulExpr(LargeMulOps)); |
| 1257 | } else { |
| 1258 | Ok = false; |
| 1259 | break; |
| 1260 | } |
| 1261 | } |
| 1262 | if (Ok) { |
| 1263 | // Evaluate the expression in the larger type. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1264 | const SCEV *Fold = getAddExpr(LargeOps); |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1265 | // If it folds to something simple, use it. Otherwise, don't. |
| 1266 | if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold)) |
| 1267 | return getTruncateExpr(Fold, DstType); |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | // Skip past any other cast SCEVs. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1272 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) |
| 1273 | ++Idx; |
| 1274 | |
| 1275 | // If there are add operands they would be next. |
| 1276 | if (Idx < Ops.size()) { |
| 1277 | bool DeletedAdd = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1278 | while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1279 | // If we have an add, expand the add operands onto the end of the operands |
| 1280 | // list. |
| 1281 | Ops.insert(Ops.end(), Add->op_begin(), Add->op_end()); |
| 1282 | Ops.erase(Ops.begin()+Idx); |
| 1283 | DeletedAdd = true; |
| 1284 | } |
| 1285 | |
| 1286 | // If we deleted at least one add, we added operands to the end of the list, |
| 1287 | // and they are not necessarily sorted. Recurse to resort and resimplify |
| 1288 | // any operands we just aquired. |
| 1289 | if (DeletedAdd) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1290 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | // Skip over the add expression until we get to a multiply. |
| 1294 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1295 | ++Idx; |
| 1296 | |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1297 | // Check to see if there are any folding opportunities present with |
| 1298 | // operands multiplied by constant values. |
| 1299 | if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) { |
| 1300 | uint64_t BitWidth = getTypeSizeInBits(Ty); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1301 | DenseMap<const SCEV *, APInt> M; |
| 1302 | SmallVector<const SCEV *, 8> NewOps; |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1303 | APInt AccumulatedConstant(BitWidth, 0); |
| 1304 | if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
| 1305 | Ops, APInt(BitWidth, 1), *this)) { |
| 1306 | // Some interesting folding opportunity is present, so its worthwhile to |
| 1307 | // re-generate the operands list. Group the operands by constant scale, |
| 1308 | // to avoid multiplying by the same constant scale multiple times. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1309 | std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists; |
| 1310 | for (SmallVector<const SCEV *, 8>::iterator I = NewOps.begin(), |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1311 | E = NewOps.end(); I != E; ++I) |
| 1312 | MulOpLists[M.find(*I)->second].push_back(*I); |
| 1313 | // Re-generate the operands list. |
| 1314 | Ops.clear(); |
| 1315 | if (AccumulatedConstant != 0) |
| 1316 | Ops.push_back(getConstant(AccumulatedConstant)); |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1317 | for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator |
| 1318 | I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I) |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1319 | if (I->first != 0) |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1320 | Ops.push_back(getMulExpr(getConstant(I->first), |
| 1321 | getAddExpr(I->second))); |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1322 | if (Ops.empty()) |
| 1323 | return getIntegerSCEV(0, Ty); |
| 1324 | if (Ops.size() == 1) |
| 1325 | return Ops[0]; |
| 1326 | return getAddExpr(Ops); |
| 1327 | } |
| 1328 | } |
| 1329 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1330 | // If we are adding something to a multiply expression, make sure the |
| 1331 | // something is not already an operand of the multiply. If so, merge it into |
| 1332 | // the multiply. |
| 1333 | for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1334 | const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1335 | for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1336 | const SCEV *MulOpSCEV = Mul->getOperand(MulOp); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1337 | for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1338 | if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(Ops[AddOp])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1339 | // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1340 | const SCEV *InnerMul = Mul->getOperand(MulOp == 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1341 | if (Mul->getNumOperands() != 2) { |
| 1342 | // If the multiply has more than two operands, we must get the |
| 1343 | // Y*Z term. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1344 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), Mul->op_end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1345 | MulOps.erase(MulOps.begin()+MulOp); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1346 | InnerMul = getMulExpr(MulOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1347 | } |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1348 | const SCEV *One = getIntegerSCEV(1, Ty); |
| 1349 | const SCEV *AddOne = getAddExpr(InnerMul, One); |
| 1350 | const SCEV *OuterMul = getMulExpr(AddOne, Ops[AddOp]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1351 | if (Ops.size() == 2) return OuterMul; |
| 1352 | if (AddOp < Idx) { |
| 1353 | Ops.erase(Ops.begin()+AddOp); |
| 1354 | Ops.erase(Ops.begin()+Idx-1); |
| 1355 | } else { |
| 1356 | Ops.erase(Ops.begin()+Idx); |
| 1357 | Ops.erase(Ops.begin()+AddOp-1); |
| 1358 | } |
| 1359 | Ops.push_back(OuterMul); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1360 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | // Check this multiply against other multiplies being added together. |
| 1364 | for (unsigned OtherMulIdx = Idx+1; |
| 1365 | OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]); |
| 1366 | ++OtherMulIdx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1367 | const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1368 | // If MulOp occurs in OtherMul, we can fold the two multiplies |
| 1369 | // together. |
| 1370 | for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); |
| 1371 | OMulOp != e; ++OMulOp) |
| 1372 | if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { |
| 1373 | // 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] | 1374 | const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1375 | if (Mul->getNumOperands() != 2) { |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1376 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), |
| 1377 | Mul->op_end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1378 | MulOps.erase(MulOps.begin()+MulOp); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1379 | InnerMul1 = getMulExpr(MulOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1380 | } |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1381 | const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1382 | if (OtherMul->getNumOperands() != 2) { |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1383 | SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(), |
| 1384 | OtherMul->op_end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1385 | MulOps.erase(MulOps.begin()+OMulOp); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1386 | InnerMul2 = getMulExpr(MulOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1387 | } |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1388 | const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2); |
| 1389 | const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1390 | if (Ops.size() == 2) return OuterMul; |
| 1391 | Ops.erase(Ops.begin()+Idx); |
| 1392 | Ops.erase(Ops.begin()+OtherMulIdx-1); |
| 1393 | Ops.push_back(OuterMul); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1394 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1395 | } |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | // If there are any add recurrences in the operands list, see if any other |
| 1401 | // added values are loop invariant. If so, we can fold them into the |
| 1402 | // recurrence. |
| 1403 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1404 | ++Idx; |
| 1405 | |
| 1406 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1407 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1408 | // Scan all of the other operands to this add and add them to the vector if |
| 1409 | // they are loop invariant w.r.t. the recurrence. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1410 | SmallVector<const SCEV *, 8> LIOps; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1411 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1412 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1413 | if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { |
| 1414 | LIOps.push_back(Ops[i]); |
| 1415 | Ops.erase(Ops.begin()+i); |
| 1416 | --i; --e; |
| 1417 | } |
| 1418 | |
| 1419 | // If we found some loop invariants, fold them into the recurrence. |
| 1420 | if (!LIOps.empty()) { |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1421 | // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1422 | LIOps.push_back(AddRec->getStart()); |
| 1423 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1424 | SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(), |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1425 | AddRec->op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1426 | AddRecOps[0] = getAddExpr(LIOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1427 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1428 | const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1429 | // If all of the other operands were loop invariant, we are done. |
| 1430 | if (Ops.size() == 1) return NewRec; |
| 1431 | |
| 1432 | // Otherwise, add the folded AddRec by the non-liv parts. |
| 1433 | for (unsigned i = 0;; ++i) |
| 1434 | if (Ops[i] == AddRec) { |
| 1435 | Ops[i] = NewRec; |
| 1436 | break; |
| 1437 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1438 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1442 | // there are multiple AddRec's with the same loop induction variable being |
| 1443 | // added together. If so, we can fold them. |
| 1444 | for (unsigned OtherIdx = Idx+1; |
| 1445 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) |
| 1446 | if (OtherIdx != Idx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1447 | const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1448 | if (AddRec->getLoop() == OtherAddRec->getLoop()) { |
| 1449 | // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D} |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1450 | SmallVector<const SCEV *, 4> NewOps(AddRec->op_begin(), |
| 1451 | AddRec->op_end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1452 | for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) { |
| 1453 | if (i >= NewOps.size()) { |
| 1454 | NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i, |
| 1455 | OtherAddRec->op_end()); |
| 1456 | break; |
| 1457 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1458 | NewOps[i] = getAddExpr(NewOps[i], OtherAddRec->getOperand(i)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1459 | } |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1460 | const SCEV *NewAddRec = getAddRecExpr(NewOps, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1461 | |
| 1462 | if (Ops.size() == 2) return NewAddRec; |
| 1463 | |
| 1464 | Ops.erase(Ops.begin()+Idx); |
| 1465 | Ops.erase(Ops.begin()+OtherIdx-1); |
| 1466 | Ops.push_back(NewAddRec); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1467 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1472 | // next one. |
| 1473 | } |
| 1474 | |
| 1475 | // Okay, it looks like we really DO need an add expr. Check to see if we |
| 1476 | // already have one, otherwise create a new one. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1477 | FoldingSetNodeID ID; |
| 1478 | ID.AddInteger(scAddExpr); |
| 1479 | ID.AddInteger(Ops.size()); |
| 1480 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1481 | ID.AddPointer(Ops[i]); |
| 1482 | void *IP = 0; |
| 1483 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1484 | SCEV *S = SCEVAllocator.Allocate<SCEVAddExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1485 | new (S) SCEVAddExpr(ID, Ops); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1486 | UniqueSCEVs.InsertNode(S, IP); |
| 1487 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1491 | /// getMulExpr - Get a canonical multiply expression, or something simpler if |
| 1492 | /// possible. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1493 | const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1494 | assert(!Ops.empty() && "Cannot get empty mul!"); |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1495 | #ifndef NDEBUG |
| 1496 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1497 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1498 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1499 | "SCEVMulExpr operand types don't match!"); |
| 1500 | #endif |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1501 | |
| 1502 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1503 | GroupByComplexity(Ops, LI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1504 | |
| 1505 | // If there are any constants, fold them together. |
| 1506 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1507 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1508 | |
| 1509 | // C1*(C2+V) -> C1*C2 + C1*V |
| 1510 | if (Ops.size() == 2) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1511 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1512 | if (Add->getNumOperands() == 2 && |
| 1513 | isa<SCEVConstant>(Add->getOperand(0))) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1514 | return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)), |
| 1515 | getMulExpr(LHSC, Add->getOperand(1))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1516 | |
| 1517 | |
| 1518 | ++Idx; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1519 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1520 | // We found two constants, fold them together! |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame^] | 1521 | ConstantInt *Fold = ConstantInt::get(getContext(), |
| 1522 | LHSC->getValue()->getValue() * |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1523 | RHSC->getValue()->getValue()); |
| 1524 | Ops[0] = getConstant(Fold); |
| 1525 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1526 | if (Ops.size() == 1) return Ops[0]; |
| 1527 | LHSC = cast<SCEVConstant>(Ops[0]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | // If we are left with a constant one being multiplied, strip it off. |
| 1531 | if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) { |
| 1532 | Ops.erase(Ops.begin()); |
| 1533 | --Idx; |
| 1534 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
| 1535 | // If we have a multiply of zero, it will always be zero. |
| 1536 | return Ops[0]; |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | // Skip over the add expression until we get to a multiply. |
| 1541 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1542 | ++Idx; |
| 1543 | |
| 1544 | if (Ops.size() == 1) |
| 1545 | return Ops[0]; |
| 1546 | |
| 1547 | // If there are mul operands inline them all into this expression. |
| 1548 | if (Idx < Ops.size()) { |
| 1549 | bool DeletedMul = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1550 | while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1551 | // If we have an mul, expand the mul operands onto the end of the operands |
| 1552 | // list. |
| 1553 | Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end()); |
| 1554 | Ops.erase(Ops.begin()+Idx); |
| 1555 | DeletedMul = true; |
| 1556 | } |
| 1557 | |
| 1558 | // If we deleted at least one mul, we added operands to the end of the list, |
| 1559 | // and they are not necessarily sorted. Recurse to resort and resimplify |
| 1560 | // any operands we just aquired. |
| 1561 | if (DeletedMul) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1562 | return getMulExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | // If there are any add recurrences in the operands list, see if any other |
| 1566 | // added values are loop invariant. If so, we can fold them into the |
| 1567 | // recurrence. |
| 1568 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1569 | ++Idx; |
| 1570 | |
| 1571 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1572 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1573 | // Scan all of the other operands to this mul and add them to the vector if |
| 1574 | // they are loop invariant w.r.t. the recurrence. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1575 | SmallVector<const SCEV *, 8> LIOps; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1576 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1577 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1578 | if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { |
| 1579 | LIOps.push_back(Ops[i]); |
| 1580 | Ops.erase(Ops.begin()+i); |
| 1581 | --i; --e; |
| 1582 | } |
| 1583 | |
| 1584 | // If we found some loop invariants, fold them into the recurrence. |
| 1585 | if (!LIOps.empty()) { |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1586 | // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step} |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1587 | SmallVector<const SCEV *, 4> NewOps; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1588 | NewOps.reserve(AddRec->getNumOperands()); |
| 1589 | if (LIOps.size() == 1) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1590 | const SCEV *Scale = LIOps[0]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1591 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1592 | NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1593 | } else { |
| 1594 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1595 | SmallVector<const SCEV *, 4> MulOps(LIOps.begin(), LIOps.end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1596 | MulOps.push_back(AddRec->getOperand(i)); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1597 | NewOps.push_back(getMulExpr(MulOps)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1598 | } |
| 1599 | } |
| 1600 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1601 | const SCEV *NewRec = getAddRecExpr(NewOps, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1602 | |
| 1603 | // If all of the other operands were loop invariant, we are done. |
| 1604 | if (Ops.size() == 1) return NewRec; |
| 1605 | |
| 1606 | // Otherwise, multiply the folded AddRec by the non-liv parts. |
| 1607 | for (unsigned i = 0;; ++i) |
| 1608 | if (Ops[i] == AddRec) { |
| 1609 | Ops[i] = NewRec; |
| 1610 | break; |
| 1611 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1612 | return getMulExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1616 | // there are multiple AddRec's with the same loop induction variable being |
| 1617 | // multiplied together. If so, we can fold them. |
| 1618 | for (unsigned OtherIdx = Idx+1; |
| 1619 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) |
| 1620 | if (OtherIdx != Idx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1621 | const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1622 | if (AddRec->getLoop() == OtherAddRec->getLoop()) { |
| 1623 | // 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] | 1624 | const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec; |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1625 | const SCEV *NewStart = getMulExpr(F->getStart(), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1626 | G->getStart()); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1627 | const SCEV *B = F->getStepRecurrence(*this); |
| 1628 | const SCEV *D = G->getStepRecurrence(*this); |
| 1629 | const SCEV *NewStep = getAddExpr(getMulExpr(F, D), |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1630 | getMulExpr(G, B), |
| 1631 | getMulExpr(B, D)); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1632 | const SCEV *NewAddRec = getAddRecExpr(NewStart, NewStep, |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1633 | F->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1634 | if (Ops.size() == 2) return NewAddRec; |
| 1635 | |
| 1636 | Ops.erase(Ops.begin()+Idx); |
| 1637 | Ops.erase(Ops.begin()+OtherIdx-1); |
| 1638 | Ops.push_back(NewAddRec); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1639 | return getMulExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1644 | // next one. |
| 1645 | } |
| 1646 | |
| 1647 | // Okay, it looks like we really DO need an mul expr. Check to see if we |
| 1648 | // already have one, otherwise create a new one. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1649 | FoldingSetNodeID ID; |
| 1650 | ID.AddInteger(scMulExpr); |
| 1651 | ID.AddInteger(Ops.size()); |
| 1652 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1653 | ID.AddPointer(Ops[i]); |
| 1654 | void *IP = 0; |
| 1655 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1656 | SCEV *S = SCEVAllocator.Allocate<SCEVMulExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1657 | new (S) SCEVMulExpr(ID, Ops); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1658 | UniqueSCEVs.InsertNode(S, IP); |
| 1659 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1662 | /// getUDivExpr - Get a canonical multiply expression, or something simpler if |
| 1663 | /// possible. |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1664 | const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS, |
| 1665 | const SCEV *RHS) { |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1666 | assert(getEffectiveSCEVType(LHS->getType()) == |
| 1667 | getEffectiveSCEVType(RHS->getType()) && |
| 1668 | "SCEVUDivExpr operand types don't match!"); |
| 1669 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1670 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1671 | if (RHSC->getValue()->equalsInt(1)) |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 1672 | return LHS; // X udiv 1 --> x |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1673 | if (RHSC->isZero()) |
| 1674 | return getIntegerSCEV(0, LHS->getType()); // value is undefined |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1675 | |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1676 | // Determine if the division can be folded into the operands of |
| 1677 | // its operands. |
| 1678 | // TODO: Generalize this to non-constants by using known-bits information. |
| 1679 | const Type *Ty = LHS->getType(); |
| 1680 | unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros(); |
| 1681 | unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ; |
| 1682 | // For non-power-of-two values, effectively round the value up to the |
| 1683 | // nearest power of two. |
| 1684 | if (!RHSC->getValue()->getValue().isPowerOf2()) |
| 1685 | ++MaxShiftAmt; |
| 1686 | const IntegerType *ExtTy = |
| 1687 | IntegerType::get(getTypeSizeInBits(Ty) + MaxShiftAmt); |
| 1688 | // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. |
| 1689 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) |
| 1690 | if (const SCEVConstant *Step = |
| 1691 | dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) |
| 1692 | if (!Step->getValue()->getValue() |
| 1693 | .urem(RHSC->getValue()->getValue()) && |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1694 | getZeroExtendExpr(AR, ExtTy) == |
| 1695 | getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), |
| 1696 | getZeroExtendExpr(Step, ExtTy), |
| 1697 | AR->getLoop())) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1698 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1699 | for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i) |
| 1700 | Operands.push_back(getUDivExpr(AR->getOperand(i), RHS)); |
| 1701 | return getAddRecExpr(Operands, AR->getLoop()); |
| 1702 | } |
| 1703 | // (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] | 1704 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1705 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1706 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) |
| 1707 | Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy)); |
| 1708 | if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1709 | // Find an operand that's safely divisible. |
| 1710 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1711 | const SCEV *Op = M->getOperand(i); |
| 1712 | const SCEV *Div = getUDivExpr(Op, RHSC); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1713 | if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1714 | const SmallVectorImpl<const SCEV *> &MOperands = M->getOperands(); |
| 1715 | Operands = SmallVector<const SCEV *, 4>(MOperands.begin(), |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1716 | MOperands.end()); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1717 | Operands[i] = Div; |
| 1718 | return getMulExpr(Operands); |
| 1719 | } |
| 1720 | } |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1721 | } |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1722 | // (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] | 1723 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1724 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1725 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) |
| 1726 | Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy)); |
| 1727 | if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { |
| 1728 | Operands.clear(); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1729 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1730 | const SCEV *Op = getUDivExpr(A->getOperand(i), RHS); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1731 | if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i)) |
| 1732 | break; |
| 1733 | Operands.push_back(Op); |
| 1734 | } |
| 1735 | if (Operands.size() == A->getNumOperands()) |
| 1736 | return getAddExpr(Operands); |
| 1737 | } |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1738 | } |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1739 | |
| 1740 | // Fold if both operands are constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1741 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1742 | Constant *LHSCV = LHSC->getValue(); |
| 1743 | Constant *RHSCV = RHSC->getValue(); |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 1744 | return getConstant(cast<ConstantInt>(getContext().getConstantExprUDiv(LHSCV, |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 1745 | RHSCV))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1746 | } |
| 1747 | } |
| 1748 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1749 | FoldingSetNodeID ID; |
| 1750 | ID.AddInteger(scUDivExpr); |
| 1751 | ID.AddPointer(LHS); |
| 1752 | ID.AddPointer(RHS); |
| 1753 | void *IP = 0; |
| 1754 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1755 | SCEV *S = SCEVAllocator.Allocate<SCEVUDivExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1756 | new (S) SCEVUDivExpr(ID, LHS, RHS); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1757 | UniqueSCEVs.InsertNode(S, IP); |
| 1758 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1762 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 1763 | /// Simplify the expression as much as possible. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1764 | const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, |
Dan Gohman | 1c4054f | 2009-07-24 01:03:59 +0000 | [diff] [blame] | 1765 | const SCEV *Step, const Loop *L) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1766 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1767 | Operands.push_back(Start); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1768 | if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1769 | if (StepChrec->getLoop() == L) { |
| 1770 | Operands.insert(Operands.end(), StepChrec->op_begin(), |
| 1771 | StepChrec->op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1772 | return getAddRecExpr(Operands, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | Operands.push_back(Step); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1776 | return getAddRecExpr(Operands, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1779 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 1780 | /// Simplify the expression as much as possible. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1781 | const SCEV * |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1782 | ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands, |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1783 | const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1784 | if (Operands.size() == 1) return Operands[0]; |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1785 | #ifndef NDEBUG |
| 1786 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 1787 | assert(getEffectiveSCEVType(Operands[i]->getType()) == |
| 1788 | getEffectiveSCEVType(Operands[0]->getType()) && |
| 1789 | "SCEVAddRecExpr operand types don't match!"); |
| 1790 | #endif |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1791 | |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1792 | if (Operands.back()->isZero()) { |
| 1793 | Operands.pop_back(); |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1794 | return getAddRecExpr(Operands, L); // {X,+,0} --> X |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1795 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1796 | |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1797 | // Canonicalize nested AddRecs in by nesting them in order of loop depth. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1798 | if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) { |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1799 | const Loop* NestedLoop = NestedAR->getLoop(); |
| 1800 | if (L->getLoopDepth() < NestedLoop->getLoopDepth()) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1801 | SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(), |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1802 | NestedAR->op_end()); |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1803 | Operands[0] = NestedAR->getStart(); |
Dan Gohman | 08c4c07 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 1804 | // AddRecs require their operands be loop-invariant with respect to their |
| 1805 | // loops. Don't perform this transformation if it would break this |
| 1806 | // requirement. |
| 1807 | bool AllInvariant = true; |
| 1808 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 1809 | if (!Operands[i]->isLoopInvariant(L)) { |
| 1810 | AllInvariant = false; |
| 1811 | break; |
| 1812 | } |
| 1813 | if (AllInvariant) { |
| 1814 | NestedOperands[0] = getAddRecExpr(Operands, L); |
| 1815 | AllInvariant = true; |
| 1816 | for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i) |
| 1817 | if (!NestedOperands[i]->isLoopInvariant(NestedLoop)) { |
| 1818 | AllInvariant = false; |
| 1819 | break; |
| 1820 | } |
| 1821 | if (AllInvariant) |
| 1822 | // Ok, both add recurrences are valid after the transformation. |
| 1823 | return getAddRecExpr(NestedOperands, NestedLoop); |
| 1824 | } |
| 1825 | // Reset Operands to its original state. |
| 1826 | Operands[0] = NestedAR; |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1827 | } |
| 1828 | } |
| 1829 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1830 | FoldingSetNodeID ID; |
| 1831 | ID.AddInteger(scAddRecExpr); |
| 1832 | ID.AddInteger(Operands.size()); |
| 1833 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 1834 | ID.AddPointer(Operands[i]); |
| 1835 | ID.AddPointer(L); |
| 1836 | void *IP = 0; |
| 1837 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1838 | SCEV *S = SCEVAllocator.Allocate<SCEVAddRecExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1839 | new (S) SCEVAddRecExpr(ID, Operands, L); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1840 | UniqueSCEVs.InsertNode(S, IP); |
| 1841 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1844 | const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, |
| 1845 | const SCEV *RHS) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1846 | SmallVector<const SCEV *, 2> Ops; |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1847 | Ops.push_back(LHS); |
| 1848 | Ops.push_back(RHS); |
| 1849 | return getSMaxExpr(Ops); |
| 1850 | } |
| 1851 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1852 | const SCEV * |
| 1853 | ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1854 | assert(!Ops.empty() && "Cannot get empty smax!"); |
| 1855 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1856 | #ifndef NDEBUG |
| 1857 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1858 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1859 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1860 | "SCEVSMaxExpr operand types don't match!"); |
| 1861 | #endif |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1862 | |
| 1863 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1864 | GroupByComplexity(Ops, LI); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1865 | |
| 1866 | // If there are any constants, fold them together. |
| 1867 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1868 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1869 | ++Idx; |
| 1870 | assert(Idx < Ops.size()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1871 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1872 | // We found two constants, fold them together! |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame^] | 1873 | ConstantInt *Fold = ConstantInt::get(getContext(), |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1874 | APIntOps::smax(LHSC->getValue()->getValue(), |
| 1875 | RHSC->getValue()->getValue())); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1876 | Ops[0] = getConstant(Fold); |
| 1877 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1878 | if (Ops.size() == 1) return Ops[0]; |
| 1879 | LHSC = cast<SCEVConstant>(Ops[0]); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
Dan Gohman | d156c09 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1882 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1883 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) { |
| 1884 | Ops.erase(Ops.begin()); |
| 1885 | --Idx; |
Dan Gohman | d156c09 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1886 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) { |
| 1887 | // If we have an smax with a constant maximum-int, it will always be |
| 1888 | // maximum-int. |
| 1889 | return Ops[0]; |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | if (Ops.size() == 1) return Ops[0]; |
| 1894 | |
| 1895 | // Find the first SMax |
| 1896 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr) |
| 1897 | ++Idx; |
| 1898 | |
| 1899 | // Check to see if one of the operands is an SMax. If so, expand its operands |
| 1900 | // onto our operand list, and recurse to simplify. |
| 1901 | if (Idx < Ops.size()) { |
| 1902 | bool DeletedSMax = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1903 | while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1904 | Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end()); |
| 1905 | Ops.erase(Ops.begin()+Idx); |
| 1906 | DeletedSMax = true; |
| 1907 | } |
| 1908 | |
| 1909 | if (DeletedSMax) |
| 1910 | return getSMaxExpr(Ops); |
| 1911 | } |
| 1912 | |
| 1913 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1914 | // so, delete one. Since we sorted the list, these values are required to |
| 1915 | // be adjacent. |
| 1916 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1917 | if (Ops[i] == Ops[i+1]) { // X smax Y smax Y --> X smax Y |
| 1918 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 1919 | --i; --e; |
| 1920 | } |
| 1921 | |
| 1922 | if (Ops.size() == 1) return Ops[0]; |
| 1923 | |
| 1924 | assert(!Ops.empty() && "Reduced smax down to nothing!"); |
| 1925 | |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1926 | // 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] | 1927 | // already have one, otherwise create a new one. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1928 | FoldingSetNodeID ID; |
| 1929 | ID.AddInteger(scSMaxExpr); |
| 1930 | ID.AddInteger(Ops.size()); |
| 1931 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1932 | ID.AddPointer(Ops[i]); |
| 1933 | void *IP = 0; |
| 1934 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1935 | SCEV *S = SCEVAllocator.Allocate<SCEVSMaxExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1936 | new (S) SCEVSMaxExpr(ID, Ops); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1937 | UniqueSCEVs.InsertNode(S, IP); |
| 1938 | return S; |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1941 | const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, |
| 1942 | const SCEV *RHS) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1943 | SmallVector<const SCEV *, 2> Ops; |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1944 | Ops.push_back(LHS); |
| 1945 | Ops.push_back(RHS); |
| 1946 | return getUMaxExpr(Ops); |
| 1947 | } |
| 1948 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1949 | const SCEV * |
| 1950 | ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1951 | assert(!Ops.empty() && "Cannot get empty umax!"); |
| 1952 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1953 | #ifndef NDEBUG |
| 1954 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1955 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1956 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1957 | "SCEVUMaxExpr operand types don't match!"); |
| 1958 | #endif |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1959 | |
| 1960 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1961 | GroupByComplexity(Ops, LI); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1962 | |
| 1963 | // If there are any constants, fold them together. |
| 1964 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1965 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1966 | ++Idx; |
| 1967 | assert(Idx < Ops.size()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1968 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1969 | // We found two constants, fold them together! |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame^] | 1970 | ConstantInt *Fold = ConstantInt::get(getContext(), |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1971 | APIntOps::umax(LHSC->getValue()->getValue(), |
| 1972 | RHSC->getValue()->getValue())); |
| 1973 | Ops[0] = getConstant(Fold); |
| 1974 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1975 | if (Ops.size() == 1) return Ops[0]; |
| 1976 | LHSC = cast<SCEVConstant>(Ops[0]); |
| 1977 | } |
| 1978 | |
Dan Gohman | d156c09 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1979 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1980 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) { |
| 1981 | Ops.erase(Ops.begin()); |
| 1982 | --Idx; |
Dan Gohman | d156c09 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1983 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) { |
| 1984 | // If we have an umax with a constant maximum-int, it will always be |
| 1985 | // maximum-int. |
| 1986 | return Ops[0]; |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | if (Ops.size() == 1) return Ops[0]; |
| 1991 | |
| 1992 | // Find the first UMax |
| 1993 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr) |
| 1994 | ++Idx; |
| 1995 | |
| 1996 | // Check to see if one of the operands is a UMax. If so, expand its operands |
| 1997 | // onto our operand list, and recurse to simplify. |
| 1998 | if (Idx < Ops.size()) { |
| 1999 | bool DeletedUMax = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2000 | while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2001 | Ops.insert(Ops.end(), UMax->op_begin(), UMax->op_end()); |
| 2002 | Ops.erase(Ops.begin()+Idx); |
| 2003 | DeletedUMax = true; |
| 2004 | } |
| 2005 | |
| 2006 | if (DeletedUMax) |
| 2007 | return getUMaxExpr(Ops); |
| 2008 | } |
| 2009 | |
| 2010 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 2011 | // so, delete one. Since we sorted the list, these values are required to |
| 2012 | // be adjacent. |
| 2013 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 2014 | if (Ops[i] == Ops[i+1]) { // X umax Y umax Y --> X umax Y |
| 2015 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 2016 | --i; --e; |
| 2017 | } |
| 2018 | |
| 2019 | if (Ops.size() == 1) return Ops[0]; |
| 2020 | |
| 2021 | assert(!Ops.empty() && "Reduced umax down to nothing!"); |
| 2022 | |
| 2023 | // Okay, it looks like we really DO need a umax expr. Check to see if we |
| 2024 | // already have one, otherwise create a new one. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2025 | FoldingSetNodeID ID; |
| 2026 | ID.AddInteger(scUMaxExpr); |
| 2027 | ID.AddInteger(Ops.size()); |
| 2028 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 2029 | ID.AddPointer(Ops[i]); |
| 2030 | void *IP = 0; |
| 2031 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 2032 | SCEV *S = SCEVAllocator.Allocate<SCEVUMaxExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 2033 | new (S) SCEVUMaxExpr(ID, Ops); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2034 | UniqueSCEVs.InsertNode(S, IP); |
| 2035 | return S; |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2038 | const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS, |
| 2039 | const SCEV *RHS) { |
Dan Gohman | d01fff8 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2040 | // ~smax(~x, ~y) == smin(x, y). |
| 2041 | return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 2042 | } |
| 2043 | |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2044 | const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS, |
| 2045 | const SCEV *RHS) { |
Dan Gohman | d01fff8 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2046 | // ~umax(~x, ~y) == umin(x, y) |
| 2047 | return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 2048 | } |
| 2049 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2050 | const SCEV *ScalarEvolution::getUnknown(Value *V) { |
Dan Gohman | 984c78a | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2051 | // Don't attempt to do anything other than create a SCEVUnknown object |
| 2052 | // here. createSCEV only calls getUnknown after checking for all other |
| 2053 | // interesting possibilities, and any other code that calls getUnknown |
| 2054 | // is doing so in order to hide a value from SCEV canonicalization. |
| 2055 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2056 | FoldingSetNodeID ID; |
| 2057 | ID.AddInteger(scUnknown); |
| 2058 | ID.AddPointer(V); |
| 2059 | void *IP = 0; |
| 2060 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 2061 | SCEV *S = SCEVAllocator.Allocate<SCEVUnknown>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 2062 | new (S) SCEVUnknown(ID, V); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2063 | UniqueSCEVs.InsertNode(S, IP); |
| 2064 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2067 | //===----------------------------------------------------------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2068 | // Basic SCEV Analysis and PHI Idiom Recognition Code |
| 2069 | // |
| 2070 | |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2071 | /// isSCEVable - Test if values of the given type are analyzable within |
| 2072 | /// the SCEV framework. This primarily includes integer types, and it |
| 2073 | /// can optionally include pointer types if the ScalarEvolution class |
| 2074 | /// has access to target-specific information. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2075 | bool ScalarEvolution::isSCEVable(const Type *Ty) const { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2076 | // Integers are always SCEVable. |
| 2077 | if (Ty->isInteger()) |
| 2078 | return true; |
| 2079 | |
| 2080 | // Pointers are SCEVable if TargetData information is available |
| 2081 | // to provide pointer size information. |
| 2082 | if (isa<PointerType>(Ty)) |
| 2083 | return TD != NULL; |
| 2084 | |
| 2085 | // Otherwise it's not SCEVable. |
| 2086 | return false; |
| 2087 | } |
| 2088 | |
| 2089 | /// getTypeSizeInBits - Return the size in bits of the specified type, |
| 2090 | /// for which isSCEVable must return true. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2091 | uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2092 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2093 | |
| 2094 | // If we have a TargetData, use it! |
| 2095 | if (TD) |
| 2096 | return TD->getTypeSizeInBits(Ty); |
| 2097 | |
| 2098 | // Otherwise, we support only integer types. |
| 2099 | assert(Ty->isInteger() && "isSCEVable permitted a non-SCEVable type!"); |
| 2100 | return Ty->getPrimitiveSizeInBits(); |
| 2101 | } |
| 2102 | |
| 2103 | /// getEffectiveSCEVType - Return a type with the same bitwidth as |
| 2104 | /// the given type and which represents how SCEV will treat the given |
| 2105 | /// type, for which isSCEVable must return true. For pointer types, |
| 2106 | /// this is the pointer-sized integer type. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2107 | const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2108 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2109 | |
| 2110 | if (Ty->isInteger()) |
| 2111 | return Ty; |
| 2112 | |
| 2113 | assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!"); |
| 2114 | return TD->getIntPtrType(); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2115 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2116 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2117 | const SCEV *ScalarEvolution::getCouldNotCompute() { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2118 | return &CouldNotCompute; |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2121 | /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the |
| 2122 | /// expression and create a new one. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2123 | const SCEV *ScalarEvolution::getSCEV(Value *V) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2124 | assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2125 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2126 | std::map<SCEVCallbackVH, const SCEV *>::iterator I = Scalars.find(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2127 | if (I != Scalars.end()) return I->second; |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2128 | const SCEV *S = createSCEV(V); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2129 | Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2130 | return S; |
| 2131 | } |
| 2132 | |
Dan Gohman | 984c78a | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2133 | /// getIntegerSCEV - Given a SCEVable type, create a constant for the |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2134 | /// specified signed integer value and return a SCEV for the constant. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2135 | const SCEV *ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) { |
Dan Gohman | 984c78a | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2136 | const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty)); |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame^] | 2137 | return getConstant(ConstantInt::get(ITy, Val)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V |
| 2141 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2142 | const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) { |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2143 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2144 | return getConstant( |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 2145 | cast<ConstantInt>(getContext().getConstantExprNeg(VC->getValue()))); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2146 | |
| 2147 | const Type *Ty = V->getType(); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2148 | Ty = getEffectiveSCEVType(Ty); |
Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2149 | return getMulExpr(V, |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 2150 | getConstant(cast<ConstantInt>(getContext().getAllOnesValue(Ty)))); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2154 | const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) { |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2155 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2156 | return getConstant( |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 2157 | cast<ConstantInt>(getContext().getConstantExprNot(VC->getValue()))); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2158 | |
| 2159 | const Type *Ty = V->getType(); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2160 | Ty = getEffectiveSCEVType(Ty); |
Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2161 | const SCEV *AllOnes = |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 2162 | getConstant(cast<ConstantInt>(getContext().getAllOnesValue(Ty))); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2163 | return getMinusSCEV(AllOnes, V); |
| 2164 | } |
| 2165 | |
| 2166 | /// getMinusSCEV - Return a SCEV corresponding to LHS - RHS. |
| 2167 | /// |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2168 | const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, |
| 2169 | const SCEV *RHS) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2170 | // X - Y --> X + -Y |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2171 | return getAddExpr(LHS, getNegativeSCEV(RHS)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2175 | /// input value to the specified type. If the type must be extended, it is zero |
| 2176 | /// extended. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2177 | const SCEV * |
| 2178 | ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2179 | const Type *Ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2180 | const Type *SrcTy = V->getType(); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2181 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2182 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2183 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2184 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2185 | return V; // No conversion |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2186 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2187 | return getTruncateExpr(V, Ty); |
| 2188 | return getZeroExtendExpr(V, Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2192 | /// input value to the specified type. If the type must be extended, it is sign |
| 2193 | /// extended. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2194 | const SCEV * |
| 2195 | ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2196 | const Type *Ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2197 | const Type *SrcTy = V->getType(); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2198 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2199 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2200 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2201 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2202 | return V; // No conversion |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2203 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2204 | return getTruncateExpr(V, Ty); |
| 2205 | return getSignExtendExpr(V, Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2208 | /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2209 | /// input value to the specified type. If the type must be extended, it is zero |
| 2210 | /// extended. The conversion must not be narrowing. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2211 | const SCEV * |
| 2212 | ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2213 | const Type *SrcTy = V->getType(); |
| 2214 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2215 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 2216 | "Cannot noop or zero extend with non-integer arguments!"); |
| 2217 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2218 | "getNoopOrZeroExtend cannot truncate!"); |
| 2219 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2220 | return V; // No conversion |
| 2221 | return getZeroExtendExpr(V, Ty); |
| 2222 | } |
| 2223 | |
| 2224 | /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2225 | /// input value to the specified type. If the type must be extended, it is sign |
| 2226 | /// extended. The conversion must not be narrowing. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2227 | const SCEV * |
| 2228 | ScalarEvolution::getNoopOrSignExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2229 | const Type *SrcTy = V->getType(); |
| 2230 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2231 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 2232 | "Cannot noop or sign extend with non-integer arguments!"); |
| 2233 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2234 | "getNoopOrSignExtend cannot truncate!"); |
| 2235 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2236 | return V; // No conversion |
| 2237 | return getSignExtendExpr(V, Ty); |
| 2238 | } |
| 2239 | |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2240 | /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of |
| 2241 | /// the input value to the specified type. If the type must be extended, |
| 2242 | /// it is extended with unspecified bits. The conversion must not be |
| 2243 | /// narrowing. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2244 | const SCEV * |
| 2245 | ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2246 | const Type *SrcTy = V->getType(); |
| 2247 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2248 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 2249 | "Cannot noop or any extend with non-integer arguments!"); |
| 2250 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2251 | "getNoopOrAnyExtend cannot truncate!"); |
| 2252 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2253 | return V; // No conversion |
| 2254 | return getAnyExtendExpr(V, Ty); |
| 2255 | } |
| 2256 | |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2257 | /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the |
| 2258 | /// input value to the specified type. The conversion must not be widening. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2259 | const SCEV * |
| 2260 | ScalarEvolution::getTruncateOrNoop(const SCEV *V, const Type *Ty) { |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2261 | const Type *SrcTy = V->getType(); |
| 2262 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2263 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 2264 | "Cannot truncate or noop with non-integer arguments!"); |
| 2265 | assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) && |
| 2266 | "getTruncateOrNoop cannot extend!"); |
| 2267 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2268 | return V; // No conversion |
| 2269 | return getTruncateExpr(V, Ty); |
| 2270 | } |
| 2271 | |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2272 | /// getUMaxFromMismatchedTypes - Promote the operands to the wider of |
| 2273 | /// the types using zero-extension, and then perform a umax operation |
| 2274 | /// with them. |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2275 | const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS, |
| 2276 | const SCEV *RHS) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2277 | const SCEV *PromotedLHS = LHS; |
| 2278 | const SCEV *PromotedRHS = RHS; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2279 | |
| 2280 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2281 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2282 | else |
| 2283 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2284 | |
| 2285 | return getUMaxExpr(PromotedLHS, PromotedRHS); |
| 2286 | } |
| 2287 | |
Dan Gohman | 9e62bb0 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2288 | /// getUMinFromMismatchedTypes - Promote the operands to the wider of |
| 2289 | /// the types using zero-extension, and then perform a umin operation |
| 2290 | /// with them. |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2291 | const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS, |
| 2292 | const SCEV *RHS) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2293 | const SCEV *PromotedLHS = LHS; |
| 2294 | const SCEV *PromotedRHS = RHS; |
Dan Gohman | 9e62bb0 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2295 | |
| 2296 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2297 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2298 | else |
| 2299 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2300 | |
| 2301 | return getUMinExpr(PromotedLHS, PromotedRHS); |
| 2302 | } |
| 2303 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2304 | /// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value for |
| 2305 | /// the specified instruction and replaces any references to the symbolic value |
| 2306 | /// SymName with the specified value. This is used during PHI resolution. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2307 | void |
| 2308 | ScalarEvolution::ReplaceSymbolicValueWithConcrete(Instruction *I, |
| 2309 | const SCEV *SymName, |
| 2310 | const SCEV *NewVal) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2311 | std::map<SCEVCallbackVH, const SCEV *>::iterator SI = |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2312 | Scalars.find(SCEVCallbackVH(I, this)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2313 | if (SI == Scalars.end()) return; |
| 2314 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2315 | const SCEV *NV = |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2316 | SI->second->replaceSymbolicValuesWithConcrete(SymName, NewVal, *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2317 | if (NV == SI->second) return; // No change. |
| 2318 | |
| 2319 | SI->second = NV; // Update the scalars map! |
| 2320 | |
| 2321 | // Any instruction values that use this instruction might also need to be |
| 2322 | // updated! |
| 2323 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 2324 | UI != E; ++UI) |
| 2325 | ReplaceSymbolicValueWithConcrete(cast<Instruction>(*UI), SymName, NewVal); |
| 2326 | } |
| 2327 | |
| 2328 | /// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in |
| 2329 | /// a loop header, making it a potential recurrence, or it doesn't. |
| 2330 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2331 | const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2332 | if (PN->getNumIncomingValues() == 2) // The loops have been canonicalized. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2333 | if (const Loop *L = LI->getLoopFor(PN->getParent())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2334 | if (L->getHeader() == PN->getParent()) { |
| 2335 | // If it lives in the loop header, it has two incoming values, one |
| 2336 | // from outside the loop, and one from inside. |
| 2337 | unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0)); |
| 2338 | unsigned BackEdge = IncomingEdge^1; |
| 2339 | |
| 2340 | // While we are analyzing this PHI node, handle its value symbolically. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2341 | const SCEV *SymbolicName = getUnknown(PN); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2342 | assert(Scalars.find(PN) == Scalars.end() && |
| 2343 | "PHI node already processed?"); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2344 | Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2345 | |
| 2346 | // Using this symbolic name for the PHI, analyze the value coming around |
| 2347 | // the back-edge. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2348 | const SCEV *BEValue = getSCEV(PN->getIncomingValue(BackEdge)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2349 | |
| 2350 | // NOTE: If BEValue is loop invariant, we know that the PHI node just |
| 2351 | // has a special value for the first iteration of the loop. |
| 2352 | |
| 2353 | // If the value coming around the backedge is an add with the symbolic |
| 2354 | // value we just inserted, then we found a simple induction variable! |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2355 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2356 | // If there is a single occurrence of the symbolic value, replace it |
| 2357 | // with a recurrence. |
| 2358 | unsigned FoundIndex = Add->getNumOperands(); |
| 2359 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2360 | if (Add->getOperand(i) == SymbolicName) |
| 2361 | if (FoundIndex == e) { |
| 2362 | FoundIndex = i; |
| 2363 | break; |
| 2364 | } |
| 2365 | |
| 2366 | if (FoundIndex != Add->getNumOperands()) { |
| 2367 | // Create an add with everything but the specified operand. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2368 | SmallVector<const SCEV *, 8> Ops; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2369 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2370 | if (i != FoundIndex) |
| 2371 | Ops.push_back(Add->getOperand(i)); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2372 | const SCEV *Accum = getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2373 | |
| 2374 | // This is not a valid addrec if the step amount is varying each |
| 2375 | // loop iteration, but is not itself an addrec in this loop. |
| 2376 | if (Accum->isLoopInvariant(L) || |
| 2377 | (isa<SCEVAddRecExpr>(Accum) && |
| 2378 | cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) { |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2379 | const SCEV *StartVal = |
| 2380 | getSCEV(PN->getIncomingValue(IncomingEdge)); |
| 2381 | const SCEV *PHISCEV = |
| 2382 | getAddRecExpr(StartVal, Accum, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2383 | |
| 2384 | // Okay, for the entire analysis of this edge we assumed the PHI |
| 2385 | // to be symbolic. We now need to go back and update all of the |
| 2386 | // entries for the scalars that use the PHI (except for the PHI |
| 2387 | // itself) to use the new analyzed value instead of the "symbolic" |
| 2388 | // value. |
| 2389 | ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV); |
| 2390 | return PHISCEV; |
| 2391 | } |
| 2392 | } |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2393 | } else if (const SCEVAddRecExpr *AddRec = |
| 2394 | dyn_cast<SCEVAddRecExpr>(BEValue)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2395 | // Otherwise, this could be a loop like this: |
| 2396 | // i = 0; for (j = 1; ..; ++j) { .... i = j; } |
| 2397 | // In this case, j = {1,+,1} and BEValue is j. |
| 2398 | // Because the other in-value of i (0) fits the evolution of BEValue |
| 2399 | // i really is an addrec evolution. |
| 2400 | if (AddRec->getLoop() == L && AddRec->isAffine()) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2401 | const SCEV *StartVal = getSCEV(PN->getIncomingValue(IncomingEdge)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2402 | |
| 2403 | // If StartVal = j.start - j.stride, we can use StartVal as the |
| 2404 | // initial step of the addrec evolution. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2405 | if (StartVal == getMinusSCEV(AddRec->getOperand(0), |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 2406 | AddRec->getOperand(1))) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2407 | const SCEV *PHISCEV = |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2408 | getAddRecExpr(StartVal, AddRec->getOperand(1), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2409 | |
| 2410 | // Okay, for the entire analysis of this edge we assumed the PHI |
| 2411 | // to be symbolic. We now need to go back and update all of the |
| 2412 | // entries for the scalars that use the PHI (except for the PHI |
| 2413 | // itself) to use the new analyzed value instead of the "symbolic" |
| 2414 | // value. |
| 2415 | ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV); |
| 2416 | return PHISCEV; |
| 2417 | } |
| 2418 | } |
| 2419 | } |
| 2420 | |
| 2421 | return SymbolicName; |
| 2422 | } |
| 2423 | |
Dan Gohman | 32f35cc | 2009-07-14 14:06:25 +0000 | [diff] [blame] | 2424 | // It's tempting to recognize PHIs with a unique incoming value, however |
| 2425 | // this leads passes like indvars to break LCSSA form. Fortunately, such |
| 2426 | // PHIs are rare, as instcombine zaps them. |
| 2427 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2428 | // 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] | 2429 | return getUnknown(PN); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2432 | /// createNodeForGEP - Expand GEP instructions into add and multiply |
| 2433 | /// operations. This allows them to be analyzed by regular SCEV code. |
| 2434 | /// |
Dan Gohman | 9545fb0 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 2435 | const SCEV *ScalarEvolution::createNodeForGEP(Operator *GEP) { |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2436 | |
| 2437 | const Type *IntPtrTy = TD->getIntPtrType(); |
Dan Gohman | c7034fa | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2438 | Value *Base = GEP->getOperand(0); |
Dan Gohman | d586a4f | 2009-05-09 00:14:52 +0000 | [diff] [blame] | 2439 | // Don't attempt to analyze GEPs over unsized objects. |
| 2440 | if (!cast<PointerType>(Base->getType())->getElementType()->isSized()) |
| 2441 | return getUnknown(GEP); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2442 | const SCEV *TotalOffset = getIntegerSCEV(0, IntPtrTy); |
Dan Gohman | c7034fa | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2443 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 2444 | for (GetElementPtrInst::op_iterator I = next(GEP->op_begin()), |
| 2445 | E = GEP->op_end(); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2446 | I != E; ++I) { |
| 2447 | Value *Index = *I; |
| 2448 | // Compute the (potentially symbolic) offset in bytes for this index. |
| 2449 | if (const StructType *STy = dyn_cast<StructType>(*GTI++)) { |
| 2450 | // For a struct, add the member offset. |
| 2451 | const StructLayout &SL = *TD->getStructLayout(STy); |
| 2452 | unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue(); |
| 2453 | uint64_t Offset = SL.getElementOffset(FieldNo); |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2454 | TotalOffset = getAddExpr(TotalOffset, getIntegerSCEV(Offset, IntPtrTy)); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2455 | } else { |
| 2456 | // For an array, add the element offset, explicitly scaled. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2457 | const SCEV *LocalOffset = getSCEV(Index); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2458 | if (!isa<PointerType>(LocalOffset->getType())) |
| 2459 | // Getelementptr indicies are signed. |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2460 | LocalOffset = getTruncateOrSignExtend(LocalOffset, IntPtrTy); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2461 | LocalOffset = |
| 2462 | getMulExpr(LocalOffset, |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2463 | getIntegerSCEV(TD->getTypeAllocSize(*GTI), IntPtrTy)); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2464 | TotalOffset = getAddExpr(TotalOffset, LocalOffset); |
| 2465 | } |
| 2466 | } |
| 2467 | return getAddExpr(getSCEV(Base), TotalOffset); |
| 2468 | } |
| 2469 | |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2470 | /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is |
| 2471 | /// guaranteed to end in (at every loop iteration). It is, at the same time, |
| 2472 | /// the minimum number of times S is divisible by 2. For example, given {4,+,8} |
| 2473 | /// 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] | 2474 | uint32_t |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2475 | ScalarEvolution::GetMinTrailingZeros(const SCEV *S) { |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2476 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Chris Lattner | 6ecce2a | 2007-11-23 22:36:49 +0000 | [diff] [blame] | 2477 | return C->getValue()->getValue().countTrailingZeros(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2478 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2479 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S)) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2480 | return std::min(GetMinTrailingZeros(T->getOperand()), |
| 2481 | (uint32_t)getTypeSizeInBits(T->getType())); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2482 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2483 | if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) { |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2484 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 2485 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 2486 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2489 | if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) { |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2490 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 2491 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 2492 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2495 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2496 | // The result is the min of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2497 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2498 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2499 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2500 | return MinOpRes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2503 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2504 | // The result is the sum of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2505 | uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0)); |
| 2506 | uint32_t BitWidth = getTypeSizeInBits(M->getType()); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2507 | for (unsigned i = 1, e = M->getNumOperands(); |
| 2508 | SumOpRes != BitWidth && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2509 | SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2510 | BitWidth); |
| 2511 | return SumOpRes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2512 | } |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2513 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2514 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2515 | // The result is the min of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2516 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2517 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2518 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2519 | return MinOpRes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2520 | } |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2521 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2522 | if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2523 | // The result is the min of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2524 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2525 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2526 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2527 | return MinOpRes; |
| 2528 | } |
| 2529 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2530 | if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2531 | // The result is the min of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2532 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2533 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2534 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2535 | return MinOpRes; |
| 2536 | } |
| 2537 | |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2538 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2539 | // For a SCEVUnknown, ask ValueTracking. |
| 2540 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2541 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 2542 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 2543 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones); |
| 2544 | return Zeros.countTrailingOnes(); |
| 2545 | } |
| 2546 | |
| 2547 | // SCEVUDivExpr |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2548 | return 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2551 | /// getUnsignedRange - Determine the unsigned range for a particular SCEV. |
| 2552 | /// |
| 2553 | ConstantRange |
| 2554 | ScalarEvolution::getUnsignedRange(const SCEV *S) { |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2555 | |
| 2556 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2557 | return ConstantRange(C->getValue()->getValue()); |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2558 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2559 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 2560 | ConstantRange X = getUnsignedRange(Add->getOperand(0)); |
| 2561 | for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) |
| 2562 | X = X.add(getUnsignedRange(Add->getOperand(i))); |
| 2563 | return X; |
| 2564 | } |
| 2565 | |
| 2566 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { |
| 2567 | ConstantRange X = getUnsignedRange(Mul->getOperand(0)); |
| 2568 | for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) |
| 2569 | X = X.multiply(getUnsignedRange(Mul->getOperand(i))); |
| 2570 | return X; |
| 2571 | } |
| 2572 | |
| 2573 | if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) { |
| 2574 | ConstantRange X = getUnsignedRange(SMax->getOperand(0)); |
| 2575 | for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) |
| 2576 | X = X.smax(getUnsignedRange(SMax->getOperand(i))); |
| 2577 | return X; |
| 2578 | } |
| 2579 | |
| 2580 | if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) { |
| 2581 | ConstantRange X = getUnsignedRange(UMax->getOperand(0)); |
| 2582 | for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) |
| 2583 | X = X.umax(getUnsignedRange(UMax->getOperand(i))); |
| 2584 | return X; |
| 2585 | } |
| 2586 | |
| 2587 | if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { |
| 2588 | ConstantRange X = getUnsignedRange(UDiv->getLHS()); |
| 2589 | ConstantRange Y = getUnsignedRange(UDiv->getRHS()); |
| 2590 | return X.udiv(Y); |
| 2591 | } |
| 2592 | |
| 2593 | if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { |
| 2594 | ConstantRange X = getUnsignedRange(ZExt->getOperand()); |
| 2595 | return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth()); |
| 2596 | } |
| 2597 | |
| 2598 | if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { |
| 2599 | ConstantRange X = getUnsignedRange(SExt->getOperand()); |
| 2600 | return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth()); |
| 2601 | } |
| 2602 | |
| 2603 | if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { |
| 2604 | ConstantRange X = getUnsignedRange(Trunc->getOperand()); |
| 2605 | return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth()); |
| 2606 | } |
| 2607 | |
| 2608 | ConstantRange FullSet(getTypeSizeInBits(S->getType()), true); |
| 2609 | |
| 2610 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { |
| 2611 | const SCEV *T = getBackedgeTakenCount(AddRec->getLoop()); |
| 2612 | const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T); |
| 2613 | if (!Trip) return FullSet; |
| 2614 | |
| 2615 | // TODO: non-affine addrec |
| 2616 | if (AddRec->isAffine()) { |
| 2617 | const Type *Ty = AddRec->getType(); |
| 2618 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop()); |
| 2619 | if (getTypeSizeInBits(MaxBECount->getType()) <= getTypeSizeInBits(Ty)) { |
| 2620 | MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty); |
| 2621 | |
| 2622 | const SCEV *Start = AddRec->getStart(); |
Dan Gohman | 13dca60 | 2009-07-21 00:42:47 +0000 | [diff] [blame] | 2623 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2624 | const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this); |
| 2625 | |
| 2626 | // Check for overflow. |
Dan Gohman | 13dca60 | 2009-07-21 00:42:47 +0000 | [diff] [blame] | 2627 | // TODO: This is very conservative. |
| 2628 | if (!(Step->isOne() && |
| 2629 | isKnownPredicate(ICmpInst::ICMP_ULT, Start, End)) && |
| 2630 | !(Step->isAllOnesValue() && |
| 2631 | isKnownPredicate(ICmpInst::ICMP_UGT, Start, End))) |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2632 | return FullSet; |
| 2633 | |
| 2634 | ConstantRange StartRange = getUnsignedRange(Start); |
| 2635 | ConstantRange EndRange = getUnsignedRange(End); |
| 2636 | APInt Min = APIntOps::umin(StartRange.getUnsignedMin(), |
| 2637 | EndRange.getUnsignedMin()); |
| 2638 | APInt Max = APIntOps::umax(StartRange.getUnsignedMax(), |
| 2639 | EndRange.getUnsignedMax()); |
| 2640 | if (Min.isMinValue() && Max.isMaxValue()) |
Dan Gohman | 56e1859 | 2009-07-20 22:41:51 +0000 | [diff] [blame] | 2641 | return FullSet; |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2642 | return ConstantRange(Min, Max+1); |
| 2643 | } |
| 2644 | } |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2648 | // For a SCEVUnknown, ask ValueTracking. |
| 2649 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2650 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 2651 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 2652 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD); |
Dan Gohman | 0762051 | 2009-07-20 22:34:18 +0000 | [diff] [blame] | 2653 | if (Ones == ~Zeros + 1) |
| 2654 | return FullSet; |
| 2655 | return ConstantRange(Ones, ~Zeros + 1); |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2656 | } |
| 2657 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2658 | return FullSet; |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2661 | /// getSignedRange - Determine the signed range for a particular SCEV. |
| 2662 | /// |
| 2663 | ConstantRange |
| 2664 | ScalarEvolution::getSignedRange(const SCEV *S) { |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2665 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2666 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
| 2667 | return ConstantRange(C->getValue()->getValue()); |
| 2668 | |
| 2669 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 2670 | ConstantRange X = getSignedRange(Add->getOperand(0)); |
| 2671 | for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) |
| 2672 | X = X.add(getSignedRange(Add->getOperand(i))); |
| 2673 | return X; |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2676 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { |
| 2677 | ConstantRange X = getSignedRange(Mul->getOperand(0)); |
| 2678 | for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) |
| 2679 | X = X.multiply(getSignedRange(Mul->getOperand(i))); |
| 2680 | return X; |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2683 | if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) { |
| 2684 | ConstantRange X = getSignedRange(SMax->getOperand(0)); |
| 2685 | for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) |
| 2686 | X = X.smax(getSignedRange(SMax->getOperand(i))); |
| 2687 | return X; |
| 2688 | } |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2689 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2690 | if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) { |
| 2691 | ConstantRange X = getSignedRange(UMax->getOperand(0)); |
| 2692 | for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) |
| 2693 | X = X.umax(getSignedRange(UMax->getOperand(i))); |
| 2694 | return X; |
| 2695 | } |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2696 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2697 | if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { |
| 2698 | ConstantRange X = getSignedRange(UDiv->getLHS()); |
| 2699 | ConstantRange Y = getSignedRange(UDiv->getRHS()); |
| 2700 | return X.udiv(Y); |
| 2701 | } |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2702 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2703 | if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { |
| 2704 | ConstantRange X = getSignedRange(ZExt->getOperand()); |
| 2705 | return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth()); |
| 2706 | } |
| 2707 | |
| 2708 | if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { |
| 2709 | ConstantRange X = getSignedRange(SExt->getOperand()); |
| 2710 | return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth()); |
| 2711 | } |
| 2712 | |
| 2713 | if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { |
| 2714 | ConstantRange X = getSignedRange(Trunc->getOperand()); |
| 2715 | return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth()); |
| 2716 | } |
| 2717 | |
| 2718 | ConstantRange FullSet(getTypeSizeInBits(S->getType()), true); |
| 2719 | |
| 2720 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { |
| 2721 | const SCEV *T = getBackedgeTakenCount(AddRec->getLoop()); |
| 2722 | const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T); |
| 2723 | if (!Trip) return FullSet; |
| 2724 | |
| 2725 | // TODO: non-affine addrec |
| 2726 | if (AddRec->isAffine()) { |
| 2727 | const Type *Ty = AddRec->getType(); |
| 2728 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop()); |
| 2729 | if (getTypeSizeInBits(MaxBECount->getType()) <= getTypeSizeInBits(Ty)) { |
| 2730 | MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty); |
| 2731 | |
| 2732 | const SCEV *Start = AddRec->getStart(); |
| 2733 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
| 2734 | const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this); |
| 2735 | |
| 2736 | // Check for overflow. |
Dan Gohman | 13dca60 | 2009-07-21 00:42:47 +0000 | [diff] [blame] | 2737 | // TODO: This is very conservative. |
| 2738 | if (!(Step->isOne() && |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2739 | isKnownPredicate(ICmpInst::ICMP_SLT, Start, End)) && |
Dan Gohman | 13dca60 | 2009-07-21 00:42:47 +0000 | [diff] [blame] | 2740 | !(Step->isAllOnesValue() && |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2741 | isKnownPredicate(ICmpInst::ICMP_SGT, Start, End))) |
| 2742 | return FullSet; |
| 2743 | |
| 2744 | ConstantRange StartRange = getSignedRange(Start); |
| 2745 | ConstantRange EndRange = getSignedRange(End); |
| 2746 | APInt Min = APIntOps::smin(StartRange.getSignedMin(), |
| 2747 | EndRange.getSignedMin()); |
| 2748 | APInt Max = APIntOps::smax(StartRange.getSignedMax(), |
| 2749 | EndRange.getSignedMax()); |
| 2750 | if (Min.isMinSignedValue() && Max.isMaxSignedValue()) |
Dan Gohman | dc87c86 | 2009-07-21 00:37:45 +0000 | [diff] [blame] | 2751 | return FullSet; |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2752 | return ConstantRange(Min, Max+1); |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2753 | } |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2754 | } |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2755 | } |
| 2756 | |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2757 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2758 | // For a SCEVUnknown, ask ValueTracking. |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2759 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2760 | unsigned NS = ComputeNumSignBits(U->getValue(), TD); |
| 2761 | if (NS == 1) |
| 2762 | return FullSet; |
| 2763 | return |
| 2764 | ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1), |
| 2765 | APInt::getSignedMaxValue(BitWidth).ashr(NS - 1)+1); |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2768 | return FullSet; |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2771 | /// createSCEV - We know that there is no SCEV for the specified value. |
| 2772 | /// Analyze the expression. |
| 2773 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2774 | const SCEV *ScalarEvolution::createSCEV(Value *V) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2775 | if (!isSCEVable(V->getType())) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2776 | return getUnknown(V); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2777 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2778 | unsigned Opcode = Instruction::UserOp1; |
| 2779 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 2780 | Opcode = I->getOpcode(); |
| 2781 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 2782 | Opcode = CE->getOpcode(); |
Dan Gohman | 984c78a | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2783 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 2784 | return getConstant(CI); |
| 2785 | else if (isa<ConstantPointerNull>(V)) |
| 2786 | return getIntegerSCEV(0, V->getType()); |
| 2787 | else if (isa<UndefValue>(V)) |
| 2788 | return getIntegerSCEV(0, V->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2789 | else |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2790 | return getUnknown(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2791 | |
Dan Gohman | 9545fb0 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 2792 | Operator *U = cast<Operator>(V); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2793 | switch (Opcode) { |
| 2794 | case Instruction::Add: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2795 | return getAddExpr(getSCEV(U->getOperand(0)), |
| 2796 | getSCEV(U->getOperand(1))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2797 | case Instruction::Mul: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2798 | return getMulExpr(getSCEV(U->getOperand(0)), |
| 2799 | getSCEV(U->getOperand(1))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2800 | case Instruction::UDiv: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2801 | return getUDivExpr(getSCEV(U->getOperand(0)), |
| 2802 | getSCEV(U->getOperand(1))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2803 | case Instruction::Sub: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2804 | return getMinusSCEV(getSCEV(U->getOperand(0)), |
| 2805 | getSCEV(U->getOperand(1))); |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2806 | case Instruction::And: |
| 2807 | // For an expression like x&255 that merely masks off the high bits, |
| 2808 | // use zext(trunc(x)) as the SCEV expression. |
| 2809 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 91ae1e7 | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2810 | if (CI->isNullValue()) |
| 2811 | return getSCEV(U->getOperand(1)); |
Dan Gohman | c7ebba1 | 2009-04-27 01:41:10 +0000 | [diff] [blame] | 2812 | if (CI->isAllOnesValue()) |
| 2813 | return getSCEV(U->getOperand(0)); |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2814 | const APInt &A = CI->getValue(); |
Dan Gohman | a7726c3 | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2815 | |
| 2816 | // Instcombine's ShrinkDemandedConstant may strip bits out of |
| 2817 | // constants, obscuring what would otherwise be a low-bits mask. |
| 2818 | // Use ComputeMaskedBits to compute what ShrinkDemandedConstant |
| 2819 | // knew about to reconstruct a low-bits mask value. |
| 2820 | unsigned LZ = A.countLeadingZeros(); |
| 2821 | unsigned BitWidth = A.getBitWidth(); |
| 2822 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
| 2823 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 2824 | ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD); |
| 2825 | |
| 2826 | APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ); |
| 2827 | |
Dan Gohman | ae1d7dd | 2009-06-17 23:54:37 +0000 | [diff] [blame] | 2828 | if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask)) |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2829 | return |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2830 | getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)), |
Dan Gohman | a7726c3 | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2831 | IntegerType::get(BitWidth - LZ)), |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2832 | U->getType()); |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2833 | } |
| 2834 | break; |
Dan Gohman | a7726c3 | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2835 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2836 | case Instruction::Or: |
| 2837 | // If the RHS of the Or is a constant, we may have something like: |
| 2838 | // X*4+1 which got turned into X*4|1. Handle this as an Add so loop |
| 2839 | // optimizations will transparently handle this case. |
| 2840 | // |
| 2841 | // In order for this transformation to be safe, the LHS must be of the |
| 2842 | // form X*(2^n) and the Or constant must be less than 2^n. |
| 2843 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2844 | const SCEV *LHS = getSCEV(U->getOperand(0)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2845 | const APInt &CIVal = CI->getValue(); |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2846 | if (GetMinTrailingZeros(LHS) >= |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2847 | (CIVal.getBitWidth() - CIVal.countLeadingZeros())) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2848 | return getAddExpr(LHS, getSCEV(U->getOperand(1))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2849 | } |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2850 | break; |
| 2851 | case Instruction::Xor: |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2852 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2853 | // If the RHS of the xor is a signbit, then this is just an add. |
| 2854 | // Instcombine turns add of signbit into xor as a strength reduction step. |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2855 | if (CI->getValue().isSignBit()) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2856 | return getAddExpr(getSCEV(U->getOperand(0)), |
| 2857 | getSCEV(U->getOperand(1))); |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2858 | |
| 2859 | // 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] | 2860 | if (CI->isAllOnesValue()) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2861 | return getNotSCEV(getSCEV(U->getOperand(0))); |
Dan Gohman | fc78cff | 2009-05-18 16:29:04 +0000 | [diff] [blame] | 2862 | |
| 2863 | // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask. |
| 2864 | // This is a variant of the check for xor with -1, and it handles |
| 2865 | // the case where instcombine has trimmed non-demanded bits out |
| 2866 | // of an xor with -1. |
| 2867 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0))) |
| 2868 | if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1))) |
| 2869 | if (BO->getOpcode() == Instruction::And && |
| 2870 | LCI->getValue() == CI->getValue()) |
| 2871 | if (const SCEVZeroExtendExpr *Z = |
Dan Gohman | e49ae43 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 2872 | dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) { |
Dan Gohman | ed1d8bb | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 2873 | const Type *UTy = U->getType(); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2874 | const SCEV *Z0 = Z->getOperand(); |
Dan Gohman | ed1d8bb | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 2875 | const Type *Z0Ty = Z0->getType(); |
| 2876 | unsigned Z0TySize = getTypeSizeInBits(Z0Ty); |
| 2877 | |
| 2878 | // If C is a low-bits mask, the zero extend is zerving to |
| 2879 | // mask off the high bits. Complement the operand and |
| 2880 | // re-apply the zext. |
| 2881 | if (APIntOps::isMask(Z0TySize, CI->getValue())) |
| 2882 | return getZeroExtendExpr(getNotSCEV(Z0), UTy); |
| 2883 | |
| 2884 | // If C is a single bit, it may be in the sign-bit position |
| 2885 | // before the zero-extend. In this case, represent the xor |
| 2886 | // using an add, which is equivalent, and re-apply the zext. |
| 2887 | APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize); |
| 2888 | if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() && |
| 2889 | Trunc.isSignBit()) |
| 2890 | return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)), |
| 2891 | UTy); |
Dan Gohman | e49ae43 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 2892 | } |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2893 | } |
| 2894 | break; |
| 2895 | |
| 2896 | case Instruction::Shl: |
| 2897 | // Turn shift left of a constant amount into a multiply. |
| 2898 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 2899 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame^] | 2900 | Constant *X = ConstantInt::get(getContext(), |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2901 | APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2902 | return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2903 | } |
| 2904 | break; |
| 2905 | |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2906 | case Instruction::LShr: |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 2907 | // Turn logical shift right of a constant into a unsigned divide. |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2908 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 2909 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame^] | 2910 | Constant *X = ConstantInt::get(getContext(), |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2911 | APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2912 | return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2913 | } |
| 2914 | break; |
| 2915 | |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2916 | case Instruction::AShr: |
| 2917 | // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression. |
| 2918 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) |
| 2919 | if (Instruction *L = dyn_cast<Instruction>(U->getOperand(0))) |
| 2920 | if (L->getOpcode() == Instruction::Shl && |
| 2921 | L->getOperand(1) == U->getOperand(1)) { |
Dan Gohman | 91ae1e7 | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2922 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2923 | uint64_t Amt = BitWidth - CI->getZExtValue(); |
| 2924 | if (Amt == BitWidth) |
| 2925 | return getSCEV(L->getOperand(0)); // shift by zero --> noop |
| 2926 | if (Amt > BitWidth) |
| 2927 | return getIntegerSCEV(0, U->getType()); // value is undefined |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2928 | return |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2929 | getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)), |
Dan Gohman | 91ae1e7 | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2930 | IntegerType::get(Amt)), |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2931 | U->getType()); |
| 2932 | } |
| 2933 | break; |
| 2934 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2935 | case Instruction::Trunc: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2936 | return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2937 | |
| 2938 | case Instruction::ZExt: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2939 | return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2940 | |
| 2941 | case Instruction::SExt: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2942 | return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2943 | |
| 2944 | case Instruction::BitCast: |
| 2945 | // BitCasts are no-op casts so we just eliminate the cast. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2946 | if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2947 | return getSCEV(U->getOperand(0)); |
| 2948 | break; |
| 2949 | |
Dan Gohman | 2ec15e6 | 2009-07-20 17:43:30 +0000 | [diff] [blame] | 2950 | // It's tempting to handle inttoptr and ptrtoint, however this can |
| 2951 | // lead to pointer expressions which cannot be expanded to GEPs |
| 2952 | // (because they may overflow). For now, the only pointer-typed |
| 2953 | // expressions we handle are GEPs and address literals. |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2954 | |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2955 | case Instruction::GetElementPtr: |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2956 | if (!TD) break; // Without TD we can't analyze pointers. |
Dan Gohman | ca5a39e | 2009-05-08 20:58:38 +0000 | [diff] [blame] | 2957 | return createNodeForGEP(U); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2958 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2959 | case Instruction::PHI: |
| 2960 | return createNodeForPHI(cast<PHINode>(U)); |
| 2961 | |
| 2962 | case Instruction::Select: |
| 2963 | // This could be a smax or umax that was lowered earlier. |
| 2964 | // Try to recover it. |
| 2965 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) { |
| 2966 | Value *LHS = ICI->getOperand(0); |
| 2967 | Value *RHS = ICI->getOperand(1); |
| 2968 | switch (ICI->getPredicate()) { |
| 2969 | case ICmpInst::ICMP_SLT: |
| 2970 | case ICmpInst::ICMP_SLE: |
| 2971 | std::swap(LHS, RHS); |
| 2972 | // fall through |
| 2973 | case ICmpInst::ICMP_SGT: |
| 2974 | case ICmpInst::ICMP_SGE: |
| 2975 | if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2976 | return getSMaxExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2977 | else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) |
Dan Gohman | d01fff8 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2978 | return getSMinExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2979 | break; |
| 2980 | case ICmpInst::ICMP_ULT: |
| 2981 | case ICmpInst::ICMP_ULE: |
| 2982 | std::swap(LHS, RHS); |
| 2983 | // fall through |
| 2984 | case ICmpInst::ICMP_UGT: |
| 2985 | case ICmpInst::ICMP_UGE: |
| 2986 | if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2987 | return getUMaxExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2988 | else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) |
Dan Gohman | d01fff8 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2989 | return getUMinExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2990 | break; |
Dan Gohman | f27dc69 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 2991 | case ICmpInst::ICMP_NE: |
| 2992 | // n != 0 ? n : 1 -> umax(n, 1) |
| 2993 | if (LHS == U->getOperand(1) && |
| 2994 | isa<ConstantInt>(U->getOperand(2)) && |
| 2995 | cast<ConstantInt>(U->getOperand(2))->isOne() && |
| 2996 | isa<ConstantInt>(RHS) && |
| 2997 | cast<ConstantInt>(RHS)->isZero()) |
| 2998 | return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(2))); |
| 2999 | break; |
| 3000 | case ICmpInst::ICMP_EQ: |
| 3001 | // n == 0 ? 1 : n -> umax(n, 1) |
| 3002 | if (LHS == U->getOperand(2) && |
| 3003 | isa<ConstantInt>(U->getOperand(1)) && |
| 3004 | cast<ConstantInt>(U->getOperand(1))->isOne() && |
| 3005 | isa<ConstantInt>(RHS) && |
| 3006 | cast<ConstantInt>(RHS)->isZero()) |
| 3007 | return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(1))); |
| 3008 | break; |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3009 | default: |
| 3010 | break; |
| 3011 | } |
| 3012 | } |
| 3013 | |
| 3014 | default: // We cannot analyze this expression. |
| 3015 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3018 | return getUnknown(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | |
| 3022 | |
| 3023 | //===----------------------------------------------------------------------===// |
| 3024 | // Iteration Count Computation Code |
| 3025 | // |
| 3026 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3027 | /// getBackedgeTakenCount - If the specified loop has a predictable |
| 3028 | /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute |
| 3029 | /// object. The backedge-taken count is the number of times the loop header |
| 3030 | /// will be branched to from within the loop. This is one less than the |
| 3031 | /// trip count of the loop, since it doesn't count the first iteration, |
| 3032 | /// when the header is branched to from outside the loop. |
| 3033 | /// |
| 3034 | /// Note that it is not valid to call this method on a loop without a |
| 3035 | /// loop-invariant backedge-taken count (see |
| 3036 | /// hasLoopInvariantBackedgeTakenCount). |
| 3037 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3038 | const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3039 | return getBackedgeTakenInfo(L).Exact; |
| 3040 | } |
| 3041 | |
| 3042 | /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except |
| 3043 | /// return the least SCEV value that is known never to be less than the |
| 3044 | /// actual backedge taken count. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3045 | const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3046 | return getBackedgeTakenInfo(L).Max; |
| 3047 | } |
| 3048 | |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3049 | /// PushLoopPHIs - Push PHI nodes in the header of the given loop |
| 3050 | /// onto the given Worklist. |
| 3051 | static void |
| 3052 | PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) { |
| 3053 | BasicBlock *Header = L->getHeader(); |
| 3054 | |
| 3055 | // Push all Loop-header PHIs onto the Worklist stack. |
| 3056 | for (BasicBlock::iterator I = Header->begin(); |
| 3057 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 3058 | Worklist.push_back(PN); |
| 3059 | } |
| 3060 | |
| 3061 | /// PushDefUseChildren - Push users of the given Instruction |
| 3062 | /// onto the given Worklist. |
| 3063 | static void |
| 3064 | PushDefUseChildren(Instruction *I, |
| 3065 | SmallVectorImpl<Instruction *> &Worklist) { |
| 3066 | // Push the def-use children onto the Worklist stack. |
| 3067 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 3068 | UI != UE; ++UI) |
| 3069 | Worklist.push_back(cast<Instruction>(UI)); |
| 3070 | } |
| 3071 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3072 | const ScalarEvolution::BackedgeTakenInfo & |
| 3073 | ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3074 | // Initially insert a CouldNotCompute for this loop. If the insertion |
| 3075 | // succeeds, procede to actually compute a backedge-taken count and |
| 3076 | // update the value. The temporary CouldNotCompute value tells SCEV |
| 3077 | // code elsewhere that it shouldn't attempt to request a new |
| 3078 | // backedge-taken count, which could result in infinite recursion. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3079 | std::pair<std::map<const Loop*, BackedgeTakenInfo>::iterator, bool> Pair = |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3080 | BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute())); |
| 3081 | if (Pair.second) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3082 | BackedgeTakenInfo ItCount = ComputeBackedgeTakenCount(L); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3083 | if (ItCount.Exact != getCouldNotCompute()) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3084 | assert(ItCount.Exact->isLoopInvariant(L) && |
| 3085 | ItCount.Max->isLoopInvariant(L) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3086 | "Computed trip count isn't loop invariant for loop!"); |
| 3087 | ++NumTripCountsComputed; |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3088 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3089 | // Update the value in the map. |
| 3090 | Pair.first->second = ItCount; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3091 | } else { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3092 | if (ItCount.Max != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3093 | // Update the value in the map. |
| 3094 | Pair.first->second = ItCount; |
| 3095 | if (isa<PHINode>(L->getHeader()->begin())) |
| 3096 | // Only count loops that have phi nodes as not being computable. |
| 3097 | ++NumTripCountsNotComputed; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3098 | } |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3099 | |
| 3100 | // Now that we know more about the trip count for this loop, forget any |
| 3101 | // 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] | 3102 | // conservative estimates made without the benefit of trip count |
| 3103 | // information. This is similar to the code in |
| 3104 | // forgetLoopBackedgeTakenCount, except that it handles SCEVUnknown PHI |
| 3105 | // nodes specially. |
| 3106 | if (ItCount.hasAnyInfo()) { |
| 3107 | SmallVector<Instruction *, 16> Worklist; |
| 3108 | PushLoopPHIs(L, Worklist); |
| 3109 | |
| 3110 | SmallPtrSet<Instruction *, 8> Visited; |
| 3111 | while (!Worklist.empty()) { |
| 3112 | Instruction *I = Worklist.pop_back_val(); |
| 3113 | if (!Visited.insert(I)) continue; |
| 3114 | |
| 3115 | std::map<SCEVCallbackVH, const SCEV*>::iterator It = |
| 3116 | Scalars.find(static_cast<Value *>(I)); |
| 3117 | if (It != Scalars.end()) { |
| 3118 | // SCEVUnknown for a PHI either means that it has an unrecognized |
| 3119 | // structure, or it's a PHI that's in the progress of being computed |
Dan Gohman | 0fa91f3 | 2009-07-13 22:04:06 +0000 | [diff] [blame] | 3120 | // by createNodeForPHI. In the former case, additional loop trip |
| 3121 | // count information isn't going to change anything. In the later |
| 3122 | // case, createNodeForPHI will perform the necessary updates on its |
| 3123 | // own when it gets to that point. |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3124 | if (!isa<PHINode>(I) || !isa<SCEVUnknown>(It->second)) |
| 3125 | Scalars.erase(It); |
| 3126 | ValuesAtScopes.erase(I); |
| 3127 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3128 | ConstantEvolutionLoopExitValue.erase(PN); |
| 3129 | } |
| 3130 | |
| 3131 | PushDefUseChildren(I, Worklist); |
| 3132 | } |
| 3133 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3134 | } |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3135 | return Pair.first->second; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3138 | /// forgetLoopBackedgeTakenCount - This method should be called by the |
Dan Gohman | f3a060a | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 3139 | /// 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] | 3140 | /// ScalarEvolution's ability to compute a trip count, or if the loop |
| 3141 | /// is deleted. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3142 | void ScalarEvolution::forgetLoopBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3143 | BackedgeTakenCounts.erase(L); |
Dan Gohman | 9462302 | 2009-05-02 17:43:35 +0000 | [diff] [blame] | 3144 | |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3145 | SmallVector<Instruction *, 16> Worklist; |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3146 | PushLoopPHIs(L, Worklist); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3147 | |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3148 | SmallPtrSet<Instruction *, 8> Visited; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3149 | while (!Worklist.empty()) { |
| 3150 | Instruction *I = Worklist.pop_back_val(); |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3151 | if (!Visited.insert(I)) continue; |
| 3152 | |
| 3153 | std::map<SCEVCallbackVH, const SCEV*>::iterator It = |
| 3154 | Scalars.find(static_cast<Value *>(I)); |
| 3155 | if (It != Scalars.end()) { |
| 3156 | Scalars.erase(It); |
| 3157 | ValuesAtScopes.erase(I); |
| 3158 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3159 | ConstantEvolutionLoopExitValue.erase(PN); |
| 3160 | } |
| 3161 | |
| 3162 | PushDefUseChildren(I, Worklist); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3163 | } |
Dan Gohman | f3a060a | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3166 | /// ComputeBackedgeTakenCount - Compute the number of times the backedge |
| 3167 | /// of the specified loop will execute. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3168 | ScalarEvolution::BackedgeTakenInfo |
| 3169 | ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3170 | SmallVector<BasicBlock*, 8> ExitingBlocks; |
| 3171 | L->getExitingBlocks(ExitingBlocks); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3172 | |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3173 | // Examine all exits and pick the most conservative values. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3174 | const SCEV *BECount = getCouldNotCompute(); |
| 3175 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3176 | bool CouldNotComputeBECount = false; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3177 | for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { |
| 3178 | BackedgeTakenInfo NewBTI = |
| 3179 | ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3180 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3181 | if (NewBTI.Exact == getCouldNotCompute()) { |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3182 | // We couldn't compute an exact value for this exit, so |
Dan Gohman | c6e8c83 | 2009-06-22 21:10:22 +0000 | [diff] [blame] | 3183 | // 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] | 3184 | CouldNotComputeBECount = true; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3185 | BECount = getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3186 | } else if (!CouldNotComputeBECount) { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3187 | if (BECount == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3188 | BECount = NewBTI.Exact; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3189 | else |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3190 | BECount = getUMinFromMismatchedTypes(BECount, NewBTI.Exact); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3191 | } |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3192 | if (MaxBECount == getCouldNotCompute()) |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3193 | MaxBECount = NewBTI.Max; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3194 | else if (NewBTI.Max != getCouldNotCompute()) |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3195 | MaxBECount = getUMinFromMismatchedTypes(MaxBECount, NewBTI.Max); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3199 | } |
| 3200 | |
| 3201 | /// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge |
| 3202 | /// of the specified loop will execute if it exits via the specified block. |
| 3203 | ScalarEvolution::BackedgeTakenInfo |
| 3204 | ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L, |
| 3205 | BasicBlock *ExitingBlock) { |
| 3206 | |
| 3207 | // Okay, we've chosen an exiting block. See what condition causes us to |
| 3208 | // exit at this block. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3209 | // |
| 3210 | // FIXME: we should be able to handle switch instructions (with a single exit) |
| 3211 | BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3212 | if (ExitBr == 0) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3213 | assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!"); |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3214 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3215 | // At this point, we know we have a conditional branch that determines whether |
| 3216 | // the loop is exited. However, we don't know if the branch is executed each |
| 3217 | // time through the loop. If not, then the execution count of the branch will |
| 3218 | // not be equal to the trip count of the loop. |
| 3219 | // |
| 3220 | // Currently we check for this by checking to see if the Exit branch goes to |
| 3221 | // the loop header. If so, we know it will always execute the same number of |
| 3222 | // 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] | 3223 | // loop header. This is common for un-rotated loops. |
| 3224 | // |
| 3225 | // If both of those tests fail, walk up the unique predecessor chain to the |
| 3226 | // header, stopping if there is an edge that doesn't exit the loop. If the |
| 3227 | // header is reached, the execution count of the branch will be equal to the |
| 3228 | // trip count of the loop. |
| 3229 | // |
| 3230 | // More extensive analysis could be done to handle more cases here. |
| 3231 | // |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3232 | if (ExitBr->getSuccessor(0) != L->getHeader() && |
| 3233 | ExitBr->getSuccessor(1) != L->getHeader() && |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3234 | ExitBr->getParent() != L->getHeader()) { |
| 3235 | // The simple checks failed, try climbing the unique predecessor chain |
| 3236 | // up to the header. |
| 3237 | bool Ok = false; |
| 3238 | for (BasicBlock *BB = ExitBr->getParent(); BB; ) { |
| 3239 | BasicBlock *Pred = BB->getUniquePredecessor(); |
| 3240 | if (!Pred) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3241 | return getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3242 | TerminatorInst *PredTerm = Pred->getTerminator(); |
| 3243 | for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) { |
| 3244 | BasicBlock *PredSucc = PredTerm->getSuccessor(i); |
| 3245 | if (PredSucc == BB) |
| 3246 | continue; |
| 3247 | // If the predecessor has a successor that isn't BB and isn't |
| 3248 | // outside the loop, assume the worst. |
| 3249 | if (L->contains(PredSucc)) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3250 | return getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3251 | } |
| 3252 | if (Pred == L->getHeader()) { |
| 3253 | Ok = true; |
| 3254 | break; |
| 3255 | } |
| 3256 | BB = Pred; |
| 3257 | } |
| 3258 | if (!Ok) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3259 | return getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | // Procede to the next level to examine the exit condition expression. |
| 3263 | return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(), |
| 3264 | ExitBr->getSuccessor(0), |
| 3265 | ExitBr->getSuccessor(1)); |
| 3266 | } |
| 3267 | |
| 3268 | /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the |
| 3269 | /// backedge of the specified loop will execute if its exit condition |
| 3270 | /// were a conditional branch of ExitCond, TBB, and FBB. |
| 3271 | ScalarEvolution::BackedgeTakenInfo |
| 3272 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L, |
| 3273 | Value *ExitCond, |
| 3274 | BasicBlock *TBB, |
| 3275 | BasicBlock *FBB) { |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3276 | // 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] | 3277 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) { |
| 3278 | if (BO->getOpcode() == Instruction::And) { |
| 3279 | // Recurse on the operands of the and. |
| 3280 | BackedgeTakenInfo BTI0 = |
| 3281 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 3282 | BackedgeTakenInfo BTI1 = |
| 3283 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3284 | const SCEV *BECount = getCouldNotCompute(); |
| 3285 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3286 | if (L->contains(TBB)) { |
| 3287 | // Both conditions must be true for the loop to continue executing. |
| 3288 | // Choose the less conservative count. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3289 | if (BTI0.Exact == getCouldNotCompute() || |
| 3290 | BTI1.Exact == getCouldNotCompute()) |
| 3291 | BECount = getCouldNotCompute(); |
Dan Gohman | ac958b3 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3292 | else |
| 3293 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3294 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3295 | MaxBECount = BTI1.Max; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3296 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3297 | MaxBECount = BTI0.Max; |
Dan Gohman | ac958b3 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3298 | else |
| 3299 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3300 | } else { |
| 3301 | // Both conditions must be true for the loop to exit. |
| 3302 | assert(L->contains(FBB) && "Loop block has no successor in loop!"); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3303 | if (BTI0.Exact != getCouldNotCompute() && |
| 3304 | BTI1.Exact != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3305 | BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3306 | if (BTI0.Max != getCouldNotCompute() && |
| 3307 | BTI1.Max != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3308 | MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max); |
| 3309 | } |
| 3310 | |
| 3311 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3312 | } |
| 3313 | if (BO->getOpcode() == Instruction::Or) { |
| 3314 | // Recurse on the operands of the or. |
| 3315 | BackedgeTakenInfo BTI0 = |
| 3316 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 3317 | BackedgeTakenInfo BTI1 = |
| 3318 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3319 | const SCEV *BECount = getCouldNotCompute(); |
| 3320 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3321 | if (L->contains(FBB)) { |
| 3322 | // Both conditions must be false for the loop to continue executing. |
| 3323 | // Choose the less conservative count. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3324 | if (BTI0.Exact == getCouldNotCompute() || |
| 3325 | BTI1.Exact == getCouldNotCompute()) |
| 3326 | BECount = getCouldNotCompute(); |
Dan Gohman | ac958b3 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3327 | else |
| 3328 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3329 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3330 | MaxBECount = BTI1.Max; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3331 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3332 | MaxBECount = BTI0.Max; |
Dan Gohman | ac958b3 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3333 | else |
| 3334 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3335 | } else { |
| 3336 | // Both conditions must be false for the loop to exit. |
| 3337 | assert(L->contains(TBB) && "Loop block has no successor in loop!"); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3338 | if (BTI0.Exact != getCouldNotCompute() && |
| 3339 | BTI1.Exact != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3340 | BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3341 | if (BTI0.Max != getCouldNotCompute() && |
| 3342 | BTI1.Max != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3343 | MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max); |
| 3344 | } |
| 3345 | |
| 3346 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3347 | } |
| 3348 | } |
| 3349 | |
| 3350 | // With an icmp, it may be feasible to compute an exact backedge-taken count. |
| 3351 | // Procede to the next level to examine the icmp. |
| 3352 | if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) |
| 3353 | return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3354 | |
Eli Friedman | 459d729 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3355 | // 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] | 3356 | return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
| 3357 | } |
| 3358 | |
| 3359 | /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the |
| 3360 | /// backedge of the specified loop will execute if its exit condition |
| 3361 | /// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB. |
| 3362 | ScalarEvolution::BackedgeTakenInfo |
| 3363 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L, |
| 3364 | ICmpInst *ExitCond, |
| 3365 | BasicBlock *TBB, |
| 3366 | BasicBlock *FBB) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3367 | |
| 3368 | // If the condition was exit on true, convert the condition to exit on false |
| 3369 | ICmpInst::Predicate Cond; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3370 | if (!L->contains(FBB)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3371 | Cond = ExitCond->getPredicate(); |
| 3372 | else |
| 3373 | Cond = ExitCond->getInversePredicate(); |
| 3374 | |
| 3375 | // Handle common loops like: for (X = "string"; *X; ++X) |
| 3376 | if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0))) |
| 3377 | if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3378 | const SCEV *ItCnt = |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3379 | ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3380 | if (!isa<SCEVCouldNotCompute>(ItCnt)) { |
| 3381 | unsigned BitWidth = getTypeSizeInBits(ItCnt->getType()); |
| 3382 | return BackedgeTakenInfo(ItCnt, |
| 3383 | isa<SCEVConstant>(ItCnt) ? ItCnt : |
| 3384 | getConstant(APInt::getMaxValue(BitWidth)-1)); |
| 3385 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3388 | const SCEV *LHS = getSCEV(ExitCond->getOperand(0)); |
| 3389 | const SCEV *RHS = getSCEV(ExitCond->getOperand(1)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3390 | |
| 3391 | // Try to evaluate any dependencies out of the loop. |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3392 | LHS = getSCEVAtScope(LHS, L); |
| 3393 | RHS = getSCEVAtScope(RHS, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3394 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3395 | // 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] | 3396 | // loop the predicate will return true for these inputs. |
Dan Gohman | 2d96e35 | 2008-09-16 18:52:57 +0000 | [diff] [blame] | 3397 | if (LHS->isLoopInvariant(L) && !RHS->isLoopInvariant(L)) { |
| 3398 | // If there is a loop-invariant, force it into the RHS. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3399 | std::swap(LHS, RHS); |
| 3400 | Cond = ICmpInst::getSwappedPredicate(Cond); |
| 3401 | } |
| 3402 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3403 | // If we have a comparison of a chrec against a constant, try to use value |
| 3404 | // ranges to answer this query. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3405 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) |
| 3406 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3407 | if (AddRec->getLoop() == L) { |
Eli Friedman | 459d729 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3408 | // Form the constant range. |
| 3409 | ConstantRange CompRange( |
| 3410 | ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue())); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3411 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3412 | const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this); |
Eli Friedman | 459d729 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3413 | if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
| 3416 | switch (Cond) { |
| 3417 | case ICmpInst::ICMP_NE: { // while (X != Y) |
| 3418 | // Convert to: while (X-Y != 0) |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3419 | const SCEV *TC = HowFarToZero(getMinusSCEV(LHS, RHS), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3420 | if (!isa<SCEVCouldNotCompute>(TC)) return TC; |
| 3421 | break; |
| 3422 | } |
| 3423 | case ICmpInst::ICMP_EQ: { |
| 3424 | // Convert to: while (X-Y == 0) // while (X == Y) |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3425 | const SCEV *TC = HowFarToNonZero(getMinusSCEV(LHS, RHS), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3426 | if (!isa<SCEVCouldNotCompute>(TC)) return TC; |
| 3427 | break; |
| 3428 | } |
| 3429 | case ICmpInst::ICMP_SLT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3430 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true); |
| 3431 | if (BTI.hasAnyInfo()) return BTI; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3432 | break; |
| 3433 | } |
| 3434 | case ICmpInst::ICMP_SGT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3435 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 3436 | getNotSCEV(RHS), L, true); |
| 3437 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | b7c2894 | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 3438 | break; |
| 3439 | } |
| 3440 | case ICmpInst::ICMP_ULT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3441 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false); |
| 3442 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | b7c2894 | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 3443 | break; |
| 3444 | } |
| 3445 | case ICmpInst::ICMP_UGT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3446 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 3447 | getNotSCEV(RHS), L, false); |
| 3448 | if (BTI.hasAnyInfo()) return BTI; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3449 | break; |
| 3450 | } |
| 3451 | default: |
| 3452 | #if 0 |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3453 | errs() << "ComputeBackedgeTakenCount "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3454 | if (ExitCond->getOperand(0)->getType()->isUnsigned()) |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3455 | errs() << "[unsigned] "; |
| 3456 | errs() << *LHS << " " |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3457 | << Instruction::getOpcodeName(Instruction::ICmp) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3458 | << " " << *RHS << "\n"; |
| 3459 | #endif |
| 3460 | break; |
| 3461 | } |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3462 | return |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3463 | ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3464 | } |
| 3465 | |
| 3466 | static ConstantInt * |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3467 | EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, |
| 3468 | ScalarEvolution &SE) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3469 | const SCEV *InVal = SE.getConstant(C); |
| 3470 | const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3471 | assert(isa<SCEVConstant>(Val) && |
| 3472 | "Evaluation of SCEV at constant didn't fold correctly?"); |
| 3473 | return cast<SCEVConstant>(Val)->getValue(); |
| 3474 | } |
| 3475 | |
| 3476 | /// GetAddressedElementFromGlobal - Given a global variable with an initializer |
| 3477 | /// and a GEP expression (missing the pointer index) indexing into it, return |
| 3478 | /// the addressed element of the initializer or null if the index expression is |
| 3479 | /// invalid. |
| 3480 | static Constant * |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3481 | GetAddressedElementFromGlobal(LLVMContext &Context, GlobalVariable *GV, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3482 | const std::vector<ConstantInt*> &Indices) { |
| 3483 | Constant *Init = GV->getInitializer(); |
| 3484 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) { |
| 3485 | uint64_t Idx = Indices[i]->getZExtValue(); |
| 3486 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) { |
| 3487 | assert(Idx < CS->getNumOperands() && "Bad struct index!"); |
| 3488 | Init = cast<Constant>(CS->getOperand(Idx)); |
| 3489 | } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) { |
| 3490 | if (Idx >= CA->getNumOperands()) return 0; // Bogus program |
| 3491 | Init = cast<Constant>(CA->getOperand(Idx)); |
| 3492 | } else if (isa<ConstantAggregateZero>(Init)) { |
| 3493 | if (const StructType *STy = dyn_cast<StructType>(Init->getType())) { |
| 3494 | assert(Idx < STy->getNumElements() && "Bad struct index!"); |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3495 | Init = Context.getNullValue(STy->getElementType(Idx)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3496 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) { |
| 3497 | if (Idx >= ATy->getNumElements()) return 0; // Bogus program |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3498 | Init = Context.getNullValue(ATy->getElementType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3499 | } else { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3500 | llvm_unreachable("Unknown constant aggregate type!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3501 | } |
| 3502 | return 0; |
| 3503 | } else { |
| 3504 | return 0; // Unknown initializer type |
| 3505 | } |
| 3506 | } |
| 3507 | return Init; |
| 3508 | } |
| 3509 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3510 | /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of |
| 3511 | /// 'icmp op load X, cst', try to see if we can compute the backedge |
| 3512 | /// execution count. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3513 | const SCEV * |
| 3514 | ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount( |
| 3515 | LoadInst *LI, |
| 3516 | Constant *RHS, |
| 3517 | const Loop *L, |
| 3518 | ICmpInst::Predicate predicate) { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3519 | if (LI->isVolatile()) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3520 | |
| 3521 | // Check to see if the loaded pointer is a getelementptr of a global. |
| 3522 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3523 | if (!GEP) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3524 | |
| 3525 | // Make sure that it is really a constant global we are gepping, with an |
| 3526 | // initializer, and make sure the first IDX is really 0. |
| 3527 | GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); |
| 3528 | if (!GV || !GV->isConstant() || !GV->hasInitializer() || |
| 3529 | GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) || |
| 3530 | !cast<Constant>(GEP->getOperand(1))->isNullValue()) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3531 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3532 | |
| 3533 | // Okay, we allow one non-constant index into the GEP instruction. |
| 3534 | Value *VarIdx = 0; |
| 3535 | std::vector<ConstantInt*> Indexes; |
| 3536 | unsigned VarIdxNum = 0; |
| 3537 | for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) |
| 3538 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 3539 | Indexes.push_back(CI); |
| 3540 | } else if (!isa<ConstantInt>(GEP->getOperand(i))) { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3541 | if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3542 | VarIdx = GEP->getOperand(i); |
| 3543 | VarIdxNum = i-2; |
| 3544 | Indexes.push_back(0); |
| 3545 | } |
| 3546 | |
| 3547 | // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant. |
| 3548 | // Check to see if X is a loop variant variable value now. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3549 | const SCEV *Idx = getSCEV(VarIdx); |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3550 | Idx = getSCEVAtScope(Idx, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3551 | |
| 3552 | // We can only recognize very limited forms of loop index expressions, in |
| 3553 | // particular, only affine AddRec's like {C1,+,C2}. |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3554 | const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3555 | if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) || |
| 3556 | !isa<SCEVConstant>(IdxExpr->getOperand(0)) || |
| 3557 | !isa<SCEVConstant>(IdxExpr->getOperand(1))) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3558 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3559 | |
| 3560 | unsigned MaxSteps = MaxBruteForceIterations; |
| 3561 | for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) { |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame^] | 3562 | ConstantInt *ItCst = ConstantInt::get( |
Owen Anderson | 9f5b2aa | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 3563 | cast<IntegerType>(IdxExpr->getType()), IterationNum); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3564 | ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3565 | |
| 3566 | // Form the GEP offset. |
| 3567 | Indexes[VarIdxNum] = Val; |
| 3568 | |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3569 | Constant *Result = GetAddressedElementFromGlobal(getContext(), GV, Indexes); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3570 | if (Result == 0) break; // Cannot compute! |
| 3571 | |
| 3572 | // Evaluate the condition for this iteration. |
| 3573 | Result = ConstantExpr::getICmp(predicate, Result, RHS); |
| 3574 | if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure |
| 3575 | if (cast<ConstantInt>(Result)->getValue().isMinValue()) { |
| 3576 | #if 0 |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3577 | errs() << "\n***\n*** Computed loop count " << *ItCst |
| 3578 | << "\n*** From global " << *GV << "*** BB: " << *L->getHeader() |
| 3579 | << "***\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3580 | #endif |
| 3581 | ++NumArrayLenItCounts; |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3582 | return getConstant(ItCst); // Found terminating iteration! |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3583 | } |
| 3584 | } |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3585 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
| 3588 | |
| 3589 | /// CanConstantFold - Return true if we can constant fold an instruction of the |
| 3590 | /// specified type, assuming that all operands were constants. |
| 3591 | static bool CanConstantFold(const Instruction *I) { |
| 3592 | if (isa<BinaryOperator>(I) || isa<CmpInst>(I) || |
| 3593 | isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I)) |
| 3594 | return true; |
| 3595 | |
| 3596 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 3597 | if (const Function *F = CI->getCalledFunction()) |
Dan Gohman | e6e001f | 2008-01-31 01:05:10 +0000 | [diff] [blame] | 3598 | return canConstantFoldCallTo(F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3599 | return false; |
| 3600 | } |
| 3601 | |
| 3602 | /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node |
| 3603 | /// in the loop that V is derived from. We allow arbitrary operations along the |
| 3604 | /// way, but the operands of an operation must either be constants or a value |
| 3605 | /// derived from a constant PHI. If this expression does not fit with these |
| 3606 | /// constraints, return null. |
| 3607 | static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) { |
| 3608 | // If this is not an instruction, or if this is an instruction outside of the |
| 3609 | // loop, it can't be derived from a loop PHI. |
| 3610 | Instruction *I = dyn_cast<Instruction>(V); |
| 3611 | if (I == 0 || !L->contains(I->getParent())) return 0; |
| 3612 | |
Anton Korobeynikov | 357a27d | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 3613 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3614 | if (L->getHeader() == I->getParent()) |
| 3615 | return PN; |
| 3616 | else |
| 3617 | // We don't currently keep track of the control flow needed to evaluate |
| 3618 | // PHIs, so we cannot handle PHIs inside of loops. |
| 3619 | return 0; |
Anton Korobeynikov | 357a27d | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 3620 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3621 | |
| 3622 | // If we won't be able to constant fold this expression even if the operands |
| 3623 | // are constants, return early. |
| 3624 | if (!CanConstantFold(I)) return 0; |
| 3625 | |
| 3626 | // Otherwise, we can evaluate this instruction if all of its operands are |
| 3627 | // constant or derived from a PHI node themselves. |
| 3628 | PHINode *PHI = 0; |
| 3629 | for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op) |
| 3630 | if (!(isa<Constant>(I->getOperand(Op)) || |
| 3631 | isa<GlobalValue>(I->getOperand(Op)))) { |
| 3632 | PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L); |
| 3633 | if (P == 0) return 0; // Not evolving from PHI |
| 3634 | if (PHI == 0) |
| 3635 | PHI = P; |
| 3636 | else if (PHI != P) |
| 3637 | return 0; // Evolving from multiple different PHIs. |
| 3638 | } |
| 3639 | |
| 3640 | // This is a expression evolving from a constant PHI! |
| 3641 | return PHI; |
| 3642 | } |
| 3643 | |
| 3644 | /// EvaluateExpression - Given an expression that passes the |
| 3645 | /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node |
| 3646 | /// in the loop has the value PHIVal. If we can't fold this expression for some |
| 3647 | /// reason, return null. |
| 3648 | static Constant *EvaluateExpression(Value *V, Constant *PHIVal) { |
| 3649 | if (isa<PHINode>(V)) return PHIVal; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3650 | if (Constant *C = dyn_cast<Constant>(V)) return C; |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3651 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3652 | Instruction *I = cast<Instruction>(V); |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3653 | LLVMContext &Context = I->getParent()->getContext(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3654 | |
| 3655 | std::vector<Constant*> Operands; |
| 3656 | Operands.resize(I->getNumOperands()); |
| 3657 | |
| 3658 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 3659 | Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal); |
| 3660 | if (Operands[i] == 0) return 0; |
| 3661 | } |
| 3662 | |
Chris Lattner | d6e5691 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3663 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 3664 | return ConstantFoldCompareInstOperands(CI->getPredicate(), |
Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 3665 | &Operands[0], Operands.size(), |
| 3666 | Context); |
Chris Lattner | d6e5691 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3667 | else |
| 3668 | return ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 3669 | &Operands[0], Operands.size(), |
| 3670 | Context); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is |
| 3674 | /// in the header of its containing loop, we know the loop executes a |
| 3675 | /// constant number of times, and the PHI node is just a recurrence |
| 3676 | /// involving constants, fold it. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3677 | Constant * |
| 3678 | ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN, |
| 3679 | const APInt& BEs, |
| 3680 | const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3681 | std::map<PHINode*, Constant*>::iterator I = |
| 3682 | ConstantEvolutionLoopExitValue.find(PN); |
| 3683 | if (I != ConstantEvolutionLoopExitValue.end()) |
| 3684 | return I->second; |
| 3685 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3686 | if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations))) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3687 | return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it. |
| 3688 | |
| 3689 | Constant *&RetVal = ConstantEvolutionLoopExitValue[PN]; |
| 3690 | |
| 3691 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 3692 | // entry must be a constant (coming in from outside of the loop), and the |
| 3693 | // second must be derived from the same PHI. |
| 3694 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 3695 | Constant *StartCST = |
| 3696 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
| 3697 | if (StartCST == 0) |
| 3698 | return RetVal = 0; // Must be a constant. |
| 3699 | |
| 3700 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
| 3701 | PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); |
| 3702 | if (PN2 != PN) |
| 3703 | return RetVal = 0; // Not derived from same PHI. |
| 3704 | |
| 3705 | // Execute the loop symbolically to determine the exit value. |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3706 | if (BEs.getActiveBits() >= 32) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3707 | return RetVal = 0; // More than 2^32-1 iterations?? Not doing it! |
| 3708 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3709 | unsigned NumIterations = BEs.getZExtValue(); // must be in range |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3710 | unsigned IterationNum = 0; |
| 3711 | for (Constant *PHIVal = StartCST; ; ++IterationNum) { |
| 3712 | if (IterationNum == NumIterations) |
| 3713 | return RetVal = PHIVal; // Got exit value! |
| 3714 | |
| 3715 | // Compute the value of the PHI node for the next iteration. |
| 3716 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal); |
| 3717 | if (NextPHI == PHIVal) |
| 3718 | return RetVal = NextPHI; // Stopped evolving! |
| 3719 | if (NextPHI == 0) |
| 3720 | return 0; // Couldn't evaluate! |
| 3721 | PHIVal = NextPHI; |
| 3722 | } |
| 3723 | } |
| 3724 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3725 | /// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute a |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3726 | /// constant number of times (the condition evolves only from constants), |
| 3727 | /// try to evaluate a few iterations of the loop until we get the exit |
| 3728 | /// condition gets a value of ExitWhen (true or false). If we cannot |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3729 | /// evaluate the trip count of the loop, return getCouldNotCompute(). |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3730 | const SCEV * |
| 3731 | ScalarEvolution::ComputeBackedgeTakenCountExhaustively(const Loop *L, |
| 3732 | Value *Cond, |
| 3733 | bool ExitWhen) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3734 | PHINode *PN = getConstantEvolvingPHI(Cond, L); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3735 | if (PN == 0) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3736 | |
| 3737 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 3738 | // entry must be a constant (coming in from outside of the loop), and the |
| 3739 | // second must be derived from the same PHI. |
| 3740 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 3741 | Constant *StartCST = |
| 3742 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3743 | if (StartCST == 0) return getCouldNotCompute(); // Must be a constant. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3744 | |
| 3745 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
| 3746 | PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3747 | if (PN2 != PN) return getCouldNotCompute(); // Not derived from same PHI. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3748 | |
| 3749 | // Okay, we find a PHI node that defines the trip count of this loop. Execute |
| 3750 | // the loop symbolically to determine when the condition gets a value of |
| 3751 | // "ExitWhen". |
| 3752 | unsigned IterationNum = 0; |
| 3753 | unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis. |
| 3754 | for (Constant *PHIVal = StartCST; |
| 3755 | IterationNum != MaxIterations; ++IterationNum) { |
| 3756 | ConstantInt *CondVal = |
| 3757 | dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal)); |
| 3758 | |
| 3759 | // Couldn't symbolically evaluate. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3760 | if (!CondVal) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3761 | |
| 3762 | if (CondVal->getValue() == uint64_t(ExitWhen)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3763 | ++NumBruteForceTripCountsComputed; |
Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 3764 | return getConstant(Type::Int32Ty, IterationNum); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
| 3767 | // Compute the value of the PHI node for the next iteration. |
| 3768 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal); |
| 3769 | if (NextPHI == 0 || NextPHI == PHIVal) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3770 | return getCouldNotCompute();// Couldn't evaluate or not making progress... |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3771 | PHIVal = NextPHI; |
| 3772 | } |
| 3773 | |
| 3774 | // Too many iterations were needed to evaluate. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3775 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
Dan Gohman | dd40e9a | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 3778 | /// getSCEVAtScope - Return a SCEV expression handle for the specified value |
| 3779 | /// at the specified scope in the program. The L value specifies a loop |
| 3780 | /// nest to evaluate the expression at, where null is the top-level or a |
| 3781 | /// specified loop is immediately inside of the loop. |
| 3782 | /// |
| 3783 | /// This method can be used to compute the exit value for a variable defined |
| 3784 | /// in a loop by querying what the value will hold in the parent loop. |
| 3785 | /// |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3786 | /// In the case that a relevant loop exit value cannot be computed, the |
| 3787 | /// original value V is returned. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3788 | const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3789 | // FIXME: this should be turned into a virtual method on SCEV! |
| 3790 | |
| 3791 | if (isa<SCEVConstant>(V)) return V; |
| 3792 | |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 3793 | // If this instruction is evolved from a constant-evolving PHI, compute the |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3794 | // exit value from the loop without using SCEVs. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3795 | if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3796 | if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3797 | const Loop *LI = (*this->LI)[I->getParent()]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3798 | if (LI && LI->getParentLoop() == L) // Looking for loop exit value. |
| 3799 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3800 | if (PN->getParent() == LI->getHeader()) { |
| 3801 | // Okay, there is no closed form solution for the PHI node. Check |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3802 | // to see if the loop that contains it has a known backedge-taken |
| 3803 | // count. If so, we may be able to force computation of the exit |
| 3804 | // value. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3805 | const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3806 | if (const SCEVConstant *BTCC = |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3807 | dyn_cast<SCEVConstant>(BackedgeTakenCount)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3808 | // Okay, we know how many times the containing loop executes. If |
| 3809 | // this is a constant evolving PHI node, get the final value at |
| 3810 | // the specified iteration number. |
| 3811 | Constant *RV = getConstantEvolutionLoopExitValue(PN, |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3812 | BTCC->getValue()->getValue(), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3813 | LI); |
Dan Gohman | 652caf1 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3814 | if (RV) return getSCEV(RV); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3815 | } |
| 3816 | } |
| 3817 | |
| 3818 | // Okay, this is an expression that we cannot symbolically evaluate |
| 3819 | // into a SCEV. Check to see if it's possible to symbolically evaluate |
| 3820 | // the arguments into constants, and if so, try to constant propagate the |
| 3821 | // result. This is particularly useful for computing loop exit values. |
| 3822 | if (CanConstantFold(I)) { |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3823 | // Check to see if we've folded this instruction at this loop before. |
| 3824 | std::map<const Loop *, Constant *> &Values = ValuesAtScopes[I]; |
| 3825 | std::pair<std::map<const Loop *, Constant *>::iterator, bool> Pair = |
| 3826 | Values.insert(std::make_pair(L, static_cast<Constant *>(0))); |
| 3827 | if (!Pair.second) |
Dan Gohman | 652caf1 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3828 | return Pair.first->second ? &*getSCEV(Pair.first->second) : V; |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3829 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3830 | std::vector<Constant*> Operands; |
| 3831 | Operands.reserve(I->getNumOperands()); |
| 3832 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 3833 | Value *Op = I->getOperand(i); |
| 3834 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| 3835 | Operands.push_back(C); |
| 3836 | } else { |
Chris Lattner | 3fff464 | 2007-11-23 08:46:22 +0000 | [diff] [blame] | 3837 | // If any of the operands is non-constant and if they are |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3838 | // non-integer and non-pointer, don't even try to analyze them |
| 3839 | // with scev techniques. |
Dan Gohman | 5e4eb76 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3840 | if (!isSCEVable(Op->getType())) |
Chris Lattner | 3fff464 | 2007-11-23 08:46:22 +0000 | [diff] [blame] | 3841 | return V; |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3842 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3843 | const SCEV* OpV = getSCEVAtScope(Op, L); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3844 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) { |
Dan Gohman | 5e4eb76 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3845 | Constant *C = SC->getValue(); |
| 3846 | if (C->getType() != Op->getType()) |
| 3847 | C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 3848 | Op->getType(), |
| 3849 | false), |
| 3850 | C, Op->getType()); |
| 3851 | Operands.push_back(C); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3852 | } else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) { |
Dan Gohman | 5e4eb76 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3853 | if (Constant *C = dyn_cast<Constant>(SU->getValue())) { |
| 3854 | if (C->getType() != Op->getType()) |
| 3855 | C = |
| 3856 | ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 3857 | Op->getType(), |
| 3858 | false), |
| 3859 | C, Op->getType()); |
| 3860 | Operands.push_back(C); |
| 3861 | } else |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3862 | return V; |
| 3863 | } else { |
| 3864 | return V; |
| 3865 | } |
| 3866 | } |
| 3867 | } |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3868 | |
Chris Lattner | d6e5691 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3869 | Constant *C; |
| 3870 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 3871 | C = ConstantFoldCompareInstOperands(CI->getPredicate(), |
Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 3872 | &Operands[0], Operands.size(), |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3873 | getContext()); |
Chris Lattner | d6e5691 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3874 | else |
| 3875 | C = ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3876 | &Operands[0], Operands.size(), |
| 3877 | getContext()); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3878 | Pair.first->second = C; |
Dan Gohman | 652caf1 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3879 | return getSCEV(C); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3880 | } |
| 3881 | } |
| 3882 | |
| 3883 | // This is some other type of SCEVUnknown, just return it. |
| 3884 | return V; |
| 3885 | } |
| 3886 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3887 | if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3888 | // Avoid performing the look-up in the common case where the specified |
| 3889 | // expression has no loop-variant portions. |
| 3890 | for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3891 | const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3892 | if (OpAtScope != Comm->getOperand(i)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3893 | // Okay, at least one of these operands is loop variant but might be |
| 3894 | // foldable. Build a new instance of the folded commutative expression. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3895 | SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(), |
| 3896 | Comm->op_begin()+i); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3897 | NewOps.push_back(OpAtScope); |
| 3898 | |
| 3899 | for (++i; i != e; ++i) { |
| 3900 | OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3901 | NewOps.push_back(OpAtScope); |
| 3902 | } |
| 3903 | if (isa<SCEVAddExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3904 | return getAddExpr(NewOps); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 3905 | if (isa<SCEVMulExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3906 | return getMulExpr(NewOps); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 3907 | if (isa<SCEVSMaxExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3908 | return getSMaxExpr(NewOps); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 3909 | if (isa<SCEVUMaxExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3910 | return getUMaxExpr(NewOps); |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3911 | llvm_unreachable("Unknown commutative SCEV type!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3912 | } |
| 3913 | } |
| 3914 | // If we got here, all operands are loop invariant. |
| 3915 | return Comm; |
| 3916 | } |
| 3917 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3918 | if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3919 | const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L); |
| 3920 | const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L); |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 3921 | if (LHS == Div->getLHS() && RHS == Div->getRHS()) |
| 3922 | return Div; // must be loop invariant |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3923 | return getUDivExpr(LHS, RHS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
| 3926 | // If this is a loop recurrence for a loop that does not contain L, then we |
| 3927 | // are dealing with the final value computed by the loop. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3928 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3929 | if (!L || !AddRec->getLoop()->contains(L->getHeader())) { |
| 3930 | // To evaluate this recurrence, we need to know how many times the AddRec |
| 3931 | // loop iterates. Compute this now. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3932 | const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop()); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3933 | if (BackedgeTakenCount == getCouldNotCompute()) return AddRec; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3934 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 3935 | // Then, evaluate the AddRec. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3936 | return AddRec->evaluateAtIteration(BackedgeTakenCount, *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3937 | } |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3938 | return AddRec; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3941 | if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3942 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | 78d63c8 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 3943 | if (Op == Cast->getOperand()) |
| 3944 | return Cast; // must be loop invariant |
| 3945 | return getZeroExtendExpr(Op, Cast->getType()); |
| 3946 | } |
| 3947 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3948 | if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3949 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | 78d63c8 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 3950 | if (Op == Cast->getOperand()) |
| 3951 | return Cast; // must be loop invariant |
| 3952 | return getSignExtendExpr(Op, Cast->getType()); |
| 3953 | } |
| 3954 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3955 | if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3956 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | 78d63c8 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 3957 | if (Op == Cast->getOperand()) |
| 3958 | return Cast; // must be loop invariant |
| 3959 | return getTruncateExpr(Op, Cast->getType()); |
| 3960 | } |
| 3961 | |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3962 | llvm_unreachable("Unknown SCEV type!"); |
Daniel Dunbar | a95d96c | 2009-05-18 16:43:04 +0000 | [diff] [blame] | 3963 | return 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
Dan Gohman | dd40e9a | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 3966 | /// getSCEVAtScope - This is a convenience function which does |
| 3967 | /// getSCEVAtScope(getSCEV(V), L). |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3968 | const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3969 | return getSCEVAtScope(getSCEV(V), L); |
| 3970 | } |
| 3971 | |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3972 | /// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the |
| 3973 | /// following equation: |
| 3974 | /// |
| 3975 | /// A * X = B (mod N) |
| 3976 | /// |
| 3977 | /// where N = 2^BW and BW is the common bit width of A and B. The signedness of |
| 3978 | /// A and B isn't important. |
| 3979 | /// |
| 3980 | /// If the equation does not have a solution, SCEVCouldNotCompute is returned. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3981 | static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const APInt &B, |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3982 | ScalarEvolution &SE) { |
| 3983 | uint32_t BW = A.getBitWidth(); |
| 3984 | assert(BW == B.getBitWidth() && "Bit widths must be the same."); |
| 3985 | assert(A != 0 && "A must be non-zero."); |
| 3986 | |
| 3987 | // 1. D = gcd(A, N) |
| 3988 | // |
| 3989 | // The gcd of A and N may have only one prime factor: 2. The number of |
| 3990 | // trailing zeros in A is its multiplicity |
| 3991 | uint32_t Mult2 = A.countTrailingZeros(); |
| 3992 | // D = 2^Mult2 |
| 3993 | |
| 3994 | // 2. Check if B is divisible by D. |
| 3995 | // |
| 3996 | // B is divisible by D if and only if the multiplicity of prime factor 2 for B |
| 3997 | // is not less than multiplicity of this prime factor for D. |
| 3998 | if (B.countTrailingZeros() < Mult2) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 3999 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4000 | |
| 4001 | // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic |
| 4002 | // modulo (N / D). |
| 4003 | // |
| 4004 | // (N / D) may need BW+1 bits in its representation. Hence, we'll use this |
| 4005 | // bit width during computations. |
| 4006 | APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D |
| 4007 | APInt Mod(BW + 1, 0); |
| 4008 | Mod.set(BW - Mult2); // Mod = N / D |
| 4009 | APInt I = AD.multiplicativeInverse(Mod); |
| 4010 | |
| 4011 | // 4. Compute the minimum unsigned root of the equation: |
| 4012 | // I * (B / D) mod (N / D) |
| 4013 | APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod); |
| 4014 | |
| 4015 | // The result is guaranteed to be less than 2^BW so we may truncate it to BW |
| 4016 | // bits. |
| 4017 | return SE.getConstant(Result.trunc(BW)); |
| 4018 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4019 | |
| 4020 | /// SolveQuadraticEquation - Find the roots of the quadratic equation for the |
| 4021 | /// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which |
| 4022 | /// might be the same) or two SCEVCouldNotCompute objects. |
| 4023 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4024 | static std::pair<const SCEV *,const SCEV *> |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4025 | SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4026 | assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4027 | const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0)); |
| 4028 | const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1)); |
| 4029 | const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4030 | |
| 4031 | // We currently can only solve this if the coefficients are constants. |
| 4032 | if (!LC || !MC || !NC) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4033 | const SCEV *CNC = SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4034 | return std::make_pair(CNC, CNC); |
| 4035 | } |
| 4036 | |
| 4037 | uint32_t BitWidth = LC->getValue()->getValue().getBitWidth(); |
| 4038 | const APInt &L = LC->getValue()->getValue(); |
| 4039 | const APInt &M = MC->getValue()->getValue(); |
| 4040 | const APInt &N = NC->getValue()->getValue(); |
| 4041 | APInt Two(BitWidth, 2); |
| 4042 | APInt Four(BitWidth, 4); |
| 4043 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4044 | { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4045 | using namespace APIntOps; |
| 4046 | const APInt& C = L; |
| 4047 | // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C |
| 4048 | // The B coefficient is M-N/2 |
| 4049 | APInt B(M); |
| 4050 | B -= sdiv(N,Two); |
| 4051 | |
| 4052 | // The A coefficient is N/2 |
| 4053 | APInt A(N.sdiv(Two)); |
| 4054 | |
| 4055 | // Compute the B^2-4ac term. |
| 4056 | APInt SqrtTerm(B); |
| 4057 | SqrtTerm *= B; |
| 4058 | SqrtTerm -= Four * (A * C); |
| 4059 | |
| 4060 | // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest |
| 4061 | // integer value or else APInt::sqrt() will assert. |
| 4062 | APInt SqrtVal(SqrtTerm.sqrt()); |
| 4063 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4064 | // Compute the two solutions for the quadratic formula. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4065 | // The divisions must be performed as signed divisions. |
| 4066 | APInt NegB(-B); |
| 4067 | APInt TwoA( A << 1 ); |
Nick Lewycky | 3577669 | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 4068 | if (TwoA.isMinValue()) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4069 | const SCEV *CNC = SE.getCouldNotCompute(); |
Nick Lewycky | 3577669 | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 4070 | return std::make_pair(CNC, CNC); |
| 4071 | } |
| 4072 | |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 4073 | LLVMContext &Context = SE.getContext(); |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4074 | |
| 4075 | ConstantInt *Solution1 = |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame^] | 4076 | ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA)); |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4077 | ConstantInt *Solution2 = |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame^] | 4078 | ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4079 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4080 | return std::make_pair(SE.getConstant(Solution1), |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4081 | SE.getConstant(Solution2)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4082 | } // end APIntOps namespace |
| 4083 | } |
| 4084 | |
| 4085 | /// HowFarToZero - Return the number of times a backedge comparing the specified |
Dan Gohman | 0c85091 | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4086 | /// value to zero will execute. If not computable, return CouldNotCompute. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4087 | const SCEV *ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4088 | // If the value is a constant |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4089 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4090 | // If the value is already zero, the branch will execute zero times. |
| 4091 | if (C->getValue()->isZero()) return C; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4092 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4093 | } |
| 4094 | |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4095 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4096 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4097 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4098 | |
| 4099 | if (AddRec->isAffine()) { |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4100 | // If this is an affine expression, the execution count of this branch is |
| 4101 | // the minimum unsigned root of the following equation: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4102 | // |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4103 | // Start + Step*N = 0 (mod 2^BW) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4104 | // |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4105 | // equivalent to: |
| 4106 | // |
| 4107 | // Step*N = -Start (mod 2^BW) |
| 4108 | // |
| 4109 | // where BW is the common bit width of Start and Step. |
| 4110 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4111 | // Get the initial value for the loop. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4112 | const SCEV *Start = getSCEVAtScope(AddRec->getStart(), |
| 4113 | L->getParentLoop()); |
| 4114 | const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), |
| 4115 | L->getParentLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4116 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4117 | if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) { |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4118 | // For now we handle only constant steps. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4119 | |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4120 | // First, handle unitary steps. |
| 4121 | if (StepC->getValue()->equalsInt(1)) // 1*N = -Start (mod 2^BW), so: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4122 | return getNegativeSCEV(Start); // N = -Start (as unsigned) |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4123 | if (StepC->getValue()->isAllOnesValue()) // -1*N = -Start (mod 2^BW), so: |
| 4124 | return Start; // N = Start (as unsigned) |
| 4125 | |
| 4126 | // Then, try to solve the above equation provided that Start is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4127 | if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4128 | return SolveLinEquationWithOverflow(StepC->getValue()->getValue(), |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4129 | -StartC->getValue()->getValue(), |
| 4130 | *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4131 | } |
| 4132 | } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) { |
| 4133 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of |
| 4134 | // the quadratic equation to solve it. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4135 | std::pair<const SCEV *,const SCEV *> Roots = SolveQuadraticEquation(AddRec, |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4136 | *this); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4137 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 4138 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4139 | if (R1) { |
| 4140 | #if 0 |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 4141 | errs() << "HFTZ: " << *V << " - sol#1: " << *R1 |
| 4142 | << " sol#2: " << *R2 << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4143 | #endif |
| 4144 | // Pick the smallest positive root value. |
| 4145 | if (ConstantInt *CB = |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 4146 | dyn_cast<ConstantInt>(getContext().getConstantExprICmp(ICmpInst::ICMP_ULT, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4147 | R1->getValue(), R2->getValue()))) { |
| 4148 | if (CB->getZExtValue() == false) |
| 4149 | std::swap(R1, R2); // R1 is the minimum root now. |
| 4150 | |
| 4151 | // We can only use this value if the chrec ends up with an exact zero |
| 4152 | // value at this index. When solving for "X*X != 5", for example, we |
| 4153 | // should not accept a root of 2. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4154 | const SCEV *Val = AddRec->evaluateAtIteration(R1, *this); |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 4155 | if (Val->isZero()) |
| 4156 | return R1; // We found a quadratic root! |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4157 | } |
| 4158 | } |
| 4159 | } |
| 4160 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4161 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | /// HowFarToNonZero - Return the number of times a backedge checking the |
| 4165 | /// specified value for nonzero will execute. If not computable, return |
Dan Gohman | 0c85091 | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4166 | /// CouldNotCompute |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4167 | const SCEV *ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4168 | // Loops that look like: while (X == 0) are very strange indeed. We don't |
| 4169 | // handle them yet except for the trivial case. This could be expanded in the |
| 4170 | // future as needed. |
| 4171 | |
| 4172 | // If the value is a constant, check to see if it is known to be non-zero |
| 4173 | // already. If so, the backedge will execute zero times. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4174 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Nick Lewycky | f680518 | 2008-02-21 09:14:53 +0000 | [diff] [blame] | 4175 | if (!C->getValue()->isNullValue()) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4176 | return getIntegerSCEV(0, C->getType()); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4177 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | // We could implement others, but I really doubt anyone writes loops like |
| 4181 | // this, and if they did, they would already be constant folded. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4182 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4183 | } |
| 4184 | |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4185 | /// getLoopPredecessor - If the given loop's header has exactly one unique |
| 4186 | /// predecessor outside the loop, return it. Otherwise return null. |
| 4187 | /// |
| 4188 | BasicBlock *ScalarEvolution::getLoopPredecessor(const Loop *L) { |
| 4189 | BasicBlock *Header = L->getHeader(); |
| 4190 | BasicBlock *Pred = 0; |
| 4191 | for (pred_iterator PI = pred_begin(Header), E = pred_end(Header); |
| 4192 | PI != E; ++PI) |
| 4193 | if (!L->contains(*PI)) { |
| 4194 | if (Pred && Pred != *PI) return 0; // Multiple predecessors. |
| 4195 | Pred = *PI; |
| 4196 | } |
| 4197 | return Pred; |
| 4198 | } |
| 4199 | |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4200 | /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB |
| 4201 | /// (which may not be an immediate predecessor) which has exactly one |
| 4202 | /// successor from which BB is reachable, or null if no such block is |
| 4203 | /// found. |
| 4204 | /// |
| 4205 | BasicBlock * |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4206 | ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) { |
Dan Gohman | 1116ea7 | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 4207 | // If the block has a unique predecessor, then there is no path from the |
| 4208 | // predecessor to the block that does not go through the direct edge |
| 4209 | // from the predecessor to the block. |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4210 | if (BasicBlock *Pred = BB->getSinglePredecessor()) |
| 4211 | return Pred; |
| 4212 | |
| 4213 | // 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] | 4214 | // If the header has a unique predecessor outside the loop, it must be |
| 4215 | // a block that has exactly one successor that can reach the loop. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4216 | if (Loop *L = LI->getLoopFor(BB)) |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4217 | return getLoopPredecessor(L); |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4218 | |
| 4219 | return 0; |
| 4220 | } |
| 4221 | |
Dan Gohman | bc1e347 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4222 | /// HasSameValue - SCEV structural equivalence is usually sufficient for |
| 4223 | /// testing whether two expressions are equal, however for the purposes of |
| 4224 | /// looking for a condition guarding a loop, it can be useful to be a little |
| 4225 | /// more general, since a front-end may have replicated the controlling |
| 4226 | /// expression. |
| 4227 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4228 | static bool HasSameValue(const SCEV *A, const SCEV *B) { |
Dan Gohman | bc1e347 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4229 | // Quick check to see if they are the same SCEV. |
| 4230 | if (A == B) return true; |
| 4231 | |
| 4232 | // Otherwise, if they're both SCEVUnknown, it's possible that they hold |
| 4233 | // two different instructions with the same value. Check for this case. |
| 4234 | if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A)) |
| 4235 | if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B)) |
| 4236 | if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue())) |
| 4237 | if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue())) |
| 4238 | if (AI->isIdenticalTo(BI)) |
| 4239 | return true; |
| 4240 | |
| 4241 | // Otherwise assume they may have a different value. |
| 4242 | return false; |
| 4243 | } |
| 4244 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4245 | bool ScalarEvolution::isKnownNegative(const SCEV *S) { |
| 4246 | return getSignedRange(S).getSignedMax().isNegative(); |
| 4247 | } |
| 4248 | |
| 4249 | bool ScalarEvolution::isKnownPositive(const SCEV *S) { |
| 4250 | return getSignedRange(S).getSignedMin().isStrictlyPositive(); |
| 4251 | } |
| 4252 | |
| 4253 | bool ScalarEvolution::isKnownNonNegative(const SCEV *S) { |
| 4254 | return !getSignedRange(S).getSignedMin().isNegative(); |
| 4255 | } |
| 4256 | |
| 4257 | bool ScalarEvolution::isKnownNonPositive(const SCEV *S) { |
| 4258 | return !getSignedRange(S).getSignedMax().isStrictlyPositive(); |
| 4259 | } |
| 4260 | |
| 4261 | bool ScalarEvolution::isKnownNonZero(const SCEV *S) { |
| 4262 | return isKnownNegative(S) || isKnownPositive(S); |
| 4263 | } |
| 4264 | |
| 4265 | bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred, |
| 4266 | const SCEV *LHS, const SCEV *RHS) { |
| 4267 | |
| 4268 | if (HasSameValue(LHS, RHS)) |
| 4269 | return ICmpInst::isTrueWhenEqual(Pred); |
| 4270 | |
| 4271 | switch (Pred) { |
| 4272 | default: |
Dan Gohman | 2d4f5b1 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4273 | llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4274 | break; |
| 4275 | case ICmpInst::ICMP_SGT: |
| 4276 | Pred = ICmpInst::ICMP_SLT; |
| 4277 | std::swap(LHS, RHS); |
| 4278 | case ICmpInst::ICMP_SLT: { |
| 4279 | ConstantRange LHSRange = getSignedRange(LHS); |
| 4280 | ConstantRange RHSRange = getSignedRange(RHS); |
| 4281 | if (LHSRange.getSignedMax().slt(RHSRange.getSignedMin())) |
| 4282 | return true; |
| 4283 | if (LHSRange.getSignedMin().sge(RHSRange.getSignedMax())) |
| 4284 | return false; |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4285 | break; |
| 4286 | } |
| 4287 | case ICmpInst::ICMP_SGE: |
| 4288 | Pred = ICmpInst::ICMP_SLE; |
| 4289 | std::swap(LHS, RHS); |
| 4290 | case ICmpInst::ICMP_SLE: { |
| 4291 | ConstantRange LHSRange = getSignedRange(LHS); |
| 4292 | ConstantRange RHSRange = getSignedRange(RHS); |
| 4293 | if (LHSRange.getSignedMax().sle(RHSRange.getSignedMin())) |
| 4294 | return true; |
| 4295 | if (LHSRange.getSignedMin().sgt(RHSRange.getSignedMax())) |
| 4296 | return false; |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4297 | break; |
| 4298 | } |
| 4299 | case ICmpInst::ICMP_UGT: |
| 4300 | Pred = ICmpInst::ICMP_ULT; |
| 4301 | std::swap(LHS, RHS); |
| 4302 | case ICmpInst::ICMP_ULT: { |
| 4303 | ConstantRange LHSRange = getUnsignedRange(LHS); |
| 4304 | ConstantRange RHSRange = getUnsignedRange(RHS); |
| 4305 | if (LHSRange.getUnsignedMax().ult(RHSRange.getUnsignedMin())) |
| 4306 | return true; |
| 4307 | if (LHSRange.getUnsignedMin().uge(RHSRange.getUnsignedMax())) |
| 4308 | return false; |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4309 | break; |
| 4310 | } |
| 4311 | case ICmpInst::ICMP_UGE: |
| 4312 | Pred = ICmpInst::ICMP_ULE; |
| 4313 | std::swap(LHS, RHS); |
| 4314 | case ICmpInst::ICMP_ULE: { |
| 4315 | ConstantRange LHSRange = getUnsignedRange(LHS); |
| 4316 | ConstantRange RHSRange = getUnsignedRange(RHS); |
| 4317 | if (LHSRange.getUnsignedMax().ule(RHSRange.getUnsignedMin())) |
| 4318 | return true; |
| 4319 | if (LHSRange.getUnsignedMin().ugt(RHSRange.getUnsignedMax())) |
| 4320 | return false; |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4321 | break; |
| 4322 | } |
| 4323 | case ICmpInst::ICMP_NE: { |
| 4324 | if (getUnsignedRange(LHS).intersectWith(getUnsignedRange(RHS)).isEmptySet()) |
| 4325 | return true; |
| 4326 | if (getSignedRange(LHS).intersectWith(getSignedRange(RHS)).isEmptySet()) |
| 4327 | return true; |
| 4328 | |
| 4329 | const SCEV *Diff = getMinusSCEV(LHS, RHS); |
| 4330 | if (isKnownNonZero(Diff)) |
| 4331 | return true; |
| 4332 | break; |
| 4333 | } |
| 4334 | case ICmpInst::ICMP_EQ: |
Dan Gohman | 44e675f | 2009-07-20 23:54:43 +0000 | [diff] [blame] | 4335 | // The check at the top of the function catches the case where |
| 4336 | // the values are known to be equal. |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4337 | break; |
| 4338 | } |
| 4339 | return false; |
| 4340 | } |
| 4341 | |
| 4342 | /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is |
| 4343 | /// protected by a conditional between LHS and RHS. This is used to |
| 4344 | /// to eliminate casts. |
| 4345 | bool |
| 4346 | ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L, |
| 4347 | ICmpInst::Predicate Pred, |
| 4348 | const SCEV *LHS, const SCEV *RHS) { |
| 4349 | // Interpret a null as meaning no loop, where there is obviously no guard |
| 4350 | // (interprocedural conditions notwithstanding). |
| 4351 | if (!L) return true; |
| 4352 | |
| 4353 | BasicBlock *Latch = L->getLoopLatch(); |
| 4354 | if (!Latch) |
| 4355 | return false; |
| 4356 | |
| 4357 | BranchInst *LoopContinuePredicate = |
| 4358 | dyn_cast<BranchInst>(Latch->getTerminator()); |
| 4359 | if (!LoopContinuePredicate || |
| 4360 | LoopContinuePredicate->isUnconditional()) |
| 4361 | return false; |
| 4362 | |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4363 | return isImpliedCond(LoopContinuePredicate->getCondition(), Pred, LHS, RHS, |
| 4364 | LoopContinuePredicate->getSuccessor(0) != L->getHeader()); |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4365 | } |
| 4366 | |
| 4367 | /// isLoopGuardedByCond - Test whether entry to the loop is protected |
| 4368 | /// by a conditional between LHS and RHS. This is used to help avoid max |
| 4369 | /// expressions in loop trip counts, and to eliminate casts. |
| 4370 | bool |
| 4371 | ScalarEvolution::isLoopGuardedByCond(const Loop *L, |
| 4372 | ICmpInst::Predicate Pred, |
| 4373 | const SCEV *LHS, const SCEV *RHS) { |
Dan Gohman | 8b93818 | 2009-05-18 16:03:58 +0000 | [diff] [blame] | 4374 | // Interpret a null as meaning no loop, where there is obviously no guard |
| 4375 | // (interprocedural conditions notwithstanding). |
| 4376 | if (!L) return false; |
| 4377 | |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4378 | BasicBlock *Predecessor = getLoopPredecessor(L); |
| 4379 | BasicBlock *PredecessorDest = L->getHeader(); |
Nick Lewycky | 1b020bf | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4380 | |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4381 | // Starting at the loop predecessor, climb up the predecessor chain, as long |
| 4382 | // as there are predecessors that can be found that have unique successors |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4383 | // leading to the original header. |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4384 | for (; Predecessor; |
| 4385 | PredecessorDest = Predecessor, |
| 4386 | Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) { |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4387 | |
| 4388 | BranchInst *LoopEntryPredicate = |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4389 | dyn_cast<BranchInst>(Predecessor->getTerminator()); |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4390 | if (!LoopEntryPredicate || |
| 4391 | LoopEntryPredicate->isUnconditional()) |
| 4392 | continue; |
| 4393 | |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4394 | if (isImpliedCond(LoopEntryPredicate->getCondition(), Pred, LHS, RHS, |
| 4395 | LoopEntryPredicate->getSuccessor(0) != PredecessorDest)) |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4396 | return true; |
Nick Lewycky | 1b020bf | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4397 | } |
| 4398 | |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4399 | return false; |
Nick Lewycky | 1b020bf | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4400 | } |
| 4401 | |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4402 | /// isImpliedCond - Test whether the condition described by Pred, LHS, |
| 4403 | /// and RHS is true whenever the given Cond value evaluates to true. |
| 4404 | bool ScalarEvolution::isImpliedCond(Value *CondValue, |
| 4405 | ICmpInst::Predicate Pred, |
| 4406 | const SCEV *LHS, const SCEV *RHS, |
| 4407 | bool Inverse) { |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4408 | // Recursivly handle And and Or conditions. |
| 4409 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CondValue)) { |
| 4410 | if (BO->getOpcode() == Instruction::And) { |
| 4411 | if (!Inverse) |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4412 | return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) || |
| 4413 | isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse); |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4414 | } else if (BO->getOpcode() == Instruction::Or) { |
| 4415 | if (Inverse) |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4416 | return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) || |
| 4417 | isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse); |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4418 | } |
| 4419 | } |
| 4420 | |
| 4421 | ICmpInst *ICI = dyn_cast<ICmpInst>(CondValue); |
| 4422 | if (!ICI) return false; |
| 4423 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4424 | // Bail if the ICmp's operands' types are wider than the needed type |
| 4425 | // before attempting to call getSCEV on them. This avoids infinite |
| 4426 | // recursion, since the analysis of widening casts can require loop |
| 4427 | // exit condition information for overflow checking, which would |
| 4428 | // lead back here. |
| 4429 | if (getTypeSizeInBits(LHS->getType()) < |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4430 | getTypeSizeInBits(ICI->getOperand(0)->getType())) |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4431 | return false; |
| 4432 | |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4433 | // Now that we found a conditional branch that dominates the loop, check to |
| 4434 | // see if it is the comparison we are looking for. |
| 4435 | ICmpInst::Predicate FoundPred; |
| 4436 | if (Inverse) |
| 4437 | FoundPred = ICI->getInversePredicate(); |
| 4438 | else |
| 4439 | FoundPred = ICI->getPredicate(); |
| 4440 | |
| 4441 | const SCEV *FoundLHS = getSCEV(ICI->getOperand(0)); |
| 4442 | const SCEV *FoundRHS = getSCEV(ICI->getOperand(1)); |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4443 | |
| 4444 | // Balance the types. The case where FoundLHS' type is wider than |
| 4445 | // LHS' type is checked for above. |
| 4446 | if (getTypeSizeInBits(LHS->getType()) > |
| 4447 | getTypeSizeInBits(FoundLHS->getType())) { |
| 4448 | if (CmpInst::isSigned(Pred)) { |
| 4449 | FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType()); |
| 4450 | FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType()); |
| 4451 | } else { |
| 4452 | FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType()); |
| 4453 | FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType()); |
| 4454 | } |
| 4455 | } |
| 4456 | |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4457 | // Canonicalize the query to match the way instcombine will have |
| 4458 | // canonicalized the comparison. |
| 4459 | // First, put a constant operand on the right. |
| 4460 | if (isa<SCEVConstant>(LHS)) { |
| 4461 | std::swap(LHS, RHS); |
| 4462 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4463 | } |
| 4464 | // Then, canonicalize comparisons with boundary cases. |
| 4465 | if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) { |
| 4466 | const APInt &RA = RC->getValue()->getValue(); |
| 4467 | switch (Pred) { |
| 4468 | default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
| 4469 | case ICmpInst::ICMP_EQ: |
| 4470 | case ICmpInst::ICMP_NE: |
| 4471 | break; |
| 4472 | case ICmpInst::ICMP_UGE: |
| 4473 | if ((RA - 1).isMinValue()) { |
| 4474 | Pred = ICmpInst::ICMP_NE; |
| 4475 | RHS = getConstant(RA - 1); |
| 4476 | break; |
| 4477 | } |
| 4478 | if (RA.isMaxValue()) { |
| 4479 | Pred = ICmpInst::ICMP_EQ; |
| 4480 | break; |
| 4481 | } |
| 4482 | if (RA.isMinValue()) return true; |
| 4483 | break; |
| 4484 | case ICmpInst::ICMP_ULE: |
| 4485 | if ((RA + 1).isMaxValue()) { |
| 4486 | Pred = ICmpInst::ICMP_NE; |
| 4487 | RHS = getConstant(RA + 1); |
| 4488 | break; |
| 4489 | } |
| 4490 | if (RA.isMinValue()) { |
| 4491 | Pred = ICmpInst::ICMP_EQ; |
| 4492 | break; |
| 4493 | } |
| 4494 | if (RA.isMaxValue()) return true; |
| 4495 | break; |
| 4496 | case ICmpInst::ICMP_SGE: |
| 4497 | if ((RA - 1).isMinSignedValue()) { |
| 4498 | Pred = ICmpInst::ICMP_NE; |
| 4499 | RHS = getConstant(RA - 1); |
| 4500 | break; |
| 4501 | } |
| 4502 | if (RA.isMaxSignedValue()) { |
| 4503 | Pred = ICmpInst::ICMP_EQ; |
| 4504 | break; |
| 4505 | } |
| 4506 | if (RA.isMinSignedValue()) return true; |
| 4507 | break; |
| 4508 | case ICmpInst::ICMP_SLE: |
| 4509 | if ((RA + 1).isMaxSignedValue()) { |
| 4510 | Pred = ICmpInst::ICMP_NE; |
| 4511 | RHS = getConstant(RA + 1); |
| 4512 | break; |
| 4513 | } |
| 4514 | if (RA.isMinSignedValue()) { |
| 4515 | Pred = ICmpInst::ICMP_EQ; |
| 4516 | break; |
| 4517 | } |
| 4518 | if (RA.isMaxSignedValue()) return true; |
| 4519 | break; |
| 4520 | case ICmpInst::ICMP_UGT: |
| 4521 | if (RA.isMinValue()) { |
| 4522 | Pred = ICmpInst::ICMP_NE; |
| 4523 | break; |
| 4524 | } |
| 4525 | if ((RA + 1).isMaxValue()) { |
| 4526 | Pred = ICmpInst::ICMP_EQ; |
| 4527 | RHS = getConstant(RA + 1); |
| 4528 | break; |
| 4529 | } |
| 4530 | if (RA.isMaxValue()) return false; |
| 4531 | break; |
| 4532 | case ICmpInst::ICMP_ULT: |
| 4533 | if (RA.isMaxValue()) { |
| 4534 | Pred = ICmpInst::ICMP_NE; |
| 4535 | break; |
| 4536 | } |
| 4537 | if ((RA - 1).isMinValue()) { |
| 4538 | Pred = ICmpInst::ICMP_EQ; |
| 4539 | RHS = getConstant(RA - 1); |
| 4540 | break; |
| 4541 | } |
| 4542 | if (RA.isMinValue()) return false; |
| 4543 | break; |
| 4544 | case ICmpInst::ICMP_SGT: |
| 4545 | if (RA.isMinSignedValue()) { |
| 4546 | Pred = ICmpInst::ICMP_NE; |
| 4547 | break; |
| 4548 | } |
| 4549 | if ((RA + 1).isMaxSignedValue()) { |
| 4550 | Pred = ICmpInst::ICMP_EQ; |
| 4551 | RHS = getConstant(RA + 1); |
| 4552 | break; |
| 4553 | } |
| 4554 | if (RA.isMaxSignedValue()) return false; |
| 4555 | break; |
| 4556 | case ICmpInst::ICMP_SLT: |
| 4557 | if (RA.isMaxSignedValue()) { |
| 4558 | Pred = ICmpInst::ICMP_NE; |
| 4559 | break; |
| 4560 | } |
| 4561 | if ((RA - 1).isMinSignedValue()) { |
| 4562 | Pred = ICmpInst::ICMP_EQ; |
| 4563 | RHS = getConstant(RA - 1); |
| 4564 | break; |
| 4565 | } |
| 4566 | if (RA.isMinSignedValue()) return false; |
| 4567 | break; |
| 4568 | } |
| 4569 | } |
| 4570 | |
| 4571 | // Check to see if we can make the LHS or RHS match. |
| 4572 | if (LHS == FoundRHS || RHS == FoundLHS) { |
| 4573 | if (isa<SCEVConstant>(RHS)) { |
| 4574 | std::swap(FoundLHS, FoundRHS); |
| 4575 | FoundPred = ICmpInst::getSwappedPredicate(FoundPred); |
| 4576 | } else { |
| 4577 | std::swap(LHS, RHS); |
| 4578 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4579 | } |
| 4580 | } |
| 4581 | |
| 4582 | // Check whether the found predicate is the same as the desired predicate. |
| 4583 | if (FoundPred == Pred) |
| 4584 | return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS); |
| 4585 | |
| 4586 | // Check whether swapping the found predicate makes it the same as the |
| 4587 | // desired predicate. |
| 4588 | if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) { |
| 4589 | if (isa<SCEVConstant>(RHS)) |
| 4590 | return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS); |
| 4591 | else |
| 4592 | return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred), |
| 4593 | RHS, LHS, FoundLHS, FoundRHS); |
| 4594 | } |
| 4595 | |
| 4596 | // Check whether the actual condition is beyond sufficient. |
| 4597 | if (FoundPred == ICmpInst::ICMP_EQ) |
| 4598 | if (ICmpInst::isTrueWhenEqual(Pred)) |
| 4599 | if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS)) |
| 4600 | return true; |
| 4601 | if (Pred == ICmpInst::ICMP_NE) |
| 4602 | if (!ICmpInst::isTrueWhenEqual(FoundPred)) |
| 4603 | if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS)) |
| 4604 | return true; |
| 4605 | |
| 4606 | // Otherwise assume the worst. |
| 4607 | return false; |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4608 | } |
| 4609 | |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4610 | /// isImpliedCondOperands - Test whether the condition described by Pred, |
| 4611 | /// LHS, and RHS is true whenever the condition desribed by Pred, FoundLHS, |
| 4612 | /// and FoundRHS is true. |
| 4613 | bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred, |
| 4614 | const SCEV *LHS, const SCEV *RHS, |
| 4615 | const SCEV *FoundLHS, |
| 4616 | const SCEV *FoundRHS) { |
| 4617 | return isImpliedCondOperandsHelper(Pred, LHS, RHS, |
| 4618 | FoundLHS, FoundRHS) || |
| 4619 | // ~x < ~y --> x > y |
| 4620 | isImpliedCondOperandsHelper(Pred, LHS, RHS, |
| 4621 | getNotSCEV(FoundRHS), |
| 4622 | getNotSCEV(FoundLHS)); |
| 4623 | } |
| 4624 | |
| 4625 | /// isImpliedCondOperandsHelper - Test whether the condition described by |
| 4626 | /// Pred, LHS, and RHS is true whenever the condition desribed by Pred, |
| 4627 | /// FoundLHS, and FoundRHS is true. |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4628 | bool |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4629 | ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred, |
| 4630 | const SCEV *LHS, const SCEV *RHS, |
| 4631 | const SCEV *FoundLHS, |
| 4632 | const SCEV *FoundRHS) { |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4633 | switch (Pred) { |
Dan Gohman | 2d4f5b1 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4634 | default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
| 4635 | case ICmpInst::ICMP_EQ: |
| 4636 | case ICmpInst::ICMP_NE: |
| 4637 | if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS)) |
| 4638 | return true; |
| 4639 | break; |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4640 | case ICmpInst::ICMP_SLT: |
Dan Gohman | 2d4f5b1 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4641 | case ICmpInst::ICMP_SLE: |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4642 | if (isKnownPredicate(ICmpInst::ICMP_SLE, LHS, FoundLHS) && |
| 4643 | isKnownPredicate(ICmpInst::ICMP_SGE, RHS, FoundRHS)) |
| 4644 | return true; |
| 4645 | break; |
| 4646 | case ICmpInst::ICMP_SGT: |
Dan Gohman | 2d4f5b1 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4647 | case ICmpInst::ICMP_SGE: |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4648 | if (isKnownPredicate(ICmpInst::ICMP_SGE, LHS, FoundLHS) && |
| 4649 | isKnownPredicate(ICmpInst::ICMP_SLE, RHS, FoundRHS)) |
| 4650 | return true; |
| 4651 | break; |
| 4652 | case ICmpInst::ICMP_ULT: |
Dan Gohman | 2d4f5b1 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4653 | case ICmpInst::ICMP_ULE: |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4654 | if (isKnownPredicate(ICmpInst::ICMP_ULE, LHS, FoundLHS) && |
| 4655 | isKnownPredicate(ICmpInst::ICMP_UGE, RHS, FoundRHS)) |
| 4656 | return true; |
| 4657 | break; |
| 4658 | case ICmpInst::ICMP_UGT: |
Dan Gohman | 2d4f5b1 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4659 | case ICmpInst::ICMP_UGE: |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4660 | if (isKnownPredicate(ICmpInst::ICMP_UGE, LHS, FoundLHS) && |
| 4661 | isKnownPredicate(ICmpInst::ICMP_ULE, RHS, FoundRHS)) |
| 4662 | return true; |
| 4663 | break; |
| 4664 | } |
| 4665 | |
| 4666 | return false; |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4667 | } |
| 4668 | |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4669 | /// getBECount - Subtract the end and start values and divide by the step, |
| 4670 | /// rounding up, to get the number of times the backedge is executed. Return |
| 4671 | /// CouldNotCompute if an intermediate computation overflows. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4672 | const SCEV *ScalarEvolution::getBECount(const SCEV *Start, |
Dan Gohman | 69eacc7 | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 4673 | const SCEV *End, |
| 4674 | const SCEV *Step) { |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4675 | const Type *Ty = Start->getType(); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4676 | const SCEV *NegOne = getIntegerSCEV(-1, Ty); |
| 4677 | const SCEV *Diff = getMinusSCEV(End, Start); |
| 4678 | const SCEV *RoundUp = getAddExpr(Step, NegOne); |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4679 | |
| 4680 | // Add an adjustment to the difference between End and Start so that |
| 4681 | // the division will effectively round up. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4682 | const SCEV *Add = getAddExpr(Diff, RoundUp); |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4683 | |
| 4684 | // Check Add for unsigned overflow. |
| 4685 | // TODO: More sophisticated things could be done here. |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 4686 | const Type *WideTy = getContext().getIntegerType(getTypeSizeInBits(Ty) + 1); |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4687 | const SCEV *EDiff = getZeroExtendExpr(Diff, WideTy); |
| 4688 | const SCEV *ERoundUp = getZeroExtendExpr(RoundUp, WideTy); |
| 4689 | const SCEV *OperandExtendedAdd = getAddExpr(EDiff, ERoundUp); |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4690 | if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4691 | return getCouldNotCompute(); |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4692 | |
| 4693 | return getUDivExpr(Add, Step); |
| 4694 | } |
| 4695 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4696 | /// HowManyLessThans - Return the number of times a backedge containing the |
| 4697 | /// specified less-than comparison will execute. If not computable, return |
Dan Gohman | 0c85091 | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4698 | /// CouldNotCompute. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4699 | ScalarEvolution::BackedgeTakenInfo |
| 4700 | ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS, |
| 4701 | const Loop *L, bool isSigned) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4702 | // Only handle: "ADDREC < LoopInvariant". |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4703 | if (!RHS->isLoopInvariant(L)) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4704 | |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4705 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4706 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4707 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4708 | |
| 4709 | if (AddRec->isAffine()) { |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 4710 | // FORNOW: We only support unit strides. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4711 | unsigned BitWidth = getTypeSizeInBits(AddRec->getType()); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4712 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4713 | |
| 4714 | // TODO: handle non-constant strides. |
| 4715 | const SCEVConstant *CStep = dyn_cast<SCEVConstant>(Step); |
| 4716 | if (!CStep || CStep->isZero()) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4717 | return getCouldNotCompute(); |
Dan Gohman | f8bc8e8 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 4718 | if (CStep->isOne()) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4719 | // With unit stride, the iteration never steps past the limit value. |
| 4720 | } else if (CStep->getValue()->getValue().isStrictlyPositive()) { |
| 4721 | if (const SCEVConstant *CLimit = dyn_cast<SCEVConstant>(RHS)) { |
| 4722 | // Test whether a positive iteration iteration can step past the limit |
| 4723 | // value and past the maximum value for its type in a single step. |
| 4724 | if (isSigned) { |
| 4725 | APInt Max = APInt::getSignedMaxValue(BitWidth); |
| 4726 | if ((Max - CStep->getValue()->getValue()) |
| 4727 | .slt(CLimit->getValue()->getValue())) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4728 | return getCouldNotCompute(); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4729 | } else { |
| 4730 | APInt Max = APInt::getMaxValue(BitWidth); |
| 4731 | if ((Max - CStep->getValue()->getValue()) |
| 4732 | .ult(CLimit->getValue()->getValue())) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4733 | return getCouldNotCompute(); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4734 | } |
| 4735 | } else |
| 4736 | // TODO: handle non-constant limit values below. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4737 | return getCouldNotCompute(); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4738 | } else |
| 4739 | // TODO: handle negative strides below. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4740 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4741 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4742 | // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant |
| 4743 | // m. So, we count the number of iterations in which {n,+,s} < m is true. |
| 4744 | // 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] | 4745 | // treat m-n as signed nor unsigned due to overflow possibility. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4746 | |
Wojciech Matyjewicz | ebc77b1 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4747 | // 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] | 4748 | const SCEV *Start = AddRec->getOperand(0); |
Wojciech Matyjewicz | ebc77b1 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4749 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4750 | // Determine the minimum constant start value. |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4751 | const SCEV *MinStart = getConstant(isSigned ? |
| 4752 | getSignedRange(Start).getSignedMin() : |
| 4753 | getUnsignedRange(Start).getUnsignedMin()); |
Wojciech Matyjewicz | ebc77b1 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4754 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4755 | // If we know that the condition is true in order to enter the loop, |
| 4756 | // 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] | 4757 | // only know that it will execute (max(m,n)-n)/s times. In both cases, |
| 4758 | // the division must round up. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4759 | const SCEV *End = RHS; |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4760 | if (!isLoopGuardedByCond(L, |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4761 | isSigned ? ICmpInst::ICMP_SLT : |
| 4762 | ICmpInst::ICMP_ULT, |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4763 | getMinusSCEV(Start, Step), RHS)) |
| 4764 | End = isSigned ? getSMaxExpr(RHS, Start) |
| 4765 | : getUMaxExpr(RHS, Start); |
| 4766 | |
| 4767 | // Determine the maximum constant end value. |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4768 | const SCEV *MaxEnd = getConstant(isSigned ? |
| 4769 | getSignedRange(End).getSignedMax() : |
| 4770 | getUnsignedRange(End).getUnsignedMax()); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4771 | |
| 4772 | // Finally, we subtract these two values and divide, rounding up, to get |
| 4773 | // the number of times the backedge is executed. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4774 | const SCEV *BECount = getBECount(Start, End, Step); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4775 | |
| 4776 | // The maximum backedge count is similar, except using the minimum start |
| 4777 | // value and the maximum end value. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4778 | const SCEV *MaxBECount = getBECount(MinStart, MaxEnd, Step); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4779 | |
| 4780 | return BackedgeTakenInfo(BECount, MaxBECount); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4781 | } |
| 4782 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4783 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4784 | } |
| 4785 | |
| 4786 | /// getNumIterationsInRange - Return the number of iterations of this loop that |
| 4787 | /// produce values in the specified constant range. Another way of looking at |
| 4788 | /// this is that it returns the first iteration number where the value is not in |
| 4789 | /// the condition, thus computing the exit count. If the iteration count can't |
| 4790 | /// be computed, an instance of SCEVCouldNotCompute is returned. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4791 | const SCEV *SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4792 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4793 | if (Range.isFullSet()) // Infinite loop. |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4794 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4795 | |
| 4796 | // 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] | 4797 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4798 | if (!SC->getValue()->isZero()) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4799 | SmallVector<const SCEV *, 4> Operands(op_begin(), op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4800 | Operands[0] = SE.getIntegerSCEV(0, SC->getType()); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4801 | const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4802 | if (const SCEVAddRecExpr *ShiftedAddRec = |
| 4803 | dyn_cast<SCEVAddRecExpr>(Shifted)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4804 | return ShiftedAddRec->getNumIterationsInRange( |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4805 | Range.subtract(SC->getValue()->getValue()), SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4806 | // This is strange and shouldn't happen. |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4807 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4808 | } |
| 4809 | |
| 4810 | // The only time we can solve this is when we have all constant indices. |
| 4811 | // Otherwise, we cannot determine the overflow conditions. |
| 4812 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 4813 | if (!isa<SCEVConstant>(getOperand(i))) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4814 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4815 | |
| 4816 | |
| 4817 | // Okay at this point we know that all elements of the chrec are constants and |
| 4818 | // that the start element is zero. |
| 4819 | |
| 4820 | // First check to see if the range contains zero. If not, the first |
| 4821 | // iteration exits. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 4822 | unsigned BitWidth = SE.getTypeSizeInBits(getType()); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4823 | if (!Range.contains(APInt(BitWidth, 0))) |
Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 4824 | return SE.getIntegerSCEV(0, getType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4825 | |
| 4826 | if (isAffine()) { |
| 4827 | // If this is an affine expression then we have this situation: |
| 4828 | // Solve {0,+,A} in Range === Ax in Range |
| 4829 | |
| 4830 | // We know that zero is in the range. If A is positive then we know that |
| 4831 | // the upper value of the range must be the first possible exit value. |
| 4832 | // If A is negative then the lower of the range is the last possible loop |
| 4833 | // value. Also note that we already checked for a full range. |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4834 | APInt One(BitWidth,1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4835 | APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue(); |
| 4836 | APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower(); |
| 4837 | |
| 4838 | // The exit value should be (End+A)/A. |
Nick Lewycky | a0facae | 2007-09-27 14:12:54 +0000 | [diff] [blame] | 4839 | APInt ExitVal = (End + A).udiv(A); |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame^] | 4840 | ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4841 | |
| 4842 | // Evaluate at the exit value. If we really did fall out of the valid |
| 4843 | // range, then we computed our trip count, otherwise wrap around or other |
| 4844 | // things must have happened. |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4845 | ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4846 | if (Range.contains(Val->getValue())) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4847 | return SE.getCouldNotCompute(); // Something strange happened |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4848 | |
| 4849 | // Ensure that the previous value is in the range. This is a sanity check. |
| 4850 | assert(Range.contains( |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4851 | EvaluateConstantChrecAtConstant(this, |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame^] | 4852 | ConstantInt::get(SE.getContext(), ExitVal - One), SE)->getValue()) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4853 | "Linear scev computation is off in a bad way!"); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4854 | return SE.getConstant(ExitValue); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4855 | } else if (isQuadratic()) { |
| 4856 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the |
| 4857 | // quadratic equation to solve it. To do this, we must frame our problem in |
| 4858 | // terms of figuring out when zero is crossed, instead of when |
| 4859 | // Range.getUpper() is crossed. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4860 | SmallVector<const SCEV *, 4> NewOps(op_begin(), op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4861 | NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper())); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4862 | const SCEV *NewAddRec = SE.getAddRecExpr(NewOps, getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4863 | |
| 4864 | // Next, solve the constructed addrec |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4865 | std::pair<const SCEV *,const SCEV *> Roots = |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4866 | SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4867 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 4868 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4869 | if (R1) { |
| 4870 | // Pick the smallest positive root value. |
| 4871 | if (ConstantInt *CB = |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4872 | dyn_cast<ConstantInt>( |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 4873 | SE.getContext().getConstantExprICmp(ICmpInst::ICMP_ULT, |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4874 | R1->getValue(), R2->getValue()))) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4875 | if (CB->getZExtValue() == false) |
| 4876 | std::swap(R1, R2); // R1 is the minimum root now. |
| 4877 | |
| 4878 | // Make sure the root is not off by one. The returned iteration should |
| 4879 | // not be in the range, but the previous one should be. When solving |
| 4880 | // for "X*X < 5", for example, we should not return a root of 2. |
| 4881 | ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this, |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4882 | R1->getValue(), |
| 4883 | SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4884 | if (Range.contains(R1Val->getValue())) { |
| 4885 | // The next iteration must be out of the range... |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4886 | ConstantInt *NextVal = |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame^] | 4887 | ConstantInt::get(SE.getContext(), R1->getValue()->getValue()+1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4888 | |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4889 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4890 | if (!Range.contains(R1Val->getValue())) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4891 | return SE.getConstant(NextVal); |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4892 | return SE.getCouldNotCompute(); // Something strange happened |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4893 | } |
| 4894 | |
| 4895 | // If R1 was not in the range, then it is a good return value. Make |
| 4896 | // sure that R1-1 WAS in the range though, just in case. |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4897 | ConstantInt *NextVal = |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame^] | 4898 | ConstantInt::get(SE.getContext(), R1->getValue()->getValue()-1); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4899 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4900 | if (Range.contains(R1Val->getValue())) |
| 4901 | return R1; |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4902 | return SE.getCouldNotCompute(); // Something strange happened |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4903 | } |
| 4904 | } |
| 4905 | } |
| 4906 | |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4907 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | |
| 4911 | |
| 4912 | //===----------------------------------------------------------------------===// |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4913 | // SCEVCallbackVH Class Implementation |
| 4914 | //===----------------------------------------------------------------------===// |
| 4915 | |
Dan Gohman | 999d14e | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 4916 | void ScalarEvolution::SCEVCallbackVH::deleted() { |
Dan Gohman | 31b69c1 | 2009-07-13 22:20:53 +0000 | [diff] [blame] | 4917 | assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4918 | if (PHINode *PN = dyn_cast<PHINode>(getValPtr())) |
| 4919 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 4920 | if (Instruction *I = dyn_cast<Instruction>(getValPtr())) |
| 4921 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4922 | SE->Scalars.erase(getValPtr()); |
| 4923 | // this now dangles! |
| 4924 | } |
| 4925 | |
Dan Gohman | 999d14e | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 4926 | void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *) { |
Dan Gohman | 31b69c1 | 2009-07-13 22:20:53 +0000 | [diff] [blame] | 4927 | assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4928 | |
| 4929 | // Forget all the expressions associated with users of the old value, |
| 4930 | // so that future queries will recompute the expressions using the new |
| 4931 | // value. |
| 4932 | SmallVector<User *, 16> Worklist; |
Dan Gohman | 6b9da31 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 4933 | SmallPtrSet<User *, 8> Visited; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4934 | Value *Old = getValPtr(); |
| 4935 | bool DeleteOld = false; |
| 4936 | for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end(); |
| 4937 | UI != UE; ++UI) |
| 4938 | Worklist.push_back(*UI); |
| 4939 | while (!Worklist.empty()) { |
| 4940 | User *U = Worklist.pop_back_val(); |
| 4941 | // Deleting the Old value will cause this to dangle. Postpone |
| 4942 | // that until everything else is done. |
| 4943 | if (U == Old) { |
| 4944 | DeleteOld = true; |
| 4945 | continue; |
| 4946 | } |
Dan Gohman | 6b9da31 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 4947 | if (!Visited.insert(U)) |
| 4948 | continue; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4949 | if (PHINode *PN = dyn_cast<PHINode>(U)) |
| 4950 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 4951 | if (Instruction *I = dyn_cast<Instruction>(U)) |
| 4952 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | 6b9da31 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 4953 | SE->Scalars.erase(U); |
| 4954 | for (Value::use_iterator UI = U->use_begin(), UE = U->use_end(); |
| 4955 | UI != UE; ++UI) |
| 4956 | Worklist.push_back(*UI); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4957 | } |
Dan Gohman | 6b9da31 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 4958 | // Delete the Old value if it (indirectly) references itself. |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4959 | if (DeleteOld) { |
| 4960 | if (PHINode *PN = dyn_cast<PHINode>(Old)) |
| 4961 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 4962 | if (Instruction *I = dyn_cast<Instruction>(Old)) |
| 4963 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4964 | SE->Scalars.erase(Old); |
| 4965 | // this now dangles! |
| 4966 | } |
| 4967 | // this may dangle! |
| 4968 | } |
| 4969 | |
Dan Gohman | 999d14e | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 4970 | ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4971 | : CallbackVH(V), SE(se) {} |
| 4972 | |
| 4973 | //===----------------------------------------------------------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4974 | // ScalarEvolution Class Implementation |
| 4975 | //===----------------------------------------------------------------------===// |
| 4976 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4977 | ScalarEvolution::ScalarEvolution() |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4978 | : FunctionPass(&ID) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4979 | } |
| 4980 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4981 | bool ScalarEvolution::runOnFunction(Function &F) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4982 | this->F = &F; |
| 4983 | LI = &getAnalysis<LoopInfo>(); |
| 4984 | TD = getAnalysisIfAvailable<TargetData>(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4985 | return false; |
| 4986 | } |
| 4987 | |
| 4988 | void ScalarEvolution::releaseMemory() { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4989 | Scalars.clear(); |
| 4990 | BackedgeTakenCounts.clear(); |
| 4991 | ConstantEvolutionLoopExitValue.clear(); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 4992 | ValuesAtScopes.clear(); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4993 | UniqueSCEVs.clear(); |
| 4994 | SCEVAllocator.Reset(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4995 | } |
| 4996 | |
| 4997 | void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const { |
| 4998 | AU.setPreservesAll(); |
| 4999 | AU.addRequiredTransitive<LoopInfo>(); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 5000 | } |
| 5001 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5002 | bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5003 | return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5004 | } |
| 5005 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5006 | static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5007 | const Loop *L) { |
| 5008 | // Print all inner loops first |
| 5009 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 5010 | PrintLoopInfo(OS, SE, *I); |
| 5011 | |
Nick Lewycky | e5da191 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 5012 | OS << "Loop " << L->getHeader()->getName() << ": "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5013 | |
Devang Patel | 02451fa | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 5014 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5015 | L->getExitBlocks(ExitBlocks); |
| 5016 | if (ExitBlocks.size() != 1) |
Nick Lewycky | e5da191 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 5017 | OS << "<multiple exits> "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5018 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5019 | if (SE->hasLoopInvariantBackedgeTakenCount(L)) { |
| 5020 | OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5021 | } else { |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5022 | OS << "Unpredictable backedge-taken count. "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
Nick Lewycky | e5da191 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 5025 | OS << "\n"; |
Dan Gohman | b6b9e9e | 2009-06-24 00:33:16 +0000 | [diff] [blame] | 5026 | OS << "Loop " << L->getHeader()->getName() << ": "; |
| 5027 | |
| 5028 | if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) { |
| 5029 | OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L); |
| 5030 | } else { |
| 5031 | OS << "Unpredictable max backedge-taken count. "; |
| 5032 | } |
| 5033 | |
| 5034 | OS << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5035 | } |
| 5036 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 5037 | void ScalarEvolution::print(raw_ostream &OS, const Module* ) const { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5038 | // ScalarEvolution's implementaiton of the print method is to print |
| 5039 | // out SCEV values of all instructions that are interesting. Doing |
| 5040 | // this potentially causes it to create new SCEV objects though, |
| 5041 | // which technically conflicts with the const qualifier. This isn't |
Dan Gohman | ac2a9d6 | 2009-07-10 20:25:29 +0000 | [diff] [blame] | 5042 | // observable from outside the class though, so casting away the |
| 5043 | // const isn't dangerous. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5044 | ScalarEvolution &SE = *const_cast<ScalarEvolution*>(this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5045 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5046 | OS << "Classifying expressions for: " << F->getName() << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5047 | 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] | 5048 | if (isSCEVable(I->getType())) { |
Dan Gohman | 12668ad | 2009-07-13 23:03:05 +0000 | [diff] [blame] | 5049 | OS << *I << '\n'; |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 5050 | OS << " --> "; |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5051 | const SCEV *SV = SE.getSCEV(&*I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5052 | SV->print(OS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5053 | |
Dan Gohman | 8db598a | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 5054 | const Loop *L = LI->getLoopFor((*I).getParent()); |
| 5055 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5056 | const SCEV *AtUse = SE.getSCEVAtScope(SV, L); |
Dan Gohman | 8db598a | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 5057 | if (AtUse != SV) { |
| 5058 | OS << " --> "; |
| 5059 | AtUse->print(OS); |
| 5060 | } |
| 5061 | |
| 5062 | if (L) { |
Dan Gohman | e5b6084 | 2009-06-18 00:37:45 +0000 | [diff] [blame] | 5063 | OS << "\t\t" "Exits: "; |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5064 | const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop()); |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 5065 | if (!ExitValue->isLoopInvariant(L)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5066 | OS << "<<Unknown>>"; |
| 5067 | } else { |
| 5068 | OS << *ExitValue; |
| 5069 | } |
| 5070 | } |
| 5071 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5072 | OS << "\n"; |
| 5073 | } |
| 5074 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5075 | OS << "Determining loop execution counts for: " << F->getName() << "\n"; |
| 5076 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
| 5077 | PrintLoopInfo(OS, &SE, *I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5078 | } |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 5079 | |
| 5080 | void ScalarEvolution::print(std::ostream &o, const Module *M) const { |
| 5081 | raw_os_ostream OS(o); |
| 5082 | print(OS, M); |
| 5083 | } |