Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- ScalarEvolution.cpp - Scalar Evolution Analysis ----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the implementation of the scalar evolution analysis |
| 11 | // engine, which is used primarily to analyze expressions involving induction |
| 12 | // variables in loops. |
| 13 | // |
| 14 | // There are several aspects to this library. First is the representation of |
| 15 | // scalar expressions, which are represented as subclasses of the SCEV class. |
| 16 | // These classes are used to represent certain types of subexpressions that we |
Dan Gohman | b4b55af | 2009-07-25 16:18:07 +0000 | [diff] [blame] | 17 | // can handle. We only create one SCEV of a particular shape, so |
| 18 | // pointer-comparisons for equality are legal. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 19 | // |
| 20 | // One important aspect of the SCEV objects is that they are never cyclic, even |
| 21 | // if there is a cycle in the dataflow for an expression (ie, a PHI node). If |
| 22 | // the PHI node is one of the idioms that we can represent (e.g., a polynomial |
| 23 | // recurrence) then we represent it directly as a recurrence node, otherwise we |
| 24 | // represent it as a SCEVUnknown node. |
| 25 | // |
| 26 | // In addition to being able to represent expressions of various types, we also |
| 27 | // have folders that are used to build the *canonical* representation for a |
| 28 | // particular expression. These folders are capable of using a variety of |
| 29 | // rewrite rules to simplify the expressions. |
| 30 | // |
| 31 | // Once the folders are defined, we can implement the more interesting |
| 32 | // higher-level code, such as the code that recognizes PHI nodes of various |
| 33 | // types, computes the execution count of a loop, etc. |
| 34 | // |
| 35 | // TODO: We should use these routines and value representations to implement |
| 36 | // dependence analysis! |
| 37 | // |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | // |
| 40 | // There are several good references for the techniques used in this analysis. |
| 41 | // |
| 42 | // Chains of recurrences -- a method to expedite the evaluation |
| 43 | // of closed-form functions |
| 44 | // Olaf Bachmann, Paul S. Wang, Eugene V. Zima |
| 45 | // |
| 46 | // On computational properties of chains of recurrences |
| 47 | // Eugene V. Zima |
| 48 | // |
| 49 | // Symbolic Evaluation of Chains of Recurrences for Loop Optimization |
| 50 | // Robert A. van Engelen |
| 51 | // |
| 52 | // Efficient Symbolic Analysis for Optimizing Compilers |
| 53 | // Robert A. van Engelen |
| 54 | // |
| 55 | // Using the chains of recurrences algebra for data dependence testing and |
| 56 | // induction variable substitution |
| 57 | // MS Thesis, Johnie Birch |
| 58 | // |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
| 61 | #define DEBUG_TYPE "scalar-evolution" |
| 62 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 63 | #include "llvm/Constants.h" |
| 64 | #include "llvm/DerivedTypes.h" |
| 65 | #include "llvm/GlobalVariable.h" |
| 66 | #include "llvm/Instructions.h" |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 67 | #include "llvm/LLVMContext.h" |
Dan Gohman | 9545fb0 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 68 | #include "llvm/Operator.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | #include "llvm/Analysis/ConstantFolding.h" |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 70 | #include "llvm/Analysis/Dominators.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 71 | #include "llvm/Analysis/LoopInfo.h" |
Dan Gohman | a7726c3 | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 72 | #include "llvm/Analysis/ValueTracking.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 73 | #include "llvm/Assembly/Writer.h" |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 74 | #include "llvm/Target/TargetData.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 75 | #include "llvm/Support/CommandLine.h" |
| 76 | #include "llvm/Support/Compiler.h" |
| 77 | #include "llvm/Support/ConstantRange.h" |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 78 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 79 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 80 | #include "llvm/Support/InstIterator.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 81 | #include "llvm/Support/MathExtras.h" |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 82 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 83 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 84 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 85 | #include "llvm/ADT/SmallPtrSet.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 86 | #include <algorithm> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 87 | using namespace llvm; |
| 88 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 89 | STATISTIC(NumArrayLenItCounts, |
| 90 | "Number of trip counts computed with array length"); |
| 91 | STATISTIC(NumTripCountsComputed, |
| 92 | "Number of loops with predictable loop counts"); |
| 93 | STATISTIC(NumTripCountsNotComputed, |
| 94 | "Number of loops without predictable loop counts"); |
| 95 | STATISTIC(NumBruteForceTripCountsComputed, |
| 96 | "Number of loops with trip counts computed by force"); |
| 97 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 98 | static cl::opt<unsigned> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 99 | MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, |
| 100 | cl::desc("Maximum number of iterations SCEV will " |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 101 | "symbolically execute a constant " |
| 102 | "derived loop"), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 103 | cl::init(100)); |
| 104 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 105 | static RegisterPass<ScalarEvolution> |
| 106 | R("scalar-evolution", "Scalar Evolution Analysis", false, true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 107 | char ScalarEvolution::ID = 0; |
| 108 | |
| 109 | //===----------------------------------------------------------------------===// |
| 110 | // SCEV class definitions |
| 111 | //===----------------------------------------------------------------------===// |
| 112 | |
| 113 | //===----------------------------------------------------------------------===// |
| 114 | // Implementation of the SCEV class. |
| 115 | // |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 116 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 117 | SCEV::~SCEV() {} |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 118 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 119 | void SCEV::dump() const { |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 120 | print(errs()); |
| 121 | errs() << '\n'; |
| 122 | } |
| 123 | |
| 124 | void SCEV::print(std::ostream &o) const { |
| 125 | raw_os_ostream OS(o); |
| 126 | print(OS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 129 | bool SCEV::isZero() const { |
| 130 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 131 | return SC->getValue()->isZero(); |
| 132 | return false; |
| 133 | } |
| 134 | |
Dan Gohman | f8bc8e8 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 135 | bool SCEV::isOne() const { |
| 136 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 137 | return SC->getValue()->isOne(); |
| 138 | return false; |
| 139 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 140 | |
Dan Gohman | f05118e | 2009-06-24 00:30:26 +0000 | [diff] [blame] | 141 | bool SCEV::isAllOnesValue() const { |
| 142 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 143 | return SC->getValue()->isAllOnesValue(); |
| 144 | return false; |
| 145 | } |
| 146 | |
Owen Anderson | b70139d | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 147 | SCEVCouldNotCompute::SCEVCouldNotCompute() : |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 148 | SCEV(FoldingSetNodeID(), scCouldNotCompute) {} |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 149 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 150 | bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 151 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | |
| 155 | const Type *SCEVCouldNotCompute::getType() const { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 156 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 161 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | |
Dan Gohman | 2aa3f04 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 165 | bool SCEVCouldNotCompute::hasOperand(const SCEV *) const { |
| 166 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
| 167 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 170 | void SCEVCouldNotCompute::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 171 | OS << "***COULDNOTCOMPUTE***"; |
| 172 | } |
| 173 | |
| 174 | bool SCEVCouldNotCompute::classof(const SCEV *S) { |
| 175 | return S->getSCEVType() == scCouldNotCompute; |
| 176 | } |
| 177 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 178 | const SCEV *ScalarEvolution::getConstant(ConstantInt *V) { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 179 | FoldingSetNodeID ID; |
| 180 | ID.AddInteger(scConstant); |
| 181 | ID.AddPointer(V); |
| 182 | void *IP = 0; |
| 183 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 184 | SCEV *S = SCEVAllocator.Allocate<SCEVConstant>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 185 | new (S) SCEVConstant(ID, V); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 186 | UniqueSCEVs.InsertNode(S, IP); |
| 187 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 190 | const SCEV *ScalarEvolution::getConstant(const APInt& Val) { |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 191 | return getConstant(ConstantInt::get(getContext(), Val)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 194 | const SCEV * |
Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 195 | ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) { |
Owen Anderson | 9f5b2aa | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 196 | return getConstant( |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 197 | ConstantInt::get(cast<IntegerType>(Ty), V, isSigned)); |
Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 200 | const Type *SCEVConstant::getType() const { return V->getType(); } |
| 201 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 202 | void SCEVConstant::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 203 | WriteAsOperand(OS, V, false); |
| 204 | } |
| 205 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 206 | SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeID &ID, |
| 207 | unsigned SCEVTy, const SCEV *op, const Type *ty) |
| 208 | : SCEV(ID, SCEVTy), Op(op), Ty(ty) {} |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 209 | |
Dan Gohman | 2a38153 | 2009-04-21 01:25:57 +0000 | [diff] [blame] | 210 | bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 211 | return Op->dominates(BB, DT); |
| 212 | } |
| 213 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 214 | SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeID &ID, |
| 215 | const SCEV *op, const Type *ty) |
| 216 | : SCEVCastExpr(ID, scTruncate, op, ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 217 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 218 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 219 | "Cannot truncate non-integer value!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 222 | void SCEVTruncateExpr::print(raw_ostream &OS) const { |
Dan Gohman | c911922 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 223 | OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 226 | SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeID &ID, |
| 227 | const SCEV *op, const Type *ty) |
| 228 | : SCEVCastExpr(ID, scZeroExtend, op, ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 229 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 230 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 231 | "Cannot zero extend non-integer value!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 234 | void SCEVZeroExtendExpr::print(raw_ostream &OS) const { |
Dan Gohman | c911922 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 235 | OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 238 | SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeID &ID, |
| 239 | const SCEV *op, const Type *ty) |
| 240 | : SCEVCastExpr(ID, scSignExtend, op, ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 241 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 242 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 243 | "Cannot sign extend non-integer value!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 246 | void SCEVSignExtendExpr::print(raw_ostream &OS) const { |
Dan Gohman | c911922 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 247 | OS << "(sext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 250 | void SCEVCommutativeExpr::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 251 | assert(Operands.size() > 1 && "This plus expr shouldn't exist!"); |
| 252 | const char *OpStr = getOperationStr(); |
| 253 | OS << "(" << *Operands[0]; |
| 254 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 255 | OS << OpStr << *Operands[i]; |
| 256 | OS << ")"; |
| 257 | } |
| 258 | |
Dan Gohman | 72a8a02 | 2009-05-07 14:00:19 +0000 | [diff] [blame] | 259 | bool SCEVNAryExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 260 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 261 | if (!getOperand(i)->dominates(BB, DT)) |
| 262 | return false; |
| 263 | } |
| 264 | return true; |
| 265 | } |
| 266 | |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 267 | bool SCEVUDivExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 268 | return LHS->dominates(BB, DT) && RHS->dominates(BB, DT); |
| 269 | } |
| 270 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 271 | void SCEVUDivExpr::print(raw_ostream &OS) const { |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 272 | OS << "(" << *LHS << " /u " << *RHS << ")"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 275 | const Type *SCEVUDivExpr::getType() const { |
Dan Gohman | 140f08f | 2009-05-26 17:44:05 +0000 | [diff] [blame] | 276 | // In most cases the types of LHS and RHS will be the same, but in some |
| 277 | // crazy cases one or the other may be a pointer. ScalarEvolution doesn't |
| 278 | // depend on the type for correctness, but handling types carefully can |
| 279 | // avoid extra casts in the SCEVExpander. The LHS is more likely to be |
| 280 | // a pointer type than the RHS, so use the RHS' type here. |
| 281 | return RHS->getType(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 284 | bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const { |
Dan Gohman | ae1eaae | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 285 | // Add recurrences are never invariant in the function-body (null loop). |
Dan Gohman | 2d888d8 | 2009-06-26 22:17:21 +0000 | [diff] [blame] | 286 | if (!QueryLoop) |
| 287 | return false; |
| 288 | |
| 289 | // This recurrence is variant w.r.t. QueryLoop if QueryLoop contains L. |
| 290 | if (QueryLoop->contains(L->getHeader())) |
| 291 | return false; |
| 292 | |
| 293 | // This recurrence is variant w.r.t. QueryLoop if any of its operands |
| 294 | // are variant. |
| 295 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 296 | if (!getOperand(i)->isLoopInvariant(QueryLoop)) |
| 297 | return false; |
| 298 | |
| 299 | // Otherwise it's loop-invariant. |
| 300 | return true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 303 | void SCEVAddRecExpr::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 304 | OS << "{" << *Operands[0]; |
| 305 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 306 | OS << ",+," << *Operands[i]; |
| 307 | OS << "}<" << L->getHeader()->getName() + ">"; |
| 308 | } |
| 309 | |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 310 | void SCEVFieldOffsetExpr::print(raw_ostream &OS) const { |
| 311 | // LLVM struct fields don't have names, so just print the field number. |
| 312 | OS << "offsetof(" << *STy << ", " << FieldNo << ")"; |
| 313 | } |
| 314 | |
| 315 | void SCEVAllocSizeExpr::print(raw_ostream &OS) const { |
| 316 | OS << "sizeof(" << *AllocTy << ")"; |
| 317 | } |
| 318 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 319 | bool SCEVUnknown::isLoopInvariant(const Loop *L) const { |
| 320 | // All non-instruction values are loop invariant. All instructions are loop |
| 321 | // invariant if they are not contained in the specified loop. |
Dan Gohman | ae1eaae | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 322 | // Instructions are never considered invariant in the function body |
| 323 | // (null loop) because they are defined within the "loop". |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 324 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Dan Gohman | ae1eaae | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 325 | return L && !L->contains(I->getParent()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 326 | return true; |
| 327 | } |
| 328 | |
Evan Cheng | 98c073b | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 329 | bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 330 | if (Instruction *I = dyn_cast<Instruction>(getValue())) |
| 331 | return DT->dominates(I->getParent(), BB); |
| 332 | return true; |
| 333 | } |
| 334 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 335 | const Type *SCEVUnknown::getType() const { |
| 336 | return V->getType(); |
| 337 | } |
| 338 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 339 | void SCEVUnknown::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 340 | WriteAsOperand(OS, V, false); |
| 341 | } |
| 342 | |
| 343 | //===----------------------------------------------------------------------===// |
| 344 | // SCEV Utilities |
| 345 | //===----------------------------------------------------------------------===// |
| 346 | |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 347 | static bool CompareTypes(const Type *A, const Type *B) { |
| 348 | if (A->getTypeID() != B->getTypeID()) |
| 349 | return A->getTypeID() < B->getTypeID(); |
| 350 | if (const IntegerType *AI = dyn_cast<IntegerType>(A)) { |
| 351 | const IntegerType *BI = cast<IntegerType>(B); |
| 352 | return AI->getBitWidth() < BI->getBitWidth(); |
| 353 | } |
| 354 | if (const PointerType *AI = dyn_cast<PointerType>(A)) { |
| 355 | const PointerType *BI = cast<PointerType>(B); |
| 356 | return CompareTypes(AI->getElementType(), BI->getElementType()); |
| 357 | } |
| 358 | if (const ArrayType *AI = dyn_cast<ArrayType>(A)) { |
| 359 | const ArrayType *BI = cast<ArrayType>(B); |
| 360 | if (AI->getNumElements() != BI->getNumElements()) |
| 361 | return AI->getNumElements() < BI->getNumElements(); |
| 362 | return CompareTypes(AI->getElementType(), BI->getElementType()); |
| 363 | } |
| 364 | if (const VectorType *AI = dyn_cast<VectorType>(A)) { |
| 365 | const VectorType *BI = cast<VectorType>(B); |
| 366 | if (AI->getNumElements() != BI->getNumElements()) |
| 367 | return AI->getNumElements() < BI->getNumElements(); |
| 368 | return CompareTypes(AI->getElementType(), BI->getElementType()); |
| 369 | } |
| 370 | if (const StructType *AI = dyn_cast<StructType>(A)) { |
| 371 | const StructType *BI = cast<StructType>(B); |
| 372 | if (AI->getNumElements() != BI->getNumElements()) |
| 373 | return AI->getNumElements() < BI->getNumElements(); |
| 374 | for (unsigned i = 0, e = AI->getNumElements(); i != e; ++i) |
| 375 | if (CompareTypes(AI->getElementType(i), BI->getElementType(i)) || |
| 376 | CompareTypes(BI->getElementType(i), AI->getElementType(i))) |
| 377 | return CompareTypes(AI->getElementType(i), BI->getElementType(i)); |
| 378 | } |
| 379 | return false; |
| 380 | } |
| 381 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 382 | namespace { |
| 383 | /// SCEVComplexityCompare - Return true if the complexity of the LHS is less |
| 384 | /// than the complexity of the RHS. This comparator is used to canonicalize |
| 385 | /// expressions. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 386 | class VISIBILITY_HIDDEN SCEVComplexityCompare { |
| 387 | LoopInfo *LI; |
| 388 | public: |
| 389 | explicit SCEVComplexityCompare(LoopInfo *li) : LI(li) {} |
| 390 | |
Dan Gohman | c0c69cf | 2008-04-14 18:23:56 +0000 | [diff] [blame] | 391 | bool operator()(const SCEV *LHS, const SCEV *RHS) const { |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 392 | // Primarily, sort the SCEVs by their getSCEVType(). |
| 393 | if (LHS->getSCEVType() != RHS->getSCEVType()) |
| 394 | return LHS->getSCEVType() < RHS->getSCEVType(); |
| 395 | |
| 396 | // Aside from the getSCEVType() ordering, the particular ordering |
| 397 | // isn't very important except that it's beneficial to be consistent, |
| 398 | // so that (a + b) and (b + a) don't end up as different expressions. |
| 399 | |
| 400 | // Sort SCEVUnknown values with some loose heuristics. TODO: This is |
| 401 | // not as complete as it could be. |
| 402 | if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) { |
| 403 | const SCEVUnknown *RU = cast<SCEVUnknown>(RHS); |
| 404 | |
Dan Gohman | d0c0123 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 405 | // Order pointer values after integer values. This helps SCEVExpander |
| 406 | // form GEPs. |
| 407 | if (isa<PointerType>(LU->getType()) && !isa<PointerType>(RU->getType())) |
| 408 | return false; |
| 409 | if (isa<PointerType>(RU->getType()) && !isa<PointerType>(LU->getType())) |
| 410 | return true; |
| 411 | |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 412 | // Compare getValueID values. |
| 413 | if (LU->getValue()->getValueID() != RU->getValue()->getValueID()) |
| 414 | return LU->getValue()->getValueID() < RU->getValue()->getValueID(); |
| 415 | |
| 416 | // Sort arguments by their position. |
| 417 | if (const Argument *LA = dyn_cast<Argument>(LU->getValue())) { |
| 418 | const Argument *RA = cast<Argument>(RU->getValue()); |
| 419 | return LA->getArgNo() < RA->getArgNo(); |
| 420 | } |
| 421 | |
| 422 | // For instructions, compare their loop depth, and their opcode. |
| 423 | // This is pretty loose. |
| 424 | if (Instruction *LV = dyn_cast<Instruction>(LU->getValue())) { |
| 425 | Instruction *RV = cast<Instruction>(RU->getValue()); |
| 426 | |
| 427 | // Compare loop depths. |
| 428 | if (LI->getLoopDepth(LV->getParent()) != |
| 429 | LI->getLoopDepth(RV->getParent())) |
| 430 | return LI->getLoopDepth(LV->getParent()) < |
| 431 | LI->getLoopDepth(RV->getParent()); |
| 432 | |
| 433 | // Compare opcodes. |
| 434 | if (LV->getOpcode() != RV->getOpcode()) |
| 435 | return LV->getOpcode() < RV->getOpcode(); |
| 436 | |
| 437 | // Compare the number of operands. |
| 438 | if (LV->getNumOperands() != RV->getNumOperands()) |
| 439 | return LV->getNumOperands() < RV->getNumOperands(); |
| 440 | } |
| 441 | |
| 442 | return false; |
| 443 | } |
| 444 | |
Dan Gohman | 56fc8f1 | 2009-06-14 22:51:25 +0000 | [diff] [blame] | 445 | // Compare constant values. |
| 446 | if (const SCEVConstant *LC = dyn_cast<SCEVConstant>(LHS)) { |
| 447 | const SCEVConstant *RC = cast<SCEVConstant>(RHS); |
Nick Lewycky | 9bb1405 | 2009-07-04 17:24:52 +0000 | [diff] [blame] | 448 | if (LC->getValue()->getBitWidth() != RC->getValue()->getBitWidth()) |
| 449 | return LC->getValue()->getBitWidth() < RC->getValue()->getBitWidth(); |
Dan Gohman | 56fc8f1 | 2009-06-14 22:51:25 +0000 | [diff] [blame] | 450 | return LC->getValue()->getValue().ult(RC->getValue()->getValue()); |
| 451 | } |
| 452 | |
| 453 | // Compare addrec loop depths. |
| 454 | if (const SCEVAddRecExpr *LA = dyn_cast<SCEVAddRecExpr>(LHS)) { |
| 455 | const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS); |
| 456 | if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth()) |
| 457 | return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth(); |
| 458 | } |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 459 | |
| 460 | // Lexicographically compare n-ary expressions. |
| 461 | if (const SCEVNAryExpr *LC = dyn_cast<SCEVNAryExpr>(LHS)) { |
| 462 | const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS); |
| 463 | for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) { |
| 464 | if (i >= RC->getNumOperands()) |
| 465 | return false; |
| 466 | if (operator()(LC->getOperand(i), RC->getOperand(i))) |
| 467 | return true; |
| 468 | if (operator()(RC->getOperand(i), LC->getOperand(i))) |
| 469 | return false; |
| 470 | } |
| 471 | return LC->getNumOperands() < RC->getNumOperands(); |
| 472 | } |
| 473 | |
Dan Gohman | 6e10db1 | 2009-05-07 19:23:21 +0000 | [diff] [blame] | 474 | // Lexicographically compare udiv expressions. |
| 475 | if (const SCEVUDivExpr *LC = dyn_cast<SCEVUDivExpr>(LHS)) { |
| 476 | const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS); |
| 477 | if (operator()(LC->getLHS(), RC->getLHS())) |
| 478 | return true; |
| 479 | if (operator()(RC->getLHS(), LC->getLHS())) |
| 480 | return false; |
| 481 | if (operator()(LC->getRHS(), RC->getRHS())) |
| 482 | return true; |
| 483 | if (operator()(RC->getRHS(), LC->getRHS())) |
| 484 | return false; |
| 485 | return false; |
| 486 | } |
| 487 | |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 488 | // Compare cast expressions by operand. |
| 489 | if (const SCEVCastExpr *LC = dyn_cast<SCEVCastExpr>(LHS)) { |
| 490 | const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS); |
| 491 | return operator()(LC->getOperand(), RC->getOperand()); |
| 492 | } |
| 493 | |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 494 | // Compare offsetof expressions. |
| 495 | if (const SCEVFieldOffsetExpr *LA = dyn_cast<SCEVFieldOffsetExpr>(LHS)) { |
| 496 | const SCEVFieldOffsetExpr *RA = cast<SCEVFieldOffsetExpr>(RHS); |
| 497 | if (CompareTypes(LA->getStructType(), RA->getStructType()) || |
| 498 | CompareTypes(RA->getStructType(), LA->getStructType())) |
| 499 | return CompareTypes(LA->getStructType(), RA->getStructType()); |
| 500 | return LA->getFieldNo() < RA->getFieldNo(); |
| 501 | } |
| 502 | |
| 503 | // Compare sizeof expressions by the allocation type. |
| 504 | if (const SCEVAllocSizeExpr *LA = dyn_cast<SCEVAllocSizeExpr>(LHS)) { |
| 505 | const SCEVAllocSizeExpr *RA = cast<SCEVAllocSizeExpr>(RHS); |
| 506 | return CompareTypes(LA->getAllocType(), RA->getAllocType()); |
| 507 | } |
| 508 | |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 509 | llvm_unreachable("Unknown SCEV kind!"); |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 510 | return false; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 511 | } |
| 512 | }; |
| 513 | } |
| 514 | |
| 515 | /// GroupByComplexity - Given a list of SCEV objects, order them by their |
| 516 | /// complexity, and group objects of the same complexity together by value. |
| 517 | /// When this routine is finished, we know that any duplicates in the vector are |
| 518 | /// consecutive and that complexity is monotonically increasing. |
| 519 | /// |
| 520 | /// Note that we go take special precautions to ensure that we get determinstic |
| 521 | /// results from this routine. In other words, we don't want the results of |
| 522 | /// this to depend on where the addresses of various SCEV objects happened to |
| 523 | /// land in memory. |
| 524 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 525 | static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops, |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 526 | LoopInfo *LI) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 527 | if (Ops.size() < 2) return; // Noop |
| 528 | if (Ops.size() == 2) { |
| 529 | // This is the common case, which also happens to be trivially simple. |
| 530 | // Special case it. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 531 | if (SCEVComplexityCompare(LI)(Ops[1], Ops[0])) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 532 | std::swap(Ops[0], Ops[1]); |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | // Do the rough sort by complexity. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 537 | std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 538 | |
| 539 | // Now that we are sorted by complexity, group elements of the same |
| 540 | // complexity. Note that this is, at worst, N^2, but the vector is likely to |
| 541 | // be extremely short in practice. Note that we take this approach because we |
| 542 | // do not want to depend on the addresses of the objects we are grouping. |
| 543 | for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 544 | const SCEV *S = Ops[i]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 545 | unsigned Complexity = S->getSCEVType(); |
| 546 | |
| 547 | // If there are any objects of the same complexity and same value as this |
| 548 | // one, group them. |
| 549 | for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { |
| 550 | if (Ops[j] == S) { // Found a duplicate. |
| 551 | // Move it to immediately after i'th element. |
| 552 | std::swap(Ops[i+1], Ops[j]); |
| 553 | ++i; // no need to rescan it. |
| 554 | if (i == e-2) return; // Done! |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | |
| 561 | |
| 562 | //===----------------------------------------------------------------------===// |
| 563 | // Simple SCEV method implementations |
| 564 | //===----------------------------------------------------------------------===// |
| 565 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 566 | /// BinomialCoefficient - Compute BC(It, K). The result has width W. |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 567 | /// Assume, K > 0. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 568 | static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K, |
Dan Gohman | f5606fd | 2009-07-21 00:38:55 +0000 | [diff] [blame] | 569 | ScalarEvolution &SE, |
| 570 | const Type* ResultTy) { |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 571 | // Handle the simplest case efficiently. |
| 572 | if (K == 1) |
| 573 | return SE.getTruncateOrZeroExtend(It, ResultTy); |
| 574 | |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 575 | // We are using the following formula for BC(It, K): |
| 576 | // |
| 577 | // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! |
| 578 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 579 | // Suppose, W is the bitwidth of the return value. We must be prepared for |
| 580 | // overflow. Hence, we must assure that the result of our computation is |
| 581 | // equal to the accurate one modulo 2^W. Unfortunately, division isn't |
| 582 | // safe in modular arithmetic. |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 583 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 584 | // However, this code doesn't use exactly that formula; the formula it uses |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 585 | // is something like the following, where T is the number of factors of 2 in |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 586 | // K! (i.e. trailing zeros in the binary representation of K!), and ^ is |
| 587 | // exponentiation: |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 588 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 589 | // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T) |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 590 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 591 | // This formula is trivially equivalent to the previous formula. However, |
| 592 | // this formula can be implemented much more efficiently. The trick is that |
| 593 | // K! / 2^T is odd, and exact division by an odd number *is* safe in modular |
| 594 | // arithmetic. To do exact division in modular arithmetic, all we have |
| 595 | // to do is multiply by the inverse. Therefore, this step can be done at |
| 596 | // width W. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 597 | // |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 598 | // The next issue is how to safely do the division by 2^T. The way this |
| 599 | // is done is by doing the multiplication step at a width of at least W + T |
| 600 | // bits. This way, the bottom W+T bits of the product are accurate. Then, |
| 601 | // when we perform the division by 2^T (which is equivalent to a right shift |
| 602 | // by T), the bottom W bits are accurate. Extra bits are okay; they'll get |
| 603 | // truncated out after the division by 2^T. |
| 604 | // |
| 605 | // In comparison to just directly using the first formula, this technique |
| 606 | // is much more efficient; using the first formula requires W * K bits, |
| 607 | // but this formula less than W + K bits. Also, the first formula requires |
| 608 | // a division step, whereas this formula only requires multiplies and shifts. |
| 609 | // |
| 610 | // It doesn't matter whether the subtraction step is done in the calculation |
| 611 | // width or the input iteration count's width; if the subtraction overflows, |
| 612 | // the result must be zero anyway. We prefer here to do it in the width of |
| 613 | // the induction variable because it helps a lot for certain cases; CodeGen |
| 614 | // isn't smart enough to ignore the overflow, which leads to much less |
| 615 | // efficient code if the width of the subtraction is wider than the native |
| 616 | // register width. |
| 617 | // |
| 618 | // (It's possible to not widen at all by pulling out factors of 2 before |
| 619 | // the multiplication; for example, K=2 can be calculated as |
| 620 | // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires |
| 621 | // extra arithmetic, so it's not an obvious win, and it gets |
| 622 | // much more complicated for K > 3.) |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 623 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 624 | // Protection from insane SCEVs; this bound is conservative, |
| 625 | // but it probably doesn't matter. |
| 626 | if (K > 1000) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 627 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 628 | |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 629 | unsigned W = SE.getTypeSizeInBits(ResultTy); |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 630 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 631 | // Calculate K! / 2^T and T; we divide out the factors of two before |
| 632 | // multiplying for calculating K! / 2^T to avoid overflow. |
| 633 | // Other overflow doesn't matter because we only care about the bottom |
| 634 | // W bits of the result. |
| 635 | APInt OddFactorial(W, 1); |
| 636 | unsigned T = 1; |
| 637 | for (unsigned i = 3; i <= K; ++i) { |
| 638 | APInt Mult(W, i); |
| 639 | unsigned TwoFactors = Mult.countTrailingZeros(); |
| 640 | T += TwoFactors; |
| 641 | Mult = Mult.lshr(TwoFactors); |
| 642 | OddFactorial *= Mult; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 643 | } |
Nick Lewycky | dbaa60a | 2008-06-13 04:38:55 +0000 | [diff] [blame] | 644 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 645 | // We need at least W + T bits for the multiplication step |
nicholas | 9e3e5fd | 2009-01-25 08:16:27 +0000 | [diff] [blame] | 646 | unsigned CalculationBits = W + T; |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 647 | |
| 648 | // Calcuate 2^T, at width T+W. |
| 649 | APInt DivFactor = APInt(CalculationBits, 1).shl(T); |
| 650 | |
| 651 | // Calculate the multiplicative inverse of K! / 2^T; |
| 652 | // this multiplication factor will perform the exact division by |
| 653 | // K! / 2^T. |
| 654 | APInt Mod = APInt::getSignedMinValue(W+1); |
| 655 | APInt MultiplyFactor = OddFactorial.zext(W+1); |
| 656 | MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); |
| 657 | MultiplyFactor = MultiplyFactor.trunc(W); |
| 658 | |
| 659 | // Calculate the product, at width T+W |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 660 | const IntegerType *CalculationTy = IntegerType::get(SE.getContext(), |
| 661 | CalculationBits); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 662 | const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 663 | for (unsigned i = 1; i != K; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 664 | const SCEV *S = SE.getMinusSCEV(It, SE.getIntegerSCEV(i, It->getType())); |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 665 | Dividend = SE.getMulExpr(Dividend, |
| 666 | SE.getTruncateOrZeroExtend(S, CalculationTy)); |
| 667 | } |
| 668 | |
| 669 | // Divide by 2^T |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 670 | const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 671 | |
| 672 | // Truncate the result, and divide by K! / 2^T. |
| 673 | |
| 674 | return SE.getMulExpr(SE.getConstant(MultiplyFactor), |
| 675 | SE.getTruncateOrZeroExtend(DivResult, ResultTy)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 678 | /// evaluateAtIteration - Return the value of this chain of recurrences at |
| 679 | /// the specified iteration number. We can evaluate this recurrence by |
| 680 | /// multiplying each element in the chain by the binomial coefficient |
| 681 | /// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as: |
| 682 | /// |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 683 | /// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 684 | /// |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 685 | /// where BC(It, k) stands for binomial coefficient. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 686 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 687 | const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It, |
Dan Gohman | f5606fd | 2009-07-21 00:38:55 +0000 | [diff] [blame] | 688 | ScalarEvolution &SE) const { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 689 | const SCEV *Result = getStart(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 690 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
Wojciech Matyjewicz | 2211fec | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 691 | // The computation is correct in the face of overflow provided that the |
| 692 | // multiplication is performed _after_ the evaluation of the binomial |
| 693 | // coefficient. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 694 | const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType()); |
Nick Lewycky | b6218e0 | 2008-10-13 03:58:02 +0000 | [diff] [blame] | 695 | if (isa<SCEVCouldNotCompute>(Coeff)) |
| 696 | return Coeff; |
| 697 | |
| 698 | Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 699 | } |
| 700 | return Result; |
| 701 | } |
| 702 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 703 | //===----------------------------------------------------------------------===// |
| 704 | // SCEV Expression folder implementations |
| 705 | //===----------------------------------------------------------------------===// |
| 706 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 707 | const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, |
Dan Gohman | 69eacc7 | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 708 | const Type *Ty) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 709 | assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 710 | "This is not a truncating conversion!"); |
Dan Gohman | 13a51e2 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 711 | assert(isSCEVable(Ty) && |
| 712 | "This is not a conversion to a SCEVable type!"); |
| 713 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 714 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 715 | FoldingSetNodeID ID; |
| 716 | ID.AddInteger(scTruncate); |
| 717 | ID.AddPointer(Op); |
| 718 | ID.AddPointer(Ty); |
| 719 | void *IP = 0; |
| 720 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 721 | |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 722 | // Fold if the operand is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 723 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 724 | return getConstant( |
| 725 | cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 726 | |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 727 | // trunc(trunc(x)) --> trunc(x) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 728 | if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 729 | return getTruncateExpr(ST->getOperand(), Ty); |
| 730 | |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 731 | // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 732 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 733 | return getTruncateOrSignExtend(SS->getOperand(), Ty); |
| 734 | |
| 735 | // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 736 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 737 | return getTruncateOrZeroExtend(SZ->getOperand(), Ty); |
| 738 | |
Dan Gohman | 1c0aa2c | 2009-06-18 16:24:47 +0000 | [diff] [blame] | 739 | // If the input value is a chrec scev, truncate the chrec's operands. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 740 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 741 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 742 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 743 | Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty)); |
| 744 | return getAddRecExpr(Operands, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 747 | // The cast wasn't folded; create an explicit cast node. |
| 748 | // Recompute the insert position, as it may have been invalidated. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 749 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 750 | SCEV *S = SCEVAllocator.Allocate<SCEVTruncateExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 751 | new (S) SCEVTruncateExpr(ID, Op, Ty); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 752 | UniqueSCEVs.InsertNode(S, IP); |
| 753 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 756 | const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op, |
Dan Gohman | 69eacc7 | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 757 | const Type *Ty) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 758 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | 36d4092 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 759 | "This is not an extending conversion!"); |
Dan Gohman | 13a51e2 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 760 | assert(isSCEVable(Ty) && |
| 761 | "This is not a conversion to a SCEVable type!"); |
| 762 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | 36d4092 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 763 | |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 764 | // Fold if the operand is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 765 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 766 | const Type *IntTy = getEffectiveSCEVType(Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 767 | Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy); |
| 768 | if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 769 | return getConstant(cast<ConstantInt>(C)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 770 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 771 | |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 772 | // zext(zext(x)) --> zext(x) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 773 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 774 | return getZeroExtendExpr(SZ->getOperand(), Ty); |
| 775 | |
Dan Gohman | db88842 | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 776 | // Before doing any expensive analysis, check to see if we've already |
| 777 | // computed a SCEV for this Op and Ty. |
| 778 | FoldingSetNodeID ID; |
| 779 | ID.AddInteger(scZeroExtend); |
| 780 | ID.AddPointer(Op); |
| 781 | ID.AddPointer(Ty); |
| 782 | void *IP = 0; |
| 783 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 784 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 785 | // If the input value is a chrec scev, and we can prove that the value |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 786 | // did not overflow the old, smaller, value, we can zero extend all of the |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 787 | // operands (often constants). This allows analysis of something like |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 788 | // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 789 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 790 | if (AR->isAffine()) { |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 791 | const SCEV *Start = AR->getStart(); |
| 792 | const SCEV *Step = AR->getStepRecurrence(*this); |
| 793 | unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
| 794 | const Loop *L = AR->getLoop(); |
| 795 | |
Dan Gohman | 1bc5316 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 796 | // If we have special knowledge that this addrec won't overflow, |
| 797 | // we don't need to do any further analysis. |
| 798 | if (AR->hasNoUnsignedOverflow()) |
| 799 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 800 | getZeroExtendExpr(Step, Ty), |
| 801 | L); |
| 802 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 803 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 804 | // Note that this serves two purposes: It filters out loops that are |
| 805 | // simply not analyzable, and it covers the case where this code is |
| 806 | // being called from within backedge-taken count analysis, such that |
| 807 | // attempting to ask for the backedge-taken count would likely result |
| 808 | // in infinite recursion. In the later case, the analysis code will |
| 809 | // cope with a conservative value, and it will take care to purge |
| 810 | // that value once it has finished. |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 811 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 812 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | 4ada77f | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 813 | // Manually compute the final value for AR, checking for |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 814 | // overflow. |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 815 | |
| 816 | // Check whether the backedge-taken count can be losslessly casted to |
| 817 | // the addrec's type. The count is always unsigned. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 818 | const SCEV *CastedMaxBECount = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 819 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 820 | const SCEV *RecastedMaxBECount = |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 821 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 822 | if (MaxBECount == RecastedMaxBECount) { |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 823 | const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 824 | // Check whether Start+Step*MaxBECount has no unsigned overflow. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 825 | const SCEV *ZMul = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 826 | getMulExpr(CastedMaxBECount, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 827 | getTruncateOrZeroExtend(Step, Start->getType())); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 828 | const SCEV *Add = getAddExpr(Start, ZMul); |
| 829 | const SCEV *OperandExtendedAdd = |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 830 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 831 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 832 | getZeroExtendExpr(Step, WideTy))); |
| 833 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 834 | // Return the expression with the addrec on the outside. |
| 835 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 836 | getZeroExtendExpr(Step, Ty), |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 837 | L); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 838 | |
| 839 | // Similar to above, only this time treat the step value as signed. |
| 840 | // This covers loops that count down. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 841 | const SCEV *SMul = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 842 | getMulExpr(CastedMaxBECount, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 843 | getTruncateOrSignExtend(Step, Start->getType())); |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 844 | Add = getAddExpr(Start, SMul); |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 845 | OperandExtendedAdd = |
| 846 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 847 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 848 | getSignExtendExpr(Step, WideTy))); |
| 849 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 850 | // Return the expression with the addrec on the outside. |
| 851 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 852 | getSignExtendExpr(Step, Ty), |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 853 | L); |
| 854 | } |
| 855 | |
| 856 | // If the backedge is guarded by a comparison with the pre-inc value |
| 857 | // the addrec is safe. Also, if the entry is guarded by a comparison |
| 858 | // with the start value and the backedge is guarded by a comparison |
| 859 | // with the post-inc value, the addrec is safe. |
| 860 | if (isKnownPositive(Step)) { |
| 861 | const SCEV *N = getConstant(APInt::getMinValue(BitWidth) - |
| 862 | getUnsignedRange(Step).getUnsignedMax()); |
| 863 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) || |
| 864 | (isLoopGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) && |
| 865 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, |
| 866 | AR->getPostIncExpr(*this), N))) |
| 867 | // Return the expression with the addrec on the outside. |
| 868 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 869 | getZeroExtendExpr(Step, Ty), |
| 870 | L); |
| 871 | } else if (isKnownNegative(Step)) { |
| 872 | const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) - |
| 873 | getSignedRange(Step).getSignedMin()); |
| 874 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) && |
| 875 | (isLoopGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) || |
| 876 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, |
| 877 | AR->getPostIncExpr(*this), N))) |
| 878 | // Return the expression with the addrec on the outside. |
| 879 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 880 | getSignExtendExpr(Step, Ty), |
| 881 | L); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 885 | |
Dan Gohman | db88842 | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 886 | // The cast wasn't folded; create an explicit cast node. |
| 887 | // Recompute the insert position, as it may have been invalidated. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 888 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 889 | SCEV *S = SCEVAllocator.Allocate<SCEVZeroExtendExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 890 | new (S) SCEVZeroExtendExpr(ID, Op, Ty); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 891 | UniqueSCEVs.InsertNode(S, IP); |
| 892 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 895 | const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op, |
Dan Gohman | 69eacc7 | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 896 | const Type *Ty) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 897 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 898 | "This is not an extending conversion!"); |
Dan Gohman | 13a51e2 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 899 | assert(isSCEVable(Ty) && |
| 900 | "This is not a conversion to a SCEVable type!"); |
| 901 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | f62cfe5 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 902 | |
Dan Gohman | c86c0df | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 903 | // Fold if the operand is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 904 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 905 | const Type *IntTy = getEffectiveSCEVType(Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 906 | Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy); |
| 907 | if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 908 | return getConstant(cast<ConstantInt>(C)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 909 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 910 | |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 911 | // sext(sext(x)) --> sext(x) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 912 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Dan Gohman | 1a5c499 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 913 | return getSignExtendExpr(SS->getOperand(), Ty); |
| 914 | |
Dan Gohman | db88842 | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 915 | // Before doing any expensive analysis, check to see if we've already |
| 916 | // computed a SCEV for this Op and Ty. |
| 917 | FoldingSetNodeID ID; |
| 918 | ID.AddInteger(scSignExtend); |
| 919 | ID.AddPointer(Op); |
| 920 | ID.AddPointer(Ty); |
| 921 | void *IP = 0; |
| 922 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 923 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 924 | // If the input value is a chrec scev, and we can prove that the value |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 925 | // did not overflow the old, smaller, value, we can sign extend all of the |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 926 | // operands (often constants). This allows analysis of something like |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 927 | // this: for (signed char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 928 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 929 | if (AR->isAffine()) { |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 930 | const SCEV *Start = AR->getStart(); |
| 931 | const SCEV *Step = AR->getStepRecurrence(*this); |
| 932 | unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
| 933 | const Loop *L = AR->getLoop(); |
| 934 | |
Dan Gohman | 1bc5316 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 935 | // If we have special knowledge that this addrec won't overflow, |
| 936 | // we don't need to do any further analysis. |
| 937 | if (AR->hasNoSignedOverflow()) |
| 938 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 939 | getSignExtendExpr(Step, Ty), |
| 940 | L); |
| 941 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 942 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 943 | // Note that this serves two purposes: It filters out loops that are |
| 944 | // simply not analyzable, and it covers the case where this code is |
| 945 | // being called from within backedge-taken count analysis, such that |
| 946 | // attempting to ask for the backedge-taken count would likely result |
| 947 | // in infinite recursion. In the later case, the analysis code will |
| 948 | // cope with a conservative value, and it will take care to purge |
| 949 | // that value once it has finished. |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 950 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 951 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | 4ada77f | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 952 | // Manually compute the final value for AR, checking for |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 953 | // overflow. |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 954 | |
| 955 | // Check whether the backedge-taken count can be losslessly casted to |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 956 | // the addrec's type. The count is always unsigned. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 957 | const SCEV *CastedMaxBECount = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 958 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 959 | const SCEV *RecastedMaxBECount = |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 960 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 961 | if (MaxBECount == RecastedMaxBECount) { |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 962 | const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 963 | // Check whether Start+Step*MaxBECount has no signed overflow. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 964 | const SCEV *SMul = |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 965 | getMulExpr(CastedMaxBECount, |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 966 | getTruncateOrSignExtend(Step, Start->getType())); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 967 | const SCEV *Add = getAddExpr(Start, SMul); |
| 968 | const SCEV *OperandExtendedAdd = |
Dan Gohman | 3bb37f5 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 969 | getAddExpr(getSignExtendExpr(Start, WideTy), |
| 970 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 971 | getSignExtendExpr(Step, WideTy))); |
| 972 | if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | 3ded5b2 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 973 | // Return the expression with the addrec on the outside. |
| 974 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 975 | getSignExtendExpr(Step, Ty), |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 976 | L); |
Dan Gohman | 2d4f5b1 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 977 | |
| 978 | // Similar to above, only this time treat the step value as unsigned. |
| 979 | // This covers loops that count up with an unsigned step. |
| 980 | const SCEV *UMul = |
| 981 | getMulExpr(CastedMaxBECount, |
| 982 | getTruncateOrZeroExtend(Step, Start->getType())); |
| 983 | Add = getAddExpr(Start, UMul); |
| 984 | OperandExtendedAdd = |
Dan Gohman | 0658f61 | 2009-07-25 16:03:30 +0000 | [diff] [blame] | 985 | getAddExpr(getSignExtendExpr(Start, WideTy), |
Dan Gohman | 2d4f5b1 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 986 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 987 | getZeroExtendExpr(Step, WideTy))); |
Dan Gohman | 0658f61 | 2009-07-25 16:03:30 +0000 | [diff] [blame] | 988 | if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | 2d4f5b1 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 989 | // Return the expression with the addrec on the outside. |
| 990 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 991 | getZeroExtendExpr(Step, Ty), |
| 992 | L); |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | // If the backedge is guarded by a comparison with the pre-inc value |
| 996 | // the addrec is safe. Also, if the entry is guarded by a comparison |
| 997 | // with the start value and the backedge is guarded by a comparison |
| 998 | // with the post-inc value, the addrec is safe. |
| 999 | if (isKnownPositive(Step)) { |
| 1000 | const SCEV *N = getConstant(APInt::getSignedMinValue(BitWidth) - |
| 1001 | getSignedRange(Step).getSignedMax()); |
| 1002 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, AR, N) || |
| 1003 | (isLoopGuardedByCond(L, ICmpInst::ICMP_SLT, Start, N) && |
| 1004 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, |
| 1005 | AR->getPostIncExpr(*this), N))) |
| 1006 | // Return the expression with the addrec on the outside. |
| 1007 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 1008 | getSignExtendExpr(Step, Ty), |
| 1009 | L); |
| 1010 | } else if (isKnownNegative(Step)) { |
| 1011 | const SCEV *N = getConstant(APInt::getSignedMaxValue(BitWidth) - |
| 1012 | getSignedRange(Step).getSignedMin()); |
| 1013 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, AR, N) || |
| 1014 | (isLoopGuardedByCond(L, ICmpInst::ICMP_SGT, Start, N) && |
| 1015 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, |
| 1016 | AR->getPostIncExpr(*this), N))) |
| 1017 | // Return the expression with the addrec on the outside. |
| 1018 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 1019 | getSignExtendExpr(Step, Ty), |
| 1020 | L); |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1024 | |
Dan Gohman | db88842 | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 1025 | // The cast wasn't folded; create an explicit cast node. |
| 1026 | // Recompute the insert position, as it may have been invalidated. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1027 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1028 | SCEV *S = SCEVAllocator.Allocate<SCEVSignExtendExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1029 | new (S) SCEVSignExtendExpr(ID, Op, Ty); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1030 | UniqueSCEVs.InsertNode(S, IP); |
| 1031 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1034 | /// getAnyExtendExpr - Return a SCEV for the given operand extended with |
| 1035 | /// unspecified bits out to the given type. |
| 1036 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1037 | const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op, |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 1038 | const Type *Ty) { |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1039 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
| 1040 | "This is not an extending conversion!"); |
| 1041 | assert(isSCEVable(Ty) && |
| 1042 | "This is not a conversion to a SCEVable type!"); |
| 1043 | Ty = getEffectiveSCEVType(Ty); |
| 1044 | |
| 1045 | // Sign-extend negative constants. |
| 1046 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| 1047 | if (SC->getValue()->getValue().isNegative()) |
| 1048 | return getSignExtendExpr(Op, Ty); |
| 1049 | |
| 1050 | // Peel off a truncate cast. |
| 1051 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1052 | const SCEV *NewOp = T->getOperand(); |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1053 | if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty)) |
| 1054 | return getAnyExtendExpr(NewOp, Ty); |
| 1055 | return getTruncateOrNoop(NewOp, Ty); |
| 1056 | } |
| 1057 | |
| 1058 | // Next try a zext cast. If the cast is folded, use it. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1059 | const SCEV *ZExt = getZeroExtendExpr(Op, Ty); |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1060 | if (!isa<SCEVZeroExtendExpr>(ZExt)) |
| 1061 | return ZExt; |
| 1062 | |
| 1063 | // Next try a sext cast. If the cast is folded, use it. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1064 | const SCEV *SExt = getSignExtendExpr(Op, Ty); |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1065 | if (!isa<SCEVSignExtendExpr>(SExt)) |
| 1066 | return SExt; |
| 1067 | |
| 1068 | // If the expression is obviously signed, use the sext cast value. |
| 1069 | if (isa<SCEVSMaxExpr>(Op)) |
| 1070 | return SExt; |
| 1071 | |
| 1072 | // Absent any other information, use the zext cast value. |
| 1073 | return ZExt; |
| 1074 | } |
| 1075 | |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1076 | /// CollectAddOperandsWithScales - Process the given Ops list, which is |
| 1077 | /// a list of operands to be added under the given scale, update the given |
| 1078 | /// map. This is a helper function for getAddRecExpr. As an example of |
| 1079 | /// what it does, given a sequence of operands that would form an add |
| 1080 | /// expression like this: |
| 1081 | /// |
| 1082 | /// m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r) |
| 1083 | /// |
| 1084 | /// where A and B are constants, update the map with these values: |
| 1085 | /// |
| 1086 | /// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0) |
| 1087 | /// |
| 1088 | /// and add 13 + A*B*29 to AccumulatedConstant. |
| 1089 | /// This will allow getAddRecExpr to produce this: |
| 1090 | /// |
| 1091 | /// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B) |
| 1092 | /// |
| 1093 | /// This form often exposes folding opportunities that are hidden in |
| 1094 | /// the original operand list. |
| 1095 | /// |
| 1096 | /// Return true iff it appears that any interesting folding opportunities |
| 1097 | /// may be exposed. This helps getAddRecExpr short-circuit extra work in |
| 1098 | /// the common case where no interesting opportunities are present, and |
| 1099 | /// is also used as a check to avoid infinite recursion. |
| 1100 | /// |
| 1101 | static bool |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1102 | CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M, |
| 1103 | SmallVector<const SCEV *, 8> &NewOps, |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1104 | APInt &AccumulatedConstant, |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1105 | const SmallVectorImpl<const SCEV *> &Ops, |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1106 | const APInt &Scale, |
| 1107 | ScalarEvolution &SE) { |
| 1108 | bool Interesting = false; |
| 1109 | |
| 1110 | // Iterate over the add operands. |
| 1111 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 1112 | const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]); |
| 1113 | if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) { |
| 1114 | APInt NewScale = |
| 1115 | Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue(); |
| 1116 | if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) { |
| 1117 | // A multiplication of a constant with another add; recurse. |
| 1118 | Interesting |= |
| 1119 | CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
| 1120 | cast<SCEVAddExpr>(Mul->getOperand(1)) |
| 1121 | ->getOperands(), |
| 1122 | NewScale, SE); |
| 1123 | } else { |
| 1124 | // A multiplication of a constant with some other value. Update |
| 1125 | // the map. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1126 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end()); |
| 1127 | const SCEV *Key = SE.getMulExpr(MulOps); |
| 1128 | std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = |
Dan Gohman | 3bf01f0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1129 | M.insert(std::make_pair(Key, NewScale)); |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1130 | if (Pair.second) { |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1131 | NewOps.push_back(Pair.first->first); |
| 1132 | } else { |
| 1133 | Pair.first->second += NewScale; |
| 1134 | // The map already had an entry for this value, which may indicate |
| 1135 | // a folding opportunity. |
| 1136 | Interesting = true; |
| 1137 | } |
| 1138 | } |
| 1139 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| 1140 | // Pull a buried constant out to the outside. |
| 1141 | if (Scale != 1 || AccumulatedConstant != 0 || C->isZero()) |
| 1142 | Interesting = true; |
| 1143 | AccumulatedConstant += Scale * C->getValue()->getValue(); |
| 1144 | } else { |
| 1145 | // An ordinary operand. Update the map. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1146 | std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = |
Dan Gohman | 3bf01f0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1147 | M.insert(std::make_pair(Ops[i], Scale)); |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1148 | if (Pair.second) { |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1149 | NewOps.push_back(Pair.first->first); |
| 1150 | } else { |
| 1151 | Pair.first->second += Scale; |
| 1152 | // The map already had an entry for this value, which may indicate |
| 1153 | // a folding opportunity. |
| 1154 | Interesting = true; |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | return Interesting; |
| 1160 | } |
| 1161 | |
| 1162 | namespace { |
| 1163 | struct APIntCompare { |
| 1164 | bool operator()(const APInt &LHS, const APInt &RHS) const { |
| 1165 | return LHS.ult(RHS); |
| 1166 | } |
| 1167 | }; |
| 1168 | } |
| 1169 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1170 | /// getAddExpr - Get a canonical add expression, or something simpler if |
| 1171 | /// possible. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1172 | const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1173 | assert(!Ops.empty() && "Cannot get empty add!"); |
| 1174 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1175 | #ifndef NDEBUG |
| 1176 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1177 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1178 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1179 | "SCEVAddExpr operand types don't match!"); |
| 1180 | #endif |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1181 | |
| 1182 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1183 | GroupByComplexity(Ops, LI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1184 | |
| 1185 | // If there are any constants, fold them together. |
| 1186 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1187 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1188 | ++Idx; |
| 1189 | assert(Idx < Ops.size()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1190 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1191 | // We found two constants, fold them together! |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1192 | Ops[0] = getConstant(LHSC->getValue()->getValue() + |
| 1193 | RHSC->getValue()->getValue()); |
Dan Gohman | 68f23e8 | 2009-06-14 22:53:57 +0000 | [diff] [blame] | 1194 | if (Ops.size() == 2) return Ops[0]; |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1195 | Ops.erase(Ops.begin()+1); // Erase the folded element |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1196 | LHSC = cast<SCEVConstant>(Ops[0]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | // If we are left with a constant zero being added, strip it off. |
| 1200 | if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
| 1201 | Ops.erase(Ops.begin()); |
| 1202 | --Idx; |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | if (Ops.size() == 1) return Ops[0]; |
| 1207 | |
| 1208 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1209 | // so, merge them together into an multiply expression. Since we sorted the |
| 1210 | // list, these values are required to be adjacent. |
| 1211 | const Type *Ty = Ops[0]->getType(); |
| 1212 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1213 | if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2 |
| 1214 | // Found a match, merge the two values into a multiply, and add any |
| 1215 | // remaining values to the result. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1216 | const SCEV *Two = getIntegerSCEV(2, Ty); |
| 1217 | const SCEV *Mul = getMulExpr(Ops[i], Two); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1218 | if (Ops.size() == 2) |
| 1219 | return Mul; |
| 1220 | Ops.erase(Ops.begin()+i, Ops.begin()+i+2); |
| 1221 | Ops.push_back(Mul); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1222 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1225 | // Check for truncates. If all the operands are truncated from the same |
| 1226 | // type, see if factoring out the truncate would permit the result to be |
| 1227 | // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n) |
| 1228 | // if the contents of the resulting outer trunc fold to something simple. |
| 1229 | for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) { |
| 1230 | const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]); |
| 1231 | const Type *DstType = Trunc->getType(); |
| 1232 | const Type *SrcType = Trunc->getOperand()->getType(); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1233 | SmallVector<const SCEV *, 8> LargeOps; |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1234 | bool Ok = true; |
| 1235 | // Check all the operands to see if they can be represented in the |
| 1236 | // source type of the truncate. |
| 1237 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 1238 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) { |
| 1239 | if (T->getOperand()->getType() != SrcType) { |
| 1240 | Ok = false; |
| 1241 | break; |
| 1242 | } |
| 1243 | LargeOps.push_back(T->getOperand()); |
| 1244 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| 1245 | // This could be either sign or zero extension, but sign extension |
| 1246 | // is much more likely to be foldable here. |
| 1247 | LargeOps.push_back(getSignExtendExpr(C, SrcType)); |
| 1248 | } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1249 | SmallVector<const SCEV *, 8> LargeMulOps; |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1250 | for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { |
| 1251 | if (const SCEVTruncateExpr *T = |
| 1252 | dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) { |
| 1253 | if (T->getOperand()->getType() != SrcType) { |
| 1254 | Ok = false; |
| 1255 | break; |
| 1256 | } |
| 1257 | LargeMulOps.push_back(T->getOperand()); |
| 1258 | } else if (const SCEVConstant *C = |
| 1259 | dyn_cast<SCEVConstant>(M->getOperand(j))) { |
| 1260 | // This could be either sign or zero extension, but sign extension |
| 1261 | // is much more likely to be foldable here. |
| 1262 | LargeMulOps.push_back(getSignExtendExpr(C, SrcType)); |
| 1263 | } else { |
| 1264 | Ok = false; |
| 1265 | break; |
| 1266 | } |
| 1267 | } |
| 1268 | if (Ok) |
| 1269 | LargeOps.push_back(getMulExpr(LargeMulOps)); |
| 1270 | } else { |
| 1271 | Ok = false; |
| 1272 | break; |
| 1273 | } |
| 1274 | } |
| 1275 | if (Ok) { |
| 1276 | // Evaluate the expression in the larger type. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1277 | const SCEV *Fold = getAddExpr(LargeOps); |
Dan Gohman | 45b3b54 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1278 | // If it folds to something simple, use it. Otherwise, don't. |
| 1279 | if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold)) |
| 1280 | return getTruncateExpr(Fold, DstType); |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | // Skip past any other cast SCEVs. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1285 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) |
| 1286 | ++Idx; |
| 1287 | |
| 1288 | // If there are add operands they would be next. |
| 1289 | if (Idx < Ops.size()) { |
| 1290 | bool DeletedAdd = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1291 | while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1292 | // If we have an add, expand the add operands onto the end of the operands |
| 1293 | // list. |
| 1294 | Ops.insert(Ops.end(), Add->op_begin(), Add->op_end()); |
| 1295 | Ops.erase(Ops.begin()+Idx); |
| 1296 | DeletedAdd = true; |
| 1297 | } |
| 1298 | |
| 1299 | // If we deleted at least one add, we added operands to the end of the list, |
| 1300 | // and they are not necessarily sorted. Recurse to resort and resimplify |
| 1301 | // any operands we just aquired. |
| 1302 | if (DeletedAdd) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1303 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | // Skip over the add expression until we get to a multiply. |
| 1307 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1308 | ++Idx; |
| 1309 | |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1310 | // Check to see if there are any folding opportunities present with |
| 1311 | // operands multiplied by constant values. |
| 1312 | if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) { |
| 1313 | uint64_t BitWidth = getTypeSizeInBits(Ty); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1314 | DenseMap<const SCEV *, APInt> M; |
| 1315 | SmallVector<const SCEV *, 8> NewOps; |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1316 | APInt AccumulatedConstant(BitWidth, 0); |
| 1317 | if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
| 1318 | Ops, APInt(BitWidth, 1), *this)) { |
| 1319 | // Some interesting folding opportunity is present, so its worthwhile to |
| 1320 | // re-generate the operands list. Group the operands by constant scale, |
| 1321 | // to avoid multiplying by the same constant scale multiple times. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1322 | std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists; |
| 1323 | for (SmallVector<const SCEV *, 8>::iterator I = NewOps.begin(), |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1324 | E = NewOps.end(); I != E; ++I) |
| 1325 | MulOpLists[M.find(*I)->second].push_back(*I); |
| 1326 | // Re-generate the operands list. |
| 1327 | Ops.clear(); |
| 1328 | if (AccumulatedConstant != 0) |
| 1329 | Ops.push_back(getConstant(AccumulatedConstant)); |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1330 | for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator |
| 1331 | I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I) |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1332 | if (I->first != 0) |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1333 | Ops.push_back(getMulExpr(getConstant(I->first), |
| 1334 | getAddExpr(I->second))); |
Dan Gohman | 27bd4cb | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1335 | if (Ops.empty()) |
| 1336 | return getIntegerSCEV(0, Ty); |
| 1337 | if (Ops.size() == 1) |
| 1338 | return Ops[0]; |
| 1339 | return getAddExpr(Ops); |
| 1340 | } |
| 1341 | } |
| 1342 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1343 | // If we are adding something to a multiply expression, make sure the |
| 1344 | // something is not already an operand of the multiply. If so, merge it into |
| 1345 | // the multiply. |
| 1346 | for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1347 | const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1348 | for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1349 | const SCEV *MulOpSCEV = Mul->getOperand(MulOp); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1350 | for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1351 | if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(Ops[AddOp])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1352 | // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1353 | const SCEV *InnerMul = Mul->getOperand(MulOp == 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1354 | if (Mul->getNumOperands() != 2) { |
| 1355 | // If the multiply has more than two operands, we must get the |
| 1356 | // Y*Z term. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1357 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), Mul->op_end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1358 | MulOps.erase(MulOps.begin()+MulOp); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1359 | InnerMul = getMulExpr(MulOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1360 | } |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1361 | const SCEV *One = getIntegerSCEV(1, Ty); |
| 1362 | const SCEV *AddOne = getAddExpr(InnerMul, One); |
| 1363 | const SCEV *OuterMul = getMulExpr(AddOne, Ops[AddOp]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1364 | if (Ops.size() == 2) return OuterMul; |
| 1365 | if (AddOp < Idx) { |
| 1366 | Ops.erase(Ops.begin()+AddOp); |
| 1367 | Ops.erase(Ops.begin()+Idx-1); |
| 1368 | } else { |
| 1369 | Ops.erase(Ops.begin()+Idx); |
| 1370 | Ops.erase(Ops.begin()+AddOp-1); |
| 1371 | } |
| 1372 | Ops.push_back(OuterMul); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1373 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | // Check this multiply against other multiplies being added together. |
| 1377 | for (unsigned OtherMulIdx = Idx+1; |
| 1378 | OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]); |
| 1379 | ++OtherMulIdx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1380 | const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1381 | // If MulOp occurs in OtherMul, we can fold the two multiplies |
| 1382 | // together. |
| 1383 | for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); |
| 1384 | OMulOp != e; ++OMulOp) |
| 1385 | if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { |
| 1386 | // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E)) |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1387 | const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1388 | if (Mul->getNumOperands() != 2) { |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1389 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), |
| 1390 | Mul->op_end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1391 | MulOps.erase(MulOps.begin()+MulOp); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1392 | InnerMul1 = getMulExpr(MulOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1393 | } |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1394 | const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1395 | if (OtherMul->getNumOperands() != 2) { |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1396 | SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(), |
| 1397 | OtherMul->op_end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1398 | MulOps.erase(MulOps.begin()+OMulOp); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1399 | InnerMul2 = getMulExpr(MulOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1400 | } |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1401 | const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2); |
| 1402 | const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1403 | if (Ops.size() == 2) return OuterMul; |
| 1404 | Ops.erase(Ops.begin()+Idx); |
| 1405 | Ops.erase(Ops.begin()+OtherMulIdx-1); |
| 1406 | Ops.push_back(OuterMul); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1407 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | // If there are any add recurrences in the operands list, see if any other |
| 1414 | // added values are loop invariant. If so, we can fold them into the |
| 1415 | // recurrence. |
| 1416 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1417 | ++Idx; |
| 1418 | |
| 1419 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1420 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1421 | // Scan all of the other operands to this add and add them to the vector if |
| 1422 | // they are loop invariant w.r.t. the recurrence. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1423 | SmallVector<const SCEV *, 8> LIOps; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1424 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1425 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1426 | if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { |
| 1427 | LIOps.push_back(Ops[i]); |
| 1428 | Ops.erase(Ops.begin()+i); |
| 1429 | --i; --e; |
| 1430 | } |
| 1431 | |
| 1432 | // If we found some loop invariants, fold them into the recurrence. |
| 1433 | if (!LIOps.empty()) { |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1434 | // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1435 | LIOps.push_back(AddRec->getStart()); |
| 1436 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1437 | SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(), |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1438 | AddRec->op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1439 | AddRecOps[0] = getAddExpr(LIOps); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1440 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1441 | const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1442 | // If all of the other operands were loop invariant, we are done. |
| 1443 | if (Ops.size() == 1) return NewRec; |
| 1444 | |
| 1445 | // Otherwise, add the folded AddRec by the non-liv parts. |
| 1446 | for (unsigned i = 0;; ++i) |
| 1447 | if (Ops[i] == AddRec) { |
| 1448 | Ops[i] = NewRec; |
| 1449 | break; |
| 1450 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1451 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1455 | // there are multiple AddRec's with the same loop induction variable being |
| 1456 | // added together. If so, we can fold them. |
| 1457 | for (unsigned OtherIdx = Idx+1; |
| 1458 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) |
| 1459 | if (OtherIdx != Idx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1460 | const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1461 | if (AddRec->getLoop() == OtherAddRec->getLoop()) { |
| 1462 | // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D} |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1463 | SmallVector<const SCEV *, 4> NewOps(AddRec->op_begin(), |
| 1464 | AddRec->op_end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1465 | for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) { |
| 1466 | if (i >= NewOps.size()) { |
| 1467 | NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i, |
| 1468 | OtherAddRec->op_end()); |
| 1469 | break; |
| 1470 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1471 | NewOps[i] = getAddExpr(NewOps[i], OtherAddRec->getOperand(i)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1472 | } |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1473 | const SCEV *NewAddRec = getAddRecExpr(NewOps, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1474 | |
| 1475 | if (Ops.size() == 2) return NewAddRec; |
| 1476 | |
| 1477 | Ops.erase(Ops.begin()+Idx); |
| 1478 | Ops.erase(Ops.begin()+OtherIdx-1); |
| 1479 | Ops.push_back(NewAddRec); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1480 | return getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1485 | // next one. |
| 1486 | } |
| 1487 | |
| 1488 | // Okay, it looks like we really DO need an add expr. Check to see if we |
| 1489 | // already have one, otherwise create a new one. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1490 | FoldingSetNodeID ID; |
| 1491 | ID.AddInteger(scAddExpr); |
| 1492 | ID.AddInteger(Ops.size()); |
| 1493 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1494 | ID.AddPointer(Ops[i]); |
| 1495 | void *IP = 0; |
| 1496 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1497 | SCEV *S = SCEVAllocator.Allocate<SCEVAddExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1498 | new (S) SCEVAddExpr(ID, Ops); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1499 | UniqueSCEVs.InsertNode(S, IP); |
| 1500 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1504 | /// getMulExpr - Get a canonical multiply expression, or something simpler if |
| 1505 | /// possible. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1506 | const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1507 | assert(!Ops.empty() && "Cannot get empty mul!"); |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1508 | #ifndef NDEBUG |
| 1509 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1510 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1511 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1512 | "SCEVMulExpr operand types don't match!"); |
| 1513 | #endif |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1514 | |
| 1515 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1516 | GroupByComplexity(Ops, LI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1517 | |
| 1518 | // If there are any constants, fold them together. |
| 1519 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1520 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1521 | |
| 1522 | // C1*(C2+V) -> C1*C2 + C1*V |
| 1523 | if (Ops.size() == 2) |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1524 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1525 | if (Add->getNumOperands() == 2 && |
| 1526 | isa<SCEVConstant>(Add->getOperand(0))) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1527 | return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)), |
| 1528 | getMulExpr(LHSC, Add->getOperand(1))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1529 | |
| 1530 | |
| 1531 | ++Idx; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1532 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1533 | // We found two constants, fold them together! |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1534 | ConstantInt *Fold = ConstantInt::get(getContext(), |
| 1535 | LHSC->getValue()->getValue() * |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1536 | RHSC->getValue()->getValue()); |
| 1537 | Ops[0] = getConstant(Fold); |
| 1538 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1539 | if (Ops.size() == 1) return Ops[0]; |
| 1540 | LHSC = cast<SCEVConstant>(Ops[0]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | // If we are left with a constant one being multiplied, strip it off. |
| 1544 | if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) { |
| 1545 | Ops.erase(Ops.begin()); |
| 1546 | --Idx; |
| 1547 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
| 1548 | // If we have a multiply of zero, it will always be zero. |
| 1549 | return Ops[0]; |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | // Skip over the add expression until we get to a multiply. |
| 1554 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1555 | ++Idx; |
| 1556 | |
| 1557 | if (Ops.size() == 1) |
| 1558 | return Ops[0]; |
| 1559 | |
| 1560 | // If there are mul operands inline them all into this expression. |
| 1561 | if (Idx < Ops.size()) { |
| 1562 | bool DeletedMul = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1563 | while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1564 | // If we have an mul, expand the mul operands onto the end of the operands |
| 1565 | // list. |
| 1566 | Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end()); |
| 1567 | Ops.erase(Ops.begin()+Idx); |
| 1568 | DeletedMul = true; |
| 1569 | } |
| 1570 | |
| 1571 | // If we deleted at least one mul, we added operands to the end of the list, |
| 1572 | // and they are not necessarily sorted. Recurse to resort and resimplify |
| 1573 | // any operands we just aquired. |
| 1574 | if (DeletedMul) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1575 | return getMulExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | // If there are any add recurrences in the operands list, see if any other |
| 1579 | // added values are loop invariant. If so, we can fold them into the |
| 1580 | // recurrence. |
| 1581 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1582 | ++Idx; |
| 1583 | |
| 1584 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1585 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1586 | // Scan all of the other operands to this mul and add them to the vector if |
| 1587 | // they are loop invariant w.r.t. the recurrence. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1588 | SmallVector<const SCEV *, 8> LIOps; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1589 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1590 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1591 | if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { |
| 1592 | LIOps.push_back(Ops[i]); |
| 1593 | Ops.erase(Ops.begin()+i); |
| 1594 | --i; --e; |
| 1595 | } |
| 1596 | |
| 1597 | // If we found some loop invariants, fold them into the recurrence. |
| 1598 | if (!LIOps.empty()) { |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1599 | // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step} |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1600 | SmallVector<const SCEV *, 4> NewOps; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1601 | NewOps.reserve(AddRec->getNumOperands()); |
| 1602 | if (LIOps.size() == 1) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1603 | const SCEV *Scale = LIOps[0]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1604 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1605 | NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1606 | } else { |
| 1607 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1608 | SmallVector<const SCEV *, 4> MulOps(LIOps.begin(), LIOps.end()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1609 | MulOps.push_back(AddRec->getOperand(i)); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1610 | NewOps.push_back(getMulExpr(MulOps)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1611 | } |
| 1612 | } |
| 1613 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1614 | const SCEV *NewRec = getAddRecExpr(NewOps, AddRec->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1615 | |
| 1616 | // If all of the other operands were loop invariant, we are done. |
| 1617 | if (Ops.size() == 1) return NewRec; |
| 1618 | |
| 1619 | // Otherwise, multiply the folded AddRec by the non-liv parts. |
| 1620 | for (unsigned i = 0;; ++i) |
| 1621 | if (Ops[i] == AddRec) { |
| 1622 | Ops[i] = NewRec; |
| 1623 | break; |
| 1624 | } |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1625 | return getMulExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1629 | // there are multiple AddRec's with the same loop induction variable being |
| 1630 | // multiplied together. If so, we can fold them. |
| 1631 | for (unsigned OtherIdx = Idx+1; |
| 1632 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) |
| 1633 | if (OtherIdx != Idx) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1634 | const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1635 | if (AddRec->getLoop() == OtherAddRec->getLoop()) { |
| 1636 | // F * G --> {A,+,B} * {C,+,D} --> {A*C,+,F*D + G*B + B*D} |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1637 | const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec; |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1638 | const SCEV *NewStart = getMulExpr(F->getStart(), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1639 | G->getStart()); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1640 | const SCEV *B = F->getStepRecurrence(*this); |
| 1641 | const SCEV *D = G->getStepRecurrence(*this); |
| 1642 | const SCEV *NewStep = getAddExpr(getMulExpr(F, D), |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1643 | getMulExpr(G, B), |
| 1644 | getMulExpr(B, D)); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1645 | const SCEV *NewAddRec = getAddRecExpr(NewStart, NewStep, |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1646 | F->getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1647 | if (Ops.size() == 2) return NewAddRec; |
| 1648 | |
| 1649 | Ops.erase(Ops.begin()+Idx); |
| 1650 | Ops.erase(Ops.begin()+OtherIdx-1); |
| 1651 | Ops.push_back(NewAddRec); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1652 | return getMulExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1657 | // next one. |
| 1658 | } |
| 1659 | |
| 1660 | // Okay, it looks like we really DO need an mul expr. Check to see if we |
| 1661 | // already have one, otherwise create a new one. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1662 | FoldingSetNodeID ID; |
| 1663 | ID.AddInteger(scMulExpr); |
| 1664 | ID.AddInteger(Ops.size()); |
| 1665 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1666 | ID.AddPointer(Ops[i]); |
| 1667 | void *IP = 0; |
| 1668 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1669 | SCEV *S = SCEVAllocator.Allocate<SCEVMulExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1670 | new (S) SCEVMulExpr(ID, Ops); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1671 | UniqueSCEVs.InsertNode(S, IP); |
| 1672 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Andreas Bolka | 63e4643 | 2009-08-07 22:55:26 +0000 | [diff] [blame] | 1675 | /// getUDivExpr - Get a canonical unsigned division expression, or something |
| 1676 | /// simpler if possible. |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1677 | const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS, |
| 1678 | const SCEV *RHS) { |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1679 | assert(getEffectiveSCEVType(LHS->getType()) == |
| 1680 | getEffectiveSCEVType(RHS->getType()) && |
| 1681 | "SCEVUDivExpr operand types don't match!"); |
| 1682 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1683 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1684 | if (RHSC->getValue()->equalsInt(1)) |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 1685 | return LHS; // X udiv 1 --> x |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1686 | if (RHSC->isZero()) |
| 1687 | return getIntegerSCEV(0, LHS->getType()); // value is undefined |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1688 | |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1689 | // Determine if the division can be folded into the operands of |
| 1690 | // its operands. |
| 1691 | // TODO: Generalize this to non-constants by using known-bits information. |
| 1692 | const Type *Ty = LHS->getType(); |
| 1693 | unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros(); |
| 1694 | unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ; |
| 1695 | // For non-power-of-two values, effectively round the value up to the |
| 1696 | // nearest power of two. |
| 1697 | if (!RHSC->getValue()->getValue().isPowerOf2()) |
| 1698 | ++MaxShiftAmt; |
| 1699 | const IntegerType *ExtTy = |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1700 | IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1701 | // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. |
| 1702 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) |
| 1703 | if (const SCEVConstant *Step = |
| 1704 | dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) |
| 1705 | if (!Step->getValue()->getValue() |
| 1706 | .urem(RHSC->getValue()->getValue()) && |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1707 | getZeroExtendExpr(AR, ExtTy) == |
| 1708 | getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), |
| 1709 | getZeroExtendExpr(Step, ExtTy), |
| 1710 | AR->getLoop())) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1711 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1712 | for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i) |
| 1713 | Operands.push_back(getUDivExpr(AR->getOperand(i), RHS)); |
| 1714 | return getAddRecExpr(Operands, AR->getLoop()); |
| 1715 | } |
| 1716 | // (A*B)/C --> A*(B/C) if safe and B/C can be folded. |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1717 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1718 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1719 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) |
| 1720 | Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy)); |
| 1721 | if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1722 | // Find an operand that's safely divisible. |
| 1723 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1724 | const SCEV *Op = M->getOperand(i); |
| 1725 | const SCEV *Div = getUDivExpr(Op, RHSC); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1726 | if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1727 | const SmallVectorImpl<const SCEV *> &MOperands = M->getOperands(); |
| 1728 | Operands = SmallVector<const SCEV *, 4>(MOperands.begin(), |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1729 | MOperands.end()); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1730 | Operands[i] = Div; |
| 1731 | return getMulExpr(Operands); |
| 1732 | } |
| 1733 | } |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1734 | } |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1735 | // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded. |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1736 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1737 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1738 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) |
| 1739 | Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy)); |
| 1740 | if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { |
| 1741 | Operands.clear(); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1742 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1743 | const SCEV *Op = getUDivExpr(A->getOperand(i), RHS); |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1744 | if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i)) |
| 1745 | break; |
| 1746 | Operands.push_back(Op); |
| 1747 | } |
| 1748 | if (Operands.size() == A->getNumOperands()) |
| 1749 | return getAddExpr(Operands); |
| 1750 | } |
Dan Gohman | 14374d3 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1751 | } |
Dan Gohman | af0a151 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1752 | |
| 1753 | // Fold if both operands are constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1754 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1755 | Constant *LHSCV = LHSC->getValue(); |
| 1756 | Constant *RHSCV = RHSC->getValue(); |
Owen Anderson | 02b48c3 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1757 | return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV, |
Dan Gohman | 55788cf | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 1758 | RHSCV))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1759 | } |
| 1760 | } |
| 1761 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1762 | FoldingSetNodeID ID; |
| 1763 | ID.AddInteger(scUDivExpr); |
| 1764 | ID.AddPointer(LHS); |
| 1765 | ID.AddPointer(RHS); |
| 1766 | void *IP = 0; |
| 1767 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1768 | SCEV *S = SCEVAllocator.Allocate<SCEVUDivExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1769 | new (S) SCEVUDivExpr(ID, LHS, RHS); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1770 | UniqueSCEVs.InsertNode(S, IP); |
| 1771 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1775 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 1776 | /// Simplify the expression as much as possible. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1777 | const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, |
Dan Gohman | 1c4054f | 2009-07-24 01:03:59 +0000 | [diff] [blame] | 1778 | const SCEV *Step, const Loop *L) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1779 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1780 | Operands.push_back(Start); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1781 | if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1782 | if (StepChrec->getLoop() == L) { |
| 1783 | Operands.insert(Operands.end(), StepChrec->op_begin(), |
| 1784 | StepChrec->op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1785 | return getAddRecExpr(Operands, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | Operands.push_back(Step); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1789 | return getAddRecExpr(Operands, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1792 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 1793 | /// Simplify the expression as much as possible. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1794 | const SCEV * |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1795 | ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands, |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1796 | const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1797 | if (Operands.size() == 1) return Operands[0]; |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1798 | #ifndef NDEBUG |
| 1799 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 1800 | assert(getEffectiveSCEVType(Operands[i]->getType()) == |
| 1801 | getEffectiveSCEVType(Operands[0]->getType()) && |
| 1802 | "SCEVAddRecExpr operand types don't match!"); |
| 1803 | #endif |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1804 | |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1805 | if (Operands.back()->isZero()) { |
| 1806 | Operands.pop_back(); |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1807 | return getAddRecExpr(Operands, L); // {X,+,0} --> X |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1808 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1809 | |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1810 | // Canonicalize nested AddRecs in by nesting them in order of loop depth. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1811 | if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) { |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1812 | const Loop* NestedLoop = NestedAR->getLoop(); |
| 1813 | if (L->getLoopDepth() < NestedLoop->getLoopDepth()) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1814 | SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(), |
Dan Gohman | 02ff939 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1815 | NestedAR->op_end()); |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1816 | Operands[0] = NestedAR->getStart(); |
Dan Gohman | 08c4c07 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 1817 | // AddRecs require their operands be loop-invariant with respect to their |
| 1818 | // loops. Don't perform this transformation if it would break this |
| 1819 | // requirement. |
| 1820 | bool AllInvariant = true; |
| 1821 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 1822 | if (!Operands[i]->isLoopInvariant(L)) { |
| 1823 | AllInvariant = false; |
| 1824 | break; |
| 1825 | } |
| 1826 | if (AllInvariant) { |
| 1827 | NestedOperands[0] = getAddRecExpr(Operands, L); |
| 1828 | AllInvariant = true; |
| 1829 | for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i) |
| 1830 | if (!NestedOperands[i]->isLoopInvariant(NestedLoop)) { |
| 1831 | AllInvariant = false; |
| 1832 | break; |
| 1833 | } |
| 1834 | if (AllInvariant) |
| 1835 | // Ok, both add recurrences are valid after the transformation. |
| 1836 | return getAddRecExpr(NestedOperands, NestedLoop); |
| 1837 | } |
| 1838 | // Reset Operands to its original state. |
| 1839 | Operands[0] = NestedAR; |
Dan Gohman | 4293688 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1840 | } |
| 1841 | } |
| 1842 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1843 | FoldingSetNodeID ID; |
| 1844 | ID.AddInteger(scAddRecExpr); |
| 1845 | ID.AddInteger(Operands.size()); |
| 1846 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 1847 | ID.AddPointer(Operands[i]); |
| 1848 | ID.AddPointer(L); |
| 1849 | void *IP = 0; |
| 1850 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1851 | SCEV *S = SCEVAllocator.Allocate<SCEVAddRecExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1852 | new (S) SCEVAddRecExpr(ID, Operands, L); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1853 | UniqueSCEVs.InsertNode(S, IP); |
| 1854 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1857 | const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, |
| 1858 | const SCEV *RHS) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1859 | SmallVector<const SCEV *, 2> Ops; |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1860 | Ops.push_back(LHS); |
| 1861 | Ops.push_back(RHS); |
| 1862 | return getSMaxExpr(Ops); |
| 1863 | } |
| 1864 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1865 | const SCEV * |
| 1866 | ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1867 | assert(!Ops.empty() && "Cannot get empty smax!"); |
| 1868 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1869 | #ifndef NDEBUG |
| 1870 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1871 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1872 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1873 | "SCEVSMaxExpr operand types don't match!"); |
| 1874 | #endif |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1875 | |
| 1876 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1877 | GroupByComplexity(Ops, LI); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1878 | |
| 1879 | // If there are any constants, fold them together. |
| 1880 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1881 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1882 | ++Idx; |
| 1883 | assert(Idx < Ops.size()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1884 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1885 | // We found two constants, fold them together! |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1886 | ConstantInt *Fold = ConstantInt::get(getContext(), |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1887 | APIntOps::smax(LHSC->getValue()->getValue(), |
| 1888 | RHSC->getValue()->getValue())); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1889 | Ops[0] = getConstant(Fold); |
| 1890 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1891 | if (Ops.size() == 1) return Ops[0]; |
| 1892 | LHSC = cast<SCEVConstant>(Ops[0]); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
Dan Gohman | d156c09 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1895 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1896 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) { |
| 1897 | Ops.erase(Ops.begin()); |
| 1898 | --Idx; |
Dan Gohman | d156c09 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1899 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) { |
| 1900 | // If we have an smax with a constant maximum-int, it will always be |
| 1901 | // maximum-int. |
| 1902 | return Ops[0]; |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | if (Ops.size() == 1) return Ops[0]; |
| 1907 | |
| 1908 | // Find the first SMax |
| 1909 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr) |
| 1910 | ++Idx; |
| 1911 | |
| 1912 | // Check to see if one of the operands is an SMax. If so, expand its operands |
| 1913 | // onto our operand list, and recurse to simplify. |
| 1914 | if (Idx < Ops.size()) { |
| 1915 | bool DeletedSMax = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1916 | while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1917 | Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end()); |
| 1918 | Ops.erase(Ops.begin()+Idx); |
| 1919 | DeletedSMax = true; |
| 1920 | } |
| 1921 | |
| 1922 | if (DeletedSMax) |
| 1923 | return getSMaxExpr(Ops); |
| 1924 | } |
| 1925 | |
| 1926 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1927 | // so, delete one. Since we sorted the list, these values are required to |
| 1928 | // be adjacent. |
| 1929 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1930 | if (Ops[i] == Ops[i+1]) { // X smax Y smax Y --> X smax Y |
| 1931 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 1932 | --i; --e; |
| 1933 | } |
| 1934 | |
| 1935 | if (Ops.size() == 1) return Ops[0]; |
| 1936 | |
| 1937 | assert(!Ops.empty() && "Reduced smax down to nothing!"); |
| 1938 | |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1939 | // Okay, it looks like we really DO need an smax expr. Check to see if we |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1940 | // already have one, otherwise create a new one. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1941 | FoldingSetNodeID ID; |
| 1942 | ID.AddInteger(scSMaxExpr); |
| 1943 | ID.AddInteger(Ops.size()); |
| 1944 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1945 | ID.AddPointer(Ops[i]); |
| 1946 | void *IP = 0; |
| 1947 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1948 | SCEV *S = SCEVAllocator.Allocate<SCEVSMaxExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1949 | new (S) SCEVSMaxExpr(ID, Ops); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1950 | UniqueSCEVs.InsertNode(S, IP); |
| 1951 | return S; |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1954 | const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, |
| 1955 | const SCEV *RHS) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1956 | SmallVector<const SCEV *, 2> Ops; |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1957 | Ops.push_back(LHS); |
| 1958 | Ops.push_back(RHS); |
| 1959 | return getUMaxExpr(Ops); |
| 1960 | } |
| 1961 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1962 | const SCEV * |
| 1963 | ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1964 | assert(!Ops.empty() && "Cannot get empty umax!"); |
| 1965 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | a77b3d4 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1966 | #ifndef NDEBUG |
| 1967 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1968 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1969 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1970 | "SCEVUMaxExpr operand types don't match!"); |
| 1971 | #endif |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1972 | |
| 1973 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 5d48645 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1974 | GroupByComplexity(Ops, LI); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1975 | |
| 1976 | // If there are any constants, fold them together. |
| 1977 | unsigned Idx = 0; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1978 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1979 | ++Idx; |
| 1980 | assert(Idx < Ops.size()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1981 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1982 | // We found two constants, fold them together! |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1983 | ConstantInt *Fold = ConstantInt::get(getContext(), |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1984 | APIntOps::umax(LHSC->getValue()->getValue(), |
| 1985 | RHSC->getValue()->getValue())); |
| 1986 | Ops[0] = getConstant(Fold); |
| 1987 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1988 | if (Ops.size() == 1) return Ops[0]; |
| 1989 | LHSC = cast<SCEVConstant>(Ops[0]); |
| 1990 | } |
| 1991 | |
Dan Gohman | d156c09 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1992 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1993 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) { |
| 1994 | Ops.erase(Ops.begin()); |
| 1995 | --Idx; |
Dan Gohman | d156c09 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1996 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) { |
| 1997 | // If we have an umax with a constant maximum-int, it will always be |
| 1998 | // maximum-int. |
| 1999 | return Ops[0]; |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | if (Ops.size() == 1) return Ops[0]; |
| 2004 | |
| 2005 | // Find the first UMax |
| 2006 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr) |
| 2007 | ++Idx; |
| 2008 | |
| 2009 | // Check to see if one of the operands is a UMax. If so, expand its operands |
| 2010 | // onto our operand list, and recurse to simplify. |
| 2011 | if (Idx < Ops.size()) { |
| 2012 | bool DeletedUMax = false; |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2013 | while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2014 | Ops.insert(Ops.end(), UMax->op_begin(), UMax->op_end()); |
| 2015 | Ops.erase(Ops.begin()+Idx); |
| 2016 | DeletedUMax = true; |
| 2017 | } |
| 2018 | |
| 2019 | if (DeletedUMax) |
| 2020 | return getUMaxExpr(Ops); |
| 2021 | } |
| 2022 | |
| 2023 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 2024 | // so, delete one. Since we sorted the list, these values are required to |
| 2025 | // be adjacent. |
| 2026 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 2027 | if (Ops[i] == Ops[i+1]) { // X umax Y umax Y --> X umax Y |
| 2028 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 2029 | --i; --e; |
| 2030 | } |
| 2031 | |
| 2032 | if (Ops.size() == 1) return Ops[0]; |
| 2033 | |
| 2034 | assert(!Ops.empty() && "Reduced umax down to nothing!"); |
| 2035 | |
| 2036 | // Okay, it looks like we really DO need a umax expr. Check to see if we |
| 2037 | // already have one, otherwise create a new one. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2038 | FoldingSetNodeID ID; |
| 2039 | ID.AddInteger(scUMaxExpr); |
| 2040 | ID.AddInteger(Ops.size()); |
| 2041 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 2042 | ID.AddPointer(Ops[i]); |
| 2043 | void *IP = 0; |
| 2044 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 2045 | SCEV *S = SCEVAllocator.Allocate<SCEVUMaxExpr>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 2046 | new (S) SCEVUMaxExpr(ID, Ops); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2047 | UniqueSCEVs.InsertNode(S, IP); |
| 2048 | return S; |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2051 | const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS, |
| 2052 | const SCEV *RHS) { |
Dan Gohman | d01fff8 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2053 | // ~smax(~x, ~y) == smin(x, y). |
| 2054 | return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 2055 | } |
| 2056 | |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2057 | const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS, |
| 2058 | const SCEV *RHS) { |
Dan Gohman | d01fff8 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2059 | // ~umax(~x, ~y) == umin(x, y) |
| 2060 | return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 2061 | } |
| 2062 | |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2063 | const SCEV *ScalarEvolution::getFieldOffsetExpr(const StructType *STy, |
| 2064 | unsigned FieldNo) { |
| 2065 | // If we have TargetData we can determine the constant offset. |
| 2066 | if (TD) { |
| 2067 | const Type *IntPtrTy = TD->getIntPtrType(getContext()); |
| 2068 | const StructLayout &SL = *TD->getStructLayout(STy); |
| 2069 | uint64_t Offset = SL.getElementOffset(FieldNo); |
| 2070 | return getIntegerSCEV(Offset, IntPtrTy); |
| 2071 | } |
| 2072 | |
| 2073 | // Field 0 is always at offset 0. |
| 2074 | if (FieldNo == 0) { |
| 2075 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy)); |
| 2076 | return getIntegerSCEV(0, Ty); |
| 2077 | } |
| 2078 | |
| 2079 | // Okay, it looks like we really DO need an offsetof expr. Check to see if we |
| 2080 | // already have one, otherwise create a new one. |
| 2081 | FoldingSetNodeID ID; |
| 2082 | ID.AddInteger(scFieldOffset); |
| 2083 | ID.AddPointer(STy); |
| 2084 | ID.AddInteger(FieldNo); |
| 2085 | void *IP = 0; |
| 2086 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 2087 | SCEV *S = SCEVAllocator.Allocate<SCEVFieldOffsetExpr>(); |
| 2088 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy)); |
| 2089 | new (S) SCEVFieldOffsetExpr(ID, Ty, STy, FieldNo); |
| 2090 | UniqueSCEVs.InsertNode(S, IP); |
| 2091 | return S; |
| 2092 | } |
| 2093 | |
| 2094 | const SCEV *ScalarEvolution::getAllocSizeExpr(const Type *AllocTy) { |
| 2095 | // If we have TargetData we can determine the constant size. |
| 2096 | if (TD && AllocTy->isSized()) { |
| 2097 | const Type *IntPtrTy = TD->getIntPtrType(getContext()); |
| 2098 | return getIntegerSCEV(TD->getTypeAllocSize(AllocTy), IntPtrTy); |
| 2099 | } |
| 2100 | |
| 2101 | // Expand an array size into the element size times the number |
| 2102 | // of elements. |
| 2103 | if (const ArrayType *ATy = dyn_cast<ArrayType>(AllocTy)) { |
| 2104 | const SCEV *E = getAllocSizeExpr(ATy->getElementType()); |
| 2105 | return getMulExpr( |
| 2106 | E, getConstant(ConstantInt::get(cast<IntegerType>(E->getType()), |
| 2107 | ATy->getNumElements()))); |
| 2108 | } |
| 2109 | |
| 2110 | // Expand a vector size into the element size times the number |
| 2111 | // of elements. |
| 2112 | if (const VectorType *VTy = dyn_cast<VectorType>(AllocTy)) { |
| 2113 | const SCEV *E = getAllocSizeExpr(VTy->getElementType()); |
| 2114 | return getMulExpr( |
| 2115 | E, getConstant(ConstantInt::get(cast<IntegerType>(E->getType()), |
| 2116 | VTy->getNumElements()))); |
| 2117 | } |
| 2118 | |
| 2119 | // Okay, it looks like we really DO need a sizeof expr. Check to see if we |
| 2120 | // already have one, otherwise create a new one. |
| 2121 | FoldingSetNodeID ID; |
| 2122 | ID.AddInteger(scAllocSize); |
| 2123 | ID.AddPointer(AllocTy); |
| 2124 | void *IP = 0; |
| 2125 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 2126 | SCEV *S = SCEVAllocator.Allocate<SCEVAllocSizeExpr>(); |
| 2127 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy)); |
| 2128 | new (S) SCEVAllocSizeExpr(ID, Ty, AllocTy); |
| 2129 | UniqueSCEVs.InsertNode(S, IP); |
| 2130 | return S; |
| 2131 | } |
| 2132 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2133 | const SCEV *ScalarEvolution::getUnknown(Value *V) { |
Dan Gohman | 984c78a | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2134 | // Don't attempt to do anything other than create a SCEVUnknown object |
| 2135 | // here. createSCEV only calls getUnknown after checking for all other |
| 2136 | // interesting possibilities, and any other code that calls getUnknown |
| 2137 | // is doing so in order to hide a value from SCEV canonicalization. |
| 2138 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2139 | FoldingSetNodeID ID; |
| 2140 | ID.AddInteger(scUnknown); |
| 2141 | ID.AddPointer(V); |
| 2142 | void *IP = 0; |
| 2143 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 2144 | SCEV *S = SCEVAllocator.Allocate<SCEVUnknown>(); |
Dan Gohman | d43a828 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 2145 | new (S) SCEVUnknown(ID, V); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2146 | UniqueSCEVs.InsertNode(S, IP); |
| 2147 | return S; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2150 | //===----------------------------------------------------------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2151 | // Basic SCEV Analysis and PHI Idiom Recognition Code |
| 2152 | // |
| 2153 | |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2154 | /// isSCEVable - Test if values of the given type are analyzable within |
| 2155 | /// the SCEV framework. This primarily includes integer types, and it |
| 2156 | /// can optionally include pointer types if the ScalarEvolution class |
| 2157 | /// has access to target-specific information. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2158 | bool ScalarEvolution::isSCEVable(const Type *Ty) const { |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2159 | // Integers and pointers are always SCEVable. |
| 2160 | return Ty->isInteger() || isa<PointerType>(Ty); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | /// getTypeSizeInBits - Return the size in bits of the specified type, |
| 2164 | /// for which isSCEVable must return true. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2165 | uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2166 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2167 | |
| 2168 | // If we have a TargetData, use it! |
| 2169 | if (TD) |
| 2170 | return TD->getTypeSizeInBits(Ty); |
| 2171 | |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2172 | // Integer types have fixed sizes. |
| 2173 | if (Ty->isInteger()) |
| 2174 | return Ty->getPrimitiveSizeInBits(); |
| 2175 | |
| 2176 | // The only other support type is pointer. Without TargetData, conservatively |
| 2177 | // assume pointers are 64-bit. |
| 2178 | assert(isa<PointerType>(Ty) && "isSCEVable permitted a non-SCEVable type!"); |
| 2179 | return 64; |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | /// getEffectiveSCEVType - Return a type with the same bitwidth as |
| 2183 | /// the given type and which represents how SCEV will treat the given |
| 2184 | /// type, for which isSCEVable must return true. For pointer types, |
| 2185 | /// this is the pointer-sized integer type. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2186 | const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2187 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2188 | |
| 2189 | if (Ty->isInteger()) |
| 2190 | return Ty; |
| 2191 | |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2192 | // The only other support type is pointer. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2193 | assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!"); |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2194 | if (TD) return TD->getIntPtrType(getContext()); |
| 2195 | |
| 2196 | // Without TargetData, conservatively assume pointers are 64-bit. |
| 2197 | return Type::getInt64Ty(getContext()); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2198 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2199 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2200 | const SCEV *ScalarEvolution::getCouldNotCompute() { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2201 | return &CouldNotCompute; |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2204 | /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the |
| 2205 | /// expression and create a new one. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2206 | const SCEV *ScalarEvolution::getSCEV(Value *V) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2207 | assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2208 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2209 | std::map<SCEVCallbackVH, const SCEV *>::iterator I = Scalars.find(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2210 | if (I != Scalars.end()) return I->second; |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2211 | const SCEV *S = createSCEV(V); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2212 | Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2213 | return S; |
| 2214 | } |
| 2215 | |
Dan Gohman | 984c78a | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2216 | /// getIntegerSCEV - Given a SCEVable type, create a constant for the |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2217 | /// specified signed integer value and return a SCEV for the constant. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2218 | const SCEV *ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) { |
Dan Gohman | 984c78a | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2219 | const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty)); |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2220 | return getConstant(ConstantInt::get(ITy, Val)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V |
| 2224 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2225 | const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) { |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2226 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2227 | return getConstant( |
Owen Anderson | 02b48c3 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2228 | cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue()))); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2229 | |
| 2230 | const Type *Ty = V->getType(); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2231 | Ty = getEffectiveSCEVType(Ty); |
Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2232 | return getMulExpr(V, |
Owen Anderson | aac2837 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2233 | getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty)))); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
| 2236 | /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2237 | const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) { |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2238 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2239 | return getConstant( |
Owen Anderson | 02b48c3 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2240 | cast<ConstantInt>(ConstantExpr::getNot(VC->getValue()))); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2241 | |
| 2242 | const Type *Ty = V->getType(); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2243 | Ty = getEffectiveSCEVType(Ty); |
Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2244 | const SCEV *AllOnes = |
Owen Anderson | aac2837 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2245 | getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2246 | return getMinusSCEV(AllOnes, V); |
| 2247 | } |
| 2248 | |
| 2249 | /// getMinusSCEV - Return a SCEV corresponding to LHS - RHS. |
| 2250 | /// |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2251 | const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, |
| 2252 | const SCEV *RHS) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2253 | // X - Y --> X + -Y |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2254 | return getAddExpr(LHS, getNegativeSCEV(RHS)); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2258 | /// input value to the specified type. If the type must be extended, it is zero |
| 2259 | /// extended. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2260 | const SCEV * |
| 2261 | ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2262 | const Type *Ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2263 | const Type *SrcTy = V->getType(); |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2264 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && |
| 2265 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2266 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2267 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2268 | return V; // No conversion |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2269 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2270 | return getTruncateExpr(V, Ty); |
| 2271 | return getZeroExtendExpr(V, Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2275 | /// input value to the specified type. If the type must be extended, it is sign |
| 2276 | /// extended. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2277 | const SCEV * |
| 2278 | ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, |
Nick Lewycky | 37d0464 | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2279 | const Type *Ty) { |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2280 | const Type *SrcTy = V->getType(); |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2281 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && |
| 2282 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2283 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2284 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2285 | return V; // No conversion |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2286 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2287 | return getTruncateExpr(V, Ty); |
| 2288 | return getSignExtendExpr(V, Ty); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2291 | /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2292 | /// input value to the specified type. If the type must be extended, it is zero |
| 2293 | /// extended. The conversion must not be narrowing. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2294 | const SCEV * |
| 2295 | ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2296 | const Type *SrcTy = V->getType(); |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2297 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && |
| 2298 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2299 | "Cannot noop or zero extend with non-integer arguments!"); |
| 2300 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2301 | "getNoopOrZeroExtend cannot truncate!"); |
| 2302 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2303 | return V; // No conversion |
| 2304 | return getZeroExtendExpr(V, Ty); |
| 2305 | } |
| 2306 | |
| 2307 | /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2308 | /// input value to the specified type. If the type must be extended, it is sign |
| 2309 | /// extended. The conversion must not be narrowing. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2310 | const SCEV * |
| 2311 | ScalarEvolution::getNoopOrSignExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2312 | const Type *SrcTy = V->getType(); |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2313 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && |
| 2314 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2315 | "Cannot noop or sign extend with non-integer arguments!"); |
| 2316 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2317 | "getNoopOrSignExtend cannot truncate!"); |
| 2318 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2319 | return V; // No conversion |
| 2320 | return getSignExtendExpr(V, Ty); |
| 2321 | } |
| 2322 | |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2323 | /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of |
| 2324 | /// the input value to the specified type. If the type must be extended, |
| 2325 | /// it is extended with unspecified bits. The conversion must not be |
| 2326 | /// narrowing. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2327 | const SCEV * |
| 2328 | ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2329 | const Type *SrcTy = V->getType(); |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2330 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && |
| 2331 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | e1ca7e8 | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2332 | "Cannot noop or any extend with non-integer arguments!"); |
| 2333 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2334 | "getNoopOrAnyExtend cannot truncate!"); |
| 2335 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2336 | return V; // No conversion |
| 2337 | return getAnyExtendExpr(V, Ty); |
| 2338 | } |
| 2339 | |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2340 | /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the |
| 2341 | /// input value to the specified type. The conversion must not be widening. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2342 | const SCEV * |
| 2343 | ScalarEvolution::getTruncateOrNoop(const SCEV *V, const Type *Ty) { |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2344 | const Type *SrcTy = V->getType(); |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2345 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && |
| 2346 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | ac95933 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2347 | "Cannot truncate or noop with non-integer arguments!"); |
| 2348 | assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) && |
| 2349 | "getTruncateOrNoop cannot extend!"); |
| 2350 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2351 | return V; // No conversion |
| 2352 | return getTruncateExpr(V, Ty); |
| 2353 | } |
| 2354 | |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2355 | /// getUMaxFromMismatchedTypes - Promote the operands to the wider of |
| 2356 | /// the types using zero-extension, and then perform a umax operation |
| 2357 | /// with them. |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2358 | const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS, |
| 2359 | const SCEV *RHS) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2360 | const SCEV *PromotedLHS = LHS; |
| 2361 | const SCEV *PromotedRHS = RHS; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2362 | |
| 2363 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2364 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2365 | else |
| 2366 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2367 | |
| 2368 | return getUMaxExpr(PromotedLHS, PromotedRHS); |
| 2369 | } |
| 2370 | |
Dan Gohman | 9e62bb0 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2371 | /// getUMinFromMismatchedTypes - Promote the operands to the wider of |
| 2372 | /// the types using zero-extension, and then perform a umin operation |
| 2373 | /// with them. |
Dan Gohman | 8c4f20b | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2374 | const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS, |
| 2375 | const SCEV *RHS) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2376 | const SCEV *PromotedLHS = LHS; |
| 2377 | const SCEV *PromotedRHS = RHS; |
Dan Gohman | 9e62bb0 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2378 | |
| 2379 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2380 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2381 | else |
| 2382 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2383 | |
| 2384 | return getUMinExpr(PromotedLHS, PromotedRHS); |
| 2385 | } |
| 2386 | |
Dan Gohman | 2aa3f04 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2387 | /// PushDefUseChildren - Push users of the given Instruction |
| 2388 | /// onto the given Worklist. |
| 2389 | static void |
| 2390 | PushDefUseChildren(Instruction *I, |
| 2391 | SmallVectorImpl<Instruction *> &Worklist) { |
| 2392 | // Push the def-use children onto the Worklist stack. |
| 2393 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 2394 | UI != UE; ++UI) |
| 2395 | Worklist.push_back(cast<Instruction>(UI)); |
| 2396 | } |
| 2397 | |
| 2398 | /// ForgetSymbolicValue - This looks up computed SCEV values for all |
| 2399 | /// instructions that depend on the given instruction and removes them from |
| 2400 | /// the Scalars map if they reference SymName. This is used during PHI |
| 2401 | /// resolution. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2402 | void |
Dan Gohman | 2aa3f04 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2403 | ScalarEvolution::ForgetSymbolicName(Instruction *I, const SCEV *SymName) { |
| 2404 | SmallVector<Instruction *, 16> Worklist; |
| 2405 | PushDefUseChildren(I, Worklist); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2406 | |
Dan Gohman | 2aa3f04 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2407 | SmallPtrSet<Instruction *, 8> Visited; |
| 2408 | Visited.insert(I); |
| 2409 | while (!Worklist.empty()) { |
| 2410 | Instruction *I = Worklist.pop_back_val(); |
| 2411 | if (!Visited.insert(I)) continue; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2412 | |
Dan Gohman | 2aa3f04 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2413 | std::map<SCEVCallbackVH, const SCEV*>::iterator It = |
| 2414 | Scalars.find(static_cast<Value *>(I)); |
| 2415 | if (It != Scalars.end()) { |
| 2416 | // Short-circuit the def-use traversal if the symbolic name |
| 2417 | // ceases to appear in expressions. |
| 2418 | if (!It->second->hasOperand(SymName)) |
| 2419 | continue; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2420 | |
Dan Gohman | 2aa3f04 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2421 | // SCEVUnknown for a PHI either means that it has an unrecognized |
| 2422 | // structure, or it's a PHI that's in the progress of being computed |
| 2423 | // by createNodeForPHI. In the former case, additional loop trip |
| 2424 | // count information isn't going to change anything. In the later |
| 2425 | // case, createNodeForPHI will perform the necessary updates on its |
| 2426 | // own when it gets to that point. |
| 2427 | if (!isa<PHINode>(I) || !isa<SCEVUnknown>(It->second)) |
| 2428 | Scalars.erase(It); |
| 2429 | ValuesAtScopes.erase(I); |
| 2430 | } |
| 2431 | |
| 2432 | PushDefUseChildren(I, Worklist); |
| 2433 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
| 2436 | /// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in |
| 2437 | /// a loop header, making it a potential recurrence, or it doesn't. |
| 2438 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2439 | const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2440 | if (PN->getNumIncomingValues() == 2) // The loops have been canonicalized. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2441 | if (const Loop *L = LI->getLoopFor(PN->getParent())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2442 | if (L->getHeader() == PN->getParent()) { |
| 2443 | // If it lives in the loop header, it has two incoming values, one |
| 2444 | // from outside the loop, and one from inside. |
| 2445 | unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0)); |
| 2446 | unsigned BackEdge = IncomingEdge^1; |
| 2447 | |
| 2448 | // While we are analyzing this PHI node, handle its value symbolically. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2449 | const SCEV *SymbolicName = getUnknown(PN); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2450 | assert(Scalars.find(PN) == Scalars.end() && |
| 2451 | "PHI node already processed?"); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2452 | Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2453 | |
| 2454 | // Using this symbolic name for the PHI, analyze the value coming around |
| 2455 | // the back-edge. |
Dan Gohman | 2aa3f04 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2456 | Value *BEValueV = PN->getIncomingValue(BackEdge); |
| 2457 | const SCEV *BEValue = getSCEV(BEValueV); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2458 | |
| 2459 | // NOTE: If BEValue is loop invariant, we know that the PHI node just |
| 2460 | // has a special value for the first iteration of the loop. |
| 2461 | |
| 2462 | // If the value coming around the backedge is an add with the symbolic |
| 2463 | // value we just inserted, then we found a simple induction variable! |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2464 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2465 | // If there is a single occurrence of the symbolic value, replace it |
| 2466 | // with a recurrence. |
| 2467 | unsigned FoundIndex = Add->getNumOperands(); |
| 2468 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2469 | if (Add->getOperand(i) == SymbolicName) |
| 2470 | if (FoundIndex == e) { |
| 2471 | FoundIndex = i; |
| 2472 | break; |
| 2473 | } |
| 2474 | |
| 2475 | if (FoundIndex != Add->getNumOperands()) { |
| 2476 | // Create an add with everything but the specified operand. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2477 | SmallVector<const SCEV *, 8> Ops; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2478 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2479 | if (i != FoundIndex) |
| 2480 | Ops.push_back(Add->getOperand(i)); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2481 | const SCEV *Accum = getAddExpr(Ops); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2482 | |
| 2483 | // This is not a valid addrec if the step amount is varying each |
| 2484 | // loop iteration, but is not itself an addrec in this loop. |
| 2485 | if (Accum->isLoopInvariant(L) || |
| 2486 | (isa<SCEVAddRecExpr>(Accum) && |
| 2487 | cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) { |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2488 | const SCEV *StartVal = |
| 2489 | getSCEV(PN->getIncomingValue(IncomingEdge)); |
Dan Gohman | 1bc5316 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2490 | const SCEVAddRecExpr *PHISCEV = |
| 2491 | cast<SCEVAddRecExpr>(getAddRecExpr(StartVal, Accum, L)); |
| 2492 | |
| 2493 | // If the increment doesn't overflow, then neither the addrec nor the |
| 2494 | // post-increment will overflow. |
| 2495 | if (const AddOperator *OBO = dyn_cast<AddOperator>(BEValueV)) |
| 2496 | if (OBO->getOperand(0) == PN && |
| 2497 | getSCEV(OBO->getOperand(1)) == |
| 2498 | PHISCEV->getStepRecurrence(*this)) { |
| 2499 | const SCEVAddRecExpr *PostInc = PHISCEV->getPostIncExpr(*this); |
| 2500 | if (OBO->hasNoUnsignedOverflow()) { |
| 2501 | const_cast<SCEVAddRecExpr *>(PHISCEV) |
| 2502 | ->setHasNoUnsignedOverflow(true); |
| 2503 | const_cast<SCEVAddRecExpr *>(PostInc) |
| 2504 | ->setHasNoUnsignedOverflow(true); |
| 2505 | } |
| 2506 | if (OBO->hasNoSignedOverflow()) { |
| 2507 | const_cast<SCEVAddRecExpr *>(PHISCEV) |
| 2508 | ->setHasNoSignedOverflow(true); |
| 2509 | const_cast<SCEVAddRecExpr *>(PostInc) |
| 2510 | ->setHasNoSignedOverflow(true); |
| 2511 | } |
| 2512 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2513 | |
| 2514 | // Okay, for the entire analysis of this edge we assumed the PHI |
Dan Gohman | 2aa3f04 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2515 | // to be symbolic. We now need to go back and purge all of the |
| 2516 | // entries for the scalars that use the symbolic expression. |
| 2517 | ForgetSymbolicName(PN, SymbolicName); |
| 2518 | Scalars[SCEVCallbackVH(PN, this)] = PHISCEV; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2519 | return PHISCEV; |
| 2520 | } |
| 2521 | } |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2522 | } else if (const SCEVAddRecExpr *AddRec = |
| 2523 | dyn_cast<SCEVAddRecExpr>(BEValue)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2524 | // Otherwise, this could be a loop like this: |
| 2525 | // i = 0; for (j = 1; ..; ++j) { .... i = j; } |
| 2526 | // In this case, j = {1,+,1} and BEValue is j. |
| 2527 | // Because the other in-value of i (0) fits the evolution of BEValue |
| 2528 | // i really is an addrec evolution. |
| 2529 | if (AddRec->getLoop() == L && AddRec->isAffine()) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2530 | const SCEV *StartVal = getSCEV(PN->getIncomingValue(IncomingEdge)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2531 | |
| 2532 | // If StartVal = j.start - j.stride, we can use StartVal as the |
| 2533 | // initial step of the addrec evolution. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2534 | if (StartVal == getMinusSCEV(AddRec->getOperand(0), |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 2535 | AddRec->getOperand(1))) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2536 | const SCEV *PHISCEV = |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2537 | getAddRecExpr(StartVal, AddRec->getOperand(1), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2538 | |
| 2539 | // Okay, for the entire analysis of this edge we assumed the PHI |
Dan Gohman | 2aa3f04 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2540 | // to be symbolic. We now need to go back and purge all of the |
| 2541 | // entries for the scalars that use the symbolic expression. |
| 2542 | ForgetSymbolicName(PN, SymbolicName); |
| 2543 | Scalars[SCEVCallbackVH(PN, this)] = PHISCEV; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2544 | return PHISCEV; |
| 2545 | } |
| 2546 | } |
| 2547 | } |
| 2548 | |
| 2549 | return SymbolicName; |
| 2550 | } |
| 2551 | |
Dan Gohman | 32f35cc | 2009-07-14 14:06:25 +0000 | [diff] [blame] | 2552 | // It's tempting to recognize PHIs with a unique incoming value, however |
| 2553 | // this leads passes like indvars to break LCSSA form. Fortunately, such |
| 2554 | // PHIs are rare, as instcombine zaps them. |
| 2555 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2556 | // If it's not a loop phi, we can't handle it yet. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2557 | return getUnknown(PN); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2560 | /// createNodeForGEP - Expand GEP instructions into add and multiply |
| 2561 | /// operations. This allows them to be analyzed by regular SCEV code. |
| 2562 | /// |
Dan Gohman | 9545fb0 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 2563 | const SCEV *ScalarEvolution::createNodeForGEP(Operator *GEP) { |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2564 | |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2565 | const Type *IntPtrTy = getEffectiveSCEVType(GEP->getType()); |
Dan Gohman | c7034fa | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2566 | Value *Base = GEP->getOperand(0); |
Dan Gohman | d586a4f | 2009-05-09 00:14:52 +0000 | [diff] [blame] | 2567 | // Don't attempt to analyze GEPs over unsized objects. |
| 2568 | if (!cast<PointerType>(Base->getType())->getElementType()->isSized()) |
| 2569 | return getUnknown(GEP); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2570 | const SCEV *TotalOffset = getIntegerSCEV(0, IntPtrTy); |
Dan Gohman | c7034fa | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2571 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 2572 | for (GetElementPtrInst::op_iterator I = next(GEP->op_begin()), |
| 2573 | E = GEP->op_end(); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2574 | I != E; ++I) { |
| 2575 | Value *Index = *I; |
| 2576 | // Compute the (potentially symbolic) offset in bytes for this index. |
| 2577 | if (const StructType *STy = dyn_cast<StructType>(*GTI++)) { |
| 2578 | // For a struct, add the member offset. |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2579 | unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue(); |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2580 | TotalOffset = getAddExpr(TotalOffset, |
| 2581 | getFieldOffsetExpr(STy, FieldNo)); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2582 | } else { |
| 2583 | // For an array, add the element offset, explicitly scaled. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2584 | const SCEV *LocalOffset = getSCEV(Index); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2585 | if (!isa<PointerType>(LocalOffset->getType())) |
| 2586 | // Getelementptr indicies are signed. |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2587 | LocalOffset = getTruncateOrSignExtend(LocalOffset, IntPtrTy); |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2588 | LocalOffset = getMulExpr(LocalOffset, getAllocSizeExpr(*GTI)); |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2589 | TotalOffset = getAddExpr(TotalOffset, LocalOffset); |
| 2590 | } |
| 2591 | } |
| 2592 | return getAddExpr(getSCEV(Base), TotalOffset); |
| 2593 | } |
| 2594 | |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2595 | /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is |
| 2596 | /// guaranteed to end in (at every loop iteration). It is, at the same time, |
| 2597 | /// the minimum number of times S is divisible by 2. For example, given {4,+,8} |
| 2598 | /// it returns 2. If S is guaranteed to be 0, it returns the bitwidth of S. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2599 | uint32_t |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2600 | ScalarEvolution::GetMinTrailingZeros(const SCEV *S) { |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2601 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Chris Lattner | 6ecce2a | 2007-11-23 22:36:49 +0000 | [diff] [blame] | 2602 | return C->getValue()->getValue().countTrailingZeros(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2603 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2604 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S)) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2605 | return std::min(GetMinTrailingZeros(T->getOperand()), |
| 2606 | (uint32_t)getTypeSizeInBits(T->getType())); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2607 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2608 | if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) { |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2609 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 2610 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 2611 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2614 | if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) { |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2615 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 2616 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 2617 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2620 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2621 | // The result is the min of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2622 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2623 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2624 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2625 | return MinOpRes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2628 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2629 | // The result is the sum of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2630 | uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0)); |
| 2631 | uint32_t BitWidth = getTypeSizeInBits(M->getType()); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2632 | for (unsigned i = 1, e = M->getNumOperands(); |
| 2633 | SumOpRes != BitWidth && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2634 | SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2635 | BitWidth); |
| 2636 | return SumOpRes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2637 | } |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2638 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2639 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2640 | // The result is the min of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2641 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2642 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2643 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2644 | return MinOpRes; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2645 | } |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2646 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2647 | if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) { |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2648 | // The result is the min of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2649 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2650 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2651 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2652 | return MinOpRes; |
| 2653 | } |
| 2654 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2655 | if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) { |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2656 | // The result is the min of all operands results. |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2657 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2658 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2659 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2660 | return MinOpRes; |
| 2661 | } |
| 2662 | |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2663 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2664 | // For a SCEVUnknown, ask ValueTracking. |
| 2665 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2666 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 2667 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 2668 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones); |
| 2669 | return Zeros.countTrailingOnes(); |
| 2670 | } |
| 2671 | |
| 2672 | // SCEVUDivExpr |
Nick Lewycky | 4cb604b | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2673 | return 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2676 | /// getUnsignedRange - Determine the unsigned range for a particular SCEV. |
| 2677 | /// |
| 2678 | ConstantRange |
| 2679 | ScalarEvolution::getUnsignedRange(const SCEV *S) { |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2680 | |
| 2681 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2682 | return ConstantRange(C->getValue()->getValue()); |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2683 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2684 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 2685 | ConstantRange X = getUnsignedRange(Add->getOperand(0)); |
| 2686 | for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) |
| 2687 | X = X.add(getUnsignedRange(Add->getOperand(i))); |
| 2688 | return X; |
| 2689 | } |
| 2690 | |
| 2691 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { |
| 2692 | ConstantRange X = getUnsignedRange(Mul->getOperand(0)); |
| 2693 | for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) |
| 2694 | X = X.multiply(getUnsignedRange(Mul->getOperand(i))); |
| 2695 | return X; |
| 2696 | } |
| 2697 | |
| 2698 | if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) { |
| 2699 | ConstantRange X = getUnsignedRange(SMax->getOperand(0)); |
| 2700 | for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) |
| 2701 | X = X.smax(getUnsignedRange(SMax->getOperand(i))); |
| 2702 | return X; |
| 2703 | } |
| 2704 | |
| 2705 | if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) { |
| 2706 | ConstantRange X = getUnsignedRange(UMax->getOperand(0)); |
| 2707 | for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) |
| 2708 | X = X.umax(getUnsignedRange(UMax->getOperand(i))); |
| 2709 | return X; |
| 2710 | } |
| 2711 | |
| 2712 | if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { |
| 2713 | ConstantRange X = getUnsignedRange(UDiv->getLHS()); |
| 2714 | ConstantRange Y = getUnsignedRange(UDiv->getRHS()); |
| 2715 | return X.udiv(Y); |
| 2716 | } |
| 2717 | |
| 2718 | if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { |
| 2719 | ConstantRange X = getUnsignedRange(ZExt->getOperand()); |
| 2720 | return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth()); |
| 2721 | } |
| 2722 | |
| 2723 | if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { |
| 2724 | ConstantRange X = getUnsignedRange(SExt->getOperand()); |
| 2725 | return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth()); |
| 2726 | } |
| 2727 | |
| 2728 | if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { |
| 2729 | ConstantRange X = getUnsignedRange(Trunc->getOperand()); |
| 2730 | return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth()); |
| 2731 | } |
| 2732 | |
| 2733 | ConstantRange FullSet(getTypeSizeInBits(S->getType()), true); |
| 2734 | |
| 2735 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { |
| 2736 | const SCEV *T = getBackedgeTakenCount(AddRec->getLoop()); |
| 2737 | const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T); |
| 2738 | if (!Trip) return FullSet; |
| 2739 | |
| 2740 | // TODO: non-affine addrec |
| 2741 | if (AddRec->isAffine()) { |
| 2742 | const Type *Ty = AddRec->getType(); |
| 2743 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop()); |
| 2744 | if (getTypeSizeInBits(MaxBECount->getType()) <= getTypeSizeInBits(Ty)) { |
| 2745 | MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty); |
| 2746 | |
| 2747 | const SCEV *Start = AddRec->getStart(); |
Dan Gohman | 13dca60 | 2009-07-21 00:42:47 +0000 | [diff] [blame] | 2748 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2749 | const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this); |
| 2750 | |
| 2751 | // Check for overflow. |
Dan Gohman | 13dca60 | 2009-07-21 00:42:47 +0000 | [diff] [blame] | 2752 | // TODO: This is very conservative. |
| 2753 | if (!(Step->isOne() && |
| 2754 | isKnownPredicate(ICmpInst::ICMP_ULT, Start, End)) && |
| 2755 | !(Step->isAllOnesValue() && |
| 2756 | isKnownPredicate(ICmpInst::ICMP_UGT, Start, End))) |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2757 | return FullSet; |
| 2758 | |
| 2759 | ConstantRange StartRange = getUnsignedRange(Start); |
| 2760 | ConstantRange EndRange = getUnsignedRange(End); |
| 2761 | APInt Min = APIntOps::umin(StartRange.getUnsignedMin(), |
| 2762 | EndRange.getUnsignedMin()); |
| 2763 | APInt Max = APIntOps::umax(StartRange.getUnsignedMax(), |
| 2764 | EndRange.getUnsignedMax()); |
| 2765 | if (Min.isMinValue() && Max.isMaxValue()) |
Dan Gohman | 56e1859 | 2009-07-20 22:41:51 +0000 | [diff] [blame] | 2766 | return FullSet; |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2767 | return ConstantRange(Min, Max+1); |
| 2768 | } |
| 2769 | } |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2773 | // For a SCEVUnknown, ask ValueTracking. |
| 2774 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2775 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 2776 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 2777 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD); |
Dan Gohman | 0762051 | 2009-07-20 22:34:18 +0000 | [diff] [blame] | 2778 | if (Ones == ~Zeros + 1) |
| 2779 | return FullSet; |
| 2780 | return ConstantRange(Ones, ~Zeros + 1); |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2783 | return FullSet; |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2786 | /// getSignedRange - Determine the signed range for a particular SCEV. |
| 2787 | /// |
| 2788 | ConstantRange |
| 2789 | ScalarEvolution::getSignedRange(const SCEV *S) { |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2790 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2791 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
| 2792 | return ConstantRange(C->getValue()->getValue()); |
| 2793 | |
| 2794 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 2795 | ConstantRange X = getSignedRange(Add->getOperand(0)); |
| 2796 | for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) |
| 2797 | X = X.add(getSignedRange(Add->getOperand(i))); |
| 2798 | return X; |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2801 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { |
| 2802 | ConstantRange X = getSignedRange(Mul->getOperand(0)); |
| 2803 | for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) |
| 2804 | X = X.multiply(getSignedRange(Mul->getOperand(i))); |
| 2805 | return X; |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2808 | if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) { |
| 2809 | ConstantRange X = getSignedRange(SMax->getOperand(0)); |
| 2810 | for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) |
| 2811 | X = X.smax(getSignedRange(SMax->getOperand(i))); |
| 2812 | return X; |
| 2813 | } |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2814 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2815 | if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) { |
| 2816 | ConstantRange X = getSignedRange(UMax->getOperand(0)); |
| 2817 | for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) |
| 2818 | X = X.umax(getSignedRange(UMax->getOperand(i))); |
| 2819 | return X; |
| 2820 | } |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2821 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2822 | if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { |
| 2823 | ConstantRange X = getSignedRange(UDiv->getLHS()); |
| 2824 | ConstantRange Y = getSignedRange(UDiv->getRHS()); |
| 2825 | return X.udiv(Y); |
| 2826 | } |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2827 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2828 | if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { |
| 2829 | ConstantRange X = getSignedRange(ZExt->getOperand()); |
| 2830 | return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth()); |
| 2831 | } |
| 2832 | |
| 2833 | if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { |
| 2834 | ConstantRange X = getSignedRange(SExt->getOperand()); |
| 2835 | return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth()); |
| 2836 | } |
| 2837 | |
| 2838 | if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { |
| 2839 | ConstantRange X = getSignedRange(Trunc->getOperand()); |
| 2840 | return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth()); |
| 2841 | } |
| 2842 | |
| 2843 | ConstantRange FullSet(getTypeSizeInBits(S->getType()), true); |
| 2844 | |
| 2845 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { |
| 2846 | const SCEV *T = getBackedgeTakenCount(AddRec->getLoop()); |
| 2847 | const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T); |
| 2848 | if (!Trip) return FullSet; |
| 2849 | |
| 2850 | // TODO: non-affine addrec |
| 2851 | if (AddRec->isAffine()) { |
| 2852 | const Type *Ty = AddRec->getType(); |
| 2853 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop()); |
| 2854 | if (getTypeSizeInBits(MaxBECount->getType()) <= getTypeSizeInBits(Ty)) { |
| 2855 | MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty); |
| 2856 | |
| 2857 | const SCEV *Start = AddRec->getStart(); |
| 2858 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
| 2859 | const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this); |
| 2860 | |
| 2861 | // Check for overflow. |
Dan Gohman | 13dca60 | 2009-07-21 00:42:47 +0000 | [diff] [blame] | 2862 | // TODO: This is very conservative. |
| 2863 | if (!(Step->isOne() && |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2864 | isKnownPredicate(ICmpInst::ICMP_SLT, Start, End)) && |
Dan Gohman | 13dca60 | 2009-07-21 00:42:47 +0000 | [diff] [blame] | 2865 | !(Step->isAllOnesValue() && |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2866 | isKnownPredicate(ICmpInst::ICMP_SGT, Start, End))) |
| 2867 | return FullSet; |
| 2868 | |
| 2869 | ConstantRange StartRange = getSignedRange(Start); |
| 2870 | ConstantRange EndRange = getSignedRange(End); |
| 2871 | APInt Min = APIntOps::smin(StartRange.getSignedMin(), |
| 2872 | EndRange.getSignedMin()); |
| 2873 | APInt Max = APIntOps::smax(StartRange.getSignedMax(), |
| 2874 | EndRange.getSignedMax()); |
| 2875 | if (Min.isMinSignedValue() && Max.isMaxSignedValue()) |
Dan Gohman | dc87c86 | 2009-07-21 00:37:45 +0000 | [diff] [blame] | 2876 | return FullSet; |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2877 | return ConstantRange(Min, Max+1); |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2878 | } |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2879 | } |
Dan Gohman | 61e0c4c | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2882 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2883 | // For a SCEVUnknown, ask ValueTracking. |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2884 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2885 | unsigned NS = ComputeNumSignBits(U->getValue(), TD); |
| 2886 | if (NS == 1) |
| 2887 | return FullSet; |
| 2888 | return |
| 2889 | ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1), |
| 2890 | APInt::getSignedMaxValue(BitWidth).ashr(NS - 1)+1); |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2893 | return FullSet; |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2896 | /// createSCEV - We know that there is no SCEV for the specified value. |
| 2897 | /// Analyze the expression. |
| 2898 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2899 | const SCEV *ScalarEvolution::createSCEV(Value *V) { |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2900 | if (!isSCEVable(V->getType())) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2901 | return getUnknown(V); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2902 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2903 | unsigned Opcode = Instruction::UserOp1; |
| 2904 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 2905 | Opcode = I->getOpcode(); |
| 2906 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 2907 | Opcode = CE->getOpcode(); |
Dan Gohman | 984c78a | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2908 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 2909 | return getConstant(CI); |
| 2910 | else if (isa<ConstantPointerNull>(V)) |
| 2911 | return getIntegerSCEV(0, V->getType()); |
| 2912 | else if (isa<UndefValue>(V)) |
| 2913 | return getIntegerSCEV(0, V->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2914 | else |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2915 | return getUnknown(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2916 | |
Dan Gohman | 9545fb0 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 2917 | Operator *U = cast<Operator>(V); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2918 | switch (Opcode) { |
| 2919 | case Instruction::Add: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2920 | return getAddExpr(getSCEV(U->getOperand(0)), |
| 2921 | getSCEV(U->getOperand(1))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2922 | case Instruction::Mul: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2923 | return getMulExpr(getSCEV(U->getOperand(0)), |
| 2924 | getSCEV(U->getOperand(1))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2925 | case Instruction::UDiv: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2926 | return getUDivExpr(getSCEV(U->getOperand(0)), |
| 2927 | getSCEV(U->getOperand(1))); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2928 | case Instruction::Sub: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2929 | return getMinusSCEV(getSCEV(U->getOperand(0)), |
| 2930 | getSCEV(U->getOperand(1))); |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2931 | case Instruction::And: |
| 2932 | // For an expression like x&255 that merely masks off the high bits, |
| 2933 | // use zext(trunc(x)) as the SCEV expression. |
| 2934 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 91ae1e7 | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2935 | if (CI->isNullValue()) |
| 2936 | return getSCEV(U->getOperand(1)); |
Dan Gohman | c7ebba1 | 2009-04-27 01:41:10 +0000 | [diff] [blame] | 2937 | if (CI->isAllOnesValue()) |
| 2938 | return getSCEV(U->getOperand(0)); |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2939 | const APInt &A = CI->getValue(); |
Dan Gohman | a7726c3 | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2940 | |
| 2941 | // Instcombine's ShrinkDemandedConstant may strip bits out of |
| 2942 | // constants, obscuring what would otherwise be a low-bits mask. |
| 2943 | // Use ComputeMaskedBits to compute what ShrinkDemandedConstant |
| 2944 | // knew about to reconstruct a low-bits mask value. |
| 2945 | unsigned LZ = A.countLeadingZeros(); |
| 2946 | unsigned BitWidth = A.getBitWidth(); |
| 2947 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
| 2948 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 2949 | ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD); |
| 2950 | |
| 2951 | APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ); |
| 2952 | |
Dan Gohman | ae1d7dd | 2009-06-17 23:54:37 +0000 | [diff] [blame] | 2953 | if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask)) |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2954 | return |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2955 | getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)), |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2956 | IntegerType::get(getContext(), BitWidth - LZ)), |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2957 | U->getType()); |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2958 | } |
| 2959 | break; |
Dan Gohman | a7726c3 | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2960 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2961 | case Instruction::Or: |
| 2962 | // If the RHS of the Or is a constant, we may have something like: |
| 2963 | // X*4+1 which got turned into X*4|1. Handle this as an Add so loop |
| 2964 | // optimizations will transparently handle this case. |
| 2965 | // |
| 2966 | // In order for this transformation to be safe, the LHS must be of the |
| 2967 | // form X*(2^n) and the Or constant must be less than 2^n. |
| 2968 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2969 | const SCEV *LHS = getSCEV(U->getOperand(0)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2970 | const APInt &CIVal = CI->getValue(); |
Dan Gohman | 6e923a7 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2971 | if (GetMinTrailingZeros(LHS) >= |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2972 | (CIVal.getBitWidth() - CIVal.countLeadingZeros())) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2973 | return getAddExpr(LHS, getSCEV(U->getOperand(1))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2974 | } |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2975 | break; |
| 2976 | case Instruction::Xor: |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2977 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2978 | // If the RHS of the xor is a signbit, then this is just an add. |
| 2979 | // Instcombine turns add of signbit into xor as a strength reduction step. |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2980 | if (CI->getValue().isSignBit()) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2981 | return getAddExpr(getSCEV(U->getOperand(0)), |
| 2982 | getSCEV(U->getOperand(1))); |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2983 | |
| 2984 | // If the RHS of xor is -1, then this is a not operation. |
Dan Gohman | c897f75 | 2009-05-18 16:17:44 +0000 | [diff] [blame] | 2985 | if (CI->isAllOnesValue()) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2986 | return getNotSCEV(getSCEV(U->getOperand(0))); |
Dan Gohman | fc78cff | 2009-05-18 16:29:04 +0000 | [diff] [blame] | 2987 | |
| 2988 | // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask. |
| 2989 | // This is a variant of the check for xor with -1, and it handles |
| 2990 | // the case where instcombine has trimmed non-demanded bits out |
| 2991 | // of an xor with -1. |
| 2992 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0))) |
| 2993 | if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1))) |
| 2994 | if (BO->getOpcode() == Instruction::And && |
| 2995 | LCI->getValue() == CI->getValue()) |
| 2996 | if (const SCEVZeroExtendExpr *Z = |
Dan Gohman | e49ae43 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 2997 | dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) { |
Dan Gohman | ed1d8bb | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 2998 | const Type *UTy = U->getType(); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2999 | const SCEV *Z0 = Z->getOperand(); |
Dan Gohman | ed1d8bb | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 3000 | const Type *Z0Ty = Z0->getType(); |
| 3001 | unsigned Z0TySize = getTypeSizeInBits(Z0Ty); |
| 3002 | |
| 3003 | // If C is a low-bits mask, the zero extend is zerving to |
| 3004 | // mask off the high bits. Complement the operand and |
| 3005 | // re-apply the zext. |
| 3006 | if (APIntOps::isMask(Z0TySize, CI->getValue())) |
| 3007 | return getZeroExtendExpr(getNotSCEV(Z0), UTy); |
| 3008 | |
| 3009 | // If C is a single bit, it may be in the sign-bit position |
| 3010 | // before the zero-extend. In this case, represent the xor |
| 3011 | // using an add, which is equivalent, and re-apply the zext. |
| 3012 | APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize); |
| 3013 | if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() && |
| 3014 | Trunc.isSignBit()) |
| 3015 | return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)), |
| 3016 | UTy); |
Dan Gohman | e49ae43 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 3017 | } |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3018 | } |
| 3019 | break; |
| 3020 | |
| 3021 | case Instruction::Shl: |
| 3022 | // Turn shift left of a constant amount into a multiply. |
| 3023 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 3024 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3025 | Constant *X = ConstantInt::get(getContext(), |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3026 | APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3027 | return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3028 | } |
| 3029 | break; |
| 3030 | |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3031 | case Instruction::LShr: |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 3032 | // Turn logical shift right of a constant into a unsigned divide. |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3033 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 3034 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3035 | Constant *X = ConstantInt::get(getContext(), |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3036 | APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3037 | return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Nick Lewycky | 7fd2789 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3038 | } |
| 3039 | break; |
| 3040 | |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3041 | case Instruction::AShr: |
| 3042 | // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression. |
| 3043 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) |
| 3044 | if (Instruction *L = dyn_cast<Instruction>(U->getOperand(0))) |
| 3045 | if (L->getOpcode() == Instruction::Shl && |
| 3046 | L->getOperand(1) == U->getOperand(1)) { |
Dan Gohman | 91ae1e7 | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 3047 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 3048 | uint64_t Amt = BitWidth - CI->getZExtValue(); |
| 3049 | if (Amt == BitWidth) |
| 3050 | return getSCEV(L->getOperand(0)); // shift by zero --> noop |
| 3051 | if (Amt > BitWidth) |
| 3052 | return getIntegerSCEV(0, U->getType()); // value is undefined |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3053 | return |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3054 | getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)), |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3055 | IntegerType::get(getContext(), Amt)), |
Dan Gohman | 53bf64a | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3056 | U->getType()); |
| 3057 | } |
| 3058 | break; |
| 3059 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3060 | case Instruction::Trunc: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3061 | return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3062 | |
| 3063 | case Instruction::ZExt: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3064 | return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3065 | |
| 3066 | case Instruction::SExt: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3067 | return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3068 | |
| 3069 | case Instruction::BitCast: |
| 3070 | // BitCasts are no-op casts so we just eliminate the cast. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 3071 | if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3072 | return getSCEV(U->getOperand(0)); |
| 3073 | break; |
| 3074 | |
Dan Gohman | 2ec15e6 | 2009-07-20 17:43:30 +0000 | [diff] [blame] | 3075 | // It's tempting to handle inttoptr and ptrtoint, however this can |
| 3076 | // lead to pointer expressions which cannot be expanded to GEPs |
| 3077 | // (because they may overflow). For now, the only pointer-typed |
| 3078 | // expressions we handle are GEPs and address literals. |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3079 | |
Dan Gohman | 509cf4d | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 3080 | case Instruction::GetElementPtr: |
Dan Gohman | ca5a39e | 2009-05-08 20:58:38 +0000 | [diff] [blame] | 3081 | return createNodeForGEP(U); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3082 | |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3083 | case Instruction::PHI: |
| 3084 | return createNodeForPHI(cast<PHINode>(U)); |
| 3085 | |
| 3086 | case Instruction::Select: |
| 3087 | // This could be a smax or umax that was lowered earlier. |
| 3088 | // Try to recover it. |
| 3089 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) { |
| 3090 | Value *LHS = ICI->getOperand(0); |
| 3091 | Value *RHS = ICI->getOperand(1); |
| 3092 | switch (ICI->getPredicate()) { |
| 3093 | case ICmpInst::ICMP_SLT: |
| 3094 | case ICmpInst::ICMP_SLE: |
| 3095 | std::swap(LHS, RHS); |
| 3096 | // fall through |
| 3097 | case ICmpInst::ICMP_SGT: |
| 3098 | case ICmpInst::ICMP_SGE: |
| 3099 | if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3100 | return getSMaxExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3101 | else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) |
Dan Gohman | d01fff8 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 3102 | return getSMinExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3103 | break; |
| 3104 | case ICmpInst::ICMP_ULT: |
| 3105 | case ICmpInst::ICMP_ULE: |
| 3106 | std::swap(LHS, RHS); |
| 3107 | // fall through |
| 3108 | case ICmpInst::ICMP_UGT: |
| 3109 | case ICmpInst::ICMP_UGE: |
| 3110 | if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3111 | return getUMaxExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3112 | else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) |
Dan Gohman | d01fff8 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 3113 | return getUMinExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3114 | break; |
Dan Gohman | f27dc69 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3115 | case ICmpInst::ICMP_NE: |
| 3116 | // n != 0 ? n : 1 -> umax(n, 1) |
| 3117 | if (LHS == U->getOperand(1) && |
| 3118 | isa<ConstantInt>(U->getOperand(2)) && |
| 3119 | cast<ConstantInt>(U->getOperand(2))->isOne() && |
| 3120 | isa<ConstantInt>(RHS) && |
| 3121 | cast<ConstantInt>(RHS)->isZero()) |
| 3122 | return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(2))); |
| 3123 | break; |
| 3124 | case ICmpInst::ICMP_EQ: |
| 3125 | // n == 0 ? 1 : n -> umax(n, 1) |
| 3126 | if (LHS == U->getOperand(2) && |
| 3127 | isa<ConstantInt>(U->getOperand(1)) && |
| 3128 | cast<ConstantInt>(U->getOperand(1))->isOne() && |
| 3129 | isa<ConstantInt>(RHS) && |
| 3130 | cast<ConstantInt>(RHS)->isZero()) |
| 3131 | return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(1))); |
| 3132 | break; |
Dan Gohman | 3996f47 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3133 | default: |
| 3134 | break; |
| 3135 | } |
| 3136 | } |
| 3137 | |
| 3138 | default: // We cannot analyze this expression. |
| 3139 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3142 | return getUnknown(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | |
| 3146 | |
| 3147 | //===----------------------------------------------------------------------===// |
| 3148 | // Iteration Count Computation Code |
| 3149 | // |
| 3150 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3151 | /// getBackedgeTakenCount - If the specified loop has a predictable |
| 3152 | /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute |
| 3153 | /// object. The backedge-taken count is the number of times the loop header |
| 3154 | /// will be branched to from within the loop. This is one less than the |
| 3155 | /// trip count of the loop, since it doesn't count the first iteration, |
| 3156 | /// when the header is branched to from outside the loop. |
| 3157 | /// |
| 3158 | /// Note that it is not valid to call this method on a loop without a |
| 3159 | /// loop-invariant backedge-taken count (see |
| 3160 | /// hasLoopInvariantBackedgeTakenCount). |
| 3161 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3162 | const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3163 | return getBackedgeTakenInfo(L).Exact; |
| 3164 | } |
| 3165 | |
| 3166 | /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except |
| 3167 | /// return the least SCEV value that is known never to be less than the |
| 3168 | /// actual backedge taken count. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3169 | const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3170 | return getBackedgeTakenInfo(L).Max; |
| 3171 | } |
| 3172 | |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3173 | /// PushLoopPHIs - Push PHI nodes in the header of the given loop |
| 3174 | /// onto the given Worklist. |
| 3175 | static void |
| 3176 | PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) { |
| 3177 | BasicBlock *Header = L->getHeader(); |
| 3178 | |
| 3179 | // Push all Loop-header PHIs onto the Worklist stack. |
| 3180 | for (BasicBlock::iterator I = Header->begin(); |
| 3181 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 3182 | Worklist.push_back(PN); |
| 3183 | } |
| 3184 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3185 | const ScalarEvolution::BackedgeTakenInfo & |
| 3186 | ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3187 | // Initially insert a CouldNotCompute for this loop. If the insertion |
| 3188 | // succeeds, procede to actually compute a backedge-taken count and |
| 3189 | // update the value. The temporary CouldNotCompute value tells SCEV |
| 3190 | // code elsewhere that it shouldn't attempt to request a new |
| 3191 | // backedge-taken count, which could result in infinite recursion. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3192 | std::pair<std::map<const Loop*, BackedgeTakenInfo>::iterator, bool> Pair = |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3193 | BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute())); |
| 3194 | if (Pair.second) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3195 | BackedgeTakenInfo ItCount = ComputeBackedgeTakenCount(L); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3196 | if (ItCount.Exact != getCouldNotCompute()) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3197 | assert(ItCount.Exact->isLoopInvariant(L) && |
| 3198 | ItCount.Max->isLoopInvariant(L) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3199 | "Computed trip count isn't loop invariant for loop!"); |
| 3200 | ++NumTripCountsComputed; |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3201 | |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3202 | // Update the value in the map. |
| 3203 | Pair.first->second = ItCount; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3204 | } else { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3205 | if (ItCount.Max != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3206 | // Update the value in the map. |
| 3207 | Pair.first->second = ItCount; |
| 3208 | if (isa<PHINode>(L->getHeader()->begin())) |
| 3209 | // Only count loops that have phi nodes as not being computable. |
| 3210 | ++NumTripCountsNotComputed; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3211 | } |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3212 | |
| 3213 | // Now that we know more about the trip count for this loop, forget any |
| 3214 | // existing SCEV values for PHI nodes in this loop since they are only |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3215 | // conservative estimates made without the benefit of trip count |
| 3216 | // information. This is similar to the code in |
| 3217 | // forgetLoopBackedgeTakenCount, except that it handles SCEVUnknown PHI |
| 3218 | // nodes specially. |
| 3219 | if (ItCount.hasAnyInfo()) { |
| 3220 | SmallVector<Instruction *, 16> Worklist; |
| 3221 | PushLoopPHIs(L, Worklist); |
| 3222 | |
| 3223 | SmallPtrSet<Instruction *, 8> Visited; |
| 3224 | while (!Worklist.empty()) { |
| 3225 | Instruction *I = Worklist.pop_back_val(); |
| 3226 | if (!Visited.insert(I)) continue; |
| 3227 | |
| 3228 | std::map<SCEVCallbackVH, const SCEV*>::iterator It = |
| 3229 | Scalars.find(static_cast<Value *>(I)); |
| 3230 | if (It != Scalars.end()) { |
| 3231 | // SCEVUnknown for a PHI either means that it has an unrecognized |
| 3232 | // structure, or it's a PHI that's in the progress of being computed |
Dan Gohman | 0fa91f3 | 2009-07-13 22:04:06 +0000 | [diff] [blame] | 3233 | // by createNodeForPHI. In the former case, additional loop trip |
| 3234 | // count information isn't going to change anything. In the later |
| 3235 | // case, createNodeForPHI will perform the necessary updates on its |
| 3236 | // own when it gets to that point. |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3237 | if (!isa<PHINode>(I) || !isa<SCEVUnknown>(It->second)) |
| 3238 | Scalars.erase(It); |
| 3239 | ValuesAtScopes.erase(I); |
| 3240 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3241 | ConstantEvolutionLoopExitValue.erase(PN); |
| 3242 | } |
| 3243 | |
| 3244 | PushDefUseChildren(I, Worklist); |
| 3245 | } |
| 3246 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3247 | } |
Dan Gohman | a9dba96 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3248 | return Pair.first->second; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3249 | } |
| 3250 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3251 | /// forgetLoopBackedgeTakenCount - This method should be called by the |
Dan Gohman | f3a060a | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 3252 | /// client when it has changed a loop in a way that may effect |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3253 | /// ScalarEvolution's ability to compute a trip count, or if the loop |
| 3254 | /// is deleted. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3255 | void ScalarEvolution::forgetLoopBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3256 | BackedgeTakenCounts.erase(L); |
Dan Gohman | 9462302 | 2009-05-02 17:43:35 +0000 | [diff] [blame] | 3257 | |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3258 | SmallVector<Instruction *, 16> Worklist; |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3259 | PushLoopPHIs(L, Worklist); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3260 | |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3261 | SmallPtrSet<Instruction *, 8> Visited; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3262 | while (!Worklist.empty()) { |
| 3263 | Instruction *I = Worklist.pop_back_val(); |
Dan Gohman | b7d04aa | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3264 | if (!Visited.insert(I)) continue; |
| 3265 | |
| 3266 | std::map<SCEVCallbackVH, const SCEV*>::iterator It = |
| 3267 | Scalars.find(static_cast<Value *>(I)); |
| 3268 | if (It != Scalars.end()) { |
| 3269 | Scalars.erase(It); |
| 3270 | ValuesAtScopes.erase(I); |
| 3271 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3272 | ConstantEvolutionLoopExitValue.erase(PN); |
| 3273 | } |
| 3274 | |
| 3275 | PushDefUseChildren(I, Worklist); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3276 | } |
Dan Gohman | f3a060a | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3279 | /// ComputeBackedgeTakenCount - Compute the number of times the backedge |
| 3280 | /// of the specified loop will execute. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3281 | ScalarEvolution::BackedgeTakenInfo |
| 3282 | ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3283 | SmallVector<BasicBlock*, 8> ExitingBlocks; |
| 3284 | L->getExitingBlocks(ExitingBlocks); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3285 | |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3286 | // Examine all exits and pick the most conservative values. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3287 | const SCEV *BECount = getCouldNotCompute(); |
| 3288 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3289 | bool CouldNotComputeBECount = false; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3290 | for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { |
| 3291 | BackedgeTakenInfo NewBTI = |
| 3292 | ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3293 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3294 | if (NewBTI.Exact == getCouldNotCompute()) { |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3295 | // We couldn't compute an exact value for this exit, so |
Dan Gohman | c6e8c83 | 2009-06-22 21:10:22 +0000 | [diff] [blame] | 3296 | // we won't be able to compute an exact value for the loop. |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3297 | CouldNotComputeBECount = true; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3298 | BECount = getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3299 | } else if (!CouldNotComputeBECount) { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3300 | if (BECount == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3301 | BECount = NewBTI.Exact; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3302 | else |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3303 | BECount = getUMinFromMismatchedTypes(BECount, NewBTI.Exact); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3304 | } |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3305 | if (MaxBECount == getCouldNotCompute()) |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3306 | MaxBECount = NewBTI.Max; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3307 | else if (NewBTI.Max != getCouldNotCompute()) |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3308 | MaxBECount = getUMinFromMismatchedTypes(MaxBECount, NewBTI.Max); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
| 3311 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3312 | } |
| 3313 | |
| 3314 | /// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge |
| 3315 | /// of the specified loop will execute if it exits via the specified block. |
| 3316 | ScalarEvolution::BackedgeTakenInfo |
| 3317 | ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L, |
| 3318 | BasicBlock *ExitingBlock) { |
| 3319 | |
| 3320 | // Okay, we've chosen an exiting block. See what condition causes us to |
| 3321 | // exit at this block. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3322 | // |
| 3323 | // FIXME: we should be able to handle switch instructions (with a single exit) |
| 3324 | BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3325 | if (ExitBr == 0) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3326 | assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!"); |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3327 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3328 | // At this point, we know we have a conditional branch that determines whether |
| 3329 | // the loop is exited. However, we don't know if the branch is executed each |
| 3330 | // time through the loop. If not, then the execution count of the branch will |
| 3331 | // not be equal to the trip count of the loop. |
| 3332 | // |
| 3333 | // Currently we check for this by checking to see if the Exit branch goes to |
| 3334 | // the loop header. If so, we know it will always execute the same number of |
| 3335 | // times as the loop. We also handle the case where the exit block *is* the |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3336 | // loop header. This is common for un-rotated loops. |
| 3337 | // |
| 3338 | // If both of those tests fail, walk up the unique predecessor chain to the |
| 3339 | // header, stopping if there is an edge that doesn't exit the loop. If the |
| 3340 | // header is reached, the execution count of the branch will be equal to the |
| 3341 | // trip count of the loop. |
| 3342 | // |
| 3343 | // More extensive analysis could be done to handle more cases here. |
| 3344 | // |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3345 | if (ExitBr->getSuccessor(0) != L->getHeader() && |
| 3346 | ExitBr->getSuccessor(1) != L->getHeader() && |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3347 | ExitBr->getParent() != L->getHeader()) { |
| 3348 | // The simple checks failed, try climbing the unique predecessor chain |
| 3349 | // up to the header. |
| 3350 | bool Ok = false; |
| 3351 | for (BasicBlock *BB = ExitBr->getParent(); BB; ) { |
| 3352 | BasicBlock *Pred = BB->getUniquePredecessor(); |
| 3353 | if (!Pred) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3354 | return getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3355 | TerminatorInst *PredTerm = Pred->getTerminator(); |
| 3356 | for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) { |
| 3357 | BasicBlock *PredSucc = PredTerm->getSuccessor(i); |
| 3358 | if (PredSucc == BB) |
| 3359 | continue; |
| 3360 | // If the predecessor has a successor that isn't BB and isn't |
| 3361 | // outside the loop, assume the worst. |
| 3362 | if (L->contains(PredSucc)) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3363 | return getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3364 | } |
| 3365 | if (Pred == L->getHeader()) { |
| 3366 | Ok = true; |
| 3367 | break; |
| 3368 | } |
| 3369 | BB = Pred; |
| 3370 | } |
| 3371 | if (!Ok) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3372 | return getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3373 | } |
| 3374 | |
| 3375 | // Procede to the next level to examine the exit condition expression. |
| 3376 | return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(), |
| 3377 | ExitBr->getSuccessor(0), |
| 3378 | ExitBr->getSuccessor(1)); |
| 3379 | } |
| 3380 | |
| 3381 | /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the |
| 3382 | /// backedge of the specified loop will execute if its exit condition |
| 3383 | /// were a conditional branch of ExitCond, TBB, and FBB. |
| 3384 | ScalarEvolution::BackedgeTakenInfo |
| 3385 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L, |
| 3386 | Value *ExitCond, |
| 3387 | BasicBlock *TBB, |
| 3388 | BasicBlock *FBB) { |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3389 | // Check if the controlling expression for this loop is an And or Or. |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3390 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) { |
| 3391 | if (BO->getOpcode() == Instruction::And) { |
| 3392 | // Recurse on the operands of the and. |
| 3393 | BackedgeTakenInfo BTI0 = |
| 3394 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 3395 | BackedgeTakenInfo BTI1 = |
| 3396 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3397 | const SCEV *BECount = getCouldNotCompute(); |
| 3398 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3399 | if (L->contains(TBB)) { |
| 3400 | // Both conditions must be true for the loop to continue executing. |
| 3401 | // Choose the less conservative count. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3402 | if (BTI0.Exact == getCouldNotCompute() || |
| 3403 | BTI1.Exact == getCouldNotCompute()) |
| 3404 | BECount = getCouldNotCompute(); |
Dan Gohman | ac958b3 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3405 | else |
| 3406 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3407 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3408 | MaxBECount = BTI1.Max; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3409 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3410 | MaxBECount = BTI0.Max; |
Dan Gohman | ac958b3 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3411 | else |
| 3412 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3413 | } else { |
| 3414 | // Both conditions must be true for the loop to exit. |
| 3415 | assert(L->contains(FBB) && "Loop block has no successor in loop!"); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3416 | if (BTI0.Exact != getCouldNotCompute() && |
| 3417 | BTI1.Exact != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3418 | BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3419 | if (BTI0.Max != getCouldNotCompute() && |
| 3420 | BTI1.Max != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3421 | MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max); |
| 3422 | } |
| 3423 | |
| 3424 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3425 | } |
| 3426 | if (BO->getOpcode() == Instruction::Or) { |
| 3427 | // Recurse on the operands of the or. |
| 3428 | BackedgeTakenInfo BTI0 = |
| 3429 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 3430 | BackedgeTakenInfo BTI1 = |
| 3431 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3432 | const SCEV *BECount = getCouldNotCompute(); |
| 3433 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3434 | if (L->contains(FBB)) { |
| 3435 | // Both conditions must be false for the loop to continue executing. |
| 3436 | // Choose the less conservative count. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3437 | if (BTI0.Exact == getCouldNotCompute() || |
| 3438 | BTI1.Exact == getCouldNotCompute()) |
| 3439 | BECount = getCouldNotCompute(); |
Dan Gohman | ac958b3 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3440 | else |
| 3441 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3442 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3443 | MaxBECount = BTI1.Max; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3444 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3445 | MaxBECount = BTI0.Max; |
Dan Gohman | ac958b3 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3446 | else |
| 3447 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3448 | } else { |
| 3449 | // Both conditions must be false for the loop to exit. |
| 3450 | assert(L->contains(TBB) && "Loop block has no successor in loop!"); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3451 | if (BTI0.Exact != getCouldNotCompute() && |
| 3452 | BTI1.Exact != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3453 | BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3454 | if (BTI0.Max != getCouldNotCompute() && |
| 3455 | BTI1.Max != getCouldNotCompute()) |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3456 | MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max); |
| 3457 | } |
| 3458 | |
| 3459 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3460 | } |
| 3461 | } |
| 3462 | |
| 3463 | // With an icmp, it may be feasible to compute an exact backedge-taken count. |
| 3464 | // Procede to the next level to examine the icmp. |
| 3465 | if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) |
| 3466 | return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3467 | |
Eli Friedman | 459d729 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3468 | // If it's not an integer or pointer comparison then compute it the hard way. |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3469 | return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
| 3470 | } |
| 3471 | |
| 3472 | /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the |
| 3473 | /// backedge of the specified loop will execute if its exit condition |
| 3474 | /// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB. |
| 3475 | ScalarEvolution::BackedgeTakenInfo |
| 3476 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L, |
| 3477 | ICmpInst *ExitCond, |
| 3478 | BasicBlock *TBB, |
| 3479 | BasicBlock *FBB) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3480 | |
| 3481 | // If the condition was exit on true, convert the condition to exit on false |
| 3482 | ICmpInst::Predicate Cond; |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3483 | if (!L->contains(FBB)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3484 | Cond = ExitCond->getPredicate(); |
| 3485 | else |
| 3486 | Cond = ExitCond->getInversePredicate(); |
| 3487 | |
| 3488 | // Handle common loops like: for (X = "string"; *X; ++X) |
| 3489 | if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0))) |
| 3490 | if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3491 | const SCEV *ItCnt = |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3492 | ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond); |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3493 | if (!isa<SCEVCouldNotCompute>(ItCnt)) { |
| 3494 | unsigned BitWidth = getTypeSizeInBits(ItCnt->getType()); |
| 3495 | return BackedgeTakenInfo(ItCnt, |
| 3496 | isa<SCEVConstant>(ItCnt) ? ItCnt : |
| 3497 | getConstant(APInt::getMaxValue(BitWidth)-1)); |
| 3498 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3499 | } |
| 3500 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3501 | const SCEV *LHS = getSCEV(ExitCond->getOperand(0)); |
| 3502 | const SCEV *RHS = getSCEV(ExitCond->getOperand(1)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3503 | |
| 3504 | // Try to evaluate any dependencies out of the loop. |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3505 | LHS = getSCEVAtScope(LHS, L); |
| 3506 | RHS = getSCEVAtScope(RHS, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3507 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3508 | // At this point, we would like to compute how many iterations of the |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3509 | // loop the predicate will return true for these inputs. |
Dan Gohman | 2d96e35 | 2008-09-16 18:52:57 +0000 | [diff] [blame] | 3510 | if (LHS->isLoopInvariant(L) && !RHS->isLoopInvariant(L)) { |
| 3511 | // If there is a loop-invariant, force it into the RHS. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3512 | std::swap(LHS, RHS); |
| 3513 | Cond = ICmpInst::getSwappedPredicate(Cond); |
| 3514 | } |
| 3515 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3516 | // If we have a comparison of a chrec against a constant, try to use value |
| 3517 | // ranges to answer this query. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3518 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) |
| 3519 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3520 | if (AddRec->getLoop() == L) { |
Eli Friedman | 459d729 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3521 | // Form the constant range. |
| 3522 | ConstantRange CompRange( |
| 3523 | ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue())); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3524 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3525 | const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this); |
Eli Friedman | 459d729 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3526 | if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
| 3529 | switch (Cond) { |
| 3530 | case ICmpInst::ICMP_NE: { // while (X != Y) |
| 3531 | // Convert to: while (X-Y != 0) |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3532 | const SCEV *TC = HowFarToZero(getMinusSCEV(LHS, RHS), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3533 | if (!isa<SCEVCouldNotCompute>(TC)) return TC; |
| 3534 | break; |
| 3535 | } |
| 3536 | case ICmpInst::ICMP_EQ: { |
| 3537 | // Convert to: while (X-Y == 0) // while (X == Y) |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3538 | const SCEV *TC = HowFarToNonZero(getMinusSCEV(LHS, RHS), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3539 | if (!isa<SCEVCouldNotCompute>(TC)) return TC; |
| 3540 | break; |
| 3541 | } |
| 3542 | case ICmpInst::ICMP_SLT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3543 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true); |
| 3544 | if (BTI.hasAnyInfo()) return BTI; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3545 | break; |
| 3546 | } |
| 3547 | case ICmpInst::ICMP_SGT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3548 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 3549 | getNotSCEV(RHS), L, true); |
| 3550 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | b7c2894 | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 3551 | break; |
| 3552 | } |
| 3553 | case ICmpInst::ICMP_ULT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3554 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false); |
| 3555 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | b7c2894 | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 3556 | break; |
| 3557 | } |
| 3558 | case ICmpInst::ICMP_UGT: { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3559 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 3560 | getNotSCEV(RHS), L, false); |
| 3561 | if (BTI.hasAnyInfo()) return BTI; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3562 | break; |
| 3563 | } |
| 3564 | default: |
| 3565 | #if 0 |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3566 | errs() << "ComputeBackedgeTakenCount "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3567 | if (ExitCond->getOperand(0)->getType()->isUnsigned()) |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3568 | errs() << "[unsigned] "; |
| 3569 | errs() << *LHS << " " |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3570 | << Instruction::getOpcodeName(Instruction::ICmp) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3571 | << " " << *RHS << "\n"; |
| 3572 | #endif |
| 3573 | break; |
| 3574 | } |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3575 | return |
Dan Gohman | 8e8b523 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3576 | ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
| 3579 | static ConstantInt * |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3580 | EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, |
| 3581 | ScalarEvolution &SE) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3582 | const SCEV *InVal = SE.getConstant(C); |
| 3583 | const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3584 | assert(isa<SCEVConstant>(Val) && |
| 3585 | "Evaluation of SCEV at constant didn't fold correctly?"); |
| 3586 | return cast<SCEVConstant>(Val)->getValue(); |
| 3587 | } |
| 3588 | |
| 3589 | /// GetAddressedElementFromGlobal - Given a global variable with an initializer |
| 3590 | /// and a GEP expression (missing the pointer index) indexing into it, return |
| 3591 | /// the addressed element of the initializer or null if the index expression is |
| 3592 | /// invalid. |
| 3593 | static Constant * |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3594 | GetAddressedElementFromGlobal(LLVMContext &Context, GlobalVariable *GV, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3595 | const std::vector<ConstantInt*> &Indices) { |
| 3596 | Constant *Init = GV->getInitializer(); |
| 3597 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) { |
| 3598 | uint64_t Idx = Indices[i]->getZExtValue(); |
| 3599 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) { |
| 3600 | assert(Idx < CS->getNumOperands() && "Bad struct index!"); |
| 3601 | Init = cast<Constant>(CS->getOperand(Idx)); |
| 3602 | } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) { |
| 3603 | if (Idx >= CA->getNumOperands()) return 0; // Bogus program |
| 3604 | Init = cast<Constant>(CA->getOperand(Idx)); |
| 3605 | } else if (isa<ConstantAggregateZero>(Init)) { |
| 3606 | if (const StructType *STy = dyn_cast<StructType>(Init->getType())) { |
| 3607 | assert(Idx < STy->getNumElements() && "Bad struct index!"); |
Owen Anderson | aac2837 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3608 | Init = Constant::getNullValue(STy->getElementType(Idx)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3609 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) { |
| 3610 | if (Idx >= ATy->getNumElements()) return 0; // Bogus program |
Owen Anderson | aac2837 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3611 | Init = Constant::getNullValue(ATy->getElementType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3612 | } else { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3613 | llvm_unreachable("Unknown constant aggregate type!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3614 | } |
| 3615 | return 0; |
| 3616 | } else { |
| 3617 | return 0; // Unknown initializer type |
| 3618 | } |
| 3619 | } |
| 3620 | return Init; |
| 3621 | } |
| 3622 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3623 | /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of |
| 3624 | /// 'icmp op load X, cst', try to see if we can compute the backedge |
| 3625 | /// execution count. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3626 | const SCEV * |
| 3627 | ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount( |
| 3628 | LoadInst *LI, |
| 3629 | Constant *RHS, |
| 3630 | const Loop *L, |
| 3631 | ICmpInst::Predicate predicate) { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3632 | if (LI->isVolatile()) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3633 | |
| 3634 | // Check to see if the loaded pointer is a getelementptr of a global. |
| 3635 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3636 | if (!GEP) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3637 | |
| 3638 | // Make sure that it is really a constant global we are gepping, with an |
| 3639 | // initializer, and make sure the first IDX is really 0. |
| 3640 | GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); |
Dan Gohman | 5e423ed | 2009-08-19 18:20:44 +0000 | [diff] [blame^] | 3641 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() || |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3642 | GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) || |
| 3643 | !cast<Constant>(GEP->getOperand(1))->isNullValue()) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3644 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3645 | |
| 3646 | // Okay, we allow one non-constant index into the GEP instruction. |
| 3647 | Value *VarIdx = 0; |
| 3648 | std::vector<ConstantInt*> Indexes; |
| 3649 | unsigned VarIdxNum = 0; |
| 3650 | for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) |
| 3651 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 3652 | Indexes.push_back(CI); |
| 3653 | } else if (!isa<ConstantInt>(GEP->getOperand(i))) { |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3654 | if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3655 | VarIdx = GEP->getOperand(i); |
| 3656 | VarIdxNum = i-2; |
| 3657 | Indexes.push_back(0); |
| 3658 | } |
| 3659 | |
| 3660 | // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant. |
| 3661 | // Check to see if X is a loop variant variable value now. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3662 | const SCEV *Idx = getSCEV(VarIdx); |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3663 | Idx = getSCEVAtScope(Idx, L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3664 | |
| 3665 | // We can only recognize very limited forms of loop index expressions, in |
| 3666 | // particular, only affine AddRec's like {C1,+,C2}. |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3667 | const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3668 | if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) || |
| 3669 | !isa<SCEVConstant>(IdxExpr->getOperand(0)) || |
| 3670 | !isa<SCEVConstant>(IdxExpr->getOperand(1))) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3671 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3672 | |
| 3673 | unsigned MaxSteps = MaxBruteForceIterations; |
| 3674 | for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) { |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3675 | ConstantInt *ItCst = ConstantInt::get( |
Owen Anderson | 9f5b2aa | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 3676 | cast<IntegerType>(IdxExpr->getType()), IterationNum); |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3677 | ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3678 | |
| 3679 | // Form the GEP offset. |
| 3680 | Indexes[VarIdxNum] = Val; |
| 3681 | |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3682 | Constant *Result = GetAddressedElementFromGlobal(getContext(), GV, Indexes); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3683 | if (Result == 0) break; // Cannot compute! |
| 3684 | |
| 3685 | // Evaluate the condition for this iteration. |
| 3686 | Result = ConstantExpr::getICmp(predicate, Result, RHS); |
| 3687 | if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure |
| 3688 | if (cast<ConstantInt>(Result)->getValue().isMinValue()) { |
| 3689 | #if 0 |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3690 | errs() << "\n***\n*** Computed loop count " << *ItCst |
| 3691 | << "\n*** From global " << *GV << "*** BB: " << *L->getHeader() |
| 3692 | << "***\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3693 | #endif |
| 3694 | ++NumArrayLenItCounts; |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3695 | return getConstant(ItCst); // Found terminating iteration! |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3696 | } |
| 3697 | } |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3698 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | |
| 3702 | /// CanConstantFold - Return true if we can constant fold an instruction of the |
| 3703 | /// specified type, assuming that all operands were constants. |
| 3704 | static bool CanConstantFold(const Instruction *I) { |
| 3705 | if (isa<BinaryOperator>(I) || isa<CmpInst>(I) || |
| 3706 | isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I)) |
| 3707 | return true; |
| 3708 | |
| 3709 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 3710 | if (const Function *F = CI->getCalledFunction()) |
Dan Gohman | e6e001f | 2008-01-31 01:05:10 +0000 | [diff] [blame] | 3711 | return canConstantFoldCallTo(F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3712 | return false; |
| 3713 | } |
| 3714 | |
| 3715 | /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node |
| 3716 | /// in the loop that V is derived from. We allow arbitrary operations along the |
| 3717 | /// way, but the operands of an operation must either be constants or a value |
| 3718 | /// derived from a constant PHI. If this expression does not fit with these |
| 3719 | /// constraints, return null. |
| 3720 | static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) { |
| 3721 | // If this is not an instruction, or if this is an instruction outside of the |
| 3722 | // loop, it can't be derived from a loop PHI. |
| 3723 | Instruction *I = dyn_cast<Instruction>(V); |
| 3724 | if (I == 0 || !L->contains(I->getParent())) return 0; |
| 3725 | |
Anton Korobeynikov | 357a27d | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 3726 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3727 | if (L->getHeader() == I->getParent()) |
| 3728 | return PN; |
| 3729 | else |
| 3730 | // We don't currently keep track of the control flow needed to evaluate |
| 3731 | // PHIs, so we cannot handle PHIs inside of loops. |
| 3732 | return 0; |
Anton Korobeynikov | 357a27d | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 3733 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3734 | |
| 3735 | // If we won't be able to constant fold this expression even if the operands |
| 3736 | // are constants, return early. |
| 3737 | if (!CanConstantFold(I)) return 0; |
| 3738 | |
| 3739 | // Otherwise, we can evaluate this instruction if all of its operands are |
| 3740 | // constant or derived from a PHI node themselves. |
| 3741 | PHINode *PHI = 0; |
| 3742 | for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op) |
| 3743 | if (!(isa<Constant>(I->getOperand(Op)) || |
| 3744 | isa<GlobalValue>(I->getOperand(Op)))) { |
| 3745 | PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L); |
| 3746 | if (P == 0) return 0; // Not evolving from PHI |
| 3747 | if (PHI == 0) |
| 3748 | PHI = P; |
| 3749 | else if (PHI != P) |
| 3750 | return 0; // Evolving from multiple different PHIs. |
| 3751 | } |
| 3752 | |
| 3753 | // This is a expression evolving from a constant PHI! |
| 3754 | return PHI; |
| 3755 | } |
| 3756 | |
| 3757 | /// EvaluateExpression - Given an expression that passes the |
| 3758 | /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node |
| 3759 | /// in the loop has the value PHIVal. If we can't fold this expression for some |
| 3760 | /// reason, return null. |
| 3761 | static Constant *EvaluateExpression(Value *V, Constant *PHIVal) { |
| 3762 | if (isa<PHINode>(V)) return PHIVal; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3763 | if (Constant *C = dyn_cast<Constant>(V)) return C; |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3764 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3765 | Instruction *I = cast<Instruction>(V); |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3766 | LLVMContext &Context = I->getParent()->getContext(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3767 | |
| 3768 | std::vector<Constant*> Operands; |
| 3769 | Operands.resize(I->getNumOperands()); |
| 3770 | |
| 3771 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 3772 | Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal); |
| 3773 | if (Operands[i] == 0) return 0; |
| 3774 | } |
| 3775 | |
Chris Lattner | d6e5691 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3776 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 3777 | return ConstantFoldCompareInstOperands(CI->getPredicate(), |
Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 3778 | &Operands[0], Operands.size(), |
| 3779 | Context); |
Chris Lattner | d6e5691 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3780 | else |
| 3781 | return ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 3782 | &Operands[0], Operands.size(), |
| 3783 | Context); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
| 3786 | /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is |
| 3787 | /// in the header of its containing loop, we know the loop executes a |
| 3788 | /// constant number of times, and the PHI node is just a recurrence |
| 3789 | /// involving constants, fold it. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3790 | Constant * |
| 3791 | ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN, |
| 3792 | const APInt& BEs, |
| 3793 | const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3794 | std::map<PHINode*, Constant*>::iterator I = |
| 3795 | ConstantEvolutionLoopExitValue.find(PN); |
| 3796 | if (I != ConstantEvolutionLoopExitValue.end()) |
| 3797 | return I->second; |
| 3798 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3799 | if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations))) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3800 | return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it. |
| 3801 | |
| 3802 | Constant *&RetVal = ConstantEvolutionLoopExitValue[PN]; |
| 3803 | |
| 3804 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 3805 | // entry must be a constant (coming in from outside of the loop), and the |
| 3806 | // second must be derived from the same PHI. |
| 3807 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 3808 | Constant *StartCST = |
| 3809 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
| 3810 | if (StartCST == 0) |
| 3811 | return RetVal = 0; // Must be a constant. |
| 3812 | |
| 3813 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
| 3814 | PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); |
| 3815 | if (PN2 != PN) |
| 3816 | return RetVal = 0; // Not derived from same PHI. |
| 3817 | |
| 3818 | // Execute the loop symbolically to determine the exit value. |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3819 | if (BEs.getActiveBits() >= 32) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3820 | return RetVal = 0; // More than 2^32-1 iterations?? Not doing it! |
| 3821 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3822 | unsigned NumIterations = BEs.getZExtValue(); // must be in range |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3823 | unsigned IterationNum = 0; |
| 3824 | for (Constant *PHIVal = StartCST; ; ++IterationNum) { |
| 3825 | if (IterationNum == NumIterations) |
| 3826 | return RetVal = PHIVal; // Got exit value! |
| 3827 | |
| 3828 | // Compute the value of the PHI node for the next iteration. |
| 3829 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal); |
| 3830 | if (NextPHI == PHIVal) |
| 3831 | return RetVal = NextPHI; // Stopped evolving! |
| 3832 | if (NextPHI == 0) |
| 3833 | return 0; // Couldn't evaluate! |
| 3834 | PHIVal = NextPHI; |
| 3835 | } |
| 3836 | } |
| 3837 | |
Dan Gohman | c3f6034 | 2009-07-27 16:09:48 +0000 | [diff] [blame] | 3838 | /// ComputeBackedgeTakenCountExhaustively - If the loop is known to execute a |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3839 | /// constant number of times (the condition evolves only from constants), |
| 3840 | /// try to evaluate a few iterations of the loop until we get the exit |
| 3841 | /// condition gets a value of ExitWhen (true or false). If we cannot |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3842 | /// evaluate the trip count of the loop, return getCouldNotCompute(). |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3843 | const SCEV * |
| 3844 | ScalarEvolution::ComputeBackedgeTakenCountExhaustively(const Loop *L, |
| 3845 | Value *Cond, |
| 3846 | bool ExitWhen) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3847 | PHINode *PN = getConstantEvolvingPHI(Cond, L); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3848 | if (PN == 0) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3849 | |
| 3850 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 3851 | // entry must be a constant (coming in from outside of the loop), and the |
| 3852 | // second must be derived from the same PHI. |
| 3853 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 3854 | Constant *StartCST = |
| 3855 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3856 | if (StartCST == 0) return getCouldNotCompute(); // Must be a constant. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3857 | |
| 3858 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
| 3859 | PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3860 | if (PN2 != PN) return getCouldNotCompute(); // Not derived from same PHI. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3861 | |
| 3862 | // Okay, we find a PHI node that defines the trip count of this loop. Execute |
| 3863 | // the loop symbolically to determine when the condition gets a value of |
| 3864 | // "ExitWhen". |
| 3865 | unsigned IterationNum = 0; |
| 3866 | unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis. |
| 3867 | for (Constant *PHIVal = StartCST; |
| 3868 | IterationNum != MaxIterations; ++IterationNum) { |
| 3869 | ConstantInt *CondVal = |
| 3870 | dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal)); |
| 3871 | |
| 3872 | // Couldn't symbolically evaluate. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3873 | if (!CondVal) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3874 | |
| 3875 | if (CondVal->getValue() == uint64_t(ExitWhen)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3876 | ++NumBruteForceTripCountsComputed; |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3877 | return getConstant(Type::getInt32Ty(getContext()), IterationNum); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
| 3880 | // Compute the value of the PHI node for the next iteration. |
| 3881 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal); |
| 3882 | if (NextPHI == 0 || NextPHI == PHIVal) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3883 | return getCouldNotCompute();// Couldn't evaluate or not making progress... |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3884 | PHIVal = NextPHI; |
| 3885 | } |
| 3886 | |
| 3887 | // Too many iterations were needed to evaluate. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3888 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
Dan Gohman | dd40e9a | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 3891 | /// getSCEVAtScope - Return a SCEV expression handle for the specified value |
| 3892 | /// at the specified scope in the program. The L value specifies a loop |
| 3893 | /// nest to evaluate the expression at, where null is the top-level or a |
| 3894 | /// specified loop is immediately inside of the loop. |
| 3895 | /// |
| 3896 | /// This method can be used to compute the exit value for a variable defined |
| 3897 | /// in a loop by querying what the value will hold in the parent loop. |
| 3898 | /// |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3899 | /// In the case that a relevant loop exit value cannot be computed, the |
| 3900 | /// original value V is returned. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3901 | const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3902 | // FIXME: this should be turned into a virtual method on SCEV! |
| 3903 | |
| 3904 | if (isa<SCEVConstant>(V)) return V; |
| 3905 | |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 3906 | // If this instruction is evolved from a constant-evolving PHI, compute the |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3907 | // exit value from the loop without using SCEVs. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3908 | if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3909 | if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3910 | const Loop *LI = (*this->LI)[I->getParent()]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3911 | if (LI && LI->getParentLoop() == L) // Looking for loop exit value. |
| 3912 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3913 | if (PN->getParent() == LI->getHeader()) { |
| 3914 | // Okay, there is no closed form solution for the PHI node. Check |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3915 | // to see if the loop that contains it has a known backedge-taken |
| 3916 | // count. If so, we may be able to force computation of the exit |
| 3917 | // value. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3918 | const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3919 | if (const SCEVConstant *BTCC = |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3920 | dyn_cast<SCEVConstant>(BackedgeTakenCount)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3921 | // Okay, we know how many times the containing loop executes. If |
| 3922 | // this is a constant evolving PHI node, get the final value at |
| 3923 | // the specified iteration number. |
| 3924 | Constant *RV = getConstantEvolutionLoopExitValue(PN, |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3925 | BTCC->getValue()->getValue(), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3926 | LI); |
Dan Gohman | 652caf1 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3927 | if (RV) return getSCEV(RV); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3928 | } |
| 3929 | } |
| 3930 | |
| 3931 | // Okay, this is an expression that we cannot symbolically evaluate |
| 3932 | // into a SCEV. Check to see if it's possible to symbolically evaluate |
| 3933 | // the arguments into constants, and if so, try to constant propagate the |
| 3934 | // result. This is particularly useful for computing loop exit values. |
| 3935 | if (CanConstantFold(I)) { |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3936 | // Check to see if we've folded this instruction at this loop before. |
| 3937 | std::map<const Loop *, Constant *> &Values = ValuesAtScopes[I]; |
| 3938 | std::pair<std::map<const Loop *, Constant *>::iterator, bool> Pair = |
| 3939 | Values.insert(std::make_pair(L, static_cast<Constant *>(0))); |
| 3940 | if (!Pair.second) |
Dan Gohman | 652caf1 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3941 | return Pair.first->second ? &*getSCEV(Pair.first->second) : V; |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3942 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3943 | std::vector<Constant*> Operands; |
| 3944 | Operands.reserve(I->getNumOperands()); |
| 3945 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 3946 | Value *Op = I->getOperand(i); |
| 3947 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| 3948 | Operands.push_back(C); |
| 3949 | } else { |
Chris Lattner | 3fff464 | 2007-11-23 08:46:22 +0000 | [diff] [blame] | 3950 | // If any of the operands is non-constant and if they are |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3951 | // non-integer and non-pointer, don't even try to analyze them |
| 3952 | // with scev techniques. |
Dan Gohman | 5e4eb76 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3953 | if (!isSCEVable(Op->getType())) |
Chris Lattner | 3fff464 | 2007-11-23 08:46:22 +0000 | [diff] [blame] | 3954 | return V; |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3955 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3956 | const SCEV* OpV = getSCEVAtScope(Op, L); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3957 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) { |
Dan Gohman | 5e4eb76 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3958 | Constant *C = SC->getValue(); |
| 3959 | if (C->getType() != Op->getType()) |
| 3960 | C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 3961 | Op->getType(), |
| 3962 | false), |
| 3963 | C, Op->getType()); |
| 3964 | Operands.push_back(C); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3965 | } else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) { |
Dan Gohman | 5e4eb76 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3966 | if (Constant *C = dyn_cast<Constant>(SU->getValue())) { |
| 3967 | if (C->getType() != Op->getType()) |
| 3968 | C = |
| 3969 | ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 3970 | Op->getType(), |
| 3971 | false), |
| 3972 | C, Op->getType()); |
| 3973 | Operands.push_back(C); |
| 3974 | } else |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3975 | return V; |
| 3976 | } else { |
| 3977 | return V; |
| 3978 | } |
| 3979 | } |
| 3980 | } |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3981 | |
Chris Lattner | d6e5691 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3982 | Constant *C; |
| 3983 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 3984 | C = ConstantFoldCompareInstOperands(CI->getPredicate(), |
Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 3985 | &Operands[0], Operands.size(), |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3986 | getContext()); |
Chris Lattner | d6e5691 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3987 | else |
| 3988 | C = ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3989 | &Operands[0], Operands.size(), |
| 3990 | getContext()); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 3991 | Pair.first->second = C; |
Dan Gohman | 652caf1 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3992 | return getSCEV(C); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3993 | } |
| 3994 | } |
| 3995 | |
| 3996 | // This is some other type of SCEVUnknown, just return it. |
| 3997 | return V; |
| 3998 | } |
| 3999 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4000 | if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4001 | // Avoid performing the look-up in the common case where the specified |
| 4002 | // expression has no loop-variant portions. |
| 4003 | for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4004 | const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4005 | if (OpAtScope != Comm->getOperand(i)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4006 | // Okay, at least one of these operands is loop variant but might be |
| 4007 | // foldable. Build a new instance of the folded commutative expression. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4008 | SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(), |
| 4009 | Comm->op_begin()+i); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4010 | NewOps.push_back(OpAtScope); |
| 4011 | |
| 4012 | for (++i; i != e; ++i) { |
| 4013 | OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4014 | NewOps.push_back(OpAtScope); |
| 4015 | } |
| 4016 | if (isa<SCEVAddExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4017 | return getAddExpr(NewOps); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 4018 | if (isa<SCEVMulExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4019 | return getMulExpr(NewOps); |
Nick Lewycky | 711640a | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 4020 | if (isa<SCEVSMaxExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4021 | return getSMaxExpr(NewOps); |
Nick Lewycky | e7a24ff | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 4022 | if (isa<SCEVUMaxExpr>(Comm)) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4023 | return getUMaxExpr(NewOps); |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4024 | llvm_unreachable("Unknown commutative SCEV type!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4025 | } |
| 4026 | } |
| 4027 | // If we got here, all operands are loop invariant. |
| 4028 | return Comm; |
| 4029 | } |
| 4030 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4031 | if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4032 | const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L); |
| 4033 | const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L); |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 4034 | if (LHS == Div->getLHS() && RHS == Div->getRHS()) |
| 4035 | return Div; // must be loop invariant |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4036 | return getUDivExpr(LHS, RHS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4037 | } |
| 4038 | |
| 4039 | // If this is a loop recurrence for a loop that does not contain L, then we |
| 4040 | // are dealing with the final value computed by the loop. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4041 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4042 | if (!L || !AddRec->getLoop()->contains(L->getHeader())) { |
| 4043 | // To evaluate this recurrence, we need to know how many times the AddRec |
| 4044 | // loop iterates. Compute this now. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4045 | const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop()); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4046 | if (BackedgeTakenCount == getCouldNotCompute()) return AddRec; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4047 | |
Eli Friedman | 7489ec9 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 4048 | // Then, evaluate the AddRec. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4049 | return AddRec->evaluateAtIteration(BackedgeTakenCount, *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4050 | } |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 4051 | return AddRec; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4052 | } |
| 4053 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4054 | if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4055 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | 78d63c8 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4056 | if (Op == Cast->getOperand()) |
| 4057 | return Cast; // must be loop invariant |
| 4058 | return getZeroExtendExpr(Op, Cast->getType()); |
| 4059 | } |
| 4060 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4061 | if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4062 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | 78d63c8 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4063 | if (Op == Cast->getOperand()) |
| 4064 | return Cast; // must be loop invariant |
| 4065 | return getSignExtendExpr(Op, Cast->getType()); |
| 4066 | } |
| 4067 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4068 | if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4069 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | 78d63c8 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4070 | if (Op == Cast->getOperand()) |
| 4071 | return Cast; // must be loop invariant |
| 4072 | return getTruncateExpr(Op, Cast->getType()); |
| 4073 | } |
| 4074 | |
Dan Gohman | 14f74f5 | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 4075 | if (isa<SCEVTargetDataConstant>(V)) |
| 4076 | return V; |
| 4077 | |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4078 | llvm_unreachable("Unknown SCEV type!"); |
Daniel Dunbar | a95d96c | 2009-05-18 16:43:04 +0000 | [diff] [blame] | 4079 | return 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
Dan Gohman | dd40e9a | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 4082 | /// getSCEVAtScope - This is a convenience function which does |
| 4083 | /// getSCEVAtScope(getSCEV(V), L). |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4084 | const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4085 | return getSCEVAtScope(getSCEV(V), L); |
| 4086 | } |
| 4087 | |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4088 | /// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the |
| 4089 | /// following equation: |
| 4090 | /// |
| 4091 | /// A * X = B (mod N) |
| 4092 | /// |
| 4093 | /// where N = 2^BW and BW is the common bit width of A and B. The signedness of |
| 4094 | /// A and B isn't important. |
| 4095 | /// |
| 4096 | /// If the equation does not have a solution, SCEVCouldNotCompute is returned. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4097 | static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const APInt &B, |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4098 | ScalarEvolution &SE) { |
| 4099 | uint32_t BW = A.getBitWidth(); |
| 4100 | assert(BW == B.getBitWidth() && "Bit widths must be the same."); |
| 4101 | assert(A != 0 && "A must be non-zero."); |
| 4102 | |
| 4103 | // 1. D = gcd(A, N) |
| 4104 | // |
| 4105 | // The gcd of A and N may have only one prime factor: 2. The number of |
| 4106 | // trailing zeros in A is its multiplicity |
| 4107 | uint32_t Mult2 = A.countTrailingZeros(); |
| 4108 | // D = 2^Mult2 |
| 4109 | |
| 4110 | // 2. Check if B is divisible by D. |
| 4111 | // |
| 4112 | // B is divisible by D if and only if the multiplicity of prime factor 2 for B |
| 4113 | // is not less than multiplicity of this prime factor for D. |
| 4114 | if (B.countTrailingZeros() < Mult2) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4115 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4116 | |
| 4117 | // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic |
| 4118 | // modulo (N / D). |
| 4119 | // |
| 4120 | // (N / D) may need BW+1 bits in its representation. Hence, we'll use this |
| 4121 | // bit width during computations. |
| 4122 | APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D |
| 4123 | APInt Mod(BW + 1, 0); |
| 4124 | Mod.set(BW - Mult2); // Mod = N / D |
| 4125 | APInt I = AD.multiplicativeInverse(Mod); |
| 4126 | |
| 4127 | // 4. Compute the minimum unsigned root of the equation: |
| 4128 | // I * (B / D) mod (N / D) |
| 4129 | APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod); |
| 4130 | |
| 4131 | // The result is guaranteed to be less than 2^BW so we may truncate it to BW |
| 4132 | // bits. |
| 4133 | return SE.getConstant(Result.trunc(BW)); |
| 4134 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4135 | |
| 4136 | /// SolveQuadraticEquation - Find the roots of the quadratic equation for the |
| 4137 | /// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which |
| 4138 | /// might be the same) or two SCEVCouldNotCompute objects. |
| 4139 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4140 | static std::pair<const SCEV *,const SCEV *> |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4141 | SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4142 | assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4143 | const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0)); |
| 4144 | const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1)); |
| 4145 | const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4146 | |
| 4147 | // We currently can only solve this if the coefficients are constants. |
| 4148 | if (!LC || !MC || !NC) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4149 | const SCEV *CNC = SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4150 | return std::make_pair(CNC, CNC); |
| 4151 | } |
| 4152 | |
| 4153 | uint32_t BitWidth = LC->getValue()->getValue().getBitWidth(); |
| 4154 | const APInt &L = LC->getValue()->getValue(); |
| 4155 | const APInt &M = MC->getValue()->getValue(); |
| 4156 | const APInt &N = NC->getValue()->getValue(); |
| 4157 | APInt Two(BitWidth, 2); |
| 4158 | APInt Four(BitWidth, 4); |
| 4159 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4160 | { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4161 | using namespace APIntOps; |
| 4162 | const APInt& C = L; |
| 4163 | // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C |
| 4164 | // The B coefficient is M-N/2 |
| 4165 | APInt B(M); |
| 4166 | B -= sdiv(N,Two); |
| 4167 | |
| 4168 | // The A coefficient is N/2 |
| 4169 | APInt A(N.sdiv(Two)); |
| 4170 | |
| 4171 | // Compute the B^2-4ac term. |
| 4172 | APInt SqrtTerm(B); |
| 4173 | SqrtTerm *= B; |
| 4174 | SqrtTerm -= Four * (A * C); |
| 4175 | |
| 4176 | // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest |
| 4177 | // integer value or else APInt::sqrt() will assert. |
| 4178 | APInt SqrtVal(SqrtTerm.sqrt()); |
| 4179 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4180 | // Compute the two solutions for the quadratic formula. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4181 | // The divisions must be performed as signed divisions. |
| 4182 | APInt NegB(-B); |
| 4183 | APInt TwoA( A << 1 ); |
Nick Lewycky | 3577669 | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 4184 | if (TwoA.isMinValue()) { |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4185 | const SCEV *CNC = SE.getCouldNotCompute(); |
Nick Lewycky | 3577669 | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 4186 | return std::make_pair(CNC, CNC); |
| 4187 | } |
| 4188 | |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 4189 | LLVMContext &Context = SE.getContext(); |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4190 | |
| 4191 | ConstantInt *Solution1 = |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4192 | ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA)); |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4193 | ConstantInt *Solution2 = |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4194 | ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4195 | |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4196 | return std::make_pair(SE.getConstant(Solution1), |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4197 | SE.getConstant(Solution2)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4198 | } // end APIntOps namespace |
| 4199 | } |
| 4200 | |
| 4201 | /// HowFarToZero - Return the number of times a backedge comparing the specified |
Dan Gohman | 0c85091 | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4202 | /// value to zero will execute. If not computable, return CouldNotCompute. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4203 | const SCEV *ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4204 | // If the value is a constant |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4205 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4206 | // If the value is already zero, the branch will execute zero times. |
| 4207 | if (C->getValue()->isZero()) return C; |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4208 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4211 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4212 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4213 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4214 | |
| 4215 | if (AddRec->isAffine()) { |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4216 | // If this is an affine expression, the execution count of this branch is |
| 4217 | // the minimum unsigned root of the following equation: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4218 | // |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4219 | // Start + Step*N = 0 (mod 2^BW) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4220 | // |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4221 | // equivalent to: |
| 4222 | // |
| 4223 | // Step*N = -Start (mod 2^BW) |
| 4224 | // |
| 4225 | // where BW is the common bit width of Start and Step. |
| 4226 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4227 | // Get the initial value for the loop. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4228 | const SCEV *Start = getSCEVAtScope(AddRec->getStart(), |
| 4229 | L->getParentLoop()); |
| 4230 | const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), |
| 4231 | L->getParentLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4232 | |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4233 | if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) { |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4234 | // For now we handle only constant steps. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4235 | |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4236 | // First, handle unitary steps. |
| 4237 | if (StepC->getValue()->equalsInt(1)) // 1*N = -Start (mod 2^BW), so: |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4238 | return getNegativeSCEV(Start); // N = -Start (as unsigned) |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4239 | if (StepC->getValue()->isAllOnesValue()) // -1*N = -Start (mod 2^BW), so: |
| 4240 | return Start; // N = Start (as unsigned) |
| 4241 | |
| 4242 | // Then, try to solve the above equation provided that Start is constant. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4243 | if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) |
Wojciech Matyjewicz | 961b34c | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4244 | return SolveLinEquationWithOverflow(StepC->getValue()->getValue(), |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4245 | -StartC->getValue()->getValue(), |
| 4246 | *this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4247 | } |
| 4248 | } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) { |
| 4249 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of |
| 4250 | // the quadratic equation to solve it. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4251 | std::pair<const SCEV *,const SCEV *> Roots = SolveQuadraticEquation(AddRec, |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4252 | *this); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4253 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 4254 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4255 | if (R1) { |
| 4256 | #if 0 |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 4257 | errs() << "HFTZ: " << *V << " - sol#1: " << *R1 |
| 4258 | << " sol#2: " << *R2 << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4259 | #endif |
| 4260 | // Pick the smallest positive root value. |
| 4261 | if (ConstantInt *CB = |
Owen Anderson | 02b48c3 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 4262 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4263 | R1->getValue(), R2->getValue()))) { |
| 4264 | if (CB->getZExtValue() == false) |
| 4265 | std::swap(R1, R2); // R1 is the minimum root now. |
| 4266 | |
| 4267 | // We can only use this value if the chrec ends up with an exact zero |
| 4268 | // value at this index. When solving for "X*X != 5", for example, we |
| 4269 | // should not accept a root of 2. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4270 | const SCEV *Val = AddRec->evaluateAtIteration(R1, *this); |
Dan Gohman | 7b560c4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 4271 | if (Val->isZero()) |
| 4272 | return R1; // We found a quadratic root! |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4273 | } |
| 4274 | } |
| 4275 | } |
| 4276 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4277 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4278 | } |
| 4279 | |
| 4280 | /// HowFarToNonZero - Return the number of times a backedge checking the |
| 4281 | /// specified value for nonzero will execute. If not computable, return |
Dan Gohman | 0c85091 | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4282 | /// CouldNotCompute |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4283 | const SCEV *ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4284 | // Loops that look like: while (X == 0) are very strange indeed. We don't |
| 4285 | // handle them yet except for the trivial case. This could be expanded in the |
| 4286 | // future as needed. |
| 4287 | |
| 4288 | // If the value is a constant, check to see if it is known to be non-zero |
| 4289 | // already. If so, the backedge will execute zero times. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4290 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Nick Lewycky | f680518 | 2008-02-21 09:14:53 +0000 | [diff] [blame] | 4291 | if (!C->getValue()->isNullValue()) |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4292 | return getIntegerSCEV(0, C->getType()); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4293 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4294 | } |
| 4295 | |
| 4296 | // We could implement others, but I really doubt anyone writes loops like |
| 4297 | // this, and if they did, they would already be constant folded. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4298 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4299 | } |
| 4300 | |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4301 | /// getLoopPredecessor - If the given loop's header has exactly one unique |
| 4302 | /// predecessor outside the loop, return it. Otherwise return null. |
| 4303 | /// |
| 4304 | BasicBlock *ScalarEvolution::getLoopPredecessor(const Loop *L) { |
| 4305 | BasicBlock *Header = L->getHeader(); |
| 4306 | BasicBlock *Pred = 0; |
| 4307 | for (pred_iterator PI = pred_begin(Header), E = pred_end(Header); |
| 4308 | PI != E; ++PI) |
| 4309 | if (!L->contains(*PI)) { |
| 4310 | if (Pred && Pred != *PI) return 0; // Multiple predecessors. |
| 4311 | Pred = *PI; |
| 4312 | } |
| 4313 | return Pred; |
| 4314 | } |
| 4315 | |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4316 | /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB |
| 4317 | /// (which may not be an immediate predecessor) which has exactly one |
| 4318 | /// successor from which BB is reachable, or null if no such block is |
| 4319 | /// found. |
| 4320 | /// |
| 4321 | BasicBlock * |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4322 | ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) { |
Dan Gohman | 1116ea7 | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 4323 | // If the block has a unique predecessor, then there is no path from the |
| 4324 | // predecessor to the block that does not go through the direct edge |
| 4325 | // from the predecessor to the block. |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4326 | if (BasicBlock *Pred = BB->getSinglePredecessor()) |
| 4327 | return Pred; |
| 4328 | |
| 4329 | // A loop's header is defined to be a block that dominates the loop. |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4330 | // If the header has a unique predecessor outside the loop, it must be |
| 4331 | // a block that has exactly one successor that can reach the loop. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4332 | if (Loop *L = LI->getLoopFor(BB)) |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4333 | return getLoopPredecessor(L); |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4334 | |
| 4335 | return 0; |
| 4336 | } |
| 4337 | |
Dan Gohman | bc1e347 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4338 | /// HasSameValue - SCEV structural equivalence is usually sufficient for |
| 4339 | /// testing whether two expressions are equal, however for the purposes of |
| 4340 | /// looking for a condition guarding a loop, it can be useful to be a little |
| 4341 | /// more general, since a front-end may have replicated the controlling |
| 4342 | /// expression. |
| 4343 | /// |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4344 | static bool HasSameValue(const SCEV *A, const SCEV *B) { |
Dan Gohman | bc1e347 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4345 | // Quick check to see if they are the same SCEV. |
| 4346 | if (A == B) return true; |
| 4347 | |
| 4348 | // Otherwise, if they're both SCEVUnknown, it's possible that they hold |
| 4349 | // two different instructions with the same value. Check for this case. |
| 4350 | if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A)) |
| 4351 | if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B)) |
| 4352 | if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue())) |
| 4353 | if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue())) |
| 4354 | if (AI->isIdenticalTo(BI)) |
| 4355 | return true; |
| 4356 | |
| 4357 | // Otherwise assume they may have a different value. |
| 4358 | return false; |
| 4359 | } |
| 4360 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4361 | bool ScalarEvolution::isKnownNegative(const SCEV *S) { |
| 4362 | return getSignedRange(S).getSignedMax().isNegative(); |
| 4363 | } |
| 4364 | |
| 4365 | bool ScalarEvolution::isKnownPositive(const SCEV *S) { |
| 4366 | return getSignedRange(S).getSignedMin().isStrictlyPositive(); |
| 4367 | } |
| 4368 | |
| 4369 | bool ScalarEvolution::isKnownNonNegative(const SCEV *S) { |
| 4370 | return !getSignedRange(S).getSignedMin().isNegative(); |
| 4371 | } |
| 4372 | |
| 4373 | bool ScalarEvolution::isKnownNonPositive(const SCEV *S) { |
| 4374 | return !getSignedRange(S).getSignedMax().isStrictlyPositive(); |
| 4375 | } |
| 4376 | |
| 4377 | bool ScalarEvolution::isKnownNonZero(const SCEV *S) { |
| 4378 | return isKnownNegative(S) || isKnownPositive(S); |
| 4379 | } |
| 4380 | |
| 4381 | bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred, |
| 4382 | const SCEV *LHS, const SCEV *RHS) { |
| 4383 | |
| 4384 | if (HasSameValue(LHS, RHS)) |
| 4385 | return ICmpInst::isTrueWhenEqual(Pred); |
| 4386 | |
| 4387 | switch (Pred) { |
| 4388 | default: |
Dan Gohman | 2d4f5b1 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4389 | llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4390 | break; |
| 4391 | case ICmpInst::ICMP_SGT: |
| 4392 | Pred = ICmpInst::ICMP_SLT; |
| 4393 | std::swap(LHS, RHS); |
| 4394 | case ICmpInst::ICMP_SLT: { |
| 4395 | ConstantRange LHSRange = getSignedRange(LHS); |
| 4396 | ConstantRange RHSRange = getSignedRange(RHS); |
| 4397 | if (LHSRange.getSignedMax().slt(RHSRange.getSignedMin())) |
| 4398 | return true; |
| 4399 | if (LHSRange.getSignedMin().sge(RHSRange.getSignedMax())) |
| 4400 | return false; |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4401 | break; |
| 4402 | } |
| 4403 | case ICmpInst::ICMP_SGE: |
| 4404 | Pred = ICmpInst::ICMP_SLE; |
| 4405 | std::swap(LHS, RHS); |
| 4406 | case ICmpInst::ICMP_SLE: { |
| 4407 | ConstantRange LHSRange = getSignedRange(LHS); |
| 4408 | ConstantRange RHSRange = getSignedRange(RHS); |
| 4409 | if (LHSRange.getSignedMax().sle(RHSRange.getSignedMin())) |
| 4410 | return true; |
| 4411 | if (LHSRange.getSignedMin().sgt(RHSRange.getSignedMax())) |
| 4412 | return false; |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4413 | break; |
| 4414 | } |
| 4415 | case ICmpInst::ICMP_UGT: |
| 4416 | Pred = ICmpInst::ICMP_ULT; |
| 4417 | std::swap(LHS, RHS); |
| 4418 | case ICmpInst::ICMP_ULT: { |
| 4419 | ConstantRange LHSRange = getUnsignedRange(LHS); |
| 4420 | ConstantRange RHSRange = getUnsignedRange(RHS); |
| 4421 | if (LHSRange.getUnsignedMax().ult(RHSRange.getUnsignedMin())) |
| 4422 | return true; |
| 4423 | if (LHSRange.getUnsignedMin().uge(RHSRange.getUnsignedMax())) |
| 4424 | return false; |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4425 | break; |
| 4426 | } |
| 4427 | case ICmpInst::ICMP_UGE: |
| 4428 | Pred = ICmpInst::ICMP_ULE; |
| 4429 | std::swap(LHS, RHS); |
| 4430 | case ICmpInst::ICMP_ULE: { |
| 4431 | ConstantRange LHSRange = getUnsignedRange(LHS); |
| 4432 | ConstantRange RHSRange = getUnsignedRange(RHS); |
| 4433 | if (LHSRange.getUnsignedMax().ule(RHSRange.getUnsignedMin())) |
| 4434 | return true; |
| 4435 | if (LHSRange.getUnsignedMin().ugt(RHSRange.getUnsignedMax())) |
| 4436 | return false; |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4437 | break; |
| 4438 | } |
| 4439 | case ICmpInst::ICMP_NE: { |
| 4440 | if (getUnsignedRange(LHS).intersectWith(getUnsignedRange(RHS)).isEmptySet()) |
| 4441 | return true; |
| 4442 | if (getSignedRange(LHS).intersectWith(getSignedRange(RHS)).isEmptySet()) |
| 4443 | return true; |
| 4444 | |
| 4445 | const SCEV *Diff = getMinusSCEV(LHS, RHS); |
| 4446 | if (isKnownNonZero(Diff)) |
| 4447 | return true; |
| 4448 | break; |
| 4449 | } |
| 4450 | case ICmpInst::ICMP_EQ: |
Dan Gohman | 44e675f | 2009-07-20 23:54:43 +0000 | [diff] [blame] | 4451 | // The check at the top of the function catches the case where |
| 4452 | // the values are known to be equal. |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4453 | break; |
| 4454 | } |
| 4455 | return false; |
| 4456 | } |
| 4457 | |
| 4458 | /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is |
| 4459 | /// protected by a conditional between LHS and RHS. This is used to |
| 4460 | /// to eliminate casts. |
| 4461 | bool |
| 4462 | ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L, |
| 4463 | ICmpInst::Predicate Pred, |
| 4464 | const SCEV *LHS, const SCEV *RHS) { |
| 4465 | // Interpret a null as meaning no loop, where there is obviously no guard |
| 4466 | // (interprocedural conditions notwithstanding). |
| 4467 | if (!L) return true; |
| 4468 | |
| 4469 | BasicBlock *Latch = L->getLoopLatch(); |
| 4470 | if (!Latch) |
| 4471 | return false; |
| 4472 | |
| 4473 | BranchInst *LoopContinuePredicate = |
| 4474 | dyn_cast<BranchInst>(Latch->getTerminator()); |
| 4475 | if (!LoopContinuePredicate || |
| 4476 | LoopContinuePredicate->isUnconditional()) |
| 4477 | return false; |
| 4478 | |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4479 | return isImpliedCond(LoopContinuePredicate->getCondition(), Pred, LHS, RHS, |
| 4480 | LoopContinuePredicate->getSuccessor(0) != L->getHeader()); |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
| 4483 | /// isLoopGuardedByCond - Test whether entry to the loop is protected |
| 4484 | /// by a conditional between LHS and RHS. This is used to help avoid max |
| 4485 | /// expressions in loop trip counts, and to eliminate casts. |
| 4486 | bool |
| 4487 | ScalarEvolution::isLoopGuardedByCond(const Loop *L, |
| 4488 | ICmpInst::Predicate Pred, |
| 4489 | const SCEV *LHS, const SCEV *RHS) { |
Dan Gohman | 8b93818 | 2009-05-18 16:03:58 +0000 | [diff] [blame] | 4490 | // Interpret a null as meaning no loop, where there is obviously no guard |
| 4491 | // (interprocedural conditions notwithstanding). |
| 4492 | if (!L) return false; |
| 4493 | |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4494 | BasicBlock *Predecessor = getLoopPredecessor(L); |
| 4495 | BasicBlock *PredecessorDest = L->getHeader(); |
Nick Lewycky | 1b020bf | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4496 | |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4497 | // Starting at the loop predecessor, climb up the predecessor chain, as long |
| 4498 | // as there are predecessors that can be found that have unique successors |
Dan Gohman | 1cddf97 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4499 | // leading to the original header. |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4500 | for (; Predecessor; |
| 4501 | PredecessorDest = Predecessor, |
| 4502 | Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) { |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4503 | |
| 4504 | BranchInst *LoopEntryPredicate = |
Dan Gohman | ab157b2 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4505 | dyn_cast<BranchInst>(Predecessor->getTerminator()); |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4506 | if (!LoopEntryPredicate || |
| 4507 | LoopEntryPredicate->isUnconditional()) |
| 4508 | continue; |
| 4509 | |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4510 | if (isImpliedCond(LoopEntryPredicate->getCondition(), Pred, LHS, RHS, |
| 4511 | LoopEntryPredicate->getSuccessor(0) != PredecessorDest)) |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4512 | return true; |
Nick Lewycky | 1b020bf | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4513 | } |
| 4514 | |
Dan Gohman | ab678fb | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4515 | return false; |
Nick Lewycky | 1b020bf | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4516 | } |
| 4517 | |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4518 | /// isImpliedCond - Test whether the condition described by Pred, LHS, |
| 4519 | /// and RHS is true whenever the given Cond value evaluates to true. |
| 4520 | bool ScalarEvolution::isImpliedCond(Value *CondValue, |
| 4521 | ICmpInst::Predicate Pred, |
| 4522 | const SCEV *LHS, const SCEV *RHS, |
| 4523 | bool Inverse) { |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4524 | // Recursivly handle And and Or conditions. |
| 4525 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CondValue)) { |
| 4526 | if (BO->getOpcode() == Instruction::And) { |
| 4527 | if (!Inverse) |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4528 | return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) || |
| 4529 | isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse); |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4530 | } else if (BO->getOpcode() == Instruction::Or) { |
| 4531 | if (Inverse) |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4532 | return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) || |
| 4533 | isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse); |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4534 | } |
| 4535 | } |
| 4536 | |
| 4537 | ICmpInst *ICI = dyn_cast<ICmpInst>(CondValue); |
| 4538 | if (!ICI) return false; |
| 4539 | |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4540 | // Bail if the ICmp's operands' types are wider than the needed type |
| 4541 | // before attempting to call getSCEV on them. This avoids infinite |
| 4542 | // recursion, since the analysis of widening casts can require loop |
| 4543 | // exit condition information for overflow checking, which would |
| 4544 | // lead back here. |
| 4545 | if (getTypeSizeInBits(LHS->getType()) < |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4546 | getTypeSizeInBits(ICI->getOperand(0)->getType())) |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4547 | return false; |
| 4548 | |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4549 | // Now that we found a conditional branch that dominates the loop, check to |
| 4550 | // see if it is the comparison we are looking for. |
| 4551 | ICmpInst::Predicate FoundPred; |
| 4552 | if (Inverse) |
| 4553 | FoundPred = ICI->getInversePredicate(); |
| 4554 | else |
| 4555 | FoundPred = ICI->getPredicate(); |
| 4556 | |
| 4557 | const SCEV *FoundLHS = getSCEV(ICI->getOperand(0)); |
| 4558 | const SCEV *FoundRHS = getSCEV(ICI->getOperand(1)); |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4559 | |
| 4560 | // Balance the types. The case where FoundLHS' type is wider than |
| 4561 | // LHS' type is checked for above. |
| 4562 | if (getTypeSizeInBits(LHS->getType()) > |
| 4563 | getTypeSizeInBits(FoundLHS->getType())) { |
| 4564 | if (CmpInst::isSigned(Pred)) { |
| 4565 | FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType()); |
| 4566 | FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType()); |
| 4567 | } else { |
| 4568 | FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType()); |
| 4569 | FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType()); |
| 4570 | } |
| 4571 | } |
| 4572 | |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4573 | // Canonicalize the query to match the way instcombine will have |
| 4574 | // canonicalized the comparison. |
| 4575 | // First, put a constant operand on the right. |
| 4576 | if (isa<SCEVConstant>(LHS)) { |
| 4577 | std::swap(LHS, RHS); |
| 4578 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4579 | } |
| 4580 | // Then, canonicalize comparisons with boundary cases. |
| 4581 | if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) { |
| 4582 | const APInt &RA = RC->getValue()->getValue(); |
| 4583 | switch (Pred) { |
| 4584 | default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
| 4585 | case ICmpInst::ICMP_EQ: |
| 4586 | case ICmpInst::ICMP_NE: |
| 4587 | break; |
| 4588 | case ICmpInst::ICMP_UGE: |
| 4589 | if ((RA - 1).isMinValue()) { |
| 4590 | Pred = ICmpInst::ICMP_NE; |
| 4591 | RHS = getConstant(RA - 1); |
| 4592 | break; |
| 4593 | } |
| 4594 | if (RA.isMaxValue()) { |
| 4595 | Pred = ICmpInst::ICMP_EQ; |
| 4596 | break; |
| 4597 | } |
| 4598 | if (RA.isMinValue()) return true; |
| 4599 | break; |
| 4600 | case ICmpInst::ICMP_ULE: |
| 4601 | if ((RA + 1).isMaxValue()) { |
| 4602 | Pred = ICmpInst::ICMP_NE; |
| 4603 | RHS = getConstant(RA + 1); |
| 4604 | break; |
| 4605 | } |
| 4606 | if (RA.isMinValue()) { |
| 4607 | Pred = ICmpInst::ICMP_EQ; |
| 4608 | break; |
| 4609 | } |
| 4610 | if (RA.isMaxValue()) return true; |
| 4611 | break; |
| 4612 | case ICmpInst::ICMP_SGE: |
| 4613 | if ((RA - 1).isMinSignedValue()) { |
| 4614 | Pred = ICmpInst::ICMP_NE; |
| 4615 | RHS = getConstant(RA - 1); |
| 4616 | break; |
| 4617 | } |
| 4618 | if (RA.isMaxSignedValue()) { |
| 4619 | Pred = ICmpInst::ICMP_EQ; |
| 4620 | break; |
| 4621 | } |
| 4622 | if (RA.isMinSignedValue()) return true; |
| 4623 | break; |
| 4624 | case ICmpInst::ICMP_SLE: |
| 4625 | if ((RA + 1).isMaxSignedValue()) { |
| 4626 | Pred = ICmpInst::ICMP_NE; |
| 4627 | RHS = getConstant(RA + 1); |
| 4628 | break; |
| 4629 | } |
| 4630 | if (RA.isMinSignedValue()) { |
| 4631 | Pred = ICmpInst::ICMP_EQ; |
| 4632 | break; |
| 4633 | } |
| 4634 | if (RA.isMaxSignedValue()) return true; |
| 4635 | break; |
| 4636 | case ICmpInst::ICMP_UGT: |
| 4637 | if (RA.isMinValue()) { |
| 4638 | Pred = ICmpInst::ICMP_NE; |
| 4639 | break; |
| 4640 | } |
| 4641 | if ((RA + 1).isMaxValue()) { |
| 4642 | Pred = ICmpInst::ICMP_EQ; |
| 4643 | RHS = getConstant(RA + 1); |
| 4644 | break; |
| 4645 | } |
| 4646 | if (RA.isMaxValue()) return false; |
| 4647 | break; |
| 4648 | case ICmpInst::ICMP_ULT: |
| 4649 | if (RA.isMaxValue()) { |
| 4650 | Pred = ICmpInst::ICMP_NE; |
| 4651 | break; |
| 4652 | } |
| 4653 | if ((RA - 1).isMinValue()) { |
| 4654 | Pred = ICmpInst::ICMP_EQ; |
| 4655 | RHS = getConstant(RA - 1); |
| 4656 | break; |
| 4657 | } |
| 4658 | if (RA.isMinValue()) return false; |
| 4659 | break; |
| 4660 | case ICmpInst::ICMP_SGT: |
| 4661 | if (RA.isMinSignedValue()) { |
| 4662 | Pred = ICmpInst::ICMP_NE; |
| 4663 | break; |
| 4664 | } |
| 4665 | if ((RA + 1).isMaxSignedValue()) { |
| 4666 | Pred = ICmpInst::ICMP_EQ; |
| 4667 | RHS = getConstant(RA + 1); |
| 4668 | break; |
| 4669 | } |
| 4670 | if (RA.isMaxSignedValue()) return false; |
| 4671 | break; |
| 4672 | case ICmpInst::ICMP_SLT: |
| 4673 | if (RA.isMaxSignedValue()) { |
| 4674 | Pred = ICmpInst::ICMP_NE; |
| 4675 | break; |
| 4676 | } |
| 4677 | if ((RA - 1).isMinSignedValue()) { |
| 4678 | Pred = ICmpInst::ICMP_EQ; |
| 4679 | RHS = getConstant(RA - 1); |
| 4680 | break; |
| 4681 | } |
| 4682 | if (RA.isMinSignedValue()) return false; |
| 4683 | break; |
| 4684 | } |
| 4685 | } |
| 4686 | |
| 4687 | // Check to see if we can make the LHS or RHS match. |
| 4688 | if (LHS == FoundRHS || RHS == FoundLHS) { |
| 4689 | if (isa<SCEVConstant>(RHS)) { |
| 4690 | std::swap(FoundLHS, FoundRHS); |
| 4691 | FoundPred = ICmpInst::getSwappedPredicate(FoundPred); |
| 4692 | } else { |
| 4693 | std::swap(LHS, RHS); |
| 4694 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4695 | } |
| 4696 | } |
| 4697 | |
| 4698 | // Check whether the found predicate is the same as the desired predicate. |
| 4699 | if (FoundPred == Pred) |
| 4700 | return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS); |
| 4701 | |
| 4702 | // Check whether swapping the found predicate makes it the same as the |
| 4703 | // desired predicate. |
| 4704 | if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) { |
| 4705 | if (isa<SCEVConstant>(RHS)) |
| 4706 | return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS); |
| 4707 | else |
| 4708 | return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred), |
| 4709 | RHS, LHS, FoundLHS, FoundRHS); |
| 4710 | } |
| 4711 | |
| 4712 | // Check whether the actual condition is beyond sufficient. |
| 4713 | if (FoundPred == ICmpInst::ICMP_EQ) |
| 4714 | if (ICmpInst::isTrueWhenEqual(Pred)) |
| 4715 | if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS)) |
| 4716 | return true; |
| 4717 | if (Pred == ICmpInst::ICMP_NE) |
| 4718 | if (!ICmpInst::isTrueWhenEqual(FoundPred)) |
| 4719 | if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS)) |
| 4720 | return true; |
| 4721 | |
| 4722 | // Otherwise assume the worst. |
| 4723 | return false; |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4726 | /// isImpliedCondOperands - Test whether the condition described by Pred, |
| 4727 | /// LHS, and RHS is true whenever the condition desribed by Pred, FoundLHS, |
| 4728 | /// and FoundRHS is true. |
| 4729 | bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred, |
| 4730 | const SCEV *LHS, const SCEV *RHS, |
| 4731 | const SCEV *FoundLHS, |
| 4732 | const SCEV *FoundRHS) { |
| 4733 | return isImpliedCondOperandsHelper(Pred, LHS, RHS, |
| 4734 | FoundLHS, FoundRHS) || |
| 4735 | // ~x < ~y --> x > y |
| 4736 | isImpliedCondOperandsHelper(Pred, LHS, RHS, |
| 4737 | getNotSCEV(FoundRHS), |
| 4738 | getNotSCEV(FoundLHS)); |
| 4739 | } |
| 4740 | |
| 4741 | /// isImpliedCondOperandsHelper - Test whether the condition described by |
| 4742 | /// Pred, LHS, and RHS is true whenever the condition desribed by Pred, |
| 4743 | /// FoundLHS, and FoundRHS is true. |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4744 | bool |
Dan Gohman | 920446d | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4745 | ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred, |
| 4746 | const SCEV *LHS, const SCEV *RHS, |
| 4747 | const SCEV *FoundLHS, |
| 4748 | const SCEV *FoundRHS) { |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4749 | switch (Pred) { |
Dan Gohman | 2d4f5b1 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4750 | default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
| 4751 | case ICmpInst::ICMP_EQ: |
| 4752 | case ICmpInst::ICMP_NE: |
| 4753 | if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS)) |
| 4754 | return true; |
| 4755 | break; |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4756 | case ICmpInst::ICMP_SLT: |
Dan Gohman | 2d4f5b1 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4757 | case ICmpInst::ICMP_SLE: |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4758 | if (isKnownPredicate(ICmpInst::ICMP_SLE, LHS, FoundLHS) && |
| 4759 | isKnownPredicate(ICmpInst::ICMP_SGE, RHS, FoundRHS)) |
| 4760 | return true; |
| 4761 | break; |
| 4762 | case ICmpInst::ICMP_SGT: |
Dan Gohman | 2d4f5b1 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4763 | case ICmpInst::ICMP_SGE: |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4764 | if (isKnownPredicate(ICmpInst::ICMP_SGE, LHS, FoundLHS) && |
| 4765 | isKnownPredicate(ICmpInst::ICMP_SLE, RHS, FoundRHS)) |
| 4766 | return true; |
| 4767 | break; |
| 4768 | case ICmpInst::ICMP_ULT: |
Dan Gohman | 2d4f5b1 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4769 | case ICmpInst::ICMP_ULE: |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4770 | if (isKnownPredicate(ICmpInst::ICMP_ULE, LHS, FoundLHS) && |
| 4771 | isKnownPredicate(ICmpInst::ICMP_UGE, RHS, FoundRHS)) |
| 4772 | return true; |
| 4773 | break; |
| 4774 | case ICmpInst::ICMP_UGT: |
Dan Gohman | 2d4f5b1 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4775 | case ICmpInst::ICMP_UGE: |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4776 | if (isKnownPredicate(ICmpInst::ICMP_UGE, LHS, FoundLHS) && |
| 4777 | isKnownPredicate(ICmpInst::ICMP_ULE, RHS, FoundRHS)) |
| 4778 | return true; |
| 4779 | break; |
| 4780 | } |
| 4781 | |
| 4782 | return false; |
Dan Gohman | 423ed6c | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4785 | /// getBECount - Subtract the end and start values and divide by the step, |
| 4786 | /// rounding up, to get the number of times the backedge is executed. Return |
| 4787 | /// CouldNotCompute if an intermediate computation overflows. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4788 | const SCEV *ScalarEvolution::getBECount(const SCEV *Start, |
Dan Gohman | 69eacc7 | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 4789 | const SCEV *End, |
| 4790 | const SCEV *Step) { |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4791 | const Type *Ty = Start->getType(); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4792 | const SCEV *NegOne = getIntegerSCEV(-1, Ty); |
| 4793 | const SCEV *Diff = getMinusSCEV(End, Start); |
| 4794 | const SCEV *RoundUp = getAddExpr(Step, NegOne); |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4795 | |
| 4796 | // Add an adjustment to the difference between End and Start so that |
| 4797 | // the division will effectively round up. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4798 | const SCEV *Add = getAddExpr(Diff, RoundUp); |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4799 | |
| 4800 | // Check Add for unsigned overflow. |
| 4801 | // TODO: More sophisticated things could be done here. |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4802 | const Type *WideTy = IntegerType::get(getContext(), |
| 4803 | getTypeSizeInBits(Ty) + 1); |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4804 | const SCEV *EDiff = getZeroExtendExpr(Diff, WideTy); |
| 4805 | const SCEV *ERoundUp = getZeroExtendExpr(RoundUp, WideTy); |
| 4806 | const SCEV *OperandExtendedAdd = getAddExpr(EDiff, ERoundUp); |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4807 | if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4808 | return getCouldNotCompute(); |
Dan Gohman | d2b62c4 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4809 | |
| 4810 | return getUDivExpr(Add, Step); |
| 4811 | } |
| 4812 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4813 | /// HowManyLessThans - Return the number of times a backedge containing the |
| 4814 | /// specified less-than comparison will execute. If not computable, return |
Dan Gohman | 0c85091 | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4815 | /// CouldNotCompute. |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4816 | ScalarEvolution::BackedgeTakenInfo |
| 4817 | ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS, |
| 4818 | const Loop *L, bool isSigned) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4819 | // Only handle: "ADDREC < LoopInvariant". |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4820 | if (!RHS->isLoopInvariant(L)) return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4821 | |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4822 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4823 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4824 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4825 | |
| 4826 | if (AddRec->isAffine()) { |
Nick Lewycky | 35b5602 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 4827 | // FORNOW: We only support unit strides. |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4828 | unsigned BitWidth = getTypeSizeInBits(AddRec->getType()); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4829 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4830 | |
| 4831 | // TODO: handle non-constant strides. |
| 4832 | const SCEVConstant *CStep = dyn_cast<SCEVConstant>(Step); |
| 4833 | if (!CStep || CStep->isZero()) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4834 | return getCouldNotCompute(); |
Dan Gohman | f8bc8e8 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 4835 | if (CStep->isOne()) { |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4836 | // With unit stride, the iteration never steps past the limit value. |
| 4837 | } else if (CStep->getValue()->getValue().isStrictlyPositive()) { |
| 4838 | if (const SCEVConstant *CLimit = dyn_cast<SCEVConstant>(RHS)) { |
| 4839 | // Test whether a positive iteration iteration can step past the limit |
| 4840 | // value and past the maximum value for its type in a single step. |
| 4841 | if (isSigned) { |
| 4842 | APInt Max = APInt::getSignedMaxValue(BitWidth); |
| 4843 | if ((Max - CStep->getValue()->getValue()) |
| 4844 | .slt(CLimit->getValue()->getValue())) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4845 | return getCouldNotCompute(); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4846 | } else { |
| 4847 | APInt Max = APInt::getMaxValue(BitWidth); |
| 4848 | if ((Max - CStep->getValue()->getValue()) |
| 4849 | .ult(CLimit->getValue()->getValue())) |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4850 | return getCouldNotCompute(); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4851 | } |
| 4852 | } else |
| 4853 | // TODO: handle non-constant limit values below. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4854 | return getCouldNotCompute(); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4855 | } else |
| 4856 | // TODO: handle negative strides below. |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4857 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4858 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4859 | // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant |
| 4860 | // m. So, we count the number of iterations in which {n,+,s} < m is true. |
| 4861 | // Note that we cannot simply return max(m-n,0)/s because it's not safe to |
Wojciech Matyjewicz | 1377a54 | 2008-02-13 12:21:32 +0000 | [diff] [blame] | 4862 | // treat m-n as signed nor unsigned due to overflow possibility. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4863 | |
Wojciech Matyjewicz | ebc77b1 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4864 | // First, we get the value of the LHS in the first iteration: n |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4865 | const SCEV *Start = AddRec->getOperand(0); |
Wojciech Matyjewicz | ebc77b1 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4866 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4867 | // Determine the minimum constant start value. |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4868 | const SCEV *MinStart = getConstant(isSigned ? |
| 4869 | getSignedRange(Start).getSignedMin() : |
| 4870 | getUnsignedRange(Start).getUnsignedMin()); |
Wojciech Matyjewicz | ebc77b1 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4871 | |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4872 | // If we know that the condition is true in order to enter the loop, |
| 4873 | // then we know that it will run exactly (m-n)/s times. Otherwise, we |
Dan Gohman | c8a2927 | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 4874 | // only know that it will execute (max(m,n)-n)/s times. In both cases, |
| 4875 | // the division must round up. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4876 | const SCEV *End = RHS; |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4877 | if (!isLoopGuardedByCond(L, |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4878 | isSigned ? ICmpInst::ICMP_SLT : |
| 4879 | ICmpInst::ICMP_ULT, |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4880 | getMinusSCEV(Start, Step), RHS)) |
| 4881 | End = isSigned ? getSMaxExpr(RHS, Start) |
| 4882 | : getUMaxExpr(RHS, Start); |
| 4883 | |
| 4884 | // Determine the maximum constant end value. |
Dan Gohman | 55e2d7e | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4885 | const SCEV *MaxEnd = getConstant(isSigned ? |
| 4886 | getSignedRange(End).getSignedMax() : |
| 4887 | getUnsignedRange(End).getUnsignedMax()); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4888 | |
| 4889 | // Finally, we subtract these two values and divide, rounding up, to get |
| 4890 | // the number of times the backedge is executed. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4891 | const SCEV *BECount = getBECount(Start, End, Step); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4892 | |
| 4893 | // The maximum backedge count is similar, except using the minimum start |
| 4894 | // value and the maximum end value. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4895 | const SCEV *MaxBECount = getBECount(MinStart, MaxEnd, Step); |
Dan Gohman | f7d3d2554 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4896 | |
| 4897 | return BackedgeTakenInfo(BECount, MaxBECount); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4898 | } |
| 4899 | |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4900 | return getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4901 | } |
| 4902 | |
| 4903 | /// getNumIterationsInRange - Return the number of iterations of this loop that |
| 4904 | /// produce values in the specified constant range. Another way of looking at |
| 4905 | /// this is that it returns the first iteration number where the value is not in |
| 4906 | /// the condition, thus computing the exit count. If the iteration count can't |
| 4907 | /// be computed, an instance of SCEVCouldNotCompute is returned. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4908 | const SCEV *SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4909 | ScalarEvolution &SE) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4910 | if (Range.isFullSet()) // Infinite loop. |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4911 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4912 | |
| 4913 | // If the start is a non-zero constant, shift the range to simplify things. |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4914 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4915 | if (!SC->getValue()->isZero()) { |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4916 | SmallVector<const SCEV *, 4> Operands(op_begin(), op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4917 | Operands[0] = SE.getIntegerSCEV(0, SC->getType()); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4918 | const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop()); |
Dan Gohman | c76b545 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4919 | if (const SCEVAddRecExpr *ShiftedAddRec = |
| 4920 | dyn_cast<SCEVAddRecExpr>(Shifted)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4921 | return ShiftedAddRec->getNumIterationsInRange( |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4922 | Range.subtract(SC->getValue()->getValue()), SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4923 | // This is strange and shouldn't happen. |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4924 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4925 | } |
| 4926 | |
| 4927 | // The only time we can solve this is when we have all constant indices. |
| 4928 | // Otherwise, we cannot determine the overflow conditions. |
| 4929 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 4930 | if (!isa<SCEVConstant>(getOperand(i))) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4931 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4932 | |
| 4933 | |
| 4934 | // Okay at this point we know that all elements of the chrec are constants and |
| 4935 | // that the start element is zero. |
| 4936 | |
| 4937 | // First check to see if the range contains zero. If not, the first |
| 4938 | // iteration exits. |
Dan Gohman | b98c1a3 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 4939 | unsigned BitWidth = SE.getTypeSizeInBits(getType()); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4940 | if (!Range.contains(APInt(BitWidth, 0))) |
Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 4941 | return SE.getIntegerSCEV(0, getType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4942 | |
| 4943 | if (isAffine()) { |
| 4944 | // If this is an affine expression then we have this situation: |
| 4945 | // Solve {0,+,A} in Range === Ax in Range |
| 4946 | |
| 4947 | // We know that zero is in the range. If A is positive then we know that |
| 4948 | // the upper value of the range must be the first possible exit value. |
| 4949 | // If A is negative then the lower of the range is the last possible loop |
| 4950 | // value. Also note that we already checked for a full range. |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4951 | APInt One(BitWidth,1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4952 | APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue(); |
| 4953 | APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower(); |
| 4954 | |
| 4955 | // The exit value should be (End+A)/A. |
Nick Lewycky | a0facae | 2007-09-27 14:12:54 +0000 | [diff] [blame] | 4956 | APInt ExitVal = (End + A).udiv(A); |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4957 | ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4958 | |
| 4959 | // Evaluate at the exit value. If we really did fall out of the valid |
| 4960 | // range, then we computed our trip count, otherwise wrap around or other |
| 4961 | // things must have happened. |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4962 | ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4963 | if (Range.contains(Val->getValue())) |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4964 | return SE.getCouldNotCompute(); // Something strange happened |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4965 | |
| 4966 | // Ensure that the previous value is in the range. This is a sanity check. |
| 4967 | assert(Range.contains( |
Dan Gohman | 9bc642f | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4968 | EvaluateConstantChrecAtConstant(this, |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4969 | ConstantInt::get(SE.getContext(), ExitVal - One), SE)->getValue()) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4970 | "Linear scev computation is off in a bad way!"); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4971 | return SE.getConstant(ExitValue); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4972 | } else if (isQuadratic()) { |
| 4973 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the |
| 4974 | // quadratic equation to solve it. To do this, we must frame our problem in |
| 4975 | // terms of figuring out when zero is crossed, instead of when |
| 4976 | // Range.getUpper() is crossed. |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4977 | SmallVector<const SCEV *, 4> NewOps(op_begin(), op_end()); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4978 | NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper())); |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4979 | const SCEV *NewAddRec = SE.getAddRecExpr(NewOps, getLoop()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4980 | |
| 4981 | // Next, solve the constructed addrec |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4982 | std::pair<const SCEV *,const SCEV *> Roots = |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4983 | SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4984 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 4985 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4986 | if (R1) { |
| 4987 | // Pick the smallest positive root value. |
| 4988 | if (ConstantInt *CB = |
Owen Anderson | 02b48c3 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 4989 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4990 | R1->getValue(), R2->getValue()))) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4991 | if (CB->getZExtValue() == false) |
| 4992 | std::swap(R1, R2); // R1 is the minimum root now. |
| 4993 | |
| 4994 | // Make sure the root is not off by one. The returned iteration should |
| 4995 | // not be in the range, but the previous one should be. When solving |
| 4996 | // for "X*X < 5", for example, we should not return a root of 2. |
| 4997 | ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this, |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4998 | R1->getValue(), |
| 4999 | SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5000 | if (Range.contains(R1Val->getValue())) { |
| 5001 | // The next iteration must be out of the range... |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 5002 | ConstantInt *NextVal = |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5003 | ConstantInt::get(SE.getContext(), R1->getValue()->getValue()+1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5004 | |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5005 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5006 | if (!Range.contains(R1Val->getValue())) |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5007 | return SE.getConstant(NextVal); |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5008 | return SE.getCouldNotCompute(); // Something strange happened |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5009 | } |
| 5010 | |
| 5011 | // If R1 was not in the range, then it is a good return value. Make |
| 5012 | // sure that R1-1 WAS in the range though, just in case. |
Owen Anderson | e755b09 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 5013 | ConstantInt *NextVal = |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5014 | ConstantInt::get(SE.getContext(), R1->getValue()->getValue()-1); |
Dan Gohman | 89f8505 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5015 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5016 | if (Range.contains(R1Val->getValue())) |
| 5017 | return R1; |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5018 | return SE.getCouldNotCompute(); // Something strange happened |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5019 | } |
| 5020 | } |
| 5021 | } |
| 5022 | |
Dan Gohman | 0ad08b0 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5023 | return SE.getCouldNotCompute(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5024 | } |
| 5025 | |
| 5026 | |
| 5027 | |
| 5028 | //===----------------------------------------------------------------------===// |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5029 | // SCEVCallbackVH Class Implementation |
| 5030 | //===----------------------------------------------------------------------===// |
| 5031 | |
Dan Gohman | 999d14e | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 5032 | void ScalarEvolution::SCEVCallbackVH::deleted() { |
Dan Gohman | 31b69c1 | 2009-07-13 22:20:53 +0000 | [diff] [blame] | 5033 | assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5034 | if (PHINode *PN = dyn_cast<PHINode>(getValPtr())) |
| 5035 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 5036 | if (Instruction *I = dyn_cast<Instruction>(getValPtr())) |
| 5037 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5038 | SE->Scalars.erase(getValPtr()); |
| 5039 | // this now dangles! |
| 5040 | } |
| 5041 | |
Dan Gohman | 999d14e | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 5042 | void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *) { |
Dan Gohman | 31b69c1 | 2009-07-13 22:20:53 +0000 | [diff] [blame] | 5043 | assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5044 | |
| 5045 | // Forget all the expressions associated with users of the old value, |
| 5046 | // so that future queries will recompute the expressions using the new |
| 5047 | // value. |
| 5048 | SmallVector<User *, 16> Worklist; |
Dan Gohman | 6b9da31 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5049 | SmallPtrSet<User *, 8> Visited; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5050 | Value *Old = getValPtr(); |
| 5051 | bool DeleteOld = false; |
| 5052 | for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end(); |
| 5053 | UI != UE; ++UI) |
| 5054 | Worklist.push_back(*UI); |
| 5055 | while (!Worklist.empty()) { |
| 5056 | User *U = Worklist.pop_back_val(); |
| 5057 | // Deleting the Old value will cause this to dangle. Postpone |
| 5058 | // that until everything else is done. |
| 5059 | if (U == Old) { |
| 5060 | DeleteOld = true; |
| 5061 | continue; |
| 5062 | } |
Dan Gohman | 6b9da31 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5063 | if (!Visited.insert(U)) |
| 5064 | continue; |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5065 | if (PHINode *PN = dyn_cast<PHINode>(U)) |
| 5066 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 5067 | if (Instruction *I = dyn_cast<Instruction>(U)) |
| 5068 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | 6b9da31 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5069 | SE->Scalars.erase(U); |
| 5070 | for (Value::use_iterator UI = U->use_begin(), UE = U->use_end(); |
| 5071 | UI != UE; ++UI) |
| 5072 | Worklist.push_back(*UI); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5073 | } |
Dan Gohman | 6b9da31 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5074 | // Delete the Old value if it (indirectly) references itself. |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5075 | if (DeleteOld) { |
| 5076 | if (PHINode *PN = dyn_cast<PHINode>(Old)) |
| 5077 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 5078 | if (Instruction *I = dyn_cast<Instruction>(Old)) |
| 5079 | SE->ValuesAtScopes.erase(I); |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5080 | SE->Scalars.erase(Old); |
| 5081 | // this now dangles! |
| 5082 | } |
| 5083 | // this may dangle! |
| 5084 | } |
| 5085 | |
Dan Gohman | 999d14e | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 5086 | ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) |
Dan Gohman | bff6b58 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5087 | : CallbackVH(V), SE(se) {} |
| 5088 | |
| 5089 | //===----------------------------------------------------------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5090 | // ScalarEvolution Class Implementation |
| 5091 | //===----------------------------------------------------------------------===// |
| 5092 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5093 | ScalarEvolution::ScalarEvolution() |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5094 | : FunctionPass(&ID) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5097 | bool ScalarEvolution::runOnFunction(Function &F) { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5098 | this->F = &F; |
| 5099 | LI = &getAnalysis<LoopInfo>(); |
| 5100 | TD = getAnalysisIfAvailable<TargetData>(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5101 | return false; |
| 5102 | } |
| 5103 | |
| 5104 | void ScalarEvolution::releaseMemory() { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5105 | Scalars.clear(); |
| 5106 | BackedgeTakenCounts.clear(); |
| 5107 | ConstantEvolutionLoopExitValue.clear(); |
Dan Gohman | da0071e | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 5108 | ValuesAtScopes.clear(); |
Dan Gohman | c6475cb | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5109 | UniqueSCEVs.clear(); |
| 5110 | SCEVAllocator.Reset(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5111 | } |
| 5112 | |
| 5113 | void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const { |
| 5114 | AU.setPreservesAll(); |
| 5115 | AU.addRequiredTransitive<LoopInfo>(); |
Dan Gohman | 01c2ee7 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 5116 | } |
| 5117 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5118 | bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5119 | return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5120 | } |
| 5121 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5122 | static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5123 | const Loop *L) { |
| 5124 | // Print all inner loops first |
| 5125 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 5126 | PrintLoopInfo(OS, SE, *I); |
| 5127 | |
Nick Lewycky | e5da191 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 5128 | OS << "Loop " << L->getHeader()->getName() << ": "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5129 | |
Devang Patel | 02451fa | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 5130 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5131 | L->getExitBlocks(ExitBlocks); |
| 5132 | if (ExitBlocks.size() != 1) |
Nick Lewycky | e5da191 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 5133 | OS << "<multiple exits> "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5134 | |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5135 | if (SE->hasLoopInvariantBackedgeTakenCount(L)) { |
| 5136 | OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5137 | } else { |
Dan Gohman | 76d5a0d | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5138 | OS << "Unpredictable backedge-taken count. "; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5139 | } |
| 5140 | |
Nick Lewycky | e5da191 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 5141 | OS << "\n"; |
Dan Gohman | b6b9e9e | 2009-06-24 00:33:16 +0000 | [diff] [blame] | 5142 | OS << "Loop " << L->getHeader()->getName() << ": "; |
| 5143 | |
| 5144 | if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) { |
| 5145 | OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L); |
| 5146 | } else { |
| 5147 | OS << "Unpredictable max backedge-taken count. "; |
| 5148 | } |
| 5149 | |
| 5150 | OS << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5151 | } |
| 5152 | |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 5153 | void ScalarEvolution::print(raw_ostream &OS, const Module* ) const { |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5154 | // ScalarEvolution's implementaiton of the print method is to print |
| 5155 | // out SCEV values of all instructions that are interesting. Doing |
| 5156 | // this potentially causes it to create new SCEV objects though, |
| 5157 | // which technically conflicts with the const qualifier. This isn't |
Dan Gohman | ac2a9d6 | 2009-07-10 20:25:29 +0000 | [diff] [blame] | 5158 | // observable from outside the class though, so casting away the |
| 5159 | // const isn't dangerous. |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5160 | ScalarEvolution &SE = *const_cast<ScalarEvolution*>(this); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5161 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5162 | OS << "Classifying expressions for: " << F->getName() << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5163 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
Dan Gohman | 43d37e9 | 2009-04-30 01:30:18 +0000 | [diff] [blame] | 5164 | if (isSCEVable(I->getType())) { |
Dan Gohman | 12668ad | 2009-07-13 23:03:05 +0000 | [diff] [blame] | 5165 | OS << *I << '\n'; |
Dan Gohman | abe991f | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 5166 | OS << " --> "; |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5167 | const SCEV *SV = SE.getSCEV(&*I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5168 | SV->print(OS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5169 | |
Dan Gohman | 8db598a | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 5170 | const Loop *L = LI->getLoopFor((*I).getParent()); |
| 5171 | |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5172 | const SCEV *AtUse = SE.getSCEVAtScope(SV, L); |
Dan Gohman | 8db598a | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 5173 | if (AtUse != SV) { |
| 5174 | OS << " --> "; |
| 5175 | AtUse->print(OS); |
| 5176 | } |
| 5177 | |
| 5178 | if (L) { |
Dan Gohman | e5b6084 | 2009-06-18 00:37:45 +0000 | [diff] [blame] | 5179 | OS << "\t\t" "Exits: "; |
Dan Gohman | 161ea03 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5180 | const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop()); |
Dan Gohman | aff14d6 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 5181 | if (!ExitValue->isLoopInvariant(L)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5182 | OS << "<<Unknown>>"; |
| 5183 | } else { |
| 5184 | OS << *ExitValue; |
| 5185 | } |
| 5186 | } |
| 5187 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5188 | OS << "\n"; |
| 5189 | } |
| 5190 | |
Dan Gohman | ffd36ba | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5191 | OS << "Determining loop execution counts for: " << F->getName() << "\n"; |
| 5192 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
| 5193 | PrintLoopInfo(OS, &SE, *I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5194 | } |
Dan Gohman | 13058cc | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 5195 | |
| 5196 | void ScalarEvolution::print(std::ostream &o, const Module *M) const { |
| 5197 | raw_os_ostream OS(o); |
| 5198 | print(OS, M); |
| 5199 | } |