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 << "}<"; |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 160 | if (AR->getNoWrapFlags(FlagNUW)) |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 161 | OS << "nuw><"; |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 162 | if (AR->getNoWrapFlags(FlagNSW)) |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 163 | OS << "nsw><"; |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 164 | if (AR->getNoWrapFlags(FlagNW) && |
| 165 | !AR->getNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW))) |
| 166 | OS << "nw><"; |
Dan Gohman | 4ce32db | 2010-11-17 22:27:42 +0000 | [diff] [blame] | 167 | WriteAsOperand(OS, AR->getLoop()->getHeader(), /*PrintType=*/false); |
| 168 | OS << ">"; |
| 169 | return; |
| 170 | } |
| 171 | case scAddExpr: |
| 172 | case scMulExpr: |
| 173 | case scUMaxExpr: |
| 174 | case scSMaxExpr: { |
| 175 | const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(this); |
Benjamin Kramer | b458b15 | 2010-11-19 11:37:26 +0000 | [diff] [blame] | 176 | const char *OpStr = 0; |
Dan Gohman | 4ce32db | 2010-11-17 22:27:42 +0000 | [diff] [blame] | 177 | switch (NAry->getSCEVType()) { |
| 178 | case scAddExpr: OpStr = " + "; break; |
| 179 | case scMulExpr: OpStr = " * "; break; |
| 180 | case scUMaxExpr: OpStr = " umax "; break; |
| 181 | case scSMaxExpr: OpStr = " smax "; break; |
| 182 | } |
| 183 | OS << "("; |
| 184 | for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end(); |
| 185 | I != E; ++I) { |
| 186 | OS << **I; |
| 187 | if (llvm::next(I) != E) |
| 188 | OS << OpStr; |
| 189 | } |
| 190 | OS << ")"; |
| 191 | return; |
| 192 | } |
| 193 | case scUDivExpr: { |
| 194 | const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(this); |
| 195 | OS << "(" << *UDiv->getLHS() << " /u " << *UDiv->getRHS() << ")"; |
| 196 | return; |
| 197 | } |
| 198 | case scUnknown: { |
| 199 | const SCEVUnknown *U = cast<SCEVUnknown>(this); |
| 200 | const Type *AllocTy; |
| 201 | if (U->isSizeOf(AllocTy)) { |
| 202 | OS << "sizeof(" << *AllocTy << ")"; |
| 203 | return; |
| 204 | } |
| 205 | if (U->isAlignOf(AllocTy)) { |
| 206 | OS << "alignof(" << *AllocTy << ")"; |
| 207 | return; |
| 208 | } |
Andrew Trick | 635f718 | 2011-03-09 17:23:39 +0000 | [diff] [blame] | 209 | |
Dan Gohman | 4ce32db | 2010-11-17 22:27:42 +0000 | [diff] [blame] | 210 | const Type *CTy; |
| 211 | Constant *FieldNo; |
| 212 | if (U->isOffsetOf(CTy, FieldNo)) { |
| 213 | OS << "offsetof(" << *CTy << ", "; |
| 214 | WriteAsOperand(OS, FieldNo, false); |
| 215 | OS << ")"; |
| 216 | return; |
| 217 | } |
Andrew Trick | 635f718 | 2011-03-09 17:23:39 +0000 | [diff] [blame] | 218 | |
Dan Gohman | 4ce32db | 2010-11-17 22:27:42 +0000 | [diff] [blame] | 219 | // Otherwise just print it normally. |
| 220 | WriteAsOperand(OS, U->getValue(), false); |
| 221 | return; |
| 222 | } |
| 223 | case scCouldNotCompute: |
| 224 | OS << "***COULDNOTCOMPUTE***"; |
| 225 | return; |
| 226 | default: break; |
| 227 | } |
| 228 | llvm_unreachable("Unknown SCEV kind!"); |
| 229 | } |
| 230 | |
| 231 | const Type *SCEV::getType() const { |
| 232 | switch (getSCEVType()) { |
| 233 | case scConstant: |
| 234 | return cast<SCEVConstant>(this)->getType(); |
| 235 | case scTruncate: |
| 236 | case scZeroExtend: |
| 237 | case scSignExtend: |
| 238 | return cast<SCEVCastExpr>(this)->getType(); |
| 239 | case scAddRecExpr: |
| 240 | case scMulExpr: |
| 241 | case scUMaxExpr: |
| 242 | case scSMaxExpr: |
| 243 | return cast<SCEVNAryExpr>(this)->getType(); |
| 244 | case scAddExpr: |
| 245 | return cast<SCEVAddExpr>(this)->getType(); |
| 246 | case scUDivExpr: |
| 247 | return cast<SCEVUDivExpr>(this)->getType(); |
| 248 | case scUnknown: |
| 249 | return cast<SCEVUnknown>(this)->getType(); |
| 250 | case scCouldNotCompute: |
| 251 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
| 252 | return 0; |
| 253 | default: break; |
| 254 | } |
| 255 | llvm_unreachable("Unknown SCEV kind!"); |
| 256 | return 0; |
| 257 | } |
| 258 | |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 259 | bool SCEV::isZero() const { |
| 260 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 261 | return SC->getValue()->isZero(); |
| 262 | return false; |
| 263 | } |
| 264 | |
Dan Gohman | 70a1fe7 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 265 | bool SCEV::isOne() const { |
| 266 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 267 | return SC->getValue()->isOne(); |
| 268 | return false; |
| 269 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 270 | |
Dan Gohman | 4d289bf | 2009-06-24 00:30:26 +0000 | [diff] [blame] | 271 | bool SCEV::isAllOnesValue() const { |
| 272 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 273 | return SC->getValue()->isAllOnesValue(); |
| 274 | return false; |
| 275 | } |
| 276 | |
Owen Anderson | 753ad61 | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 277 | SCEVCouldNotCompute::SCEVCouldNotCompute() : |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 278 | SCEV(FoldingSetNodeIDRef(), scCouldNotCompute) {} |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 280 | bool SCEVCouldNotCompute::classof(const SCEV *S) { |
| 281 | return S->getSCEVType() == scCouldNotCompute; |
| 282 | } |
| 283 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 284 | const SCEV *ScalarEvolution::getConstant(ConstantInt *V) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 285 | FoldingSetNodeID ID; |
| 286 | ID.AddInteger(scConstant); |
| 287 | ID.AddPointer(V); |
| 288 | void *IP = 0; |
| 289 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 290 | SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 291 | UniqueSCEVs.InsertNode(S, IP); |
| 292 | return S; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 293 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 294 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 295 | const SCEV *ScalarEvolution::getConstant(const APInt& Val) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 296 | return getConstant(ConstantInt::get(getContext(), Val)); |
Dan Gohman | 9a6ae96 | 2007-07-09 15:25:17 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 299 | const SCEV * |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 300 | ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) { |
Dan Gohman | a560fd2 | 2010-04-21 16:04:04 +0000 | [diff] [blame] | 301 | const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty)); |
| 302 | return getConstant(ConstantInt::get(ITy, V, isSigned)); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 305 | SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID, |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 306 | unsigned SCEVTy, const SCEV *op, const Type *ty) |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 307 | : SCEV(ID, SCEVTy), Op(op), Ty(ty) {} |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 308 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 309 | SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID, |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 310 | const SCEV *op, const Type *ty) |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 311 | : SCEVCastExpr(ID, scTruncate, op, ty) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 312 | assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) && |
| 313 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 314 | "Cannot truncate non-integer value!"); |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 315 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 316 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 317 | SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID, |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 318 | const SCEV *op, const Type *ty) |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 319 | : SCEVCastExpr(ID, scZeroExtend, op, ty) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 320 | assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) && |
| 321 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 322 | "Cannot zero extend non-integer value!"); |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 325 | SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID, |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 326 | const SCEV *op, const Type *ty) |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 327 | : SCEVCastExpr(ID, scSignExtend, op, ty) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 328 | assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) && |
| 329 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 330 | "Cannot sign extend non-integer value!"); |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 333 | void SCEVUnknown::deleted() { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 334 | // Clear this SCEVUnknown from various maps. |
Dan Gohman | 56a7568 | 2010-11-17 23:28:48 +0000 | [diff] [blame] | 335 | SE->forgetMemoizedResults(this); |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 336 | |
| 337 | // Remove this SCEVUnknown from the uniquing map. |
| 338 | SE->UniqueSCEVs.RemoveNode(this); |
| 339 | |
| 340 | // Release the value. |
| 341 | setValPtr(0); |
| 342 | } |
| 343 | |
| 344 | void SCEVUnknown::allUsesReplacedWith(Value *New) { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 345 | // Clear this SCEVUnknown from various maps. |
Dan Gohman | 56a7568 | 2010-11-17 23:28:48 +0000 | [diff] [blame] | 346 | SE->forgetMemoizedResults(this); |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 347 | |
| 348 | // Remove this SCEVUnknown from the uniquing map. |
| 349 | SE->UniqueSCEVs.RemoveNode(this); |
| 350 | |
| 351 | // Update this SCEVUnknown to point to the new value. This is needed |
| 352 | // because there may still be outstanding SCEVs which still point to |
| 353 | // this SCEVUnknown. |
| 354 | setValPtr(New); |
| 355 | } |
| 356 | |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 357 | bool SCEVUnknown::isSizeOf(const Type *&AllocTy) const { |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 358 | if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue())) |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 359 | if (VCE->getOpcode() == Instruction::PtrToInt) |
| 360 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0))) |
Dan Gohman | 8db08df | 2010-02-02 01:38:49 +0000 | [diff] [blame] | 361 | if (CE->getOpcode() == Instruction::GetElementPtr && |
| 362 | CE->getOperand(0)->isNullValue() && |
| 363 | CE->getNumOperands() == 2) |
| 364 | if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(1))) |
| 365 | if (CI->isOne()) { |
| 366 | AllocTy = cast<PointerType>(CE->getOperand(0)->getType()) |
| 367 | ->getElementType(); |
| 368 | return true; |
| 369 | } |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 370 | |
| 371 | return false; |
| 372 | } |
| 373 | |
| 374 | bool SCEVUnknown::isAlignOf(const Type *&AllocTy) const { |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 375 | if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue())) |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 376 | if (VCE->getOpcode() == Instruction::PtrToInt) |
| 377 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0))) |
Dan Gohman | 8db08df | 2010-02-02 01:38:49 +0000 | [diff] [blame] | 378 | if (CE->getOpcode() == Instruction::GetElementPtr && |
| 379 | CE->getOperand(0)->isNullValue()) { |
| 380 | const Type *Ty = |
| 381 | cast<PointerType>(CE->getOperand(0)->getType())->getElementType(); |
| 382 | if (const StructType *STy = dyn_cast<StructType>(Ty)) |
| 383 | if (!STy->isPacked() && |
| 384 | CE->getNumOperands() == 3 && |
| 385 | CE->getOperand(1)->isNullValue()) { |
| 386 | if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(2))) |
| 387 | if (CI->isOne() && |
| 388 | STy->getNumElements() == 2 && |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 389 | STy->getElementType(0)->isIntegerTy(1)) { |
Dan Gohman | 8db08df | 2010-02-02 01:38:49 +0000 | [diff] [blame] | 390 | AllocTy = STy->getElementType(1); |
| 391 | return true; |
| 392 | } |
| 393 | } |
| 394 | } |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 395 | |
| 396 | return false; |
| 397 | } |
| 398 | |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 399 | bool SCEVUnknown::isOffsetOf(const Type *&CTy, Constant *&FieldNo) const { |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 400 | if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue())) |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 401 | if (VCE->getOpcode() == Instruction::PtrToInt) |
| 402 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0))) |
| 403 | if (CE->getOpcode() == Instruction::GetElementPtr && |
| 404 | CE->getNumOperands() == 3 && |
| 405 | CE->getOperand(0)->isNullValue() && |
| 406 | CE->getOperand(1)->isNullValue()) { |
| 407 | const Type *Ty = |
| 408 | cast<PointerType>(CE->getOperand(0)->getType())->getElementType(); |
| 409 | // Ignore vector types here so that ScalarEvolutionExpander doesn't |
| 410 | // emit getelementptrs that index into vectors. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 411 | if (Ty->isStructTy() || Ty->isArrayTy()) { |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 412 | CTy = Ty; |
| 413 | FieldNo = CE->getOperand(2); |
| 414 | return true; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | return false; |
| 419 | } |
| 420 | |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 421 | //===----------------------------------------------------------------------===// |
| 422 | // SCEV Utilities |
| 423 | //===----------------------------------------------------------------------===// |
| 424 | |
| 425 | namespace { |
| 426 | /// SCEVComplexityCompare - Return true if the complexity of the LHS is less |
| 427 | /// than the complexity of the RHS. This comparator is used to canonicalize |
| 428 | /// expressions. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 429 | class SCEVComplexityCompare { |
Dan Gohman | 9f1fb42 | 2010-08-13 20:17:27 +0000 | [diff] [blame] | 430 | const LoopInfo *const LI; |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 431 | public: |
Dan Gohman | e72079a | 2010-07-23 21:18:55 +0000 | [diff] [blame] | 432 | explicit SCEVComplexityCompare(const LoopInfo *li) : LI(li) {} |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 433 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 434 | // 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] | 435 | bool operator()(const SCEV *LHS, const SCEV *RHS) const { |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 436 | return compare(LHS, RHS) < 0; |
| 437 | } |
| 438 | |
| 439 | // Return negative, zero, or positive, if LHS is less than, equal to, or |
| 440 | // greater than RHS, respectively. A three-way result allows recursive |
| 441 | // comparisons to be more efficient. |
| 442 | int compare(const SCEV *LHS, const SCEV *RHS) const { |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 443 | // Fast-path: SCEVs are uniqued so we can do a quick equality check. |
| 444 | if (LHS == RHS) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 445 | return 0; |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 446 | |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 447 | // Primarily, sort the SCEVs by their getSCEVType(). |
Dan Gohman | 304a7a6 | 2010-07-23 21:20:52 +0000 | [diff] [blame] | 448 | unsigned LType = LHS->getSCEVType(), RType = RHS->getSCEVType(); |
| 449 | if (LType != RType) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 450 | return (int)LType - (int)RType; |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 451 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 452 | // Aside from the getSCEVType() ordering, the particular ordering |
| 453 | // isn't very important except that it's beneficial to be consistent, |
| 454 | // 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] | 455 | switch (LType) { |
| 456 | case scUnknown: { |
| 457 | const SCEVUnknown *LU = cast<SCEVUnknown>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 458 | const SCEVUnknown *RU = cast<SCEVUnknown>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 459 | |
| 460 | // Sort SCEVUnknown values with some loose heuristics. TODO: This is |
| 461 | // not as complete as it could be. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 462 | const Value *LV = LU->getValue(), *RV = RU->getValue(); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 463 | |
| 464 | // Order pointer values after integer values. This helps SCEVExpander |
| 465 | // form GEPs. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 466 | bool LIsPointer = LV->getType()->isPointerTy(), |
| 467 | RIsPointer = RV->getType()->isPointerTy(); |
Dan Gohman | 304a7a6 | 2010-07-23 21:20:52 +0000 | [diff] [blame] | 468 | if (LIsPointer != RIsPointer) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 469 | return (int)LIsPointer - (int)RIsPointer; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 470 | |
| 471 | // Compare getValueID values. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 472 | unsigned LID = LV->getValueID(), |
| 473 | RID = RV->getValueID(); |
Dan Gohman | 304a7a6 | 2010-07-23 21:20:52 +0000 | [diff] [blame] | 474 | if (LID != RID) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 475 | return (int)LID - (int)RID; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 476 | |
| 477 | // Sort arguments by their position. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 478 | if (const Argument *LA = dyn_cast<Argument>(LV)) { |
| 479 | const Argument *RA = cast<Argument>(RV); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 480 | unsigned LArgNo = LA->getArgNo(), RArgNo = RA->getArgNo(); |
| 481 | return (int)LArgNo - (int)RArgNo; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 484 | // For instructions, compare their loop depth, and their operand |
| 485 | // count. This is pretty loose. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 486 | if (const Instruction *LInst = dyn_cast<Instruction>(LV)) { |
| 487 | const Instruction *RInst = cast<Instruction>(RV); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 488 | |
| 489 | // Compare loop depths. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 490 | const BasicBlock *LParent = LInst->getParent(), |
| 491 | *RParent = RInst->getParent(); |
| 492 | if (LParent != RParent) { |
| 493 | unsigned LDepth = LI->getLoopDepth(LParent), |
| 494 | RDepth = LI->getLoopDepth(RParent); |
| 495 | if (LDepth != RDepth) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 496 | return (int)LDepth - (int)RDepth; |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 497 | } |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 498 | |
| 499 | // Compare the number of operands. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 500 | unsigned LNumOps = LInst->getNumOperands(), |
| 501 | RNumOps = RInst->getNumOperands(); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 502 | return (int)LNumOps - (int)RNumOps; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 505 | return 0; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 508 | case scConstant: { |
| 509 | const SCEVConstant *LC = cast<SCEVConstant>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 510 | const SCEVConstant *RC = cast<SCEVConstant>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 511 | |
| 512 | // Compare constant values. |
Dan Gohman | e28d792 | 2010-08-16 16:25:35 +0000 | [diff] [blame] | 513 | const APInt &LA = LC->getValue()->getValue(); |
| 514 | const APInt &RA = RC->getValue()->getValue(); |
| 515 | unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.getBitWidth(); |
Dan Gohman | 304a7a6 | 2010-07-23 21:20:52 +0000 | [diff] [blame] | 516 | if (LBitWidth != RBitWidth) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 517 | return (int)LBitWidth - (int)RBitWidth; |
| 518 | return LA.ult(RA) ? -1 : 1; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 521 | case scAddRecExpr: { |
| 522 | const SCEVAddRecExpr *LA = cast<SCEVAddRecExpr>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 523 | const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 524 | |
| 525 | // Compare addrec loop depths. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 526 | const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop(); |
| 527 | if (LLoop != RLoop) { |
| 528 | unsigned LDepth = LLoop->getLoopDepth(), |
| 529 | RDepth = RLoop->getLoopDepth(); |
| 530 | if (LDepth != RDepth) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 531 | return (int)LDepth - (int)RDepth; |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 532 | } |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 533 | |
| 534 | // Addrec complexity grows with operand count. |
| 535 | unsigned LNumOps = LA->getNumOperands(), RNumOps = RA->getNumOperands(); |
| 536 | if (LNumOps != RNumOps) |
| 537 | return (int)LNumOps - (int)RNumOps; |
| 538 | |
| 539 | // Lexicographically compare. |
| 540 | for (unsigned i = 0; i != LNumOps; ++i) { |
| 541 | long X = compare(LA->getOperand(i), RA->getOperand(i)); |
| 542 | if (X != 0) |
| 543 | return X; |
| 544 | } |
| 545 | |
| 546 | return 0; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 549 | case scAddExpr: |
| 550 | case scMulExpr: |
| 551 | case scSMaxExpr: |
| 552 | case scUMaxExpr: { |
| 553 | const SCEVNAryExpr *LC = cast<SCEVNAryExpr>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 554 | const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 555 | |
| 556 | // Lexicographically compare n-ary expressions. |
Dan Gohman | 304a7a6 | 2010-07-23 21:20:52 +0000 | [diff] [blame] | 557 | unsigned LNumOps = LC->getNumOperands(), RNumOps = RC->getNumOperands(); |
| 558 | for (unsigned i = 0; i != LNumOps; ++i) { |
| 559 | if (i >= RNumOps) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 560 | return 1; |
| 561 | long X = compare(LC->getOperand(i), RC->getOperand(i)); |
| 562 | if (X != 0) |
| 563 | return X; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 564 | } |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 565 | return (int)LNumOps - (int)RNumOps; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 568 | case scUDivExpr: { |
| 569 | const SCEVUDivExpr *LC = cast<SCEVUDivExpr>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 570 | const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 571 | |
| 572 | // Lexicographically compare udiv expressions. |
| 573 | long X = compare(LC->getLHS(), RC->getLHS()); |
| 574 | if (X != 0) |
| 575 | return X; |
| 576 | return compare(LC->getRHS(), RC->getRHS()); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 579 | case scTruncate: |
| 580 | case scZeroExtend: |
| 581 | case scSignExtend: { |
| 582 | const SCEVCastExpr *LC = cast<SCEVCastExpr>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 583 | const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 584 | |
| 585 | // Compare cast expressions by operand. |
| 586 | return compare(LC->getOperand(), RC->getOperand()); |
| 587 | } |
| 588 | |
| 589 | default: |
| 590 | break; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | llvm_unreachable("Unknown SCEV kind!"); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 594 | return 0; |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 595 | } |
| 596 | }; |
| 597 | } |
| 598 | |
| 599 | /// GroupByComplexity - Given a list of SCEV objects, order them by their |
| 600 | /// complexity, and group objects of the same complexity together by value. |
| 601 | /// When this routine is finished, we know that any duplicates in the vector are |
| 602 | /// consecutive and that complexity is monotonically increasing. |
| 603 | /// |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 604 | /// 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] | 605 | /// results from this routine. In other words, we don't want the results of |
| 606 | /// this to depend on where the addresses of various SCEV objects happened to |
| 607 | /// land in memory. |
| 608 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 609 | static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops, |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 610 | LoopInfo *LI) { |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 611 | if (Ops.size() < 2) return; // Noop |
| 612 | if (Ops.size() == 2) { |
| 613 | // This is the common case, which also happens to be trivially simple. |
| 614 | // Special case it. |
Dan Gohman | c6a8e99 | 2010-08-29 15:07:13 +0000 | [diff] [blame] | 615 | const SCEV *&LHS = Ops[0], *&RHS = Ops[1]; |
| 616 | if (SCEVComplexityCompare(LI)(RHS, LHS)) |
| 617 | std::swap(LHS, RHS); |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 618 | return; |
| 619 | } |
| 620 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 621 | // Do the rough sort by complexity. |
| 622 | std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI)); |
| 623 | |
| 624 | // Now that we are sorted by complexity, group elements of the same |
| 625 | // complexity. Note that this is, at worst, N^2, but the vector is likely to |
| 626 | // be extremely short in practice. Note that we take this approach because we |
| 627 | // do not want to depend on the addresses of the objects we are grouping. |
| 628 | for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { |
| 629 | const SCEV *S = Ops[i]; |
| 630 | unsigned Complexity = S->getSCEVType(); |
| 631 | |
| 632 | // If there are any objects of the same complexity and same value as this |
| 633 | // one, group them. |
| 634 | for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { |
| 635 | if (Ops[j] == S) { // Found a duplicate. |
| 636 | // Move it to immediately after i'th element. |
| 637 | std::swap(Ops[i+1], Ops[j]); |
| 638 | ++i; // no need to rescan it. |
| 639 | if (i == e-2) return; // Done! |
| 640 | } |
| 641 | } |
| 642 | } |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 645 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 646 | |
| 647 | //===----------------------------------------------------------------------===// |
| 648 | // Simple SCEV method implementations |
| 649 | //===----------------------------------------------------------------------===// |
| 650 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 651 | /// BinomialCoefficient - Compute BC(It, K). The result has width W. |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 652 | /// Assume, K > 0. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 653 | static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K, |
Dan Gohman | c2b015e | 2009-07-21 00:38:55 +0000 | [diff] [blame] | 654 | ScalarEvolution &SE, |
| 655 | const Type* ResultTy) { |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 656 | // Handle the simplest case efficiently. |
| 657 | if (K == 1) |
| 658 | return SE.getTruncateOrZeroExtend(It, ResultTy); |
| 659 | |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 660 | // We are using the following formula for BC(It, K): |
| 661 | // |
| 662 | // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! |
| 663 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 664 | // Suppose, W is the bitwidth of the return value. We must be prepared for |
| 665 | // overflow. Hence, we must assure that the result of our computation is |
| 666 | // equal to the accurate one modulo 2^W. Unfortunately, division isn't |
| 667 | // safe in modular arithmetic. |
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 | // 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] | 670 | // 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] | 671 | // K! (i.e. trailing zeros in the binary representation of K!), and ^ is |
| 672 | // exponentiation: |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 673 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 674 | // 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] | 675 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 676 | // This formula is trivially equivalent to the previous formula. However, |
| 677 | // this formula can be implemented much more efficiently. The trick is that |
| 678 | // K! / 2^T is odd, and exact division by an odd number *is* safe in modular |
| 679 | // arithmetic. To do exact division in modular arithmetic, all we have |
| 680 | // to do is multiply by the inverse. Therefore, this step can be done at |
| 681 | // width W. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 682 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 683 | // The next issue is how to safely do the division by 2^T. The way this |
| 684 | // is done is by doing the multiplication step at a width of at least W + T |
| 685 | // bits. This way, the bottom W+T bits of the product are accurate. Then, |
| 686 | // when we perform the division by 2^T (which is equivalent to a right shift |
| 687 | // by T), the bottom W bits are accurate. Extra bits are okay; they'll get |
| 688 | // truncated out after the division by 2^T. |
| 689 | // |
| 690 | // In comparison to just directly using the first formula, this technique |
| 691 | // is much more efficient; using the first formula requires W * K bits, |
| 692 | // but this formula less than W + K bits. Also, the first formula requires |
| 693 | // a division step, whereas this formula only requires multiplies and shifts. |
| 694 | // |
| 695 | // It doesn't matter whether the subtraction step is done in the calculation |
| 696 | // width or the input iteration count's width; if the subtraction overflows, |
| 697 | // the result must be zero anyway. We prefer here to do it in the width of |
| 698 | // the induction variable because it helps a lot for certain cases; CodeGen |
| 699 | // isn't smart enough to ignore the overflow, which leads to much less |
| 700 | // efficient code if the width of the subtraction is wider than the native |
| 701 | // register width. |
| 702 | // |
| 703 | // (It's possible to not widen at all by pulling out factors of 2 before |
| 704 | // the multiplication; for example, K=2 can be calculated as |
| 705 | // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires |
| 706 | // extra arithmetic, so it's not an obvious win, and it gets |
| 707 | // much more complicated for K > 3.) |
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 | // Protection from insane SCEVs; this bound is conservative, |
| 710 | // but it probably doesn't matter. |
| 711 | if (K > 1000) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 712 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 713 | |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 714 | unsigned W = SE.getTypeSizeInBits(ResultTy); |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 715 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 716 | // Calculate K! / 2^T and T; we divide out the factors of two before |
| 717 | // multiplying for calculating K! / 2^T to avoid overflow. |
| 718 | // Other overflow doesn't matter because we only care about the bottom |
| 719 | // W bits of the result. |
| 720 | APInt OddFactorial(W, 1); |
| 721 | unsigned T = 1; |
| 722 | for (unsigned i = 3; i <= K; ++i) { |
| 723 | APInt Mult(W, i); |
| 724 | unsigned TwoFactors = Mult.countTrailingZeros(); |
| 725 | T += TwoFactors; |
| 726 | Mult = Mult.lshr(TwoFactors); |
| 727 | OddFactorial *= Mult; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 728 | } |
Nick Lewycky | 6f8abf9 | 2008-06-13 04:38:55 +0000 | [diff] [blame] | 729 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 730 | // We need at least W + T bits for the multiplication step |
Nick Lewycky | 237d873 | 2009-01-25 08:16:27 +0000 | [diff] [blame] | 731 | unsigned CalculationBits = W + T; |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 732 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 733 | // Calculate 2^T, at width T+W. |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 734 | APInt DivFactor = APInt(CalculationBits, 1).shl(T); |
| 735 | |
| 736 | // Calculate the multiplicative inverse of K! / 2^T; |
| 737 | // this multiplication factor will perform the exact division by |
| 738 | // K! / 2^T. |
| 739 | APInt Mod = APInt::getSignedMinValue(W+1); |
| 740 | APInt MultiplyFactor = OddFactorial.zext(W+1); |
| 741 | MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); |
| 742 | MultiplyFactor = MultiplyFactor.trunc(W); |
| 743 | |
| 744 | // Calculate the product, at width T+W |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 745 | const IntegerType *CalculationTy = IntegerType::get(SE.getContext(), |
| 746 | CalculationBits); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 747 | const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 748 | for (unsigned i = 1; i != K; ++i) { |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 749 | const SCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i)); |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 750 | Dividend = SE.getMulExpr(Dividend, |
| 751 | SE.getTruncateOrZeroExtend(S, CalculationTy)); |
| 752 | } |
| 753 | |
| 754 | // Divide by 2^T |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 755 | const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 756 | |
| 757 | // Truncate the result, and divide by K! / 2^T. |
| 758 | |
| 759 | return SE.getMulExpr(SE.getConstant(MultiplyFactor), |
| 760 | SE.getTruncateOrZeroExtend(DivResult, ResultTy)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 763 | /// evaluateAtIteration - Return the value of this chain of recurrences at |
| 764 | /// the specified iteration number. We can evaluate this recurrence by |
| 765 | /// multiplying each element in the chain by the binomial coefficient |
| 766 | /// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as: |
| 767 | /// |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 768 | /// 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] | 769 | /// |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 770 | /// where BC(It, k) stands for binomial coefficient. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 771 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 772 | const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It, |
Dan Gohman | c2b015e | 2009-07-21 00:38:55 +0000 | [diff] [blame] | 773 | ScalarEvolution &SE) const { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 774 | const SCEV *Result = getStart(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 775 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 776 | // The computation is correct in the face of overflow provided that the |
| 777 | // multiplication is performed _after_ the evaluation of the binomial |
| 778 | // coefficient. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 779 | const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType()); |
Nick Lewycky | cb8f1b5 | 2008-10-13 03:58:02 +0000 | [diff] [blame] | 780 | if (isa<SCEVCouldNotCompute>(Coeff)) |
| 781 | return Coeff; |
| 782 | |
| 783 | Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 784 | } |
| 785 | return Result; |
| 786 | } |
| 787 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 788 | //===----------------------------------------------------------------------===// |
| 789 | // SCEV Expression folder implementations |
| 790 | //===----------------------------------------------------------------------===// |
| 791 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 792 | const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, |
Dan Gohman | f5074ec | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 793 | const Type *Ty) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 794 | assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 795 | "This is not a truncating conversion!"); |
Dan Gohman | 10b9479 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 796 | assert(isSCEVable(Ty) && |
| 797 | "This is not a conversion to a SCEVable type!"); |
| 798 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 799 | |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 800 | FoldingSetNodeID ID; |
| 801 | ID.AddInteger(scTruncate); |
| 802 | ID.AddPointer(Op); |
| 803 | ID.AddPointer(Ty); |
| 804 | void *IP = 0; |
| 805 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 806 | |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 807 | // Fold if the operand is constant. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 808 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
Dan Gohman | b8be8b7 | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 809 | return getConstant( |
Dan Gohman | 1faa882 | 2010-06-24 16:33:38 +0000 | [diff] [blame] | 810 | cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), |
| 811 | getEffectiveSCEVType(Ty)))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 812 | |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 813 | // trunc(trunc(x)) --> trunc(x) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 814 | if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 815 | return getTruncateExpr(ST->getOperand(), Ty); |
| 816 | |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 817 | // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 818 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 819 | return getTruncateOrSignExtend(SS->getOperand(), Ty); |
| 820 | |
| 821 | // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 822 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 823 | return getTruncateOrZeroExtend(SZ->getOperand(), Ty); |
| 824 | |
Nick Lewycky | 30aa8b1 | 2011-01-19 16:59:46 +0000 | [diff] [blame] | 825 | // trunc(x1+x2+...+xN) --> trunc(x1)+trunc(x2)+...+trunc(xN) if we can |
| 826 | // eliminate all the truncates. |
| 827 | if (const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Op)) { |
| 828 | SmallVector<const SCEV *, 4> Operands; |
| 829 | bool hasTrunc = false; |
| 830 | for (unsigned i = 0, e = SA->getNumOperands(); i != e && !hasTrunc; ++i) { |
| 831 | const SCEV *S = getTruncateExpr(SA->getOperand(i), Ty); |
| 832 | hasTrunc = isa<SCEVTruncateExpr>(S); |
| 833 | Operands.push_back(S); |
| 834 | } |
| 835 | if (!hasTrunc) |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 836 | return getAddExpr(Operands); |
Nick Lewycky | e19b7b8 | 2011-01-26 08:40:22 +0000 | [diff] [blame] | 837 | UniqueSCEVs.FindNodeOrInsertPos(ID, IP); // Mutates IP, returns NULL. |
Nick Lewycky | 30aa8b1 | 2011-01-19 16:59:46 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Nick Lewycky | 5c6fc1c | 2011-01-19 18:56:00 +0000 | [diff] [blame] | 840 | // trunc(x1*x2*...*xN) --> trunc(x1)*trunc(x2)*...*trunc(xN) if we can |
| 841 | // eliminate all the truncates. |
| 842 | if (const SCEVMulExpr *SM = dyn_cast<SCEVMulExpr>(Op)) { |
| 843 | SmallVector<const SCEV *, 4> Operands; |
| 844 | bool hasTrunc = false; |
| 845 | for (unsigned i = 0, e = SM->getNumOperands(); i != e && !hasTrunc; ++i) { |
| 846 | const SCEV *S = getTruncateExpr(SM->getOperand(i), Ty); |
| 847 | hasTrunc = isa<SCEVTruncateExpr>(S); |
| 848 | Operands.push_back(S); |
| 849 | } |
| 850 | if (!hasTrunc) |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 851 | return getMulExpr(Operands); |
Nick Lewycky | e19b7b8 | 2011-01-26 08:40:22 +0000 | [diff] [blame] | 852 | UniqueSCEVs.FindNodeOrInsertPos(ID, IP); // Mutates IP, returns NULL. |
Nick Lewycky | 5c6fc1c | 2011-01-19 18:56:00 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Dan Gohman | 6864db6 | 2009-06-18 16:24:47 +0000 | [diff] [blame] | 855 | // 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] | 856 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 857 | SmallVector<const SCEV *, 4> Operands; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 858 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 859 | Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty)); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 860 | return getAddRecExpr(Operands, AddRec->getLoop(), SCEV::FlagAnyWrap); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Dan Gohman | f53462d | 2010-07-15 20:02:11 +0000 | [diff] [blame] | 863 | // As a special case, fold trunc(undef) to undef. We don't want to |
| 864 | // know too much about SCEVUnknowns, but this special case is handy |
| 865 | // and harmless. |
| 866 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Op)) |
| 867 | if (isa<UndefValue>(U->getValue())) |
| 868 | return getSCEV(UndefValue::get(Ty)); |
| 869 | |
Dan Gohman | 420ab91 | 2010-06-25 18:47:08 +0000 | [diff] [blame] | 870 | // The cast wasn't folded; create an explicit cast node. We can reuse |
| 871 | // the existing insert position since if we get here, we won't have |
| 872 | // made any changes which would invalidate it. |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 873 | SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator), |
| 874 | Op, Ty); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 875 | UniqueSCEVs.InsertNode(S, IP); |
| 876 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 879 | const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op, |
Dan Gohman | f5074ec | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 880 | const Type *Ty) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 881 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | 8170a68 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 882 | "This is not an extending conversion!"); |
Dan Gohman | 10b9479 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 883 | assert(isSCEVable(Ty) && |
| 884 | "This is not a conversion to a SCEVable type!"); |
| 885 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | 8170a68 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 886 | |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 887 | // Fold if the operand is constant. |
Dan Gohman | eaf6cf2 | 2010-06-24 16:47:03 +0000 | [diff] [blame] | 888 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| 889 | return getConstant( |
| 890 | cast<ConstantInt>(ConstantExpr::getZExt(SC->getValue(), |
| 891 | getEffectiveSCEVType(Ty)))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 892 | |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 893 | // zext(zext(x)) --> zext(x) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 894 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 895 | return getZeroExtendExpr(SZ->getOperand(), Ty); |
| 896 | |
Dan Gohman | 69fbc7f | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 897 | // Before doing any expensive analysis, check to see if we've already |
| 898 | // computed a SCEV for this Op and Ty. |
| 899 | FoldingSetNodeID ID; |
| 900 | ID.AddInteger(scZeroExtend); |
| 901 | ID.AddPointer(Op); |
| 902 | ID.AddPointer(Ty); |
| 903 | void *IP = 0; |
| 904 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 905 | |
Nick Lewycky | 630d85a | 2011-01-23 06:20:19 +0000 | [diff] [blame] | 906 | // zext(trunc(x)) --> zext(x) or x or trunc(x) |
| 907 | if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) { |
| 908 | // It's possible the bits taken off by the truncate were all zero bits. If |
| 909 | // so, we should be able to simplify this further. |
| 910 | const SCEV *X = ST->getOperand(); |
| 911 | ConstantRange CR = getUnsignedRange(X); |
Nick Lewycky | 630d85a | 2011-01-23 06:20:19 +0000 | [diff] [blame] | 912 | unsigned TruncBits = getTypeSizeInBits(ST->getType()); |
| 913 | unsigned NewBits = getTypeSizeInBits(Ty); |
| 914 | if (CR.truncate(TruncBits).zeroExtend(NewBits).contains( |
Nick Lewycky | 76167af | 2011-01-23 20:06:05 +0000 | [diff] [blame] | 915 | CR.zextOrTrunc(NewBits))) |
| 916 | return getTruncateOrZeroExtend(X, Ty); |
Nick Lewycky | 630d85a | 2011-01-23 06:20:19 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 919 | // 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] | 920 | // 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] | 921 | // operands (often constants). This allows analysis of something like |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 922 | // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 923 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 924 | if (AR->isAffine()) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 925 | const SCEV *Start = AR->getStart(); |
| 926 | const SCEV *Step = AR->getStepRecurrence(*this); |
| 927 | unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
| 928 | const Loop *L = AR->getLoop(); |
| 929 | |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 930 | // If we have special knowledge that this addrec won't overflow, |
| 931 | // we don't need to do any further analysis. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 932 | if (AR->getNoWrapFlags(SCEV::FlagNUW)) |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 933 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 934 | getZeroExtendExpr(Step, Ty), |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 935 | L, AR->getNoWrapFlags()); |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 936 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 937 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 938 | // Note that this serves two purposes: It filters out loops that are |
| 939 | // simply not analyzable, and it covers the case where this code is |
| 940 | // being called from within backedge-taken count analysis, such that |
| 941 | // attempting to ask for the backedge-taken count would likely result |
| 942 | // in infinite recursion. In the later case, the analysis code will |
| 943 | // cope with a conservative value, and it will take care to purge |
| 944 | // that value once it has finished. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 945 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 946 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | f0aa485 | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 947 | // Manually compute the final value for AR, checking for |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 948 | // overflow. |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 949 | |
| 950 | // Check whether the backedge-taken count can be losslessly casted to |
| 951 | // the addrec's type. The count is always unsigned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 952 | const SCEV *CastedMaxBECount = |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 953 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 954 | const SCEV *RecastedMaxBECount = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 955 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 956 | if (MaxBECount == RecastedMaxBECount) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 957 | const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 958 | // Check whether Start+Step*MaxBECount has no unsigned overflow. |
Dan Gohman | 8f767d9 | 2010-02-24 19:31:06 +0000 | [diff] [blame] | 959 | const SCEV *ZMul = getMulExpr(CastedMaxBECount, Step); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 960 | const SCEV *Add = getAddExpr(Start, ZMul); |
| 961 | const SCEV *OperandExtendedAdd = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 962 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 963 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 964 | getZeroExtendExpr(Step, WideTy))); |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 965 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) { |
| 966 | // Cache knowledge of AR NUW, which is propagated to this AddRec. |
| 967 | const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW); |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 968 | // Return the expression with the addrec on the outside. |
| 969 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 970 | getZeroExtendExpr(Step, Ty), |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 971 | L, AR->getNoWrapFlags()); |
| 972 | } |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 973 | // Similar to above, only this time treat the step value as signed. |
| 974 | // This covers loops that count down. |
Dan Gohman | 8f767d9 | 2010-02-24 19:31:06 +0000 | [diff] [blame] | 975 | const SCEV *SMul = getMulExpr(CastedMaxBECount, Step); |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 976 | Add = getAddExpr(Start, SMul); |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 977 | OperandExtendedAdd = |
| 978 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 979 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 980 | getSignExtendExpr(Step, WideTy))); |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 981 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) { |
| 982 | // Cache knowledge of AR NW, which is propagated to this AddRec. |
| 983 | // Negative step causes unsigned wrap, but it still can't self-wrap. |
| 984 | const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW); |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 985 | // Return the expression with the addrec on the outside. |
| 986 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 987 | getSignExtendExpr(Step, Ty), |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 988 | L, AR->getNoWrapFlags()); |
| 989 | } |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | // If the backedge is guarded by a comparison with the pre-inc value |
| 993 | // the addrec is safe. Also, if the entry is guarded by a comparison |
| 994 | // with the start value and the backedge is guarded by a comparison |
| 995 | // with the post-inc value, the addrec is safe. |
| 996 | if (isKnownPositive(Step)) { |
| 997 | const SCEV *N = getConstant(APInt::getMinValue(BitWidth) - |
| 998 | getUnsignedRange(Step).getUnsignedMax()); |
| 999 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) || |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 1000 | (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) && |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1001 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 1002 | AR->getPostIncExpr(*this), N))) { |
| 1003 | // Cache knowledge of AR NUW, which is propagated to this AddRec. |
| 1004 | const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1005 | // Return the expression with the addrec on the outside. |
| 1006 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 1007 | getZeroExtendExpr(Step, Ty), |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 1008 | L, AR->getNoWrapFlags()); |
| 1009 | } |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1010 | } else if (isKnownNegative(Step)) { |
| 1011 | const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) - |
| 1012 | getSignedRange(Step).getSignedMin()); |
Dan Gohman | c0ed009 | 2010-05-04 01:11:15 +0000 | [diff] [blame] | 1013 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) || |
| 1014 | (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) && |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1015 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 1016 | AR->getPostIncExpr(*this), N))) { |
| 1017 | // Cache knowledge of AR NW, which is propagated to this AddRec. |
| 1018 | // Negative step causes unsigned wrap, but it still can't self-wrap. |
| 1019 | const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW); |
| 1020 | // Return the expression with the addrec on the outside. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1021 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 1022 | getSignExtendExpr(Step, Ty), |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 1023 | L, AR->getNoWrapFlags()); |
| 1024 | } |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1028 | |
Dan Gohman | 69fbc7f | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 1029 | // The cast wasn't folded; create an explicit cast node. |
| 1030 | // Recompute the insert position, as it may have been invalidated. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1031 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1032 | SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator), |
| 1033 | Op, Ty); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1034 | UniqueSCEVs.InsertNode(S, IP); |
| 1035 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Andrew Trick | b1ce4c0 | 2011-05-31 21:17:47 +0000 | [diff] [blame^] | 1038 | // Get the limit of a recurrence such that incrementing by Step cannot cause |
| 1039 | // signed overflow as long as the value of the recurrence within the loop does |
| 1040 | // not exceed this limit before incrementing. |
| 1041 | static const SCEV *getOverflowLimitForStep(const SCEV *Step, |
| 1042 | ICmpInst::Predicate *Pred, |
| 1043 | ScalarEvolution *SE) { |
| 1044 | unsigned BitWidth = SE->getTypeSizeInBits(Step->getType()); |
| 1045 | if (SE->isKnownPositive(Step)) { |
| 1046 | *Pred = ICmpInst::ICMP_SLT; |
| 1047 | return SE->getConstant(APInt::getSignedMinValue(BitWidth) - |
| 1048 | SE->getSignedRange(Step).getSignedMax()); |
| 1049 | } |
| 1050 | if (SE->isKnownNegative(Step)) { |
| 1051 | *Pred = ICmpInst::ICMP_SGT; |
| 1052 | return SE->getConstant(APInt::getSignedMaxValue(BitWidth) - |
| 1053 | SE->getSignedRange(Step).getSignedMin()); |
| 1054 | } |
| 1055 | return 0; |
| 1056 | } |
| 1057 | |
| 1058 | // The recurrence AR has been shown to have no signed wrap. Typically, if we can |
| 1059 | // prove NSW for AR, then we can just as easily prove NSW for its preincrement |
| 1060 | // or postincrement sibling. This allows normalizing a sign extended AddRec as |
| 1061 | // such: {sext(Step + Start),+,Step} => {(Step + sext(Start),+,Step} As a |
| 1062 | // result, the expression "Step + sext(PreIncAR)" is congruent with |
| 1063 | // "sext(PostIncAR)" |
| 1064 | static const SCEV *getPreStartForSignExtend(const SCEVAddRecExpr *AR, |
| 1065 | const Type *Ty, |
| 1066 | ScalarEvolution *SE) { |
| 1067 | const Loop *L = AR->getLoop(); |
| 1068 | const SCEV *Start = AR->getStart(); |
| 1069 | const SCEV *Step = AR->getStepRecurrence(*SE); |
| 1070 | |
| 1071 | // Check for a simple looking step prior to loop entry. |
| 1072 | const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Start); |
| 1073 | if (!SA || SA->getNumOperands() != 2 || SA->getOperand(0) != Step) |
| 1074 | return 0; |
| 1075 | |
| 1076 | // This is a postinc AR. Check for overflow on the preinc recurrence using the |
| 1077 | // same three conditions that getSignExtendedExpr checks. |
| 1078 | |
| 1079 | // 1. NSW flags on the step increment. |
| 1080 | const SCEV *PreStart = SA->getOperand(1); |
| 1081 | const SCEVAddRecExpr *PreAR = dyn_cast<SCEVAddRecExpr>( |
| 1082 | SE->getAddRecExpr(PreStart, Step, L, SCEV::FlagAnyWrap)); |
| 1083 | |
| 1084 | if (PreAR && PreAR->getNoWrapFlags(SCEV::FlagNSW)) { |
| 1085 | return PreStart; |
| 1086 | } |
| 1087 | |
| 1088 | // 2. Direct overflow check on the step operation's expression. |
| 1089 | unsigned BitWidth = SE->getTypeSizeInBits(AR->getType()); |
| 1090 | const Type *WideTy = IntegerType::get(SE->getContext(), BitWidth * 2); |
| 1091 | const SCEV *OperandExtendedStart = |
| 1092 | SE->getAddExpr(SE->getSignExtendExpr(PreStart, WideTy), |
| 1093 | SE->getSignExtendExpr(Step, WideTy)); |
| 1094 | if (SE->getSignExtendExpr(Start, WideTy) == OperandExtendedStart) { |
| 1095 | // Cache knowledge of PreAR NSW. |
| 1096 | if (PreAR) |
| 1097 | const_cast<SCEVAddRecExpr *>(PreAR)->setNoWrapFlags(SCEV::FlagNSW); |
| 1098 | // FIXME: this optimization needs a unit test |
| 1099 | DEBUG(dbgs() << "SCEV: untested prestart overflow check\n"); |
| 1100 | return PreStart; |
| 1101 | } |
| 1102 | |
| 1103 | // 3. Loop precondition. |
| 1104 | ICmpInst::Predicate Pred; |
| 1105 | const SCEV *OverflowLimit = getOverflowLimitForStep(Step, &Pred, SE); |
| 1106 | |
| 1107 | if (SE->isLoopEntryGuardedByCond(L, Pred, PreStart, OverflowLimit)) { |
| 1108 | return PreStart; |
| 1109 | } |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | // Get the normalized sign-extended expression for this AddRec's Start. |
| 1114 | static const SCEV *getSignExtendAddRecStart(const SCEVAddRecExpr *AR, |
| 1115 | const Type *Ty, |
| 1116 | ScalarEvolution *SE) { |
| 1117 | const SCEV *PreStart = getPreStartForSignExtend(AR, Ty, SE); |
| 1118 | if (!PreStart) |
| 1119 | return SE->getSignExtendExpr(AR->getStart(), Ty); |
| 1120 | |
| 1121 | return SE->getAddExpr(SE->getSignExtendExpr(AR->getStepRecurrence(*SE), Ty), |
| 1122 | SE->getSignExtendExpr(PreStart, Ty)); |
| 1123 | } |
| 1124 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1125 | const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op, |
Dan Gohman | f5074ec | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 1126 | const Type *Ty) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 1127 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 1128 | "This is not an extending conversion!"); |
Dan Gohman | 10b9479 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 1129 | assert(isSCEVable(Ty) && |
| 1130 | "This is not a conversion to a SCEVable type!"); |
| 1131 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 1132 | |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 1133 | // Fold if the operand is constant. |
Dan Gohman | eaf6cf2 | 2010-06-24 16:47:03 +0000 | [diff] [blame] | 1134 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| 1135 | return getConstant( |
| 1136 | cast<ConstantInt>(ConstantExpr::getSExt(SC->getValue(), |
| 1137 | getEffectiveSCEVType(Ty)))); |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 1138 | |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 1139 | // sext(sext(x)) --> sext(x) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1140 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 1141 | return getSignExtendExpr(SS->getOperand(), Ty); |
| 1142 | |
Nick Lewycky | 73f565e | 2011-01-19 15:56:12 +0000 | [diff] [blame] | 1143 | // sext(zext(x)) --> zext(x) |
| 1144 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
| 1145 | return getZeroExtendExpr(SZ->getOperand(), Ty); |
| 1146 | |
Dan Gohman | 69fbc7f | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 1147 | // Before doing any expensive analysis, check to see if we've already |
| 1148 | // computed a SCEV for this Op and Ty. |
| 1149 | FoldingSetNodeID ID; |
| 1150 | ID.AddInteger(scSignExtend); |
| 1151 | ID.AddPointer(Op); |
| 1152 | ID.AddPointer(Ty); |
| 1153 | void *IP = 0; |
| 1154 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1155 | |
Nick Lewycky | 9b8d2c2 | 2011-01-22 22:06:21 +0000 | [diff] [blame] | 1156 | // If the input value is provably positive, build a zext instead. |
| 1157 | if (isKnownNonNegative(Op)) |
| 1158 | return getZeroExtendExpr(Op, Ty); |
| 1159 | |
Nick Lewycky | 630d85a | 2011-01-23 06:20:19 +0000 | [diff] [blame] | 1160 | // sext(trunc(x)) --> sext(x) or x or trunc(x) |
| 1161 | if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) { |
| 1162 | // It's possible the bits taken off by the truncate were all sign bits. If |
| 1163 | // so, we should be able to simplify this further. |
| 1164 | const SCEV *X = ST->getOperand(); |
| 1165 | ConstantRange CR = getSignedRange(X); |
Nick Lewycky | 630d85a | 2011-01-23 06:20:19 +0000 | [diff] [blame] | 1166 | unsigned TruncBits = getTypeSizeInBits(ST->getType()); |
| 1167 | unsigned NewBits = getTypeSizeInBits(Ty); |
| 1168 | if (CR.truncate(TruncBits).signExtend(NewBits).contains( |
Nick Lewycky | 76167af | 2011-01-23 20:06:05 +0000 | [diff] [blame] | 1169 | CR.sextOrTrunc(NewBits))) |
| 1170 | return getTruncateOrSignExtend(X, Ty); |
Nick Lewycky | 630d85a | 2011-01-23 06:20:19 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1173 | // 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] | 1174 | // 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] | 1175 | // operands (often constants). This allows analysis of something like |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 1176 | // this: for (signed char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1177 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1178 | if (AR->isAffine()) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1179 | const SCEV *Start = AR->getStart(); |
| 1180 | const SCEV *Step = AR->getStepRecurrence(*this); |
| 1181 | unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
| 1182 | const Loop *L = AR->getLoop(); |
| 1183 | |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 1184 | // If we have special knowledge that this addrec won't overflow, |
| 1185 | // we don't need to do any further analysis. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1186 | if (AR->getNoWrapFlags(SCEV::FlagNSW)) |
Andrew Trick | b1ce4c0 | 2011-05-31 21:17:47 +0000 | [diff] [blame^] | 1187 | return getAddRecExpr(getSignExtendAddRecStart(AR, Ty, this), |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 1188 | getSignExtendExpr(Step, Ty), |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 1189 | L, SCEV::FlagNSW); |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 1190 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1191 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 1192 | // Note that this serves two purposes: It filters out loops that are |
| 1193 | // simply not analyzable, and it covers the case where this code is |
| 1194 | // being called from within backedge-taken count analysis, such that |
| 1195 | // attempting to ask for the backedge-taken count would likely result |
| 1196 | // in infinite recursion. In the later case, the analysis code will |
| 1197 | // cope with a conservative value, and it will take care to purge |
| 1198 | // that value once it has finished. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1199 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 1200 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | f0aa485 | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 1201 | // Manually compute the final value for AR, checking for |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 1202 | // overflow. |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1203 | |
| 1204 | // Check whether the backedge-taken count can be losslessly casted to |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 1205 | // the addrec's type. The count is always unsigned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1206 | const SCEV *CastedMaxBECount = |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 1207 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1208 | const SCEV *RecastedMaxBECount = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 1209 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 1210 | if (MaxBECount == RecastedMaxBECount) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1211 | const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 1212 | // Check whether Start+Step*MaxBECount has no signed overflow. |
Dan Gohman | 8f767d9 | 2010-02-24 19:31:06 +0000 | [diff] [blame] | 1213 | const SCEV *SMul = getMulExpr(CastedMaxBECount, Step); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1214 | const SCEV *Add = getAddExpr(Start, SMul); |
| 1215 | const SCEV *OperandExtendedAdd = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 1216 | getAddExpr(getSignExtendExpr(Start, WideTy), |
| 1217 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 1218 | getSignExtendExpr(Step, WideTy))); |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 1219 | if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) { |
| 1220 | // Cache knowledge of AR NSW, which is propagated to this AddRec. |
| 1221 | const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW); |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 1222 | // Return the expression with the addrec on the outside. |
Andrew Trick | b1ce4c0 | 2011-05-31 21:17:47 +0000 | [diff] [blame^] | 1223 | return getAddRecExpr(getSignExtendAddRecStart(AR, Ty, this), |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 1224 | getSignExtendExpr(Step, Ty), |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 1225 | L, AR->getNoWrapFlags()); |
| 1226 | } |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 1227 | // Similar to above, only this time treat the step value as unsigned. |
| 1228 | // This covers loops that count up with an unsigned step. |
Dan Gohman | 8f767d9 | 2010-02-24 19:31:06 +0000 | [diff] [blame] | 1229 | const SCEV *UMul = getMulExpr(CastedMaxBECount, Step); |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 1230 | Add = getAddExpr(Start, UMul); |
| 1231 | OperandExtendedAdd = |
Dan Gohman | 19378d6 | 2009-07-25 16:03:30 +0000 | [diff] [blame] | 1232 | getAddExpr(getSignExtendExpr(Start, WideTy), |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 1233 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 1234 | getZeroExtendExpr(Step, WideTy))); |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 1235 | if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) { |
| 1236 | // Cache knowledge of AR NSW, which is propagated to this AddRec. |
| 1237 | const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW); |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 1238 | // Return the expression with the addrec on the outside. |
Andrew Trick | b1ce4c0 | 2011-05-31 21:17:47 +0000 | [diff] [blame^] | 1239 | return getAddRecExpr(getSignExtendAddRecStart(AR, Ty, this), |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 1240 | getZeroExtendExpr(Step, Ty), |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 1241 | L, AR->getNoWrapFlags()); |
| 1242 | } |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | // If the backedge is guarded by a comparison with the pre-inc value |
| 1246 | // the addrec is safe. Also, if the entry is guarded by a comparison |
| 1247 | // with the start value and the backedge is guarded by a comparison |
| 1248 | // with the post-inc value, the addrec is safe. |
Andrew Trick | b1ce4c0 | 2011-05-31 21:17:47 +0000 | [diff] [blame^] | 1249 | ICmpInst::Predicate Pred; |
| 1250 | const SCEV *OverflowLimit = getOverflowLimitForStep(Step, &Pred, this); |
| 1251 | if (OverflowLimit && |
| 1252 | (isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) || |
| 1253 | (isLoopEntryGuardedByCond(L, Pred, Start, OverflowLimit) && |
| 1254 | isLoopBackedgeGuardedByCond(L, Pred, AR->getPostIncExpr(*this), |
| 1255 | OverflowLimit)))) { |
| 1256 | // Cache knowledge of AR NSW, then propagate NSW to the wide AddRec. |
| 1257 | const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW); |
| 1258 | return getAddRecExpr(getSignExtendAddRecStart(AR, Ty, this), |
| 1259 | getSignExtendExpr(Step, Ty), |
| 1260 | L, AR->getNoWrapFlags()); |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | } |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 1264 | |
Dan Gohman | 69fbc7f | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 1265 | // The cast wasn't folded; create an explicit cast node. |
| 1266 | // Recompute the insert position, as it may have been invalidated. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1267 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1268 | SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator), |
| 1269 | Op, Ty); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1270 | UniqueSCEVs.InsertNode(S, IP); |
| 1271 | return S; |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1274 | /// getAnyExtendExpr - Return a SCEV for the given operand extended with |
| 1275 | /// unspecified bits out to the given type. |
| 1276 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1277 | const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op, |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 1278 | const Type *Ty) { |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1279 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
| 1280 | "This is not an extending conversion!"); |
| 1281 | assert(isSCEVable(Ty) && |
| 1282 | "This is not a conversion to a SCEVable type!"); |
| 1283 | Ty = getEffectiveSCEVType(Ty); |
| 1284 | |
| 1285 | // Sign-extend negative constants. |
| 1286 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| 1287 | if (SC->getValue()->getValue().isNegative()) |
| 1288 | return getSignExtendExpr(Op, Ty); |
| 1289 | |
| 1290 | // Peel off a truncate cast. |
| 1291 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1292 | const SCEV *NewOp = T->getOperand(); |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1293 | if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty)) |
| 1294 | return getAnyExtendExpr(NewOp, Ty); |
| 1295 | return getTruncateOrNoop(NewOp, Ty); |
| 1296 | } |
| 1297 | |
| 1298 | // Next try a zext cast. If the cast is folded, use it. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1299 | const SCEV *ZExt = getZeroExtendExpr(Op, Ty); |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1300 | if (!isa<SCEVZeroExtendExpr>(ZExt)) |
| 1301 | return ZExt; |
| 1302 | |
| 1303 | // Next try a sext cast. If the cast is folded, use it. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1304 | const SCEV *SExt = getSignExtendExpr(Op, Ty); |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1305 | if (!isa<SCEVSignExtendExpr>(SExt)) |
| 1306 | return SExt; |
| 1307 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1308 | // Force the cast to be folded into the operands of an addrec. |
| 1309 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) { |
| 1310 | SmallVector<const SCEV *, 4> Ops; |
| 1311 | for (SCEVAddRecExpr::op_iterator I = AR->op_begin(), E = AR->op_end(); |
| 1312 | I != E; ++I) |
| 1313 | Ops.push_back(getAnyExtendExpr(*I, Ty)); |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 1314 | return getAddRecExpr(Ops, AR->getLoop(), SCEV::FlagNW); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Dan Gohman | f53462d | 2010-07-15 20:02:11 +0000 | [diff] [blame] | 1317 | // As a special case, fold anyext(undef) to undef. We don't want to |
| 1318 | // know too much about SCEVUnknowns, but this special case is handy |
| 1319 | // and harmless. |
| 1320 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Op)) |
| 1321 | if (isa<UndefValue>(U->getValue())) |
| 1322 | return getSCEV(UndefValue::get(Ty)); |
| 1323 | |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1324 | // If the expression is obviously signed, use the sext cast value. |
| 1325 | if (isa<SCEVSMaxExpr>(Op)) |
| 1326 | return SExt; |
| 1327 | |
| 1328 | // Absent any other information, use the zext cast value. |
| 1329 | return ZExt; |
| 1330 | } |
| 1331 | |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1332 | /// CollectAddOperandsWithScales - Process the given Ops list, which is |
| 1333 | /// a list of operands to be added under the given scale, update the given |
| 1334 | /// map. This is a helper function for getAddRecExpr. As an example of |
| 1335 | /// what it does, given a sequence of operands that would form an add |
| 1336 | /// expression like this: |
| 1337 | /// |
| 1338 | /// m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r) |
| 1339 | /// |
| 1340 | /// where A and B are constants, update the map with these values: |
| 1341 | /// |
| 1342 | /// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0) |
| 1343 | /// |
| 1344 | /// and add 13 + A*B*29 to AccumulatedConstant. |
| 1345 | /// This will allow getAddRecExpr to produce this: |
| 1346 | /// |
| 1347 | /// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B) |
| 1348 | /// |
| 1349 | /// This form often exposes folding opportunities that are hidden in |
| 1350 | /// the original operand list. |
| 1351 | /// |
| 1352 | /// Return true iff it appears that any interesting folding opportunities |
| 1353 | /// may be exposed. This helps getAddRecExpr short-circuit extra work in |
| 1354 | /// the common case where no interesting opportunities are present, and |
| 1355 | /// is also used as a check to avoid infinite recursion. |
| 1356 | /// |
| 1357 | static bool |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1358 | CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M, |
| 1359 | SmallVector<const SCEV *, 8> &NewOps, |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1360 | APInt &AccumulatedConstant, |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1361 | const SCEV *const *Ops, size_t NumOperands, |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1362 | const APInt &Scale, |
| 1363 | ScalarEvolution &SE) { |
| 1364 | bool Interesting = false; |
| 1365 | |
Dan Gohman | e0f0c7b | 2010-06-18 19:12:32 +0000 | [diff] [blame] | 1366 | // Iterate over the add operands. They are sorted, with constants first. |
| 1367 | unsigned i = 0; |
| 1368 | while (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| 1369 | ++i; |
| 1370 | // Pull a buried constant out to the outside. |
| 1371 | if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero()) |
| 1372 | Interesting = true; |
| 1373 | AccumulatedConstant += Scale * C->getValue()->getValue(); |
| 1374 | } |
| 1375 | |
| 1376 | // Next comes everything else. We're especially interested in multiplies |
| 1377 | // here, but they're in the middle, so just visit the rest with one loop. |
| 1378 | for (; i != NumOperands; ++i) { |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1379 | const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]); |
| 1380 | if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) { |
| 1381 | APInt NewScale = |
| 1382 | Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue(); |
| 1383 | if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) { |
| 1384 | // A multiplication of a constant with another add; recurse. |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1385 | const SCEVAddExpr *Add = cast<SCEVAddExpr>(Mul->getOperand(1)); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1386 | Interesting |= |
| 1387 | CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1388 | Add->op_begin(), Add->getNumOperands(), |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1389 | NewScale, SE); |
| 1390 | } else { |
| 1391 | // A multiplication of a constant with some other value. Update |
| 1392 | // the map. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1393 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end()); |
| 1394 | const SCEV *Key = SE.getMulExpr(MulOps); |
| 1395 | std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = |
Dan Gohman | 23737e0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1396 | M.insert(std::make_pair(Key, NewScale)); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1397 | if (Pair.second) { |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1398 | NewOps.push_back(Pair.first->first); |
| 1399 | } else { |
| 1400 | Pair.first->second += NewScale; |
| 1401 | // The map already had an entry for this value, which may indicate |
| 1402 | // a folding opportunity. |
| 1403 | Interesting = true; |
| 1404 | } |
| 1405 | } |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1406 | } else { |
| 1407 | // An ordinary operand. Update the map. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1408 | std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = |
Dan Gohman | 23737e0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1409 | M.insert(std::make_pair(Ops[i], Scale)); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1410 | if (Pair.second) { |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1411 | NewOps.push_back(Pair.first->first); |
| 1412 | } else { |
| 1413 | Pair.first->second += Scale; |
| 1414 | // The map already had an entry for this value, which may indicate |
| 1415 | // a folding opportunity. |
| 1416 | Interesting = true; |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | return Interesting; |
| 1422 | } |
| 1423 | |
| 1424 | namespace { |
| 1425 | struct APIntCompare { |
| 1426 | bool operator()(const APInt &LHS, const APInt &RHS) const { |
| 1427 | return LHS.ult(RHS); |
| 1428 | } |
| 1429 | }; |
| 1430 | } |
| 1431 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1432 | /// getAddExpr - Get a canonical add expression, or something simpler if |
| 1433 | /// possible. |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1434 | const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops, |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1435 | SCEV::NoWrapFlags Flags) { |
| 1436 | assert(!(Flags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) && |
| 1437 | "only nuw or nsw allowed"); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1438 | assert(!Ops.empty() && "Cannot get empty add!"); |
Chris Lattner | 627018b | 2004-04-07 16:16:11 +0000 | [diff] [blame] | 1439 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1440 | #ifndef NDEBUG |
Dan Gohman | c72f0c8 | 2010-06-18 19:09:27 +0000 | [diff] [blame] | 1441 | const Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1442 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
Dan Gohman | c72f0c8 | 2010-06-18 19:09:27 +0000 | [diff] [blame] | 1443 | assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1444 | "SCEVAddExpr operand types don't match!"); |
| 1445 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1446 | |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1447 | // If FlagNSW is true and all the operands are non-negative, infer FlagNUW. |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 1448 | // And vice-versa. |
| 1449 | int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW; |
| 1450 | SCEV::NoWrapFlags SignOrUnsignWrap = maskFlags(Flags, SignOrUnsignMask); |
| 1451 | if (SignOrUnsignWrap && (SignOrUnsignWrap != SignOrUnsignMask)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1452 | bool All = true; |
Dan Gohman | 2d16fc5 | 2010-08-16 16:27:53 +0000 | [diff] [blame] | 1453 | for (SmallVectorImpl<const SCEV *>::const_iterator I = Ops.begin(), |
| 1454 | E = Ops.end(); I != E; ++I) |
| 1455 | if (!isKnownNonNegative(*I)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1456 | All = false; |
| 1457 | break; |
| 1458 | } |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 1459 | if (All) Flags = setFlags(Flags, (SCEV::NoWrapFlags)SignOrUnsignMask); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1462 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1463 | GroupByComplexity(Ops, LI); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1464 | |
| 1465 | // If there are any constants, fold them together. |
| 1466 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1467 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1468 | ++Idx; |
Chris Lattner | 627018b | 2004-04-07 16:16:11 +0000 | [diff] [blame] | 1469 | assert(Idx < Ops.size()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1470 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1471 | // We found two constants, fold them together! |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1472 | Ops[0] = getConstant(LHSC->getValue()->getValue() + |
| 1473 | RHSC->getValue()->getValue()); |
Dan Gohman | 7f7c436 | 2009-06-14 22:53:57 +0000 | [diff] [blame] | 1474 | if (Ops.size() == 2) return Ops[0]; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1475 | Ops.erase(Ops.begin()+1); // Erase the folded element |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1476 | LHSC = cast<SCEVConstant>(Ops[0]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | // 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] | 1480 | if (LHSC->getValue()->isZero()) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1481 | Ops.erase(Ops.begin()); |
| 1482 | --Idx; |
| 1483 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1484 | |
Dan Gohman | bca091d | 2010-04-12 23:08:18 +0000 | [diff] [blame] | 1485 | if (Ops.size() == 1) return Ops[0]; |
| 1486 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1487 | |
Dan Gohman | 68ff776 | 2010-08-27 21:39:59 +0000 | [diff] [blame] | 1488 | // Okay, check to see if the same value occurs in the operand list more than |
| 1489 | // once. If so, merge them together into an multiply expression. Since we |
| 1490 | // sorted the list, these values are required to be adjacent. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1491 | const Type *Ty = Ops[0]->getType(); |
Dan Gohman | dc7692b | 2010-08-12 14:46:54 +0000 | [diff] [blame] | 1492 | bool FoundMatch = false; |
Dan Gohman | 68ff776 | 2010-08-27 21:39:59 +0000 | [diff] [blame] | 1493 | for (unsigned i = 0, e = Ops.size(); i != e-1; ++i) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1494 | if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2 |
Dan Gohman | 68ff776 | 2010-08-27 21:39:59 +0000 | [diff] [blame] | 1495 | // Scan ahead to count how many equal operands there are. |
| 1496 | unsigned Count = 2; |
| 1497 | while (i+Count != e && Ops[i+Count] == Ops[i]) |
| 1498 | ++Count; |
| 1499 | // Merge the values into a multiply. |
| 1500 | const SCEV *Scale = getConstant(Ty, Count); |
| 1501 | const SCEV *Mul = getMulExpr(Scale, Ops[i]); |
| 1502 | if (Ops.size() == Count) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1503 | return Mul; |
Dan Gohman | dc7692b | 2010-08-12 14:46:54 +0000 | [diff] [blame] | 1504 | Ops[i] = Mul; |
Dan Gohman | 68ff776 | 2010-08-27 21:39:59 +0000 | [diff] [blame] | 1505 | Ops.erase(Ops.begin()+i+1, Ops.begin()+i+Count); |
Dan Gohman | 5bb307d | 2010-08-28 00:39:27 +0000 | [diff] [blame] | 1506 | --i; e -= Count - 1; |
Dan Gohman | dc7692b | 2010-08-12 14:46:54 +0000 | [diff] [blame] | 1507 | FoundMatch = true; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1508 | } |
Dan Gohman | dc7692b | 2010-08-12 14:46:54 +0000 | [diff] [blame] | 1509 | if (FoundMatch) |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1510 | return getAddExpr(Ops, Flags); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1511 | |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1512 | // Check for truncates. If all the operands are truncated from the same |
| 1513 | // type, see if factoring out the truncate would permit the result to be |
| 1514 | // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n) |
| 1515 | // if the contents of the resulting outer trunc fold to something simple. |
| 1516 | for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) { |
| 1517 | const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]); |
| 1518 | const Type *DstType = Trunc->getType(); |
| 1519 | const Type *SrcType = Trunc->getOperand()->getType(); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1520 | SmallVector<const SCEV *, 8> LargeOps; |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1521 | bool Ok = true; |
| 1522 | // Check all the operands to see if they can be represented in the |
| 1523 | // source type of the truncate. |
| 1524 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 1525 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) { |
| 1526 | if (T->getOperand()->getType() != SrcType) { |
| 1527 | Ok = false; |
| 1528 | break; |
| 1529 | } |
| 1530 | LargeOps.push_back(T->getOperand()); |
| 1531 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
Dan Gohman | c686398 | 2010-04-23 01:51:29 +0000 | [diff] [blame] | 1532 | LargeOps.push_back(getAnyExtendExpr(C, SrcType)); |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1533 | } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1534 | SmallVector<const SCEV *, 8> LargeMulOps; |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1535 | for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { |
| 1536 | if (const SCEVTruncateExpr *T = |
| 1537 | dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) { |
| 1538 | if (T->getOperand()->getType() != SrcType) { |
| 1539 | Ok = false; |
| 1540 | break; |
| 1541 | } |
| 1542 | LargeMulOps.push_back(T->getOperand()); |
| 1543 | } else if (const SCEVConstant *C = |
| 1544 | dyn_cast<SCEVConstant>(M->getOperand(j))) { |
Dan Gohman | c686398 | 2010-04-23 01:51:29 +0000 | [diff] [blame] | 1545 | LargeMulOps.push_back(getAnyExtendExpr(C, SrcType)); |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1546 | } else { |
| 1547 | Ok = false; |
| 1548 | break; |
| 1549 | } |
| 1550 | } |
| 1551 | if (Ok) |
| 1552 | LargeOps.push_back(getMulExpr(LargeMulOps)); |
| 1553 | } else { |
| 1554 | Ok = false; |
| 1555 | break; |
| 1556 | } |
| 1557 | } |
| 1558 | if (Ok) { |
| 1559 | // Evaluate the expression in the larger type. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1560 | const SCEV *Fold = getAddExpr(LargeOps, Flags); |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1561 | // If it folds to something simple, use it. Otherwise, don't. |
| 1562 | if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold)) |
| 1563 | return getTruncateExpr(Fold, DstType); |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | // Skip past any other cast SCEVs. |
Dan Gohman | f50cd74 | 2007-06-18 19:30:09 +0000 | [diff] [blame] | 1568 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) |
| 1569 | ++Idx; |
| 1570 | |
| 1571 | // If there are add operands they would be next. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1572 | if (Idx < Ops.size()) { |
| 1573 | bool DeletedAdd = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1574 | while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1575 | // If we have an add, expand the add operands onto the end of the operands |
| 1576 | // list. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1577 | Ops.erase(Ops.begin()+Idx); |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 1578 | Ops.append(Add->op_begin(), Add->op_end()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1579 | DeletedAdd = true; |
| 1580 | } |
| 1581 | |
| 1582 | // If we deleted at least one add, we added operands to the end of the list, |
| 1583 | // and they are not necessarily sorted. Recurse to resort and resimplify |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 1584 | // any operands we just acquired. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1585 | if (DeletedAdd) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1586 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | // Skip over the add expression until we get to a multiply. |
| 1590 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1591 | ++Idx; |
| 1592 | |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1593 | // Check to see if there are any folding opportunities present with |
| 1594 | // operands multiplied by constant values. |
| 1595 | if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) { |
| 1596 | uint64_t BitWidth = getTypeSizeInBits(Ty); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1597 | DenseMap<const SCEV *, APInt> M; |
| 1598 | SmallVector<const SCEV *, 8> NewOps; |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1599 | APInt AccumulatedConstant(BitWidth, 0); |
| 1600 | if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1601 | Ops.data(), Ops.size(), |
| 1602 | APInt(BitWidth, 1), *this)) { |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1603 | // Some interesting folding opportunity is present, so its worthwhile to |
| 1604 | // re-generate the operands list. Group the operands by constant scale, |
| 1605 | // to avoid multiplying by the same constant scale multiple times. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1606 | std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists; |
Dan Gohman | 8d9c7a6 | 2010-08-16 16:30:01 +0000 | [diff] [blame] | 1607 | for (SmallVector<const SCEV *, 8>::const_iterator I = NewOps.begin(), |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1608 | E = NewOps.end(); I != E; ++I) |
| 1609 | MulOpLists[M.find(*I)->second].push_back(*I); |
| 1610 | // Re-generate the operands list. |
| 1611 | Ops.clear(); |
| 1612 | if (AccumulatedConstant != 0) |
| 1613 | Ops.push_back(getConstant(AccumulatedConstant)); |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1614 | for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator |
| 1615 | I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I) |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1616 | if (I->first != 0) |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1617 | Ops.push_back(getMulExpr(getConstant(I->first), |
| 1618 | getAddExpr(I->second))); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1619 | if (Ops.empty()) |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1620 | return getConstant(Ty, 0); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1621 | if (Ops.size() == 1) |
| 1622 | return Ops[0]; |
| 1623 | return getAddExpr(Ops); |
| 1624 | } |
| 1625 | } |
| 1626 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1627 | // If we are adding something to a multiply expression, make sure the |
| 1628 | // something is not already an operand of the multiply. If so, merge it into |
| 1629 | // the multiply. |
| 1630 | for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1631 | const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1632 | for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1633 | const SCEV *MulOpSCEV = Mul->getOperand(MulOp); |
Dan Gohman | 918e76b | 2010-08-12 14:52:55 +0000 | [diff] [blame] | 1634 | if (isa<SCEVConstant>(MulOpSCEV)) |
| 1635 | continue; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1636 | for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) |
Dan Gohman | 918e76b | 2010-08-12 14:52:55 +0000 | [diff] [blame] | 1637 | if (MulOpSCEV == Ops[AddOp]) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1638 | // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1639 | const SCEV *InnerMul = Mul->getOperand(MulOp == 0); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1640 | if (Mul->getNumOperands() != 2) { |
| 1641 | // If the multiply has more than two operands, we must get the |
| 1642 | // Y*Z term. |
Dan Gohman | 1895991 | 2010-08-16 16:57:24 +0000 | [diff] [blame] | 1643 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), |
| 1644 | Mul->op_begin()+MulOp); |
| 1645 | MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1646 | InnerMul = getMulExpr(MulOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1647 | } |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1648 | const SCEV *One = getConstant(Ty, 1); |
Dan Gohman | 58a85b9 | 2010-08-13 20:17:14 +0000 | [diff] [blame] | 1649 | const SCEV *AddOne = getAddExpr(One, InnerMul); |
Dan Gohman | 918e76b | 2010-08-12 14:52:55 +0000 | [diff] [blame] | 1650 | const SCEV *OuterMul = getMulExpr(AddOne, MulOpSCEV); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1651 | if (Ops.size() == 2) return OuterMul; |
| 1652 | if (AddOp < Idx) { |
| 1653 | Ops.erase(Ops.begin()+AddOp); |
| 1654 | Ops.erase(Ops.begin()+Idx-1); |
| 1655 | } else { |
| 1656 | Ops.erase(Ops.begin()+Idx); |
| 1657 | Ops.erase(Ops.begin()+AddOp-1); |
| 1658 | } |
| 1659 | Ops.push_back(OuterMul); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1660 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1661 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1662 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1663 | // Check this multiply against other multiplies being added together. |
| 1664 | for (unsigned OtherMulIdx = Idx+1; |
| 1665 | OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]); |
| 1666 | ++OtherMulIdx) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1667 | const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1668 | // If MulOp occurs in OtherMul, we can fold the two multiplies |
| 1669 | // together. |
| 1670 | for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); |
| 1671 | OMulOp != e; ++OMulOp) |
| 1672 | if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { |
| 1673 | // 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] | 1674 | const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1675 | if (Mul->getNumOperands() != 2) { |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1676 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), |
Dan Gohman | 1895991 | 2010-08-16 16:57:24 +0000 | [diff] [blame] | 1677 | Mul->op_begin()+MulOp); |
| 1678 | MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1679 | InnerMul1 = getMulExpr(MulOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1680 | } |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1681 | const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1682 | if (OtherMul->getNumOperands() != 2) { |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1683 | SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(), |
Dan Gohman | 1895991 | 2010-08-16 16:57:24 +0000 | [diff] [blame] | 1684 | OtherMul->op_begin()+OMulOp); |
| 1685 | MulOps.append(OtherMul->op_begin()+OMulOp+1, OtherMul->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1686 | InnerMul2 = getMulExpr(MulOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1687 | } |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1688 | const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2); |
| 1689 | const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1690 | if (Ops.size() == 2) return OuterMul; |
Dan Gohman | 90b5f25 | 2010-08-31 22:50:31 +0000 | [diff] [blame] | 1691 | Ops.erase(Ops.begin()+Idx); |
| 1692 | Ops.erase(Ops.begin()+OtherMulIdx-1); |
| 1693 | Ops.push_back(OuterMul); |
| 1694 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1695 | } |
| 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | // If there are any add recurrences in the operands list, see if any other |
| 1701 | // added values are loop invariant. If so, we can fold them into the |
| 1702 | // recurrence. |
| 1703 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1704 | ++Idx; |
| 1705 | |
| 1706 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1707 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1708 | // Scan all of the other operands to this add and add them to the vector if |
| 1709 | // they are loop invariant w.r.t. the recurrence. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1710 | SmallVector<const SCEV *, 8> LIOps; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1711 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Dan Gohman | bca091d | 2010-04-12 23:08:18 +0000 | [diff] [blame] | 1712 | const Loop *AddRecLoop = AddRec->getLoop(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1713 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1714 | if (isLoopInvariant(Ops[i], AddRecLoop)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1715 | LIOps.push_back(Ops[i]); |
| 1716 | Ops.erase(Ops.begin()+i); |
| 1717 | --i; --e; |
| 1718 | } |
| 1719 | |
| 1720 | // If we found some loop invariants, fold them into the recurrence. |
| 1721 | if (!LIOps.empty()) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1722 | // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step} |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1723 | LIOps.push_back(AddRec->getStart()); |
| 1724 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1725 | SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(), |
Dan Gohman | 3a5d409 | 2009-12-18 03:57:04 +0000 | [diff] [blame] | 1726 | AddRec->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1727 | AddRecOps[0] = getAddExpr(LIOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1728 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 1729 | // Build the new addrec. Propagate the NUW and NSW flags if both the |
Eric Christopher | 8737683 | 2011-01-11 09:02:09 +0000 | [diff] [blame] | 1730 | // outer add and the inner addrec are guaranteed to have no overflow. |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 1731 | // Always propagate NW. |
| 1732 | Flags = AddRec->getNoWrapFlags(setFlags(Flags, SCEV::FlagNW)); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1733 | const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRecLoop, Flags); |
Dan Gohman | 59de33e | 2009-12-18 18:45:31 +0000 | [diff] [blame] | 1734 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1735 | // If all of the other operands were loop invariant, we are done. |
| 1736 | if (Ops.size() == 1) return NewRec; |
| 1737 | |
| 1738 | // Otherwise, add the folded AddRec by the non-liv parts. |
| 1739 | for (unsigned i = 0;; ++i) |
| 1740 | if (Ops[i] == AddRec) { |
| 1741 | Ops[i] = NewRec; |
| 1742 | break; |
| 1743 | } |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1744 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1748 | // there are multiple AddRec's with the same loop induction variable being |
| 1749 | // added together. If so, we can fold them. |
| 1750 | for (unsigned OtherIdx = Idx+1; |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1751 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); |
| 1752 | ++OtherIdx) |
| 1753 | if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) { |
| 1754 | // Other + {A,+,B}<L> + {C,+,D}<L> --> Other + {A+C,+,B+D}<L> |
| 1755 | SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(), |
| 1756 | AddRec->op_end()); |
| 1757 | for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); |
| 1758 | ++OtherIdx) |
Dan Gohman | 30cbc86 | 2010-08-29 14:53:34 +0000 | [diff] [blame] | 1759 | if (const SCEVAddRecExpr *OtherAddRec = |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1760 | dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx])) |
Dan Gohman | 30cbc86 | 2010-08-29 14:53:34 +0000 | [diff] [blame] | 1761 | if (OtherAddRec->getLoop() == AddRecLoop) { |
| 1762 | for (unsigned i = 0, e = OtherAddRec->getNumOperands(); |
| 1763 | i != e; ++i) { |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1764 | if (i >= AddRecOps.size()) { |
Dan Gohman | 30cbc86 | 2010-08-29 14:53:34 +0000 | [diff] [blame] | 1765 | AddRecOps.append(OtherAddRec->op_begin()+i, |
| 1766 | OtherAddRec->op_end()); |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1767 | break; |
| 1768 | } |
Dan Gohman | 30cbc86 | 2010-08-29 14:53:34 +0000 | [diff] [blame] | 1769 | AddRecOps[i] = getAddExpr(AddRecOps[i], |
| 1770 | OtherAddRec->getOperand(i)); |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1771 | } |
| 1772 | Ops.erase(Ops.begin() + OtherIdx); --OtherIdx; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1773 | } |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1774 | // Step size has changed, so we cannot guarantee no self-wraparound. |
| 1775 | Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop, SCEV::FlagAnyWrap); |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1776 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1780 | // next one. |
| 1781 | } |
| 1782 | |
| 1783 | // Okay, it looks like we really DO need an add expr. Check to see if we |
| 1784 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1785 | FoldingSetNodeID ID; |
| 1786 | ID.AddInteger(scAddExpr); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1787 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1788 | ID.AddPointer(Ops[i]); |
| 1789 | void *IP = 0; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1790 | SCEVAddExpr *S = |
| 1791 | static_cast<SCEVAddExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
| 1792 | if (!S) { |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1793 | const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); |
| 1794 | std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1795 | S = new (SCEVAllocator) SCEVAddExpr(ID.Intern(SCEVAllocator), |
| 1796 | O, Ops.size()); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1797 | UniqueSCEVs.InsertNode(S, IP); |
| 1798 | } |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1799 | S->setNoWrapFlags(Flags); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1800 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1803 | /// getMulExpr - Get a canonical multiply expression, or something simpler if |
| 1804 | /// possible. |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1805 | const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops, |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1806 | SCEV::NoWrapFlags Flags) { |
| 1807 | assert(Flags == maskFlags(Flags, SCEV::FlagNUW | SCEV::FlagNSW) && |
| 1808 | "only nuw or nsw allowed"); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1809 | assert(!Ops.empty() && "Cannot get empty mul!"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1810 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1811 | #ifndef NDEBUG |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 1812 | const Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1813 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 1814 | assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1815 | "SCEVMulExpr operand types don't match!"); |
| 1816 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1817 | |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1818 | // If FlagNSW is true and all the operands are non-negative, infer FlagNUW. |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 1819 | // And vice-versa. |
| 1820 | int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW; |
| 1821 | SCEV::NoWrapFlags SignOrUnsignWrap = maskFlags(Flags, SignOrUnsignMask); |
| 1822 | if (SignOrUnsignWrap && (SignOrUnsignWrap != SignOrUnsignMask)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1823 | bool All = true; |
Dan Gohman | 2d16fc5 | 2010-08-16 16:27:53 +0000 | [diff] [blame] | 1824 | for (SmallVectorImpl<const SCEV *>::const_iterator I = Ops.begin(), |
| 1825 | E = Ops.end(); I != E; ++I) |
| 1826 | if (!isKnownNonNegative(*I)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1827 | All = false; |
| 1828 | break; |
| 1829 | } |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 1830 | if (All) Flags = setFlags(Flags, (SCEV::NoWrapFlags)SignOrUnsignMask); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1833 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1834 | GroupByComplexity(Ops, LI); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1835 | |
| 1836 | // If there are any constants, fold them together. |
| 1837 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1838 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1839 | |
| 1840 | // C1*(C2+V) -> C1*C2 + C1*V |
| 1841 | if (Ops.size() == 2) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1842 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1843 | if (Add->getNumOperands() == 2 && |
| 1844 | isa<SCEVConstant>(Add->getOperand(0))) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1845 | return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)), |
| 1846 | getMulExpr(LHSC, Add->getOperand(1))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1847 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1848 | ++Idx; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1849 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1850 | // We found two constants, fold them together! |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1851 | ConstantInt *Fold = ConstantInt::get(getContext(), |
| 1852 | LHSC->getValue()->getValue() * |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1853 | RHSC->getValue()->getValue()); |
| 1854 | Ops[0] = getConstant(Fold); |
| 1855 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1856 | if (Ops.size() == 1) return Ops[0]; |
| 1857 | LHSC = cast<SCEVConstant>(Ops[0]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | // If we are left with a constant one being multiplied, strip it off. |
| 1861 | if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) { |
| 1862 | Ops.erase(Ops.begin()); |
| 1863 | --Idx; |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1864 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1865 | // If we have a multiply of zero, it will always be zero. |
| 1866 | return Ops[0]; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1867 | } else if (Ops[0]->isAllOnesValue()) { |
| 1868 | // If we have a mul by -1 of an add, try distributing the -1 among the |
| 1869 | // add operands. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1870 | if (Ops.size() == 2) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1871 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) { |
| 1872 | SmallVector<const SCEV *, 4> NewOps; |
| 1873 | bool AnyFolded = false; |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1874 | for (SCEVAddRecExpr::op_iterator I = Add->op_begin(), |
| 1875 | E = Add->op_end(); I != E; ++I) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1876 | const SCEV *Mul = getMulExpr(Ops[0], *I); |
| 1877 | if (!isa<SCEVMulExpr>(Mul)) AnyFolded = true; |
| 1878 | NewOps.push_back(Mul); |
| 1879 | } |
| 1880 | if (AnyFolded) |
| 1881 | return getAddExpr(NewOps); |
| 1882 | } |
Andrew Trick | a053b21 | 2011-03-14 17:38:54 +0000 | [diff] [blame] | 1883 | else if (const SCEVAddRecExpr * |
| 1884 | AddRec = dyn_cast<SCEVAddRecExpr>(Ops[1])) { |
| 1885 | // Negation preserves a recurrence's no self-wrap property. |
| 1886 | SmallVector<const SCEV *, 4> Operands; |
| 1887 | for (SCEVAddRecExpr::op_iterator I = AddRec->op_begin(), |
| 1888 | E = AddRec->op_end(); I != E; ++I) { |
| 1889 | Operands.push_back(getMulExpr(Ops[0], *I)); |
| 1890 | } |
| 1891 | return getAddRecExpr(Operands, AddRec->getLoop(), |
| 1892 | AddRec->getNoWrapFlags(SCEV::FlagNW)); |
| 1893 | } |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1894 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1895 | } |
Dan Gohman | 3ab1312 | 2010-04-13 16:49:23 +0000 | [diff] [blame] | 1896 | |
| 1897 | if (Ops.size() == 1) |
| 1898 | return Ops[0]; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | // Skip over the add expression until we get to a multiply. |
| 1902 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1903 | ++Idx; |
| 1904 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1905 | // If there are mul operands inline them all into this expression. |
| 1906 | if (Idx < Ops.size()) { |
| 1907 | bool DeletedMul = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1908 | while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1909 | // If we have an mul, expand the mul operands onto the end of the operands |
| 1910 | // list. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1911 | Ops.erase(Ops.begin()+Idx); |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 1912 | Ops.append(Mul->op_begin(), Mul->op_end()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1913 | DeletedMul = true; |
| 1914 | } |
| 1915 | |
| 1916 | // If we deleted at least one mul, we added operands to the end of the list, |
| 1917 | // and they are not necessarily sorted. Recurse to resort and resimplify |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 1918 | // any operands we just acquired. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1919 | if (DeletedMul) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1920 | return getMulExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | // If there are any add recurrences in the operands list, see if any other |
| 1924 | // added values are loop invariant. If so, we can fold them into the |
| 1925 | // recurrence. |
| 1926 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1927 | ++Idx; |
| 1928 | |
| 1929 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1930 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1931 | // Scan all of the other operands to this mul and add them to the vector if |
| 1932 | // they are loop invariant w.r.t. the recurrence. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1933 | SmallVector<const SCEV *, 8> LIOps; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1934 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Dan Gohman | 0f32ae3 | 2010-08-29 14:55:19 +0000 | [diff] [blame] | 1935 | const Loop *AddRecLoop = AddRec->getLoop(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1936 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1937 | if (isLoopInvariant(Ops[i], AddRecLoop)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1938 | LIOps.push_back(Ops[i]); |
| 1939 | Ops.erase(Ops.begin()+i); |
| 1940 | --i; --e; |
| 1941 | } |
| 1942 | |
| 1943 | // If we found some loop invariants, fold them into the recurrence. |
| 1944 | if (!LIOps.empty()) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1945 | // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step} |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1946 | SmallVector<const SCEV *, 4> NewOps; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1947 | NewOps.reserve(AddRec->getNumOperands()); |
Dan Gohman | 27ed6a4 | 2010-06-17 23:34:09 +0000 | [diff] [blame] | 1948 | const SCEV *Scale = getMulExpr(LIOps); |
| 1949 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
| 1950 | NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1951 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 1952 | // Build the new addrec. Propagate the NUW and NSW flags if both the |
| 1953 | // outer mul and the inner addrec are guaranteed to have no overflow. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1954 | // |
| 1955 | // No self-wrap cannot be guaranteed after changing the step size, but |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 1956 | // will be inferred if either NUW or NSW is true. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1957 | Flags = AddRec->getNoWrapFlags(clearFlags(Flags, SCEV::FlagNW)); |
| 1958 | const SCEV *NewRec = getAddRecExpr(NewOps, AddRecLoop, Flags); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1959 | |
| 1960 | // If all of the other operands were loop invariant, we are done. |
| 1961 | if (Ops.size() == 1) return NewRec; |
| 1962 | |
| 1963 | // Otherwise, multiply the folded AddRec by the non-liv parts. |
| 1964 | for (unsigned i = 0;; ++i) |
| 1965 | if (Ops[i] == AddRec) { |
| 1966 | Ops[i] = NewRec; |
| 1967 | break; |
| 1968 | } |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1969 | return getMulExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1973 | // there are multiple AddRec's with the same loop induction variable being |
| 1974 | // multiplied together. If so, we can fold them. |
| 1975 | for (unsigned OtherIdx = Idx+1; |
Dan Gohman | 6a0c125 | 2010-08-31 22:52:12 +0000 | [diff] [blame] | 1976 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); |
| 1977 | ++OtherIdx) |
| 1978 | if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) { |
| 1979 | // F * G, where F = {A,+,B}<L> and G = {C,+,D}<L> --> |
| 1980 | // {A*C,+,F*D + G*B + B*D}<L> |
| 1981 | for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); |
| 1982 | ++OtherIdx) |
| 1983 | if (const SCEVAddRecExpr *OtherAddRec = |
| 1984 | dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx])) |
| 1985 | if (OtherAddRec->getLoop() == AddRecLoop) { |
| 1986 | const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec; |
| 1987 | const SCEV *NewStart = getMulExpr(F->getStart(), G->getStart()); |
| 1988 | const SCEV *B = F->getStepRecurrence(*this); |
| 1989 | const SCEV *D = G->getStepRecurrence(*this); |
| 1990 | const SCEV *NewStep = getAddExpr(getMulExpr(F, D), |
| 1991 | getMulExpr(G, B), |
| 1992 | getMulExpr(B, D)); |
| 1993 | const SCEV *NewAddRec = getAddRecExpr(NewStart, NewStep, |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 1994 | F->getLoop(), |
| 1995 | SCEV::FlagAnyWrap); |
Dan Gohman | 6a0c125 | 2010-08-31 22:52:12 +0000 | [diff] [blame] | 1996 | if (Ops.size() == 2) return NewAddRec; |
| 1997 | Ops[Idx] = AddRec = cast<SCEVAddRecExpr>(NewAddRec); |
| 1998 | Ops.erase(Ops.begin() + OtherIdx); --OtherIdx; |
| 1999 | } |
| 2000 | return getMulExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 2004 | // next one. |
| 2005 | } |
| 2006 | |
| 2007 | // Okay, it looks like we really DO need an mul expr. Check to see if we |
| 2008 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2009 | FoldingSetNodeID ID; |
| 2010 | ID.AddInteger(scMulExpr); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2011 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 2012 | ID.AddPointer(Ops[i]); |
| 2013 | void *IP = 0; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2014 | SCEVMulExpr *S = |
| 2015 | static_cast<SCEVMulExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
| 2016 | if (!S) { |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 2017 | const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); |
| 2018 | std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 2019 | S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator), |
| 2020 | O, Ops.size()); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2021 | UniqueSCEVs.InsertNode(S, IP); |
| 2022 | } |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2023 | S->setNoWrapFlags(Flags); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2024 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
Andreas Bolka | 8a11c98 | 2009-08-07 22:55:26 +0000 | [diff] [blame] | 2027 | /// getUDivExpr - Get a canonical unsigned division expression, or something |
| 2028 | /// simpler if possible. |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2029 | const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS, |
| 2030 | const SCEV *RHS) { |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2031 | assert(getEffectiveSCEVType(LHS->getType()) == |
| 2032 | getEffectiveSCEVType(RHS->getType()) && |
| 2033 | "SCEVUDivExpr operand types don't match!"); |
| 2034 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2035 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2036 | if (RHSC->getValue()->equalsInt(1)) |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 2037 | return LHS; // X udiv 1 --> x |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 2038 | // If the denominator is zero, the result of the udiv is undefined. Don't |
| 2039 | // try to analyze it, because the resolution chosen here may differ from |
| 2040 | // the resolution chosen in other parts of the compiler. |
| 2041 | if (!RHSC->getValue()->isZero()) { |
| 2042 | // Determine if the division can be folded into the operands of |
| 2043 | // its operands. |
| 2044 | // TODO: Generalize this to non-constants by using known-bits information. |
| 2045 | const Type *Ty = LHS->getType(); |
| 2046 | unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros(); |
Dan Gohman | ddd3a88 | 2010-08-04 19:52:50 +0000 | [diff] [blame] | 2047 | unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1; |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 2048 | // For non-power-of-two values, effectively round the value up to the |
| 2049 | // nearest power of two. |
| 2050 | if (!RHSC->getValue()->getValue().isPowerOf2()) |
| 2051 | ++MaxShiftAmt; |
| 2052 | const IntegerType *ExtTy = |
| 2053 | IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt); |
| 2054 | // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. |
| 2055 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) |
| 2056 | if (const SCEVConstant *Step = |
| 2057 | dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) |
| 2058 | if (!Step->getValue()->getValue() |
| 2059 | .urem(RHSC->getValue()->getValue()) && |
| 2060 | getZeroExtendExpr(AR, ExtTy) == |
| 2061 | getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), |
| 2062 | getZeroExtendExpr(Step, ExtTy), |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2063 | AR->getLoop(), SCEV::FlagAnyWrap)) { |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 2064 | SmallVector<const SCEV *, 4> Operands; |
| 2065 | for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i) |
| 2066 | Operands.push_back(getUDivExpr(AR->getOperand(i), RHS)); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2067 | return getAddRecExpr(Operands, AR->getLoop(), |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 2068 | SCEV::FlagNW); |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 2069 | } |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 2070 | // (A*B)/C --> A*(B/C) if safe and B/C can be folded. |
| 2071 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) { |
| 2072 | SmallVector<const SCEV *, 4> Operands; |
| 2073 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) |
| 2074 | Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy)); |
| 2075 | if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) |
| 2076 | // Find an operand that's safely divisible. |
| 2077 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { |
| 2078 | const SCEV *Op = M->getOperand(i); |
| 2079 | const SCEV *Div = getUDivExpr(Op, RHSC); |
| 2080 | if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) { |
| 2081 | Operands = SmallVector<const SCEV *, 4>(M->op_begin(), |
| 2082 | M->op_end()); |
| 2083 | Operands[i] = Div; |
| 2084 | return getMulExpr(Operands); |
| 2085 | } |
| 2086 | } |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 2087 | } |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 2088 | // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded. |
Andrew Trick | a2a1620 | 2011-04-27 18:17:36 +0000 | [diff] [blame] | 2089 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(LHS)) { |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 2090 | SmallVector<const SCEV *, 4> Operands; |
| 2091 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) |
| 2092 | Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy)); |
| 2093 | if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { |
| 2094 | Operands.clear(); |
| 2095 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { |
| 2096 | const SCEV *Op = getUDivExpr(A->getOperand(i), RHS); |
| 2097 | if (isa<SCEVUDivExpr>(Op) || |
| 2098 | getMulExpr(Op, RHS) != A->getOperand(i)) |
| 2099 | break; |
| 2100 | Operands.push_back(Op); |
| 2101 | } |
| 2102 | if (Operands.size() == A->getNumOperands()) |
| 2103 | return getAddExpr(Operands); |
| 2104 | } |
| 2105 | } |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 2106 | |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 2107 | // Fold if both operands are constant. |
| 2108 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { |
| 2109 | Constant *LHSCV = LHSC->getValue(); |
| 2110 | Constant *RHSCV = RHSC->getValue(); |
| 2111 | return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV, |
| 2112 | RHSCV))); |
| 2113 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2114 | } |
| 2115 | } |
| 2116 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2117 | FoldingSetNodeID ID; |
| 2118 | ID.AddInteger(scUDivExpr); |
| 2119 | ID.AddPointer(LHS); |
| 2120 | ID.AddPointer(RHS); |
| 2121 | void *IP = 0; |
| 2122 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 2123 | SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator), |
| 2124 | LHS, RHS); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2125 | UniqueSCEVs.InsertNode(S, IP); |
| 2126 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 2130 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 2131 | /// Simplify the expression as much as possible. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2132 | const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, const SCEV *Step, |
| 2133 | const Loop *L, |
| 2134 | SCEV::NoWrapFlags Flags) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2135 | SmallVector<const SCEV *, 4> Operands; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2136 | Operands.push_back(Start); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2137 | if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step)) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2138 | if (StepChrec->getLoop() == L) { |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 2139 | Operands.append(StepChrec->op_begin(), StepChrec->op_end()); |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 2140 | return getAddRecExpr(Operands, L, maskFlags(Flags, SCEV::FlagNW)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | Operands.push_back(Step); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2144 | return getAddRecExpr(Operands, L, Flags); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 2147 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 2148 | /// Simplify the expression as much as possible. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2149 | const SCEV * |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2150 | ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands, |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2151 | const Loop *L, SCEV::NoWrapFlags Flags) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2152 | if (Operands.size() == 1) return Operands[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2153 | #ifndef NDEBUG |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 2154 | const Type *ETy = getEffectiveSCEVType(Operands[0]->getType()); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2155 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 2156 | assert(getEffectiveSCEVType(Operands[i]->getType()) == ETy && |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2157 | "SCEVAddRecExpr operand types don't match!"); |
Dan Gohman | 203a723 | 2010-11-17 20:48:38 +0000 | [diff] [blame] | 2158 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 2159 | assert(isLoopInvariant(Operands[i], L) && |
Dan Gohman | 203a723 | 2010-11-17 20:48:38 +0000 | [diff] [blame] | 2160 | "SCEVAddRecExpr operand is not loop-invariant!"); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2161 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2162 | |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 2163 | if (Operands.back()->isZero()) { |
| 2164 | Operands.pop_back(); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2165 | return getAddRecExpr(Operands, L, SCEV::FlagAnyWrap); // {X,+,0} --> X |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 2166 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2167 | |
Dan Gohman | bc02853 | 2010-02-19 18:49:22 +0000 | [diff] [blame] | 2168 | // It's tempting to want to call getMaxBackedgeTakenCount count here and |
| 2169 | // use that information to infer NUW and NSW flags. However, computing a |
| 2170 | // BE count requires calling getAddRecExpr, so we may not yet have a |
| 2171 | // meaningful BE count at this point (and if we don't, we'd be stuck |
| 2172 | // with a SCEVCouldNotCompute as the cached BE count). |
| 2173 | |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2174 | // If FlagNSW is true and all the operands are non-negative, infer FlagNUW. |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 2175 | // And vice-versa. |
| 2176 | int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW; |
| 2177 | SCEV::NoWrapFlags SignOrUnsignWrap = maskFlags(Flags, SignOrUnsignMask); |
| 2178 | if (SignOrUnsignWrap && (SignOrUnsignWrap != SignOrUnsignMask)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2179 | bool All = true; |
Dan Gohman | 2d16fc5 | 2010-08-16 16:27:53 +0000 | [diff] [blame] | 2180 | for (SmallVectorImpl<const SCEV *>::const_iterator I = Operands.begin(), |
| 2181 | E = Operands.end(); I != E; ++I) |
| 2182 | if (!isKnownNonNegative(*I)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2183 | All = false; |
| 2184 | break; |
| 2185 | } |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 2186 | if (All) Flags = setFlags(Flags, (SCEV::NoWrapFlags)SignOrUnsignMask); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 2189 | // Canonicalize nested AddRecs in by nesting them in order of loop depth. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2190 | if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) { |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 2191 | const Loop *NestedLoop = NestedAR->getLoop(); |
Dan Gohman | 9cba978 | 2010-08-13 20:23:25 +0000 | [diff] [blame] | 2192 | if (L->contains(NestedLoop) ? |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2193 | (L->getLoopDepth() < NestedLoop->getLoopDepth()) : |
Dan Gohman | 9cba978 | 2010-08-13 20:23:25 +0000 | [diff] [blame] | 2194 | (!NestedLoop->contains(L) && |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2195 | DT->dominates(L->getHeader(), NestedLoop->getHeader()))) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2196 | SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(), |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 2197 | NestedAR->op_end()); |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 2198 | Operands[0] = NestedAR->getStart(); |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 2199 | // AddRecs require their operands be loop-invariant with respect to their |
| 2200 | // loops. Don't perform this transformation if it would break this |
| 2201 | // requirement. |
| 2202 | bool AllInvariant = true; |
| 2203 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 2204 | if (!isLoopInvariant(Operands[i], L)) { |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 2205 | AllInvariant = false; |
| 2206 | break; |
| 2207 | } |
| 2208 | if (AllInvariant) { |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2209 | // Create a recurrence for the outer loop with the same step size. |
| 2210 | // |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2211 | // The outer recurrence keeps its NW flag but only keeps NUW/NSW if the |
| 2212 | // inner recurrence has the same property. |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 2213 | SCEV::NoWrapFlags OuterFlags = |
| 2214 | maskFlags(Flags, SCEV::FlagNW | NestedAR->getNoWrapFlags()); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2215 | |
| 2216 | NestedOperands[0] = getAddRecExpr(Operands, L, OuterFlags); |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 2217 | AllInvariant = true; |
| 2218 | for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 2219 | if (!isLoopInvariant(NestedOperands[i], NestedLoop)) { |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 2220 | AllInvariant = false; |
| 2221 | break; |
| 2222 | } |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2223 | if (AllInvariant) { |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 2224 | // Ok, both add recurrences are valid after the transformation. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2225 | // |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2226 | // The inner recurrence keeps its NW flag but only keeps NUW/NSW if |
| 2227 | // the outer recurrence has the same property. |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 2228 | SCEV::NoWrapFlags InnerFlags = |
| 2229 | maskFlags(NestedAR->getNoWrapFlags(), SCEV::FlagNW | Flags); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2230 | return getAddRecExpr(NestedOperands, NestedLoop, InnerFlags); |
| 2231 | } |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 2232 | } |
| 2233 | // Reset Operands to its original state. |
| 2234 | Operands[0] = NestedAR; |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 2235 | } |
| 2236 | } |
| 2237 | |
Dan Gohman | 6784753 | 2010-01-19 22:27:22 +0000 | [diff] [blame] | 2238 | // Okay, it looks like we really DO need an addrec expr. Check to see if we |
| 2239 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2240 | FoldingSetNodeID ID; |
| 2241 | ID.AddInteger(scAddRecExpr); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2242 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 2243 | ID.AddPointer(Operands[i]); |
| 2244 | ID.AddPointer(L); |
| 2245 | void *IP = 0; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2246 | SCEVAddRecExpr *S = |
| 2247 | static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
| 2248 | if (!S) { |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 2249 | const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Operands.size()); |
| 2250 | std::uninitialized_copy(Operands.begin(), Operands.end(), O); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 2251 | S = new (SCEVAllocator) SCEVAddRecExpr(ID.Intern(SCEVAllocator), |
| 2252 | O, Operands.size(), L); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2253 | UniqueSCEVs.InsertNode(S, IP); |
| 2254 | } |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2255 | S->setNoWrapFlags(Flags); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2256 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2259 | const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, |
| 2260 | const SCEV *RHS) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2261 | SmallVector<const SCEV *, 2> Ops; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2262 | Ops.push_back(LHS); |
| 2263 | Ops.push_back(RHS); |
| 2264 | return getSMaxExpr(Ops); |
| 2265 | } |
| 2266 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2267 | const SCEV * |
| 2268 | ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2269 | assert(!Ops.empty() && "Cannot get empty smax!"); |
| 2270 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2271 | #ifndef NDEBUG |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 2272 | const Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2273 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 2274 | assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2275 | "SCEVSMaxExpr operand types don't match!"); |
| 2276 | #endif |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2277 | |
| 2278 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 2279 | GroupByComplexity(Ops, LI); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2280 | |
| 2281 | // If there are any constants, fold them together. |
| 2282 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2283 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2284 | ++Idx; |
| 2285 | assert(Idx < Ops.size()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2286 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2287 | // We found two constants, fold them together! |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2288 | ConstantInt *Fold = ConstantInt::get(getContext(), |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2289 | APIntOps::smax(LHSC->getValue()->getValue(), |
| 2290 | RHSC->getValue()->getValue())); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2291 | Ops[0] = getConstant(Fold); |
| 2292 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 2293 | if (Ops.size() == 1) return Ops[0]; |
| 2294 | LHSC = cast<SCEVConstant>(Ops[0]); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 2297 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2298 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) { |
| 2299 | Ops.erase(Ops.begin()); |
| 2300 | --Idx; |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 2301 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) { |
| 2302 | // If we have an smax with a constant maximum-int, it will always be |
| 2303 | // maximum-int. |
| 2304 | return Ops[0]; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2305 | } |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2306 | |
Dan Gohman | 3ab1312 | 2010-04-13 16:49:23 +0000 | [diff] [blame] | 2307 | if (Ops.size() == 1) return Ops[0]; |
| 2308 | } |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2309 | |
| 2310 | // Find the first SMax |
| 2311 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr) |
| 2312 | ++Idx; |
| 2313 | |
| 2314 | // Check to see if one of the operands is an SMax. If so, expand its operands |
| 2315 | // onto our operand list, and recurse to simplify. |
| 2316 | if (Idx < Ops.size()) { |
| 2317 | bool DeletedSMax = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2318 | while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2319 | Ops.erase(Ops.begin()+Idx); |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 2320 | Ops.append(SMax->op_begin(), SMax->op_end()); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2321 | DeletedSMax = true; |
| 2322 | } |
| 2323 | |
| 2324 | if (DeletedSMax) |
| 2325 | return getSMaxExpr(Ops); |
| 2326 | } |
| 2327 | |
| 2328 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 2329 | // so, delete one. Since we sorted the list, these values are required to |
| 2330 | // be adjacent. |
| 2331 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
Dan Gohman | 2828779 | 2010-04-13 16:51:03 +0000 | [diff] [blame] | 2332 | // X smax Y smax Y --> X smax Y |
| 2333 | // X smax Y --> X, if X is always greater than Y |
| 2334 | if (Ops[i] == Ops[i+1] || |
| 2335 | isKnownPredicate(ICmpInst::ICMP_SGE, Ops[i], Ops[i+1])) { |
| 2336 | Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2); |
| 2337 | --i; --e; |
| 2338 | } else if (isKnownPredicate(ICmpInst::ICMP_SLE, Ops[i], Ops[i+1])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2339 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 2340 | --i; --e; |
| 2341 | } |
| 2342 | |
| 2343 | if (Ops.size() == 1) return Ops[0]; |
| 2344 | |
| 2345 | assert(!Ops.empty() && "Reduced smax down to nothing!"); |
| 2346 | |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2347 | // 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] | 2348 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2349 | FoldingSetNodeID ID; |
| 2350 | ID.AddInteger(scSMaxExpr); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2351 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 2352 | ID.AddPointer(Ops[i]); |
| 2353 | void *IP = 0; |
| 2354 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 2355 | const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); |
| 2356 | std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 2357 | SCEV *S = new (SCEVAllocator) SCEVSMaxExpr(ID.Intern(SCEVAllocator), |
| 2358 | O, Ops.size()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2359 | UniqueSCEVs.InsertNode(S, IP); |
| 2360 | return S; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2363 | const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, |
| 2364 | const SCEV *RHS) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2365 | SmallVector<const SCEV *, 2> Ops; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2366 | Ops.push_back(LHS); |
| 2367 | Ops.push_back(RHS); |
| 2368 | return getUMaxExpr(Ops); |
| 2369 | } |
| 2370 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2371 | const SCEV * |
| 2372 | ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2373 | assert(!Ops.empty() && "Cannot get empty umax!"); |
| 2374 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2375 | #ifndef NDEBUG |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 2376 | const Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2377 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 2378 | assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2379 | "SCEVUMaxExpr operand types don't match!"); |
| 2380 | #endif |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2381 | |
| 2382 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 2383 | GroupByComplexity(Ops, LI); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2384 | |
| 2385 | // If there are any constants, fold them together. |
| 2386 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2387 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2388 | ++Idx; |
| 2389 | assert(Idx < Ops.size()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2390 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2391 | // We found two constants, fold them together! |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2392 | ConstantInt *Fold = ConstantInt::get(getContext(), |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2393 | APIntOps::umax(LHSC->getValue()->getValue(), |
| 2394 | RHSC->getValue()->getValue())); |
| 2395 | Ops[0] = getConstant(Fold); |
| 2396 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 2397 | if (Ops.size() == 1) return Ops[0]; |
| 2398 | LHSC = cast<SCEVConstant>(Ops[0]); |
| 2399 | } |
| 2400 | |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 2401 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2402 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) { |
| 2403 | Ops.erase(Ops.begin()); |
| 2404 | --Idx; |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 2405 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) { |
| 2406 | // If we have an umax with a constant maximum-int, it will always be |
| 2407 | // maximum-int. |
| 2408 | return Ops[0]; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2409 | } |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2410 | |
Dan Gohman | 3ab1312 | 2010-04-13 16:49:23 +0000 | [diff] [blame] | 2411 | if (Ops.size() == 1) return Ops[0]; |
| 2412 | } |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2413 | |
| 2414 | // Find the first UMax |
| 2415 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr) |
| 2416 | ++Idx; |
| 2417 | |
| 2418 | // Check to see if one of the operands is a UMax. If so, expand its operands |
| 2419 | // onto our operand list, and recurse to simplify. |
| 2420 | if (Idx < Ops.size()) { |
| 2421 | bool DeletedUMax = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2422 | while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2423 | Ops.erase(Ops.begin()+Idx); |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 2424 | Ops.append(UMax->op_begin(), UMax->op_end()); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2425 | DeletedUMax = true; |
| 2426 | } |
| 2427 | |
| 2428 | if (DeletedUMax) |
| 2429 | return getUMaxExpr(Ops); |
| 2430 | } |
| 2431 | |
| 2432 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 2433 | // so, delete one. Since we sorted the list, these values are required to |
| 2434 | // be adjacent. |
| 2435 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
Dan Gohman | 2828779 | 2010-04-13 16:51:03 +0000 | [diff] [blame] | 2436 | // X umax Y umax Y --> X umax Y |
| 2437 | // X umax Y --> X, if X is always greater than Y |
| 2438 | if (Ops[i] == Ops[i+1] || |
| 2439 | isKnownPredicate(ICmpInst::ICMP_UGE, Ops[i], Ops[i+1])) { |
| 2440 | Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2); |
| 2441 | --i; --e; |
| 2442 | } else if (isKnownPredicate(ICmpInst::ICMP_ULE, Ops[i], Ops[i+1])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2443 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 2444 | --i; --e; |
| 2445 | } |
| 2446 | |
| 2447 | if (Ops.size() == 1) return Ops[0]; |
| 2448 | |
| 2449 | assert(!Ops.empty() && "Reduced umax down to nothing!"); |
| 2450 | |
| 2451 | // Okay, it looks like we really DO need a umax expr. Check to see if we |
| 2452 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2453 | FoldingSetNodeID ID; |
| 2454 | ID.AddInteger(scUMaxExpr); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2455 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 2456 | ID.AddPointer(Ops[i]); |
| 2457 | void *IP = 0; |
| 2458 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 2459 | const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); |
| 2460 | std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 2461 | SCEV *S = new (SCEVAllocator) SCEVUMaxExpr(ID.Intern(SCEVAllocator), |
| 2462 | O, Ops.size()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2463 | UniqueSCEVs.InsertNode(S, IP); |
| 2464 | return S; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2467 | const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS, |
| 2468 | const SCEV *RHS) { |
Dan Gohman | f9a9a99 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2469 | // ~smax(~x, ~y) == smin(x, y). |
| 2470 | return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 2471 | } |
| 2472 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2473 | const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS, |
| 2474 | const SCEV *RHS) { |
Dan Gohman | f9a9a99 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2475 | // ~umax(~x, ~y) == umin(x, y) |
| 2476 | return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 2477 | } |
| 2478 | |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2479 | const SCEV *ScalarEvolution::getSizeOfExpr(const Type *AllocTy) { |
Dan Gohman | 6ab10f6 | 2010-04-12 23:03:26 +0000 | [diff] [blame] | 2480 | // If we have TargetData, we can bypass creating a target-independent |
| 2481 | // constant expression and then folding it back into a ConstantInt. |
| 2482 | // This is just a compile-time optimization. |
| 2483 | if (TD) |
| 2484 | return getConstant(TD->getIntPtrType(getContext()), |
| 2485 | TD->getTypeAllocSize(AllocTy)); |
| 2486 | |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2487 | Constant *C = ConstantExpr::getSizeOf(AllocTy); |
| 2488 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
Dan Gohman | 7000122 | 2010-05-28 16:12:08 +0000 | [diff] [blame] | 2489 | if (Constant *Folded = ConstantFoldConstantExpression(CE, TD)) |
| 2490 | C = Folded; |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2491 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy)); |
| 2492 | return getTruncateOrZeroExtend(getSCEV(C), Ty); |
| 2493 | } |
| 2494 | |
| 2495 | const SCEV *ScalarEvolution::getAlignOfExpr(const Type *AllocTy) { |
| 2496 | Constant *C = ConstantExpr::getAlignOf(AllocTy); |
| 2497 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
Dan Gohman | 7000122 | 2010-05-28 16:12:08 +0000 | [diff] [blame] | 2498 | if (Constant *Folded = ConstantFoldConstantExpression(CE, TD)) |
| 2499 | C = Folded; |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2500 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy)); |
| 2501 | return getTruncateOrZeroExtend(getSCEV(C), Ty); |
| 2502 | } |
| 2503 | |
| 2504 | const SCEV *ScalarEvolution::getOffsetOfExpr(const StructType *STy, |
| 2505 | unsigned FieldNo) { |
Dan Gohman | 6ab10f6 | 2010-04-12 23:03:26 +0000 | [diff] [blame] | 2506 | // If we have TargetData, we can bypass creating a target-independent |
| 2507 | // constant expression and then folding it back into a ConstantInt. |
| 2508 | // This is just a compile-time optimization. |
| 2509 | if (TD) |
| 2510 | return getConstant(TD->getIntPtrType(getContext()), |
| 2511 | TD->getStructLayout(STy)->getElementOffset(FieldNo)); |
| 2512 | |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 2513 | Constant *C = ConstantExpr::getOffsetOf(STy, FieldNo); |
| 2514 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
Dan Gohman | 7000122 | 2010-05-28 16:12:08 +0000 | [diff] [blame] | 2515 | if (Constant *Folded = ConstantFoldConstantExpression(CE, TD)) |
| 2516 | C = Folded; |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2517 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy)); |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 2518 | return getTruncateOrZeroExtend(getSCEV(C), Ty); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2521 | const SCEV *ScalarEvolution::getOffsetOfExpr(const Type *CTy, |
| 2522 | Constant *FieldNo) { |
| 2523 | Constant *C = ConstantExpr::getOffsetOf(CTy, FieldNo); |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 2524 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
Dan Gohman | 7000122 | 2010-05-28 16:12:08 +0000 | [diff] [blame] | 2525 | if (Constant *Folded = ConstantFoldConstantExpression(CE, TD)) |
| 2526 | C = Folded; |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2527 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(CTy)); |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 2528 | return getTruncateOrZeroExtend(getSCEV(C), Ty); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2529 | } |
| 2530 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2531 | const SCEV *ScalarEvolution::getUnknown(Value *V) { |
Dan Gohman | 6bbcba1 | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2532 | // Don't attempt to do anything other than create a SCEVUnknown object |
| 2533 | // here. createSCEV only calls getUnknown after checking for all other |
| 2534 | // interesting possibilities, and any other code that calls getUnknown |
| 2535 | // is doing so in order to hide a value from SCEV canonicalization. |
| 2536 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2537 | FoldingSetNodeID ID; |
| 2538 | ID.AddInteger(scUnknown); |
| 2539 | ID.AddPointer(V); |
| 2540 | void *IP = 0; |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 2541 | if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) { |
| 2542 | assert(cast<SCEVUnknown>(S)->getValue() == V && |
| 2543 | "Stale SCEVUnknown in uniquing map!"); |
| 2544 | return S; |
| 2545 | } |
| 2546 | SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator), V, this, |
| 2547 | FirstUnknown); |
| 2548 | FirstUnknown = cast<SCEVUnknown>(S); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2549 | UniqueSCEVs.InsertNode(S, IP); |
| 2550 | return S; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2553 | //===----------------------------------------------------------------------===// |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2554 | // Basic SCEV Analysis and PHI Idiom Recognition Code |
| 2555 | // |
| 2556 | |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2557 | /// isSCEVable - Test if values of the given type are analyzable within |
| 2558 | /// the SCEV framework. This primarily includes integer types, and it |
| 2559 | /// can optionally include pointer types if the ScalarEvolution class |
| 2560 | /// has access to target-specific information. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2561 | bool ScalarEvolution::isSCEVable(const Type *Ty) const { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2562 | // Integers and pointers are always SCEVable. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2563 | return Ty->isIntegerTy() || Ty->isPointerTy(); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2564 | } |
| 2565 | |
| 2566 | /// getTypeSizeInBits - Return the size in bits of the specified type, |
| 2567 | /// for which isSCEVable must return true. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2568 | uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2569 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2570 | |
| 2571 | // If we have a TargetData, use it! |
| 2572 | if (TD) |
| 2573 | return TD->getTypeSizeInBits(Ty); |
| 2574 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2575 | // Integer types have fixed sizes. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2576 | if (Ty->isIntegerTy()) |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2577 | return Ty->getPrimitiveSizeInBits(); |
| 2578 | |
| 2579 | // The only other support type is pointer. Without TargetData, conservatively |
| 2580 | // assume pointers are 64-bit. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2581 | assert(Ty->isPointerTy() && "isSCEVable permitted a non-SCEVable type!"); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2582 | return 64; |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | /// getEffectiveSCEVType - Return a type with the same bitwidth as |
| 2586 | /// the given type and which represents how SCEV will treat the given |
| 2587 | /// type, for which isSCEVable must return true. For pointer types, |
| 2588 | /// this is the pointer-sized integer type. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2589 | const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2590 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2591 | |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2592 | if (Ty->isIntegerTy()) |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2593 | return Ty; |
| 2594 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2595 | // The only other support type is pointer. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2596 | assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!"); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2597 | if (TD) return TD->getIntPtrType(getContext()); |
| 2598 | |
| 2599 | // Without TargetData, conservatively assume pointers are 64-bit. |
| 2600 | return Type::getInt64Ty(getContext()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2601 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2602 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2603 | const SCEV *ScalarEvolution::getCouldNotCompute() { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2604 | return &CouldNotCompute; |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2607 | /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the |
| 2608 | /// expression and create a new one. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2609 | const SCEV *ScalarEvolution::getSCEV(Value *V) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2610 | assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2611 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2612 | ValueExprMapType::const_iterator I = ValueExprMap.find(V); |
| 2613 | if (I != ValueExprMap.end()) return I->second; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2614 | const SCEV *S = createSCEV(V); |
Dan Gohman | 619d332 | 2010-08-16 16:31:39 +0000 | [diff] [blame] | 2615 | |
| 2616 | // The process of creating a SCEV for V may have caused other SCEVs |
| 2617 | // to have been created, so it's necessary to insert the new entry |
| 2618 | // from scratch, rather than trying to remember the insert position |
| 2619 | // above. |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2620 | ValueExprMap.insert(std::make_pair(SCEVCallbackVH(V, this), S)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2621 | return S; |
| 2622 | } |
| 2623 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2624 | /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V |
| 2625 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2626 | const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) { |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2627 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2628 | return getConstant( |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2629 | cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue()))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2630 | |
| 2631 | const Type *Ty = V->getType(); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2632 | Ty = getEffectiveSCEVType(Ty); |
Owen Anderson | 73c6b71 | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2633 | return getMulExpr(V, |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2634 | getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty)))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
| 2637 | /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2638 | const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) { |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2639 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Owen Anderson | 73c6b71 | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2640 | return getConstant( |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2641 | cast<ConstantInt>(ConstantExpr::getNot(VC->getValue()))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2642 | |
| 2643 | const Type *Ty = V->getType(); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2644 | Ty = getEffectiveSCEVType(Ty); |
Owen Anderson | 73c6b71 | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2645 | const SCEV *AllOnes = |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2646 | getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2647 | return getMinusSCEV(AllOnes, V); |
| 2648 | } |
| 2649 | |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2650 | /// getMinusSCEV - Return LHS-RHS. Minus is represented in SCEV as A+B*-1. |
Chris Lattner | 992efb0 | 2011-01-09 22:26:35 +0000 | [diff] [blame] | 2651 | const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, const SCEV *RHS, |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2652 | SCEV::NoWrapFlags Flags) { |
Andrew Trick | 4dbe200 | 2011-03-15 01:16:14 +0000 | [diff] [blame] | 2653 | assert(!maskFlags(Flags, SCEV::FlagNUW) && "subtraction does not have NUW"); |
| 2654 | |
Dan Gohman | eb4152c | 2010-07-20 16:53:00 +0000 | [diff] [blame] | 2655 | // Fast path: X - X --> 0. |
| 2656 | if (LHS == RHS) |
| 2657 | return getConstant(LHS->getType(), 0); |
| 2658 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2659 | // X - Y --> X + -Y |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2660 | return getAddExpr(LHS, getNegativeSCEV(RHS), Flags); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2664 | /// input value to the specified type. If the type must be extended, it is zero |
| 2665 | /// extended. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2666 | const SCEV * |
Chris Lattner | 992efb0 | 2011-01-09 22:26:35 +0000 | [diff] [blame] | 2667 | ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2668 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2669 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2670 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2671 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2672 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2673 | return V; // No conversion |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2674 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2675 | return getTruncateExpr(V, Ty); |
| 2676 | return getZeroExtendExpr(V, Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2680 | /// input value to the specified type. If the type must be extended, it is sign |
| 2681 | /// extended. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2682 | const SCEV * |
| 2683 | ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2684 | const Type *Ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2685 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2686 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2687 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2688 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2689 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2690 | return V; // No conversion |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2691 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2692 | return getTruncateExpr(V, Ty); |
| 2693 | return getSignExtendExpr(V, Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2696 | /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2697 | /// input value to the specified type. If the type must be extended, it is zero |
| 2698 | /// extended. The conversion must not be narrowing. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2699 | const SCEV * |
| 2700 | ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2701 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2702 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2703 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2704 | "Cannot noop or zero extend with non-integer arguments!"); |
| 2705 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2706 | "getNoopOrZeroExtend cannot truncate!"); |
| 2707 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2708 | return V; // No conversion |
| 2709 | return getZeroExtendExpr(V, Ty); |
| 2710 | } |
| 2711 | |
| 2712 | /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2713 | /// input value to the specified type. If the type must be extended, it is sign |
| 2714 | /// extended. The conversion must not be narrowing. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2715 | const SCEV * |
| 2716 | ScalarEvolution::getNoopOrSignExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2717 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2718 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2719 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2720 | "Cannot noop or sign extend with non-integer arguments!"); |
| 2721 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2722 | "getNoopOrSignExtend cannot truncate!"); |
| 2723 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2724 | return V; // No conversion |
| 2725 | return getSignExtendExpr(V, Ty); |
| 2726 | } |
| 2727 | |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2728 | /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of |
| 2729 | /// the input value to the specified type. If the type must be extended, |
| 2730 | /// it is extended with unspecified bits. The conversion must not be |
| 2731 | /// narrowing. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2732 | const SCEV * |
| 2733 | ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2734 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2735 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2736 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2737 | "Cannot noop or any extend with non-integer arguments!"); |
| 2738 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2739 | "getNoopOrAnyExtend cannot truncate!"); |
| 2740 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2741 | return V; // No conversion |
| 2742 | return getAnyExtendExpr(V, Ty); |
| 2743 | } |
| 2744 | |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2745 | /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the |
| 2746 | /// input value to the specified type. The conversion must not be widening. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2747 | const SCEV * |
| 2748 | ScalarEvolution::getTruncateOrNoop(const SCEV *V, const Type *Ty) { |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2749 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2750 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2751 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2752 | "Cannot truncate or noop with non-integer arguments!"); |
| 2753 | assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) && |
| 2754 | "getTruncateOrNoop cannot extend!"); |
| 2755 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2756 | return V; // No conversion |
| 2757 | return getTruncateExpr(V, Ty); |
| 2758 | } |
| 2759 | |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2760 | /// getUMaxFromMismatchedTypes - Promote the operands to the wider of |
| 2761 | /// the types using zero-extension, and then perform a umax operation |
| 2762 | /// with them. |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2763 | const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS, |
| 2764 | const SCEV *RHS) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2765 | const SCEV *PromotedLHS = LHS; |
| 2766 | const SCEV *PromotedRHS = RHS; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2767 | |
| 2768 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2769 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2770 | else |
| 2771 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2772 | |
| 2773 | return getUMaxExpr(PromotedLHS, PromotedRHS); |
| 2774 | } |
| 2775 | |
Dan Gohman | c9759e8 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2776 | /// getUMinFromMismatchedTypes - Promote the operands to the wider of |
| 2777 | /// the types using zero-extension, and then perform a umin operation |
| 2778 | /// with them. |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2779 | const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS, |
| 2780 | const SCEV *RHS) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2781 | const SCEV *PromotedLHS = LHS; |
| 2782 | const SCEV *PromotedRHS = RHS; |
Dan Gohman | c9759e8 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2783 | |
| 2784 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2785 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2786 | else |
| 2787 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2788 | |
| 2789 | return getUMinExpr(PromotedLHS, PromotedRHS); |
| 2790 | } |
| 2791 | |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 2792 | /// getPointerBase - Transitively follow the chain of pointer-type operands |
| 2793 | /// until reaching a SCEV that does not have a single pointer operand. This |
| 2794 | /// returns a SCEVUnknown pointer for well-formed pointer-type expressions, |
| 2795 | /// but corner cases do exist. |
| 2796 | const SCEV *ScalarEvolution::getPointerBase(const SCEV *V) { |
| 2797 | // A pointer operand may evaluate to a nonpointer expression, such as null. |
| 2798 | if (!V->getType()->isPointerTy()) |
| 2799 | return V; |
| 2800 | |
| 2801 | if (const SCEVCastExpr *Cast = dyn_cast<SCEVCastExpr>(V)) { |
| 2802 | return getPointerBase(Cast->getOperand()); |
| 2803 | } |
| 2804 | else if (const SCEVNAryExpr *NAry = dyn_cast<SCEVNAryExpr>(V)) { |
| 2805 | const SCEV *PtrOp = 0; |
| 2806 | for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end(); |
| 2807 | I != E; ++I) { |
| 2808 | if ((*I)->getType()->isPointerTy()) { |
| 2809 | // Cannot find the base of an expression with multiple pointer operands. |
| 2810 | if (PtrOp) |
| 2811 | return V; |
| 2812 | PtrOp = *I; |
| 2813 | } |
| 2814 | } |
| 2815 | if (!PtrOp) |
| 2816 | return V; |
| 2817 | return getPointerBase(PtrOp); |
| 2818 | } |
| 2819 | return V; |
| 2820 | } |
| 2821 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2822 | /// PushDefUseChildren - Push users of the given Instruction |
| 2823 | /// onto the given Worklist. |
| 2824 | static void |
| 2825 | PushDefUseChildren(Instruction *I, |
| 2826 | SmallVectorImpl<Instruction *> &Worklist) { |
| 2827 | // Push the def-use children onto the Worklist stack. |
| 2828 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 2829 | UI != UE; ++UI) |
Gabor Greif | 96f1d8e | 2010-07-22 13:36:47 +0000 | [diff] [blame] | 2830 | Worklist.push_back(cast<Instruction>(*UI)); |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
| 2833 | /// ForgetSymbolicValue - This looks up computed SCEV values for all |
| 2834 | /// instructions that depend on the given instruction and removes them from |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2835 | /// the ValueExprMapType map if they reference SymName. This is used during PHI |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2836 | /// resolution. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2837 | void |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2838 | ScalarEvolution::ForgetSymbolicName(Instruction *PN, const SCEV *SymName) { |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2839 | SmallVector<Instruction *, 16> Worklist; |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2840 | PushDefUseChildren(PN, Worklist); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2841 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2842 | SmallPtrSet<Instruction *, 8> Visited; |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2843 | Visited.insert(PN); |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2844 | while (!Worklist.empty()) { |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2845 | Instruction *I = Worklist.pop_back_val(); |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2846 | if (!Visited.insert(I)) continue; |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2847 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2848 | ValueExprMapType::iterator It = |
| 2849 | ValueExprMap.find(static_cast<Value *>(I)); |
| 2850 | if (It != ValueExprMap.end()) { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 2851 | const SCEV *Old = It->second; |
| 2852 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2853 | // Short-circuit the def-use traversal if the symbolic name |
| 2854 | // ceases to appear in expressions. |
Dan Gohman | 4ce32db | 2010-11-17 22:27:42 +0000 | [diff] [blame] | 2855 | if (Old != SymName && !hasOperand(Old, SymName)) |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2856 | continue; |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2857 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2858 | // SCEVUnknown for a PHI either means that it has an unrecognized |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2859 | // structure, it's a PHI that's in the progress of being computed |
| 2860 | // by createNodeForPHI, or it's a single-value PHI. In the first case, |
| 2861 | // additional loop trip count information isn't going to change anything. |
| 2862 | // In the second case, createNodeForPHI will perform the necessary |
| 2863 | // updates on its own when it gets to that point. In the third, we do |
| 2864 | // want to forget the SCEVUnknown. |
| 2865 | if (!isa<PHINode>(I) || |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 2866 | !isa<SCEVUnknown>(Old) || |
| 2867 | (I != PN && Old == SymName)) { |
Dan Gohman | 56a7568 | 2010-11-17 23:28:48 +0000 | [diff] [blame] | 2868 | forgetMemoizedResults(Old); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2869 | ValueExprMap.erase(It); |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 2870 | } |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | PushDefUseChildren(I, Worklist); |
| 2874 | } |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2875 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2876 | |
| 2877 | /// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in |
| 2878 | /// a loop header, making it a potential recurrence, or it doesn't. |
| 2879 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2880 | const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) { |
Dan Gohman | 27dead4 | 2010-04-12 07:49:36 +0000 | [diff] [blame] | 2881 | if (const Loop *L = LI->getLoopFor(PN->getParent())) |
| 2882 | if (L->getHeader() == PN->getParent()) { |
| 2883 | // The loop may have multiple entrances or multiple exits; we can analyze |
| 2884 | // this phi as an addrec if it has a unique entry value and a unique |
| 2885 | // backedge value. |
| 2886 | Value *BEValueV = 0, *StartValueV = 0; |
| 2887 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 2888 | Value *V = PN->getIncomingValue(i); |
| 2889 | if (L->contains(PN->getIncomingBlock(i))) { |
| 2890 | if (!BEValueV) { |
| 2891 | BEValueV = V; |
| 2892 | } else if (BEValueV != V) { |
| 2893 | BEValueV = 0; |
| 2894 | break; |
| 2895 | } |
| 2896 | } else if (!StartValueV) { |
| 2897 | StartValueV = V; |
| 2898 | } else if (StartValueV != V) { |
| 2899 | StartValueV = 0; |
| 2900 | break; |
| 2901 | } |
| 2902 | } |
| 2903 | if (BEValueV && StartValueV) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2904 | // While we are analyzing this PHI node, handle its value symbolically. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2905 | const SCEV *SymbolicName = getUnknown(PN); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2906 | assert(ValueExprMap.find(PN) == ValueExprMap.end() && |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2907 | "PHI node already processed?"); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2908 | ValueExprMap.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2909 | |
| 2910 | // Using this symbolic name for the PHI, analyze the value coming around |
| 2911 | // the back-edge. |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2912 | const SCEV *BEValue = getSCEV(BEValueV); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2913 | |
| 2914 | // NOTE: If BEValue is loop invariant, we know that the PHI node just |
| 2915 | // has a special value for the first iteration of the loop. |
| 2916 | |
| 2917 | // If the value coming around the backedge is an add with the symbolic |
| 2918 | // value we just inserted, then we found a simple induction variable! |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2919 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2920 | // If there is a single occurrence of the symbolic value, replace it |
| 2921 | // with a recurrence. |
| 2922 | unsigned FoundIndex = Add->getNumOperands(); |
| 2923 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2924 | if (Add->getOperand(i) == SymbolicName) |
| 2925 | if (FoundIndex == e) { |
| 2926 | FoundIndex = i; |
| 2927 | break; |
| 2928 | } |
| 2929 | |
| 2930 | if (FoundIndex != Add->getNumOperands()) { |
| 2931 | // Create an add with everything but the specified operand. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2932 | SmallVector<const SCEV *, 8> Ops; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2933 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2934 | if (i != FoundIndex) |
| 2935 | Ops.push_back(Add->getOperand(i)); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2936 | const SCEV *Accum = getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2937 | |
| 2938 | // This is not a valid addrec if the step amount is varying each |
| 2939 | // loop iteration, but is not itself an addrec in this loop. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 2940 | if (isLoopInvariant(Accum, L) || |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2941 | (isa<SCEVAddRecExpr>(Accum) && |
| 2942 | cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) { |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2943 | SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2944 | |
| 2945 | // If the increment doesn't overflow, then neither the addrec nor |
| 2946 | // the post-increment will overflow. |
| 2947 | if (const AddOperator *OBO = dyn_cast<AddOperator>(BEValueV)) { |
| 2948 | if (OBO->hasNoUnsignedWrap()) |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2949 | Flags = setFlags(Flags, SCEV::FlagNUW); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2950 | if (OBO->hasNoSignedWrap()) |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2951 | Flags = setFlags(Flags, SCEV::FlagNSW); |
Andrew Trick | 635f718 | 2011-03-09 17:23:39 +0000 | [diff] [blame] | 2952 | } else if (const GEPOperator *GEP = |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2953 | dyn_cast<GEPOperator>(BEValueV)) { |
| 2954 | // If the increment is an inbounds GEP, then we know the address |
| 2955 | // space cannot be wrapped around. We cannot make any guarantee |
| 2956 | // about signed or unsigned overflow because pointers are |
| 2957 | // unsigned but we may have a negative index from the base |
| 2958 | // pointer. |
| 2959 | if (GEP->isInBounds()) |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 2960 | Flags = setFlags(Flags, SCEV::FlagNW); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
Dan Gohman | 27dead4 | 2010-04-12 07:49:36 +0000 | [diff] [blame] | 2963 | const SCEV *StartVal = getSCEV(StartValueV); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2964 | const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags); |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2965 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2966 | // Since the no-wrap flags are on the increment, they apply to the |
| 2967 | // post-incremented value as well. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 2968 | if (isLoopInvariant(Accum, L)) |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2969 | (void)getAddRecExpr(getAddExpr(StartVal, Accum), |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2970 | Accum, L, Flags); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2971 | |
| 2972 | // Okay, for the entire analysis of this edge we assumed the PHI |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2973 | // to be symbolic. We now need to go back and purge all of the |
| 2974 | // entries for the scalars that use the symbolic expression. |
| 2975 | ForgetSymbolicName(PN, SymbolicName); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2976 | ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2977 | return PHISCEV; |
| 2978 | } |
| 2979 | } |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2980 | } else if (const SCEVAddRecExpr *AddRec = |
| 2981 | dyn_cast<SCEVAddRecExpr>(BEValue)) { |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2982 | // Otherwise, this could be a loop like this: |
| 2983 | // i = 0; for (j = 1; ..; ++j) { .... i = j; } |
| 2984 | // In this case, j = {1,+,1} and BEValue is j. |
| 2985 | // Because the other in-value of i (0) fits the evolution of BEValue |
| 2986 | // i really is an addrec evolution. |
| 2987 | if (AddRec->getLoop() == L && AddRec->isAffine()) { |
Dan Gohman | 27dead4 | 2010-04-12 07:49:36 +0000 | [diff] [blame] | 2988 | const SCEV *StartVal = getSCEV(StartValueV); |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2989 | |
| 2990 | // If StartVal = j.start - j.stride, we can use StartVal as the |
| 2991 | // initial step of the addrec evolution. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2992 | if (StartVal == getMinusSCEV(AddRec->getOperand(0), |
Dan Gohman | 5ee60f7 | 2010-04-11 23:44:58 +0000 | [diff] [blame] | 2993 | AddRec->getOperand(1))) { |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2994 | // FIXME: For constant StartVal, we should be able to infer |
| 2995 | // no-wrap flags. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2996 | const SCEV *PHISCEV = |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 2997 | getAddRecExpr(StartVal, AddRec->getOperand(1), L, |
| 2998 | SCEV::FlagAnyWrap); |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2999 | |
| 3000 | // Okay, for the entire analysis of this edge we assumed the PHI |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 3001 | // to be symbolic. We now need to go back and purge all of the |
| 3002 | // entries for the scalars that use the symbolic expression. |
| 3003 | ForgetSymbolicName(PN, SymbolicName); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3004 | ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV; |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 3005 | return PHISCEV; |
| 3006 | } |
| 3007 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3008 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3009 | } |
Dan Gohman | 27dead4 | 2010-04-12 07:49:36 +0000 | [diff] [blame] | 3010 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3011 | |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 3012 | // If the PHI has a single incoming value, follow that value, unless the |
| 3013 | // PHI's incoming blocks are in a different loop, in which case doing so |
| 3014 | // risks breaking LCSSA form. Instcombine would normally zap these, but |
| 3015 | // it doesn't have DominatorTree information, so it may miss cases. |
Duncan Sands | d0c6f3d | 2010-11-18 19:59:41 +0000 | [diff] [blame] | 3016 | if (Value *V = SimplifyInstruction(PN, TD, DT)) |
| 3017 | if (LI->replacementPreservesLCSSAForm(PN, V)) |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 3018 | return getSCEV(V); |
Duncan Sands | 6f8a5dd | 2010-11-17 20:49:12 +0000 | [diff] [blame] | 3019 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3020 | // 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] | 3021 | return getUnknown(PN); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 3024 | /// createNodeForGEP - Expand GEP instructions into add and multiply |
| 3025 | /// operations. This allows them to be analyzed by regular SCEV code. |
| 3026 | /// |
Dan Gohman | d281ed2 | 2009-12-18 02:09:29 +0000 | [diff] [blame] | 3027 | const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) { |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 3028 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 3029 | // Don't blindly transfer the inbounds flag from the GEP instruction to the |
| 3030 | // Add expression, because the Instruction may be guarded by control flow |
| 3031 | // 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] | 3032 | // context. |
Chris Lattner | 8ebaf90 | 2011-02-13 03:14:49 +0000 | [diff] [blame] | 3033 | bool isInBounds = GEP->isInBounds(); |
Dan Gohman | 7a64257 | 2010-06-29 01:41:41 +0000 | [diff] [blame] | 3034 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 3035 | const Type *IntPtrTy = getEffectiveSCEVType(GEP->getType()); |
Dan Gohman | e810b0d | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 3036 | Value *Base = GEP->getOperand(0); |
Dan Gohman | c63a627 | 2009-05-09 00:14:52 +0000 | [diff] [blame] | 3037 | // Don't attempt to analyze GEPs over unsized objects. |
| 3038 | if (!cast<PointerType>(Base->getType())->getElementType()->isSized()) |
| 3039 | return getUnknown(GEP); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 3040 | const SCEV *TotalOffset = getConstant(IntPtrTy, 0); |
Dan Gohman | e810b0d | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 3041 | gep_type_iterator GTI = gep_type_begin(GEP); |
Oscar Fuentes | ee56c42 | 2010-08-02 06:00:15 +0000 | [diff] [blame] | 3042 | for (GetElementPtrInst::op_iterator I = llvm::next(GEP->op_begin()), |
Dan Gohman | e810b0d | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 3043 | E = GEP->op_end(); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 3044 | I != E; ++I) { |
| 3045 | Value *Index = *I; |
| 3046 | // Compute the (potentially symbolic) offset in bytes for this index. |
| 3047 | if (const StructType *STy = dyn_cast<StructType>(*GTI++)) { |
| 3048 | // For a struct, add the member offset. |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 3049 | unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue(); |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 3050 | const SCEV *FieldOffset = getOffsetOfExpr(STy, FieldNo); |
| 3051 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 3052 | // Add the field offset to the running total offset. |
Dan Gohman | 70eff63 | 2010-06-30 17:27:11 +0000 | [diff] [blame] | 3053 | TotalOffset = getAddExpr(TotalOffset, FieldOffset); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 3054 | } else { |
| 3055 | // For an array, add the element offset, explicitly scaled. |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 3056 | const SCEV *ElementSize = getSizeOfExpr(*GTI); |
| 3057 | const SCEV *IndexS = getSCEV(Index); |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 3058 | // Getelementptr indices are signed. |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 3059 | IndexS = getTruncateOrSignExtend(IndexS, IntPtrTy); |
| 3060 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 3061 | // Multiply the index by the element size to compute the element offset. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 3062 | const SCEV *LocalOffset = getMulExpr(IndexS, ElementSize, |
| 3063 | isInBounds ? SCEV::FlagNSW : |
| 3064 | SCEV::FlagAnyWrap); |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 3065 | |
| 3066 | // Add the element offset to the running total offset. |
Dan Gohman | 70eff63 | 2010-06-30 17:27:11 +0000 | [diff] [blame] | 3067 | TotalOffset = getAddExpr(TotalOffset, LocalOffset); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 3068 | } |
| 3069 | } |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 3070 | |
| 3071 | // Get the SCEV for the GEP base. |
| 3072 | const SCEV *BaseS = getSCEV(Base); |
| 3073 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 3074 | // Add the total offset from all the GEP indices to the base. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 3075 | return getAddExpr(BaseS, TotalOffset, |
| 3076 | isInBounds ? SCEV::FlagNSW : SCEV::FlagAnyWrap); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 3079 | /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is |
| 3080 | /// guaranteed to end in (at every loop iteration). It is, at the same time, |
| 3081 | /// the minimum number of times S is divisible by 2. For example, given {4,+,8} |
| 3082 | /// 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] | 3083 | uint32_t |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3084 | ScalarEvolution::GetMinTrailingZeros(const SCEV *S) { |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3085 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Chris Lattner | 8314a0c | 2007-11-23 22:36:49 +0000 | [diff] [blame] | 3086 | return C->getValue()->getValue().countTrailingZeros(); |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 3087 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3088 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S)) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3089 | return std::min(GetMinTrailingZeros(T->getOperand()), |
| 3090 | (uint32_t)getTypeSizeInBits(T->getType())); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 3091 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3092 | if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) { |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3093 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 3094 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 3095 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3098 | if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) { |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3099 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 3100 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 3101 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3104 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 3105 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3106 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 3107 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3108 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 3109 | return MinOpRes; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3112 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 3113 | // The result is the sum of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3114 | uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0)); |
| 3115 | uint32_t BitWidth = getTypeSizeInBits(M->getType()); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 3116 | for (unsigned i = 1, e = M->getNumOperands(); |
| 3117 | SumOpRes != BitWidth && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3118 | SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 3119 | BitWidth); |
| 3120 | return SumOpRes; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 3121 | } |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 3122 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3123 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 3124 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3125 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 3126 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3127 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 3128 | return MinOpRes; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 3129 | } |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 3130 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3131 | if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 3132 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3133 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 3134 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3135 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 3136 | return MinOpRes; |
| 3137 | } |
| 3138 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3139 | if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 3140 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3141 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 3142 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3143 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 3144 | return MinOpRes; |
| 3145 | } |
| 3146 | |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3147 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 3148 | // For a SCEVUnknown, ask ValueTracking. |
| 3149 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 3150 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 3151 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 3152 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones); |
| 3153 | return Zeros.countTrailingOnes(); |
| 3154 | } |
| 3155 | |
| 3156 | // SCEVUDivExpr |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 3157 | return 0; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 3158 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3159 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3160 | /// getUnsignedRange - Determine the unsigned range for a particular SCEV. |
| 3161 | /// |
| 3162 | ConstantRange |
| 3163 | ScalarEvolution::getUnsignedRange(const SCEV *S) { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 3164 | // See if we've computed this range already. |
| 3165 | DenseMap<const SCEV *, ConstantRange>::iterator I = UnsignedRanges.find(S); |
| 3166 | if (I != UnsignedRanges.end()) |
| 3167 | return I->second; |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3168 | |
| 3169 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3170 | return setUnsignedRange(C, ConstantRange(C->getValue()->getValue())); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3171 | |
Dan Gohman | c9c36cb | 2010-01-26 19:19:05 +0000 | [diff] [blame] | 3172 | unsigned BitWidth = getTypeSizeInBits(S->getType()); |
| 3173 | ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true); |
| 3174 | |
| 3175 | // If the value has known zeros, the maximum unsigned value will have those |
| 3176 | // known zeros as well. |
| 3177 | uint32_t TZ = GetMinTrailingZeros(S); |
| 3178 | if (TZ != 0) |
| 3179 | ConservativeResult = |
| 3180 | ConstantRange(APInt::getMinValue(BitWidth), |
| 3181 | APInt::getMaxValue(BitWidth).lshr(TZ).shl(TZ) + 1); |
| 3182 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3183 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 3184 | ConstantRange X = getUnsignedRange(Add->getOperand(0)); |
| 3185 | for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) |
| 3186 | X = X.add(getUnsignedRange(Add->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3187 | return setUnsignedRange(Add, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { |
| 3191 | ConstantRange X = getUnsignedRange(Mul->getOperand(0)); |
| 3192 | for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) |
| 3193 | X = X.multiply(getUnsignedRange(Mul->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3194 | return setUnsignedRange(Mul, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
| 3197 | if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) { |
| 3198 | ConstantRange X = getUnsignedRange(SMax->getOperand(0)); |
| 3199 | for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) |
| 3200 | X = X.smax(getUnsignedRange(SMax->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3201 | return setUnsignedRange(SMax, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3202 | } |
| 3203 | |
| 3204 | if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) { |
| 3205 | ConstantRange X = getUnsignedRange(UMax->getOperand(0)); |
| 3206 | for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) |
| 3207 | X = X.umax(getUnsignedRange(UMax->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3208 | return setUnsignedRange(UMax, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3209 | } |
| 3210 | |
| 3211 | if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { |
| 3212 | ConstantRange X = getUnsignedRange(UDiv->getLHS()); |
| 3213 | ConstantRange Y = getUnsignedRange(UDiv->getRHS()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3214 | return setUnsignedRange(UDiv, ConservativeResult.intersectWith(X.udiv(Y))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
| 3217 | if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { |
| 3218 | ConstantRange X = getUnsignedRange(ZExt->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3219 | return setUnsignedRange(ZExt, |
| 3220 | ConservativeResult.intersectWith(X.zeroExtend(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { |
| 3224 | ConstantRange X = getUnsignedRange(SExt->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3225 | return setUnsignedRange(SExt, |
| 3226 | ConservativeResult.intersectWith(X.signExtend(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { |
| 3230 | ConstantRange X = getUnsignedRange(Trunc->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3231 | return setUnsignedRange(Trunc, |
| 3232 | ConservativeResult.intersectWith(X.truncate(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3235 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3236 | // If there's no unsigned wrap, the value will never be less than its |
| 3237 | // initial value. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 3238 | if (AddRec->getNoWrapFlags(SCEV::FlagNUW)) |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3239 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(AddRec->getStart())) |
Dan Gohman | bca091d | 2010-04-12 23:08:18 +0000 | [diff] [blame] | 3240 | if (!C->getValue()->isZero()) |
Dan Gohman | bc7129f | 2010-04-11 22:12:18 +0000 | [diff] [blame] | 3241 | ConservativeResult = |
Dan Gohman | 8a18d6b | 2010-06-30 06:58:35 +0000 | [diff] [blame] | 3242 | ConservativeResult.intersectWith( |
| 3243 | ConstantRange(C->getValue()->getValue(), APInt(BitWidth, 0))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3244 | |
| 3245 | // TODO: non-affine addrec |
Dan Gohman | c9c36cb | 2010-01-26 19:19:05 +0000 | [diff] [blame] | 3246 | if (AddRec->isAffine()) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3247 | const Type *Ty = AddRec->getType(); |
| 3248 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop()); |
Dan Gohman | c9c36cb | 2010-01-26 19:19:05 +0000 | [diff] [blame] | 3249 | if (!isa<SCEVCouldNotCompute>(MaxBECount) && |
| 3250 | getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3251 | MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty); |
| 3252 | |
| 3253 | const SCEV *Start = AddRec->getStart(); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3254 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3255 | |
| 3256 | ConstantRange StartRange = getUnsignedRange(Start); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3257 | ConstantRange StepRange = getSignedRange(Step); |
| 3258 | ConstantRange MaxBECountRange = getUnsignedRange(MaxBECount); |
| 3259 | ConstantRange EndRange = |
| 3260 | StartRange.add(MaxBECountRange.multiply(StepRange)); |
| 3261 | |
| 3262 | // Check for overflow. This must be done with ConstantRange arithmetic |
| 3263 | // because we could be called from within the ScalarEvolution overflow |
| 3264 | // checking code. |
| 3265 | ConstantRange ExtStartRange = StartRange.zextOrTrunc(BitWidth*2+1); |
| 3266 | ConstantRange ExtStepRange = StepRange.sextOrTrunc(BitWidth*2+1); |
| 3267 | ConstantRange ExtMaxBECountRange = |
| 3268 | MaxBECountRange.zextOrTrunc(BitWidth*2+1); |
| 3269 | ConstantRange ExtEndRange = EndRange.zextOrTrunc(BitWidth*2+1); |
| 3270 | if (ExtStartRange.add(ExtMaxBECountRange.multiply(ExtStepRange)) != |
| 3271 | ExtEndRange) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3272 | return setUnsignedRange(AddRec, ConservativeResult); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3273 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3274 | APInt Min = APIntOps::umin(StartRange.getUnsignedMin(), |
| 3275 | EndRange.getUnsignedMin()); |
| 3276 | APInt Max = APIntOps::umax(StartRange.getUnsignedMax(), |
| 3277 | EndRange.getUnsignedMax()); |
| 3278 | if (Min.isMinValue() && Max.isMaxValue()) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3279 | return setUnsignedRange(AddRec, ConservativeResult); |
| 3280 | return setUnsignedRange(AddRec, |
| 3281 | ConservativeResult.intersectWith(ConstantRange(Min, Max+1))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3282 | } |
| 3283 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3284 | |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3285 | return setUnsignedRange(AddRec, ConservativeResult); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 3289 | // For a SCEVUnknown, ask ValueTracking. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3290 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 3291 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 3292 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD); |
Dan Gohman | 746f3b1 | 2009-07-20 22:34:18 +0000 | [diff] [blame] | 3293 | if (Ones == ~Zeros + 1) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3294 | return setUnsignedRange(U, ConservativeResult); |
| 3295 | return setUnsignedRange(U, |
| 3296 | ConservativeResult.intersectWith(ConstantRange(Ones, ~Zeros + 1))); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3299 | return setUnsignedRange(S, ConservativeResult); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3302 | /// getSignedRange - Determine the signed range for a particular SCEV. |
| 3303 | /// |
| 3304 | ConstantRange |
| 3305 | ScalarEvolution::getSignedRange(const SCEV *S) { |
Dan Gohman | a3bbf24 | 2011-01-24 17:54:18 +0000 | [diff] [blame] | 3306 | // See if we've computed this range already. |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 3307 | DenseMap<const SCEV *, ConstantRange>::iterator I = SignedRanges.find(S); |
| 3308 | if (I != SignedRanges.end()) |
| 3309 | return I->second; |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3310 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3311 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3312 | return setSignedRange(C, ConstantRange(C->getValue()->getValue())); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3313 | |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 3314 | unsigned BitWidth = getTypeSizeInBits(S->getType()); |
| 3315 | ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true); |
| 3316 | |
| 3317 | // If the value has known zeros, the maximum signed value will have those |
| 3318 | // known zeros as well. |
| 3319 | uint32_t TZ = GetMinTrailingZeros(S); |
| 3320 | if (TZ != 0) |
| 3321 | ConservativeResult = |
| 3322 | ConstantRange(APInt::getSignedMinValue(BitWidth), |
| 3323 | APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1); |
| 3324 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3325 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 3326 | ConstantRange X = getSignedRange(Add->getOperand(0)); |
| 3327 | for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) |
| 3328 | X = X.add(getSignedRange(Add->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3329 | return setSignedRange(Add, ConservativeResult.intersectWith(X)); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3330 | } |
| 3331 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3332 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { |
| 3333 | ConstantRange X = getSignedRange(Mul->getOperand(0)); |
| 3334 | for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) |
| 3335 | X = X.multiply(getSignedRange(Mul->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3336 | return setSignedRange(Mul, ConservativeResult.intersectWith(X)); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3339 | if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) { |
| 3340 | ConstantRange X = getSignedRange(SMax->getOperand(0)); |
| 3341 | for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) |
| 3342 | X = X.smax(getSignedRange(SMax->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3343 | return setSignedRange(SMax, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3344 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3345 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3346 | if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) { |
| 3347 | ConstantRange X = getSignedRange(UMax->getOperand(0)); |
| 3348 | for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) |
| 3349 | X = X.umax(getSignedRange(UMax->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3350 | return setSignedRange(UMax, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3351 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3352 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3353 | if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { |
| 3354 | ConstantRange X = getSignedRange(UDiv->getLHS()); |
| 3355 | ConstantRange Y = getSignedRange(UDiv->getRHS()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3356 | return setSignedRange(UDiv, ConservativeResult.intersectWith(X.udiv(Y))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3357 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3358 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3359 | if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { |
| 3360 | ConstantRange X = getSignedRange(ZExt->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3361 | return setSignedRange(ZExt, |
| 3362 | ConservativeResult.intersectWith(X.zeroExtend(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { |
| 3366 | ConstantRange X = getSignedRange(SExt->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3367 | return setSignedRange(SExt, |
| 3368 | ConservativeResult.intersectWith(X.signExtend(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
| 3371 | if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { |
| 3372 | ConstantRange X = getSignedRange(Trunc->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3373 | return setSignedRange(Trunc, |
| 3374 | ConservativeResult.intersectWith(X.truncate(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3377 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3378 | // If there's no signed wrap, and all the operands have the same sign or |
| 3379 | // zero, the value won't ever change sign. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 3380 | if (AddRec->getNoWrapFlags(SCEV::FlagNSW)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3381 | bool AllNonNeg = true; |
| 3382 | bool AllNonPos = true; |
| 3383 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { |
| 3384 | if (!isKnownNonNegative(AddRec->getOperand(i))) AllNonNeg = false; |
| 3385 | if (!isKnownNonPositive(AddRec->getOperand(i))) AllNonPos = false; |
| 3386 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3387 | if (AllNonNeg) |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 3388 | ConservativeResult = ConservativeResult.intersectWith( |
| 3389 | ConstantRange(APInt(BitWidth, 0), |
| 3390 | APInt::getSignedMinValue(BitWidth))); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3391 | else if (AllNonPos) |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 3392 | ConservativeResult = ConservativeResult.intersectWith( |
| 3393 | ConstantRange(APInt::getSignedMinValue(BitWidth), |
| 3394 | APInt(BitWidth, 1))); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3395 | } |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3396 | |
| 3397 | // TODO: non-affine addrec |
Dan Gohman | c9c36cb | 2010-01-26 19:19:05 +0000 | [diff] [blame] | 3398 | if (AddRec->isAffine()) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3399 | const Type *Ty = AddRec->getType(); |
| 3400 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop()); |
Dan Gohman | c9c36cb | 2010-01-26 19:19:05 +0000 | [diff] [blame] | 3401 | if (!isa<SCEVCouldNotCompute>(MaxBECount) && |
| 3402 | getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3403 | MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty); |
| 3404 | |
| 3405 | const SCEV *Start = AddRec->getStart(); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3406 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3407 | |
| 3408 | ConstantRange StartRange = getSignedRange(Start); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3409 | ConstantRange StepRange = getSignedRange(Step); |
| 3410 | ConstantRange MaxBECountRange = getUnsignedRange(MaxBECount); |
| 3411 | ConstantRange EndRange = |
| 3412 | StartRange.add(MaxBECountRange.multiply(StepRange)); |
| 3413 | |
| 3414 | // Check for overflow. This must be done with ConstantRange arithmetic |
| 3415 | // because we could be called from within the ScalarEvolution overflow |
| 3416 | // checking code. |
| 3417 | ConstantRange ExtStartRange = StartRange.sextOrTrunc(BitWidth*2+1); |
| 3418 | ConstantRange ExtStepRange = StepRange.sextOrTrunc(BitWidth*2+1); |
| 3419 | ConstantRange ExtMaxBECountRange = |
| 3420 | MaxBECountRange.zextOrTrunc(BitWidth*2+1); |
| 3421 | ConstantRange ExtEndRange = EndRange.sextOrTrunc(BitWidth*2+1); |
| 3422 | if (ExtStartRange.add(ExtMaxBECountRange.multiply(ExtStepRange)) != |
| 3423 | ExtEndRange) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3424 | return setSignedRange(AddRec, ConservativeResult); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3425 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3426 | APInt Min = APIntOps::smin(StartRange.getSignedMin(), |
| 3427 | EndRange.getSignedMin()); |
| 3428 | APInt Max = APIntOps::smax(StartRange.getSignedMax(), |
| 3429 | EndRange.getSignedMax()); |
| 3430 | if (Min.isMinSignedValue() && Max.isMaxSignedValue()) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3431 | return setSignedRange(AddRec, ConservativeResult); |
| 3432 | return setSignedRange(AddRec, |
| 3433 | ConservativeResult.intersectWith(ConstantRange(Min, Max+1))); |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3434 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3435 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3436 | |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3437 | return setSignedRange(AddRec, ConservativeResult); |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3440 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 3441 | // For a SCEVUnknown, ask ValueTracking. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3442 | if (!U->getValue()->getType()->isIntegerTy() && !TD) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3443 | return setSignedRange(U, ConservativeResult); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3444 | unsigned NS = ComputeNumSignBits(U->getValue(), TD); |
| 3445 | if (NS == 1) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3446 | return setSignedRange(U, ConservativeResult); |
| 3447 | return setSignedRange(U, ConservativeResult.intersectWith( |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3448 | ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1), |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3449 | APInt::getSignedMaxValue(BitWidth).ashr(NS - 1)+1))); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3452 | return setSignedRange(S, ConservativeResult); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3455 | /// createSCEV - We know that there is no SCEV for the specified value. |
| 3456 | /// Analyze the expression. |
| 3457 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3458 | const SCEV *ScalarEvolution::createSCEV(Value *V) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 3459 | if (!isSCEVable(V->getType())) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3460 | return getUnknown(V); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3461 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3462 | unsigned Opcode = Instruction::UserOp1; |
Dan Gohman | 4ecbca5 | 2010-03-09 23:46:50 +0000 | [diff] [blame] | 3463 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3464 | Opcode = I->getOpcode(); |
Dan Gohman | 4ecbca5 | 2010-03-09 23:46:50 +0000 | [diff] [blame] | 3465 | |
| 3466 | // Don't attempt to analyze instructions in blocks that aren't |
| 3467 | // reachable. Such instructions don't matter, and they aren't required |
| 3468 | // to obey basic rules for definitions dominating uses which this |
| 3469 | // analysis depends on. |
| 3470 | if (!DT->isReachableFromEntry(I->getParent())) |
| 3471 | return getUnknown(V); |
| 3472 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3473 | Opcode = CE->getOpcode(); |
Dan Gohman | 6bbcba1 | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 3474 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 3475 | return getConstant(CI); |
| 3476 | else if (isa<ConstantPointerNull>(V)) |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 3477 | return getConstant(V->getType(), 0); |
Dan Gohman | 2681232 | 2009-08-25 17:49:57 +0000 | [diff] [blame] | 3478 | else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) |
| 3479 | return GA->mayBeOverridden() ? getUnknown(V) : getSCEV(GA->getAliasee()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3480 | else |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3481 | return getUnknown(V); |
Chris Lattner | 2811f2a | 2007-04-02 05:41:38 +0000 | [diff] [blame] | 3482 | |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 3483 | Operator *U = cast<Operator>(V); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3484 | switch (Opcode) { |
Dan Gohman | d3f171d | 2010-08-16 16:03:49 +0000 | [diff] [blame] | 3485 | case Instruction::Add: { |
| 3486 | // The simple thing to do would be to just call getSCEV on both operands |
| 3487 | // and call getAddExpr with the result. However if we're looking at a |
| 3488 | // bunch of things all added together, this can be quite inefficient, |
| 3489 | // because it leads to N-1 getAddExpr calls for N ultimate operands. |
| 3490 | // Instead, gather up all the operands and make a single getAddExpr call. |
| 3491 | // LLVM IR canonical form means we need only traverse the left operands. |
| 3492 | SmallVector<const SCEV *, 4> AddOps; |
| 3493 | AddOps.push_back(getSCEV(U->getOperand(1))); |
Dan Gohman | 3f19c09 | 2010-08-31 22:53:17 +0000 | [diff] [blame] | 3494 | for (Value *Op = U->getOperand(0); ; Op = U->getOperand(0)) { |
| 3495 | unsigned Opcode = Op->getValueID() - Value::InstructionVal; |
| 3496 | if (Opcode != Instruction::Add && Opcode != Instruction::Sub) |
| 3497 | break; |
Dan Gohman | d3f171d | 2010-08-16 16:03:49 +0000 | [diff] [blame] | 3498 | U = cast<Operator>(Op); |
Dan Gohman | 3f19c09 | 2010-08-31 22:53:17 +0000 | [diff] [blame] | 3499 | const SCEV *Op1 = getSCEV(U->getOperand(1)); |
| 3500 | if (Opcode == Instruction::Sub) |
| 3501 | AddOps.push_back(getNegativeSCEV(Op1)); |
| 3502 | else |
| 3503 | AddOps.push_back(Op1); |
Dan Gohman | d3f171d | 2010-08-16 16:03:49 +0000 | [diff] [blame] | 3504 | } |
| 3505 | AddOps.push_back(getSCEV(U->getOperand(0))); |
| 3506 | return getAddExpr(AddOps); |
| 3507 | } |
| 3508 | case Instruction::Mul: { |
| 3509 | // See the Add code above. |
| 3510 | SmallVector<const SCEV *, 4> MulOps; |
| 3511 | MulOps.push_back(getSCEV(U->getOperand(1))); |
| 3512 | for (Value *Op = U->getOperand(0); |
Andrew Trick | 635f718 | 2011-03-09 17:23:39 +0000 | [diff] [blame] | 3513 | Op->getValueID() == Instruction::Mul + Value::InstructionVal; |
Dan Gohman | d3f171d | 2010-08-16 16:03:49 +0000 | [diff] [blame] | 3514 | Op = U->getOperand(0)) { |
| 3515 | U = cast<Operator>(Op); |
| 3516 | MulOps.push_back(getSCEV(U->getOperand(1))); |
| 3517 | } |
| 3518 | MulOps.push_back(getSCEV(U->getOperand(0))); |
| 3519 | return getMulExpr(MulOps); |
| 3520 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3521 | case Instruction::UDiv: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3522 | return getUDivExpr(getSCEV(U->getOperand(0)), |
| 3523 | getSCEV(U->getOperand(1))); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3524 | case Instruction::Sub: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3525 | return getMinusSCEV(getSCEV(U->getOperand(0)), |
| 3526 | getSCEV(U->getOperand(1))); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3527 | case Instruction::And: |
| 3528 | // For an expression like x&255 that merely masks off the high bits, |
| 3529 | // use zext(trunc(x)) as the SCEV expression. |
| 3530 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 2c73d5f | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 3531 | if (CI->isNullValue()) |
| 3532 | return getSCEV(U->getOperand(1)); |
Dan Gohman | d6c3295 | 2009-04-27 01:41:10 +0000 | [diff] [blame] | 3533 | if (CI->isAllOnesValue()) |
| 3534 | return getSCEV(U->getOperand(0)); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3535 | const APInt &A = CI->getValue(); |
Dan Gohman | 61ffa8e | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 3536 | |
| 3537 | // Instcombine's ShrinkDemandedConstant may strip bits out of |
| 3538 | // constants, obscuring what would otherwise be a low-bits mask. |
| 3539 | // Use ComputeMaskedBits to compute what ShrinkDemandedConstant |
| 3540 | // knew about to reconstruct a low-bits mask value. |
| 3541 | unsigned LZ = A.countLeadingZeros(); |
| 3542 | unsigned BitWidth = A.getBitWidth(); |
| 3543 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
| 3544 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3545 | ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD); |
| 3546 | |
| 3547 | APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ); |
| 3548 | |
Dan Gohman | fc3641b | 2009-06-17 23:54:37 +0000 | [diff] [blame] | 3549 | if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask)) |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3550 | return |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3551 | getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3552 | IntegerType::get(getContext(), BitWidth - LZ)), |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3553 | U->getType()); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3554 | } |
| 3555 | break; |
Dan Gohman | 61ffa8e | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 3556 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3557 | case Instruction::Or: |
| 3558 | // If the RHS of the Or is a constant, we may have something like: |
| 3559 | // X*4+1 which got turned into X*4|1. Handle this as an Add so loop |
| 3560 | // optimizations will transparently handle this case. |
| 3561 | // |
| 3562 | // In order for this transformation to be safe, the LHS must be of the |
| 3563 | // form X*(2^n) and the Or constant must be less than 2^n. |
| 3564 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3565 | const SCEV *LHS = getSCEV(U->getOperand(0)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3566 | const APInt &CIVal = CI->getValue(); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3567 | if (GetMinTrailingZeros(LHS) >= |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 3568 | (CIVal.getBitWidth() - CIVal.countLeadingZeros())) { |
| 3569 | // Build a plain add SCEV. |
| 3570 | const SCEV *S = getAddExpr(LHS, getSCEV(CI)); |
| 3571 | // If the LHS of the add was an addrec and it has no-wrap flags, |
| 3572 | // transfer the no-wrap flags, since an or won't introduce a wrap. |
| 3573 | if (const SCEVAddRecExpr *NewAR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 3574 | const SCEVAddRecExpr *OldAR = cast<SCEVAddRecExpr>(LHS); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 3575 | const_cast<SCEVAddRecExpr *>(NewAR)->setNoWrapFlags( |
| 3576 | OldAR->getNoWrapFlags()); |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 3577 | } |
| 3578 | return S; |
| 3579 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3580 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3581 | break; |
| 3582 | case Instruction::Xor: |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3583 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3584 | // If the RHS of the xor is a signbit, then this is just an add. |
| 3585 | // Instcombine turns add of signbit into xor as a strength reduction step. |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3586 | if (CI->getValue().isSignBit()) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3587 | return getAddExpr(getSCEV(U->getOperand(0)), |
| 3588 | getSCEV(U->getOperand(1))); |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3589 | |
| 3590 | // 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] | 3591 | if (CI->isAllOnesValue()) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3592 | return getNotSCEV(getSCEV(U->getOperand(0))); |
Dan Gohman | 10978bd | 2009-05-18 16:29:04 +0000 | [diff] [blame] | 3593 | |
| 3594 | // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask. |
| 3595 | // This is a variant of the check for xor with -1, and it handles |
| 3596 | // the case where instcombine has trimmed non-demanded bits out |
| 3597 | // of an xor with -1. |
| 3598 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0))) |
| 3599 | if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1))) |
| 3600 | if (BO->getOpcode() == Instruction::And && |
| 3601 | LCI->getValue() == CI->getValue()) |
| 3602 | if (const SCEVZeroExtendExpr *Z = |
Dan Gohman | 3034c10 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 3603 | dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) { |
Dan Gohman | 8205283 | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 3604 | const Type *UTy = U->getType(); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3605 | const SCEV *Z0 = Z->getOperand(); |
Dan Gohman | 8205283 | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 3606 | const Type *Z0Ty = Z0->getType(); |
| 3607 | unsigned Z0TySize = getTypeSizeInBits(Z0Ty); |
| 3608 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 3609 | // 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] | 3610 | // mask off the high bits. Complement the operand and |
| 3611 | // re-apply the zext. |
| 3612 | if (APIntOps::isMask(Z0TySize, CI->getValue())) |
| 3613 | return getZeroExtendExpr(getNotSCEV(Z0), UTy); |
| 3614 | |
| 3615 | // If C is a single bit, it may be in the sign-bit position |
| 3616 | // before the zero-extend. In this case, represent the xor |
| 3617 | // using an add, which is equivalent, and re-apply the zext. |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 3618 | APInt Trunc = CI->getValue().trunc(Z0TySize); |
| 3619 | if (Trunc.zext(getTypeSizeInBits(UTy)) == CI->getValue() && |
Dan Gohman | 8205283 | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 3620 | Trunc.isSignBit()) |
| 3621 | return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)), |
| 3622 | UTy); |
Dan Gohman | 3034c10 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 3623 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3624 | } |
| 3625 | break; |
| 3626 | |
| 3627 | case Instruction::Shl: |
| 3628 | // Turn shift left of a constant amount into a multiply. |
| 3629 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 3630 | uint32_t BitWidth = cast<IntegerType>(U->getType())->getBitWidth(); |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3631 | |
| 3632 | // If the shift count is not less than the bitwidth, the result of |
| 3633 | // the shift is undefined. Don't try to analyze it, because the |
| 3634 | // resolution chosen here may differ from the resolution chosen in |
| 3635 | // other parts of the compiler. |
| 3636 | if (SA->getValue().uge(BitWidth)) |
| 3637 | break; |
| 3638 | |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3639 | Constant *X = ConstantInt::get(getContext(), |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3640 | APInt(BitWidth, 1).shl(SA->getZExtValue())); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3641 | return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3642 | } |
| 3643 | break; |
| 3644 | |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3645 | case Instruction::LShr: |
Nick Lewycky | 789558d | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 3646 | // Turn logical shift right of a constant into a unsigned divide. |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3647 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 3648 | uint32_t BitWidth = cast<IntegerType>(U->getType())->getBitWidth(); |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3649 | |
| 3650 | // If the shift count is not less than the bitwidth, the result of |
| 3651 | // the shift is undefined. Don't try to analyze it, because the |
| 3652 | // resolution chosen here may differ from the resolution chosen in |
| 3653 | // other parts of the compiler. |
| 3654 | if (SA->getValue().uge(BitWidth)) |
| 3655 | break; |
| 3656 | |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3657 | Constant *X = ConstantInt::get(getContext(), |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3658 | APInt(BitWidth, 1).shl(SA->getZExtValue())); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3659 | return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3660 | } |
| 3661 | break; |
| 3662 | |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3663 | case Instruction::AShr: |
| 3664 | // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression. |
| 3665 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3666 | if (Operator *L = dyn_cast<Operator>(U->getOperand(0))) |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3667 | if (L->getOpcode() == Instruction::Shl && |
| 3668 | L->getOperand(1) == U->getOperand(1)) { |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3669 | uint64_t BitWidth = getTypeSizeInBits(U->getType()); |
| 3670 | |
| 3671 | // If the shift count is not less than the bitwidth, the result of |
| 3672 | // the shift is undefined. Don't try to analyze it, because the |
| 3673 | // resolution chosen here may differ from the resolution chosen in |
| 3674 | // other parts of the compiler. |
| 3675 | if (CI->getValue().uge(BitWidth)) |
| 3676 | break; |
| 3677 | |
Dan Gohman | 2c73d5f | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 3678 | uint64_t Amt = BitWidth - CI->getZExtValue(); |
| 3679 | if (Amt == BitWidth) |
| 3680 | return getSCEV(L->getOperand(0)); // shift by zero --> noop |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3681 | return |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3682 | getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)), |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3683 | IntegerType::get(getContext(), |
| 3684 | Amt)), |
| 3685 | U->getType()); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3686 | } |
| 3687 | break; |
| 3688 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3689 | case Instruction::Trunc: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3690 | return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3691 | |
| 3692 | case Instruction::ZExt: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3693 | return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3694 | |
| 3695 | case Instruction::SExt: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3696 | return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3697 | |
| 3698 | case Instruction::BitCast: |
| 3699 | // BitCasts are no-op casts so we just eliminate the cast. |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 3700 | if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3701 | return getSCEV(U->getOperand(0)); |
| 3702 | break; |
| 3703 | |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 3704 | // It's tempting to handle inttoptr and ptrtoint as no-ops, however this can |
| 3705 | // lead to pointer expressions which cannot safely be expanded to GEPs, |
| 3706 | // because ScalarEvolution doesn't respect the GEP aliasing rules when |
| 3707 | // simplifying integer expressions. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3708 | |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 3709 | case Instruction::GetElementPtr: |
Dan Gohman | d281ed2 | 2009-12-18 02:09:29 +0000 | [diff] [blame] | 3710 | return createNodeForGEP(cast<GEPOperator>(U)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3711 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3712 | case Instruction::PHI: |
| 3713 | return createNodeForPHI(cast<PHINode>(U)); |
| 3714 | |
| 3715 | case Instruction::Select: |
| 3716 | // This could be a smax or umax that was lowered earlier. |
| 3717 | // Try to recover it. |
| 3718 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) { |
| 3719 | Value *LHS = ICI->getOperand(0); |
| 3720 | Value *RHS = ICI->getOperand(1); |
| 3721 | switch (ICI->getPredicate()) { |
| 3722 | case ICmpInst::ICMP_SLT: |
| 3723 | case ICmpInst::ICMP_SLE: |
| 3724 | std::swap(LHS, RHS); |
| 3725 | // fall through |
| 3726 | case ICmpInst::ICMP_SGT: |
| 3727 | case ICmpInst::ICMP_SGE: |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3728 | // a >s b ? a+x : b+x -> smax(a, b)+x |
| 3729 | // a >s b ? b+x : a+x -> smin(a, b)+x |
| 3730 | if (LHS->getType() == U->getType()) { |
| 3731 | const SCEV *LS = getSCEV(LHS); |
| 3732 | const SCEV *RS = getSCEV(RHS); |
| 3733 | const SCEV *LA = getSCEV(U->getOperand(1)); |
| 3734 | const SCEV *RA = getSCEV(U->getOperand(2)); |
| 3735 | const SCEV *LDiff = getMinusSCEV(LA, LS); |
| 3736 | const SCEV *RDiff = getMinusSCEV(RA, RS); |
| 3737 | if (LDiff == RDiff) |
| 3738 | return getAddExpr(getSMaxExpr(LS, RS), LDiff); |
| 3739 | LDiff = getMinusSCEV(LA, RS); |
| 3740 | RDiff = getMinusSCEV(RA, LS); |
| 3741 | if (LDiff == RDiff) |
| 3742 | return getAddExpr(getSMinExpr(LS, RS), LDiff); |
| 3743 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3744 | break; |
| 3745 | case ICmpInst::ICMP_ULT: |
| 3746 | case ICmpInst::ICMP_ULE: |
| 3747 | std::swap(LHS, RHS); |
| 3748 | // fall through |
| 3749 | case ICmpInst::ICMP_UGT: |
| 3750 | case ICmpInst::ICMP_UGE: |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3751 | // a >u b ? a+x : b+x -> umax(a, b)+x |
| 3752 | // a >u b ? b+x : a+x -> umin(a, b)+x |
| 3753 | if (LHS->getType() == U->getType()) { |
| 3754 | const SCEV *LS = getSCEV(LHS); |
| 3755 | const SCEV *RS = getSCEV(RHS); |
| 3756 | const SCEV *LA = getSCEV(U->getOperand(1)); |
| 3757 | const SCEV *RA = getSCEV(U->getOperand(2)); |
| 3758 | const SCEV *LDiff = getMinusSCEV(LA, LS); |
| 3759 | const SCEV *RDiff = getMinusSCEV(RA, RS); |
| 3760 | if (LDiff == RDiff) |
| 3761 | return getAddExpr(getUMaxExpr(LS, RS), LDiff); |
| 3762 | LDiff = getMinusSCEV(LA, RS); |
| 3763 | RDiff = getMinusSCEV(RA, LS); |
| 3764 | if (LDiff == RDiff) |
| 3765 | return getAddExpr(getUMinExpr(LS, RS), LDiff); |
| 3766 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3767 | break; |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3768 | case ICmpInst::ICMP_NE: |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3769 | // n != 0 ? n+x : 1+x -> umax(n, 1)+x |
| 3770 | if (LHS->getType() == U->getType() && |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3771 | isa<ConstantInt>(RHS) && |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3772 | cast<ConstantInt>(RHS)->isZero()) { |
| 3773 | const SCEV *One = getConstant(LHS->getType(), 1); |
| 3774 | const SCEV *LS = getSCEV(LHS); |
| 3775 | const SCEV *LA = getSCEV(U->getOperand(1)); |
| 3776 | const SCEV *RA = getSCEV(U->getOperand(2)); |
| 3777 | const SCEV *LDiff = getMinusSCEV(LA, LS); |
| 3778 | const SCEV *RDiff = getMinusSCEV(RA, One); |
| 3779 | if (LDiff == RDiff) |
Dan Gohman | 58a85b9 | 2010-08-13 20:17:14 +0000 | [diff] [blame] | 3780 | return getAddExpr(getUMaxExpr(One, LS), LDiff); |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3781 | } |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3782 | break; |
| 3783 | case ICmpInst::ICMP_EQ: |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3784 | // n == 0 ? 1+x : n+x -> umax(n, 1)+x |
| 3785 | if (LHS->getType() == U->getType() && |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3786 | isa<ConstantInt>(RHS) && |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3787 | cast<ConstantInt>(RHS)->isZero()) { |
| 3788 | const SCEV *One = getConstant(LHS->getType(), 1); |
| 3789 | const SCEV *LS = getSCEV(LHS); |
| 3790 | const SCEV *LA = getSCEV(U->getOperand(1)); |
| 3791 | const SCEV *RA = getSCEV(U->getOperand(2)); |
| 3792 | const SCEV *LDiff = getMinusSCEV(LA, One); |
| 3793 | const SCEV *RDiff = getMinusSCEV(RA, LS); |
| 3794 | if (LDiff == RDiff) |
Dan Gohman | 58a85b9 | 2010-08-13 20:17:14 +0000 | [diff] [blame] | 3795 | return getAddExpr(getUMaxExpr(One, LS), LDiff); |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3796 | } |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3797 | break; |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3798 | default: |
| 3799 | break; |
| 3800 | } |
| 3801 | } |
| 3802 | |
| 3803 | default: // We cannot analyze this expression. |
| 3804 | break; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3805 | } |
| 3806 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3807 | return getUnknown(V); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3808 | } |
| 3809 | |
| 3810 | |
| 3811 | |
| 3812 | //===----------------------------------------------------------------------===// |
| 3813 | // Iteration Count Computation Code |
| 3814 | // |
| 3815 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3816 | /// getBackedgeTakenCount - If the specified loop has a predictable |
| 3817 | /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute |
| 3818 | /// object. The backedge-taken count is the number of times the loop header |
| 3819 | /// will be branched to from within the loop. This is one less than the |
| 3820 | /// trip count of the loop, since it doesn't count the first iteration, |
| 3821 | /// when the header is branched to from outside the loop. |
| 3822 | /// |
| 3823 | /// Note that it is not valid to call this method on a loop without a |
| 3824 | /// loop-invariant backedge-taken count (see |
| 3825 | /// hasLoopInvariantBackedgeTakenCount). |
| 3826 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3827 | const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3828 | return getBackedgeTakenInfo(L).Exact; |
| 3829 | } |
| 3830 | |
| 3831 | /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except |
| 3832 | /// return the least SCEV value that is known never to be less than the |
| 3833 | /// actual backedge taken count. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3834 | const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3835 | return getBackedgeTakenInfo(L).Max; |
| 3836 | } |
| 3837 | |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3838 | /// PushLoopPHIs - Push PHI nodes in the header of the given loop |
| 3839 | /// onto the given Worklist. |
| 3840 | static void |
| 3841 | PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) { |
| 3842 | BasicBlock *Header = L->getHeader(); |
| 3843 | |
| 3844 | // Push all Loop-header PHIs onto the Worklist stack. |
| 3845 | for (BasicBlock::iterator I = Header->begin(); |
| 3846 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 3847 | Worklist.push_back(PN); |
| 3848 | } |
| 3849 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3850 | const ScalarEvolution::BackedgeTakenInfo & |
| 3851 | ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3852 | // Initially insert a CouldNotCompute for this loop. If the insertion |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 3853 | // succeeds, proceed to actually compute a backedge-taken count and |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3854 | // update the value. The temporary CouldNotCompute value tells SCEV |
| 3855 | // code elsewhere that it shouldn't attempt to request a new |
| 3856 | // backedge-taken count, which could result in infinite recursion. |
Dan Gohman | 77a2c4c | 2011-05-09 18:44:09 +0000 | [diff] [blame] | 3857 | std::pair<DenseMap<const Loop *, BackedgeTakenInfo>::iterator, bool> Pair = |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3858 | BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute())); |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 3859 | if (!Pair.second) |
| 3860 | return Pair.first->second; |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3861 | |
Dan Gohman | 308bec3 | 2011-04-25 22:48:29 +0000 | [diff] [blame] | 3862 | BackedgeTakenInfo Result = getCouldNotCompute(); |
| 3863 | BackedgeTakenInfo Computed = ComputeBackedgeTakenCount(L); |
| 3864 | if (Computed.Exact != getCouldNotCompute()) { |
| 3865 | assert(isLoopInvariant(Computed.Exact, L) && |
| 3866 | isLoopInvariant(Computed.Max, L) && |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 3867 | "Computed backedge-taken count isn't loop invariant for loop!"); |
| 3868 | ++NumTripCountsComputed; |
| 3869 | |
| 3870 | // Update the value in the map. |
Dan Gohman | 308bec3 | 2011-04-25 22:48:29 +0000 | [diff] [blame] | 3871 | Result = Computed; |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 3872 | } else { |
Dan Gohman | 308bec3 | 2011-04-25 22:48:29 +0000 | [diff] [blame] | 3873 | if (Computed.Max != getCouldNotCompute()) |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3874 | // Update the value in the map. |
Dan Gohman | 308bec3 | 2011-04-25 22:48:29 +0000 | [diff] [blame] | 3875 | Result = Computed; |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 3876 | if (isa<PHINode>(L->getHeader()->begin())) |
| 3877 | // Only count loops that have phi nodes as not being computable. |
| 3878 | ++NumTripCountsNotComputed; |
| 3879 | } |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3880 | |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 3881 | // Now that we know more about the trip count for this loop, forget any |
| 3882 | // existing SCEV values for PHI nodes in this loop since they are only |
| 3883 | // conservative estimates made without the benefit of trip count |
| 3884 | // information. This is similar to the code in forgetLoop, except that |
| 3885 | // it handles SCEVUnknown PHI nodes specially. |
Dan Gohman | 308bec3 | 2011-04-25 22:48:29 +0000 | [diff] [blame] | 3886 | if (Computed.hasAnyInfo()) { |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 3887 | SmallVector<Instruction *, 16> Worklist; |
| 3888 | PushLoopPHIs(L, Worklist); |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3889 | |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 3890 | SmallPtrSet<Instruction *, 8> Visited; |
| 3891 | while (!Worklist.empty()) { |
| 3892 | Instruction *I = Worklist.pop_back_val(); |
| 3893 | if (!Visited.insert(I)) continue; |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3894 | |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 3895 | ValueExprMapType::iterator It = |
| 3896 | ValueExprMap.find(static_cast<Value *>(I)); |
| 3897 | if (It != ValueExprMap.end()) { |
| 3898 | const SCEV *Old = It->second; |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 3899 | |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 3900 | // SCEVUnknown for a PHI either means that it has an unrecognized |
| 3901 | // structure, or it's a PHI that's in the progress of being computed |
| 3902 | // by createNodeForPHI. In the former case, additional loop trip |
| 3903 | // count information isn't going to change anything. In the later |
| 3904 | // case, createNodeForPHI will perform the necessary updates on its |
| 3905 | // own when it gets to that point. |
| 3906 | if (!isa<PHINode>(I) || !isa<SCEVUnknown>(Old)) { |
| 3907 | forgetMemoizedResults(Old); |
| 3908 | ValueExprMap.erase(It); |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3909 | } |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 3910 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3911 | ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3912 | } |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 3913 | |
| 3914 | PushDefUseChildren(I, Worklist); |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3915 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3916 | } |
Dan Gohman | 308bec3 | 2011-04-25 22:48:29 +0000 | [diff] [blame] | 3917 | |
| 3918 | // Re-lookup the insert position, since the call to |
| 3919 | // ComputeBackedgeTakenCount above could result in a |
| 3920 | // recusive call to getBackedgeTakenInfo (on a different |
| 3921 | // loop), which would invalidate the iterator computed |
| 3922 | // earlier. |
| 3923 | return BackedgeTakenCounts.find(L)->second = Result; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
Dan Gohman | 4c7279a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 3926 | /// forgetLoop - This method should be called by the client when it has |
| 3927 | /// changed a loop in a way that may effect ScalarEvolution's ability to |
| 3928 | /// compute a trip count, or if the loop is deleted. |
| 3929 | void ScalarEvolution::forgetLoop(const Loop *L) { |
| 3930 | // Drop any stored trip count value. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3931 | BackedgeTakenCounts.erase(L); |
Dan Gohman | fb7d35f | 2009-05-02 17:43:35 +0000 | [diff] [blame] | 3932 | |
Dan Gohman | 4c7279a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 3933 | // Drop information about expressions based on loop-header PHIs. |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3934 | SmallVector<Instruction *, 16> Worklist; |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3935 | PushLoopPHIs(L, Worklist); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3936 | |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3937 | SmallPtrSet<Instruction *, 8> Visited; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3938 | while (!Worklist.empty()) { |
| 3939 | Instruction *I = Worklist.pop_back_val(); |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3940 | if (!Visited.insert(I)) continue; |
| 3941 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3942 | ValueExprMapType::iterator It = ValueExprMap.find(static_cast<Value *>(I)); |
| 3943 | if (It != ValueExprMap.end()) { |
Dan Gohman | 56a7568 | 2010-11-17 23:28:48 +0000 | [diff] [blame] | 3944 | forgetMemoizedResults(It->second); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3945 | ValueExprMap.erase(It); |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3946 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3947 | ConstantEvolutionLoopExitValue.erase(PN); |
| 3948 | } |
| 3949 | |
| 3950 | PushDefUseChildren(I, Worklist); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3951 | } |
Dan Gohman | e60dcb5 | 2010-10-29 20:16:10 +0000 | [diff] [blame] | 3952 | |
| 3953 | // Forget all contained loops too, to avoid dangling entries in the |
| 3954 | // ValuesAtScopes map. |
| 3955 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 3956 | forgetLoop(*I); |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 3957 | } |
| 3958 | |
Eric Christopher | e6cbfa6 | 2010-07-29 01:25:38 +0000 | [diff] [blame] | 3959 | /// forgetValue - This method should be called by the client when it has |
| 3960 | /// changed a value in a way that may effect its value, or which may |
| 3961 | /// disconnect it from a def-use chain linking it to a loop. |
| 3962 | void ScalarEvolution::forgetValue(Value *V) { |
Dale Johannesen | 45a2d7d | 2010-02-19 07:14:22 +0000 | [diff] [blame] | 3963 | Instruction *I = dyn_cast<Instruction>(V); |
| 3964 | if (!I) return; |
| 3965 | |
| 3966 | // Drop information about expressions based on loop-header PHIs. |
| 3967 | SmallVector<Instruction *, 16> Worklist; |
| 3968 | Worklist.push_back(I); |
| 3969 | |
| 3970 | SmallPtrSet<Instruction *, 8> Visited; |
| 3971 | while (!Worklist.empty()) { |
| 3972 | I = Worklist.pop_back_val(); |
| 3973 | if (!Visited.insert(I)) continue; |
| 3974 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3975 | ValueExprMapType::iterator It = ValueExprMap.find(static_cast<Value *>(I)); |
| 3976 | if (It != ValueExprMap.end()) { |
Dan Gohman | 56a7568 | 2010-11-17 23:28:48 +0000 | [diff] [blame] | 3977 | forgetMemoizedResults(It->second); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3978 | ValueExprMap.erase(It); |
Dale Johannesen | 45a2d7d | 2010-02-19 07:14:22 +0000 | [diff] [blame] | 3979 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3980 | ConstantEvolutionLoopExitValue.erase(PN); |
| 3981 | } |
| 3982 | |
| 3983 | PushDefUseChildren(I, Worklist); |
| 3984 | } |
| 3985 | } |
| 3986 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3987 | /// ComputeBackedgeTakenCount - Compute the number of times the backedge |
| 3988 | /// of the specified loop will execute. |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3989 | ScalarEvolution::BackedgeTakenInfo |
| 3990 | ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 3991 | SmallVector<BasicBlock *, 8> ExitingBlocks; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3992 | L->getExitingBlocks(ExitingBlocks); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3993 | |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3994 | // Examine all exits and pick the most conservative values. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3995 | const SCEV *BECount = getCouldNotCompute(); |
| 3996 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3997 | bool CouldNotComputeBECount = false; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3998 | for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { |
| 3999 | BackedgeTakenInfo NewBTI = |
| 4000 | ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4001 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4002 | if (NewBTI.Exact == getCouldNotCompute()) { |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4003 | // We couldn't compute an exact value for this exit, so |
Dan Gohman | d32f5bf | 2009-06-22 21:10:22 +0000 | [diff] [blame] | 4004 | // 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] | 4005 | CouldNotComputeBECount = true; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4006 | BECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4007 | } else if (!CouldNotComputeBECount) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4008 | if (BECount == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4009 | BECount = NewBTI.Exact; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4010 | else |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4011 | BECount = getUMinFromMismatchedTypes(BECount, NewBTI.Exact); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4012 | } |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4013 | if (MaxBECount == getCouldNotCompute()) |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4014 | MaxBECount = NewBTI.Max; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4015 | else if (NewBTI.Max != getCouldNotCompute()) |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4016 | MaxBECount = getUMinFromMismatchedTypes(MaxBECount, NewBTI.Max); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
| 4019 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 4020 | } |
| 4021 | |
| 4022 | /// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge |
| 4023 | /// of the specified loop will execute if it exits via the specified block. |
| 4024 | ScalarEvolution::BackedgeTakenInfo |
| 4025 | ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L, |
| 4026 | BasicBlock *ExitingBlock) { |
| 4027 | |
| 4028 | // Okay, we've chosen an exiting block. See what condition causes us to |
| 4029 | // exit at this block. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4030 | // |
| 4031 | // 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] | 4032 | BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4033 | if (ExitBr == 0) return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4034 | assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!"); |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4035 | |
Chris Lattner | 8b0e360 | 2007-01-07 02:24:26 +0000 | [diff] [blame] | 4036 | // At this point, we know we have a conditional branch that determines whether |
| 4037 | // the loop is exited. However, we don't know if the branch is executed each |
| 4038 | // time through the loop. If not, then the execution count of the branch will |
| 4039 | // not be equal to the trip count of the loop. |
| 4040 | // |
| 4041 | // Currently we check for this by checking to see if the Exit branch goes to |
| 4042 | // 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] | 4043 | // 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] | 4044 | // loop header. This is common for un-rotated loops. |
| 4045 | // |
| 4046 | // If both of those tests fail, walk up the unique predecessor chain to the |
| 4047 | // header, stopping if there is an edge that doesn't exit the loop. If the |
| 4048 | // header is reached, the execution count of the branch will be equal to the |
| 4049 | // trip count of the loop. |
| 4050 | // |
| 4051 | // More extensive analysis could be done to handle more cases here. |
| 4052 | // |
Chris Lattner | 8b0e360 | 2007-01-07 02:24:26 +0000 | [diff] [blame] | 4053 | if (ExitBr->getSuccessor(0) != L->getHeader() && |
Chris Lattner | 192e403 | 2007-01-14 01:24:47 +0000 | [diff] [blame] | 4054 | ExitBr->getSuccessor(1) != L->getHeader() && |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4055 | ExitBr->getParent() != L->getHeader()) { |
| 4056 | // The simple checks failed, try climbing the unique predecessor chain |
| 4057 | // up to the header. |
| 4058 | bool Ok = false; |
| 4059 | for (BasicBlock *BB = ExitBr->getParent(); BB; ) { |
| 4060 | BasicBlock *Pred = BB->getUniquePredecessor(); |
| 4061 | if (!Pred) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4062 | return getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4063 | TerminatorInst *PredTerm = Pred->getTerminator(); |
| 4064 | for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) { |
| 4065 | BasicBlock *PredSucc = PredTerm->getSuccessor(i); |
| 4066 | if (PredSucc == BB) |
| 4067 | continue; |
| 4068 | // If the predecessor has a successor that isn't BB and isn't |
| 4069 | // outside the loop, assume the worst. |
| 4070 | if (L->contains(PredSucc)) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4071 | return getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4072 | } |
| 4073 | if (Pred == L->getHeader()) { |
| 4074 | Ok = true; |
| 4075 | break; |
| 4076 | } |
| 4077 | BB = Pred; |
| 4078 | } |
| 4079 | if (!Ok) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4080 | return getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 4083 | // Proceed to the next level to examine the exit condition expression. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4084 | return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(), |
| 4085 | ExitBr->getSuccessor(0), |
| 4086 | ExitBr->getSuccessor(1)); |
| 4087 | } |
| 4088 | |
| 4089 | /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the |
| 4090 | /// backedge of the specified loop will execute if its exit condition |
| 4091 | /// were a conditional branch of ExitCond, TBB, and FBB. |
| 4092 | ScalarEvolution::BackedgeTakenInfo |
| 4093 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L, |
| 4094 | Value *ExitCond, |
| 4095 | BasicBlock *TBB, |
| 4096 | BasicBlock *FBB) { |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4097 | // 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] | 4098 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) { |
| 4099 | if (BO->getOpcode() == Instruction::And) { |
| 4100 | // Recurse on the operands of the and. |
| 4101 | BackedgeTakenInfo BTI0 = |
| 4102 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 4103 | BackedgeTakenInfo BTI1 = |
| 4104 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4105 | const SCEV *BECount = getCouldNotCompute(); |
| 4106 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4107 | if (L->contains(TBB)) { |
| 4108 | // Both conditions must be true for the loop to continue executing. |
| 4109 | // Choose the less conservative count. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4110 | if (BTI0.Exact == getCouldNotCompute() || |
| 4111 | BTI1.Exact == getCouldNotCompute()) |
| 4112 | BECount = getCouldNotCompute(); |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 4113 | else |
| 4114 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4115 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4116 | MaxBECount = BTI1.Max; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4117 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4118 | MaxBECount = BTI0.Max; |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 4119 | else |
| 4120 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4121 | } else { |
Dan Gohman | 4ee8739 | 2010-08-11 00:12:36 +0000 | [diff] [blame] | 4122 | // Both conditions must be true at the same time for the loop to exit. |
| 4123 | // For now, be conservative. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4124 | assert(L->contains(FBB) && "Loop block has no successor in loop!"); |
Dan Gohman | 4ee8739 | 2010-08-11 00:12:36 +0000 | [diff] [blame] | 4125 | if (BTI0.Max == BTI1.Max) |
| 4126 | MaxBECount = BTI0.Max; |
| 4127 | if (BTI0.Exact == BTI1.Exact) |
| 4128 | BECount = BTI0.Exact; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4129 | } |
| 4130 | |
| 4131 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 4132 | } |
| 4133 | if (BO->getOpcode() == Instruction::Or) { |
| 4134 | // Recurse on the operands of the or. |
| 4135 | BackedgeTakenInfo BTI0 = |
| 4136 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 4137 | BackedgeTakenInfo BTI1 = |
| 4138 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4139 | const SCEV *BECount = getCouldNotCompute(); |
| 4140 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4141 | if (L->contains(FBB)) { |
| 4142 | // Both conditions must be false for the loop to continue executing. |
| 4143 | // Choose the less conservative count. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4144 | if (BTI0.Exact == getCouldNotCompute() || |
| 4145 | BTI1.Exact == getCouldNotCompute()) |
| 4146 | BECount = getCouldNotCompute(); |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 4147 | else |
| 4148 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4149 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4150 | MaxBECount = BTI1.Max; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4151 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4152 | MaxBECount = BTI0.Max; |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 4153 | else |
| 4154 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4155 | } else { |
Dan Gohman | 4ee8739 | 2010-08-11 00:12:36 +0000 | [diff] [blame] | 4156 | // Both conditions must be false at the same time for the loop to exit. |
| 4157 | // For now, be conservative. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4158 | assert(L->contains(TBB) && "Loop block has no successor in loop!"); |
Dan Gohman | 4ee8739 | 2010-08-11 00:12:36 +0000 | [diff] [blame] | 4159 | if (BTI0.Max == BTI1.Max) |
| 4160 | MaxBECount = BTI0.Max; |
| 4161 | if (BTI0.Exact == BTI1.Exact) |
| 4162 | BECount = BTI0.Exact; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
| 4165 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 4166 | } |
| 4167 | } |
| 4168 | |
| 4169 | // 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] | 4170 | // Proceed to the next level to examine the icmp. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4171 | if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) |
| 4172 | return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4173 | |
Dan Gohman | 00cb5b7 | 2010-02-19 18:12:07 +0000 | [diff] [blame] | 4174 | // Check for a constant condition. These are normally stripped out by |
| 4175 | // SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to |
| 4176 | // preserve the CFG and is temporarily leaving constant conditions |
| 4177 | // in place. |
| 4178 | if (ConstantInt *CI = dyn_cast<ConstantInt>(ExitCond)) { |
| 4179 | if (L->contains(FBB) == !CI->getZExtValue()) |
| 4180 | // The backedge is always taken. |
| 4181 | return getCouldNotCompute(); |
| 4182 | else |
| 4183 | // The backedge is never taken. |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 4184 | return getConstant(CI->getType(), 0); |
Dan Gohman | 00cb5b7 | 2010-02-19 18:12:07 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
Eli Friedman | 361e54d | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 4187 | // 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] | 4188 | return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
| 4189 | } |
| 4190 | |
| 4191 | /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the |
| 4192 | /// backedge of the specified loop will execute if its exit condition |
| 4193 | /// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB. |
| 4194 | ScalarEvolution::BackedgeTakenInfo |
| 4195 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L, |
| 4196 | ICmpInst *ExitCond, |
| 4197 | BasicBlock *TBB, |
| 4198 | BasicBlock *FBB) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4199 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4200 | // If the condition was exit on true, convert the condition to exit on false |
| 4201 | ICmpInst::Predicate Cond; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4202 | if (!L->contains(FBB)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4203 | Cond = ExitCond->getPredicate(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4204 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4205 | Cond = ExitCond->getInversePredicate(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4206 | |
| 4207 | // Handle common loops like: for (X = "string"; *X; ++X) |
| 4208 | if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0))) |
| 4209 | if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) { |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 4210 | BackedgeTakenInfo ItCnt = |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4211 | ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond); |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 4212 | if (ItCnt.hasAnyInfo()) |
| 4213 | return ItCnt; |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4216 | const SCEV *LHS = getSCEV(ExitCond->getOperand(0)); |
| 4217 | const SCEV *RHS = getSCEV(ExitCond->getOperand(1)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4218 | |
| 4219 | // Try to evaluate any dependencies out of the loop. |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 4220 | LHS = getSCEVAtScope(LHS, L); |
| 4221 | RHS = getSCEVAtScope(RHS, L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4222 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4223 | // 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] | 4224 | // loop the predicate will return true for these inputs. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 4225 | if (isLoopInvariant(LHS, L) && !isLoopInvariant(RHS, L)) { |
Dan Gohman | 70ff4cf | 2008-09-16 18:52:57 +0000 | [diff] [blame] | 4226 | // If there is a loop-invariant, force it into the RHS. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4227 | std::swap(LHS, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4228 | Cond = ICmpInst::getSwappedPredicate(Cond); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4229 | } |
| 4230 | |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 4231 | // Simplify the operands before analyzing them. |
| 4232 | (void)SimplifyICmpOperands(Cond, LHS, RHS); |
| 4233 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4234 | // If we have a comparison of a chrec against a constant, try to use value |
| 4235 | // ranges to answer this query. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4236 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) |
| 4237 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS)) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4238 | if (AddRec->getLoop() == L) { |
Eli Friedman | 361e54d | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 4239 | // Form the constant range. |
| 4240 | ConstantRange CompRange( |
| 4241 | ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue())); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4242 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4243 | const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this); |
Eli Friedman | 361e54d | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 4244 | if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4245 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4246 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4247 | switch (Cond) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4248 | case ICmpInst::ICMP_NE: { // while (X != Y) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4249 | // Convert to: while (X-Y != 0) |
Andrew Trick | 4dbe200 | 2011-03-15 01:16:14 +0000 | [diff] [blame] | 4250 | BackedgeTakenInfo BTI = HowFarToZero(getMinusSCEV(LHS, RHS), L); |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 4251 | if (BTI.hasAnyInfo()) return BTI; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4252 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4253 | } |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 4254 | case ICmpInst::ICMP_EQ: { // while (X == Y) |
| 4255 | // Convert to: while (X-Y == 0) |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 4256 | BackedgeTakenInfo BTI = HowFarToNonZero(getMinusSCEV(LHS, RHS), L); |
| 4257 | if (BTI.hasAnyInfo()) return BTI; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4258 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4259 | } |
| 4260 | case ICmpInst::ICMP_SLT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4261 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true); |
| 4262 | if (BTI.hasAnyInfo()) return BTI; |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4263 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4264 | } |
| 4265 | case ICmpInst::ICMP_SGT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4266 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 4267 | getNotSCEV(RHS), L, true); |
| 4268 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | d6dac0e | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 4269 | break; |
| 4270 | } |
| 4271 | case ICmpInst::ICMP_ULT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4272 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false); |
| 4273 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | d6dac0e | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 4274 | break; |
| 4275 | } |
| 4276 | case ICmpInst::ICMP_UGT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4277 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 4278 | getNotSCEV(RHS), L, false); |
| 4279 | if (BTI.hasAnyInfo()) return BTI; |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4280 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4281 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4282 | default: |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 4283 | #if 0 |
David Greene | 25e0e87 | 2009-12-23 22:18:14 +0000 | [diff] [blame] | 4284 | dbgs() << "ComputeBackedgeTakenCount "; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4285 | if (ExitCond->getOperand(0)->getType()->isUnsigned()) |
David Greene | 25e0e87 | 2009-12-23 22:18:14 +0000 | [diff] [blame] | 4286 | dbgs() << "[unsigned] "; |
| 4287 | dbgs() << *LHS << " " |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4288 | << Instruction::getOpcodeName(Instruction::ICmp) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4289 | << " " << *RHS << "\n"; |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 4290 | #endif |
Chris Lattner | e34c0b4 | 2004-04-03 00:43:03 +0000 | [diff] [blame] | 4291 | break; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4292 | } |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4293 | return |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4294 | ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4295 | } |
| 4296 | |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4297 | static ConstantInt * |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4298 | EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, |
| 4299 | ScalarEvolution &SE) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4300 | const SCEV *InVal = SE.getConstant(C); |
| 4301 | const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4302 | assert(isa<SCEVConstant>(Val) && |
| 4303 | "Evaluation of SCEV at constant didn't fold correctly?"); |
| 4304 | return cast<SCEVConstant>(Val)->getValue(); |
| 4305 | } |
| 4306 | |
| 4307 | /// GetAddressedElementFromGlobal - Given a global variable with an initializer |
| 4308 | /// and a GEP expression (missing the pointer index) indexing into it, return |
| 4309 | /// the addressed element of the initializer or null if the index expression is |
| 4310 | /// invalid. |
| 4311 | static Constant * |
Nick Lewycky | c6501b1 | 2009-11-23 03:26:09 +0000 | [diff] [blame] | 4312 | GetAddressedElementFromGlobal(GlobalVariable *GV, |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4313 | const std::vector<ConstantInt*> &Indices) { |
| 4314 | Constant *Init = GV->getInitializer(); |
| 4315 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4316 | uint64_t Idx = Indices[i]->getZExtValue(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4317 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) { |
| 4318 | assert(Idx < CS->getNumOperands() && "Bad struct index!"); |
| 4319 | Init = cast<Constant>(CS->getOperand(Idx)); |
| 4320 | } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) { |
| 4321 | if (Idx >= CA->getNumOperands()) return 0; // Bogus program |
| 4322 | Init = cast<Constant>(CA->getOperand(Idx)); |
| 4323 | } else if (isa<ConstantAggregateZero>(Init)) { |
| 4324 | if (const StructType *STy = dyn_cast<StructType>(Init->getType())) { |
| 4325 | assert(Idx < STy->getNumElements() && "Bad struct index!"); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 4326 | Init = Constant::getNullValue(STy->getElementType(Idx)); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4327 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) { |
| 4328 | if (Idx >= ATy->getNumElements()) return 0; // Bogus program |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 4329 | Init = Constant::getNullValue(ATy->getElementType()); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4330 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4331 | llvm_unreachable("Unknown constant aggregate type!"); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4332 | } |
| 4333 | return 0; |
| 4334 | } else { |
| 4335 | return 0; // Unknown initializer type |
| 4336 | } |
| 4337 | } |
| 4338 | return Init; |
| 4339 | } |
| 4340 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4341 | /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of |
| 4342 | /// 'icmp op load X, cst', try to see if we can compute the backedge |
| 4343 | /// execution count. |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 4344 | ScalarEvolution::BackedgeTakenInfo |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4345 | ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount( |
| 4346 | LoadInst *LI, |
| 4347 | Constant *RHS, |
| 4348 | const Loop *L, |
| 4349 | ICmpInst::Predicate predicate) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4350 | if (LI->isVolatile()) return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4351 | |
| 4352 | // 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] | 4353 | // TODO: Use SCEV instead of manually grubbing with GEPs. |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4354 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4355 | if (!GEP) return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4356 | |
| 4357 | // Make sure that it is really a constant global we are gepping, with an |
| 4358 | // initializer, and make sure the first IDX is really 0. |
| 4359 | GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); |
Dan Gohman | 8255573 | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 4360 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() || |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4361 | GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) || |
| 4362 | !cast<Constant>(GEP->getOperand(1))->isNullValue()) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4363 | return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4364 | |
| 4365 | // Okay, we allow one non-constant index into the GEP instruction. |
| 4366 | Value *VarIdx = 0; |
| 4367 | std::vector<ConstantInt*> Indexes; |
| 4368 | unsigned VarIdxNum = 0; |
| 4369 | for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) |
| 4370 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 4371 | Indexes.push_back(CI); |
| 4372 | } else if (!isa<ConstantInt>(GEP->getOperand(i))) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4373 | if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's. |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4374 | VarIdx = GEP->getOperand(i); |
| 4375 | VarIdxNum = i-2; |
| 4376 | Indexes.push_back(0); |
| 4377 | } |
| 4378 | |
| 4379 | // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant. |
| 4380 | // Check to see if X is a loop variant variable value now. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4381 | const SCEV *Idx = getSCEV(VarIdx); |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 4382 | Idx = getSCEVAtScope(Idx, L); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4383 | |
| 4384 | // We can only recognize very limited forms of loop index expressions, in |
| 4385 | // particular, only affine AddRec's like {C1,+,C2}. |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4386 | const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx); |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 4387 | if (!IdxExpr || !IdxExpr->isAffine() || isLoopInvariant(IdxExpr, L) || |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4388 | !isa<SCEVConstant>(IdxExpr->getOperand(0)) || |
| 4389 | !isa<SCEVConstant>(IdxExpr->getOperand(1))) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4390 | return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4391 | |
| 4392 | unsigned MaxSteps = MaxBruteForceIterations; |
| 4393 | for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4394 | ConstantInt *ItCst = ConstantInt::get( |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 4395 | cast<IntegerType>(IdxExpr->getType()), IterationNum); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4396 | ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4397 | |
| 4398 | // Form the GEP offset. |
| 4399 | Indexes[VarIdxNum] = Val; |
| 4400 | |
Nick Lewycky | c6501b1 | 2009-11-23 03:26:09 +0000 | [diff] [blame] | 4401 | Constant *Result = GetAddressedElementFromGlobal(GV, Indexes); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4402 | if (Result == 0) break; // Cannot compute! |
| 4403 | |
| 4404 | // Evaluate the condition for this iteration. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4405 | Result = ConstantExpr::getICmp(predicate, Result, RHS); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4406 | if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4407 | if (cast<ConstantInt>(Result)->getValue().isMinValue()) { |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4408 | #if 0 |
David Greene | 25e0e87 | 2009-12-23 22:18:14 +0000 | [diff] [blame] | 4409 | dbgs() << "\n***\n*** Computed loop count " << *ItCst |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 4410 | << "\n*** From global " << *GV << "*** BB: " << *L->getHeader() |
| 4411 | << "***\n"; |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4412 | #endif |
| 4413 | ++NumArrayLenItCounts; |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4414 | return getConstant(ItCst); // Found terminating iteration! |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4415 | } |
| 4416 | } |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4417 | return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4421 | /// CanConstantFold - Return true if we can constant fold an instruction of the |
| 4422 | /// specified type, assuming that all operands were constants. |
| 4423 | static bool CanConstantFold(const Instruction *I) { |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4424 | if (isa<BinaryOperator>(I) || isa<CmpInst>(I) || |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4425 | isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I)) |
| 4426 | return true; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4427 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4428 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 4429 | if (const Function *F = CI->getCalledFunction()) |
Dan Gohman | fa9b80e | 2008-01-31 01:05:10 +0000 | [diff] [blame] | 4430 | return canConstantFoldCallTo(F); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4431 | return false; |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4434 | /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node |
| 4435 | /// in the loop that V is derived from. We allow arbitrary operations along the |
| 4436 | /// way, but the operands of an operation must either be constants or a value |
| 4437 | /// derived from a constant PHI. If this expression does not fit with these |
| 4438 | /// constraints, return null. |
| 4439 | static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) { |
| 4440 | // If this is not an instruction, or if this is an instruction outside of the |
| 4441 | // loop, it can't be derived from a loop PHI. |
| 4442 | Instruction *I = dyn_cast<Instruction>(V); |
Dan Gohman | 92329c7 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 4443 | if (I == 0 || !L->contains(I)) return 0; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4444 | |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 4445 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4446 | if (L->getHeader() == I->getParent()) |
| 4447 | return PN; |
| 4448 | else |
| 4449 | // We don't currently keep track of the control flow needed to evaluate |
| 4450 | // PHIs, so we cannot handle PHIs inside of loops. |
| 4451 | return 0; |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 4452 | } |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4453 | |
| 4454 | // If we won't be able to constant fold this expression even if the operands |
| 4455 | // are constants, return early. |
| 4456 | if (!CanConstantFold(I)) return 0; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4457 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4458 | // Otherwise, we can evaluate this instruction if all of its operands are |
| 4459 | // constant or derived from a PHI node themselves. |
| 4460 | PHINode *PHI = 0; |
| 4461 | for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op) |
Dan Gohman | 9d4588f | 2010-06-22 13:15:46 +0000 | [diff] [blame] | 4462 | if (!isa<Constant>(I->getOperand(Op))) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4463 | PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L); |
| 4464 | if (P == 0) return 0; // Not evolving from PHI |
| 4465 | if (PHI == 0) |
| 4466 | PHI = P; |
| 4467 | else if (PHI != P) |
| 4468 | return 0; // Evolving from multiple different PHIs. |
| 4469 | } |
| 4470 | |
| 4471 | // This is a expression evolving from a constant PHI! |
| 4472 | return PHI; |
| 4473 | } |
| 4474 | |
| 4475 | /// EvaluateExpression - Given an expression that passes the |
| 4476 | /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node |
| 4477 | /// in the loop has the value PHIVal. If we can't fold this expression for some |
| 4478 | /// reason, return null. |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4479 | static Constant *EvaluateExpression(Value *V, Constant *PHIVal, |
| 4480 | const TargetData *TD) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4481 | if (isa<PHINode>(V)) return PHIVal; |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 4482 | if (Constant *C = dyn_cast<Constant>(V)) return C; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4483 | Instruction *I = cast<Instruction>(V); |
| 4484 | |
Dan Gohman | 9d4588f | 2010-06-22 13:15:46 +0000 | [diff] [blame] | 4485 | std::vector<Constant*> Operands(I->getNumOperands()); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4486 | |
| 4487 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4488 | Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal, TD); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4489 | if (Operands[i] == 0) return 0; |
| 4490 | } |
| 4491 | |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 4492 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
Chris Lattner | 8f73dea | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 4493 | return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0], |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4494 | Operands[1], TD); |
Chris Lattner | 8f73dea | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 4495 | return ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4496 | &Operands[0], Operands.size(), TD); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4497 | } |
| 4498 | |
| 4499 | /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is |
| 4500 | /// in the header of its containing loop, we know the loop executes a |
| 4501 | /// constant number of times, and the PHI node is just a recurrence |
| 4502 | /// involving constants, fold it. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4503 | Constant * |
| 4504 | ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN, |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 4505 | const APInt &BEs, |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4506 | const Loop *L) { |
Dan Gohman | 77a2c4c | 2011-05-09 18:44:09 +0000 | [diff] [blame] | 4507 | DenseMap<PHINode*, Constant*>::const_iterator I = |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4508 | ConstantEvolutionLoopExitValue.find(PN); |
| 4509 | if (I != ConstantEvolutionLoopExitValue.end()) |
| 4510 | return I->second; |
| 4511 | |
Dan Gohman | e056781 | 2010-04-08 23:03:40 +0000 | [diff] [blame] | 4512 | if (BEs.ugt(MaxBruteForceIterations)) |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4513 | return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it. |
| 4514 | |
| 4515 | Constant *&RetVal = ConstantEvolutionLoopExitValue[PN]; |
| 4516 | |
| 4517 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 4518 | // entry must be a constant (coming in from outside of the loop), and the |
| 4519 | // second must be derived from the same PHI. |
| 4520 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 4521 | Constant *StartCST = |
| 4522 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
| 4523 | if (StartCST == 0) |
| 4524 | return RetVal = 0; // Must be a constant. |
| 4525 | |
| 4526 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
Dan Gohman | 9d4588f | 2010-06-22 13:15:46 +0000 | [diff] [blame] | 4527 | if (getConstantEvolvingPHI(BEValue, L) != PN && |
| 4528 | !isa<Constant>(BEValue)) |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4529 | return RetVal = 0; // Not derived from same PHI. |
| 4530 | |
| 4531 | // Execute the loop symbolically to determine the exit value. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4532 | if (BEs.getActiveBits() >= 32) |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4533 | return RetVal = 0; // More than 2^32-1 iterations?? Not doing it! |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4534 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4535 | unsigned NumIterations = BEs.getZExtValue(); // must be in range |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4536 | unsigned IterationNum = 0; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4537 | for (Constant *PHIVal = StartCST; ; ++IterationNum) { |
| 4538 | if (IterationNum == NumIterations) |
| 4539 | return RetVal = PHIVal; // Got exit value! |
| 4540 | |
| 4541 | // Compute the value of the PHI node for the next iteration. |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4542 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal, TD); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4543 | if (NextPHI == PHIVal) |
| 4544 | return RetVal = NextPHI; // Stopped evolving! |
| 4545 | if (NextPHI == 0) |
| 4546 | return 0; // Couldn't evaluate! |
| 4547 | PHIVal = NextPHI; |
| 4548 | } |
| 4549 | } |
| 4550 | |
Dan Gohman | 07ad19b | 2009-07-27 16:09:48 +0000 | [diff] [blame] | 4551 | /// ComputeBackedgeTakenCountExhaustively - If the loop is known to execute a |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4552 | /// constant number of times (the condition evolves only from constants), |
| 4553 | /// try to evaluate a few iterations of the loop until we get the exit |
| 4554 | /// condition gets a value of ExitWhen (true or false). If we cannot |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4555 | /// evaluate the trip count of the loop, return getCouldNotCompute(). |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4556 | const SCEV * |
| 4557 | ScalarEvolution::ComputeBackedgeTakenCountExhaustively(const Loop *L, |
| 4558 | Value *Cond, |
| 4559 | bool ExitWhen) { |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4560 | PHINode *PN = getConstantEvolvingPHI(Cond, L); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4561 | if (PN == 0) return getCouldNotCompute(); |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4562 | |
Dan Gohman | b92654d | 2010-06-19 14:17:24 +0000 | [diff] [blame] | 4563 | // If the loop is canonicalized, the PHI will have exactly two entries. |
| 4564 | // That's the only form we support here. |
| 4565 | if (PN->getNumIncomingValues() != 2) return getCouldNotCompute(); |
| 4566 | |
| 4567 | // 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] | 4568 | // second must be derived from the same PHI. |
| 4569 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 4570 | Constant *StartCST = |
| 4571 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4572 | if (StartCST == 0) return getCouldNotCompute(); // Must be a constant. |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4573 | |
| 4574 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
Dan Gohman | 9d4588f | 2010-06-22 13:15:46 +0000 | [diff] [blame] | 4575 | if (getConstantEvolvingPHI(BEValue, L) != PN && |
| 4576 | !isa<Constant>(BEValue)) |
| 4577 | return getCouldNotCompute(); // Not derived from same PHI. |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4578 | |
| 4579 | // Okay, we find a PHI node that defines the trip count of this loop. Execute |
| 4580 | // the loop symbolically to determine when the condition gets a value of |
| 4581 | // "ExitWhen". |
| 4582 | unsigned IterationNum = 0; |
| 4583 | unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis. |
| 4584 | for (Constant *PHIVal = StartCST; |
| 4585 | IterationNum != MaxIterations; ++IterationNum) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4586 | ConstantInt *CondVal = |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4587 | dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal, TD)); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4588 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4589 | // Couldn't symbolically evaluate. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4590 | if (!CondVal) return getCouldNotCompute(); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4591 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4592 | if (CondVal->getValue() == uint64_t(ExitWhen)) { |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4593 | ++NumBruteForceTripCountsComputed; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4594 | return getConstant(Type::getInt32Ty(getContext()), IterationNum); |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4595 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4596 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4597 | // Compute the value of the PHI node for the next iteration. |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4598 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal, TD); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4599 | if (NextPHI == 0 || NextPHI == PHIVal) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4600 | return getCouldNotCompute();// Couldn't evaluate or not making progress... |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4601 | PHIVal = NextPHI; |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4602 | } |
| 4603 | |
| 4604 | // Too many iterations were needed to evaluate. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4605 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4606 | } |
| 4607 | |
Dan Gohman | e7125f4 | 2009-09-03 15:00:26 +0000 | [diff] [blame] | 4608 | /// getSCEVAtScope - Return a SCEV expression for the specified value |
Dan Gohman | 66a7e85 | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 4609 | /// at the specified scope in the program. The L value specifies a loop |
| 4610 | /// nest to evaluate the expression at, where null is the top-level or a |
| 4611 | /// specified loop is immediately inside of the loop. |
| 4612 | /// |
| 4613 | /// This method can be used to compute the exit value for a variable defined |
| 4614 | /// in a loop by querying what the value will hold in the parent loop. |
| 4615 | /// |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 4616 | /// In the case that a relevant loop exit value cannot be computed, the |
| 4617 | /// original value V is returned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4618 | const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 4619 | // Check to see if we've folded this expression at this loop before. |
| 4620 | std::map<const Loop *, const SCEV *> &Values = ValuesAtScopes[V]; |
| 4621 | std::pair<std::map<const Loop *, const SCEV *>::iterator, bool> Pair = |
| 4622 | Values.insert(std::make_pair(L, static_cast<const SCEV *>(0))); |
| 4623 | if (!Pair.second) |
| 4624 | return Pair.first->second ? Pair.first->second : V; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4625 | |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 4626 | // Otherwise compute it. |
| 4627 | const SCEV *C = computeSCEVAtScope(V, L); |
Dan Gohman | a5505cb | 2009-08-31 21:58:28 +0000 | [diff] [blame] | 4628 | ValuesAtScopes[V][L] = C; |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 4629 | return C; |
| 4630 | } |
| 4631 | |
| 4632 | const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4633 | if (isa<SCEVConstant>(V)) return V; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4634 | |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 4635 | // If this instruction is evolved from a constant-evolving PHI, compute the |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4636 | // exit value from the loop without using SCEVs. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4637 | if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4638 | if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4639 | const Loop *LI = (*this->LI)[I->getParent()]; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4640 | if (LI && LI->getParentLoop() == L) // Looking for loop exit value. |
| 4641 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 4642 | if (PN->getParent() == LI->getHeader()) { |
| 4643 | // Okay, there is no closed form solution for the PHI node. Check |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4644 | // to see if the loop that contains it has a known backedge-taken |
| 4645 | // count. If so, we may be able to force computation of the exit |
| 4646 | // value. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4647 | const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4648 | if (const SCEVConstant *BTCC = |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4649 | dyn_cast<SCEVConstant>(BackedgeTakenCount)) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4650 | // Okay, we know how many times the containing loop executes. If |
| 4651 | // this is a constant evolving PHI node, get the final value at |
| 4652 | // the specified iteration number. |
| 4653 | Constant *RV = getConstantEvolutionLoopExitValue(PN, |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4654 | BTCC->getValue()->getValue(), |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4655 | LI); |
Dan Gohman | 0998796 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 4656 | if (RV) return getSCEV(RV); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4657 | } |
| 4658 | } |
| 4659 | |
Reid Spencer | 09906f3 | 2006-12-04 21:33:23 +0000 | [diff] [blame] | 4660 | // Okay, this is an expression that we cannot symbolically evaluate |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4661 | // 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] | 4662 | // the arguments into constants, and if so, try to constant propagate the |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4663 | // result. This is particularly useful for computing loop exit values. |
| 4664 | if (CanConstantFold(I)) { |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4665 | SmallVector<Constant *, 4> Operands; |
| 4666 | bool MadeImprovement = false; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4667 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 4668 | Value *Op = I->getOperand(i); |
| 4669 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| 4670 | Operands.push_back(C); |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4671 | continue; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4672 | } |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4673 | |
| 4674 | // If any of the operands is non-constant and if they are |
| 4675 | // non-integer and non-pointer, don't even try to analyze them |
| 4676 | // with scev techniques. |
| 4677 | if (!isSCEVable(Op->getType())) |
| 4678 | return V; |
| 4679 | |
| 4680 | const SCEV *OrigV = getSCEV(Op); |
| 4681 | const SCEV *OpV = getSCEVAtScope(OrigV, L); |
| 4682 | MadeImprovement |= OrigV != OpV; |
| 4683 | |
| 4684 | Constant *C = 0; |
| 4685 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) |
| 4686 | C = SC->getValue(); |
| 4687 | if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) |
| 4688 | C = dyn_cast<Constant>(SU->getValue()); |
| 4689 | if (!C) return V; |
| 4690 | if (C->getType() != Op->getType()) |
| 4691 | C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 4692 | Op->getType(), |
| 4693 | false), |
| 4694 | C, Op->getType()); |
| 4695 | Operands.push_back(C); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4696 | } |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4697 | |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4698 | // Check to see if getSCEVAtScope actually made an improvement. |
| 4699 | if (MadeImprovement) { |
| 4700 | Constant *C = 0; |
| 4701 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 4702 | C = ConstantFoldCompareInstOperands(CI->getPredicate(), |
| 4703 | Operands[0], Operands[1], TD); |
| 4704 | else |
| 4705 | C = ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
| 4706 | &Operands[0], Operands.size(), TD); |
| 4707 | if (!C) return V; |
Dan Gohman | e177c9a | 2010-02-24 19:31:47 +0000 | [diff] [blame] | 4708 | return getSCEV(C); |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4709 | } |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4710 | } |
| 4711 | } |
| 4712 | |
| 4713 | // This is some other type of SCEVUnknown, just return it. |
| 4714 | return V; |
| 4715 | } |
| 4716 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4717 | if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4718 | // Avoid performing the look-up in the common case where the specified |
| 4719 | // expression has no loop-variant portions. |
| 4720 | for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4721 | const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4722 | if (OpAtScope != Comm->getOperand(i)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4723 | // Okay, at least one of these operands is loop variant but might be |
| 4724 | // foldable. Build a new instance of the folded commutative expression. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4725 | SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(), |
| 4726 | Comm->op_begin()+i); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4727 | NewOps.push_back(OpAtScope); |
| 4728 | |
| 4729 | for (++i; i != e; ++i) { |
| 4730 | OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4731 | NewOps.push_back(OpAtScope); |
| 4732 | } |
| 4733 | if (isa<SCEVAddExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4734 | return getAddExpr(NewOps); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 4735 | if (isa<SCEVMulExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4736 | return getMulExpr(NewOps); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 4737 | if (isa<SCEVSMaxExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4738 | return getSMaxExpr(NewOps); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 4739 | if (isa<SCEVUMaxExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4740 | return getUMaxExpr(NewOps); |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4741 | llvm_unreachable("Unknown commutative SCEV type!"); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4742 | } |
| 4743 | } |
| 4744 | // If we got here, all operands are loop invariant. |
| 4745 | return Comm; |
| 4746 | } |
| 4747 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4748 | if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4749 | const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L); |
| 4750 | const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L); |
Nick Lewycky | 789558d | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 4751 | if (LHS == Div->getLHS() && RHS == Div->getRHS()) |
| 4752 | return Div; // must be loop invariant |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4753 | return getUDivExpr(LHS, RHS); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4754 | } |
| 4755 | |
| 4756 | // If this is a loop recurrence for a loop that does not contain L, then we |
| 4757 | // are dealing with the final value computed by the loop. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4758 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) { |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4759 | // First, attempt to evaluate each operand. |
| 4760 | // Avoid performing the look-up in the common case where the specified |
| 4761 | // expression has no loop-variant portions. |
| 4762 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { |
| 4763 | const SCEV *OpAtScope = getSCEVAtScope(AddRec->getOperand(i), L); |
| 4764 | if (OpAtScope == AddRec->getOperand(i)) |
| 4765 | continue; |
| 4766 | |
| 4767 | // Okay, at least one of these operands is loop variant but might be |
| 4768 | // foldable. Build a new instance of the folded commutative expression. |
| 4769 | SmallVector<const SCEV *, 8> NewOps(AddRec->op_begin(), |
| 4770 | AddRec->op_begin()+i); |
| 4771 | NewOps.push_back(OpAtScope); |
| 4772 | for (++i; i != e; ++i) |
| 4773 | NewOps.push_back(getSCEVAtScope(AddRec->getOperand(i), L)); |
| 4774 | |
Andrew Trick | 3f95c88 | 2011-04-27 01:21:25 +0000 | [diff] [blame] | 4775 | const SCEV *FoldedRec = |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 4776 | getAddRecExpr(NewOps, AddRec->getLoop(), |
Andrew Trick | 3f95c88 | 2011-04-27 01:21:25 +0000 | [diff] [blame] | 4777 | AddRec->getNoWrapFlags(SCEV::FlagNW)); |
| 4778 | AddRec = dyn_cast<SCEVAddRecExpr>(FoldedRec); |
Andrew Trick | 104f4ad | 2011-04-27 05:42:17 +0000 | [diff] [blame] | 4779 | // The addrec may be folded to a nonrecurrence, for example, if the |
| 4780 | // induction variable is multiplied by zero after constant folding. Go |
| 4781 | // ahead and return the folded value. |
Andrew Trick | 3f95c88 | 2011-04-27 01:21:25 +0000 | [diff] [blame] | 4782 | if (!AddRec) |
| 4783 | return FoldedRec; |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4784 | break; |
| 4785 | } |
| 4786 | |
| 4787 | // If the scope is outside the addrec's loop, evaluate it by using the |
| 4788 | // loop exit value of the addrec. |
| 4789 | if (!AddRec->getLoop()->contains(L)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4790 | // To evaluate this recurrence, we need to know how many times the AddRec |
| 4791 | // loop iterates. Compute this now. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4792 | const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4793 | if (BackedgeTakenCount == getCouldNotCompute()) return AddRec; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4794 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 4795 | // Then, evaluate the AddRec. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4796 | return AddRec->evaluateAtIteration(BackedgeTakenCount, *this); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4797 | } |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4798 | |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 4799 | return AddRec; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4800 | } |
| 4801 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4802 | if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4803 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | eb3948b | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4804 | if (Op == Cast->getOperand()) |
| 4805 | return Cast; // must be loop invariant |
| 4806 | return getZeroExtendExpr(Op, Cast->getType()); |
| 4807 | } |
| 4808 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4809 | if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4810 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | eb3948b | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4811 | if (Op == Cast->getOperand()) |
| 4812 | return Cast; // must be loop invariant |
| 4813 | return getSignExtendExpr(Op, Cast->getType()); |
| 4814 | } |
| 4815 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4816 | if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4817 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | eb3948b | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4818 | if (Op == Cast->getOperand()) |
| 4819 | return Cast; // must be loop invariant |
| 4820 | return getTruncateExpr(Op, Cast->getType()); |
| 4821 | } |
| 4822 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4823 | llvm_unreachable("Unknown SCEV type!"); |
Daniel Dunbar | 8c562e2 | 2009-05-18 16:43:04 +0000 | [diff] [blame] | 4824 | return 0; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4825 | } |
| 4826 | |
Dan Gohman | 66a7e85 | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 4827 | /// getSCEVAtScope - This is a convenience function which does |
| 4828 | /// getSCEVAtScope(getSCEV(V), L). |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4829 | const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4830 | return getSCEVAtScope(getSCEV(V), L); |
| 4831 | } |
| 4832 | |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4833 | /// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the |
| 4834 | /// following equation: |
| 4835 | /// |
| 4836 | /// A * X = B (mod N) |
| 4837 | /// |
| 4838 | /// where N = 2^BW and BW is the common bit width of A and B. The signedness of |
| 4839 | /// A and B isn't important. |
| 4840 | /// |
| 4841 | /// If the equation does not have a solution, SCEVCouldNotCompute is returned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4842 | static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const APInt &B, |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4843 | ScalarEvolution &SE) { |
| 4844 | uint32_t BW = A.getBitWidth(); |
| 4845 | assert(BW == B.getBitWidth() && "Bit widths must be the same."); |
| 4846 | assert(A != 0 && "A must be non-zero."); |
| 4847 | |
| 4848 | // 1. D = gcd(A, N) |
| 4849 | // |
| 4850 | // The gcd of A and N may have only one prime factor: 2. The number of |
| 4851 | // trailing zeros in A is its multiplicity |
| 4852 | uint32_t Mult2 = A.countTrailingZeros(); |
| 4853 | // D = 2^Mult2 |
| 4854 | |
| 4855 | // 2. Check if B is divisible by D. |
| 4856 | // |
| 4857 | // B is divisible by D if and only if the multiplicity of prime factor 2 for B |
| 4858 | // is not less than multiplicity of this prime factor for D. |
| 4859 | if (B.countTrailingZeros() < Mult2) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4860 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4861 | |
| 4862 | // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic |
| 4863 | // modulo (N / D). |
| 4864 | // |
| 4865 | // (N / D) may need BW+1 bits in its representation. Hence, we'll use this |
| 4866 | // bit width during computations. |
| 4867 | APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D |
| 4868 | APInt Mod(BW + 1, 0); |
Jay Foad | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 4869 | Mod.setBit(BW - Mult2); // Mod = N / D |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4870 | APInt I = AD.multiplicativeInverse(Mod); |
| 4871 | |
| 4872 | // 4. Compute the minimum unsigned root of the equation: |
| 4873 | // I * (B / D) mod (N / D) |
| 4874 | APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod); |
| 4875 | |
| 4876 | // The result is guaranteed to be less than 2^BW so we may truncate it to BW |
| 4877 | // bits. |
| 4878 | return SE.getConstant(Result.trunc(BW)); |
| 4879 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4880 | |
| 4881 | /// SolveQuadraticEquation - Find the roots of the quadratic equation for the |
| 4882 | /// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which |
| 4883 | /// might be the same) or two SCEVCouldNotCompute objects. |
| 4884 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4885 | static std::pair<const SCEV *,const SCEV *> |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4886 | SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4887 | assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4888 | const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0)); |
| 4889 | const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1)); |
| 4890 | const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2)); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4891 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4892 | // We currently can only solve this if the coefficients are constants. |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4893 | if (!LC || !MC || !NC) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4894 | const SCEV *CNC = SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4895 | return std::make_pair(CNC, CNC); |
| 4896 | } |
| 4897 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4898 | uint32_t BitWidth = LC->getValue()->getValue().getBitWidth(); |
Chris Lattner | fe560b8 | 2007-04-15 19:52:49 +0000 | [diff] [blame] | 4899 | const APInt &L = LC->getValue()->getValue(); |
| 4900 | const APInt &M = MC->getValue()->getValue(); |
| 4901 | const APInt &N = NC->getValue()->getValue(); |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4902 | APInt Two(BitWidth, 2); |
| 4903 | APInt Four(BitWidth, 4); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4904 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4905 | { |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4906 | using namespace APIntOps; |
Zhou Sheng | 414de4d | 2007-04-07 17:48:27 +0000 | [diff] [blame] | 4907 | const APInt& C = L; |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4908 | // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C |
| 4909 | // The B coefficient is M-N/2 |
| 4910 | APInt B(M); |
| 4911 | B -= sdiv(N,Two); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4912 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4913 | // The A coefficient is N/2 |
Zhou Sheng | 414de4d | 2007-04-07 17:48:27 +0000 | [diff] [blame] | 4914 | APInt A(N.sdiv(Two)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4915 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4916 | // Compute the B^2-4ac term. |
| 4917 | APInt SqrtTerm(B); |
| 4918 | SqrtTerm *= B; |
| 4919 | SqrtTerm -= Four * (A * C); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4920 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4921 | // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest |
| 4922 | // integer value or else APInt::sqrt() will assert. |
| 4923 | APInt SqrtVal(SqrtTerm.sqrt()); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4924 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4925 | // Compute the two solutions for the quadratic formula. |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4926 | // The divisions must be performed as signed divisions. |
| 4927 | APInt NegB(-B); |
Reid Spencer | 3e35c8d | 2007-04-16 02:24:41 +0000 | [diff] [blame] | 4928 | APInt TwoA( A << 1 ); |
Nick Lewycky | 8f4d5eb | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 4929 | if (TwoA.isMinValue()) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4930 | const SCEV *CNC = SE.getCouldNotCompute(); |
Nick Lewycky | 8f4d5eb | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 4931 | return std::make_pair(CNC, CNC); |
| 4932 | } |
| 4933 | |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 4934 | LLVMContext &Context = SE.getContext(); |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4935 | |
| 4936 | ConstantInt *Solution1 = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4937 | ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA)); |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4938 | ConstantInt *Solution2 = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4939 | ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA)); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4940 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4941 | return std::make_pair(SE.getConstant(Solution1), |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4942 | SE.getConstant(Solution2)); |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4943 | } // end APIntOps namespace |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4944 | } |
| 4945 | |
| 4946 | /// HowFarToZero - Return the number of times a backedge comparing the specified |
Dan Gohman | 86fbf2f | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4947 | /// value to zero will execute. If not computable, return CouldNotCompute. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 4948 | /// |
| 4949 | /// This is only used for loops with a "x != y" exit test. The exit condition is |
| 4950 | /// now expressed as a single expression, V = x-y. So the exit test is |
| 4951 | /// effectively V != 0. We know and take advantage of the fact that this |
| 4952 | /// expression only being used in a comparison by zero context. |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 4953 | ScalarEvolution::BackedgeTakenInfo |
| 4954 | ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4955 | // If the value is a constant |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4956 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4957 | // If the value is already zero, the branch will execute zero times. |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 4958 | if (C->getValue()->isZero()) return C; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4959 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4960 | } |
| 4961 | |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4962 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4963 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4964 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4965 | |
Chris Lattner | 7975e3e | 2011-01-09 22:39:48 +0000 | [diff] [blame] | 4966 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of |
| 4967 | // the quadratic equation to solve it. |
| 4968 | if (AddRec->isQuadratic() && AddRec->getType()->isIntegerTy()) { |
| 4969 | std::pair<const SCEV *,const SCEV *> Roots = |
| 4970 | SolveQuadraticEquation(AddRec, *this); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4971 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 4972 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Chris Lattner | 7975e3e | 2011-01-09 22:39:48 +0000 | [diff] [blame] | 4973 | if (R1 && R2) { |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 4974 | #if 0 |
David Greene | 25e0e87 | 2009-12-23 22:18:14 +0000 | [diff] [blame] | 4975 | dbgs() << "HFTZ: " << *V << " - sol#1: " << *R1 |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 4976 | << " sol#2: " << *R2 << "\n"; |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 4977 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4978 | // Pick the smallest positive root value. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4979 | if (ConstantInt *CB = |
Chris Lattner | 53e1d45 | 2011-01-09 22:58:47 +0000 | [diff] [blame] | 4980 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(CmpInst::ICMP_ULT, |
| 4981 | R1->getValue(), |
| 4982 | R2->getValue()))) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4983 | if (CB->getZExtValue() == false) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4984 | std::swap(R1, R2); // R1 is the minimum root now. |
Andrew Trick | 635f718 | 2011-03-09 17:23:39 +0000 | [diff] [blame] | 4985 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4986 | // We can only use this value if the chrec ends up with an exact zero |
| 4987 | // value at this index. When solving for "X*X != 5", for example, we |
| 4988 | // should not accept a root of 2. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4989 | const SCEV *Val = AddRec->evaluateAtIteration(R1, *this); |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 4990 | if (Val->isZero()) |
| 4991 | return R1; // We found a quadratic root! |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4992 | } |
| 4993 | } |
Chris Lattner | 7975e3e | 2011-01-09 22:39:48 +0000 | [diff] [blame] | 4994 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4995 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4996 | |
Chris Lattner | 7975e3e | 2011-01-09 22:39:48 +0000 | [diff] [blame] | 4997 | // Otherwise we can only handle this if it is affine. |
| 4998 | if (!AddRec->isAffine()) |
| 4999 | return getCouldNotCompute(); |
| 5000 | |
| 5001 | // If this is an affine expression, the execution count of this branch is |
| 5002 | // the minimum unsigned root of the following equation: |
| 5003 | // |
| 5004 | // Start + Step*N = 0 (mod 2^BW) |
| 5005 | // |
| 5006 | // equivalent to: |
| 5007 | // |
| 5008 | // Step*N = -Start (mod 2^BW) |
| 5009 | // |
| 5010 | // where BW is the common bit width of Start and Step. |
| 5011 | |
| 5012 | // Get the initial value for the loop. |
| 5013 | const SCEV *Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop()); |
| 5014 | const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), L->getParentLoop()); |
| 5015 | |
| 5016 | // For now we handle only constant steps. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 5017 | // |
| 5018 | // TODO: Handle a nonconstant Step given AddRec<NUW>. If the |
| 5019 | // AddRec is NUW, then (in an unsigned sense) it cannot be counting up to wrap |
| 5020 | // to 0, it must be counting down to equal 0. Consequently, N = Start / -Step. |
| 5021 | // We have not yet seen any such cases. |
Chris Lattner | 7975e3e | 2011-01-09 22:39:48 +0000 | [diff] [blame] | 5022 | const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step); |
| 5023 | if (StepC == 0) |
| 5024 | return getCouldNotCompute(); |
| 5025 | |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 5026 | // For positive steps (counting up until unsigned overflow): |
| 5027 | // N = -Start/Step (as unsigned) |
| 5028 | // For negative steps (counting down to zero): |
| 5029 | // N = Start/-Step |
| 5030 | // First compute the unsigned distance from zero in the direction of Step. |
Andrew Trick | dcfd404 | 2011-03-14 17:28:02 +0000 | [diff] [blame] | 5031 | bool CountDown = StepC->getValue()->getValue().isNegative(); |
| 5032 | const SCEV *Distance = CountDown ? Start : getNegativeSCEV(Start); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 5033 | |
| 5034 | // Handle unitary steps, which cannot wraparound. |
Andrew Trick | dcfd404 | 2011-03-14 17:28:02 +0000 | [diff] [blame] | 5035 | // 1*N = -Start; -1*N = Start (mod 2^BW), so: |
| 5036 | // N = Distance (as unsigned) |
| 5037 | if (StepC->getValue()->equalsInt(1) || StepC->getValue()->isAllOnesValue()) |
| 5038 | return Distance; |
Andrew Trick | 635f718 | 2011-03-09 17:23:39 +0000 | [diff] [blame] | 5039 | |
Andrew Trick | dcfd404 | 2011-03-14 17:28:02 +0000 | [diff] [blame] | 5040 | // If the recurrence is known not to wraparound, unsigned divide computes the |
| 5041 | // back edge count. We know that the value will either become zero (and thus |
| 5042 | // the loop terminates), that the loop will terminate through some other exit |
| 5043 | // condition first, or that the loop has undefined behavior. This means |
| 5044 | // we can't "miss" the exit value, even with nonunit stride. |
| 5045 | // |
| 5046 | // FIXME: Prove that loops always exhibits *acceptable* undefined |
| 5047 | // behavior. Loops must exhibit defined behavior until a wrapped value is |
| 5048 | // actually used. So the trip count computed by udiv could be smaller than the |
| 5049 | // number of well-defined iterations. |
| 5050 | if (AddRec->getNoWrapFlags(SCEV::FlagNW)) |
| 5051 | // FIXME: We really want an "isexact" bit for udiv. |
| 5052 | return getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step); |
Chris Lattner | 7975e3e | 2011-01-09 22:39:48 +0000 | [diff] [blame] | 5053 | |
| 5054 | // Then, try to solve the above equation provided that Start is constant. |
| 5055 | if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) |
| 5056 | return SolveLinEquationWithOverflow(StepC->getValue()->getValue(), |
| 5057 | -StartC->getValue()->getValue(), |
| 5058 | *this); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5059 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5060 | } |
| 5061 | |
| 5062 | /// HowFarToNonZero - Return the number of times a backedge checking the |
| 5063 | /// specified value for nonzero will execute. If not computable, return |
Dan Gohman | 86fbf2f | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 5064 | /// CouldNotCompute |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 5065 | ScalarEvolution::BackedgeTakenInfo |
| 5066 | ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5067 | // Loops that look like: while (X == 0) are very strange indeed. We don't |
| 5068 | // handle them yet except for the trivial case. This could be expanded in the |
| 5069 | // future as needed. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5070 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5071 | // If the value is a constant, check to see if it is known to be non-zero |
| 5072 | // already. If so, the backedge will execute zero times. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 5073 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Nick Lewycky | 39442af | 2008-02-21 09:14:53 +0000 | [diff] [blame] | 5074 | if (!C->getValue()->isNullValue()) |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 5075 | return getConstant(C->getType(), 0); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5076 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5077 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5078 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5079 | // We could implement others, but I really doubt anyone writes loops like |
| 5080 | // this, and if they did, they would already be constant folded. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5081 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5082 | } |
| 5083 | |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 5084 | /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB |
| 5085 | /// (which may not be an immediate predecessor) which has exactly one |
| 5086 | /// successor from which BB is reachable, or null if no such block is |
| 5087 | /// found. |
| 5088 | /// |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 5089 | std::pair<BasicBlock *, BasicBlock *> |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5090 | ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) { |
Dan Gohman | 3d739fe | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 5091 | // If the block has a unique predecessor, then there is no path from the |
| 5092 | // predecessor to the block that does not go through the direct edge |
| 5093 | // from the predecessor to the block. |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 5094 | if (BasicBlock *Pred = BB->getSinglePredecessor()) |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 5095 | return std::make_pair(Pred, BB); |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 5096 | |
| 5097 | // 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] | 5098 | // If the header has a unique predecessor outside the loop, it must be |
| 5099 | // a block that has exactly one successor that can reach the loop. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5100 | if (Loop *L = LI->getLoopFor(BB)) |
Dan Gohman | 605c14f | 2010-06-22 23:43:28 +0000 | [diff] [blame] | 5101 | return std::make_pair(L->getLoopPredecessor(), L->getHeader()); |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 5102 | |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 5103 | return std::pair<BasicBlock *, BasicBlock *>(); |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 5104 | } |
| 5105 | |
Dan Gohman | 763bad1 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 5106 | /// HasSameValue - SCEV structural equivalence is usually sufficient for |
| 5107 | /// testing whether two expressions are equal, however for the purposes of |
| 5108 | /// looking for a condition guarding a loop, it can be useful to be a little |
| 5109 | /// more general, since a front-end may have replicated the controlling |
| 5110 | /// expression. |
| 5111 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5112 | static bool HasSameValue(const SCEV *A, const SCEV *B) { |
Dan Gohman | 763bad1 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 5113 | // Quick check to see if they are the same SCEV. |
| 5114 | if (A == B) return true; |
| 5115 | |
| 5116 | // Otherwise, if they're both SCEVUnknown, it's possible that they hold |
| 5117 | // two different instructions with the same value. Check for this case. |
| 5118 | if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A)) |
| 5119 | if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B)) |
| 5120 | if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue())) |
| 5121 | if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue())) |
Dan Gohman | 041de42 | 2009-08-25 17:56:57 +0000 | [diff] [blame] | 5122 | if (AI->isIdenticalTo(BI) && !AI->mayReadFromMemory()) |
Dan Gohman | 763bad1 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 5123 | return true; |
| 5124 | |
| 5125 | // Otherwise assume they may have a different value. |
| 5126 | return false; |
| 5127 | } |
| 5128 | |
Dan Gohman | e979650 | 2010-04-24 01:28:42 +0000 | [diff] [blame] | 5129 | /// SimplifyICmpOperands - Simplify LHS and RHS in a comparison with |
| 5130 | /// predicate Pred. Return true iff any changes were made. |
| 5131 | /// |
| 5132 | bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred, |
| 5133 | const SCEV *&LHS, const SCEV *&RHS) { |
| 5134 | bool Changed = false; |
| 5135 | |
| 5136 | // Canonicalize a constant to the right side. |
| 5137 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { |
| 5138 | // Check for both operands constant. |
| 5139 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { |
| 5140 | if (ConstantExpr::getICmp(Pred, |
| 5141 | LHSC->getValue(), |
| 5142 | RHSC->getValue())->isNullValue()) |
| 5143 | goto trivially_false; |
| 5144 | else |
| 5145 | goto trivially_true; |
| 5146 | } |
| 5147 | // Otherwise swap the operands to put the constant on the right. |
| 5148 | std::swap(LHS, RHS); |
| 5149 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 5150 | Changed = true; |
| 5151 | } |
| 5152 | |
| 5153 | // 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] | 5154 | // addrec's loop, put the addrec on the left. Also make a dominance check, |
| 5155 | // as both operands could be addrecs loop-invariant in each other's loop. |
| 5156 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(RHS)) { |
| 5157 | const Loop *L = AR->getLoop(); |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 5158 | if (isLoopInvariant(LHS, L) && properlyDominates(LHS, L->getHeader())) { |
Dan Gohman | e979650 | 2010-04-24 01:28:42 +0000 | [diff] [blame] | 5159 | std::swap(LHS, RHS); |
| 5160 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 5161 | Changed = true; |
| 5162 | } |
Dan Gohman | 3abb69c | 2010-05-03 17:00:11 +0000 | [diff] [blame] | 5163 | } |
Dan Gohman | e979650 | 2010-04-24 01:28:42 +0000 | [diff] [blame] | 5164 | |
| 5165 | // If there's a constant operand, canonicalize comparisons with boundary |
| 5166 | // cases, and canonicalize *-or-equal comparisons to regular comparisons. |
| 5167 | if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) { |
| 5168 | const APInt &RA = RC->getValue()->getValue(); |
| 5169 | switch (Pred) { |
| 5170 | default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
| 5171 | case ICmpInst::ICMP_EQ: |
| 5172 | case ICmpInst::ICMP_NE: |
| 5173 | break; |
| 5174 | case ICmpInst::ICMP_UGE: |
| 5175 | if ((RA - 1).isMinValue()) { |
| 5176 | Pred = ICmpInst::ICMP_NE; |
| 5177 | RHS = getConstant(RA - 1); |
| 5178 | Changed = true; |
| 5179 | break; |
| 5180 | } |
| 5181 | if (RA.isMaxValue()) { |
| 5182 | Pred = ICmpInst::ICMP_EQ; |
| 5183 | Changed = true; |
| 5184 | break; |
| 5185 | } |
| 5186 | if (RA.isMinValue()) goto trivially_true; |
| 5187 | |
| 5188 | Pred = ICmpInst::ICMP_UGT; |
| 5189 | RHS = getConstant(RA - 1); |
| 5190 | Changed = true; |
| 5191 | break; |
| 5192 | case ICmpInst::ICMP_ULE: |
| 5193 | if ((RA + 1).isMaxValue()) { |
| 5194 | Pred = ICmpInst::ICMP_NE; |
| 5195 | RHS = getConstant(RA + 1); |
| 5196 | Changed = true; |
| 5197 | break; |
| 5198 | } |
| 5199 | if (RA.isMinValue()) { |
| 5200 | Pred = ICmpInst::ICMP_EQ; |
| 5201 | Changed = true; |
| 5202 | break; |
| 5203 | } |
| 5204 | if (RA.isMaxValue()) goto trivially_true; |
| 5205 | |
| 5206 | Pred = ICmpInst::ICMP_ULT; |
| 5207 | RHS = getConstant(RA + 1); |
| 5208 | Changed = true; |
| 5209 | break; |
| 5210 | case ICmpInst::ICMP_SGE: |
| 5211 | if ((RA - 1).isMinSignedValue()) { |
| 5212 | Pred = ICmpInst::ICMP_NE; |
| 5213 | RHS = getConstant(RA - 1); |
| 5214 | Changed = true; |
| 5215 | break; |
| 5216 | } |
| 5217 | if (RA.isMaxSignedValue()) { |
| 5218 | Pred = ICmpInst::ICMP_EQ; |
| 5219 | Changed = true; |
| 5220 | break; |
| 5221 | } |
| 5222 | if (RA.isMinSignedValue()) goto trivially_true; |
| 5223 | |
| 5224 | Pred = ICmpInst::ICMP_SGT; |
| 5225 | RHS = getConstant(RA - 1); |
| 5226 | Changed = true; |
| 5227 | break; |
| 5228 | case ICmpInst::ICMP_SLE: |
| 5229 | if ((RA + 1).isMaxSignedValue()) { |
| 5230 | Pred = ICmpInst::ICMP_NE; |
| 5231 | RHS = getConstant(RA + 1); |
| 5232 | Changed = true; |
| 5233 | break; |
| 5234 | } |
| 5235 | if (RA.isMinSignedValue()) { |
| 5236 | Pred = ICmpInst::ICMP_EQ; |
| 5237 | Changed = true; |
| 5238 | break; |
| 5239 | } |
| 5240 | if (RA.isMaxSignedValue()) goto trivially_true; |
| 5241 | |
| 5242 | Pred = ICmpInst::ICMP_SLT; |
| 5243 | RHS = getConstant(RA + 1); |
| 5244 | Changed = true; |
| 5245 | break; |
| 5246 | case ICmpInst::ICMP_UGT: |
| 5247 | if (RA.isMinValue()) { |
| 5248 | Pred = ICmpInst::ICMP_NE; |
| 5249 | Changed = true; |
| 5250 | break; |
| 5251 | } |
| 5252 | if ((RA + 1).isMaxValue()) { |
| 5253 | Pred = ICmpInst::ICMP_EQ; |
| 5254 | RHS = getConstant(RA + 1); |
| 5255 | Changed = true; |
| 5256 | break; |
| 5257 | } |
| 5258 | if (RA.isMaxValue()) goto trivially_false; |
| 5259 | break; |
| 5260 | case ICmpInst::ICMP_ULT: |
| 5261 | if (RA.isMaxValue()) { |
| 5262 | Pred = ICmpInst::ICMP_NE; |
| 5263 | Changed = true; |
| 5264 | break; |
| 5265 | } |
| 5266 | if ((RA - 1).isMinValue()) { |
| 5267 | Pred = ICmpInst::ICMP_EQ; |
| 5268 | RHS = getConstant(RA - 1); |
| 5269 | Changed = true; |
| 5270 | break; |
| 5271 | } |
| 5272 | if (RA.isMinValue()) goto trivially_false; |
| 5273 | break; |
| 5274 | case ICmpInst::ICMP_SGT: |
| 5275 | if (RA.isMinSignedValue()) { |
| 5276 | Pred = ICmpInst::ICMP_NE; |
| 5277 | Changed = true; |
| 5278 | break; |
| 5279 | } |
| 5280 | if ((RA + 1).isMaxSignedValue()) { |
| 5281 | Pred = ICmpInst::ICMP_EQ; |
| 5282 | RHS = getConstant(RA + 1); |
| 5283 | Changed = true; |
| 5284 | break; |
| 5285 | } |
| 5286 | if (RA.isMaxSignedValue()) goto trivially_false; |
| 5287 | break; |
| 5288 | case ICmpInst::ICMP_SLT: |
| 5289 | if (RA.isMaxSignedValue()) { |
| 5290 | Pred = ICmpInst::ICMP_NE; |
| 5291 | Changed = true; |
| 5292 | break; |
| 5293 | } |
| 5294 | if ((RA - 1).isMinSignedValue()) { |
| 5295 | Pred = ICmpInst::ICMP_EQ; |
| 5296 | RHS = getConstant(RA - 1); |
| 5297 | Changed = true; |
| 5298 | break; |
| 5299 | } |
| 5300 | if (RA.isMinSignedValue()) goto trivially_false; |
| 5301 | break; |
| 5302 | } |
| 5303 | } |
| 5304 | |
| 5305 | // Check for obvious equality. |
| 5306 | if (HasSameValue(LHS, RHS)) { |
| 5307 | if (ICmpInst::isTrueWhenEqual(Pred)) |
| 5308 | goto trivially_true; |
| 5309 | if (ICmpInst::isFalseWhenEqual(Pred)) |
| 5310 | goto trivially_false; |
| 5311 | } |
| 5312 | |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5313 | // If possible, canonicalize GE/LE comparisons to GT/LT comparisons, by |
| 5314 | // adding or subtracting 1 from one of the operands. |
| 5315 | switch (Pred) { |
| 5316 | case ICmpInst::ICMP_SLE: |
| 5317 | if (!getSignedRange(RHS).getSignedMax().isMaxSignedValue()) { |
| 5318 | RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS, |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 5319 | SCEV::FlagNSW); |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5320 | Pred = ICmpInst::ICMP_SLT; |
| 5321 | Changed = true; |
| 5322 | } else if (!getSignedRange(LHS).getSignedMin().isMinSignedValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5323 | LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS, |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 5324 | SCEV::FlagNSW); |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5325 | Pred = ICmpInst::ICMP_SLT; |
| 5326 | Changed = true; |
| 5327 | } |
| 5328 | break; |
| 5329 | case ICmpInst::ICMP_SGE: |
| 5330 | if (!getSignedRange(RHS).getSignedMin().isMinSignedValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5331 | RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS, |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 5332 | SCEV::FlagNSW); |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5333 | Pred = ICmpInst::ICMP_SGT; |
| 5334 | Changed = true; |
| 5335 | } else if (!getSignedRange(LHS).getSignedMax().isMaxSignedValue()) { |
| 5336 | LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS, |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 5337 | SCEV::FlagNSW); |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5338 | Pred = ICmpInst::ICMP_SGT; |
| 5339 | Changed = true; |
| 5340 | } |
| 5341 | break; |
| 5342 | case ICmpInst::ICMP_ULE: |
| 5343 | if (!getUnsignedRange(RHS).getUnsignedMax().isMaxValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5344 | RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS, |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 5345 | SCEV::FlagNUW); |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5346 | Pred = ICmpInst::ICMP_ULT; |
| 5347 | Changed = true; |
| 5348 | } else if (!getUnsignedRange(LHS).getUnsignedMin().isMinValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5349 | LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS, |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 5350 | SCEV::FlagNUW); |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5351 | Pred = ICmpInst::ICMP_ULT; |
| 5352 | Changed = true; |
| 5353 | } |
| 5354 | break; |
| 5355 | case ICmpInst::ICMP_UGE: |
| 5356 | if (!getUnsignedRange(RHS).getUnsignedMin().isMinValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5357 | RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS, |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 5358 | SCEV::FlagNUW); |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5359 | Pred = ICmpInst::ICMP_UGT; |
| 5360 | Changed = true; |
| 5361 | } else if (!getUnsignedRange(LHS).getUnsignedMax().isMaxValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5362 | LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS, |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 5363 | SCEV::FlagNUW); |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5364 | Pred = ICmpInst::ICMP_UGT; |
| 5365 | Changed = true; |
| 5366 | } |
| 5367 | break; |
| 5368 | default: |
| 5369 | break; |
| 5370 | } |
| 5371 | |
Dan Gohman | e979650 | 2010-04-24 01:28:42 +0000 | [diff] [blame] | 5372 | // TODO: More simplifications are possible here. |
| 5373 | |
| 5374 | return Changed; |
| 5375 | |
| 5376 | trivially_true: |
| 5377 | // Return 0 == 0. |
Benjamin Kramer | f601d6d | 2010-11-20 18:43:35 +0000 | [diff] [blame] | 5378 | LHS = RHS = getConstant(ConstantInt::getFalse(getContext())); |
Dan Gohman | e979650 | 2010-04-24 01:28:42 +0000 | [diff] [blame] | 5379 | Pred = ICmpInst::ICMP_EQ; |
| 5380 | return true; |
| 5381 | |
| 5382 | trivially_false: |
| 5383 | // Return 0 != 0. |
Benjamin Kramer | f601d6d | 2010-11-20 18:43:35 +0000 | [diff] [blame] | 5384 | LHS = RHS = getConstant(ConstantInt::getFalse(getContext())); |
Dan Gohman | e979650 | 2010-04-24 01:28:42 +0000 | [diff] [blame] | 5385 | Pred = ICmpInst::ICMP_NE; |
| 5386 | return true; |
| 5387 | } |
| 5388 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5389 | bool ScalarEvolution::isKnownNegative(const SCEV *S) { |
| 5390 | return getSignedRange(S).getSignedMax().isNegative(); |
| 5391 | } |
| 5392 | |
| 5393 | bool ScalarEvolution::isKnownPositive(const SCEV *S) { |
| 5394 | return getSignedRange(S).getSignedMin().isStrictlyPositive(); |
| 5395 | } |
| 5396 | |
| 5397 | bool ScalarEvolution::isKnownNonNegative(const SCEV *S) { |
| 5398 | return !getSignedRange(S).getSignedMin().isNegative(); |
| 5399 | } |
| 5400 | |
| 5401 | bool ScalarEvolution::isKnownNonPositive(const SCEV *S) { |
| 5402 | return !getSignedRange(S).getSignedMax().isStrictlyPositive(); |
| 5403 | } |
| 5404 | |
| 5405 | bool ScalarEvolution::isKnownNonZero(const SCEV *S) { |
| 5406 | return isKnownNegative(S) || isKnownPositive(S); |
| 5407 | } |
| 5408 | |
| 5409 | bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred, |
| 5410 | const SCEV *LHS, const SCEV *RHS) { |
Dan Gohman | d19bba6 | 2010-04-24 01:38:36 +0000 | [diff] [blame] | 5411 | // Canonicalize the inputs first. |
| 5412 | (void)SimplifyICmpOperands(Pred, LHS, RHS); |
| 5413 | |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5414 | // If LHS or RHS is an addrec, check to see if the condition is true in |
| 5415 | // every iteration of the loop. |
| 5416 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) |
| 5417 | if (isLoopEntryGuardedByCond( |
| 5418 | AR->getLoop(), Pred, AR->getStart(), RHS) && |
| 5419 | isLoopBackedgeGuardedByCond( |
Dan Gohman | acd8cab | 2010-05-04 01:12:27 +0000 | [diff] [blame] | 5420 | AR->getLoop(), Pred, AR->getPostIncExpr(*this), RHS)) |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5421 | return true; |
| 5422 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(RHS)) |
| 5423 | if (isLoopEntryGuardedByCond( |
| 5424 | AR->getLoop(), Pred, LHS, AR->getStart()) && |
| 5425 | isLoopBackedgeGuardedByCond( |
Dan Gohman | acd8cab | 2010-05-04 01:12:27 +0000 | [diff] [blame] | 5426 | AR->getLoop(), Pred, LHS, AR->getPostIncExpr(*this))) |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5427 | return true; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5428 | |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5429 | // Otherwise see what can be done with known constant ranges. |
| 5430 | return isKnownPredicateWithRanges(Pred, LHS, RHS); |
| 5431 | } |
| 5432 | |
| 5433 | bool |
| 5434 | ScalarEvolution::isKnownPredicateWithRanges(ICmpInst::Predicate Pred, |
| 5435 | const SCEV *LHS, const SCEV *RHS) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5436 | if (HasSameValue(LHS, RHS)) |
| 5437 | return ICmpInst::isTrueWhenEqual(Pred); |
| 5438 | |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5439 | // This code is split out from isKnownPredicate because it is called from |
| 5440 | // within isLoopEntryGuardedByCond. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5441 | switch (Pred) { |
| 5442 | default: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5443 | llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5444 | break; |
| 5445 | case ICmpInst::ICMP_SGT: |
| 5446 | Pred = ICmpInst::ICMP_SLT; |
| 5447 | std::swap(LHS, RHS); |
| 5448 | case ICmpInst::ICMP_SLT: { |
| 5449 | ConstantRange LHSRange = getSignedRange(LHS); |
| 5450 | ConstantRange RHSRange = getSignedRange(RHS); |
| 5451 | if (LHSRange.getSignedMax().slt(RHSRange.getSignedMin())) |
| 5452 | return true; |
| 5453 | if (LHSRange.getSignedMin().sge(RHSRange.getSignedMax())) |
| 5454 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5455 | break; |
| 5456 | } |
| 5457 | case ICmpInst::ICMP_SGE: |
| 5458 | Pred = ICmpInst::ICMP_SLE; |
| 5459 | std::swap(LHS, RHS); |
| 5460 | case ICmpInst::ICMP_SLE: { |
| 5461 | ConstantRange LHSRange = getSignedRange(LHS); |
| 5462 | ConstantRange RHSRange = getSignedRange(RHS); |
| 5463 | if (LHSRange.getSignedMax().sle(RHSRange.getSignedMin())) |
| 5464 | return true; |
| 5465 | if (LHSRange.getSignedMin().sgt(RHSRange.getSignedMax())) |
| 5466 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5467 | break; |
| 5468 | } |
| 5469 | case ICmpInst::ICMP_UGT: |
| 5470 | Pred = ICmpInst::ICMP_ULT; |
| 5471 | std::swap(LHS, RHS); |
| 5472 | case ICmpInst::ICMP_ULT: { |
| 5473 | ConstantRange LHSRange = getUnsignedRange(LHS); |
| 5474 | ConstantRange RHSRange = getUnsignedRange(RHS); |
| 5475 | if (LHSRange.getUnsignedMax().ult(RHSRange.getUnsignedMin())) |
| 5476 | return true; |
| 5477 | if (LHSRange.getUnsignedMin().uge(RHSRange.getUnsignedMax())) |
| 5478 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5479 | break; |
| 5480 | } |
| 5481 | case ICmpInst::ICMP_UGE: |
| 5482 | Pred = ICmpInst::ICMP_ULE; |
| 5483 | std::swap(LHS, RHS); |
| 5484 | case ICmpInst::ICMP_ULE: { |
| 5485 | ConstantRange LHSRange = getUnsignedRange(LHS); |
| 5486 | ConstantRange RHSRange = getUnsignedRange(RHS); |
| 5487 | if (LHSRange.getUnsignedMax().ule(RHSRange.getUnsignedMin())) |
| 5488 | return true; |
| 5489 | if (LHSRange.getUnsignedMin().ugt(RHSRange.getUnsignedMax())) |
| 5490 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5491 | break; |
| 5492 | } |
| 5493 | case ICmpInst::ICMP_NE: { |
| 5494 | if (getUnsignedRange(LHS).intersectWith(getUnsignedRange(RHS)).isEmptySet()) |
| 5495 | return true; |
| 5496 | if (getSignedRange(LHS).intersectWith(getSignedRange(RHS)).isEmptySet()) |
| 5497 | return true; |
| 5498 | |
| 5499 | const SCEV *Diff = getMinusSCEV(LHS, RHS); |
| 5500 | if (isKnownNonZero(Diff)) |
| 5501 | return true; |
| 5502 | break; |
| 5503 | } |
| 5504 | case ICmpInst::ICMP_EQ: |
Dan Gohman | f117ed4 | 2009-07-20 23:54:43 +0000 | [diff] [blame] | 5505 | // The check at the top of the function catches the case where |
| 5506 | // the values are known to be equal. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5507 | break; |
| 5508 | } |
| 5509 | return false; |
| 5510 | } |
| 5511 | |
| 5512 | /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is |
| 5513 | /// protected by a conditional between LHS and RHS. This is used to |
| 5514 | /// to eliminate casts. |
| 5515 | bool |
| 5516 | ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L, |
| 5517 | ICmpInst::Predicate Pred, |
| 5518 | const SCEV *LHS, const SCEV *RHS) { |
| 5519 | // Interpret a null as meaning no loop, where there is obviously no guard |
| 5520 | // (interprocedural conditions notwithstanding). |
| 5521 | if (!L) return true; |
| 5522 | |
| 5523 | BasicBlock *Latch = L->getLoopLatch(); |
| 5524 | if (!Latch) |
| 5525 | return false; |
| 5526 | |
| 5527 | BranchInst *LoopContinuePredicate = |
| 5528 | dyn_cast<BranchInst>(Latch->getTerminator()); |
| 5529 | if (!LoopContinuePredicate || |
| 5530 | LoopContinuePredicate->isUnconditional()) |
| 5531 | return false; |
| 5532 | |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5533 | return isImpliedCond(Pred, LHS, RHS, |
| 5534 | LoopContinuePredicate->getCondition(), |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5535 | LoopContinuePredicate->getSuccessor(0) != L->getHeader()); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5536 | } |
| 5537 | |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 5538 | /// isLoopEntryGuardedByCond - Test whether entry to the loop is protected |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5539 | /// by a conditional between LHS and RHS. This is used to help avoid max |
| 5540 | /// expressions in loop trip counts, and to eliminate casts. |
| 5541 | bool |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 5542 | ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L, |
| 5543 | ICmpInst::Predicate Pred, |
| 5544 | const SCEV *LHS, const SCEV *RHS) { |
Dan Gohman | 8ea9452 | 2009-05-18 16:03:58 +0000 | [diff] [blame] | 5545 | // Interpret a null as meaning no loop, where there is obviously no guard |
| 5546 | // (interprocedural conditions notwithstanding). |
| 5547 | if (!L) return false; |
| 5548 | |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 5549 | // Starting at the loop predecessor, climb up the predecessor chain, as long |
| 5550 | // as there are predecessors that can be found that have unique successors |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 5551 | // leading to the original header. |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 5552 | for (std::pair<BasicBlock *, BasicBlock *> |
Dan Gohman | 605c14f | 2010-06-22 23:43:28 +0000 | [diff] [blame] | 5553 | Pair(L->getLoopPredecessor(), L->getHeader()); |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 5554 | Pair.first; |
| 5555 | Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) { |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 5556 | |
| 5557 | BranchInst *LoopEntryPredicate = |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 5558 | dyn_cast<BranchInst>(Pair.first->getTerminator()); |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 5559 | if (!LoopEntryPredicate || |
| 5560 | LoopEntryPredicate->isUnconditional()) |
| 5561 | continue; |
| 5562 | |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5563 | if (isImpliedCond(Pred, LHS, RHS, |
| 5564 | LoopEntryPredicate->getCondition(), |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 5565 | LoopEntryPredicate->getSuccessor(0) != Pair.second)) |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 5566 | return true; |
Nick Lewycky | 59cff12 | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 5567 | } |
| 5568 | |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 5569 | return false; |
Nick Lewycky | 59cff12 | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5572 | /// isImpliedCond - Test whether the condition described by Pred, LHS, |
| 5573 | /// and RHS is true whenever the given Cond value evaluates to true. |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5574 | bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5575 | const SCEV *LHS, const SCEV *RHS, |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5576 | Value *FoundCondValue, |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5577 | bool Inverse) { |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 5578 | // Recursively handle And and Or conditions. |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5579 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FoundCondValue)) { |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 5580 | if (BO->getOpcode() == Instruction::And) { |
| 5581 | if (!Inverse) |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5582 | return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) || |
| 5583 | isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 5584 | } else if (BO->getOpcode() == Instruction::Or) { |
| 5585 | if (Inverse) |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5586 | return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) || |
| 5587 | isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 5588 | } |
| 5589 | } |
| 5590 | |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5591 | ICmpInst *ICI = dyn_cast<ICmpInst>(FoundCondValue); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 5592 | if (!ICI) return false; |
| 5593 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5594 | // Bail if the ICmp's operands' types are wider than the needed type |
| 5595 | // before attempting to call getSCEV on them. This avoids infinite |
| 5596 | // recursion, since the analysis of widening casts can require loop |
| 5597 | // exit condition information for overflow checking, which would |
| 5598 | // lead back here. |
| 5599 | if (getTypeSizeInBits(LHS->getType()) < |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5600 | getTypeSizeInBits(ICI->getOperand(0)->getType())) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5601 | return false; |
| 5602 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5603 | // Now that we found a conditional branch that dominates the loop, check to |
| 5604 | // see if it is the comparison we are looking for. |
| 5605 | ICmpInst::Predicate FoundPred; |
| 5606 | if (Inverse) |
| 5607 | FoundPred = ICI->getInversePredicate(); |
| 5608 | else |
| 5609 | FoundPred = ICI->getPredicate(); |
| 5610 | |
| 5611 | const SCEV *FoundLHS = getSCEV(ICI->getOperand(0)); |
| 5612 | const SCEV *FoundRHS = getSCEV(ICI->getOperand(1)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5613 | |
| 5614 | // Balance the types. The case where FoundLHS' type is wider than |
| 5615 | // LHS' type is checked for above. |
| 5616 | if (getTypeSizeInBits(LHS->getType()) > |
| 5617 | getTypeSizeInBits(FoundLHS->getType())) { |
| 5618 | if (CmpInst::isSigned(Pred)) { |
| 5619 | FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType()); |
| 5620 | FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType()); |
| 5621 | } else { |
| 5622 | FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType()); |
| 5623 | FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType()); |
| 5624 | } |
| 5625 | } |
| 5626 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5627 | // Canonicalize the query to match the way instcombine will have |
| 5628 | // canonicalized the comparison. |
Dan Gohman | d4da5af | 2010-04-24 01:34:53 +0000 | [diff] [blame] | 5629 | if (SimplifyICmpOperands(Pred, LHS, RHS)) |
| 5630 | if (LHS == RHS) |
Dan Gohman | 34c3e36 | 2010-05-03 18:00:24 +0000 | [diff] [blame] | 5631 | return CmpInst::isTrueWhenEqual(Pred); |
Dan Gohman | d4da5af | 2010-04-24 01:34:53 +0000 | [diff] [blame] | 5632 | if (SimplifyICmpOperands(FoundPred, FoundLHS, FoundRHS)) |
| 5633 | if (FoundLHS == FoundRHS) |
Dan Gohman | 34c3e36 | 2010-05-03 18:00:24 +0000 | [diff] [blame] | 5634 | return CmpInst::isFalseWhenEqual(Pred); |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5635 | |
| 5636 | // Check to see if we can make the LHS or RHS match. |
| 5637 | if (LHS == FoundRHS || RHS == FoundLHS) { |
| 5638 | if (isa<SCEVConstant>(RHS)) { |
| 5639 | std::swap(FoundLHS, FoundRHS); |
| 5640 | FoundPred = ICmpInst::getSwappedPredicate(FoundPred); |
| 5641 | } else { |
| 5642 | std::swap(LHS, RHS); |
| 5643 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 5644 | } |
| 5645 | } |
| 5646 | |
| 5647 | // Check whether the found predicate is the same as the desired predicate. |
| 5648 | if (FoundPred == Pred) |
| 5649 | return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS); |
| 5650 | |
| 5651 | // Check whether swapping the found predicate makes it the same as the |
| 5652 | // desired predicate. |
| 5653 | if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) { |
| 5654 | if (isa<SCEVConstant>(RHS)) |
| 5655 | return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS); |
| 5656 | else |
| 5657 | return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred), |
| 5658 | RHS, LHS, FoundLHS, FoundRHS); |
| 5659 | } |
| 5660 | |
| 5661 | // Check whether the actual condition is beyond sufficient. |
| 5662 | if (FoundPred == ICmpInst::ICMP_EQ) |
| 5663 | if (ICmpInst::isTrueWhenEqual(Pred)) |
| 5664 | if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS)) |
| 5665 | return true; |
| 5666 | if (Pred == ICmpInst::ICMP_NE) |
| 5667 | if (!ICmpInst::isTrueWhenEqual(FoundPred)) |
| 5668 | if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS)) |
| 5669 | return true; |
| 5670 | |
| 5671 | // Otherwise assume the worst. |
| 5672 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5673 | } |
| 5674 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5675 | /// isImpliedCondOperands - Test whether the condition described by Pred, |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 5676 | /// LHS, and RHS is true whenever the condition described by Pred, FoundLHS, |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5677 | /// and FoundRHS is true. |
| 5678 | bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred, |
| 5679 | const SCEV *LHS, const SCEV *RHS, |
| 5680 | const SCEV *FoundLHS, |
| 5681 | const SCEV *FoundRHS) { |
| 5682 | return isImpliedCondOperandsHelper(Pred, LHS, RHS, |
| 5683 | FoundLHS, FoundRHS) || |
| 5684 | // ~x < ~y --> x > y |
| 5685 | isImpliedCondOperandsHelper(Pred, LHS, RHS, |
| 5686 | getNotSCEV(FoundRHS), |
| 5687 | getNotSCEV(FoundLHS)); |
| 5688 | } |
| 5689 | |
| 5690 | /// isImpliedCondOperandsHelper - Test whether the condition described by |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 5691 | /// Pred, LHS, and RHS is true whenever the condition described by Pred, |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5692 | /// FoundLHS, and FoundRHS is true. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5693 | bool |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5694 | ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred, |
| 5695 | const SCEV *LHS, const SCEV *RHS, |
| 5696 | const SCEV *FoundLHS, |
| 5697 | const SCEV *FoundRHS) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5698 | switch (Pred) { |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5699 | default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
| 5700 | case ICmpInst::ICMP_EQ: |
| 5701 | case ICmpInst::ICMP_NE: |
| 5702 | if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS)) |
| 5703 | return true; |
| 5704 | break; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5705 | case ICmpInst::ICMP_SLT: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5706 | case ICmpInst::ICMP_SLE: |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5707 | if (isKnownPredicateWithRanges(ICmpInst::ICMP_SLE, LHS, FoundLHS) && |
| 5708 | isKnownPredicateWithRanges(ICmpInst::ICMP_SGE, RHS, FoundRHS)) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5709 | return true; |
| 5710 | break; |
| 5711 | case ICmpInst::ICMP_SGT: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5712 | case ICmpInst::ICMP_SGE: |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5713 | if (isKnownPredicateWithRanges(ICmpInst::ICMP_SGE, LHS, FoundLHS) && |
| 5714 | isKnownPredicateWithRanges(ICmpInst::ICMP_SLE, RHS, FoundRHS)) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5715 | return true; |
| 5716 | break; |
| 5717 | case ICmpInst::ICMP_ULT: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5718 | case ICmpInst::ICMP_ULE: |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5719 | if (isKnownPredicateWithRanges(ICmpInst::ICMP_ULE, LHS, FoundLHS) && |
| 5720 | isKnownPredicateWithRanges(ICmpInst::ICMP_UGE, RHS, FoundRHS)) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5721 | return true; |
| 5722 | break; |
| 5723 | case ICmpInst::ICMP_UGT: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5724 | case ICmpInst::ICMP_UGE: |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5725 | if (isKnownPredicateWithRanges(ICmpInst::ICMP_UGE, LHS, FoundLHS) && |
| 5726 | isKnownPredicateWithRanges(ICmpInst::ICMP_ULE, RHS, FoundRHS)) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5727 | return true; |
| 5728 | break; |
| 5729 | } |
| 5730 | |
| 5731 | return false; |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 5732 | } |
| 5733 | |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 5734 | /// getBECount - Subtract the end and start values and divide by the step, |
| 5735 | /// rounding up, to get the number of times the backedge is executed. Return |
| 5736 | /// CouldNotCompute if an intermediate computation overflows. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5737 | const SCEV *ScalarEvolution::getBECount(const SCEV *Start, |
Dan Gohman | f5074ec | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 5738 | const SCEV *End, |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 5739 | const SCEV *Step, |
| 5740 | bool NoWrap) { |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5741 | assert(!isKnownNegative(Step) && |
| 5742 | "This code doesn't handle negative strides yet!"); |
| 5743 | |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 5744 | const Type *Ty = Start->getType(); |
Andrew Trick | e62289b | 2011-03-09 17:29:58 +0000 | [diff] [blame] | 5745 | |
| 5746 | // When Start == End, we have an exact BECount == 0. Short-circuit this case |
| 5747 | // here because SCEV may not be able to determine that the unsigned division |
| 5748 | // after rounding is zero. |
| 5749 | if (Start == End) |
| 5750 | return getConstant(Ty, 0); |
| 5751 | |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 5752 | const SCEV *NegOne = getConstant(Ty, (uint64_t)-1); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5753 | const SCEV *Diff = getMinusSCEV(End, Start); |
| 5754 | const SCEV *RoundUp = getAddExpr(Step, NegOne); |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 5755 | |
| 5756 | // Add an adjustment to the difference between End and Start so that |
| 5757 | // the division will effectively round up. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5758 | const SCEV *Add = getAddExpr(Diff, RoundUp); |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 5759 | |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 5760 | if (!NoWrap) { |
| 5761 | // Check Add for unsigned overflow. |
| 5762 | // TODO: More sophisticated things could be done here. |
| 5763 | const Type *WideTy = IntegerType::get(getContext(), |
| 5764 | getTypeSizeInBits(Ty) + 1); |
| 5765 | const SCEV *EDiff = getZeroExtendExpr(Diff, WideTy); |
| 5766 | const SCEV *ERoundUp = getZeroExtendExpr(RoundUp, WideTy); |
| 5767 | const SCEV *OperandExtendedAdd = getAddExpr(EDiff, ERoundUp); |
| 5768 | if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd) |
| 5769 | return getCouldNotCompute(); |
| 5770 | } |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 5771 | |
| 5772 | return getUDivExpr(Add, Step); |
| 5773 | } |
| 5774 | |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5775 | /// HowManyLessThans - Return the number of times a backedge containing the |
| 5776 | /// specified less-than comparison will execute. If not computable, return |
Dan Gohman | 86fbf2f | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 5777 | /// CouldNotCompute. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 5778 | ScalarEvolution::BackedgeTakenInfo |
| 5779 | ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS, |
| 5780 | const Loop *L, bool isSigned) { |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5781 | // Only handle: "ADDREC < LoopInvariant". |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5782 | if (!isLoopInvariant(RHS, L)) return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5783 | |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5784 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5785 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5786 | return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5787 | |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 5788 | // Check to see if we have a flag which makes analysis easy. |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 5789 | bool NoWrap = isSigned ? AddRec->getNoWrapFlags(SCEV::FlagNSW) : |
| 5790 | AddRec->getNoWrapFlags(SCEV::FlagNUW); |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 5791 | |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5792 | if (AddRec->isAffine()) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5793 | unsigned BitWidth = getTypeSizeInBits(AddRec->getType()); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5794 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5795 | |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5796 | if (Step->isZero()) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5797 | return getCouldNotCompute(); |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5798 | if (Step->isOne()) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5799 | // With unit stride, the iteration never steps past the limit value. |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5800 | } else if (isKnownPositive(Step)) { |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 5801 | // Test whether a positive iteration can step past the limit |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5802 | // value and past the maximum value for its type in a single step. |
| 5803 | // Note that it's not sufficient to check NoWrap here, because even |
| 5804 | // though the value after a wrap is undefined, it's not undefined |
| 5805 | // behavior, so if wrap does occur, the loop could either terminate or |
Dan Gohman | 155eec7 | 2010-01-26 18:32:54 +0000 | [diff] [blame] | 5806 | // loop infinitely, but in either case, the loop is guaranteed to |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5807 | // iterate at least until the iteration where the wrapping occurs. |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 5808 | const SCEV *One = getConstant(Step->getType(), 1); |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5809 | if (isSigned) { |
| 5810 | APInt Max = APInt::getSignedMaxValue(BitWidth); |
| 5811 | if ((Max - getSignedRange(getMinusSCEV(Step, One)).getSignedMax()) |
| 5812 | .slt(getSignedRange(RHS).getSignedMax())) |
| 5813 | return getCouldNotCompute(); |
| 5814 | } else { |
| 5815 | APInt Max = APInt::getMaxValue(BitWidth); |
| 5816 | if ((Max - getUnsignedRange(getMinusSCEV(Step, One)).getUnsignedMax()) |
| 5817 | .ult(getUnsignedRange(RHS).getUnsignedMax())) |
| 5818 | return getCouldNotCompute(); |
| 5819 | } |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5820 | } else |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5821 | // TODO: Handle negative strides here and below. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5822 | return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5823 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5824 | // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant |
| 5825 | // m. So, we count the number of iterations in which {n,+,s} < m is true. |
| 5826 | // 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] | 5827 | // treat m-n as signed nor unsigned due to overflow possibility. |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5828 | |
Wojciech Matyjewicz | 3a4cbe2 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 5829 | // 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] | 5830 | const SCEV *Start = AddRec->getOperand(0); |
Wojciech Matyjewicz | 3a4cbe2 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 5831 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5832 | // Determine the minimum constant start value. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5833 | const SCEV *MinStart = getConstant(isSigned ? |
| 5834 | getSignedRange(Start).getSignedMin() : |
| 5835 | getUnsignedRange(Start).getUnsignedMin()); |
Wojciech Matyjewicz | 3a4cbe2 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 5836 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5837 | // If we know that the condition is true in order to enter the loop, |
| 5838 | // 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] | 5839 | // only know that it will execute (max(m,n)-n)/s times. In both cases, |
| 5840 | // the division must round up. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5841 | const SCEV *End = RHS; |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 5842 | if (!isLoopEntryGuardedByCond(L, |
| 5843 | isSigned ? ICmpInst::ICMP_SLT : |
| 5844 | ICmpInst::ICMP_ULT, |
| 5845 | getMinusSCEV(Start, Step), RHS)) |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5846 | End = isSigned ? getSMaxExpr(RHS, Start) |
| 5847 | : getUMaxExpr(RHS, Start); |
| 5848 | |
| 5849 | // Determine the maximum constant end value. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5850 | const SCEV *MaxEnd = getConstant(isSigned ? |
| 5851 | getSignedRange(End).getSignedMax() : |
| 5852 | getUnsignedRange(End).getUnsignedMax()); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5853 | |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5854 | // If MaxEnd is within a step of the maximum integer value in its type, |
| 5855 | // 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] | 5856 | // This allows the subsequent ceiling division of (N+(step-1))/step to |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5857 | // compute the correct value. |
| 5858 | const SCEV *StepMinusOne = getMinusSCEV(Step, |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 5859 | getConstant(Step->getType(), 1)); |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5860 | MaxEnd = isSigned ? |
| 5861 | getSMinExpr(MaxEnd, |
| 5862 | getMinusSCEV(getConstant(APInt::getSignedMaxValue(BitWidth)), |
| 5863 | StepMinusOne)) : |
| 5864 | getUMinExpr(MaxEnd, |
| 5865 | getMinusSCEV(getConstant(APInt::getMaxValue(BitWidth)), |
| 5866 | StepMinusOne)); |
| 5867 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5868 | // Finally, we subtract these two values and divide, rounding up, to get |
| 5869 | // the number of times the backedge is executed. |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 5870 | const SCEV *BECount = getBECount(Start, End, Step, NoWrap); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5871 | |
| 5872 | // The maximum backedge count is similar, except using the minimum start |
| 5873 | // value and the maximum end value. |
Andrew Trick | e62289b | 2011-03-09 17:29:58 +0000 | [diff] [blame] | 5874 | // If we already have an exact constant BECount, use it instead. |
| 5875 | const SCEV *MaxBECount = isa<SCEVConstant>(BECount) ? BECount |
| 5876 | : getBECount(MinStart, MaxEnd, Step, NoWrap); |
| 5877 | |
| 5878 | // If the stride is nonconstant, and NoWrap == true, then |
| 5879 | // getBECount(MinStart, MaxEnd) may not compute. This would result in an |
| 5880 | // exact BECount and invalid MaxBECount, which should be avoided to catch |
| 5881 | // more optimization opportunities. |
| 5882 | if (isa<SCEVCouldNotCompute>(MaxBECount)) |
| 5883 | MaxBECount = BECount; |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5884 | |
| 5885 | return BackedgeTakenInfo(BECount, MaxBECount); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5886 | } |
| 5887 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5888 | return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5889 | } |
| 5890 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5891 | /// getNumIterationsInRange - Return the number of iterations of this loop that |
| 5892 | /// produce values in the specified constant range. Another way of looking at |
| 5893 | /// this is that it returns the first iteration number where the value is not in |
| 5894 | /// the condition, thus computing the exit count. If the iteration count can't |
| 5895 | /// be computed, an instance of SCEVCouldNotCompute is returned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5896 | const SCEV *SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 5897 | ScalarEvolution &SE) const { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5898 | if (Range.isFullSet()) // Infinite loop. |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5899 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5900 | |
| 5901 | // 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] | 5902 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart())) |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 5903 | if (!SC->getValue()->isZero()) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5904 | SmallVector<const SCEV *, 4> Operands(op_begin(), op_end()); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 5905 | Operands[0] = SE.getConstant(SC->getType(), 0); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 5906 | const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop(), |
Andrew Trick | c343c1e | 2011-03-15 00:37:00 +0000 | [diff] [blame] | 5907 | getNoWrapFlags(FlagNW)); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 5908 | if (const SCEVAddRecExpr *ShiftedAddRec = |
| 5909 | dyn_cast<SCEVAddRecExpr>(Shifted)) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5910 | return ShiftedAddRec->getNumIterationsInRange( |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5911 | Range.subtract(SC->getValue()->getValue()), SE); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5912 | // This is strange and shouldn't happen. |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5913 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5914 | } |
| 5915 | |
| 5916 | // The only time we can solve this is when we have all constant indices. |
| 5917 | // Otherwise, we cannot determine the overflow conditions. |
| 5918 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 5919 | if (!isa<SCEVConstant>(getOperand(i))) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5920 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5921 | |
| 5922 | |
| 5923 | // Okay at this point we know that all elements of the chrec are constants and |
| 5924 | // that the start element is zero. |
| 5925 | |
| 5926 | // First check to see if the range contains zero. If not, the first |
| 5927 | // iteration exits. |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 5928 | unsigned BitWidth = SE.getTypeSizeInBits(getType()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 5929 | if (!Range.contains(APInt(BitWidth, 0))) |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 5930 | return SE.getConstant(getType(), 0); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5931 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5932 | if (isAffine()) { |
| 5933 | // If this is an affine expression then we have this situation: |
| 5934 | // Solve {0,+,A} in Range === Ax in Range |
| 5935 | |
Nick Lewycky | eefdebe | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 5936 | // We know that zero is in the range. If A is positive then we know that |
| 5937 | // the upper value of the range must be the first possible exit value. |
| 5938 | // If A is negative then the lower of the range is the last possible loop |
| 5939 | // value. Also note that we already checked for a full range. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 5940 | APInt One(BitWidth,1); |
Nick Lewycky | eefdebe | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 5941 | APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue(); |
| 5942 | APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5943 | |
Nick Lewycky | eefdebe | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 5944 | // The exit value should be (End+A)/A. |
Nick Lewycky | 9a2f931 | 2007-09-27 14:12:54 +0000 | [diff] [blame] | 5945 | APInt ExitVal = (End + A).udiv(A); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5946 | ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5947 | |
| 5948 | // Evaluate at the exit value. If we really did fall out of the valid |
| 5949 | // range, then we computed our trip count, otherwise wrap around or other |
| 5950 | // things must have happened. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5951 | ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5952 | if (Range.contains(Val->getValue())) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5953 | return SE.getCouldNotCompute(); // Something strange happened |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5954 | |
| 5955 | // 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] | 5956 | assert(Range.contains( |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 5957 | EvaluateConstantChrecAtConstant(this, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5958 | ConstantInt::get(SE.getContext(), ExitVal - One), SE)->getValue()) && |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5959 | "Linear scev computation is off in a bad way!"); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5960 | return SE.getConstant(ExitValue); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5961 | } else if (isQuadratic()) { |
| 5962 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the |
| 5963 | // quadratic equation to solve it. To do this, we must frame our problem in |
| 5964 | // terms of figuring out when zero is crossed, instead of when |
| 5965 | // Range.getUpper() is crossed. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5966 | SmallVector<const SCEV *, 4> NewOps(op_begin(), op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5967 | NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper())); |
Andrew Trick | 3228cc2 | 2011-03-14 16:50:06 +0000 | [diff] [blame] | 5968 | const SCEV *NewAddRec = SE.getAddRecExpr(NewOps, getLoop(), |
| 5969 | // getNoWrapFlags(FlagNW) |
| 5970 | FlagAnyWrap); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5971 | |
| 5972 | // Next, solve the constructed addrec |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5973 | std::pair<const SCEV *,const SCEV *> Roots = |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5974 | SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5975 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 5976 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5977 | if (R1) { |
| 5978 | // Pick the smallest positive root value. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5979 | if (ConstantInt *CB = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 5980 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 5981 | R1->getValue(), R2->getValue()))) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5982 | if (CB->getZExtValue() == false) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5983 | std::swap(R1, R2); // R1 is the minimum root now. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5984 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5985 | // Make sure the root is not off by one. The returned iteration should |
| 5986 | // not be in the range, but the previous one should be. When solving |
| 5987 | // for "X*X < 5", for example, we should not return a root of 2. |
| 5988 | ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this, |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5989 | R1->getValue(), |
| 5990 | SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5991 | if (Range.contains(R1Val->getValue())) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5992 | // The next iteration must be out of the range... |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 5993 | ConstantInt *NextVal = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5994 | ConstantInt::get(SE.getContext(), R1->getValue()->getValue()+1); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5995 | |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5996 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5997 | if (!Range.contains(R1Val->getValue())) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5998 | return SE.getConstant(NextVal); |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5999 | return SE.getCouldNotCompute(); // Something strange happened |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6000 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 6001 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6002 | // If R1 was not in the range, then it is a good return value. Make |
| 6003 | // sure that R1-1 WAS in the range though, just in case. |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 6004 | ConstantInt *NextVal = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 6005 | ConstantInt::get(SE.getContext(), R1->getValue()->getValue()-1); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 6006 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 6007 | if (Range.contains(R1Val->getValue())) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6008 | return R1; |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 6009 | return SE.getCouldNotCompute(); // Something strange happened |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6010 | } |
| 6011 | } |
| 6012 | } |
| 6013 | |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 6014 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6015 | } |
| 6016 | |
| 6017 | |
| 6018 | |
| 6019 | //===----------------------------------------------------------------------===// |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 6020 | // SCEVCallbackVH Class Implementation |
| 6021 | //===----------------------------------------------------------------------===// |
| 6022 | |
Dan Gohman | 1959b75 | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 6023 | void ScalarEvolution::SCEVCallbackVH::deleted() { |
Dan Gohman | ddf9f99 | 2009-07-13 22:20:53 +0000 | [diff] [blame] | 6024 | assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 6025 | if (PHINode *PN = dyn_cast<PHINode>(getValPtr())) |
| 6026 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 6027 | SE->ValueExprMap.erase(getValPtr()); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 6028 | // this now dangles! |
| 6029 | } |
| 6030 | |
Dan Gohman | 81f9121 | 2010-07-28 01:09:07 +0000 | [diff] [blame] | 6031 | void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *V) { |
Dan Gohman | ddf9f99 | 2009-07-13 22:20:53 +0000 | [diff] [blame] | 6032 | assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); |
Eric Christopher | e6cbfa6 | 2010-07-29 01:25:38 +0000 | [diff] [blame] | 6033 | |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 6034 | // Forget all the expressions associated with users of the old value, |
| 6035 | // so that future queries will recompute the expressions using the new |
| 6036 | // value. |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 6037 | Value *Old = getValPtr(); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 6038 | SmallVector<User *, 16> Worklist; |
Dan Gohman | 69fcae9 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 6039 | SmallPtrSet<User *, 8> Visited; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 6040 | for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end(); |
| 6041 | UI != UE; ++UI) |
| 6042 | Worklist.push_back(*UI); |
| 6043 | while (!Worklist.empty()) { |
| 6044 | User *U = Worklist.pop_back_val(); |
| 6045 | // Deleting the Old value will cause this to dangle. Postpone |
| 6046 | // that until everything else is done. |
Dan Gohman | 59846ac | 2010-07-28 00:28:25 +0000 | [diff] [blame] | 6047 | if (U == Old) |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 6048 | continue; |
Dan Gohman | 69fcae9 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 6049 | if (!Visited.insert(U)) |
| 6050 | continue; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 6051 | if (PHINode *PN = dyn_cast<PHINode>(U)) |
| 6052 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 6053 | SE->ValueExprMap.erase(U); |
Dan Gohman | 69fcae9 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 6054 | for (Value::use_iterator UI = U->use_begin(), UE = U->use_end(); |
| 6055 | UI != UE; ++UI) |
| 6056 | Worklist.push_back(*UI); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 6057 | } |
Dan Gohman | 59846ac | 2010-07-28 00:28:25 +0000 | [diff] [blame] | 6058 | // Delete the Old value. |
| 6059 | if (PHINode *PN = dyn_cast<PHINode>(Old)) |
| 6060 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 6061 | SE->ValueExprMap.erase(Old); |
Dan Gohman | 59846ac | 2010-07-28 00:28:25 +0000 | [diff] [blame] | 6062 | // this now dangles! |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 6063 | } |
| 6064 | |
Dan Gohman | 1959b75 | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 6065 | ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 6066 | : CallbackVH(V), SE(se) {} |
| 6067 | |
| 6068 | //===----------------------------------------------------------------------===// |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6069 | // ScalarEvolution Class Implementation |
| 6070 | //===----------------------------------------------------------------------===// |
| 6071 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 6072 | ScalarEvolution::ScalarEvolution() |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 6073 | : FunctionPass(ID), FirstUnknown(0) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 6074 | initializeScalarEvolutionPass(*PassRegistry::getPassRegistry()); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 6075 | } |
| 6076 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6077 | bool ScalarEvolution::runOnFunction(Function &F) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 6078 | this->F = &F; |
| 6079 | LI = &getAnalysis<LoopInfo>(); |
| 6080 | TD = getAnalysisIfAvailable<TargetData>(); |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 6081 | DT = &getAnalysis<DominatorTree>(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6082 | return false; |
| 6083 | } |
| 6084 | |
| 6085 | void ScalarEvolution::releaseMemory() { |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 6086 | // Iterate through all the SCEVUnknown instances and call their |
| 6087 | // destructors, so that they release their references to their values. |
| 6088 | for (SCEVUnknown *U = FirstUnknown; U; U = U->Next) |
| 6089 | U->~SCEVUnknown(); |
| 6090 | FirstUnknown = 0; |
| 6091 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 6092 | ValueExprMap.clear(); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 6093 | BackedgeTakenCounts.clear(); |
| 6094 | ConstantEvolutionLoopExitValue.clear(); |
Dan Gohman | 6bce643 | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 6095 | ValuesAtScopes.clear(); |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 6096 | LoopDispositions.clear(); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6097 | BlockDispositions.clear(); |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 6098 | UnsignedRanges.clear(); |
| 6099 | SignedRanges.clear(); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 6100 | UniqueSCEVs.clear(); |
| 6101 | SCEVAllocator.Reset(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6102 | } |
| 6103 | |
| 6104 | void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const { |
| 6105 | AU.setPreservesAll(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6106 | AU.addRequiredTransitive<LoopInfo>(); |
Dan Gohman | 1cd9275 | 2010-01-19 22:21:27 +0000 | [diff] [blame] | 6107 | AU.addRequiredTransitive<DominatorTree>(); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 6108 | } |
| 6109 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 6110 | bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 6111 | return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6112 | } |
| 6113 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 6114 | static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6115 | const Loop *L) { |
| 6116 | // Print all inner loops first |
| 6117 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 6118 | PrintLoopInfo(OS, SE, *I); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 6119 | |
Dan Gohman | 3073329 | 2010-01-09 18:17:45 +0000 | [diff] [blame] | 6120 | OS << "Loop "; |
| 6121 | WriteAsOperand(OS, L->getHeader(), /*PrintType=*/false); |
| 6122 | OS << ": "; |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 6123 | |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 6124 | SmallVector<BasicBlock *, 8> ExitBlocks; |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 6125 | L->getExitBlocks(ExitBlocks); |
| 6126 | if (ExitBlocks.size() != 1) |
Nick Lewycky | aeb5e5c | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 6127 | OS << "<multiple exits> "; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6128 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 6129 | if (SE->hasLoopInvariantBackedgeTakenCount(L)) { |
| 6130 | OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6131 | } else { |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 6132 | OS << "Unpredictable backedge-taken count. "; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6133 | } |
| 6134 | |
Dan Gohman | 3073329 | 2010-01-09 18:17:45 +0000 | [diff] [blame] | 6135 | OS << "\n" |
| 6136 | "Loop "; |
| 6137 | WriteAsOperand(OS, L->getHeader(), /*PrintType=*/false); |
| 6138 | OS << ": "; |
Dan Gohman | aa551ae | 2009-06-24 00:33:16 +0000 | [diff] [blame] | 6139 | |
| 6140 | if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) { |
| 6141 | OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L); |
| 6142 | } else { |
| 6143 | OS << "Unpredictable max backedge-taken count. "; |
| 6144 | } |
| 6145 | |
| 6146 | OS << "\n"; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6147 | } |
| 6148 | |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 6149 | void ScalarEvolution::print(raw_ostream &OS, const Module *) const { |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 6150 | // ScalarEvolution's implementation of the print method is to print |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 6151 | // out SCEV values of all instructions that are interesting. Doing |
| 6152 | // this potentially causes it to create new SCEV objects though, |
| 6153 | // which technically conflicts with the const qualifier. This isn't |
Dan Gohman | 1afdc5f | 2009-07-10 20:25:29 +0000 | [diff] [blame] | 6154 | // observable from outside the class though, so casting away the |
| 6155 | // const isn't dangerous. |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 6156 | ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6157 | |
Dan Gohman | 3073329 | 2010-01-09 18:17:45 +0000 | [diff] [blame] | 6158 | OS << "Classifying expressions for: "; |
| 6159 | WriteAsOperand(OS, F, /*PrintType=*/false); |
| 6160 | OS << "\n"; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6161 | 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] | 6162 | if (isSCEVable(I->getType()) && !isa<CmpInst>(*I)) { |
Dan Gohman | c902e13 | 2009-07-13 23:03:05 +0000 | [diff] [blame] | 6163 | OS << *I << '\n'; |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 6164 | OS << " --> "; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 6165 | const SCEV *SV = SE.getSCEV(&*I); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6166 | SV->print(OS); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 6167 | |
Dan Gohman | 0c689c5 | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 6168 | const Loop *L = LI->getLoopFor((*I).getParent()); |
| 6169 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 6170 | const SCEV *AtUse = SE.getSCEVAtScope(SV, L); |
Dan Gohman | 0c689c5 | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 6171 | if (AtUse != SV) { |
| 6172 | OS << " --> "; |
| 6173 | AtUse->print(OS); |
| 6174 | } |
| 6175 | |
| 6176 | if (L) { |
Dan Gohman | 9e7d988 | 2009-06-18 00:37:45 +0000 | [diff] [blame] | 6177 | OS << "\t\t" "Exits: "; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 6178 | const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop()); |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6179 | if (!SE.isLoopInvariant(ExitValue, L)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6180 | OS << "<<Unknown>>"; |
| 6181 | } else { |
| 6182 | OS << *ExitValue; |
| 6183 | } |
| 6184 | } |
| 6185 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6186 | OS << "\n"; |
| 6187 | } |
| 6188 | |
Dan Gohman | 3073329 | 2010-01-09 18:17:45 +0000 | [diff] [blame] | 6189 | OS << "Determining loop execution counts for: "; |
| 6190 | WriteAsOperand(OS, F, /*PrintType=*/false); |
| 6191 | OS << "\n"; |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 6192 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
| 6193 | PrintLoopInfo(OS, &SE, *I); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 6194 | } |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 6195 | |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 6196 | ScalarEvolution::LoopDisposition |
| 6197 | ScalarEvolution::getLoopDisposition(const SCEV *S, const Loop *L) { |
| 6198 | std::map<const Loop *, LoopDisposition> &Values = LoopDispositions[S]; |
| 6199 | std::pair<std::map<const Loop *, LoopDisposition>::iterator, bool> Pair = |
| 6200 | Values.insert(std::make_pair(L, LoopVariant)); |
| 6201 | if (!Pair.second) |
| 6202 | return Pair.first->second; |
| 6203 | |
| 6204 | LoopDisposition D = computeLoopDisposition(S, L); |
| 6205 | return LoopDispositions[S][L] = D; |
| 6206 | } |
| 6207 | |
| 6208 | ScalarEvolution::LoopDisposition |
| 6209 | ScalarEvolution::computeLoopDisposition(const SCEV *S, const Loop *L) { |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6210 | switch (S->getSCEVType()) { |
| 6211 | case scConstant: |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 6212 | return LoopInvariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6213 | case scTruncate: |
| 6214 | case scZeroExtend: |
| 6215 | case scSignExtend: |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 6216 | return getLoopDisposition(cast<SCEVCastExpr>(S)->getOperand(), L); |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6217 | case scAddRecExpr: { |
| 6218 | const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S); |
| 6219 | |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 6220 | // If L is the addrec's loop, it's computable. |
| 6221 | if (AR->getLoop() == L) |
| 6222 | return LoopComputable; |
| 6223 | |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6224 | // Add recurrences are never invariant in the function-body (null loop). |
| 6225 | if (!L) |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 6226 | return LoopVariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6227 | |
| 6228 | // This recurrence is variant w.r.t. L if L contains AR's loop. |
| 6229 | if (L->contains(AR->getLoop())) |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 6230 | return LoopVariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6231 | |
| 6232 | // This recurrence is invariant w.r.t. L if AR's loop contains L. |
| 6233 | if (AR->getLoop()->contains(L)) |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 6234 | return LoopInvariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6235 | |
| 6236 | // This recurrence is variant w.r.t. L if any of its operands |
| 6237 | // are variant. |
| 6238 | for (SCEVAddRecExpr::op_iterator I = AR->op_begin(), E = AR->op_end(); |
| 6239 | I != E; ++I) |
| 6240 | if (!isLoopInvariant(*I, L)) |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 6241 | return LoopVariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6242 | |
| 6243 | // Otherwise it's loop-invariant. |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 6244 | return LoopInvariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6245 | } |
| 6246 | case scAddExpr: |
| 6247 | case scMulExpr: |
| 6248 | case scUMaxExpr: |
| 6249 | case scSMaxExpr: { |
| 6250 | const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S); |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6251 | bool HasVarying = false; |
| 6252 | for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end(); |
| 6253 | I != E; ++I) { |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 6254 | LoopDisposition D = getLoopDisposition(*I, L); |
| 6255 | if (D == LoopVariant) |
| 6256 | return LoopVariant; |
| 6257 | if (D == LoopComputable) |
| 6258 | HasVarying = true; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6259 | } |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 6260 | return HasVarying ? LoopComputable : LoopInvariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6261 | } |
| 6262 | case scUDivExpr: { |
| 6263 | const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S); |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 6264 | LoopDisposition LD = getLoopDisposition(UDiv->getLHS(), L); |
| 6265 | if (LD == LoopVariant) |
| 6266 | return LoopVariant; |
| 6267 | LoopDisposition RD = getLoopDisposition(UDiv->getRHS(), L); |
| 6268 | if (RD == LoopVariant) |
| 6269 | return LoopVariant; |
| 6270 | return (LD == LoopInvariant && RD == LoopInvariant) ? |
| 6271 | LoopInvariant : LoopComputable; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6272 | } |
| 6273 | case scUnknown: |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 6274 | // All non-instruction values are loop invariant. All instructions are loop |
| 6275 | // invariant if they are not contained in the specified loop. |
| 6276 | // Instructions are never considered invariant in the function body |
| 6277 | // (null loop) because they are defined within the "loop". |
| 6278 | if (Instruction *I = dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) |
| 6279 | return (L && !L->contains(I)) ? LoopInvariant : LoopVariant; |
| 6280 | return LoopInvariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6281 | case scCouldNotCompute: |
| 6282 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 6283 | return LoopVariant; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6284 | default: break; |
| 6285 | } |
| 6286 | llvm_unreachable("Unknown SCEV kind!"); |
Dan Gohman | 714b529 | 2010-11-17 23:21:44 +0000 | [diff] [blame] | 6287 | return LoopVariant; |
| 6288 | } |
| 6289 | |
| 6290 | bool ScalarEvolution::isLoopInvariant(const SCEV *S, const Loop *L) { |
| 6291 | return getLoopDisposition(S, L) == LoopInvariant; |
| 6292 | } |
| 6293 | |
| 6294 | bool ScalarEvolution::hasComputableLoopEvolution(const SCEV *S, const Loop *L) { |
| 6295 | return getLoopDisposition(S, L) == LoopComputable; |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 6296 | } |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6297 | |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6298 | ScalarEvolution::BlockDisposition |
| 6299 | ScalarEvolution::getBlockDisposition(const SCEV *S, const BasicBlock *BB) { |
| 6300 | std::map<const BasicBlock *, BlockDisposition> &Values = BlockDispositions[S]; |
| 6301 | std::pair<std::map<const BasicBlock *, BlockDisposition>::iterator, bool> |
| 6302 | Pair = Values.insert(std::make_pair(BB, DoesNotDominateBlock)); |
| 6303 | if (!Pair.second) |
| 6304 | return Pair.first->second; |
| 6305 | |
| 6306 | BlockDisposition D = computeBlockDisposition(S, BB); |
| 6307 | return BlockDispositions[S][BB] = D; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6308 | } |
| 6309 | |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6310 | ScalarEvolution::BlockDisposition |
| 6311 | ScalarEvolution::computeBlockDisposition(const SCEV *S, const BasicBlock *BB) { |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6312 | switch (S->getSCEVType()) { |
| 6313 | case scConstant: |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6314 | return ProperlyDominatesBlock; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6315 | case scTruncate: |
| 6316 | case scZeroExtend: |
| 6317 | case scSignExtend: |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6318 | return getBlockDisposition(cast<SCEVCastExpr>(S)->getOperand(), BB); |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6319 | case scAddRecExpr: { |
| 6320 | // This uses a "dominates" query instead of "properly dominates" query |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6321 | // to test for proper dominance too, because the instruction which |
| 6322 | // produces the addrec's value is a PHI, and a PHI effectively properly |
| 6323 | // dominates its entire containing block. |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6324 | const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S); |
| 6325 | if (!DT->dominates(AR->getLoop()->getHeader(), BB)) |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6326 | return DoesNotDominateBlock; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6327 | } |
| 6328 | // FALL THROUGH into SCEVNAryExpr handling. |
| 6329 | case scAddExpr: |
| 6330 | case scMulExpr: |
| 6331 | case scUMaxExpr: |
| 6332 | case scSMaxExpr: { |
| 6333 | const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6334 | bool Proper = true; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6335 | for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end(); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6336 | I != E; ++I) { |
| 6337 | BlockDisposition D = getBlockDisposition(*I, BB); |
| 6338 | if (D == DoesNotDominateBlock) |
| 6339 | return DoesNotDominateBlock; |
| 6340 | if (D == DominatesBlock) |
| 6341 | Proper = false; |
| 6342 | } |
| 6343 | return Proper ? ProperlyDominatesBlock : DominatesBlock; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6344 | } |
| 6345 | case scUDivExpr: { |
| 6346 | const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6347 | const SCEV *LHS = UDiv->getLHS(), *RHS = UDiv->getRHS(); |
| 6348 | BlockDisposition LD = getBlockDisposition(LHS, BB); |
| 6349 | if (LD == DoesNotDominateBlock) |
| 6350 | return DoesNotDominateBlock; |
| 6351 | BlockDisposition RD = getBlockDisposition(RHS, BB); |
| 6352 | if (RD == DoesNotDominateBlock) |
| 6353 | return DoesNotDominateBlock; |
| 6354 | return (LD == ProperlyDominatesBlock && RD == ProperlyDominatesBlock) ? |
| 6355 | ProperlyDominatesBlock : DominatesBlock; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6356 | } |
| 6357 | case scUnknown: |
| 6358 | if (Instruction *I = |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6359 | dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) { |
| 6360 | if (I->getParent() == BB) |
| 6361 | return DominatesBlock; |
| 6362 | if (DT->properlyDominates(I->getParent(), BB)) |
| 6363 | return ProperlyDominatesBlock; |
| 6364 | return DoesNotDominateBlock; |
| 6365 | } |
| 6366 | return ProperlyDominatesBlock; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6367 | case scCouldNotCompute: |
| 6368 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6369 | return DoesNotDominateBlock; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6370 | default: break; |
| 6371 | } |
| 6372 | llvm_unreachable("Unknown SCEV kind!"); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6373 | return DoesNotDominateBlock; |
| 6374 | } |
| 6375 | |
| 6376 | bool ScalarEvolution::dominates(const SCEV *S, const BasicBlock *BB) { |
| 6377 | return getBlockDisposition(S, BB) >= DominatesBlock; |
| 6378 | } |
| 6379 | |
| 6380 | bool ScalarEvolution::properlyDominates(const SCEV *S, const BasicBlock *BB) { |
| 6381 | return getBlockDisposition(S, BB) == ProperlyDominatesBlock; |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame] | 6382 | } |
Dan Gohman | 4ce32db | 2010-11-17 22:27:42 +0000 | [diff] [blame] | 6383 | |
| 6384 | bool ScalarEvolution::hasOperand(const SCEV *S, const SCEV *Op) const { |
| 6385 | switch (S->getSCEVType()) { |
| 6386 | case scConstant: |
| 6387 | return false; |
| 6388 | case scTruncate: |
| 6389 | case scZeroExtend: |
| 6390 | case scSignExtend: { |
| 6391 | const SCEVCastExpr *Cast = cast<SCEVCastExpr>(S); |
| 6392 | const SCEV *CastOp = Cast->getOperand(); |
| 6393 | return Op == CastOp || hasOperand(CastOp, Op); |
| 6394 | } |
| 6395 | case scAddRecExpr: |
| 6396 | case scAddExpr: |
| 6397 | case scMulExpr: |
| 6398 | case scUMaxExpr: |
| 6399 | case scSMaxExpr: { |
| 6400 | const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S); |
| 6401 | for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end(); |
| 6402 | I != E; ++I) { |
| 6403 | const SCEV *NAryOp = *I; |
| 6404 | if (NAryOp == Op || hasOperand(NAryOp, Op)) |
| 6405 | return true; |
| 6406 | } |
| 6407 | return false; |
| 6408 | } |
| 6409 | case scUDivExpr: { |
| 6410 | const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S); |
| 6411 | const SCEV *LHS = UDiv->getLHS(), *RHS = UDiv->getRHS(); |
| 6412 | return LHS == Op || hasOperand(LHS, Op) || |
| 6413 | RHS == Op || hasOperand(RHS, Op); |
| 6414 | } |
| 6415 | case scUnknown: |
| 6416 | return false; |
| 6417 | case scCouldNotCompute: |
| 6418 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
| 6419 | return false; |
| 6420 | default: break; |
| 6421 | } |
| 6422 | llvm_unreachable("Unknown SCEV kind!"); |
| 6423 | return false; |
| 6424 | } |
Dan Gohman | 56a7568 | 2010-11-17 23:28:48 +0000 | [diff] [blame] | 6425 | |
| 6426 | void ScalarEvolution::forgetMemoizedResults(const SCEV *S) { |
| 6427 | ValuesAtScopes.erase(S); |
| 6428 | LoopDispositions.erase(S); |
Dan Gohman | 9c9fcfc | 2010-11-18 00:34:22 +0000 | [diff] [blame] | 6429 | BlockDispositions.erase(S); |
Dan Gohman | 56a7568 | 2010-11-17 23:28:48 +0000 | [diff] [blame] | 6430 | UnsignedRanges.erase(S); |
| 6431 | SignedRanges.erase(S); |
| 6432 | } |