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 |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 17 | // can handle. These classes are reference counted, managed by the const SCEV* |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 18 | // class. We only create one SCEV of a particular shape, so pointer-comparisons |
| 19 | // for equality are legal. |
| 20 | // |
| 21 | // One important aspect of the SCEV objects is that they are never cyclic, even |
| 22 | // if there is a cycle in the dataflow for an expression (ie, a PHI node). If |
| 23 | // the PHI node is one of the idioms that we can represent (e.g., a polynomial |
| 24 | // recurrence) then we represent it directly as a recurrence node, otherwise we |
| 25 | // represent it as a SCEVUnknown node. |
| 26 | // |
| 27 | // In addition to being able to represent expressions of various types, we also |
| 28 | // have folders that are used to build the *canonical* representation for a |
| 29 | // particular expression. These folders are capable of using a variety of |
| 30 | // rewrite rules to simplify the expressions. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 31 | // |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 32 | // Once the folders are defined, we can implement the more interesting |
| 33 | // higher-level code, such as the code that recognizes PHI nodes of various |
| 34 | // types, computes the execution count of a loop, etc. |
| 35 | // |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 36 | // TODO: We should use these routines and value representations to implement |
| 37 | // dependence analysis! |
| 38 | // |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | // |
| 41 | // There are several good references for the techniques used in this analysis. |
| 42 | // |
| 43 | // Chains of recurrences -- a method to expedite the evaluation |
| 44 | // of closed-form functions |
| 45 | // Olaf Bachmann, Paul S. Wang, Eugene V. Zima |
| 46 | // |
| 47 | // On computational properties of chains of recurrences |
| 48 | // Eugene V. Zima |
| 49 | // |
| 50 | // Symbolic Evaluation of Chains of Recurrences for Loop Optimization |
| 51 | // Robert A. van Engelen |
| 52 | // |
| 53 | // Efficient Symbolic Analysis for Optimizing Compilers |
| 54 | // Robert A. van Engelen |
| 55 | // |
| 56 | // Using the chains of recurrences algebra for data dependence testing and |
| 57 | // induction variable substitution |
| 58 | // MS Thesis, Johnie Birch |
| 59 | // |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | |
Chris Lattner | 3b27d68 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 62 | #define DEBUG_TYPE "scalar-evolution" |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 63 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 64 | #include "llvm/Constants.h" |
| 65 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 66 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 67 | #include "llvm/Instructions.h" |
John Criswell | a115643 | 2005-10-27 15:54:34 +0000 | [diff] [blame] | 68 | #include "llvm/Analysis/ConstantFolding.h" |
Evan Cheng | 5a6c1a8 | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 69 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 70 | #include "llvm/Analysis/LoopInfo.h" |
Dan Gohman | 61ffa8e | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 71 | #include "llvm/Analysis/ValueTracking.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 72 | #include "llvm/Assembly/Writer.h" |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 73 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 74 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | b336409 | 2006-10-04 21:49:37 +0000 | [diff] [blame] | 75 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 76 | #include "llvm/Support/ConstantRange.h" |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 77 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 78 | #include "llvm/Support/InstIterator.h" |
Chris Lattner | 75de5ab | 2006-12-19 01:16:02 +0000 | [diff] [blame] | 79 | #include "llvm/Support/MathExtras.h" |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 80 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 81 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 82 | #include "llvm/ADT/STLExtras.h" |
Alkis Evlogimenos | 20aa474 | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 83 | #include <algorithm> |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 84 | using namespace llvm; |
| 85 | |
Chris Lattner | 3b27d68 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 86 | STATISTIC(NumArrayLenItCounts, |
| 87 | "Number of trip counts computed with array length"); |
| 88 | STATISTIC(NumTripCountsComputed, |
| 89 | "Number of loops with predictable loop counts"); |
| 90 | STATISTIC(NumTripCountsNotComputed, |
| 91 | "Number of loops without predictable loop counts"); |
| 92 | STATISTIC(NumBruteForceTripCountsComputed, |
| 93 | "Number of loops with trip counts computed by force"); |
| 94 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 95 | static cl::opt<unsigned> |
Chris Lattner | 3b27d68 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 96 | MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, |
| 97 | cl::desc("Maximum number of iterations SCEV will " |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 98 | "symbolically execute a constant " |
| 99 | "derived loop"), |
Chris Lattner | 3b27d68 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 100 | cl::init(100)); |
| 101 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 102 | static RegisterPass<ScalarEvolution> |
| 103 | R("scalar-evolution", "Scalar Evolution Analysis", false, true); |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 104 | char ScalarEvolution::ID = 0; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 105 | |
| 106 | //===----------------------------------------------------------------------===// |
| 107 | // SCEV class definitions |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | |
| 110 | //===----------------------------------------------------------------------===// |
| 111 | // Implementation of the SCEV class. |
| 112 | // |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 114 | SCEV::~SCEV() {} |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 116 | void SCEV::dump() const { |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 117 | print(errs()); |
| 118 | errs() << '\n'; |
| 119 | } |
| 120 | |
| 121 | void SCEV::print(std::ostream &o) const { |
| 122 | raw_os_ostream OS(o); |
| 123 | print(OS); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 126 | bool SCEV::isZero() const { |
| 127 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 128 | return SC->getValue()->isZero(); |
| 129 | return false; |
| 130 | } |
| 131 | |
Dan Gohman | 70a1fe7 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 132 | bool SCEV::isOne() const { |
| 133 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 134 | return SC->getValue()->isOne(); |
| 135 | return false; |
| 136 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 137 | |
Dan Gohman | 4d289bf | 2009-06-24 00:30:26 +0000 | [diff] [blame] | 138 | bool SCEV::isAllOnesValue() const { |
| 139 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 140 | return SC->getValue()->isAllOnesValue(); |
| 141 | return false; |
| 142 | } |
| 143 | |
Owen Anderson | 753ad61 | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 144 | SCEVCouldNotCompute::SCEVCouldNotCompute() : |
| 145 | SCEV(scCouldNotCompute) {} |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 146 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 147 | void SCEVCouldNotCompute::Profile(FoldingSetNodeID &ID) const { |
| 148 | assert(0 && "Attempt to use a SCEVCouldNotCompute object!"); |
| 149 | } |
| 150 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 151 | bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const { |
| 152 | assert(0 && "Attempt to use a SCEVCouldNotCompute object!"); |
Misha Brukman | bb2aff1 | 2004-04-05 19:00:46 +0000 | [diff] [blame] | 153 | return false; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | const Type *SCEVCouldNotCompute::getType() const { |
| 157 | assert(0 && "Attempt to use a SCEVCouldNotCompute object!"); |
Misha Brukman | bb2aff1 | 2004-04-05 19:00:46 +0000 | [diff] [blame] | 158 | return 0; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const { |
| 162 | assert(0 && "Attempt to use a SCEVCouldNotCompute object!"); |
| 163 | return false; |
| 164 | } |
| 165 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 166 | const SCEV * |
| 167 | SCEVCouldNotCompute::replaceSymbolicValuesWithConcrete( |
| 168 | const SCEV *Sym, |
| 169 | const SCEV *Conc, |
| 170 | ScalarEvolution &SE) const { |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 171 | return this; |
| 172 | } |
| 173 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 174 | void SCEVCouldNotCompute::print(raw_ostream &OS) const { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 175 | OS << "***COULDNOTCOMPUTE***"; |
| 176 | } |
| 177 | |
| 178 | bool SCEVCouldNotCompute::classof(const SCEV *S) { |
| 179 | return S->getSCEVType() == scCouldNotCompute; |
| 180 | } |
| 181 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 182 | const SCEV* ScalarEvolution::getConstant(ConstantInt *V) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 183 | FoldingSetNodeID ID; |
| 184 | ID.AddInteger(scConstant); |
| 185 | ID.AddPointer(V); |
| 186 | void *IP = 0; |
| 187 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 188 | SCEV *S = SCEVAllocator.Allocate<SCEVConstant>(); |
| 189 | new (S) SCEVConstant(V); |
| 190 | UniqueSCEVs.InsertNode(S, IP); |
| 191 | return S; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 192 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 193 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 194 | const SCEV* ScalarEvolution::getConstant(const APInt& Val) { |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 195 | return getConstant(ConstantInt::get(Val)); |
Dan Gohman | 9a6ae96 | 2007-07-09 15:25:17 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 198 | const SCEV* |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 199 | ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) { |
| 200 | return getConstant(ConstantInt::get(cast<IntegerType>(Ty), V, isSigned)); |
| 201 | } |
| 202 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 203 | void SCEVConstant::Profile(FoldingSetNodeID &ID) const { |
| 204 | ID.AddInteger(scConstant); |
| 205 | ID.AddPointer(V); |
| 206 | } |
| 207 | |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 208 | const Type *SCEVConstant::getType() const { return V->getType(); } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 209 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 210 | void SCEVConstant::print(raw_ostream &OS) const { |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 211 | WriteAsOperand(OS, V, false); |
| 212 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 213 | |
Dan Gohman | 8492360 | 2009-04-21 01:25:57 +0000 | [diff] [blame] | 214 | SCEVCastExpr::SCEVCastExpr(unsigned SCEVTy, |
Owen Anderson | 753ad61 | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 215 | const SCEV* op, const Type *ty) |
| 216 | : SCEV(SCEVTy), Op(op), Ty(ty) {} |
Dan Gohman | 8492360 | 2009-04-21 01:25:57 +0000 | [diff] [blame] | 217 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 218 | void SCEVCastExpr::Profile(FoldingSetNodeID &ID) const { |
| 219 | ID.AddInteger(getSCEVType()); |
| 220 | ID.AddPointer(Op); |
| 221 | ID.AddPointer(Ty); |
| 222 | } |
| 223 | |
Dan Gohman | 8492360 | 2009-04-21 01:25:57 +0000 | [diff] [blame] | 224 | bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 225 | return Op->dominates(BB, DT); |
| 226 | } |
| 227 | |
Owen Anderson | 753ad61 | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 228 | SCEVTruncateExpr::SCEVTruncateExpr(const SCEV* op, const Type *ty) |
| 229 | : SCEVCastExpr(scTruncate, op, ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 230 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 231 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 232 | "Cannot truncate non-integer value!"); |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 233 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 234 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 235 | void SCEVTruncateExpr::print(raw_ostream &OS) const { |
Dan Gohman | 36b8e53 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 236 | OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Owen Anderson | 753ad61 | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 239 | SCEVZeroExtendExpr::SCEVZeroExtendExpr(const SCEV* op, const Type *ty) |
| 240 | : SCEVCastExpr(scZeroExtend, op, ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 241 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 242 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 243 | "Cannot zero extend non-integer value!"); |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 246 | void SCEVZeroExtendExpr::print(raw_ostream &OS) const { |
Dan Gohman | 36b8e53 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 247 | OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Owen Anderson | 753ad61 | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 250 | SCEVSignExtendExpr::SCEVSignExtendExpr(const SCEV* op, const Type *ty) |
| 251 | : SCEVCastExpr(scSignExtend, op, ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 252 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 253 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 254 | "Cannot sign extend non-integer value!"); |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 257 | void SCEVSignExtendExpr::print(raw_ostream &OS) const { |
Dan Gohman | 36b8e53 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 258 | OS << "(sext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 261 | void SCEVCommutativeExpr::print(raw_ostream &OS) const { |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 262 | assert(Operands.size() > 1 && "This plus expr shouldn't exist!"); |
| 263 | const char *OpStr = getOperationStr(); |
| 264 | OS << "(" << *Operands[0]; |
| 265 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 266 | OS << OpStr << *Operands[i]; |
| 267 | OS << ")"; |
| 268 | } |
| 269 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 270 | const SCEV * |
| 271 | SCEVCommutativeExpr::replaceSymbolicValuesWithConcrete( |
| 272 | const SCEV *Sym, |
| 273 | const SCEV *Conc, |
| 274 | ScalarEvolution &SE) const { |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 275 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 276 | const SCEV* H = |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 277 | getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc, SE); |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 278 | if (H != getOperand(i)) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 279 | SmallVector<const SCEV*, 8> NewOps; |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 280 | NewOps.reserve(getNumOperands()); |
| 281 | for (unsigned j = 0; j != i; ++j) |
| 282 | NewOps.push_back(getOperand(j)); |
| 283 | NewOps.push_back(H); |
| 284 | for (++i; i != e; ++i) |
| 285 | NewOps.push_back(getOperand(i)-> |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 286 | replaceSymbolicValuesWithConcrete(Sym, Conc, SE)); |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 287 | |
| 288 | if (isa<SCEVAddExpr>(this)) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 289 | return SE.getAddExpr(NewOps); |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 290 | else if (isa<SCEVMulExpr>(this)) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 291 | return SE.getMulExpr(NewOps); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 292 | else if (isa<SCEVSMaxExpr>(this)) |
| 293 | return SE.getSMaxExpr(NewOps); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 294 | else if (isa<SCEVUMaxExpr>(this)) |
| 295 | return SE.getUMaxExpr(NewOps); |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 296 | else |
| 297 | assert(0 && "Unknown commutative expr!"); |
| 298 | } |
| 299 | } |
| 300 | return this; |
| 301 | } |
| 302 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 303 | void SCEVNAryExpr::Profile(FoldingSetNodeID &ID) const { |
| 304 | ID.AddInteger(getSCEVType()); |
| 305 | ID.AddInteger(Operands.size()); |
| 306 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 307 | ID.AddPointer(Operands[i]); |
| 308 | } |
| 309 | |
Dan Gohman | ecb403a | 2009-05-07 14:00:19 +0000 | [diff] [blame] | 310 | bool SCEVNAryExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
Evan Cheng | 5a6c1a8 | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 311 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 312 | if (!getOperand(i)->dominates(BB, DT)) |
| 313 | return false; |
| 314 | } |
| 315 | return true; |
| 316 | } |
| 317 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 318 | void SCEVUDivExpr::Profile(FoldingSetNodeID &ID) const { |
| 319 | ID.AddInteger(scUDivExpr); |
| 320 | ID.AddPointer(LHS); |
| 321 | ID.AddPointer(RHS); |
| 322 | } |
| 323 | |
Evan Cheng | 5a6c1a8 | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 324 | bool SCEVUDivExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 325 | return LHS->dominates(BB, DT) && RHS->dominates(BB, DT); |
| 326 | } |
| 327 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 328 | void SCEVUDivExpr::print(raw_ostream &OS) const { |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 329 | OS << "(" << *LHS << " /u " << *RHS << ")"; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 332 | const Type *SCEVUDivExpr::getType() const { |
Dan Gohman | 91bb61a | 2009-05-26 17:44:05 +0000 | [diff] [blame] | 333 | // In most cases the types of LHS and RHS will be the same, but in some |
| 334 | // crazy cases one or the other may be a pointer. ScalarEvolution doesn't |
| 335 | // depend on the type for correctness, but handling types carefully can |
| 336 | // avoid extra casts in the SCEVExpander. The LHS is more likely to be |
| 337 | // a pointer type than the RHS, so use the RHS' type here. |
| 338 | return RHS->getType(); |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 341 | void SCEVAddRecExpr::Profile(FoldingSetNodeID &ID) const { |
| 342 | ID.AddInteger(scAddRecExpr); |
| 343 | ID.AddInteger(Operands.size()); |
| 344 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 345 | ID.AddPointer(Operands[i]); |
| 346 | ID.AddPointer(L); |
| 347 | } |
| 348 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 349 | const SCEV * |
| 350 | SCEVAddRecExpr::replaceSymbolicValuesWithConcrete(const SCEV *Sym, |
| 351 | const SCEV *Conc, |
| 352 | ScalarEvolution &SE) const { |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 353 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 354 | const SCEV* H = |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 355 | getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc, SE); |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 356 | if (H != getOperand(i)) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 357 | SmallVector<const SCEV*, 8> NewOps; |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 358 | NewOps.reserve(getNumOperands()); |
| 359 | for (unsigned j = 0; j != i; ++j) |
| 360 | NewOps.push_back(getOperand(j)); |
| 361 | NewOps.push_back(H); |
| 362 | for (++i; i != e; ++i) |
| 363 | NewOps.push_back(getOperand(i)-> |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 364 | replaceSymbolicValuesWithConcrete(Sym, Conc, SE)); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 365 | |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 366 | return SE.getAddRecExpr(NewOps, L); |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | return this; |
| 370 | } |
| 371 | |
| 372 | |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 373 | bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const { |
Dan Gohman | a3035a6 | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 374 | // Add recurrences are never invariant in the function-body (null loop). |
Dan Gohman | e890eea | 2009-06-26 22:17:21 +0000 | [diff] [blame] | 375 | if (!QueryLoop) |
| 376 | return false; |
| 377 | |
| 378 | // This recurrence is variant w.r.t. QueryLoop if QueryLoop contains L. |
| 379 | if (QueryLoop->contains(L->getHeader())) |
| 380 | return false; |
| 381 | |
| 382 | // This recurrence is variant w.r.t. QueryLoop if any of its operands |
| 383 | // are variant. |
| 384 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 385 | if (!getOperand(i)->isLoopInvariant(QueryLoop)) |
| 386 | return false; |
| 387 | |
| 388 | // Otherwise it's loop-invariant. |
| 389 | return true; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 392 | void SCEVAddRecExpr::print(raw_ostream &OS) const { |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 393 | OS << "{" << *Operands[0]; |
| 394 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 395 | OS << ",+," << *Operands[i]; |
| 396 | OS << "}<" << L->getHeader()->getName() + ">"; |
| 397 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 398 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 399 | void SCEVUnknown::Profile(FoldingSetNodeID &ID) const { |
| 400 | ID.AddInteger(scUnknown); |
| 401 | ID.AddPointer(V); |
| 402 | } |
| 403 | |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 404 | bool SCEVUnknown::isLoopInvariant(const Loop *L) const { |
| 405 | // All non-instruction values are loop invariant. All instructions are loop |
| 406 | // invariant if they are not contained in the specified loop. |
Dan Gohman | a3035a6 | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 407 | // Instructions are never considered invariant in the function body |
| 408 | // (null loop) because they are defined within the "loop". |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 409 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Dan Gohman | a3035a6 | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 410 | return L && !L->contains(I->getParent()); |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 411 | return true; |
| 412 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 413 | |
Evan Cheng | 5a6c1a8 | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 414 | bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 415 | if (Instruction *I = dyn_cast<Instruction>(getValue())) |
| 416 | return DT->dominates(I->getParent(), BB); |
| 417 | return true; |
| 418 | } |
| 419 | |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 420 | const Type *SCEVUnknown::getType() const { |
| 421 | return V->getType(); |
| 422 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 423 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 424 | void SCEVUnknown::print(raw_ostream &OS) const { |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 425 | WriteAsOperand(OS, V, false); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 428 | //===----------------------------------------------------------------------===// |
| 429 | // SCEV Utilities |
| 430 | //===----------------------------------------------------------------------===// |
| 431 | |
| 432 | namespace { |
| 433 | /// SCEVComplexityCompare - Return true if the complexity of the LHS is less |
| 434 | /// than the complexity of the RHS. This comparator is used to canonicalize |
| 435 | /// expressions. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 436 | class VISIBILITY_HIDDEN SCEVComplexityCompare { |
| 437 | LoopInfo *LI; |
| 438 | public: |
| 439 | explicit SCEVComplexityCompare(LoopInfo *li) : LI(li) {} |
| 440 | |
Dan Gohman | f7b37b2 | 2008-04-14 18:23:56 +0000 | [diff] [blame] | 441 | bool operator()(const SCEV *LHS, const SCEV *RHS) const { |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 442 | // Primarily, sort the SCEVs by their getSCEVType(). |
| 443 | if (LHS->getSCEVType() != RHS->getSCEVType()) |
| 444 | return LHS->getSCEVType() < RHS->getSCEVType(); |
| 445 | |
| 446 | // Aside from the getSCEVType() ordering, the particular ordering |
| 447 | // isn't very important except that it's beneficial to be consistent, |
| 448 | // so that (a + b) and (b + a) don't end up as different expressions. |
| 449 | |
| 450 | // Sort SCEVUnknown values with some loose heuristics. TODO: This is |
| 451 | // not as complete as it could be. |
| 452 | if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) { |
| 453 | const SCEVUnknown *RU = cast<SCEVUnknown>(RHS); |
| 454 | |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 455 | // Order pointer values after integer values. This helps SCEVExpander |
| 456 | // form GEPs. |
| 457 | if (isa<PointerType>(LU->getType()) && !isa<PointerType>(RU->getType())) |
| 458 | return false; |
| 459 | if (isa<PointerType>(RU->getType()) && !isa<PointerType>(LU->getType())) |
| 460 | return true; |
| 461 | |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 462 | // Compare getValueID values. |
| 463 | if (LU->getValue()->getValueID() != RU->getValue()->getValueID()) |
| 464 | return LU->getValue()->getValueID() < RU->getValue()->getValueID(); |
| 465 | |
| 466 | // Sort arguments by their position. |
| 467 | if (const Argument *LA = dyn_cast<Argument>(LU->getValue())) { |
| 468 | const Argument *RA = cast<Argument>(RU->getValue()); |
| 469 | return LA->getArgNo() < RA->getArgNo(); |
| 470 | } |
| 471 | |
| 472 | // For instructions, compare their loop depth, and their opcode. |
| 473 | // This is pretty loose. |
| 474 | if (Instruction *LV = dyn_cast<Instruction>(LU->getValue())) { |
| 475 | Instruction *RV = cast<Instruction>(RU->getValue()); |
| 476 | |
| 477 | // Compare loop depths. |
| 478 | if (LI->getLoopDepth(LV->getParent()) != |
| 479 | LI->getLoopDepth(RV->getParent())) |
| 480 | return LI->getLoopDepth(LV->getParent()) < |
| 481 | LI->getLoopDepth(RV->getParent()); |
| 482 | |
| 483 | // Compare opcodes. |
| 484 | if (LV->getOpcode() != RV->getOpcode()) |
| 485 | return LV->getOpcode() < RV->getOpcode(); |
| 486 | |
| 487 | // Compare the number of operands. |
| 488 | if (LV->getNumOperands() != RV->getNumOperands()) |
| 489 | return LV->getNumOperands() < RV->getNumOperands(); |
| 490 | } |
| 491 | |
| 492 | return false; |
| 493 | } |
| 494 | |
Dan Gohman | 4dfad29 | 2009-06-14 22:51:25 +0000 | [diff] [blame] | 495 | // Compare constant values. |
| 496 | if (const SCEVConstant *LC = dyn_cast<SCEVConstant>(LHS)) { |
| 497 | const SCEVConstant *RC = cast<SCEVConstant>(RHS); |
Nick Lewycky | d1ec989 | 2009-07-04 17:24:52 +0000 | [diff] [blame] | 498 | if (LC->getValue()->getBitWidth() != RC->getValue()->getBitWidth()) |
| 499 | return LC->getValue()->getBitWidth() < RC->getValue()->getBitWidth(); |
Dan Gohman | 4dfad29 | 2009-06-14 22:51:25 +0000 | [diff] [blame] | 500 | return LC->getValue()->getValue().ult(RC->getValue()->getValue()); |
| 501 | } |
| 502 | |
| 503 | // Compare addrec loop depths. |
| 504 | if (const SCEVAddRecExpr *LA = dyn_cast<SCEVAddRecExpr>(LHS)) { |
| 505 | const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS); |
| 506 | if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth()) |
| 507 | return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth(); |
| 508 | } |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 509 | |
| 510 | // Lexicographically compare n-ary expressions. |
| 511 | if (const SCEVNAryExpr *LC = dyn_cast<SCEVNAryExpr>(LHS)) { |
| 512 | const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS); |
| 513 | for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) { |
| 514 | if (i >= RC->getNumOperands()) |
| 515 | return false; |
| 516 | if (operator()(LC->getOperand(i), RC->getOperand(i))) |
| 517 | return true; |
| 518 | if (operator()(RC->getOperand(i), LC->getOperand(i))) |
| 519 | return false; |
| 520 | } |
| 521 | return LC->getNumOperands() < RC->getNumOperands(); |
| 522 | } |
| 523 | |
Dan Gohman | a6b35e2 | 2009-05-07 19:23:21 +0000 | [diff] [blame] | 524 | // Lexicographically compare udiv expressions. |
| 525 | if (const SCEVUDivExpr *LC = dyn_cast<SCEVUDivExpr>(LHS)) { |
| 526 | const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS); |
| 527 | if (operator()(LC->getLHS(), RC->getLHS())) |
| 528 | return true; |
| 529 | if (operator()(RC->getLHS(), LC->getLHS())) |
| 530 | return false; |
| 531 | if (operator()(LC->getRHS(), RC->getRHS())) |
| 532 | return true; |
| 533 | if (operator()(RC->getRHS(), LC->getRHS())) |
| 534 | return false; |
| 535 | return false; |
| 536 | } |
| 537 | |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 538 | // Compare cast expressions by operand. |
| 539 | if (const SCEVCastExpr *LC = dyn_cast<SCEVCastExpr>(LHS)) { |
| 540 | const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS); |
| 541 | return operator()(LC->getOperand(), RC->getOperand()); |
| 542 | } |
| 543 | |
| 544 | assert(0 && "Unknown SCEV kind!"); |
| 545 | return false; |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 546 | } |
| 547 | }; |
| 548 | } |
| 549 | |
| 550 | /// GroupByComplexity - Given a list of SCEV objects, order them by their |
| 551 | /// complexity, and group objects of the same complexity together by value. |
| 552 | /// When this routine is finished, we know that any duplicates in the vector are |
| 553 | /// consecutive and that complexity is monotonically increasing. |
| 554 | /// |
| 555 | /// Note that we go take special precautions to ensure that we get determinstic |
| 556 | /// results from this routine. In other words, we don't want the results of |
| 557 | /// this to depend on where the addresses of various SCEV objects happened to |
| 558 | /// land in memory. |
| 559 | /// |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 560 | static void GroupByComplexity(SmallVectorImpl<const SCEV*> &Ops, |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 561 | LoopInfo *LI) { |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 562 | if (Ops.size() < 2) return; // Noop |
| 563 | if (Ops.size() == 2) { |
| 564 | // This is the common case, which also happens to be trivially simple. |
| 565 | // Special case it. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 566 | if (SCEVComplexityCompare(LI)(Ops[1], Ops[0])) |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 567 | std::swap(Ops[0], Ops[1]); |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | // Do the rough sort by complexity. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 572 | std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI)); |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 573 | |
| 574 | // Now that we are sorted by complexity, group elements of the same |
| 575 | // complexity. Note that this is, at worst, N^2, but the vector is likely to |
| 576 | // be extremely short in practice. Note that we take this approach because we |
| 577 | // do not want to depend on the addresses of the objects we are grouping. |
Chris Lattner | 2d58452 | 2004-06-20 17:01:44 +0000 | [diff] [blame] | 578 | for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 579 | const SCEV *S = Ops[i]; |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 580 | unsigned Complexity = S->getSCEVType(); |
| 581 | |
| 582 | // If there are any objects of the same complexity and same value as this |
| 583 | // one, group them. |
| 584 | for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { |
| 585 | if (Ops[j] == S) { // Found a duplicate. |
| 586 | // Move it to immediately after i'th element. |
| 587 | std::swap(Ops[i+1], Ops[j]); |
| 588 | ++i; // no need to rescan it. |
Chris Lattner | 541ad5e | 2004-06-20 20:32:16 +0000 | [diff] [blame] | 589 | if (i == e-2) return; // Done! |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 595 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 596 | |
| 597 | //===----------------------------------------------------------------------===// |
| 598 | // Simple SCEV method implementations |
| 599 | //===----------------------------------------------------------------------===// |
| 600 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 601 | /// BinomialCoefficient - Compute BC(It, K). The result has width W. |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 602 | /// Assume, K > 0. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 603 | static const SCEV* BinomialCoefficient(const SCEV* It, unsigned K, |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 604 | ScalarEvolution &SE, |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 605 | const Type* ResultTy) { |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 606 | // Handle the simplest case efficiently. |
| 607 | if (K == 1) |
| 608 | return SE.getTruncateOrZeroExtend(It, ResultTy); |
| 609 | |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 610 | // We are using the following formula for BC(It, K): |
| 611 | // |
| 612 | // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! |
| 613 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 614 | // Suppose, W is the bitwidth of the return value. We must be prepared for |
| 615 | // overflow. Hence, we must assure that the result of our computation is |
| 616 | // equal to the accurate one modulo 2^W. Unfortunately, division isn't |
| 617 | // safe in modular arithmetic. |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 618 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 619 | // 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] | 620 | // 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] | 621 | // K! (i.e. trailing zeros in the binary representation of K!), and ^ is |
| 622 | // exponentiation: |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 623 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 624 | // 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] | 625 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 626 | // This formula is trivially equivalent to the previous formula. However, |
| 627 | // this formula can be implemented much more efficiently. The trick is that |
| 628 | // K! / 2^T is odd, and exact division by an odd number *is* safe in modular |
| 629 | // arithmetic. To do exact division in modular arithmetic, all we have |
| 630 | // to do is multiply by the inverse. Therefore, this step can be done at |
| 631 | // width W. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 632 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 633 | // The next issue is how to safely do the division by 2^T. The way this |
| 634 | // is done is by doing the multiplication step at a width of at least W + T |
| 635 | // bits. This way, the bottom W+T bits of the product are accurate. Then, |
| 636 | // when we perform the division by 2^T (which is equivalent to a right shift |
| 637 | // by T), the bottom W bits are accurate. Extra bits are okay; they'll get |
| 638 | // truncated out after the division by 2^T. |
| 639 | // |
| 640 | // In comparison to just directly using the first formula, this technique |
| 641 | // is much more efficient; using the first formula requires W * K bits, |
| 642 | // but this formula less than W + K bits. Also, the first formula requires |
| 643 | // a division step, whereas this formula only requires multiplies and shifts. |
| 644 | // |
| 645 | // It doesn't matter whether the subtraction step is done in the calculation |
| 646 | // width or the input iteration count's width; if the subtraction overflows, |
| 647 | // the result must be zero anyway. We prefer here to do it in the width of |
| 648 | // the induction variable because it helps a lot for certain cases; CodeGen |
| 649 | // isn't smart enough to ignore the overflow, which leads to much less |
| 650 | // efficient code if the width of the subtraction is wider than the native |
| 651 | // register width. |
| 652 | // |
| 653 | // (It's possible to not widen at all by pulling out factors of 2 before |
| 654 | // the multiplication; for example, K=2 can be calculated as |
| 655 | // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires |
| 656 | // extra arithmetic, so it's not an obvious win, and it gets |
| 657 | // much more complicated for K > 3.) |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 658 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 659 | // Protection from insane SCEVs; this bound is conservative, |
| 660 | // but it probably doesn't matter. |
| 661 | if (K > 1000) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 662 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 663 | |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 664 | unsigned W = SE.getTypeSizeInBits(ResultTy); |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 665 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 666 | // Calculate K! / 2^T and T; we divide out the factors of two before |
| 667 | // multiplying for calculating K! / 2^T to avoid overflow. |
| 668 | // Other overflow doesn't matter because we only care about the bottom |
| 669 | // W bits of the result. |
| 670 | APInt OddFactorial(W, 1); |
| 671 | unsigned T = 1; |
| 672 | for (unsigned i = 3; i <= K; ++i) { |
| 673 | APInt Mult(W, i); |
| 674 | unsigned TwoFactors = Mult.countTrailingZeros(); |
| 675 | T += TwoFactors; |
| 676 | Mult = Mult.lshr(TwoFactors); |
| 677 | OddFactorial *= Mult; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 678 | } |
Nick Lewycky | 6f8abf9 | 2008-06-13 04:38:55 +0000 | [diff] [blame] | 679 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 680 | // We need at least W + T bits for the multiplication step |
Nick Lewycky | 237d873 | 2009-01-25 08:16:27 +0000 | [diff] [blame] | 681 | unsigned CalculationBits = W + T; |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 682 | |
| 683 | // Calcuate 2^T, at width T+W. |
| 684 | APInt DivFactor = APInt(CalculationBits, 1).shl(T); |
| 685 | |
| 686 | // Calculate the multiplicative inverse of K! / 2^T; |
| 687 | // this multiplication factor will perform the exact division by |
| 688 | // K! / 2^T. |
| 689 | APInt Mod = APInt::getSignedMinValue(W+1); |
| 690 | APInt MultiplyFactor = OddFactorial.zext(W+1); |
| 691 | MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); |
| 692 | MultiplyFactor = MultiplyFactor.trunc(W); |
| 693 | |
| 694 | // Calculate the product, at width T+W |
| 695 | const IntegerType *CalculationTy = IntegerType::get(CalculationBits); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 696 | const SCEV* Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 697 | for (unsigned i = 1; i != K; ++i) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 698 | const SCEV* S = SE.getMinusSCEV(It, SE.getIntegerSCEV(i, It->getType())); |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 699 | Dividend = SE.getMulExpr(Dividend, |
| 700 | SE.getTruncateOrZeroExtend(S, CalculationTy)); |
| 701 | } |
| 702 | |
| 703 | // Divide by 2^T |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 704 | const SCEV* DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 705 | |
| 706 | // Truncate the result, and divide by K! / 2^T. |
| 707 | |
| 708 | return SE.getMulExpr(SE.getConstant(MultiplyFactor), |
| 709 | SE.getTruncateOrZeroExtend(DivResult, ResultTy)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 712 | /// evaluateAtIteration - Return the value of this chain of recurrences at |
| 713 | /// the specified iteration number. We can evaluate this recurrence by |
| 714 | /// multiplying each element in the chain by the binomial coefficient |
| 715 | /// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as: |
| 716 | /// |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 717 | /// 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] | 718 | /// |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 719 | /// where BC(It, k) stands for binomial coefficient. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 720 | /// |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 721 | const SCEV* SCEVAddRecExpr::evaluateAtIteration(const SCEV* It, |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 722 | ScalarEvolution &SE) const { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 723 | const SCEV* Result = getStart(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 724 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 725 | // The computation is correct in the face of overflow provided that the |
| 726 | // multiplication is performed _after_ the evaluation of the binomial |
| 727 | // coefficient. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 728 | const SCEV* Coeff = BinomialCoefficient(It, i, SE, getType()); |
Nick Lewycky | cb8f1b5 | 2008-10-13 03:58:02 +0000 | [diff] [blame] | 729 | if (isa<SCEVCouldNotCompute>(Coeff)) |
| 730 | return Coeff; |
| 731 | |
| 732 | Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 733 | } |
| 734 | return Result; |
| 735 | } |
| 736 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 737 | //===----------------------------------------------------------------------===// |
| 738 | // SCEV Expression folder implementations |
| 739 | //===----------------------------------------------------------------------===// |
| 740 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 741 | const SCEV* ScalarEvolution::getTruncateExpr(const SCEV* Op, |
Dan Gohman | 99243b3 | 2009-05-01 16:44:56 +0000 | [diff] [blame] | 742 | const Type *Ty) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 743 | assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 744 | "This is not a truncating conversion!"); |
Dan Gohman | 10b9479 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 745 | assert(isSCEVable(Ty) && |
| 746 | "This is not a conversion to a SCEVable type!"); |
| 747 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 748 | |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 749 | // Fold if the operand is constant. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 750 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
Dan Gohman | b8be8b7 | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 751 | return getConstant( |
| 752 | cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 753 | |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 754 | // trunc(trunc(x)) --> trunc(x) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 755 | if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 756 | return getTruncateExpr(ST->getOperand(), Ty); |
| 757 | |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 758 | // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 759 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 760 | return getTruncateOrSignExtend(SS->getOperand(), Ty); |
| 761 | |
| 762 | // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 763 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 764 | return getTruncateOrZeroExtend(SZ->getOperand(), Ty); |
| 765 | |
Dan Gohman | 6864db6 | 2009-06-18 16:24:47 +0000 | [diff] [blame] | 766 | // 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] | 767 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 768 | SmallVector<const SCEV*, 4> Operands; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 769 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 770 | Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty)); |
| 771 | return getAddRecExpr(Operands, AddRec->getLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 774 | FoldingSetNodeID ID; |
| 775 | ID.AddInteger(scTruncate); |
| 776 | ID.AddPointer(Op); |
| 777 | ID.AddPointer(Ty); |
| 778 | void *IP = 0; |
| 779 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 780 | SCEV *S = SCEVAllocator.Allocate<SCEVTruncateExpr>(); |
| 781 | new (S) SCEVTruncateExpr(Op, Ty); |
| 782 | UniqueSCEVs.InsertNode(S, IP); |
| 783 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 786 | const SCEV* ScalarEvolution::getZeroExtendExpr(const SCEV* Op, |
Dan Gohman | 8170a68 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 787 | const Type *Ty) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 788 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | 8170a68 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 789 | "This is not an extending conversion!"); |
Dan Gohman | 10b9479 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 790 | assert(isSCEVable(Ty) && |
| 791 | "This is not a conversion to a SCEVable type!"); |
| 792 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | 8170a68 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 793 | |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 794 | // Fold if the operand is constant. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 795 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 796 | const Type *IntTy = getEffectiveSCEVType(Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 797 | Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy); |
| 798 | if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); |
Dan Gohman | b8be8b7 | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 799 | return getConstant(cast<ConstantInt>(C)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 800 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 801 | |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 802 | // zext(zext(x)) --> zext(x) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 803 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 804 | return getZeroExtendExpr(SZ->getOperand(), Ty); |
| 805 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 806 | // 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] | 807 | // 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] | 808 | // operands (often constants). This allows analysis of something like |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 809 | // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 810 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 811 | if (AR->isAffine()) { |
| 812 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 813 | // Note that this serves two purposes: It filters out loops that are |
| 814 | // simply not analyzable, and it covers the case where this code is |
| 815 | // being called from within backedge-taken count analysis, such that |
| 816 | // attempting to ask for the backedge-taken count would likely result |
| 817 | // in infinite recursion. In the later case, the analysis code will |
| 818 | // cope with a conservative value, and it will take care to purge |
| 819 | // that value once it has finished. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 820 | const SCEV* MaxBECount = getMaxBackedgeTakenCount(AR->getLoop()); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 821 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | f0aa485 | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 822 | // Manually compute the final value for AR, checking for |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 823 | // overflow. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 824 | const SCEV* Start = AR->getStart(); |
| 825 | const SCEV* Step = AR->getStepRecurrence(*this); |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 826 | |
| 827 | // Check whether the backedge-taken count can be losslessly casted to |
| 828 | // the addrec's type. The count is always unsigned. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 829 | const SCEV* CastedMaxBECount = |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 830 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 831 | const SCEV* RecastedMaxBECount = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 832 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 833 | if (MaxBECount == RecastedMaxBECount) { |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 834 | const Type *WideTy = |
| 835 | IntegerType::get(getTypeSizeInBits(Start->getType()) * 2); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 836 | // Check whether Start+Step*MaxBECount has no unsigned overflow. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 837 | const SCEV* ZMul = |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 838 | getMulExpr(CastedMaxBECount, |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 839 | getTruncateOrZeroExtend(Step, Start->getType())); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 840 | const SCEV* Add = getAddExpr(Start, ZMul); |
| 841 | const SCEV* OperandExtendedAdd = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 842 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 843 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 844 | getZeroExtendExpr(Step, WideTy))); |
| 845 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 846 | // Return the expression with the addrec on the outside. |
| 847 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 848 | getZeroExtendExpr(Step, Ty), |
| 849 | AR->getLoop()); |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 850 | |
| 851 | // Similar to above, only this time treat the step value as signed. |
| 852 | // This covers loops that count down. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 853 | const SCEV* SMul = |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 854 | getMulExpr(CastedMaxBECount, |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 855 | getTruncateOrSignExtend(Step, Start->getType())); |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 856 | Add = getAddExpr(Start, SMul); |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 857 | OperandExtendedAdd = |
| 858 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 859 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 860 | getSignExtendExpr(Step, WideTy))); |
| 861 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 862 | // Return the expression with the addrec on the outside. |
| 863 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 864 | getSignExtendExpr(Step, Ty), |
| 865 | AR->getLoop()); |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 869 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 870 | FoldingSetNodeID ID; |
| 871 | ID.AddInteger(scZeroExtend); |
| 872 | ID.AddPointer(Op); |
| 873 | ID.AddPointer(Ty); |
| 874 | void *IP = 0; |
| 875 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 876 | SCEV *S = SCEVAllocator.Allocate<SCEVZeroExtendExpr>(); |
| 877 | new (S) SCEVZeroExtendExpr(Op, Ty); |
| 878 | UniqueSCEVs.InsertNode(S, IP); |
| 879 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 882 | const SCEV* ScalarEvolution::getSignExtendExpr(const SCEV* Op, |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 883 | const Type *Ty) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 884 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 885 | "This is not an extending conversion!"); |
Dan Gohman | 10b9479 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 886 | assert(isSCEVable(Ty) && |
| 887 | "This is not a conversion to a SCEVable type!"); |
| 888 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 889 | |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 890 | // Fold if the operand is constant. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 891 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 892 | const Type *IntTy = getEffectiveSCEVType(Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 893 | Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy); |
| 894 | if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); |
Dan Gohman | b8be8b7 | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 895 | return getConstant(cast<ConstantInt>(C)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 896 | } |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 897 | |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 898 | // sext(sext(x)) --> sext(x) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 899 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 900 | return getSignExtendExpr(SS->getOperand(), Ty); |
| 901 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 902 | // 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] | 903 | // 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] | 904 | // operands (often constants). This allows analysis of something like |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 905 | // this: for (signed char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 906 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 907 | if (AR->isAffine()) { |
| 908 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 909 | // Note that this serves two purposes: It filters out loops that are |
| 910 | // simply not analyzable, and it covers the case where this code is |
| 911 | // being called from within backedge-taken count analysis, such that |
| 912 | // attempting to ask for the backedge-taken count would likely result |
| 913 | // in infinite recursion. In the later case, the analysis code will |
| 914 | // cope with a conservative value, and it will take care to purge |
| 915 | // that value once it has finished. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 916 | const SCEV* MaxBECount = getMaxBackedgeTakenCount(AR->getLoop()); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 917 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | f0aa485 | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 918 | // Manually compute the final value for AR, checking for |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 919 | // overflow. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 920 | const SCEV* Start = AR->getStart(); |
| 921 | const SCEV* Step = AR->getStepRecurrence(*this); |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 922 | |
| 923 | // Check whether the backedge-taken count can be losslessly casted to |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 924 | // the addrec's type. The count is always unsigned. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 925 | const SCEV* CastedMaxBECount = |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 926 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 927 | const SCEV* RecastedMaxBECount = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 928 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 929 | if (MaxBECount == RecastedMaxBECount) { |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 930 | const Type *WideTy = |
| 931 | IntegerType::get(getTypeSizeInBits(Start->getType()) * 2); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 932 | // Check whether Start+Step*MaxBECount has no signed overflow. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 933 | const SCEV* SMul = |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 934 | getMulExpr(CastedMaxBECount, |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 935 | getTruncateOrSignExtend(Step, Start->getType())); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 936 | const SCEV* Add = getAddExpr(Start, SMul); |
| 937 | const SCEV* OperandExtendedAdd = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 938 | getAddExpr(getSignExtendExpr(Start, WideTy), |
| 939 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 940 | getSignExtendExpr(Step, WideTy))); |
| 941 | if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 942 | // Return the expression with the addrec on the outside. |
| 943 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 944 | getSignExtendExpr(Step, Ty), |
| 945 | AR->getLoop()); |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | } |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 949 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 950 | FoldingSetNodeID ID; |
| 951 | ID.AddInteger(scSignExtend); |
| 952 | ID.AddPointer(Op); |
| 953 | ID.AddPointer(Ty); |
| 954 | void *IP = 0; |
| 955 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 956 | SCEV *S = SCEVAllocator.Allocate<SCEVSignExtendExpr>(); |
| 957 | new (S) SCEVSignExtendExpr(Op, Ty); |
| 958 | UniqueSCEVs.InsertNode(S, IP); |
| 959 | return S; |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 962 | /// getAnyExtendExpr - Return a SCEV for the given operand extended with |
| 963 | /// unspecified bits out to the given type. |
| 964 | /// |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 965 | const SCEV* ScalarEvolution::getAnyExtendExpr(const SCEV* Op, |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 966 | const Type *Ty) { |
| 967 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
| 968 | "This is not an extending conversion!"); |
| 969 | assert(isSCEVable(Ty) && |
| 970 | "This is not a conversion to a SCEVable type!"); |
| 971 | Ty = getEffectiveSCEVType(Ty); |
| 972 | |
| 973 | // Sign-extend negative constants. |
| 974 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| 975 | if (SC->getValue()->getValue().isNegative()) |
| 976 | return getSignExtendExpr(Op, Ty); |
| 977 | |
| 978 | // Peel off a truncate cast. |
| 979 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 980 | const SCEV* NewOp = T->getOperand(); |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 981 | if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty)) |
| 982 | return getAnyExtendExpr(NewOp, Ty); |
| 983 | return getTruncateOrNoop(NewOp, Ty); |
| 984 | } |
| 985 | |
| 986 | // Next try a zext cast. If the cast is folded, use it. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 987 | const SCEV* ZExt = getZeroExtendExpr(Op, Ty); |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 988 | if (!isa<SCEVZeroExtendExpr>(ZExt)) |
| 989 | return ZExt; |
| 990 | |
| 991 | // Next try a sext cast. If the cast is folded, use it. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 992 | const SCEV* SExt = getSignExtendExpr(Op, Ty); |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 993 | if (!isa<SCEVSignExtendExpr>(SExt)) |
| 994 | return SExt; |
| 995 | |
| 996 | // If the expression is obviously signed, use the sext cast value. |
| 997 | if (isa<SCEVSMaxExpr>(Op)) |
| 998 | return SExt; |
| 999 | |
| 1000 | // Absent any other information, use the zext cast value. |
| 1001 | return ZExt; |
| 1002 | } |
| 1003 | |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1004 | /// CollectAddOperandsWithScales - Process the given Ops list, which is |
| 1005 | /// a list of operands to be added under the given scale, update the given |
| 1006 | /// map. This is a helper function for getAddRecExpr. As an example of |
| 1007 | /// what it does, given a sequence of operands that would form an add |
| 1008 | /// expression like this: |
| 1009 | /// |
| 1010 | /// m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r) |
| 1011 | /// |
| 1012 | /// where A and B are constants, update the map with these values: |
| 1013 | /// |
| 1014 | /// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0) |
| 1015 | /// |
| 1016 | /// and add 13 + A*B*29 to AccumulatedConstant. |
| 1017 | /// This will allow getAddRecExpr to produce this: |
| 1018 | /// |
| 1019 | /// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B) |
| 1020 | /// |
| 1021 | /// This form often exposes folding opportunities that are hidden in |
| 1022 | /// the original operand list. |
| 1023 | /// |
| 1024 | /// Return true iff it appears that any interesting folding opportunities |
| 1025 | /// may be exposed. This helps getAddRecExpr short-circuit extra work in |
| 1026 | /// the common case where no interesting opportunities are present, and |
| 1027 | /// is also used as a check to avoid infinite recursion. |
| 1028 | /// |
| 1029 | static bool |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1030 | CollectAddOperandsWithScales(DenseMap<const SCEV*, APInt> &M, |
| 1031 | SmallVector<const SCEV*, 8> &NewOps, |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1032 | APInt &AccumulatedConstant, |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1033 | const SmallVectorImpl<const SCEV*> &Ops, |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1034 | const APInt &Scale, |
| 1035 | ScalarEvolution &SE) { |
| 1036 | bool Interesting = false; |
| 1037 | |
| 1038 | // Iterate over the add operands. |
| 1039 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 1040 | const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]); |
| 1041 | if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) { |
| 1042 | APInt NewScale = |
| 1043 | Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue(); |
| 1044 | if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) { |
| 1045 | // A multiplication of a constant with another add; recurse. |
| 1046 | Interesting |= |
| 1047 | CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
| 1048 | cast<SCEVAddExpr>(Mul->getOperand(1)) |
| 1049 | ->getOperands(), |
| 1050 | NewScale, SE); |
| 1051 | } else { |
| 1052 | // A multiplication of a constant with some other value. Update |
| 1053 | // the map. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1054 | SmallVector<const SCEV*, 4> MulOps(Mul->op_begin()+1, Mul->op_end()); |
| 1055 | const SCEV* Key = SE.getMulExpr(MulOps); |
| 1056 | std::pair<DenseMap<const SCEV*, APInt>::iterator, bool> Pair = |
Dan Gohman | 23737e0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1057 | M.insert(std::make_pair(Key, NewScale)); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1058 | if (Pair.second) { |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1059 | NewOps.push_back(Pair.first->first); |
| 1060 | } else { |
| 1061 | Pair.first->second += NewScale; |
| 1062 | // The map already had an entry for this value, which may indicate |
| 1063 | // a folding opportunity. |
| 1064 | Interesting = true; |
| 1065 | } |
| 1066 | } |
| 1067 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| 1068 | // Pull a buried constant out to the outside. |
| 1069 | if (Scale != 1 || AccumulatedConstant != 0 || C->isZero()) |
| 1070 | Interesting = true; |
| 1071 | AccumulatedConstant += Scale * C->getValue()->getValue(); |
| 1072 | } else { |
| 1073 | // An ordinary operand. Update the map. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1074 | std::pair<DenseMap<const SCEV*, APInt>::iterator, bool> Pair = |
Dan Gohman | 23737e0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1075 | M.insert(std::make_pair(Ops[i], Scale)); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1076 | if (Pair.second) { |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1077 | NewOps.push_back(Pair.first->first); |
| 1078 | } else { |
| 1079 | Pair.first->second += Scale; |
| 1080 | // The map already had an entry for this value, which may indicate |
| 1081 | // a folding opportunity. |
| 1082 | Interesting = true; |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | return Interesting; |
| 1088 | } |
| 1089 | |
| 1090 | namespace { |
| 1091 | struct APIntCompare { |
| 1092 | bool operator()(const APInt &LHS, const APInt &RHS) const { |
| 1093 | return LHS.ult(RHS); |
| 1094 | } |
| 1095 | }; |
| 1096 | } |
| 1097 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1098 | /// getAddExpr - Get a canonical add expression, or something simpler if |
| 1099 | /// possible. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1100 | const SCEV* ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV*> &Ops) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1101 | assert(!Ops.empty() && "Cannot get empty add!"); |
Chris Lattner | 627018b | 2004-04-07 16:16:11 +0000 | [diff] [blame] | 1102 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1103 | #ifndef NDEBUG |
| 1104 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1105 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1106 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1107 | "SCEVAddExpr operand types don't match!"); |
| 1108 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1109 | |
| 1110 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1111 | GroupByComplexity(Ops, LI); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1112 | |
| 1113 | // If there are any constants, fold them together. |
| 1114 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1115 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1116 | ++Idx; |
Chris Lattner | 627018b | 2004-04-07 16:16:11 +0000 | [diff] [blame] | 1117 | assert(Idx < Ops.size()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1118 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1119 | // We found two constants, fold them together! |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1120 | Ops[0] = getConstant(LHSC->getValue()->getValue() + |
| 1121 | RHSC->getValue()->getValue()); |
Dan Gohman | 7f7c436 | 2009-06-14 22:53:57 +0000 | [diff] [blame] | 1122 | if (Ops.size() == 2) return Ops[0]; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1123 | Ops.erase(Ops.begin()+1); // Erase the folded element |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1124 | LHSC = cast<SCEVConstant>(Ops[0]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | // If we are left with a constant zero being added, strip it off. |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1128 | if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1129 | Ops.erase(Ops.begin()); |
| 1130 | --Idx; |
| 1131 | } |
| 1132 | } |
| 1133 | |
Chris Lattner | 627018b | 2004-04-07 16:16:11 +0000 | [diff] [blame] | 1134 | if (Ops.size() == 1) return Ops[0]; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1135 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1136 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1137 | // so, merge them together into an multiply expression. Since we sorted the |
| 1138 | // list, these values are required to be adjacent. |
| 1139 | const Type *Ty = Ops[0]->getType(); |
| 1140 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1141 | if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2 |
| 1142 | // Found a match, merge the two values into a multiply, and add any |
| 1143 | // remaining values to the result. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1144 | const SCEV* Two = getIntegerSCEV(2, Ty); |
| 1145 | const SCEV* Mul = getMulExpr(Ops[i], Two); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1146 | if (Ops.size() == 2) |
| 1147 | return Mul; |
| 1148 | Ops.erase(Ops.begin()+i, Ops.begin()+i+2); |
| 1149 | Ops.push_back(Mul); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1150 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1153 | // Check for truncates. If all the operands are truncated from the same |
| 1154 | // type, see if factoring out the truncate would permit the result to be |
| 1155 | // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n) |
| 1156 | // if the contents of the resulting outer trunc fold to something simple. |
| 1157 | for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) { |
| 1158 | const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]); |
| 1159 | const Type *DstType = Trunc->getType(); |
| 1160 | const Type *SrcType = Trunc->getOperand()->getType(); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1161 | SmallVector<const SCEV*, 8> LargeOps; |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1162 | bool Ok = true; |
| 1163 | // Check all the operands to see if they can be represented in the |
| 1164 | // source type of the truncate. |
| 1165 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 1166 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) { |
| 1167 | if (T->getOperand()->getType() != SrcType) { |
| 1168 | Ok = false; |
| 1169 | break; |
| 1170 | } |
| 1171 | LargeOps.push_back(T->getOperand()); |
| 1172 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| 1173 | // This could be either sign or zero extension, but sign extension |
| 1174 | // is much more likely to be foldable here. |
| 1175 | LargeOps.push_back(getSignExtendExpr(C, SrcType)); |
| 1176 | } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1177 | SmallVector<const SCEV*, 8> LargeMulOps; |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1178 | for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { |
| 1179 | if (const SCEVTruncateExpr *T = |
| 1180 | dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) { |
| 1181 | if (T->getOperand()->getType() != SrcType) { |
| 1182 | Ok = false; |
| 1183 | break; |
| 1184 | } |
| 1185 | LargeMulOps.push_back(T->getOperand()); |
| 1186 | } else if (const SCEVConstant *C = |
| 1187 | dyn_cast<SCEVConstant>(M->getOperand(j))) { |
| 1188 | // This could be either sign or zero extension, but sign extension |
| 1189 | // is much more likely to be foldable here. |
| 1190 | LargeMulOps.push_back(getSignExtendExpr(C, SrcType)); |
| 1191 | } else { |
| 1192 | Ok = false; |
| 1193 | break; |
| 1194 | } |
| 1195 | } |
| 1196 | if (Ok) |
| 1197 | LargeOps.push_back(getMulExpr(LargeMulOps)); |
| 1198 | } else { |
| 1199 | Ok = false; |
| 1200 | break; |
| 1201 | } |
| 1202 | } |
| 1203 | if (Ok) { |
| 1204 | // Evaluate the expression in the larger type. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1205 | const SCEV* Fold = getAddExpr(LargeOps); |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1206 | // If it folds to something simple, use it. Otherwise, don't. |
| 1207 | if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold)) |
| 1208 | return getTruncateExpr(Fold, DstType); |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | // Skip past any other cast SCEVs. |
Dan Gohman | f50cd74 | 2007-06-18 19:30:09 +0000 | [diff] [blame] | 1213 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) |
| 1214 | ++Idx; |
| 1215 | |
| 1216 | // If there are add operands they would be next. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1217 | if (Idx < Ops.size()) { |
| 1218 | bool DeletedAdd = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1219 | while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1220 | // If we have an add, expand the add operands onto the end of the operands |
| 1221 | // list. |
| 1222 | Ops.insert(Ops.end(), Add->op_begin(), Add->op_end()); |
| 1223 | Ops.erase(Ops.begin()+Idx); |
| 1224 | DeletedAdd = true; |
| 1225 | } |
| 1226 | |
| 1227 | // If we deleted at least one add, we added operands to the end of the list, |
| 1228 | // and they are not necessarily sorted. Recurse to resort and resimplify |
| 1229 | // any operands we just aquired. |
| 1230 | if (DeletedAdd) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1231 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | // Skip over the add expression until we get to a multiply. |
| 1235 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1236 | ++Idx; |
| 1237 | |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1238 | // Check to see if there are any folding opportunities present with |
| 1239 | // operands multiplied by constant values. |
| 1240 | if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) { |
| 1241 | uint64_t BitWidth = getTypeSizeInBits(Ty); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1242 | DenseMap<const SCEV*, APInt> M; |
| 1243 | SmallVector<const SCEV*, 8> NewOps; |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1244 | APInt AccumulatedConstant(BitWidth, 0); |
| 1245 | if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
| 1246 | Ops, APInt(BitWidth, 1), *this)) { |
| 1247 | // Some interesting folding opportunity is present, so its worthwhile to |
| 1248 | // re-generate the operands list. Group the operands by constant scale, |
| 1249 | // to avoid multiplying by the same constant scale multiple times. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1250 | std::map<APInt, SmallVector<const SCEV*, 4>, APIntCompare> MulOpLists; |
| 1251 | for (SmallVector<const SCEV*, 8>::iterator I = NewOps.begin(), |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1252 | E = NewOps.end(); I != E; ++I) |
| 1253 | MulOpLists[M.find(*I)->second].push_back(*I); |
| 1254 | // Re-generate the operands list. |
| 1255 | Ops.clear(); |
| 1256 | if (AccumulatedConstant != 0) |
| 1257 | Ops.push_back(getConstant(AccumulatedConstant)); |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1258 | for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator |
| 1259 | I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I) |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1260 | if (I->first != 0) |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1261 | Ops.push_back(getMulExpr(getConstant(I->first), |
| 1262 | getAddExpr(I->second))); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1263 | if (Ops.empty()) |
| 1264 | return getIntegerSCEV(0, Ty); |
| 1265 | if (Ops.size() == 1) |
| 1266 | return Ops[0]; |
| 1267 | return getAddExpr(Ops); |
| 1268 | } |
| 1269 | } |
| 1270 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1271 | // If we are adding something to a multiply expression, make sure the |
| 1272 | // something is not already an operand of the multiply. If so, merge it into |
| 1273 | // the multiply. |
| 1274 | for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1275 | const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1276 | for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1277 | const SCEV *MulOpSCEV = Mul->getOperand(MulOp); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1278 | for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1279 | if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(Ops[AddOp])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1280 | // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1281 | const SCEV* InnerMul = Mul->getOperand(MulOp == 0); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1282 | if (Mul->getNumOperands() != 2) { |
| 1283 | // If the multiply has more than two operands, we must get the |
| 1284 | // Y*Z term. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1285 | SmallVector<const SCEV*, 4> MulOps(Mul->op_begin(), Mul->op_end()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1286 | MulOps.erase(MulOps.begin()+MulOp); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1287 | InnerMul = getMulExpr(MulOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1288 | } |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1289 | const SCEV* One = getIntegerSCEV(1, Ty); |
| 1290 | const SCEV* AddOne = getAddExpr(InnerMul, One); |
| 1291 | const SCEV* OuterMul = getMulExpr(AddOne, Ops[AddOp]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1292 | if (Ops.size() == 2) return OuterMul; |
| 1293 | if (AddOp < Idx) { |
| 1294 | Ops.erase(Ops.begin()+AddOp); |
| 1295 | Ops.erase(Ops.begin()+Idx-1); |
| 1296 | } else { |
| 1297 | Ops.erase(Ops.begin()+Idx); |
| 1298 | Ops.erase(Ops.begin()+AddOp-1); |
| 1299 | } |
| 1300 | Ops.push_back(OuterMul); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1301 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1302 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1303 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1304 | // Check this multiply against other multiplies being added together. |
| 1305 | for (unsigned OtherMulIdx = Idx+1; |
| 1306 | OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]); |
| 1307 | ++OtherMulIdx) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1308 | const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1309 | // If MulOp occurs in OtherMul, we can fold the two multiplies |
| 1310 | // together. |
| 1311 | for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); |
| 1312 | OMulOp != e; ++OMulOp) |
| 1313 | if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { |
| 1314 | // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E)) |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1315 | const SCEV* InnerMul1 = Mul->getOperand(MulOp == 0); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1316 | if (Mul->getNumOperands() != 2) { |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1317 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), |
| 1318 | Mul->op_end()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1319 | MulOps.erase(MulOps.begin()+MulOp); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1320 | InnerMul1 = getMulExpr(MulOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1321 | } |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1322 | const SCEV* InnerMul2 = OtherMul->getOperand(OMulOp == 0); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1323 | if (OtherMul->getNumOperands() != 2) { |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1324 | SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(), |
| 1325 | OtherMul->op_end()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1326 | MulOps.erase(MulOps.begin()+OMulOp); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1327 | InnerMul2 = getMulExpr(MulOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1328 | } |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1329 | const SCEV* InnerMulSum = getAddExpr(InnerMul1,InnerMul2); |
| 1330 | const SCEV* OuterMul = getMulExpr(MulOpSCEV, InnerMulSum); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1331 | if (Ops.size() == 2) return OuterMul; |
| 1332 | Ops.erase(Ops.begin()+Idx); |
| 1333 | Ops.erase(Ops.begin()+OtherMulIdx-1); |
| 1334 | Ops.push_back(OuterMul); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1335 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | // If there are any add recurrences in the operands list, see if any other |
| 1342 | // added values are loop invariant. If so, we can fold them into the |
| 1343 | // recurrence. |
| 1344 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1345 | ++Idx; |
| 1346 | |
| 1347 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1348 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1349 | // Scan all of the other operands to this add and add them to the vector if |
| 1350 | // they are loop invariant w.r.t. the recurrence. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1351 | SmallVector<const SCEV*, 8> LIOps; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1352 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1353 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1354 | if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { |
| 1355 | LIOps.push_back(Ops[i]); |
| 1356 | Ops.erase(Ops.begin()+i); |
| 1357 | --i; --e; |
| 1358 | } |
| 1359 | |
| 1360 | // If we found some loop invariants, fold them into the recurrence. |
| 1361 | if (!LIOps.empty()) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1362 | // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step} |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1363 | LIOps.push_back(AddRec->getStart()); |
| 1364 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1365 | SmallVector<const SCEV*, 4> AddRecOps(AddRec->op_begin(), |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1366 | AddRec->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1367 | AddRecOps[0] = getAddExpr(LIOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1368 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1369 | const SCEV* NewRec = getAddRecExpr(AddRecOps, AddRec->getLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1370 | // If all of the other operands were loop invariant, we are done. |
| 1371 | if (Ops.size() == 1) return NewRec; |
| 1372 | |
| 1373 | // Otherwise, add the folded AddRec by the non-liv parts. |
| 1374 | for (unsigned i = 0;; ++i) |
| 1375 | if (Ops[i] == AddRec) { |
| 1376 | Ops[i] = NewRec; |
| 1377 | break; |
| 1378 | } |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1379 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1383 | // there are multiple AddRec's with the same loop induction variable being |
| 1384 | // added together. If so, we can fold them. |
| 1385 | for (unsigned OtherIdx = Idx+1; |
| 1386 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) |
| 1387 | if (OtherIdx != Idx) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1388 | const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1389 | if (AddRec->getLoop() == OtherAddRec->getLoop()) { |
| 1390 | // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D} |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1391 | SmallVector<const SCEV *, 4> NewOps(AddRec->op_begin(), |
| 1392 | AddRec->op_end()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1393 | for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) { |
| 1394 | if (i >= NewOps.size()) { |
| 1395 | NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i, |
| 1396 | OtherAddRec->op_end()); |
| 1397 | break; |
| 1398 | } |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1399 | NewOps[i] = getAddExpr(NewOps[i], OtherAddRec->getOperand(i)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1400 | } |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1401 | const SCEV* NewAddRec = getAddRecExpr(NewOps, AddRec->getLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1402 | |
| 1403 | if (Ops.size() == 2) return NewAddRec; |
| 1404 | |
| 1405 | Ops.erase(Ops.begin()+Idx); |
| 1406 | Ops.erase(Ops.begin()+OtherIdx-1); |
| 1407 | Ops.push_back(NewAddRec); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1408 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1413 | // next one. |
| 1414 | } |
| 1415 | |
| 1416 | // Okay, it looks like we really DO need an add expr. Check to see if we |
| 1417 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1418 | FoldingSetNodeID ID; |
| 1419 | ID.AddInteger(scAddExpr); |
| 1420 | ID.AddInteger(Ops.size()); |
| 1421 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1422 | ID.AddPointer(Ops[i]); |
| 1423 | void *IP = 0; |
| 1424 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1425 | SCEV *S = SCEVAllocator.Allocate<SCEVAddExpr>(); |
| 1426 | new (S) SCEVAddExpr(Ops); |
| 1427 | UniqueSCEVs.InsertNode(S, IP); |
| 1428 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1432 | /// getMulExpr - Get a canonical multiply expression, or something simpler if |
| 1433 | /// possible. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1434 | const SCEV* ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV*> &Ops) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1435 | assert(!Ops.empty() && "Cannot get empty mul!"); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1436 | #ifndef NDEBUG |
| 1437 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1438 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1439 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1440 | "SCEVMulExpr operand types don't match!"); |
| 1441 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1442 | |
| 1443 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1444 | GroupByComplexity(Ops, LI); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1445 | |
| 1446 | // If there are any constants, fold them together. |
| 1447 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1448 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1449 | |
| 1450 | // C1*(C2+V) -> C1*C2 + C1*V |
| 1451 | if (Ops.size() == 2) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1452 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1453 | if (Add->getNumOperands() == 2 && |
| 1454 | isa<SCEVConstant>(Add->getOperand(0))) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1455 | return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)), |
| 1456 | getMulExpr(LHSC, Add->getOperand(1))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1457 | |
| 1458 | |
| 1459 | ++Idx; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1460 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1461 | // We found two constants, fold them together! |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1462 | ConstantInt *Fold = ConstantInt::get(LHSC->getValue()->getValue() * |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1463 | RHSC->getValue()->getValue()); |
| 1464 | Ops[0] = getConstant(Fold); |
| 1465 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1466 | if (Ops.size() == 1) return Ops[0]; |
| 1467 | LHSC = cast<SCEVConstant>(Ops[0]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | // If we are left with a constant one being multiplied, strip it off. |
| 1471 | if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) { |
| 1472 | Ops.erase(Ops.begin()); |
| 1473 | --Idx; |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1474 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1475 | // If we have a multiply of zero, it will always be zero. |
| 1476 | return Ops[0]; |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | // Skip over the add expression until we get to a multiply. |
| 1481 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1482 | ++Idx; |
| 1483 | |
| 1484 | if (Ops.size() == 1) |
| 1485 | return Ops[0]; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1486 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1487 | // If there are mul operands inline them all into this expression. |
| 1488 | if (Idx < Ops.size()) { |
| 1489 | bool DeletedMul = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1490 | while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1491 | // If we have an mul, expand the mul operands onto the end of the operands |
| 1492 | // list. |
| 1493 | Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end()); |
| 1494 | Ops.erase(Ops.begin()+Idx); |
| 1495 | DeletedMul = true; |
| 1496 | } |
| 1497 | |
| 1498 | // If we deleted at least one mul, we added operands to the end of the list, |
| 1499 | // and they are not necessarily sorted. Recurse to resort and resimplify |
| 1500 | // any operands we just aquired. |
| 1501 | if (DeletedMul) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1502 | return getMulExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | // If there are any add recurrences in the operands list, see if any other |
| 1506 | // added values are loop invariant. If so, we can fold them into the |
| 1507 | // recurrence. |
| 1508 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1509 | ++Idx; |
| 1510 | |
| 1511 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1512 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1513 | // Scan all of the other operands to this mul and add them to the vector if |
| 1514 | // they are loop invariant w.r.t. the recurrence. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1515 | SmallVector<const SCEV*, 8> LIOps; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1516 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1517 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1518 | if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { |
| 1519 | LIOps.push_back(Ops[i]); |
| 1520 | Ops.erase(Ops.begin()+i); |
| 1521 | --i; --e; |
| 1522 | } |
| 1523 | |
| 1524 | // If we found some loop invariants, fold them into the recurrence. |
| 1525 | if (!LIOps.empty()) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1526 | // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step} |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1527 | SmallVector<const SCEV*, 4> NewOps; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1528 | NewOps.reserve(AddRec->getNumOperands()); |
| 1529 | if (LIOps.size() == 1) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1530 | const SCEV *Scale = LIOps[0]; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1531 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1532 | NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1533 | } else { |
| 1534 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1535 | SmallVector<const SCEV*, 4> MulOps(LIOps.begin(), LIOps.end()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1536 | MulOps.push_back(AddRec->getOperand(i)); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1537 | NewOps.push_back(getMulExpr(MulOps)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1538 | } |
| 1539 | } |
| 1540 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1541 | const SCEV* NewRec = getAddRecExpr(NewOps, AddRec->getLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1542 | |
| 1543 | // If all of the other operands were loop invariant, we are done. |
| 1544 | if (Ops.size() == 1) return NewRec; |
| 1545 | |
| 1546 | // Otherwise, multiply the folded AddRec by the non-liv parts. |
| 1547 | for (unsigned i = 0;; ++i) |
| 1548 | if (Ops[i] == AddRec) { |
| 1549 | Ops[i] = NewRec; |
| 1550 | break; |
| 1551 | } |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1552 | return getMulExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1556 | // there are multiple AddRec's with the same loop induction variable being |
| 1557 | // multiplied together. If so, we can fold them. |
| 1558 | for (unsigned OtherIdx = Idx+1; |
| 1559 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) |
| 1560 | if (OtherIdx != Idx) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1561 | const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1562 | if (AddRec->getLoop() == OtherAddRec->getLoop()) { |
| 1563 | // F * G --> {A,+,B} * {C,+,D} --> {A*C,+,F*D + G*B + B*D} |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1564 | const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec; |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1565 | const SCEV* NewStart = getMulExpr(F->getStart(), |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1566 | G->getStart()); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1567 | const SCEV* B = F->getStepRecurrence(*this); |
| 1568 | const SCEV* D = G->getStepRecurrence(*this); |
| 1569 | const SCEV* NewStep = getAddExpr(getMulExpr(F, D), |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1570 | getMulExpr(G, B), |
| 1571 | getMulExpr(B, D)); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1572 | const SCEV* NewAddRec = getAddRecExpr(NewStart, NewStep, |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1573 | F->getLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1574 | if (Ops.size() == 2) return NewAddRec; |
| 1575 | |
| 1576 | Ops.erase(Ops.begin()+Idx); |
| 1577 | Ops.erase(Ops.begin()+OtherIdx-1); |
| 1578 | Ops.push_back(NewAddRec); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1579 | return getMulExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1584 | // next one. |
| 1585 | } |
| 1586 | |
| 1587 | // Okay, it looks like we really DO need an mul expr. Check to see if we |
| 1588 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1589 | FoldingSetNodeID ID; |
| 1590 | ID.AddInteger(scMulExpr); |
| 1591 | ID.AddInteger(Ops.size()); |
| 1592 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1593 | ID.AddPointer(Ops[i]); |
| 1594 | void *IP = 0; |
| 1595 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1596 | SCEV *S = SCEVAllocator.Allocate<SCEVMulExpr>(); |
| 1597 | new (S) SCEVMulExpr(Ops); |
| 1598 | UniqueSCEVs.InsertNode(S, IP); |
| 1599 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1602 | /// getUDivExpr - Get a canonical multiply expression, or something simpler if |
| 1603 | /// possible. |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1604 | const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS, |
| 1605 | const SCEV *RHS) { |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1606 | assert(getEffectiveSCEVType(LHS->getType()) == |
| 1607 | getEffectiveSCEVType(RHS->getType()) && |
| 1608 | "SCEVUDivExpr operand types don't match!"); |
| 1609 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1610 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1611 | if (RHSC->getValue()->equalsInt(1)) |
Nick Lewycky | 789558d | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 1612 | return LHS; // X udiv 1 --> x |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1613 | if (RHSC->isZero()) |
| 1614 | return getIntegerSCEV(0, LHS->getType()); // value is undefined |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1615 | |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1616 | // Determine if the division can be folded into the operands of |
| 1617 | // its operands. |
| 1618 | // TODO: Generalize this to non-constants by using known-bits information. |
| 1619 | const Type *Ty = LHS->getType(); |
| 1620 | unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros(); |
| 1621 | unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ; |
| 1622 | // For non-power-of-two values, effectively round the value up to the |
| 1623 | // nearest power of two. |
| 1624 | if (!RHSC->getValue()->getValue().isPowerOf2()) |
| 1625 | ++MaxShiftAmt; |
| 1626 | const IntegerType *ExtTy = |
| 1627 | IntegerType::get(getTypeSizeInBits(Ty) + MaxShiftAmt); |
| 1628 | // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. |
| 1629 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) |
| 1630 | if (const SCEVConstant *Step = |
| 1631 | dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) |
| 1632 | if (!Step->getValue()->getValue() |
| 1633 | .urem(RHSC->getValue()->getValue()) && |
Dan Gohman | b028593 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1634 | getZeroExtendExpr(AR, ExtTy) == |
| 1635 | getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), |
| 1636 | getZeroExtendExpr(Step, ExtTy), |
| 1637 | AR->getLoop())) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1638 | SmallVector<const SCEV*, 4> Operands; |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1639 | for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i) |
| 1640 | Operands.push_back(getUDivExpr(AR->getOperand(i), RHS)); |
| 1641 | return getAddRecExpr(Operands, AR->getLoop()); |
| 1642 | } |
| 1643 | // (A*B)/C --> A*(B/C) if safe and B/C can be folded. |
Dan Gohman | b028593 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1644 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1645 | SmallVector<const SCEV*, 4> Operands; |
Dan Gohman | b028593 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1646 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) |
| 1647 | Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy)); |
| 1648 | if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1649 | // Find an operand that's safely divisible. |
| 1650 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1651 | const SCEV* Op = M->getOperand(i); |
| 1652 | const SCEV* Div = getUDivExpr(Op, RHSC); |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1653 | if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1654 | const SmallVectorImpl<const SCEV*> &MOperands = M->getOperands(); |
| 1655 | Operands = SmallVector<const SCEV*, 4>(MOperands.begin(), |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1656 | MOperands.end()); |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1657 | Operands[i] = Div; |
| 1658 | return getMulExpr(Operands); |
| 1659 | } |
| 1660 | } |
Dan Gohman | b028593 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1661 | } |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1662 | // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded. |
Dan Gohman | b028593 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1663 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1664 | SmallVector<const SCEV*, 4> Operands; |
Dan Gohman | b028593 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1665 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) |
| 1666 | Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy)); |
| 1667 | if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { |
| 1668 | Operands.clear(); |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1669 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1670 | const SCEV* Op = getUDivExpr(A->getOperand(i), RHS); |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1671 | if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i)) |
| 1672 | break; |
| 1673 | Operands.push_back(Op); |
| 1674 | } |
| 1675 | if (Operands.size() == A->getNumOperands()) |
| 1676 | return getAddExpr(Operands); |
| 1677 | } |
Dan Gohman | b028593 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1678 | } |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1679 | |
| 1680 | // Fold if both operands are constant. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1681 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1682 | Constant *LHSCV = LHSC->getValue(); |
| 1683 | Constant *RHSCV = RHSC->getValue(); |
Dan Gohman | b8be8b7 | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 1684 | return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV, |
| 1685 | RHSCV))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1686 | } |
| 1687 | } |
| 1688 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1689 | FoldingSetNodeID ID; |
| 1690 | ID.AddInteger(scUDivExpr); |
| 1691 | ID.AddPointer(LHS); |
| 1692 | ID.AddPointer(RHS); |
| 1693 | void *IP = 0; |
| 1694 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1695 | SCEV *S = SCEVAllocator.Allocate<SCEVUDivExpr>(); |
| 1696 | new (S) SCEVUDivExpr(LHS, RHS); |
| 1697 | UniqueSCEVs.InsertNode(S, IP); |
| 1698 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1702 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 1703 | /// Simplify the expression as much as possible. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1704 | const SCEV* ScalarEvolution::getAddRecExpr(const SCEV* Start, |
| 1705 | const SCEV* Step, const Loop *L) { |
| 1706 | SmallVector<const SCEV*, 4> Operands; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1707 | Operands.push_back(Start); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1708 | if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step)) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1709 | if (StepChrec->getLoop() == L) { |
| 1710 | Operands.insert(Operands.end(), StepChrec->op_begin(), |
| 1711 | StepChrec->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1712 | return getAddRecExpr(Operands, L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | Operands.push_back(Step); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1716 | return getAddRecExpr(Operands, L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1719 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 1720 | /// Simplify the expression as much as possible. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1721 | const SCEV * |
| 1722 | ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV*> &Operands, |
| 1723 | const Loop *L) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1724 | if (Operands.size() == 1) return Operands[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1725 | #ifndef NDEBUG |
| 1726 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 1727 | assert(getEffectiveSCEVType(Operands[i]->getType()) == |
| 1728 | getEffectiveSCEVType(Operands[0]->getType()) && |
| 1729 | "SCEVAddRecExpr operand types don't match!"); |
| 1730 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1731 | |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1732 | if (Operands.back()->isZero()) { |
| 1733 | Operands.pop_back(); |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1734 | return getAddRecExpr(Operands, L); // {X,+,0} --> X |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1735 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1736 | |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1737 | // Canonicalize nested AddRecs in by nesting them in order of loop depth. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1738 | if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) { |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1739 | const Loop* NestedLoop = NestedAR->getLoop(); |
| 1740 | if (L->getLoopDepth() < NestedLoop->getLoopDepth()) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1741 | SmallVector<const SCEV*, 4> NestedOperands(NestedAR->op_begin(), |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1742 | NestedAR->op_end()); |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1743 | Operands[0] = NestedAR->getStart(); |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 1744 | // AddRecs require their operands be loop-invariant with respect to their |
| 1745 | // loops. Don't perform this transformation if it would break this |
| 1746 | // requirement. |
| 1747 | bool AllInvariant = true; |
| 1748 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 1749 | if (!Operands[i]->isLoopInvariant(L)) { |
| 1750 | AllInvariant = false; |
| 1751 | break; |
| 1752 | } |
| 1753 | if (AllInvariant) { |
| 1754 | NestedOperands[0] = getAddRecExpr(Operands, L); |
| 1755 | AllInvariant = true; |
| 1756 | for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i) |
| 1757 | if (!NestedOperands[i]->isLoopInvariant(NestedLoop)) { |
| 1758 | AllInvariant = false; |
| 1759 | break; |
| 1760 | } |
| 1761 | if (AllInvariant) |
| 1762 | // Ok, both add recurrences are valid after the transformation. |
| 1763 | return getAddRecExpr(NestedOperands, NestedLoop); |
| 1764 | } |
| 1765 | // Reset Operands to its original state. |
| 1766 | Operands[0] = NestedAR; |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1767 | } |
| 1768 | } |
| 1769 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1770 | FoldingSetNodeID ID; |
| 1771 | ID.AddInteger(scAddRecExpr); |
| 1772 | ID.AddInteger(Operands.size()); |
| 1773 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 1774 | ID.AddPointer(Operands[i]); |
| 1775 | ID.AddPointer(L); |
| 1776 | void *IP = 0; |
| 1777 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1778 | SCEV *S = SCEVAllocator.Allocate<SCEVAddRecExpr>(); |
| 1779 | new (S) SCEVAddRecExpr(Operands, L); |
| 1780 | UniqueSCEVs.InsertNode(S, IP); |
| 1781 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1784 | const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, |
| 1785 | const SCEV *RHS) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1786 | SmallVector<const SCEV*, 2> Ops; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1787 | Ops.push_back(LHS); |
| 1788 | Ops.push_back(RHS); |
| 1789 | return getSMaxExpr(Ops); |
| 1790 | } |
| 1791 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1792 | const SCEV* |
| 1793 | ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV*> &Ops) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1794 | assert(!Ops.empty() && "Cannot get empty smax!"); |
| 1795 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1796 | #ifndef NDEBUG |
| 1797 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1798 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1799 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1800 | "SCEVSMaxExpr operand types don't match!"); |
| 1801 | #endif |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1802 | |
| 1803 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1804 | GroupByComplexity(Ops, LI); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1805 | |
| 1806 | // If there are any constants, fold them together. |
| 1807 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1808 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1809 | ++Idx; |
| 1810 | assert(Idx < Ops.size()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1811 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1812 | // We found two constants, fold them together! |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1813 | ConstantInt *Fold = ConstantInt::get( |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1814 | APIntOps::smax(LHSC->getValue()->getValue(), |
| 1815 | RHSC->getValue()->getValue())); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1816 | Ops[0] = getConstant(Fold); |
| 1817 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1818 | if (Ops.size() == 1) return Ops[0]; |
| 1819 | LHSC = cast<SCEVConstant>(Ops[0]); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1822 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1823 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) { |
| 1824 | Ops.erase(Ops.begin()); |
| 1825 | --Idx; |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1826 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) { |
| 1827 | // If we have an smax with a constant maximum-int, it will always be |
| 1828 | // maximum-int. |
| 1829 | return Ops[0]; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | if (Ops.size() == 1) return Ops[0]; |
| 1834 | |
| 1835 | // Find the first SMax |
| 1836 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr) |
| 1837 | ++Idx; |
| 1838 | |
| 1839 | // Check to see if one of the operands is an SMax. If so, expand its operands |
| 1840 | // onto our operand list, and recurse to simplify. |
| 1841 | if (Idx < Ops.size()) { |
| 1842 | bool DeletedSMax = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1843 | while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1844 | Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end()); |
| 1845 | Ops.erase(Ops.begin()+Idx); |
| 1846 | DeletedSMax = true; |
| 1847 | } |
| 1848 | |
| 1849 | if (DeletedSMax) |
| 1850 | return getSMaxExpr(Ops); |
| 1851 | } |
| 1852 | |
| 1853 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1854 | // so, delete one. Since we sorted the list, these values are required to |
| 1855 | // be adjacent. |
| 1856 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1857 | if (Ops[i] == Ops[i+1]) { // X smax Y smax Y --> X smax Y |
| 1858 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 1859 | --i; --e; |
| 1860 | } |
| 1861 | |
| 1862 | if (Ops.size() == 1) return Ops[0]; |
| 1863 | |
| 1864 | assert(!Ops.empty() && "Reduced smax down to nothing!"); |
| 1865 | |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1866 | // 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] | 1867 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1868 | FoldingSetNodeID ID; |
| 1869 | ID.AddInteger(scSMaxExpr); |
| 1870 | ID.AddInteger(Ops.size()); |
| 1871 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1872 | ID.AddPointer(Ops[i]); |
| 1873 | void *IP = 0; |
| 1874 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1875 | SCEV *S = SCEVAllocator.Allocate<SCEVSMaxExpr>(); |
| 1876 | new (S) SCEVSMaxExpr(Ops); |
| 1877 | UniqueSCEVs.InsertNode(S, IP); |
| 1878 | return S; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1881 | const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, |
| 1882 | const SCEV *RHS) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1883 | SmallVector<const SCEV*, 2> Ops; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1884 | Ops.push_back(LHS); |
| 1885 | Ops.push_back(RHS); |
| 1886 | return getUMaxExpr(Ops); |
| 1887 | } |
| 1888 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1889 | const SCEV* |
| 1890 | ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV*> &Ops) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1891 | assert(!Ops.empty() && "Cannot get empty umax!"); |
| 1892 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1893 | #ifndef NDEBUG |
| 1894 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1895 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1896 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1897 | "SCEVUMaxExpr operand types don't match!"); |
| 1898 | #endif |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1899 | |
| 1900 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1901 | GroupByComplexity(Ops, LI); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1902 | |
| 1903 | // If there are any constants, fold them together. |
| 1904 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1905 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1906 | ++Idx; |
| 1907 | assert(Idx < Ops.size()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1908 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1909 | // We found two constants, fold them together! |
| 1910 | ConstantInt *Fold = ConstantInt::get( |
| 1911 | APIntOps::umax(LHSC->getValue()->getValue(), |
| 1912 | RHSC->getValue()->getValue())); |
| 1913 | Ops[0] = getConstant(Fold); |
| 1914 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1915 | if (Ops.size() == 1) return Ops[0]; |
| 1916 | LHSC = cast<SCEVConstant>(Ops[0]); |
| 1917 | } |
| 1918 | |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1919 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1920 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) { |
| 1921 | Ops.erase(Ops.begin()); |
| 1922 | --Idx; |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1923 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) { |
| 1924 | // If we have an umax with a constant maximum-int, it will always be |
| 1925 | // maximum-int. |
| 1926 | return Ops[0]; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | if (Ops.size() == 1) return Ops[0]; |
| 1931 | |
| 1932 | // Find the first UMax |
| 1933 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr) |
| 1934 | ++Idx; |
| 1935 | |
| 1936 | // Check to see if one of the operands is a UMax. If so, expand its operands |
| 1937 | // onto our operand list, and recurse to simplify. |
| 1938 | if (Idx < Ops.size()) { |
| 1939 | bool DeletedUMax = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1940 | while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1941 | Ops.insert(Ops.end(), UMax->op_begin(), UMax->op_end()); |
| 1942 | Ops.erase(Ops.begin()+Idx); |
| 1943 | DeletedUMax = true; |
| 1944 | } |
| 1945 | |
| 1946 | if (DeletedUMax) |
| 1947 | return getUMaxExpr(Ops); |
| 1948 | } |
| 1949 | |
| 1950 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1951 | // so, delete one. Since we sorted the list, these values are required to |
| 1952 | // be adjacent. |
| 1953 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1954 | if (Ops[i] == Ops[i+1]) { // X umax Y umax Y --> X umax Y |
| 1955 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 1956 | --i; --e; |
| 1957 | } |
| 1958 | |
| 1959 | if (Ops.size() == 1) return Ops[0]; |
| 1960 | |
| 1961 | assert(!Ops.empty() && "Reduced umax down to nothing!"); |
| 1962 | |
| 1963 | // Okay, it looks like we really DO need a umax expr. Check to see if we |
| 1964 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1965 | FoldingSetNodeID ID; |
| 1966 | ID.AddInteger(scUMaxExpr); |
| 1967 | ID.AddInteger(Ops.size()); |
| 1968 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1969 | ID.AddPointer(Ops[i]); |
| 1970 | void *IP = 0; |
| 1971 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1972 | SCEV *S = SCEVAllocator.Allocate<SCEVUMaxExpr>(); |
| 1973 | new (S) SCEVUMaxExpr(Ops); |
| 1974 | UniqueSCEVs.InsertNode(S, IP); |
| 1975 | return S; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1978 | const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS, |
| 1979 | const SCEV *RHS) { |
Dan Gohman | f9a9a99 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 1980 | // ~smax(~x, ~y) == smin(x, y). |
| 1981 | return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 1982 | } |
| 1983 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1984 | const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS, |
| 1985 | const SCEV *RHS) { |
Dan Gohman | f9a9a99 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 1986 | // ~umax(~x, ~y) == umin(x, y) |
| 1987 | return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 1988 | } |
| 1989 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 1990 | const SCEV* ScalarEvolution::getUnknown(Value *V) { |
Dan Gohman | 6bbcba1 | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 1991 | // Don't attempt to do anything other than create a SCEVUnknown object |
| 1992 | // here. createSCEV only calls getUnknown after checking for all other |
| 1993 | // interesting possibilities, and any other code that calls getUnknown |
| 1994 | // is doing so in order to hide a value from SCEV canonicalization. |
| 1995 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1996 | FoldingSetNodeID ID; |
| 1997 | ID.AddInteger(scUnknown); |
| 1998 | ID.AddPointer(V); |
| 1999 | void *IP = 0; |
| 2000 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 2001 | SCEV *S = SCEVAllocator.Allocate<SCEVUnknown>(); |
| 2002 | new (S) SCEVUnknown(V); |
| 2003 | UniqueSCEVs.InsertNode(S, IP); |
| 2004 | return S; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2007 | //===----------------------------------------------------------------------===// |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2008 | // Basic SCEV Analysis and PHI Idiom Recognition Code |
| 2009 | // |
| 2010 | |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2011 | /// isSCEVable - Test if values of the given type are analyzable within |
| 2012 | /// the SCEV framework. This primarily includes integer types, and it |
| 2013 | /// can optionally include pointer types if the ScalarEvolution class |
| 2014 | /// has access to target-specific information. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2015 | bool ScalarEvolution::isSCEVable(const Type *Ty) const { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2016 | // Integers are always SCEVable. |
| 2017 | if (Ty->isInteger()) |
| 2018 | return true; |
| 2019 | |
| 2020 | // Pointers are SCEVable if TargetData information is available |
| 2021 | // to provide pointer size information. |
| 2022 | if (isa<PointerType>(Ty)) |
| 2023 | return TD != NULL; |
| 2024 | |
| 2025 | // Otherwise it's not SCEVable. |
| 2026 | return false; |
| 2027 | } |
| 2028 | |
| 2029 | /// getTypeSizeInBits - Return the size in bits of the specified type, |
| 2030 | /// for which isSCEVable must return true. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2031 | uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2032 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2033 | |
| 2034 | // If we have a TargetData, use it! |
| 2035 | if (TD) |
| 2036 | return TD->getTypeSizeInBits(Ty); |
| 2037 | |
| 2038 | // Otherwise, we support only integer types. |
| 2039 | assert(Ty->isInteger() && "isSCEVable permitted a non-SCEVable type!"); |
| 2040 | return Ty->getPrimitiveSizeInBits(); |
| 2041 | } |
| 2042 | |
| 2043 | /// getEffectiveSCEVType - Return a type with the same bitwidth as |
| 2044 | /// the given type and which represents how SCEV will treat the given |
| 2045 | /// type, for which isSCEVable must return true. For pointer types, |
| 2046 | /// this is the pointer-sized integer type. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2047 | const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2048 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2049 | |
| 2050 | if (Ty->isInteger()) |
| 2051 | return Ty; |
| 2052 | |
| 2053 | assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!"); |
| 2054 | return TD->getIntPtrType(); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2055 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2056 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2057 | const SCEV* ScalarEvolution::getCouldNotCompute() { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2058 | return &CouldNotCompute; |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Dan Gohman | 92fa56e | 2009-05-04 22:20:30 +0000 | [diff] [blame] | 2061 | /// hasSCEV - Return true if the SCEV for this value has already been |
Torok Edwin | e3d1285 | 2009-05-01 08:33:47 +0000 | [diff] [blame] | 2062 | /// computed. |
| 2063 | bool ScalarEvolution::hasSCEV(Value *V) const { |
| 2064 | return Scalars.count(V); |
| 2065 | } |
| 2066 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2067 | /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the |
| 2068 | /// expression and create a new one. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2069 | const SCEV* ScalarEvolution::getSCEV(Value *V) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2070 | assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2071 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2072 | std::map<SCEVCallbackVH, const SCEV*>::iterator I = Scalars.find(V); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2073 | if (I != Scalars.end()) return I->second; |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2074 | const SCEV* S = createSCEV(V); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2075 | Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2076 | return S; |
| 2077 | } |
| 2078 | |
Dan Gohman | 6bbcba1 | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2079 | /// getIntegerSCEV - Given a SCEVable type, create a constant for the |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2080 | /// specified signed integer value and return a SCEV for the constant. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2081 | const SCEV* ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) { |
Dan Gohman | 6bbcba1 | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2082 | const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty)); |
| 2083 | return getConstant(ConstantInt::get(ITy, Val)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V |
| 2087 | /// |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2088 | const SCEV* ScalarEvolution::getNegativeSCEV(const SCEV* V) { |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2089 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Dan Gohman | b8be8b7 | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 2090 | return getConstant(cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue()))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2091 | |
| 2092 | const Type *Ty = V->getType(); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2093 | Ty = getEffectiveSCEVType(Ty); |
| 2094 | return getMulExpr(V, getConstant(ConstantInt::getAllOnesValue(Ty))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2098 | const SCEV* ScalarEvolution::getNotSCEV(const SCEV* V) { |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2099 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Dan Gohman | b8be8b7 | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 2100 | return getConstant(cast<ConstantInt>(ConstantExpr::getNot(VC->getValue()))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2101 | |
| 2102 | const Type *Ty = V->getType(); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2103 | Ty = getEffectiveSCEVType(Ty); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2104 | const SCEV* AllOnes = getConstant(ConstantInt::getAllOnesValue(Ty)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2105 | return getMinusSCEV(AllOnes, V); |
| 2106 | } |
| 2107 | |
| 2108 | /// getMinusSCEV - Return a SCEV corresponding to LHS - RHS. |
| 2109 | /// |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2110 | const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, |
| 2111 | const SCEV *RHS) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2112 | // X - Y --> X + -Y |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2113 | return getAddExpr(LHS, getNegativeSCEV(RHS)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
| 2116 | /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2117 | /// input value to the specified type. If the type must be extended, it is zero |
| 2118 | /// extended. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2119 | const SCEV* |
| 2120 | ScalarEvolution::getTruncateOrZeroExtend(const SCEV* V, |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2121 | const Type *Ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2122 | const Type *SrcTy = V->getType(); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2123 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2124 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2125 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2126 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2127 | return V; // No conversion |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2128 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2129 | return getTruncateExpr(V, Ty); |
| 2130 | return getZeroExtendExpr(V, Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2134 | /// input value to the specified type. If the type must be extended, it is sign |
| 2135 | /// extended. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2136 | const SCEV* |
| 2137 | ScalarEvolution::getTruncateOrSignExtend(const SCEV* V, |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2138 | const Type *Ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2139 | const Type *SrcTy = V->getType(); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2140 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2141 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2142 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2143 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2144 | return V; // No conversion |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2145 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2146 | return getTruncateExpr(V, Ty); |
| 2147 | return getSignExtendExpr(V, Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2150 | /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2151 | /// input value to the specified type. If the type must be extended, it is zero |
| 2152 | /// extended. The conversion must not be narrowing. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2153 | const SCEV* |
| 2154 | ScalarEvolution::getNoopOrZeroExtend(const SCEV* V, const Type *Ty) { |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2155 | const Type *SrcTy = V->getType(); |
| 2156 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2157 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 2158 | "Cannot noop or zero extend with non-integer arguments!"); |
| 2159 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2160 | "getNoopOrZeroExtend cannot truncate!"); |
| 2161 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2162 | return V; // No conversion |
| 2163 | return getZeroExtendExpr(V, Ty); |
| 2164 | } |
| 2165 | |
| 2166 | /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2167 | /// input value to the specified type. If the type must be extended, it is sign |
| 2168 | /// extended. The conversion must not be narrowing. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2169 | const SCEV* |
| 2170 | ScalarEvolution::getNoopOrSignExtend(const SCEV* V, const Type *Ty) { |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2171 | const Type *SrcTy = V->getType(); |
| 2172 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2173 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 2174 | "Cannot noop or sign extend with non-integer arguments!"); |
| 2175 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2176 | "getNoopOrSignExtend cannot truncate!"); |
| 2177 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2178 | return V; // No conversion |
| 2179 | return getSignExtendExpr(V, Ty); |
| 2180 | } |
| 2181 | |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2182 | /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of |
| 2183 | /// the input value to the specified type. If the type must be extended, |
| 2184 | /// it is extended with unspecified bits. The conversion must not be |
| 2185 | /// narrowing. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2186 | const SCEV* |
| 2187 | ScalarEvolution::getNoopOrAnyExtend(const SCEV* V, const Type *Ty) { |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2188 | const Type *SrcTy = V->getType(); |
| 2189 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2190 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 2191 | "Cannot noop or any extend with non-integer arguments!"); |
| 2192 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2193 | "getNoopOrAnyExtend cannot truncate!"); |
| 2194 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2195 | return V; // No conversion |
| 2196 | return getAnyExtendExpr(V, Ty); |
| 2197 | } |
| 2198 | |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2199 | /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the |
| 2200 | /// input value to the specified type. The conversion must not be widening. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2201 | const SCEV* |
| 2202 | ScalarEvolution::getTruncateOrNoop(const SCEV* V, const Type *Ty) { |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2203 | const Type *SrcTy = V->getType(); |
| 2204 | assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && |
| 2205 | (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && |
| 2206 | "Cannot truncate or noop with non-integer arguments!"); |
| 2207 | assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) && |
| 2208 | "getTruncateOrNoop cannot extend!"); |
| 2209 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2210 | return V; // No conversion |
| 2211 | return getTruncateExpr(V, Ty); |
| 2212 | } |
| 2213 | |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2214 | /// getUMaxFromMismatchedTypes - Promote the operands to the wider of |
| 2215 | /// the types using zero-extension, and then perform a umax operation |
| 2216 | /// with them. |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2217 | const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS, |
| 2218 | const SCEV *RHS) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2219 | const SCEV* PromotedLHS = LHS; |
| 2220 | const SCEV* PromotedRHS = RHS; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2221 | |
| 2222 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2223 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2224 | else |
| 2225 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2226 | |
| 2227 | return getUMaxExpr(PromotedLHS, PromotedRHS); |
| 2228 | } |
| 2229 | |
Dan Gohman | c9759e8 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2230 | /// getUMinFromMismatchedTypes - Promote the operands to the wider of |
| 2231 | /// the types using zero-extension, and then perform a umin operation |
| 2232 | /// with them. |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2233 | const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS, |
| 2234 | const SCEV *RHS) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2235 | const SCEV* PromotedLHS = LHS; |
| 2236 | const SCEV* PromotedRHS = RHS; |
Dan Gohman | c9759e8 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2237 | |
| 2238 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2239 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2240 | else |
| 2241 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2242 | |
| 2243 | return getUMinExpr(PromotedLHS, PromotedRHS); |
| 2244 | } |
| 2245 | |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2246 | /// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value for |
| 2247 | /// the specified instruction and replaces any references to the symbolic value |
| 2248 | /// SymName with the specified value. This is used during PHI resolution. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2249 | void |
| 2250 | ScalarEvolution::ReplaceSymbolicValueWithConcrete(Instruction *I, |
| 2251 | const SCEV *SymName, |
| 2252 | const SCEV *NewVal) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2253 | std::map<SCEVCallbackVH, const SCEV*>::iterator SI = |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2254 | Scalars.find(SCEVCallbackVH(I, this)); |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2255 | if (SI == Scalars.end()) return; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2256 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2257 | const SCEV* NV = |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2258 | SI->second->replaceSymbolicValuesWithConcrete(SymName, NewVal, *this); |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2259 | if (NV == SI->second) return; // No change. |
| 2260 | |
| 2261 | SI->second = NV; // Update the scalars map! |
| 2262 | |
| 2263 | // Any instruction values that use this instruction might also need to be |
| 2264 | // updated! |
| 2265 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 2266 | UI != E; ++UI) |
| 2267 | ReplaceSymbolicValueWithConcrete(cast<Instruction>(*UI), SymName, NewVal); |
| 2268 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2269 | |
| 2270 | /// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in |
| 2271 | /// a loop header, making it a potential recurrence, or it doesn't. |
| 2272 | /// |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2273 | const SCEV* ScalarEvolution::createNodeForPHI(PHINode *PN) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2274 | if (PN->getNumIncomingValues() == 2) // The loops have been canonicalized. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2275 | if (const Loop *L = LI->getLoopFor(PN->getParent())) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2276 | if (L->getHeader() == PN->getParent()) { |
| 2277 | // If it lives in the loop header, it has two incoming values, one |
| 2278 | // from outside the loop, and one from inside. |
| 2279 | unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0)); |
| 2280 | unsigned BackEdge = IncomingEdge^1; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2281 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2282 | // While we are analyzing this PHI node, handle its value symbolically. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2283 | const SCEV* SymbolicName = getUnknown(PN); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2284 | assert(Scalars.find(PN) == Scalars.end() && |
| 2285 | "PHI node already processed?"); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2286 | Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2287 | |
| 2288 | // Using this symbolic name for the PHI, analyze the value coming around |
| 2289 | // the back-edge. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2290 | const SCEV* BEValue = getSCEV(PN->getIncomingValue(BackEdge)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2291 | |
| 2292 | // NOTE: If BEValue is loop invariant, we know that the PHI node just |
| 2293 | // has a special value for the first iteration of the loop. |
| 2294 | |
| 2295 | // If the value coming around the backedge is an add with the symbolic |
| 2296 | // value we just inserted, then we found a simple induction variable! |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2297 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2298 | // If there is a single occurrence of the symbolic value, replace it |
| 2299 | // with a recurrence. |
| 2300 | unsigned FoundIndex = Add->getNumOperands(); |
| 2301 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2302 | if (Add->getOperand(i) == SymbolicName) |
| 2303 | if (FoundIndex == e) { |
| 2304 | FoundIndex = i; |
| 2305 | break; |
| 2306 | } |
| 2307 | |
| 2308 | if (FoundIndex != Add->getNumOperands()) { |
| 2309 | // Create an add with everything but the specified operand. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2310 | SmallVector<const SCEV*, 8> Ops; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2311 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2312 | if (i != FoundIndex) |
| 2313 | Ops.push_back(Add->getOperand(i)); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2314 | const SCEV* Accum = getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2315 | |
| 2316 | // This is not a valid addrec if the step amount is varying each |
| 2317 | // loop iteration, but is not itself an addrec in this loop. |
| 2318 | if (Accum->isLoopInvariant(L) || |
| 2319 | (isa<SCEVAddRecExpr>(Accum) && |
| 2320 | cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) { |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2321 | const SCEV *StartVal = |
| 2322 | getSCEV(PN->getIncomingValue(IncomingEdge)); |
| 2323 | const SCEV *PHISCEV = |
| 2324 | getAddRecExpr(StartVal, Accum, L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2325 | |
| 2326 | // Okay, for the entire analysis of this edge we assumed the PHI |
| 2327 | // to be symbolic. We now need to go back and update all of the |
| 2328 | // entries for the scalars that use the PHI (except for the PHI |
| 2329 | // itself) to use the new analyzed value instead of the "symbolic" |
| 2330 | // value. |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2331 | ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2332 | return PHISCEV; |
| 2333 | } |
| 2334 | } |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2335 | } else if (const SCEVAddRecExpr *AddRec = |
| 2336 | dyn_cast<SCEVAddRecExpr>(BEValue)) { |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2337 | // Otherwise, this could be a loop like this: |
| 2338 | // i = 0; for (j = 1; ..; ++j) { .... i = j; } |
| 2339 | // In this case, j = {1,+,1} and BEValue is j. |
| 2340 | // Because the other in-value of i (0) fits the evolution of BEValue |
| 2341 | // i really is an addrec evolution. |
| 2342 | if (AddRec->getLoop() == L && AddRec->isAffine()) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2343 | const SCEV* StartVal = getSCEV(PN->getIncomingValue(IncomingEdge)); |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2344 | |
| 2345 | // If StartVal = j.start - j.stride, we can use StartVal as the |
| 2346 | // initial step of the addrec evolution. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2347 | if (StartVal == getMinusSCEV(AddRec->getOperand(0), |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 2348 | AddRec->getOperand(1))) { |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2349 | const SCEV* PHISCEV = |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2350 | getAddRecExpr(StartVal, AddRec->getOperand(1), L); |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2351 | |
| 2352 | // Okay, for the entire analysis of this edge we assumed the PHI |
| 2353 | // to be symbolic. We now need to go back and update all of the |
| 2354 | // entries for the scalars that use the PHI (except for the PHI |
| 2355 | // itself) to use the new analyzed value instead of the "symbolic" |
| 2356 | // value. |
| 2357 | ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV); |
| 2358 | return PHISCEV; |
| 2359 | } |
| 2360 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | return SymbolicName; |
| 2364 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2365 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2366 | // 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] | 2367 | return getUnknown(PN); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2370 | /// createNodeForGEP - Expand GEP instructions into add and multiply |
| 2371 | /// operations. This allows them to be analyzed by regular SCEV code. |
| 2372 | /// |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2373 | const SCEV* ScalarEvolution::createNodeForGEP(User *GEP) { |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2374 | |
| 2375 | const Type *IntPtrTy = TD->getIntPtrType(); |
Dan Gohman | e810b0d | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2376 | Value *Base = GEP->getOperand(0); |
Dan Gohman | c63a627 | 2009-05-09 00:14:52 +0000 | [diff] [blame] | 2377 | // Don't attempt to analyze GEPs over unsized objects. |
| 2378 | if (!cast<PointerType>(Base->getType())->getElementType()->isSized()) |
| 2379 | return getUnknown(GEP); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2380 | const SCEV* TotalOffset = getIntegerSCEV(0, IntPtrTy); |
Dan Gohman | e810b0d | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2381 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 2382 | for (GetElementPtrInst::op_iterator I = next(GEP->op_begin()), |
| 2383 | E = GEP->op_end(); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2384 | I != E; ++I) { |
| 2385 | Value *Index = *I; |
| 2386 | // Compute the (potentially symbolic) offset in bytes for this index. |
| 2387 | if (const StructType *STy = dyn_cast<StructType>(*GTI++)) { |
| 2388 | // For a struct, add the member offset. |
| 2389 | const StructLayout &SL = *TD->getStructLayout(STy); |
| 2390 | unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue(); |
| 2391 | uint64_t Offset = SL.getElementOffset(FieldNo); |
| 2392 | TotalOffset = getAddExpr(TotalOffset, |
| 2393 | getIntegerSCEV(Offset, IntPtrTy)); |
| 2394 | } else { |
| 2395 | // For an array, add the element offset, explicitly scaled. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2396 | const SCEV* LocalOffset = getSCEV(Index); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2397 | if (!isa<PointerType>(LocalOffset->getType())) |
| 2398 | // Getelementptr indicies are signed. |
| 2399 | LocalOffset = getTruncateOrSignExtend(LocalOffset, |
| 2400 | IntPtrTy); |
| 2401 | LocalOffset = |
| 2402 | getMulExpr(LocalOffset, |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 2403 | getIntegerSCEV(TD->getTypeAllocSize(*GTI), |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2404 | IntPtrTy)); |
| 2405 | TotalOffset = getAddExpr(TotalOffset, LocalOffset); |
| 2406 | } |
| 2407 | } |
| 2408 | return getAddExpr(getSCEV(Base), TotalOffset); |
| 2409 | } |
| 2410 | |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2411 | /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is |
| 2412 | /// guaranteed to end in (at every loop iteration). It is, at the same time, |
| 2413 | /// the minimum number of times S is divisible by 2. For example, given {4,+,8} |
| 2414 | /// 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] | 2415 | uint32_t |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2416 | ScalarEvolution::GetMinTrailingZeros(const SCEV* S) { |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2417 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Chris Lattner | 8314a0c | 2007-11-23 22:36:49 +0000 | [diff] [blame] | 2418 | return C->getValue()->getValue().countTrailingZeros(); |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2419 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2420 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S)) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2421 | return std::min(GetMinTrailingZeros(T->getOperand()), |
| 2422 | (uint32_t)getTypeSizeInBits(T->getType())); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2423 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2424 | if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) { |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2425 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 2426 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 2427 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2430 | if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) { |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2431 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 2432 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 2433 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2436 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2437 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2438 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2439 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2440 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2441 | return MinOpRes; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2444 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2445 | // The result is the sum of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2446 | uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0)); |
| 2447 | uint32_t BitWidth = getTypeSizeInBits(M->getType()); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2448 | for (unsigned i = 1, e = M->getNumOperands(); |
| 2449 | SumOpRes != BitWidth && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2450 | SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2451 | BitWidth); |
| 2452 | return SumOpRes; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2453 | } |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2454 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2455 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2456 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2457 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2458 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2459 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2460 | return MinOpRes; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2461 | } |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2462 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2463 | if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2464 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2465 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2466 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2467 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2468 | return MinOpRes; |
| 2469 | } |
| 2470 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2471 | if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2472 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2473 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2474 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2475 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2476 | return MinOpRes; |
| 2477 | } |
| 2478 | |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2479 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2480 | // For a SCEVUnknown, ask ValueTracking. |
| 2481 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2482 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 2483 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 2484 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones); |
| 2485 | return Zeros.countTrailingOnes(); |
| 2486 | } |
| 2487 | |
| 2488 | // SCEVUDivExpr |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2489 | return 0; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2490 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2491 | |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2492 | uint32_t |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2493 | ScalarEvolution::GetMinLeadingZeros(const SCEV* S) { |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2494 | // TODO: Handle other SCEV expression types here. |
| 2495 | |
| 2496 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
| 2497 | return C->getValue()->getValue().countLeadingZeros(); |
| 2498 | |
| 2499 | if (const SCEVZeroExtendExpr *C = dyn_cast<SCEVZeroExtendExpr>(S)) { |
| 2500 | // A zero-extension cast adds zero bits. |
| 2501 | return GetMinLeadingZeros(C->getOperand()) + |
| 2502 | (getTypeSizeInBits(C->getType()) - |
| 2503 | getTypeSizeInBits(C->getOperand()->getType())); |
| 2504 | } |
| 2505 | |
| 2506 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2507 | // For a SCEVUnknown, ask ValueTracking. |
| 2508 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2509 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 2510 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 2511 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD); |
| 2512 | return Zeros.countLeadingOnes(); |
| 2513 | } |
| 2514 | |
| 2515 | return 1; |
| 2516 | } |
| 2517 | |
| 2518 | uint32_t |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2519 | ScalarEvolution::GetMinSignBits(const SCEV* S) { |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2520 | // TODO: Handle other SCEV expression types here. |
| 2521 | |
| 2522 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) { |
| 2523 | const APInt &A = C->getValue()->getValue(); |
| 2524 | return A.isNegative() ? A.countLeadingOnes() : |
| 2525 | A.countLeadingZeros(); |
| 2526 | } |
| 2527 | |
| 2528 | if (const SCEVSignExtendExpr *C = dyn_cast<SCEVSignExtendExpr>(S)) { |
| 2529 | // A sign-extension cast adds sign bits. |
| 2530 | return GetMinSignBits(C->getOperand()) + |
| 2531 | (getTypeSizeInBits(C->getType()) - |
| 2532 | getTypeSizeInBits(C->getOperand()->getType())); |
| 2533 | } |
| 2534 | |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2535 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { |
| 2536 | unsigned BitWidth = getTypeSizeInBits(A->getType()); |
| 2537 | |
| 2538 | // Special case decrementing a value (ADD X, -1): |
| 2539 | if (const SCEVConstant *CRHS = dyn_cast<SCEVConstant>(A->getOperand(0))) |
| 2540 | if (CRHS->isAllOnesValue()) { |
| 2541 | SmallVector<const SCEV *, 4> OtherOps(A->op_begin() + 1, A->op_end()); |
| 2542 | const SCEV *OtherOpsAdd = getAddExpr(OtherOps); |
| 2543 | unsigned LZ = GetMinLeadingZeros(OtherOpsAdd); |
| 2544 | |
| 2545 | // If the input is known to be 0 or 1, the output is 0/-1, which is all |
| 2546 | // sign bits set. |
| 2547 | if (LZ == BitWidth - 1) |
| 2548 | return BitWidth; |
| 2549 | |
| 2550 | // If we are subtracting one from a positive number, there is no carry |
| 2551 | // out of the result. |
| 2552 | if (LZ > 0) |
| 2553 | return GetMinSignBits(OtherOpsAdd); |
| 2554 | } |
| 2555 | |
| 2556 | // Add can have at most one carry bit. Thus we know that the output |
| 2557 | // is, at worst, one more bit than the inputs. |
| 2558 | unsigned Min = BitWidth; |
| 2559 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { |
| 2560 | unsigned N = GetMinSignBits(A->getOperand(i)); |
| 2561 | Min = std::min(Min, N) - 1; |
| 2562 | if (Min == 0) return 1; |
| 2563 | } |
| 2564 | return 1; |
| 2565 | } |
| 2566 | |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2567 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2568 | // For a SCEVUnknown, ask ValueTracking. |
| 2569 | return ComputeNumSignBits(U->getValue(), TD); |
| 2570 | } |
| 2571 | |
| 2572 | return 1; |
| 2573 | } |
| 2574 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2575 | /// createSCEV - We know that there is no SCEV for the specified value. |
| 2576 | /// Analyze the expression. |
| 2577 | /// |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2578 | const SCEV* ScalarEvolution::createSCEV(Value *V) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2579 | if (!isSCEVable(V->getType())) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2580 | return getUnknown(V); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2581 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2582 | unsigned Opcode = Instruction::UserOp1; |
| 2583 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 2584 | Opcode = I->getOpcode(); |
| 2585 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 2586 | Opcode = CE->getOpcode(); |
Dan Gohman | 6bbcba1 | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2587 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 2588 | return getConstant(CI); |
| 2589 | else if (isa<ConstantPointerNull>(V)) |
| 2590 | return getIntegerSCEV(0, V->getType()); |
| 2591 | else if (isa<UndefValue>(V)) |
| 2592 | return getIntegerSCEV(0, V->getType()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2593 | else |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2594 | return getUnknown(V); |
Chris Lattner | 2811f2a | 2007-04-02 05:41:38 +0000 | [diff] [blame] | 2595 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2596 | User *U = cast<User>(V); |
| 2597 | switch (Opcode) { |
| 2598 | case Instruction::Add: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2599 | return getAddExpr(getSCEV(U->getOperand(0)), |
| 2600 | getSCEV(U->getOperand(1))); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2601 | case Instruction::Mul: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2602 | return getMulExpr(getSCEV(U->getOperand(0)), |
| 2603 | getSCEV(U->getOperand(1))); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2604 | case Instruction::UDiv: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2605 | return getUDivExpr(getSCEV(U->getOperand(0)), |
| 2606 | getSCEV(U->getOperand(1))); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2607 | case Instruction::Sub: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2608 | return getMinusSCEV(getSCEV(U->getOperand(0)), |
| 2609 | getSCEV(U->getOperand(1))); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2610 | case Instruction::And: |
| 2611 | // For an expression like x&255 that merely masks off the high bits, |
| 2612 | // use zext(trunc(x)) as the SCEV expression. |
| 2613 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 2c73d5f | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2614 | if (CI->isNullValue()) |
| 2615 | return getSCEV(U->getOperand(1)); |
Dan Gohman | d6c3295 | 2009-04-27 01:41:10 +0000 | [diff] [blame] | 2616 | if (CI->isAllOnesValue()) |
| 2617 | return getSCEV(U->getOperand(0)); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2618 | const APInt &A = CI->getValue(); |
Dan Gohman | 61ffa8e | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2619 | |
| 2620 | // Instcombine's ShrinkDemandedConstant may strip bits out of |
| 2621 | // constants, obscuring what would otherwise be a low-bits mask. |
| 2622 | // Use ComputeMaskedBits to compute what ShrinkDemandedConstant |
| 2623 | // knew about to reconstruct a low-bits mask value. |
| 2624 | unsigned LZ = A.countLeadingZeros(); |
| 2625 | unsigned BitWidth = A.getBitWidth(); |
| 2626 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
| 2627 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 2628 | ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD); |
| 2629 | |
| 2630 | APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ); |
| 2631 | |
Dan Gohman | fc3641b | 2009-06-17 23:54:37 +0000 | [diff] [blame] | 2632 | if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask)) |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2633 | return |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2634 | getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)), |
Dan Gohman | 61ffa8e | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2635 | IntegerType::get(BitWidth - LZ)), |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2636 | U->getType()); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2637 | } |
| 2638 | break; |
Dan Gohman | 61ffa8e | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2639 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2640 | case Instruction::Or: |
| 2641 | // If the RHS of the Or is a constant, we may have something like: |
| 2642 | // X*4+1 which got turned into X*4|1. Handle this as an Add so loop |
| 2643 | // optimizations will transparently handle this case. |
| 2644 | // |
| 2645 | // In order for this transformation to be safe, the LHS must be of the |
| 2646 | // form X*(2^n) and the Or constant must be less than 2^n. |
| 2647 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2648 | const SCEV* LHS = getSCEV(U->getOperand(0)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2649 | const APInt &CIVal = CI->getValue(); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2650 | if (GetMinTrailingZeros(LHS) >= |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2651 | (CIVal.getBitWidth() - CIVal.countLeadingZeros())) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2652 | return getAddExpr(LHS, getSCEV(U->getOperand(1))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2653 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2654 | break; |
| 2655 | case Instruction::Xor: |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2656 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2657 | // If the RHS of the xor is a signbit, then this is just an add. |
| 2658 | // Instcombine turns add of signbit into xor as a strength reduction step. |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2659 | if (CI->getValue().isSignBit()) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2660 | return getAddExpr(getSCEV(U->getOperand(0)), |
| 2661 | getSCEV(U->getOperand(1))); |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2662 | |
| 2663 | // 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] | 2664 | if (CI->isAllOnesValue()) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2665 | return getNotSCEV(getSCEV(U->getOperand(0))); |
Dan Gohman | 10978bd | 2009-05-18 16:29:04 +0000 | [diff] [blame] | 2666 | |
| 2667 | // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask. |
| 2668 | // This is a variant of the check for xor with -1, and it handles |
| 2669 | // the case where instcombine has trimmed non-demanded bits out |
| 2670 | // of an xor with -1. |
| 2671 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0))) |
| 2672 | if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1))) |
| 2673 | if (BO->getOpcode() == Instruction::And && |
| 2674 | LCI->getValue() == CI->getValue()) |
| 2675 | if (const SCEVZeroExtendExpr *Z = |
Dan Gohman | 3034c10 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 2676 | dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) { |
Dan Gohman | 8205283 | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 2677 | const Type *UTy = U->getType(); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2678 | const SCEV* Z0 = Z->getOperand(); |
Dan Gohman | 8205283 | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 2679 | const Type *Z0Ty = Z0->getType(); |
| 2680 | unsigned Z0TySize = getTypeSizeInBits(Z0Ty); |
| 2681 | |
| 2682 | // If C is a low-bits mask, the zero extend is zerving to |
| 2683 | // mask off the high bits. Complement the operand and |
| 2684 | // re-apply the zext. |
| 2685 | if (APIntOps::isMask(Z0TySize, CI->getValue())) |
| 2686 | return getZeroExtendExpr(getNotSCEV(Z0), UTy); |
| 2687 | |
| 2688 | // If C is a single bit, it may be in the sign-bit position |
| 2689 | // before the zero-extend. In this case, represent the xor |
| 2690 | // using an add, which is equivalent, and re-apply the zext. |
| 2691 | APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize); |
| 2692 | if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() && |
| 2693 | Trunc.isSignBit()) |
| 2694 | return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)), |
| 2695 | UTy); |
Dan Gohman | 3034c10 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 2696 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2697 | } |
| 2698 | break; |
| 2699 | |
| 2700 | case Instruction::Shl: |
| 2701 | // Turn shift left of a constant amount into a multiply. |
| 2702 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 2703 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
| 2704 | Constant *X = ConstantInt::get( |
| 2705 | APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2706 | return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2707 | } |
| 2708 | break; |
| 2709 | |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2710 | case Instruction::LShr: |
Nick Lewycky | 789558d | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 2711 | // Turn logical shift right of a constant into a unsigned divide. |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2712 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 2713 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
| 2714 | Constant *X = ConstantInt::get( |
| 2715 | APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2716 | return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2717 | } |
| 2718 | break; |
| 2719 | |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2720 | case Instruction::AShr: |
| 2721 | // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression. |
| 2722 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) |
| 2723 | if (Instruction *L = dyn_cast<Instruction>(U->getOperand(0))) |
| 2724 | if (L->getOpcode() == Instruction::Shl && |
| 2725 | L->getOperand(1) == U->getOperand(1)) { |
Dan Gohman | 2c73d5f | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2726 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2727 | uint64_t Amt = BitWidth - CI->getZExtValue(); |
| 2728 | if (Amt == BitWidth) |
| 2729 | return getSCEV(L->getOperand(0)); // shift by zero --> noop |
| 2730 | if (Amt > BitWidth) |
| 2731 | return getIntegerSCEV(0, U->getType()); // value is undefined |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2732 | return |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2733 | getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)), |
Dan Gohman | 2c73d5f | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2734 | IntegerType::get(Amt)), |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2735 | U->getType()); |
| 2736 | } |
| 2737 | break; |
| 2738 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2739 | case Instruction::Trunc: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2740 | return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2741 | |
| 2742 | case Instruction::ZExt: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2743 | return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2744 | |
| 2745 | case Instruction::SExt: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2746 | return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2747 | |
| 2748 | case Instruction::BitCast: |
| 2749 | // BitCasts are no-op casts so we just eliminate the cast. |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2750 | if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2751 | return getSCEV(U->getOperand(0)); |
| 2752 | break; |
| 2753 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2754 | case Instruction::IntToPtr: |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2755 | if (!TD) break; // Without TD we can't analyze pointers. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2756 | return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)), |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2757 | TD->getIntPtrType()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2758 | |
| 2759 | case Instruction::PtrToInt: |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2760 | if (!TD) break; // Without TD we can't analyze pointers. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2761 | return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)), |
| 2762 | U->getType()); |
| 2763 | |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2764 | case Instruction::GetElementPtr: |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2765 | if (!TD) break; // Without TD we can't analyze pointers. |
Dan Gohman | fb79160 | 2009-05-08 20:58:38 +0000 | [diff] [blame] | 2766 | return createNodeForGEP(U); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2767 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2768 | case Instruction::PHI: |
| 2769 | return createNodeForPHI(cast<PHINode>(U)); |
| 2770 | |
| 2771 | case Instruction::Select: |
| 2772 | // This could be a smax or umax that was lowered earlier. |
| 2773 | // Try to recover it. |
| 2774 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) { |
| 2775 | Value *LHS = ICI->getOperand(0); |
| 2776 | Value *RHS = ICI->getOperand(1); |
| 2777 | switch (ICI->getPredicate()) { |
| 2778 | case ICmpInst::ICMP_SLT: |
| 2779 | case ICmpInst::ICMP_SLE: |
| 2780 | std::swap(LHS, RHS); |
| 2781 | // fall through |
| 2782 | case ICmpInst::ICMP_SGT: |
| 2783 | case ICmpInst::ICMP_SGE: |
| 2784 | if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2785 | return getSMaxExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2786 | else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) |
Dan Gohman | f9a9a99 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2787 | return getSMinExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2788 | break; |
| 2789 | case ICmpInst::ICMP_ULT: |
| 2790 | case ICmpInst::ICMP_ULE: |
| 2791 | std::swap(LHS, RHS); |
| 2792 | // fall through |
| 2793 | case ICmpInst::ICMP_UGT: |
| 2794 | case ICmpInst::ICMP_UGE: |
| 2795 | if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2796 | return getUMaxExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2797 | else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) |
Dan Gohman | f9a9a99 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2798 | return getUMinExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2799 | break; |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 2800 | case ICmpInst::ICMP_NE: |
| 2801 | // n != 0 ? n : 1 -> umax(n, 1) |
| 2802 | if (LHS == U->getOperand(1) && |
| 2803 | isa<ConstantInt>(U->getOperand(2)) && |
| 2804 | cast<ConstantInt>(U->getOperand(2))->isOne() && |
| 2805 | isa<ConstantInt>(RHS) && |
| 2806 | cast<ConstantInt>(RHS)->isZero()) |
| 2807 | return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(2))); |
| 2808 | break; |
| 2809 | case ICmpInst::ICMP_EQ: |
| 2810 | // n == 0 ? 1 : n -> umax(n, 1) |
| 2811 | if (LHS == U->getOperand(2) && |
| 2812 | isa<ConstantInt>(U->getOperand(1)) && |
| 2813 | cast<ConstantInt>(U->getOperand(1))->isOne() && |
| 2814 | isa<ConstantInt>(RHS) && |
| 2815 | cast<ConstantInt>(RHS)->isZero()) |
| 2816 | return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(1))); |
| 2817 | break; |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2818 | default: |
| 2819 | break; |
| 2820 | } |
| 2821 | } |
| 2822 | |
| 2823 | default: // We cannot analyze this expression. |
| 2824 | break; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2827 | return getUnknown(V); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | |
| 2831 | |
| 2832 | //===----------------------------------------------------------------------===// |
| 2833 | // Iteration Count Computation Code |
| 2834 | // |
| 2835 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2836 | /// getBackedgeTakenCount - If the specified loop has a predictable |
| 2837 | /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute |
| 2838 | /// object. The backedge-taken count is the number of times the loop header |
| 2839 | /// will be branched to from within the loop. This is one less than the |
| 2840 | /// trip count of the loop, since it doesn't count the first iteration, |
| 2841 | /// when the header is branched to from outside the loop. |
| 2842 | /// |
| 2843 | /// Note that it is not valid to call this method on a loop without a |
| 2844 | /// loop-invariant backedge-taken count (see |
| 2845 | /// hasLoopInvariantBackedgeTakenCount). |
| 2846 | /// |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2847 | const SCEV* ScalarEvolution::getBackedgeTakenCount(const Loop *L) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2848 | return getBackedgeTakenInfo(L).Exact; |
| 2849 | } |
| 2850 | |
| 2851 | /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except |
| 2852 | /// return the least SCEV value that is known never to be less than the |
| 2853 | /// actual backedge taken count. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 2854 | const SCEV* ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2855 | return getBackedgeTakenInfo(L).Max; |
| 2856 | } |
| 2857 | |
| 2858 | const ScalarEvolution::BackedgeTakenInfo & |
| 2859 | ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2860 | // Initially insert a CouldNotCompute for this loop. If the insertion |
| 2861 | // succeeds, procede to actually compute a backedge-taken count and |
| 2862 | // update the value. The temporary CouldNotCompute value tells SCEV |
| 2863 | // code elsewhere that it shouldn't attempt to request a new |
| 2864 | // backedge-taken count, which could result in infinite recursion. |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2865 | std::pair<std::map<const Loop*, BackedgeTakenInfo>::iterator, bool> Pair = |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2866 | BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute())); |
| 2867 | if (Pair.second) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2868 | BackedgeTakenInfo ItCount = ComputeBackedgeTakenCount(L); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2869 | if (ItCount.Exact != getCouldNotCompute()) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2870 | assert(ItCount.Exact->isLoopInvariant(L) && |
| 2871 | ItCount.Max->isLoopInvariant(L) && |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2872 | "Computed trip count isn't loop invariant for loop!"); |
| 2873 | ++NumTripCountsComputed; |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2874 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2875 | // Update the value in the map. |
| 2876 | Pair.first->second = ItCount; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2877 | } else { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2878 | if (ItCount.Max != getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2879 | // Update the value in the map. |
| 2880 | Pair.first->second = ItCount; |
| 2881 | if (isa<PHINode>(L->getHeader()->begin())) |
| 2882 | // Only count loops that have phi nodes as not being computable. |
| 2883 | ++NumTripCountsNotComputed; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2884 | } |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2885 | |
| 2886 | // Now that we know more about the trip count for this loop, forget any |
| 2887 | // existing SCEV values for PHI nodes in this loop since they are only |
| 2888 | // conservative estimates made without the benefit |
| 2889 | // of trip count information. |
| 2890 | if (ItCount.hasAnyInfo()) |
Dan Gohman | fb7d35f | 2009-05-02 17:43:35 +0000 | [diff] [blame] | 2891 | forgetLoopPHIs(L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2892 | } |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 2893 | return Pair.first->second; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2896 | /// forgetLoopBackedgeTakenCount - This method should be called by the |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 2897 | /// client when it has changed a loop in a way that may effect |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2898 | /// ScalarEvolution's ability to compute a trip count, or if the loop |
| 2899 | /// is deleted. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2900 | void ScalarEvolution::forgetLoopBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2901 | BackedgeTakenCounts.erase(L); |
Dan Gohman | fb7d35f | 2009-05-02 17:43:35 +0000 | [diff] [blame] | 2902 | forgetLoopPHIs(L); |
| 2903 | } |
| 2904 | |
| 2905 | /// forgetLoopPHIs - Delete the memoized SCEVs associated with the |
| 2906 | /// PHI nodes in the given loop. This is used when the trip count of |
| 2907 | /// the loop may have changed. |
| 2908 | void ScalarEvolution::forgetLoopPHIs(const Loop *L) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2909 | BasicBlock *Header = L->getHeader(); |
| 2910 | |
Dan Gohman | efb9fbf | 2009-05-12 01:27:58 +0000 | [diff] [blame] | 2911 | // Push all Loop-header PHIs onto the Worklist stack, except those |
| 2912 | // that are presently represented via a SCEVUnknown. SCEVUnknown for |
| 2913 | // a PHI either means that it has an unrecognized structure, or it's |
| 2914 | // a PHI that's in the progress of being computed by createNodeForPHI. |
| 2915 | // In the former case, additional loop trip count information isn't |
| 2916 | // going to change anything. In the later case, createNodeForPHI will |
| 2917 | // perform the necessary updates on its own when it gets to that point. |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2918 | SmallVector<Instruction *, 16> Worklist; |
| 2919 | for (BasicBlock::iterator I = Header->begin(); |
Dan Gohman | efb9fbf | 2009-05-12 01:27:58 +0000 | [diff] [blame] | 2920 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2921 | std::map<SCEVCallbackVH, const SCEV*>::iterator It = |
| 2922 | Scalars.find((Value*)I); |
Dan Gohman | efb9fbf | 2009-05-12 01:27:58 +0000 | [diff] [blame] | 2923 | if (It != Scalars.end() && !isa<SCEVUnknown>(It->second)) |
| 2924 | Worklist.push_back(PN); |
| 2925 | } |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2926 | |
| 2927 | while (!Worklist.empty()) { |
| 2928 | Instruction *I = Worklist.pop_back_val(); |
| 2929 | if (Scalars.erase(I)) |
| 2930 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 2931 | UI != UE; ++UI) |
| 2932 | Worklist.push_back(cast<Instruction>(UI)); |
| 2933 | } |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 2936 | /// ComputeBackedgeTakenCount - Compute the number of times the backedge |
| 2937 | /// of the specified loop will execute. |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 2938 | ScalarEvolution::BackedgeTakenInfo |
| 2939 | ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) { |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2940 | SmallVector<BasicBlock*, 8> ExitingBlocks; |
| 2941 | L->getExitingBlocks(ExitingBlocks); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2942 | |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2943 | // Examine all exits and pick the most conservative values. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2944 | const SCEV* BECount = getCouldNotCompute(); |
| 2945 | const SCEV* MaxBECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2946 | bool CouldNotComputeBECount = false; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2947 | for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { |
| 2948 | BackedgeTakenInfo NewBTI = |
| 2949 | ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2950 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2951 | if (NewBTI.Exact == getCouldNotCompute()) { |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2952 | // We couldn't compute an exact value for this exit, so |
Dan Gohman | d32f5bf | 2009-06-22 21:10:22 +0000 | [diff] [blame] | 2953 | // 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] | 2954 | CouldNotComputeBECount = true; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2955 | BECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2956 | } else if (!CouldNotComputeBECount) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2957 | if (BECount == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2958 | BECount = NewBTI.Exact; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2959 | else |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 2960 | BECount = getUMinFromMismatchedTypes(BECount, NewBTI.Exact); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2961 | } |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2962 | if (MaxBECount == getCouldNotCompute()) |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 2963 | MaxBECount = NewBTI.Max; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2964 | else if (NewBTI.Max != getCouldNotCompute()) |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 2965 | MaxBECount = getUMinFromMismatchedTypes(MaxBECount, NewBTI.Max); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
| 2968 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 2969 | } |
| 2970 | |
| 2971 | /// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge |
| 2972 | /// of the specified loop will execute if it exits via the specified block. |
| 2973 | ScalarEvolution::BackedgeTakenInfo |
| 2974 | ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L, |
| 2975 | BasicBlock *ExitingBlock) { |
| 2976 | |
| 2977 | // Okay, we've chosen an exiting block. See what condition causes us to |
| 2978 | // exit at this block. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2979 | // |
| 2980 | // 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] | 2981 | BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2982 | if (ExitBr == 0) return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2983 | assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!"); |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2984 | |
Chris Lattner | 8b0e360 | 2007-01-07 02:24:26 +0000 | [diff] [blame] | 2985 | // At this point, we know we have a conditional branch that determines whether |
| 2986 | // the loop is exited. However, we don't know if the branch is executed each |
| 2987 | // time through the loop. If not, then the execution count of the branch will |
| 2988 | // not be equal to the trip count of the loop. |
| 2989 | // |
| 2990 | // Currently we check for this by checking to see if the Exit branch goes to |
| 2991 | // 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] | 2992 | // 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] | 2993 | // loop header. This is common for un-rotated loops. |
| 2994 | // |
| 2995 | // If both of those tests fail, walk up the unique predecessor chain to the |
| 2996 | // header, stopping if there is an edge that doesn't exit the loop. If the |
| 2997 | // header is reached, the execution count of the branch will be equal to the |
| 2998 | // trip count of the loop. |
| 2999 | // |
| 3000 | // More extensive analysis could be done to handle more cases here. |
| 3001 | // |
Chris Lattner | 8b0e360 | 2007-01-07 02:24:26 +0000 | [diff] [blame] | 3002 | if (ExitBr->getSuccessor(0) != L->getHeader() && |
Chris Lattner | 192e403 | 2007-01-14 01:24:47 +0000 | [diff] [blame] | 3003 | ExitBr->getSuccessor(1) != L->getHeader() && |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3004 | ExitBr->getParent() != L->getHeader()) { |
| 3005 | // The simple checks failed, try climbing the unique predecessor chain |
| 3006 | // up to the header. |
| 3007 | bool Ok = false; |
| 3008 | for (BasicBlock *BB = ExitBr->getParent(); BB; ) { |
| 3009 | BasicBlock *Pred = BB->getUniquePredecessor(); |
| 3010 | if (!Pred) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3011 | return getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3012 | TerminatorInst *PredTerm = Pred->getTerminator(); |
| 3013 | for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) { |
| 3014 | BasicBlock *PredSucc = PredTerm->getSuccessor(i); |
| 3015 | if (PredSucc == BB) |
| 3016 | continue; |
| 3017 | // If the predecessor has a successor that isn't BB and isn't |
| 3018 | // outside the loop, assume the worst. |
| 3019 | if (L->contains(PredSucc)) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3020 | return getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3021 | } |
| 3022 | if (Pred == L->getHeader()) { |
| 3023 | Ok = true; |
| 3024 | break; |
| 3025 | } |
| 3026 | BB = Pred; |
| 3027 | } |
| 3028 | if (!Ok) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3029 | return getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | // Procede to the next level to examine the exit condition expression. |
| 3033 | return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(), |
| 3034 | ExitBr->getSuccessor(0), |
| 3035 | ExitBr->getSuccessor(1)); |
| 3036 | } |
| 3037 | |
| 3038 | /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the |
| 3039 | /// backedge of the specified loop will execute if its exit condition |
| 3040 | /// were a conditional branch of ExitCond, TBB, and FBB. |
| 3041 | ScalarEvolution::BackedgeTakenInfo |
| 3042 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L, |
| 3043 | Value *ExitCond, |
| 3044 | BasicBlock *TBB, |
| 3045 | BasicBlock *FBB) { |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3046 | // 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] | 3047 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) { |
| 3048 | if (BO->getOpcode() == Instruction::And) { |
| 3049 | // Recurse on the operands of the and. |
| 3050 | BackedgeTakenInfo BTI0 = |
| 3051 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 3052 | BackedgeTakenInfo BTI1 = |
| 3053 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3054 | const SCEV* BECount = getCouldNotCompute(); |
| 3055 | const SCEV* MaxBECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3056 | if (L->contains(TBB)) { |
| 3057 | // Both conditions must be true for the loop to continue executing. |
| 3058 | // Choose the less conservative count. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3059 | if (BTI0.Exact == getCouldNotCompute() || |
| 3060 | BTI1.Exact == getCouldNotCompute()) |
| 3061 | BECount = getCouldNotCompute(); |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3062 | else |
| 3063 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3064 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3065 | MaxBECount = BTI1.Max; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3066 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3067 | MaxBECount = BTI0.Max; |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3068 | else |
| 3069 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3070 | } else { |
| 3071 | // Both conditions must be true for the loop to exit. |
| 3072 | assert(L->contains(FBB) && "Loop block has no successor in loop!"); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3073 | if (BTI0.Exact != getCouldNotCompute() && |
| 3074 | BTI1.Exact != getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3075 | BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3076 | if (BTI0.Max != getCouldNotCompute() && |
| 3077 | BTI1.Max != getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3078 | MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max); |
| 3079 | } |
| 3080 | |
| 3081 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3082 | } |
| 3083 | if (BO->getOpcode() == Instruction::Or) { |
| 3084 | // Recurse on the operands of the or. |
| 3085 | BackedgeTakenInfo BTI0 = |
| 3086 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 3087 | BackedgeTakenInfo BTI1 = |
| 3088 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3089 | const SCEV* BECount = getCouldNotCompute(); |
| 3090 | const SCEV* MaxBECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3091 | if (L->contains(FBB)) { |
| 3092 | // Both conditions must be false for the loop to continue executing. |
| 3093 | // Choose the less conservative count. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3094 | if (BTI0.Exact == getCouldNotCompute() || |
| 3095 | BTI1.Exact == getCouldNotCompute()) |
| 3096 | BECount = getCouldNotCompute(); |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3097 | else |
| 3098 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3099 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3100 | MaxBECount = BTI1.Max; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3101 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3102 | MaxBECount = BTI0.Max; |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3103 | else |
| 3104 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3105 | } else { |
| 3106 | // Both conditions must be false for the loop to exit. |
| 3107 | assert(L->contains(TBB) && "Loop block has no successor in loop!"); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3108 | if (BTI0.Exact != getCouldNotCompute() && |
| 3109 | BTI1.Exact != getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3110 | BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3111 | if (BTI0.Max != getCouldNotCompute() && |
| 3112 | BTI1.Max != getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3113 | MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max); |
| 3114 | } |
| 3115 | |
| 3116 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3117 | } |
| 3118 | } |
| 3119 | |
| 3120 | // With an icmp, it may be feasible to compute an exact backedge-taken count. |
| 3121 | // Procede to the next level to examine the icmp. |
| 3122 | if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) |
| 3123 | return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3124 | |
Eli Friedman | 361e54d | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3125 | // 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] | 3126 | return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
| 3127 | } |
| 3128 | |
| 3129 | /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the |
| 3130 | /// backedge of the specified loop will execute if its exit condition |
| 3131 | /// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB. |
| 3132 | ScalarEvolution::BackedgeTakenInfo |
| 3133 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L, |
| 3134 | ICmpInst *ExitCond, |
| 3135 | BasicBlock *TBB, |
| 3136 | BasicBlock *FBB) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3137 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3138 | // If the condition was exit on true, convert the condition to exit on false |
| 3139 | ICmpInst::Predicate Cond; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3140 | if (!L->contains(FBB)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3141 | Cond = ExitCond->getPredicate(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3142 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3143 | Cond = ExitCond->getInversePredicate(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3144 | |
| 3145 | // Handle common loops like: for (X = "string"; *X; ++X) |
| 3146 | if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0))) |
| 3147 | if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3148 | const SCEV* ItCnt = |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3149 | ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3150 | if (!isa<SCEVCouldNotCompute>(ItCnt)) { |
| 3151 | unsigned BitWidth = getTypeSizeInBits(ItCnt->getType()); |
| 3152 | return BackedgeTakenInfo(ItCnt, |
| 3153 | isa<SCEVConstant>(ItCnt) ? ItCnt : |
| 3154 | getConstant(APInt::getMaxValue(BitWidth)-1)); |
| 3155 | } |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3158 | const SCEV* LHS = getSCEV(ExitCond->getOperand(0)); |
| 3159 | const SCEV* RHS = getSCEV(ExitCond->getOperand(1)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3160 | |
| 3161 | // Try to evaluate any dependencies out of the loop. |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3162 | LHS = getSCEVAtScope(LHS, L); |
| 3163 | RHS = getSCEVAtScope(RHS, L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3164 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3165 | // 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] | 3166 | // loop the predicate will return true for these inputs. |
Dan Gohman | 70ff4cf | 2008-09-16 18:52:57 +0000 | [diff] [blame] | 3167 | if (LHS->isLoopInvariant(L) && !RHS->isLoopInvariant(L)) { |
| 3168 | // If there is a loop-invariant, force it into the RHS. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3169 | std::swap(LHS, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3170 | Cond = ICmpInst::getSwappedPredicate(Cond); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3173 | // If we have a comparison of a chrec against a constant, try to use value |
| 3174 | // ranges to answer this query. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3175 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) |
| 3176 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS)) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3177 | if (AddRec->getLoop() == L) { |
Eli Friedman | 361e54d | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3178 | // Form the constant range. |
| 3179 | ConstantRange CompRange( |
| 3180 | ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue())); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3181 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3182 | const SCEV* Ret = AddRec->getNumIterationsInRange(CompRange, *this); |
Eli Friedman | 361e54d | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3183 | if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3184 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3185 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3186 | switch (Cond) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3187 | case ICmpInst::ICMP_NE: { // while (X != Y) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3188 | // Convert to: while (X-Y != 0) |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3189 | const SCEV* TC = HowFarToZero(getMinusSCEV(LHS, RHS), L); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3190 | if (!isa<SCEVCouldNotCompute>(TC)) return TC; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3191 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3192 | } |
| 3193 | case ICmpInst::ICMP_EQ: { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3194 | // Convert to: while (X-Y == 0) // while (X == Y) |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3195 | const SCEV* TC = HowFarToNonZero(getMinusSCEV(LHS, RHS), L); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3196 | if (!isa<SCEVCouldNotCompute>(TC)) return TC; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3197 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3198 | } |
| 3199 | case ICmpInst::ICMP_SLT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3200 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true); |
| 3201 | if (BTI.hasAnyInfo()) return BTI; |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 3202 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3203 | } |
| 3204 | case ICmpInst::ICMP_SGT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3205 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 3206 | getNotSCEV(RHS), L, true); |
| 3207 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | d6dac0e | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 3208 | break; |
| 3209 | } |
| 3210 | case ICmpInst::ICMP_ULT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3211 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false); |
| 3212 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | d6dac0e | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 3213 | break; |
| 3214 | } |
| 3215 | case ICmpInst::ICMP_UGT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3216 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 3217 | getNotSCEV(RHS), L, false); |
| 3218 | if (BTI.hasAnyInfo()) return BTI; |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 3219 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3220 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3221 | default: |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 3222 | #if 0 |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3223 | errs() << "ComputeBackedgeTakenCount "; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3224 | if (ExitCond->getOperand(0)->getType()->isUnsigned()) |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3225 | errs() << "[unsigned] "; |
| 3226 | errs() << *LHS << " " |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3227 | << Instruction::getOpcodeName(Instruction::ICmp) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3228 | << " " << *RHS << "\n"; |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 3229 | #endif |
Chris Lattner | e34c0b4 | 2004-04-03 00:43:03 +0000 | [diff] [blame] | 3230 | break; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3231 | } |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3232 | return |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3233 | ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3234 | } |
| 3235 | |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3236 | static ConstantInt * |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3237 | EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, |
| 3238 | ScalarEvolution &SE) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3239 | const SCEV* InVal = SE.getConstant(C); |
| 3240 | const SCEV* Val = AddRec->evaluateAtIteration(InVal, SE); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3241 | assert(isa<SCEVConstant>(Val) && |
| 3242 | "Evaluation of SCEV at constant didn't fold correctly?"); |
| 3243 | return cast<SCEVConstant>(Val)->getValue(); |
| 3244 | } |
| 3245 | |
| 3246 | /// GetAddressedElementFromGlobal - Given a global variable with an initializer |
| 3247 | /// and a GEP expression (missing the pointer index) indexing into it, return |
| 3248 | /// the addressed element of the initializer or null if the index expression is |
| 3249 | /// invalid. |
| 3250 | static Constant * |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3251 | GetAddressedElementFromGlobal(GlobalVariable *GV, |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3252 | const std::vector<ConstantInt*> &Indices) { |
| 3253 | Constant *Init = GV->getInitializer(); |
| 3254 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3255 | uint64_t Idx = Indices[i]->getZExtValue(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3256 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) { |
| 3257 | assert(Idx < CS->getNumOperands() && "Bad struct index!"); |
| 3258 | Init = cast<Constant>(CS->getOperand(Idx)); |
| 3259 | } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) { |
| 3260 | if (Idx >= CA->getNumOperands()) return 0; // Bogus program |
| 3261 | Init = cast<Constant>(CA->getOperand(Idx)); |
| 3262 | } else if (isa<ConstantAggregateZero>(Init)) { |
| 3263 | if (const StructType *STy = dyn_cast<StructType>(Init->getType())) { |
| 3264 | assert(Idx < STy->getNumElements() && "Bad struct index!"); |
| 3265 | Init = Constant::getNullValue(STy->getElementType(Idx)); |
| 3266 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) { |
| 3267 | if (Idx >= ATy->getNumElements()) return 0; // Bogus program |
| 3268 | Init = Constant::getNullValue(ATy->getElementType()); |
| 3269 | } else { |
| 3270 | assert(0 && "Unknown constant aggregate type!"); |
| 3271 | } |
| 3272 | return 0; |
| 3273 | } else { |
| 3274 | return 0; // Unknown initializer type |
| 3275 | } |
| 3276 | } |
| 3277 | return Init; |
| 3278 | } |
| 3279 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3280 | /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of |
| 3281 | /// 'icmp op load X, cst', try to see if we can compute the backedge |
| 3282 | /// execution count. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3283 | const SCEV * |
| 3284 | ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount( |
| 3285 | LoadInst *LI, |
| 3286 | Constant *RHS, |
| 3287 | const Loop *L, |
| 3288 | ICmpInst::Predicate predicate) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3289 | if (LI->isVolatile()) return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3290 | |
| 3291 | // Check to see if the loaded pointer is a getelementptr of a global. |
| 3292 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3293 | if (!GEP) return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3294 | |
| 3295 | // Make sure that it is really a constant global we are gepping, with an |
| 3296 | // initializer, and make sure the first IDX is really 0. |
| 3297 | GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); |
| 3298 | if (!GV || !GV->isConstant() || !GV->hasInitializer() || |
| 3299 | GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) || |
| 3300 | !cast<Constant>(GEP->getOperand(1))->isNullValue()) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3301 | return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3302 | |
| 3303 | // Okay, we allow one non-constant index into the GEP instruction. |
| 3304 | Value *VarIdx = 0; |
| 3305 | std::vector<ConstantInt*> Indexes; |
| 3306 | unsigned VarIdxNum = 0; |
| 3307 | for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) |
| 3308 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 3309 | Indexes.push_back(CI); |
| 3310 | } else if (!isa<ConstantInt>(GEP->getOperand(i))) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3311 | if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's. |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3312 | VarIdx = GEP->getOperand(i); |
| 3313 | VarIdxNum = i-2; |
| 3314 | Indexes.push_back(0); |
| 3315 | } |
| 3316 | |
| 3317 | // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant. |
| 3318 | // Check to see if X is a loop variant variable value now. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3319 | const SCEV* Idx = getSCEV(VarIdx); |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3320 | Idx = getSCEVAtScope(Idx, L); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3321 | |
| 3322 | // We can only recognize very limited forms of loop index expressions, in |
| 3323 | // particular, only affine AddRec's like {C1,+,C2}. |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3324 | const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3325 | if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) || |
| 3326 | !isa<SCEVConstant>(IdxExpr->getOperand(0)) || |
| 3327 | !isa<SCEVConstant>(IdxExpr->getOperand(1))) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3328 | return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3329 | |
| 3330 | unsigned MaxSteps = MaxBruteForceIterations; |
| 3331 | for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3332 | ConstantInt *ItCst = |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 3333 | ConstantInt::get(cast<IntegerType>(IdxExpr->getType()), IterationNum); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3334 | ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3335 | |
| 3336 | // Form the GEP offset. |
| 3337 | Indexes[VarIdxNum] = Val; |
| 3338 | |
| 3339 | Constant *Result = GetAddressedElementFromGlobal(GV, Indexes); |
| 3340 | if (Result == 0) break; // Cannot compute! |
| 3341 | |
| 3342 | // Evaluate the condition for this iteration. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3343 | Result = ConstantExpr::getICmp(predicate, Result, RHS); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3344 | if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3345 | if (cast<ConstantInt>(Result)->getValue().isMinValue()) { |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3346 | #if 0 |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3347 | errs() << "\n***\n*** Computed loop count " << *ItCst |
| 3348 | << "\n*** From global " << *GV << "*** BB: " << *L->getHeader() |
| 3349 | << "***\n"; |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3350 | #endif |
| 3351 | ++NumArrayLenItCounts; |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3352 | return getConstant(ItCst); // Found terminating iteration! |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3353 | } |
| 3354 | } |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3355 | return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
| 3358 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3359 | /// CanConstantFold - Return true if we can constant fold an instruction of the |
| 3360 | /// specified type, assuming that all operands were constants. |
| 3361 | static bool CanConstantFold(const Instruction *I) { |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3362 | if (isa<BinaryOperator>(I) || isa<CmpInst>(I) || |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3363 | isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I)) |
| 3364 | return true; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3365 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3366 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 3367 | if (const Function *F = CI->getCalledFunction()) |
Dan Gohman | fa9b80e | 2008-01-31 01:05:10 +0000 | [diff] [blame] | 3368 | return canConstantFoldCallTo(F); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3369 | return false; |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3372 | /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node |
| 3373 | /// in the loop that V is derived from. We allow arbitrary operations along the |
| 3374 | /// way, but the operands of an operation must either be constants or a value |
| 3375 | /// derived from a constant PHI. If this expression does not fit with these |
| 3376 | /// constraints, return null. |
| 3377 | static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) { |
| 3378 | // If this is not an instruction, or if this is an instruction outside of the |
| 3379 | // loop, it can't be derived from a loop PHI. |
| 3380 | Instruction *I = dyn_cast<Instruction>(V); |
| 3381 | if (I == 0 || !L->contains(I->getParent())) return 0; |
| 3382 | |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 3383 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3384 | if (L->getHeader() == I->getParent()) |
| 3385 | return PN; |
| 3386 | else |
| 3387 | // We don't currently keep track of the control flow needed to evaluate |
| 3388 | // PHIs, so we cannot handle PHIs inside of loops. |
| 3389 | return 0; |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 3390 | } |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3391 | |
| 3392 | // If we won't be able to constant fold this expression even if the operands |
| 3393 | // are constants, return early. |
| 3394 | if (!CanConstantFold(I)) return 0; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3395 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3396 | // Otherwise, we can evaluate this instruction if all of its operands are |
| 3397 | // constant or derived from a PHI node themselves. |
| 3398 | PHINode *PHI = 0; |
| 3399 | for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op) |
| 3400 | if (!(isa<Constant>(I->getOperand(Op)) || |
| 3401 | isa<GlobalValue>(I->getOperand(Op)))) { |
| 3402 | PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L); |
| 3403 | if (P == 0) return 0; // Not evolving from PHI |
| 3404 | if (PHI == 0) |
| 3405 | PHI = P; |
| 3406 | else if (PHI != P) |
| 3407 | return 0; // Evolving from multiple different PHIs. |
| 3408 | } |
| 3409 | |
| 3410 | // This is a expression evolving from a constant PHI! |
| 3411 | return PHI; |
| 3412 | } |
| 3413 | |
| 3414 | /// EvaluateExpression - Given an expression that passes the |
| 3415 | /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node |
| 3416 | /// in the loop has the value PHIVal. If we can't fold this expression for some |
| 3417 | /// reason, return null. |
| 3418 | static Constant *EvaluateExpression(Value *V, Constant *PHIVal) { |
| 3419 | if (isa<PHINode>(V)) return PHIVal; |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 3420 | if (Constant *C = dyn_cast<Constant>(V)) return C; |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3421 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3422 | Instruction *I = cast<Instruction>(V); |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame^] | 3423 | LLVMContext* Context = I->getParent()->getContext(); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3424 | |
| 3425 | std::vector<Constant*> Operands; |
| 3426 | Operands.resize(I->getNumOperands()); |
| 3427 | |
| 3428 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 3429 | Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal); |
| 3430 | if (Operands[i] == 0) return 0; |
| 3431 | } |
| 3432 | |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3433 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 3434 | return ConstantFoldCompareInstOperands(CI->getPredicate(), |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame^] | 3435 | &Operands[0], Operands.size(), |
| 3436 | Context); |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3437 | else |
| 3438 | return ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame^] | 3439 | &Operands[0], Operands.size(), |
| 3440 | Context); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
| 3443 | /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is |
| 3444 | /// in the header of its containing loop, we know the loop executes a |
| 3445 | /// constant number of times, and the PHI node is just a recurrence |
| 3446 | /// involving constants, fold it. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3447 | Constant * |
| 3448 | ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN, |
| 3449 | const APInt& BEs, |
| 3450 | const Loop *L) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3451 | std::map<PHINode*, Constant*>::iterator I = |
| 3452 | ConstantEvolutionLoopExitValue.find(PN); |
| 3453 | if (I != ConstantEvolutionLoopExitValue.end()) |
| 3454 | return I->second; |
| 3455 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3456 | if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations))) |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3457 | return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it. |
| 3458 | |
| 3459 | Constant *&RetVal = ConstantEvolutionLoopExitValue[PN]; |
| 3460 | |
| 3461 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 3462 | // entry must be a constant (coming in from outside of the loop), and the |
| 3463 | // second must be derived from the same PHI. |
| 3464 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 3465 | Constant *StartCST = |
| 3466 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
| 3467 | if (StartCST == 0) |
| 3468 | return RetVal = 0; // Must be a constant. |
| 3469 | |
| 3470 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
| 3471 | PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); |
| 3472 | if (PN2 != PN) |
| 3473 | return RetVal = 0; // Not derived from same PHI. |
| 3474 | |
| 3475 | // Execute the loop symbolically to determine the exit value. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3476 | if (BEs.getActiveBits() >= 32) |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3477 | return RetVal = 0; // More than 2^32-1 iterations?? Not doing it! |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3478 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3479 | unsigned NumIterations = BEs.getZExtValue(); // must be in range |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3480 | unsigned IterationNum = 0; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3481 | for (Constant *PHIVal = StartCST; ; ++IterationNum) { |
| 3482 | if (IterationNum == NumIterations) |
| 3483 | return RetVal = PHIVal; // Got exit value! |
| 3484 | |
| 3485 | // Compute the value of the PHI node for the next iteration. |
| 3486 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal); |
| 3487 | if (NextPHI == PHIVal) |
| 3488 | return RetVal = NextPHI; // Stopped evolving! |
| 3489 | if (NextPHI == 0) |
| 3490 | return 0; // Couldn't evaluate! |
| 3491 | PHIVal = NextPHI; |
| 3492 | } |
| 3493 | } |
| 3494 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3495 | /// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute a |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3496 | /// constant number of times (the condition evolves only from constants), |
| 3497 | /// try to evaluate a few iterations of the loop until we get the exit |
| 3498 | /// condition gets a value of ExitWhen (true or false). If we cannot |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3499 | /// evaluate the trip count of the loop, return getCouldNotCompute(). |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3500 | const SCEV * |
| 3501 | ScalarEvolution::ComputeBackedgeTakenCountExhaustively(const Loop *L, |
| 3502 | Value *Cond, |
| 3503 | bool ExitWhen) { |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3504 | PHINode *PN = getConstantEvolvingPHI(Cond, L); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3505 | if (PN == 0) return getCouldNotCompute(); |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3506 | |
| 3507 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 3508 | // entry must be a constant (coming in from outside of the loop), and the |
| 3509 | // second must be derived from the same PHI. |
| 3510 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 3511 | Constant *StartCST = |
| 3512 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3513 | if (StartCST == 0) return getCouldNotCompute(); // Must be a constant. |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3514 | |
| 3515 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
| 3516 | PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3517 | if (PN2 != PN) return getCouldNotCompute(); // Not derived from same PHI. |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3518 | |
| 3519 | // Okay, we find a PHI node that defines the trip count of this loop. Execute |
| 3520 | // the loop symbolically to determine when the condition gets a value of |
| 3521 | // "ExitWhen". |
| 3522 | unsigned IterationNum = 0; |
| 3523 | unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis. |
| 3524 | for (Constant *PHIVal = StartCST; |
| 3525 | IterationNum != MaxIterations; ++IterationNum) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3526 | ConstantInt *CondVal = |
| 3527 | dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal)); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3528 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3529 | // Couldn't symbolically evaluate. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3530 | if (!CondVal) return getCouldNotCompute(); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3531 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3532 | if (CondVal->getValue() == uint64_t(ExitWhen)) { |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3533 | ++NumBruteForceTripCountsComputed; |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 3534 | return getConstant(Type::Int32Ty, IterationNum); |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3535 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3536 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3537 | // Compute the value of the PHI node for the next iteration. |
| 3538 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal); |
| 3539 | if (NextPHI == 0 || NextPHI == PHIVal) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3540 | return getCouldNotCompute();// Couldn't evaluate or not making progress... |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3541 | PHIVal = NextPHI; |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
| 3544 | // Too many iterations were needed to evaluate. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3545 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3546 | } |
| 3547 | |
Dan Gohman | 66a7e85 | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 3548 | /// getSCEVAtScope - Return a SCEV expression handle for the specified value |
| 3549 | /// at the specified scope in the program. The L value specifies a loop |
| 3550 | /// nest to evaluate the expression at, where null is the top-level or a |
| 3551 | /// specified loop is immediately inside of the loop. |
| 3552 | /// |
| 3553 | /// This method can be used to compute the exit value for a variable defined |
| 3554 | /// in a loop by querying what the value will hold in the parent loop. |
| 3555 | /// |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3556 | /// In the case that a relevant loop exit value cannot be computed, the |
| 3557 | /// original value V is returned. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3558 | const SCEV* ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3559 | // FIXME: this should be turned into a virtual method on SCEV! |
| 3560 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3561 | if (isa<SCEVConstant>(V)) return V; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3562 | |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 3563 | // If this instruction is evolved from a constant-evolving PHI, compute the |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3564 | // exit value from the loop without using SCEVs. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3565 | if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3566 | if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3567 | const Loop *LI = (*this->LI)[I->getParent()]; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3568 | if (LI && LI->getParentLoop() == L) // Looking for loop exit value. |
| 3569 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3570 | if (PN->getParent() == LI->getHeader()) { |
| 3571 | // Okay, there is no closed form solution for the PHI node. Check |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3572 | // to see if the loop that contains it has a known backedge-taken |
| 3573 | // count. If so, we may be able to force computation of the exit |
| 3574 | // value. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3575 | const SCEV* BackedgeTakenCount = getBackedgeTakenCount(LI); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3576 | if (const SCEVConstant *BTCC = |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3577 | dyn_cast<SCEVConstant>(BackedgeTakenCount)) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3578 | // Okay, we know how many times the containing loop executes. If |
| 3579 | // this is a constant evolving PHI node, get the final value at |
| 3580 | // the specified iteration number. |
| 3581 | Constant *RV = getConstantEvolutionLoopExitValue(PN, |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3582 | BTCC->getValue()->getValue(), |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3583 | LI); |
Dan Gohman | 0998796 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3584 | if (RV) return getSCEV(RV); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3585 | } |
| 3586 | } |
| 3587 | |
Reid Spencer | 09906f3 | 2006-12-04 21:33:23 +0000 | [diff] [blame] | 3588 | // Okay, this is an expression that we cannot symbolically evaluate |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3589 | // 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] | 3590 | // the arguments into constants, and if so, try to constant propagate the |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3591 | // result. This is particularly useful for computing loop exit values. |
| 3592 | if (CanConstantFold(I)) { |
Dan Gohman | 6bce643 | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3593 | // Check to see if we've folded this instruction at this loop before. |
| 3594 | std::map<const Loop *, Constant *> &Values = ValuesAtScopes[I]; |
| 3595 | std::pair<std::map<const Loop *, Constant *>::iterator, bool> Pair = |
| 3596 | Values.insert(std::make_pair(L, static_cast<Constant *>(0))); |
| 3597 | if (!Pair.second) |
Dan Gohman | 0998796 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3598 | return Pair.first->second ? &*getSCEV(Pair.first->second) : V; |
Dan Gohman | 6bce643 | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3599 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3600 | std::vector<Constant*> Operands; |
| 3601 | Operands.reserve(I->getNumOperands()); |
| 3602 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 3603 | Value *Op = I->getOperand(i); |
| 3604 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| 3605 | Operands.push_back(C); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3606 | } else { |
Chris Lattner | 42b5e08 | 2007-11-23 08:46:22 +0000 | [diff] [blame] | 3607 | // If any of the operands is non-constant and if they are |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3608 | // non-integer and non-pointer, don't even try to analyze them |
| 3609 | // with scev techniques. |
Dan Gohman | 4acd12a | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3610 | if (!isSCEVable(Op->getType())) |
Chris Lattner | 42b5e08 | 2007-11-23 08:46:22 +0000 | [diff] [blame] | 3611 | return V; |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3612 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3613 | const SCEV* OpV = getSCEVAtScope(getSCEV(Op), L); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3614 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) { |
Dan Gohman | 4acd12a | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3615 | Constant *C = SC->getValue(); |
| 3616 | if (C->getType() != Op->getType()) |
| 3617 | C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 3618 | Op->getType(), |
| 3619 | false), |
| 3620 | C, Op->getType()); |
| 3621 | Operands.push_back(C); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3622 | } else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) { |
Dan Gohman | 4acd12a | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3623 | if (Constant *C = dyn_cast<Constant>(SU->getValue())) { |
| 3624 | if (C->getType() != Op->getType()) |
| 3625 | C = |
| 3626 | ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 3627 | Op->getType(), |
| 3628 | false), |
| 3629 | C, Op->getType()); |
| 3630 | Operands.push_back(C); |
| 3631 | } else |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3632 | return V; |
| 3633 | } else { |
| 3634 | return V; |
| 3635 | } |
| 3636 | } |
| 3637 | } |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3638 | |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3639 | Constant *C; |
| 3640 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 3641 | C = ConstantFoldCompareInstOperands(CI->getPredicate(), |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame^] | 3642 | &Operands[0], Operands.size(), |
| 3643 | Context); |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3644 | else |
| 3645 | C = ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame^] | 3646 | &Operands[0], Operands.size(), Context); |
Dan Gohman | 6bce643 | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3647 | Pair.first->second = C; |
Dan Gohman | 0998796 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3648 | return getSCEV(C); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3649 | } |
| 3650 | } |
| 3651 | |
| 3652 | // This is some other type of SCEVUnknown, just return it. |
| 3653 | return V; |
| 3654 | } |
| 3655 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3656 | if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3657 | // Avoid performing the look-up in the common case where the specified |
| 3658 | // expression has no loop-variant portions. |
| 3659 | for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3660 | const SCEV* OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3661 | if (OpAtScope != Comm->getOperand(i)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3662 | // Okay, at least one of these operands is loop variant but might be |
| 3663 | // foldable. Build a new instance of the folded commutative expression. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3664 | SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(), |
| 3665 | Comm->op_begin()+i); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3666 | NewOps.push_back(OpAtScope); |
| 3667 | |
| 3668 | for (++i; i != e; ++i) { |
| 3669 | OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3670 | NewOps.push_back(OpAtScope); |
| 3671 | } |
| 3672 | if (isa<SCEVAddExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3673 | return getAddExpr(NewOps); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 3674 | if (isa<SCEVMulExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3675 | return getMulExpr(NewOps); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 3676 | if (isa<SCEVSMaxExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3677 | return getSMaxExpr(NewOps); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 3678 | if (isa<SCEVUMaxExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3679 | return getUMaxExpr(NewOps); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 3680 | assert(0 && "Unknown commutative SCEV type!"); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3681 | } |
| 3682 | } |
| 3683 | // If we got here, all operands are loop invariant. |
| 3684 | return Comm; |
| 3685 | } |
| 3686 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3687 | if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3688 | const SCEV* LHS = getSCEVAtScope(Div->getLHS(), L); |
| 3689 | const SCEV* RHS = getSCEVAtScope(Div->getRHS(), L); |
Nick Lewycky | 789558d | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 3690 | if (LHS == Div->getLHS() && RHS == Div->getRHS()) |
| 3691 | return Div; // must be loop invariant |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3692 | return getUDivExpr(LHS, RHS); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
| 3695 | // If this is a loop recurrence for a loop that does not contain L, then we |
| 3696 | // are dealing with the final value computed by the loop. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3697 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3698 | if (!L || !AddRec->getLoop()->contains(L->getHeader())) { |
| 3699 | // To evaluate this recurrence, we need to know how many times the AddRec |
| 3700 | // loop iterates. Compute this now. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3701 | const SCEV* BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3702 | if (BackedgeTakenCount == getCouldNotCompute()) return AddRec; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3703 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 3704 | // Then, evaluate the AddRec. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3705 | return AddRec->evaluateAtIteration(BackedgeTakenCount, *this); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3706 | } |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3707 | return AddRec; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3710 | if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3711 | const SCEV* Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | eb3948b | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 3712 | if (Op == Cast->getOperand()) |
| 3713 | return Cast; // must be loop invariant |
| 3714 | return getZeroExtendExpr(Op, Cast->getType()); |
| 3715 | } |
| 3716 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3717 | if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3718 | const SCEV* Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | eb3948b | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 3719 | if (Op == Cast->getOperand()) |
| 3720 | return Cast; // must be loop invariant |
| 3721 | return getSignExtendExpr(Op, Cast->getType()); |
| 3722 | } |
| 3723 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3724 | if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3725 | const SCEV* Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | eb3948b | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 3726 | if (Op == Cast->getOperand()) |
| 3727 | return Cast; // must be loop invariant |
| 3728 | return getTruncateExpr(Op, Cast->getType()); |
| 3729 | } |
| 3730 | |
| 3731 | assert(0 && "Unknown SCEV type!"); |
Daniel Dunbar | 8c562e2 | 2009-05-18 16:43:04 +0000 | [diff] [blame] | 3732 | return 0; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3733 | } |
| 3734 | |
Dan Gohman | 66a7e85 | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 3735 | /// getSCEVAtScope - This is a convenience function which does |
| 3736 | /// getSCEVAtScope(getSCEV(V), L). |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3737 | const SCEV* ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3738 | return getSCEVAtScope(getSCEV(V), L); |
| 3739 | } |
| 3740 | |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3741 | /// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the |
| 3742 | /// following equation: |
| 3743 | /// |
| 3744 | /// A * X = B (mod N) |
| 3745 | /// |
| 3746 | /// where N = 2^BW and BW is the common bit width of A and B. The signedness of |
| 3747 | /// A and B isn't important. |
| 3748 | /// |
| 3749 | /// If the equation does not have a solution, SCEVCouldNotCompute is returned. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3750 | static const SCEV* SolveLinEquationWithOverflow(const APInt &A, const APInt &B, |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3751 | ScalarEvolution &SE) { |
| 3752 | uint32_t BW = A.getBitWidth(); |
| 3753 | assert(BW == B.getBitWidth() && "Bit widths must be the same."); |
| 3754 | assert(A != 0 && "A must be non-zero."); |
| 3755 | |
| 3756 | // 1. D = gcd(A, N) |
| 3757 | // |
| 3758 | // The gcd of A and N may have only one prime factor: 2. The number of |
| 3759 | // trailing zeros in A is its multiplicity |
| 3760 | uint32_t Mult2 = A.countTrailingZeros(); |
| 3761 | // D = 2^Mult2 |
| 3762 | |
| 3763 | // 2. Check if B is divisible by D. |
| 3764 | // |
| 3765 | // B is divisible by D if and only if the multiplicity of prime factor 2 for B |
| 3766 | // is not less than multiplicity of this prime factor for D. |
| 3767 | if (B.countTrailingZeros() < Mult2) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 3768 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3769 | |
| 3770 | // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic |
| 3771 | // modulo (N / D). |
| 3772 | // |
| 3773 | // (N / D) may need BW+1 bits in its representation. Hence, we'll use this |
| 3774 | // bit width during computations. |
| 3775 | APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D |
| 3776 | APInt Mod(BW + 1, 0); |
| 3777 | Mod.set(BW - Mult2); // Mod = N / D |
| 3778 | APInt I = AD.multiplicativeInverse(Mod); |
| 3779 | |
| 3780 | // 4. Compute the minimum unsigned root of the equation: |
| 3781 | // I * (B / D) mod (N / D) |
| 3782 | APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod); |
| 3783 | |
| 3784 | // The result is guaranteed to be less than 2^BW so we may truncate it to BW |
| 3785 | // bits. |
| 3786 | return SE.getConstant(Result.trunc(BW)); |
| 3787 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3788 | |
| 3789 | /// SolveQuadraticEquation - Find the roots of the quadratic equation for the |
| 3790 | /// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which |
| 3791 | /// might be the same) or two SCEVCouldNotCompute objects. |
| 3792 | /// |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3793 | static std::pair<const SCEV*,const SCEV*> |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3794 | SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3795 | assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3796 | const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0)); |
| 3797 | const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1)); |
| 3798 | const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2)); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3799 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3800 | // We currently can only solve this if the coefficients are constants. |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3801 | if (!LC || !MC || !NC) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3802 | const SCEV *CNC = SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3803 | return std::make_pair(CNC, CNC); |
| 3804 | } |
| 3805 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3806 | uint32_t BitWidth = LC->getValue()->getValue().getBitWidth(); |
Chris Lattner | fe560b8 | 2007-04-15 19:52:49 +0000 | [diff] [blame] | 3807 | const APInt &L = LC->getValue()->getValue(); |
| 3808 | const APInt &M = MC->getValue()->getValue(); |
| 3809 | const APInt &N = NC->getValue()->getValue(); |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3810 | APInt Two(BitWidth, 2); |
| 3811 | APInt Four(BitWidth, 4); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3812 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3813 | { |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3814 | using namespace APIntOps; |
Zhou Sheng | 414de4d | 2007-04-07 17:48:27 +0000 | [diff] [blame] | 3815 | const APInt& C = L; |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3816 | // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C |
| 3817 | // The B coefficient is M-N/2 |
| 3818 | APInt B(M); |
| 3819 | B -= sdiv(N,Two); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3820 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3821 | // The A coefficient is N/2 |
Zhou Sheng | 414de4d | 2007-04-07 17:48:27 +0000 | [diff] [blame] | 3822 | APInt A(N.sdiv(Two)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3823 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3824 | // Compute the B^2-4ac term. |
| 3825 | APInt SqrtTerm(B); |
| 3826 | SqrtTerm *= B; |
| 3827 | SqrtTerm -= Four * (A * C); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3828 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3829 | // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest |
| 3830 | // integer value or else APInt::sqrt() will assert. |
| 3831 | APInt SqrtVal(SqrtTerm.sqrt()); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3832 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3833 | // Compute the two solutions for the quadratic formula. |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3834 | // The divisions must be performed as signed divisions. |
| 3835 | APInt NegB(-B); |
Reid Spencer | 3e35c8d | 2007-04-16 02:24:41 +0000 | [diff] [blame] | 3836 | APInt TwoA( A << 1 ); |
Nick Lewycky | 8f4d5eb | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 3837 | if (TwoA.isMinValue()) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3838 | const SCEV *CNC = SE.getCouldNotCompute(); |
Nick Lewycky | 8f4d5eb | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 3839 | return std::make_pair(CNC, CNC); |
| 3840 | } |
| 3841 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3842 | ConstantInt *Solution1 = ConstantInt::get((NegB + SqrtVal).sdiv(TwoA)); |
| 3843 | ConstantInt *Solution2 = ConstantInt::get((NegB - SqrtVal).sdiv(TwoA)); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3844 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3845 | return std::make_pair(SE.getConstant(Solution1), |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3846 | SE.getConstant(Solution2)); |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3847 | } // end APIntOps namespace |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
| 3850 | /// HowFarToZero - Return the number of times a backedge comparing the specified |
Dan Gohman | 86fbf2f | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 3851 | /// value to zero will execute. If not computable, return CouldNotCompute. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3852 | const SCEV* ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3853 | // If the value is a constant |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3854 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3855 | // If the value is already zero, the branch will execute zero times. |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 3856 | if (C->getValue()->isZero()) return C; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3857 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3858 | } |
| 3859 | |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3860 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3861 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3862 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3863 | |
| 3864 | if (AddRec->isAffine()) { |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3865 | // If this is an affine expression, the execution count of this branch is |
| 3866 | // the minimum unsigned root of the following equation: |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3867 | // |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3868 | // Start + Step*N = 0 (mod 2^BW) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3869 | // |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3870 | // equivalent to: |
| 3871 | // |
| 3872 | // Step*N = -Start (mod 2^BW) |
| 3873 | // |
| 3874 | // where BW is the common bit width of Start and Step. |
| 3875 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3876 | // Get the initial value for the loop. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3877 | const SCEV *Start = getSCEVAtScope(AddRec->getStart(), |
| 3878 | L->getParentLoop()); |
| 3879 | const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), |
| 3880 | L->getParentLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3881 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3882 | if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) { |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3883 | // For now we handle only constant steps. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3884 | |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3885 | // First, handle unitary steps. |
| 3886 | if (StepC->getValue()->equalsInt(1)) // 1*N = -Start (mod 2^BW), so: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3887 | return getNegativeSCEV(Start); // N = -Start (as unsigned) |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3888 | if (StepC->getValue()->isAllOnesValue()) // -1*N = -Start (mod 2^BW), so: |
| 3889 | return Start; // N = Start (as unsigned) |
| 3890 | |
| 3891 | // Then, try to solve the above equation provided that Start is constant. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3892 | if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 3893 | return SolveLinEquationWithOverflow(StepC->getValue()->getValue(), |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3894 | -StartC->getValue()->getValue(), |
| 3895 | *this); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3896 | } |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 3897 | } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3898 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of |
| 3899 | // the quadratic equation to solve it. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3900 | std::pair<const SCEV*,const SCEV*> Roots = SolveQuadraticEquation(AddRec, |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3901 | *this); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3902 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 3903 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3904 | if (R1) { |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 3905 | #if 0 |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3906 | errs() << "HFTZ: " << *V << " - sol#1: " << *R1 |
| 3907 | << " sol#2: " << *R2 << "\n"; |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 3908 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3909 | // Pick the smallest positive root value. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3910 | if (ConstantInt *CB = |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3911 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3912 | R1->getValue(), R2->getValue()))) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 3913 | if (CB->getZExtValue() == false) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3914 | std::swap(R1, R2); // R1 is the minimum root now. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3915 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3916 | // We can only use this value if the chrec ends up with an exact zero |
| 3917 | // value at this index. When solving for "X*X != 5", for example, we |
| 3918 | // should not accept a root of 2. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3919 | const SCEV* Val = AddRec->evaluateAtIteration(R1, *this); |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 3920 | if (Val->isZero()) |
| 3921 | return R1; // We found a quadratic root! |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3922 | } |
| 3923 | } |
| 3924 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3925 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3926 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | /// HowFarToNonZero - Return the number of times a backedge checking the |
| 3930 | /// specified value for nonzero will execute. If not computable, return |
Dan Gohman | 86fbf2f | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 3931 | /// CouldNotCompute |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3932 | const SCEV* ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3933 | // Loops that look like: while (X == 0) are very strange indeed. We don't |
| 3934 | // handle them yet except for the trivial case. This could be expanded in the |
| 3935 | // future as needed. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3936 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3937 | // If the value is a constant, check to see if it is known to be non-zero |
| 3938 | // already. If so, the backedge will execute zero times. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3939 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Nick Lewycky | 39442af | 2008-02-21 09:14:53 +0000 | [diff] [blame] | 3940 | if (!C->getValue()->isNullValue()) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3941 | return getIntegerSCEV(0, C->getType()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3942 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3943 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3944 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3945 | // We could implement others, but I really doubt anyone writes loops like |
| 3946 | // this, and if they did, they would already be constant folded. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3947 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 3950 | /// getLoopPredecessor - If the given loop's header has exactly one unique |
| 3951 | /// predecessor outside the loop, return it. Otherwise return null. |
| 3952 | /// |
| 3953 | BasicBlock *ScalarEvolution::getLoopPredecessor(const Loop *L) { |
| 3954 | BasicBlock *Header = L->getHeader(); |
| 3955 | BasicBlock *Pred = 0; |
| 3956 | for (pred_iterator PI = pred_begin(Header), E = pred_end(Header); |
| 3957 | PI != E; ++PI) |
| 3958 | if (!L->contains(*PI)) { |
| 3959 | if (Pred && Pred != *PI) return 0; // Multiple predecessors. |
| 3960 | Pred = *PI; |
| 3961 | } |
| 3962 | return Pred; |
| 3963 | } |
| 3964 | |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 3965 | /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB |
| 3966 | /// (which may not be an immediate predecessor) which has exactly one |
| 3967 | /// successor from which BB is reachable, or null if no such block is |
| 3968 | /// found. |
| 3969 | /// |
| 3970 | BasicBlock * |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3971 | ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) { |
Dan Gohman | 3d739fe | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 3972 | // If the block has a unique predecessor, then there is no path from the |
| 3973 | // predecessor to the block that does not go through the direct edge |
| 3974 | // from the predecessor to the block. |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 3975 | if (BasicBlock *Pred = BB->getSinglePredecessor()) |
| 3976 | return Pred; |
| 3977 | |
| 3978 | // 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] | 3979 | // If the header has a unique predecessor outside the loop, it must be |
| 3980 | // a block that has exactly one successor that can reach the loop. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3981 | if (Loop *L = LI->getLoopFor(BB)) |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 3982 | return getLoopPredecessor(L); |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 3983 | |
| 3984 | return 0; |
| 3985 | } |
| 3986 | |
Dan Gohman | 763bad1 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 3987 | /// HasSameValue - SCEV structural equivalence is usually sufficient for |
| 3988 | /// testing whether two expressions are equal, however for the purposes of |
| 3989 | /// looking for a condition guarding a loop, it can be useful to be a little |
| 3990 | /// more general, since a front-end may have replicated the controlling |
| 3991 | /// expression. |
| 3992 | /// |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 3993 | static bool HasSameValue(const SCEV* A, const SCEV* B) { |
Dan Gohman | 763bad1 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 3994 | // Quick check to see if they are the same SCEV. |
| 3995 | if (A == B) return true; |
| 3996 | |
| 3997 | // Otherwise, if they're both SCEVUnknown, it's possible that they hold |
| 3998 | // two different instructions with the same value. Check for this case. |
| 3999 | if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A)) |
| 4000 | if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B)) |
| 4001 | if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue())) |
| 4002 | if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue())) |
| 4003 | if (AI->isIdenticalTo(BI)) |
| 4004 | return true; |
| 4005 | |
| 4006 | // Otherwise assume they may have a different value. |
| 4007 | return false; |
| 4008 | } |
| 4009 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 4010 | /// isLoopGuardedByCond - Test whether entry to the loop is protected by |
Dan Gohman | 3d739fe | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 4011 | /// a conditional between LHS and RHS. This is used to help avoid max |
| 4012 | /// expressions in loop trip counts. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4013 | bool ScalarEvolution::isLoopGuardedByCond(const Loop *L, |
Dan Gohman | 3d739fe | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 4014 | ICmpInst::Predicate Pred, |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4015 | const SCEV *LHS, const SCEV *RHS) { |
Dan Gohman | 8ea9452 | 2009-05-18 16:03:58 +0000 | [diff] [blame] | 4016 | // Interpret a null as meaning no loop, where there is obviously no guard |
| 4017 | // (interprocedural conditions notwithstanding). |
| 4018 | if (!L) return false; |
| 4019 | |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4020 | BasicBlock *Predecessor = getLoopPredecessor(L); |
| 4021 | BasicBlock *PredecessorDest = L->getHeader(); |
Nick Lewycky | 59cff12 | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4022 | |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4023 | // Starting at the loop predecessor, climb up the predecessor chain, as long |
| 4024 | // as there are predecessors that can be found that have unique successors |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4025 | // leading to the original header. |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4026 | for (; Predecessor; |
| 4027 | PredecessorDest = Predecessor, |
| 4028 | Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) { |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4029 | |
| 4030 | BranchInst *LoopEntryPredicate = |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4031 | dyn_cast<BranchInst>(Predecessor->getTerminator()); |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4032 | if (!LoopEntryPredicate || |
| 4033 | LoopEntryPredicate->isUnconditional()) |
| 4034 | continue; |
| 4035 | |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4036 | if (isNecessaryCond(LoopEntryPredicate->getCondition(), Pred, LHS, RHS, |
| 4037 | LoopEntryPredicate->getSuccessor(0) != PredecessorDest)) |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4038 | return true; |
Nick Lewycky | 59cff12 | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4041 | return false; |
Nick Lewycky | 59cff12 | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4042 | } |
| 4043 | |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4044 | /// isNecessaryCond - Test whether the given CondValue value is a condition |
| 4045 | /// which is at least as strict as the one described by Pred, LHS, and RHS. |
| 4046 | bool ScalarEvolution::isNecessaryCond(Value *CondValue, |
| 4047 | ICmpInst::Predicate Pred, |
| 4048 | const SCEV *LHS, const SCEV *RHS, |
| 4049 | bool Inverse) { |
| 4050 | // Recursivly handle And and Or conditions. |
| 4051 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CondValue)) { |
| 4052 | if (BO->getOpcode() == Instruction::And) { |
| 4053 | if (!Inverse) |
| 4054 | return isNecessaryCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) || |
| 4055 | isNecessaryCond(BO->getOperand(1), Pred, LHS, RHS, Inverse); |
| 4056 | } else if (BO->getOpcode() == Instruction::Or) { |
| 4057 | if (Inverse) |
| 4058 | return isNecessaryCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) || |
| 4059 | isNecessaryCond(BO->getOperand(1), Pred, LHS, RHS, Inverse); |
| 4060 | } |
| 4061 | } |
| 4062 | |
| 4063 | ICmpInst *ICI = dyn_cast<ICmpInst>(CondValue); |
| 4064 | if (!ICI) return false; |
| 4065 | |
| 4066 | // Now that we found a conditional branch that dominates the loop, check to |
| 4067 | // see if it is the comparison we are looking for. |
| 4068 | Value *PreCondLHS = ICI->getOperand(0); |
| 4069 | Value *PreCondRHS = ICI->getOperand(1); |
| 4070 | ICmpInst::Predicate Cond; |
| 4071 | if (Inverse) |
| 4072 | Cond = ICI->getInversePredicate(); |
| 4073 | else |
| 4074 | Cond = ICI->getPredicate(); |
| 4075 | |
| 4076 | if (Cond == Pred) |
| 4077 | ; // An exact match. |
| 4078 | else if (!ICmpInst::isTrueWhenEqual(Cond) && Pred == ICmpInst::ICMP_NE) |
| 4079 | ; // The actual condition is beyond sufficient. |
| 4080 | else |
| 4081 | // Check a few special cases. |
| 4082 | switch (Cond) { |
| 4083 | case ICmpInst::ICMP_UGT: |
| 4084 | if (Pred == ICmpInst::ICMP_ULT) { |
| 4085 | std::swap(PreCondLHS, PreCondRHS); |
| 4086 | Cond = ICmpInst::ICMP_ULT; |
| 4087 | break; |
| 4088 | } |
| 4089 | return false; |
| 4090 | case ICmpInst::ICMP_SGT: |
| 4091 | if (Pred == ICmpInst::ICMP_SLT) { |
| 4092 | std::swap(PreCondLHS, PreCondRHS); |
| 4093 | Cond = ICmpInst::ICMP_SLT; |
| 4094 | break; |
| 4095 | } |
| 4096 | return false; |
| 4097 | case ICmpInst::ICMP_NE: |
| 4098 | // Expressions like (x >u 0) are often canonicalized to (x != 0), |
| 4099 | // so check for this case by checking if the NE is comparing against |
| 4100 | // a minimum or maximum constant. |
| 4101 | if (!ICmpInst::isTrueWhenEqual(Pred)) |
| 4102 | if (ConstantInt *CI = dyn_cast<ConstantInt>(PreCondRHS)) { |
| 4103 | const APInt &A = CI->getValue(); |
| 4104 | switch (Pred) { |
| 4105 | case ICmpInst::ICMP_SLT: |
| 4106 | if (A.isMaxSignedValue()) break; |
| 4107 | return false; |
| 4108 | case ICmpInst::ICMP_SGT: |
| 4109 | if (A.isMinSignedValue()) break; |
| 4110 | return false; |
| 4111 | case ICmpInst::ICMP_ULT: |
| 4112 | if (A.isMaxValue()) break; |
| 4113 | return false; |
| 4114 | case ICmpInst::ICMP_UGT: |
| 4115 | if (A.isMinValue()) break; |
| 4116 | return false; |
| 4117 | default: |
| 4118 | return false; |
| 4119 | } |
| 4120 | Cond = ICmpInst::ICMP_NE; |
| 4121 | // NE is symmetric but the original comparison may not be. Swap |
| 4122 | // the operands if necessary so that they match below. |
| 4123 | if (isa<SCEVConstant>(LHS)) |
| 4124 | std::swap(PreCondLHS, PreCondRHS); |
| 4125 | break; |
| 4126 | } |
| 4127 | return false; |
| 4128 | default: |
| 4129 | // We weren't able to reconcile the condition. |
| 4130 | return false; |
| 4131 | } |
| 4132 | |
| 4133 | if (!PreCondLHS->getType()->isInteger()) return false; |
| 4134 | |
| 4135 | const SCEV *PreCondLHSSCEV = getSCEV(PreCondLHS); |
| 4136 | const SCEV *PreCondRHSSCEV = getSCEV(PreCondRHS); |
| 4137 | return (HasSameValue(LHS, PreCondLHSSCEV) && |
| 4138 | HasSameValue(RHS, PreCondRHSSCEV)) || |
| 4139 | (HasSameValue(LHS, getNotSCEV(PreCondRHSSCEV)) && |
| 4140 | HasSameValue(RHS, getNotSCEV(PreCondLHSSCEV))); |
| 4141 | } |
| 4142 | |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4143 | /// getBECount - Subtract the end and start values and divide by the step, |
| 4144 | /// rounding up, to get the number of times the backedge is executed. Return |
| 4145 | /// CouldNotCompute if an intermediate computation overflows. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4146 | const SCEV* ScalarEvolution::getBECount(const SCEV* Start, |
| 4147 | const SCEV* End, |
| 4148 | const SCEV* Step) { |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4149 | const Type *Ty = Start->getType(); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4150 | const SCEV* NegOne = getIntegerSCEV(-1, Ty); |
| 4151 | const SCEV* Diff = getMinusSCEV(End, Start); |
| 4152 | const SCEV* RoundUp = getAddExpr(Step, NegOne); |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4153 | |
| 4154 | // Add an adjustment to the difference between End and Start so that |
| 4155 | // the division will effectively round up. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4156 | const SCEV* Add = getAddExpr(Diff, RoundUp); |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4157 | |
| 4158 | // Check Add for unsigned overflow. |
| 4159 | // TODO: More sophisticated things could be done here. |
| 4160 | const Type *WideTy = IntegerType::get(getTypeSizeInBits(Ty) + 1); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4161 | const SCEV* OperandExtendedAdd = |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4162 | getAddExpr(getZeroExtendExpr(Diff, WideTy), |
| 4163 | getZeroExtendExpr(RoundUp, WideTy)); |
| 4164 | if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4165 | return getCouldNotCompute(); |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4166 | |
| 4167 | return getUDivExpr(Add, Step); |
| 4168 | } |
| 4169 | |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4170 | /// HowManyLessThans - Return the number of times a backedge containing the |
| 4171 | /// specified less-than comparison will execute. If not computable, return |
Dan Gohman | 86fbf2f | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4172 | /// CouldNotCompute. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4173 | ScalarEvolution::BackedgeTakenInfo |
| 4174 | ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS, |
| 4175 | const Loop *L, bool isSigned) { |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4176 | // Only handle: "ADDREC < LoopInvariant". |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4177 | if (!RHS->isLoopInvariant(L)) return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4178 | |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4179 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4180 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4181 | return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4182 | |
| 4183 | if (AddRec->isAffine()) { |
Nick Lewycky | 789558d | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 4184 | // FORNOW: We only support unit strides. |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4185 | unsigned BitWidth = getTypeSizeInBits(AddRec->getType()); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4186 | const SCEV* Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4187 | |
| 4188 | // TODO: handle non-constant strides. |
| 4189 | const SCEVConstant *CStep = dyn_cast<SCEVConstant>(Step); |
| 4190 | if (!CStep || CStep->isZero()) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4191 | return getCouldNotCompute(); |
Dan Gohman | 70a1fe7 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 4192 | if (CStep->isOne()) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4193 | // With unit stride, the iteration never steps past the limit value. |
| 4194 | } else if (CStep->getValue()->getValue().isStrictlyPositive()) { |
| 4195 | if (const SCEVConstant *CLimit = dyn_cast<SCEVConstant>(RHS)) { |
| 4196 | // Test whether a positive iteration iteration can step past the limit |
| 4197 | // value and past the maximum value for its type in a single step. |
| 4198 | if (isSigned) { |
| 4199 | APInt Max = APInt::getSignedMaxValue(BitWidth); |
| 4200 | if ((Max - CStep->getValue()->getValue()) |
| 4201 | .slt(CLimit->getValue()->getValue())) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4202 | return getCouldNotCompute(); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4203 | } else { |
| 4204 | APInt Max = APInt::getMaxValue(BitWidth); |
| 4205 | if ((Max - CStep->getValue()->getValue()) |
| 4206 | .ult(CLimit->getValue()->getValue())) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4207 | return getCouldNotCompute(); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4208 | } |
| 4209 | } else |
| 4210 | // TODO: handle non-constant limit values below. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4211 | return getCouldNotCompute(); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4212 | } else |
| 4213 | // TODO: handle negative strides below. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4214 | return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4215 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4216 | // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant |
| 4217 | // m. So, we count the number of iterations in which {n,+,s} < m is true. |
| 4218 | // 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] | 4219 | // treat m-n as signed nor unsigned due to overflow possibility. |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4220 | |
Wojciech Matyjewicz | 3a4cbe2 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4221 | // First, we get the value of the LHS in the first iteration: n |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4222 | const SCEV* Start = AddRec->getOperand(0); |
Wojciech Matyjewicz | 3a4cbe2 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4223 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4224 | // Determine the minimum constant start value. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4225 | const SCEV *MinStart = isa<SCEVConstant>(Start) ? Start : |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4226 | getConstant(isSigned ? APInt::getSignedMinValue(BitWidth) : |
| 4227 | APInt::getMinValue(BitWidth)); |
Wojciech Matyjewicz | 3a4cbe2 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4228 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4229 | // If we know that the condition is true in order to enter the loop, |
| 4230 | // 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] | 4231 | // only know that it will execute (max(m,n)-n)/s times. In both cases, |
| 4232 | // the division must round up. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4233 | const SCEV* End = RHS; |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4234 | if (!isLoopGuardedByCond(L, |
| 4235 | isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
| 4236 | getMinusSCEV(Start, Step), RHS)) |
| 4237 | End = isSigned ? getSMaxExpr(RHS, Start) |
| 4238 | : getUMaxExpr(RHS, Start); |
| 4239 | |
| 4240 | // Determine the maximum constant end value. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4241 | const SCEV* MaxEnd = |
Dan Gohman | 3964acc | 2009-06-20 00:32:22 +0000 | [diff] [blame] | 4242 | isa<SCEVConstant>(End) ? End : |
| 4243 | getConstant(isSigned ? APInt::getSignedMaxValue(BitWidth) |
| 4244 | .ashr(GetMinSignBits(End) - 1) : |
| 4245 | APInt::getMaxValue(BitWidth) |
| 4246 | .lshr(GetMinLeadingZeros(End))); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4247 | |
| 4248 | // Finally, we subtract these two values and divide, rounding up, to get |
| 4249 | // the number of times the backedge is executed. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4250 | const SCEV* BECount = getBECount(Start, End, Step); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4251 | |
| 4252 | // The maximum backedge count is similar, except using the minimum start |
| 4253 | // value and the maximum end value. |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 4254 | const SCEV* MaxBECount = getBECount(MinStart, MaxEnd, Step); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4255 | |
| 4256 | return BackedgeTakenInfo(BECount, MaxBECount); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4257 | } |
| 4258 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4259 | return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4260 | } |
| 4261 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4262 | /// getNumIterationsInRange - Return the number of iterations of this loop that |
| 4263 | /// produce values in the specified constant range. Another way of looking at |
| 4264 | /// this is that it returns the first iteration number where the value is not in |
| 4265 | /// the condition, thus computing the exit count. If the iteration count can't |
| 4266 | /// be computed, an instance of SCEVCouldNotCompute is returned. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4267 | const SCEV* SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4268 | ScalarEvolution &SE) const { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4269 | if (Range.isFullSet()) // Infinite loop. |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4270 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4271 | |
| 4272 | // 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] | 4273 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart())) |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 4274 | if (!SC->getValue()->isZero()) { |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4275 | SmallVector<const SCEV*, 4> Operands(op_begin(), op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4276 | Operands[0] = SE.getIntegerSCEV(0, SC->getType()); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4277 | const SCEV* Shifted = SE.getAddRecExpr(Operands, getLoop()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4278 | if (const SCEVAddRecExpr *ShiftedAddRec = |
| 4279 | dyn_cast<SCEVAddRecExpr>(Shifted)) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4280 | return ShiftedAddRec->getNumIterationsInRange( |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4281 | Range.subtract(SC->getValue()->getValue()), SE); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4282 | // This is strange and shouldn't happen. |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4283 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4284 | } |
| 4285 | |
| 4286 | // The only time we can solve this is when we have all constant indices. |
| 4287 | // Otherwise, we cannot determine the overflow conditions. |
| 4288 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 4289 | if (!isa<SCEVConstant>(getOperand(i))) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4290 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4291 | |
| 4292 | |
| 4293 | // Okay at this point we know that all elements of the chrec are constants and |
| 4294 | // that the start element is zero. |
| 4295 | |
| 4296 | // First check to see if the range contains zero. If not, the first |
| 4297 | // iteration exits. |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 4298 | unsigned BitWidth = SE.getTypeSizeInBits(getType()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4299 | if (!Range.contains(APInt(BitWidth, 0))) |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 4300 | return SE.getIntegerSCEV(0, getType()); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4301 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4302 | if (isAffine()) { |
| 4303 | // If this is an affine expression then we have this situation: |
| 4304 | // Solve {0,+,A} in Range === Ax in Range |
| 4305 | |
Nick Lewycky | eefdebe | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 4306 | // We know that zero is in the range. If A is positive then we know that |
| 4307 | // the upper value of the range must be the first possible exit value. |
| 4308 | // If A is negative then the lower of the range is the last possible loop |
| 4309 | // value. Also note that we already checked for a full range. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4310 | APInt One(BitWidth,1); |
Nick Lewycky | eefdebe | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 4311 | APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue(); |
| 4312 | APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4313 | |
Nick Lewycky | eefdebe | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 4314 | // The exit value should be (End+A)/A. |
Nick Lewycky | 9a2f931 | 2007-09-27 14:12:54 +0000 | [diff] [blame] | 4315 | APInt ExitVal = (End + A).udiv(A); |
Reid Spencer | c7cd7a0 | 2007-03-01 19:32:33 +0000 | [diff] [blame] | 4316 | ConstantInt *ExitValue = ConstantInt::get(ExitVal); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4317 | |
| 4318 | // Evaluate at the exit value. If we really did fall out of the valid |
| 4319 | // range, then we computed our trip count, otherwise wrap around or other |
| 4320 | // things must have happened. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4321 | ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 4322 | if (Range.contains(Val->getValue())) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4323 | return SE.getCouldNotCompute(); // Something strange happened |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4324 | |
| 4325 | // 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] | 4326 | assert(Range.contains( |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4327 | EvaluateConstantChrecAtConstant(this, |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4328 | ConstantInt::get(ExitVal - One), SE)->getValue()) && |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4329 | "Linear scev computation is off in a bad way!"); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4330 | return SE.getConstant(ExitValue); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4331 | } else if (isQuadratic()) { |
| 4332 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the |
| 4333 | // quadratic equation to solve it. To do this, we must frame our problem in |
| 4334 | // terms of figuring out when zero is crossed, instead of when |
| 4335 | // Range.getUpper() is crossed. |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4336 | SmallVector<const SCEV*, 4> NewOps(op_begin(), op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4337 | NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper())); |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4338 | const SCEV* NewAddRec = SE.getAddRecExpr(NewOps, getLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4339 | |
| 4340 | // Next, solve the constructed addrec |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4341 | std::pair<const SCEV*,const SCEV*> Roots = |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4342 | SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4343 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 4344 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4345 | if (R1) { |
| 4346 | // Pick the smallest positive root value. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4347 | if (ConstantInt *CB = |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4348 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4349 | R1->getValue(), R2->getValue()))) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4350 | if (CB->getZExtValue() == false) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4351 | std::swap(R1, R2); // R1 is the minimum root now. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4352 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4353 | // Make sure the root is not off by one. The returned iteration should |
| 4354 | // not be in the range, but the previous one should be. When solving |
| 4355 | // for "X*X < 5", for example, we should not return a root of 2. |
| 4356 | ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this, |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4357 | R1->getValue(), |
| 4358 | SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 4359 | if (Range.contains(R1Val->getValue())) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4360 | // The next iteration must be out of the range... |
Dan Gohman | 9a6ae96 | 2007-07-09 15:25:17 +0000 | [diff] [blame] | 4361 | ConstantInt *NextVal = ConstantInt::get(R1->getValue()->getValue()+1); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4362 | |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4363 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 4364 | if (!Range.contains(R1Val->getValue())) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4365 | return SE.getConstant(NextVal); |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4366 | return SE.getCouldNotCompute(); // Something strange happened |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4367 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4368 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4369 | // If R1 was not in the range, then it is a good return value. Make |
| 4370 | // sure that R1-1 WAS in the range though, just in case. |
Dan Gohman | 9a6ae96 | 2007-07-09 15:25:17 +0000 | [diff] [blame] | 4371 | ConstantInt *NextVal = ConstantInt::get(R1->getValue()->getValue()-1); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4372 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 4373 | if (Range.contains(R1Val->getValue())) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4374 | return R1; |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4375 | return SE.getCouldNotCompute(); // Something strange happened |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4376 | } |
| 4377 | } |
| 4378 | } |
| 4379 | |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4380 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | |
| 4384 | |
| 4385 | //===----------------------------------------------------------------------===// |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4386 | // SCEVCallbackVH Class Implementation |
| 4387 | //===----------------------------------------------------------------------===// |
| 4388 | |
Dan Gohman | 1959b75 | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 4389 | void ScalarEvolution::SCEVCallbackVH::deleted() { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4390 | assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!"); |
| 4391 | if (PHINode *PN = dyn_cast<PHINode>(getValPtr())) |
| 4392 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | 6bce643 | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 4393 | if (Instruction *I = dyn_cast<Instruction>(getValPtr())) |
| 4394 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4395 | SE->Scalars.erase(getValPtr()); |
| 4396 | // this now dangles! |
| 4397 | } |
| 4398 | |
Dan Gohman | 1959b75 | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 4399 | void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4400 | assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!"); |
| 4401 | |
| 4402 | // Forget all the expressions associated with users of the old value, |
| 4403 | // so that future queries will recompute the expressions using the new |
| 4404 | // value. |
| 4405 | SmallVector<User *, 16> Worklist; |
| 4406 | Value *Old = getValPtr(); |
| 4407 | bool DeleteOld = false; |
| 4408 | for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end(); |
| 4409 | UI != UE; ++UI) |
| 4410 | Worklist.push_back(*UI); |
| 4411 | while (!Worklist.empty()) { |
| 4412 | User *U = Worklist.pop_back_val(); |
| 4413 | // Deleting the Old value will cause this to dangle. Postpone |
| 4414 | // that until everything else is done. |
| 4415 | if (U == Old) { |
| 4416 | DeleteOld = true; |
| 4417 | continue; |
| 4418 | } |
| 4419 | if (PHINode *PN = dyn_cast<PHINode>(U)) |
| 4420 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | 6bce643 | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 4421 | if (Instruction *I = dyn_cast<Instruction>(U)) |
| 4422 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4423 | if (SE->Scalars.erase(U)) |
| 4424 | for (Value::use_iterator UI = U->use_begin(), UE = U->use_end(); |
| 4425 | UI != UE; ++UI) |
| 4426 | Worklist.push_back(*UI); |
| 4427 | } |
| 4428 | if (DeleteOld) { |
| 4429 | if (PHINode *PN = dyn_cast<PHINode>(Old)) |
| 4430 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | 6bce643 | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 4431 | if (Instruction *I = dyn_cast<Instruction>(Old)) |
| 4432 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4433 | SE->Scalars.erase(Old); |
| 4434 | // this now dangles! |
| 4435 | } |
| 4436 | // this may dangle! |
| 4437 | } |
| 4438 | |
Dan Gohman | 1959b75 | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 4439 | ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4440 | : CallbackVH(V), SE(se) {} |
| 4441 | |
| 4442 | //===----------------------------------------------------------------------===// |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4443 | // ScalarEvolution Class Implementation |
| 4444 | //===----------------------------------------------------------------------===// |
| 4445 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4446 | ScalarEvolution::ScalarEvolution() |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4447 | : FunctionPass(&ID) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4450 | bool ScalarEvolution::runOnFunction(Function &F) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4451 | this->F = &F; |
| 4452 | LI = &getAnalysis<LoopInfo>(); |
| 4453 | TD = getAnalysisIfAvailable<TargetData>(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4454 | return false; |
| 4455 | } |
| 4456 | |
| 4457 | void ScalarEvolution::releaseMemory() { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4458 | Scalars.clear(); |
| 4459 | BackedgeTakenCounts.clear(); |
| 4460 | ConstantEvolutionLoopExitValue.clear(); |
Dan Gohman | 6bce643 | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 4461 | ValuesAtScopes.clear(); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4462 | UniqueSCEVs.clear(); |
| 4463 | SCEVAllocator.Reset(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const { |
| 4467 | AU.setPreservesAll(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4468 | AU.addRequiredTransitive<LoopInfo>(); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4469 | } |
| 4470 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4471 | bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4472 | return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4473 | } |
| 4474 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4475 | static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4476 | const Loop *L) { |
| 4477 | // Print all inner loops first |
| 4478 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 4479 | PrintLoopInfo(OS, SE, *I); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4480 | |
Nick Lewycky | aeb5e5c | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 4481 | OS << "Loop " << L->getHeader()->getName() << ": "; |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 4482 | |
Devang Patel | b7211a2 | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 4483 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 4484 | L->getExitBlocks(ExitBlocks); |
| 4485 | if (ExitBlocks.size() != 1) |
Nick Lewycky | aeb5e5c | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 4486 | OS << "<multiple exits> "; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4487 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4488 | if (SE->hasLoopInvariantBackedgeTakenCount(L)) { |
| 4489 | OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4490 | } else { |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 4491 | OS << "Unpredictable backedge-taken count. "; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
Nick Lewycky | aeb5e5c | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 4494 | OS << "\n"; |
Dan Gohman | aa551ae | 2009-06-24 00:33:16 +0000 | [diff] [blame] | 4495 | OS << "Loop " << L->getHeader()->getName() << ": "; |
| 4496 | |
| 4497 | if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) { |
| 4498 | OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L); |
| 4499 | } else { |
| 4500 | OS << "Unpredictable max backedge-taken count. "; |
| 4501 | } |
| 4502 | |
| 4503 | OS << "\n"; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4504 | } |
| 4505 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 4506 | void ScalarEvolution::print(raw_ostream &OS, const Module* ) const { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4507 | // ScalarEvolution's implementaiton of the print method is to print |
| 4508 | // out SCEV values of all instructions that are interesting. Doing |
| 4509 | // this potentially causes it to create new SCEV objects though, |
| 4510 | // which technically conflicts with the const qualifier. This isn't |
| 4511 | // observable from outside the class though (the hasSCEV function |
| 4512 | // notwithstanding), so casting away the const isn't dangerous. |
| 4513 | ScalarEvolution &SE = *const_cast<ScalarEvolution*>(this); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4514 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4515 | OS << "Classifying expressions for: " << F->getName() << "\n"; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4516 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
Dan Gohman | d9c1c85 | 2009-04-30 01:30:18 +0000 | [diff] [blame] | 4517 | if (isSCEVable(I->getType())) { |
Chris Lattner | 6ffe551 | 2004-04-27 15:13:33 +0000 | [diff] [blame] | 4518 | OS << *I; |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 4519 | OS << " --> "; |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4520 | const SCEV* SV = SE.getSCEV(&*I); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4521 | SV->print(OS); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4522 | |
Dan Gohman | 0c689c5 | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 4523 | const Loop *L = LI->getLoopFor((*I).getParent()); |
| 4524 | |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4525 | const SCEV* AtUse = SE.getSCEVAtScope(SV, L); |
Dan Gohman | 0c689c5 | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 4526 | if (AtUse != SV) { |
| 4527 | OS << " --> "; |
| 4528 | AtUse->print(OS); |
| 4529 | } |
| 4530 | |
| 4531 | if (L) { |
Dan Gohman | 9e7d988 | 2009-06-18 00:37:45 +0000 | [diff] [blame] | 4532 | OS << "\t\t" "Exits: "; |
Owen Anderson | 372b46c | 2009-06-22 21:39:50 +0000 | [diff] [blame] | 4533 | const SCEV* ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop()); |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 4534 | if (!ExitValue->isLoopInvariant(L)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4535 | OS << "<<Unknown>>"; |
| 4536 | } else { |
| 4537 | OS << *ExitValue; |
| 4538 | } |
| 4539 | } |
| 4540 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4541 | OS << "\n"; |
| 4542 | } |
| 4543 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4544 | OS << "Determining loop execution counts for: " << F->getName() << "\n"; |
| 4545 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
| 4546 | PrintLoopInfo(OS, &SE, *I); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4547 | } |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 4548 | |
| 4549 | void ScalarEvolution::print(std::ostream &o, const Module *M) const { |
| 4550 | raw_os_ostream OS(o); |
| 4551 | print(OS, M); |
| 4552 | } |