Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1 | //===- ScalarEvolution.cpp - Scalar Evolution Analysis ----------*- C++ -*-===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the implementation of the scalar evolution analysis |
| 11 | // engine, which is used primarily to analyze expressions involving induction |
| 12 | // variables in loops. |
| 13 | // |
| 14 | // There are several aspects to this library. First is the representation of |
| 15 | // scalar expressions, which are represented as subclasses of the SCEV class. |
| 16 | // These classes are used to represent certain types of subexpressions that we |
Dan Gohman | bc3d77a | 2009-07-25 16:18:07 +0000 | [diff] [blame] | 17 | // can handle. We only create one SCEV of a particular shape, so |
| 18 | // pointer-comparisons for equality are legal. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 19 | // |
| 20 | // One important aspect of the SCEV objects is that they are never cyclic, even |
| 21 | // if there is a cycle in the dataflow for an expression (ie, a PHI node). If |
| 22 | // the PHI node is one of the idioms that we can represent (e.g., a polynomial |
| 23 | // recurrence) then we represent it directly as a recurrence node, otherwise we |
| 24 | // represent it as a SCEVUnknown node. |
| 25 | // |
| 26 | // In addition to being able to represent expressions of various types, we also |
| 27 | // have folders that are used to build the *canonical* representation for a |
| 28 | // particular expression. These folders are capable of using a variety of |
| 29 | // rewrite rules to simplify the expressions. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 30 | // |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 31 | // Once the folders are defined, we can implement the more interesting |
| 32 | // higher-level code, such as the code that recognizes PHI nodes of various |
| 33 | // types, computes the execution count of a loop, etc. |
| 34 | // |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 35 | // TODO: We should use these routines and value representations to implement |
| 36 | // dependence analysis! |
| 37 | // |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | // |
| 40 | // There are several good references for the techniques used in this analysis. |
| 41 | // |
| 42 | // Chains of recurrences -- a method to expedite the evaluation |
| 43 | // of closed-form functions |
| 44 | // Olaf Bachmann, Paul S. Wang, Eugene V. Zima |
| 45 | // |
| 46 | // On computational properties of chains of recurrences |
| 47 | // Eugene V. Zima |
| 48 | // |
| 49 | // Symbolic Evaluation of Chains of Recurrences for Loop Optimization |
| 50 | // Robert A. van Engelen |
| 51 | // |
| 52 | // Efficient Symbolic Analysis for Optimizing Compilers |
| 53 | // Robert A. van Engelen |
| 54 | // |
| 55 | // Using the chains of recurrences algebra for data dependence testing and |
| 56 | // induction variable substitution |
| 57 | // MS Thesis, Johnie Birch |
| 58 | // |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
Chris Lattner | 3b27d68 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 61 | #define DEBUG_TYPE "scalar-evolution" |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 62 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 63 | #include "llvm/Constants.h" |
| 64 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 65 | #include "llvm/GlobalVariable.h" |
Dan Gohman | 2681232 | 2009-08-25 17:49:57 +0000 | [diff] [blame] | 66 | #include "llvm/GlobalAlias.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 67 | #include "llvm/Instructions.h" |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 68 | #include "llvm/LLVMContext.h" |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 69 | #include "llvm/Operator.h" |
John Criswell | a115643 | 2005-10-27 15:54:34 +0000 | [diff] [blame] | 70 | #include "llvm/Analysis/ConstantFolding.h" |
Evan Cheng | 5a6c1a8 | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 71 | #include "llvm/Analysis/Dominators.h" |
Duncan Sands | a0c5244 | 2010-11-17 04:18:45 +0000 | [diff] [blame] | 72 | #include "llvm/Analysis/InstructionSimplify.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 73 | #include "llvm/Analysis/LoopInfo.h" |
Dan Gohman | 61ffa8e | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 74 | #include "llvm/Analysis/ValueTracking.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 75 | #include "llvm/Assembly/Writer.h" |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 76 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 77 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 78 | #include "llvm/Support/ConstantRange.h" |
David Greene | 63c9463 | 2009-12-23 22:58:38 +0000 | [diff] [blame] | 79 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 80 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 81 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 82 | #include "llvm/Support/InstIterator.h" |
Chris Lattner | 75de5ab | 2006-12-19 01:16:02 +0000 | [diff] [blame] | 83 | #include "llvm/Support/MathExtras.h" |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 84 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 85 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 86 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 87 | #include "llvm/ADT/SmallPtrSet.h" |
Alkis Evlogimenos | 20aa474 | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 88 | #include <algorithm> |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 89 | using namespace llvm; |
| 90 | |
Chris Lattner | 3b27d68 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 91 | STATISTIC(NumArrayLenItCounts, |
| 92 | "Number of trip counts computed with array length"); |
| 93 | STATISTIC(NumTripCountsComputed, |
| 94 | "Number of loops with predictable loop counts"); |
| 95 | STATISTIC(NumTripCountsNotComputed, |
| 96 | "Number of loops without predictable loop counts"); |
| 97 | STATISTIC(NumBruteForceTripCountsComputed, |
| 98 | "Number of loops with trip counts computed by force"); |
| 99 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 100 | static cl::opt<unsigned> |
Chris Lattner | 3b27d68 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 101 | MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, |
| 102 | cl::desc("Maximum number of iterations SCEV will " |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 103 | "symbolically execute a constant " |
| 104 | "derived loop"), |
Chris Lattner | 3b27d68 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 105 | cl::init(100)); |
| 106 | |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 107 | INITIALIZE_PASS_BEGIN(ScalarEvolution, "scalar-evolution", |
| 108 | "Scalar Evolution Analysis", false, true) |
| 109 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 110 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 111 | INITIALIZE_PASS_END(ScalarEvolution, "scalar-evolution", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 112 | "Scalar Evolution Analysis", false, true) |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 113 | char ScalarEvolution::ID = 0; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 114 | |
| 115 | //===----------------------------------------------------------------------===// |
| 116 | // SCEV class definitions |
| 117 | //===----------------------------------------------------------------------===// |
| 118 | |
| 119 | //===----------------------------------------------------------------------===// |
| 120 | // Implementation of the SCEV class. |
| 121 | // |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 123 | void SCEV::dump() const { |
David Greene | 25e0e87 | 2009-12-23 22:18:14 +0000 | [diff] [blame] | 124 | print(dbgs()); |
| 125 | dbgs() << '\n'; |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Dan Gohman | 4ce32db | 2010-11-17 22:27:42 +0000 | [diff] [blame] | 128 | void SCEV::print(raw_ostream &OS) const { |
| 129 | switch (getSCEVType()) { |
| 130 | case scConstant: |
| 131 | WriteAsOperand(OS, cast<SCEVConstant>(this)->getValue(), false); |
| 132 | return; |
| 133 | case scTruncate: { |
| 134 | const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(this); |
| 135 | const SCEV *Op = Trunc->getOperand(); |
| 136 | OS << "(trunc " << *Op->getType() << " " << *Op << " to " |
| 137 | << *Trunc->getType() << ")"; |
| 138 | return; |
| 139 | } |
| 140 | case scZeroExtend: { |
| 141 | const SCEVZeroExtendExpr *ZExt = cast<SCEVZeroExtendExpr>(this); |
| 142 | const SCEV *Op = ZExt->getOperand(); |
| 143 | OS << "(zext " << *Op->getType() << " " << *Op << " to " |
| 144 | << *ZExt->getType() << ")"; |
| 145 | return; |
| 146 | } |
| 147 | case scSignExtend: { |
| 148 | const SCEVSignExtendExpr *SExt = cast<SCEVSignExtendExpr>(this); |
| 149 | const SCEV *Op = SExt->getOperand(); |
| 150 | OS << "(sext " << *Op->getType() << " " << *Op << " to " |
| 151 | << *SExt->getType() << ")"; |
| 152 | return; |
| 153 | } |
| 154 | case scAddRecExpr: { |
| 155 | const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(this); |
| 156 | OS << "{" << *AR->getOperand(0); |
| 157 | for (unsigned i = 1, e = AR->getNumOperands(); i != e; ++i) |
| 158 | OS << ",+," << *AR->getOperand(i); |
| 159 | OS << "}<"; |
| 160 | WriteAsOperand(OS, AR->getLoop()->getHeader(), /*PrintType=*/false); |
| 161 | OS << ">"; |
| 162 | return; |
| 163 | } |
| 164 | case scAddExpr: |
| 165 | case scMulExpr: |
| 166 | case scUMaxExpr: |
| 167 | case scSMaxExpr: { |
| 168 | const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(this); |
Benjamin Kramer | b458b15 | 2010-11-19 11:37:26 +0000 | [diff] [blame^] | 169 | const char *OpStr = 0; |
Dan Gohman | 4ce32db | 2010-11-17 22:27:42 +0000 | [diff] [blame] | 170 | switch (NAry->getSCEVType()) { |
| 171 | case scAddExpr: OpStr = " + "; break; |
| 172 | case scMulExpr: OpStr = " * "; break; |
| 173 | case scUMaxExpr: OpStr = " umax "; break; |
| 174 | case scSMaxExpr: OpStr = " smax "; break; |
| 175 | } |
| 176 | OS << "("; |
| 177 | for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end(); |
| 178 | I != E; ++I) { |
| 179 | OS << **I; |
| 180 | if (llvm::next(I) != E) |
| 181 | OS << OpStr; |
| 182 | } |
| 183 | OS << ")"; |
| 184 | return; |
| 185 | } |
| 186 | case scUDivExpr: { |
| 187 | const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(this); |
| 188 | OS << "(" << *UDiv->getLHS() << " /u " << *UDiv->getRHS() << ")"; |
| 189 | return; |
| 190 | } |
| 191 | case scUnknown: { |
| 192 | const SCEVUnknown *U = cast<SCEVUnknown>(this); |
| 193 | const Type *AllocTy; |
| 194 | if (U->isSizeOf(AllocTy)) { |
| 195 | OS << "sizeof(" << *AllocTy << ")"; |
| 196 | return; |
| 197 | } |
| 198 | if (U->isAlignOf(AllocTy)) { |
| 199 | OS << "alignof(" << *AllocTy << ")"; |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | const Type *CTy; |
| 204 | Constant *FieldNo; |
| 205 | if (U->isOffsetOf(CTy, FieldNo)) { |
| 206 | OS << "offsetof(" << *CTy << ", "; |
| 207 | WriteAsOperand(OS, FieldNo, false); |
| 208 | OS << ")"; |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | // Otherwise just print it normally. |
| 213 | WriteAsOperand(OS, U->getValue(), false); |
| 214 | return; |
| 215 | } |
| 216 | case scCouldNotCompute: |
| 217 | OS << "***COULDNOTCOMPUTE***"; |
| 218 | return; |
| 219 | default: break; |
| 220 | } |
| 221 | llvm_unreachable("Unknown SCEV kind!"); |
| 222 | } |
| 223 | |
| 224 | const Type *SCEV::getType() const { |
| 225 | switch (getSCEVType()) { |
| 226 | case scConstant: |
| 227 | return cast<SCEVConstant>(this)->getType(); |
| 228 | case scTruncate: |
| 229 | case scZeroExtend: |
| 230 | case scSignExtend: |
| 231 | return cast<SCEVCastExpr>(this)->getType(); |
| 232 | case scAddRecExpr: |
| 233 | case scMulExpr: |
| 234 | case scUMaxExpr: |
| 235 | case scSMaxExpr: |
| 236 | return cast<SCEVNAryExpr>(this)->getType(); |
| 237 | case scAddExpr: |
| 238 | return cast<SCEVAddExpr>(this)->getType(); |
| 239 | case scUDivExpr: |
| 240 | return cast<SCEVUDivExpr>(this)->getType(); |
| 241 | case scUnknown: |
| 242 | return cast<SCEVUnknown>(this)->getType(); |
| 243 | case scCouldNotCompute: |
| 244 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
| 245 | return 0; |
| 246 | default: break; |
| 247 | } |
| 248 | llvm_unreachable("Unknown SCEV kind!"); |
| 249 | return 0; |
| 250 | } |
| 251 | |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 252 | bool SCEV::isZero() const { |
| 253 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 254 | return SC->getValue()->isZero(); |
| 255 | return false; |
| 256 | } |
| 257 | |
Dan Gohman | 70a1fe7 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 258 | bool SCEV::isOne() const { |
| 259 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 260 | return SC->getValue()->isOne(); |
| 261 | return false; |
| 262 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 263 | |
Dan Gohman | 4d289bf | 2009-06-24 00:30:26 +0000 | [diff] [blame] | 264 | bool SCEV::isAllOnesValue() const { |
| 265 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 266 | return SC->getValue()->isAllOnesValue(); |
| 267 | return false; |
| 268 | } |
| 269 | |
Owen Anderson | 753ad61 | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 270 | SCEVCouldNotCompute::SCEVCouldNotCompute() : |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 271 | SCEV(FoldingSetNodeIDRef(), scCouldNotCompute) {} |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 273 | bool SCEVCouldNotCompute::classof(const SCEV *S) { |
| 274 | return S->getSCEVType() == scCouldNotCompute; |
| 275 | } |
| 276 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 277 | const SCEV *ScalarEvolution::getConstant(ConstantInt *V) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 278 | FoldingSetNodeID ID; |
| 279 | ID.AddInteger(scConstant); |
| 280 | ID.AddPointer(V); |
| 281 | void *IP = 0; |
| 282 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 283 | SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 284 | UniqueSCEVs.InsertNode(S, IP); |
| 285 | return S; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 286 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 287 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 288 | const SCEV *ScalarEvolution::getConstant(const APInt& Val) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 289 | return getConstant(ConstantInt::get(getContext(), Val)); |
Dan Gohman | 9a6ae96 | 2007-07-09 15:25:17 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 292 | const SCEV * |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 293 | ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) { |
Dan Gohman | a560fd2 | 2010-04-21 16:04:04 +0000 | [diff] [blame] | 294 | const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty)); |
| 295 | return getConstant(ConstantInt::get(ITy, V, isSigned)); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 298 | SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID, |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 299 | unsigned SCEVTy, const SCEV *op, const Type *ty) |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 300 | : SCEV(ID, SCEVTy), Op(op), Ty(ty) {} |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 301 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 302 | SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID, |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 303 | const SCEV *op, const Type *ty) |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 304 | : SCEVCastExpr(ID, scTruncate, op, ty) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 305 | assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) && |
| 306 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 307 | "Cannot truncate non-integer value!"); |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 308 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 309 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 310 | SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID, |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 311 | const SCEV *op, const Type *ty) |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 312 | : SCEVCastExpr(ID, scZeroExtend, op, ty) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 313 | assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) && |
| 314 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 315 | "Cannot zero extend non-integer value!"); |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 318 | SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID, |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 319 | const SCEV *op, const Type *ty) |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 320 | : SCEVCastExpr(ID, scSignExtend, op, ty) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 321 | assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) && |
| 322 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 323 | "Cannot sign extend non-integer value!"); |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 326 | void SCEVUnknown::deleted() { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 327 | // Clear this SCEVUnknown from various maps. |
Dan Gohman | 56a7568 | 2010-11-17 23:28:48 +0000 | [diff] [blame] | 328 | SE->forgetMemoizedResults(this); |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 329 | |
| 330 | // Remove this SCEVUnknown from the uniquing map. |
| 331 | SE->UniqueSCEVs.RemoveNode(this); |
| 332 | |
| 333 | // Release the value. |
| 334 | setValPtr(0); |
| 335 | } |
| 336 | |
| 337 | void SCEVUnknown::allUsesReplacedWith(Value *New) { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 338 | // Clear this SCEVUnknown from various maps. |
Dan Gohman | 56a7568 | 2010-11-17 23:28:48 +0000 | [diff] [blame] | 339 | SE->forgetMemoizedResults(this); |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 340 | |
| 341 | // Remove this SCEVUnknown from the uniquing map. |
| 342 | SE->UniqueSCEVs.RemoveNode(this); |
| 343 | |
| 344 | // Update this SCEVUnknown to point to the new value. This is needed |
| 345 | // because there may still be outstanding SCEVs which still point to |
| 346 | // this SCEVUnknown. |
| 347 | setValPtr(New); |
| 348 | } |
| 349 | |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 350 | bool SCEVUnknown::isSizeOf(const Type *&AllocTy) const { |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 351 | if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue())) |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 352 | if (VCE->getOpcode() == Instruction::PtrToInt) |
| 353 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0))) |
Dan Gohman | 8db08df | 2010-02-02 01:38:49 +0000 | [diff] [blame] | 354 | if (CE->getOpcode() == Instruction::GetElementPtr && |
| 355 | CE->getOperand(0)->isNullValue() && |
| 356 | CE->getNumOperands() == 2) |
| 357 | if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(1))) |
| 358 | if (CI->isOne()) { |
| 359 | AllocTy = cast<PointerType>(CE->getOperand(0)->getType()) |
| 360 | ->getElementType(); |
| 361 | return true; |
| 362 | } |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 363 | |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | bool SCEVUnknown::isAlignOf(const Type *&AllocTy) const { |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 368 | if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue())) |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 369 | if (VCE->getOpcode() == Instruction::PtrToInt) |
| 370 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0))) |
Dan Gohman | 8db08df | 2010-02-02 01:38:49 +0000 | [diff] [blame] | 371 | if (CE->getOpcode() == Instruction::GetElementPtr && |
| 372 | CE->getOperand(0)->isNullValue()) { |
| 373 | const Type *Ty = |
| 374 | cast<PointerType>(CE->getOperand(0)->getType())->getElementType(); |
| 375 | if (const StructType *STy = dyn_cast<StructType>(Ty)) |
| 376 | if (!STy->isPacked() && |
| 377 | CE->getNumOperands() == 3 && |
| 378 | CE->getOperand(1)->isNullValue()) { |
| 379 | if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(2))) |
| 380 | if (CI->isOne() && |
| 381 | STy->getNumElements() == 2 && |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 382 | STy->getElementType(0)->isIntegerTy(1)) { |
Dan Gohman | 8db08df | 2010-02-02 01:38:49 +0000 | [diff] [blame] | 383 | AllocTy = STy->getElementType(1); |
| 384 | return true; |
| 385 | } |
| 386 | } |
| 387 | } |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 388 | |
| 389 | return false; |
| 390 | } |
| 391 | |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 392 | bool SCEVUnknown::isOffsetOf(const Type *&CTy, Constant *&FieldNo) const { |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 393 | if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue())) |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 394 | if (VCE->getOpcode() == Instruction::PtrToInt) |
| 395 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0))) |
| 396 | if (CE->getOpcode() == Instruction::GetElementPtr && |
| 397 | CE->getNumOperands() == 3 && |
| 398 | CE->getOperand(0)->isNullValue() && |
| 399 | CE->getOperand(1)->isNullValue()) { |
| 400 | const Type *Ty = |
| 401 | cast<PointerType>(CE->getOperand(0)->getType())->getElementType(); |
| 402 | // Ignore vector types here so that ScalarEvolutionExpander doesn't |
| 403 | // emit getelementptrs that index into vectors. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 404 | if (Ty->isStructTy() || Ty->isArrayTy()) { |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 405 | CTy = Ty; |
| 406 | FieldNo = CE->getOperand(2); |
| 407 | return true; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | return false; |
| 412 | } |
| 413 | |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 414 | //===----------------------------------------------------------------------===// |
| 415 | // SCEV Utilities |
| 416 | //===----------------------------------------------------------------------===// |
| 417 | |
| 418 | namespace { |
| 419 | /// SCEVComplexityCompare - Return true if the complexity of the LHS is less |
| 420 | /// than the complexity of the RHS. This comparator is used to canonicalize |
| 421 | /// expressions. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 422 | class SCEVComplexityCompare { |
Dan Gohman | 9f1fb42 | 2010-08-13 20:17:27 +0000 | [diff] [blame] | 423 | const LoopInfo *const LI; |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 424 | public: |
Dan Gohman | e72079a | 2010-07-23 21:18:55 +0000 | [diff] [blame] | 425 | explicit SCEVComplexityCompare(const LoopInfo *li) : LI(li) {} |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 426 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 427 | // Return true or false if LHS is less than, or at least RHS, respectively. |
Dan Gohman | f7b37b2 | 2008-04-14 18:23:56 +0000 | [diff] [blame] | 428 | bool operator()(const SCEV *LHS, const SCEV *RHS) const { |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 429 | return compare(LHS, RHS) < 0; |
| 430 | } |
| 431 | |
| 432 | // Return negative, zero, or positive, if LHS is less than, equal to, or |
| 433 | // greater than RHS, respectively. A three-way result allows recursive |
| 434 | // comparisons to be more efficient. |
| 435 | int compare(const SCEV *LHS, const SCEV *RHS) const { |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 436 | // Fast-path: SCEVs are uniqued so we can do a quick equality check. |
| 437 | if (LHS == RHS) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 438 | return 0; |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 439 | |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 440 | // Primarily, sort the SCEVs by their getSCEVType(). |
Dan Gohman | 304a7a6 | 2010-07-23 21:20:52 +0000 | [diff] [blame] | 441 | unsigned LType = LHS->getSCEVType(), RType = RHS->getSCEVType(); |
| 442 | if (LType != RType) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 443 | return (int)LType - (int)RType; |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 444 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 445 | // Aside from the getSCEVType() ordering, the particular ordering |
| 446 | // isn't very important except that it's beneficial to be consistent, |
| 447 | // so that (a + b) and (b + a) don't end up as different expressions. |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 448 | switch (LType) { |
| 449 | case scUnknown: { |
| 450 | const SCEVUnknown *LU = cast<SCEVUnknown>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 451 | const SCEVUnknown *RU = cast<SCEVUnknown>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 452 | |
| 453 | // Sort SCEVUnknown values with some loose heuristics. TODO: This is |
| 454 | // not as complete as it could be. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 455 | const Value *LV = LU->getValue(), *RV = RU->getValue(); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 456 | |
| 457 | // Order pointer values after integer values. This helps SCEVExpander |
| 458 | // form GEPs. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 459 | bool LIsPointer = LV->getType()->isPointerTy(), |
| 460 | RIsPointer = RV->getType()->isPointerTy(); |
Dan Gohman | 304a7a6 | 2010-07-23 21:20:52 +0000 | [diff] [blame] | 461 | if (LIsPointer != RIsPointer) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 462 | return (int)LIsPointer - (int)RIsPointer; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 463 | |
| 464 | // Compare getValueID values. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 465 | unsigned LID = LV->getValueID(), |
| 466 | RID = RV->getValueID(); |
Dan Gohman | 304a7a6 | 2010-07-23 21:20:52 +0000 | [diff] [blame] | 467 | if (LID != RID) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 468 | return (int)LID - (int)RID; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 469 | |
| 470 | // Sort arguments by their position. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 471 | if (const Argument *LA = dyn_cast<Argument>(LV)) { |
| 472 | const Argument *RA = cast<Argument>(RV); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 473 | unsigned LArgNo = LA->getArgNo(), RArgNo = RA->getArgNo(); |
| 474 | return (int)LArgNo - (int)RArgNo; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 477 | // For instructions, compare their loop depth, and their operand |
| 478 | // count. This is pretty loose. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 479 | if (const Instruction *LInst = dyn_cast<Instruction>(LV)) { |
| 480 | const Instruction *RInst = cast<Instruction>(RV); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 481 | |
| 482 | // Compare loop depths. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 483 | const BasicBlock *LParent = LInst->getParent(), |
| 484 | *RParent = RInst->getParent(); |
| 485 | if (LParent != RParent) { |
| 486 | unsigned LDepth = LI->getLoopDepth(LParent), |
| 487 | RDepth = LI->getLoopDepth(RParent); |
| 488 | if (LDepth != RDepth) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 489 | return (int)LDepth - (int)RDepth; |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 490 | } |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 491 | |
| 492 | // Compare the number of operands. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 493 | unsigned LNumOps = LInst->getNumOperands(), |
| 494 | RNumOps = RInst->getNumOperands(); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 495 | return (int)LNumOps - (int)RNumOps; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 498 | return 0; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 501 | case scConstant: { |
| 502 | const SCEVConstant *LC = cast<SCEVConstant>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 503 | const SCEVConstant *RC = cast<SCEVConstant>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 504 | |
| 505 | // Compare constant values. |
Dan Gohman | e28d792 | 2010-08-16 16:25:35 +0000 | [diff] [blame] | 506 | const APInt &LA = LC->getValue()->getValue(); |
| 507 | const APInt &RA = RC->getValue()->getValue(); |
| 508 | unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.getBitWidth(); |
Dan Gohman | 304a7a6 | 2010-07-23 21:20:52 +0000 | [diff] [blame] | 509 | if (LBitWidth != RBitWidth) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 510 | return (int)LBitWidth - (int)RBitWidth; |
| 511 | return LA.ult(RA) ? -1 : 1; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 514 | case scAddRecExpr: { |
| 515 | const SCEVAddRecExpr *LA = cast<SCEVAddRecExpr>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 516 | const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 517 | |
| 518 | // Compare addrec loop depths. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 519 | const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop(); |
| 520 | if (LLoop != RLoop) { |
| 521 | unsigned LDepth = LLoop->getLoopDepth(), |
| 522 | RDepth = RLoop->getLoopDepth(); |
| 523 | if (LDepth != RDepth) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 524 | return (int)LDepth - (int)RDepth; |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 525 | } |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 526 | |
| 527 | // Addrec complexity grows with operand count. |
| 528 | unsigned LNumOps = LA->getNumOperands(), RNumOps = RA->getNumOperands(); |
| 529 | if (LNumOps != RNumOps) |
| 530 | return (int)LNumOps - (int)RNumOps; |
| 531 | |
| 532 | // Lexicographically compare. |
| 533 | for (unsigned i = 0; i != LNumOps; ++i) { |
| 534 | long X = compare(LA->getOperand(i), RA->getOperand(i)); |
| 535 | if (X != 0) |
| 536 | return X; |
| 537 | } |
| 538 | |
| 539 | return 0; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 542 | case scAddExpr: |
| 543 | case scMulExpr: |
| 544 | case scSMaxExpr: |
| 545 | case scUMaxExpr: { |
| 546 | const SCEVNAryExpr *LC = cast<SCEVNAryExpr>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 547 | const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 548 | |
| 549 | // Lexicographically compare n-ary expressions. |
Dan Gohman | 304a7a6 | 2010-07-23 21:20:52 +0000 | [diff] [blame] | 550 | unsigned LNumOps = LC->getNumOperands(), RNumOps = RC->getNumOperands(); |
| 551 | for (unsigned i = 0; i != LNumOps; ++i) { |
| 552 | if (i >= RNumOps) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 553 | return 1; |
| 554 | long X = compare(LC->getOperand(i), RC->getOperand(i)); |
| 555 | if (X != 0) |
| 556 | return X; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 557 | } |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 558 | return (int)LNumOps - (int)RNumOps; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 561 | case scUDivExpr: { |
| 562 | const SCEVUDivExpr *LC = cast<SCEVUDivExpr>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 563 | const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 564 | |
| 565 | // Lexicographically compare udiv expressions. |
| 566 | long X = compare(LC->getLHS(), RC->getLHS()); |
| 567 | if (X != 0) |
| 568 | return X; |
| 569 | return compare(LC->getRHS(), RC->getRHS()); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 572 | case scTruncate: |
| 573 | case scZeroExtend: |
| 574 | case scSignExtend: { |
| 575 | const SCEVCastExpr *LC = cast<SCEVCastExpr>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 576 | const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 577 | |
| 578 | // Compare cast expressions by operand. |
| 579 | return compare(LC->getOperand(), RC->getOperand()); |
| 580 | } |
| 581 | |
| 582 | default: |
| 583 | break; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | llvm_unreachable("Unknown SCEV kind!"); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 587 | return 0; |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 588 | } |
| 589 | }; |
| 590 | } |
| 591 | |
| 592 | /// GroupByComplexity - Given a list of SCEV objects, order them by their |
| 593 | /// complexity, and group objects of the same complexity together by value. |
| 594 | /// When this routine is finished, we know that any duplicates in the vector are |
| 595 | /// consecutive and that complexity is monotonically increasing. |
| 596 | /// |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 597 | /// Note that we go take special precautions to ensure that we get deterministic |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 598 | /// results from this routine. In other words, we don't want the results of |
| 599 | /// this to depend on where the addresses of various SCEV objects happened to |
| 600 | /// land in memory. |
| 601 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 602 | static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops, |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 603 | LoopInfo *LI) { |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 604 | if (Ops.size() < 2) return; // Noop |
| 605 | if (Ops.size() == 2) { |
| 606 | // This is the common case, which also happens to be trivially simple. |
| 607 | // Special case it. |
Dan Gohman | c6a8e99 | 2010-08-29 15:07:13 +0000 | [diff] [blame] | 608 | const SCEV *&LHS = Ops[0], *&RHS = Ops[1]; |
| 609 | if (SCEVComplexityCompare(LI)(RHS, LHS)) |
| 610 | std::swap(LHS, RHS); |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 611 | return; |
| 612 | } |
| 613 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 614 | // Do the rough sort by complexity. |
| 615 | std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI)); |
| 616 | |
| 617 | // Now that we are sorted by complexity, group elements of the same |
| 618 | // complexity. Note that this is, at worst, N^2, but the vector is likely to |
| 619 | // be extremely short in practice. Note that we take this approach because we |
| 620 | // do not want to depend on the addresses of the objects we are grouping. |
| 621 | for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { |
| 622 | const SCEV *S = Ops[i]; |
| 623 | unsigned Complexity = S->getSCEVType(); |
| 624 | |
| 625 | // If there are any objects of the same complexity and same value as this |
| 626 | // one, group them. |
| 627 | for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { |
| 628 | if (Ops[j] == S) { // Found a duplicate. |
| 629 | // Move it to immediately after i'th element. |
| 630 | std::swap(Ops[i+1], Ops[j]); |
| 631 | ++i; // no need to rescan it. |
| 632 | if (i == e-2) return; // Done! |
| 633 | } |
| 634 | } |
| 635 | } |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 638 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 639 | |
| 640 | //===----------------------------------------------------------------------===// |
| 641 | // Simple SCEV method implementations |
| 642 | //===----------------------------------------------------------------------===// |
| 643 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 644 | /// BinomialCoefficient - Compute BC(It, K). The result has width W. |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 645 | /// Assume, K > 0. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 646 | static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K, |
Dan Gohman | c2b015e | 2009-07-21 00:38:55 +0000 | [diff] [blame] | 647 | ScalarEvolution &SE, |
| 648 | const Type* ResultTy) { |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 649 | // Handle the simplest case efficiently. |
| 650 | if (K == 1) |
| 651 | return SE.getTruncateOrZeroExtend(It, ResultTy); |
| 652 | |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 653 | // We are using the following formula for BC(It, K): |
| 654 | // |
| 655 | // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! |
| 656 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 657 | // Suppose, W is the bitwidth of the return value. We must be prepared for |
| 658 | // overflow. Hence, we must assure that the result of our computation is |
| 659 | // equal to the accurate one modulo 2^W. Unfortunately, division isn't |
| 660 | // safe in modular arithmetic. |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 661 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 662 | // However, this code doesn't use exactly that formula; the formula it uses |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 663 | // is something like the following, where T is the number of factors of 2 in |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 664 | // K! (i.e. trailing zeros in the binary representation of K!), and ^ is |
| 665 | // exponentiation: |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 666 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 667 | // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T) |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 668 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 669 | // This formula is trivially equivalent to the previous formula. However, |
| 670 | // this formula can be implemented much more efficiently. The trick is that |
| 671 | // K! / 2^T is odd, and exact division by an odd number *is* safe in modular |
| 672 | // arithmetic. To do exact division in modular arithmetic, all we have |
| 673 | // to do is multiply by the inverse. Therefore, this step can be done at |
| 674 | // width W. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 675 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 676 | // The next issue is how to safely do the division by 2^T. The way this |
| 677 | // is done is by doing the multiplication step at a width of at least W + T |
| 678 | // bits. This way, the bottom W+T bits of the product are accurate. Then, |
| 679 | // when we perform the division by 2^T (which is equivalent to a right shift |
| 680 | // by T), the bottom W bits are accurate. Extra bits are okay; they'll get |
| 681 | // truncated out after the division by 2^T. |
| 682 | // |
| 683 | // In comparison to just directly using the first formula, this technique |
| 684 | // is much more efficient; using the first formula requires W * K bits, |
| 685 | // but this formula less than W + K bits. Also, the first formula requires |
| 686 | // a division step, whereas this formula only requires multiplies and shifts. |
| 687 | // |
| 688 | // It doesn't matter whether the subtraction step is done in the calculation |
| 689 | // width or the input iteration count's width; if the subtraction overflows, |
| 690 | // the result must be zero anyway. We prefer here to do it in the width of |
| 691 | // the induction variable because it helps a lot for certain cases; CodeGen |
| 692 | // isn't smart enough to ignore the overflow, which leads to much less |
| 693 | // efficient code if the width of the subtraction is wider than the native |
| 694 | // register width. |
| 695 | // |
| 696 | // (It's possible to not widen at all by pulling out factors of 2 before |
| 697 | // the multiplication; for example, K=2 can be calculated as |
| 698 | // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires |
| 699 | // extra arithmetic, so it's not an obvious win, and it gets |
| 700 | // much more complicated for K > 3.) |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 701 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 702 | // Protection from insane SCEVs; this bound is conservative, |
| 703 | // but it probably doesn't matter. |
| 704 | if (K > 1000) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 705 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 706 | |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 707 | unsigned W = SE.getTypeSizeInBits(ResultTy); |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 708 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 709 | // Calculate K! / 2^T and T; we divide out the factors of two before |
| 710 | // multiplying for calculating K! / 2^T to avoid overflow. |
| 711 | // Other overflow doesn't matter because we only care about the bottom |
| 712 | // W bits of the result. |
| 713 | APInt OddFactorial(W, 1); |
| 714 | unsigned T = 1; |
| 715 | for (unsigned i = 3; i <= K; ++i) { |
| 716 | APInt Mult(W, i); |
| 717 | unsigned TwoFactors = Mult.countTrailingZeros(); |
| 718 | T += TwoFactors; |
| 719 | Mult = Mult.lshr(TwoFactors); |
| 720 | OddFactorial *= Mult; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 721 | } |
Nick Lewycky | 6f8abf9 | 2008-06-13 04:38:55 +0000 | [diff] [blame] | 722 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 723 | // We need at least W + T bits for the multiplication step |
Nick Lewycky | 237d873 | 2009-01-25 08:16:27 +0000 | [diff] [blame] | 724 | unsigned CalculationBits = W + T; |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 725 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 726 | // Calculate 2^T, at width T+W. |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 727 | APInt DivFactor = APInt(CalculationBits, 1).shl(T); |
| 728 | |
| 729 | // Calculate the multiplicative inverse of K! / 2^T; |
| 730 | // this multiplication factor will perform the exact division by |
| 731 | // K! / 2^T. |
| 732 | APInt Mod = APInt::getSignedMinValue(W+1); |
| 733 | APInt MultiplyFactor = OddFactorial.zext(W+1); |
| 734 | MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); |
| 735 | MultiplyFactor = MultiplyFactor.trunc(W); |
| 736 | |
| 737 | // Calculate the product, at width T+W |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 738 | const IntegerType *CalculationTy = IntegerType::get(SE.getContext(), |
| 739 | CalculationBits); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 740 | const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 741 | for (unsigned i = 1; i != K; ++i) { |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 742 | const SCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i)); |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 743 | Dividend = SE.getMulExpr(Dividend, |
| 744 | SE.getTruncateOrZeroExtend(S, CalculationTy)); |
| 745 | } |
| 746 | |
| 747 | // Divide by 2^T |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 748 | const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 749 | |
| 750 | // Truncate the result, and divide by K! / 2^T. |
| 751 | |
| 752 | return SE.getMulExpr(SE.getConstant(MultiplyFactor), |
| 753 | SE.getTruncateOrZeroExtend(DivResult, ResultTy)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 756 | /// evaluateAtIteration - Return the value of this chain of recurrences at |
| 757 | /// the specified iteration number. We can evaluate this recurrence by |
| 758 | /// multiplying each element in the chain by the binomial coefficient |
| 759 | /// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as: |
| 760 | /// |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 761 | /// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 762 | /// |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 763 | /// where BC(It, k) stands for binomial coefficient. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 764 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 765 | const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It, |
Dan Gohman | c2b015e | 2009-07-21 00:38:55 +0000 | [diff] [blame] | 766 | ScalarEvolution &SE) const { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 767 | const SCEV *Result = getStart(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 768 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 769 | // The computation is correct in the face of overflow provided that the |
| 770 | // multiplication is performed _after_ the evaluation of the binomial |
| 771 | // coefficient. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 772 | const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType()); |
Nick Lewycky | cb8f1b5 | 2008-10-13 03:58:02 +0000 | [diff] [blame] | 773 | if (isa<SCEVCouldNotCompute>(Coeff)) |
| 774 | return Coeff; |
| 775 | |
| 776 | Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 777 | } |
| 778 | return Result; |
| 779 | } |
| 780 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 781 | //===----------------------------------------------------------------------===// |
| 782 | // SCEV Expression folder implementations |
| 783 | //===----------------------------------------------------------------------===// |
| 784 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 785 | const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, |
Dan Gohman | f5074ec | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 786 | const Type *Ty) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 787 | assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 788 | "This is not a truncating conversion!"); |
Dan Gohman | 10b9479 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 789 | assert(isSCEVable(Ty) && |
| 790 | "This is not a conversion to a SCEVable type!"); |
| 791 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 792 | |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 793 | FoldingSetNodeID ID; |
| 794 | ID.AddInteger(scTruncate); |
| 795 | ID.AddPointer(Op); |
| 796 | ID.AddPointer(Ty); |
| 797 | void *IP = 0; |
| 798 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 799 | |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 800 | // Fold if the operand is constant. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 801 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
Dan Gohman | b8be8b7 | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 802 | return getConstant( |
Dan Gohman | 1faa882 | 2010-06-24 16:33:38 +0000 | [diff] [blame] | 803 | cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), |
| 804 | getEffectiveSCEVType(Ty)))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 805 | |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 806 | // trunc(trunc(x)) --> trunc(x) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 807 | if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 808 | return getTruncateExpr(ST->getOperand(), Ty); |
| 809 | |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 810 | // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 811 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 812 | return getTruncateOrSignExtend(SS->getOperand(), Ty); |
| 813 | |
| 814 | // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 815 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 816 | return getTruncateOrZeroExtend(SZ->getOperand(), Ty); |
| 817 | |
Dan Gohman | 6864db6 | 2009-06-18 16:24:47 +0000 | [diff] [blame] | 818 | // If the input value is a chrec scev, truncate the chrec's operands. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 819 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 820 | SmallVector<const SCEV *, 4> Operands; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 821 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 822 | Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty)); |
| 823 | return getAddRecExpr(Operands, AddRec->getLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Dan Gohman | f53462d | 2010-07-15 20:02:11 +0000 | [diff] [blame] | 826 | // As a special case, fold trunc(undef) to undef. We don't want to |
| 827 | // know too much about SCEVUnknowns, but this special case is handy |
| 828 | // and harmless. |
| 829 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Op)) |
| 830 | if (isa<UndefValue>(U->getValue())) |
| 831 | return getSCEV(UndefValue::get(Ty)); |
| 832 | |
Dan Gohman | 420ab91 | 2010-06-25 18:47:08 +0000 | [diff] [blame] | 833 | // The cast wasn't folded; create an explicit cast node. We can reuse |
| 834 | // the existing insert position since if we get here, we won't have |
| 835 | // made any changes which would invalidate it. |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 836 | SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator), |
| 837 | Op, Ty); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 838 | UniqueSCEVs.InsertNode(S, IP); |
| 839 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 842 | const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op, |
Dan Gohman | f5074ec | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 843 | const Type *Ty) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 844 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | 8170a68 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 845 | "This is not an extending conversion!"); |
Dan Gohman | 10b9479 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 846 | assert(isSCEVable(Ty) && |
| 847 | "This is not a conversion to a SCEVable type!"); |
| 848 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | 8170a68 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 849 | |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 850 | // Fold if the operand is constant. |
Dan Gohman | eaf6cf2 | 2010-06-24 16:47:03 +0000 | [diff] [blame] | 851 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| 852 | return getConstant( |
| 853 | cast<ConstantInt>(ConstantExpr::getZExt(SC->getValue(), |
| 854 | getEffectiveSCEVType(Ty)))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 855 | |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 856 | // zext(zext(x)) --> zext(x) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 857 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 858 | return getZeroExtendExpr(SZ->getOperand(), Ty); |
| 859 | |
Dan Gohman | 69fbc7f | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 860 | // Before doing any expensive analysis, check to see if we've already |
| 861 | // computed a SCEV for this Op and Ty. |
| 862 | FoldingSetNodeID ID; |
| 863 | ID.AddInteger(scZeroExtend); |
| 864 | ID.AddPointer(Op); |
| 865 | ID.AddPointer(Ty); |
| 866 | void *IP = 0; |
| 867 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 868 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 869 | // If the input value is a chrec scev, and we can prove that the value |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 870 | // did not overflow the old, smaller, value, we can zero extend all of the |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 871 | // operands (often constants). This allows analysis of something like |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 872 | // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 873 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 874 | if (AR->isAffine()) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 875 | const SCEV *Start = AR->getStart(); |
| 876 | const SCEV *Step = AR->getStepRecurrence(*this); |
| 877 | unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
| 878 | const Loop *L = AR->getLoop(); |
| 879 | |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 880 | // If we have special knowledge that this addrec won't overflow, |
| 881 | // we don't need to do any further analysis. |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 882 | if (AR->hasNoUnsignedWrap()) |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 883 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 884 | getZeroExtendExpr(Step, Ty), |
| 885 | L); |
| 886 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 887 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 888 | // Note that this serves two purposes: It filters out loops that are |
| 889 | // simply not analyzable, and it covers the case where this code is |
| 890 | // being called from within backedge-taken count analysis, such that |
| 891 | // attempting to ask for the backedge-taken count would likely result |
| 892 | // in infinite recursion. In the later case, the analysis code will |
| 893 | // cope with a conservative value, and it will take care to purge |
| 894 | // that value once it has finished. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 895 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 896 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | f0aa485 | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 897 | // Manually compute the final value for AR, checking for |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 898 | // overflow. |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 899 | |
| 900 | // Check whether the backedge-taken count can be losslessly casted to |
| 901 | // the addrec's type. The count is always unsigned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 902 | const SCEV *CastedMaxBECount = |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 903 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 904 | const SCEV *RecastedMaxBECount = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 905 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 906 | if (MaxBECount == RecastedMaxBECount) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 907 | const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 908 | // Check whether Start+Step*MaxBECount has no unsigned overflow. |
Dan Gohman | 8f767d9 | 2010-02-24 19:31:06 +0000 | [diff] [blame] | 909 | const SCEV *ZMul = getMulExpr(CastedMaxBECount, Step); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 910 | const SCEV *Add = getAddExpr(Start, ZMul); |
| 911 | const SCEV *OperandExtendedAdd = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 912 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 913 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 914 | getZeroExtendExpr(Step, WideTy))); |
| 915 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 916 | // Return the expression with the addrec on the outside. |
| 917 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 918 | getZeroExtendExpr(Step, Ty), |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 919 | L); |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 920 | |
| 921 | // Similar to above, only this time treat the step value as signed. |
| 922 | // This covers loops that count down. |
Dan Gohman | 8f767d9 | 2010-02-24 19:31:06 +0000 | [diff] [blame] | 923 | const SCEV *SMul = getMulExpr(CastedMaxBECount, Step); |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 924 | Add = getAddExpr(Start, SMul); |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 925 | OperandExtendedAdd = |
| 926 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 927 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 928 | getSignExtendExpr(Step, WideTy))); |
| 929 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 930 | // Return the expression with the addrec on the outside. |
| 931 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 932 | getSignExtendExpr(Step, Ty), |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 933 | L); |
| 934 | } |
| 935 | |
| 936 | // If the backedge is guarded by a comparison with the pre-inc value |
| 937 | // the addrec is safe. Also, if the entry is guarded by a comparison |
| 938 | // with the start value and the backedge is guarded by a comparison |
| 939 | // with the post-inc value, the addrec is safe. |
| 940 | if (isKnownPositive(Step)) { |
| 941 | const SCEV *N = getConstant(APInt::getMinValue(BitWidth) - |
| 942 | getUnsignedRange(Step).getUnsignedMax()); |
| 943 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) || |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 944 | (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) && |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 945 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, |
| 946 | AR->getPostIncExpr(*this), N))) |
| 947 | // Return the expression with the addrec on the outside. |
| 948 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 949 | getZeroExtendExpr(Step, Ty), |
| 950 | L); |
| 951 | } else if (isKnownNegative(Step)) { |
| 952 | const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) - |
| 953 | getSignedRange(Step).getSignedMin()); |
Dan Gohman | c0ed009 | 2010-05-04 01:11:15 +0000 | [diff] [blame] | 954 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) || |
| 955 | (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) && |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 956 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, |
| 957 | AR->getPostIncExpr(*this), N))) |
| 958 | // Return the expression with the addrec on the outside. |
| 959 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 960 | getSignExtendExpr(Step, Ty), |
| 961 | L); |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 965 | |
Dan Gohman | 69fbc7f | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 966 | // The cast wasn't folded; create an explicit cast node. |
| 967 | // Recompute the insert position, as it may have been invalidated. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 968 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 969 | SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator), |
| 970 | Op, Ty); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 971 | UniqueSCEVs.InsertNode(S, IP); |
| 972 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 975 | const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op, |
Dan Gohman | f5074ec | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 976 | const Type *Ty) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 977 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 978 | "This is not an extending conversion!"); |
Dan Gohman | 10b9479 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 979 | assert(isSCEVable(Ty) && |
| 980 | "This is not a conversion to a SCEVable type!"); |
| 981 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 982 | |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 983 | // Fold if the operand is constant. |
Dan Gohman | eaf6cf2 | 2010-06-24 16:47:03 +0000 | [diff] [blame] | 984 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| 985 | return getConstant( |
| 986 | cast<ConstantInt>(ConstantExpr::getSExt(SC->getValue(), |
| 987 | getEffectiveSCEVType(Ty)))); |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 988 | |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 989 | // sext(sext(x)) --> sext(x) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 990 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 991 | return getSignExtendExpr(SS->getOperand(), Ty); |
| 992 | |
Dan Gohman | 69fbc7f | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 993 | // Before doing any expensive analysis, check to see if we've already |
| 994 | // computed a SCEV for this Op and Ty. |
| 995 | FoldingSetNodeID ID; |
| 996 | ID.AddInteger(scSignExtend); |
| 997 | ID.AddPointer(Op); |
| 998 | ID.AddPointer(Ty); |
| 999 | void *IP = 0; |
| 1000 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1001 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1002 | // If the input value is a chrec scev, and we can prove that the value |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 1003 | // did not overflow the old, smaller, value, we can sign extend all of the |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1004 | // operands (often constants). This allows analysis of something like |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 1005 | // this: for (signed char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1006 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1007 | if (AR->isAffine()) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1008 | const SCEV *Start = AR->getStart(); |
| 1009 | const SCEV *Step = AR->getStepRecurrence(*this); |
| 1010 | unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
| 1011 | const Loop *L = AR->getLoop(); |
| 1012 | |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 1013 | // If we have special knowledge that this addrec won't overflow, |
| 1014 | // we don't need to do any further analysis. |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 1015 | if (AR->hasNoSignedWrap()) |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 1016 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 1017 | getSignExtendExpr(Step, Ty), |
| 1018 | L); |
| 1019 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1020 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 1021 | // Note that this serves two purposes: It filters out loops that are |
| 1022 | // simply not analyzable, and it covers the case where this code is |
| 1023 | // being called from within backedge-taken count analysis, such that |
| 1024 | // attempting to ask for the backedge-taken count would likely result |
| 1025 | // in infinite recursion. In the later case, the analysis code will |
| 1026 | // cope with a conservative value, and it will take care to purge |
| 1027 | // that value once it has finished. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1028 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 1029 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | f0aa485 | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 1030 | // Manually compute the final value for AR, checking for |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 1031 | // overflow. |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1032 | |
| 1033 | // Check whether the backedge-taken count can be losslessly casted to |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 1034 | // the addrec's type. The count is always unsigned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1035 | const SCEV *CastedMaxBECount = |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 1036 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1037 | const SCEV *RecastedMaxBECount = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 1038 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 1039 | if (MaxBECount == RecastedMaxBECount) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1040 | const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 1041 | // Check whether Start+Step*MaxBECount has no signed overflow. |
Dan Gohman | 8f767d9 | 2010-02-24 19:31:06 +0000 | [diff] [blame] | 1042 | const SCEV *SMul = getMulExpr(CastedMaxBECount, Step); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1043 | const SCEV *Add = getAddExpr(Start, SMul); |
| 1044 | const SCEV *OperandExtendedAdd = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 1045 | getAddExpr(getSignExtendExpr(Start, WideTy), |
| 1046 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 1047 | getSignExtendExpr(Step, WideTy))); |
| 1048 | if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 1049 | // Return the expression with the addrec on the outside. |
| 1050 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 1051 | getSignExtendExpr(Step, Ty), |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1052 | L); |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 1053 | |
| 1054 | // Similar to above, only this time treat the step value as unsigned. |
| 1055 | // This covers loops that count up with an unsigned step. |
Dan Gohman | 8f767d9 | 2010-02-24 19:31:06 +0000 | [diff] [blame] | 1056 | const SCEV *UMul = getMulExpr(CastedMaxBECount, Step); |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 1057 | Add = getAddExpr(Start, UMul); |
| 1058 | OperandExtendedAdd = |
Dan Gohman | 19378d6 | 2009-07-25 16:03:30 +0000 | [diff] [blame] | 1059 | getAddExpr(getSignExtendExpr(Start, WideTy), |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 1060 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 1061 | getZeroExtendExpr(Step, WideTy))); |
Dan Gohman | 19378d6 | 2009-07-25 16:03:30 +0000 | [diff] [blame] | 1062 | if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 1063 | // Return the expression with the addrec on the outside. |
| 1064 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 1065 | getZeroExtendExpr(Step, Ty), |
| 1066 | L); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | // If the backedge is guarded by a comparison with the pre-inc value |
| 1070 | // the addrec is safe. Also, if the entry is guarded by a comparison |
| 1071 | // with the start value and the backedge is guarded by a comparison |
| 1072 | // with the post-inc value, the addrec is safe. |
| 1073 | if (isKnownPositive(Step)) { |
| 1074 | const SCEV *N = getConstant(APInt::getSignedMinValue(BitWidth) - |
| 1075 | getSignedRange(Step).getSignedMax()); |
| 1076 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, AR, N) || |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 1077 | (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SLT, Start, N) && |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1078 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, |
| 1079 | AR->getPostIncExpr(*this), N))) |
| 1080 | // Return the expression with the addrec on the outside. |
| 1081 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 1082 | getSignExtendExpr(Step, Ty), |
| 1083 | L); |
| 1084 | } else if (isKnownNegative(Step)) { |
| 1085 | const SCEV *N = getConstant(APInt::getSignedMaxValue(BitWidth) - |
| 1086 | getSignedRange(Step).getSignedMin()); |
| 1087 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, AR, N) || |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 1088 | (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SGT, Start, N) && |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1089 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, |
| 1090 | AR->getPostIncExpr(*this), N))) |
| 1091 | // Return the expression with the addrec on the outside. |
| 1092 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 1093 | getSignExtendExpr(Step, Ty), |
| 1094 | L); |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1095 | } |
| 1096 | } |
| 1097 | } |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 1098 | |
Dan Gohman | 69fbc7f | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 1099 | // The cast wasn't folded; create an explicit cast node. |
| 1100 | // Recompute the insert position, as it may have been invalidated. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1101 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1102 | SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator), |
| 1103 | Op, Ty); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1104 | UniqueSCEVs.InsertNode(S, IP); |
| 1105 | return S; |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1108 | /// getAnyExtendExpr - Return a SCEV for the given operand extended with |
| 1109 | /// unspecified bits out to the given type. |
| 1110 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1111 | const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op, |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 1112 | const Type *Ty) { |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1113 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
| 1114 | "This is not an extending conversion!"); |
| 1115 | assert(isSCEVable(Ty) && |
| 1116 | "This is not a conversion to a SCEVable type!"); |
| 1117 | Ty = getEffectiveSCEVType(Ty); |
| 1118 | |
| 1119 | // Sign-extend negative constants. |
| 1120 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| 1121 | if (SC->getValue()->getValue().isNegative()) |
| 1122 | return getSignExtendExpr(Op, Ty); |
| 1123 | |
| 1124 | // Peel off a truncate cast. |
| 1125 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1126 | const SCEV *NewOp = T->getOperand(); |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1127 | if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty)) |
| 1128 | return getAnyExtendExpr(NewOp, Ty); |
| 1129 | return getTruncateOrNoop(NewOp, Ty); |
| 1130 | } |
| 1131 | |
| 1132 | // Next try a zext cast. If the cast is folded, use it. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1133 | const SCEV *ZExt = getZeroExtendExpr(Op, Ty); |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1134 | if (!isa<SCEVZeroExtendExpr>(ZExt)) |
| 1135 | return ZExt; |
| 1136 | |
| 1137 | // Next try a sext cast. If the cast is folded, use it. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1138 | const SCEV *SExt = getSignExtendExpr(Op, Ty); |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1139 | if (!isa<SCEVSignExtendExpr>(SExt)) |
| 1140 | return SExt; |
| 1141 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1142 | // Force the cast to be folded into the operands of an addrec. |
| 1143 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) { |
| 1144 | SmallVector<const SCEV *, 4> Ops; |
| 1145 | for (SCEVAddRecExpr::op_iterator I = AR->op_begin(), E = AR->op_end(); |
| 1146 | I != E; ++I) |
| 1147 | Ops.push_back(getAnyExtendExpr(*I, Ty)); |
| 1148 | return getAddRecExpr(Ops, AR->getLoop()); |
| 1149 | } |
| 1150 | |
Dan Gohman | f53462d | 2010-07-15 20:02:11 +0000 | [diff] [blame] | 1151 | // As a special case, fold anyext(undef) to undef. We don't want to |
| 1152 | // know too much about SCEVUnknowns, but this special case is handy |
| 1153 | // and harmless. |
| 1154 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Op)) |
| 1155 | if (isa<UndefValue>(U->getValue())) |
| 1156 | return getSCEV(UndefValue::get(Ty)); |
| 1157 | |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1158 | // If the expression is obviously signed, use the sext cast value. |
| 1159 | if (isa<SCEVSMaxExpr>(Op)) |
| 1160 | return SExt; |
| 1161 | |
| 1162 | // Absent any other information, use the zext cast value. |
| 1163 | return ZExt; |
| 1164 | } |
| 1165 | |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1166 | /// CollectAddOperandsWithScales - Process the given Ops list, which is |
| 1167 | /// a list of operands to be added under the given scale, update the given |
| 1168 | /// map. This is a helper function for getAddRecExpr. As an example of |
| 1169 | /// what it does, given a sequence of operands that would form an add |
| 1170 | /// expression like this: |
| 1171 | /// |
| 1172 | /// m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r) |
| 1173 | /// |
| 1174 | /// where A and B are constants, update the map with these values: |
| 1175 | /// |
| 1176 | /// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0) |
| 1177 | /// |
| 1178 | /// and add 13 + A*B*29 to AccumulatedConstant. |
| 1179 | /// This will allow getAddRecExpr to produce this: |
| 1180 | /// |
| 1181 | /// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B) |
| 1182 | /// |
| 1183 | /// This form often exposes folding opportunities that are hidden in |
| 1184 | /// the original operand list. |
| 1185 | /// |
| 1186 | /// Return true iff it appears that any interesting folding opportunities |
| 1187 | /// may be exposed. This helps getAddRecExpr short-circuit extra work in |
| 1188 | /// the common case where no interesting opportunities are present, and |
| 1189 | /// is also used as a check to avoid infinite recursion. |
| 1190 | /// |
| 1191 | static bool |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1192 | CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M, |
| 1193 | SmallVector<const SCEV *, 8> &NewOps, |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1194 | APInt &AccumulatedConstant, |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1195 | const SCEV *const *Ops, size_t NumOperands, |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1196 | const APInt &Scale, |
| 1197 | ScalarEvolution &SE) { |
| 1198 | bool Interesting = false; |
| 1199 | |
Dan Gohman | e0f0c7b | 2010-06-18 19:12:32 +0000 | [diff] [blame] | 1200 | // Iterate over the add operands. They are sorted, with constants first. |
| 1201 | unsigned i = 0; |
| 1202 | while (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| 1203 | ++i; |
| 1204 | // Pull a buried constant out to the outside. |
| 1205 | if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero()) |
| 1206 | Interesting = true; |
| 1207 | AccumulatedConstant += Scale * C->getValue()->getValue(); |
| 1208 | } |
| 1209 | |
| 1210 | // Next comes everything else. We're especially interested in multiplies |
| 1211 | // here, but they're in the middle, so just visit the rest with one loop. |
| 1212 | for (; i != NumOperands; ++i) { |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1213 | const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]); |
| 1214 | if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) { |
| 1215 | APInt NewScale = |
| 1216 | Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue(); |
| 1217 | if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) { |
| 1218 | // A multiplication of a constant with another add; recurse. |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1219 | const SCEVAddExpr *Add = cast<SCEVAddExpr>(Mul->getOperand(1)); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1220 | Interesting |= |
| 1221 | CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1222 | Add->op_begin(), Add->getNumOperands(), |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1223 | NewScale, SE); |
| 1224 | } else { |
| 1225 | // A multiplication of a constant with some other value. Update |
| 1226 | // the map. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1227 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end()); |
| 1228 | const SCEV *Key = SE.getMulExpr(MulOps); |
| 1229 | std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = |
Dan Gohman | 23737e0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1230 | M.insert(std::make_pair(Key, NewScale)); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1231 | if (Pair.second) { |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1232 | NewOps.push_back(Pair.first->first); |
| 1233 | } else { |
| 1234 | Pair.first->second += NewScale; |
| 1235 | // The map already had an entry for this value, which may indicate |
| 1236 | // a folding opportunity. |
| 1237 | Interesting = true; |
| 1238 | } |
| 1239 | } |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1240 | } else { |
| 1241 | // An ordinary operand. Update the map. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1242 | std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = |
Dan Gohman | 23737e0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1243 | M.insert(std::make_pair(Ops[i], Scale)); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1244 | if (Pair.second) { |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1245 | NewOps.push_back(Pair.first->first); |
| 1246 | } else { |
| 1247 | Pair.first->second += Scale; |
| 1248 | // The map already had an entry for this value, which may indicate |
| 1249 | // a folding opportunity. |
| 1250 | Interesting = true; |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | return Interesting; |
| 1256 | } |
| 1257 | |
| 1258 | namespace { |
| 1259 | struct APIntCompare { |
| 1260 | bool operator()(const APInt &LHS, const APInt &RHS) const { |
| 1261 | return LHS.ult(RHS); |
| 1262 | } |
| 1263 | }; |
| 1264 | } |
| 1265 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1266 | /// getAddExpr - Get a canonical add expression, or something simpler if |
| 1267 | /// possible. |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1268 | const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops, |
| 1269 | bool HasNUW, bool HasNSW) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1270 | assert(!Ops.empty() && "Cannot get empty add!"); |
Chris Lattner | 627018b | 2004-04-07 16:16:11 +0000 | [diff] [blame] | 1271 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1272 | #ifndef NDEBUG |
Dan Gohman | c72f0c8 | 2010-06-18 19:09:27 +0000 | [diff] [blame] | 1273 | const Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1274 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
Dan Gohman | c72f0c8 | 2010-06-18 19:09:27 +0000 | [diff] [blame] | 1275 | assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1276 | "SCEVAddExpr operand types don't match!"); |
| 1277 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1278 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1279 | // If HasNSW is true and all the operands are non-negative, infer HasNUW. |
| 1280 | if (!HasNUW && HasNSW) { |
| 1281 | bool All = true; |
Dan Gohman | 2d16fc5 | 2010-08-16 16:27:53 +0000 | [diff] [blame] | 1282 | for (SmallVectorImpl<const SCEV *>::const_iterator I = Ops.begin(), |
| 1283 | E = Ops.end(); I != E; ++I) |
| 1284 | if (!isKnownNonNegative(*I)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1285 | All = false; |
| 1286 | break; |
| 1287 | } |
| 1288 | if (All) HasNUW = true; |
| 1289 | } |
| 1290 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1291 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1292 | GroupByComplexity(Ops, LI); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1293 | |
| 1294 | // If there are any constants, fold them together. |
| 1295 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1296 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1297 | ++Idx; |
Chris Lattner | 627018b | 2004-04-07 16:16:11 +0000 | [diff] [blame] | 1298 | assert(Idx < Ops.size()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1299 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1300 | // We found two constants, fold them together! |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1301 | Ops[0] = getConstant(LHSC->getValue()->getValue() + |
| 1302 | RHSC->getValue()->getValue()); |
Dan Gohman | 7f7c436 | 2009-06-14 22:53:57 +0000 | [diff] [blame] | 1303 | if (Ops.size() == 2) return Ops[0]; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1304 | Ops.erase(Ops.begin()+1); // Erase the folded element |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1305 | LHSC = cast<SCEVConstant>(Ops[0]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | // If we are left with a constant zero being added, strip it off. |
Dan Gohman | bca091d | 2010-04-12 23:08:18 +0000 | [diff] [blame] | 1309 | if (LHSC->getValue()->isZero()) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1310 | Ops.erase(Ops.begin()); |
| 1311 | --Idx; |
| 1312 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1313 | |
Dan Gohman | bca091d | 2010-04-12 23:08:18 +0000 | [diff] [blame] | 1314 | if (Ops.size() == 1) return Ops[0]; |
| 1315 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1316 | |
Dan Gohman | 68ff776 | 2010-08-27 21:39:59 +0000 | [diff] [blame] | 1317 | // Okay, check to see if the same value occurs in the operand list more than |
| 1318 | // once. If so, merge them together into an multiply expression. Since we |
| 1319 | // sorted the list, these values are required to be adjacent. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1320 | const Type *Ty = Ops[0]->getType(); |
Dan Gohman | dc7692b | 2010-08-12 14:46:54 +0000 | [diff] [blame] | 1321 | bool FoundMatch = false; |
Dan Gohman | 68ff776 | 2010-08-27 21:39:59 +0000 | [diff] [blame] | 1322 | for (unsigned i = 0, e = Ops.size(); i != e-1; ++i) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1323 | if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2 |
Dan Gohman | 68ff776 | 2010-08-27 21:39:59 +0000 | [diff] [blame] | 1324 | // Scan ahead to count how many equal operands there are. |
| 1325 | unsigned Count = 2; |
| 1326 | while (i+Count != e && Ops[i+Count] == Ops[i]) |
| 1327 | ++Count; |
| 1328 | // Merge the values into a multiply. |
| 1329 | const SCEV *Scale = getConstant(Ty, Count); |
| 1330 | const SCEV *Mul = getMulExpr(Scale, Ops[i]); |
| 1331 | if (Ops.size() == Count) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1332 | return Mul; |
Dan Gohman | dc7692b | 2010-08-12 14:46:54 +0000 | [diff] [blame] | 1333 | Ops[i] = Mul; |
Dan Gohman | 68ff776 | 2010-08-27 21:39:59 +0000 | [diff] [blame] | 1334 | Ops.erase(Ops.begin()+i+1, Ops.begin()+i+Count); |
Dan Gohman | 5bb307d | 2010-08-28 00:39:27 +0000 | [diff] [blame] | 1335 | --i; e -= Count - 1; |
Dan Gohman | dc7692b | 2010-08-12 14:46:54 +0000 | [diff] [blame] | 1336 | FoundMatch = true; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1337 | } |
Dan Gohman | dc7692b | 2010-08-12 14:46:54 +0000 | [diff] [blame] | 1338 | if (FoundMatch) |
| 1339 | return getAddExpr(Ops, HasNUW, HasNSW); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1340 | |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1341 | // Check for truncates. If all the operands are truncated from the same |
| 1342 | // type, see if factoring out the truncate would permit the result to be |
| 1343 | // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n) |
| 1344 | // if the contents of the resulting outer trunc fold to something simple. |
| 1345 | for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) { |
| 1346 | const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]); |
| 1347 | const Type *DstType = Trunc->getType(); |
| 1348 | const Type *SrcType = Trunc->getOperand()->getType(); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1349 | SmallVector<const SCEV *, 8> LargeOps; |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1350 | bool Ok = true; |
| 1351 | // Check all the operands to see if they can be represented in the |
| 1352 | // source type of the truncate. |
| 1353 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 1354 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) { |
| 1355 | if (T->getOperand()->getType() != SrcType) { |
| 1356 | Ok = false; |
| 1357 | break; |
| 1358 | } |
| 1359 | LargeOps.push_back(T->getOperand()); |
| 1360 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
Dan Gohman | c686398 | 2010-04-23 01:51:29 +0000 | [diff] [blame] | 1361 | LargeOps.push_back(getAnyExtendExpr(C, SrcType)); |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1362 | } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1363 | SmallVector<const SCEV *, 8> LargeMulOps; |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1364 | for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { |
| 1365 | if (const SCEVTruncateExpr *T = |
| 1366 | dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) { |
| 1367 | if (T->getOperand()->getType() != SrcType) { |
| 1368 | Ok = false; |
| 1369 | break; |
| 1370 | } |
| 1371 | LargeMulOps.push_back(T->getOperand()); |
| 1372 | } else if (const SCEVConstant *C = |
| 1373 | dyn_cast<SCEVConstant>(M->getOperand(j))) { |
Dan Gohman | c686398 | 2010-04-23 01:51:29 +0000 | [diff] [blame] | 1374 | LargeMulOps.push_back(getAnyExtendExpr(C, SrcType)); |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1375 | } else { |
| 1376 | Ok = false; |
| 1377 | break; |
| 1378 | } |
| 1379 | } |
| 1380 | if (Ok) |
| 1381 | LargeOps.push_back(getMulExpr(LargeMulOps)); |
| 1382 | } else { |
| 1383 | Ok = false; |
| 1384 | break; |
| 1385 | } |
| 1386 | } |
| 1387 | if (Ok) { |
| 1388 | // Evaluate the expression in the larger type. |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1389 | const SCEV *Fold = getAddExpr(LargeOps, HasNUW, HasNSW); |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1390 | // If it folds to something simple, use it. Otherwise, don't. |
| 1391 | if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold)) |
| 1392 | return getTruncateExpr(Fold, DstType); |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | // Skip past any other cast SCEVs. |
Dan Gohman | f50cd74 | 2007-06-18 19:30:09 +0000 | [diff] [blame] | 1397 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) |
| 1398 | ++Idx; |
| 1399 | |
| 1400 | // If there are add operands they would be next. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1401 | if (Idx < Ops.size()) { |
| 1402 | bool DeletedAdd = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1403 | while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1404 | // If we have an add, expand the add operands onto the end of the operands |
| 1405 | // list. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1406 | Ops.erase(Ops.begin()+Idx); |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 1407 | Ops.append(Add->op_begin(), Add->op_end()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1408 | DeletedAdd = true; |
| 1409 | } |
| 1410 | |
| 1411 | // If we deleted at least one add, we added operands to the end of the list, |
| 1412 | // and they are not necessarily sorted. Recurse to resort and resimplify |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 1413 | // any operands we just acquired. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1414 | if (DeletedAdd) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1415 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | // Skip over the add expression until we get to a multiply. |
| 1419 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1420 | ++Idx; |
| 1421 | |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1422 | // Check to see if there are any folding opportunities present with |
| 1423 | // operands multiplied by constant values. |
| 1424 | if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) { |
| 1425 | uint64_t BitWidth = getTypeSizeInBits(Ty); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1426 | DenseMap<const SCEV *, APInt> M; |
| 1427 | SmallVector<const SCEV *, 8> NewOps; |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1428 | APInt AccumulatedConstant(BitWidth, 0); |
| 1429 | if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1430 | Ops.data(), Ops.size(), |
| 1431 | APInt(BitWidth, 1), *this)) { |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1432 | // Some interesting folding opportunity is present, so its worthwhile to |
| 1433 | // re-generate the operands list. Group the operands by constant scale, |
| 1434 | // to avoid multiplying by the same constant scale multiple times. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1435 | std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists; |
Dan Gohman | 8d9c7a6 | 2010-08-16 16:30:01 +0000 | [diff] [blame] | 1436 | for (SmallVector<const SCEV *, 8>::const_iterator I = NewOps.begin(), |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1437 | E = NewOps.end(); I != E; ++I) |
| 1438 | MulOpLists[M.find(*I)->second].push_back(*I); |
| 1439 | // Re-generate the operands list. |
| 1440 | Ops.clear(); |
| 1441 | if (AccumulatedConstant != 0) |
| 1442 | Ops.push_back(getConstant(AccumulatedConstant)); |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1443 | for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator |
| 1444 | I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I) |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1445 | if (I->first != 0) |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1446 | Ops.push_back(getMulExpr(getConstant(I->first), |
| 1447 | getAddExpr(I->second))); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1448 | if (Ops.empty()) |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1449 | return getConstant(Ty, 0); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1450 | if (Ops.size() == 1) |
| 1451 | return Ops[0]; |
| 1452 | return getAddExpr(Ops); |
| 1453 | } |
| 1454 | } |
| 1455 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1456 | // If we are adding something to a multiply expression, make sure the |
| 1457 | // something is not already an operand of the multiply. If so, merge it into |
| 1458 | // the multiply. |
| 1459 | for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1460 | const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1461 | for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1462 | const SCEV *MulOpSCEV = Mul->getOperand(MulOp); |
Dan Gohman | 918e76b | 2010-08-12 14:52:55 +0000 | [diff] [blame] | 1463 | if (isa<SCEVConstant>(MulOpSCEV)) |
| 1464 | continue; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1465 | for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) |
Dan Gohman | 918e76b | 2010-08-12 14:52:55 +0000 | [diff] [blame] | 1466 | if (MulOpSCEV == Ops[AddOp]) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1467 | // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1468 | const SCEV *InnerMul = Mul->getOperand(MulOp == 0); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1469 | if (Mul->getNumOperands() != 2) { |
| 1470 | // If the multiply has more than two operands, we must get the |
| 1471 | // Y*Z term. |
Dan Gohman | 1895991 | 2010-08-16 16:57:24 +0000 | [diff] [blame] | 1472 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), |
| 1473 | Mul->op_begin()+MulOp); |
| 1474 | MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1475 | InnerMul = getMulExpr(MulOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1476 | } |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1477 | const SCEV *One = getConstant(Ty, 1); |
Dan Gohman | 58a85b9 | 2010-08-13 20:17:14 +0000 | [diff] [blame] | 1478 | const SCEV *AddOne = getAddExpr(One, InnerMul); |
Dan Gohman | 918e76b | 2010-08-12 14:52:55 +0000 | [diff] [blame] | 1479 | const SCEV *OuterMul = getMulExpr(AddOne, MulOpSCEV); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1480 | if (Ops.size() == 2) return OuterMul; |
| 1481 | if (AddOp < Idx) { |
| 1482 | Ops.erase(Ops.begin()+AddOp); |
| 1483 | Ops.erase(Ops.begin()+Idx-1); |
| 1484 | } else { |
| 1485 | Ops.erase(Ops.begin()+Idx); |
| 1486 | Ops.erase(Ops.begin()+AddOp-1); |
| 1487 | } |
| 1488 | Ops.push_back(OuterMul); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1489 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1490 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1491 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1492 | // Check this multiply against other multiplies being added together. |
| 1493 | for (unsigned OtherMulIdx = Idx+1; |
| 1494 | OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]); |
| 1495 | ++OtherMulIdx) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1496 | const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1497 | // If MulOp occurs in OtherMul, we can fold the two multiplies |
| 1498 | // together. |
| 1499 | for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); |
| 1500 | OMulOp != e; ++OMulOp) |
| 1501 | if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { |
| 1502 | // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E)) |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1503 | const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1504 | if (Mul->getNumOperands() != 2) { |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1505 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), |
Dan Gohman | 1895991 | 2010-08-16 16:57:24 +0000 | [diff] [blame] | 1506 | Mul->op_begin()+MulOp); |
| 1507 | MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1508 | InnerMul1 = getMulExpr(MulOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1509 | } |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1510 | const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1511 | if (OtherMul->getNumOperands() != 2) { |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1512 | SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(), |
Dan Gohman | 1895991 | 2010-08-16 16:57:24 +0000 | [diff] [blame] | 1513 | OtherMul->op_begin()+OMulOp); |
| 1514 | MulOps.append(OtherMul->op_begin()+OMulOp+1, OtherMul->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1515 | InnerMul2 = getMulExpr(MulOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1516 | } |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1517 | const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2); |
| 1518 | const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1519 | if (Ops.size() == 2) return OuterMul; |
Dan Gohman | 90b5f25 | 2010-08-31 22:50:31 +0000 | [diff] [blame] | 1520 | Ops.erase(Ops.begin()+Idx); |
| 1521 | Ops.erase(Ops.begin()+OtherMulIdx-1); |
| 1522 | Ops.push_back(OuterMul); |
| 1523 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1524 | } |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | // If there are any add recurrences in the operands list, see if any other |
| 1530 | // added values are loop invariant. If so, we can fold them into the |
| 1531 | // recurrence. |
| 1532 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1533 | ++Idx; |
| 1534 | |
| 1535 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1536 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1537 | // Scan all of the other operands to this add and add them to the vector if |
| 1538 | // they are loop invariant w.r.t. the recurrence. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1539 | SmallVector<const SCEV *, 8> LIOps; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1540 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Dan Gohman | bca091d | 2010-04-12 23:08:18 +0000 | [diff] [blame] | 1541 | const Loop *AddRecLoop = AddRec->getLoop(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1542 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1543 | if (isLoopInvariant(Ops[i], AddRecLoop)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1544 | LIOps.push_back(Ops[i]); |
| 1545 | Ops.erase(Ops.begin()+i); |
| 1546 | --i; --e; |
| 1547 | } |
| 1548 | |
| 1549 | // If we found some loop invariants, fold them into the recurrence. |
| 1550 | if (!LIOps.empty()) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1551 | // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step} |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1552 | LIOps.push_back(AddRec->getStart()); |
| 1553 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1554 | SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(), |
Dan Gohman | 3a5d409 | 2009-12-18 03:57:04 +0000 | [diff] [blame] | 1555 | AddRec->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1556 | AddRecOps[0] = getAddExpr(LIOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1557 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 1558 | // Build the new addrec. Propagate the NUW and NSW flags if both the |
| 1559 | // outer add and the inner addrec are guaranteed to have no overflow. |
| 1560 | const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRecLoop, |
| 1561 | HasNUW && AddRec->hasNoUnsignedWrap(), |
| 1562 | HasNSW && AddRec->hasNoSignedWrap()); |
Dan Gohman | 59de33e | 2009-12-18 18:45:31 +0000 | [diff] [blame] | 1563 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1564 | // If all of the other operands were loop invariant, we are done. |
| 1565 | if (Ops.size() == 1) return NewRec; |
| 1566 | |
| 1567 | // Otherwise, add the folded AddRec by the non-liv parts. |
| 1568 | for (unsigned i = 0;; ++i) |
| 1569 | if (Ops[i] == AddRec) { |
| 1570 | Ops[i] = NewRec; |
| 1571 | break; |
| 1572 | } |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1573 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1577 | // there are multiple AddRec's with the same loop induction variable being |
| 1578 | // added together. If so, we can fold them. |
| 1579 | for (unsigned OtherIdx = Idx+1; |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1580 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); |
| 1581 | ++OtherIdx) |
| 1582 | if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) { |
| 1583 | // Other + {A,+,B}<L> + {C,+,D}<L> --> Other + {A+C,+,B+D}<L> |
| 1584 | SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(), |
| 1585 | AddRec->op_end()); |
| 1586 | for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); |
| 1587 | ++OtherIdx) |
Dan Gohman | 30cbc86 | 2010-08-29 14:53:34 +0000 | [diff] [blame] | 1588 | if (const SCEVAddRecExpr *OtherAddRec = |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1589 | dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx])) |
Dan Gohman | 30cbc86 | 2010-08-29 14:53:34 +0000 | [diff] [blame] | 1590 | if (OtherAddRec->getLoop() == AddRecLoop) { |
| 1591 | for (unsigned i = 0, e = OtherAddRec->getNumOperands(); |
| 1592 | i != e; ++i) { |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1593 | if (i >= AddRecOps.size()) { |
Dan Gohman | 30cbc86 | 2010-08-29 14:53:34 +0000 | [diff] [blame] | 1594 | AddRecOps.append(OtherAddRec->op_begin()+i, |
| 1595 | OtherAddRec->op_end()); |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1596 | break; |
| 1597 | } |
Dan Gohman | 30cbc86 | 2010-08-29 14:53:34 +0000 | [diff] [blame] | 1598 | AddRecOps[i] = getAddExpr(AddRecOps[i], |
| 1599 | OtherAddRec->getOperand(i)); |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1600 | } |
| 1601 | Ops.erase(Ops.begin() + OtherIdx); --OtherIdx; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1602 | } |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1603 | Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop); |
| 1604 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1608 | // next one. |
| 1609 | } |
| 1610 | |
| 1611 | // Okay, it looks like we really DO need an add expr. Check to see if we |
| 1612 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1613 | FoldingSetNodeID ID; |
| 1614 | ID.AddInteger(scAddExpr); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1615 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1616 | ID.AddPointer(Ops[i]); |
| 1617 | void *IP = 0; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1618 | SCEVAddExpr *S = |
| 1619 | static_cast<SCEVAddExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
| 1620 | if (!S) { |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1621 | const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); |
| 1622 | std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1623 | S = new (SCEVAllocator) SCEVAddExpr(ID.Intern(SCEVAllocator), |
| 1624 | O, Ops.size()); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1625 | UniqueSCEVs.InsertNode(S, IP); |
| 1626 | } |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1627 | if (HasNUW) S->setHasNoUnsignedWrap(true); |
| 1628 | if (HasNSW) S->setHasNoSignedWrap(true); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1629 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1632 | /// getMulExpr - Get a canonical multiply expression, or something simpler if |
| 1633 | /// possible. |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1634 | const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops, |
| 1635 | bool HasNUW, bool HasNSW) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1636 | assert(!Ops.empty() && "Cannot get empty mul!"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1637 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1638 | #ifndef NDEBUG |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 1639 | const Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1640 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 1641 | assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1642 | "SCEVMulExpr operand types don't match!"); |
| 1643 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1644 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1645 | // If HasNSW is true and all the operands are non-negative, infer HasNUW. |
| 1646 | if (!HasNUW && HasNSW) { |
| 1647 | bool All = true; |
Dan Gohman | 2d16fc5 | 2010-08-16 16:27:53 +0000 | [diff] [blame] | 1648 | for (SmallVectorImpl<const SCEV *>::const_iterator I = Ops.begin(), |
| 1649 | E = Ops.end(); I != E; ++I) |
| 1650 | if (!isKnownNonNegative(*I)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1651 | All = false; |
| 1652 | break; |
| 1653 | } |
| 1654 | if (All) HasNUW = true; |
| 1655 | } |
| 1656 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1657 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1658 | GroupByComplexity(Ops, LI); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1659 | |
| 1660 | // If there are any constants, fold them together. |
| 1661 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1662 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1663 | |
| 1664 | // C1*(C2+V) -> C1*C2 + C1*V |
| 1665 | if (Ops.size() == 2) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1666 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1667 | if (Add->getNumOperands() == 2 && |
| 1668 | isa<SCEVConstant>(Add->getOperand(0))) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1669 | return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)), |
| 1670 | getMulExpr(LHSC, Add->getOperand(1))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1671 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1672 | ++Idx; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1673 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1674 | // We found two constants, fold them together! |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1675 | ConstantInt *Fold = ConstantInt::get(getContext(), |
| 1676 | LHSC->getValue()->getValue() * |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1677 | RHSC->getValue()->getValue()); |
| 1678 | Ops[0] = getConstant(Fold); |
| 1679 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1680 | if (Ops.size() == 1) return Ops[0]; |
| 1681 | LHSC = cast<SCEVConstant>(Ops[0]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | // If we are left with a constant one being multiplied, strip it off. |
| 1685 | if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) { |
| 1686 | Ops.erase(Ops.begin()); |
| 1687 | --Idx; |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1688 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1689 | // If we have a multiply of zero, it will always be zero. |
| 1690 | return Ops[0]; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1691 | } else if (Ops[0]->isAllOnesValue()) { |
| 1692 | // If we have a mul by -1 of an add, try distributing the -1 among the |
| 1693 | // add operands. |
| 1694 | if (Ops.size() == 2) |
| 1695 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) { |
| 1696 | SmallVector<const SCEV *, 4> NewOps; |
| 1697 | bool AnyFolded = false; |
| 1698 | for (SCEVAddRecExpr::op_iterator I = Add->op_begin(), E = Add->op_end(); |
| 1699 | I != E; ++I) { |
| 1700 | const SCEV *Mul = getMulExpr(Ops[0], *I); |
| 1701 | if (!isa<SCEVMulExpr>(Mul)) AnyFolded = true; |
| 1702 | NewOps.push_back(Mul); |
| 1703 | } |
| 1704 | if (AnyFolded) |
| 1705 | return getAddExpr(NewOps); |
| 1706 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1707 | } |
Dan Gohman | 3ab1312 | 2010-04-13 16:49:23 +0000 | [diff] [blame] | 1708 | |
| 1709 | if (Ops.size() == 1) |
| 1710 | return Ops[0]; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | // Skip over the add expression until we get to a multiply. |
| 1714 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1715 | ++Idx; |
| 1716 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1717 | // If there are mul operands inline them all into this expression. |
| 1718 | if (Idx < Ops.size()) { |
| 1719 | bool DeletedMul = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1720 | while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1721 | // If we have an mul, expand the mul operands onto the end of the operands |
| 1722 | // list. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1723 | Ops.erase(Ops.begin()+Idx); |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 1724 | Ops.append(Mul->op_begin(), Mul->op_end()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1725 | DeletedMul = true; |
| 1726 | } |
| 1727 | |
| 1728 | // If we deleted at least one mul, we added operands to the end of the list, |
| 1729 | // and they are not necessarily sorted. Recurse to resort and resimplify |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 1730 | // any operands we just acquired. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1731 | if (DeletedMul) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1732 | return getMulExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | // If there are any add recurrences in the operands list, see if any other |
| 1736 | // added values are loop invariant. If so, we can fold them into the |
| 1737 | // recurrence. |
| 1738 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1739 | ++Idx; |
| 1740 | |
| 1741 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1742 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1743 | // Scan all of the other operands to this mul and add them to the vector if |
| 1744 | // they are loop invariant w.r.t. the recurrence. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1745 | SmallVector<const SCEV *, 8> LIOps; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1746 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Dan Gohman | 0f32ae3 | 2010-08-29 14:55:19 +0000 | [diff] [blame] | 1747 | const Loop *AddRecLoop = AddRec->getLoop(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1748 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1749 | if (isLoopInvariant(Ops[i], AddRecLoop)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1750 | LIOps.push_back(Ops[i]); |
| 1751 | Ops.erase(Ops.begin()+i); |
| 1752 | --i; --e; |
| 1753 | } |
| 1754 | |
| 1755 | // If we found some loop invariants, fold them into the recurrence. |
| 1756 | if (!LIOps.empty()) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1757 | // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step} |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1758 | SmallVector<const SCEV *, 4> NewOps; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1759 | NewOps.reserve(AddRec->getNumOperands()); |
Dan Gohman | 27ed6a4 | 2010-06-17 23:34:09 +0000 | [diff] [blame] | 1760 | const SCEV *Scale = getMulExpr(LIOps); |
| 1761 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
| 1762 | NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1763 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 1764 | // Build the new addrec. Propagate the NUW and NSW flags if both the |
| 1765 | // outer mul and the inner addrec are guaranteed to have no overflow. |
Dan Gohman | 0f32ae3 | 2010-08-29 14:55:19 +0000 | [diff] [blame] | 1766 | const SCEV *NewRec = getAddRecExpr(NewOps, AddRecLoop, |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1767 | HasNUW && AddRec->hasNoUnsignedWrap(), |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 1768 | HasNSW && AddRec->hasNoSignedWrap()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1769 | |
| 1770 | // If all of the other operands were loop invariant, we are done. |
| 1771 | if (Ops.size() == 1) return NewRec; |
| 1772 | |
| 1773 | // Otherwise, multiply the folded AddRec by the non-liv parts. |
| 1774 | for (unsigned i = 0;; ++i) |
| 1775 | if (Ops[i] == AddRec) { |
| 1776 | Ops[i] = NewRec; |
| 1777 | break; |
| 1778 | } |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1779 | return getMulExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1783 | // there are multiple AddRec's with the same loop induction variable being |
| 1784 | // multiplied together. If so, we can fold them. |
| 1785 | for (unsigned OtherIdx = Idx+1; |
Dan Gohman | 6a0c125 | 2010-08-31 22:52:12 +0000 | [diff] [blame] | 1786 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); |
| 1787 | ++OtherIdx) |
| 1788 | if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) { |
| 1789 | // F * G, where F = {A,+,B}<L> and G = {C,+,D}<L> --> |
| 1790 | // {A*C,+,F*D + G*B + B*D}<L> |
| 1791 | for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); |
| 1792 | ++OtherIdx) |
| 1793 | if (const SCEVAddRecExpr *OtherAddRec = |
| 1794 | dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx])) |
| 1795 | if (OtherAddRec->getLoop() == AddRecLoop) { |
| 1796 | const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec; |
| 1797 | const SCEV *NewStart = getMulExpr(F->getStart(), G->getStart()); |
| 1798 | const SCEV *B = F->getStepRecurrence(*this); |
| 1799 | const SCEV *D = G->getStepRecurrence(*this); |
| 1800 | const SCEV *NewStep = getAddExpr(getMulExpr(F, D), |
| 1801 | getMulExpr(G, B), |
| 1802 | getMulExpr(B, D)); |
| 1803 | const SCEV *NewAddRec = getAddRecExpr(NewStart, NewStep, |
| 1804 | F->getLoop()); |
| 1805 | if (Ops.size() == 2) return NewAddRec; |
| 1806 | Ops[Idx] = AddRec = cast<SCEVAddRecExpr>(NewAddRec); |
| 1807 | Ops.erase(Ops.begin() + OtherIdx); --OtherIdx; |
| 1808 | } |
| 1809 | return getMulExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1813 | // next one. |
| 1814 | } |
| 1815 | |
| 1816 | // Okay, it looks like we really DO need an mul expr. Check to see if we |
| 1817 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1818 | FoldingSetNodeID ID; |
| 1819 | ID.AddInteger(scMulExpr); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1820 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1821 | ID.AddPointer(Ops[i]); |
| 1822 | void *IP = 0; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1823 | SCEVMulExpr *S = |
| 1824 | static_cast<SCEVMulExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
| 1825 | if (!S) { |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1826 | const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); |
| 1827 | std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1828 | S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator), |
| 1829 | O, Ops.size()); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1830 | UniqueSCEVs.InsertNode(S, IP); |
| 1831 | } |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1832 | if (HasNUW) S->setHasNoUnsignedWrap(true); |
| 1833 | if (HasNSW) S->setHasNoSignedWrap(true); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1834 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
Andreas Bolka | 8a11c98 | 2009-08-07 22:55:26 +0000 | [diff] [blame] | 1837 | /// getUDivExpr - Get a canonical unsigned division expression, or something |
| 1838 | /// simpler if possible. |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1839 | const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS, |
| 1840 | const SCEV *RHS) { |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1841 | assert(getEffectiveSCEVType(LHS->getType()) == |
| 1842 | getEffectiveSCEVType(RHS->getType()) && |
| 1843 | "SCEVUDivExpr operand types don't match!"); |
| 1844 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1845 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1846 | if (RHSC->getValue()->equalsInt(1)) |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 1847 | return LHS; // X udiv 1 --> x |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 1848 | // If the denominator is zero, the result of the udiv is undefined. Don't |
| 1849 | // try to analyze it, because the resolution chosen here may differ from |
| 1850 | // the resolution chosen in other parts of the compiler. |
| 1851 | if (!RHSC->getValue()->isZero()) { |
| 1852 | // Determine if the division can be folded into the operands of |
| 1853 | // its operands. |
| 1854 | // TODO: Generalize this to non-constants by using known-bits information. |
| 1855 | const Type *Ty = LHS->getType(); |
| 1856 | unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros(); |
Dan Gohman | ddd3a88 | 2010-08-04 19:52:50 +0000 | [diff] [blame] | 1857 | unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1; |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 1858 | // For non-power-of-two values, effectively round the value up to the |
| 1859 | // nearest power of two. |
| 1860 | if (!RHSC->getValue()->getValue().isPowerOf2()) |
| 1861 | ++MaxShiftAmt; |
| 1862 | const IntegerType *ExtTy = |
| 1863 | IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt); |
| 1864 | // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. |
| 1865 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) |
| 1866 | if (const SCEVConstant *Step = |
| 1867 | dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) |
| 1868 | if (!Step->getValue()->getValue() |
| 1869 | .urem(RHSC->getValue()->getValue()) && |
| 1870 | getZeroExtendExpr(AR, ExtTy) == |
| 1871 | getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), |
| 1872 | getZeroExtendExpr(Step, ExtTy), |
| 1873 | AR->getLoop())) { |
| 1874 | SmallVector<const SCEV *, 4> Operands; |
| 1875 | for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i) |
| 1876 | Operands.push_back(getUDivExpr(AR->getOperand(i), RHS)); |
| 1877 | return getAddRecExpr(Operands, AR->getLoop()); |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1878 | } |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 1879 | // (A*B)/C --> A*(B/C) if safe and B/C can be folded. |
| 1880 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) { |
| 1881 | SmallVector<const SCEV *, 4> Operands; |
| 1882 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) |
| 1883 | Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy)); |
| 1884 | if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) |
| 1885 | // Find an operand that's safely divisible. |
| 1886 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { |
| 1887 | const SCEV *Op = M->getOperand(i); |
| 1888 | const SCEV *Div = getUDivExpr(Op, RHSC); |
| 1889 | if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) { |
| 1890 | Operands = SmallVector<const SCEV *, 4>(M->op_begin(), |
| 1891 | M->op_end()); |
| 1892 | Operands[i] = Div; |
| 1893 | return getMulExpr(Operands); |
| 1894 | } |
| 1895 | } |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1896 | } |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 1897 | // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded. |
| 1898 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) { |
| 1899 | SmallVector<const SCEV *, 4> Operands; |
| 1900 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) |
| 1901 | Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy)); |
| 1902 | if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { |
| 1903 | Operands.clear(); |
| 1904 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { |
| 1905 | const SCEV *Op = getUDivExpr(A->getOperand(i), RHS); |
| 1906 | if (isa<SCEVUDivExpr>(Op) || |
| 1907 | getMulExpr(Op, RHS) != A->getOperand(i)) |
| 1908 | break; |
| 1909 | Operands.push_back(Op); |
| 1910 | } |
| 1911 | if (Operands.size() == A->getNumOperands()) |
| 1912 | return getAddExpr(Operands); |
| 1913 | } |
| 1914 | } |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1915 | |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 1916 | // Fold if both operands are constant. |
| 1917 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { |
| 1918 | Constant *LHSCV = LHSC->getValue(); |
| 1919 | Constant *RHSCV = RHSC->getValue(); |
| 1920 | return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV, |
| 1921 | RHSCV))); |
| 1922 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1923 | } |
| 1924 | } |
| 1925 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1926 | FoldingSetNodeID ID; |
| 1927 | ID.AddInteger(scUDivExpr); |
| 1928 | ID.AddPointer(LHS); |
| 1929 | ID.AddPointer(RHS); |
| 1930 | void *IP = 0; |
| 1931 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1932 | SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator), |
| 1933 | LHS, RHS); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1934 | UniqueSCEVs.InsertNode(S, IP); |
| 1935 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1939 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 1940 | /// Simplify the expression as much as possible. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1941 | const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1942 | const SCEV *Step, const Loop *L, |
| 1943 | bool HasNUW, bool HasNSW) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1944 | SmallVector<const SCEV *, 4> Operands; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1945 | Operands.push_back(Start); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1946 | if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step)) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1947 | if (StepChrec->getLoop() == L) { |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 1948 | Operands.append(StepChrec->op_begin(), StepChrec->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1949 | return getAddRecExpr(Operands, L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | Operands.push_back(Step); |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1953 | return getAddRecExpr(Operands, L, HasNUW, HasNSW); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1956 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 1957 | /// Simplify the expression as much as possible. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1958 | const SCEV * |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1959 | ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands, |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1960 | const Loop *L, |
| 1961 | bool HasNUW, bool HasNSW) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1962 | if (Operands.size() == 1) return Operands[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1963 | #ifndef NDEBUG |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 1964 | const Type *ETy = getEffectiveSCEVType(Operands[0]->getType()); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1965 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 1966 | assert(getEffectiveSCEVType(Operands[i]->getType()) == ETy && |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1967 | "SCEVAddRecExpr operand types don't match!"); |
Dan Gohman | 203a723 | 2010-11-17 20:48:38 +0000 | [diff] [blame] | 1968 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1969 | assert(isLoopInvariant(Operands[i], L) && |
Dan Gohman | 203a723 | 2010-11-17 20:48:38 +0000 | [diff] [blame] | 1970 | "SCEVAddRecExpr operand is not loop-invariant!"); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1971 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1972 | |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1973 | if (Operands.back()->isZero()) { |
| 1974 | Operands.pop_back(); |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1975 | return getAddRecExpr(Operands, L, HasNUW, HasNSW); // {X,+,0} --> X |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1976 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1977 | |
Dan Gohman | bc02853 | 2010-02-19 18:49:22 +0000 | [diff] [blame] | 1978 | // It's tempting to want to call getMaxBackedgeTakenCount count here and |
| 1979 | // use that information to infer NUW and NSW flags. However, computing a |
| 1980 | // BE count requires calling getAddRecExpr, so we may not yet have a |
| 1981 | // meaningful BE count at this point (and if we don't, we'd be stuck |
| 1982 | // with a SCEVCouldNotCompute as the cached BE count). |
| 1983 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1984 | // If HasNSW is true and all the operands are non-negative, infer HasNUW. |
| 1985 | if (!HasNUW && HasNSW) { |
| 1986 | bool All = true; |
Dan Gohman | 2d16fc5 | 2010-08-16 16:27:53 +0000 | [diff] [blame] | 1987 | for (SmallVectorImpl<const SCEV *>::const_iterator I = Operands.begin(), |
| 1988 | E = Operands.end(); I != E; ++I) |
| 1989 | if (!isKnownNonNegative(*I)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1990 | All = false; |
| 1991 | break; |
| 1992 | } |
| 1993 | if (All) HasNUW = true; |
| 1994 | } |
| 1995 | |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1996 | // Canonicalize nested AddRecs in by nesting them in order of loop depth. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1997 | if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) { |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 1998 | const Loop *NestedLoop = NestedAR->getLoop(); |
Dan Gohman | 9cba978 | 2010-08-13 20:23:25 +0000 | [diff] [blame] | 1999 | if (L->contains(NestedLoop) ? |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2000 | (L->getLoopDepth() < NestedLoop->getLoopDepth()) : |
Dan Gohman | 9cba978 | 2010-08-13 20:23:25 +0000 | [diff] [blame] | 2001 | (!NestedLoop->contains(L) && |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2002 | DT->dominates(L->getHeader(), NestedLoop->getHeader()))) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2003 | SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(), |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 2004 | NestedAR->op_end()); |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 2005 | Operands[0] = NestedAR->getStart(); |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 2006 | // AddRecs require their operands be loop-invariant with respect to their |
| 2007 | // loops. Don't perform this transformation if it would break this |
| 2008 | // requirement. |
| 2009 | bool AllInvariant = true; |
| 2010 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 2011 | if (!isLoopInvariant(Operands[i], L)) { |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 2012 | AllInvariant = false; |
| 2013 | break; |
| 2014 | } |
| 2015 | if (AllInvariant) { |
| 2016 | NestedOperands[0] = getAddRecExpr(Operands, L); |
| 2017 | AllInvariant = true; |
| 2018 | for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 2019 | if (!isLoopInvariant(NestedOperands[i], NestedLoop)) { |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 2020 | AllInvariant = false; |
| 2021 | break; |
| 2022 | } |
| 2023 | if (AllInvariant) |
| 2024 | // Ok, both add recurrences are valid after the transformation. |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 2025 | return getAddRecExpr(NestedOperands, NestedLoop, HasNUW, HasNSW); |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 2026 | } |
| 2027 | // Reset Operands to its original state. |
| 2028 | Operands[0] = NestedAR; |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 2029 | } |
| 2030 | } |
| 2031 | |
Dan Gohman | 6784753 | 2010-01-19 22:27:22 +0000 | [diff] [blame] | 2032 | // Okay, it looks like we really DO need an addrec expr. Check to see if we |
| 2033 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2034 | FoldingSetNodeID ID; |
| 2035 | ID.AddInteger(scAddRecExpr); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2036 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 2037 | ID.AddPointer(Operands[i]); |
| 2038 | ID.AddPointer(L); |
| 2039 | void *IP = 0; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2040 | SCEVAddRecExpr *S = |
| 2041 | static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
| 2042 | if (!S) { |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 2043 | const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Operands.size()); |
| 2044 | std::uninitialized_copy(Operands.begin(), Operands.end(), O); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 2045 | S = new (SCEVAllocator) SCEVAddRecExpr(ID.Intern(SCEVAllocator), |
| 2046 | O, Operands.size(), L); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2047 | UniqueSCEVs.InsertNode(S, IP); |
| 2048 | } |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 2049 | if (HasNUW) S->setHasNoUnsignedWrap(true); |
| 2050 | if (HasNSW) S->setHasNoSignedWrap(true); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2051 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2054 | const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, |
| 2055 | const SCEV *RHS) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2056 | SmallVector<const SCEV *, 2> Ops; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2057 | Ops.push_back(LHS); |
| 2058 | Ops.push_back(RHS); |
| 2059 | return getSMaxExpr(Ops); |
| 2060 | } |
| 2061 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2062 | const SCEV * |
| 2063 | ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2064 | assert(!Ops.empty() && "Cannot get empty smax!"); |
| 2065 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2066 | #ifndef NDEBUG |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 2067 | const Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2068 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 2069 | assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2070 | "SCEVSMaxExpr operand types don't match!"); |
| 2071 | #endif |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2072 | |
| 2073 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 2074 | GroupByComplexity(Ops, LI); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2075 | |
| 2076 | // If there are any constants, fold them together. |
| 2077 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2078 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2079 | ++Idx; |
| 2080 | assert(Idx < Ops.size()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2081 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2082 | // We found two constants, fold them together! |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2083 | ConstantInt *Fold = ConstantInt::get(getContext(), |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2084 | APIntOps::smax(LHSC->getValue()->getValue(), |
| 2085 | RHSC->getValue()->getValue())); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2086 | Ops[0] = getConstant(Fold); |
| 2087 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 2088 | if (Ops.size() == 1) return Ops[0]; |
| 2089 | LHSC = cast<SCEVConstant>(Ops[0]); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 2092 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2093 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) { |
| 2094 | Ops.erase(Ops.begin()); |
| 2095 | --Idx; |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 2096 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) { |
| 2097 | // If we have an smax with a constant maximum-int, it will always be |
| 2098 | // maximum-int. |
| 2099 | return Ops[0]; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2100 | } |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2101 | |
Dan Gohman | 3ab1312 | 2010-04-13 16:49:23 +0000 | [diff] [blame] | 2102 | if (Ops.size() == 1) return Ops[0]; |
| 2103 | } |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2104 | |
| 2105 | // Find the first SMax |
| 2106 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr) |
| 2107 | ++Idx; |
| 2108 | |
| 2109 | // Check to see if one of the operands is an SMax. If so, expand its operands |
| 2110 | // onto our operand list, and recurse to simplify. |
| 2111 | if (Idx < Ops.size()) { |
| 2112 | bool DeletedSMax = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2113 | while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2114 | Ops.erase(Ops.begin()+Idx); |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 2115 | Ops.append(SMax->op_begin(), SMax->op_end()); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2116 | DeletedSMax = true; |
| 2117 | } |
| 2118 | |
| 2119 | if (DeletedSMax) |
| 2120 | return getSMaxExpr(Ops); |
| 2121 | } |
| 2122 | |
| 2123 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 2124 | // so, delete one. Since we sorted the list, these values are required to |
| 2125 | // be adjacent. |
| 2126 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
Dan Gohman | 2828779 | 2010-04-13 16:51:03 +0000 | [diff] [blame] | 2127 | // X smax Y smax Y --> X smax Y |
| 2128 | // X smax Y --> X, if X is always greater than Y |
| 2129 | if (Ops[i] == Ops[i+1] || |
| 2130 | isKnownPredicate(ICmpInst::ICMP_SGE, Ops[i], Ops[i+1])) { |
| 2131 | Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2); |
| 2132 | --i; --e; |
| 2133 | } else if (isKnownPredicate(ICmpInst::ICMP_SLE, Ops[i], Ops[i+1])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2134 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 2135 | --i; --e; |
| 2136 | } |
| 2137 | |
| 2138 | if (Ops.size() == 1) return Ops[0]; |
| 2139 | |
| 2140 | assert(!Ops.empty() && "Reduced smax down to nothing!"); |
| 2141 | |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2142 | // Okay, it looks like we really DO need an smax expr. Check to see if we |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2143 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2144 | FoldingSetNodeID ID; |
| 2145 | ID.AddInteger(scSMaxExpr); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2146 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 2147 | ID.AddPointer(Ops[i]); |
| 2148 | void *IP = 0; |
| 2149 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 2150 | const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); |
| 2151 | std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 2152 | SCEV *S = new (SCEVAllocator) SCEVSMaxExpr(ID.Intern(SCEVAllocator), |
| 2153 | O, Ops.size()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2154 | UniqueSCEVs.InsertNode(S, IP); |
| 2155 | return S; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2158 | const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, |
| 2159 | const SCEV *RHS) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2160 | SmallVector<const SCEV *, 2> Ops; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2161 | Ops.push_back(LHS); |
| 2162 | Ops.push_back(RHS); |
| 2163 | return getUMaxExpr(Ops); |
| 2164 | } |
| 2165 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2166 | const SCEV * |
| 2167 | ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2168 | assert(!Ops.empty() && "Cannot get empty umax!"); |
| 2169 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2170 | #ifndef NDEBUG |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 2171 | const Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2172 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 2173 | assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2174 | "SCEVUMaxExpr operand types don't match!"); |
| 2175 | #endif |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2176 | |
| 2177 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 2178 | GroupByComplexity(Ops, LI); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2179 | |
| 2180 | // If there are any constants, fold them together. |
| 2181 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2182 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2183 | ++Idx; |
| 2184 | assert(Idx < Ops.size()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2185 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2186 | // We found two constants, fold them together! |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2187 | ConstantInt *Fold = ConstantInt::get(getContext(), |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2188 | APIntOps::umax(LHSC->getValue()->getValue(), |
| 2189 | RHSC->getValue()->getValue())); |
| 2190 | Ops[0] = getConstant(Fold); |
| 2191 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 2192 | if (Ops.size() == 1) return Ops[0]; |
| 2193 | LHSC = cast<SCEVConstant>(Ops[0]); |
| 2194 | } |
| 2195 | |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 2196 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2197 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) { |
| 2198 | Ops.erase(Ops.begin()); |
| 2199 | --Idx; |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 2200 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) { |
| 2201 | // If we have an umax with a constant maximum-int, it will always be |
| 2202 | // maximum-int. |
| 2203 | return Ops[0]; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2204 | } |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2205 | |
Dan Gohman | 3ab1312 | 2010-04-13 16:49:23 +0000 | [diff] [blame] | 2206 | if (Ops.size() == 1) return Ops[0]; |
| 2207 | } |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2208 | |
| 2209 | // Find the first UMax |
| 2210 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr) |
| 2211 | ++Idx; |
| 2212 | |
| 2213 | // Check to see if one of the operands is a UMax. If so, expand its operands |
| 2214 | // onto our operand list, and recurse to simplify. |
| 2215 | if (Idx < Ops.size()) { |
| 2216 | bool DeletedUMax = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2217 | while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2218 | Ops.erase(Ops.begin()+Idx); |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 2219 | Ops.append(UMax->op_begin(), UMax->op_end()); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2220 | DeletedUMax = true; |
| 2221 | } |
| 2222 | |
| 2223 | if (DeletedUMax) |
| 2224 | return getUMaxExpr(Ops); |
| 2225 | } |
| 2226 | |
| 2227 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 2228 | // so, delete one. Since we sorted the list, these values are required to |
| 2229 | // be adjacent. |
| 2230 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
Dan Gohman | 2828779 | 2010-04-13 16:51:03 +0000 | [diff] [blame] | 2231 | // X umax Y umax Y --> X umax Y |
| 2232 | // X umax Y --> X, if X is always greater than Y |
| 2233 | if (Ops[i] == Ops[i+1] || |
| 2234 | isKnownPredicate(ICmpInst::ICMP_UGE, Ops[i], Ops[i+1])) { |
| 2235 | Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2); |
| 2236 | --i; --e; |
| 2237 | } else if (isKnownPredicate(ICmpInst::ICMP_ULE, Ops[i], Ops[i+1])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2238 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 2239 | --i; --e; |
| 2240 | } |
| 2241 | |
| 2242 | if (Ops.size() == 1) return Ops[0]; |
| 2243 | |
| 2244 | assert(!Ops.empty() && "Reduced umax down to nothing!"); |
| 2245 | |
| 2246 | // Okay, it looks like we really DO need a umax expr. Check to see if we |
| 2247 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2248 | FoldingSetNodeID ID; |
| 2249 | ID.AddInteger(scUMaxExpr); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2250 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 2251 | ID.AddPointer(Ops[i]); |
| 2252 | void *IP = 0; |
| 2253 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 2254 | const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); |
| 2255 | std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 2256 | SCEV *S = new (SCEVAllocator) SCEVUMaxExpr(ID.Intern(SCEVAllocator), |
| 2257 | O, Ops.size()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2258 | UniqueSCEVs.InsertNode(S, IP); |
| 2259 | return S; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2262 | const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS, |
| 2263 | const SCEV *RHS) { |
Dan Gohman | f9a9a99 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2264 | // ~smax(~x, ~y) == smin(x, y). |
| 2265 | return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 2266 | } |
| 2267 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2268 | const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS, |
| 2269 | const SCEV *RHS) { |
Dan Gohman | f9a9a99 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2270 | // ~umax(~x, ~y) == umin(x, y) |
| 2271 | return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 2272 | } |
| 2273 | |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2274 | const SCEV *ScalarEvolution::getSizeOfExpr(const Type *AllocTy) { |
Dan Gohman | 6ab10f6 | 2010-04-12 23:03:26 +0000 | [diff] [blame] | 2275 | // If we have TargetData, we can bypass creating a target-independent |
| 2276 | // constant expression and then folding it back into a ConstantInt. |
| 2277 | // This is just a compile-time optimization. |
| 2278 | if (TD) |
| 2279 | return getConstant(TD->getIntPtrType(getContext()), |
| 2280 | TD->getTypeAllocSize(AllocTy)); |
| 2281 | |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2282 | Constant *C = ConstantExpr::getSizeOf(AllocTy); |
| 2283 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
Dan Gohman | 7000122 | 2010-05-28 16:12:08 +0000 | [diff] [blame] | 2284 | if (Constant *Folded = ConstantFoldConstantExpression(CE, TD)) |
| 2285 | C = Folded; |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2286 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy)); |
| 2287 | return getTruncateOrZeroExtend(getSCEV(C), Ty); |
| 2288 | } |
| 2289 | |
| 2290 | const SCEV *ScalarEvolution::getAlignOfExpr(const Type *AllocTy) { |
| 2291 | Constant *C = ConstantExpr::getAlignOf(AllocTy); |
| 2292 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
Dan Gohman | 7000122 | 2010-05-28 16:12:08 +0000 | [diff] [blame] | 2293 | if (Constant *Folded = ConstantFoldConstantExpression(CE, TD)) |
| 2294 | C = Folded; |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2295 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy)); |
| 2296 | return getTruncateOrZeroExtend(getSCEV(C), Ty); |
| 2297 | } |
| 2298 | |
| 2299 | const SCEV *ScalarEvolution::getOffsetOfExpr(const StructType *STy, |
| 2300 | unsigned FieldNo) { |
Dan Gohman | 6ab10f6 | 2010-04-12 23:03:26 +0000 | [diff] [blame] | 2301 | // If we have TargetData, we can bypass creating a target-independent |
| 2302 | // constant expression and then folding it back into a ConstantInt. |
| 2303 | // This is just a compile-time optimization. |
| 2304 | if (TD) |
| 2305 | return getConstant(TD->getIntPtrType(getContext()), |
| 2306 | TD->getStructLayout(STy)->getElementOffset(FieldNo)); |
| 2307 | |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 2308 | Constant *C = ConstantExpr::getOffsetOf(STy, FieldNo); |
| 2309 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
Dan Gohman | 7000122 | 2010-05-28 16:12:08 +0000 | [diff] [blame] | 2310 | if (Constant *Folded = ConstantFoldConstantExpression(CE, TD)) |
| 2311 | C = Folded; |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2312 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy)); |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 2313 | return getTruncateOrZeroExtend(getSCEV(C), Ty); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2316 | const SCEV *ScalarEvolution::getOffsetOfExpr(const Type *CTy, |
| 2317 | Constant *FieldNo) { |
| 2318 | Constant *C = ConstantExpr::getOffsetOf(CTy, FieldNo); |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 2319 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
Dan Gohman | 7000122 | 2010-05-28 16:12:08 +0000 | [diff] [blame] | 2320 | if (Constant *Folded = ConstantFoldConstantExpression(CE, TD)) |
| 2321 | C = Folded; |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2322 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(CTy)); |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 2323 | return getTruncateOrZeroExtend(getSCEV(C), Ty); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2326 | const SCEV *ScalarEvolution::getUnknown(Value *V) { |
Dan Gohman | 6bbcba1 | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2327 | // Don't attempt to do anything other than create a SCEVUnknown object |
| 2328 | // here. createSCEV only calls getUnknown after checking for all other |
| 2329 | // interesting possibilities, and any other code that calls getUnknown |
| 2330 | // is doing so in order to hide a value from SCEV canonicalization. |
| 2331 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2332 | FoldingSetNodeID ID; |
| 2333 | ID.AddInteger(scUnknown); |
| 2334 | ID.AddPointer(V); |
| 2335 | void *IP = 0; |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 2336 | if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) { |
| 2337 | assert(cast<SCEVUnknown>(S)->getValue() == V && |
| 2338 | "Stale SCEVUnknown in uniquing map!"); |
| 2339 | return S; |
| 2340 | } |
| 2341 | SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator), V, this, |
| 2342 | FirstUnknown); |
| 2343 | FirstUnknown = cast<SCEVUnknown>(S); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2344 | UniqueSCEVs.InsertNode(S, IP); |
| 2345 | return S; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2348 | //===----------------------------------------------------------------------===// |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2349 | // Basic SCEV Analysis and PHI Idiom Recognition Code |
| 2350 | // |
| 2351 | |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2352 | /// isSCEVable - Test if values of the given type are analyzable within |
| 2353 | /// the SCEV framework. This primarily includes integer types, and it |
| 2354 | /// can optionally include pointer types if the ScalarEvolution class |
| 2355 | /// has access to target-specific information. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2356 | bool ScalarEvolution::isSCEVable(const Type *Ty) const { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2357 | // Integers and pointers are always SCEVable. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2358 | return Ty->isIntegerTy() || Ty->isPointerTy(); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | /// getTypeSizeInBits - Return the size in bits of the specified type, |
| 2362 | /// for which isSCEVable must return true. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2363 | uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2364 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2365 | |
| 2366 | // If we have a TargetData, use it! |
| 2367 | if (TD) |
| 2368 | return TD->getTypeSizeInBits(Ty); |
| 2369 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2370 | // Integer types have fixed sizes. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2371 | if (Ty->isIntegerTy()) |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2372 | return Ty->getPrimitiveSizeInBits(); |
| 2373 | |
| 2374 | // The only other support type is pointer. Without TargetData, conservatively |
| 2375 | // assume pointers are 64-bit. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2376 | assert(Ty->isPointerTy() && "isSCEVable permitted a non-SCEVable type!"); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2377 | return 64; |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | /// getEffectiveSCEVType - Return a type with the same bitwidth as |
| 2381 | /// the given type and which represents how SCEV will treat the given |
| 2382 | /// type, for which isSCEVable must return true. For pointer types, |
| 2383 | /// this is the pointer-sized integer type. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2384 | const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2385 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2386 | |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2387 | if (Ty->isIntegerTy()) |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2388 | return Ty; |
| 2389 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2390 | // The only other support type is pointer. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2391 | assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!"); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2392 | if (TD) return TD->getIntPtrType(getContext()); |
| 2393 | |
| 2394 | // Without TargetData, conservatively assume pointers are 64-bit. |
| 2395 | return Type::getInt64Ty(getContext()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2396 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2397 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2398 | const SCEV *ScalarEvolution::getCouldNotCompute() { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2399 | return &CouldNotCompute; |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2402 | /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the |
| 2403 | /// expression and create a new one. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2404 | const SCEV *ScalarEvolution::getSCEV(Value *V) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2405 | assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2406 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2407 | ValueExprMapType::const_iterator I = ValueExprMap.find(V); |
| 2408 | if (I != ValueExprMap.end()) return I->second; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2409 | const SCEV *S = createSCEV(V); |
Dan Gohman | 619d332 | 2010-08-16 16:31:39 +0000 | [diff] [blame] | 2410 | |
| 2411 | // The process of creating a SCEV for V may have caused other SCEVs |
| 2412 | // to have been created, so it's necessary to insert the new entry |
| 2413 | // from scratch, rather than trying to remember the insert position |
| 2414 | // above. |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2415 | ValueExprMap.insert(std::make_pair(SCEVCallbackVH(V, this), S)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2416 | return S; |
| 2417 | } |
| 2418 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2419 | /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V |
| 2420 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2421 | const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) { |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2422 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2423 | return getConstant( |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2424 | cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue()))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2425 | |
| 2426 | const Type *Ty = V->getType(); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2427 | Ty = getEffectiveSCEVType(Ty); |
Owen Anderson | 73c6b71 | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2428 | return getMulExpr(V, |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2429 | getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty)))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2433 | const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) { |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2434 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Owen Anderson | 73c6b71 | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2435 | return getConstant( |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2436 | cast<ConstantInt>(ConstantExpr::getNot(VC->getValue()))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2437 | |
| 2438 | const Type *Ty = V->getType(); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2439 | Ty = getEffectiveSCEVType(Ty); |
Owen Anderson | 73c6b71 | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2440 | const SCEV *AllOnes = |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2441 | getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2442 | return getMinusSCEV(AllOnes, V); |
| 2443 | } |
| 2444 | |
| 2445 | /// getMinusSCEV - Return a SCEV corresponding to LHS - RHS. |
| 2446 | /// |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2447 | const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, |
| 2448 | const SCEV *RHS) { |
Dan Gohman | eb4152c | 2010-07-20 16:53:00 +0000 | [diff] [blame] | 2449 | // Fast path: X - X --> 0. |
| 2450 | if (LHS == RHS) |
| 2451 | return getConstant(LHS->getType(), 0); |
| 2452 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2453 | // X - Y --> X + -Y |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2454 | return getAddExpr(LHS, getNegativeSCEV(RHS)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
| 2457 | /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2458 | /// input value to the specified type. If the type must be extended, it is zero |
| 2459 | /// extended. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2460 | const SCEV * |
| 2461 | ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2462 | const Type *Ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2463 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2464 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2465 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2466 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2467 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2468 | return V; // No conversion |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2469 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2470 | return getTruncateExpr(V, Ty); |
| 2471 | return getZeroExtendExpr(V, Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2475 | /// input value to the specified type. If the type must be extended, it is sign |
| 2476 | /// extended. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2477 | const SCEV * |
| 2478 | ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2479 | const Type *Ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2480 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2481 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2482 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2483 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2484 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2485 | return V; // No conversion |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2486 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2487 | return getTruncateExpr(V, Ty); |
| 2488 | return getSignExtendExpr(V, Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2491 | /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2492 | /// input value to the specified type. If the type must be extended, it is zero |
| 2493 | /// extended. The conversion must not be narrowing. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2494 | const SCEV * |
| 2495 | ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2496 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2497 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2498 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2499 | "Cannot noop or zero extend with non-integer arguments!"); |
| 2500 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2501 | "getNoopOrZeroExtend cannot truncate!"); |
| 2502 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2503 | return V; // No conversion |
| 2504 | return getZeroExtendExpr(V, Ty); |
| 2505 | } |
| 2506 | |
| 2507 | /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2508 | /// input value to the specified type. If the type must be extended, it is sign |
| 2509 | /// extended. The conversion must not be narrowing. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2510 | const SCEV * |
| 2511 | ScalarEvolution::getNoopOrSignExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2512 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2513 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2514 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2515 | "Cannot noop or sign extend with non-integer arguments!"); |
| 2516 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2517 | "getNoopOrSignExtend cannot truncate!"); |
| 2518 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2519 | return V; // No conversion |
| 2520 | return getSignExtendExpr(V, Ty); |
| 2521 | } |
| 2522 | |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2523 | /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of |
| 2524 | /// the input value to the specified type. If the type must be extended, |
| 2525 | /// it is extended with unspecified bits. The conversion must not be |
| 2526 | /// narrowing. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2527 | const SCEV * |
| 2528 | ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2529 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2530 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2531 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2532 | "Cannot noop or any extend with non-integer arguments!"); |
| 2533 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2534 | "getNoopOrAnyExtend cannot truncate!"); |
| 2535 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2536 | return V; // No conversion |
| 2537 | return getAnyExtendExpr(V, Ty); |
| 2538 | } |
| 2539 | |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2540 | /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the |
| 2541 | /// input value to the specified type. The conversion must not be widening. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2542 | const SCEV * |
| 2543 | ScalarEvolution::getTruncateOrNoop(const SCEV *V, const Type *Ty) { |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2544 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2545 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2546 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2547 | "Cannot truncate or noop with non-integer arguments!"); |
| 2548 | assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) && |
| 2549 | "getTruncateOrNoop cannot extend!"); |
| 2550 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2551 | return V; // No conversion |
| 2552 | return getTruncateExpr(V, Ty); |
| 2553 | } |
| 2554 | |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2555 | /// getUMaxFromMismatchedTypes - Promote the operands to the wider of |
| 2556 | /// the types using zero-extension, and then perform a umax operation |
| 2557 | /// with them. |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2558 | const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS, |
| 2559 | const SCEV *RHS) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2560 | const SCEV *PromotedLHS = LHS; |
| 2561 | const SCEV *PromotedRHS = RHS; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2562 | |
| 2563 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2564 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2565 | else |
| 2566 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2567 | |
| 2568 | return getUMaxExpr(PromotedLHS, PromotedRHS); |
| 2569 | } |
| 2570 | |
Dan Gohman | c9759e8 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2571 | /// getUMinFromMismatchedTypes - Promote the operands to the wider of |
| 2572 | /// the types using zero-extension, and then perform a umin operation |
| 2573 | /// with them. |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2574 | const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS, |
| 2575 | const SCEV *RHS) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2576 | const SCEV *PromotedLHS = LHS; |
| 2577 | const SCEV *PromotedRHS = RHS; |
Dan Gohman | c9759e8 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2578 | |
| 2579 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2580 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2581 | else |
| 2582 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2583 | |
| 2584 | return getUMinExpr(PromotedLHS, PromotedRHS); |
| 2585 | } |
| 2586 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2587 | /// PushDefUseChildren - Push users of the given Instruction |
| 2588 | /// onto the given Worklist. |
| 2589 | static void |
| 2590 | PushDefUseChildren(Instruction *I, |
| 2591 | SmallVectorImpl<Instruction *> &Worklist) { |
| 2592 | // Push the def-use children onto the Worklist stack. |
| 2593 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 2594 | UI != UE; ++UI) |
Gabor Greif | 96f1d8e | 2010-07-22 13:36:47 +0000 | [diff] [blame] | 2595 | Worklist.push_back(cast<Instruction>(*UI)); |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2596 | } |
| 2597 | |
| 2598 | /// ForgetSymbolicValue - This looks up computed SCEV values for all |
| 2599 | /// instructions that depend on the given instruction and removes them from |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2600 | /// the ValueExprMapType map if they reference SymName. This is used during PHI |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2601 | /// resolution. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2602 | void |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2603 | ScalarEvolution::ForgetSymbolicName(Instruction *PN, const SCEV *SymName) { |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2604 | SmallVector<Instruction *, 16> Worklist; |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2605 | PushDefUseChildren(PN, Worklist); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2606 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2607 | SmallPtrSet<Instruction *, 8> Visited; |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2608 | Visited.insert(PN); |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2609 | while (!Worklist.empty()) { |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2610 | Instruction *I = Worklist.pop_back_val(); |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2611 | if (!Visited.insert(I)) continue; |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2612 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2613 | ValueExprMapType::iterator It = |
| 2614 | ValueExprMap.find(static_cast<Value *>(I)); |
| 2615 | if (It != ValueExprMap.end()) { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 2616 | const SCEV *Old = It->second; |
| 2617 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2618 | // Short-circuit the def-use traversal if the symbolic name |
| 2619 | // ceases to appear in expressions. |
Dan Gohman | 4ce32db | 2010-11-17 22:27:42 +0000 | [diff] [blame] | 2620 | if (Old != SymName && !hasOperand(Old, SymName)) |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2621 | continue; |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2622 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2623 | // SCEVUnknown for a PHI either means that it has an unrecognized |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2624 | // structure, it's a PHI that's in the progress of being computed |
| 2625 | // by createNodeForPHI, or it's a single-value PHI. In the first case, |
| 2626 | // additional loop trip count information isn't going to change anything. |
| 2627 | // In the second case, createNodeForPHI will perform the necessary |
| 2628 | // updates on its own when it gets to that point. In the third, we do |
| 2629 | // want to forget the SCEVUnknown. |
| 2630 | if (!isa<PHINode>(I) || |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 2631 | !isa<SCEVUnknown>(Old) || |
| 2632 | (I != PN && Old == SymName)) { |
Dan Gohman | 56a7568 | 2010-11-17 23:28:48 +0000 | [diff] [blame] | 2633 | forgetMemoizedResults(Old); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2634 | ValueExprMap.erase(It); |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 2635 | } |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | PushDefUseChildren(I, Worklist); |
| 2639 | } |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2640 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2641 | |
| 2642 | /// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in |
| 2643 | /// a loop header, making it a potential recurrence, or it doesn't. |
| 2644 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2645 | const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) { |
Dan Gohman | 27dead4 | 2010-04-12 07:49:36 +0000 | [diff] [blame] | 2646 | if (const Loop *L = LI->getLoopFor(PN->getParent())) |
| 2647 | if (L->getHeader() == PN->getParent()) { |
| 2648 | // The loop may have multiple entrances or multiple exits; we can analyze |
| 2649 | // this phi as an addrec if it has a unique entry value and a unique |
| 2650 | // backedge value. |
| 2651 | Value *BEValueV = 0, *StartValueV = 0; |
| 2652 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 2653 | Value *V = PN->getIncomingValue(i); |
| 2654 | if (L->contains(PN->getIncomingBlock(i))) { |
| 2655 | if (!BEValueV) { |
| 2656 | BEValueV = V; |
| 2657 | } else if (BEValueV != V) { |
| 2658 | BEValueV = 0; |
| 2659 | break; |
| 2660 | } |
| 2661 | } else if (!StartValueV) { |
| 2662 | StartValueV = V; |
| 2663 | } else if (StartValueV != V) { |
| 2664 | StartValueV = 0; |
| 2665 | break; |
| 2666 | } |
| 2667 | } |
| 2668 | if (BEValueV && StartValueV) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2669 | // While we are analyzing this PHI node, handle its value symbolically. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2670 | const SCEV *SymbolicName = getUnknown(PN); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2671 | assert(ValueExprMap.find(PN) == ValueExprMap.end() && |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2672 | "PHI node already processed?"); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2673 | ValueExprMap.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2674 | |
| 2675 | // Using this symbolic name for the PHI, analyze the value coming around |
| 2676 | // the back-edge. |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2677 | const SCEV *BEValue = getSCEV(BEValueV); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2678 | |
| 2679 | // NOTE: If BEValue is loop invariant, we know that the PHI node just |
| 2680 | // has a special value for the first iteration of the loop. |
| 2681 | |
| 2682 | // If the value coming around the backedge is an add with the symbolic |
| 2683 | // value we just inserted, then we found a simple induction variable! |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2684 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2685 | // If there is a single occurrence of the symbolic value, replace it |
| 2686 | // with a recurrence. |
| 2687 | unsigned FoundIndex = Add->getNumOperands(); |
| 2688 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2689 | if (Add->getOperand(i) == SymbolicName) |
| 2690 | if (FoundIndex == e) { |
| 2691 | FoundIndex = i; |
| 2692 | break; |
| 2693 | } |
| 2694 | |
| 2695 | if (FoundIndex != Add->getNumOperands()) { |
| 2696 | // Create an add with everything but the specified operand. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2697 | SmallVector<const SCEV *, 8> Ops; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2698 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2699 | if (i != FoundIndex) |
| 2700 | Ops.push_back(Add->getOperand(i)); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2701 | const SCEV *Accum = getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2702 | |
| 2703 | // This is not a valid addrec if the step amount is varying each |
| 2704 | // loop iteration, but is not itself an addrec in this loop. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 2705 | if (isLoopInvariant(Accum, L) || |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2706 | (isa<SCEVAddRecExpr>(Accum) && |
| 2707 | cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2708 | bool HasNUW = false; |
| 2709 | bool HasNSW = false; |
| 2710 | |
| 2711 | // If the increment doesn't overflow, then neither the addrec nor |
| 2712 | // the post-increment will overflow. |
| 2713 | if (const AddOperator *OBO = dyn_cast<AddOperator>(BEValueV)) { |
| 2714 | if (OBO->hasNoUnsignedWrap()) |
| 2715 | HasNUW = true; |
| 2716 | if (OBO->hasNoSignedWrap()) |
| 2717 | HasNSW = true; |
| 2718 | } |
| 2719 | |
Dan Gohman | 27dead4 | 2010-04-12 07:49:36 +0000 | [diff] [blame] | 2720 | const SCEV *StartVal = getSCEV(StartValueV); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2721 | const SCEV *PHISCEV = |
| 2722 | getAddRecExpr(StartVal, Accum, L, HasNUW, HasNSW); |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2723 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2724 | // Since the no-wrap flags are on the increment, they apply to the |
| 2725 | // post-incremented value as well. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 2726 | if (isLoopInvariant(Accum, L)) |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2727 | (void)getAddRecExpr(getAddExpr(StartVal, Accum), |
| 2728 | Accum, L, HasNUW, HasNSW); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2729 | |
| 2730 | // Okay, for the entire analysis of this edge we assumed the PHI |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2731 | // to be symbolic. We now need to go back and purge all of the |
| 2732 | // entries for the scalars that use the symbolic expression. |
| 2733 | ForgetSymbolicName(PN, SymbolicName); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2734 | ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2735 | return PHISCEV; |
| 2736 | } |
| 2737 | } |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2738 | } else if (const SCEVAddRecExpr *AddRec = |
| 2739 | dyn_cast<SCEVAddRecExpr>(BEValue)) { |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2740 | // Otherwise, this could be a loop like this: |
| 2741 | // i = 0; for (j = 1; ..; ++j) { .... i = j; } |
| 2742 | // In this case, j = {1,+,1} and BEValue is j. |
| 2743 | // Because the other in-value of i (0) fits the evolution of BEValue |
| 2744 | // i really is an addrec evolution. |
| 2745 | if (AddRec->getLoop() == L && AddRec->isAffine()) { |
Dan Gohman | 27dead4 | 2010-04-12 07:49:36 +0000 | [diff] [blame] | 2746 | const SCEV *StartVal = getSCEV(StartValueV); |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2747 | |
| 2748 | // If StartVal = j.start - j.stride, we can use StartVal as the |
| 2749 | // initial step of the addrec evolution. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2750 | if (StartVal == getMinusSCEV(AddRec->getOperand(0), |
Dan Gohman | 5ee60f7 | 2010-04-11 23:44:58 +0000 | [diff] [blame] | 2751 | AddRec->getOperand(1))) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2752 | const SCEV *PHISCEV = |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2753 | getAddRecExpr(StartVal, AddRec->getOperand(1), L); |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2754 | |
| 2755 | // Okay, for the entire analysis of this edge we assumed the PHI |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2756 | // to be symbolic. We now need to go back and purge all of the |
| 2757 | // entries for the scalars that use the symbolic expression. |
| 2758 | ForgetSymbolicName(PN, SymbolicName); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2759 | ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV; |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2760 | return PHISCEV; |
| 2761 | } |
| 2762 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2763 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2764 | } |
Dan Gohman | 27dead4 | 2010-04-12 07:49:36 +0000 | [diff] [blame] | 2765 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2766 | |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2767 | // If the PHI has a single incoming value, follow that value, unless the |
| 2768 | // PHI's incoming blocks are in a different loop, in which case doing so |
| 2769 | // risks breaking LCSSA form. Instcombine would normally zap these, but |
| 2770 | // it doesn't have DominatorTree information, so it may miss cases. |
Duncan Sands | d0c6f3d | 2010-11-18 19:59:41 +0000 | [diff] [blame] | 2771 | if (Value *V = SimplifyInstruction(PN, TD, DT)) |
| 2772 | if (LI->replacementPreservesLCSSAForm(PN, V)) |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2773 | return getSCEV(V); |
Duncan Sands | 6f8a5dd | 2010-11-17 20:49:12 +0000 | [diff] [blame] | 2774 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2775 | // If it's not a loop phi, we can't handle it yet. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2776 | return getUnknown(PN); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2779 | /// createNodeForGEP - Expand GEP instructions into add and multiply |
| 2780 | /// operations. This allows them to be analyzed by regular SCEV code. |
| 2781 | /// |
Dan Gohman | d281ed2 | 2009-12-18 02:09:29 +0000 | [diff] [blame] | 2782 | const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) { |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2783 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2784 | // Don't blindly transfer the inbounds flag from the GEP instruction to the |
| 2785 | // Add expression, because the Instruction may be guarded by control flow |
| 2786 | // and the no-overflow bits may not be valid for the expression in any |
Dan Gohman | 70eff63 | 2010-06-30 17:27:11 +0000 | [diff] [blame] | 2787 | // context. |
Dan Gohman | 7a64257 | 2010-06-29 01:41:41 +0000 | [diff] [blame] | 2788 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2789 | const Type *IntPtrTy = getEffectiveSCEVType(GEP->getType()); |
Dan Gohman | e810b0d | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2790 | Value *Base = GEP->getOperand(0); |
Dan Gohman | c63a627 | 2009-05-09 00:14:52 +0000 | [diff] [blame] | 2791 | // Don't attempt to analyze GEPs over unsized objects. |
| 2792 | if (!cast<PointerType>(Base->getType())->getElementType()->isSized()) |
| 2793 | return getUnknown(GEP); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 2794 | const SCEV *TotalOffset = getConstant(IntPtrTy, 0); |
Dan Gohman | e810b0d | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2795 | gep_type_iterator GTI = gep_type_begin(GEP); |
Oscar Fuentes | ee56c42 | 2010-08-02 06:00:15 +0000 | [diff] [blame] | 2796 | for (GetElementPtrInst::op_iterator I = llvm::next(GEP->op_begin()), |
Dan Gohman | e810b0d | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2797 | E = GEP->op_end(); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2798 | I != E; ++I) { |
| 2799 | Value *Index = *I; |
| 2800 | // Compute the (potentially symbolic) offset in bytes for this index. |
| 2801 | if (const StructType *STy = dyn_cast<StructType>(*GTI++)) { |
| 2802 | // For a struct, add the member offset. |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2803 | unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue(); |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2804 | const SCEV *FieldOffset = getOffsetOfExpr(STy, FieldNo); |
| 2805 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2806 | // Add the field offset to the running total offset. |
Dan Gohman | 70eff63 | 2010-06-30 17:27:11 +0000 | [diff] [blame] | 2807 | TotalOffset = getAddExpr(TotalOffset, FieldOffset); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2808 | } else { |
| 2809 | // For an array, add the element offset, explicitly scaled. |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2810 | const SCEV *ElementSize = getSizeOfExpr(*GTI); |
| 2811 | const SCEV *IndexS = getSCEV(Index); |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 2812 | // Getelementptr indices are signed. |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2813 | IndexS = getTruncateOrSignExtend(IndexS, IntPtrTy); |
| 2814 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2815 | // Multiply the index by the element size to compute the element offset. |
Dan Gohman | 70eff63 | 2010-06-30 17:27:11 +0000 | [diff] [blame] | 2816 | const SCEV *LocalOffset = getMulExpr(IndexS, ElementSize); |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2817 | |
| 2818 | // Add the element offset to the running total offset. |
Dan Gohman | 70eff63 | 2010-06-30 17:27:11 +0000 | [diff] [blame] | 2819 | TotalOffset = getAddExpr(TotalOffset, LocalOffset); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2820 | } |
| 2821 | } |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2822 | |
| 2823 | // Get the SCEV for the GEP base. |
| 2824 | const SCEV *BaseS = getSCEV(Base); |
| 2825 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2826 | // Add the total offset from all the GEP indices to the base. |
Dan Gohman | 70eff63 | 2010-06-30 17:27:11 +0000 | [diff] [blame] | 2827 | return getAddExpr(BaseS, TotalOffset); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2830 | /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is |
| 2831 | /// guaranteed to end in (at every loop iteration). It is, at the same time, |
| 2832 | /// the minimum number of times S is divisible by 2. For example, given {4,+,8} |
| 2833 | /// it returns 2. If S is guaranteed to be 0, it returns the bitwidth of S. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2834 | uint32_t |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2835 | ScalarEvolution::GetMinTrailingZeros(const SCEV *S) { |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2836 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Chris Lattner | 8314a0c | 2007-11-23 22:36:49 +0000 | [diff] [blame] | 2837 | return C->getValue()->getValue().countTrailingZeros(); |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2838 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2839 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S)) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2840 | return std::min(GetMinTrailingZeros(T->getOperand()), |
| 2841 | (uint32_t)getTypeSizeInBits(T->getType())); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2842 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2843 | if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) { |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2844 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 2845 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 2846 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2849 | if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) { |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2850 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 2851 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 2852 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2855 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2856 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2857 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2858 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2859 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2860 | return MinOpRes; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2863 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2864 | // The result is the sum of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2865 | uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0)); |
| 2866 | uint32_t BitWidth = getTypeSizeInBits(M->getType()); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2867 | for (unsigned i = 1, e = M->getNumOperands(); |
| 2868 | SumOpRes != BitWidth && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2869 | SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2870 | BitWidth); |
| 2871 | return SumOpRes; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2872 | } |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2873 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2874 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2875 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2876 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2877 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2878 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2879 | return MinOpRes; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2880 | } |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2881 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2882 | if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2883 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2884 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2885 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2886 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2887 | return MinOpRes; |
| 2888 | } |
| 2889 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2890 | if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2891 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2892 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2893 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2894 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2895 | return MinOpRes; |
| 2896 | } |
| 2897 | |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2898 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2899 | // For a SCEVUnknown, ask ValueTracking. |
| 2900 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2901 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 2902 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 2903 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones); |
| 2904 | return Zeros.countTrailingOnes(); |
| 2905 | } |
| 2906 | |
| 2907 | // SCEVUDivExpr |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2908 | return 0; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2909 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2910 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2911 | /// getUnsignedRange - Determine the unsigned range for a particular SCEV. |
| 2912 | /// |
| 2913 | ConstantRange |
| 2914 | ScalarEvolution::getUnsignedRange(const SCEV *S) { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 2915 | // See if we've computed this range already. |
| 2916 | DenseMap<const SCEV *, ConstantRange>::iterator I = UnsignedRanges.find(S); |
| 2917 | if (I != UnsignedRanges.end()) |
| 2918 | return I->second; |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2919 | |
| 2920 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2921 | return setUnsignedRange(C, ConstantRange(C->getValue()->getValue())); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2922 | |
Dan Gohman | c9c36cb | 2010-01-26 19:19:05 +0000 | [diff] [blame] | 2923 | unsigned BitWidth = getTypeSizeInBits(S->getType()); |
| 2924 | ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true); |
| 2925 | |
| 2926 | // If the value has known zeros, the maximum unsigned value will have those |
| 2927 | // known zeros as well. |
| 2928 | uint32_t TZ = GetMinTrailingZeros(S); |
| 2929 | if (TZ != 0) |
| 2930 | ConservativeResult = |
| 2931 | ConstantRange(APInt::getMinValue(BitWidth), |
| 2932 | APInt::getMaxValue(BitWidth).lshr(TZ).shl(TZ) + 1); |
| 2933 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2934 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 2935 | ConstantRange X = getUnsignedRange(Add->getOperand(0)); |
| 2936 | for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) |
| 2937 | X = X.add(getUnsignedRange(Add->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2938 | return setUnsignedRange(Add, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
| 2941 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { |
| 2942 | ConstantRange X = getUnsignedRange(Mul->getOperand(0)); |
| 2943 | for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) |
| 2944 | X = X.multiply(getUnsignedRange(Mul->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2945 | return setUnsignedRange(Mul, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) { |
| 2949 | ConstantRange X = getUnsignedRange(SMax->getOperand(0)); |
| 2950 | for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) |
| 2951 | X = X.smax(getUnsignedRange(SMax->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2952 | return setUnsignedRange(SMax, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
| 2955 | if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) { |
| 2956 | ConstantRange X = getUnsignedRange(UMax->getOperand(0)); |
| 2957 | for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) |
| 2958 | X = X.umax(getUnsignedRange(UMax->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2959 | return setUnsignedRange(UMax, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
| 2962 | if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { |
| 2963 | ConstantRange X = getUnsignedRange(UDiv->getLHS()); |
| 2964 | ConstantRange Y = getUnsignedRange(UDiv->getRHS()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2965 | return setUnsignedRange(UDiv, ConservativeResult.intersectWith(X.udiv(Y))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
| 2968 | if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { |
| 2969 | ConstantRange X = getUnsignedRange(ZExt->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2970 | return setUnsignedRange(ZExt, |
| 2971 | ConservativeResult.intersectWith(X.zeroExtend(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { |
| 2975 | ConstantRange X = getUnsignedRange(SExt->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2976 | return setUnsignedRange(SExt, |
| 2977 | ConservativeResult.intersectWith(X.signExtend(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
| 2980 | if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { |
| 2981 | ConstantRange X = getUnsignedRange(Trunc->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2982 | return setUnsignedRange(Trunc, |
| 2983 | ConservativeResult.intersectWith(X.truncate(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2986 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2987 | // If there's no unsigned wrap, the value will never be less than its |
| 2988 | // initial value. |
| 2989 | if (AddRec->hasNoUnsignedWrap()) |
| 2990 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(AddRec->getStart())) |
Dan Gohman | bca091d | 2010-04-12 23:08:18 +0000 | [diff] [blame] | 2991 | if (!C->getValue()->isZero()) |
Dan Gohman | bc7129f | 2010-04-11 22:12:18 +0000 | [diff] [blame] | 2992 | ConservativeResult = |
Dan Gohman | 8a18d6b | 2010-06-30 06:58:35 +0000 | [diff] [blame] | 2993 | ConservativeResult.intersectWith( |
| 2994 | ConstantRange(C->getValue()->getValue(), APInt(BitWidth, 0))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2995 | |
| 2996 | // TODO: non-affine addrec |
Dan Gohman | c9c36cb | 2010-01-26 19:19:05 +0000 | [diff] [blame] | 2997 | if (AddRec->isAffine()) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2998 | const Type *Ty = AddRec->getType(); |
| 2999 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop()); |
Dan Gohman | c9c36cb | 2010-01-26 19:19:05 +0000 | [diff] [blame] | 3000 | if (!isa<SCEVCouldNotCompute>(MaxBECount) && |
| 3001 | getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3002 | MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty); |
| 3003 | |
| 3004 | const SCEV *Start = AddRec->getStart(); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3005 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3006 | |
| 3007 | ConstantRange StartRange = getUnsignedRange(Start); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3008 | ConstantRange StepRange = getSignedRange(Step); |
| 3009 | ConstantRange MaxBECountRange = getUnsignedRange(MaxBECount); |
| 3010 | ConstantRange EndRange = |
| 3011 | StartRange.add(MaxBECountRange.multiply(StepRange)); |
| 3012 | |
| 3013 | // Check for overflow. This must be done with ConstantRange arithmetic |
| 3014 | // because we could be called from within the ScalarEvolution overflow |
| 3015 | // checking code. |
| 3016 | ConstantRange ExtStartRange = StartRange.zextOrTrunc(BitWidth*2+1); |
| 3017 | ConstantRange ExtStepRange = StepRange.sextOrTrunc(BitWidth*2+1); |
| 3018 | ConstantRange ExtMaxBECountRange = |
| 3019 | MaxBECountRange.zextOrTrunc(BitWidth*2+1); |
| 3020 | ConstantRange ExtEndRange = EndRange.zextOrTrunc(BitWidth*2+1); |
| 3021 | if (ExtStartRange.add(ExtMaxBECountRange.multiply(ExtStepRange)) != |
| 3022 | ExtEndRange) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3023 | return setUnsignedRange(AddRec, ConservativeResult); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3024 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3025 | APInt Min = APIntOps::umin(StartRange.getUnsignedMin(), |
| 3026 | EndRange.getUnsignedMin()); |
| 3027 | APInt Max = APIntOps::umax(StartRange.getUnsignedMax(), |
| 3028 | EndRange.getUnsignedMax()); |
| 3029 | if (Min.isMinValue() && Max.isMaxValue()) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3030 | return setUnsignedRange(AddRec, ConservativeResult); |
| 3031 | return setUnsignedRange(AddRec, |
| 3032 | ConservativeResult.intersectWith(ConstantRange(Min, Max+1))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3033 | } |
| 3034 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3035 | |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3036 | return setUnsignedRange(AddRec, ConservativeResult); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
| 3039 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 3040 | // For a SCEVUnknown, ask ValueTracking. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3041 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 3042 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 3043 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD); |
Dan Gohman | 746f3b1 | 2009-07-20 22:34:18 +0000 | [diff] [blame] | 3044 | if (Ones == ~Zeros + 1) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3045 | return setUnsignedRange(U, ConservativeResult); |
| 3046 | return setUnsignedRange(U, |
| 3047 | ConservativeResult.intersectWith(ConstantRange(Ones, ~Zeros + 1))); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3050 | return setUnsignedRange(S, ConservativeResult); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3051 | } |
| 3052 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3053 | /// getSignedRange - Determine the signed range for a particular SCEV. |
| 3054 | /// |
| 3055 | ConstantRange |
| 3056 | ScalarEvolution::getSignedRange(const SCEV *S) { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 3057 | DenseMap<const SCEV *, ConstantRange>::iterator I = SignedRanges.find(S); |
| 3058 | if (I != SignedRanges.end()) |
| 3059 | return I->second; |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3060 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3061 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3062 | return setSignedRange(C, ConstantRange(C->getValue()->getValue())); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3063 | |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 3064 | unsigned BitWidth = getTypeSizeInBits(S->getType()); |
| 3065 | ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true); |
| 3066 | |
| 3067 | // If the value has known zeros, the maximum signed value will have those |
| 3068 | // known zeros as well. |
| 3069 | uint32_t TZ = GetMinTrailingZeros(S); |
| 3070 | if (TZ != 0) |
| 3071 | ConservativeResult = |
| 3072 | ConstantRange(APInt::getSignedMinValue(BitWidth), |
| 3073 | APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1); |
| 3074 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3075 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 3076 | ConstantRange X = getSignedRange(Add->getOperand(0)); |
| 3077 | for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) |
| 3078 | X = X.add(getSignedRange(Add->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3079 | return setSignedRange(Add, ConservativeResult.intersectWith(X)); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3082 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { |
| 3083 | ConstantRange X = getSignedRange(Mul->getOperand(0)); |
| 3084 | for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) |
| 3085 | X = X.multiply(getSignedRange(Mul->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3086 | return setSignedRange(Mul, ConservativeResult.intersectWith(X)); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3089 | if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) { |
| 3090 | ConstantRange X = getSignedRange(SMax->getOperand(0)); |
| 3091 | for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) |
| 3092 | X = X.smax(getSignedRange(SMax->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3093 | return setSignedRange(SMax, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3094 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3095 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3096 | if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) { |
| 3097 | ConstantRange X = getSignedRange(UMax->getOperand(0)); |
| 3098 | for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) |
| 3099 | X = X.umax(getSignedRange(UMax->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3100 | return setSignedRange(UMax, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3101 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3102 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3103 | if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { |
| 3104 | ConstantRange X = getSignedRange(UDiv->getLHS()); |
| 3105 | ConstantRange Y = getSignedRange(UDiv->getRHS()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3106 | return setSignedRange(UDiv, ConservativeResult.intersectWith(X.udiv(Y))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3107 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3108 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3109 | if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { |
| 3110 | ConstantRange X = getSignedRange(ZExt->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3111 | return setSignedRange(ZExt, |
| 3112 | ConservativeResult.intersectWith(X.zeroExtend(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3113 | } |
| 3114 | |
| 3115 | if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { |
| 3116 | ConstantRange X = getSignedRange(SExt->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3117 | return setSignedRange(SExt, |
| 3118 | ConservativeResult.intersectWith(X.signExtend(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3119 | } |
| 3120 | |
| 3121 | if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { |
| 3122 | ConstantRange X = getSignedRange(Trunc->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3123 | return setSignedRange(Trunc, |
| 3124 | ConservativeResult.intersectWith(X.truncate(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3125 | } |
| 3126 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3127 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3128 | // If there's no signed wrap, and all the operands have the same sign or |
| 3129 | // zero, the value won't ever change sign. |
| 3130 | if (AddRec->hasNoSignedWrap()) { |
| 3131 | bool AllNonNeg = true; |
| 3132 | bool AllNonPos = true; |
| 3133 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { |
| 3134 | if (!isKnownNonNegative(AddRec->getOperand(i))) AllNonNeg = false; |
| 3135 | if (!isKnownNonPositive(AddRec->getOperand(i))) AllNonPos = false; |
| 3136 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3137 | if (AllNonNeg) |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 3138 | ConservativeResult = ConservativeResult.intersectWith( |
| 3139 | ConstantRange(APInt(BitWidth, 0), |
| 3140 | APInt::getSignedMinValue(BitWidth))); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3141 | else if (AllNonPos) |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 3142 | ConservativeResult = ConservativeResult.intersectWith( |
| 3143 | ConstantRange(APInt::getSignedMinValue(BitWidth), |
| 3144 | APInt(BitWidth, 1))); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3145 | } |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3146 | |
| 3147 | // TODO: non-affine addrec |
Dan Gohman | c9c36cb | 2010-01-26 19:19:05 +0000 | [diff] [blame] | 3148 | if (AddRec->isAffine()) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3149 | const Type *Ty = AddRec->getType(); |
| 3150 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop()); |
Dan Gohman | c9c36cb | 2010-01-26 19:19:05 +0000 | [diff] [blame] | 3151 | if (!isa<SCEVCouldNotCompute>(MaxBECount) && |
| 3152 | getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3153 | MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty); |
| 3154 | |
| 3155 | const SCEV *Start = AddRec->getStart(); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3156 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3157 | |
| 3158 | ConstantRange StartRange = getSignedRange(Start); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3159 | ConstantRange StepRange = getSignedRange(Step); |
| 3160 | ConstantRange MaxBECountRange = getUnsignedRange(MaxBECount); |
| 3161 | ConstantRange EndRange = |
| 3162 | StartRange.add(MaxBECountRange.multiply(StepRange)); |
| 3163 | |
| 3164 | // Check for overflow. This must be done with ConstantRange arithmetic |
| 3165 | // because we could be called from within the ScalarEvolution overflow |
| 3166 | // checking code. |
| 3167 | ConstantRange ExtStartRange = StartRange.sextOrTrunc(BitWidth*2+1); |
| 3168 | ConstantRange ExtStepRange = StepRange.sextOrTrunc(BitWidth*2+1); |
| 3169 | ConstantRange ExtMaxBECountRange = |
| 3170 | MaxBECountRange.zextOrTrunc(BitWidth*2+1); |
| 3171 | ConstantRange ExtEndRange = EndRange.sextOrTrunc(BitWidth*2+1); |
| 3172 | if (ExtStartRange.add(ExtMaxBECountRange.multiply(ExtStepRange)) != |
| 3173 | ExtEndRange) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3174 | return setSignedRange(AddRec, ConservativeResult); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3175 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3176 | APInt Min = APIntOps::smin(StartRange.getSignedMin(), |
| 3177 | EndRange.getSignedMin()); |
| 3178 | APInt Max = APIntOps::smax(StartRange.getSignedMax(), |
| 3179 | EndRange.getSignedMax()); |
| 3180 | if (Min.isMinSignedValue() && Max.isMaxSignedValue()) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3181 | return setSignedRange(AddRec, ConservativeResult); |
| 3182 | return setSignedRange(AddRec, |
| 3183 | ConservativeResult.intersectWith(ConstantRange(Min, Max+1))); |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3184 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3185 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3186 | |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3187 | return setSignedRange(AddRec, ConservativeResult); |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3190 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 3191 | // For a SCEVUnknown, ask ValueTracking. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3192 | if (!U->getValue()->getType()->isIntegerTy() && !TD) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3193 | return setSignedRange(U, ConservativeResult); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3194 | unsigned NS = ComputeNumSignBits(U->getValue(), TD); |
| 3195 | if (NS == 1) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3196 | return setSignedRange(U, ConservativeResult); |
| 3197 | return setSignedRange(U, ConservativeResult.intersectWith( |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3198 | ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1), |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3199 | APInt::getSignedMaxValue(BitWidth).ashr(NS - 1)+1))); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3202 | return setSignedRange(S, ConservativeResult); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3205 | /// createSCEV - We know that there is no SCEV for the specified value. |
| 3206 | /// Analyze the expression. |
| 3207 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3208 | const SCEV *ScalarEvolution::createSCEV(Value *V) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 3209 | if (!isSCEVable(V->getType())) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3210 | return getUnknown(V); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3211 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3212 | unsigned Opcode = Instruction::UserOp1; |
Dan Gohman | 4ecbca5 | 2010-03-09 23:46:50 +0000 | [diff] [blame] | 3213 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3214 | Opcode = I->getOpcode(); |
Dan Gohman | 4ecbca5 | 2010-03-09 23:46:50 +0000 | [diff] [blame] | 3215 | |
| 3216 | // Don't attempt to analyze instructions in blocks that aren't |
| 3217 | // reachable. Such instructions don't matter, and they aren't required |
| 3218 | // to obey basic rules for definitions dominating uses which this |
| 3219 | // analysis depends on. |
| 3220 | if (!DT->isReachableFromEntry(I->getParent())) |
| 3221 | return getUnknown(V); |
| 3222 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3223 | Opcode = CE->getOpcode(); |
Dan Gohman | 6bbcba1 | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 3224 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 3225 | return getConstant(CI); |
| 3226 | else if (isa<ConstantPointerNull>(V)) |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 3227 | return getConstant(V->getType(), 0); |
Dan Gohman | 2681232 | 2009-08-25 17:49:57 +0000 | [diff] [blame] | 3228 | else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) |
| 3229 | return GA->mayBeOverridden() ? getUnknown(V) : getSCEV(GA->getAliasee()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3230 | else |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3231 | return getUnknown(V); |
Chris Lattner | 2811f2a | 2007-04-02 05:41:38 +0000 | [diff] [blame] | 3232 | |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 3233 | Operator *U = cast<Operator>(V); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3234 | switch (Opcode) { |
Dan Gohman | d3f171d | 2010-08-16 16:03:49 +0000 | [diff] [blame] | 3235 | case Instruction::Add: { |
| 3236 | // The simple thing to do would be to just call getSCEV on both operands |
| 3237 | // and call getAddExpr with the result. However if we're looking at a |
| 3238 | // bunch of things all added together, this can be quite inefficient, |
| 3239 | // because it leads to N-1 getAddExpr calls for N ultimate operands. |
| 3240 | // Instead, gather up all the operands and make a single getAddExpr call. |
| 3241 | // LLVM IR canonical form means we need only traverse the left operands. |
| 3242 | SmallVector<const SCEV *, 4> AddOps; |
| 3243 | AddOps.push_back(getSCEV(U->getOperand(1))); |
Dan Gohman | 3f19c09 | 2010-08-31 22:53:17 +0000 | [diff] [blame] | 3244 | for (Value *Op = U->getOperand(0); ; Op = U->getOperand(0)) { |
| 3245 | unsigned Opcode = Op->getValueID() - Value::InstructionVal; |
| 3246 | if (Opcode != Instruction::Add && Opcode != Instruction::Sub) |
| 3247 | break; |
Dan Gohman | d3f171d | 2010-08-16 16:03:49 +0000 | [diff] [blame] | 3248 | U = cast<Operator>(Op); |
Dan Gohman | 3f19c09 | 2010-08-31 22:53:17 +0000 | [diff] [blame] | 3249 | const SCEV *Op1 = getSCEV(U->getOperand(1)); |
| 3250 | if (Opcode == Instruction::Sub) |
| 3251 | AddOps.push_back(getNegativeSCEV(Op1)); |
| 3252 | else |
| 3253 | AddOps.push_back(Op1); |
Dan Gohman | d3f171d | 2010-08-16 16:03:49 +0000 | [diff] [blame] | 3254 | } |
| 3255 | AddOps.push_back(getSCEV(U->getOperand(0))); |
| 3256 | return getAddExpr(AddOps); |
| 3257 | } |
| 3258 | case Instruction::Mul: { |
| 3259 | // See the Add code above. |
| 3260 | SmallVector<const SCEV *, 4> MulOps; |
| 3261 | MulOps.push_back(getSCEV(U->getOperand(1))); |
| 3262 | for (Value *Op = U->getOperand(0); |
| 3263 | Op->getValueID() == Instruction::Mul + Value::InstructionVal; |
| 3264 | Op = U->getOperand(0)) { |
| 3265 | U = cast<Operator>(Op); |
| 3266 | MulOps.push_back(getSCEV(U->getOperand(1))); |
| 3267 | } |
| 3268 | MulOps.push_back(getSCEV(U->getOperand(0))); |
| 3269 | return getMulExpr(MulOps); |
| 3270 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3271 | case Instruction::UDiv: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3272 | return getUDivExpr(getSCEV(U->getOperand(0)), |
| 3273 | getSCEV(U->getOperand(1))); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3274 | case Instruction::Sub: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3275 | return getMinusSCEV(getSCEV(U->getOperand(0)), |
| 3276 | getSCEV(U->getOperand(1))); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3277 | case Instruction::And: |
| 3278 | // For an expression like x&255 that merely masks off the high bits, |
| 3279 | // use zext(trunc(x)) as the SCEV expression. |
| 3280 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 2c73d5f | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 3281 | if (CI->isNullValue()) |
| 3282 | return getSCEV(U->getOperand(1)); |
Dan Gohman | d6c3295 | 2009-04-27 01:41:10 +0000 | [diff] [blame] | 3283 | if (CI->isAllOnesValue()) |
| 3284 | return getSCEV(U->getOperand(0)); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3285 | const APInt &A = CI->getValue(); |
Dan Gohman | 61ffa8e | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 3286 | |
| 3287 | // Instcombine's ShrinkDemandedConstant may strip bits out of |
| 3288 | // constants, obscuring what would otherwise be a low-bits mask. |
| 3289 | // Use ComputeMaskedBits to compute what ShrinkDemandedConstant |
| 3290 | // knew about to reconstruct a low-bits mask value. |
| 3291 | unsigned LZ = A.countLeadingZeros(); |
| 3292 | unsigned BitWidth = A.getBitWidth(); |
| 3293 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
| 3294 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3295 | ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD); |
| 3296 | |
| 3297 | APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ); |
| 3298 | |
Dan Gohman | fc3641b | 2009-06-17 23:54:37 +0000 | [diff] [blame] | 3299 | if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask)) |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3300 | return |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3301 | getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3302 | IntegerType::get(getContext(), BitWidth - LZ)), |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3303 | U->getType()); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3304 | } |
| 3305 | break; |
Dan Gohman | 61ffa8e | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 3306 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3307 | case Instruction::Or: |
| 3308 | // If the RHS of the Or is a constant, we may have something like: |
| 3309 | // X*4+1 which got turned into X*4|1. Handle this as an Add so loop |
| 3310 | // optimizations will transparently handle this case. |
| 3311 | // |
| 3312 | // In order for this transformation to be safe, the LHS must be of the |
| 3313 | // form X*(2^n) and the Or constant must be less than 2^n. |
| 3314 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3315 | const SCEV *LHS = getSCEV(U->getOperand(0)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3316 | const APInt &CIVal = CI->getValue(); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3317 | if (GetMinTrailingZeros(LHS) >= |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 3318 | (CIVal.getBitWidth() - CIVal.countLeadingZeros())) { |
| 3319 | // Build a plain add SCEV. |
| 3320 | const SCEV *S = getAddExpr(LHS, getSCEV(CI)); |
| 3321 | // If the LHS of the add was an addrec and it has no-wrap flags, |
| 3322 | // transfer the no-wrap flags, since an or won't introduce a wrap. |
| 3323 | if (const SCEVAddRecExpr *NewAR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 3324 | const SCEVAddRecExpr *OldAR = cast<SCEVAddRecExpr>(LHS); |
| 3325 | if (OldAR->hasNoUnsignedWrap()) |
| 3326 | const_cast<SCEVAddRecExpr *>(NewAR)->setHasNoUnsignedWrap(true); |
| 3327 | if (OldAR->hasNoSignedWrap()) |
| 3328 | const_cast<SCEVAddRecExpr *>(NewAR)->setHasNoSignedWrap(true); |
| 3329 | } |
| 3330 | return S; |
| 3331 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3332 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3333 | break; |
| 3334 | case Instruction::Xor: |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3335 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3336 | // If the RHS of the xor is a signbit, then this is just an add. |
| 3337 | // Instcombine turns add of signbit into xor as a strength reduction step. |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3338 | if (CI->getValue().isSignBit()) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3339 | return getAddExpr(getSCEV(U->getOperand(0)), |
| 3340 | getSCEV(U->getOperand(1))); |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3341 | |
| 3342 | // If the RHS of xor is -1, then this is a not operation. |
Dan Gohman | 0bac95e | 2009-05-18 16:17:44 +0000 | [diff] [blame] | 3343 | if (CI->isAllOnesValue()) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3344 | return getNotSCEV(getSCEV(U->getOperand(0))); |
Dan Gohman | 10978bd | 2009-05-18 16:29:04 +0000 | [diff] [blame] | 3345 | |
| 3346 | // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask. |
| 3347 | // This is a variant of the check for xor with -1, and it handles |
| 3348 | // the case where instcombine has trimmed non-demanded bits out |
| 3349 | // of an xor with -1. |
| 3350 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0))) |
| 3351 | if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1))) |
| 3352 | if (BO->getOpcode() == Instruction::And && |
| 3353 | LCI->getValue() == CI->getValue()) |
| 3354 | if (const SCEVZeroExtendExpr *Z = |
Dan Gohman | 3034c10 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 3355 | dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) { |
Dan Gohman | 8205283 | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 3356 | const Type *UTy = U->getType(); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3357 | const SCEV *Z0 = Z->getOperand(); |
Dan Gohman | 8205283 | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 3358 | const Type *Z0Ty = Z0->getType(); |
| 3359 | unsigned Z0TySize = getTypeSizeInBits(Z0Ty); |
| 3360 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 3361 | // If C is a low-bits mask, the zero extend is serving to |
Dan Gohman | 8205283 | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 3362 | // mask off the high bits. Complement the operand and |
| 3363 | // re-apply the zext. |
| 3364 | if (APIntOps::isMask(Z0TySize, CI->getValue())) |
| 3365 | return getZeroExtendExpr(getNotSCEV(Z0), UTy); |
| 3366 | |
| 3367 | // If C is a single bit, it may be in the sign-bit position |
| 3368 | // before the zero-extend. In this case, represent the xor |
| 3369 | // using an add, which is equivalent, and re-apply the zext. |
| 3370 | APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize); |
| 3371 | if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() && |
| 3372 | Trunc.isSignBit()) |
| 3373 | return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)), |
| 3374 | UTy); |
Dan Gohman | 3034c10 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 3375 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3376 | } |
| 3377 | break; |
| 3378 | |
| 3379 | case Instruction::Shl: |
| 3380 | // Turn shift left of a constant amount into a multiply. |
| 3381 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 3382 | uint32_t BitWidth = cast<IntegerType>(U->getType())->getBitWidth(); |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3383 | |
| 3384 | // If the shift count is not less than the bitwidth, the result of |
| 3385 | // the shift is undefined. Don't try to analyze it, because the |
| 3386 | // resolution chosen here may differ from the resolution chosen in |
| 3387 | // other parts of the compiler. |
| 3388 | if (SA->getValue().uge(BitWidth)) |
| 3389 | break; |
| 3390 | |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3391 | Constant *X = ConstantInt::get(getContext(), |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3392 | APInt(BitWidth, 1).shl(SA->getZExtValue())); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3393 | return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3394 | } |
| 3395 | break; |
| 3396 | |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3397 | case Instruction::LShr: |
Nick Lewycky | 789558d | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 3398 | // Turn logical shift right of a constant into a unsigned divide. |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3399 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 3400 | uint32_t BitWidth = cast<IntegerType>(U->getType())->getBitWidth(); |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3401 | |
| 3402 | // If the shift count is not less than the bitwidth, the result of |
| 3403 | // the shift is undefined. Don't try to analyze it, because the |
| 3404 | // resolution chosen here may differ from the resolution chosen in |
| 3405 | // other parts of the compiler. |
| 3406 | if (SA->getValue().uge(BitWidth)) |
| 3407 | break; |
| 3408 | |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3409 | Constant *X = ConstantInt::get(getContext(), |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3410 | APInt(BitWidth, 1).shl(SA->getZExtValue())); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3411 | return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3412 | } |
| 3413 | break; |
| 3414 | |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3415 | case Instruction::AShr: |
| 3416 | // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression. |
| 3417 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3418 | if (Operator *L = dyn_cast<Operator>(U->getOperand(0))) |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3419 | if (L->getOpcode() == Instruction::Shl && |
| 3420 | L->getOperand(1) == U->getOperand(1)) { |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3421 | uint64_t BitWidth = getTypeSizeInBits(U->getType()); |
| 3422 | |
| 3423 | // If the shift count is not less than the bitwidth, the result of |
| 3424 | // the shift is undefined. Don't try to analyze it, because the |
| 3425 | // resolution chosen here may differ from the resolution chosen in |
| 3426 | // other parts of the compiler. |
| 3427 | if (CI->getValue().uge(BitWidth)) |
| 3428 | break; |
| 3429 | |
Dan Gohman | 2c73d5f | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 3430 | uint64_t Amt = BitWidth - CI->getZExtValue(); |
| 3431 | if (Amt == BitWidth) |
| 3432 | return getSCEV(L->getOperand(0)); // shift by zero --> noop |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3433 | return |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3434 | getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)), |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3435 | IntegerType::get(getContext(), |
| 3436 | Amt)), |
| 3437 | U->getType()); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3438 | } |
| 3439 | break; |
| 3440 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3441 | case Instruction::Trunc: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3442 | return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3443 | |
| 3444 | case Instruction::ZExt: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3445 | return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3446 | |
| 3447 | case Instruction::SExt: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3448 | return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3449 | |
| 3450 | case Instruction::BitCast: |
| 3451 | // BitCasts are no-op casts so we just eliminate the cast. |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 3452 | if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3453 | return getSCEV(U->getOperand(0)); |
| 3454 | break; |
| 3455 | |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 3456 | // It's tempting to handle inttoptr and ptrtoint as no-ops, however this can |
| 3457 | // lead to pointer expressions which cannot safely be expanded to GEPs, |
| 3458 | // because ScalarEvolution doesn't respect the GEP aliasing rules when |
| 3459 | // simplifying integer expressions. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3460 | |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 3461 | case Instruction::GetElementPtr: |
Dan Gohman | d281ed2 | 2009-12-18 02:09:29 +0000 | [diff] [blame] | 3462 | return createNodeForGEP(cast<GEPOperator>(U)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3463 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3464 | case Instruction::PHI: |
| 3465 | return createNodeForPHI(cast<PHINode>(U)); |
| 3466 | |
| 3467 | case Instruction::Select: |
| 3468 | // This could be a smax or umax that was lowered earlier. |
| 3469 | // Try to recover it. |
| 3470 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) { |
| 3471 | Value *LHS = ICI->getOperand(0); |
| 3472 | Value *RHS = ICI->getOperand(1); |
| 3473 | switch (ICI->getPredicate()) { |
| 3474 | case ICmpInst::ICMP_SLT: |
| 3475 | case ICmpInst::ICMP_SLE: |
| 3476 | std::swap(LHS, RHS); |
| 3477 | // fall through |
| 3478 | case ICmpInst::ICMP_SGT: |
| 3479 | case ICmpInst::ICMP_SGE: |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3480 | // a >s b ? a+x : b+x -> smax(a, b)+x |
| 3481 | // a >s b ? b+x : a+x -> smin(a, b)+x |
| 3482 | if (LHS->getType() == U->getType()) { |
| 3483 | const SCEV *LS = getSCEV(LHS); |
| 3484 | const SCEV *RS = getSCEV(RHS); |
| 3485 | const SCEV *LA = getSCEV(U->getOperand(1)); |
| 3486 | const SCEV *RA = getSCEV(U->getOperand(2)); |
| 3487 | const SCEV *LDiff = getMinusSCEV(LA, LS); |
| 3488 | const SCEV *RDiff = getMinusSCEV(RA, RS); |
| 3489 | if (LDiff == RDiff) |
| 3490 | return getAddExpr(getSMaxExpr(LS, RS), LDiff); |
| 3491 | LDiff = getMinusSCEV(LA, RS); |
| 3492 | RDiff = getMinusSCEV(RA, LS); |
| 3493 | if (LDiff == RDiff) |
| 3494 | return getAddExpr(getSMinExpr(LS, RS), LDiff); |
| 3495 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3496 | break; |
| 3497 | case ICmpInst::ICMP_ULT: |
| 3498 | case ICmpInst::ICMP_ULE: |
| 3499 | std::swap(LHS, RHS); |
| 3500 | // fall through |
| 3501 | case ICmpInst::ICMP_UGT: |
| 3502 | case ICmpInst::ICMP_UGE: |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3503 | // a >u b ? a+x : b+x -> umax(a, b)+x |
| 3504 | // a >u b ? b+x : a+x -> umin(a, b)+x |
| 3505 | if (LHS->getType() == U->getType()) { |
| 3506 | const SCEV *LS = getSCEV(LHS); |
| 3507 | const SCEV *RS = getSCEV(RHS); |
| 3508 | const SCEV *LA = getSCEV(U->getOperand(1)); |
| 3509 | const SCEV *RA = getSCEV(U->getOperand(2)); |
| 3510 | const SCEV *LDiff = getMinusSCEV(LA, LS); |
| 3511 | const SCEV *RDiff = getMinusSCEV(RA, RS); |
| 3512 | if (LDiff == RDiff) |
| 3513 | return getAddExpr(getUMaxExpr(LS, RS), LDiff); |
| 3514 | LDiff = getMinusSCEV(LA, RS); |
| 3515 | RDiff = getMinusSCEV(RA, LS); |
| 3516 | if (LDiff == RDiff) |
| 3517 | return getAddExpr(getUMinExpr(LS, RS), LDiff); |
| 3518 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3519 | break; |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3520 | case ICmpInst::ICMP_NE: |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3521 | // n != 0 ? n+x : 1+x -> umax(n, 1)+x |
| 3522 | if (LHS->getType() == U->getType() && |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3523 | isa<ConstantInt>(RHS) && |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3524 | cast<ConstantInt>(RHS)->isZero()) { |
| 3525 | const SCEV *One = getConstant(LHS->getType(), 1); |
| 3526 | const SCEV *LS = getSCEV(LHS); |
| 3527 | const SCEV *LA = getSCEV(U->getOperand(1)); |
| 3528 | const SCEV *RA = getSCEV(U->getOperand(2)); |
| 3529 | const SCEV *LDiff = getMinusSCEV(LA, LS); |
| 3530 | const SCEV *RDiff = getMinusSCEV(RA, One); |
| 3531 | if (LDiff == RDiff) |
Dan Gohman | 58a85b9 | 2010-08-13 20:17:14 +0000 | [diff] [blame] | 3532 | return getAddExpr(getUMaxExpr(One, LS), LDiff); |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3533 | } |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3534 | break; |
| 3535 | case ICmpInst::ICMP_EQ: |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3536 | // n == 0 ? 1+x : n+x -> umax(n, 1)+x |
| 3537 | if (LHS->getType() == U->getType() && |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3538 | isa<ConstantInt>(RHS) && |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3539 | cast<ConstantInt>(RHS)->isZero()) { |
| 3540 | const SCEV *One = getConstant(LHS->getType(), 1); |
| 3541 | const SCEV *LS = getSCEV(LHS); |
| 3542 | const SCEV *LA = getSCEV(U->getOperand(1)); |
| 3543 | const SCEV *RA = getSCEV(U->getOperand(2)); |
| 3544 | const SCEV *LDiff = getMinusSCEV(LA, One); |
| 3545 | const SCEV *RDiff = getMinusSCEV(RA, LS); |
| 3546 | if (LDiff == RDiff) |
Dan Gohman | 58a85b9 | 2010-08-13 20:17:14 +0000 | [diff] [blame] | 3547 | return getAddExpr(getUMaxExpr(One, LS), LDiff); |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3548 | } |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3549 | break; |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3550 | default: |
| 3551 | break; |
| 3552 | } |
| 3553 | } |
| 3554 | |
| 3555 | default: // We cannot analyze this expression. |
| 3556 | break; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3559 | return getUnknown(V); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3560 | } |
| 3561 | |
| 3562 | |
| 3563 | |
| 3564 | //===----------------------------------------------------------------------===// |
| 3565 | // Iteration Count Computation Code |
| 3566 | // |
| 3567 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3568 | /// getBackedgeTakenCount - If the specified loop has a predictable |
| 3569 | /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute |
| 3570 | /// object. The backedge-taken count is the number of times the loop header |
| 3571 | /// will be branched to from within the loop. This is one less than the |
| 3572 | /// trip count of the loop, since it doesn't count the first iteration, |
| 3573 | /// when the header is branched to from outside the loop. |
| 3574 | /// |
| 3575 | /// Note that it is not valid to call this method on a loop without a |
| 3576 | /// loop-invariant backedge-taken count (see |
| 3577 | /// hasLoopInvariantBackedgeTakenCount). |
| 3578 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3579 | const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3580 | return getBackedgeTakenInfo(L).Exact; |
| 3581 | } |
| 3582 | |
| 3583 | /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except |
| 3584 | /// return the least SCEV value that is known never to be less than the |
| 3585 | /// actual backedge taken count. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3586 | const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3587 | return getBackedgeTakenInfo(L).Max; |
| 3588 | } |
| 3589 | |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3590 | /// PushLoopPHIs - Push PHI nodes in the header of the given loop |
| 3591 | /// onto the given Worklist. |
| 3592 | static void |
| 3593 | PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) { |
| 3594 | BasicBlock *Header = L->getHeader(); |
| 3595 | |
| 3596 | // Push all Loop-header PHIs onto the Worklist stack. |
| 3597 | for (BasicBlock::iterator I = Header->begin(); |
| 3598 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 3599 | Worklist.push_back(PN); |
| 3600 | } |
| 3601 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3602 | const ScalarEvolution::BackedgeTakenInfo & |
| 3603 | ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3604 | // Initially insert a CouldNotCompute for this loop. If the insertion |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 3605 | // succeeds, proceed to actually compute a backedge-taken count and |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3606 | // update the value. The temporary CouldNotCompute value tells SCEV |
| 3607 | // code elsewhere that it shouldn't attempt to request a new |
| 3608 | // backedge-taken count, which could result in infinite recursion. |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 3609 | std::pair<std::map<const Loop *, BackedgeTakenInfo>::iterator, bool> Pair = |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3610 | BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute())); |
| 3611 | if (Pair.second) { |
Dan Gohman | 93dacad | 2010-01-26 16:46:18 +0000 | [diff] [blame] | 3612 | BackedgeTakenInfo BECount = ComputeBackedgeTakenCount(L); |
| 3613 | if (BECount.Exact != getCouldNotCompute()) { |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 3614 | assert(isLoopInvariant(BECount.Exact, L) && |
| 3615 | isLoopInvariant(BECount.Max, L) && |
Dan Gohman | 93dacad | 2010-01-26 16:46:18 +0000 | [diff] [blame] | 3616 | "Computed backedge-taken count isn't loop invariant for loop!"); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3617 | ++NumTripCountsComputed; |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3618 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3619 | // Update the value in the map. |
Dan Gohman | 93dacad | 2010-01-26 16:46:18 +0000 | [diff] [blame] | 3620 | Pair.first->second = BECount; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3621 | } else { |
Dan Gohman | 93dacad | 2010-01-26 16:46:18 +0000 | [diff] [blame] | 3622 | if (BECount.Max != getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3623 | // Update the value in the map. |
Dan Gohman | 93dacad | 2010-01-26 16:46:18 +0000 | [diff] [blame] | 3624 | Pair.first->second = BECount; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3625 | if (isa<PHINode>(L->getHeader()->begin())) |
| 3626 | // Only count loops that have phi nodes as not being computable. |
| 3627 | ++NumTripCountsNotComputed; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3628 | } |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3629 | |
| 3630 | // Now that we know more about the trip count for this loop, forget any |
| 3631 | // existing SCEV values for PHI nodes in this loop since they are only |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3632 | // conservative estimates made without the benefit of trip count |
Dan Gohman | 4c7279a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 3633 | // information. This is similar to the code in forgetLoop, except that |
| 3634 | // it handles SCEVUnknown PHI nodes specially. |
Dan Gohman | 93dacad | 2010-01-26 16:46:18 +0000 | [diff] [blame] | 3635 | if (BECount.hasAnyInfo()) { |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3636 | SmallVector<Instruction *, 16> Worklist; |
| 3637 | PushLoopPHIs(L, Worklist); |
| 3638 | |
| 3639 | SmallPtrSet<Instruction *, 8> Visited; |
| 3640 | while (!Worklist.empty()) { |
| 3641 | Instruction *I = Worklist.pop_back_val(); |
| 3642 | if (!Visited.insert(I)) continue; |
| 3643 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3644 | ValueExprMapType::iterator It = |
| 3645 | ValueExprMap.find(static_cast<Value *>(I)); |
| 3646 | if (It != ValueExprMap.end()) { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 3647 | const SCEV *Old = It->second; |
| 3648 | |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3649 | // SCEVUnknown for a PHI either means that it has an unrecognized |
| 3650 | // structure, or it's a PHI that's in the progress of being computed |
Dan Gohman | ba70188 | 2009-07-13 22:04:06 +0000 | [diff] [blame] | 3651 | // by createNodeForPHI. In the former case, additional loop trip |
| 3652 | // count information isn't going to change anything. In the later |
| 3653 | // case, createNodeForPHI will perform the necessary updates on its |
| 3654 | // own when it gets to that point. |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 3655 | if (!isa<PHINode>(I) || !isa<SCEVUnknown>(Old)) { |
Dan Gohman | 56a7568 | 2010-11-17 23:28:48 +0000 | [diff] [blame] | 3656 | forgetMemoizedResults(Old); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3657 | ValueExprMap.erase(It); |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 3658 | } |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3659 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3660 | ConstantEvolutionLoopExitValue.erase(PN); |
| 3661 | } |
| 3662 | |
| 3663 | PushDefUseChildren(I, Worklist); |
| 3664 | } |
| 3665 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3666 | } |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3667 | return Pair.first->second; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
Dan Gohman | 4c7279a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 3670 | /// forgetLoop - This method should be called by the client when it has |
| 3671 | /// changed a loop in a way that may effect ScalarEvolution's ability to |
| 3672 | /// compute a trip count, or if the loop is deleted. |
| 3673 | void ScalarEvolution::forgetLoop(const Loop *L) { |
| 3674 | // Drop any stored trip count value. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3675 | BackedgeTakenCounts.erase(L); |
Dan Gohman | fb7d35f | 2009-05-02 17:43:35 +0000 | [diff] [blame] | 3676 | |
Dan Gohman | 4c7279a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 3677 | // Drop information about expressions based on loop-header PHIs. |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3678 | SmallVector<Instruction *, 16> Worklist; |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3679 | PushLoopPHIs(L, Worklist); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3680 | |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3681 | SmallPtrSet<Instruction *, 8> Visited; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3682 | while (!Worklist.empty()) { |
| 3683 | Instruction *I = Worklist.pop_back_val(); |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3684 | if (!Visited.insert(I)) continue; |
| 3685 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3686 | ValueExprMapType::iterator It = ValueExprMap.find(static_cast<Value *>(I)); |
| 3687 | if (It != ValueExprMap.end()) { |
Dan Gohman | 56a7568 | 2010-11-17 23:28:48 +0000 | [diff] [blame] | 3688 | forgetMemoizedResults(It->second); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3689 | ValueExprMap.erase(It); |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3690 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3691 | ConstantEvolutionLoopExitValue.erase(PN); |
| 3692 | } |
| 3693 | |
| 3694 | PushDefUseChildren(I, Worklist); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3695 | } |
Dan Gohman | e60dcb5 | 2010-10-29 20:16:10 +0000 | [diff] [blame] | 3696 | |
| 3697 | // Forget all contained loops too, to avoid dangling entries in the |
| 3698 | // ValuesAtScopes map. |
| 3699 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 3700 | forgetLoop(*I); |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 3701 | } |
| 3702 | |
Eric Christopher | e6cbfa6 | 2010-07-29 01:25:38 +0000 | [diff] [blame] | 3703 | /// forgetValue - This method should be called by the client when it has |
| 3704 | /// changed a value in a way that may effect its value, or which may |
| 3705 | /// disconnect it from a def-use chain linking it to a loop. |
| 3706 | void ScalarEvolution::forgetValue(Value *V) { |
Dale Johannesen | 45a2d7d | 2010-02-19 07:14:22 +0000 | [diff] [blame] | 3707 | Instruction *I = dyn_cast<Instruction>(V); |
| 3708 | if (!I) return; |
| 3709 | |
| 3710 | // Drop information about expressions based on loop-header PHIs. |
| 3711 | SmallVector<Instruction *, 16> Worklist; |
| 3712 | Worklist.push_back(I); |
| 3713 | |
| 3714 | SmallPtrSet<Instruction *, 8> Visited; |
| 3715 | while (!Worklist.empty()) { |
| 3716 | I = Worklist.pop_back_val(); |
| 3717 | if (!Visited.insert(I)) continue; |
| 3718 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3719 | ValueExprMapType::iterator It = ValueExprMap.find(static_cast<Value *>(I)); |
| 3720 | if (It != ValueExprMap.end()) { |
Dan Gohman | 56a7568 | 2010-11-17 23:28:48 +0000 | [diff] [blame] | 3721 | forgetMemoizedResults(It->second); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3722 | ValueExprMap.erase(It); |
Dale Johannesen | 45a2d7d | 2010-02-19 07:14:22 +0000 | [diff] [blame] | 3723 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3724 | ConstantEvolutionLoopExitValue.erase(PN); |
| 3725 | } |
| 3726 | |
| 3727 | PushDefUseChildren(I, Worklist); |
| 3728 | } |
| 3729 | } |
| 3730 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3731 | /// ComputeBackedgeTakenCount - Compute the number of times the backedge |
| 3732 | /// of the specified loop will execute. |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3733 | ScalarEvolution::BackedgeTakenInfo |
| 3734 | ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 3735 | SmallVector<BasicBlock *, 8> ExitingBlocks; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3736 | L->getExitingBlocks(ExitingBlocks); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3737 | |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3738 | // Examine all exits and pick the most conservative values. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3739 | const SCEV *BECount = getCouldNotCompute(); |
| 3740 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3741 | bool CouldNotComputeBECount = false; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3742 | for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { |
| 3743 | BackedgeTakenInfo NewBTI = |
| 3744 | ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3745 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3746 | if (NewBTI.Exact == getCouldNotCompute()) { |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3747 | // We couldn't compute an exact value for this exit, so |
Dan Gohman | d32f5bf | 2009-06-22 21:10:22 +0000 | [diff] [blame] | 3748 | // we won't be able to compute an exact value for the loop. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3749 | CouldNotComputeBECount = true; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3750 | BECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3751 | } else if (!CouldNotComputeBECount) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3752 | if (BECount == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3753 | BECount = NewBTI.Exact; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3754 | else |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3755 | BECount = getUMinFromMismatchedTypes(BECount, NewBTI.Exact); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3756 | } |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3757 | if (MaxBECount == getCouldNotCompute()) |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3758 | MaxBECount = NewBTI.Max; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3759 | else if (NewBTI.Max != getCouldNotCompute()) |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3760 | MaxBECount = getUMinFromMismatchedTypes(MaxBECount, NewBTI.Max); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3761 | } |
| 3762 | |
| 3763 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3764 | } |
| 3765 | |
| 3766 | /// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge |
| 3767 | /// of the specified loop will execute if it exits via the specified block. |
| 3768 | ScalarEvolution::BackedgeTakenInfo |
| 3769 | ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L, |
| 3770 | BasicBlock *ExitingBlock) { |
| 3771 | |
| 3772 | // Okay, we've chosen an exiting block. See what condition causes us to |
| 3773 | // exit at this block. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3774 | // |
| 3775 | // FIXME: we should be able to handle switch instructions (with a single exit) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3776 | BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3777 | if (ExitBr == 0) return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3778 | assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!"); |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3779 | |
Chris Lattner | 8b0e360 | 2007-01-07 02:24:26 +0000 | [diff] [blame] | 3780 | // At this point, we know we have a conditional branch that determines whether |
| 3781 | // the loop is exited. However, we don't know if the branch is executed each |
| 3782 | // time through the loop. If not, then the execution count of the branch will |
| 3783 | // not be equal to the trip count of the loop. |
| 3784 | // |
| 3785 | // Currently we check for this by checking to see if the Exit branch goes to |
| 3786 | // the loop header. If so, we know it will always execute the same number of |
Chris Lattner | 192e403 | 2007-01-14 01:24:47 +0000 | [diff] [blame] | 3787 | // times as the loop. We also handle the case where the exit block *is* the |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3788 | // loop header. This is common for un-rotated loops. |
| 3789 | // |
| 3790 | // If both of those tests fail, walk up the unique predecessor chain to the |
| 3791 | // header, stopping if there is an edge that doesn't exit the loop. If the |
| 3792 | // header is reached, the execution count of the branch will be equal to the |
| 3793 | // trip count of the loop. |
| 3794 | // |
| 3795 | // More extensive analysis could be done to handle more cases here. |
| 3796 | // |
Chris Lattner | 8b0e360 | 2007-01-07 02:24:26 +0000 | [diff] [blame] | 3797 | if (ExitBr->getSuccessor(0) != L->getHeader() && |
Chris Lattner | 192e403 | 2007-01-14 01:24:47 +0000 | [diff] [blame] | 3798 | ExitBr->getSuccessor(1) != L->getHeader() && |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3799 | ExitBr->getParent() != L->getHeader()) { |
| 3800 | // The simple checks failed, try climbing the unique predecessor chain |
| 3801 | // up to the header. |
| 3802 | bool Ok = false; |
| 3803 | for (BasicBlock *BB = ExitBr->getParent(); BB; ) { |
| 3804 | BasicBlock *Pred = BB->getUniquePredecessor(); |
| 3805 | if (!Pred) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3806 | return getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3807 | TerminatorInst *PredTerm = Pred->getTerminator(); |
| 3808 | for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) { |
| 3809 | BasicBlock *PredSucc = PredTerm->getSuccessor(i); |
| 3810 | if (PredSucc == BB) |
| 3811 | continue; |
| 3812 | // If the predecessor has a successor that isn't BB and isn't |
| 3813 | // outside the loop, assume the worst. |
| 3814 | if (L->contains(PredSucc)) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3815 | return getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3816 | } |
| 3817 | if (Pred == L->getHeader()) { |
| 3818 | Ok = true; |
| 3819 | break; |
| 3820 | } |
| 3821 | BB = Pred; |
| 3822 | } |
| 3823 | if (!Ok) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3824 | return getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 3827 | // Proceed to the next level to examine the exit condition expression. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3828 | return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(), |
| 3829 | ExitBr->getSuccessor(0), |
| 3830 | ExitBr->getSuccessor(1)); |
| 3831 | } |
| 3832 | |
| 3833 | /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the |
| 3834 | /// backedge of the specified loop will execute if its exit condition |
| 3835 | /// were a conditional branch of ExitCond, TBB, and FBB. |
| 3836 | ScalarEvolution::BackedgeTakenInfo |
| 3837 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L, |
| 3838 | Value *ExitCond, |
| 3839 | BasicBlock *TBB, |
| 3840 | BasicBlock *FBB) { |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3841 | // Check if the controlling expression for this loop is an And or Or. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3842 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) { |
| 3843 | if (BO->getOpcode() == Instruction::And) { |
| 3844 | // Recurse on the operands of the and. |
| 3845 | BackedgeTakenInfo BTI0 = |
| 3846 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 3847 | BackedgeTakenInfo BTI1 = |
| 3848 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3849 | const SCEV *BECount = getCouldNotCompute(); |
| 3850 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3851 | if (L->contains(TBB)) { |
| 3852 | // Both conditions must be true for the loop to continue executing. |
| 3853 | // Choose the less conservative count. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3854 | if (BTI0.Exact == getCouldNotCompute() || |
| 3855 | BTI1.Exact == getCouldNotCompute()) |
| 3856 | BECount = getCouldNotCompute(); |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3857 | else |
| 3858 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3859 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3860 | MaxBECount = BTI1.Max; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3861 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3862 | MaxBECount = BTI0.Max; |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3863 | else |
| 3864 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3865 | } else { |
Dan Gohman | 4ee8739 | 2010-08-11 00:12:36 +0000 | [diff] [blame] | 3866 | // Both conditions must be true at the same time for the loop to exit. |
| 3867 | // For now, be conservative. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3868 | assert(L->contains(FBB) && "Loop block has no successor in loop!"); |
Dan Gohman | 4ee8739 | 2010-08-11 00:12:36 +0000 | [diff] [blame] | 3869 | if (BTI0.Max == BTI1.Max) |
| 3870 | MaxBECount = BTI0.Max; |
| 3871 | if (BTI0.Exact == BTI1.Exact) |
| 3872 | BECount = BTI0.Exact; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3873 | } |
| 3874 | |
| 3875 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3876 | } |
| 3877 | if (BO->getOpcode() == Instruction::Or) { |
| 3878 | // Recurse on the operands of the or. |
| 3879 | BackedgeTakenInfo BTI0 = |
| 3880 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 3881 | BackedgeTakenInfo BTI1 = |
| 3882 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3883 | const SCEV *BECount = getCouldNotCompute(); |
| 3884 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3885 | if (L->contains(FBB)) { |
| 3886 | // Both conditions must be false for the loop to continue executing. |
| 3887 | // Choose the less conservative count. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3888 | if (BTI0.Exact == getCouldNotCompute() || |
| 3889 | BTI1.Exact == getCouldNotCompute()) |
| 3890 | BECount = getCouldNotCompute(); |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3891 | else |
| 3892 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3893 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3894 | MaxBECount = BTI1.Max; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3895 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3896 | MaxBECount = BTI0.Max; |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3897 | else |
| 3898 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3899 | } else { |
Dan Gohman | 4ee8739 | 2010-08-11 00:12:36 +0000 | [diff] [blame] | 3900 | // Both conditions must be false at the same time for the loop to exit. |
| 3901 | // For now, be conservative. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3902 | assert(L->contains(TBB) && "Loop block has no successor in loop!"); |
Dan Gohman | 4ee8739 | 2010-08-11 00:12:36 +0000 | [diff] [blame] | 3903 | if (BTI0.Max == BTI1.Max) |
| 3904 | MaxBECount = BTI0.Max; |
| 3905 | if (BTI0.Exact == BTI1.Exact) |
| 3906 | BECount = BTI0.Exact; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3907 | } |
| 3908 | |
| 3909 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | // With an icmp, it may be feasible to compute an exact backedge-taken count. |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 3914 | // Proceed to the next level to examine the icmp. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3915 | if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) |
| 3916 | return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3917 | |
Dan Gohman | 00cb5b7 | 2010-02-19 18:12:07 +0000 | [diff] [blame] | 3918 | // Check for a constant condition. These are normally stripped out by |
| 3919 | // SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to |
| 3920 | // preserve the CFG and is temporarily leaving constant conditions |
| 3921 | // in place. |
| 3922 | if (ConstantInt *CI = dyn_cast<ConstantInt>(ExitCond)) { |
| 3923 | if (L->contains(FBB) == !CI->getZExtValue()) |
| 3924 | // The backedge is always taken. |
| 3925 | return getCouldNotCompute(); |
| 3926 | else |
| 3927 | // The backedge is never taken. |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 3928 | return getConstant(CI->getType(), 0); |
Dan Gohman | 00cb5b7 | 2010-02-19 18:12:07 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
Eli Friedman | 361e54d | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3931 | // If it's not an integer or pointer comparison then compute it the hard way. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3932 | return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
| 3933 | } |
| 3934 | |
| 3935 | /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the |
| 3936 | /// backedge of the specified loop will execute if its exit condition |
| 3937 | /// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB. |
| 3938 | ScalarEvolution::BackedgeTakenInfo |
| 3939 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L, |
| 3940 | ICmpInst *ExitCond, |
| 3941 | BasicBlock *TBB, |
| 3942 | BasicBlock *FBB) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3943 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3944 | // If the condition was exit on true, convert the condition to exit on false |
| 3945 | ICmpInst::Predicate Cond; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3946 | if (!L->contains(FBB)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3947 | Cond = ExitCond->getPredicate(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3948 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3949 | Cond = ExitCond->getInversePredicate(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3950 | |
| 3951 | // Handle common loops like: for (X = "string"; *X; ++X) |
| 3952 | if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0))) |
| 3953 | if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) { |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 3954 | BackedgeTakenInfo ItCnt = |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3955 | ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond); |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 3956 | if (ItCnt.hasAnyInfo()) |
| 3957 | return ItCnt; |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3960 | const SCEV *LHS = getSCEV(ExitCond->getOperand(0)); |
| 3961 | const SCEV *RHS = getSCEV(ExitCond->getOperand(1)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3962 | |
| 3963 | // Try to evaluate any dependencies out of the loop. |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3964 | LHS = getSCEVAtScope(LHS, L); |
| 3965 | RHS = getSCEVAtScope(RHS, L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3966 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3967 | // At this point, we would like to compute how many iterations of the |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3968 | // loop the predicate will return true for these inputs. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 3969 | if (isLoopInvariant(LHS, L) && !isLoopInvariant(RHS, L)) { |
Dan Gohman | 70ff4cf | 2008-09-16 18:52:57 +0000 | [diff] [blame] | 3970 | // If there is a loop-invariant, force it into the RHS. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3971 | std::swap(LHS, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3972 | Cond = ICmpInst::getSwappedPredicate(Cond); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 3975 | // Simplify the operands before analyzing them. |
| 3976 | (void)SimplifyICmpOperands(Cond, LHS, RHS); |
| 3977 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3978 | // If we have a comparison of a chrec against a constant, try to use value |
| 3979 | // ranges to answer this query. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3980 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) |
| 3981 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS)) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3982 | if (AddRec->getLoop() == L) { |
Eli Friedman | 361e54d | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3983 | // Form the constant range. |
| 3984 | ConstantRange CompRange( |
| 3985 | ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue())); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3986 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3987 | const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this); |
Eli Friedman | 361e54d | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3988 | if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3989 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3990 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3991 | switch (Cond) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3992 | case ICmpInst::ICMP_NE: { // while (X != Y) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3993 | // Convert to: while (X-Y != 0) |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 3994 | BackedgeTakenInfo BTI = HowFarToZero(getMinusSCEV(LHS, RHS), L); |
| 3995 | if (BTI.hasAnyInfo()) return BTI; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3996 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3997 | } |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 3998 | case ICmpInst::ICMP_EQ: { // while (X == Y) |
| 3999 | // Convert to: while (X-Y == 0) |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 4000 | BackedgeTakenInfo BTI = HowFarToNonZero(getMinusSCEV(LHS, RHS), L); |
| 4001 | if (BTI.hasAnyInfo()) return BTI; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4002 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4003 | } |
| 4004 | case ICmpInst::ICMP_SLT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4005 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true); |
| 4006 | if (BTI.hasAnyInfo()) return BTI; |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4007 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4008 | } |
| 4009 | case ICmpInst::ICMP_SGT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4010 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 4011 | getNotSCEV(RHS), L, true); |
| 4012 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | d6dac0e | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 4013 | break; |
| 4014 | } |
| 4015 | case ICmpInst::ICMP_ULT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4016 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false); |
| 4017 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | d6dac0e | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 4018 | break; |
| 4019 | } |
| 4020 | case ICmpInst::ICMP_UGT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4021 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 4022 | getNotSCEV(RHS), L, false); |
| 4023 | if (BTI.hasAnyInfo()) return BTI; |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4024 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4025 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4026 | default: |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 4027 | #if 0 |
David Greene | 25e0e87 | 2009-12-23 22:18:14 +0000 | [diff] [blame] | 4028 | dbgs() << "ComputeBackedgeTakenCount "; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4029 | if (ExitCond->getOperand(0)->getType()->isUnsigned()) |
David Greene | 25e0e87 | 2009-12-23 22:18:14 +0000 | [diff] [blame] | 4030 | dbgs() << "[unsigned] "; |
| 4031 | dbgs() << *LHS << " " |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4032 | << Instruction::getOpcodeName(Instruction::ICmp) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4033 | << " " << *RHS << "\n"; |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 4034 | #endif |
Chris Lattner | e34c0b4 | 2004-04-03 00:43:03 +0000 | [diff] [blame] | 4035 | break; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4036 | } |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4037 | return |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4038 | ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4041 | static ConstantInt * |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4042 | EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, |
| 4043 | ScalarEvolution &SE) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4044 | const SCEV *InVal = SE.getConstant(C); |
| 4045 | const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4046 | assert(isa<SCEVConstant>(Val) && |
| 4047 | "Evaluation of SCEV at constant didn't fold correctly?"); |
| 4048 | return cast<SCEVConstant>(Val)->getValue(); |
| 4049 | } |
| 4050 | |
| 4051 | /// GetAddressedElementFromGlobal - Given a global variable with an initializer |
| 4052 | /// and a GEP expression (missing the pointer index) indexing into it, return |
| 4053 | /// the addressed element of the initializer or null if the index expression is |
| 4054 | /// invalid. |
| 4055 | static Constant * |
Nick Lewycky | c6501b1 | 2009-11-23 03:26:09 +0000 | [diff] [blame] | 4056 | GetAddressedElementFromGlobal(GlobalVariable *GV, |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4057 | const std::vector<ConstantInt*> &Indices) { |
| 4058 | Constant *Init = GV->getInitializer(); |
| 4059 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4060 | uint64_t Idx = Indices[i]->getZExtValue(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4061 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) { |
| 4062 | assert(Idx < CS->getNumOperands() && "Bad struct index!"); |
| 4063 | Init = cast<Constant>(CS->getOperand(Idx)); |
| 4064 | } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) { |
| 4065 | if (Idx >= CA->getNumOperands()) return 0; // Bogus program |
| 4066 | Init = cast<Constant>(CA->getOperand(Idx)); |
| 4067 | } else if (isa<ConstantAggregateZero>(Init)) { |
| 4068 | if (const StructType *STy = dyn_cast<StructType>(Init->getType())) { |
| 4069 | assert(Idx < STy->getNumElements() && "Bad struct index!"); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 4070 | Init = Constant::getNullValue(STy->getElementType(Idx)); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4071 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) { |
| 4072 | if (Idx >= ATy->getNumElements()) return 0; // Bogus program |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 4073 | Init = Constant::getNullValue(ATy->getElementType()); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4074 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4075 | llvm_unreachable("Unknown constant aggregate type!"); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4076 | } |
| 4077 | return 0; |
| 4078 | } else { |
| 4079 | return 0; // Unknown initializer type |
| 4080 | } |
| 4081 | } |
| 4082 | return Init; |
| 4083 | } |
| 4084 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4085 | /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of |
| 4086 | /// 'icmp op load X, cst', try to see if we can compute the backedge |
| 4087 | /// execution count. |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 4088 | ScalarEvolution::BackedgeTakenInfo |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4089 | ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount( |
| 4090 | LoadInst *LI, |
| 4091 | Constant *RHS, |
| 4092 | const Loop *L, |
| 4093 | ICmpInst::Predicate predicate) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4094 | if (LI->isVolatile()) return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4095 | |
| 4096 | // Check to see if the loaded pointer is a getelementptr of a global. |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 4097 | // TODO: Use SCEV instead of manually grubbing with GEPs. |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4098 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4099 | if (!GEP) return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4100 | |
| 4101 | // Make sure that it is really a constant global we are gepping, with an |
| 4102 | // initializer, and make sure the first IDX is really 0. |
| 4103 | GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); |
Dan Gohman | 8255573 | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 4104 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() || |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4105 | GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) || |
| 4106 | !cast<Constant>(GEP->getOperand(1))->isNullValue()) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4107 | return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4108 | |
| 4109 | // Okay, we allow one non-constant index into the GEP instruction. |
| 4110 | Value *VarIdx = 0; |
| 4111 | std::vector<ConstantInt*> Indexes; |
| 4112 | unsigned VarIdxNum = 0; |
| 4113 | for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) |
| 4114 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 4115 | Indexes.push_back(CI); |
| 4116 | } else if (!isa<ConstantInt>(GEP->getOperand(i))) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4117 | if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's. |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4118 | VarIdx = GEP->getOperand(i); |
| 4119 | VarIdxNum = i-2; |
| 4120 | Indexes.push_back(0); |
| 4121 | } |
| 4122 | |
| 4123 | // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant. |
| 4124 | // Check to see if X is a loop variant variable value now. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4125 | const SCEV *Idx = getSCEV(VarIdx); |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 4126 | Idx = getSCEVAtScope(Idx, L); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4127 | |
| 4128 | // We can only recognize very limited forms of loop index expressions, in |
| 4129 | // particular, only affine AddRec's like {C1,+,C2}. |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4130 | const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx); |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 4131 | if (!IdxExpr || !IdxExpr->isAffine() || isLoopInvariant(IdxExpr, L) || |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4132 | !isa<SCEVConstant>(IdxExpr->getOperand(0)) || |
| 4133 | !isa<SCEVConstant>(IdxExpr->getOperand(1))) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4134 | return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4135 | |
| 4136 | unsigned MaxSteps = MaxBruteForceIterations; |
| 4137 | for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4138 | ConstantInt *ItCst = ConstantInt::get( |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 4139 | cast<IntegerType>(IdxExpr->getType()), IterationNum); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4140 | ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4141 | |
| 4142 | // Form the GEP offset. |
| 4143 | Indexes[VarIdxNum] = Val; |
| 4144 | |
Nick Lewycky | c6501b1 | 2009-11-23 03:26:09 +0000 | [diff] [blame] | 4145 | Constant *Result = GetAddressedElementFromGlobal(GV, Indexes); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4146 | if (Result == 0) break; // Cannot compute! |
| 4147 | |
| 4148 | // Evaluate the condition for this iteration. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4149 | Result = ConstantExpr::getICmp(predicate, Result, RHS); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4150 | if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4151 | if (cast<ConstantInt>(Result)->getValue().isMinValue()) { |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4152 | #if 0 |
David Greene | 25e0e87 | 2009-12-23 22:18:14 +0000 | [diff] [blame] | 4153 | dbgs() << "\n***\n*** Computed loop count " << *ItCst |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 4154 | << "\n*** From global " << *GV << "*** BB: " << *L->getHeader() |
| 4155 | << "***\n"; |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4156 | #endif |
| 4157 | ++NumArrayLenItCounts; |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4158 | return getConstant(ItCst); // Found terminating iteration! |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4159 | } |
| 4160 | } |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4161 | return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4165 | /// CanConstantFold - Return true if we can constant fold an instruction of the |
| 4166 | /// specified type, assuming that all operands were constants. |
| 4167 | static bool CanConstantFold(const Instruction *I) { |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4168 | if (isa<BinaryOperator>(I) || isa<CmpInst>(I) || |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4169 | isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I)) |
| 4170 | return true; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4171 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4172 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 4173 | if (const Function *F = CI->getCalledFunction()) |
Dan Gohman | fa9b80e | 2008-01-31 01:05:10 +0000 | [diff] [blame] | 4174 | return canConstantFoldCallTo(F); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4175 | return false; |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4176 | } |
| 4177 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4178 | /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node |
| 4179 | /// in the loop that V is derived from. We allow arbitrary operations along the |
| 4180 | /// way, but the operands of an operation must either be constants or a value |
| 4181 | /// derived from a constant PHI. If this expression does not fit with these |
| 4182 | /// constraints, return null. |
| 4183 | static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) { |
| 4184 | // If this is not an instruction, or if this is an instruction outside of the |
| 4185 | // loop, it can't be derived from a loop PHI. |
| 4186 | Instruction *I = dyn_cast<Instruction>(V); |
Dan Gohman | 92329c7 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 4187 | if (I == 0 || !L->contains(I)) return 0; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4188 | |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 4189 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4190 | if (L->getHeader() == I->getParent()) |
| 4191 | return PN; |
| 4192 | else |
| 4193 | // We don't currently keep track of the control flow needed to evaluate |
| 4194 | // PHIs, so we cannot handle PHIs inside of loops. |
| 4195 | return 0; |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 4196 | } |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4197 | |
| 4198 | // If we won't be able to constant fold this expression even if the operands |
| 4199 | // are constants, return early. |
| 4200 | if (!CanConstantFold(I)) return 0; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4201 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4202 | // Otherwise, we can evaluate this instruction if all of its operands are |
| 4203 | // constant or derived from a PHI node themselves. |
| 4204 | PHINode *PHI = 0; |
| 4205 | for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op) |
Dan Gohman | 9d4588f | 2010-06-22 13:15:46 +0000 | [diff] [blame] | 4206 | if (!isa<Constant>(I->getOperand(Op))) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4207 | PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L); |
| 4208 | if (P == 0) return 0; // Not evolving from PHI |
| 4209 | if (PHI == 0) |
| 4210 | PHI = P; |
| 4211 | else if (PHI != P) |
| 4212 | return 0; // Evolving from multiple different PHIs. |
| 4213 | } |
| 4214 | |
| 4215 | // This is a expression evolving from a constant PHI! |
| 4216 | return PHI; |
| 4217 | } |
| 4218 | |
| 4219 | /// EvaluateExpression - Given an expression that passes the |
| 4220 | /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node |
| 4221 | /// in the loop has the value PHIVal. If we can't fold this expression for some |
| 4222 | /// reason, return null. |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4223 | static Constant *EvaluateExpression(Value *V, Constant *PHIVal, |
| 4224 | const TargetData *TD) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4225 | if (isa<PHINode>(V)) return PHIVal; |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 4226 | if (Constant *C = dyn_cast<Constant>(V)) return C; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4227 | Instruction *I = cast<Instruction>(V); |
| 4228 | |
Dan Gohman | 9d4588f | 2010-06-22 13:15:46 +0000 | [diff] [blame] | 4229 | std::vector<Constant*> Operands(I->getNumOperands()); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4230 | |
| 4231 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4232 | Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal, TD); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4233 | if (Operands[i] == 0) return 0; |
| 4234 | } |
| 4235 | |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 4236 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
Chris Lattner | 8f73dea | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 4237 | return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0], |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4238 | Operands[1], TD); |
Chris Lattner | 8f73dea | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 4239 | return ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4240 | &Operands[0], Operands.size(), TD); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
| 4243 | /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is |
| 4244 | /// in the header of its containing loop, we know the loop executes a |
| 4245 | /// constant number of times, and the PHI node is just a recurrence |
| 4246 | /// involving constants, fold it. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4247 | Constant * |
| 4248 | ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN, |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 4249 | const APInt &BEs, |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4250 | const Loop *L) { |
Dan Gohman | 8d9c7a6 | 2010-08-16 16:30:01 +0000 | [diff] [blame] | 4251 | std::map<PHINode*, Constant*>::const_iterator I = |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4252 | ConstantEvolutionLoopExitValue.find(PN); |
| 4253 | if (I != ConstantEvolutionLoopExitValue.end()) |
| 4254 | return I->second; |
| 4255 | |
Dan Gohman | e056781 | 2010-04-08 23:03:40 +0000 | [diff] [blame] | 4256 | if (BEs.ugt(MaxBruteForceIterations)) |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4257 | return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it. |
| 4258 | |
| 4259 | Constant *&RetVal = ConstantEvolutionLoopExitValue[PN]; |
| 4260 | |
| 4261 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 4262 | // entry must be a constant (coming in from outside of the loop), and the |
| 4263 | // second must be derived from the same PHI. |
| 4264 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 4265 | Constant *StartCST = |
| 4266 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
| 4267 | if (StartCST == 0) |
| 4268 | return RetVal = 0; // Must be a constant. |
| 4269 | |
| 4270 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
Dan Gohman | 9d4588f | 2010-06-22 13:15:46 +0000 | [diff] [blame] | 4271 | if (getConstantEvolvingPHI(BEValue, L) != PN && |
| 4272 | !isa<Constant>(BEValue)) |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4273 | return RetVal = 0; // Not derived from same PHI. |
| 4274 | |
| 4275 | // Execute the loop symbolically to determine the exit value. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4276 | if (BEs.getActiveBits() >= 32) |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4277 | return RetVal = 0; // More than 2^32-1 iterations?? Not doing it! |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4278 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4279 | unsigned NumIterations = BEs.getZExtValue(); // must be in range |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4280 | unsigned IterationNum = 0; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4281 | for (Constant *PHIVal = StartCST; ; ++IterationNum) { |
| 4282 | if (IterationNum == NumIterations) |
| 4283 | return RetVal = PHIVal; // Got exit value! |
| 4284 | |
| 4285 | // Compute the value of the PHI node for the next iteration. |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4286 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal, TD); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4287 | if (NextPHI == PHIVal) |
| 4288 | return RetVal = NextPHI; // Stopped evolving! |
| 4289 | if (NextPHI == 0) |
| 4290 | return 0; // Couldn't evaluate! |
| 4291 | PHIVal = NextPHI; |
| 4292 | } |
| 4293 | } |
| 4294 | |
Dan Gohman | 07ad19b | 2009-07-27 16:09:48 +0000 | [diff] [blame] | 4295 | /// ComputeBackedgeTakenCountExhaustively - If the loop is known to execute a |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4296 | /// constant number of times (the condition evolves only from constants), |
| 4297 | /// try to evaluate a few iterations of the loop until we get the exit |
| 4298 | /// condition gets a value of ExitWhen (true or false). If we cannot |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4299 | /// evaluate the trip count of the loop, return getCouldNotCompute(). |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4300 | const SCEV * |
| 4301 | ScalarEvolution::ComputeBackedgeTakenCountExhaustively(const Loop *L, |
| 4302 | Value *Cond, |
| 4303 | bool ExitWhen) { |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4304 | PHINode *PN = getConstantEvolvingPHI(Cond, L); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4305 | if (PN == 0) return getCouldNotCompute(); |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4306 | |
Dan Gohman | b92654d | 2010-06-19 14:17:24 +0000 | [diff] [blame] | 4307 | // If the loop is canonicalized, the PHI will have exactly two entries. |
| 4308 | // That's the only form we support here. |
| 4309 | if (PN->getNumIncomingValues() != 2) return getCouldNotCompute(); |
| 4310 | |
| 4311 | // One entry must be a constant (coming in from outside of the loop), and the |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4312 | // second must be derived from the same PHI. |
| 4313 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 4314 | Constant *StartCST = |
| 4315 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4316 | if (StartCST == 0) return getCouldNotCompute(); // Must be a constant. |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4317 | |
| 4318 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
Dan Gohman | 9d4588f | 2010-06-22 13:15:46 +0000 | [diff] [blame] | 4319 | if (getConstantEvolvingPHI(BEValue, L) != PN && |
| 4320 | !isa<Constant>(BEValue)) |
| 4321 | return getCouldNotCompute(); // Not derived from same PHI. |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4322 | |
| 4323 | // Okay, we find a PHI node that defines the trip count of this loop. Execute |
| 4324 | // the loop symbolically to determine when the condition gets a value of |
| 4325 | // "ExitWhen". |
| 4326 | unsigned IterationNum = 0; |
| 4327 | unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis. |
| 4328 | for (Constant *PHIVal = StartCST; |
| 4329 | IterationNum != MaxIterations; ++IterationNum) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4330 | ConstantInt *CondVal = |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4331 | dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal, TD)); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4332 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4333 | // Couldn't symbolically evaluate. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4334 | if (!CondVal) return getCouldNotCompute(); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4335 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4336 | if (CondVal->getValue() == uint64_t(ExitWhen)) { |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4337 | ++NumBruteForceTripCountsComputed; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4338 | return getConstant(Type::getInt32Ty(getContext()), IterationNum); |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4339 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4340 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4341 | // Compute the value of the PHI node for the next iteration. |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4342 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal, TD); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4343 | if (NextPHI == 0 || NextPHI == PHIVal) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4344 | return getCouldNotCompute();// Couldn't evaluate or not making progress... |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4345 | PHIVal = NextPHI; |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4346 | } |
| 4347 | |
| 4348 | // Too many iterations were needed to evaluate. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4349 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4350 | } |
| 4351 | |
Dan Gohman | e7125f4 | 2009-09-03 15:00:26 +0000 | [diff] [blame] | 4352 | /// getSCEVAtScope - Return a SCEV expression for the specified value |
Dan Gohman | 66a7e85 | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 4353 | /// at the specified scope in the program. The L value specifies a loop |
| 4354 | /// nest to evaluate the expression at, where null is the top-level or a |
| 4355 | /// specified loop is immediately inside of the loop. |
| 4356 | /// |
| 4357 | /// This method can be used to compute the exit value for a variable defined |
| 4358 | /// in a loop by querying what the value will hold in the parent loop. |
| 4359 | /// |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 4360 | /// In the case that a relevant loop exit value cannot be computed, the |
| 4361 | /// original value V is returned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4362 | const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 4363 | // Check to see if we've folded this expression at this loop before. |
| 4364 | std::map<const Loop *, const SCEV *> &Values = ValuesAtScopes[V]; |
| 4365 | std::pair<std::map<const Loop *, const SCEV *>::iterator, bool> Pair = |
| 4366 | Values.insert(std::make_pair(L, static_cast<const SCEV *>(0))); |
| 4367 | if (!Pair.second) |
| 4368 | return Pair.first->second ? Pair.first->second : V; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4369 | |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 4370 | // Otherwise compute it. |
| 4371 | const SCEV *C = computeSCEVAtScope(V, L); |
Dan Gohman | a5505cb | 2009-08-31 21:58:28 +0000 | [diff] [blame] | 4372 | ValuesAtScopes[V][L] = C; |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 4373 | return C; |
| 4374 | } |
| 4375 | |
| 4376 | const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4377 | if (isa<SCEVConstant>(V)) return V; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4378 | |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 4379 | // If this instruction is evolved from a constant-evolving PHI, compute the |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4380 | // exit value from the loop without using SCEVs. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4381 | if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4382 | if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4383 | const Loop *LI = (*this->LI)[I->getParent()]; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4384 | if (LI && LI->getParentLoop() == L) // Looking for loop exit value. |
| 4385 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 4386 | if (PN->getParent() == LI->getHeader()) { |
| 4387 | // Okay, there is no closed form solution for the PHI node. Check |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4388 | // to see if the loop that contains it has a known backedge-taken |
| 4389 | // count. If so, we may be able to force computation of the exit |
| 4390 | // value. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4391 | const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4392 | if (const SCEVConstant *BTCC = |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4393 | dyn_cast<SCEVConstant>(BackedgeTakenCount)) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4394 | // Okay, we know how many times the containing loop executes. If |
| 4395 | // this is a constant evolving PHI node, get the final value at |
| 4396 | // the specified iteration number. |
| 4397 | Constant *RV = getConstantEvolutionLoopExitValue(PN, |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4398 | BTCC->getValue()->getValue(), |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4399 | LI); |
Dan Gohman | 0998796 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 4400 | if (RV) return getSCEV(RV); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4401 | } |
| 4402 | } |
| 4403 | |
Reid Spencer | 09906f3 | 2006-12-04 21:33:23 +0000 | [diff] [blame] | 4404 | // Okay, this is an expression that we cannot symbolically evaluate |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4405 | // into a SCEV. Check to see if it's possible to symbolically evaluate |
Reid Spencer | 09906f3 | 2006-12-04 21:33:23 +0000 | [diff] [blame] | 4406 | // the arguments into constants, and if so, try to constant propagate the |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4407 | // result. This is particularly useful for computing loop exit values. |
| 4408 | if (CanConstantFold(I)) { |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4409 | SmallVector<Constant *, 4> Operands; |
| 4410 | bool MadeImprovement = false; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4411 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 4412 | Value *Op = I->getOperand(i); |
| 4413 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| 4414 | Operands.push_back(C); |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4415 | continue; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4416 | } |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4417 | |
| 4418 | // If any of the operands is non-constant and if they are |
| 4419 | // non-integer and non-pointer, don't even try to analyze them |
| 4420 | // with scev techniques. |
| 4421 | if (!isSCEVable(Op->getType())) |
| 4422 | return V; |
| 4423 | |
| 4424 | const SCEV *OrigV = getSCEV(Op); |
| 4425 | const SCEV *OpV = getSCEVAtScope(OrigV, L); |
| 4426 | MadeImprovement |= OrigV != OpV; |
| 4427 | |
| 4428 | Constant *C = 0; |
| 4429 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) |
| 4430 | C = SC->getValue(); |
| 4431 | if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) |
| 4432 | C = dyn_cast<Constant>(SU->getValue()); |
| 4433 | if (!C) return V; |
| 4434 | if (C->getType() != Op->getType()) |
| 4435 | C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 4436 | Op->getType(), |
| 4437 | false), |
| 4438 | C, Op->getType()); |
| 4439 | Operands.push_back(C); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4440 | } |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4441 | |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4442 | // Check to see if getSCEVAtScope actually made an improvement. |
| 4443 | if (MadeImprovement) { |
| 4444 | Constant *C = 0; |
| 4445 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 4446 | C = ConstantFoldCompareInstOperands(CI->getPredicate(), |
| 4447 | Operands[0], Operands[1], TD); |
| 4448 | else |
| 4449 | C = ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
| 4450 | &Operands[0], Operands.size(), TD); |
| 4451 | if (!C) return V; |
Dan Gohman | e177c9a | 2010-02-24 19:31:47 +0000 | [diff] [blame] | 4452 | return getSCEV(C); |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4453 | } |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4454 | } |
| 4455 | } |
| 4456 | |
| 4457 | // This is some other type of SCEVUnknown, just return it. |
| 4458 | return V; |
| 4459 | } |
| 4460 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4461 | if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4462 | // Avoid performing the look-up in the common case where the specified |
| 4463 | // expression has no loop-variant portions. |
| 4464 | for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4465 | const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4466 | if (OpAtScope != Comm->getOperand(i)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4467 | // Okay, at least one of these operands is loop variant but might be |
| 4468 | // foldable. Build a new instance of the folded commutative expression. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4469 | SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(), |
| 4470 | Comm->op_begin()+i); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4471 | NewOps.push_back(OpAtScope); |
| 4472 | |
| 4473 | for (++i; i != e; ++i) { |
| 4474 | OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4475 | NewOps.push_back(OpAtScope); |
| 4476 | } |
| 4477 | if (isa<SCEVAddExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4478 | return getAddExpr(NewOps); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 4479 | if (isa<SCEVMulExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4480 | return getMulExpr(NewOps); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 4481 | if (isa<SCEVSMaxExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4482 | return getSMaxExpr(NewOps); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 4483 | if (isa<SCEVUMaxExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4484 | return getUMaxExpr(NewOps); |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4485 | llvm_unreachable("Unknown commutative SCEV type!"); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4486 | } |
| 4487 | } |
| 4488 | // If we got here, all operands are loop invariant. |
| 4489 | return Comm; |
| 4490 | } |
| 4491 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4492 | if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4493 | const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L); |
| 4494 | const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L); |
Nick Lewycky | 789558d | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 4495 | if (LHS == Div->getLHS() && RHS == Div->getRHS()) |
| 4496 | return Div; // must be loop invariant |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4497 | return getUDivExpr(LHS, RHS); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4498 | } |
| 4499 | |
| 4500 | // If this is a loop recurrence for a loop that does not contain L, then we |
| 4501 | // are dealing with the final value computed by the loop. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4502 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) { |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4503 | // First, attempt to evaluate each operand. |
| 4504 | // Avoid performing the look-up in the common case where the specified |
| 4505 | // expression has no loop-variant portions. |
| 4506 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { |
| 4507 | const SCEV *OpAtScope = getSCEVAtScope(AddRec->getOperand(i), L); |
| 4508 | if (OpAtScope == AddRec->getOperand(i)) |
| 4509 | continue; |
| 4510 | |
| 4511 | // Okay, at least one of these operands is loop variant but might be |
| 4512 | // foldable. Build a new instance of the folded commutative expression. |
| 4513 | SmallVector<const SCEV *, 8> NewOps(AddRec->op_begin(), |
| 4514 | AddRec->op_begin()+i); |
| 4515 | NewOps.push_back(OpAtScope); |
| 4516 | for (++i; i != e; ++i) |
| 4517 | NewOps.push_back(getSCEVAtScope(AddRec->getOperand(i), L)); |
| 4518 | |
| 4519 | AddRec = cast<SCEVAddRecExpr>(getAddRecExpr(NewOps, AddRec->getLoop())); |
| 4520 | break; |
| 4521 | } |
| 4522 | |
| 4523 | // If the scope is outside the addrec's loop, evaluate it by using the |
| 4524 | // loop exit value of the addrec. |
| 4525 | if (!AddRec->getLoop()->contains(L)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4526 | // To evaluate this recurrence, we need to know how many times the AddRec |
| 4527 | // loop iterates. Compute this now. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4528 | const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4529 | if (BackedgeTakenCount == getCouldNotCompute()) return AddRec; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4530 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 4531 | // Then, evaluate the AddRec. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4532 | return AddRec->evaluateAtIteration(BackedgeTakenCount, *this); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4533 | } |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4534 | |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 4535 | return AddRec; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4538 | if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4539 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | eb3948b | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4540 | if (Op == Cast->getOperand()) |
| 4541 | return Cast; // must be loop invariant |
| 4542 | return getZeroExtendExpr(Op, Cast->getType()); |
| 4543 | } |
| 4544 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4545 | if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4546 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | eb3948b | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4547 | if (Op == Cast->getOperand()) |
| 4548 | return Cast; // must be loop invariant |
| 4549 | return getSignExtendExpr(Op, Cast->getType()); |
| 4550 | } |
| 4551 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4552 | if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4553 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | eb3948b | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4554 | if (Op == Cast->getOperand()) |
| 4555 | return Cast; // must be loop invariant |
| 4556 | return getTruncateExpr(Op, Cast->getType()); |
| 4557 | } |
| 4558 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4559 | llvm_unreachable("Unknown SCEV type!"); |
Daniel Dunbar | 8c562e2 | 2009-05-18 16:43:04 +0000 | [diff] [blame] | 4560 | return 0; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4561 | } |
| 4562 | |
Dan Gohman | 66a7e85 | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 4563 | /// getSCEVAtScope - This is a convenience function which does |
| 4564 | /// getSCEVAtScope(getSCEV(V), L). |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4565 | const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4566 | return getSCEVAtScope(getSCEV(V), L); |
| 4567 | } |
| 4568 | |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4569 | /// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the |
| 4570 | /// following equation: |
| 4571 | /// |
| 4572 | /// A * X = B (mod N) |
| 4573 | /// |
| 4574 | /// where N = 2^BW and BW is the common bit width of A and B. The signedness of |
| 4575 | /// A and B isn't important. |
| 4576 | /// |
| 4577 | /// If the equation does not have a solution, SCEVCouldNotCompute is returned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4578 | static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const APInt &B, |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4579 | ScalarEvolution &SE) { |
| 4580 | uint32_t BW = A.getBitWidth(); |
| 4581 | assert(BW == B.getBitWidth() && "Bit widths must be the same."); |
| 4582 | assert(A != 0 && "A must be non-zero."); |
| 4583 | |
| 4584 | // 1. D = gcd(A, N) |
| 4585 | // |
| 4586 | // The gcd of A and N may have only one prime factor: 2. The number of |
| 4587 | // trailing zeros in A is its multiplicity |
| 4588 | uint32_t Mult2 = A.countTrailingZeros(); |
| 4589 | // D = 2^Mult2 |
| 4590 | |
| 4591 | // 2. Check if B is divisible by D. |
| 4592 | // |
| 4593 | // B is divisible by D if and only if the multiplicity of prime factor 2 for B |
| 4594 | // is not less than multiplicity of this prime factor for D. |
| 4595 | if (B.countTrailingZeros() < Mult2) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4596 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4597 | |
| 4598 | // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic |
| 4599 | // modulo (N / D). |
| 4600 | // |
| 4601 | // (N / D) may need BW+1 bits in its representation. Hence, we'll use this |
| 4602 | // bit width during computations. |
| 4603 | APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D |
| 4604 | APInt Mod(BW + 1, 0); |
| 4605 | Mod.set(BW - Mult2); // Mod = N / D |
| 4606 | APInt I = AD.multiplicativeInverse(Mod); |
| 4607 | |
| 4608 | // 4. Compute the minimum unsigned root of the equation: |
| 4609 | // I * (B / D) mod (N / D) |
| 4610 | APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod); |
| 4611 | |
| 4612 | // The result is guaranteed to be less than 2^BW so we may truncate it to BW |
| 4613 | // bits. |
| 4614 | return SE.getConstant(Result.trunc(BW)); |
| 4615 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4616 | |
| 4617 | /// SolveQuadraticEquation - Find the roots of the quadratic equation for the |
| 4618 | /// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which |
| 4619 | /// might be the same) or two SCEVCouldNotCompute objects. |
| 4620 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4621 | static std::pair<const SCEV *,const SCEV *> |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4622 | SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4623 | assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4624 | const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0)); |
| 4625 | const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1)); |
| 4626 | const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2)); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4627 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4628 | // We currently can only solve this if the coefficients are constants. |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4629 | if (!LC || !MC || !NC) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4630 | const SCEV *CNC = SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4631 | return std::make_pair(CNC, CNC); |
| 4632 | } |
| 4633 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4634 | uint32_t BitWidth = LC->getValue()->getValue().getBitWidth(); |
Chris Lattner | fe560b8 | 2007-04-15 19:52:49 +0000 | [diff] [blame] | 4635 | const APInt &L = LC->getValue()->getValue(); |
| 4636 | const APInt &M = MC->getValue()->getValue(); |
| 4637 | const APInt &N = NC->getValue()->getValue(); |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4638 | APInt Two(BitWidth, 2); |
| 4639 | APInt Four(BitWidth, 4); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4640 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4641 | { |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4642 | using namespace APIntOps; |
Zhou Sheng | 414de4d | 2007-04-07 17:48:27 +0000 | [diff] [blame] | 4643 | const APInt& C = L; |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4644 | // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C |
| 4645 | // The B coefficient is M-N/2 |
| 4646 | APInt B(M); |
| 4647 | B -= sdiv(N,Two); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4648 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4649 | // The A coefficient is N/2 |
Zhou Sheng | 414de4d | 2007-04-07 17:48:27 +0000 | [diff] [blame] | 4650 | APInt A(N.sdiv(Two)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4651 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4652 | // Compute the B^2-4ac term. |
| 4653 | APInt SqrtTerm(B); |
| 4654 | SqrtTerm *= B; |
| 4655 | SqrtTerm -= Four * (A * C); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4656 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4657 | // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest |
| 4658 | // integer value or else APInt::sqrt() will assert. |
| 4659 | APInt SqrtVal(SqrtTerm.sqrt()); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4660 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4661 | // Compute the two solutions for the quadratic formula. |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4662 | // The divisions must be performed as signed divisions. |
| 4663 | APInt NegB(-B); |
Reid Spencer | 3e35c8d | 2007-04-16 02:24:41 +0000 | [diff] [blame] | 4664 | APInt TwoA( A << 1 ); |
Nick Lewycky | 8f4d5eb | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 4665 | if (TwoA.isMinValue()) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4666 | const SCEV *CNC = SE.getCouldNotCompute(); |
Nick Lewycky | 8f4d5eb | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 4667 | return std::make_pair(CNC, CNC); |
| 4668 | } |
| 4669 | |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 4670 | LLVMContext &Context = SE.getContext(); |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4671 | |
| 4672 | ConstantInt *Solution1 = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4673 | ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA)); |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4674 | ConstantInt *Solution2 = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4675 | ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA)); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4676 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4677 | return std::make_pair(SE.getConstant(Solution1), |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4678 | SE.getConstant(Solution2)); |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4679 | } // end APIntOps namespace |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | /// HowFarToZero - Return the number of times a backedge comparing the specified |
Dan Gohman | 86fbf2f | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4683 | /// value to zero will execute. If not computable, return CouldNotCompute. |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 4684 | ScalarEvolution::BackedgeTakenInfo |
| 4685 | ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4686 | // If the value is a constant |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4687 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4688 | // If the value is already zero, the branch will execute zero times. |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 4689 | if (C->getValue()->isZero()) return C; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4690 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4691 | } |
| 4692 | |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4693 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4694 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4695 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4696 | |
| 4697 | if (AddRec->isAffine()) { |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4698 | // If this is an affine expression, the execution count of this branch is |
| 4699 | // the minimum unsigned root of the following equation: |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4700 | // |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4701 | // Start + Step*N = 0 (mod 2^BW) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4702 | // |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4703 | // equivalent to: |
| 4704 | // |
| 4705 | // Step*N = -Start (mod 2^BW) |
| 4706 | // |
| 4707 | // where BW is the common bit width of Start and Step. |
| 4708 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4709 | // Get the initial value for the loop. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4710 | const SCEV *Start = getSCEVAtScope(AddRec->getStart(), |
| 4711 | L->getParentLoop()); |
| 4712 | const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), |
| 4713 | L->getParentLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4714 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4715 | if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) { |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4716 | // For now we handle only constant steps. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4717 | |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4718 | // First, handle unitary steps. |
| 4719 | if (StepC->getValue()->equalsInt(1)) // 1*N = -Start (mod 2^BW), so: |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 4720 | return getNegativeSCEV(Start); // N = -Start (as unsigned) |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4721 | if (StepC->getValue()->isAllOnesValue()) // -1*N = -Start (mod 2^BW), so: |
| 4722 | return Start; // N = Start (as unsigned) |
| 4723 | |
| 4724 | // Then, try to solve the above equation provided that Start is constant. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4725 | if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4726 | return SolveLinEquationWithOverflow(StepC->getValue()->getValue(), |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4727 | -StartC->getValue()->getValue(), |
| 4728 | *this); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4729 | } |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4730 | } else if (AddRec->isQuadratic() && AddRec->getType()->isIntegerTy()) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4731 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of |
| 4732 | // the quadratic equation to solve it. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4733 | std::pair<const SCEV *,const SCEV *> Roots = SolveQuadraticEquation(AddRec, |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4734 | *this); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4735 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 4736 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4737 | if (R1) { |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 4738 | #if 0 |
David Greene | 25e0e87 | 2009-12-23 22:18:14 +0000 | [diff] [blame] | 4739 | dbgs() << "HFTZ: " << *V << " - sol#1: " << *R1 |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 4740 | << " sol#2: " << *R2 << "\n"; |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 4741 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4742 | // Pick the smallest positive root value. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4743 | if (ConstantInt *CB = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 4744 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4745 | R1->getValue(), R2->getValue()))) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4746 | if (CB->getZExtValue() == false) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4747 | std::swap(R1, R2); // R1 is the minimum root now. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4748 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4749 | // We can only use this value if the chrec ends up with an exact zero |
| 4750 | // value at this index. When solving for "X*X != 5", for example, we |
| 4751 | // should not accept a root of 2. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4752 | const SCEV *Val = AddRec->evaluateAtIteration(R1, *this); |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 4753 | if (Val->isZero()) |
| 4754 | return R1; // We found a quadratic root! |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4755 | } |
| 4756 | } |
| 4757 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4758 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4759 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4760 | } |
| 4761 | |
| 4762 | /// HowFarToNonZero - Return the number of times a backedge checking the |
| 4763 | /// specified value for nonzero will execute. If not computable, return |
Dan Gohman | 86fbf2f | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4764 | /// CouldNotCompute |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 4765 | ScalarEvolution::BackedgeTakenInfo |
| 4766 | ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4767 | // Loops that look like: while (X == 0) are very strange indeed. We don't |
| 4768 | // handle them yet except for the trivial case. This could be expanded in the |
| 4769 | // future as needed. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4770 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4771 | // If the value is a constant, check to see if it is known to be non-zero |
| 4772 | // already. If so, the backedge will execute zero times. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4773 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Nick Lewycky | 39442af | 2008-02-21 09:14:53 +0000 | [diff] [blame] | 4774 | if (!C->getValue()->isNullValue()) |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 4775 | return getConstant(C->getType(), 0); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4776 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4777 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4778 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4779 | // We could implement others, but I really doubt anyone writes loops like |
| 4780 | // this, and if they did, they would already be constant folded. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4781 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4784 | /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB |
| 4785 | /// (which may not be an immediate predecessor) which has exactly one |
| 4786 | /// successor from which BB is reachable, or null if no such block is |
| 4787 | /// found. |
| 4788 | /// |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 4789 | std::pair<BasicBlock *, BasicBlock *> |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4790 | ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) { |
Dan Gohman | 3d739fe | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 4791 | // If the block has a unique predecessor, then there is no path from the |
| 4792 | // predecessor to the block that does not go through the direct edge |
| 4793 | // from the predecessor to the block. |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4794 | if (BasicBlock *Pred = BB->getSinglePredecessor()) |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 4795 | return std::make_pair(Pred, BB); |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4796 | |
| 4797 | // A loop's header is defined to be a block that dominates the loop. |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4798 | // If the header has a unique predecessor outside the loop, it must be |
| 4799 | // a block that has exactly one successor that can reach the loop. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4800 | if (Loop *L = LI->getLoopFor(BB)) |
Dan Gohman | 605c14f | 2010-06-22 23:43:28 +0000 | [diff] [blame] | 4801 | return std::make_pair(L->getLoopPredecessor(), L->getHeader()); |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4802 | |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 4803 | return std::pair<BasicBlock *, BasicBlock *>(); |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4804 | } |
| 4805 | |
Dan Gohman | 763bad1 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4806 | /// HasSameValue - SCEV structural equivalence is usually sufficient for |
| 4807 | /// testing whether two expressions are equal, however for the purposes of |
| 4808 | /// looking for a condition guarding a loop, it can be useful to be a little |
| 4809 | /// more general, since a front-end may have replicated the controlling |
| 4810 | /// expression. |
| 4811 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4812 | static bool HasSameValue(const SCEV *A, const SCEV *B) { |
Dan Gohman | 763bad1 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4813 | // Quick check to see if they are the same SCEV. |
| 4814 | if (A == B) return true; |
| 4815 | |
| 4816 | // Otherwise, if they're both SCEVUnknown, it's possible that they hold |
| 4817 | // two different instructions with the same value. Check for this case. |
| 4818 | if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A)) |
| 4819 | if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B)) |
| 4820 | if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue())) |
| 4821 | if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue())) |
Dan Gohman | 041de42 | 2009-08-25 17:56:57 +0000 | [diff] [blame] | 4822 | if (AI->isIdenticalTo(BI) && !AI->mayReadFromMemory()) |
Dan Gohman | 763bad1 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4823 | return true; |
| 4824 | |
| 4825 | // Otherwise assume they may have a different value. |
| 4826 | return false; |
| 4827 | } |
| 4828 | |
Dan Gohman | e979650 | 2010-04-24 01:28:42 +0000 | [diff] [blame] | 4829 | /// SimplifyICmpOperands - Simplify LHS and RHS in a comparison with |
| 4830 | /// predicate Pred. Return true iff any changes were made. |
| 4831 | /// |
| 4832 | bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred, |
| 4833 | const SCEV *&LHS, const SCEV *&RHS) { |
| 4834 | bool Changed = false; |
| 4835 | |
| 4836 | // Canonicalize a constant to the right side. |
| 4837 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { |
| 4838 | // Check for both operands constant. |
| 4839 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { |
| 4840 | if (ConstantExpr::getICmp(Pred, |
| 4841 | LHSC->getValue(), |
| 4842 | RHSC->getValue())->isNullValue()) |
| 4843 | goto trivially_false; |
| 4844 | else |
| 4845 | goto trivially_true; |
| 4846 | } |
| 4847 | // Otherwise swap the operands to put the constant on the right. |
| 4848 | std::swap(LHS, RHS); |
| 4849 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4850 | Changed = true; |
| 4851 | } |
| 4852 | |
| 4853 | // If we're comparing an addrec with a value which is loop-invariant in the |
Dan Gohman | 3abb69c | 2010-05-03 17:00:11 +0000 | [diff] [blame] | 4854 | // addrec's loop, put the addrec on the left. Also make a dominance check, |
| 4855 | // as both operands could be addrecs loop-invariant in each other's loop. |
| 4856 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(RHS)) { |
| 4857 | const Loop *L = AR->getLoop(); |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 4858 | if (isLoopInvariant(LHS, L) && properlyDominates(LHS, L->getHeader())) { |
Dan Gohman | e979650 | 2010-04-24 01:28:42 +0000 | [diff] [blame] | 4859 | std::swap(LHS, RHS); |
| 4860 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4861 | Changed = true; |
| 4862 | } |
Dan Gohman | 3abb69c | 2010-05-03 17:00:11 +0000 | [diff] [blame] | 4863 | } |
Dan Gohman | e979650 | 2010-04-24 01:28:42 +0000 | [diff] [blame] | 4864 | |
| 4865 | // If there's a constant operand, canonicalize comparisons with boundary |
| 4866 | // cases, and canonicalize *-or-equal comparisons to regular comparisons. |
| 4867 | if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) { |
| 4868 | const APInt &RA = RC->getValue()->getValue(); |
| 4869 | switch (Pred) { |
| 4870 | default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
| 4871 | case ICmpInst::ICMP_EQ: |
| 4872 | case ICmpInst::ICMP_NE: |
| 4873 | break; |
| 4874 | case ICmpInst::ICMP_UGE: |
| 4875 | if ((RA - 1).isMinValue()) { |
| 4876 | Pred = ICmpInst::ICMP_NE; |
| 4877 | RHS = getConstant(RA - 1); |
| 4878 | Changed = true; |
| 4879 | break; |
| 4880 | } |
| 4881 | if (RA.isMaxValue()) { |
| 4882 | Pred = ICmpInst::ICMP_EQ; |
| 4883 | Changed = true; |
| 4884 | break; |
| 4885 | } |
| 4886 | if (RA.isMinValue()) goto trivially_true; |
| 4887 | |
| 4888 | Pred = ICmpInst::ICMP_UGT; |
| 4889 | RHS = getConstant(RA - 1); |
| 4890 | Changed = true; |
| 4891 | break; |
| 4892 | case ICmpInst::ICMP_ULE: |
| 4893 | if ((RA + 1).isMaxValue()) { |
| 4894 | Pred = ICmpInst::ICMP_NE; |
| 4895 | RHS = getConstant(RA + 1); |
| 4896 | Changed = true; |
| 4897 | break; |
| 4898 | } |
| 4899 | if (RA.isMinValue()) { |
| 4900 | Pred = ICmpInst::ICMP_EQ; |
| 4901 | Changed = true; |
| 4902 | break; |
| 4903 | } |
| 4904 | if (RA.isMaxValue()) goto trivially_true; |
| 4905 | |
| 4906 | Pred = ICmpInst::ICMP_ULT; |
| 4907 | RHS = getConstant(RA + 1); |
| 4908 | Changed = true; |
| 4909 | break; |
| 4910 | case ICmpInst::ICMP_SGE: |
| 4911 | if ((RA - 1).isMinSignedValue()) { |
| 4912 | Pred = ICmpInst::ICMP_NE; |
| 4913 | RHS = getConstant(RA - 1); |
| 4914 | Changed = true; |
| 4915 | break; |
| 4916 | } |
| 4917 | if (RA.isMaxSignedValue()) { |
| 4918 | Pred = ICmpInst::ICMP_EQ; |
| 4919 | Changed = true; |
| 4920 | break; |
| 4921 | } |
| 4922 | if (RA.isMinSignedValue()) goto trivially_true; |
| 4923 | |
| 4924 | Pred = ICmpInst::ICMP_SGT; |
| 4925 | RHS = getConstant(RA - 1); |
| 4926 | Changed = true; |
| 4927 | break; |
| 4928 | case ICmpInst::ICMP_SLE: |
| 4929 | if ((RA + 1).isMaxSignedValue()) { |
| 4930 | Pred = ICmpInst::ICMP_NE; |
| 4931 | RHS = getConstant(RA + 1); |
| 4932 | Changed = true; |
| 4933 | break; |
| 4934 | } |
| 4935 | if (RA.isMinSignedValue()) { |
| 4936 | Pred = ICmpInst::ICMP_EQ; |
| 4937 | Changed = true; |
| 4938 | break; |
| 4939 | } |
| 4940 | if (RA.isMaxSignedValue()) goto trivially_true; |
| 4941 | |
| 4942 | Pred = ICmpInst::ICMP_SLT; |
| 4943 | RHS = getConstant(RA + 1); |
| 4944 | Changed = true; |
| 4945 | break; |
| 4946 | case ICmpInst::ICMP_UGT: |
| 4947 | if (RA.isMinValue()) { |
| 4948 | Pred = ICmpInst::ICMP_NE; |
| 4949 | Changed = true; |
| 4950 | break; |
| 4951 | } |
| 4952 | if ((RA + 1).isMaxValue()) { |
| 4953 | Pred = ICmpInst::ICMP_EQ; |
| 4954 | RHS = getConstant(RA + 1); |
| 4955 | Changed = true; |
| 4956 | break; |
| 4957 | } |
| 4958 | if (RA.isMaxValue()) goto trivially_false; |
| 4959 | break; |
| 4960 | case ICmpInst::ICMP_ULT: |
| 4961 | if (RA.isMaxValue()) { |
| 4962 | Pred = ICmpInst::ICMP_NE; |
| 4963 | Changed = true; |
| 4964 | break; |
| 4965 | } |
| 4966 | if ((RA - 1).isMinValue()) { |
| 4967 | Pred = ICmpInst::ICMP_EQ; |
| 4968 | RHS = getConstant(RA - 1); |
| 4969 | Changed = true; |
| 4970 | break; |
| 4971 | } |
| 4972 | if (RA.isMinValue()) goto trivially_false; |
| 4973 | break; |
| 4974 | case ICmpInst::ICMP_SGT: |
| 4975 | if (RA.isMinSignedValue()) { |
| 4976 | Pred = ICmpInst::ICMP_NE; |
| 4977 | Changed = true; |
| 4978 | break; |
| 4979 | } |
| 4980 | if ((RA + 1).isMaxSignedValue()) { |
| 4981 | Pred = ICmpInst::ICMP_EQ; |
| 4982 | RHS = getConstant(RA + 1); |
| 4983 | Changed = true; |
| 4984 | break; |
| 4985 | } |
| 4986 | if (RA.isMaxSignedValue()) goto trivially_false; |
| 4987 | break; |
| 4988 | case ICmpInst::ICMP_SLT: |
| 4989 | if (RA.isMaxSignedValue()) { |
| 4990 | Pred = ICmpInst::ICMP_NE; |
| 4991 | Changed = true; |
| 4992 | break; |
| 4993 | } |
| 4994 | if ((RA - 1).isMinSignedValue()) { |
| 4995 | Pred = ICmpInst::ICMP_EQ; |
| 4996 | RHS = getConstant(RA - 1); |
| 4997 | Changed = true; |
| 4998 | break; |
| 4999 | } |
| 5000 | if (RA.isMinSignedValue()) goto trivially_false; |
| 5001 | break; |
| 5002 | } |
| 5003 | } |
| 5004 | |
| 5005 | // Check for obvious equality. |
| 5006 | if (HasSameValue(LHS, RHS)) { |
| 5007 | if (ICmpInst::isTrueWhenEqual(Pred)) |
| 5008 | goto trivially_true; |
| 5009 | if (ICmpInst::isFalseWhenEqual(Pred)) |
| 5010 | goto trivially_false; |
| 5011 | } |
| 5012 | |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5013 | // If possible, canonicalize GE/LE comparisons to GT/LT comparisons, by |
| 5014 | // adding or subtracting 1 from one of the operands. |
| 5015 | switch (Pred) { |
| 5016 | case ICmpInst::ICMP_SLE: |
| 5017 | if (!getSignedRange(RHS).getSignedMax().isMaxSignedValue()) { |
| 5018 | RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS, |
| 5019 | /*HasNUW=*/false, /*HasNSW=*/true); |
| 5020 | Pred = ICmpInst::ICMP_SLT; |
| 5021 | Changed = true; |
| 5022 | } else if (!getSignedRange(LHS).getSignedMin().isMinSignedValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5023 | LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS, |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5024 | /*HasNUW=*/false, /*HasNSW=*/true); |
| 5025 | Pred = ICmpInst::ICMP_SLT; |
| 5026 | Changed = true; |
| 5027 | } |
| 5028 | break; |
| 5029 | case ICmpInst::ICMP_SGE: |
| 5030 | if (!getSignedRange(RHS).getSignedMin().isMinSignedValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5031 | RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS, |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5032 | /*HasNUW=*/false, /*HasNSW=*/true); |
| 5033 | Pred = ICmpInst::ICMP_SGT; |
| 5034 | Changed = true; |
| 5035 | } else if (!getSignedRange(LHS).getSignedMax().isMaxSignedValue()) { |
| 5036 | LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS, |
| 5037 | /*HasNUW=*/false, /*HasNSW=*/true); |
| 5038 | Pred = ICmpInst::ICMP_SGT; |
| 5039 | Changed = true; |
| 5040 | } |
| 5041 | break; |
| 5042 | case ICmpInst::ICMP_ULE: |
| 5043 | if (!getUnsignedRange(RHS).getUnsignedMax().isMaxValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5044 | RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS, |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5045 | /*HasNUW=*/true, /*HasNSW=*/false); |
| 5046 | Pred = ICmpInst::ICMP_ULT; |
| 5047 | Changed = true; |
| 5048 | } else if (!getUnsignedRange(LHS).getUnsignedMin().isMinValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5049 | LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS, |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5050 | /*HasNUW=*/true, /*HasNSW=*/false); |
| 5051 | Pred = ICmpInst::ICMP_ULT; |
| 5052 | Changed = true; |
| 5053 | } |
| 5054 | break; |
| 5055 | case ICmpInst::ICMP_UGE: |
| 5056 | if (!getUnsignedRange(RHS).getUnsignedMin().isMinValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5057 | RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS, |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5058 | /*HasNUW=*/true, /*HasNSW=*/false); |
| 5059 | Pred = ICmpInst::ICMP_UGT; |
| 5060 | Changed = true; |
| 5061 | } else if (!getUnsignedRange(LHS).getUnsignedMax().isMaxValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5062 | LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS, |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5063 | /*HasNUW=*/true, /*HasNSW=*/false); |
| 5064 | Pred = ICmpInst::ICMP_UGT; |
| 5065 | Changed = true; |
| 5066 | } |
| 5067 | break; |
| 5068 | default: |
| 5069 | break; |
| 5070 | } |
| 5071 | |
Dan Gohman | e979650 | 2010-04-24 01:28:42 +0000 | [diff] [blame] | 5072 | // TODO: More simplifications are possible here. |
| 5073 | |
| 5074 | return Changed; |
| 5075 | |
| 5076 | trivially_true: |
| 5077 | // Return 0 == 0. |
| 5078 | LHS = RHS = getConstant(Type::getInt1Ty(getContext()), 0); |
| 5079 | Pred = ICmpInst::ICMP_EQ; |
| 5080 | return true; |
| 5081 | |
| 5082 | trivially_false: |
| 5083 | // Return 0 != 0. |
| 5084 | LHS = RHS = getConstant(Type::getInt1Ty(getContext()), 0); |
| 5085 | Pred = ICmpInst::ICMP_NE; |
| 5086 | return true; |
| 5087 | } |
| 5088 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5089 | bool ScalarEvolution::isKnownNegative(const SCEV *S) { |
| 5090 | return getSignedRange(S).getSignedMax().isNegative(); |
| 5091 | } |
| 5092 | |
| 5093 | bool ScalarEvolution::isKnownPositive(const SCEV *S) { |
| 5094 | return getSignedRange(S).getSignedMin().isStrictlyPositive(); |
| 5095 | } |
| 5096 | |
| 5097 | bool ScalarEvolution::isKnownNonNegative(const SCEV *S) { |
| 5098 | return !getSignedRange(S).getSignedMin().isNegative(); |
| 5099 | } |
| 5100 | |
| 5101 | bool ScalarEvolution::isKnownNonPositive(const SCEV *S) { |
| 5102 | return !getSignedRange(S).getSignedMax().isStrictlyPositive(); |
| 5103 | } |
| 5104 | |
| 5105 | bool ScalarEvolution::isKnownNonZero(const SCEV *S) { |
| 5106 | return isKnownNegative(S) || isKnownPositive(S); |
| 5107 | } |
| 5108 | |
| 5109 | bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred, |
| 5110 | const SCEV *LHS, const SCEV *RHS) { |
Dan Gohman | d19bba6 | 2010-04-24 01:38:36 +0000 | [diff] [blame] | 5111 | // Canonicalize the inputs first. |
| 5112 | (void)SimplifyICmpOperands(Pred, LHS, RHS); |
| 5113 | |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5114 | // If LHS or RHS is an addrec, check to see if the condition is true in |
| 5115 | // every iteration of the loop. |
| 5116 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) |
| 5117 | if (isLoopEntryGuardedByCond( |
| 5118 | AR->getLoop(), Pred, AR->getStart(), RHS) && |
| 5119 | isLoopBackedgeGuardedByCond( |
Dan Gohman | acd8cab | 2010-05-04 01:12:27 +0000 | [diff] [blame] | 5120 | AR->getLoop(), Pred, AR->getPostIncExpr(*this), RHS)) |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5121 | return true; |
| 5122 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(RHS)) |
| 5123 | if (isLoopEntryGuardedByCond( |
| 5124 | AR->getLoop(), Pred, LHS, AR->getStart()) && |
| 5125 | isLoopBackedgeGuardedByCond( |
Dan Gohman | acd8cab | 2010-05-04 01:12:27 +0000 | [diff] [blame] | 5126 | AR->getLoop(), Pred, LHS, AR->getPostIncExpr(*this))) |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5127 | return true; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5128 | |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5129 | // Otherwise see what can be done with known constant ranges. |
| 5130 | return isKnownPredicateWithRanges(Pred, LHS, RHS); |
| 5131 | } |
| 5132 | |
| 5133 | bool |
| 5134 | ScalarEvolution::isKnownPredicateWithRanges(ICmpInst::Predicate Pred, |
| 5135 | const SCEV *LHS, const SCEV *RHS) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5136 | if (HasSameValue(LHS, RHS)) |
| 5137 | return ICmpInst::isTrueWhenEqual(Pred); |
| 5138 | |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5139 | // This code is split out from isKnownPredicate because it is called from |
| 5140 | // within isLoopEntryGuardedByCond. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5141 | switch (Pred) { |
| 5142 | default: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5143 | llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5144 | break; |
| 5145 | case ICmpInst::ICMP_SGT: |
| 5146 | Pred = ICmpInst::ICMP_SLT; |
| 5147 | std::swap(LHS, RHS); |
| 5148 | case ICmpInst::ICMP_SLT: { |
| 5149 | ConstantRange LHSRange = getSignedRange(LHS); |
| 5150 | ConstantRange RHSRange = getSignedRange(RHS); |
| 5151 | if (LHSRange.getSignedMax().slt(RHSRange.getSignedMin())) |
| 5152 | return true; |
| 5153 | if (LHSRange.getSignedMin().sge(RHSRange.getSignedMax())) |
| 5154 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5155 | break; |
| 5156 | } |
| 5157 | case ICmpInst::ICMP_SGE: |
| 5158 | Pred = ICmpInst::ICMP_SLE; |
| 5159 | std::swap(LHS, RHS); |
| 5160 | case ICmpInst::ICMP_SLE: { |
| 5161 | ConstantRange LHSRange = getSignedRange(LHS); |
| 5162 | ConstantRange RHSRange = getSignedRange(RHS); |
| 5163 | if (LHSRange.getSignedMax().sle(RHSRange.getSignedMin())) |
| 5164 | return true; |
| 5165 | if (LHSRange.getSignedMin().sgt(RHSRange.getSignedMax())) |
| 5166 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5167 | break; |
| 5168 | } |
| 5169 | case ICmpInst::ICMP_UGT: |
| 5170 | Pred = ICmpInst::ICMP_ULT; |
| 5171 | std::swap(LHS, RHS); |
| 5172 | case ICmpInst::ICMP_ULT: { |
| 5173 | ConstantRange LHSRange = getUnsignedRange(LHS); |
| 5174 | ConstantRange RHSRange = getUnsignedRange(RHS); |
| 5175 | if (LHSRange.getUnsignedMax().ult(RHSRange.getUnsignedMin())) |
| 5176 | return true; |
| 5177 | if (LHSRange.getUnsignedMin().uge(RHSRange.getUnsignedMax())) |
| 5178 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5179 | break; |
| 5180 | } |
| 5181 | case ICmpInst::ICMP_UGE: |
| 5182 | Pred = ICmpInst::ICMP_ULE; |
| 5183 | std::swap(LHS, RHS); |
| 5184 | case ICmpInst::ICMP_ULE: { |
| 5185 | ConstantRange LHSRange = getUnsignedRange(LHS); |
| 5186 | ConstantRange RHSRange = getUnsignedRange(RHS); |
| 5187 | if (LHSRange.getUnsignedMax().ule(RHSRange.getUnsignedMin())) |
| 5188 | return true; |
| 5189 | if (LHSRange.getUnsignedMin().ugt(RHSRange.getUnsignedMax())) |
| 5190 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5191 | break; |
| 5192 | } |
| 5193 | case ICmpInst::ICMP_NE: { |
| 5194 | if (getUnsignedRange(LHS).intersectWith(getUnsignedRange(RHS)).isEmptySet()) |
| 5195 | return true; |
| 5196 | if (getSignedRange(LHS).intersectWith(getSignedRange(RHS)).isEmptySet()) |
| 5197 | return true; |
| 5198 | |
| 5199 | const SCEV *Diff = getMinusSCEV(LHS, RHS); |
| 5200 | if (isKnownNonZero(Diff)) |
| 5201 | return true; |
| 5202 | break; |
| 5203 | } |
| 5204 | case ICmpInst::ICMP_EQ: |
Dan Gohman | f117ed4 | 2009-07-20 23:54:43 +0000 | [diff] [blame] | 5205 | // The check at the top of the function catches the case where |
| 5206 | // the values are known to be equal. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5207 | break; |
| 5208 | } |
| 5209 | return false; |
| 5210 | } |
| 5211 | |
| 5212 | /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is |
| 5213 | /// protected by a conditional between LHS and RHS. This is used to |
| 5214 | /// to eliminate casts. |
| 5215 | bool |
| 5216 | ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L, |
| 5217 | ICmpInst::Predicate Pred, |
| 5218 | const SCEV *LHS, const SCEV *RHS) { |
| 5219 | // Interpret a null as meaning no loop, where there is obviously no guard |
| 5220 | // (interprocedural conditions notwithstanding). |
| 5221 | if (!L) return true; |
| 5222 | |
| 5223 | BasicBlock *Latch = L->getLoopLatch(); |
| 5224 | if (!Latch) |
| 5225 | return false; |
| 5226 | |
| 5227 | BranchInst *LoopContinuePredicate = |
| 5228 | dyn_cast<BranchInst>(Latch->getTerminator()); |
| 5229 | if (!LoopContinuePredicate || |
| 5230 | LoopContinuePredicate->isUnconditional()) |
| 5231 | return false; |
| 5232 | |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5233 | return isImpliedCond(Pred, LHS, RHS, |
| 5234 | LoopContinuePredicate->getCondition(), |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5235 | LoopContinuePredicate->getSuccessor(0) != L->getHeader()); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5236 | } |
| 5237 | |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 5238 | /// isLoopEntryGuardedByCond - Test whether entry to the loop is protected |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5239 | /// by a conditional between LHS and RHS. This is used to help avoid max |
| 5240 | /// expressions in loop trip counts, and to eliminate casts. |
| 5241 | bool |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 5242 | ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L, |
| 5243 | ICmpInst::Predicate Pred, |
| 5244 | const SCEV *LHS, const SCEV *RHS) { |
Dan Gohman | 8ea9452 | 2009-05-18 16:03:58 +0000 | [diff] [blame] | 5245 | // Interpret a null as meaning no loop, where there is obviously no guard |
| 5246 | // (interprocedural conditions notwithstanding). |
| 5247 | if (!L) return false; |
| 5248 | |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 5249 | // Starting at the loop predecessor, climb up the predecessor chain, as long |
| 5250 | // as there are predecessors that can be found that have unique successors |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 5251 | // leading to the original header. |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 5252 | for (std::pair<BasicBlock *, BasicBlock *> |
Dan Gohman | 605c14f | 2010-06-22 23:43:28 +0000 | [diff] [blame] | 5253 | Pair(L->getLoopPredecessor(), L->getHeader()); |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 5254 | Pair.first; |
| 5255 | Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) { |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 5256 | |
| 5257 | BranchInst *LoopEntryPredicate = |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 5258 | dyn_cast<BranchInst>(Pair.first->getTerminator()); |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 5259 | if (!LoopEntryPredicate || |
| 5260 | LoopEntryPredicate->isUnconditional()) |
| 5261 | continue; |
| 5262 | |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5263 | if (isImpliedCond(Pred, LHS, RHS, |
| 5264 | LoopEntryPredicate->getCondition(), |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 5265 | LoopEntryPredicate->getSuccessor(0) != Pair.second)) |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 5266 | return true; |
Nick Lewycky | 59cff12 | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 5267 | } |
| 5268 | |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 5269 | return false; |
Nick Lewycky | 59cff12 | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 5270 | } |
| 5271 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5272 | /// isImpliedCond - Test whether the condition described by Pred, LHS, |
| 5273 | /// and RHS is true whenever the given Cond value evaluates to true. |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5274 | bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5275 | const SCEV *LHS, const SCEV *RHS, |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5276 | Value *FoundCondValue, |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5277 | bool Inverse) { |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 5278 | // Recursively handle And and Or conditions. |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5279 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FoundCondValue)) { |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 5280 | if (BO->getOpcode() == Instruction::And) { |
| 5281 | if (!Inverse) |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5282 | return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) || |
| 5283 | isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 5284 | } else if (BO->getOpcode() == Instruction::Or) { |
| 5285 | if (Inverse) |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5286 | return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) || |
| 5287 | isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 5288 | } |
| 5289 | } |
| 5290 | |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5291 | ICmpInst *ICI = dyn_cast<ICmpInst>(FoundCondValue); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 5292 | if (!ICI) return false; |
| 5293 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5294 | // Bail if the ICmp's operands' types are wider than the needed type |
| 5295 | // before attempting to call getSCEV on them. This avoids infinite |
| 5296 | // recursion, since the analysis of widening casts can require loop |
| 5297 | // exit condition information for overflow checking, which would |
| 5298 | // lead back here. |
| 5299 | if (getTypeSizeInBits(LHS->getType()) < |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5300 | getTypeSizeInBits(ICI->getOperand(0)->getType())) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5301 | return false; |
| 5302 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5303 | // Now that we found a conditional branch that dominates the loop, check to |
| 5304 | // see if it is the comparison we are looking for. |
| 5305 | ICmpInst::Predicate FoundPred; |
| 5306 | if (Inverse) |
| 5307 | FoundPred = ICI->getInversePredicate(); |
| 5308 | else |
| 5309 | FoundPred = ICI->getPredicate(); |
| 5310 | |
| 5311 | const SCEV *FoundLHS = getSCEV(ICI->getOperand(0)); |
| 5312 | const SCEV *FoundRHS = getSCEV(ICI->getOperand(1)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5313 | |
| 5314 | // Balance the types. The case where FoundLHS' type is wider than |
| 5315 | // LHS' type is checked for above. |
| 5316 | if (getTypeSizeInBits(LHS->getType()) > |
| 5317 | getTypeSizeInBits(FoundLHS->getType())) { |
| 5318 | if (CmpInst::isSigned(Pred)) { |
| 5319 | FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType()); |
| 5320 | FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType()); |
| 5321 | } else { |
| 5322 | FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType()); |
| 5323 | FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType()); |
| 5324 | } |
| 5325 | } |
| 5326 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5327 | // Canonicalize the query to match the way instcombine will have |
| 5328 | // canonicalized the comparison. |
Dan Gohman | d4da5af | 2010-04-24 01:34:53 +0000 | [diff] [blame] | 5329 | if (SimplifyICmpOperands(Pred, LHS, RHS)) |
| 5330 | if (LHS == RHS) |
Dan Gohman | 34c3e36 | 2010-05-03 18:00:24 +0000 | [diff] [blame] | 5331 | return CmpInst::isTrueWhenEqual(Pred); |
Dan Gohman | d4da5af | 2010-04-24 01:34:53 +0000 | [diff] [blame] | 5332 | if (SimplifyICmpOperands(FoundPred, FoundLHS, FoundRHS)) |
| 5333 | if (FoundLHS == FoundRHS) |
Dan Gohman | 34c3e36 | 2010-05-03 18:00:24 +0000 | [diff] [blame] | 5334 | return CmpInst::isFalseWhenEqual(Pred); |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5335 | |
| 5336 | // Check to see if we can make the LHS or RHS match. |
| 5337 | if (LHS == FoundRHS || RHS == FoundLHS) { |
| 5338 | if (isa<SCEVConstant>(RHS)) { |
| 5339 | std::swap(FoundLHS, FoundRHS); |
| 5340 | FoundPred = ICmpInst::getSwappedPredicate(FoundPred); |
| 5341 | } else { |
| 5342 | std::swap(LHS, RHS); |
| 5343 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 5344 | } |
| 5345 | } |
| 5346 | |
| 5347 | // Check whether the found predicate is the same as the desired predicate. |
| 5348 | if (FoundPred == Pred) |
| 5349 | return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS); |
| 5350 | |
| 5351 | // Check whether swapping the found predicate makes it the same as the |
| 5352 | // desired predicate. |
| 5353 | if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) { |
| 5354 | if (isa<SCEVConstant>(RHS)) |
| 5355 | return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS); |
| 5356 | else |
| 5357 | return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred), |
| 5358 | RHS, LHS, FoundLHS, FoundRHS); |
| 5359 | } |
| 5360 | |
| 5361 | // Check whether the actual condition is beyond sufficient. |
| 5362 | if (FoundPred == ICmpInst::ICMP_EQ) |
| 5363 | if (ICmpInst::isTrueWhenEqual(Pred)) |
| 5364 | if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS)) |
| 5365 | return true; |
| 5366 | if (Pred == ICmpInst::ICMP_NE) |
| 5367 | if (!ICmpInst::isTrueWhenEqual(FoundPred)) |
| 5368 | if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS)) |
| 5369 | return true; |
| 5370 | |
| 5371 | // Otherwise assume the worst. |
| 5372 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5373 | } |
| 5374 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5375 | /// isImpliedCondOperands - Test whether the condition described by Pred, |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 5376 | /// LHS, and RHS is true whenever the condition described by Pred, FoundLHS, |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5377 | /// and FoundRHS is true. |
| 5378 | bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred, |
| 5379 | const SCEV *LHS, const SCEV *RHS, |
| 5380 | const SCEV *FoundLHS, |
| 5381 | const SCEV *FoundRHS) { |
| 5382 | return isImpliedCondOperandsHelper(Pred, LHS, RHS, |
| 5383 | FoundLHS, FoundRHS) || |
| 5384 | // ~x < ~y --> x > y |
| 5385 | isImpliedCondOperandsHelper(Pred, LHS, RHS, |
| 5386 | getNotSCEV(FoundRHS), |
| 5387 | getNotSCEV(FoundLHS)); |
| 5388 | } |
| 5389 | |
| 5390 | /// isImpliedCondOperandsHelper - Test whether the condition described by |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 5391 | /// Pred, LHS, and RHS is true whenever the condition described by Pred, |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5392 | /// FoundLHS, and FoundRHS is true. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5393 | bool |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5394 | ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred, |
| 5395 | const SCEV *LHS, const SCEV *RHS, |
| 5396 | const SCEV *FoundLHS, |
| 5397 | const SCEV *FoundRHS) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5398 | switch (Pred) { |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5399 | default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
| 5400 | case ICmpInst::ICMP_EQ: |
| 5401 | case ICmpInst::ICMP_NE: |
| 5402 | if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS)) |
| 5403 | return true; |
| 5404 | break; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5405 | case ICmpInst::ICMP_SLT: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5406 | case ICmpInst::ICMP_SLE: |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5407 | if (isKnownPredicateWithRanges(ICmpInst::ICMP_SLE, LHS, FoundLHS) && |
| 5408 | isKnownPredicateWithRanges(ICmpInst::ICMP_SGE, RHS, FoundRHS)) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5409 | return true; |
| 5410 | break; |
| 5411 | case ICmpInst::ICMP_SGT: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5412 | case ICmpInst::ICMP_SGE: |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5413 | if (isKnownPredicateWithRanges(ICmpInst::ICMP_SGE, LHS, FoundLHS) && |
| 5414 | isKnownPredicateWithRanges(ICmpInst::ICMP_SLE, RHS, FoundRHS)) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5415 | return true; |
| 5416 | break; |
| 5417 | case ICmpInst::ICMP_ULT: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5418 | case ICmpInst::ICMP_ULE: |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5419 | if (isKnownPredicateWithRanges(ICmpInst::ICMP_ULE, LHS, FoundLHS) && |
| 5420 | isKnownPredicateWithRanges(ICmpInst::ICMP_UGE, RHS, FoundRHS)) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5421 | return true; |
| 5422 | break; |
| 5423 | case ICmpInst::ICMP_UGT: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5424 | case ICmpInst::ICMP_UGE: |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5425 | if (isKnownPredicateWithRanges(ICmpInst::ICMP_UGE, LHS, FoundLHS) && |
| 5426 | isKnownPredicateWithRanges(ICmpInst::ICMP_ULE, RHS, FoundRHS)) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5427 | return true; |
| 5428 | break; |
| 5429 | } |
| 5430 | |
| 5431 | return false; |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 5432 | } |
| 5433 | |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 5434 | /// getBECount - Subtract the end and start values and divide by the step, |
| 5435 | /// rounding up, to get the number of times the backedge is executed. Return |
| 5436 | /// CouldNotCompute if an intermediate computation overflows. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5437 | const SCEV *ScalarEvolution::getBECount(const SCEV *Start, |
Dan Gohman | f5074ec | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 5438 | const SCEV *End, |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 5439 | const SCEV *Step, |
| 5440 | bool NoWrap) { |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5441 | assert(!isKnownNegative(Step) && |
| 5442 | "This code doesn't handle negative strides yet!"); |
| 5443 | |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 5444 | const Type *Ty = Start->getType(); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 5445 | const SCEV *NegOne = getConstant(Ty, (uint64_t)-1); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5446 | const SCEV *Diff = getMinusSCEV(End, Start); |
| 5447 | const SCEV *RoundUp = getAddExpr(Step, NegOne); |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 5448 | |
| 5449 | // Add an adjustment to the difference between End and Start so that |
| 5450 | // the division will effectively round up. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5451 | const SCEV *Add = getAddExpr(Diff, RoundUp); |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 5452 | |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 5453 | if (!NoWrap) { |
| 5454 | // Check Add for unsigned overflow. |
| 5455 | // TODO: More sophisticated things could be done here. |
| 5456 | const Type *WideTy = IntegerType::get(getContext(), |
| 5457 | getTypeSizeInBits(Ty) + 1); |
| 5458 | const SCEV *EDiff = getZeroExtendExpr(Diff, WideTy); |
| 5459 | const SCEV *ERoundUp = getZeroExtendExpr(RoundUp, WideTy); |
| 5460 | const SCEV *OperandExtendedAdd = getAddExpr(EDiff, ERoundUp); |
| 5461 | if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd) |
| 5462 | return getCouldNotCompute(); |
| 5463 | } |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 5464 | |
| 5465 | return getUDivExpr(Add, Step); |
| 5466 | } |
| 5467 | |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5468 | /// HowManyLessThans - Return the number of times a backedge containing the |
| 5469 | /// specified less-than comparison will execute. If not computable, return |
Dan Gohman | 86fbf2f | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 5470 | /// CouldNotCompute. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 5471 | ScalarEvolution::BackedgeTakenInfo |
| 5472 | ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS, |
| 5473 | const Loop *L, bool isSigned) { |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5474 | // Only handle: "ADDREC < LoopInvariant". |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5475 | if (!isLoopInvariant(RHS, L)) return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5476 | |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5477 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5478 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5479 | return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5480 | |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 5481 | // Check to see if we have a flag which makes analysis easy. |
| 5482 | bool NoWrap = isSigned ? AddRec->hasNoSignedWrap() : |
| 5483 | AddRec->hasNoUnsignedWrap(); |
| 5484 | |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5485 | if (AddRec->isAffine()) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5486 | unsigned BitWidth = getTypeSizeInBits(AddRec->getType()); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5487 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5488 | |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5489 | if (Step->isZero()) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5490 | return getCouldNotCompute(); |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5491 | if (Step->isOne()) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5492 | // With unit stride, the iteration never steps past the limit value. |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5493 | } else if (isKnownPositive(Step)) { |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 5494 | // Test whether a positive iteration can step past the limit |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5495 | // value and past the maximum value for its type in a single step. |
| 5496 | // Note that it's not sufficient to check NoWrap here, because even |
| 5497 | // though the value after a wrap is undefined, it's not undefined |
| 5498 | // behavior, so if wrap does occur, the loop could either terminate or |
Dan Gohman | 155eec7 | 2010-01-26 18:32:54 +0000 | [diff] [blame] | 5499 | // loop infinitely, but in either case, the loop is guaranteed to |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5500 | // iterate at least until the iteration where the wrapping occurs. |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 5501 | const SCEV *One = getConstant(Step->getType(), 1); |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5502 | if (isSigned) { |
| 5503 | APInt Max = APInt::getSignedMaxValue(BitWidth); |
| 5504 | if ((Max - getSignedRange(getMinusSCEV(Step, One)).getSignedMax()) |
| 5505 | .slt(getSignedRange(RHS).getSignedMax())) |
| 5506 | return getCouldNotCompute(); |
| 5507 | } else { |
| 5508 | APInt Max = APInt::getMaxValue(BitWidth); |
| 5509 | if ((Max - getUnsignedRange(getMinusSCEV(Step, One)).getUnsignedMax()) |
| 5510 | .ult(getUnsignedRange(RHS).getUnsignedMax())) |
| 5511 | return getCouldNotCompute(); |
| 5512 | } |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5513 | } else |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5514 | // TODO: Handle negative strides here and below. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5515 | return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5516 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5517 | // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant |
| 5518 | // m. So, we count the number of iterations in which {n,+,s} < m is true. |
| 5519 | // Note that we cannot simply return max(m-n,0)/s because it's not safe to |
Wojciech Matyjewicz | a65ee03 | 2008-02-13 12:21:32 +0000 | [diff] [blame] | 5520 | // treat m-n as signed nor unsigned due to overflow possibility. |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5521 | |
Wojciech Matyjewicz | 3a4cbe2 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 5522 | // First, we get the value of the LHS in the first iteration: n |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5523 | const SCEV *Start = AddRec->getOperand(0); |
Wojciech Matyjewicz | 3a4cbe2 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 5524 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5525 | // Determine the minimum constant start value. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5526 | const SCEV *MinStart = getConstant(isSigned ? |
| 5527 | getSignedRange(Start).getSignedMin() : |
| 5528 | getUnsignedRange(Start).getUnsignedMin()); |
Wojciech Matyjewicz | 3a4cbe2 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 5529 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5530 | // If we know that the condition is true in order to enter the loop, |
| 5531 | // then we know that it will run exactly (m-n)/s times. Otherwise, we |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 5532 | // only know that it will execute (max(m,n)-n)/s times. In both cases, |
| 5533 | // the division must round up. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5534 | const SCEV *End = RHS; |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 5535 | if (!isLoopEntryGuardedByCond(L, |
| 5536 | isSigned ? ICmpInst::ICMP_SLT : |
| 5537 | ICmpInst::ICMP_ULT, |
| 5538 | getMinusSCEV(Start, Step), RHS)) |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5539 | End = isSigned ? getSMaxExpr(RHS, Start) |
| 5540 | : getUMaxExpr(RHS, Start); |
| 5541 | |
| 5542 | // Determine the maximum constant end value. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5543 | const SCEV *MaxEnd = getConstant(isSigned ? |
| 5544 | getSignedRange(End).getSignedMax() : |
| 5545 | getUnsignedRange(End).getUnsignedMax()); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5546 | |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5547 | // If MaxEnd is within a step of the maximum integer value in its type, |
| 5548 | // adjust it down to the minimum value which would produce the same effect. |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 5549 | // This allows the subsequent ceiling division of (N+(step-1))/step to |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5550 | // compute the correct value. |
| 5551 | const SCEV *StepMinusOne = getMinusSCEV(Step, |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 5552 | getConstant(Step->getType(), 1)); |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5553 | MaxEnd = isSigned ? |
| 5554 | getSMinExpr(MaxEnd, |
| 5555 | getMinusSCEV(getConstant(APInt::getSignedMaxValue(BitWidth)), |
| 5556 | StepMinusOne)) : |
| 5557 | getUMinExpr(MaxEnd, |
| 5558 | getMinusSCEV(getConstant(APInt::getMaxValue(BitWidth)), |
| 5559 | StepMinusOne)); |
| 5560 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5561 | // Finally, we subtract these two values and divide, rounding up, to get |
| 5562 | // the number of times the backedge is executed. |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 5563 | const SCEV *BECount = getBECount(Start, End, Step, NoWrap); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5564 | |
| 5565 | // The maximum backedge count is similar, except using the minimum start |
| 5566 | // value and the maximum end value. |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 5567 | const SCEV *MaxBECount = getBECount(MinStart, MaxEnd, Step, NoWrap); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5568 | |
| 5569 | return BackedgeTakenInfo(BECount, MaxBECount); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5572 | return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5573 | } |
| 5574 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5575 | /// getNumIterationsInRange - Return the number of iterations of this loop that |
| 5576 | /// produce values in the specified constant range. Another way of looking at |
| 5577 | /// this is that it returns the first iteration number where the value is not in |
| 5578 | /// the condition, thus computing the exit count. If the iteration count can't |
| 5579 | /// be computed, an instance of SCEVCouldNotCompute is returned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5580 | const SCEV *SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 5581 | ScalarEvolution &SE) const { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5582 | if (Range.isFullSet()) // Infinite loop. |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5583 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5584 | |
| 5585 | // If the start is a non-zero constant, shift the range to simplify things. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 5586 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart())) |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 5587 | if (!SC->getValue()->isZero()) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5588 | SmallVector<const SCEV *, 4> Operands(op_begin(), op_end()); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 5589 | Operands[0] = SE.getConstant(SC->getType(), 0); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5590 | const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 5591 | if (const SCEVAddRecExpr *ShiftedAddRec = |
| 5592 | dyn_cast<SCEVAddRecExpr>(Shifted)) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5593 | return ShiftedAddRec->getNumIterationsInRange( |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5594 | Range.subtract(SC->getValue()->getValue()), SE); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5595 | // This is strange and shouldn't happen. |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5596 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5597 | } |
| 5598 | |
| 5599 | // The only time we can solve this is when we have all constant indices. |
| 5600 | // Otherwise, we cannot determine the overflow conditions. |
| 5601 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 5602 | if (!isa<SCEVConstant>(getOperand(i))) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5603 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5604 | |
| 5605 | |
| 5606 | // Okay at this point we know that all elements of the chrec are constants and |
| 5607 | // that the start element is zero. |
| 5608 | |
| 5609 | // First check to see if the range contains zero. If not, the first |
| 5610 | // iteration exits. |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 5611 | unsigned BitWidth = SE.getTypeSizeInBits(getType()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 5612 | if (!Range.contains(APInt(BitWidth, 0))) |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 5613 | return SE.getConstant(getType(), 0); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5614 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5615 | if (isAffine()) { |
| 5616 | // If this is an affine expression then we have this situation: |
| 5617 | // Solve {0,+,A} in Range === Ax in Range |
| 5618 | |
Nick Lewycky | eefdebe | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 5619 | // We know that zero is in the range. If A is positive then we know that |
| 5620 | // the upper value of the range must be the first possible exit value. |
| 5621 | // If A is negative then the lower of the range is the last possible loop |
| 5622 | // value. Also note that we already checked for a full range. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 5623 | APInt One(BitWidth,1); |
Nick Lewycky | eefdebe | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 5624 | APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue(); |
| 5625 | APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5626 | |
Nick Lewycky | eefdebe | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 5627 | // The exit value should be (End+A)/A. |
Nick Lewycky | 9a2f931 | 2007-09-27 14:12:54 +0000 | [diff] [blame] | 5628 | APInt ExitVal = (End + A).udiv(A); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5629 | ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5630 | |
| 5631 | // Evaluate at the exit value. If we really did fall out of the valid |
| 5632 | // range, then we computed our trip count, otherwise wrap around or other |
| 5633 | // things must have happened. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5634 | ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5635 | if (Range.contains(Val->getValue())) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5636 | return SE.getCouldNotCompute(); // Something strange happened |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5637 | |
| 5638 | // Ensure that the previous value is in the range. This is a sanity check. |
Reid Spencer | 581b0d4 | 2007-02-28 19:57:34 +0000 | [diff] [blame] | 5639 | assert(Range.contains( |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 5640 | EvaluateConstantChrecAtConstant(this, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5641 | ConstantInt::get(SE.getContext(), ExitVal - One), SE)->getValue()) && |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5642 | "Linear scev computation is off in a bad way!"); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5643 | return SE.getConstant(ExitValue); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5644 | } else if (isQuadratic()) { |
| 5645 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the |
| 5646 | // quadratic equation to solve it. To do this, we must frame our problem in |
| 5647 | // terms of figuring out when zero is crossed, instead of when |
| 5648 | // Range.getUpper() is crossed. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5649 | SmallVector<const SCEV *, 4> NewOps(op_begin(), op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5650 | NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper())); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5651 | const SCEV *NewAddRec = SE.getAddRecExpr(NewOps, getLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5652 | |
| 5653 | // Next, solve the constructed addrec |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5654 | std::pair<const SCEV *,const SCEV *> Roots = |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5655 | SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5656 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 5657 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5658 | if (R1) { |
| 5659 | // Pick the smallest positive root value. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5660 | if (ConstantInt *CB = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 5661 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 5662 | R1->getValue(), R2->getValue()))) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5663 | if (CB->getZExtValue() == false) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5664 | std::swap(R1, R2); // R1 is the minimum root now. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5665 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5666 | // Make sure the root is not off by one. The returned iteration should |
| 5667 | // not be in the range, but the previous one should be. When solving |
| 5668 | // for "X*X < 5", for example, we should not return a root of 2. |
| 5669 | ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this, |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5670 | R1->getValue(), |
| 5671 | SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5672 | if (Range.contains(R1Val->getValue())) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5673 | // The next iteration must be out of the range... |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 5674 | ConstantInt *NextVal = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5675 | ConstantInt::get(SE.getContext(), R1->getValue()->getValue()+1); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5676 | |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5677 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5678 | if (!Range.contains(R1Val->getValue())) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5679 | return SE.getConstant(NextVal); |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5680 | return SE.getCouldNotCompute(); // Something strange happened |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5681 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5682 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5683 | // If R1 was not in the range, then it is a good return value. Make |
| 5684 | // sure that R1-1 WAS in the range though, just in case. |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 5685 | ConstantInt *NextVal = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5686 | ConstantInt::get(SE.getContext(), R1->getValue()->getValue()-1); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5687 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5688 | if (Range.contains(R1Val->getValue())) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5689 | return R1; |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5690 | return SE.getCouldNotCompute(); // Something strange happened |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5691 | } |
| 5692 | } |
| 5693 | } |
| 5694 | |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5695 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5696 | } |
| 5697 | |
| 5698 | |
| 5699 | |
| 5700 | //===----------------------------------------------------------------------===// |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5701 | // SCEVCallbackVH Class Implementation |
| 5702 | //===----------------------------------------------------------------------===// |
| 5703 | |
Dan Gohman | 1959b75 | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 5704 | void ScalarEvolution::SCEVCallbackVH::deleted() { |
Dan Gohman | ddf9f99 | 2009-07-13 22:20:53 +0000 | [diff] [blame] | 5705 | assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5706 | if (PHINode *PN = dyn_cast<PHINode>(getValPtr())) |
| 5707 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 5708 | SE->ValueExprMap.erase(getValPtr()); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5709 | // this now dangles! |
| 5710 | } |
| 5711 | |
Dan Gohman | 81f9121 | 2010-07-28 01:09:07 +0000 | [diff] [blame] | 5712 | void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *V) { |
Dan Gohman | ddf9f99 | 2009-07-13 22:20:53 +0000 | [diff] [blame] | 5713 | assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); |
Eric Christopher | e6cbfa6 | 2010-07-29 01:25:38 +0000 | [diff] [blame] | 5714 | |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5715 | // Forget all the expressions associated with users of the old value, |
| 5716 | // so that future queries will recompute the expressions using the new |
| 5717 | // value. |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 5718 | Value *Old = getValPtr(); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5719 | SmallVector<User *, 16> Worklist; |
Dan Gohman | 69fcae9 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5720 | SmallPtrSet<User *, 8> Visited; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5721 | for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end(); |
| 5722 | UI != UE; ++UI) |
| 5723 | Worklist.push_back(*UI); |
| 5724 | while (!Worklist.empty()) { |
| 5725 | User *U = Worklist.pop_back_val(); |
| 5726 | // Deleting the Old value will cause this to dangle. Postpone |
| 5727 | // that until everything else is done. |
Dan Gohman | 59846ac | 2010-07-28 00:28:25 +0000 | [diff] [blame] | 5728 | if (U == Old) |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5729 | continue; |
Dan Gohman | 69fcae9 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5730 | if (!Visited.insert(U)) |
| 5731 | continue; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5732 | if (PHINode *PN = dyn_cast<PHINode>(U)) |
| 5733 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 5734 | SE->ValueExprMap.erase(U); |
Dan Gohman | 69fcae9 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5735 | for (Value::use_iterator UI = U->use_begin(), UE = U->use_end(); |
| 5736 | UI != UE; ++UI) |
| 5737 | Worklist.push_back(*UI); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5738 | } |
Dan Gohman | 59846ac | 2010-07-28 00:28:25 +0000 | [diff] [blame] | 5739 | // Delete the Old value. |
| 5740 | if (PHINode *PN = dyn_cast<PHINode>(Old)) |
| 5741 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 5742 | SE->ValueExprMap.erase(Old); |
Dan Gohman | 59846ac | 2010-07-28 00:28:25 +0000 | [diff] [blame] | 5743 | // this now dangles! |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5744 | } |
| 5745 | |
Dan Gohman | 1959b75 | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 5746 | ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5747 | : CallbackVH(V), SE(se) {} |
| 5748 | |
| 5749 | //===----------------------------------------------------------------------===// |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5750 | // ScalarEvolution Class Implementation |
| 5751 | //===----------------------------------------------------------------------===// |
| 5752 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5753 | ScalarEvolution::ScalarEvolution() |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 5754 | : FunctionPass(ID), FirstUnknown(0) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 5755 | initializeScalarEvolutionPass(*PassRegistry::getPassRegistry()); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5756 | } |
| 5757 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5758 | bool ScalarEvolution::runOnFunction(Function &F) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5759 | this->F = &F; |
| 5760 | LI = &getAnalysis<LoopInfo>(); |
| 5761 | TD = getAnalysisIfAvailable<TargetData>(); |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 5762 | DT = &getAnalysis<DominatorTree>(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5763 | return false; |
| 5764 | } |
| 5765 | |
| 5766 | void ScalarEvolution::releaseMemory() { |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 5767 | // Iterate through all the SCEVUnknown instances and call their |
| 5768 | // destructors, so that they release their references to their values. |
| 5769 | for (SCEVUnknown *U = FirstUnknown; U; U = U->Next) |
| 5770 | U->~SCEVUnknown(); |
| 5771 | FirstUnknown = 0; |
| 5772 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 5773 | ValueExprMap.clear(); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5774 | BackedgeTakenCounts.clear(); |
| 5775 | ConstantEvolutionLoopExitValue.clear(); |
Dan Gohman | 6bce643 | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 5776 | ValuesAtScopes.clear(); |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 5777 | LoopDispositions.clear(); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 5778 | BlockDispositions.clear(); |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 5779 | UnsignedRanges.clear(); |
| 5780 | SignedRanges.clear(); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5781 | UniqueSCEVs.clear(); |
| 5782 | SCEVAllocator.Reset(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5783 | } |
| 5784 | |
| 5785 | void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const { |
| 5786 | AU.setPreservesAll(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5787 | AU.addRequiredTransitive<LoopInfo>(); |
Dan Gohman | 1cd9275 | 2010-01-19 22:21:27 +0000 | [diff] [blame] | 5788 | AU.addRequiredTransitive<DominatorTree>(); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 5789 | } |
| 5790 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5791 | bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5792 | return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5793 | } |
| 5794 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5795 | static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5796 | const Loop *L) { |
| 5797 | // Print all inner loops first |
| 5798 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 5799 | PrintLoopInfo(OS, SE, *I); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5800 | |
Dan Gohman | 3073329 | 2010-01-09 18:17:45 +0000 | [diff] [blame] | 5801 | OS << "Loop "; |
| 5802 | WriteAsOperand(OS, L->getHeader(), /*PrintType=*/false); |
| 5803 | OS << ": "; |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 5804 | |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 5805 | SmallVector<BasicBlock *, 8> ExitBlocks; |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 5806 | L->getExitBlocks(ExitBlocks); |
| 5807 | if (ExitBlocks.size() != 1) |
Nick Lewycky | aeb5e5c | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 5808 | OS << "<multiple exits> "; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5809 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5810 | if (SE->hasLoopInvariantBackedgeTakenCount(L)) { |
| 5811 | OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5812 | } else { |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5813 | OS << "Unpredictable backedge-taken count. "; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5814 | } |
| 5815 | |
Dan Gohman | 3073329 | 2010-01-09 18:17:45 +0000 | [diff] [blame] | 5816 | OS << "\n" |
| 5817 | "Loop "; |
| 5818 | WriteAsOperand(OS, L->getHeader(), /*PrintType=*/false); |
| 5819 | OS << ": "; |
Dan Gohman | aa551ae | 2009-06-24 00:33:16 +0000 | [diff] [blame] | 5820 | |
| 5821 | if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) { |
| 5822 | OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L); |
| 5823 | } else { |
| 5824 | OS << "Unpredictable max backedge-taken count. "; |
| 5825 | } |
| 5826 | |
| 5827 | OS << "\n"; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5828 | } |
| 5829 | |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 5830 | void ScalarEvolution::print(raw_ostream &OS, const Module *) const { |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 5831 | // ScalarEvolution's implementation of the print method is to print |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5832 | // out SCEV values of all instructions that are interesting. Doing |
| 5833 | // this potentially causes it to create new SCEV objects though, |
| 5834 | // which technically conflicts with the const qualifier. This isn't |
Dan Gohman | 1afdc5f | 2009-07-10 20:25:29 +0000 | [diff] [blame] | 5835 | // observable from outside the class though, so casting away the |
| 5836 | // const isn't dangerous. |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 5837 | ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5838 | |
Dan Gohman | 3073329 | 2010-01-09 18:17:45 +0000 | [diff] [blame] | 5839 | OS << "Classifying expressions for: "; |
| 5840 | WriteAsOperand(OS, F, /*PrintType=*/false); |
| 5841 | OS << "\n"; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5842 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
Dan Gohman | a189bae | 2010-05-03 17:03:23 +0000 | [diff] [blame] | 5843 | if (isSCEVable(I->getType()) && !isa<CmpInst>(*I)) { |
Dan Gohman | c902e13 | 2009-07-13 23:03:05 +0000 | [diff] [blame] | 5844 | OS << *I << '\n'; |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 5845 | OS << " --> "; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5846 | const SCEV *SV = SE.getSCEV(&*I); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5847 | SV->print(OS); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5848 | |
Dan Gohman | 0c689c5 | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 5849 | const Loop *L = LI->getLoopFor((*I).getParent()); |
| 5850 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5851 | const SCEV *AtUse = SE.getSCEVAtScope(SV, L); |
Dan Gohman | 0c689c5 | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 5852 | if (AtUse != SV) { |
| 5853 | OS << " --> "; |
| 5854 | AtUse->print(OS); |
| 5855 | } |
| 5856 | |
| 5857 | if (L) { |
Dan Gohman | 9e7d988 | 2009-06-18 00:37:45 +0000 | [diff] [blame] | 5858 | OS << "\t\t" "Exits: "; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5859 | const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop()); |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5860 | if (!SE.isLoopInvariant(ExitValue, L)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5861 | OS << "<<Unknown>>"; |
| 5862 | } else { |
| 5863 | OS << *ExitValue; |
| 5864 | } |
| 5865 | } |
| 5866 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5867 | OS << "\n"; |
| 5868 | } |
| 5869 | |
Dan Gohman | 3073329 | 2010-01-09 18:17:45 +0000 | [diff] [blame] | 5870 | OS << "Determining loop execution counts for: "; |
| 5871 | WriteAsOperand(OS, F, /*PrintType=*/false); |
| 5872 | OS << "\n"; |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5873 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
| 5874 | PrintLoopInfo(OS, &SE, *I); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5875 | } |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 5876 | |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 5877 | ScalarEvolution::LoopDisposition |
| 5878 | ScalarEvolution::getLoopDisposition(const SCEV *S, const Loop *L) { |
| 5879 | std::map<const Loop *, LoopDisposition> &Values = LoopDispositions[S]; |
| 5880 | std::pair<std::map<const Loop *, LoopDisposition>::iterator, bool> Pair = |
| 5881 | Values.insert(std::make_pair(L, LoopVariant)); |
| 5882 | if (!Pair.second) |
| 5883 | return Pair.first->second; |
| 5884 | |
| 5885 | LoopDisposition D = computeLoopDisposition(S, L); |
| 5886 | return LoopDispositions[S][L] = D; |
| 5887 | } |
| 5888 | |
| 5889 | ScalarEvolution::LoopDisposition |
| 5890 | ScalarEvolution::computeLoopDisposition(const SCEV *S, const Loop *L) { |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5891 | switch (S->getSCEVType()) { |
| 5892 | case scConstant: |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 5893 | return LoopInvariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5894 | case scTruncate: |
| 5895 | case scZeroExtend: |
| 5896 | case scSignExtend: |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 5897 | return getLoopDisposition(cast<SCEVCastExpr>(S)->getOperand(), L); |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5898 | case scAddRecExpr: { |
| 5899 | const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S); |
| 5900 | |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 5901 | // If L is the addrec's loop, it's computable. |
| 5902 | if (AR->getLoop() == L) |
| 5903 | return LoopComputable; |
| 5904 | |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5905 | // Add recurrences are never invariant in the function-body (null loop). |
| 5906 | if (!L) |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 5907 | return LoopVariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5908 | |
| 5909 | // This recurrence is variant w.r.t. L if L contains AR's loop. |
| 5910 | if (L->contains(AR->getLoop())) |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 5911 | return LoopVariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5912 | |
| 5913 | // This recurrence is invariant w.r.t. L if AR's loop contains L. |
| 5914 | if (AR->getLoop()->contains(L)) |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 5915 | return LoopInvariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5916 | |
| 5917 | // This recurrence is variant w.r.t. L if any of its operands |
| 5918 | // are variant. |
| 5919 | for (SCEVAddRecExpr::op_iterator I = AR->op_begin(), E = AR->op_end(); |
| 5920 | I != E; ++I) |
| 5921 | if (!isLoopInvariant(*I, L)) |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 5922 | return LoopVariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5923 | |
| 5924 | // Otherwise it's loop-invariant. |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 5925 | return LoopInvariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5926 | } |
| 5927 | case scAddExpr: |
| 5928 | case scMulExpr: |
| 5929 | case scUMaxExpr: |
| 5930 | case scSMaxExpr: { |
| 5931 | const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S); |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5932 | bool HasVarying = false; |
| 5933 | for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end(); |
| 5934 | I != E; ++I) { |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 5935 | LoopDisposition D = getLoopDisposition(*I, L); |
| 5936 | if (D == LoopVariant) |
| 5937 | return LoopVariant; |
| 5938 | if (D == LoopComputable) |
| 5939 | HasVarying = true; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5940 | } |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 5941 | return HasVarying ? LoopComputable : LoopInvariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5942 | } |
| 5943 | case scUDivExpr: { |
| 5944 | const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S); |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 5945 | LoopDisposition LD = getLoopDisposition(UDiv->getLHS(), L); |
| 5946 | if (LD == LoopVariant) |
| 5947 | return LoopVariant; |
| 5948 | LoopDisposition RD = getLoopDisposition(UDiv->getRHS(), L); |
| 5949 | if (RD == LoopVariant) |
| 5950 | return LoopVariant; |
| 5951 | return (LD == LoopInvariant && RD == LoopInvariant) ? |
| 5952 | LoopInvariant : LoopComputable; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5953 | } |
| 5954 | case scUnknown: |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 5955 | // All non-instruction values are loop invariant. All instructions are loop |
| 5956 | // invariant if they are not contained in the specified loop. |
| 5957 | // Instructions are never considered invariant in the function body |
| 5958 | // (null loop) because they are defined within the "loop". |
| 5959 | if (Instruction *I = dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) |
| 5960 | return (L && !L->contains(I)) ? LoopInvariant : LoopVariant; |
| 5961 | return LoopInvariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5962 | case scCouldNotCompute: |
| 5963 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 5964 | return LoopVariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5965 | default: break; |
| 5966 | } |
| 5967 | llvm_unreachable("Unknown SCEV kind!"); |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 5968 | return LoopVariant; |
| 5969 | } |
| 5970 | |
| 5971 | bool ScalarEvolution::isLoopInvariant(const SCEV *S, const Loop *L) { |
| 5972 | return getLoopDisposition(S, L) == LoopInvariant; |
| 5973 | } |
| 5974 | |
| 5975 | bool ScalarEvolution::hasComputableLoopEvolution(const SCEV *S, const Loop *L) { |
| 5976 | return getLoopDisposition(S, L) == LoopComputable; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5977 | } |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 5978 | |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 5979 | ScalarEvolution::BlockDisposition |
| 5980 | ScalarEvolution::getBlockDisposition(const SCEV *S, const BasicBlock *BB) { |
| 5981 | std::map<const BasicBlock *, BlockDisposition> &Values = BlockDispositions[S]; |
| 5982 | std::pair<std::map<const BasicBlock *, BlockDisposition>::iterator, bool> |
| 5983 | Pair = Values.insert(std::make_pair(BB, DoesNotDominateBlock)); |
| 5984 | if (!Pair.second) |
| 5985 | return Pair.first->second; |
| 5986 | |
| 5987 | BlockDisposition D = computeBlockDisposition(S, BB); |
| 5988 | return BlockDispositions[S][BB] = D; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 5989 | } |
| 5990 | |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 5991 | ScalarEvolution::BlockDisposition |
| 5992 | ScalarEvolution::computeBlockDisposition(const SCEV *S, const BasicBlock *BB) { |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 5993 | switch (S->getSCEVType()) { |
| 5994 | case scConstant: |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 5995 | return ProperlyDominatesBlock; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 5996 | case scTruncate: |
| 5997 | case scZeroExtend: |
| 5998 | case scSignExtend: |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 5999 | return getBlockDisposition(cast<SCEVCastExpr>(S)->getOperand(), BB); |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6000 | case scAddRecExpr: { |
| 6001 | // This uses a "dominates" query instead of "properly dominates" query |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6002 | // to test for proper dominance too, because the instruction which |
| 6003 | // produces the addrec's value is a PHI, and a PHI effectively properly |
| 6004 | // dominates its entire containing block. |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6005 | const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S); |
| 6006 | if (!DT->dominates(AR->getLoop()->getHeader(), BB)) |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6007 | return DoesNotDominateBlock; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6008 | } |
| 6009 | // FALL THROUGH into SCEVNAryExpr handling. |
| 6010 | case scAddExpr: |
| 6011 | case scMulExpr: |
| 6012 | case scUMaxExpr: |
| 6013 | case scSMaxExpr: { |
| 6014 | const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6015 | bool Proper = true; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6016 | for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end(); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6017 | I != E; ++I) { |
| 6018 | BlockDisposition D = getBlockDisposition(*I, BB); |
| 6019 | if (D == DoesNotDominateBlock) |
| 6020 | return DoesNotDominateBlock; |
| 6021 | if (D == DominatesBlock) |
| 6022 | Proper = false; |
| 6023 | } |
| 6024 | return Proper ? ProperlyDominatesBlock : DominatesBlock; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6025 | } |
| 6026 | case scUDivExpr: { |
| 6027 | const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6028 | const SCEV *LHS = UDiv->getLHS(), *RHS = UDiv->getRHS(); |
| 6029 | BlockDisposition LD = getBlockDisposition(LHS, BB); |
| 6030 | if (LD == DoesNotDominateBlock) |
| 6031 | return DoesNotDominateBlock; |
| 6032 | BlockDisposition RD = getBlockDisposition(RHS, BB); |
| 6033 | if (RD == DoesNotDominateBlock) |
| 6034 | return DoesNotDominateBlock; |
| 6035 | return (LD == ProperlyDominatesBlock && RD == ProperlyDominatesBlock) ? |
| 6036 | ProperlyDominatesBlock : DominatesBlock; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6037 | } |
| 6038 | case scUnknown: |
| 6039 | if (Instruction *I = |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6040 | dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) { |
| 6041 | if (I->getParent() == BB) |
| 6042 | return DominatesBlock; |
| 6043 | if (DT->properlyDominates(I->getParent(), BB)) |
| 6044 | return ProperlyDominatesBlock; |
| 6045 | return DoesNotDominateBlock; |
| 6046 | } |
| 6047 | return ProperlyDominatesBlock; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6048 | case scCouldNotCompute: |
| 6049 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6050 | return DoesNotDominateBlock; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6051 | default: break; |
| 6052 | } |
| 6053 | llvm_unreachable("Unknown SCEV kind!"); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6054 | return DoesNotDominateBlock; |
| 6055 | } |
| 6056 | |
| 6057 | bool ScalarEvolution::dominates(const SCEV *S, const BasicBlock *BB) { |
| 6058 | return getBlockDisposition(S, BB) >= DominatesBlock; |
| 6059 | } |
| 6060 | |
| 6061 | bool ScalarEvolution::properlyDominates(const SCEV *S, const BasicBlock *BB) { |
| 6062 | return getBlockDisposition(S, BB) == ProperlyDominatesBlock; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6063 | } |
Dan Gohman | 4ce32db | 2010-11-17 22:27:42 +0000 | [diff] [blame] | 6064 | |
| 6065 | bool ScalarEvolution::hasOperand(const SCEV *S, const SCEV *Op) const { |
| 6066 | switch (S->getSCEVType()) { |
| 6067 | case scConstant: |
| 6068 | return false; |
| 6069 | case scTruncate: |
| 6070 | case scZeroExtend: |
| 6071 | case scSignExtend: { |
| 6072 | const SCEVCastExpr *Cast = cast<SCEVCastExpr>(S); |
| 6073 | const SCEV *CastOp = Cast->getOperand(); |
| 6074 | return Op == CastOp || hasOperand(CastOp, Op); |
| 6075 | } |
| 6076 | case scAddRecExpr: |
| 6077 | case scAddExpr: |
| 6078 | case scMulExpr: |
| 6079 | case scUMaxExpr: |
| 6080 | case scSMaxExpr: { |
| 6081 | const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S); |
| 6082 | for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end(); |
| 6083 | I != E; ++I) { |
| 6084 | const SCEV *NAryOp = *I; |
| 6085 | if (NAryOp == Op || hasOperand(NAryOp, Op)) |
| 6086 | return true; |
| 6087 | } |
| 6088 | return false; |
| 6089 | } |
| 6090 | case scUDivExpr: { |
| 6091 | const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S); |
| 6092 | const SCEV *LHS = UDiv->getLHS(), *RHS = UDiv->getRHS(); |
| 6093 | return LHS == Op || hasOperand(LHS, Op) || |
| 6094 | RHS == Op || hasOperand(RHS, Op); |
| 6095 | } |
| 6096 | case scUnknown: |
| 6097 | return false; |
| 6098 | case scCouldNotCompute: |
| 6099 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
| 6100 | return false; |
| 6101 | default: break; |
| 6102 | } |
| 6103 | llvm_unreachable("Unknown SCEV kind!"); |
| 6104 | return false; |
| 6105 | } |
Dan Gohman | 56a7568 | 2010-11-17 23:28:48 +0000 | [diff] [blame] | 6106 | |
| 6107 | void ScalarEvolution::forgetMemoizedResults(const SCEV *S) { |
| 6108 | ValuesAtScopes.erase(S); |
| 6109 | LoopDispositions.erase(S); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6110 | BlockDispositions.erase(S); |
Dan Gohman | 56a7568 | 2010-11-17 23:28:48 +0000 | [diff] [blame] | 6111 | UnsignedRanges.erase(S); |
| 6112 | SignedRanges.erase(S); |
| 6113 | } |