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 | SCEV::~SCEV() {} |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 125 | void SCEV::dump() const { |
David Greene | 25e0e87 | 2009-12-23 22:18:14 +0000 | [diff] [blame] | 126 | print(dbgs()); |
| 127 | dbgs() << '\n'; |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 130 | bool SCEV::isZero() const { |
| 131 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 132 | return SC->getValue()->isZero(); |
| 133 | return false; |
| 134 | } |
| 135 | |
Dan Gohman | 70a1fe7 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 136 | bool SCEV::isOne() const { |
| 137 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 138 | return SC->getValue()->isOne(); |
| 139 | return false; |
| 140 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 141 | |
Dan Gohman | 4d289bf | 2009-06-24 00:30:26 +0000 | [diff] [blame] | 142 | bool SCEV::isAllOnesValue() const { |
| 143 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 144 | return SC->getValue()->isAllOnesValue(); |
| 145 | return false; |
| 146 | } |
| 147 | |
Owen Anderson | 753ad61 | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 148 | SCEVCouldNotCompute::SCEVCouldNotCompute() : |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 149 | SCEV(FoldingSetNodeIDRef(), scCouldNotCompute) {} |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 151 | const Type *SCEVCouldNotCompute::getType() const { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 152 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
Misha Brukman | bb2aff1 | 2004-04-05 19:00:46 +0000 | [diff] [blame] | 153 | return 0; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 156 | bool SCEVCouldNotCompute::hasOperand(const SCEV *) const { |
| 157 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
| 158 | return false; |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 161 | void SCEVCouldNotCompute::print(raw_ostream &OS) const { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 162 | OS << "***COULDNOTCOMPUTE***"; |
| 163 | } |
| 164 | |
| 165 | bool SCEVCouldNotCompute::classof(const SCEV *S) { |
| 166 | return S->getSCEVType() == scCouldNotCompute; |
| 167 | } |
| 168 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 169 | const SCEV *ScalarEvolution::getConstant(ConstantInt *V) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 170 | FoldingSetNodeID ID; |
| 171 | ID.AddInteger(scConstant); |
| 172 | ID.AddPointer(V); |
| 173 | void *IP = 0; |
| 174 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 175 | SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 176 | UniqueSCEVs.InsertNode(S, IP); |
| 177 | return S; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 178 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 179 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 180 | const SCEV *ScalarEvolution::getConstant(const APInt& Val) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 181 | return getConstant(ConstantInt::get(getContext(), Val)); |
Dan Gohman | 9a6ae96 | 2007-07-09 15:25:17 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 184 | const SCEV * |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 185 | ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) { |
Dan Gohman | a560fd2 | 2010-04-21 16:04:04 +0000 | [diff] [blame] | 186 | const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty)); |
| 187 | return getConstant(ConstantInt::get(ITy, V, isSigned)); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 190 | const Type *SCEVConstant::getType() const { return V->getType(); } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 191 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 192 | void SCEVConstant::print(raw_ostream &OS) const { |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 193 | WriteAsOperand(OS, V, false); |
| 194 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 195 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 196 | SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID, |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 197 | unsigned SCEVTy, const SCEV *op, const Type *ty) |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 198 | : SCEV(ID, SCEVTy), Op(op), Ty(ty) {} |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 199 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 200 | SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID, |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 201 | const SCEV *op, const Type *ty) |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 202 | : SCEVCastExpr(ID, scTruncate, op, ty) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 203 | assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) && |
| 204 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 205 | "Cannot truncate non-integer value!"); |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 206 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 207 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 208 | void SCEVTruncateExpr::print(raw_ostream &OS) const { |
Dan Gohman | 36b8e53 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 209 | OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 212 | SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID, |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 213 | const SCEV *op, const Type *ty) |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 214 | : SCEVCastExpr(ID, scZeroExtend, op, ty) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 215 | assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) && |
| 216 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 217 | "Cannot zero extend non-integer value!"); |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 220 | void SCEVZeroExtendExpr::print(raw_ostream &OS) const { |
Dan Gohman | 36b8e53 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 221 | OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 224 | SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID, |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 225 | const SCEV *op, const Type *ty) |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 226 | : SCEVCastExpr(ID, scSignExtend, op, ty) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 227 | assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) && |
| 228 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 229 | "Cannot sign extend non-integer value!"); |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 232 | void SCEVSignExtendExpr::print(raw_ostream &OS) const { |
Dan Gohman | 36b8e53 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 233 | OS << "(sext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 236 | void SCEVCommutativeExpr::print(raw_ostream &OS) const { |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 237 | const char *OpStr = getOperationStr(); |
Dan Gohman | a5145c8 | 2010-04-16 15:03:25 +0000 | [diff] [blame] | 238 | OS << "("; |
| 239 | for (op_iterator I = op_begin(), E = op_end(); I != E; ++I) { |
| 240 | OS << **I; |
Oscar Fuentes | ee56c42 | 2010-08-02 06:00:15 +0000 | [diff] [blame] | 241 | if (llvm::next(I) != E) |
Dan Gohman | a5145c8 | 2010-04-16 15:03:25 +0000 | [diff] [blame] | 242 | OS << OpStr; |
| 243 | } |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 244 | OS << ")"; |
| 245 | } |
| 246 | |
Dan Gohman | 2f199f9 | 2010-08-16 16:21:27 +0000 | [diff] [blame] | 247 | bool SCEVNAryExpr::hasOperand(const SCEV *O) const { |
| 248 | for (op_iterator I = op_begin(), E = op_end(); I != E; ++I) { |
| 249 | const SCEV *S = *I; |
| 250 | if (O == S || S->hasOperand(O)) |
| 251 | return true; |
| 252 | } |
| 253 | return false; |
| 254 | } |
| 255 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 256 | void SCEVUDivExpr::print(raw_ostream &OS) const { |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 257 | OS << "(" << *LHS << " /u " << *RHS << ")"; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 260 | const Type *SCEVUDivExpr::getType() const { |
Dan Gohman | 91bb61a | 2009-05-26 17:44:05 +0000 | [diff] [blame] | 261 | // In most cases the types of LHS and RHS will be the same, but in some |
| 262 | // crazy cases one or the other may be a pointer. ScalarEvolution doesn't |
| 263 | // depend on the type for correctness, but handling types carefully can |
| 264 | // avoid extra casts in the SCEVExpander. The LHS is more likely to be |
| 265 | // a pointer type than the RHS, so use the RHS' type here. |
| 266 | return RHS->getType(); |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 269 | void SCEVAddRecExpr::print(raw_ostream &OS) const { |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 270 | OS << "{" << *Operands[0]; |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 271 | for (unsigned i = 1, e = NumOperands; i != e; ++i) |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 272 | OS << ",+," << *Operands[i]; |
Dan Gohman | 3073329 | 2010-01-09 18:17:45 +0000 | [diff] [blame] | 273 | OS << "}<"; |
| 274 | WriteAsOperand(OS, L->getHeader(), /*PrintType=*/false); |
| 275 | OS << ">"; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 276 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 277 | |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 278 | void SCEVUnknown::deleted() { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 279 | // Clear this SCEVUnknown from various maps. |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 280 | SE->ValuesAtScopes.erase(this); |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 281 | SE->UnsignedRanges.erase(this); |
| 282 | SE->SignedRanges.erase(this); |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 283 | |
| 284 | // Remove this SCEVUnknown from the uniquing map. |
| 285 | SE->UniqueSCEVs.RemoveNode(this); |
| 286 | |
| 287 | // Release the value. |
| 288 | setValPtr(0); |
| 289 | } |
| 290 | |
| 291 | void SCEVUnknown::allUsesReplacedWith(Value *New) { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 292 | // Clear this SCEVUnknown from various maps. |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 293 | SE->ValuesAtScopes.erase(this); |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 294 | SE->UnsignedRanges.erase(this); |
| 295 | SE->SignedRanges.erase(this); |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 296 | |
| 297 | // Remove this SCEVUnknown from the uniquing map. |
| 298 | SE->UniqueSCEVs.RemoveNode(this); |
| 299 | |
| 300 | // Update this SCEVUnknown to point to the new value. This is needed |
| 301 | // because there may still be outstanding SCEVs which still point to |
| 302 | // this SCEVUnknown. |
| 303 | setValPtr(New); |
| 304 | } |
| 305 | |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 306 | const Type *SCEVUnknown::getType() const { |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 307 | return getValue()->getType(); |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 308 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 309 | |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 310 | bool SCEVUnknown::isSizeOf(const Type *&AllocTy) const { |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 311 | if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue())) |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 312 | if (VCE->getOpcode() == Instruction::PtrToInt) |
| 313 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0))) |
Dan Gohman | 8db08df | 2010-02-02 01:38:49 +0000 | [diff] [blame] | 314 | if (CE->getOpcode() == Instruction::GetElementPtr && |
| 315 | CE->getOperand(0)->isNullValue() && |
| 316 | CE->getNumOperands() == 2) |
| 317 | if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(1))) |
| 318 | if (CI->isOne()) { |
| 319 | AllocTy = cast<PointerType>(CE->getOperand(0)->getType()) |
| 320 | ->getElementType(); |
| 321 | return true; |
| 322 | } |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 323 | |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | bool SCEVUnknown::isAlignOf(const Type *&AllocTy) const { |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 328 | if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue())) |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 329 | if (VCE->getOpcode() == Instruction::PtrToInt) |
| 330 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0))) |
Dan Gohman | 8db08df | 2010-02-02 01:38:49 +0000 | [diff] [blame] | 331 | if (CE->getOpcode() == Instruction::GetElementPtr && |
| 332 | CE->getOperand(0)->isNullValue()) { |
| 333 | const Type *Ty = |
| 334 | cast<PointerType>(CE->getOperand(0)->getType())->getElementType(); |
| 335 | if (const StructType *STy = dyn_cast<StructType>(Ty)) |
| 336 | if (!STy->isPacked() && |
| 337 | CE->getNumOperands() == 3 && |
| 338 | CE->getOperand(1)->isNullValue()) { |
| 339 | if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(2))) |
| 340 | if (CI->isOne() && |
| 341 | STy->getNumElements() == 2 && |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 342 | STy->getElementType(0)->isIntegerTy(1)) { |
Dan Gohman | 8db08df | 2010-02-02 01:38:49 +0000 | [diff] [blame] | 343 | AllocTy = STy->getElementType(1); |
| 344 | return true; |
| 345 | } |
| 346 | } |
| 347 | } |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 348 | |
| 349 | return false; |
| 350 | } |
| 351 | |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 352 | bool SCEVUnknown::isOffsetOf(const Type *&CTy, Constant *&FieldNo) const { |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 353 | if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue())) |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 354 | if (VCE->getOpcode() == Instruction::PtrToInt) |
| 355 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0))) |
| 356 | if (CE->getOpcode() == Instruction::GetElementPtr && |
| 357 | CE->getNumOperands() == 3 && |
| 358 | CE->getOperand(0)->isNullValue() && |
| 359 | CE->getOperand(1)->isNullValue()) { |
| 360 | const Type *Ty = |
| 361 | cast<PointerType>(CE->getOperand(0)->getType())->getElementType(); |
| 362 | // Ignore vector types here so that ScalarEvolutionExpander doesn't |
| 363 | // emit getelementptrs that index into vectors. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 364 | if (Ty->isStructTy() || Ty->isArrayTy()) { |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 365 | CTy = Ty; |
| 366 | FieldNo = CE->getOperand(2); |
| 367 | return true; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | return false; |
| 372 | } |
| 373 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 374 | void SCEVUnknown::print(raw_ostream &OS) const { |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 375 | const Type *AllocTy; |
| 376 | if (isSizeOf(AllocTy)) { |
| 377 | OS << "sizeof(" << *AllocTy << ")"; |
| 378 | return; |
| 379 | } |
| 380 | if (isAlignOf(AllocTy)) { |
| 381 | OS << "alignof(" << *AllocTy << ")"; |
| 382 | return; |
| 383 | } |
| 384 | |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 385 | const Type *CTy; |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 386 | Constant *FieldNo; |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 387 | if (isOffsetOf(CTy, FieldNo)) { |
| 388 | OS << "offsetof(" << *CTy << ", "; |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 389 | WriteAsOperand(OS, FieldNo, false); |
| 390 | OS << ")"; |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | // Otherwise just print it normally. |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 395 | WriteAsOperand(OS, getValue(), false); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 398 | //===----------------------------------------------------------------------===// |
| 399 | // SCEV Utilities |
| 400 | //===----------------------------------------------------------------------===// |
| 401 | |
| 402 | namespace { |
| 403 | /// SCEVComplexityCompare - Return true if the complexity of the LHS is less |
| 404 | /// than the complexity of the RHS. This comparator is used to canonicalize |
| 405 | /// expressions. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 406 | class SCEVComplexityCompare { |
Dan Gohman | 9f1fb42 | 2010-08-13 20:17:27 +0000 | [diff] [blame] | 407 | const LoopInfo *const LI; |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 408 | public: |
Dan Gohman | e72079a | 2010-07-23 21:18:55 +0000 | [diff] [blame] | 409 | explicit SCEVComplexityCompare(const LoopInfo *li) : LI(li) {} |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 410 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 411 | // 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] | 412 | bool operator()(const SCEV *LHS, const SCEV *RHS) const { |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 413 | return compare(LHS, RHS) < 0; |
| 414 | } |
| 415 | |
| 416 | // Return negative, zero, or positive, if LHS is less than, equal to, or |
| 417 | // greater than RHS, respectively. A three-way result allows recursive |
| 418 | // comparisons to be more efficient. |
| 419 | int compare(const SCEV *LHS, const SCEV *RHS) const { |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 420 | // Fast-path: SCEVs are uniqued so we can do a quick equality check. |
| 421 | if (LHS == RHS) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 422 | return 0; |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 423 | |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 424 | // Primarily, sort the SCEVs by their getSCEVType(). |
Dan Gohman | 304a7a6 | 2010-07-23 21:20:52 +0000 | [diff] [blame] | 425 | unsigned LType = LHS->getSCEVType(), RType = RHS->getSCEVType(); |
| 426 | if (LType != RType) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 427 | return (int)LType - (int)RType; |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 428 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 429 | // Aside from the getSCEVType() ordering, the particular ordering |
| 430 | // isn't very important except that it's beneficial to be consistent, |
| 431 | // 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] | 432 | switch (LType) { |
| 433 | case scUnknown: { |
| 434 | const SCEVUnknown *LU = cast<SCEVUnknown>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 435 | const SCEVUnknown *RU = cast<SCEVUnknown>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 436 | |
| 437 | // Sort SCEVUnknown values with some loose heuristics. TODO: This is |
| 438 | // not as complete as it could be. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 439 | const Value *LV = LU->getValue(), *RV = RU->getValue(); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 440 | |
| 441 | // Order pointer values after integer values. This helps SCEVExpander |
| 442 | // form GEPs. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 443 | bool LIsPointer = LV->getType()->isPointerTy(), |
| 444 | RIsPointer = RV->getType()->isPointerTy(); |
Dan Gohman | 304a7a6 | 2010-07-23 21:20:52 +0000 | [diff] [blame] | 445 | if (LIsPointer != RIsPointer) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 446 | return (int)LIsPointer - (int)RIsPointer; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 447 | |
| 448 | // Compare getValueID values. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 449 | unsigned LID = LV->getValueID(), |
| 450 | RID = RV->getValueID(); |
Dan Gohman | 304a7a6 | 2010-07-23 21:20:52 +0000 | [diff] [blame] | 451 | if (LID != RID) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 452 | return (int)LID - (int)RID; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 453 | |
| 454 | // Sort arguments by their position. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 455 | if (const Argument *LA = dyn_cast<Argument>(LV)) { |
| 456 | const Argument *RA = cast<Argument>(RV); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 457 | unsigned LArgNo = LA->getArgNo(), RArgNo = RA->getArgNo(); |
| 458 | return (int)LArgNo - (int)RArgNo; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 461 | // For instructions, compare their loop depth, and their operand |
| 462 | // count. This is pretty loose. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 463 | if (const Instruction *LInst = dyn_cast<Instruction>(LV)) { |
| 464 | const Instruction *RInst = cast<Instruction>(RV); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 465 | |
| 466 | // Compare loop depths. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 467 | const BasicBlock *LParent = LInst->getParent(), |
| 468 | *RParent = RInst->getParent(); |
| 469 | if (LParent != RParent) { |
| 470 | unsigned LDepth = LI->getLoopDepth(LParent), |
| 471 | RDepth = LI->getLoopDepth(RParent); |
| 472 | if (LDepth != RDepth) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 473 | return (int)LDepth - (int)RDepth; |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 474 | } |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 475 | |
| 476 | // Compare the number of operands. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 477 | unsigned LNumOps = LInst->getNumOperands(), |
| 478 | RNumOps = RInst->getNumOperands(); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 479 | return (int)LNumOps - (int)RNumOps; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 482 | return 0; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 485 | case scConstant: { |
| 486 | const SCEVConstant *LC = cast<SCEVConstant>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 487 | const SCEVConstant *RC = cast<SCEVConstant>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 488 | |
| 489 | // Compare constant values. |
Dan Gohman | e28d792 | 2010-08-16 16:25:35 +0000 | [diff] [blame] | 490 | const APInt &LA = LC->getValue()->getValue(); |
| 491 | const APInt &RA = RC->getValue()->getValue(); |
| 492 | unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.getBitWidth(); |
Dan Gohman | 304a7a6 | 2010-07-23 21:20:52 +0000 | [diff] [blame] | 493 | if (LBitWidth != RBitWidth) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 494 | return (int)LBitWidth - (int)RBitWidth; |
| 495 | return LA.ult(RA) ? -1 : 1; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 498 | case scAddRecExpr: { |
| 499 | const SCEVAddRecExpr *LA = cast<SCEVAddRecExpr>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 500 | const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 501 | |
| 502 | // Compare addrec loop depths. |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 503 | const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop(); |
| 504 | if (LLoop != RLoop) { |
| 505 | unsigned LDepth = LLoop->getLoopDepth(), |
| 506 | RDepth = RLoop->getLoopDepth(); |
| 507 | if (LDepth != RDepth) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 508 | return (int)LDepth - (int)RDepth; |
Dan Gohman | 0ad2c7a | 2010-08-13 21:24:58 +0000 | [diff] [blame] | 509 | } |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 510 | |
| 511 | // Addrec complexity grows with operand count. |
| 512 | unsigned LNumOps = LA->getNumOperands(), RNumOps = RA->getNumOperands(); |
| 513 | if (LNumOps != RNumOps) |
| 514 | return (int)LNumOps - (int)RNumOps; |
| 515 | |
| 516 | // Lexicographically compare. |
| 517 | for (unsigned i = 0; i != LNumOps; ++i) { |
| 518 | long X = compare(LA->getOperand(i), RA->getOperand(i)); |
| 519 | if (X != 0) |
| 520 | return X; |
| 521 | } |
| 522 | |
| 523 | return 0; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 526 | case scAddExpr: |
| 527 | case scMulExpr: |
| 528 | case scSMaxExpr: |
| 529 | case scUMaxExpr: { |
| 530 | const SCEVNAryExpr *LC = cast<SCEVNAryExpr>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 531 | const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 532 | |
| 533 | // Lexicographically compare n-ary expressions. |
Dan Gohman | 304a7a6 | 2010-07-23 21:20:52 +0000 | [diff] [blame] | 534 | unsigned LNumOps = LC->getNumOperands(), RNumOps = RC->getNumOperands(); |
| 535 | for (unsigned i = 0; i != LNumOps; ++i) { |
| 536 | if (i >= RNumOps) |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 537 | return 1; |
| 538 | long X = compare(LC->getOperand(i), RC->getOperand(i)); |
| 539 | if (X != 0) |
| 540 | return X; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 541 | } |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 542 | return (int)LNumOps - (int)RNumOps; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 545 | case scUDivExpr: { |
| 546 | const SCEVUDivExpr *LC = cast<SCEVUDivExpr>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 547 | const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 548 | |
| 549 | // Lexicographically compare udiv expressions. |
| 550 | long X = compare(LC->getLHS(), RC->getLHS()); |
| 551 | if (X != 0) |
| 552 | return X; |
| 553 | return compare(LC->getRHS(), RC->getRHS()); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 556 | case scTruncate: |
| 557 | case scZeroExtend: |
| 558 | case scSignExtend: { |
| 559 | const SCEVCastExpr *LC = cast<SCEVCastExpr>(LHS); |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 560 | const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 561 | |
| 562 | // Compare cast expressions by operand. |
| 563 | return compare(LC->getOperand(), RC->getOperand()); |
| 564 | } |
| 565 | |
| 566 | default: |
| 567 | break; |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | llvm_unreachable("Unknown SCEV kind!"); |
Dan Gohman | 67ef74e | 2010-08-27 15:26:01 +0000 | [diff] [blame] | 571 | return 0; |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 572 | } |
| 573 | }; |
| 574 | } |
| 575 | |
| 576 | /// GroupByComplexity - Given a list of SCEV objects, order them by their |
| 577 | /// complexity, and group objects of the same complexity together by value. |
| 578 | /// When this routine is finished, we know that any duplicates in the vector are |
| 579 | /// consecutive and that complexity is monotonically increasing. |
| 580 | /// |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 581 | /// 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] | 582 | /// results from this routine. In other words, we don't want the results of |
| 583 | /// this to depend on where the addresses of various SCEV objects happened to |
| 584 | /// land in memory. |
| 585 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 586 | static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops, |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 587 | LoopInfo *LI) { |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 588 | if (Ops.size() < 2) return; // Noop |
| 589 | if (Ops.size() == 2) { |
| 590 | // This is the common case, which also happens to be trivially simple. |
| 591 | // Special case it. |
Dan Gohman | c6a8e99 | 2010-08-29 15:07:13 +0000 | [diff] [blame] | 592 | const SCEV *&LHS = Ops[0], *&RHS = Ops[1]; |
| 593 | if (SCEVComplexityCompare(LI)(RHS, LHS)) |
| 594 | std::swap(LHS, RHS); |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 595 | return; |
| 596 | } |
| 597 | |
Dan Gohman | 3bf6376 | 2010-06-18 19:54:20 +0000 | [diff] [blame] | 598 | // Do the rough sort by complexity. |
| 599 | std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI)); |
| 600 | |
| 601 | // Now that we are sorted by complexity, group elements of the same |
| 602 | // complexity. Note that this is, at worst, N^2, but the vector is likely to |
| 603 | // be extremely short in practice. Note that we take this approach because we |
| 604 | // do not want to depend on the addresses of the objects we are grouping. |
| 605 | for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { |
| 606 | const SCEV *S = Ops[i]; |
| 607 | unsigned Complexity = S->getSCEVType(); |
| 608 | |
| 609 | // If there are any objects of the same complexity and same value as this |
| 610 | // one, group them. |
| 611 | for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { |
| 612 | if (Ops[j] == S) { // Found a duplicate. |
| 613 | // Move it to immediately after i'th element. |
| 614 | std::swap(Ops[i+1], Ops[j]); |
| 615 | ++i; // no need to rescan it. |
| 616 | if (i == e-2) return; // Done! |
| 617 | } |
| 618 | } |
| 619 | } |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 622 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 623 | |
| 624 | //===----------------------------------------------------------------------===// |
| 625 | // Simple SCEV method implementations |
| 626 | //===----------------------------------------------------------------------===// |
| 627 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 628 | /// BinomialCoefficient - Compute BC(It, K). The result has width W. |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 629 | /// Assume, K > 0. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 630 | static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K, |
Dan Gohman | c2b015e | 2009-07-21 00:38:55 +0000 | [diff] [blame] | 631 | ScalarEvolution &SE, |
| 632 | const Type* ResultTy) { |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 633 | // Handle the simplest case efficiently. |
| 634 | if (K == 1) |
| 635 | return SE.getTruncateOrZeroExtend(It, ResultTy); |
| 636 | |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 637 | // We are using the following formula for BC(It, K): |
| 638 | // |
| 639 | // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! |
| 640 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 641 | // Suppose, W is the bitwidth of the return value. We must be prepared for |
| 642 | // overflow. Hence, we must assure that the result of our computation is |
| 643 | // equal to the accurate one modulo 2^W. Unfortunately, division isn't |
| 644 | // safe in modular arithmetic. |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 645 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 646 | // 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] | 647 | // 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] | 648 | // K! (i.e. trailing zeros in the binary representation of K!), and ^ is |
| 649 | // exponentiation: |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 650 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 651 | // 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] | 652 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 653 | // This formula is trivially equivalent to the previous formula. However, |
| 654 | // this formula can be implemented much more efficiently. The trick is that |
| 655 | // K! / 2^T is odd, and exact division by an odd number *is* safe in modular |
| 656 | // arithmetic. To do exact division in modular arithmetic, all we have |
| 657 | // to do is multiply by the inverse. Therefore, this step can be done at |
| 658 | // width W. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 659 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 660 | // The next issue is how to safely do the division by 2^T. The way this |
| 661 | // is done is by doing the multiplication step at a width of at least W + T |
| 662 | // bits. This way, the bottom W+T bits of the product are accurate. Then, |
| 663 | // when we perform the division by 2^T (which is equivalent to a right shift |
| 664 | // by T), the bottom W bits are accurate. Extra bits are okay; they'll get |
| 665 | // truncated out after the division by 2^T. |
| 666 | // |
| 667 | // In comparison to just directly using the first formula, this technique |
| 668 | // is much more efficient; using the first formula requires W * K bits, |
| 669 | // but this formula less than W + K bits. Also, the first formula requires |
| 670 | // a division step, whereas this formula only requires multiplies and shifts. |
| 671 | // |
| 672 | // It doesn't matter whether the subtraction step is done in the calculation |
| 673 | // width or the input iteration count's width; if the subtraction overflows, |
| 674 | // the result must be zero anyway. We prefer here to do it in the width of |
| 675 | // the induction variable because it helps a lot for certain cases; CodeGen |
| 676 | // isn't smart enough to ignore the overflow, which leads to much less |
| 677 | // efficient code if the width of the subtraction is wider than the native |
| 678 | // register width. |
| 679 | // |
| 680 | // (It's possible to not widen at all by pulling out factors of 2 before |
| 681 | // the multiplication; for example, K=2 can be calculated as |
| 682 | // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires |
| 683 | // extra arithmetic, so it's not an obvious win, and it gets |
| 684 | // much more complicated for K > 3.) |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 685 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 686 | // Protection from insane SCEVs; this bound is conservative, |
| 687 | // but it probably doesn't matter. |
| 688 | if (K > 1000) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 689 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 690 | |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 691 | unsigned W = SE.getTypeSizeInBits(ResultTy); |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 692 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 693 | // Calculate K! / 2^T and T; we divide out the factors of two before |
| 694 | // multiplying for calculating K! / 2^T to avoid overflow. |
| 695 | // Other overflow doesn't matter because we only care about the bottom |
| 696 | // W bits of the result. |
| 697 | APInt OddFactorial(W, 1); |
| 698 | unsigned T = 1; |
| 699 | for (unsigned i = 3; i <= K; ++i) { |
| 700 | APInt Mult(W, i); |
| 701 | unsigned TwoFactors = Mult.countTrailingZeros(); |
| 702 | T += TwoFactors; |
| 703 | Mult = Mult.lshr(TwoFactors); |
| 704 | OddFactorial *= Mult; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 705 | } |
Nick Lewycky | 6f8abf9 | 2008-06-13 04:38:55 +0000 | [diff] [blame] | 706 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 707 | // We need at least W + T bits for the multiplication step |
Nick Lewycky | 237d873 | 2009-01-25 08:16:27 +0000 | [diff] [blame] | 708 | unsigned CalculationBits = W + T; |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 709 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 710 | // Calculate 2^T, at width T+W. |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 711 | APInt DivFactor = APInt(CalculationBits, 1).shl(T); |
| 712 | |
| 713 | // Calculate the multiplicative inverse of K! / 2^T; |
| 714 | // this multiplication factor will perform the exact division by |
| 715 | // K! / 2^T. |
| 716 | APInt Mod = APInt::getSignedMinValue(W+1); |
| 717 | APInt MultiplyFactor = OddFactorial.zext(W+1); |
| 718 | MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); |
| 719 | MultiplyFactor = MultiplyFactor.trunc(W); |
| 720 | |
| 721 | // Calculate the product, at width T+W |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 722 | const IntegerType *CalculationTy = IntegerType::get(SE.getContext(), |
| 723 | CalculationBits); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 724 | const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 725 | for (unsigned i = 1; i != K; ++i) { |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 726 | const SCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i)); |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 727 | Dividend = SE.getMulExpr(Dividend, |
| 728 | SE.getTruncateOrZeroExtend(S, CalculationTy)); |
| 729 | } |
| 730 | |
| 731 | // Divide by 2^T |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 732 | const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 733 | |
| 734 | // Truncate the result, and divide by K! / 2^T. |
| 735 | |
| 736 | return SE.getMulExpr(SE.getConstant(MultiplyFactor), |
| 737 | SE.getTruncateOrZeroExtend(DivResult, ResultTy)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 740 | /// evaluateAtIteration - Return the value of this chain of recurrences at |
| 741 | /// the specified iteration number. We can evaluate this recurrence by |
| 742 | /// multiplying each element in the chain by the binomial coefficient |
| 743 | /// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as: |
| 744 | /// |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 745 | /// 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] | 746 | /// |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 747 | /// where BC(It, k) stands for binomial coefficient. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 748 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 749 | const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It, |
Dan Gohman | c2b015e | 2009-07-21 00:38:55 +0000 | [diff] [blame] | 750 | ScalarEvolution &SE) const { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 751 | const SCEV *Result = getStart(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 752 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 753 | // The computation is correct in the face of overflow provided that the |
| 754 | // multiplication is performed _after_ the evaluation of the binomial |
| 755 | // coefficient. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 756 | const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType()); |
Nick Lewycky | cb8f1b5 | 2008-10-13 03:58:02 +0000 | [diff] [blame] | 757 | if (isa<SCEVCouldNotCompute>(Coeff)) |
| 758 | return Coeff; |
| 759 | |
| 760 | Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 761 | } |
| 762 | return Result; |
| 763 | } |
| 764 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 765 | //===----------------------------------------------------------------------===// |
| 766 | // SCEV Expression folder implementations |
| 767 | //===----------------------------------------------------------------------===// |
| 768 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 769 | const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, |
Dan Gohman | f5074ec | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 770 | const Type *Ty) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 771 | assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 772 | "This is not a truncating conversion!"); |
Dan Gohman | 10b9479 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 773 | assert(isSCEVable(Ty) && |
| 774 | "This is not a conversion to a SCEVable type!"); |
| 775 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 776 | |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 777 | FoldingSetNodeID ID; |
| 778 | ID.AddInteger(scTruncate); |
| 779 | ID.AddPointer(Op); |
| 780 | ID.AddPointer(Ty); |
| 781 | void *IP = 0; |
| 782 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 783 | |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 784 | // Fold if the operand is constant. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 785 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
Dan Gohman | b8be8b7 | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 786 | return getConstant( |
Dan Gohman | 1faa882 | 2010-06-24 16:33:38 +0000 | [diff] [blame] | 787 | cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), |
| 788 | getEffectiveSCEVType(Ty)))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 789 | |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 790 | // trunc(trunc(x)) --> trunc(x) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 791 | if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 792 | return getTruncateExpr(ST->getOperand(), Ty); |
| 793 | |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 794 | // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 795 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 796 | return getTruncateOrSignExtend(SS->getOperand(), Ty); |
| 797 | |
| 798 | // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 799 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 800 | return getTruncateOrZeroExtend(SZ->getOperand(), Ty); |
| 801 | |
Dan Gohman | 6864db6 | 2009-06-18 16:24:47 +0000 | [diff] [blame] | 802 | // 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] | 803 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 804 | SmallVector<const SCEV *, 4> Operands; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 805 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 806 | Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty)); |
| 807 | return getAddRecExpr(Operands, AddRec->getLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Dan Gohman | f53462d | 2010-07-15 20:02:11 +0000 | [diff] [blame] | 810 | // As a special case, fold trunc(undef) to undef. We don't want to |
| 811 | // know too much about SCEVUnknowns, but this special case is handy |
| 812 | // and harmless. |
| 813 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Op)) |
| 814 | if (isa<UndefValue>(U->getValue())) |
| 815 | return getSCEV(UndefValue::get(Ty)); |
| 816 | |
Dan Gohman | 420ab91 | 2010-06-25 18:47:08 +0000 | [diff] [blame] | 817 | // The cast wasn't folded; create an explicit cast node. We can reuse |
| 818 | // the existing insert position since if we get here, we won't have |
| 819 | // made any changes which would invalidate it. |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 820 | SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator), |
| 821 | Op, Ty); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 822 | UniqueSCEVs.InsertNode(S, IP); |
| 823 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 826 | const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op, |
Dan Gohman | f5074ec | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 827 | const Type *Ty) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 828 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | 8170a68 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 829 | "This is not an extending conversion!"); |
Dan Gohman | 10b9479 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 830 | assert(isSCEVable(Ty) && |
| 831 | "This is not a conversion to a SCEVable type!"); |
| 832 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | 8170a68 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 833 | |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 834 | // Fold if the operand is constant. |
Dan Gohman | eaf6cf2 | 2010-06-24 16:47:03 +0000 | [diff] [blame] | 835 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| 836 | return getConstant( |
| 837 | cast<ConstantInt>(ConstantExpr::getZExt(SC->getValue(), |
| 838 | getEffectiveSCEVType(Ty)))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 839 | |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 840 | // zext(zext(x)) --> zext(x) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 841 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 842 | return getZeroExtendExpr(SZ->getOperand(), Ty); |
| 843 | |
Dan Gohman | 69fbc7f | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 844 | // Before doing any expensive analysis, check to see if we've already |
| 845 | // computed a SCEV for this Op and Ty. |
| 846 | FoldingSetNodeID ID; |
| 847 | ID.AddInteger(scZeroExtend); |
| 848 | ID.AddPointer(Op); |
| 849 | ID.AddPointer(Ty); |
| 850 | void *IP = 0; |
| 851 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 852 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 853 | // 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] | 854 | // 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] | 855 | // operands (often constants). This allows analysis of something like |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 856 | // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 857 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 858 | if (AR->isAffine()) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 859 | const SCEV *Start = AR->getStart(); |
| 860 | const SCEV *Step = AR->getStepRecurrence(*this); |
| 861 | unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
| 862 | const Loop *L = AR->getLoop(); |
| 863 | |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 864 | // If we have special knowledge that this addrec won't overflow, |
| 865 | // we don't need to do any further analysis. |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 866 | if (AR->hasNoUnsignedWrap()) |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 867 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 868 | getZeroExtendExpr(Step, Ty), |
| 869 | L); |
| 870 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 871 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 872 | // Note that this serves two purposes: It filters out loops that are |
| 873 | // simply not analyzable, and it covers the case where this code is |
| 874 | // being called from within backedge-taken count analysis, such that |
| 875 | // attempting to ask for the backedge-taken count would likely result |
| 876 | // in infinite recursion. In the later case, the analysis code will |
| 877 | // cope with a conservative value, and it will take care to purge |
| 878 | // that value once it has finished. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 879 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 880 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | f0aa485 | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 881 | // Manually compute the final value for AR, checking for |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 882 | // overflow. |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 883 | |
| 884 | // Check whether the backedge-taken count can be losslessly casted to |
| 885 | // the addrec's type. The count is always unsigned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 886 | const SCEV *CastedMaxBECount = |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 887 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 888 | const SCEV *RecastedMaxBECount = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 889 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 890 | if (MaxBECount == RecastedMaxBECount) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 891 | const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 892 | // Check whether Start+Step*MaxBECount has no unsigned overflow. |
Dan Gohman | 8f767d9 | 2010-02-24 19:31:06 +0000 | [diff] [blame] | 893 | const SCEV *ZMul = getMulExpr(CastedMaxBECount, Step); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 894 | const SCEV *Add = getAddExpr(Start, ZMul); |
| 895 | const SCEV *OperandExtendedAdd = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 896 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 897 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 898 | getZeroExtendExpr(Step, WideTy))); |
| 899 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 900 | // Return the expression with the addrec on the outside. |
| 901 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 902 | getZeroExtendExpr(Step, Ty), |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 903 | L); |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 904 | |
| 905 | // Similar to above, only this time treat the step value as signed. |
| 906 | // This covers loops that count down. |
Dan Gohman | 8f767d9 | 2010-02-24 19:31:06 +0000 | [diff] [blame] | 907 | const SCEV *SMul = getMulExpr(CastedMaxBECount, Step); |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 908 | Add = getAddExpr(Start, SMul); |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 909 | OperandExtendedAdd = |
| 910 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 911 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 912 | getSignExtendExpr(Step, WideTy))); |
| 913 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 914 | // Return the expression with the addrec on the outside. |
| 915 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 916 | getSignExtendExpr(Step, Ty), |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 917 | L); |
| 918 | } |
| 919 | |
| 920 | // If the backedge is guarded by a comparison with the pre-inc value |
| 921 | // the addrec is safe. Also, if the entry is guarded by a comparison |
| 922 | // with the start value and the backedge is guarded by a comparison |
| 923 | // with the post-inc value, the addrec is safe. |
| 924 | if (isKnownPositive(Step)) { |
| 925 | const SCEV *N = getConstant(APInt::getMinValue(BitWidth) - |
| 926 | getUnsignedRange(Step).getUnsignedMax()); |
| 927 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) || |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 928 | (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) && |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 929 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, |
| 930 | AR->getPostIncExpr(*this), N))) |
| 931 | // Return the expression with the addrec on the outside. |
| 932 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 933 | getZeroExtendExpr(Step, Ty), |
| 934 | L); |
| 935 | } else if (isKnownNegative(Step)) { |
| 936 | const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) - |
| 937 | getSignedRange(Step).getSignedMin()); |
Dan Gohman | c0ed009 | 2010-05-04 01:11:15 +0000 | [diff] [blame] | 938 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) || |
| 939 | (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) && |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 940 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, |
| 941 | AR->getPostIncExpr(*this), N))) |
| 942 | // Return the expression with the addrec on the outside. |
| 943 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 944 | getSignExtendExpr(Step, Ty), |
| 945 | L); |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 949 | |
Dan Gohman | 69fbc7f | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 950 | // The cast wasn't folded; create an explicit cast node. |
| 951 | // Recompute the insert position, as it may have been invalidated. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 952 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 953 | SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator), |
| 954 | Op, Ty); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 955 | UniqueSCEVs.InsertNode(S, IP); |
| 956 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 959 | const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op, |
Dan Gohman | f5074ec | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 960 | const Type *Ty) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 961 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 962 | "This is not an extending conversion!"); |
Dan Gohman | 10b9479 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 963 | assert(isSCEVable(Ty) && |
| 964 | "This is not a conversion to a SCEVable type!"); |
| 965 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 966 | |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 967 | // Fold if the operand is constant. |
Dan Gohman | eaf6cf2 | 2010-06-24 16:47:03 +0000 | [diff] [blame] | 968 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| 969 | return getConstant( |
| 970 | cast<ConstantInt>(ConstantExpr::getSExt(SC->getValue(), |
| 971 | getEffectiveSCEVType(Ty)))); |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 972 | |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 973 | // sext(sext(x)) --> sext(x) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 974 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 975 | return getSignExtendExpr(SS->getOperand(), Ty); |
| 976 | |
Dan Gohman | 69fbc7f | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 977 | // Before doing any expensive analysis, check to see if we've already |
| 978 | // computed a SCEV for this Op and Ty. |
| 979 | FoldingSetNodeID ID; |
| 980 | ID.AddInteger(scSignExtend); |
| 981 | ID.AddPointer(Op); |
| 982 | ID.AddPointer(Ty); |
| 983 | void *IP = 0; |
| 984 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 985 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 986 | // 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] | 987 | // 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] | 988 | // operands (often constants). This allows analysis of something like |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 989 | // this: for (signed char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 990 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 991 | if (AR->isAffine()) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 992 | const SCEV *Start = AR->getStart(); |
| 993 | const SCEV *Step = AR->getStepRecurrence(*this); |
| 994 | unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
| 995 | const Loop *L = AR->getLoop(); |
| 996 | |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 997 | // If we have special knowledge that this addrec won't overflow, |
| 998 | // we don't need to do any further analysis. |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 999 | if (AR->hasNoSignedWrap()) |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 1000 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 1001 | getSignExtendExpr(Step, Ty), |
| 1002 | L); |
| 1003 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1004 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 1005 | // Note that this serves two purposes: It filters out loops that are |
| 1006 | // simply not analyzable, and it covers the case where this code is |
| 1007 | // being called from within backedge-taken count analysis, such that |
| 1008 | // attempting to ask for the backedge-taken count would likely result |
| 1009 | // in infinite recursion. In the later case, the analysis code will |
| 1010 | // cope with a conservative value, and it will take care to purge |
| 1011 | // that value once it has finished. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1012 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 1013 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | f0aa485 | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 1014 | // Manually compute the final value for AR, checking for |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 1015 | // overflow. |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1016 | |
| 1017 | // Check whether the backedge-taken count can be losslessly casted to |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 1018 | // the addrec's type. The count is always unsigned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1019 | const SCEV *CastedMaxBECount = |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 1020 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1021 | const SCEV *RecastedMaxBECount = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 1022 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 1023 | if (MaxBECount == RecastedMaxBECount) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1024 | const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 1025 | // Check whether Start+Step*MaxBECount has no signed overflow. |
Dan Gohman | 8f767d9 | 2010-02-24 19:31:06 +0000 | [diff] [blame] | 1026 | const SCEV *SMul = getMulExpr(CastedMaxBECount, Step); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1027 | const SCEV *Add = getAddExpr(Start, SMul); |
| 1028 | const SCEV *OperandExtendedAdd = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 1029 | getAddExpr(getSignExtendExpr(Start, WideTy), |
| 1030 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 1031 | getSignExtendExpr(Step, WideTy))); |
| 1032 | if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 1033 | // Return the expression with the addrec on the outside. |
| 1034 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 1035 | getSignExtendExpr(Step, Ty), |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1036 | L); |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 1037 | |
| 1038 | // Similar to above, only this time treat the step value as unsigned. |
| 1039 | // This covers loops that count up with an unsigned step. |
Dan Gohman | 8f767d9 | 2010-02-24 19:31:06 +0000 | [diff] [blame] | 1040 | const SCEV *UMul = getMulExpr(CastedMaxBECount, Step); |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 1041 | Add = getAddExpr(Start, UMul); |
| 1042 | OperandExtendedAdd = |
Dan Gohman | 19378d6 | 2009-07-25 16:03:30 +0000 | [diff] [blame] | 1043 | getAddExpr(getSignExtendExpr(Start, WideTy), |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 1044 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 1045 | getZeroExtendExpr(Step, WideTy))); |
Dan Gohman | 19378d6 | 2009-07-25 16:03:30 +0000 | [diff] [blame] | 1046 | if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 1047 | // Return the expression with the addrec on the outside. |
| 1048 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 1049 | getZeroExtendExpr(Step, Ty), |
| 1050 | L); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | // If the backedge is guarded by a comparison with the pre-inc value |
| 1054 | // the addrec is safe. Also, if the entry is guarded by a comparison |
| 1055 | // with the start value and the backedge is guarded by a comparison |
| 1056 | // with the post-inc value, the addrec is safe. |
| 1057 | if (isKnownPositive(Step)) { |
| 1058 | const SCEV *N = getConstant(APInt::getSignedMinValue(BitWidth) - |
| 1059 | getSignedRange(Step).getSignedMax()); |
| 1060 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, AR, N) || |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 1061 | (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SLT, Start, N) && |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1062 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, |
| 1063 | AR->getPostIncExpr(*this), N))) |
| 1064 | // Return the expression with the addrec on the outside. |
| 1065 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 1066 | getSignExtendExpr(Step, Ty), |
| 1067 | L); |
| 1068 | } else if (isKnownNegative(Step)) { |
| 1069 | const SCEV *N = getConstant(APInt::getSignedMaxValue(BitWidth) - |
| 1070 | getSignedRange(Step).getSignedMin()); |
| 1071 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, AR, N) || |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 1072 | (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_SGT, Start, N) && |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1073 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, |
| 1074 | AR->getPostIncExpr(*this), N))) |
| 1075 | // Return the expression with the addrec on the outside. |
| 1076 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 1077 | getSignExtendExpr(Step, Ty), |
| 1078 | L); |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | } |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 1082 | |
Dan Gohman | 69fbc7f | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 1083 | // The cast wasn't folded; create an explicit cast node. |
| 1084 | // Recompute the insert position, as it may have been invalidated. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1085 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1086 | SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator), |
| 1087 | Op, Ty); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1088 | UniqueSCEVs.InsertNode(S, IP); |
| 1089 | return S; |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1092 | /// getAnyExtendExpr - Return a SCEV for the given operand extended with |
| 1093 | /// unspecified bits out to the given type. |
| 1094 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1095 | const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op, |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 1096 | const Type *Ty) { |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1097 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
| 1098 | "This is not an extending conversion!"); |
| 1099 | assert(isSCEVable(Ty) && |
| 1100 | "This is not a conversion to a SCEVable type!"); |
| 1101 | Ty = getEffectiveSCEVType(Ty); |
| 1102 | |
| 1103 | // Sign-extend negative constants. |
| 1104 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| 1105 | if (SC->getValue()->getValue().isNegative()) |
| 1106 | return getSignExtendExpr(Op, Ty); |
| 1107 | |
| 1108 | // Peel off a truncate cast. |
| 1109 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1110 | const SCEV *NewOp = T->getOperand(); |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1111 | if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty)) |
| 1112 | return getAnyExtendExpr(NewOp, Ty); |
| 1113 | return getTruncateOrNoop(NewOp, Ty); |
| 1114 | } |
| 1115 | |
| 1116 | // Next try a zext cast. If the cast is folded, use it. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1117 | const SCEV *ZExt = getZeroExtendExpr(Op, Ty); |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1118 | if (!isa<SCEVZeroExtendExpr>(ZExt)) |
| 1119 | return ZExt; |
| 1120 | |
| 1121 | // Next try a sext cast. If the cast is folded, use it. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1122 | const SCEV *SExt = getSignExtendExpr(Op, Ty); |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1123 | if (!isa<SCEVSignExtendExpr>(SExt)) |
| 1124 | return SExt; |
| 1125 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1126 | // Force the cast to be folded into the operands of an addrec. |
| 1127 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) { |
| 1128 | SmallVector<const SCEV *, 4> Ops; |
| 1129 | for (SCEVAddRecExpr::op_iterator I = AR->op_begin(), E = AR->op_end(); |
| 1130 | I != E; ++I) |
| 1131 | Ops.push_back(getAnyExtendExpr(*I, Ty)); |
| 1132 | return getAddRecExpr(Ops, AR->getLoop()); |
| 1133 | } |
| 1134 | |
Dan Gohman | f53462d | 2010-07-15 20:02:11 +0000 | [diff] [blame] | 1135 | // As a special case, fold anyext(undef) to undef. We don't want to |
| 1136 | // know too much about SCEVUnknowns, but this special case is handy |
| 1137 | // and harmless. |
| 1138 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Op)) |
| 1139 | if (isa<UndefValue>(U->getValue())) |
| 1140 | return getSCEV(UndefValue::get(Ty)); |
| 1141 | |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1142 | // If the expression is obviously signed, use the sext cast value. |
| 1143 | if (isa<SCEVSMaxExpr>(Op)) |
| 1144 | return SExt; |
| 1145 | |
| 1146 | // Absent any other information, use the zext cast value. |
| 1147 | return ZExt; |
| 1148 | } |
| 1149 | |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1150 | /// CollectAddOperandsWithScales - Process the given Ops list, which is |
| 1151 | /// a list of operands to be added under the given scale, update the given |
| 1152 | /// map. This is a helper function for getAddRecExpr. As an example of |
| 1153 | /// what it does, given a sequence of operands that would form an add |
| 1154 | /// expression like this: |
| 1155 | /// |
| 1156 | /// m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r) |
| 1157 | /// |
| 1158 | /// where A and B are constants, update the map with these values: |
| 1159 | /// |
| 1160 | /// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0) |
| 1161 | /// |
| 1162 | /// and add 13 + A*B*29 to AccumulatedConstant. |
| 1163 | /// This will allow getAddRecExpr to produce this: |
| 1164 | /// |
| 1165 | /// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B) |
| 1166 | /// |
| 1167 | /// This form often exposes folding opportunities that are hidden in |
| 1168 | /// the original operand list. |
| 1169 | /// |
| 1170 | /// Return true iff it appears that any interesting folding opportunities |
| 1171 | /// may be exposed. This helps getAddRecExpr short-circuit extra work in |
| 1172 | /// the common case where no interesting opportunities are present, and |
| 1173 | /// is also used as a check to avoid infinite recursion. |
| 1174 | /// |
| 1175 | static bool |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1176 | CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M, |
| 1177 | SmallVector<const SCEV *, 8> &NewOps, |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1178 | APInt &AccumulatedConstant, |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1179 | const SCEV *const *Ops, size_t NumOperands, |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1180 | const APInt &Scale, |
| 1181 | ScalarEvolution &SE) { |
| 1182 | bool Interesting = false; |
| 1183 | |
Dan Gohman | e0f0c7b | 2010-06-18 19:12:32 +0000 | [diff] [blame] | 1184 | // Iterate over the add operands. They are sorted, with constants first. |
| 1185 | unsigned i = 0; |
| 1186 | while (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| 1187 | ++i; |
| 1188 | // Pull a buried constant out to the outside. |
| 1189 | if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero()) |
| 1190 | Interesting = true; |
| 1191 | AccumulatedConstant += Scale * C->getValue()->getValue(); |
| 1192 | } |
| 1193 | |
| 1194 | // Next comes everything else. We're especially interested in multiplies |
| 1195 | // here, but they're in the middle, so just visit the rest with one loop. |
| 1196 | for (; i != NumOperands; ++i) { |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1197 | const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]); |
| 1198 | if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) { |
| 1199 | APInt NewScale = |
| 1200 | Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue(); |
| 1201 | if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) { |
| 1202 | // A multiplication of a constant with another add; recurse. |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1203 | const SCEVAddExpr *Add = cast<SCEVAddExpr>(Mul->getOperand(1)); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1204 | Interesting |= |
| 1205 | CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1206 | Add->op_begin(), Add->getNumOperands(), |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1207 | NewScale, SE); |
| 1208 | } else { |
| 1209 | // A multiplication of a constant with some other value. Update |
| 1210 | // the map. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1211 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end()); |
| 1212 | const SCEV *Key = SE.getMulExpr(MulOps); |
| 1213 | std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = |
Dan Gohman | 23737e0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1214 | M.insert(std::make_pair(Key, NewScale)); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1215 | if (Pair.second) { |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1216 | NewOps.push_back(Pair.first->first); |
| 1217 | } else { |
| 1218 | Pair.first->second += NewScale; |
| 1219 | // The map already had an entry for this value, which may indicate |
| 1220 | // a folding opportunity. |
| 1221 | Interesting = true; |
| 1222 | } |
| 1223 | } |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1224 | } else { |
| 1225 | // An ordinary operand. Update the map. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1226 | std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = |
Dan Gohman | 23737e0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1227 | M.insert(std::make_pair(Ops[i], Scale)); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1228 | if (Pair.second) { |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1229 | NewOps.push_back(Pair.first->first); |
| 1230 | } else { |
| 1231 | Pair.first->second += Scale; |
| 1232 | // The map already had an entry for this value, which may indicate |
| 1233 | // a folding opportunity. |
| 1234 | Interesting = true; |
| 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | return Interesting; |
| 1240 | } |
| 1241 | |
| 1242 | namespace { |
| 1243 | struct APIntCompare { |
| 1244 | bool operator()(const APInt &LHS, const APInt &RHS) const { |
| 1245 | return LHS.ult(RHS); |
| 1246 | } |
| 1247 | }; |
| 1248 | } |
| 1249 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1250 | /// getAddExpr - Get a canonical add expression, or something simpler if |
| 1251 | /// possible. |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1252 | const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops, |
| 1253 | bool HasNUW, bool HasNSW) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1254 | assert(!Ops.empty() && "Cannot get empty add!"); |
Chris Lattner | 627018b | 2004-04-07 16:16:11 +0000 | [diff] [blame] | 1255 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1256 | #ifndef NDEBUG |
Dan Gohman | c72f0c8 | 2010-06-18 19:09:27 +0000 | [diff] [blame] | 1257 | const Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1258 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
Dan Gohman | c72f0c8 | 2010-06-18 19:09:27 +0000 | [diff] [blame] | 1259 | assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1260 | "SCEVAddExpr operand types don't match!"); |
| 1261 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1262 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1263 | // If HasNSW is true and all the operands are non-negative, infer HasNUW. |
| 1264 | if (!HasNUW && HasNSW) { |
| 1265 | bool All = true; |
Dan Gohman | 2d16fc5 | 2010-08-16 16:27:53 +0000 | [diff] [blame] | 1266 | for (SmallVectorImpl<const SCEV *>::const_iterator I = Ops.begin(), |
| 1267 | E = Ops.end(); I != E; ++I) |
| 1268 | if (!isKnownNonNegative(*I)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1269 | All = false; |
| 1270 | break; |
| 1271 | } |
| 1272 | if (All) HasNUW = true; |
| 1273 | } |
| 1274 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1275 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1276 | GroupByComplexity(Ops, LI); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1277 | |
| 1278 | // If there are any constants, fold them together. |
| 1279 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1280 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1281 | ++Idx; |
Chris Lattner | 627018b | 2004-04-07 16:16:11 +0000 | [diff] [blame] | 1282 | assert(Idx < Ops.size()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1283 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1284 | // We found two constants, fold them together! |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1285 | Ops[0] = getConstant(LHSC->getValue()->getValue() + |
| 1286 | RHSC->getValue()->getValue()); |
Dan Gohman | 7f7c436 | 2009-06-14 22:53:57 +0000 | [diff] [blame] | 1287 | if (Ops.size() == 2) return Ops[0]; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1288 | Ops.erase(Ops.begin()+1); // Erase the folded element |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1289 | LHSC = cast<SCEVConstant>(Ops[0]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | // 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] | 1293 | if (LHSC->getValue()->isZero()) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1294 | Ops.erase(Ops.begin()); |
| 1295 | --Idx; |
| 1296 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1297 | |
Dan Gohman | bca091d | 2010-04-12 23:08:18 +0000 | [diff] [blame] | 1298 | if (Ops.size() == 1) return Ops[0]; |
| 1299 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1300 | |
Dan Gohman | 68ff776 | 2010-08-27 21:39:59 +0000 | [diff] [blame] | 1301 | // Okay, check to see if the same value occurs in the operand list more than |
| 1302 | // once. If so, merge them together into an multiply expression. Since we |
| 1303 | // sorted the list, these values are required to be adjacent. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1304 | const Type *Ty = Ops[0]->getType(); |
Dan Gohman | dc7692b | 2010-08-12 14:46:54 +0000 | [diff] [blame] | 1305 | bool FoundMatch = false; |
Dan Gohman | 68ff776 | 2010-08-27 21:39:59 +0000 | [diff] [blame] | 1306 | for (unsigned i = 0, e = Ops.size(); i != e-1; ++i) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1307 | if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2 |
Dan Gohman | 68ff776 | 2010-08-27 21:39:59 +0000 | [diff] [blame] | 1308 | // Scan ahead to count how many equal operands there are. |
| 1309 | unsigned Count = 2; |
| 1310 | while (i+Count != e && Ops[i+Count] == Ops[i]) |
| 1311 | ++Count; |
| 1312 | // Merge the values into a multiply. |
| 1313 | const SCEV *Scale = getConstant(Ty, Count); |
| 1314 | const SCEV *Mul = getMulExpr(Scale, Ops[i]); |
| 1315 | if (Ops.size() == Count) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1316 | return Mul; |
Dan Gohman | dc7692b | 2010-08-12 14:46:54 +0000 | [diff] [blame] | 1317 | Ops[i] = Mul; |
Dan Gohman | 68ff776 | 2010-08-27 21:39:59 +0000 | [diff] [blame] | 1318 | Ops.erase(Ops.begin()+i+1, Ops.begin()+i+Count); |
Dan Gohman | 5bb307d | 2010-08-28 00:39:27 +0000 | [diff] [blame] | 1319 | --i; e -= Count - 1; |
Dan Gohman | dc7692b | 2010-08-12 14:46:54 +0000 | [diff] [blame] | 1320 | FoundMatch = true; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1321 | } |
Dan Gohman | dc7692b | 2010-08-12 14:46:54 +0000 | [diff] [blame] | 1322 | if (FoundMatch) |
| 1323 | return getAddExpr(Ops, HasNUW, HasNSW); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1324 | |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1325 | // Check for truncates. If all the operands are truncated from the same |
| 1326 | // type, see if factoring out the truncate would permit the result to be |
| 1327 | // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n) |
| 1328 | // if the contents of the resulting outer trunc fold to something simple. |
| 1329 | for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) { |
| 1330 | const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]); |
| 1331 | const Type *DstType = Trunc->getType(); |
| 1332 | const Type *SrcType = Trunc->getOperand()->getType(); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1333 | SmallVector<const SCEV *, 8> LargeOps; |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1334 | bool Ok = true; |
| 1335 | // Check all the operands to see if they can be represented in the |
| 1336 | // source type of the truncate. |
| 1337 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 1338 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) { |
| 1339 | if (T->getOperand()->getType() != SrcType) { |
| 1340 | Ok = false; |
| 1341 | break; |
| 1342 | } |
| 1343 | LargeOps.push_back(T->getOperand()); |
| 1344 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
Dan Gohman | c686398 | 2010-04-23 01:51:29 +0000 | [diff] [blame] | 1345 | LargeOps.push_back(getAnyExtendExpr(C, SrcType)); |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1346 | } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1347 | SmallVector<const SCEV *, 8> LargeMulOps; |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1348 | for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { |
| 1349 | if (const SCEVTruncateExpr *T = |
| 1350 | dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) { |
| 1351 | if (T->getOperand()->getType() != SrcType) { |
| 1352 | Ok = false; |
| 1353 | break; |
| 1354 | } |
| 1355 | LargeMulOps.push_back(T->getOperand()); |
| 1356 | } else if (const SCEVConstant *C = |
| 1357 | dyn_cast<SCEVConstant>(M->getOperand(j))) { |
Dan Gohman | c686398 | 2010-04-23 01:51:29 +0000 | [diff] [blame] | 1358 | LargeMulOps.push_back(getAnyExtendExpr(C, SrcType)); |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1359 | } else { |
| 1360 | Ok = false; |
| 1361 | break; |
| 1362 | } |
| 1363 | } |
| 1364 | if (Ok) |
| 1365 | LargeOps.push_back(getMulExpr(LargeMulOps)); |
| 1366 | } else { |
| 1367 | Ok = false; |
| 1368 | break; |
| 1369 | } |
| 1370 | } |
| 1371 | if (Ok) { |
| 1372 | // Evaluate the expression in the larger type. |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1373 | const SCEV *Fold = getAddExpr(LargeOps, HasNUW, HasNSW); |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1374 | // If it folds to something simple, use it. Otherwise, don't. |
| 1375 | if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold)) |
| 1376 | return getTruncateExpr(Fold, DstType); |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | // Skip past any other cast SCEVs. |
Dan Gohman | f50cd74 | 2007-06-18 19:30:09 +0000 | [diff] [blame] | 1381 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) |
| 1382 | ++Idx; |
| 1383 | |
| 1384 | // If there are add operands they would be next. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1385 | if (Idx < Ops.size()) { |
| 1386 | bool DeletedAdd = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1387 | while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1388 | // If we have an add, expand the add operands onto the end of the operands |
| 1389 | // list. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1390 | Ops.erase(Ops.begin()+Idx); |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 1391 | Ops.append(Add->op_begin(), Add->op_end()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1392 | DeletedAdd = true; |
| 1393 | } |
| 1394 | |
| 1395 | // If we deleted at least one add, we added operands to the end of the list, |
| 1396 | // and they are not necessarily sorted. Recurse to resort and resimplify |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 1397 | // any operands we just acquired. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1398 | if (DeletedAdd) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1399 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | // Skip over the add expression until we get to a multiply. |
| 1403 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1404 | ++Idx; |
| 1405 | |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1406 | // Check to see if there are any folding opportunities present with |
| 1407 | // operands multiplied by constant values. |
| 1408 | if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) { |
| 1409 | uint64_t BitWidth = getTypeSizeInBits(Ty); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1410 | DenseMap<const SCEV *, APInt> M; |
| 1411 | SmallVector<const SCEV *, 8> NewOps; |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1412 | APInt AccumulatedConstant(BitWidth, 0); |
| 1413 | if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1414 | Ops.data(), Ops.size(), |
| 1415 | APInt(BitWidth, 1), *this)) { |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1416 | // Some interesting folding opportunity is present, so its worthwhile to |
| 1417 | // re-generate the operands list. Group the operands by constant scale, |
| 1418 | // to avoid multiplying by the same constant scale multiple times. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1419 | std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists; |
Dan Gohman | 8d9c7a6 | 2010-08-16 16:30:01 +0000 | [diff] [blame] | 1420 | for (SmallVector<const SCEV *, 8>::const_iterator I = NewOps.begin(), |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1421 | E = NewOps.end(); I != E; ++I) |
| 1422 | MulOpLists[M.find(*I)->second].push_back(*I); |
| 1423 | // Re-generate the operands list. |
| 1424 | Ops.clear(); |
| 1425 | if (AccumulatedConstant != 0) |
| 1426 | Ops.push_back(getConstant(AccumulatedConstant)); |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1427 | for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator |
| 1428 | I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I) |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1429 | if (I->first != 0) |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1430 | Ops.push_back(getMulExpr(getConstant(I->first), |
| 1431 | getAddExpr(I->second))); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1432 | if (Ops.empty()) |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1433 | return getConstant(Ty, 0); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1434 | if (Ops.size() == 1) |
| 1435 | return Ops[0]; |
| 1436 | return getAddExpr(Ops); |
| 1437 | } |
| 1438 | } |
| 1439 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1440 | // If we are adding something to a multiply expression, make sure the |
| 1441 | // something is not already an operand of the multiply. If so, merge it into |
| 1442 | // the multiply. |
| 1443 | for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1444 | const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1445 | for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1446 | const SCEV *MulOpSCEV = Mul->getOperand(MulOp); |
Dan Gohman | 918e76b | 2010-08-12 14:52:55 +0000 | [diff] [blame] | 1447 | if (isa<SCEVConstant>(MulOpSCEV)) |
| 1448 | continue; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1449 | for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) |
Dan Gohman | 918e76b | 2010-08-12 14:52:55 +0000 | [diff] [blame] | 1450 | if (MulOpSCEV == Ops[AddOp]) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1451 | // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1452 | const SCEV *InnerMul = Mul->getOperand(MulOp == 0); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1453 | if (Mul->getNumOperands() != 2) { |
| 1454 | // If the multiply has more than two operands, we must get the |
| 1455 | // Y*Z term. |
Dan Gohman | 1895991 | 2010-08-16 16:57:24 +0000 | [diff] [blame] | 1456 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), |
| 1457 | Mul->op_begin()+MulOp); |
| 1458 | MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1459 | InnerMul = getMulExpr(MulOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1460 | } |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 1461 | const SCEV *One = getConstant(Ty, 1); |
Dan Gohman | 58a85b9 | 2010-08-13 20:17:14 +0000 | [diff] [blame] | 1462 | const SCEV *AddOne = getAddExpr(One, InnerMul); |
Dan Gohman | 918e76b | 2010-08-12 14:52:55 +0000 | [diff] [blame] | 1463 | const SCEV *OuterMul = getMulExpr(AddOne, MulOpSCEV); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1464 | if (Ops.size() == 2) return OuterMul; |
| 1465 | if (AddOp < Idx) { |
| 1466 | Ops.erase(Ops.begin()+AddOp); |
| 1467 | Ops.erase(Ops.begin()+Idx-1); |
| 1468 | } else { |
| 1469 | Ops.erase(Ops.begin()+Idx); |
| 1470 | Ops.erase(Ops.begin()+AddOp-1); |
| 1471 | } |
| 1472 | Ops.push_back(OuterMul); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1473 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1474 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1475 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1476 | // Check this multiply against other multiplies being added together. |
| 1477 | for (unsigned OtherMulIdx = Idx+1; |
| 1478 | OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]); |
| 1479 | ++OtherMulIdx) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1480 | const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1481 | // If MulOp occurs in OtherMul, we can fold the two multiplies |
| 1482 | // together. |
| 1483 | for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); |
| 1484 | OMulOp != e; ++OMulOp) |
| 1485 | if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { |
| 1486 | // 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] | 1487 | const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1488 | if (Mul->getNumOperands() != 2) { |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1489 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), |
Dan Gohman | 1895991 | 2010-08-16 16:57:24 +0000 | [diff] [blame] | 1490 | Mul->op_begin()+MulOp); |
| 1491 | MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1492 | InnerMul1 = getMulExpr(MulOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1493 | } |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1494 | const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1495 | if (OtherMul->getNumOperands() != 2) { |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1496 | SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(), |
Dan Gohman | 1895991 | 2010-08-16 16:57:24 +0000 | [diff] [blame] | 1497 | OtherMul->op_begin()+OMulOp); |
| 1498 | MulOps.append(OtherMul->op_begin()+OMulOp+1, OtherMul->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1499 | InnerMul2 = getMulExpr(MulOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1500 | } |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1501 | const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2); |
| 1502 | const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1503 | if (Ops.size() == 2) return OuterMul; |
Dan Gohman | 90b5f25 | 2010-08-31 22:50:31 +0000 | [diff] [blame] | 1504 | Ops.erase(Ops.begin()+Idx); |
| 1505 | Ops.erase(Ops.begin()+OtherMulIdx-1); |
| 1506 | Ops.push_back(OuterMul); |
| 1507 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1508 | } |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | // If there are any add recurrences in the operands list, see if any other |
| 1514 | // added values are loop invariant. If so, we can fold them into the |
| 1515 | // recurrence. |
| 1516 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1517 | ++Idx; |
| 1518 | |
| 1519 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1520 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1521 | // Scan all of the other operands to this add and add them to the vector if |
| 1522 | // they are loop invariant w.r.t. the recurrence. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1523 | SmallVector<const SCEV *, 8> LIOps; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1524 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Dan Gohman | bca091d | 2010-04-12 23:08:18 +0000 | [diff] [blame] | 1525 | const Loop *AddRecLoop = AddRec->getLoop(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1526 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1527 | if (isLoopInvariant(Ops[i], AddRecLoop)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1528 | LIOps.push_back(Ops[i]); |
| 1529 | Ops.erase(Ops.begin()+i); |
| 1530 | --i; --e; |
| 1531 | } |
| 1532 | |
| 1533 | // If we found some loop invariants, fold them into the recurrence. |
| 1534 | if (!LIOps.empty()) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1535 | // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step} |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1536 | LIOps.push_back(AddRec->getStart()); |
| 1537 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1538 | SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(), |
Dan Gohman | 3a5d409 | 2009-12-18 03:57:04 +0000 | [diff] [blame] | 1539 | AddRec->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1540 | AddRecOps[0] = getAddExpr(LIOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1541 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 1542 | // Build the new addrec. Propagate the NUW and NSW flags if both the |
| 1543 | // outer add and the inner addrec are guaranteed to have no overflow. |
| 1544 | const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRecLoop, |
| 1545 | HasNUW && AddRec->hasNoUnsignedWrap(), |
| 1546 | HasNSW && AddRec->hasNoSignedWrap()); |
Dan Gohman | 59de33e | 2009-12-18 18:45:31 +0000 | [diff] [blame] | 1547 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1548 | // If all of the other operands were loop invariant, we are done. |
| 1549 | if (Ops.size() == 1) return NewRec; |
| 1550 | |
| 1551 | // Otherwise, add the folded AddRec by the non-liv parts. |
| 1552 | for (unsigned i = 0;; ++i) |
| 1553 | if (Ops[i] == AddRec) { |
| 1554 | Ops[i] = NewRec; |
| 1555 | break; |
| 1556 | } |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1557 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1561 | // there are multiple AddRec's with the same loop induction variable being |
| 1562 | // added together. If so, we can fold them. |
| 1563 | for (unsigned OtherIdx = Idx+1; |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1564 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); |
| 1565 | ++OtherIdx) |
| 1566 | if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) { |
| 1567 | // Other + {A,+,B}<L> + {C,+,D}<L> --> Other + {A+C,+,B+D}<L> |
| 1568 | SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(), |
| 1569 | AddRec->op_end()); |
| 1570 | for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); |
| 1571 | ++OtherIdx) |
Dan Gohman | 30cbc86 | 2010-08-29 14:53:34 +0000 | [diff] [blame] | 1572 | if (const SCEVAddRecExpr *OtherAddRec = |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1573 | dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx])) |
Dan Gohman | 30cbc86 | 2010-08-29 14:53:34 +0000 | [diff] [blame] | 1574 | if (OtherAddRec->getLoop() == AddRecLoop) { |
| 1575 | for (unsigned i = 0, e = OtherAddRec->getNumOperands(); |
| 1576 | i != e; ++i) { |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1577 | if (i >= AddRecOps.size()) { |
Dan Gohman | 30cbc86 | 2010-08-29 14:53:34 +0000 | [diff] [blame] | 1578 | AddRecOps.append(OtherAddRec->op_begin()+i, |
| 1579 | OtherAddRec->op_end()); |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1580 | break; |
| 1581 | } |
Dan Gohman | 30cbc86 | 2010-08-29 14:53:34 +0000 | [diff] [blame] | 1582 | AddRecOps[i] = getAddExpr(AddRecOps[i], |
| 1583 | OtherAddRec->getOperand(i)); |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1584 | } |
| 1585 | Ops.erase(Ops.begin() + OtherIdx); --OtherIdx; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1586 | } |
Dan Gohman | 3252715 | 2010-08-27 20:45:56 +0000 | [diff] [blame] | 1587 | Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop); |
| 1588 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1592 | // next one. |
| 1593 | } |
| 1594 | |
| 1595 | // Okay, it looks like we really DO need an add expr. Check to see if we |
| 1596 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1597 | FoldingSetNodeID ID; |
| 1598 | ID.AddInteger(scAddExpr); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1599 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1600 | ID.AddPointer(Ops[i]); |
| 1601 | void *IP = 0; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1602 | SCEVAddExpr *S = |
| 1603 | static_cast<SCEVAddExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
| 1604 | if (!S) { |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1605 | const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); |
| 1606 | std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1607 | S = new (SCEVAllocator) SCEVAddExpr(ID.Intern(SCEVAllocator), |
| 1608 | O, Ops.size()); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1609 | UniqueSCEVs.InsertNode(S, IP); |
| 1610 | } |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1611 | if (HasNUW) S->setHasNoUnsignedWrap(true); |
| 1612 | if (HasNSW) S->setHasNoSignedWrap(true); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1613 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1616 | /// getMulExpr - Get a canonical multiply expression, or something simpler if |
| 1617 | /// possible. |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1618 | const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops, |
| 1619 | bool HasNUW, bool HasNSW) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1620 | assert(!Ops.empty() && "Cannot get empty mul!"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1621 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1622 | #ifndef NDEBUG |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 1623 | const Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1624 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 1625 | assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1626 | "SCEVMulExpr operand types don't match!"); |
| 1627 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1628 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1629 | // If HasNSW is true and all the operands are non-negative, infer HasNUW. |
| 1630 | if (!HasNUW && HasNSW) { |
| 1631 | bool All = true; |
Dan Gohman | 2d16fc5 | 2010-08-16 16:27:53 +0000 | [diff] [blame] | 1632 | for (SmallVectorImpl<const SCEV *>::const_iterator I = Ops.begin(), |
| 1633 | E = Ops.end(); I != E; ++I) |
| 1634 | if (!isKnownNonNegative(*I)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1635 | All = false; |
| 1636 | break; |
| 1637 | } |
| 1638 | if (All) HasNUW = true; |
| 1639 | } |
| 1640 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1641 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1642 | GroupByComplexity(Ops, LI); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1643 | |
| 1644 | // If there are any constants, fold them together. |
| 1645 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1646 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1647 | |
| 1648 | // C1*(C2+V) -> C1*C2 + C1*V |
| 1649 | if (Ops.size() == 2) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1650 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1651 | if (Add->getNumOperands() == 2 && |
| 1652 | isa<SCEVConstant>(Add->getOperand(0))) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1653 | return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)), |
| 1654 | getMulExpr(LHSC, Add->getOperand(1))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1655 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1656 | ++Idx; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1657 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1658 | // We found two constants, fold them together! |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1659 | ConstantInt *Fold = ConstantInt::get(getContext(), |
| 1660 | LHSC->getValue()->getValue() * |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1661 | RHSC->getValue()->getValue()); |
| 1662 | Ops[0] = getConstant(Fold); |
| 1663 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1664 | if (Ops.size() == 1) return Ops[0]; |
| 1665 | LHSC = cast<SCEVConstant>(Ops[0]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | // If we are left with a constant one being multiplied, strip it off. |
| 1669 | if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) { |
| 1670 | Ops.erase(Ops.begin()); |
| 1671 | --Idx; |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1672 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1673 | // If we have a multiply of zero, it will always be zero. |
| 1674 | return Ops[0]; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1675 | } else if (Ops[0]->isAllOnesValue()) { |
| 1676 | // If we have a mul by -1 of an add, try distributing the -1 among the |
| 1677 | // add operands. |
| 1678 | if (Ops.size() == 2) |
| 1679 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) { |
| 1680 | SmallVector<const SCEV *, 4> NewOps; |
| 1681 | bool AnyFolded = false; |
| 1682 | for (SCEVAddRecExpr::op_iterator I = Add->op_begin(), E = Add->op_end(); |
| 1683 | I != E; ++I) { |
| 1684 | const SCEV *Mul = getMulExpr(Ops[0], *I); |
| 1685 | if (!isa<SCEVMulExpr>(Mul)) AnyFolded = true; |
| 1686 | NewOps.push_back(Mul); |
| 1687 | } |
| 1688 | if (AnyFolded) |
| 1689 | return getAddExpr(NewOps); |
| 1690 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1691 | } |
Dan Gohman | 3ab1312 | 2010-04-13 16:49:23 +0000 | [diff] [blame] | 1692 | |
| 1693 | if (Ops.size() == 1) |
| 1694 | return Ops[0]; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | // Skip over the add expression until we get to a multiply. |
| 1698 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1699 | ++Idx; |
| 1700 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1701 | // If there are mul operands inline them all into this expression. |
| 1702 | if (Idx < Ops.size()) { |
| 1703 | bool DeletedMul = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1704 | while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1705 | // If we have an mul, expand the mul operands onto the end of the operands |
| 1706 | // list. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1707 | Ops.erase(Ops.begin()+Idx); |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 1708 | Ops.append(Mul->op_begin(), Mul->op_end()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1709 | DeletedMul = true; |
| 1710 | } |
| 1711 | |
| 1712 | // If we deleted at least one mul, we added operands to the end of the list, |
| 1713 | // and they are not necessarily sorted. Recurse to resort and resimplify |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 1714 | // any operands we just acquired. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1715 | if (DeletedMul) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1716 | return getMulExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | // If there are any add recurrences in the operands list, see if any other |
| 1720 | // added values are loop invariant. If so, we can fold them into the |
| 1721 | // recurrence. |
| 1722 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1723 | ++Idx; |
| 1724 | |
| 1725 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1726 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1727 | // Scan all of the other operands to this mul and add them to the vector if |
| 1728 | // they are loop invariant w.r.t. the recurrence. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1729 | SmallVector<const SCEV *, 8> LIOps; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1730 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Dan Gohman | 0f32ae3 | 2010-08-29 14:55:19 +0000 | [diff] [blame] | 1731 | const Loop *AddRecLoop = AddRec->getLoop(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1732 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1733 | if (isLoopInvariant(Ops[i], AddRecLoop)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1734 | LIOps.push_back(Ops[i]); |
| 1735 | Ops.erase(Ops.begin()+i); |
| 1736 | --i; --e; |
| 1737 | } |
| 1738 | |
| 1739 | // If we found some loop invariants, fold them into the recurrence. |
| 1740 | if (!LIOps.empty()) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1741 | // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step} |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1742 | SmallVector<const SCEV *, 4> NewOps; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1743 | NewOps.reserve(AddRec->getNumOperands()); |
Dan Gohman | 27ed6a4 | 2010-06-17 23:34:09 +0000 | [diff] [blame] | 1744 | const SCEV *Scale = getMulExpr(LIOps); |
| 1745 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
| 1746 | NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1747 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 1748 | // Build the new addrec. Propagate the NUW and NSW flags if both the |
| 1749 | // outer mul and the inner addrec are guaranteed to have no overflow. |
Dan Gohman | 0f32ae3 | 2010-08-29 14:55:19 +0000 | [diff] [blame] | 1750 | const SCEV *NewRec = getAddRecExpr(NewOps, AddRecLoop, |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1751 | HasNUW && AddRec->hasNoUnsignedWrap(), |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 1752 | HasNSW && AddRec->hasNoSignedWrap()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1753 | |
| 1754 | // If all of the other operands were loop invariant, we are done. |
| 1755 | if (Ops.size() == 1) return NewRec; |
| 1756 | |
| 1757 | // Otherwise, multiply the folded AddRec by the non-liv parts. |
| 1758 | for (unsigned i = 0;; ++i) |
| 1759 | if (Ops[i] == AddRec) { |
| 1760 | Ops[i] = NewRec; |
| 1761 | break; |
| 1762 | } |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1763 | return getMulExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1767 | // there are multiple AddRec's with the same loop induction variable being |
| 1768 | // multiplied together. If so, we can fold them. |
| 1769 | for (unsigned OtherIdx = Idx+1; |
Dan Gohman | 6a0c125 | 2010-08-31 22:52:12 +0000 | [diff] [blame] | 1770 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); |
| 1771 | ++OtherIdx) |
| 1772 | if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) { |
| 1773 | // F * G, where F = {A,+,B}<L> and G = {C,+,D}<L> --> |
| 1774 | // {A*C,+,F*D + G*B + B*D}<L> |
| 1775 | for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); |
| 1776 | ++OtherIdx) |
| 1777 | if (const SCEVAddRecExpr *OtherAddRec = |
| 1778 | dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx])) |
| 1779 | if (OtherAddRec->getLoop() == AddRecLoop) { |
| 1780 | const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec; |
| 1781 | const SCEV *NewStart = getMulExpr(F->getStart(), G->getStart()); |
| 1782 | const SCEV *B = F->getStepRecurrence(*this); |
| 1783 | const SCEV *D = G->getStepRecurrence(*this); |
| 1784 | const SCEV *NewStep = getAddExpr(getMulExpr(F, D), |
| 1785 | getMulExpr(G, B), |
| 1786 | getMulExpr(B, D)); |
| 1787 | const SCEV *NewAddRec = getAddRecExpr(NewStart, NewStep, |
| 1788 | F->getLoop()); |
| 1789 | if (Ops.size() == 2) return NewAddRec; |
| 1790 | Ops[Idx] = AddRec = cast<SCEVAddRecExpr>(NewAddRec); |
| 1791 | Ops.erase(Ops.begin() + OtherIdx); --OtherIdx; |
| 1792 | } |
| 1793 | return getMulExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1797 | // next one. |
| 1798 | } |
| 1799 | |
| 1800 | // Okay, it looks like we really DO need an mul expr. Check to see if we |
| 1801 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1802 | FoldingSetNodeID ID; |
| 1803 | ID.AddInteger(scMulExpr); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1804 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1805 | ID.AddPointer(Ops[i]); |
| 1806 | void *IP = 0; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1807 | SCEVMulExpr *S = |
| 1808 | static_cast<SCEVMulExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
| 1809 | if (!S) { |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 1810 | const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); |
| 1811 | std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1812 | S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator), |
| 1813 | O, Ops.size()); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1814 | UniqueSCEVs.InsertNode(S, IP); |
| 1815 | } |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1816 | if (HasNUW) S->setHasNoUnsignedWrap(true); |
| 1817 | if (HasNSW) S->setHasNoSignedWrap(true); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1818 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Andreas Bolka | 8a11c98 | 2009-08-07 22:55:26 +0000 | [diff] [blame] | 1821 | /// getUDivExpr - Get a canonical unsigned division expression, or something |
| 1822 | /// simpler if possible. |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1823 | const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS, |
| 1824 | const SCEV *RHS) { |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1825 | assert(getEffectiveSCEVType(LHS->getType()) == |
| 1826 | getEffectiveSCEVType(RHS->getType()) && |
| 1827 | "SCEVUDivExpr operand types don't match!"); |
| 1828 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1829 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1830 | if (RHSC->getValue()->equalsInt(1)) |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 1831 | return LHS; // X udiv 1 --> x |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 1832 | // If the denominator is zero, the result of the udiv is undefined. Don't |
| 1833 | // try to analyze it, because the resolution chosen here may differ from |
| 1834 | // the resolution chosen in other parts of the compiler. |
| 1835 | if (!RHSC->getValue()->isZero()) { |
| 1836 | // Determine if the division can be folded into the operands of |
| 1837 | // its operands. |
| 1838 | // TODO: Generalize this to non-constants by using known-bits information. |
| 1839 | const Type *Ty = LHS->getType(); |
| 1840 | unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros(); |
Dan Gohman | ddd3a88 | 2010-08-04 19:52:50 +0000 | [diff] [blame] | 1841 | unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1; |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 1842 | // For non-power-of-two values, effectively round the value up to the |
| 1843 | // nearest power of two. |
| 1844 | if (!RHSC->getValue()->getValue().isPowerOf2()) |
| 1845 | ++MaxShiftAmt; |
| 1846 | const IntegerType *ExtTy = |
| 1847 | IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt); |
| 1848 | // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. |
| 1849 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) |
| 1850 | if (const SCEVConstant *Step = |
| 1851 | dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) |
| 1852 | if (!Step->getValue()->getValue() |
| 1853 | .urem(RHSC->getValue()->getValue()) && |
| 1854 | getZeroExtendExpr(AR, ExtTy) == |
| 1855 | getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), |
| 1856 | getZeroExtendExpr(Step, ExtTy), |
| 1857 | AR->getLoop())) { |
| 1858 | SmallVector<const SCEV *, 4> Operands; |
| 1859 | for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i) |
| 1860 | Operands.push_back(getUDivExpr(AR->getOperand(i), RHS)); |
| 1861 | return getAddRecExpr(Operands, AR->getLoop()); |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1862 | } |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 1863 | // (A*B)/C --> A*(B/C) if safe and B/C can be folded. |
| 1864 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) { |
| 1865 | SmallVector<const SCEV *, 4> Operands; |
| 1866 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) |
| 1867 | Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy)); |
| 1868 | if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) |
| 1869 | // Find an operand that's safely divisible. |
| 1870 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { |
| 1871 | const SCEV *Op = M->getOperand(i); |
| 1872 | const SCEV *Div = getUDivExpr(Op, RHSC); |
| 1873 | if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) { |
| 1874 | Operands = SmallVector<const SCEV *, 4>(M->op_begin(), |
| 1875 | M->op_end()); |
| 1876 | Operands[i] = Div; |
| 1877 | return getMulExpr(Operands); |
| 1878 | } |
| 1879 | } |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1880 | } |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 1881 | // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded. |
| 1882 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) { |
| 1883 | SmallVector<const SCEV *, 4> Operands; |
| 1884 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) |
| 1885 | Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy)); |
| 1886 | if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { |
| 1887 | Operands.clear(); |
| 1888 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { |
| 1889 | const SCEV *Op = getUDivExpr(A->getOperand(i), RHS); |
| 1890 | if (isa<SCEVUDivExpr>(Op) || |
| 1891 | getMulExpr(Op, RHS) != A->getOperand(i)) |
| 1892 | break; |
| 1893 | Operands.push_back(Op); |
| 1894 | } |
| 1895 | if (Operands.size() == A->getNumOperands()) |
| 1896 | return getAddExpr(Operands); |
| 1897 | } |
| 1898 | } |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1899 | |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 1900 | // Fold if both operands are constant. |
| 1901 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { |
| 1902 | Constant *LHSCV = LHSC->getValue(); |
| 1903 | Constant *RHSCV = RHSC->getValue(); |
| 1904 | return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV, |
| 1905 | RHSCV))); |
| 1906 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1907 | } |
| 1908 | } |
| 1909 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1910 | FoldingSetNodeID ID; |
| 1911 | ID.AddInteger(scUDivExpr); |
| 1912 | ID.AddPointer(LHS); |
| 1913 | ID.AddPointer(RHS); |
| 1914 | void *IP = 0; |
| 1915 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 1916 | SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator), |
| 1917 | LHS, RHS); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1918 | UniqueSCEVs.InsertNode(S, IP); |
| 1919 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1923 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 1924 | /// Simplify the expression as much as possible. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1925 | const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1926 | const SCEV *Step, const Loop *L, |
| 1927 | bool HasNUW, bool HasNSW) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1928 | SmallVector<const SCEV *, 4> Operands; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1929 | Operands.push_back(Start); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1930 | if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step)) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1931 | if (StepChrec->getLoop() == L) { |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 1932 | Operands.append(StepChrec->op_begin(), StepChrec->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1933 | return getAddRecExpr(Operands, L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | Operands.push_back(Step); |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1937 | return getAddRecExpr(Operands, L, HasNUW, HasNSW); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1940 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 1941 | /// Simplify the expression as much as possible. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1942 | const SCEV * |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1943 | ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands, |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1944 | const Loop *L, |
| 1945 | bool HasNUW, bool HasNSW) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1946 | if (Operands.size() == 1) return Operands[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1947 | #ifndef NDEBUG |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 1948 | const Type *ETy = getEffectiveSCEVType(Operands[0]->getType()); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1949 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 1950 | assert(getEffectiveSCEVType(Operands[i]->getType()) == ETy && |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1951 | "SCEVAddRecExpr operand types don't match!"); |
Dan Gohman | 203a723 | 2010-11-17 20:48:38 +0000 | [diff] [blame] | 1952 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1953 | assert(isLoopInvariant(Operands[i], L) && |
Dan Gohman | 203a723 | 2010-11-17 20:48:38 +0000 | [diff] [blame] | 1954 | "SCEVAddRecExpr operand is not loop-invariant!"); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1955 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1956 | |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1957 | if (Operands.back()->isZero()) { |
| 1958 | Operands.pop_back(); |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1959 | return getAddRecExpr(Operands, L, HasNUW, HasNSW); // {X,+,0} --> X |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1960 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1961 | |
Dan Gohman | bc02853 | 2010-02-19 18:49:22 +0000 | [diff] [blame] | 1962 | // It's tempting to want to call getMaxBackedgeTakenCount count here and |
| 1963 | // use that information to infer NUW and NSW flags. However, computing a |
| 1964 | // BE count requires calling getAddRecExpr, so we may not yet have a |
| 1965 | // meaningful BE count at this point (and if we don't, we'd be stuck |
| 1966 | // with a SCEVCouldNotCompute as the cached BE count). |
| 1967 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1968 | // If HasNSW is true and all the operands are non-negative, infer HasNUW. |
| 1969 | if (!HasNUW && HasNSW) { |
| 1970 | bool All = true; |
Dan Gohman | 2d16fc5 | 2010-08-16 16:27:53 +0000 | [diff] [blame] | 1971 | for (SmallVectorImpl<const SCEV *>::const_iterator I = Operands.begin(), |
| 1972 | E = Operands.end(); I != E; ++I) |
| 1973 | if (!isKnownNonNegative(*I)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1974 | All = false; |
| 1975 | break; |
| 1976 | } |
| 1977 | if (All) HasNUW = true; |
| 1978 | } |
| 1979 | |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1980 | // Canonicalize nested AddRecs in by nesting them in order of loop depth. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1981 | if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) { |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 1982 | const Loop *NestedLoop = NestedAR->getLoop(); |
Dan Gohman | 9cba978 | 2010-08-13 20:23:25 +0000 | [diff] [blame] | 1983 | if (L->contains(NestedLoop) ? |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1984 | (L->getLoopDepth() < NestedLoop->getLoopDepth()) : |
Dan Gohman | 9cba978 | 2010-08-13 20:23:25 +0000 | [diff] [blame] | 1985 | (!NestedLoop->contains(L) && |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1986 | DT->dominates(L->getHeader(), NestedLoop->getHeader()))) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1987 | SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(), |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 1988 | NestedAR->op_end()); |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1989 | Operands[0] = NestedAR->getStart(); |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 1990 | // AddRecs require their operands be loop-invariant with respect to their |
| 1991 | // loops. Don't perform this transformation if it would break this |
| 1992 | // requirement. |
| 1993 | bool AllInvariant = true; |
| 1994 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1995 | if (!isLoopInvariant(Operands[i], L)) { |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 1996 | AllInvariant = false; |
| 1997 | break; |
| 1998 | } |
| 1999 | if (AllInvariant) { |
| 2000 | NestedOperands[0] = getAddRecExpr(Operands, L); |
| 2001 | AllInvariant = true; |
| 2002 | for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 2003 | if (!isLoopInvariant(NestedOperands[i], NestedLoop)) { |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 2004 | AllInvariant = false; |
| 2005 | break; |
| 2006 | } |
| 2007 | if (AllInvariant) |
| 2008 | // Ok, both add recurrences are valid after the transformation. |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 2009 | return getAddRecExpr(NestedOperands, NestedLoop, HasNUW, HasNSW); |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 2010 | } |
| 2011 | // Reset Operands to its original state. |
| 2012 | Operands[0] = NestedAR; |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 2013 | } |
| 2014 | } |
| 2015 | |
Dan Gohman | 6784753 | 2010-01-19 22:27:22 +0000 | [diff] [blame] | 2016 | // Okay, it looks like we really DO need an addrec expr. Check to see if we |
| 2017 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2018 | FoldingSetNodeID ID; |
| 2019 | ID.AddInteger(scAddRecExpr); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2020 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 2021 | ID.AddPointer(Operands[i]); |
| 2022 | ID.AddPointer(L); |
| 2023 | void *IP = 0; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2024 | SCEVAddRecExpr *S = |
| 2025 | static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); |
| 2026 | if (!S) { |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 2027 | const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Operands.size()); |
| 2028 | std::uninitialized_copy(Operands.begin(), Operands.end(), O); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 2029 | S = new (SCEVAllocator) SCEVAddRecExpr(ID.Intern(SCEVAllocator), |
| 2030 | O, Operands.size(), L); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2031 | UniqueSCEVs.InsertNode(S, IP); |
| 2032 | } |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 2033 | if (HasNUW) S->setHasNoUnsignedWrap(true); |
| 2034 | if (HasNSW) S->setHasNoSignedWrap(true); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2035 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2038 | const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, |
| 2039 | const SCEV *RHS) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2040 | SmallVector<const SCEV *, 2> Ops; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2041 | Ops.push_back(LHS); |
| 2042 | Ops.push_back(RHS); |
| 2043 | return getSMaxExpr(Ops); |
| 2044 | } |
| 2045 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2046 | const SCEV * |
| 2047 | ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2048 | assert(!Ops.empty() && "Cannot get empty smax!"); |
| 2049 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2050 | #ifndef NDEBUG |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 2051 | const Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2052 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 2053 | assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2054 | "SCEVSMaxExpr operand types don't match!"); |
| 2055 | #endif |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2056 | |
| 2057 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 2058 | GroupByComplexity(Ops, LI); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2059 | |
| 2060 | // If there are any constants, fold them together. |
| 2061 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2062 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2063 | ++Idx; |
| 2064 | assert(Idx < Ops.size()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2065 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2066 | // We found two constants, fold them together! |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2067 | ConstantInt *Fold = ConstantInt::get(getContext(), |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2068 | APIntOps::smax(LHSC->getValue()->getValue(), |
| 2069 | RHSC->getValue()->getValue())); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2070 | Ops[0] = getConstant(Fold); |
| 2071 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 2072 | if (Ops.size() == 1) return Ops[0]; |
| 2073 | LHSC = cast<SCEVConstant>(Ops[0]); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 2076 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2077 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) { |
| 2078 | Ops.erase(Ops.begin()); |
| 2079 | --Idx; |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 2080 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) { |
| 2081 | // If we have an smax with a constant maximum-int, it will always be |
| 2082 | // maximum-int. |
| 2083 | return Ops[0]; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2084 | } |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2085 | |
Dan Gohman | 3ab1312 | 2010-04-13 16:49:23 +0000 | [diff] [blame] | 2086 | if (Ops.size() == 1) return Ops[0]; |
| 2087 | } |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2088 | |
| 2089 | // Find the first SMax |
| 2090 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr) |
| 2091 | ++Idx; |
| 2092 | |
| 2093 | // Check to see if one of the operands is an SMax. If so, expand its operands |
| 2094 | // onto our operand list, and recurse to simplify. |
| 2095 | if (Idx < Ops.size()) { |
| 2096 | bool DeletedSMax = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2097 | while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2098 | Ops.erase(Ops.begin()+Idx); |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 2099 | Ops.append(SMax->op_begin(), SMax->op_end()); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2100 | DeletedSMax = true; |
| 2101 | } |
| 2102 | |
| 2103 | if (DeletedSMax) |
| 2104 | return getSMaxExpr(Ops); |
| 2105 | } |
| 2106 | |
| 2107 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 2108 | // so, delete one. Since we sorted the list, these values are required to |
| 2109 | // be adjacent. |
| 2110 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
Dan Gohman | 2828779 | 2010-04-13 16:51:03 +0000 | [diff] [blame] | 2111 | // X smax Y smax Y --> X smax Y |
| 2112 | // X smax Y --> X, if X is always greater than Y |
| 2113 | if (Ops[i] == Ops[i+1] || |
| 2114 | isKnownPredicate(ICmpInst::ICMP_SGE, Ops[i], Ops[i+1])) { |
| 2115 | Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2); |
| 2116 | --i; --e; |
| 2117 | } else if (isKnownPredicate(ICmpInst::ICMP_SLE, Ops[i], Ops[i+1])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2118 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 2119 | --i; --e; |
| 2120 | } |
| 2121 | |
| 2122 | if (Ops.size() == 1) return Ops[0]; |
| 2123 | |
| 2124 | assert(!Ops.empty() && "Reduced smax down to nothing!"); |
| 2125 | |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2126 | // 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] | 2127 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2128 | FoldingSetNodeID ID; |
| 2129 | ID.AddInteger(scSMaxExpr); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2130 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 2131 | ID.AddPointer(Ops[i]); |
| 2132 | void *IP = 0; |
| 2133 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 2134 | const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); |
| 2135 | std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 2136 | SCEV *S = new (SCEVAllocator) SCEVSMaxExpr(ID.Intern(SCEVAllocator), |
| 2137 | O, Ops.size()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2138 | UniqueSCEVs.InsertNode(S, IP); |
| 2139 | return S; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2142 | const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, |
| 2143 | const SCEV *RHS) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2144 | SmallVector<const SCEV *, 2> Ops; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2145 | Ops.push_back(LHS); |
| 2146 | Ops.push_back(RHS); |
| 2147 | return getUMaxExpr(Ops); |
| 2148 | } |
| 2149 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2150 | const SCEV * |
| 2151 | ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2152 | assert(!Ops.empty() && "Cannot get empty umax!"); |
| 2153 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2154 | #ifndef NDEBUG |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 2155 | const Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2156 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
Dan Gohman | c4f7798 | 2010-08-16 16:13:54 +0000 | [diff] [blame] | 2157 | assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 2158 | "SCEVUMaxExpr operand types don't match!"); |
| 2159 | #endif |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2160 | |
| 2161 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 2162 | GroupByComplexity(Ops, LI); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2163 | |
| 2164 | // If there are any constants, fold them together. |
| 2165 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2166 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2167 | ++Idx; |
| 2168 | assert(Idx < Ops.size()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2169 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2170 | // We found two constants, fold them together! |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2171 | ConstantInt *Fold = ConstantInt::get(getContext(), |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2172 | APIntOps::umax(LHSC->getValue()->getValue(), |
| 2173 | RHSC->getValue()->getValue())); |
| 2174 | Ops[0] = getConstant(Fold); |
| 2175 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 2176 | if (Ops.size() == 1) return Ops[0]; |
| 2177 | LHSC = cast<SCEVConstant>(Ops[0]); |
| 2178 | } |
| 2179 | |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 2180 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2181 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) { |
| 2182 | Ops.erase(Ops.begin()); |
| 2183 | --Idx; |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 2184 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) { |
| 2185 | // If we have an umax with a constant maximum-int, it will always be |
| 2186 | // maximum-int. |
| 2187 | return Ops[0]; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2188 | } |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2189 | |
Dan Gohman | 3ab1312 | 2010-04-13 16:49:23 +0000 | [diff] [blame] | 2190 | if (Ops.size() == 1) return Ops[0]; |
| 2191 | } |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2192 | |
| 2193 | // Find the first UMax |
| 2194 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr) |
| 2195 | ++Idx; |
| 2196 | |
| 2197 | // Check to see if one of the operands is a UMax. If so, expand its operands |
| 2198 | // onto our operand list, and recurse to simplify. |
| 2199 | if (Idx < Ops.size()) { |
| 2200 | bool DeletedUMax = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2201 | while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2202 | Ops.erase(Ops.begin()+Idx); |
Dan Gohman | 403a8cd | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 2203 | Ops.append(UMax->op_begin(), UMax->op_end()); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2204 | DeletedUMax = true; |
| 2205 | } |
| 2206 | |
| 2207 | if (DeletedUMax) |
| 2208 | return getUMaxExpr(Ops); |
| 2209 | } |
| 2210 | |
| 2211 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 2212 | // so, delete one. Since we sorted the list, these values are required to |
| 2213 | // be adjacent. |
| 2214 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
Dan Gohman | 2828779 | 2010-04-13 16:51:03 +0000 | [diff] [blame] | 2215 | // X umax Y umax Y --> X umax Y |
| 2216 | // X umax Y --> X, if X is always greater than Y |
| 2217 | if (Ops[i] == Ops[i+1] || |
| 2218 | isKnownPredicate(ICmpInst::ICMP_UGE, Ops[i], Ops[i+1])) { |
| 2219 | Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2); |
| 2220 | --i; --e; |
| 2221 | } else if (isKnownPredicate(ICmpInst::ICMP_ULE, Ops[i], Ops[i+1])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2222 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 2223 | --i; --e; |
| 2224 | } |
| 2225 | |
| 2226 | if (Ops.size() == 1) return Ops[0]; |
| 2227 | |
| 2228 | assert(!Ops.empty() && "Reduced umax down to nothing!"); |
| 2229 | |
| 2230 | // Okay, it looks like we really DO need a umax expr. Check to see if we |
| 2231 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2232 | FoldingSetNodeID ID; |
| 2233 | ID.AddInteger(scUMaxExpr); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2234 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 2235 | ID.AddPointer(Ops[i]); |
| 2236 | void *IP = 0; |
| 2237 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | f9e6472 | 2010-03-18 01:17:13 +0000 | [diff] [blame] | 2238 | const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); |
| 2239 | std::uninitialized_copy(Ops.begin(), Ops.end(), O); |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 2240 | SCEV *S = new (SCEVAllocator) SCEVUMaxExpr(ID.Intern(SCEVAllocator), |
| 2241 | O, Ops.size()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2242 | UniqueSCEVs.InsertNode(S, IP); |
| 2243 | return S; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2246 | const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS, |
| 2247 | const SCEV *RHS) { |
Dan Gohman | f9a9a99 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2248 | // ~smax(~x, ~y) == smin(x, y). |
| 2249 | return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 2250 | } |
| 2251 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2252 | const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS, |
| 2253 | const SCEV *RHS) { |
Dan Gohman | f9a9a99 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2254 | // ~umax(~x, ~y) == umin(x, y) |
| 2255 | return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 2256 | } |
| 2257 | |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2258 | const SCEV *ScalarEvolution::getSizeOfExpr(const Type *AllocTy) { |
Dan Gohman | 6ab10f6 | 2010-04-12 23:03:26 +0000 | [diff] [blame] | 2259 | // If we have TargetData, we can bypass creating a target-independent |
| 2260 | // constant expression and then folding it back into a ConstantInt. |
| 2261 | // This is just a compile-time optimization. |
| 2262 | if (TD) |
| 2263 | return getConstant(TD->getIntPtrType(getContext()), |
| 2264 | TD->getTypeAllocSize(AllocTy)); |
| 2265 | |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2266 | Constant *C = ConstantExpr::getSizeOf(AllocTy); |
| 2267 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
Dan Gohman | 7000122 | 2010-05-28 16:12:08 +0000 | [diff] [blame] | 2268 | if (Constant *Folded = ConstantFoldConstantExpression(CE, TD)) |
| 2269 | C = Folded; |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2270 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy)); |
| 2271 | return getTruncateOrZeroExtend(getSCEV(C), Ty); |
| 2272 | } |
| 2273 | |
| 2274 | const SCEV *ScalarEvolution::getAlignOfExpr(const Type *AllocTy) { |
| 2275 | Constant *C = ConstantExpr::getAlignOf(AllocTy); |
| 2276 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
Dan Gohman | 7000122 | 2010-05-28 16:12:08 +0000 | [diff] [blame] | 2277 | if (Constant *Folded = ConstantFoldConstantExpression(CE, TD)) |
| 2278 | C = Folded; |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2279 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy)); |
| 2280 | return getTruncateOrZeroExtend(getSCEV(C), Ty); |
| 2281 | } |
| 2282 | |
| 2283 | const SCEV *ScalarEvolution::getOffsetOfExpr(const StructType *STy, |
| 2284 | unsigned FieldNo) { |
Dan Gohman | 6ab10f6 | 2010-04-12 23:03:26 +0000 | [diff] [blame] | 2285 | // If we have TargetData, we can bypass creating a target-independent |
| 2286 | // constant expression and then folding it back into a ConstantInt. |
| 2287 | // This is just a compile-time optimization. |
| 2288 | if (TD) |
| 2289 | return getConstant(TD->getIntPtrType(getContext()), |
| 2290 | TD->getStructLayout(STy)->getElementOffset(FieldNo)); |
| 2291 | |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 2292 | Constant *C = ConstantExpr::getOffsetOf(STy, FieldNo); |
| 2293 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
Dan Gohman | 7000122 | 2010-05-28 16:12:08 +0000 | [diff] [blame] | 2294 | if (Constant *Folded = ConstantFoldConstantExpression(CE, TD)) |
| 2295 | C = Folded; |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2296 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy)); |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 2297 | return getTruncateOrZeroExtend(getSCEV(C), Ty); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2300 | const SCEV *ScalarEvolution::getOffsetOfExpr(const Type *CTy, |
| 2301 | Constant *FieldNo) { |
| 2302 | Constant *C = ConstantExpr::getOffsetOf(CTy, FieldNo); |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 2303 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
Dan Gohman | 7000122 | 2010-05-28 16:12:08 +0000 | [diff] [blame] | 2304 | if (Constant *Folded = ConstantFoldConstantExpression(CE, TD)) |
| 2305 | C = Folded; |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 2306 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(CTy)); |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 2307 | return getTruncateOrZeroExtend(getSCEV(C), Ty); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2310 | const SCEV *ScalarEvolution::getUnknown(Value *V) { |
Dan Gohman | 6bbcba1 | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2311 | // Don't attempt to do anything other than create a SCEVUnknown object |
| 2312 | // here. createSCEV only calls getUnknown after checking for all other |
| 2313 | // interesting possibilities, and any other code that calls getUnknown |
| 2314 | // is doing so in order to hide a value from SCEV canonicalization. |
| 2315 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2316 | FoldingSetNodeID ID; |
| 2317 | ID.AddInteger(scUnknown); |
| 2318 | ID.AddPointer(V); |
| 2319 | void *IP = 0; |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 2320 | if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) { |
| 2321 | assert(cast<SCEVUnknown>(S)->getValue() == V && |
| 2322 | "Stale SCEVUnknown in uniquing map!"); |
| 2323 | return S; |
| 2324 | } |
| 2325 | SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator), V, this, |
| 2326 | FirstUnknown); |
| 2327 | FirstUnknown = cast<SCEVUnknown>(S); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2328 | UniqueSCEVs.InsertNode(S, IP); |
| 2329 | return S; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 2330 | } |
| 2331 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2332 | //===----------------------------------------------------------------------===// |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2333 | // Basic SCEV Analysis and PHI Idiom Recognition Code |
| 2334 | // |
| 2335 | |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2336 | /// isSCEVable - Test if values of the given type are analyzable within |
| 2337 | /// the SCEV framework. This primarily includes integer types, and it |
| 2338 | /// can optionally include pointer types if the ScalarEvolution class |
| 2339 | /// has access to target-specific information. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2340 | bool ScalarEvolution::isSCEVable(const Type *Ty) const { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2341 | // Integers and pointers are always SCEVable. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2342 | return Ty->isIntegerTy() || Ty->isPointerTy(); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | /// getTypeSizeInBits - Return the size in bits of the specified type, |
| 2346 | /// for which isSCEVable must return true. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2347 | uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2348 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2349 | |
| 2350 | // If we have a TargetData, use it! |
| 2351 | if (TD) |
| 2352 | return TD->getTypeSizeInBits(Ty); |
| 2353 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2354 | // Integer types have fixed sizes. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2355 | if (Ty->isIntegerTy()) |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2356 | return Ty->getPrimitiveSizeInBits(); |
| 2357 | |
| 2358 | // The only other support type is pointer. Without TargetData, conservatively |
| 2359 | // assume pointers are 64-bit. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2360 | assert(Ty->isPointerTy() && "isSCEVable permitted a non-SCEVable type!"); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2361 | return 64; |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
| 2364 | /// getEffectiveSCEVType - Return a type with the same bitwidth as |
| 2365 | /// the given type and which represents how SCEV will treat the given |
| 2366 | /// type, for which isSCEVable must return true. For pointer types, |
| 2367 | /// this is the pointer-sized integer type. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2368 | const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2369 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2370 | |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2371 | if (Ty->isIntegerTy()) |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2372 | return Ty; |
| 2373 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2374 | // The only other support type is pointer. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2375 | assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!"); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2376 | if (TD) return TD->getIntPtrType(getContext()); |
| 2377 | |
| 2378 | // Without TargetData, conservatively assume pointers are 64-bit. |
| 2379 | return Type::getInt64Ty(getContext()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2380 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2381 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2382 | const SCEV *ScalarEvolution::getCouldNotCompute() { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2383 | return &CouldNotCompute; |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2386 | /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the |
| 2387 | /// expression and create a new one. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2388 | const SCEV *ScalarEvolution::getSCEV(Value *V) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2389 | assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2390 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2391 | ValueExprMapType::const_iterator I = ValueExprMap.find(V); |
| 2392 | if (I != ValueExprMap.end()) return I->second; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2393 | const SCEV *S = createSCEV(V); |
Dan Gohman | 619d332 | 2010-08-16 16:31:39 +0000 | [diff] [blame] | 2394 | |
| 2395 | // The process of creating a SCEV for V may have caused other SCEVs |
| 2396 | // to have been created, so it's necessary to insert the new entry |
| 2397 | // from scratch, rather than trying to remember the insert position |
| 2398 | // above. |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2399 | ValueExprMap.insert(std::make_pair(SCEVCallbackVH(V, this), S)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2400 | return S; |
| 2401 | } |
| 2402 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2403 | /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V |
| 2404 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2405 | const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) { |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2406 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2407 | return getConstant( |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2408 | cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue()))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2409 | |
| 2410 | const Type *Ty = V->getType(); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2411 | Ty = getEffectiveSCEVType(Ty); |
Owen Anderson | 73c6b71 | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2412 | return getMulExpr(V, |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2413 | getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty)))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
| 2416 | /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2417 | const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) { |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2418 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Owen Anderson | 73c6b71 | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2419 | return getConstant( |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2420 | cast<ConstantInt>(ConstantExpr::getNot(VC->getValue()))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2421 | |
| 2422 | const Type *Ty = V->getType(); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2423 | Ty = getEffectiveSCEVType(Ty); |
Owen Anderson | 73c6b71 | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2424 | const SCEV *AllOnes = |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2425 | getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2426 | return getMinusSCEV(AllOnes, V); |
| 2427 | } |
| 2428 | |
| 2429 | /// getMinusSCEV - Return a SCEV corresponding to LHS - RHS. |
| 2430 | /// |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2431 | const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, |
| 2432 | const SCEV *RHS) { |
Dan Gohman | eb4152c | 2010-07-20 16:53:00 +0000 | [diff] [blame] | 2433 | // Fast path: X - X --> 0. |
| 2434 | if (LHS == RHS) |
| 2435 | return getConstant(LHS->getType(), 0); |
| 2436 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2437 | // X - Y --> X + -Y |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2438 | return getAddExpr(LHS, getNegativeSCEV(RHS)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
| 2441 | /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2442 | /// input value to the specified type. If the type must be extended, it is zero |
| 2443 | /// extended. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2444 | const SCEV * |
| 2445 | ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2446 | const Type *Ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2447 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2448 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2449 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2450 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2451 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2452 | return V; // No conversion |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2453 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2454 | return getTruncateExpr(V, Ty); |
| 2455 | return getZeroExtendExpr(V, Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2459 | /// input value to the specified type. If the type must be extended, it is sign |
| 2460 | /// extended. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2461 | const SCEV * |
| 2462 | ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2463 | const Type *Ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2464 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2465 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2466 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2467 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2468 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2469 | return V; // No conversion |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2470 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2471 | return getTruncateExpr(V, Ty); |
| 2472 | return getSignExtendExpr(V, Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2475 | /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2476 | /// input value to the specified type. If the type must be extended, it is zero |
| 2477 | /// extended. The conversion must not be narrowing. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2478 | const SCEV * |
| 2479 | ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2480 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2481 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2482 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2483 | "Cannot noop or zero extend with non-integer arguments!"); |
| 2484 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2485 | "getNoopOrZeroExtend cannot truncate!"); |
| 2486 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2487 | return V; // No conversion |
| 2488 | return getZeroExtendExpr(V, Ty); |
| 2489 | } |
| 2490 | |
| 2491 | /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2492 | /// input value to the specified type. If the type must be extended, it is sign |
| 2493 | /// extended. The conversion must not be narrowing. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2494 | const SCEV * |
| 2495 | ScalarEvolution::getNoopOrSignExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2496 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2497 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2498 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2499 | "Cannot noop or sign extend with non-integer arguments!"); |
| 2500 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2501 | "getNoopOrSignExtend cannot truncate!"); |
| 2502 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2503 | return V; // No conversion |
| 2504 | return getSignExtendExpr(V, Ty); |
| 2505 | } |
| 2506 | |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2507 | /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of |
| 2508 | /// the input value to the specified type. If the type must be extended, |
| 2509 | /// it is extended with unspecified bits. The conversion must not be |
| 2510 | /// narrowing. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2511 | const SCEV * |
| 2512 | ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2513 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2514 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2515 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2516 | "Cannot noop or any extend with non-integer arguments!"); |
| 2517 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2518 | "getNoopOrAnyExtend cannot truncate!"); |
| 2519 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2520 | return V; // No conversion |
| 2521 | return getAnyExtendExpr(V, Ty); |
| 2522 | } |
| 2523 | |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2524 | /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the |
| 2525 | /// input value to the specified type. The conversion must not be widening. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2526 | const SCEV * |
| 2527 | ScalarEvolution::getTruncateOrNoop(const SCEV *V, const Type *Ty) { |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2528 | const Type *SrcTy = V->getType(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2529 | assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && |
| 2530 | (Ty->isIntegerTy() || Ty->isPointerTy()) && |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2531 | "Cannot truncate or noop with non-integer arguments!"); |
| 2532 | assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) && |
| 2533 | "getTruncateOrNoop cannot extend!"); |
| 2534 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2535 | return V; // No conversion |
| 2536 | return getTruncateExpr(V, Ty); |
| 2537 | } |
| 2538 | |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2539 | /// getUMaxFromMismatchedTypes - Promote the operands to the wider of |
| 2540 | /// the types using zero-extension, and then perform a umax operation |
| 2541 | /// with them. |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2542 | const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS, |
| 2543 | const SCEV *RHS) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2544 | const SCEV *PromotedLHS = LHS; |
| 2545 | const SCEV *PromotedRHS = RHS; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2546 | |
| 2547 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2548 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2549 | else |
| 2550 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2551 | |
| 2552 | return getUMaxExpr(PromotedLHS, PromotedRHS); |
| 2553 | } |
| 2554 | |
Dan Gohman | c9759e8 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2555 | /// getUMinFromMismatchedTypes - Promote the operands to the wider of |
| 2556 | /// the types using zero-extension, and then perform a umin operation |
| 2557 | /// with them. |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2558 | const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS, |
| 2559 | const SCEV *RHS) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2560 | const SCEV *PromotedLHS = LHS; |
| 2561 | const SCEV *PromotedRHS = RHS; |
Dan Gohman | c9759e8 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2562 | |
| 2563 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2564 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2565 | else |
| 2566 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2567 | |
| 2568 | return getUMinExpr(PromotedLHS, PromotedRHS); |
| 2569 | } |
| 2570 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2571 | /// PushDefUseChildren - Push users of the given Instruction |
| 2572 | /// onto the given Worklist. |
| 2573 | static void |
| 2574 | PushDefUseChildren(Instruction *I, |
| 2575 | SmallVectorImpl<Instruction *> &Worklist) { |
| 2576 | // Push the def-use children onto the Worklist stack. |
| 2577 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 2578 | UI != UE; ++UI) |
Gabor Greif | 96f1d8e | 2010-07-22 13:36:47 +0000 | [diff] [blame] | 2579 | Worklist.push_back(cast<Instruction>(*UI)); |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | /// ForgetSymbolicValue - This looks up computed SCEV values for all |
| 2583 | /// instructions that depend on the given instruction and removes them from |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2584 | /// the ValueExprMapType map if they reference SymName. This is used during PHI |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2585 | /// resolution. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2586 | void |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2587 | ScalarEvolution::ForgetSymbolicName(Instruction *PN, const SCEV *SymName) { |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2588 | SmallVector<Instruction *, 16> Worklist; |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2589 | PushDefUseChildren(PN, Worklist); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2590 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2591 | SmallPtrSet<Instruction *, 8> Visited; |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2592 | Visited.insert(PN); |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2593 | while (!Worklist.empty()) { |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2594 | Instruction *I = Worklist.pop_back_val(); |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2595 | if (!Visited.insert(I)) continue; |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2596 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2597 | ValueExprMapType::iterator It = |
| 2598 | ValueExprMap.find(static_cast<Value *>(I)); |
| 2599 | if (It != ValueExprMap.end()) { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 2600 | const SCEV *Old = It->second; |
| 2601 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2602 | // Short-circuit the def-use traversal if the symbolic name |
| 2603 | // ceases to appear in expressions. |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 2604 | if (Old != SymName && !Old->hasOperand(SymName)) |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2605 | continue; |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2606 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2607 | // SCEVUnknown for a PHI either means that it has an unrecognized |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2608 | // structure, it's a PHI that's in the progress of being computed |
| 2609 | // by createNodeForPHI, or it's a single-value PHI. In the first case, |
| 2610 | // additional loop trip count information isn't going to change anything. |
| 2611 | // In the second case, createNodeForPHI will perform the necessary |
| 2612 | // updates on its own when it gets to that point. In the third, we do |
| 2613 | // want to forget the SCEVUnknown. |
| 2614 | if (!isa<PHINode>(I) || |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 2615 | !isa<SCEVUnknown>(Old) || |
| 2616 | (I != PN && Old == SymName)) { |
| 2617 | ValuesAtScopes.erase(Old); |
| 2618 | UnsignedRanges.erase(Old); |
| 2619 | SignedRanges.erase(Old); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2620 | ValueExprMap.erase(It); |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 2621 | } |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
| 2624 | PushDefUseChildren(I, Worklist); |
| 2625 | } |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2626 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2627 | |
| 2628 | /// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in |
| 2629 | /// a loop header, making it a potential recurrence, or it doesn't. |
| 2630 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2631 | const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) { |
Dan Gohman | 27dead4 | 2010-04-12 07:49:36 +0000 | [diff] [blame] | 2632 | if (const Loop *L = LI->getLoopFor(PN->getParent())) |
| 2633 | if (L->getHeader() == PN->getParent()) { |
| 2634 | // The loop may have multiple entrances or multiple exits; we can analyze |
| 2635 | // this phi as an addrec if it has a unique entry value and a unique |
| 2636 | // backedge value. |
| 2637 | Value *BEValueV = 0, *StartValueV = 0; |
| 2638 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 2639 | Value *V = PN->getIncomingValue(i); |
| 2640 | if (L->contains(PN->getIncomingBlock(i))) { |
| 2641 | if (!BEValueV) { |
| 2642 | BEValueV = V; |
| 2643 | } else if (BEValueV != V) { |
| 2644 | BEValueV = 0; |
| 2645 | break; |
| 2646 | } |
| 2647 | } else if (!StartValueV) { |
| 2648 | StartValueV = V; |
| 2649 | } else if (StartValueV != V) { |
| 2650 | StartValueV = 0; |
| 2651 | break; |
| 2652 | } |
| 2653 | } |
| 2654 | if (BEValueV && StartValueV) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2655 | // While we are analyzing this PHI node, handle its value symbolically. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2656 | const SCEV *SymbolicName = getUnknown(PN); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2657 | assert(ValueExprMap.find(PN) == ValueExprMap.end() && |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2658 | "PHI node already processed?"); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2659 | ValueExprMap.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2660 | |
| 2661 | // Using this symbolic name for the PHI, analyze the value coming around |
| 2662 | // the back-edge. |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2663 | const SCEV *BEValue = getSCEV(BEValueV); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2664 | |
| 2665 | // NOTE: If BEValue is loop invariant, we know that the PHI node just |
| 2666 | // has a special value for the first iteration of the loop. |
| 2667 | |
| 2668 | // If the value coming around the backedge is an add with the symbolic |
| 2669 | // value we just inserted, then we found a simple induction variable! |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2670 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2671 | // If there is a single occurrence of the symbolic value, replace it |
| 2672 | // with a recurrence. |
| 2673 | unsigned FoundIndex = Add->getNumOperands(); |
| 2674 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2675 | if (Add->getOperand(i) == SymbolicName) |
| 2676 | if (FoundIndex == e) { |
| 2677 | FoundIndex = i; |
| 2678 | break; |
| 2679 | } |
| 2680 | |
| 2681 | if (FoundIndex != Add->getNumOperands()) { |
| 2682 | // Create an add with everything but the specified operand. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2683 | SmallVector<const SCEV *, 8> Ops; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2684 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2685 | if (i != FoundIndex) |
| 2686 | Ops.push_back(Add->getOperand(i)); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2687 | const SCEV *Accum = getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2688 | |
| 2689 | // This is not a valid addrec if the step amount is varying each |
| 2690 | // loop iteration, but is not itself an addrec in this loop. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 2691 | if (isLoopInvariant(Accum, L) || |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2692 | (isa<SCEVAddRecExpr>(Accum) && |
| 2693 | cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2694 | bool HasNUW = false; |
| 2695 | bool HasNSW = false; |
| 2696 | |
| 2697 | // If the increment doesn't overflow, then neither the addrec nor |
| 2698 | // the post-increment will overflow. |
| 2699 | if (const AddOperator *OBO = dyn_cast<AddOperator>(BEValueV)) { |
| 2700 | if (OBO->hasNoUnsignedWrap()) |
| 2701 | HasNUW = true; |
| 2702 | if (OBO->hasNoSignedWrap()) |
| 2703 | HasNSW = true; |
| 2704 | } |
| 2705 | |
Dan Gohman | 27dead4 | 2010-04-12 07:49:36 +0000 | [diff] [blame] | 2706 | const SCEV *StartVal = getSCEV(StartValueV); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2707 | const SCEV *PHISCEV = |
| 2708 | getAddRecExpr(StartVal, Accum, L, HasNUW, HasNSW); |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2709 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2710 | // Since the no-wrap flags are on the increment, they apply to the |
| 2711 | // post-incremented value as well. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 2712 | if (isLoopInvariant(Accum, L)) |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2713 | (void)getAddRecExpr(getAddExpr(StartVal, Accum), |
| 2714 | Accum, L, HasNUW, HasNSW); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2715 | |
| 2716 | // Okay, for the entire analysis of this edge we assumed the PHI |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2717 | // to be symbolic. We now need to go back and purge all of the |
| 2718 | // entries for the scalars that use the symbolic expression. |
| 2719 | ForgetSymbolicName(PN, SymbolicName); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2720 | ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2721 | return PHISCEV; |
| 2722 | } |
| 2723 | } |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2724 | } else if (const SCEVAddRecExpr *AddRec = |
| 2725 | dyn_cast<SCEVAddRecExpr>(BEValue)) { |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2726 | // Otherwise, this could be a loop like this: |
| 2727 | // i = 0; for (j = 1; ..; ++j) { .... i = j; } |
| 2728 | // In this case, j = {1,+,1} and BEValue is j. |
| 2729 | // Because the other in-value of i (0) fits the evolution of BEValue |
| 2730 | // i really is an addrec evolution. |
| 2731 | if (AddRec->getLoop() == L && AddRec->isAffine()) { |
Dan Gohman | 27dead4 | 2010-04-12 07:49:36 +0000 | [diff] [blame] | 2732 | const SCEV *StartVal = getSCEV(StartValueV); |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2733 | |
| 2734 | // If StartVal = j.start - j.stride, we can use StartVal as the |
| 2735 | // initial step of the addrec evolution. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2736 | if (StartVal == getMinusSCEV(AddRec->getOperand(0), |
Dan Gohman | 5ee60f7 | 2010-04-11 23:44:58 +0000 | [diff] [blame] | 2737 | AddRec->getOperand(1))) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2738 | const SCEV *PHISCEV = |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2739 | getAddRecExpr(StartVal, AddRec->getOperand(1), L); |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2740 | |
| 2741 | // Okay, for the entire analysis of this edge we assumed the PHI |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2742 | // to be symbolic. We now need to go back and purge all of the |
| 2743 | // entries for the scalars that use the symbolic expression. |
| 2744 | ForgetSymbolicName(PN, SymbolicName); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 2745 | ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV; |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2746 | return PHISCEV; |
| 2747 | } |
| 2748 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2749 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2750 | } |
Dan Gohman | 27dead4 | 2010-04-12 07:49:36 +0000 | [diff] [blame] | 2751 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2752 | |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2753 | // If the PHI has a single incoming value, follow that value, unless the |
| 2754 | // PHI's incoming blocks are in a different loop, in which case doing so |
| 2755 | // risks breaking LCSSA form. Instcombine would normally zap these, but |
| 2756 | // it doesn't have DominatorTree information, so it may miss cases. |
Duncan Sands | a0c5244 | 2010-11-17 04:18:45 +0000 | [diff] [blame] | 2757 | if (Value *V = SimplifyInstruction(PN, TD, DT)) { |
Duncan Sands | 6f8a5dd | 2010-11-17 20:49:12 +0000 | [diff] [blame] | 2758 | Instruction *I = dyn_cast<Instruction>(V); |
| 2759 | // Only instructions are problematic for preserving LCSSA form. |
| 2760 | if (!I) |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2761 | return getSCEV(V); |
Duncan Sands | 6f8a5dd | 2010-11-17 20:49:12 +0000 | [diff] [blame] | 2762 | |
| 2763 | // If the instruction is not defined in a loop, then it can be used freely. |
| 2764 | Loop *ILoop = LI->getLoopFor(I->getParent()); |
| 2765 | if (!ILoop) |
| 2766 | return getSCEV(I); |
| 2767 | |
| 2768 | // If the instruction is defined in the same loop as the phi node, or in a |
| 2769 | // loop that contains the phi node loop as an inner loop, then using it as |
| 2770 | // a replacement for the phi node will not break LCSSA form. |
| 2771 | Loop *PNLoop = LI->getLoopFor(PN->getParent()); |
| 2772 | if (ILoop->contains(PNLoop)) |
| 2773 | return getSCEV(I); |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 2774 | } |
Dan Gohman | a653fc5 | 2009-07-14 14:06:25 +0000 | [diff] [blame] | 2775 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2776 | // 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] | 2777 | return getUnknown(PN); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2780 | /// createNodeForGEP - Expand GEP instructions into add and multiply |
| 2781 | /// operations. This allows them to be analyzed by regular SCEV code. |
| 2782 | /// |
Dan Gohman | d281ed2 | 2009-12-18 02:09:29 +0000 | [diff] [blame] | 2783 | const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) { |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2784 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2785 | // Don't blindly transfer the inbounds flag from the GEP instruction to the |
| 2786 | // Add expression, because the Instruction may be guarded by control flow |
| 2787 | // 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] | 2788 | // context. |
Dan Gohman | 7a64257 | 2010-06-29 01:41:41 +0000 | [diff] [blame] | 2789 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2790 | const Type *IntPtrTy = getEffectiveSCEVType(GEP->getType()); |
Dan Gohman | e810b0d | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2791 | Value *Base = GEP->getOperand(0); |
Dan Gohman | c63a627 | 2009-05-09 00:14:52 +0000 | [diff] [blame] | 2792 | // Don't attempt to analyze GEPs over unsized objects. |
| 2793 | if (!cast<PointerType>(Base->getType())->getElementType()->isSized()) |
| 2794 | return getUnknown(GEP); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 2795 | const SCEV *TotalOffset = getConstant(IntPtrTy, 0); |
Dan Gohman | e810b0d | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2796 | gep_type_iterator GTI = gep_type_begin(GEP); |
Oscar Fuentes | ee56c42 | 2010-08-02 06:00:15 +0000 | [diff] [blame] | 2797 | for (GetElementPtrInst::op_iterator I = llvm::next(GEP->op_begin()), |
Dan Gohman | e810b0d | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2798 | E = GEP->op_end(); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2799 | I != E; ++I) { |
| 2800 | Value *Index = *I; |
| 2801 | // Compute the (potentially symbolic) offset in bytes for this index. |
| 2802 | if (const StructType *STy = dyn_cast<StructType>(*GTI++)) { |
| 2803 | // For a struct, add the member offset. |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2804 | unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue(); |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2805 | const SCEV *FieldOffset = getOffsetOfExpr(STy, FieldNo); |
| 2806 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2807 | // Add the field offset to the running total offset. |
Dan Gohman | 70eff63 | 2010-06-30 17:27:11 +0000 | [diff] [blame] | 2808 | TotalOffset = getAddExpr(TotalOffset, FieldOffset); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2809 | } else { |
| 2810 | // For an array, add the element offset, explicitly scaled. |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2811 | const SCEV *ElementSize = getSizeOfExpr(*GTI); |
| 2812 | const SCEV *IndexS = getSCEV(Index); |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 2813 | // Getelementptr indices are signed. |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2814 | IndexS = getTruncateOrSignExtend(IndexS, IntPtrTy); |
| 2815 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2816 | // Multiply the index by the element size to compute the element offset. |
Dan Gohman | 70eff63 | 2010-06-30 17:27:11 +0000 | [diff] [blame] | 2817 | const SCEV *LocalOffset = getMulExpr(IndexS, ElementSize); |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2818 | |
| 2819 | // Add the element offset to the running total offset. |
Dan Gohman | 70eff63 | 2010-06-30 17:27:11 +0000 | [diff] [blame] | 2820 | TotalOffset = getAddExpr(TotalOffset, LocalOffset); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2821 | } |
| 2822 | } |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2823 | |
| 2824 | // Get the SCEV for the GEP base. |
| 2825 | const SCEV *BaseS = getSCEV(Base); |
| 2826 | |
Dan Gohman | b9f9651 | 2010-06-30 07:16:37 +0000 | [diff] [blame] | 2827 | // Add the total offset from all the GEP indices to the base. |
Dan Gohman | 70eff63 | 2010-06-30 17:27:11 +0000 | [diff] [blame] | 2828 | return getAddExpr(BaseS, TotalOffset); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2831 | /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is |
| 2832 | /// guaranteed to end in (at every loop iteration). It is, at the same time, |
| 2833 | /// the minimum number of times S is divisible by 2. For example, given {4,+,8} |
| 2834 | /// 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] | 2835 | uint32_t |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2836 | ScalarEvolution::GetMinTrailingZeros(const SCEV *S) { |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2837 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Chris Lattner | 8314a0c | 2007-11-23 22:36:49 +0000 | [diff] [blame] | 2838 | return C->getValue()->getValue().countTrailingZeros(); |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2839 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2840 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S)) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2841 | return std::min(GetMinTrailingZeros(T->getOperand()), |
| 2842 | (uint32_t)getTypeSizeInBits(T->getType())); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2843 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2844 | if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) { |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2845 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 2846 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 2847 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2850 | if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) { |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2851 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 2852 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 2853 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2856 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2857 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2858 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2859 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2860 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2861 | return MinOpRes; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2864 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2865 | // The result is the sum of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2866 | uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0)); |
| 2867 | uint32_t BitWidth = getTypeSizeInBits(M->getType()); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2868 | for (unsigned i = 1, e = M->getNumOperands(); |
| 2869 | SumOpRes != BitWidth && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2870 | SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2871 | BitWidth); |
| 2872 | return SumOpRes; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2873 | } |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2874 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2875 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2876 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2877 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2878 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2879 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2880 | return MinOpRes; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2881 | } |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2882 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2883 | if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2884 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2885 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2886 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2887 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2888 | return MinOpRes; |
| 2889 | } |
| 2890 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2891 | if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2892 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2893 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2894 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2895 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2896 | return MinOpRes; |
| 2897 | } |
| 2898 | |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2899 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2900 | // For a SCEVUnknown, ask ValueTracking. |
| 2901 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2902 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 2903 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 2904 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones); |
| 2905 | return Zeros.countTrailingOnes(); |
| 2906 | } |
| 2907 | |
| 2908 | // SCEVUDivExpr |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2909 | return 0; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2910 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2911 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2912 | /// getUnsignedRange - Determine the unsigned range for a particular SCEV. |
| 2913 | /// |
| 2914 | ConstantRange |
| 2915 | ScalarEvolution::getUnsignedRange(const SCEV *S) { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 2916 | // See if we've computed this range already. |
| 2917 | DenseMap<const SCEV *, ConstantRange>::iterator I = UnsignedRanges.find(S); |
| 2918 | if (I != UnsignedRanges.end()) |
| 2919 | return I->second; |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2920 | |
| 2921 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2922 | return setUnsignedRange(C, ConstantRange(C->getValue()->getValue())); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2923 | |
Dan Gohman | c9c36cb | 2010-01-26 19:19:05 +0000 | [diff] [blame] | 2924 | unsigned BitWidth = getTypeSizeInBits(S->getType()); |
| 2925 | ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true); |
| 2926 | |
| 2927 | // If the value has known zeros, the maximum unsigned value will have those |
| 2928 | // known zeros as well. |
| 2929 | uint32_t TZ = GetMinTrailingZeros(S); |
| 2930 | if (TZ != 0) |
| 2931 | ConservativeResult = |
| 2932 | ConstantRange(APInt::getMinValue(BitWidth), |
| 2933 | APInt::getMaxValue(BitWidth).lshr(TZ).shl(TZ) + 1); |
| 2934 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2935 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 2936 | ConstantRange X = getUnsignedRange(Add->getOperand(0)); |
| 2937 | for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) |
| 2938 | X = X.add(getUnsignedRange(Add->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2939 | return setUnsignedRange(Add, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { |
| 2943 | ConstantRange X = getUnsignedRange(Mul->getOperand(0)); |
| 2944 | for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) |
| 2945 | X = X.multiply(getUnsignedRange(Mul->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2946 | return setUnsignedRange(Mul, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) { |
| 2950 | ConstantRange X = getUnsignedRange(SMax->getOperand(0)); |
| 2951 | for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) |
| 2952 | X = X.smax(getUnsignedRange(SMax->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2953 | return setUnsignedRange(SMax, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) { |
| 2957 | ConstantRange X = getUnsignedRange(UMax->getOperand(0)); |
| 2958 | for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) |
| 2959 | X = X.umax(getUnsignedRange(UMax->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2960 | return setUnsignedRange(UMax, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { |
| 2964 | ConstantRange X = getUnsignedRange(UDiv->getLHS()); |
| 2965 | ConstantRange Y = getUnsignedRange(UDiv->getRHS()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2966 | return setUnsignedRange(UDiv, ConservativeResult.intersectWith(X.udiv(Y))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2967 | } |
| 2968 | |
| 2969 | if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { |
| 2970 | ConstantRange X = getUnsignedRange(ZExt->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2971 | return setUnsignedRange(ZExt, |
| 2972 | ConservativeResult.intersectWith(X.zeroExtend(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
| 2975 | if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { |
| 2976 | ConstantRange X = getUnsignedRange(SExt->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2977 | return setUnsignedRange(SExt, |
| 2978 | ConservativeResult.intersectWith(X.signExtend(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
| 2981 | if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { |
| 2982 | ConstantRange X = getUnsignedRange(Trunc->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 2983 | return setUnsignedRange(Trunc, |
| 2984 | ConservativeResult.intersectWith(X.truncate(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2987 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 2988 | // If there's no unsigned wrap, the value will never be less than its |
| 2989 | // initial value. |
| 2990 | if (AddRec->hasNoUnsignedWrap()) |
| 2991 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(AddRec->getStart())) |
Dan Gohman | bca091d | 2010-04-12 23:08:18 +0000 | [diff] [blame] | 2992 | if (!C->getValue()->isZero()) |
Dan Gohman | bc7129f | 2010-04-11 22:12:18 +0000 | [diff] [blame] | 2993 | ConservativeResult = |
Dan Gohman | 8a18d6b | 2010-06-30 06:58:35 +0000 | [diff] [blame] | 2994 | ConservativeResult.intersectWith( |
| 2995 | ConstantRange(C->getValue()->getValue(), APInt(BitWidth, 0))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2996 | |
| 2997 | // TODO: non-affine addrec |
Dan Gohman | c9c36cb | 2010-01-26 19:19:05 +0000 | [diff] [blame] | 2998 | if (AddRec->isAffine()) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2999 | const Type *Ty = AddRec->getType(); |
| 3000 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop()); |
Dan Gohman | c9c36cb | 2010-01-26 19:19:05 +0000 | [diff] [blame] | 3001 | if (!isa<SCEVCouldNotCompute>(MaxBECount) && |
| 3002 | getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3003 | MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty); |
| 3004 | |
| 3005 | const SCEV *Start = AddRec->getStart(); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3006 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3007 | |
| 3008 | ConstantRange StartRange = getUnsignedRange(Start); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3009 | ConstantRange StepRange = getSignedRange(Step); |
| 3010 | ConstantRange MaxBECountRange = getUnsignedRange(MaxBECount); |
| 3011 | ConstantRange EndRange = |
| 3012 | StartRange.add(MaxBECountRange.multiply(StepRange)); |
| 3013 | |
| 3014 | // Check for overflow. This must be done with ConstantRange arithmetic |
| 3015 | // because we could be called from within the ScalarEvolution overflow |
| 3016 | // checking code. |
| 3017 | ConstantRange ExtStartRange = StartRange.zextOrTrunc(BitWidth*2+1); |
| 3018 | ConstantRange ExtStepRange = StepRange.sextOrTrunc(BitWidth*2+1); |
| 3019 | ConstantRange ExtMaxBECountRange = |
| 3020 | MaxBECountRange.zextOrTrunc(BitWidth*2+1); |
| 3021 | ConstantRange ExtEndRange = EndRange.zextOrTrunc(BitWidth*2+1); |
| 3022 | if (ExtStartRange.add(ExtMaxBECountRange.multiply(ExtStepRange)) != |
| 3023 | ExtEndRange) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3024 | return setUnsignedRange(AddRec, ConservativeResult); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3025 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3026 | APInt Min = APIntOps::umin(StartRange.getUnsignedMin(), |
| 3027 | EndRange.getUnsignedMin()); |
| 3028 | APInt Max = APIntOps::umax(StartRange.getUnsignedMax(), |
| 3029 | EndRange.getUnsignedMax()); |
| 3030 | if (Min.isMinValue() && Max.isMaxValue()) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3031 | return setUnsignedRange(AddRec, ConservativeResult); |
| 3032 | return setUnsignedRange(AddRec, |
| 3033 | ConservativeResult.intersectWith(ConstantRange(Min, Max+1))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3034 | } |
| 3035 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3036 | |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3037 | return setUnsignedRange(AddRec, ConservativeResult); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3038 | } |
| 3039 | |
| 3040 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 3041 | // For a SCEVUnknown, ask ValueTracking. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3042 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 3043 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 3044 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD); |
Dan Gohman | 746f3b1 | 2009-07-20 22:34:18 +0000 | [diff] [blame] | 3045 | if (Ones == ~Zeros + 1) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3046 | return setUnsignedRange(U, ConservativeResult); |
| 3047 | return setUnsignedRange(U, |
| 3048 | ConservativeResult.intersectWith(ConstantRange(Ones, ~Zeros + 1))); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3051 | return setUnsignedRange(S, ConservativeResult); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3052 | } |
| 3053 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3054 | /// getSignedRange - Determine the signed range for a particular SCEV. |
| 3055 | /// |
| 3056 | ConstantRange |
| 3057 | ScalarEvolution::getSignedRange(const SCEV *S) { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 3058 | DenseMap<const SCEV *, ConstantRange>::iterator I = SignedRanges.find(S); |
| 3059 | if (I != SignedRanges.end()) |
| 3060 | return I->second; |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3061 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3062 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3063 | return setSignedRange(C, ConstantRange(C->getValue()->getValue())); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3064 | |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 3065 | unsigned BitWidth = getTypeSizeInBits(S->getType()); |
| 3066 | ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true); |
| 3067 | |
| 3068 | // If the value has known zeros, the maximum signed value will have those |
| 3069 | // known zeros as well. |
| 3070 | uint32_t TZ = GetMinTrailingZeros(S); |
| 3071 | if (TZ != 0) |
| 3072 | ConservativeResult = |
| 3073 | ConstantRange(APInt::getSignedMinValue(BitWidth), |
| 3074 | APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1); |
| 3075 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3076 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 3077 | ConstantRange X = getSignedRange(Add->getOperand(0)); |
| 3078 | for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) |
| 3079 | X = X.add(getSignedRange(Add->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3080 | return setSignedRange(Add, ConservativeResult.intersectWith(X)); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3083 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { |
| 3084 | ConstantRange X = getSignedRange(Mul->getOperand(0)); |
| 3085 | for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) |
| 3086 | X = X.multiply(getSignedRange(Mul->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3087 | return setSignedRange(Mul, ConservativeResult.intersectWith(X)); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3088 | } |
| 3089 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3090 | if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) { |
| 3091 | ConstantRange X = getSignedRange(SMax->getOperand(0)); |
| 3092 | for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) |
| 3093 | X = X.smax(getSignedRange(SMax->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3094 | return setSignedRange(SMax, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3095 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3096 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3097 | if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) { |
| 3098 | ConstantRange X = getSignedRange(UMax->getOperand(0)); |
| 3099 | for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) |
| 3100 | X = X.umax(getSignedRange(UMax->getOperand(i))); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3101 | return setSignedRange(UMax, ConservativeResult.intersectWith(X)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3102 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3103 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3104 | if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { |
| 3105 | ConstantRange X = getSignedRange(UDiv->getLHS()); |
| 3106 | ConstantRange Y = getSignedRange(UDiv->getRHS()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3107 | return setSignedRange(UDiv, ConservativeResult.intersectWith(X.udiv(Y))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3108 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3109 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3110 | if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { |
| 3111 | ConstantRange X = getSignedRange(ZExt->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3112 | return setSignedRange(ZExt, |
| 3113 | ConservativeResult.intersectWith(X.zeroExtend(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { |
| 3117 | ConstantRange X = getSignedRange(SExt->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3118 | return setSignedRange(SExt, |
| 3119 | ConservativeResult.intersectWith(X.signExtend(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { |
| 3123 | ConstantRange X = getSignedRange(Trunc->getOperand()); |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3124 | return setSignedRange(Trunc, |
| 3125 | ConservativeResult.intersectWith(X.truncate(BitWidth))); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3128 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3129 | // If there's no signed wrap, and all the operands have the same sign or |
| 3130 | // zero, the value won't ever change sign. |
| 3131 | if (AddRec->hasNoSignedWrap()) { |
| 3132 | bool AllNonNeg = true; |
| 3133 | bool AllNonPos = true; |
| 3134 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { |
| 3135 | if (!isKnownNonNegative(AddRec->getOperand(i))) AllNonNeg = false; |
| 3136 | if (!isKnownNonPositive(AddRec->getOperand(i))) AllNonPos = false; |
| 3137 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3138 | if (AllNonNeg) |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 3139 | ConservativeResult = ConservativeResult.intersectWith( |
| 3140 | ConstantRange(APInt(BitWidth, 0), |
| 3141 | APInt::getSignedMinValue(BitWidth))); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3142 | else if (AllNonPos) |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 3143 | ConservativeResult = ConservativeResult.intersectWith( |
| 3144 | ConstantRange(APInt::getSignedMinValue(BitWidth), |
| 3145 | APInt(BitWidth, 1))); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3146 | } |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3147 | |
| 3148 | // TODO: non-affine addrec |
Dan Gohman | c9c36cb | 2010-01-26 19:19:05 +0000 | [diff] [blame] | 3149 | if (AddRec->isAffine()) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3150 | const Type *Ty = AddRec->getType(); |
| 3151 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop()); |
Dan Gohman | c9c36cb | 2010-01-26 19:19:05 +0000 | [diff] [blame] | 3152 | if (!isa<SCEVCouldNotCompute>(MaxBECount) && |
| 3153 | getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3154 | MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty); |
| 3155 | |
| 3156 | const SCEV *Start = AddRec->getStart(); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3157 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3158 | |
| 3159 | ConstantRange StartRange = getSignedRange(Start); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3160 | ConstantRange StepRange = getSignedRange(Step); |
| 3161 | ConstantRange MaxBECountRange = getUnsignedRange(MaxBECount); |
| 3162 | ConstantRange EndRange = |
| 3163 | StartRange.add(MaxBECountRange.multiply(StepRange)); |
| 3164 | |
| 3165 | // Check for overflow. This must be done with ConstantRange arithmetic |
| 3166 | // because we could be called from within the ScalarEvolution overflow |
| 3167 | // checking code. |
| 3168 | ConstantRange ExtStartRange = StartRange.sextOrTrunc(BitWidth*2+1); |
| 3169 | ConstantRange ExtStepRange = StepRange.sextOrTrunc(BitWidth*2+1); |
| 3170 | ConstantRange ExtMaxBECountRange = |
| 3171 | MaxBECountRange.zextOrTrunc(BitWidth*2+1); |
| 3172 | ConstantRange ExtEndRange = EndRange.sextOrTrunc(BitWidth*2+1); |
| 3173 | if (ExtStartRange.add(ExtMaxBECountRange.multiply(ExtStepRange)) != |
| 3174 | ExtEndRange) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3175 | return setSignedRange(AddRec, ConservativeResult); |
Dan Gohman | 646e047 | 2010-04-12 07:39:33 +0000 | [diff] [blame] | 3176 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3177 | APInt Min = APIntOps::smin(StartRange.getSignedMin(), |
| 3178 | EndRange.getSignedMin()); |
| 3179 | APInt Max = APIntOps::smax(StartRange.getSignedMax(), |
| 3180 | EndRange.getSignedMax()); |
| 3181 | if (Min.isMinSignedValue() && Max.isMaxSignedValue()) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3182 | return setSignedRange(AddRec, ConservativeResult); |
| 3183 | return setSignedRange(AddRec, |
| 3184 | ConservativeResult.intersectWith(ConstantRange(Min, Max+1))); |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3185 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3186 | } |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 3187 | |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3188 | return setSignedRange(AddRec, ConservativeResult); |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 3189 | } |
| 3190 | |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3191 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 3192 | // For a SCEVUnknown, ask ValueTracking. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3193 | if (!U->getValue()->getType()->isIntegerTy() && !TD) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3194 | return setSignedRange(U, ConservativeResult); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3195 | unsigned NS = ComputeNumSignBits(U->getValue(), TD); |
| 3196 | if (NS == 1) |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3197 | return setSignedRange(U, ConservativeResult); |
| 3198 | return setSignedRange(U, ConservativeResult.intersectWith( |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3199 | ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1), |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3200 | APInt::getSignedMaxValue(BitWidth).ashr(NS - 1)+1))); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
Dan Gohman | 7c0fd8e | 2010-11-17 20:23:08 +0000 | [diff] [blame] | 3203 | return setSignedRange(S, ConservativeResult); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3206 | /// createSCEV - We know that there is no SCEV for the specified value. |
| 3207 | /// Analyze the expression. |
| 3208 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3209 | const SCEV *ScalarEvolution::createSCEV(Value *V) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 3210 | if (!isSCEVable(V->getType())) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3211 | return getUnknown(V); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3212 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3213 | unsigned Opcode = Instruction::UserOp1; |
Dan Gohman | 4ecbca5 | 2010-03-09 23:46:50 +0000 | [diff] [blame] | 3214 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3215 | Opcode = I->getOpcode(); |
Dan Gohman | 4ecbca5 | 2010-03-09 23:46:50 +0000 | [diff] [blame] | 3216 | |
| 3217 | // Don't attempt to analyze instructions in blocks that aren't |
| 3218 | // reachable. Such instructions don't matter, and they aren't required |
| 3219 | // to obey basic rules for definitions dominating uses which this |
| 3220 | // analysis depends on. |
| 3221 | if (!DT->isReachableFromEntry(I->getParent())) |
| 3222 | return getUnknown(V); |
| 3223 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3224 | Opcode = CE->getOpcode(); |
Dan Gohman | 6bbcba1 | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 3225 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 3226 | return getConstant(CI); |
| 3227 | else if (isa<ConstantPointerNull>(V)) |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 3228 | return getConstant(V->getType(), 0); |
Dan Gohman | 2681232 | 2009-08-25 17:49:57 +0000 | [diff] [blame] | 3229 | else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) |
| 3230 | return GA->mayBeOverridden() ? getUnknown(V) : getSCEV(GA->getAliasee()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3231 | else |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3232 | return getUnknown(V); |
Chris Lattner | 2811f2a | 2007-04-02 05:41:38 +0000 | [diff] [blame] | 3233 | |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 3234 | Operator *U = cast<Operator>(V); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3235 | switch (Opcode) { |
Dan Gohman | d3f171d | 2010-08-16 16:03:49 +0000 | [diff] [blame] | 3236 | case Instruction::Add: { |
| 3237 | // The simple thing to do would be to just call getSCEV on both operands |
| 3238 | // and call getAddExpr with the result. However if we're looking at a |
| 3239 | // bunch of things all added together, this can be quite inefficient, |
| 3240 | // because it leads to N-1 getAddExpr calls for N ultimate operands. |
| 3241 | // Instead, gather up all the operands and make a single getAddExpr call. |
| 3242 | // LLVM IR canonical form means we need only traverse the left operands. |
| 3243 | SmallVector<const SCEV *, 4> AddOps; |
| 3244 | AddOps.push_back(getSCEV(U->getOperand(1))); |
Dan Gohman | 3f19c09 | 2010-08-31 22:53:17 +0000 | [diff] [blame] | 3245 | for (Value *Op = U->getOperand(0); ; Op = U->getOperand(0)) { |
| 3246 | unsigned Opcode = Op->getValueID() - Value::InstructionVal; |
| 3247 | if (Opcode != Instruction::Add && Opcode != Instruction::Sub) |
| 3248 | break; |
Dan Gohman | d3f171d | 2010-08-16 16:03:49 +0000 | [diff] [blame] | 3249 | U = cast<Operator>(Op); |
Dan Gohman | 3f19c09 | 2010-08-31 22:53:17 +0000 | [diff] [blame] | 3250 | const SCEV *Op1 = getSCEV(U->getOperand(1)); |
| 3251 | if (Opcode == Instruction::Sub) |
| 3252 | AddOps.push_back(getNegativeSCEV(Op1)); |
| 3253 | else |
| 3254 | AddOps.push_back(Op1); |
Dan Gohman | d3f171d | 2010-08-16 16:03:49 +0000 | [diff] [blame] | 3255 | } |
| 3256 | AddOps.push_back(getSCEV(U->getOperand(0))); |
| 3257 | return getAddExpr(AddOps); |
| 3258 | } |
| 3259 | case Instruction::Mul: { |
| 3260 | // See the Add code above. |
| 3261 | SmallVector<const SCEV *, 4> MulOps; |
| 3262 | MulOps.push_back(getSCEV(U->getOperand(1))); |
| 3263 | for (Value *Op = U->getOperand(0); |
| 3264 | Op->getValueID() == Instruction::Mul + Value::InstructionVal; |
| 3265 | Op = U->getOperand(0)) { |
| 3266 | U = cast<Operator>(Op); |
| 3267 | MulOps.push_back(getSCEV(U->getOperand(1))); |
| 3268 | } |
| 3269 | MulOps.push_back(getSCEV(U->getOperand(0))); |
| 3270 | return getMulExpr(MulOps); |
| 3271 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3272 | case Instruction::UDiv: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3273 | return getUDivExpr(getSCEV(U->getOperand(0)), |
| 3274 | getSCEV(U->getOperand(1))); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3275 | case Instruction::Sub: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3276 | return getMinusSCEV(getSCEV(U->getOperand(0)), |
| 3277 | getSCEV(U->getOperand(1))); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3278 | case Instruction::And: |
| 3279 | // For an expression like x&255 that merely masks off the high bits, |
| 3280 | // use zext(trunc(x)) as the SCEV expression. |
| 3281 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 2c73d5f | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 3282 | if (CI->isNullValue()) |
| 3283 | return getSCEV(U->getOperand(1)); |
Dan Gohman | d6c3295 | 2009-04-27 01:41:10 +0000 | [diff] [blame] | 3284 | if (CI->isAllOnesValue()) |
| 3285 | return getSCEV(U->getOperand(0)); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3286 | const APInt &A = CI->getValue(); |
Dan Gohman | 61ffa8e | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 3287 | |
| 3288 | // Instcombine's ShrinkDemandedConstant may strip bits out of |
| 3289 | // constants, obscuring what would otherwise be a low-bits mask. |
| 3290 | // Use ComputeMaskedBits to compute what ShrinkDemandedConstant |
| 3291 | // knew about to reconstruct a low-bits mask value. |
| 3292 | unsigned LZ = A.countLeadingZeros(); |
| 3293 | unsigned BitWidth = A.getBitWidth(); |
| 3294 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
| 3295 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 3296 | ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD); |
| 3297 | |
| 3298 | APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ); |
| 3299 | |
Dan Gohman | fc3641b | 2009-06-17 23:54:37 +0000 | [diff] [blame] | 3300 | if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask)) |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3301 | return |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3302 | getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3303 | IntegerType::get(getContext(), BitWidth - LZ)), |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3304 | U->getType()); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3305 | } |
| 3306 | break; |
Dan Gohman | 61ffa8e | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 3307 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3308 | case Instruction::Or: |
| 3309 | // If the RHS of the Or is a constant, we may have something like: |
| 3310 | // X*4+1 which got turned into X*4|1. Handle this as an Add so loop |
| 3311 | // optimizations will transparently handle this case. |
| 3312 | // |
| 3313 | // In order for this transformation to be safe, the LHS must be of the |
| 3314 | // form X*(2^n) and the Or constant must be less than 2^n. |
| 3315 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3316 | const SCEV *LHS = getSCEV(U->getOperand(0)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3317 | const APInt &CIVal = CI->getValue(); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3318 | if (GetMinTrailingZeros(LHS) >= |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 3319 | (CIVal.getBitWidth() - CIVal.countLeadingZeros())) { |
| 3320 | // Build a plain add SCEV. |
| 3321 | const SCEV *S = getAddExpr(LHS, getSCEV(CI)); |
| 3322 | // If the LHS of the add was an addrec and it has no-wrap flags, |
| 3323 | // transfer the no-wrap flags, since an or won't introduce a wrap. |
| 3324 | if (const SCEVAddRecExpr *NewAR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 3325 | const SCEVAddRecExpr *OldAR = cast<SCEVAddRecExpr>(LHS); |
| 3326 | if (OldAR->hasNoUnsignedWrap()) |
| 3327 | const_cast<SCEVAddRecExpr *>(NewAR)->setHasNoUnsignedWrap(true); |
| 3328 | if (OldAR->hasNoSignedWrap()) |
| 3329 | const_cast<SCEVAddRecExpr *>(NewAR)->setHasNoSignedWrap(true); |
| 3330 | } |
| 3331 | return S; |
| 3332 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3333 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3334 | break; |
| 3335 | case Instruction::Xor: |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3336 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3337 | // If the RHS of the xor is a signbit, then this is just an add. |
| 3338 | // Instcombine turns add of signbit into xor as a strength reduction step. |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3339 | if (CI->getValue().isSignBit()) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3340 | return getAddExpr(getSCEV(U->getOperand(0)), |
| 3341 | getSCEV(U->getOperand(1))); |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3342 | |
| 3343 | // 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] | 3344 | if (CI->isAllOnesValue()) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3345 | return getNotSCEV(getSCEV(U->getOperand(0))); |
Dan Gohman | 10978bd | 2009-05-18 16:29:04 +0000 | [diff] [blame] | 3346 | |
| 3347 | // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask. |
| 3348 | // This is a variant of the check for xor with -1, and it handles |
| 3349 | // the case where instcombine has trimmed non-demanded bits out |
| 3350 | // of an xor with -1. |
| 3351 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0))) |
| 3352 | if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1))) |
| 3353 | if (BO->getOpcode() == Instruction::And && |
| 3354 | LCI->getValue() == CI->getValue()) |
| 3355 | if (const SCEVZeroExtendExpr *Z = |
Dan Gohman | 3034c10 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 3356 | dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) { |
Dan Gohman | 8205283 | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 3357 | const Type *UTy = U->getType(); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3358 | const SCEV *Z0 = Z->getOperand(); |
Dan Gohman | 8205283 | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 3359 | const Type *Z0Ty = Z0->getType(); |
| 3360 | unsigned Z0TySize = getTypeSizeInBits(Z0Ty); |
| 3361 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 3362 | // 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] | 3363 | // mask off the high bits. Complement the operand and |
| 3364 | // re-apply the zext. |
| 3365 | if (APIntOps::isMask(Z0TySize, CI->getValue())) |
| 3366 | return getZeroExtendExpr(getNotSCEV(Z0), UTy); |
| 3367 | |
| 3368 | // If C is a single bit, it may be in the sign-bit position |
| 3369 | // before the zero-extend. In this case, represent the xor |
| 3370 | // using an add, which is equivalent, and re-apply the zext. |
| 3371 | APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize); |
| 3372 | if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() && |
| 3373 | Trunc.isSignBit()) |
| 3374 | return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)), |
| 3375 | UTy); |
Dan Gohman | 3034c10 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 3376 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3377 | } |
| 3378 | break; |
| 3379 | |
| 3380 | case Instruction::Shl: |
| 3381 | // Turn shift left of a constant amount into a multiply. |
| 3382 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 3383 | uint32_t BitWidth = cast<IntegerType>(U->getType())->getBitWidth(); |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3384 | |
| 3385 | // If the shift count is not less than the bitwidth, the result of |
| 3386 | // the shift is undefined. Don't try to analyze it, because the |
| 3387 | // resolution chosen here may differ from the resolution chosen in |
| 3388 | // other parts of the compiler. |
| 3389 | if (SA->getValue().uge(BitWidth)) |
| 3390 | break; |
| 3391 | |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3392 | Constant *X = ConstantInt::get(getContext(), |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3393 | APInt(BitWidth, 1).shl(SA->getZExtValue())); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3394 | return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3395 | } |
| 3396 | break; |
| 3397 | |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3398 | case Instruction::LShr: |
Nick Lewycky | 789558d | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 3399 | // Turn logical shift right of a constant into a unsigned divide. |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3400 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 3401 | uint32_t BitWidth = cast<IntegerType>(U->getType())->getBitWidth(); |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3402 | |
| 3403 | // If the shift count is not less than the bitwidth, the result of |
| 3404 | // the shift is undefined. Don't try to analyze it, because the |
| 3405 | // resolution chosen here may differ from the resolution chosen in |
| 3406 | // other parts of the compiler. |
| 3407 | if (SA->getValue().uge(BitWidth)) |
| 3408 | break; |
| 3409 | |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3410 | Constant *X = ConstantInt::get(getContext(), |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3411 | APInt(BitWidth, 1).shl(SA->getZExtValue())); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3412 | return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3413 | } |
| 3414 | break; |
| 3415 | |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3416 | case Instruction::AShr: |
| 3417 | // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression. |
| 3418 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3419 | if (Operator *L = dyn_cast<Operator>(U->getOperand(0))) |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3420 | if (L->getOpcode() == Instruction::Shl && |
| 3421 | L->getOperand(1) == U->getOperand(1)) { |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3422 | uint64_t BitWidth = getTypeSizeInBits(U->getType()); |
| 3423 | |
| 3424 | // If the shift count is not less than the bitwidth, the result of |
| 3425 | // the shift is undefined. Don't try to analyze it, because the |
| 3426 | // resolution chosen here may differ from the resolution chosen in |
| 3427 | // other parts of the compiler. |
| 3428 | if (CI->getValue().uge(BitWidth)) |
| 3429 | break; |
| 3430 | |
Dan Gohman | 2c73d5f | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 3431 | uint64_t Amt = BitWidth - CI->getZExtValue(); |
| 3432 | if (Amt == BitWidth) |
| 3433 | return getSCEV(L->getOperand(0)); // shift by zero --> noop |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3434 | return |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3435 | getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)), |
Dan Gohman | ddb3eaf | 2010-04-22 01:35:11 +0000 | [diff] [blame] | 3436 | IntegerType::get(getContext(), |
| 3437 | Amt)), |
| 3438 | U->getType()); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3439 | } |
| 3440 | break; |
| 3441 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3442 | case Instruction::Trunc: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3443 | return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3444 | |
| 3445 | case Instruction::ZExt: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3446 | return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3447 | |
| 3448 | case Instruction::SExt: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3449 | return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3450 | |
| 3451 | case Instruction::BitCast: |
| 3452 | // BitCasts are no-op casts so we just eliminate the cast. |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 3453 | if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3454 | return getSCEV(U->getOperand(0)); |
| 3455 | break; |
| 3456 | |
Dan Gohman | 4f8eea8 | 2010-02-01 18:27:38 +0000 | [diff] [blame] | 3457 | // It's tempting to handle inttoptr and ptrtoint as no-ops, however this can |
| 3458 | // lead to pointer expressions which cannot safely be expanded to GEPs, |
| 3459 | // because ScalarEvolution doesn't respect the GEP aliasing rules when |
| 3460 | // simplifying integer expressions. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3461 | |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 3462 | case Instruction::GetElementPtr: |
Dan Gohman | d281ed2 | 2009-12-18 02:09:29 +0000 | [diff] [blame] | 3463 | return createNodeForGEP(cast<GEPOperator>(U)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3464 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3465 | case Instruction::PHI: |
| 3466 | return createNodeForPHI(cast<PHINode>(U)); |
| 3467 | |
| 3468 | case Instruction::Select: |
| 3469 | // This could be a smax or umax that was lowered earlier. |
| 3470 | // Try to recover it. |
| 3471 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) { |
| 3472 | Value *LHS = ICI->getOperand(0); |
| 3473 | Value *RHS = ICI->getOperand(1); |
| 3474 | switch (ICI->getPredicate()) { |
| 3475 | case ICmpInst::ICMP_SLT: |
| 3476 | case ICmpInst::ICMP_SLE: |
| 3477 | std::swap(LHS, RHS); |
| 3478 | // fall through |
| 3479 | case ICmpInst::ICMP_SGT: |
| 3480 | case ICmpInst::ICMP_SGE: |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3481 | // a >s b ? a+x : b+x -> smax(a, b)+x |
| 3482 | // a >s b ? b+x : a+x -> smin(a, b)+x |
| 3483 | if (LHS->getType() == U->getType()) { |
| 3484 | const SCEV *LS = getSCEV(LHS); |
| 3485 | const SCEV *RS = getSCEV(RHS); |
| 3486 | const SCEV *LA = getSCEV(U->getOperand(1)); |
| 3487 | const SCEV *RA = getSCEV(U->getOperand(2)); |
| 3488 | const SCEV *LDiff = getMinusSCEV(LA, LS); |
| 3489 | const SCEV *RDiff = getMinusSCEV(RA, RS); |
| 3490 | if (LDiff == RDiff) |
| 3491 | return getAddExpr(getSMaxExpr(LS, RS), LDiff); |
| 3492 | LDiff = getMinusSCEV(LA, RS); |
| 3493 | RDiff = getMinusSCEV(RA, LS); |
| 3494 | if (LDiff == RDiff) |
| 3495 | return getAddExpr(getSMinExpr(LS, RS), LDiff); |
| 3496 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3497 | break; |
| 3498 | case ICmpInst::ICMP_ULT: |
| 3499 | case ICmpInst::ICMP_ULE: |
| 3500 | std::swap(LHS, RHS); |
| 3501 | // fall through |
| 3502 | case ICmpInst::ICMP_UGT: |
| 3503 | case ICmpInst::ICMP_UGE: |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3504 | // a >u b ? a+x : b+x -> umax(a, b)+x |
| 3505 | // a >u b ? b+x : a+x -> umin(a, b)+x |
| 3506 | if (LHS->getType() == U->getType()) { |
| 3507 | const SCEV *LS = getSCEV(LHS); |
| 3508 | const SCEV *RS = getSCEV(RHS); |
| 3509 | const SCEV *LA = getSCEV(U->getOperand(1)); |
| 3510 | const SCEV *RA = getSCEV(U->getOperand(2)); |
| 3511 | const SCEV *LDiff = getMinusSCEV(LA, LS); |
| 3512 | const SCEV *RDiff = getMinusSCEV(RA, RS); |
| 3513 | if (LDiff == RDiff) |
| 3514 | return getAddExpr(getUMaxExpr(LS, RS), LDiff); |
| 3515 | LDiff = getMinusSCEV(LA, RS); |
| 3516 | RDiff = getMinusSCEV(RA, LS); |
| 3517 | if (LDiff == RDiff) |
| 3518 | return getAddExpr(getUMinExpr(LS, RS), LDiff); |
| 3519 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3520 | break; |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3521 | case ICmpInst::ICMP_NE: |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3522 | // n != 0 ? n+x : 1+x -> umax(n, 1)+x |
| 3523 | if (LHS->getType() == U->getType() && |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3524 | isa<ConstantInt>(RHS) && |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3525 | cast<ConstantInt>(RHS)->isZero()) { |
| 3526 | const SCEV *One = getConstant(LHS->getType(), 1); |
| 3527 | const SCEV *LS = getSCEV(LHS); |
| 3528 | const SCEV *LA = getSCEV(U->getOperand(1)); |
| 3529 | const SCEV *RA = getSCEV(U->getOperand(2)); |
| 3530 | const SCEV *LDiff = getMinusSCEV(LA, LS); |
| 3531 | const SCEV *RDiff = getMinusSCEV(RA, One); |
| 3532 | if (LDiff == RDiff) |
Dan Gohman | 58a85b9 | 2010-08-13 20:17:14 +0000 | [diff] [blame] | 3533 | return getAddExpr(getUMaxExpr(One, LS), LDiff); |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3534 | } |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3535 | break; |
| 3536 | case ICmpInst::ICMP_EQ: |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3537 | // n == 0 ? 1+x : n+x -> umax(n, 1)+x |
| 3538 | if (LHS->getType() == U->getType() && |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3539 | isa<ConstantInt>(RHS) && |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3540 | cast<ConstantInt>(RHS)->isZero()) { |
| 3541 | const SCEV *One = getConstant(LHS->getType(), 1); |
| 3542 | const SCEV *LS = getSCEV(LHS); |
| 3543 | const SCEV *LA = getSCEV(U->getOperand(1)); |
| 3544 | const SCEV *RA = getSCEV(U->getOperand(2)); |
| 3545 | const SCEV *LDiff = getMinusSCEV(LA, One); |
| 3546 | const SCEV *RDiff = getMinusSCEV(RA, LS); |
| 3547 | if (LDiff == RDiff) |
Dan Gohman | 58a85b9 | 2010-08-13 20:17:14 +0000 | [diff] [blame] | 3548 | return getAddExpr(getUMaxExpr(One, LS), LDiff); |
Dan Gohman | 9f93d30 | 2010-04-24 03:09:42 +0000 | [diff] [blame] | 3549 | } |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3550 | break; |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3551 | default: |
| 3552 | break; |
| 3553 | } |
| 3554 | } |
| 3555 | |
| 3556 | default: // We cannot analyze this expression. |
| 3557 | break; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3560 | return getUnknown(V); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
| 3563 | |
| 3564 | |
| 3565 | //===----------------------------------------------------------------------===// |
| 3566 | // Iteration Count Computation Code |
| 3567 | // |
| 3568 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3569 | /// getBackedgeTakenCount - If the specified loop has a predictable |
| 3570 | /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute |
| 3571 | /// object. The backedge-taken count is the number of times the loop header |
| 3572 | /// will be branched to from within the loop. This is one less than the |
| 3573 | /// trip count of the loop, since it doesn't count the first iteration, |
| 3574 | /// when the header is branched to from outside the loop. |
| 3575 | /// |
| 3576 | /// Note that it is not valid to call this method on a loop without a |
| 3577 | /// loop-invariant backedge-taken count (see |
| 3578 | /// hasLoopInvariantBackedgeTakenCount). |
| 3579 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3580 | const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3581 | return getBackedgeTakenInfo(L).Exact; |
| 3582 | } |
| 3583 | |
| 3584 | /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except |
| 3585 | /// return the least SCEV value that is known never to be less than the |
| 3586 | /// actual backedge taken count. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3587 | const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3588 | return getBackedgeTakenInfo(L).Max; |
| 3589 | } |
| 3590 | |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3591 | /// PushLoopPHIs - Push PHI nodes in the header of the given loop |
| 3592 | /// onto the given Worklist. |
| 3593 | static void |
| 3594 | PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) { |
| 3595 | BasicBlock *Header = L->getHeader(); |
| 3596 | |
| 3597 | // Push all Loop-header PHIs onto the Worklist stack. |
| 3598 | for (BasicBlock::iterator I = Header->begin(); |
| 3599 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 3600 | Worklist.push_back(PN); |
| 3601 | } |
| 3602 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3603 | const ScalarEvolution::BackedgeTakenInfo & |
| 3604 | ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3605 | // Initially insert a CouldNotCompute for this loop. If the insertion |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 3606 | // succeeds, proceed to actually compute a backedge-taken count and |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3607 | // update the value. The temporary CouldNotCompute value tells SCEV |
| 3608 | // code elsewhere that it shouldn't attempt to request a new |
| 3609 | // backedge-taken count, which could result in infinite recursion. |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 3610 | std::pair<std::map<const Loop *, BackedgeTakenInfo>::iterator, bool> Pair = |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3611 | BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute())); |
| 3612 | if (Pair.second) { |
Dan Gohman | 93dacad | 2010-01-26 16:46:18 +0000 | [diff] [blame] | 3613 | BackedgeTakenInfo BECount = ComputeBackedgeTakenCount(L); |
| 3614 | if (BECount.Exact != getCouldNotCompute()) { |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 3615 | assert(isLoopInvariant(BECount.Exact, L) && |
| 3616 | isLoopInvariant(BECount.Max, L) && |
Dan Gohman | 93dacad | 2010-01-26 16:46:18 +0000 | [diff] [blame] | 3617 | "Computed backedge-taken count isn't loop invariant for loop!"); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3618 | ++NumTripCountsComputed; |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3619 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3620 | // Update the value in the map. |
Dan Gohman | 93dacad | 2010-01-26 16:46:18 +0000 | [diff] [blame] | 3621 | Pair.first->second = BECount; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3622 | } else { |
Dan Gohman | 93dacad | 2010-01-26 16:46:18 +0000 | [diff] [blame] | 3623 | if (BECount.Max != getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3624 | // Update the value in the map. |
Dan Gohman | 93dacad | 2010-01-26 16:46:18 +0000 | [diff] [blame] | 3625 | Pair.first->second = BECount; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3626 | if (isa<PHINode>(L->getHeader()->begin())) |
| 3627 | // Only count loops that have phi nodes as not being computable. |
| 3628 | ++NumTripCountsNotComputed; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3629 | } |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3630 | |
| 3631 | // Now that we know more about the trip count for this loop, forget any |
| 3632 | // existing SCEV values for PHI nodes in this loop since they are only |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3633 | // conservative estimates made without the benefit of trip count |
Dan Gohman | 4c7279a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 3634 | // information. This is similar to the code in forgetLoop, except that |
| 3635 | // it handles SCEVUnknown PHI nodes specially. |
Dan Gohman | 93dacad | 2010-01-26 16:46:18 +0000 | [diff] [blame] | 3636 | if (BECount.hasAnyInfo()) { |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3637 | SmallVector<Instruction *, 16> Worklist; |
| 3638 | PushLoopPHIs(L, Worklist); |
| 3639 | |
| 3640 | SmallPtrSet<Instruction *, 8> Visited; |
| 3641 | while (!Worklist.empty()) { |
| 3642 | Instruction *I = Worklist.pop_back_val(); |
| 3643 | if (!Visited.insert(I)) continue; |
| 3644 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3645 | ValueExprMapType::iterator It = |
| 3646 | ValueExprMap.find(static_cast<Value *>(I)); |
| 3647 | if (It != ValueExprMap.end()) { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 3648 | const SCEV *Old = It->second; |
| 3649 | |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3650 | // SCEVUnknown for a PHI either means that it has an unrecognized |
| 3651 | // structure, or it's a PHI that's in the progress of being computed |
Dan Gohman | ba70188 | 2009-07-13 22:04:06 +0000 | [diff] [blame] | 3652 | // by createNodeForPHI. In the former case, additional loop trip |
| 3653 | // count information isn't going to change anything. In the later |
| 3654 | // case, createNodeForPHI will perform the necessary updates on its |
| 3655 | // own when it gets to that point. |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 3656 | if (!isa<PHINode>(I) || !isa<SCEVUnknown>(Old)) { |
| 3657 | ValuesAtScopes.erase(Old); |
| 3658 | UnsignedRanges.erase(Old); |
| 3659 | SignedRanges.erase(Old); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3660 | ValueExprMap.erase(It); |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 3661 | } |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3662 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3663 | ConstantEvolutionLoopExitValue.erase(PN); |
| 3664 | } |
| 3665 | |
| 3666 | PushDefUseChildren(I, Worklist); |
| 3667 | } |
| 3668 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3669 | } |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3670 | return Pair.first->second; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
Dan Gohman | 4c7279a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 3673 | /// forgetLoop - This method should be called by the client when it has |
| 3674 | /// changed a loop in a way that may effect ScalarEvolution's ability to |
| 3675 | /// compute a trip count, or if the loop is deleted. |
| 3676 | void ScalarEvolution::forgetLoop(const Loop *L) { |
| 3677 | // Drop any stored trip count value. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3678 | BackedgeTakenCounts.erase(L); |
Dan Gohman | fb7d35f | 2009-05-02 17:43:35 +0000 | [diff] [blame] | 3679 | |
Dan Gohman | 4c7279a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 3680 | // Drop information about expressions based on loop-header PHIs. |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3681 | SmallVector<Instruction *, 16> Worklist; |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3682 | PushLoopPHIs(L, Worklist); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3683 | |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3684 | SmallPtrSet<Instruction *, 8> Visited; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3685 | while (!Worklist.empty()) { |
| 3686 | Instruction *I = Worklist.pop_back_val(); |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3687 | if (!Visited.insert(I)) continue; |
| 3688 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3689 | ValueExprMapType::iterator It = ValueExprMap.find(static_cast<Value *>(I)); |
| 3690 | if (It != ValueExprMap.end()) { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 3691 | const SCEV *Old = It->second; |
| 3692 | ValuesAtScopes.erase(Old); |
| 3693 | UnsignedRanges.erase(Old); |
| 3694 | SignedRanges.erase(Old); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3695 | ValueExprMap.erase(It); |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3696 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3697 | ConstantEvolutionLoopExitValue.erase(PN); |
| 3698 | } |
| 3699 | |
| 3700 | PushDefUseChildren(I, Worklist); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3701 | } |
Dan Gohman | e60dcb5 | 2010-10-29 20:16:10 +0000 | [diff] [blame] | 3702 | |
| 3703 | // Forget all contained loops too, to avoid dangling entries in the |
| 3704 | // ValuesAtScopes map. |
| 3705 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 3706 | forgetLoop(*I); |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
Eric Christopher | e6cbfa6 | 2010-07-29 01:25:38 +0000 | [diff] [blame] | 3709 | /// forgetValue - This method should be called by the client when it has |
| 3710 | /// changed a value in a way that may effect its value, or which may |
| 3711 | /// disconnect it from a def-use chain linking it to a loop. |
| 3712 | void ScalarEvolution::forgetValue(Value *V) { |
Dale Johannesen | 45a2d7d | 2010-02-19 07:14:22 +0000 | [diff] [blame] | 3713 | Instruction *I = dyn_cast<Instruction>(V); |
| 3714 | if (!I) return; |
| 3715 | |
| 3716 | // Drop information about expressions based on loop-header PHIs. |
| 3717 | SmallVector<Instruction *, 16> Worklist; |
| 3718 | Worklist.push_back(I); |
| 3719 | |
| 3720 | SmallPtrSet<Instruction *, 8> Visited; |
| 3721 | while (!Worklist.empty()) { |
| 3722 | I = Worklist.pop_back_val(); |
| 3723 | if (!Visited.insert(I)) continue; |
| 3724 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3725 | ValueExprMapType::iterator It = ValueExprMap.find(static_cast<Value *>(I)); |
| 3726 | if (It != ValueExprMap.end()) { |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 3727 | const SCEV *Old = It->second; |
| 3728 | ValuesAtScopes.erase(Old); |
| 3729 | UnsignedRanges.erase(Old); |
| 3730 | SignedRanges.erase(Old); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 3731 | ValueExprMap.erase(It); |
Dale Johannesen | 45a2d7d | 2010-02-19 07:14:22 +0000 | [diff] [blame] | 3732 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3733 | ConstantEvolutionLoopExitValue.erase(PN); |
| 3734 | } |
| 3735 | |
| 3736 | PushDefUseChildren(I, Worklist); |
| 3737 | } |
| 3738 | } |
| 3739 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3740 | /// ComputeBackedgeTakenCount - Compute the number of times the backedge |
| 3741 | /// of the specified loop will execute. |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3742 | ScalarEvolution::BackedgeTakenInfo |
| 3743 | ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 3744 | SmallVector<BasicBlock *, 8> ExitingBlocks; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3745 | L->getExitingBlocks(ExitingBlocks); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3746 | |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3747 | // Examine all exits and pick the most conservative values. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3748 | const SCEV *BECount = getCouldNotCompute(); |
| 3749 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3750 | bool CouldNotComputeBECount = false; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3751 | for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { |
| 3752 | BackedgeTakenInfo NewBTI = |
| 3753 | ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3754 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3755 | if (NewBTI.Exact == getCouldNotCompute()) { |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3756 | // We couldn't compute an exact value for this exit, so |
Dan Gohman | d32f5bf | 2009-06-22 21:10:22 +0000 | [diff] [blame] | 3757 | // 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] | 3758 | CouldNotComputeBECount = true; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3759 | BECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3760 | } else if (!CouldNotComputeBECount) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3761 | if (BECount == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3762 | BECount = NewBTI.Exact; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3763 | else |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3764 | BECount = getUMinFromMismatchedTypes(BECount, NewBTI.Exact); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3765 | } |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3766 | if (MaxBECount == getCouldNotCompute()) |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3767 | MaxBECount = NewBTI.Max; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3768 | else if (NewBTI.Max != getCouldNotCompute()) |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3769 | MaxBECount = getUMinFromMismatchedTypes(MaxBECount, NewBTI.Max); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3770 | } |
| 3771 | |
| 3772 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3773 | } |
| 3774 | |
| 3775 | /// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge |
| 3776 | /// of the specified loop will execute if it exits via the specified block. |
| 3777 | ScalarEvolution::BackedgeTakenInfo |
| 3778 | ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L, |
| 3779 | BasicBlock *ExitingBlock) { |
| 3780 | |
| 3781 | // Okay, we've chosen an exiting block. See what condition causes us to |
| 3782 | // exit at this block. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3783 | // |
| 3784 | // 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] | 3785 | BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3786 | if (ExitBr == 0) return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3787 | assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!"); |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3788 | |
Chris Lattner | 8b0e360 | 2007-01-07 02:24:26 +0000 | [diff] [blame] | 3789 | // At this point, we know we have a conditional branch that determines whether |
| 3790 | // the loop is exited. However, we don't know if the branch is executed each |
| 3791 | // time through the loop. If not, then the execution count of the branch will |
| 3792 | // not be equal to the trip count of the loop. |
| 3793 | // |
| 3794 | // Currently we check for this by checking to see if the Exit branch goes to |
| 3795 | // 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] | 3796 | // 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] | 3797 | // loop header. This is common for un-rotated loops. |
| 3798 | // |
| 3799 | // If both of those tests fail, walk up the unique predecessor chain to the |
| 3800 | // header, stopping if there is an edge that doesn't exit the loop. If the |
| 3801 | // header is reached, the execution count of the branch will be equal to the |
| 3802 | // trip count of the loop. |
| 3803 | // |
| 3804 | // More extensive analysis could be done to handle more cases here. |
| 3805 | // |
Chris Lattner | 8b0e360 | 2007-01-07 02:24:26 +0000 | [diff] [blame] | 3806 | if (ExitBr->getSuccessor(0) != L->getHeader() && |
Chris Lattner | 192e403 | 2007-01-14 01:24:47 +0000 | [diff] [blame] | 3807 | ExitBr->getSuccessor(1) != L->getHeader() && |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3808 | ExitBr->getParent() != L->getHeader()) { |
| 3809 | // The simple checks failed, try climbing the unique predecessor chain |
| 3810 | // up to the header. |
| 3811 | bool Ok = false; |
| 3812 | for (BasicBlock *BB = ExitBr->getParent(); BB; ) { |
| 3813 | BasicBlock *Pred = BB->getUniquePredecessor(); |
| 3814 | if (!Pred) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3815 | return getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3816 | TerminatorInst *PredTerm = Pred->getTerminator(); |
| 3817 | for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) { |
| 3818 | BasicBlock *PredSucc = PredTerm->getSuccessor(i); |
| 3819 | if (PredSucc == BB) |
| 3820 | continue; |
| 3821 | // If the predecessor has a successor that isn't BB and isn't |
| 3822 | // outside the loop, assume the worst. |
| 3823 | if (L->contains(PredSucc)) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3824 | return getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3825 | } |
| 3826 | if (Pred == L->getHeader()) { |
| 3827 | Ok = true; |
| 3828 | break; |
| 3829 | } |
| 3830 | BB = Pred; |
| 3831 | } |
| 3832 | if (!Ok) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3833 | return getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3834 | } |
| 3835 | |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 3836 | // Proceed to the next level to examine the exit condition expression. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3837 | return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(), |
| 3838 | ExitBr->getSuccessor(0), |
| 3839 | ExitBr->getSuccessor(1)); |
| 3840 | } |
| 3841 | |
| 3842 | /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the |
| 3843 | /// backedge of the specified loop will execute if its exit condition |
| 3844 | /// were a conditional branch of ExitCond, TBB, and FBB. |
| 3845 | ScalarEvolution::BackedgeTakenInfo |
| 3846 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L, |
| 3847 | Value *ExitCond, |
| 3848 | BasicBlock *TBB, |
| 3849 | BasicBlock *FBB) { |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3850 | // 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] | 3851 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) { |
| 3852 | if (BO->getOpcode() == Instruction::And) { |
| 3853 | // Recurse on the operands of the and. |
| 3854 | BackedgeTakenInfo BTI0 = |
| 3855 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 3856 | BackedgeTakenInfo BTI1 = |
| 3857 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3858 | const SCEV *BECount = getCouldNotCompute(); |
| 3859 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3860 | if (L->contains(TBB)) { |
| 3861 | // Both conditions must be true for the loop to continue executing. |
| 3862 | // Choose the less conservative count. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3863 | if (BTI0.Exact == getCouldNotCompute() || |
| 3864 | BTI1.Exact == getCouldNotCompute()) |
| 3865 | BECount = getCouldNotCompute(); |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3866 | else |
| 3867 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3868 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3869 | MaxBECount = BTI1.Max; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3870 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3871 | MaxBECount = BTI0.Max; |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3872 | else |
| 3873 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3874 | } else { |
Dan Gohman | 4ee8739 | 2010-08-11 00:12:36 +0000 | [diff] [blame] | 3875 | // Both conditions must be true at the same time for the loop to exit. |
| 3876 | // For now, be conservative. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3877 | assert(L->contains(FBB) && "Loop block has no successor in loop!"); |
Dan Gohman | 4ee8739 | 2010-08-11 00:12:36 +0000 | [diff] [blame] | 3878 | if (BTI0.Max == BTI1.Max) |
| 3879 | MaxBECount = BTI0.Max; |
| 3880 | if (BTI0.Exact == BTI1.Exact) |
| 3881 | BECount = BTI0.Exact; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
| 3884 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3885 | } |
| 3886 | if (BO->getOpcode() == Instruction::Or) { |
| 3887 | // Recurse on the operands of the or. |
| 3888 | BackedgeTakenInfo BTI0 = |
| 3889 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 3890 | BackedgeTakenInfo BTI1 = |
| 3891 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3892 | const SCEV *BECount = getCouldNotCompute(); |
| 3893 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3894 | if (L->contains(FBB)) { |
| 3895 | // Both conditions must be false for the loop to continue executing. |
| 3896 | // Choose the less conservative count. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3897 | if (BTI0.Exact == getCouldNotCompute() || |
| 3898 | BTI1.Exact == getCouldNotCompute()) |
| 3899 | BECount = getCouldNotCompute(); |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3900 | else |
| 3901 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3902 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3903 | MaxBECount = BTI1.Max; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3904 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3905 | MaxBECount = BTI0.Max; |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3906 | else |
| 3907 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3908 | } else { |
Dan Gohman | 4ee8739 | 2010-08-11 00:12:36 +0000 | [diff] [blame] | 3909 | // Both conditions must be false at the same time for the loop to exit. |
| 3910 | // For now, be conservative. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3911 | assert(L->contains(TBB) && "Loop block has no successor in loop!"); |
Dan Gohman | 4ee8739 | 2010-08-11 00:12:36 +0000 | [diff] [blame] | 3912 | if (BTI0.Max == BTI1.Max) |
| 3913 | MaxBECount = BTI0.Max; |
| 3914 | if (BTI0.Exact == BTI1.Exact) |
| 3915 | BECount = BTI0.Exact; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3919 | } |
| 3920 | } |
| 3921 | |
| 3922 | // 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] | 3923 | // Proceed to the next level to examine the icmp. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3924 | if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) |
| 3925 | return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3926 | |
Dan Gohman | 00cb5b7 | 2010-02-19 18:12:07 +0000 | [diff] [blame] | 3927 | // Check for a constant condition. These are normally stripped out by |
| 3928 | // SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to |
| 3929 | // preserve the CFG and is temporarily leaving constant conditions |
| 3930 | // in place. |
| 3931 | if (ConstantInt *CI = dyn_cast<ConstantInt>(ExitCond)) { |
| 3932 | if (L->contains(FBB) == !CI->getZExtValue()) |
| 3933 | // The backedge is always taken. |
| 3934 | return getCouldNotCompute(); |
| 3935 | else |
| 3936 | // The backedge is never taken. |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 3937 | return getConstant(CI->getType(), 0); |
Dan Gohman | 00cb5b7 | 2010-02-19 18:12:07 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
Eli Friedman | 361e54d | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3940 | // 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] | 3941 | return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
| 3942 | } |
| 3943 | |
| 3944 | /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the |
| 3945 | /// backedge of the specified loop will execute if its exit condition |
| 3946 | /// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB. |
| 3947 | ScalarEvolution::BackedgeTakenInfo |
| 3948 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L, |
| 3949 | ICmpInst *ExitCond, |
| 3950 | BasicBlock *TBB, |
| 3951 | BasicBlock *FBB) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3952 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3953 | // If the condition was exit on true, convert the condition to exit on false |
| 3954 | ICmpInst::Predicate Cond; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3955 | if (!L->contains(FBB)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3956 | Cond = ExitCond->getPredicate(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3957 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3958 | Cond = ExitCond->getInversePredicate(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3959 | |
| 3960 | // Handle common loops like: for (X = "string"; *X; ++X) |
| 3961 | if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0))) |
| 3962 | if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) { |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 3963 | BackedgeTakenInfo ItCnt = |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3964 | ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond); |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 3965 | if (ItCnt.hasAnyInfo()) |
| 3966 | return ItCnt; |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3967 | } |
| 3968 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3969 | const SCEV *LHS = getSCEV(ExitCond->getOperand(0)); |
| 3970 | const SCEV *RHS = getSCEV(ExitCond->getOperand(1)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3971 | |
| 3972 | // Try to evaluate any dependencies out of the loop. |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3973 | LHS = getSCEVAtScope(LHS, L); |
| 3974 | RHS = getSCEVAtScope(RHS, L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3975 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3976 | // 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] | 3977 | // loop the predicate will return true for these inputs. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 3978 | if (isLoopInvariant(LHS, L) && !isLoopInvariant(RHS, L)) { |
Dan Gohman | 70ff4cf | 2008-09-16 18:52:57 +0000 | [diff] [blame] | 3979 | // If there is a loop-invariant, force it into the RHS. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3980 | std::swap(LHS, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3981 | Cond = ICmpInst::getSwappedPredicate(Cond); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3982 | } |
| 3983 | |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 3984 | // Simplify the operands before analyzing them. |
| 3985 | (void)SimplifyICmpOperands(Cond, LHS, RHS); |
| 3986 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3987 | // If we have a comparison of a chrec against a constant, try to use value |
| 3988 | // ranges to answer this query. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3989 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) |
| 3990 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS)) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3991 | if (AddRec->getLoop() == L) { |
Eli Friedman | 361e54d | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3992 | // Form the constant range. |
| 3993 | ConstantRange CompRange( |
| 3994 | ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue())); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3995 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3996 | const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this); |
Eli Friedman | 361e54d | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3997 | if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3998 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3999 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4000 | switch (Cond) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4001 | case ICmpInst::ICMP_NE: { // while (X != Y) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4002 | // Convert to: while (X-Y != 0) |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 4003 | BackedgeTakenInfo BTI = HowFarToZero(getMinusSCEV(LHS, RHS), L); |
| 4004 | if (BTI.hasAnyInfo()) return BTI; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4005 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4006 | } |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 4007 | case ICmpInst::ICMP_EQ: { // while (X == Y) |
| 4008 | // Convert to: while (X-Y == 0) |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 4009 | BackedgeTakenInfo BTI = HowFarToNonZero(getMinusSCEV(LHS, RHS), L); |
| 4010 | if (BTI.hasAnyInfo()) return BTI; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4011 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4012 | } |
| 4013 | case ICmpInst::ICMP_SLT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4014 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true); |
| 4015 | if (BTI.hasAnyInfo()) return BTI; |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4016 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4017 | } |
| 4018 | case ICmpInst::ICMP_SGT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4019 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 4020 | getNotSCEV(RHS), L, true); |
| 4021 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | d6dac0e | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 4022 | break; |
| 4023 | } |
| 4024 | case ICmpInst::ICMP_ULT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4025 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false); |
| 4026 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | d6dac0e | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 4027 | break; |
| 4028 | } |
| 4029 | case ICmpInst::ICMP_UGT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4030 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 4031 | getNotSCEV(RHS), L, false); |
| 4032 | if (BTI.hasAnyInfo()) return BTI; |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4033 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4034 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4035 | default: |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 4036 | #if 0 |
David Greene | 25e0e87 | 2009-12-23 22:18:14 +0000 | [diff] [blame] | 4037 | dbgs() << "ComputeBackedgeTakenCount "; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4038 | if (ExitCond->getOperand(0)->getType()->isUnsigned()) |
David Greene | 25e0e87 | 2009-12-23 22:18:14 +0000 | [diff] [blame] | 4039 | dbgs() << "[unsigned] "; |
| 4040 | dbgs() << *LHS << " " |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4041 | << Instruction::getOpcodeName(Instruction::ICmp) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4042 | << " " << *RHS << "\n"; |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 4043 | #endif |
Chris Lattner | e34c0b4 | 2004-04-03 00:43:03 +0000 | [diff] [blame] | 4044 | break; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4045 | } |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4046 | return |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 4047 | ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4050 | static ConstantInt * |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4051 | EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, |
| 4052 | ScalarEvolution &SE) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4053 | const SCEV *InVal = SE.getConstant(C); |
| 4054 | const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4055 | assert(isa<SCEVConstant>(Val) && |
| 4056 | "Evaluation of SCEV at constant didn't fold correctly?"); |
| 4057 | return cast<SCEVConstant>(Val)->getValue(); |
| 4058 | } |
| 4059 | |
| 4060 | /// GetAddressedElementFromGlobal - Given a global variable with an initializer |
| 4061 | /// and a GEP expression (missing the pointer index) indexing into it, return |
| 4062 | /// the addressed element of the initializer or null if the index expression is |
| 4063 | /// invalid. |
| 4064 | static Constant * |
Nick Lewycky | c6501b1 | 2009-11-23 03:26:09 +0000 | [diff] [blame] | 4065 | GetAddressedElementFromGlobal(GlobalVariable *GV, |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4066 | const std::vector<ConstantInt*> &Indices) { |
| 4067 | Constant *Init = GV->getInitializer(); |
| 4068 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4069 | uint64_t Idx = Indices[i]->getZExtValue(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4070 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) { |
| 4071 | assert(Idx < CS->getNumOperands() && "Bad struct index!"); |
| 4072 | Init = cast<Constant>(CS->getOperand(Idx)); |
| 4073 | } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) { |
| 4074 | if (Idx >= CA->getNumOperands()) return 0; // Bogus program |
| 4075 | Init = cast<Constant>(CA->getOperand(Idx)); |
| 4076 | } else if (isa<ConstantAggregateZero>(Init)) { |
| 4077 | if (const StructType *STy = dyn_cast<StructType>(Init->getType())) { |
| 4078 | assert(Idx < STy->getNumElements() && "Bad struct index!"); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 4079 | Init = Constant::getNullValue(STy->getElementType(Idx)); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4080 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) { |
| 4081 | if (Idx >= ATy->getNumElements()) return 0; // Bogus program |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 4082 | Init = Constant::getNullValue(ATy->getElementType()); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4083 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4084 | llvm_unreachable("Unknown constant aggregate type!"); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4085 | } |
| 4086 | return 0; |
| 4087 | } else { |
| 4088 | return 0; // Unknown initializer type |
| 4089 | } |
| 4090 | } |
| 4091 | return Init; |
| 4092 | } |
| 4093 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4094 | /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of |
| 4095 | /// 'icmp op load X, cst', try to see if we can compute the backedge |
| 4096 | /// execution count. |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 4097 | ScalarEvolution::BackedgeTakenInfo |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4098 | ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount( |
| 4099 | LoadInst *LI, |
| 4100 | Constant *RHS, |
| 4101 | const Loop *L, |
| 4102 | ICmpInst::Predicate predicate) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4103 | if (LI->isVolatile()) return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4104 | |
| 4105 | // 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] | 4106 | // TODO: Use SCEV instead of manually grubbing with GEPs. |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4107 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4108 | if (!GEP) return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4109 | |
| 4110 | // Make sure that it is really a constant global we are gepping, with an |
| 4111 | // initializer, and make sure the first IDX is really 0. |
| 4112 | GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); |
Dan Gohman | 8255573 | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 4113 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() || |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4114 | GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) || |
| 4115 | !cast<Constant>(GEP->getOperand(1))->isNullValue()) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4116 | return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4117 | |
| 4118 | // Okay, we allow one non-constant index into the GEP instruction. |
| 4119 | Value *VarIdx = 0; |
| 4120 | std::vector<ConstantInt*> Indexes; |
| 4121 | unsigned VarIdxNum = 0; |
| 4122 | for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) |
| 4123 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 4124 | Indexes.push_back(CI); |
| 4125 | } else if (!isa<ConstantInt>(GEP->getOperand(i))) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4126 | if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's. |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4127 | VarIdx = GEP->getOperand(i); |
| 4128 | VarIdxNum = i-2; |
| 4129 | Indexes.push_back(0); |
| 4130 | } |
| 4131 | |
| 4132 | // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant. |
| 4133 | // Check to see if X is a loop variant variable value now. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4134 | const SCEV *Idx = getSCEV(VarIdx); |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 4135 | Idx = getSCEVAtScope(Idx, L); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4136 | |
| 4137 | // We can only recognize very limited forms of loop index expressions, in |
| 4138 | // particular, only affine AddRec's like {C1,+,C2}. |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4139 | const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx); |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 4140 | if (!IdxExpr || !IdxExpr->isAffine() || isLoopInvariant(IdxExpr, L) || |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4141 | !isa<SCEVConstant>(IdxExpr->getOperand(0)) || |
| 4142 | !isa<SCEVConstant>(IdxExpr->getOperand(1))) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4143 | return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4144 | |
| 4145 | unsigned MaxSteps = MaxBruteForceIterations; |
| 4146 | for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4147 | ConstantInt *ItCst = ConstantInt::get( |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 4148 | cast<IntegerType>(IdxExpr->getType()), IterationNum); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4149 | ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4150 | |
| 4151 | // Form the GEP offset. |
| 4152 | Indexes[VarIdxNum] = Val; |
| 4153 | |
Nick Lewycky | c6501b1 | 2009-11-23 03:26:09 +0000 | [diff] [blame] | 4154 | Constant *Result = GetAddressedElementFromGlobal(GV, Indexes); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4155 | if (Result == 0) break; // Cannot compute! |
| 4156 | |
| 4157 | // Evaluate the condition for this iteration. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4158 | Result = ConstantExpr::getICmp(predicate, Result, RHS); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4159 | if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4160 | if (cast<ConstantInt>(Result)->getValue().isMinValue()) { |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4161 | #if 0 |
David Greene | 25e0e87 | 2009-12-23 22:18:14 +0000 | [diff] [blame] | 4162 | dbgs() << "\n***\n*** Computed loop count " << *ItCst |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 4163 | << "\n*** From global " << *GV << "*** BB: " << *L->getHeader() |
| 4164 | << "***\n"; |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4165 | #endif |
| 4166 | ++NumArrayLenItCounts; |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4167 | return getConstant(ItCst); // Found terminating iteration! |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4168 | } |
| 4169 | } |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4170 | return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4174 | /// CanConstantFold - Return true if we can constant fold an instruction of the |
| 4175 | /// specified type, assuming that all operands were constants. |
| 4176 | static bool CanConstantFold(const Instruction *I) { |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 4177 | if (isa<BinaryOperator>(I) || isa<CmpInst>(I) || |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4178 | isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I)) |
| 4179 | return true; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4180 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4181 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 4182 | if (const Function *F = CI->getCalledFunction()) |
Dan Gohman | fa9b80e | 2008-01-31 01:05:10 +0000 | [diff] [blame] | 4183 | return canConstantFoldCallTo(F); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4184 | return false; |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4187 | /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node |
| 4188 | /// in the loop that V is derived from. We allow arbitrary operations along the |
| 4189 | /// way, but the operands of an operation must either be constants or a value |
| 4190 | /// derived from a constant PHI. If this expression does not fit with these |
| 4191 | /// constraints, return null. |
| 4192 | static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) { |
| 4193 | // If this is not an instruction, or if this is an instruction outside of the |
| 4194 | // loop, it can't be derived from a loop PHI. |
| 4195 | Instruction *I = dyn_cast<Instruction>(V); |
Dan Gohman | 92329c7 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 4196 | if (I == 0 || !L->contains(I)) return 0; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4197 | |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 4198 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4199 | if (L->getHeader() == I->getParent()) |
| 4200 | return PN; |
| 4201 | else |
| 4202 | // We don't currently keep track of the control flow needed to evaluate |
| 4203 | // PHIs, so we cannot handle PHIs inside of loops. |
| 4204 | return 0; |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 4205 | } |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4206 | |
| 4207 | // If we won't be able to constant fold this expression even if the operands |
| 4208 | // are constants, return early. |
| 4209 | if (!CanConstantFold(I)) return 0; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4210 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4211 | // Otherwise, we can evaluate this instruction if all of its operands are |
| 4212 | // constant or derived from a PHI node themselves. |
| 4213 | PHINode *PHI = 0; |
| 4214 | for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op) |
Dan Gohman | 9d4588f | 2010-06-22 13:15:46 +0000 | [diff] [blame] | 4215 | if (!isa<Constant>(I->getOperand(Op))) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4216 | PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L); |
| 4217 | if (P == 0) return 0; // Not evolving from PHI |
| 4218 | if (PHI == 0) |
| 4219 | PHI = P; |
| 4220 | else if (PHI != P) |
| 4221 | return 0; // Evolving from multiple different PHIs. |
| 4222 | } |
| 4223 | |
| 4224 | // This is a expression evolving from a constant PHI! |
| 4225 | return PHI; |
| 4226 | } |
| 4227 | |
| 4228 | /// EvaluateExpression - Given an expression that passes the |
| 4229 | /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node |
| 4230 | /// in the loop has the value PHIVal. If we can't fold this expression for some |
| 4231 | /// reason, return null. |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4232 | static Constant *EvaluateExpression(Value *V, Constant *PHIVal, |
| 4233 | const TargetData *TD) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4234 | if (isa<PHINode>(V)) return PHIVal; |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 4235 | if (Constant *C = dyn_cast<Constant>(V)) return C; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4236 | Instruction *I = cast<Instruction>(V); |
| 4237 | |
Dan Gohman | 9d4588f | 2010-06-22 13:15:46 +0000 | [diff] [blame] | 4238 | std::vector<Constant*> Operands(I->getNumOperands()); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4239 | |
| 4240 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4241 | Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal, TD); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4242 | if (Operands[i] == 0) return 0; |
| 4243 | } |
| 4244 | |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 4245 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
Chris Lattner | 8f73dea | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 4246 | return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0], |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4247 | Operands[1], TD); |
Chris Lattner | 8f73dea | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 4248 | return ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4249 | &Operands[0], Operands.size(), TD); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4250 | } |
| 4251 | |
| 4252 | /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is |
| 4253 | /// in the header of its containing loop, we know the loop executes a |
| 4254 | /// constant number of times, and the PHI node is just a recurrence |
| 4255 | /// involving constants, fold it. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4256 | Constant * |
| 4257 | ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN, |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 4258 | const APInt &BEs, |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4259 | const Loop *L) { |
Dan Gohman | 8d9c7a6 | 2010-08-16 16:30:01 +0000 | [diff] [blame] | 4260 | std::map<PHINode*, Constant*>::const_iterator I = |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4261 | ConstantEvolutionLoopExitValue.find(PN); |
| 4262 | if (I != ConstantEvolutionLoopExitValue.end()) |
| 4263 | return I->second; |
| 4264 | |
Dan Gohman | e056781 | 2010-04-08 23:03:40 +0000 | [diff] [blame] | 4265 | if (BEs.ugt(MaxBruteForceIterations)) |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4266 | return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it. |
| 4267 | |
| 4268 | Constant *&RetVal = ConstantEvolutionLoopExitValue[PN]; |
| 4269 | |
| 4270 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 4271 | // entry must be a constant (coming in from outside of the loop), and the |
| 4272 | // second must be derived from the same PHI. |
| 4273 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 4274 | Constant *StartCST = |
| 4275 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
| 4276 | if (StartCST == 0) |
| 4277 | return RetVal = 0; // Must be a constant. |
| 4278 | |
| 4279 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
Dan Gohman | 9d4588f | 2010-06-22 13:15:46 +0000 | [diff] [blame] | 4280 | if (getConstantEvolvingPHI(BEValue, L) != PN && |
| 4281 | !isa<Constant>(BEValue)) |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4282 | return RetVal = 0; // Not derived from same PHI. |
| 4283 | |
| 4284 | // Execute the loop symbolically to determine the exit value. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4285 | if (BEs.getActiveBits() >= 32) |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4286 | return RetVal = 0; // More than 2^32-1 iterations?? Not doing it! |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4287 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4288 | unsigned NumIterations = BEs.getZExtValue(); // must be in range |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4289 | unsigned IterationNum = 0; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4290 | for (Constant *PHIVal = StartCST; ; ++IterationNum) { |
| 4291 | if (IterationNum == NumIterations) |
| 4292 | return RetVal = PHIVal; // Got exit value! |
| 4293 | |
| 4294 | // Compute the value of the PHI node for the next iteration. |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4295 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal, TD); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4296 | if (NextPHI == PHIVal) |
| 4297 | return RetVal = NextPHI; // Stopped evolving! |
| 4298 | if (NextPHI == 0) |
| 4299 | return 0; // Couldn't evaluate! |
| 4300 | PHIVal = NextPHI; |
| 4301 | } |
| 4302 | } |
| 4303 | |
Dan Gohman | 07ad19b | 2009-07-27 16:09:48 +0000 | [diff] [blame] | 4304 | /// ComputeBackedgeTakenCountExhaustively - If the loop is known to execute a |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4305 | /// constant number of times (the condition evolves only from constants), |
| 4306 | /// try to evaluate a few iterations of the loop until we get the exit |
| 4307 | /// condition gets a value of ExitWhen (true or false). If we cannot |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4308 | /// evaluate the trip count of the loop, return getCouldNotCompute(). |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4309 | const SCEV * |
| 4310 | ScalarEvolution::ComputeBackedgeTakenCountExhaustively(const Loop *L, |
| 4311 | Value *Cond, |
| 4312 | bool ExitWhen) { |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4313 | PHINode *PN = getConstantEvolvingPHI(Cond, L); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4314 | if (PN == 0) return getCouldNotCompute(); |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4315 | |
Dan Gohman | b92654d | 2010-06-19 14:17:24 +0000 | [diff] [blame] | 4316 | // If the loop is canonicalized, the PHI will have exactly two entries. |
| 4317 | // That's the only form we support here. |
| 4318 | if (PN->getNumIncomingValues() != 2) return getCouldNotCompute(); |
| 4319 | |
| 4320 | // 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] | 4321 | // second must be derived from the same PHI. |
| 4322 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 4323 | Constant *StartCST = |
| 4324 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4325 | if (StartCST == 0) return getCouldNotCompute(); // Must be a constant. |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4326 | |
| 4327 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
Dan Gohman | 9d4588f | 2010-06-22 13:15:46 +0000 | [diff] [blame] | 4328 | if (getConstantEvolvingPHI(BEValue, L) != PN && |
| 4329 | !isa<Constant>(BEValue)) |
| 4330 | return getCouldNotCompute(); // Not derived from same PHI. |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4331 | |
| 4332 | // Okay, we find a PHI node that defines the trip count of this loop. Execute |
| 4333 | // the loop symbolically to determine when the condition gets a value of |
| 4334 | // "ExitWhen". |
| 4335 | unsigned IterationNum = 0; |
| 4336 | unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis. |
| 4337 | for (Constant *PHIVal = StartCST; |
| 4338 | IterationNum != MaxIterations; ++IterationNum) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4339 | ConstantInt *CondVal = |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4340 | dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal, TD)); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4341 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4342 | // Couldn't symbolically evaluate. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4343 | if (!CondVal) return getCouldNotCompute(); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4344 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4345 | if (CondVal->getValue() == uint64_t(ExitWhen)) { |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4346 | ++NumBruteForceTripCountsComputed; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4347 | return getConstant(Type::getInt32Ty(getContext()), IterationNum); |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4348 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4349 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4350 | // Compute the value of the PHI node for the next iteration. |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4351 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal, TD); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4352 | if (NextPHI == 0 || NextPHI == PHIVal) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4353 | return getCouldNotCompute();// Couldn't evaluate or not making progress... |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4354 | PHIVal = NextPHI; |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 4355 | } |
| 4356 | |
| 4357 | // Too many iterations were needed to evaluate. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4358 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
Dan Gohman | e7125f4 | 2009-09-03 15:00:26 +0000 | [diff] [blame] | 4361 | /// getSCEVAtScope - Return a SCEV expression for the specified value |
Dan Gohman | 66a7e85 | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 4362 | /// at the specified scope in the program. The L value specifies a loop |
| 4363 | /// nest to evaluate the expression at, where null is the top-level or a |
| 4364 | /// specified loop is immediately inside of the loop. |
| 4365 | /// |
| 4366 | /// This method can be used to compute the exit value for a variable defined |
| 4367 | /// in a loop by querying what the value will hold in the parent loop. |
| 4368 | /// |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 4369 | /// In the case that a relevant loop exit value cannot be computed, the |
| 4370 | /// original value V is returned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4371 | const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 4372 | // Check to see if we've folded this expression at this loop before. |
| 4373 | std::map<const Loop *, const SCEV *> &Values = ValuesAtScopes[V]; |
| 4374 | std::pair<std::map<const Loop *, const SCEV *>::iterator, bool> Pair = |
| 4375 | Values.insert(std::make_pair(L, static_cast<const SCEV *>(0))); |
| 4376 | if (!Pair.second) |
| 4377 | return Pair.first->second ? Pair.first->second : V; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4378 | |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 4379 | // Otherwise compute it. |
| 4380 | const SCEV *C = computeSCEVAtScope(V, L); |
Dan Gohman | a5505cb | 2009-08-31 21:58:28 +0000 | [diff] [blame] | 4381 | ValuesAtScopes[V][L] = C; |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 4382 | return C; |
| 4383 | } |
| 4384 | |
| 4385 | const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4386 | if (isa<SCEVConstant>(V)) return V; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4387 | |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 4388 | // If this instruction is evolved from a constant-evolving PHI, compute the |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4389 | // exit value from the loop without using SCEVs. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4390 | if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4391 | if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4392 | const Loop *LI = (*this->LI)[I->getParent()]; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4393 | if (LI && LI->getParentLoop() == L) // Looking for loop exit value. |
| 4394 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 4395 | if (PN->getParent() == LI->getHeader()) { |
| 4396 | // Okay, there is no closed form solution for the PHI node. Check |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4397 | // to see if the loop that contains it has a known backedge-taken |
| 4398 | // count. If so, we may be able to force computation of the exit |
| 4399 | // value. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4400 | const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4401 | if (const SCEVConstant *BTCC = |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4402 | dyn_cast<SCEVConstant>(BackedgeTakenCount)) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4403 | // Okay, we know how many times the containing loop executes. If |
| 4404 | // this is a constant evolving PHI node, get the final value at |
| 4405 | // the specified iteration number. |
| 4406 | Constant *RV = getConstantEvolutionLoopExitValue(PN, |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4407 | BTCC->getValue()->getValue(), |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4408 | LI); |
Dan Gohman | 0998796 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 4409 | if (RV) return getSCEV(RV); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4410 | } |
| 4411 | } |
| 4412 | |
Reid Spencer | 09906f3 | 2006-12-04 21:33:23 +0000 | [diff] [blame] | 4413 | // Okay, this is an expression that we cannot symbolically evaluate |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4414 | // 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] | 4415 | // the arguments into constants, and if so, try to constant propagate the |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4416 | // result. This is particularly useful for computing loop exit values. |
| 4417 | if (CanConstantFold(I)) { |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4418 | SmallVector<Constant *, 4> Operands; |
| 4419 | bool MadeImprovement = false; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4420 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 4421 | Value *Op = I->getOperand(i); |
| 4422 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| 4423 | Operands.push_back(C); |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4424 | continue; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4425 | } |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4426 | |
| 4427 | // If any of the operands is non-constant and if they are |
| 4428 | // non-integer and non-pointer, don't even try to analyze them |
| 4429 | // with scev techniques. |
| 4430 | if (!isSCEVable(Op->getType())) |
| 4431 | return V; |
| 4432 | |
| 4433 | const SCEV *OrigV = getSCEV(Op); |
| 4434 | const SCEV *OpV = getSCEVAtScope(OrigV, L); |
| 4435 | MadeImprovement |= OrigV != OpV; |
| 4436 | |
| 4437 | Constant *C = 0; |
| 4438 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) |
| 4439 | C = SC->getValue(); |
| 4440 | if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) |
| 4441 | C = dyn_cast<Constant>(SU->getValue()); |
| 4442 | if (!C) return V; |
| 4443 | if (C->getType() != Op->getType()) |
| 4444 | C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 4445 | Op->getType(), |
| 4446 | false), |
| 4447 | C, Op->getType()); |
| 4448 | Operands.push_back(C); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4449 | } |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4450 | |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4451 | // Check to see if getSCEVAtScope actually made an improvement. |
| 4452 | if (MadeImprovement) { |
| 4453 | Constant *C = 0; |
| 4454 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 4455 | C = ConstantFoldCompareInstOperands(CI->getPredicate(), |
| 4456 | Operands[0], Operands[1], TD); |
| 4457 | else |
| 4458 | C = ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
| 4459 | &Operands[0], Operands.size(), TD); |
| 4460 | if (!C) return V; |
Dan Gohman | e177c9a | 2010-02-24 19:31:47 +0000 | [diff] [blame] | 4461 | return getSCEV(C); |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4462 | } |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4463 | } |
| 4464 | } |
| 4465 | |
| 4466 | // This is some other type of SCEVUnknown, just return it. |
| 4467 | return V; |
| 4468 | } |
| 4469 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4470 | if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4471 | // Avoid performing the look-up in the common case where the specified |
| 4472 | // expression has no loop-variant portions. |
| 4473 | for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4474 | const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4475 | if (OpAtScope != Comm->getOperand(i)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4476 | // Okay, at least one of these operands is loop variant but might be |
| 4477 | // foldable. Build a new instance of the folded commutative expression. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4478 | SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(), |
| 4479 | Comm->op_begin()+i); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4480 | NewOps.push_back(OpAtScope); |
| 4481 | |
| 4482 | for (++i; i != e; ++i) { |
| 4483 | OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4484 | NewOps.push_back(OpAtScope); |
| 4485 | } |
| 4486 | if (isa<SCEVAddExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4487 | return getAddExpr(NewOps); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 4488 | if (isa<SCEVMulExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4489 | return getMulExpr(NewOps); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 4490 | if (isa<SCEVSMaxExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4491 | return getSMaxExpr(NewOps); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 4492 | if (isa<SCEVUMaxExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4493 | return getUMaxExpr(NewOps); |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4494 | llvm_unreachable("Unknown commutative SCEV type!"); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4495 | } |
| 4496 | } |
| 4497 | // If we got here, all operands are loop invariant. |
| 4498 | return Comm; |
| 4499 | } |
| 4500 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4501 | if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4502 | const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L); |
| 4503 | const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L); |
Nick Lewycky | 789558d | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 4504 | if (LHS == Div->getLHS() && RHS == Div->getRHS()) |
| 4505 | return Div; // must be loop invariant |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4506 | return getUDivExpr(LHS, RHS); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
| 4509 | // If this is a loop recurrence for a loop that does not contain L, then we |
| 4510 | // are dealing with the final value computed by the loop. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4511 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) { |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4512 | // First, attempt to evaluate each operand. |
| 4513 | // Avoid performing the look-up in the common case where the specified |
| 4514 | // expression has no loop-variant portions. |
| 4515 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { |
| 4516 | const SCEV *OpAtScope = getSCEVAtScope(AddRec->getOperand(i), L); |
| 4517 | if (OpAtScope == AddRec->getOperand(i)) |
| 4518 | continue; |
| 4519 | |
| 4520 | // Okay, at least one of these operands is loop variant but might be |
| 4521 | // foldable. Build a new instance of the folded commutative expression. |
| 4522 | SmallVector<const SCEV *, 8> NewOps(AddRec->op_begin(), |
| 4523 | AddRec->op_begin()+i); |
| 4524 | NewOps.push_back(OpAtScope); |
| 4525 | for (++i; i != e; ++i) |
| 4526 | NewOps.push_back(getSCEVAtScope(AddRec->getOperand(i), L)); |
| 4527 | |
| 4528 | AddRec = cast<SCEVAddRecExpr>(getAddRecExpr(NewOps, AddRec->getLoop())); |
| 4529 | break; |
| 4530 | } |
| 4531 | |
| 4532 | // If the scope is outside the addrec's loop, evaluate it by using the |
| 4533 | // loop exit value of the addrec. |
| 4534 | if (!AddRec->getLoop()->contains(L)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4535 | // To evaluate this recurrence, we need to know how many times the AddRec |
| 4536 | // loop iterates. Compute this now. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4537 | const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4538 | if (BackedgeTakenCount == getCouldNotCompute()) return AddRec; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4539 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 4540 | // Then, evaluate the AddRec. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4541 | return AddRec->evaluateAtIteration(BackedgeTakenCount, *this); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4542 | } |
Dan Gohman | 1104645 | 2010-06-29 23:43:06 +0000 | [diff] [blame] | 4543 | |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 4544 | return AddRec; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4545 | } |
| 4546 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4547 | if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4548 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | eb3948b | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4549 | if (Op == Cast->getOperand()) |
| 4550 | return Cast; // must be loop invariant |
| 4551 | return getZeroExtendExpr(Op, Cast->getType()); |
| 4552 | } |
| 4553 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4554 | if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4555 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | eb3948b | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4556 | if (Op == Cast->getOperand()) |
| 4557 | return Cast; // must be loop invariant |
| 4558 | return getSignExtendExpr(Op, Cast->getType()); |
| 4559 | } |
| 4560 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4561 | if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4562 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | eb3948b | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4563 | if (Op == Cast->getOperand()) |
| 4564 | return Cast; // must be loop invariant |
| 4565 | return getTruncateExpr(Op, Cast->getType()); |
| 4566 | } |
| 4567 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4568 | llvm_unreachable("Unknown SCEV type!"); |
Daniel Dunbar | 8c562e2 | 2009-05-18 16:43:04 +0000 | [diff] [blame] | 4569 | return 0; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4570 | } |
| 4571 | |
Dan Gohman | 66a7e85 | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 4572 | /// getSCEVAtScope - This is a convenience function which does |
| 4573 | /// getSCEVAtScope(getSCEV(V), L). |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4574 | const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4575 | return getSCEVAtScope(getSCEV(V), L); |
| 4576 | } |
| 4577 | |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4578 | /// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the |
| 4579 | /// following equation: |
| 4580 | /// |
| 4581 | /// A * X = B (mod N) |
| 4582 | /// |
| 4583 | /// where N = 2^BW and BW is the common bit width of A and B. The signedness of |
| 4584 | /// A and B isn't important. |
| 4585 | /// |
| 4586 | /// If the equation does not have a solution, SCEVCouldNotCompute is returned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4587 | static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const APInt &B, |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4588 | ScalarEvolution &SE) { |
| 4589 | uint32_t BW = A.getBitWidth(); |
| 4590 | assert(BW == B.getBitWidth() && "Bit widths must be the same."); |
| 4591 | assert(A != 0 && "A must be non-zero."); |
| 4592 | |
| 4593 | // 1. D = gcd(A, N) |
| 4594 | // |
| 4595 | // The gcd of A and N may have only one prime factor: 2. The number of |
| 4596 | // trailing zeros in A is its multiplicity |
| 4597 | uint32_t Mult2 = A.countTrailingZeros(); |
| 4598 | // D = 2^Mult2 |
| 4599 | |
| 4600 | // 2. Check if B is divisible by D. |
| 4601 | // |
| 4602 | // B is divisible by D if and only if the multiplicity of prime factor 2 for B |
| 4603 | // is not less than multiplicity of this prime factor for D. |
| 4604 | if (B.countTrailingZeros() < Mult2) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4605 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4606 | |
| 4607 | // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic |
| 4608 | // modulo (N / D). |
| 4609 | // |
| 4610 | // (N / D) may need BW+1 bits in its representation. Hence, we'll use this |
| 4611 | // bit width during computations. |
| 4612 | APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D |
| 4613 | APInt Mod(BW + 1, 0); |
| 4614 | Mod.set(BW - Mult2); // Mod = N / D |
| 4615 | APInt I = AD.multiplicativeInverse(Mod); |
| 4616 | |
| 4617 | // 4. Compute the minimum unsigned root of the equation: |
| 4618 | // I * (B / D) mod (N / D) |
| 4619 | APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod); |
| 4620 | |
| 4621 | // The result is guaranteed to be less than 2^BW so we may truncate it to BW |
| 4622 | // bits. |
| 4623 | return SE.getConstant(Result.trunc(BW)); |
| 4624 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4625 | |
| 4626 | /// SolveQuadraticEquation - Find the roots of the quadratic equation for the |
| 4627 | /// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which |
| 4628 | /// might be the same) or two SCEVCouldNotCompute objects. |
| 4629 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4630 | static std::pair<const SCEV *,const SCEV *> |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4631 | SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4632 | assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4633 | const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0)); |
| 4634 | const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1)); |
| 4635 | const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2)); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4636 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4637 | // We currently can only solve this if the coefficients are constants. |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4638 | if (!LC || !MC || !NC) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4639 | const SCEV *CNC = SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4640 | return std::make_pair(CNC, CNC); |
| 4641 | } |
| 4642 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4643 | uint32_t BitWidth = LC->getValue()->getValue().getBitWidth(); |
Chris Lattner | fe560b8 | 2007-04-15 19:52:49 +0000 | [diff] [blame] | 4644 | const APInt &L = LC->getValue()->getValue(); |
| 4645 | const APInt &M = MC->getValue()->getValue(); |
| 4646 | const APInt &N = NC->getValue()->getValue(); |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4647 | APInt Two(BitWidth, 2); |
| 4648 | APInt Four(BitWidth, 4); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4649 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4650 | { |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4651 | using namespace APIntOps; |
Zhou Sheng | 414de4d | 2007-04-07 17:48:27 +0000 | [diff] [blame] | 4652 | const APInt& C = L; |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4653 | // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C |
| 4654 | // The B coefficient is M-N/2 |
| 4655 | APInt B(M); |
| 4656 | B -= sdiv(N,Two); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4657 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4658 | // The A coefficient is N/2 |
Zhou Sheng | 414de4d | 2007-04-07 17:48:27 +0000 | [diff] [blame] | 4659 | APInt A(N.sdiv(Two)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4660 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4661 | // Compute the B^2-4ac term. |
| 4662 | APInt SqrtTerm(B); |
| 4663 | SqrtTerm *= B; |
| 4664 | SqrtTerm -= Four * (A * C); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4665 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4666 | // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest |
| 4667 | // integer value or else APInt::sqrt() will assert. |
| 4668 | APInt SqrtVal(SqrtTerm.sqrt()); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4669 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4670 | // Compute the two solutions for the quadratic formula. |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4671 | // The divisions must be performed as signed divisions. |
| 4672 | APInt NegB(-B); |
Reid Spencer | 3e35c8d | 2007-04-16 02:24:41 +0000 | [diff] [blame] | 4673 | APInt TwoA( A << 1 ); |
Nick Lewycky | 8f4d5eb | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 4674 | if (TwoA.isMinValue()) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4675 | const SCEV *CNC = SE.getCouldNotCompute(); |
Nick Lewycky | 8f4d5eb | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 4676 | return std::make_pair(CNC, CNC); |
| 4677 | } |
| 4678 | |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 4679 | LLVMContext &Context = SE.getContext(); |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4680 | |
| 4681 | ConstantInt *Solution1 = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4682 | ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA)); |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4683 | ConstantInt *Solution2 = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4684 | ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA)); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4685 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4686 | return std::make_pair(SE.getConstant(Solution1), |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4687 | SE.getConstant(Solution2)); |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4688 | } // end APIntOps namespace |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | /// HowFarToZero - Return the number of times a backedge comparing the specified |
Dan Gohman | 86fbf2f | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4692 | /// value to zero will execute. If not computable, return CouldNotCompute. |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 4693 | ScalarEvolution::BackedgeTakenInfo |
| 4694 | ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4695 | // If the value is a constant |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4696 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4697 | // If the value is already zero, the branch will execute zero times. |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 4698 | if (C->getValue()->isZero()) return C; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4699 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4700 | } |
| 4701 | |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4702 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4703 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4704 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4705 | |
| 4706 | if (AddRec->isAffine()) { |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4707 | // If this is an affine expression, the execution count of this branch is |
| 4708 | // the minimum unsigned root of the following equation: |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4709 | // |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4710 | // Start + Step*N = 0 (mod 2^BW) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4711 | // |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4712 | // equivalent to: |
| 4713 | // |
| 4714 | // Step*N = -Start (mod 2^BW) |
| 4715 | // |
| 4716 | // where BW is the common bit width of Start and Step. |
| 4717 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4718 | // Get the initial value for the loop. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4719 | const SCEV *Start = getSCEVAtScope(AddRec->getStart(), |
| 4720 | L->getParentLoop()); |
| 4721 | const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), |
| 4722 | L->getParentLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4723 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4724 | if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) { |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4725 | // For now we handle only constant steps. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4726 | |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4727 | // First, handle unitary steps. |
| 4728 | if (StepC->getValue()->equalsInt(1)) // 1*N = -Start (mod 2^BW), so: |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 4729 | return getNegativeSCEV(Start); // N = -Start (as unsigned) |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4730 | if (StepC->getValue()->isAllOnesValue()) // -1*N = -Start (mod 2^BW), so: |
| 4731 | return Start; // N = Start (as unsigned) |
| 4732 | |
| 4733 | // Then, try to solve the above equation provided that Start is constant. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4734 | if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4735 | return SolveLinEquationWithOverflow(StepC->getValue()->getValue(), |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4736 | -StartC->getValue()->getValue(), |
| 4737 | *this); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4738 | } |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 4739 | } else if (AddRec->isQuadratic() && AddRec->getType()->isIntegerTy()) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4740 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of |
| 4741 | // the quadratic equation to solve it. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4742 | std::pair<const SCEV *,const SCEV *> Roots = SolveQuadraticEquation(AddRec, |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4743 | *this); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4744 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 4745 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4746 | if (R1) { |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 4747 | #if 0 |
David Greene | 25e0e87 | 2009-12-23 22:18:14 +0000 | [diff] [blame] | 4748 | dbgs() << "HFTZ: " << *V << " - sol#1: " << *R1 |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 4749 | << " sol#2: " << *R2 << "\n"; |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 4750 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4751 | // Pick the smallest positive root value. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4752 | if (ConstantInt *CB = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 4753 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4754 | R1->getValue(), R2->getValue()))) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4755 | if (CB->getZExtValue() == false) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4756 | std::swap(R1, R2); // R1 is the minimum root now. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4757 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4758 | // We can only use this value if the chrec ends up with an exact zero |
| 4759 | // value at this index. When solving for "X*X != 5", for example, we |
| 4760 | // should not accept a root of 2. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4761 | const SCEV *Val = AddRec->evaluateAtIteration(R1, *this); |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 4762 | if (Val->isZero()) |
| 4763 | return R1; // We found a quadratic root! |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4764 | } |
| 4765 | } |
| 4766 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4767 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4768 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4769 | } |
| 4770 | |
| 4771 | /// HowFarToNonZero - Return the number of times a backedge checking the |
| 4772 | /// specified value for nonzero will execute. If not computable, return |
Dan Gohman | 86fbf2f | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4773 | /// CouldNotCompute |
Dan Gohman | f6d009f | 2010-02-24 17:31:30 +0000 | [diff] [blame] | 4774 | ScalarEvolution::BackedgeTakenInfo |
| 4775 | ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4776 | // Loops that look like: while (X == 0) are very strange indeed. We don't |
| 4777 | // handle them yet except for the trivial case. This could be expanded in the |
| 4778 | // future as needed. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4779 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4780 | // If the value is a constant, check to see if it is known to be non-zero |
| 4781 | // already. If so, the backedge will execute zero times. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4782 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Nick Lewycky | 39442af | 2008-02-21 09:14:53 +0000 | [diff] [blame] | 4783 | if (!C->getValue()->isNullValue()) |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 4784 | return getConstant(C->getType(), 0); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4785 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4786 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4787 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4788 | // We could implement others, but I really doubt anyone writes loops like |
| 4789 | // this, and if they did, they would already be constant folded. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4790 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4791 | } |
| 4792 | |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4793 | /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB |
| 4794 | /// (which may not be an immediate predecessor) which has exactly one |
| 4795 | /// successor from which BB is reachable, or null if no such block is |
| 4796 | /// found. |
| 4797 | /// |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 4798 | std::pair<BasicBlock *, BasicBlock *> |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4799 | ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) { |
Dan Gohman | 3d739fe | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 4800 | // If the block has a unique predecessor, then there is no path from the |
| 4801 | // predecessor to the block that does not go through the direct edge |
| 4802 | // from the predecessor to the block. |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4803 | if (BasicBlock *Pred = BB->getSinglePredecessor()) |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 4804 | return std::make_pair(Pred, BB); |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4805 | |
| 4806 | // 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] | 4807 | // If the header has a unique predecessor outside the loop, it must be |
| 4808 | // a block that has exactly one successor that can reach the loop. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4809 | if (Loop *L = LI->getLoopFor(BB)) |
Dan Gohman | 605c14f | 2010-06-22 23:43:28 +0000 | [diff] [blame] | 4810 | return std::make_pair(L->getLoopPredecessor(), L->getHeader()); |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4811 | |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 4812 | return std::pair<BasicBlock *, BasicBlock *>(); |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4813 | } |
| 4814 | |
Dan Gohman | 763bad1 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4815 | /// HasSameValue - SCEV structural equivalence is usually sufficient for |
| 4816 | /// testing whether two expressions are equal, however for the purposes of |
| 4817 | /// looking for a condition guarding a loop, it can be useful to be a little |
| 4818 | /// more general, since a front-end may have replicated the controlling |
| 4819 | /// expression. |
| 4820 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4821 | static bool HasSameValue(const SCEV *A, const SCEV *B) { |
Dan Gohman | 763bad1 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4822 | // Quick check to see if they are the same SCEV. |
| 4823 | if (A == B) return true; |
| 4824 | |
| 4825 | // Otherwise, if they're both SCEVUnknown, it's possible that they hold |
| 4826 | // two different instructions with the same value. Check for this case. |
| 4827 | if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A)) |
| 4828 | if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B)) |
| 4829 | if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue())) |
| 4830 | if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue())) |
Dan Gohman | 041de42 | 2009-08-25 17:56:57 +0000 | [diff] [blame] | 4831 | if (AI->isIdenticalTo(BI) && !AI->mayReadFromMemory()) |
Dan Gohman | 763bad1 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4832 | return true; |
| 4833 | |
| 4834 | // Otherwise assume they may have a different value. |
| 4835 | return false; |
| 4836 | } |
| 4837 | |
Dan Gohman | e979650 | 2010-04-24 01:28:42 +0000 | [diff] [blame] | 4838 | /// SimplifyICmpOperands - Simplify LHS and RHS in a comparison with |
| 4839 | /// predicate Pred. Return true iff any changes were made. |
| 4840 | /// |
| 4841 | bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred, |
| 4842 | const SCEV *&LHS, const SCEV *&RHS) { |
| 4843 | bool Changed = false; |
| 4844 | |
| 4845 | // Canonicalize a constant to the right side. |
| 4846 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { |
| 4847 | // Check for both operands constant. |
| 4848 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { |
| 4849 | if (ConstantExpr::getICmp(Pred, |
| 4850 | LHSC->getValue(), |
| 4851 | RHSC->getValue())->isNullValue()) |
| 4852 | goto trivially_false; |
| 4853 | else |
| 4854 | goto trivially_true; |
| 4855 | } |
| 4856 | // Otherwise swap the operands to put the constant on the right. |
| 4857 | std::swap(LHS, RHS); |
| 4858 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4859 | Changed = true; |
| 4860 | } |
| 4861 | |
| 4862 | // 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] | 4863 | // addrec's loop, put the addrec on the left. Also make a dominance check, |
| 4864 | // as both operands could be addrecs loop-invariant in each other's loop. |
| 4865 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(RHS)) { |
| 4866 | const Loop *L = AR->getLoop(); |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame^] | 4867 | if (isLoopInvariant(LHS, L) && properlyDominates(LHS, L->getHeader())) { |
Dan Gohman | e979650 | 2010-04-24 01:28:42 +0000 | [diff] [blame] | 4868 | std::swap(LHS, RHS); |
| 4869 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4870 | Changed = true; |
| 4871 | } |
Dan Gohman | 3abb69c | 2010-05-03 17:00:11 +0000 | [diff] [blame] | 4872 | } |
Dan Gohman | e979650 | 2010-04-24 01:28:42 +0000 | [diff] [blame] | 4873 | |
| 4874 | // If there's a constant operand, canonicalize comparisons with boundary |
| 4875 | // cases, and canonicalize *-or-equal comparisons to regular comparisons. |
| 4876 | if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) { |
| 4877 | const APInt &RA = RC->getValue()->getValue(); |
| 4878 | switch (Pred) { |
| 4879 | default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
| 4880 | case ICmpInst::ICMP_EQ: |
| 4881 | case ICmpInst::ICMP_NE: |
| 4882 | break; |
| 4883 | case ICmpInst::ICMP_UGE: |
| 4884 | if ((RA - 1).isMinValue()) { |
| 4885 | Pred = ICmpInst::ICMP_NE; |
| 4886 | RHS = getConstant(RA - 1); |
| 4887 | Changed = true; |
| 4888 | break; |
| 4889 | } |
| 4890 | if (RA.isMaxValue()) { |
| 4891 | Pred = ICmpInst::ICMP_EQ; |
| 4892 | Changed = true; |
| 4893 | break; |
| 4894 | } |
| 4895 | if (RA.isMinValue()) goto trivially_true; |
| 4896 | |
| 4897 | Pred = ICmpInst::ICMP_UGT; |
| 4898 | RHS = getConstant(RA - 1); |
| 4899 | Changed = true; |
| 4900 | break; |
| 4901 | case ICmpInst::ICMP_ULE: |
| 4902 | if ((RA + 1).isMaxValue()) { |
| 4903 | Pred = ICmpInst::ICMP_NE; |
| 4904 | RHS = getConstant(RA + 1); |
| 4905 | Changed = true; |
| 4906 | break; |
| 4907 | } |
| 4908 | if (RA.isMinValue()) { |
| 4909 | Pred = ICmpInst::ICMP_EQ; |
| 4910 | Changed = true; |
| 4911 | break; |
| 4912 | } |
| 4913 | if (RA.isMaxValue()) goto trivially_true; |
| 4914 | |
| 4915 | Pred = ICmpInst::ICMP_ULT; |
| 4916 | RHS = getConstant(RA + 1); |
| 4917 | Changed = true; |
| 4918 | break; |
| 4919 | case ICmpInst::ICMP_SGE: |
| 4920 | if ((RA - 1).isMinSignedValue()) { |
| 4921 | Pred = ICmpInst::ICMP_NE; |
| 4922 | RHS = getConstant(RA - 1); |
| 4923 | Changed = true; |
| 4924 | break; |
| 4925 | } |
| 4926 | if (RA.isMaxSignedValue()) { |
| 4927 | Pred = ICmpInst::ICMP_EQ; |
| 4928 | Changed = true; |
| 4929 | break; |
| 4930 | } |
| 4931 | if (RA.isMinSignedValue()) goto trivially_true; |
| 4932 | |
| 4933 | Pred = ICmpInst::ICMP_SGT; |
| 4934 | RHS = getConstant(RA - 1); |
| 4935 | Changed = true; |
| 4936 | break; |
| 4937 | case ICmpInst::ICMP_SLE: |
| 4938 | if ((RA + 1).isMaxSignedValue()) { |
| 4939 | Pred = ICmpInst::ICMP_NE; |
| 4940 | RHS = getConstant(RA + 1); |
| 4941 | Changed = true; |
| 4942 | break; |
| 4943 | } |
| 4944 | if (RA.isMinSignedValue()) { |
| 4945 | Pred = ICmpInst::ICMP_EQ; |
| 4946 | Changed = true; |
| 4947 | break; |
| 4948 | } |
| 4949 | if (RA.isMaxSignedValue()) goto trivially_true; |
| 4950 | |
| 4951 | Pred = ICmpInst::ICMP_SLT; |
| 4952 | RHS = getConstant(RA + 1); |
| 4953 | Changed = true; |
| 4954 | break; |
| 4955 | case ICmpInst::ICMP_UGT: |
| 4956 | if (RA.isMinValue()) { |
| 4957 | Pred = ICmpInst::ICMP_NE; |
| 4958 | Changed = true; |
| 4959 | break; |
| 4960 | } |
| 4961 | if ((RA + 1).isMaxValue()) { |
| 4962 | Pred = ICmpInst::ICMP_EQ; |
| 4963 | RHS = getConstant(RA + 1); |
| 4964 | Changed = true; |
| 4965 | break; |
| 4966 | } |
| 4967 | if (RA.isMaxValue()) goto trivially_false; |
| 4968 | break; |
| 4969 | case ICmpInst::ICMP_ULT: |
| 4970 | if (RA.isMaxValue()) { |
| 4971 | Pred = ICmpInst::ICMP_NE; |
| 4972 | Changed = true; |
| 4973 | break; |
| 4974 | } |
| 4975 | if ((RA - 1).isMinValue()) { |
| 4976 | Pred = ICmpInst::ICMP_EQ; |
| 4977 | RHS = getConstant(RA - 1); |
| 4978 | Changed = true; |
| 4979 | break; |
| 4980 | } |
| 4981 | if (RA.isMinValue()) goto trivially_false; |
| 4982 | break; |
| 4983 | case ICmpInst::ICMP_SGT: |
| 4984 | if (RA.isMinSignedValue()) { |
| 4985 | Pred = ICmpInst::ICMP_NE; |
| 4986 | Changed = true; |
| 4987 | break; |
| 4988 | } |
| 4989 | if ((RA + 1).isMaxSignedValue()) { |
| 4990 | Pred = ICmpInst::ICMP_EQ; |
| 4991 | RHS = getConstant(RA + 1); |
| 4992 | Changed = true; |
| 4993 | break; |
| 4994 | } |
| 4995 | if (RA.isMaxSignedValue()) goto trivially_false; |
| 4996 | break; |
| 4997 | case ICmpInst::ICMP_SLT: |
| 4998 | if (RA.isMaxSignedValue()) { |
| 4999 | Pred = ICmpInst::ICMP_NE; |
| 5000 | Changed = true; |
| 5001 | break; |
| 5002 | } |
| 5003 | if ((RA - 1).isMinSignedValue()) { |
| 5004 | Pred = ICmpInst::ICMP_EQ; |
| 5005 | RHS = getConstant(RA - 1); |
| 5006 | Changed = true; |
| 5007 | break; |
| 5008 | } |
| 5009 | if (RA.isMinSignedValue()) goto trivially_false; |
| 5010 | break; |
| 5011 | } |
| 5012 | } |
| 5013 | |
| 5014 | // Check for obvious equality. |
| 5015 | if (HasSameValue(LHS, RHS)) { |
| 5016 | if (ICmpInst::isTrueWhenEqual(Pred)) |
| 5017 | goto trivially_true; |
| 5018 | if (ICmpInst::isFalseWhenEqual(Pred)) |
| 5019 | goto trivially_false; |
| 5020 | } |
| 5021 | |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5022 | // If possible, canonicalize GE/LE comparisons to GT/LT comparisons, by |
| 5023 | // adding or subtracting 1 from one of the operands. |
| 5024 | switch (Pred) { |
| 5025 | case ICmpInst::ICMP_SLE: |
| 5026 | if (!getSignedRange(RHS).getSignedMax().isMaxSignedValue()) { |
| 5027 | RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS, |
| 5028 | /*HasNUW=*/false, /*HasNSW=*/true); |
| 5029 | Pred = ICmpInst::ICMP_SLT; |
| 5030 | Changed = true; |
| 5031 | } else if (!getSignedRange(LHS).getSignedMin().isMinSignedValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5032 | LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS, |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5033 | /*HasNUW=*/false, /*HasNSW=*/true); |
| 5034 | Pred = ICmpInst::ICMP_SLT; |
| 5035 | Changed = true; |
| 5036 | } |
| 5037 | break; |
| 5038 | case ICmpInst::ICMP_SGE: |
| 5039 | if (!getSignedRange(RHS).getSignedMin().isMinSignedValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5040 | RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS, |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5041 | /*HasNUW=*/false, /*HasNSW=*/true); |
| 5042 | Pred = ICmpInst::ICMP_SGT; |
| 5043 | Changed = true; |
| 5044 | } else if (!getSignedRange(LHS).getSignedMax().isMaxSignedValue()) { |
| 5045 | LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS, |
| 5046 | /*HasNUW=*/false, /*HasNSW=*/true); |
| 5047 | Pred = ICmpInst::ICMP_SGT; |
| 5048 | Changed = true; |
| 5049 | } |
| 5050 | break; |
| 5051 | case ICmpInst::ICMP_ULE: |
| 5052 | if (!getUnsignedRange(RHS).getUnsignedMax().isMaxValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5053 | RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS, |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5054 | /*HasNUW=*/true, /*HasNSW=*/false); |
| 5055 | Pred = ICmpInst::ICMP_ULT; |
| 5056 | Changed = true; |
| 5057 | } else if (!getUnsignedRange(LHS).getUnsignedMin().isMinValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5058 | LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS, |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5059 | /*HasNUW=*/true, /*HasNSW=*/false); |
| 5060 | Pred = ICmpInst::ICMP_ULT; |
| 5061 | Changed = true; |
| 5062 | } |
| 5063 | break; |
| 5064 | case ICmpInst::ICMP_UGE: |
| 5065 | if (!getUnsignedRange(RHS).getUnsignedMin().isMinValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5066 | RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS, |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5067 | /*HasNUW=*/true, /*HasNSW=*/false); |
| 5068 | Pred = ICmpInst::ICMP_UGT; |
| 5069 | Changed = true; |
| 5070 | } else if (!getUnsignedRange(LHS).getUnsignedMax().isMaxValue()) { |
Dan Gohman | f16c680 | 2010-05-03 20:23:47 +0000 | [diff] [blame] | 5071 | LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS, |
Dan Gohman | 03557dc | 2010-05-03 16:35:17 +0000 | [diff] [blame] | 5072 | /*HasNUW=*/true, /*HasNSW=*/false); |
| 5073 | Pred = ICmpInst::ICMP_UGT; |
| 5074 | Changed = true; |
| 5075 | } |
| 5076 | break; |
| 5077 | default: |
| 5078 | break; |
| 5079 | } |
| 5080 | |
Dan Gohman | e979650 | 2010-04-24 01:28:42 +0000 | [diff] [blame] | 5081 | // TODO: More simplifications are possible here. |
| 5082 | |
| 5083 | return Changed; |
| 5084 | |
| 5085 | trivially_true: |
| 5086 | // Return 0 == 0. |
| 5087 | LHS = RHS = getConstant(Type::getInt1Ty(getContext()), 0); |
| 5088 | Pred = ICmpInst::ICMP_EQ; |
| 5089 | return true; |
| 5090 | |
| 5091 | trivially_false: |
| 5092 | // Return 0 != 0. |
| 5093 | LHS = RHS = getConstant(Type::getInt1Ty(getContext()), 0); |
| 5094 | Pred = ICmpInst::ICMP_NE; |
| 5095 | return true; |
| 5096 | } |
| 5097 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5098 | bool ScalarEvolution::isKnownNegative(const SCEV *S) { |
| 5099 | return getSignedRange(S).getSignedMax().isNegative(); |
| 5100 | } |
| 5101 | |
| 5102 | bool ScalarEvolution::isKnownPositive(const SCEV *S) { |
| 5103 | return getSignedRange(S).getSignedMin().isStrictlyPositive(); |
| 5104 | } |
| 5105 | |
| 5106 | bool ScalarEvolution::isKnownNonNegative(const SCEV *S) { |
| 5107 | return !getSignedRange(S).getSignedMin().isNegative(); |
| 5108 | } |
| 5109 | |
| 5110 | bool ScalarEvolution::isKnownNonPositive(const SCEV *S) { |
| 5111 | return !getSignedRange(S).getSignedMax().isStrictlyPositive(); |
| 5112 | } |
| 5113 | |
| 5114 | bool ScalarEvolution::isKnownNonZero(const SCEV *S) { |
| 5115 | return isKnownNegative(S) || isKnownPositive(S); |
| 5116 | } |
| 5117 | |
| 5118 | bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred, |
| 5119 | const SCEV *LHS, const SCEV *RHS) { |
Dan Gohman | d19bba6 | 2010-04-24 01:38:36 +0000 | [diff] [blame] | 5120 | // Canonicalize the inputs first. |
| 5121 | (void)SimplifyICmpOperands(Pred, LHS, RHS); |
| 5122 | |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5123 | // If LHS or RHS is an addrec, check to see if the condition is true in |
| 5124 | // every iteration of the loop. |
| 5125 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) |
| 5126 | if (isLoopEntryGuardedByCond( |
| 5127 | AR->getLoop(), Pred, AR->getStart(), RHS) && |
| 5128 | isLoopBackedgeGuardedByCond( |
Dan Gohman | acd8cab | 2010-05-04 01:12:27 +0000 | [diff] [blame] | 5129 | AR->getLoop(), Pred, AR->getPostIncExpr(*this), RHS)) |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5130 | return true; |
| 5131 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(RHS)) |
| 5132 | if (isLoopEntryGuardedByCond( |
| 5133 | AR->getLoop(), Pred, LHS, AR->getStart()) && |
| 5134 | isLoopBackedgeGuardedByCond( |
Dan Gohman | acd8cab | 2010-05-04 01:12:27 +0000 | [diff] [blame] | 5135 | AR->getLoop(), Pred, LHS, AR->getPostIncExpr(*this))) |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5136 | return true; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5137 | |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5138 | // Otherwise see what can be done with known constant ranges. |
| 5139 | return isKnownPredicateWithRanges(Pred, LHS, RHS); |
| 5140 | } |
| 5141 | |
| 5142 | bool |
| 5143 | ScalarEvolution::isKnownPredicateWithRanges(ICmpInst::Predicate Pred, |
| 5144 | const SCEV *LHS, const SCEV *RHS) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5145 | if (HasSameValue(LHS, RHS)) |
| 5146 | return ICmpInst::isTrueWhenEqual(Pred); |
| 5147 | |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5148 | // This code is split out from isKnownPredicate because it is called from |
| 5149 | // within isLoopEntryGuardedByCond. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5150 | switch (Pred) { |
| 5151 | default: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5152 | llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5153 | break; |
| 5154 | case ICmpInst::ICMP_SGT: |
| 5155 | Pred = ICmpInst::ICMP_SLT; |
| 5156 | std::swap(LHS, RHS); |
| 5157 | case ICmpInst::ICMP_SLT: { |
| 5158 | ConstantRange LHSRange = getSignedRange(LHS); |
| 5159 | ConstantRange RHSRange = getSignedRange(RHS); |
| 5160 | if (LHSRange.getSignedMax().slt(RHSRange.getSignedMin())) |
| 5161 | return true; |
| 5162 | if (LHSRange.getSignedMin().sge(RHSRange.getSignedMax())) |
| 5163 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5164 | break; |
| 5165 | } |
| 5166 | case ICmpInst::ICMP_SGE: |
| 5167 | Pred = ICmpInst::ICMP_SLE; |
| 5168 | std::swap(LHS, RHS); |
| 5169 | case ICmpInst::ICMP_SLE: { |
| 5170 | ConstantRange LHSRange = getSignedRange(LHS); |
| 5171 | ConstantRange RHSRange = getSignedRange(RHS); |
| 5172 | if (LHSRange.getSignedMax().sle(RHSRange.getSignedMin())) |
| 5173 | return true; |
| 5174 | if (LHSRange.getSignedMin().sgt(RHSRange.getSignedMax())) |
| 5175 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5176 | break; |
| 5177 | } |
| 5178 | case ICmpInst::ICMP_UGT: |
| 5179 | Pred = ICmpInst::ICMP_ULT; |
| 5180 | std::swap(LHS, RHS); |
| 5181 | case ICmpInst::ICMP_ULT: { |
| 5182 | ConstantRange LHSRange = getUnsignedRange(LHS); |
| 5183 | ConstantRange RHSRange = getUnsignedRange(RHS); |
| 5184 | if (LHSRange.getUnsignedMax().ult(RHSRange.getUnsignedMin())) |
| 5185 | return true; |
| 5186 | if (LHSRange.getUnsignedMin().uge(RHSRange.getUnsignedMax())) |
| 5187 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5188 | break; |
| 5189 | } |
| 5190 | case ICmpInst::ICMP_UGE: |
| 5191 | Pred = ICmpInst::ICMP_ULE; |
| 5192 | std::swap(LHS, RHS); |
| 5193 | case ICmpInst::ICMP_ULE: { |
| 5194 | ConstantRange LHSRange = getUnsignedRange(LHS); |
| 5195 | ConstantRange RHSRange = getUnsignedRange(RHS); |
| 5196 | if (LHSRange.getUnsignedMax().ule(RHSRange.getUnsignedMin())) |
| 5197 | return true; |
| 5198 | if (LHSRange.getUnsignedMin().ugt(RHSRange.getUnsignedMax())) |
| 5199 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5200 | break; |
| 5201 | } |
| 5202 | case ICmpInst::ICMP_NE: { |
| 5203 | if (getUnsignedRange(LHS).intersectWith(getUnsignedRange(RHS)).isEmptySet()) |
| 5204 | return true; |
| 5205 | if (getSignedRange(LHS).intersectWith(getSignedRange(RHS)).isEmptySet()) |
| 5206 | return true; |
| 5207 | |
| 5208 | const SCEV *Diff = getMinusSCEV(LHS, RHS); |
| 5209 | if (isKnownNonZero(Diff)) |
| 5210 | return true; |
| 5211 | break; |
| 5212 | } |
| 5213 | case ICmpInst::ICMP_EQ: |
Dan Gohman | f117ed4 | 2009-07-20 23:54:43 +0000 | [diff] [blame] | 5214 | // The check at the top of the function catches the case where |
| 5215 | // the values are known to be equal. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5216 | break; |
| 5217 | } |
| 5218 | return false; |
| 5219 | } |
| 5220 | |
| 5221 | /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is |
| 5222 | /// protected by a conditional between LHS and RHS. This is used to |
| 5223 | /// to eliminate casts. |
| 5224 | bool |
| 5225 | ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L, |
| 5226 | ICmpInst::Predicate Pred, |
| 5227 | const SCEV *LHS, const SCEV *RHS) { |
| 5228 | // Interpret a null as meaning no loop, where there is obviously no guard |
| 5229 | // (interprocedural conditions notwithstanding). |
| 5230 | if (!L) return true; |
| 5231 | |
| 5232 | BasicBlock *Latch = L->getLoopLatch(); |
| 5233 | if (!Latch) |
| 5234 | return false; |
| 5235 | |
| 5236 | BranchInst *LoopContinuePredicate = |
| 5237 | dyn_cast<BranchInst>(Latch->getTerminator()); |
| 5238 | if (!LoopContinuePredicate || |
| 5239 | LoopContinuePredicate->isUnconditional()) |
| 5240 | return false; |
| 5241 | |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5242 | return isImpliedCond(Pred, LHS, RHS, |
| 5243 | LoopContinuePredicate->getCondition(), |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5244 | LoopContinuePredicate->getSuccessor(0) != L->getHeader()); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5245 | } |
| 5246 | |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 5247 | /// isLoopEntryGuardedByCond - Test whether entry to the loop is protected |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5248 | /// by a conditional between LHS and RHS. This is used to help avoid max |
| 5249 | /// expressions in loop trip counts, and to eliminate casts. |
| 5250 | bool |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 5251 | ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L, |
| 5252 | ICmpInst::Predicate Pred, |
| 5253 | const SCEV *LHS, const SCEV *RHS) { |
Dan Gohman | 8ea9452 | 2009-05-18 16:03:58 +0000 | [diff] [blame] | 5254 | // Interpret a null as meaning no loop, where there is obviously no guard |
| 5255 | // (interprocedural conditions notwithstanding). |
| 5256 | if (!L) return false; |
| 5257 | |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 5258 | // Starting at the loop predecessor, climb up the predecessor chain, as long |
| 5259 | // as there are predecessors that can be found that have unique successors |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 5260 | // leading to the original header. |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 5261 | for (std::pair<BasicBlock *, BasicBlock *> |
Dan Gohman | 605c14f | 2010-06-22 23:43:28 +0000 | [diff] [blame] | 5262 | Pair(L->getLoopPredecessor(), L->getHeader()); |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 5263 | Pair.first; |
| 5264 | Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) { |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 5265 | |
| 5266 | BranchInst *LoopEntryPredicate = |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 5267 | dyn_cast<BranchInst>(Pair.first->getTerminator()); |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 5268 | if (!LoopEntryPredicate || |
| 5269 | LoopEntryPredicate->isUnconditional()) |
| 5270 | continue; |
| 5271 | |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5272 | if (isImpliedCond(Pred, LHS, RHS, |
| 5273 | LoopEntryPredicate->getCondition(), |
Dan Gohman | 005752b | 2010-04-15 16:19:08 +0000 | [diff] [blame] | 5274 | LoopEntryPredicate->getSuccessor(0) != Pair.second)) |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 5275 | return true; |
Nick Lewycky | 59cff12 | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 5276 | } |
| 5277 | |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 5278 | return false; |
Nick Lewycky | 59cff12 | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 5279 | } |
| 5280 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5281 | /// isImpliedCond - Test whether the condition described by Pred, LHS, |
| 5282 | /// and RHS is true whenever the given Cond value evaluates to true. |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5283 | bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5284 | const SCEV *LHS, const SCEV *RHS, |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5285 | Value *FoundCondValue, |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5286 | bool Inverse) { |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 5287 | // Recursively handle And and Or conditions. |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5288 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FoundCondValue)) { |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 5289 | if (BO->getOpcode() == Instruction::And) { |
| 5290 | if (!Inverse) |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5291 | return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) || |
| 5292 | isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 5293 | } else if (BO->getOpcode() == Instruction::Or) { |
| 5294 | if (Inverse) |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5295 | return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) || |
| 5296 | isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 5297 | } |
| 5298 | } |
| 5299 | |
Dan Gohman | af08a36 | 2010-08-10 23:46:30 +0000 | [diff] [blame] | 5300 | ICmpInst *ICI = dyn_cast<ICmpInst>(FoundCondValue); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 5301 | if (!ICI) return false; |
| 5302 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5303 | // Bail if the ICmp's operands' types are wider than the needed type |
| 5304 | // before attempting to call getSCEV on them. This avoids infinite |
| 5305 | // recursion, since the analysis of widening casts can require loop |
| 5306 | // exit condition information for overflow checking, which would |
| 5307 | // lead back here. |
| 5308 | if (getTypeSizeInBits(LHS->getType()) < |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5309 | getTypeSizeInBits(ICI->getOperand(0)->getType())) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5310 | return false; |
| 5311 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5312 | // Now that we found a conditional branch that dominates the loop, check to |
| 5313 | // see if it is the comparison we are looking for. |
| 5314 | ICmpInst::Predicate FoundPred; |
| 5315 | if (Inverse) |
| 5316 | FoundPred = ICI->getInversePredicate(); |
| 5317 | else |
| 5318 | FoundPred = ICI->getPredicate(); |
| 5319 | |
| 5320 | const SCEV *FoundLHS = getSCEV(ICI->getOperand(0)); |
| 5321 | const SCEV *FoundRHS = getSCEV(ICI->getOperand(1)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5322 | |
| 5323 | // Balance the types. The case where FoundLHS' type is wider than |
| 5324 | // LHS' type is checked for above. |
| 5325 | if (getTypeSizeInBits(LHS->getType()) > |
| 5326 | getTypeSizeInBits(FoundLHS->getType())) { |
| 5327 | if (CmpInst::isSigned(Pred)) { |
| 5328 | FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType()); |
| 5329 | FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType()); |
| 5330 | } else { |
| 5331 | FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType()); |
| 5332 | FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType()); |
| 5333 | } |
| 5334 | } |
| 5335 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5336 | // Canonicalize the query to match the way instcombine will have |
| 5337 | // canonicalized the comparison. |
Dan Gohman | d4da5af | 2010-04-24 01:34:53 +0000 | [diff] [blame] | 5338 | if (SimplifyICmpOperands(Pred, LHS, RHS)) |
| 5339 | if (LHS == RHS) |
Dan Gohman | 34c3e36 | 2010-05-03 18:00:24 +0000 | [diff] [blame] | 5340 | return CmpInst::isTrueWhenEqual(Pred); |
Dan Gohman | d4da5af | 2010-04-24 01:34:53 +0000 | [diff] [blame] | 5341 | if (SimplifyICmpOperands(FoundPred, FoundLHS, FoundRHS)) |
| 5342 | if (FoundLHS == FoundRHS) |
Dan Gohman | 34c3e36 | 2010-05-03 18:00:24 +0000 | [diff] [blame] | 5343 | return CmpInst::isFalseWhenEqual(Pred); |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5344 | |
| 5345 | // Check to see if we can make the LHS or RHS match. |
| 5346 | if (LHS == FoundRHS || RHS == FoundLHS) { |
| 5347 | if (isa<SCEVConstant>(RHS)) { |
| 5348 | std::swap(FoundLHS, FoundRHS); |
| 5349 | FoundPred = ICmpInst::getSwappedPredicate(FoundPred); |
| 5350 | } else { |
| 5351 | std::swap(LHS, RHS); |
| 5352 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 5353 | } |
| 5354 | } |
| 5355 | |
| 5356 | // Check whether the found predicate is the same as the desired predicate. |
| 5357 | if (FoundPred == Pred) |
| 5358 | return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS); |
| 5359 | |
| 5360 | // Check whether swapping the found predicate makes it the same as the |
| 5361 | // desired predicate. |
| 5362 | if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) { |
| 5363 | if (isa<SCEVConstant>(RHS)) |
| 5364 | return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS); |
| 5365 | else |
| 5366 | return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred), |
| 5367 | RHS, LHS, FoundLHS, FoundRHS); |
| 5368 | } |
| 5369 | |
| 5370 | // Check whether the actual condition is beyond sufficient. |
| 5371 | if (FoundPred == ICmpInst::ICMP_EQ) |
| 5372 | if (ICmpInst::isTrueWhenEqual(Pred)) |
| 5373 | if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS)) |
| 5374 | return true; |
| 5375 | if (Pred == ICmpInst::ICMP_NE) |
| 5376 | if (!ICmpInst::isTrueWhenEqual(FoundPred)) |
| 5377 | if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS)) |
| 5378 | return true; |
| 5379 | |
| 5380 | // Otherwise assume the worst. |
| 5381 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5382 | } |
| 5383 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5384 | /// isImpliedCondOperands - Test whether the condition described by Pred, |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 5385 | /// LHS, and RHS is true whenever the condition described by Pred, FoundLHS, |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5386 | /// and FoundRHS is true. |
| 5387 | bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred, |
| 5388 | const SCEV *LHS, const SCEV *RHS, |
| 5389 | const SCEV *FoundLHS, |
| 5390 | const SCEV *FoundRHS) { |
| 5391 | return isImpliedCondOperandsHelper(Pred, LHS, RHS, |
| 5392 | FoundLHS, FoundRHS) || |
| 5393 | // ~x < ~y --> x > y |
| 5394 | isImpliedCondOperandsHelper(Pred, LHS, RHS, |
| 5395 | getNotSCEV(FoundRHS), |
| 5396 | getNotSCEV(FoundLHS)); |
| 5397 | } |
| 5398 | |
| 5399 | /// isImpliedCondOperandsHelper - Test whether the condition described by |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 5400 | /// Pred, LHS, and RHS is true whenever the condition described by Pred, |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5401 | /// FoundLHS, and FoundRHS is true. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5402 | bool |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 5403 | ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred, |
| 5404 | const SCEV *LHS, const SCEV *RHS, |
| 5405 | const SCEV *FoundLHS, |
| 5406 | const SCEV *FoundRHS) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5407 | switch (Pred) { |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5408 | default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
| 5409 | case ICmpInst::ICMP_EQ: |
| 5410 | case ICmpInst::ICMP_NE: |
| 5411 | if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS)) |
| 5412 | return true; |
| 5413 | break; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5414 | case ICmpInst::ICMP_SLT: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5415 | case ICmpInst::ICMP_SLE: |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5416 | if (isKnownPredicateWithRanges(ICmpInst::ICMP_SLE, LHS, FoundLHS) && |
| 5417 | isKnownPredicateWithRanges(ICmpInst::ICMP_SGE, RHS, FoundRHS)) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5418 | return true; |
| 5419 | break; |
| 5420 | case ICmpInst::ICMP_SGT: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5421 | case ICmpInst::ICMP_SGE: |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5422 | if (isKnownPredicateWithRanges(ICmpInst::ICMP_SGE, LHS, FoundLHS) && |
| 5423 | isKnownPredicateWithRanges(ICmpInst::ICMP_SLE, RHS, FoundRHS)) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5424 | return true; |
| 5425 | break; |
| 5426 | case ICmpInst::ICMP_ULT: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5427 | case ICmpInst::ICMP_ULE: |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5428 | if (isKnownPredicateWithRanges(ICmpInst::ICMP_ULE, LHS, FoundLHS) && |
| 5429 | isKnownPredicateWithRanges(ICmpInst::ICMP_UGE, RHS, FoundRHS)) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5430 | return true; |
| 5431 | break; |
| 5432 | case ICmpInst::ICMP_UGT: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 5433 | case ICmpInst::ICMP_UGE: |
Dan Gohman | 53c66ea | 2010-04-11 22:16:48 +0000 | [diff] [blame] | 5434 | if (isKnownPredicateWithRanges(ICmpInst::ICMP_UGE, LHS, FoundLHS) && |
| 5435 | isKnownPredicateWithRanges(ICmpInst::ICMP_ULE, RHS, FoundRHS)) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5436 | return true; |
| 5437 | break; |
| 5438 | } |
| 5439 | |
| 5440 | return false; |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 5441 | } |
| 5442 | |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 5443 | /// getBECount - Subtract the end and start values and divide by the step, |
| 5444 | /// rounding up, to get the number of times the backedge is executed. Return |
| 5445 | /// CouldNotCompute if an intermediate computation overflows. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5446 | const SCEV *ScalarEvolution::getBECount(const SCEV *Start, |
Dan Gohman | f5074ec | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 5447 | const SCEV *End, |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 5448 | const SCEV *Step, |
| 5449 | bool NoWrap) { |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5450 | assert(!isKnownNegative(Step) && |
| 5451 | "This code doesn't handle negative strides yet!"); |
| 5452 | |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 5453 | const Type *Ty = Start->getType(); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 5454 | const SCEV *NegOne = getConstant(Ty, (uint64_t)-1); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5455 | const SCEV *Diff = getMinusSCEV(End, Start); |
| 5456 | const SCEV *RoundUp = getAddExpr(Step, NegOne); |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 5457 | |
| 5458 | // Add an adjustment to the difference between End and Start so that |
| 5459 | // the division will effectively round up. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5460 | const SCEV *Add = getAddExpr(Diff, RoundUp); |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 5461 | |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 5462 | if (!NoWrap) { |
| 5463 | // Check Add for unsigned overflow. |
| 5464 | // TODO: More sophisticated things could be done here. |
| 5465 | const Type *WideTy = IntegerType::get(getContext(), |
| 5466 | getTypeSizeInBits(Ty) + 1); |
| 5467 | const SCEV *EDiff = getZeroExtendExpr(Diff, WideTy); |
| 5468 | const SCEV *ERoundUp = getZeroExtendExpr(RoundUp, WideTy); |
| 5469 | const SCEV *OperandExtendedAdd = getAddExpr(EDiff, ERoundUp); |
| 5470 | if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd) |
| 5471 | return getCouldNotCompute(); |
| 5472 | } |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 5473 | |
| 5474 | return getUDivExpr(Add, Step); |
| 5475 | } |
| 5476 | |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5477 | /// HowManyLessThans - Return the number of times a backedge containing the |
| 5478 | /// specified less-than comparison will execute. If not computable, return |
Dan Gohman | 86fbf2f | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 5479 | /// CouldNotCompute. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 5480 | ScalarEvolution::BackedgeTakenInfo |
| 5481 | ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS, |
| 5482 | const Loop *L, bool isSigned) { |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5483 | // Only handle: "ADDREC < LoopInvariant". |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5484 | if (!isLoopInvariant(RHS, L)) return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5485 | |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5486 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5487 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5488 | return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5489 | |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 5490 | // Check to see if we have a flag which makes analysis easy. |
| 5491 | bool NoWrap = isSigned ? AddRec->hasNoSignedWrap() : |
| 5492 | AddRec->hasNoUnsignedWrap(); |
| 5493 | |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5494 | if (AddRec->isAffine()) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5495 | unsigned BitWidth = getTypeSizeInBits(AddRec->getType()); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5496 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5497 | |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5498 | if (Step->isZero()) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5499 | return getCouldNotCompute(); |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5500 | if (Step->isOne()) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5501 | // With unit stride, the iteration never steps past the limit value. |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5502 | } else if (isKnownPositive(Step)) { |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 5503 | // Test whether a positive iteration can step past the limit |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5504 | // value and past the maximum value for its type in a single step. |
| 5505 | // Note that it's not sufficient to check NoWrap here, because even |
| 5506 | // though the value after a wrap is undefined, it's not undefined |
| 5507 | // behavior, so if wrap does occur, the loop could either terminate or |
Dan Gohman | 155eec7 | 2010-01-26 18:32:54 +0000 | [diff] [blame] | 5508 | // loop infinitely, but in either case, the loop is guaranteed to |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5509 | // iterate at least until the iteration where the wrapping occurs. |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 5510 | const SCEV *One = getConstant(Step->getType(), 1); |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5511 | if (isSigned) { |
| 5512 | APInt Max = APInt::getSignedMaxValue(BitWidth); |
| 5513 | if ((Max - getSignedRange(getMinusSCEV(Step, One)).getSignedMax()) |
| 5514 | .slt(getSignedRange(RHS).getSignedMax())) |
| 5515 | return getCouldNotCompute(); |
| 5516 | } else { |
| 5517 | APInt Max = APInt::getMaxValue(BitWidth); |
| 5518 | if ((Max - getUnsignedRange(getMinusSCEV(Step, One)).getUnsignedMax()) |
| 5519 | .ult(getUnsignedRange(RHS).getUnsignedMax())) |
| 5520 | return getCouldNotCompute(); |
| 5521 | } |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5522 | } else |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5523 | // TODO: Handle negative strides here and below. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5524 | return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5525 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5526 | // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant |
| 5527 | // m. So, we count the number of iterations in which {n,+,s} < m is true. |
| 5528 | // 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] | 5529 | // treat m-n as signed nor unsigned due to overflow possibility. |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5530 | |
Wojciech Matyjewicz | 3a4cbe2 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 5531 | // 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] | 5532 | const SCEV *Start = AddRec->getOperand(0); |
Wojciech Matyjewicz | 3a4cbe2 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 5533 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5534 | // Determine the minimum constant start value. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5535 | const SCEV *MinStart = getConstant(isSigned ? |
| 5536 | getSignedRange(Start).getSignedMin() : |
| 5537 | getUnsignedRange(Start).getUnsignedMin()); |
Wojciech Matyjewicz | 3a4cbe2 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 5538 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5539 | // If we know that the condition is true in order to enter the loop, |
| 5540 | // 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] | 5541 | // only know that it will execute (max(m,n)-n)/s times. In both cases, |
| 5542 | // the division must round up. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5543 | const SCEV *End = RHS; |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 5544 | if (!isLoopEntryGuardedByCond(L, |
| 5545 | isSigned ? ICmpInst::ICMP_SLT : |
| 5546 | ICmpInst::ICMP_ULT, |
| 5547 | getMinusSCEV(Start, Step), RHS)) |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5548 | End = isSigned ? getSMaxExpr(RHS, Start) |
| 5549 | : getUMaxExpr(RHS, Start); |
| 5550 | |
| 5551 | // Determine the maximum constant end value. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 5552 | const SCEV *MaxEnd = getConstant(isSigned ? |
| 5553 | getSignedRange(End).getSignedMax() : |
| 5554 | getUnsignedRange(End).getUnsignedMax()); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5555 | |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5556 | // If MaxEnd is within a step of the maximum integer value in its type, |
| 5557 | // 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] | 5558 | // This allows the subsequent ceiling division of (N+(step-1))/step to |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5559 | // compute the correct value. |
| 5560 | const SCEV *StepMinusOne = getMinusSCEV(Step, |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 5561 | getConstant(Step->getType(), 1)); |
Dan Gohman | 52fddd3 | 2010-01-26 04:40:18 +0000 | [diff] [blame] | 5562 | MaxEnd = isSigned ? |
| 5563 | getSMinExpr(MaxEnd, |
| 5564 | getMinusSCEV(getConstant(APInt::getSignedMaxValue(BitWidth)), |
| 5565 | StepMinusOne)) : |
| 5566 | getUMinExpr(MaxEnd, |
| 5567 | getMinusSCEV(getConstant(APInt::getMaxValue(BitWidth)), |
| 5568 | StepMinusOne)); |
| 5569 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5570 | // Finally, we subtract these two values and divide, rounding up, to get |
| 5571 | // the number of times the backedge is executed. |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 5572 | const SCEV *BECount = getBECount(Start, End, Step, NoWrap); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5573 | |
| 5574 | // The maximum backedge count is similar, except using the minimum start |
| 5575 | // value and the maximum end value. |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 5576 | const SCEV *MaxBECount = getBECount(MinStart, MaxEnd, Step, NoWrap); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 5577 | |
| 5578 | return BackedgeTakenInfo(BECount, MaxBECount); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5581 | return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5584 | /// getNumIterationsInRange - Return the number of iterations of this loop that |
| 5585 | /// produce values in the specified constant range. Another way of looking at |
| 5586 | /// this is that it returns the first iteration number where the value is not in |
| 5587 | /// the condition, thus computing the exit count. If the iteration count can't |
| 5588 | /// be computed, an instance of SCEVCouldNotCompute is returned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5589 | const SCEV *SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 5590 | ScalarEvolution &SE) const { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5591 | if (Range.isFullSet()) // Infinite loop. |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5592 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5593 | |
| 5594 | // 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] | 5595 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart())) |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 5596 | if (!SC->getValue()->isZero()) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5597 | SmallVector<const SCEV *, 4> Operands(op_begin(), op_end()); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 5598 | Operands[0] = SE.getConstant(SC->getType(), 0); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5599 | const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 5600 | if (const SCEVAddRecExpr *ShiftedAddRec = |
| 5601 | dyn_cast<SCEVAddRecExpr>(Shifted)) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5602 | return ShiftedAddRec->getNumIterationsInRange( |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5603 | Range.subtract(SC->getValue()->getValue()), SE); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5604 | // This is strange and shouldn't happen. |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5605 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5606 | } |
| 5607 | |
| 5608 | // The only time we can solve this is when we have all constant indices. |
| 5609 | // Otherwise, we cannot determine the overflow conditions. |
| 5610 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 5611 | if (!isa<SCEVConstant>(getOperand(i))) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5612 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5613 | |
| 5614 | |
| 5615 | // Okay at this point we know that all elements of the chrec are constants and |
| 5616 | // that the start element is zero. |
| 5617 | |
| 5618 | // First check to see if the range contains zero. If not, the first |
| 5619 | // iteration exits. |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 5620 | unsigned BitWidth = SE.getTypeSizeInBits(getType()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 5621 | if (!Range.contains(APInt(BitWidth, 0))) |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 5622 | return SE.getConstant(getType(), 0); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5623 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5624 | if (isAffine()) { |
| 5625 | // If this is an affine expression then we have this situation: |
| 5626 | // Solve {0,+,A} in Range === Ax in Range |
| 5627 | |
Nick Lewycky | eefdebe | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 5628 | // We know that zero is in the range. If A is positive then we know that |
| 5629 | // the upper value of the range must be the first possible exit value. |
| 5630 | // If A is negative then the lower of the range is the last possible loop |
| 5631 | // value. Also note that we already checked for a full range. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 5632 | APInt One(BitWidth,1); |
Nick Lewycky | eefdebe | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 5633 | APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue(); |
| 5634 | APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5635 | |
Nick Lewycky | eefdebe | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 5636 | // The exit value should be (End+A)/A. |
Nick Lewycky | 9a2f931 | 2007-09-27 14:12:54 +0000 | [diff] [blame] | 5637 | APInt ExitVal = (End + A).udiv(A); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5638 | ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5639 | |
| 5640 | // Evaluate at the exit value. If we really did fall out of the valid |
| 5641 | // range, then we computed our trip count, otherwise wrap around or other |
| 5642 | // things must have happened. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5643 | ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5644 | if (Range.contains(Val->getValue())) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5645 | return SE.getCouldNotCompute(); // Something strange happened |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5646 | |
| 5647 | // 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] | 5648 | assert(Range.contains( |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 5649 | EvaluateConstantChrecAtConstant(this, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5650 | ConstantInt::get(SE.getContext(), ExitVal - One), SE)->getValue()) && |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5651 | "Linear scev computation is off in a bad way!"); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5652 | return SE.getConstant(ExitValue); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5653 | } else if (isQuadratic()) { |
| 5654 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the |
| 5655 | // quadratic equation to solve it. To do this, we must frame our problem in |
| 5656 | // terms of figuring out when zero is crossed, instead of when |
| 5657 | // Range.getUpper() is crossed. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5658 | SmallVector<const SCEV *, 4> NewOps(op_begin(), op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5659 | NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper())); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5660 | const SCEV *NewAddRec = SE.getAddRecExpr(NewOps, getLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5661 | |
| 5662 | // Next, solve the constructed addrec |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5663 | std::pair<const SCEV *,const SCEV *> Roots = |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5664 | SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5665 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 5666 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5667 | if (R1) { |
| 5668 | // Pick the smallest positive root value. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5669 | if (ConstantInt *CB = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 5670 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 5671 | R1->getValue(), R2->getValue()))) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5672 | if (CB->getZExtValue() == false) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5673 | std::swap(R1, R2); // R1 is the minimum root now. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5674 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5675 | // Make sure the root is not off by one. The returned iteration should |
| 5676 | // not be in the range, but the previous one should be. When solving |
| 5677 | // for "X*X < 5", for example, we should not return a root of 2. |
| 5678 | ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this, |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5679 | R1->getValue(), |
| 5680 | SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5681 | if (Range.contains(R1Val->getValue())) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5682 | // The next iteration must be out of the range... |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 5683 | ConstantInt *NextVal = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5684 | ConstantInt::get(SE.getContext(), R1->getValue()->getValue()+1); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5685 | |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5686 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5687 | if (!Range.contains(R1Val->getValue())) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5688 | return SE.getConstant(NextVal); |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5689 | return SE.getCouldNotCompute(); // Something strange happened |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5690 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5691 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5692 | // If R1 was not in the range, then it is a good return value. Make |
| 5693 | // sure that R1-1 WAS in the range though, just in case. |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 5694 | ConstantInt *NextVal = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5695 | ConstantInt::get(SE.getContext(), R1->getValue()->getValue()-1); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5696 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5697 | if (Range.contains(R1Val->getValue())) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5698 | return R1; |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5699 | return SE.getCouldNotCompute(); // Something strange happened |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5700 | } |
| 5701 | } |
| 5702 | } |
| 5703 | |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5704 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5705 | } |
| 5706 | |
| 5707 | |
| 5708 | |
| 5709 | //===----------------------------------------------------------------------===// |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5710 | // SCEVCallbackVH Class Implementation |
| 5711 | //===----------------------------------------------------------------------===// |
| 5712 | |
Dan Gohman | 1959b75 | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 5713 | void ScalarEvolution::SCEVCallbackVH::deleted() { |
Dan Gohman | ddf9f99 | 2009-07-13 22:20:53 +0000 | [diff] [blame] | 5714 | assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5715 | if (PHINode *PN = dyn_cast<PHINode>(getValPtr())) |
| 5716 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 5717 | SE->ValueExprMap.erase(getValPtr()); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5718 | // this now dangles! |
| 5719 | } |
| 5720 | |
Dan Gohman | 81f9121 | 2010-07-28 01:09:07 +0000 | [diff] [blame] | 5721 | void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *V) { |
Dan Gohman | ddf9f99 | 2009-07-13 22:20:53 +0000 | [diff] [blame] | 5722 | assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); |
Eric Christopher | e6cbfa6 | 2010-07-29 01:25:38 +0000 | [diff] [blame] | 5723 | |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5724 | // Forget all the expressions associated with users of the old value, |
| 5725 | // so that future queries will recompute the expressions using the new |
| 5726 | // value. |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 5727 | Value *Old = getValPtr(); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5728 | SmallVector<User *, 16> Worklist; |
Dan Gohman | 69fcae9 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5729 | SmallPtrSet<User *, 8> Visited; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5730 | for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end(); |
| 5731 | UI != UE; ++UI) |
| 5732 | Worklist.push_back(*UI); |
| 5733 | while (!Worklist.empty()) { |
| 5734 | User *U = Worklist.pop_back_val(); |
| 5735 | // Deleting the Old value will cause this to dangle. Postpone |
| 5736 | // that until everything else is done. |
Dan Gohman | 59846ac | 2010-07-28 00:28:25 +0000 | [diff] [blame] | 5737 | if (U == Old) |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5738 | continue; |
Dan Gohman | 69fcae9 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5739 | if (!Visited.insert(U)) |
| 5740 | continue; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5741 | if (PHINode *PN = dyn_cast<PHINode>(U)) |
| 5742 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 5743 | SE->ValueExprMap.erase(U); |
Dan Gohman | 69fcae9 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5744 | for (Value::use_iterator UI = U->use_begin(), UE = U->use_end(); |
| 5745 | UI != UE; ++UI) |
| 5746 | Worklist.push_back(*UI); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5747 | } |
Dan Gohman | 59846ac | 2010-07-28 00:28:25 +0000 | [diff] [blame] | 5748 | // Delete the Old value. |
| 5749 | if (PHINode *PN = dyn_cast<PHINode>(Old)) |
| 5750 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 5751 | SE->ValueExprMap.erase(Old); |
Dan Gohman | 59846ac | 2010-07-28 00:28:25 +0000 | [diff] [blame] | 5752 | // this now dangles! |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5753 | } |
| 5754 | |
Dan Gohman | 1959b75 | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 5755 | ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5756 | : CallbackVH(V), SE(se) {} |
| 5757 | |
| 5758 | //===----------------------------------------------------------------------===// |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5759 | // ScalarEvolution Class Implementation |
| 5760 | //===----------------------------------------------------------------------===// |
| 5761 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5762 | ScalarEvolution::ScalarEvolution() |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 5763 | : FunctionPass(ID), FirstUnknown(0) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 5764 | initializeScalarEvolutionPass(*PassRegistry::getPassRegistry()); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5765 | } |
| 5766 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5767 | bool ScalarEvolution::runOnFunction(Function &F) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5768 | this->F = &F; |
| 5769 | LI = &getAnalysis<LoopInfo>(); |
| 5770 | TD = getAnalysisIfAvailable<TargetData>(); |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 5771 | DT = &getAnalysis<DominatorTree>(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5772 | return false; |
| 5773 | } |
| 5774 | |
| 5775 | void ScalarEvolution::releaseMemory() { |
Dan Gohman | ab37f50 | 2010-08-02 23:49:30 +0000 | [diff] [blame] | 5776 | // Iterate through all the SCEVUnknown instances and call their |
| 5777 | // destructors, so that they release their references to their values. |
| 5778 | for (SCEVUnknown *U = FirstUnknown; U; U = U->Next) |
| 5779 | U->~SCEVUnknown(); |
| 5780 | FirstUnknown = 0; |
| 5781 | |
Dan Gohman | e8ac3f3 | 2010-08-27 18:55:03 +0000 | [diff] [blame] | 5782 | ValueExprMap.clear(); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5783 | BackedgeTakenCounts.clear(); |
| 5784 | ConstantEvolutionLoopExitValue.clear(); |
Dan Gohman | 6bce643 | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 5785 | ValuesAtScopes.clear(); |
Dan Gohman | 6678e7b | 2010-11-17 02:44:44 +0000 | [diff] [blame] | 5786 | UnsignedRanges.clear(); |
| 5787 | SignedRanges.clear(); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5788 | UniqueSCEVs.clear(); |
| 5789 | SCEVAllocator.Reset(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5790 | } |
| 5791 | |
| 5792 | void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const { |
| 5793 | AU.setPreservesAll(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5794 | AU.addRequiredTransitive<LoopInfo>(); |
Dan Gohman | 1cd9275 | 2010-01-19 22:21:27 +0000 | [diff] [blame] | 5795 | AU.addRequiredTransitive<DominatorTree>(); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 5796 | } |
| 5797 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5798 | bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5799 | return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5800 | } |
| 5801 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5802 | static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5803 | const Loop *L) { |
| 5804 | // Print all inner loops first |
| 5805 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 5806 | PrintLoopInfo(OS, SE, *I); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5807 | |
Dan Gohman | 3073329 | 2010-01-09 18:17:45 +0000 | [diff] [blame] | 5808 | OS << "Loop "; |
| 5809 | WriteAsOperand(OS, L->getHeader(), /*PrintType=*/false); |
| 5810 | OS << ": "; |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 5811 | |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 5812 | SmallVector<BasicBlock *, 8> ExitBlocks; |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 5813 | L->getExitBlocks(ExitBlocks); |
| 5814 | if (ExitBlocks.size() != 1) |
Nick Lewycky | aeb5e5c | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 5815 | OS << "<multiple exits> "; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5816 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5817 | if (SE->hasLoopInvariantBackedgeTakenCount(L)) { |
| 5818 | OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5819 | } else { |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5820 | OS << "Unpredictable backedge-taken count. "; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5821 | } |
| 5822 | |
Dan Gohman | 3073329 | 2010-01-09 18:17:45 +0000 | [diff] [blame] | 5823 | OS << "\n" |
| 5824 | "Loop "; |
| 5825 | WriteAsOperand(OS, L->getHeader(), /*PrintType=*/false); |
| 5826 | OS << ": "; |
Dan Gohman | aa551ae | 2009-06-24 00:33:16 +0000 | [diff] [blame] | 5827 | |
| 5828 | if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) { |
| 5829 | OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L); |
| 5830 | } else { |
| 5831 | OS << "Unpredictable max backedge-taken count. "; |
| 5832 | } |
| 5833 | |
| 5834 | OS << "\n"; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5835 | } |
| 5836 | |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 5837 | void ScalarEvolution::print(raw_ostream &OS, const Module *) const { |
Dan Gohman | 3f46a3a | 2010-03-01 17:49:51 +0000 | [diff] [blame] | 5838 | // ScalarEvolution's implementation of the print method is to print |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5839 | // out SCEV values of all instructions that are interesting. Doing |
| 5840 | // this potentially causes it to create new SCEV objects though, |
| 5841 | // which technically conflicts with the const qualifier. This isn't |
Dan Gohman | 1afdc5f | 2009-07-10 20:25:29 +0000 | [diff] [blame] | 5842 | // observable from outside the class though, so casting away the |
| 5843 | // const isn't dangerous. |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 5844 | ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5845 | |
Dan Gohman | 3073329 | 2010-01-09 18:17:45 +0000 | [diff] [blame] | 5846 | OS << "Classifying expressions for: "; |
| 5847 | WriteAsOperand(OS, F, /*PrintType=*/false); |
| 5848 | OS << "\n"; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5849 | 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] | 5850 | if (isSCEVable(I->getType()) && !isa<CmpInst>(*I)) { |
Dan Gohman | c902e13 | 2009-07-13 23:03:05 +0000 | [diff] [blame] | 5851 | OS << *I << '\n'; |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 5852 | OS << " --> "; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5853 | const SCEV *SV = SE.getSCEV(&*I); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5854 | SV->print(OS); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5855 | |
Dan Gohman | 0c689c5 | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 5856 | const Loop *L = LI->getLoopFor((*I).getParent()); |
| 5857 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5858 | const SCEV *AtUse = SE.getSCEVAtScope(SV, L); |
Dan Gohman | 0c689c5 | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 5859 | if (AtUse != SV) { |
| 5860 | OS << " --> "; |
| 5861 | AtUse->print(OS); |
| 5862 | } |
| 5863 | |
| 5864 | if (L) { |
Dan Gohman | 9e7d988 | 2009-06-18 00:37:45 +0000 | [diff] [blame] | 5865 | OS << "\t\t" "Exits: "; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5866 | const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop()); |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5867 | if (!SE.isLoopInvariant(ExitValue, L)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5868 | OS << "<<Unknown>>"; |
| 5869 | } else { |
| 5870 | OS << *ExitValue; |
| 5871 | } |
| 5872 | } |
| 5873 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5874 | OS << "\n"; |
| 5875 | } |
| 5876 | |
Dan Gohman | 3073329 | 2010-01-09 18:17:45 +0000 | [diff] [blame] | 5877 | OS << "Determining loop execution counts for: "; |
| 5878 | WriteAsOperand(OS, F, /*PrintType=*/false); |
| 5879 | OS << "\n"; |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5880 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
| 5881 | PrintLoopInfo(OS, &SE, *I); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5882 | } |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 5883 | |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 5884 | bool ScalarEvolution::isLoopInvariant(const SCEV *S, const Loop *L) { |
| 5885 | switch (S->getSCEVType()) { |
| 5886 | case scConstant: |
| 5887 | return true; |
| 5888 | case scTruncate: |
| 5889 | case scZeroExtend: |
| 5890 | case scSignExtend: |
| 5891 | return isLoopInvariant(cast<SCEVCastExpr>(S)->getOperand(), L); |
| 5892 | case scAddRecExpr: { |
| 5893 | const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S); |
| 5894 | |
| 5895 | // Add recurrences are never invariant in the function-body (null loop). |
| 5896 | if (!L) |
| 5897 | return false; |
| 5898 | |
| 5899 | // This recurrence is variant w.r.t. L if L contains AR's loop. |
| 5900 | if (L->contains(AR->getLoop())) |
| 5901 | return false; |
| 5902 | |
| 5903 | // This recurrence is invariant w.r.t. L if AR's loop contains L. |
| 5904 | if (AR->getLoop()->contains(L)) |
| 5905 | return true; |
| 5906 | |
| 5907 | // This recurrence is variant w.r.t. L if any of its operands |
| 5908 | // are variant. |
| 5909 | for (SCEVAddRecExpr::op_iterator I = AR->op_begin(), E = AR->op_end(); |
| 5910 | I != E; ++I) |
| 5911 | if (!isLoopInvariant(*I, L)) |
| 5912 | return false; |
| 5913 | |
| 5914 | // Otherwise it's loop-invariant. |
| 5915 | return true; |
| 5916 | } |
| 5917 | case scAddExpr: |
| 5918 | case scMulExpr: |
| 5919 | case scUMaxExpr: |
| 5920 | case scSMaxExpr: { |
| 5921 | const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S); |
| 5922 | for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end(); |
| 5923 | I != E; ++I) |
| 5924 | if (!isLoopInvariant(*I, L)) |
| 5925 | return false; |
| 5926 | return true; |
| 5927 | } |
| 5928 | case scUDivExpr: { |
| 5929 | const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S); |
| 5930 | return isLoopInvariant(UDiv->getLHS(), L) && |
| 5931 | isLoopInvariant(UDiv->getRHS(), L); |
| 5932 | } |
| 5933 | case scUnknown: |
| 5934 | // All non-instruction values are loop invariant. All instructions are loop |
| 5935 | // invariant if they are not contained in the specified loop. |
| 5936 | // Instructions are never considered invariant in the function body |
| 5937 | // (null loop) because they are defined within the "loop". |
| 5938 | if (Instruction *I = dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) |
| 5939 | return L && !L->contains(I); |
| 5940 | return true; |
| 5941 | case scCouldNotCompute: |
| 5942 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
| 5943 | return false; |
| 5944 | default: break; |
| 5945 | } |
| 5946 | llvm_unreachable("Unknown SCEV kind!"); |
| 5947 | return false; |
| 5948 | } |
| 5949 | |
| 5950 | bool ScalarEvolution::hasComputableLoopEvolution(const SCEV *S, const Loop *L) { |
| 5951 | switch (S->getSCEVType()) { |
| 5952 | case scConstant: |
| 5953 | return false; |
| 5954 | case scTruncate: |
| 5955 | case scZeroExtend: |
| 5956 | case scSignExtend: |
| 5957 | return hasComputableLoopEvolution(cast<SCEVCastExpr>(S)->getOperand(), L); |
| 5958 | case scAddRecExpr: |
| 5959 | return cast<SCEVAddRecExpr>(S)->getLoop() == L; |
| 5960 | case scAddExpr: |
| 5961 | case scMulExpr: |
| 5962 | case scUMaxExpr: |
| 5963 | case scSMaxExpr: { |
| 5964 | const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S); |
| 5965 | bool HasVarying = false; |
| 5966 | for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end(); |
| 5967 | I != E; ++I) { |
| 5968 | const SCEV *Op = *I; |
| 5969 | if (!isLoopInvariant(Op, L)) { |
| 5970 | if (hasComputableLoopEvolution(Op, L)) |
| 5971 | HasVarying = true; |
| 5972 | else |
| 5973 | return false; |
| 5974 | } |
| 5975 | } |
| 5976 | return HasVarying; |
| 5977 | } |
| 5978 | case scUDivExpr: { |
| 5979 | const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S); |
| 5980 | bool HasVarying = false; |
| 5981 | if (!isLoopInvariant(UDiv->getLHS(), L)) { |
| 5982 | if (hasComputableLoopEvolution(UDiv->getLHS(), L)) |
| 5983 | HasVarying = true; |
| 5984 | else |
| 5985 | return false; |
| 5986 | } |
| 5987 | if (!isLoopInvariant(UDiv->getRHS(), L)) { |
| 5988 | if (hasComputableLoopEvolution(UDiv->getRHS(), L)) |
| 5989 | HasVarying = true; |
| 5990 | else |
| 5991 | return false; |
| 5992 | } |
| 5993 | return HasVarying; |
| 5994 | } |
| 5995 | case scUnknown: |
| 5996 | return false; |
| 5997 | case scCouldNotCompute: |
| 5998 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
| 5999 | return false; |
| 6000 | default: break; |
| 6001 | } |
| 6002 | llvm_unreachable("Unknown SCEV kind!"); |
| 6003 | return false; |
| 6004 | } |
Dan Gohman | dc0e8fb | 2010-11-17 21:41:58 +0000 | [diff] [blame^] | 6005 | |
| 6006 | bool ScalarEvolution::dominates(const SCEV *S, BasicBlock *BB) const { |
| 6007 | switch (S->getSCEVType()) { |
| 6008 | case scConstant: |
| 6009 | return true; |
| 6010 | case scTruncate: |
| 6011 | case scZeroExtend: |
| 6012 | case scSignExtend: |
| 6013 | return dominates(cast<SCEVCastExpr>(S)->getOperand(), BB); |
| 6014 | case scAddRecExpr: { |
| 6015 | const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S); |
| 6016 | if (!DT->dominates(AR->getLoop()->getHeader(), BB)) |
| 6017 | return false; |
| 6018 | } |
| 6019 | // FALL THROUGH into SCEVNAryExpr handling. |
| 6020 | case scAddExpr: |
| 6021 | case scMulExpr: |
| 6022 | case scUMaxExpr: |
| 6023 | case scSMaxExpr: { |
| 6024 | const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S); |
| 6025 | for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end(); |
| 6026 | I != E; ++I) |
| 6027 | if (!dominates(*I, BB)) |
| 6028 | return false; |
| 6029 | return true; |
| 6030 | } |
| 6031 | case scUDivExpr: { |
| 6032 | const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S); |
| 6033 | return dominates(UDiv->getLHS(), BB) && dominates(UDiv->getRHS(), BB); |
| 6034 | } |
| 6035 | case scUnknown: |
| 6036 | if (Instruction *I = |
| 6037 | dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) |
| 6038 | return DT->dominates(I->getParent(), BB); |
| 6039 | return true; |
| 6040 | case scCouldNotCompute: |
| 6041 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
| 6042 | return false; |
| 6043 | default: break; |
| 6044 | } |
| 6045 | llvm_unreachable("Unknown SCEV kind!"); |
| 6046 | return false; |
| 6047 | } |
| 6048 | |
| 6049 | bool ScalarEvolution::properlyDominates(const SCEV *S, BasicBlock *BB) const { |
| 6050 | switch (S->getSCEVType()) { |
| 6051 | case scConstant: |
| 6052 | return true; |
| 6053 | case scTruncate: |
| 6054 | case scZeroExtend: |
| 6055 | case scSignExtend: |
| 6056 | return properlyDominates(cast<SCEVCastExpr>(S)->getOperand(), BB); |
| 6057 | case scAddRecExpr: { |
| 6058 | // This uses a "dominates" query instead of "properly dominates" query |
| 6059 | // because the instruction which produces the addrec's value is a PHI, and |
| 6060 | // a PHI effectively properly dominates its entire containing block. |
| 6061 | const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S); |
| 6062 | if (!DT->dominates(AR->getLoop()->getHeader(), BB)) |
| 6063 | return false; |
| 6064 | } |
| 6065 | // FALL THROUGH into SCEVNAryExpr handling. |
| 6066 | case scAddExpr: |
| 6067 | case scMulExpr: |
| 6068 | case scUMaxExpr: |
| 6069 | case scSMaxExpr: { |
| 6070 | const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S); |
| 6071 | for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end(); |
| 6072 | I != E; ++I) |
| 6073 | if (!properlyDominates(*I, BB)) |
| 6074 | return false; |
| 6075 | return true; |
| 6076 | } |
| 6077 | case scUDivExpr: { |
| 6078 | const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S); |
| 6079 | return properlyDominates(UDiv->getLHS(), BB) && |
| 6080 | properlyDominates(UDiv->getRHS(), BB); |
| 6081 | } |
| 6082 | case scUnknown: |
| 6083 | if (Instruction *I = |
| 6084 | dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) |
| 6085 | return DT->properlyDominates(I->getParent(), BB); |
| 6086 | return true; |
| 6087 | case scCouldNotCompute: |
| 6088 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
| 6089 | return false; |
| 6090 | default: break; |
| 6091 | } |
| 6092 | llvm_unreachable("Unknown SCEV kind!"); |
| 6093 | return false; |
| 6094 | } |