Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1 | //===- ScalarEvolution.cpp - Scalar Evolution Analysis ----------*- C++ -*-===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the implementation of the scalar evolution analysis |
| 11 | // engine, which is used primarily to analyze expressions involving induction |
| 12 | // variables in loops. |
| 13 | // |
| 14 | // There are several aspects to this library. First is the representation of |
| 15 | // scalar expressions, which are represented as subclasses of the SCEV class. |
| 16 | // These classes are used to represent certain types of subexpressions that we |
Dan Gohman | bc3d77a | 2009-07-25 16:18:07 +0000 | [diff] [blame] | 17 | // can handle. We only create one SCEV of a particular shape, so |
| 18 | // pointer-comparisons for equality are legal. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 19 | // |
| 20 | // One important aspect of the SCEV objects is that they are never cyclic, even |
| 21 | // if there is a cycle in the dataflow for an expression (ie, a PHI node). If |
| 22 | // the PHI node is one of the idioms that we can represent (e.g., a polynomial |
| 23 | // recurrence) then we represent it directly as a recurrence node, otherwise we |
| 24 | // represent it as a SCEVUnknown node. |
| 25 | // |
| 26 | // In addition to being able to represent expressions of various types, we also |
| 27 | // have folders that are used to build the *canonical* representation for a |
| 28 | // particular expression. These folders are capable of using a variety of |
| 29 | // rewrite rules to simplify the expressions. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 30 | // |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 31 | // Once the folders are defined, we can implement the more interesting |
| 32 | // higher-level code, such as the code that recognizes PHI nodes of various |
| 33 | // types, computes the execution count of a loop, etc. |
| 34 | // |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 35 | // TODO: We should use these routines and value representations to implement |
| 36 | // dependence analysis! |
| 37 | // |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | // |
| 40 | // There are several good references for the techniques used in this analysis. |
| 41 | // |
| 42 | // Chains of recurrences -- a method to expedite the evaluation |
| 43 | // of closed-form functions |
| 44 | // Olaf Bachmann, Paul S. Wang, Eugene V. Zima |
| 45 | // |
| 46 | // On computational properties of chains of recurrences |
| 47 | // Eugene V. Zima |
| 48 | // |
| 49 | // Symbolic Evaluation of Chains of Recurrences for Loop Optimization |
| 50 | // Robert A. van Engelen |
| 51 | // |
| 52 | // Efficient Symbolic Analysis for Optimizing Compilers |
| 53 | // Robert A. van Engelen |
| 54 | // |
| 55 | // Using the chains of recurrences algebra for data dependence testing and |
| 56 | // induction variable substitution |
| 57 | // MS Thesis, Johnie Birch |
| 58 | // |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
Chris Lattner | 3b27d68 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 61 | #define DEBUG_TYPE "scalar-evolution" |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 62 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 63 | #include "llvm/Constants.h" |
| 64 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 65 | #include "llvm/GlobalVariable.h" |
Dan Gohman | 2681232 | 2009-08-25 17:49:57 +0000 | [diff] [blame] | 66 | #include "llvm/GlobalAlias.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 67 | #include "llvm/Instructions.h" |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 68 | #include "llvm/LLVMContext.h" |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 69 | #include "llvm/Operator.h" |
John Criswell | a115643 | 2005-10-27 15:54:34 +0000 | [diff] [blame] | 70 | #include "llvm/Analysis/ConstantFolding.h" |
Evan Cheng | 5a6c1a8 | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 71 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 72 | #include "llvm/Analysis/LoopInfo.h" |
Dan Gohman | 61ffa8e | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 73 | #include "llvm/Analysis/ValueTracking.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 74 | #include "llvm/Assembly/Writer.h" |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 75 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 76 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 77 | #include "llvm/Support/ConstantRange.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 78 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 79 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 80 | #include "llvm/Support/InstIterator.h" |
Chris Lattner | 75de5ab | 2006-12-19 01:16:02 +0000 | [diff] [blame] | 81 | #include "llvm/Support/MathExtras.h" |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 82 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 83 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 84 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 85 | #include "llvm/ADT/SmallPtrSet.h" |
Alkis Evlogimenos | 20aa474 | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 86 | #include <algorithm> |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 87 | using namespace llvm; |
| 88 | |
Chris Lattner | 3b27d68 | 2006-12-19 22:30:33 +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 | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 98 | static cl::opt<unsigned> |
Chris Lattner | 3b27d68 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 99 | MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, |
| 100 | cl::desc("Maximum number of iterations SCEV will " |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 101 | "symbolically execute a constant " |
| 102 | "derived loop"), |
Chris Lattner | 3b27d68 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 103 | cl::init(100)); |
| 104 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 105 | static RegisterPass<ScalarEvolution> |
| 106 | R("scalar-evolution", "Scalar Evolution Analysis", false, true); |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 107 | char ScalarEvolution::ID = 0; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 108 | |
| 109 | //===----------------------------------------------------------------------===// |
| 110 | // SCEV class definitions |
| 111 | //===----------------------------------------------------------------------===// |
| 112 | |
| 113 | //===----------------------------------------------------------------------===// |
| 114 | // Implementation of the SCEV class. |
| 115 | // |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 117 | SCEV::~SCEV() {} |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 119 | void SCEV::dump() const { |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 120 | print(errs()); |
| 121 | errs() << '\n'; |
| 122 | } |
| 123 | |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 124 | bool SCEV::isZero() const { |
| 125 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 126 | return SC->getValue()->isZero(); |
| 127 | return false; |
| 128 | } |
| 129 | |
Dan Gohman | 70a1fe7 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 130 | bool SCEV::isOne() const { |
| 131 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 132 | return SC->getValue()->isOne(); |
| 133 | return false; |
| 134 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 135 | |
Dan Gohman | 4d289bf | 2009-06-24 00:30:26 +0000 | [diff] [blame] | 136 | bool SCEV::isAllOnesValue() const { |
| 137 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) |
| 138 | return SC->getValue()->isAllOnesValue(); |
| 139 | return false; |
| 140 | } |
| 141 | |
Owen Anderson | 753ad61 | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 142 | SCEVCouldNotCompute::SCEVCouldNotCompute() : |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 143 | SCEV(FoldingSetNodeID(), scCouldNotCompute) {} |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 145 | bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 146 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
Misha Brukman | bb2aff1 | 2004-04-05 19:00:46 +0000 | [diff] [blame] | 147 | return false; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | const Type *SCEVCouldNotCompute::getType() const { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 151 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
Misha Brukman | bb2aff1 | 2004-04-05 19:00:46 +0000 | [diff] [blame] | 152 | return 0; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 156 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 160 | bool SCEVCouldNotCompute::hasOperand(const SCEV *) const { |
| 161 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); |
| 162 | return false; |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 165 | void SCEVCouldNotCompute::print(raw_ostream &OS) const { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 166 | OS << "***COULDNOTCOMPUTE***"; |
| 167 | } |
| 168 | |
| 169 | bool SCEVCouldNotCompute::classof(const SCEV *S) { |
| 170 | return S->getSCEVType() == scCouldNotCompute; |
| 171 | } |
| 172 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 173 | const SCEV *ScalarEvolution::getConstant(ConstantInt *V) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 174 | FoldingSetNodeID ID; |
| 175 | ID.AddInteger(scConstant); |
| 176 | ID.AddPointer(V); |
| 177 | void *IP = 0; |
| 178 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 179 | SCEV *S = SCEVAllocator.Allocate<SCEVConstant>(); |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 180 | new (S) SCEVConstant(ID, V); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 181 | UniqueSCEVs.InsertNode(S, IP); |
| 182 | return S; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 183 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 184 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 185 | const SCEV *ScalarEvolution::getConstant(const APInt& Val) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 186 | return getConstant(ConstantInt::get(getContext(), Val)); |
Dan Gohman | 9a6ae96 | 2007-07-09 15:25:17 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 189 | const SCEV * |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 190 | ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) { |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 191 | return getConstant( |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 192 | ConstantInt::get(cast<IntegerType>(Ty), V, isSigned)); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 195 | const Type *SCEVConstant::getType() const { return V->getType(); } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 196 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 197 | void SCEVConstant::print(raw_ostream &OS) const { |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 198 | WriteAsOperand(OS, V, false); |
| 199 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 200 | |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 201 | SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeID &ID, |
| 202 | unsigned SCEVTy, const SCEV *op, const Type *ty) |
| 203 | : SCEV(ID, SCEVTy), Op(op), Ty(ty) {} |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 204 | |
Dan Gohman | 8492360 | 2009-04-21 01:25:57 +0000 | [diff] [blame] | 205 | bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 206 | return Op->dominates(BB, DT); |
| 207 | } |
| 208 | |
Dan Gohman | 6e70e31 | 2009-09-27 15:26:03 +0000 | [diff] [blame] | 209 | bool SCEVCastExpr::properlyDominates(BasicBlock *BB, DominatorTree *DT) const { |
| 210 | return Op->properlyDominates(BB, DT); |
| 211 | } |
| 212 | |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 213 | SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeID &ID, |
| 214 | const SCEV *op, const Type *ty) |
| 215 | : SCEVCastExpr(ID, scTruncate, op, ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 216 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 217 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 218 | "Cannot truncate non-integer value!"); |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 219 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 220 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 221 | void SCEVTruncateExpr::print(raw_ostream &OS) const { |
Dan Gohman | 36b8e53 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 222 | OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 225 | SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeID &ID, |
| 226 | const SCEV *op, const Type *ty) |
| 227 | : SCEVCastExpr(ID, scZeroExtend, op, ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 228 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 229 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 230 | "Cannot zero extend non-integer value!"); |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 233 | void SCEVZeroExtendExpr::print(raw_ostream &OS) const { |
Dan Gohman | 36b8e53 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 234 | OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 237 | SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeID &ID, |
| 238 | const SCEV *op, const Type *ty) |
| 239 | : SCEVCastExpr(ID, scSignExtend, op, ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 240 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && |
| 241 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 242 | "Cannot sign extend non-integer value!"); |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 245 | void SCEVSignExtendExpr::print(raw_ostream &OS) const { |
Dan Gohman | 36b8e53 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 246 | OS << "(sext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 249 | void SCEVCommutativeExpr::print(raw_ostream &OS) const { |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 250 | assert(Operands.size() > 1 && "This plus expr shouldn't exist!"); |
| 251 | const char *OpStr = getOperationStr(); |
| 252 | OS << "(" << *Operands[0]; |
| 253 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 254 | OS << OpStr << *Operands[i]; |
| 255 | OS << ")"; |
| 256 | } |
| 257 | |
Dan Gohman | ecb403a | 2009-05-07 14:00:19 +0000 | [diff] [blame] | 258 | bool SCEVNAryExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
Evan Cheng | 5a6c1a8 | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 259 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 260 | if (!getOperand(i)->dominates(BB, DT)) |
| 261 | return false; |
| 262 | } |
| 263 | return true; |
| 264 | } |
| 265 | |
Dan Gohman | 6e70e31 | 2009-09-27 15:26:03 +0000 | [diff] [blame] | 266 | bool SCEVNAryExpr::properlyDominates(BasicBlock *BB, DominatorTree *DT) const { |
| 267 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 268 | if (!getOperand(i)->properlyDominates(BB, DT)) |
| 269 | return false; |
| 270 | } |
| 271 | return true; |
| 272 | } |
| 273 | |
Evan Cheng | 5a6c1a8 | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 274 | bool SCEVUDivExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 275 | return LHS->dominates(BB, DT) && RHS->dominates(BB, DT); |
| 276 | } |
| 277 | |
Dan Gohman | 6e70e31 | 2009-09-27 15:26:03 +0000 | [diff] [blame] | 278 | bool SCEVUDivExpr::properlyDominates(BasicBlock *BB, DominatorTree *DT) const { |
| 279 | return LHS->properlyDominates(BB, DT) && RHS->properlyDominates(BB, DT); |
| 280 | } |
| 281 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 282 | void SCEVUDivExpr::print(raw_ostream &OS) const { |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 283 | OS << "(" << *LHS << " /u " << *RHS << ")"; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 286 | const Type *SCEVUDivExpr::getType() const { |
Dan Gohman | 91bb61a | 2009-05-26 17:44:05 +0000 | [diff] [blame] | 287 | // In most cases the types of LHS and RHS will be the same, but in some |
| 288 | // crazy cases one or the other may be a pointer. ScalarEvolution doesn't |
| 289 | // depend on the type for correctness, but handling types carefully can |
| 290 | // avoid extra casts in the SCEVExpander. The LHS is more likely to be |
| 291 | // a pointer type than the RHS, so use the RHS' type here. |
| 292 | return RHS->getType(); |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 295 | bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const { |
Dan Gohman | a3035a6 | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 296 | // Add recurrences are never invariant in the function-body (null loop). |
Dan Gohman | e890eea | 2009-06-26 22:17:21 +0000 | [diff] [blame] | 297 | if (!QueryLoop) |
| 298 | return false; |
| 299 | |
| 300 | // This recurrence is variant w.r.t. QueryLoop if QueryLoop contains L. |
Dan Gohman | 92329c7 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 301 | if (QueryLoop->contains(L)) |
Dan Gohman | e890eea | 2009-06-26 22:17:21 +0000 | [diff] [blame] | 302 | return false; |
| 303 | |
| 304 | // This recurrence is variant w.r.t. QueryLoop if any of its operands |
| 305 | // are variant. |
| 306 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 307 | if (!getOperand(i)->isLoopInvariant(QueryLoop)) |
| 308 | return false; |
| 309 | |
| 310 | // Otherwise it's loop-invariant. |
| 311 | return true; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 314 | void SCEVAddRecExpr::print(raw_ostream &OS) const { |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 315 | OS << "{" << *Operands[0]; |
| 316 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 317 | OS << ",+," << *Operands[i]; |
| 318 | OS << "}<" << L->getHeader()->getName() + ">"; |
| 319 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 320 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 321 | void SCEVFieldOffsetExpr::print(raw_ostream &OS) const { |
| 322 | // LLVM struct fields don't have names, so just print the field number. |
| 323 | OS << "offsetof(" << *STy << ", " << FieldNo << ")"; |
| 324 | } |
| 325 | |
| 326 | void SCEVAllocSizeExpr::print(raw_ostream &OS) const { |
| 327 | OS << "sizeof(" << *AllocTy << ")"; |
| 328 | } |
| 329 | |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 330 | bool SCEVUnknown::isLoopInvariant(const Loop *L) const { |
| 331 | // All non-instruction values are loop invariant. All instructions are loop |
| 332 | // invariant if they are not contained in the specified loop. |
Dan Gohman | a3035a6 | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 333 | // Instructions are never considered invariant in the function body |
| 334 | // (null loop) because they are defined within the "loop". |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 335 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Dan Gohman | 92329c7 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 336 | return L && !L->contains(I); |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 337 | return true; |
| 338 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 339 | |
Evan Cheng | 5a6c1a8 | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 340 | bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const { |
| 341 | if (Instruction *I = dyn_cast<Instruction>(getValue())) |
| 342 | return DT->dominates(I->getParent(), BB); |
| 343 | return true; |
| 344 | } |
| 345 | |
Dan Gohman | 6e70e31 | 2009-09-27 15:26:03 +0000 | [diff] [blame] | 346 | bool SCEVUnknown::properlyDominates(BasicBlock *BB, DominatorTree *DT) const { |
| 347 | if (Instruction *I = dyn_cast<Instruction>(getValue())) |
| 348 | return DT->properlyDominates(I->getParent(), BB); |
| 349 | return true; |
| 350 | } |
| 351 | |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 352 | const Type *SCEVUnknown::getType() const { |
| 353 | return V->getType(); |
| 354 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 355 | |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 356 | void SCEVUnknown::print(raw_ostream &OS) const { |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 357 | WriteAsOperand(OS, V, false); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 360 | //===----------------------------------------------------------------------===// |
| 361 | // SCEV Utilities |
| 362 | //===----------------------------------------------------------------------===// |
| 363 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 364 | static bool CompareTypes(const Type *A, const Type *B) { |
| 365 | if (A->getTypeID() != B->getTypeID()) |
| 366 | return A->getTypeID() < B->getTypeID(); |
| 367 | if (const IntegerType *AI = dyn_cast<IntegerType>(A)) { |
| 368 | const IntegerType *BI = cast<IntegerType>(B); |
| 369 | return AI->getBitWidth() < BI->getBitWidth(); |
| 370 | } |
| 371 | if (const PointerType *AI = dyn_cast<PointerType>(A)) { |
| 372 | const PointerType *BI = cast<PointerType>(B); |
| 373 | return CompareTypes(AI->getElementType(), BI->getElementType()); |
| 374 | } |
| 375 | if (const ArrayType *AI = dyn_cast<ArrayType>(A)) { |
| 376 | const ArrayType *BI = cast<ArrayType>(B); |
| 377 | if (AI->getNumElements() != BI->getNumElements()) |
| 378 | return AI->getNumElements() < BI->getNumElements(); |
| 379 | return CompareTypes(AI->getElementType(), BI->getElementType()); |
| 380 | } |
| 381 | if (const VectorType *AI = dyn_cast<VectorType>(A)) { |
| 382 | const VectorType *BI = cast<VectorType>(B); |
| 383 | if (AI->getNumElements() != BI->getNumElements()) |
| 384 | return AI->getNumElements() < BI->getNumElements(); |
| 385 | return CompareTypes(AI->getElementType(), BI->getElementType()); |
| 386 | } |
| 387 | if (const StructType *AI = dyn_cast<StructType>(A)) { |
| 388 | const StructType *BI = cast<StructType>(B); |
| 389 | if (AI->getNumElements() != BI->getNumElements()) |
| 390 | return AI->getNumElements() < BI->getNumElements(); |
| 391 | for (unsigned i = 0, e = AI->getNumElements(); i != e; ++i) |
| 392 | if (CompareTypes(AI->getElementType(i), BI->getElementType(i)) || |
| 393 | CompareTypes(BI->getElementType(i), AI->getElementType(i))) |
| 394 | return CompareTypes(AI->getElementType(i), BI->getElementType(i)); |
| 395 | } |
| 396 | return false; |
| 397 | } |
| 398 | |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 399 | namespace { |
| 400 | /// SCEVComplexityCompare - Return true if the complexity of the LHS is less |
| 401 | /// than the complexity of the RHS. This comparator is used to canonicalize |
| 402 | /// expressions. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 403 | class SCEVComplexityCompare { |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 404 | LoopInfo *LI; |
| 405 | public: |
| 406 | explicit SCEVComplexityCompare(LoopInfo *li) : LI(li) {} |
| 407 | |
Dan Gohman | f7b37b2 | 2008-04-14 18:23:56 +0000 | [diff] [blame] | 408 | bool operator()(const SCEV *LHS, const SCEV *RHS) const { |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 409 | // Fast-path: SCEVs are uniqued so we can do a quick equality check. |
| 410 | if (LHS == RHS) |
| 411 | return false; |
| 412 | |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 413 | // Primarily, sort the SCEVs by their getSCEVType(). |
| 414 | if (LHS->getSCEVType() != RHS->getSCEVType()) |
| 415 | return LHS->getSCEVType() < RHS->getSCEVType(); |
| 416 | |
| 417 | // Aside from the getSCEVType() ordering, the particular ordering |
| 418 | // isn't very important except that it's beneficial to be consistent, |
| 419 | // so that (a + b) and (b + a) don't end up as different expressions. |
| 420 | |
| 421 | // Sort SCEVUnknown values with some loose heuristics. TODO: This is |
| 422 | // not as complete as it could be. |
| 423 | if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) { |
| 424 | const SCEVUnknown *RU = cast<SCEVUnknown>(RHS); |
| 425 | |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 426 | // Order pointer values after integer values. This helps SCEVExpander |
| 427 | // form GEPs. |
| 428 | if (isa<PointerType>(LU->getType()) && !isa<PointerType>(RU->getType())) |
| 429 | return false; |
| 430 | if (isa<PointerType>(RU->getType()) && !isa<PointerType>(LU->getType())) |
| 431 | return true; |
| 432 | |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 433 | // Compare getValueID values. |
| 434 | if (LU->getValue()->getValueID() != RU->getValue()->getValueID()) |
| 435 | return LU->getValue()->getValueID() < RU->getValue()->getValueID(); |
| 436 | |
| 437 | // Sort arguments by their position. |
| 438 | if (const Argument *LA = dyn_cast<Argument>(LU->getValue())) { |
| 439 | const Argument *RA = cast<Argument>(RU->getValue()); |
| 440 | return LA->getArgNo() < RA->getArgNo(); |
| 441 | } |
| 442 | |
| 443 | // For instructions, compare their loop depth, and their opcode. |
| 444 | // This is pretty loose. |
| 445 | if (Instruction *LV = dyn_cast<Instruction>(LU->getValue())) { |
| 446 | Instruction *RV = cast<Instruction>(RU->getValue()); |
| 447 | |
| 448 | // Compare loop depths. |
| 449 | if (LI->getLoopDepth(LV->getParent()) != |
| 450 | LI->getLoopDepth(RV->getParent())) |
| 451 | return LI->getLoopDepth(LV->getParent()) < |
| 452 | LI->getLoopDepth(RV->getParent()); |
| 453 | |
| 454 | // Compare opcodes. |
| 455 | if (LV->getOpcode() != RV->getOpcode()) |
| 456 | return LV->getOpcode() < RV->getOpcode(); |
| 457 | |
| 458 | // Compare the number of operands. |
| 459 | if (LV->getNumOperands() != RV->getNumOperands()) |
| 460 | return LV->getNumOperands() < RV->getNumOperands(); |
| 461 | } |
| 462 | |
| 463 | return false; |
| 464 | } |
| 465 | |
Dan Gohman | 4dfad29 | 2009-06-14 22:51:25 +0000 | [diff] [blame] | 466 | // Compare constant values. |
| 467 | if (const SCEVConstant *LC = dyn_cast<SCEVConstant>(LHS)) { |
| 468 | const SCEVConstant *RC = cast<SCEVConstant>(RHS); |
Nick Lewycky | d1ec989 | 2009-07-04 17:24:52 +0000 | [diff] [blame] | 469 | if (LC->getValue()->getBitWidth() != RC->getValue()->getBitWidth()) |
| 470 | return LC->getValue()->getBitWidth() < RC->getValue()->getBitWidth(); |
Dan Gohman | 4dfad29 | 2009-06-14 22:51:25 +0000 | [diff] [blame] | 471 | return LC->getValue()->getValue().ult(RC->getValue()->getValue()); |
| 472 | } |
| 473 | |
| 474 | // Compare addrec loop depths. |
| 475 | if (const SCEVAddRecExpr *LA = dyn_cast<SCEVAddRecExpr>(LHS)) { |
| 476 | const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS); |
| 477 | if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth()) |
| 478 | return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth(); |
| 479 | } |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 480 | |
| 481 | // Lexicographically compare n-ary expressions. |
| 482 | if (const SCEVNAryExpr *LC = dyn_cast<SCEVNAryExpr>(LHS)) { |
| 483 | const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS); |
| 484 | for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) { |
| 485 | if (i >= RC->getNumOperands()) |
| 486 | return false; |
| 487 | if (operator()(LC->getOperand(i), RC->getOperand(i))) |
| 488 | return true; |
| 489 | if (operator()(RC->getOperand(i), LC->getOperand(i))) |
| 490 | return false; |
| 491 | } |
| 492 | return LC->getNumOperands() < RC->getNumOperands(); |
| 493 | } |
| 494 | |
Dan Gohman | a6b35e2 | 2009-05-07 19:23:21 +0000 | [diff] [blame] | 495 | // Lexicographically compare udiv expressions. |
| 496 | if (const SCEVUDivExpr *LC = dyn_cast<SCEVUDivExpr>(LHS)) { |
| 497 | const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS); |
| 498 | if (operator()(LC->getLHS(), RC->getLHS())) |
| 499 | return true; |
| 500 | if (operator()(RC->getLHS(), LC->getLHS())) |
| 501 | return false; |
| 502 | if (operator()(LC->getRHS(), RC->getRHS())) |
| 503 | return true; |
| 504 | if (operator()(RC->getRHS(), LC->getRHS())) |
| 505 | return false; |
| 506 | return false; |
| 507 | } |
| 508 | |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 509 | // Compare cast expressions by operand. |
| 510 | if (const SCEVCastExpr *LC = dyn_cast<SCEVCastExpr>(LHS)) { |
| 511 | const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS); |
| 512 | return operator()(LC->getOperand(), RC->getOperand()); |
| 513 | } |
| 514 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 515 | // Compare offsetof expressions. |
| 516 | if (const SCEVFieldOffsetExpr *LA = dyn_cast<SCEVFieldOffsetExpr>(LHS)) { |
| 517 | const SCEVFieldOffsetExpr *RA = cast<SCEVFieldOffsetExpr>(RHS); |
| 518 | if (CompareTypes(LA->getStructType(), RA->getStructType()) || |
| 519 | CompareTypes(RA->getStructType(), LA->getStructType())) |
| 520 | return CompareTypes(LA->getStructType(), RA->getStructType()); |
| 521 | return LA->getFieldNo() < RA->getFieldNo(); |
| 522 | } |
| 523 | |
| 524 | // Compare sizeof expressions by the allocation type. |
| 525 | if (const SCEVAllocSizeExpr *LA = dyn_cast<SCEVAllocSizeExpr>(LHS)) { |
| 526 | const SCEVAllocSizeExpr *RA = cast<SCEVAllocSizeExpr>(RHS); |
| 527 | return CompareTypes(LA->getAllocType(), RA->getAllocType()); |
| 528 | } |
| 529 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 530 | llvm_unreachable("Unknown SCEV kind!"); |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 531 | return false; |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 532 | } |
| 533 | }; |
| 534 | } |
| 535 | |
| 536 | /// GroupByComplexity - Given a list of SCEV objects, order them by their |
| 537 | /// complexity, and group objects of the same complexity together by value. |
| 538 | /// When this routine is finished, we know that any duplicates in the vector are |
| 539 | /// consecutive and that complexity is monotonically increasing. |
| 540 | /// |
| 541 | /// Note that we go take special precautions to ensure that we get determinstic |
| 542 | /// results from this routine. In other words, we don't want the results of |
| 543 | /// this to depend on where the addresses of various SCEV objects happened to |
| 544 | /// land in memory. |
| 545 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 546 | static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops, |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 547 | LoopInfo *LI) { |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 548 | if (Ops.size() < 2) return; // Noop |
| 549 | if (Ops.size() == 2) { |
| 550 | // This is the common case, which also happens to be trivially simple. |
| 551 | // Special case it. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 552 | if (SCEVComplexityCompare(LI)(Ops[1], Ops[0])) |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 553 | std::swap(Ops[0], Ops[1]); |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | // Do the rough sort by complexity. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 558 | std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI)); |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 559 | |
| 560 | // Now that we are sorted by complexity, group elements of the same |
| 561 | // complexity. Note that this is, at worst, N^2, but the vector is likely to |
| 562 | // be extremely short in practice. Note that we take this approach because we |
| 563 | // do not want to depend on the addresses of the objects we are grouping. |
Chris Lattner | 2d58452 | 2004-06-20 17:01:44 +0000 | [diff] [blame] | 564 | for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 565 | const SCEV *S = Ops[i]; |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 566 | unsigned Complexity = S->getSCEVType(); |
| 567 | |
| 568 | // If there are any objects of the same complexity and same value as this |
| 569 | // one, group them. |
| 570 | for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { |
| 571 | if (Ops[j] == S) { // Found a duplicate. |
| 572 | // Move it to immediately after i'th element. |
| 573 | std::swap(Ops[i+1], Ops[j]); |
| 574 | ++i; // no need to rescan it. |
Chris Lattner | 541ad5e | 2004-06-20 20:32:16 +0000 | [diff] [blame] | 575 | if (i == e-2) return; // Done! |
Chris Lattner | 8d741b8 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 581 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 582 | |
| 583 | //===----------------------------------------------------------------------===// |
| 584 | // Simple SCEV method implementations |
| 585 | //===----------------------------------------------------------------------===// |
| 586 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 587 | /// BinomialCoefficient - Compute BC(It, K). The result has width W. |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 588 | /// Assume, K > 0. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 589 | static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K, |
Dan Gohman | c2b015e | 2009-07-21 00:38:55 +0000 | [diff] [blame] | 590 | ScalarEvolution &SE, |
| 591 | const Type* ResultTy) { |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 592 | // Handle the simplest case efficiently. |
| 593 | if (K == 1) |
| 594 | return SE.getTruncateOrZeroExtend(It, ResultTy); |
| 595 | |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 596 | // We are using the following formula for BC(It, K): |
| 597 | // |
| 598 | // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! |
| 599 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 600 | // Suppose, W is the bitwidth of the return value. We must be prepared for |
| 601 | // overflow. Hence, we must assure that the result of our computation is |
| 602 | // equal to the accurate one modulo 2^W. Unfortunately, division isn't |
| 603 | // safe in modular arithmetic. |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 604 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 605 | // However, this code doesn't use exactly that formula; the formula it uses |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 606 | // is something like the following, where T is the number of factors of 2 in |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 607 | // K! (i.e. trailing zeros in the binary representation of K!), and ^ is |
| 608 | // exponentiation: |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 609 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 610 | // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T) |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 611 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 612 | // This formula is trivially equivalent to the previous formula. However, |
| 613 | // this formula can be implemented much more efficiently. The trick is that |
| 614 | // K! / 2^T is odd, and exact division by an odd number *is* safe in modular |
| 615 | // arithmetic. To do exact division in modular arithmetic, all we have |
| 616 | // to do is multiply by the inverse. Therefore, this step can be done at |
| 617 | // width W. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 618 | // |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 619 | // The next issue is how to safely do the division by 2^T. The way this |
| 620 | // is done is by doing the multiplication step at a width of at least W + T |
| 621 | // bits. This way, the bottom W+T bits of the product are accurate. Then, |
| 622 | // when we perform the division by 2^T (which is equivalent to a right shift |
| 623 | // by T), the bottom W bits are accurate. Extra bits are okay; they'll get |
| 624 | // truncated out after the division by 2^T. |
| 625 | // |
| 626 | // In comparison to just directly using the first formula, this technique |
| 627 | // is much more efficient; using the first formula requires W * K bits, |
| 628 | // but this formula less than W + K bits. Also, the first formula requires |
| 629 | // a division step, whereas this formula only requires multiplies and shifts. |
| 630 | // |
| 631 | // It doesn't matter whether the subtraction step is done in the calculation |
| 632 | // width or the input iteration count's width; if the subtraction overflows, |
| 633 | // the result must be zero anyway. We prefer here to do it in the width of |
| 634 | // the induction variable because it helps a lot for certain cases; CodeGen |
| 635 | // isn't smart enough to ignore the overflow, which leads to much less |
| 636 | // efficient code if the width of the subtraction is wider than the native |
| 637 | // register width. |
| 638 | // |
| 639 | // (It's possible to not widen at all by pulling out factors of 2 before |
| 640 | // the multiplication; for example, K=2 can be calculated as |
| 641 | // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires |
| 642 | // extra arithmetic, so it's not an obvious win, and it gets |
| 643 | // much more complicated for K > 3.) |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 644 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 645 | // Protection from insane SCEVs; this bound is conservative, |
| 646 | // but it probably doesn't matter. |
| 647 | if (K > 1000) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 648 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 649 | |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 650 | unsigned W = SE.getTypeSizeInBits(ResultTy); |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 651 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 652 | // Calculate K! / 2^T and T; we divide out the factors of two before |
| 653 | // multiplying for calculating K! / 2^T to avoid overflow. |
| 654 | // Other overflow doesn't matter because we only care about the bottom |
| 655 | // W bits of the result. |
| 656 | APInt OddFactorial(W, 1); |
| 657 | unsigned T = 1; |
| 658 | for (unsigned i = 3; i <= K; ++i) { |
| 659 | APInt Mult(W, i); |
| 660 | unsigned TwoFactors = Mult.countTrailingZeros(); |
| 661 | T += TwoFactors; |
| 662 | Mult = Mult.lshr(TwoFactors); |
| 663 | OddFactorial *= Mult; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 664 | } |
Nick Lewycky | 6f8abf9 | 2008-06-13 04:38:55 +0000 | [diff] [blame] | 665 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 666 | // We need at least W + T bits for the multiplication step |
Nick Lewycky | 237d873 | 2009-01-25 08:16:27 +0000 | [diff] [blame] | 667 | unsigned CalculationBits = W + T; |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 668 | |
| 669 | // Calcuate 2^T, at width T+W. |
| 670 | APInt DivFactor = APInt(CalculationBits, 1).shl(T); |
| 671 | |
| 672 | // Calculate the multiplicative inverse of K! / 2^T; |
| 673 | // this multiplication factor will perform the exact division by |
| 674 | // K! / 2^T. |
| 675 | APInt Mod = APInt::getSignedMinValue(W+1); |
| 676 | APInt MultiplyFactor = OddFactorial.zext(W+1); |
| 677 | MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); |
| 678 | MultiplyFactor = MultiplyFactor.trunc(W); |
| 679 | |
| 680 | // Calculate the product, at width T+W |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 681 | const IntegerType *CalculationTy = IntegerType::get(SE.getContext(), |
| 682 | CalculationBits); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 683 | const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 684 | for (unsigned i = 1; i != K; ++i) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 685 | const SCEV *S = SE.getMinusSCEV(It, SE.getIntegerSCEV(i, It->getType())); |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 686 | Dividend = SE.getMulExpr(Dividend, |
| 687 | SE.getTruncateOrZeroExtend(S, CalculationTy)); |
| 688 | } |
| 689 | |
| 690 | // Divide by 2^T |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 691 | const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 692 | |
| 693 | // Truncate the result, and divide by K! / 2^T. |
| 694 | |
| 695 | return SE.getMulExpr(SE.getConstant(MultiplyFactor), |
| 696 | SE.getTruncateOrZeroExtend(DivResult, ResultTy)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 699 | /// evaluateAtIteration - Return the value of this chain of recurrences at |
| 700 | /// the specified iteration number. We can evaluate this recurrence by |
| 701 | /// multiplying each element in the chain by the binomial coefficient |
| 702 | /// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as: |
| 703 | /// |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 704 | /// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 705 | /// |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 706 | /// where BC(It, k) stands for binomial coefficient. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 707 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 708 | const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It, |
Dan Gohman | c2b015e | 2009-07-21 00:38:55 +0000 | [diff] [blame] | 709 | ScalarEvolution &SE) const { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 710 | const SCEV *Result = getStart(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 711 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
Wojciech Matyjewicz | e3320a1 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 712 | // The computation is correct in the face of overflow provided that the |
| 713 | // multiplication is performed _after_ the evaluation of the binomial |
| 714 | // coefficient. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 715 | const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType()); |
Nick Lewycky | cb8f1b5 | 2008-10-13 03:58:02 +0000 | [diff] [blame] | 716 | if (isa<SCEVCouldNotCompute>(Coeff)) |
| 717 | return Coeff; |
| 718 | |
| 719 | Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 720 | } |
| 721 | return Result; |
| 722 | } |
| 723 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 724 | //===----------------------------------------------------------------------===// |
| 725 | // SCEV Expression folder implementations |
| 726 | //===----------------------------------------------------------------------===// |
| 727 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 728 | const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, |
Dan Gohman | f5074ec | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 729 | const Type *Ty) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 730 | assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 731 | "This is not a truncating conversion!"); |
Dan Gohman | 10b9479 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 732 | assert(isSCEVable(Ty) && |
| 733 | "This is not a conversion to a SCEVable type!"); |
| 734 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 735 | |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 736 | FoldingSetNodeID ID; |
| 737 | ID.AddInteger(scTruncate); |
| 738 | ID.AddPointer(Op); |
| 739 | ID.AddPointer(Ty); |
| 740 | void *IP = 0; |
| 741 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 742 | |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 743 | // Fold if the operand is constant. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 744 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
Dan Gohman | b8be8b7 | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 745 | return getConstant( |
| 746 | cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 747 | |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 748 | // trunc(trunc(x)) --> trunc(x) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 749 | if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 750 | return getTruncateExpr(ST->getOperand(), Ty); |
| 751 | |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 752 | // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 753 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 754 | return getTruncateOrSignExtend(SS->getOperand(), Ty); |
| 755 | |
| 756 | // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 757 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 758 | return getTruncateOrZeroExtend(SZ->getOperand(), Ty); |
| 759 | |
Dan Gohman | 6864db6 | 2009-06-18 16:24:47 +0000 | [diff] [blame] | 760 | // If the input value is a chrec scev, truncate the chrec's operands. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 761 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 762 | SmallVector<const SCEV *, 4> Operands; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 763 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 764 | Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty)); |
| 765 | return getAddRecExpr(Operands, AddRec->getLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 768 | // The cast wasn't folded; create an explicit cast node. |
| 769 | // Recompute the insert position, as it may have been invalidated. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 770 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 771 | SCEV *S = SCEVAllocator.Allocate<SCEVTruncateExpr>(); |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 772 | new (S) SCEVTruncateExpr(ID, Op, Ty); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 773 | UniqueSCEVs.InsertNode(S, IP); |
| 774 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 777 | const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op, |
Dan Gohman | f5074ec | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 778 | const Type *Ty) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 779 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | 8170a68 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 780 | "This is not an extending conversion!"); |
Dan Gohman | 10b9479 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 781 | assert(isSCEVable(Ty) && |
| 782 | "This is not a conversion to a SCEVable type!"); |
| 783 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | 8170a68 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 784 | |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 785 | // Fold if the operand is constant. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 786 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 787 | const Type *IntTy = getEffectiveSCEVType(Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 788 | Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy); |
| 789 | if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); |
Dan Gohman | b8be8b7 | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 790 | return getConstant(cast<ConstantInt>(C)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 791 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 792 | |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 793 | // zext(zext(x)) --> zext(x) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 794 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 795 | return getZeroExtendExpr(SZ->getOperand(), Ty); |
| 796 | |
Dan Gohman | 69fbc7f | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 797 | // Before doing any expensive analysis, check to see if we've already |
| 798 | // computed a SCEV for this Op and Ty. |
| 799 | FoldingSetNodeID ID; |
| 800 | ID.AddInteger(scZeroExtend); |
| 801 | ID.AddPointer(Op); |
| 802 | ID.AddPointer(Ty); |
| 803 | void *IP = 0; |
| 804 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 805 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 806 | // If the input value is a chrec scev, and we can prove that the value |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 807 | // did not overflow the old, smaller, value, we can zero extend all of the |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 808 | // operands (often constants). This allows analysis of something like |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 809 | // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 810 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 811 | if (AR->isAffine()) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 812 | const SCEV *Start = AR->getStart(); |
| 813 | const SCEV *Step = AR->getStepRecurrence(*this); |
| 814 | unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
| 815 | const Loop *L = AR->getLoop(); |
| 816 | |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 817 | // If we have special knowledge that this addrec won't overflow, |
| 818 | // we don't need to do any further analysis. |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 819 | if (AR->hasNoUnsignedWrap()) |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 820 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 821 | getZeroExtendExpr(Step, Ty), |
| 822 | L); |
| 823 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 824 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 825 | // Note that this serves two purposes: It filters out loops that are |
| 826 | // simply not analyzable, and it covers the case where this code is |
| 827 | // being called from within backedge-taken count analysis, such that |
| 828 | // attempting to ask for the backedge-taken count would likely result |
| 829 | // in infinite recursion. In the later case, the analysis code will |
| 830 | // cope with a conservative value, and it will take care to purge |
| 831 | // that value once it has finished. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 832 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 833 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | f0aa485 | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 834 | // Manually compute the final value for AR, checking for |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 835 | // overflow. |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 836 | |
| 837 | // Check whether the backedge-taken count can be losslessly casted to |
| 838 | // the addrec's type. The count is always unsigned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 839 | const SCEV *CastedMaxBECount = |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 840 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 841 | const SCEV *RecastedMaxBECount = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 842 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 843 | if (MaxBECount == RecastedMaxBECount) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 844 | const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 845 | // Check whether Start+Step*MaxBECount has no unsigned overflow. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 846 | const SCEV *ZMul = |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 847 | getMulExpr(CastedMaxBECount, |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 848 | getTruncateOrZeroExtend(Step, Start->getType())); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 849 | const SCEV *Add = getAddExpr(Start, ZMul); |
| 850 | const SCEV *OperandExtendedAdd = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 851 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 852 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 853 | getZeroExtendExpr(Step, WideTy))); |
| 854 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 855 | // Return the expression with the addrec on the outside. |
| 856 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 857 | getZeroExtendExpr(Step, Ty), |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 858 | L); |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 859 | |
| 860 | // Similar to above, only this time treat the step value as signed. |
| 861 | // This covers loops that count down. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 862 | const SCEV *SMul = |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 863 | getMulExpr(CastedMaxBECount, |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 864 | getTruncateOrSignExtend(Step, Start->getType())); |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 865 | Add = getAddExpr(Start, SMul); |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 866 | OperandExtendedAdd = |
| 867 | getAddExpr(getZeroExtendExpr(Start, WideTy), |
| 868 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 869 | getSignExtendExpr(Step, WideTy))); |
| 870 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 871 | // Return the expression with the addrec on the outside. |
| 872 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 873 | getSignExtendExpr(Step, Ty), |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 874 | L); |
| 875 | } |
| 876 | |
| 877 | // If the backedge is guarded by a comparison with the pre-inc value |
| 878 | // the addrec is safe. Also, if the entry is guarded by a comparison |
| 879 | // with the start value and the backedge is guarded by a comparison |
| 880 | // with the post-inc value, the addrec is safe. |
| 881 | if (isKnownPositive(Step)) { |
| 882 | const SCEV *N = getConstant(APInt::getMinValue(BitWidth) - |
| 883 | getUnsignedRange(Step).getUnsignedMax()); |
| 884 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) || |
| 885 | (isLoopGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) && |
| 886 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, |
| 887 | AR->getPostIncExpr(*this), N))) |
| 888 | // Return the expression with the addrec on the outside. |
| 889 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 890 | getZeroExtendExpr(Step, Ty), |
| 891 | L); |
| 892 | } else if (isKnownNegative(Step)) { |
| 893 | const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) - |
| 894 | getSignedRange(Step).getSignedMin()); |
| 895 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) && |
| 896 | (isLoopGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) || |
| 897 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, |
| 898 | AR->getPostIncExpr(*this), N))) |
| 899 | // Return the expression with the addrec on the outside. |
| 900 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), |
| 901 | getSignExtendExpr(Step, Ty), |
| 902 | L); |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 906 | |
Dan Gohman | 69fbc7f | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 907 | // The cast wasn't folded; create an explicit cast node. |
| 908 | // Recompute the insert position, as it may have been invalidated. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 909 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 910 | SCEV *S = SCEVAllocator.Allocate<SCEVZeroExtendExpr>(); |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 911 | new (S) SCEVZeroExtendExpr(ID, Op, Ty); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 912 | UniqueSCEVs.InsertNode(S, IP); |
| 913 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 916 | const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op, |
Dan Gohman | f5074ec | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 917 | const Type *Ty) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 918 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 919 | "This is not an extending conversion!"); |
Dan Gohman | 10b9479 | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 920 | assert(isSCEVable(Ty) && |
| 921 | "This is not a conversion to a SCEVable type!"); |
| 922 | Ty = getEffectiveSCEVType(Ty); |
Dan Gohman | fb17fd2 | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 923 | |
Dan Gohman | c39f44b | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 924 | // Fold if the operand is constant. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 925 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 926 | const Type *IntTy = getEffectiveSCEVType(Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 927 | Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy); |
| 928 | if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); |
Dan Gohman | b8be8b7 | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 929 | return getConstant(cast<ConstantInt>(C)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 930 | } |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 931 | |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 932 | // sext(sext(x)) --> sext(x) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 933 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) |
Dan Gohman | 20900ca | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 934 | return getSignExtendExpr(SS->getOperand(), Ty); |
| 935 | |
Dan Gohman | 69fbc7f | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 936 | // Before doing any expensive analysis, check to see if we've already |
| 937 | // computed a SCEV for this Op and Ty. |
| 938 | FoldingSetNodeID ID; |
| 939 | ID.AddInteger(scSignExtend); |
| 940 | ID.AddPointer(Op); |
| 941 | ID.AddPointer(Ty); |
| 942 | void *IP = 0; |
| 943 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 944 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 945 | // If the input value is a chrec scev, and we can prove that the value |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 946 | // did not overflow the old, smaller, value, we can sign extend all of the |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 947 | // operands (often constants). This allows analysis of something like |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 948 | // this: for (signed char X = 0; X < 100; ++X) { int Y = X; } |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 949 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 950 | if (AR->isAffine()) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 951 | const SCEV *Start = AR->getStart(); |
| 952 | const SCEV *Step = AR->getStepRecurrence(*this); |
| 953 | unsigned BitWidth = getTypeSizeInBits(AR->getType()); |
| 954 | const Loop *L = AR->getLoop(); |
| 955 | |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 956 | // If we have special knowledge that this addrec won't overflow, |
| 957 | // we don't need to do any further analysis. |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 958 | if (AR->hasNoSignedWrap()) |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 959 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 960 | getSignExtendExpr(Step, Ty), |
| 961 | L); |
| 962 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 963 | // Check whether the backedge-taken count is SCEVCouldNotCompute. |
| 964 | // Note that this serves two purposes: It filters out loops that are |
| 965 | // simply not analyzable, and it covers the case where this code is |
| 966 | // being called from within backedge-taken count analysis, such that |
| 967 | // attempting to ask for the backedge-taken count would likely result |
| 968 | // in infinite recursion. In the later case, the analysis code will |
| 969 | // cope with a conservative value, and it will take care to purge |
| 970 | // that value once it has finished. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 971 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 972 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { |
Dan Gohman | f0aa485 | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 973 | // Manually compute the final value for AR, checking for |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 974 | // overflow. |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 975 | |
| 976 | // Check whether the backedge-taken count can be losslessly casted to |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 977 | // the addrec's type. The count is always unsigned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 978 | const SCEV *CastedMaxBECount = |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 979 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 980 | const SCEV *RecastedMaxBECount = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 981 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); |
| 982 | if (MaxBECount == RecastedMaxBECount) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 983 | const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 984 | // Check whether Start+Step*MaxBECount has no signed overflow. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 985 | const SCEV *SMul = |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 986 | getMulExpr(CastedMaxBECount, |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 987 | getTruncateOrSignExtend(Step, Start->getType())); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 988 | const SCEV *Add = getAddExpr(Start, SMul); |
| 989 | const SCEV *OperandExtendedAdd = |
Dan Gohman | 5183cae | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 990 | getAddExpr(getSignExtendExpr(Start, WideTy), |
| 991 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 992 | getSignExtendExpr(Step, WideTy))); |
| 993 | if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | ac70cea | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 994 | // Return the expression with the addrec on the outside. |
| 995 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 996 | getSignExtendExpr(Step, Ty), |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 997 | L); |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 998 | |
| 999 | // Similar to above, only this time treat the step value as unsigned. |
| 1000 | // This covers loops that count up with an unsigned step. |
| 1001 | const SCEV *UMul = |
| 1002 | getMulExpr(CastedMaxBECount, |
| 1003 | getTruncateOrZeroExtend(Step, Start->getType())); |
| 1004 | Add = getAddExpr(Start, UMul); |
| 1005 | OperandExtendedAdd = |
Dan Gohman | 19378d6 | 2009-07-25 16:03:30 +0000 | [diff] [blame] | 1006 | getAddExpr(getSignExtendExpr(Start, WideTy), |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 1007 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), |
| 1008 | getZeroExtendExpr(Step, WideTy))); |
Dan Gohman | 19378d6 | 2009-07-25 16:03:30 +0000 | [diff] [blame] | 1009 | if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 1010 | // Return the expression with the addrec on the outside. |
| 1011 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 1012 | getZeroExtendExpr(Step, Ty), |
| 1013 | L); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | // If the backedge is guarded by a comparison with the pre-inc value |
| 1017 | // the addrec is safe. Also, if the entry is guarded by a comparison |
| 1018 | // with the start value and the backedge is guarded by a comparison |
| 1019 | // with the post-inc value, the addrec is safe. |
| 1020 | if (isKnownPositive(Step)) { |
| 1021 | const SCEV *N = getConstant(APInt::getSignedMinValue(BitWidth) - |
| 1022 | getSignedRange(Step).getSignedMax()); |
| 1023 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, AR, N) || |
| 1024 | (isLoopGuardedByCond(L, ICmpInst::ICMP_SLT, Start, N) && |
| 1025 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, |
| 1026 | AR->getPostIncExpr(*this), N))) |
| 1027 | // Return the expression with the addrec on the outside. |
| 1028 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 1029 | getSignExtendExpr(Step, Ty), |
| 1030 | L); |
| 1031 | } else if (isKnownNegative(Step)) { |
| 1032 | const SCEV *N = getConstant(APInt::getSignedMaxValue(BitWidth) - |
| 1033 | getSignedRange(Step).getSignedMin()); |
| 1034 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, AR, N) || |
| 1035 | (isLoopGuardedByCond(L, ICmpInst::ICMP_SGT, Start, N) && |
| 1036 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, |
| 1037 | AR->getPostIncExpr(*this), N))) |
| 1038 | // Return the expression with the addrec on the outside. |
| 1039 | return getAddRecExpr(getSignExtendExpr(Start, Ty), |
| 1040 | getSignExtendExpr(Step, Ty), |
| 1041 | L); |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1042 | } |
| 1043 | } |
| 1044 | } |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 1045 | |
Dan Gohman | 69fbc7f | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 1046 | // The cast wasn't folded; create an explicit cast node. |
| 1047 | // Recompute the insert position, as it may have been invalidated. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1048 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1049 | SCEV *S = SCEVAllocator.Allocate<SCEVSignExtendExpr>(); |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1050 | new (S) SCEVSignExtendExpr(ID, Op, Ty); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1051 | UniqueSCEVs.InsertNode(S, IP); |
| 1052 | return S; |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1055 | /// getAnyExtendExpr - Return a SCEV for the given operand extended with |
| 1056 | /// unspecified bits out to the given type. |
| 1057 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1058 | const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op, |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 1059 | const Type *Ty) { |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1060 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && |
| 1061 | "This is not an extending conversion!"); |
| 1062 | assert(isSCEVable(Ty) && |
| 1063 | "This is not a conversion to a SCEVable type!"); |
| 1064 | Ty = getEffectiveSCEVType(Ty); |
| 1065 | |
| 1066 | // Sign-extend negative constants. |
| 1067 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) |
| 1068 | if (SC->getValue()->getValue().isNegative()) |
| 1069 | return getSignExtendExpr(Op, Ty); |
| 1070 | |
| 1071 | // Peel off a truncate cast. |
| 1072 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1073 | const SCEV *NewOp = T->getOperand(); |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1074 | if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty)) |
| 1075 | return getAnyExtendExpr(NewOp, Ty); |
| 1076 | return getTruncateOrNoop(NewOp, Ty); |
| 1077 | } |
| 1078 | |
| 1079 | // Next try a zext cast. If the cast is folded, use it. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1080 | const SCEV *ZExt = getZeroExtendExpr(Op, Ty); |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1081 | if (!isa<SCEVZeroExtendExpr>(ZExt)) |
| 1082 | return ZExt; |
| 1083 | |
| 1084 | // Next try a sext cast. If the cast is folded, use it. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1085 | const SCEV *SExt = getSignExtendExpr(Op, Ty); |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1086 | if (!isa<SCEVSignExtendExpr>(SExt)) |
| 1087 | return SExt; |
| 1088 | |
| 1089 | // If the expression is obviously signed, use the sext cast value. |
| 1090 | if (isa<SCEVSMaxExpr>(Op)) |
| 1091 | return SExt; |
| 1092 | |
| 1093 | // Absent any other information, use the zext cast value. |
| 1094 | return ZExt; |
| 1095 | } |
| 1096 | |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1097 | /// CollectAddOperandsWithScales - Process the given Ops list, which is |
| 1098 | /// a list of operands to be added under the given scale, update the given |
| 1099 | /// map. This is a helper function for getAddRecExpr. As an example of |
| 1100 | /// what it does, given a sequence of operands that would form an add |
| 1101 | /// expression like this: |
| 1102 | /// |
| 1103 | /// m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r) |
| 1104 | /// |
| 1105 | /// where A and B are constants, update the map with these values: |
| 1106 | /// |
| 1107 | /// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0) |
| 1108 | /// |
| 1109 | /// and add 13 + A*B*29 to AccumulatedConstant. |
| 1110 | /// This will allow getAddRecExpr to produce this: |
| 1111 | /// |
| 1112 | /// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B) |
| 1113 | /// |
| 1114 | /// This form often exposes folding opportunities that are hidden in |
| 1115 | /// the original operand list. |
| 1116 | /// |
| 1117 | /// Return true iff it appears that any interesting folding opportunities |
| 1118 | /// may be exposed. This helps getAddRecExpr short-circuit extra work in |
| 1119 | /// the common case where no interesting opportunities are present, and |
| 1120 | /// is also used as a check to avoid infinite recursion. |
| 1121 | /// |
| 1122 | static bool |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1123 | CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M, |
| 1124 | SmallVector<const SCEV *, 8> &NewOps, |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1125 | APInt &AccumulatedConstant, |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1126 | const SmallVectorImpl<const SCEV *> &Ops, |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1127 | const APInt &Scale, |
| 1128 | ScalarEvolution &SE) { |
| 1129 | bool Interesting = false; |
| 1130 | |
| 1131 | // Iterate over the add operands. |
| 1132 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 1133 | const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]); |
| 1134 | if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) { |
| 1135 | APInt NewScale = |
| 1136 | Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue(); |
| 1137 | if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) { |
| 1138 | // A multiplication of a constant with another add; recurse. |
| 1139 | Interesting |= |
| 1140 | CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
| 1141 | cast<SCEVAddExpr>(Mul->getOperand(1)) |
| 1142 | ->getOperands(), |
| 1143 | NewScale, SE); |
| 1144 | } else { |
| 1145 | // A multiplication of a constant with some other value. Update |
| 1146 | // the map. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1147 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end()); |
| 1148 | const SCEV *Key = SE.getMulExpr(MulOps); |
| 1149 | std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = |
Dan Gohman | 23737e0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1150 | M.insert(std::make_pair(Key, NewScale)); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1151 | if (Pair.second) { |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1152 | NewOps.push_back(Pair.first->first); |
| 1153 | } else { |
| 1154 | Pair.first->second += NewScale; |
| 1155 | // The map already had an entry for this value, which may indicate |
| 1156 | // a folding opportunity. |
| 1157 | Interesting = true; |
| 1158 | } |
| 1159 | } |
| 1160 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| 1161 | // Pull a buried constant out to the outside. |
| 1162 | if (Scale != 1 || AccumulatedConstant != 0 || C->isZero()) |
| 1163 | Interesting = true; |
| 1164 | AccumulatedConstant += Scale * C->getValue()->getValue(); |
| 1165 | } else { |
| 1166 | // An ordinary operand. Update the map. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1167 | std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = |
Dan Gohman | 23737e0 | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1168 | M.insert(std::make_pair(Ops[i], Scale)); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1169 | if (Pair.second) { |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1170 | NewOps.push_back(Pair.first->first); |
| 1171 | } else { |
| 1172 | Pair.first->second += Scale; |
| 1173 | // The map already had an entry for this value, which may indicate |
| 1174 | // a folding opportunity. |
| 1175 | Interesting = true; |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | return Interesting; |
| 1181 | } |
| 1182 | |
| 1183 | namespace { |
| 1184 | struct APIntCompare { |
| 1185 | bool operator()(const APInt &LHS, const APInt &RHS) const { |
| 1186 | return LHS.ult(RHS); |
| 1187 | } |
| 1188 | }; |
| 1189 | } |
| 1190 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1191 | /// getAddExpr - Get a canonical add expression, or something simpler if |
| 1192 | /// possible. |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1193 | const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops, |
| 1194 | bool HasNUW, bool HasNSW) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1195 | assert(!Ops.empty() && "Cannot get empty add!"); |
Chris Lattner | 627018b | 2004-04-07 16:16:11 +0000 | [diff] [blame] | 1196 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1197 | #ifndef NDEBUG |
| 1198 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1199 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1200 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1201 | "SCEVAddExpr operand types don't match!"); |
| 1202 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1203 | |
| 1204 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1205 | GroupByComplexity(Ops, LI); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1206 | |
| 1207 | // If there are any constants, fold them together. |
| 1208 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1209 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1210 | ++Idx; |
Chris Lattner | 627018b | 2004-04-07 16:16:11 +0000 | [diff] [blame] | 1211 | assert(Idx < Ops.size()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1212 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1213 | // We found two constants, fold them together! |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1214 | Ops[0] = getConstant(LHSC->getValue()->getValue() + |
| 1215 | RHSC->getValue()->getValue()); |
Dan Gohman | 7f7c436 | 2009-06-14 22:53:57 +0000 | [diff] [blame] | 1216 | if (Ops.size() == 2) return Ops[0]; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1217 | Ops.erase(Ops.begin()+1); // Erase the folded element |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1218 | LHSC = cast<SCEVConstant>(Ops[0]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | // If we are left with a constant zero being added, strip it off. |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1222 | if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1223 | Ops.erase(Ops.begin()); |
| 1224 | --Idx; |
| 1225 | } |
| 1226 | } |
| 1227 | |
Chris Lattner | 627018b | 2004-04-07 16:16:11 +0000 | [diff] [blame] | 1228 | if (Ops.size() == 1) return Ops[0]; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1229 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1230 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1231 | // so, merge them together into an multiply expression. Since we sorted the |
| 1232 | // list, these values are required to be adjacent. |
| 1233 | const Type *Ty = Ops[0]->getType(); |
| 1234 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1235 | if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2 |
| 1236 | // Found a match, merge the two values into a multiply, and add any |
| 1237 | // remaining values to the result. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1238 | const SCEV *Two = getIntegerSCEV(2, Ty); |
| 1239 | const SCEV *Mul = getMulExpr(Ops[i], Two); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1240 | if (Ops.size() == 2) |
| 1241 | return Mul; |
| 1242 | Ops.erase(Ops.begin()+i, Ops.begin()+i+2); |
| 1243 | Ops.push_back(Mul); |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1244 | return getAddExpr(Ops, HasNUW, HasNSW); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1247 | // Check for truncates. If all the operands are truncated from the same |
| 1248 | // type, see if factoring out the truncate would permit the result to be |
| 1249 | // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n) |
| 1250 | // if the contents of the resulting outer trunc fold to something simple. |
| 1251 | for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) { |
| 1252 | const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]); |
| 1253 | const Type *DstType = Trunc->getType(); |
| 1254 | const Type *SrcType = Trunc->getOperand()->getType(); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1255 | SmallVector<const SCEV *, 8> LargeOps; |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1256 | bool Ok = true; |
| 1257 | // Check all the operands to see if they can be represented in the |
| 1258 | // source type of the truncate. |
| 1259 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 1260 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) { |
| 1261 | if (T->getOperand()->getType() != SrcType) { |
| 1262 | Ok = false; |
| 1263 | break; |
| 1264 | } |
| 1265 | LargeOps.push_back(T->getOperand()); |
| 1266 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { |
| 1267 | // This could be either sign or zero extension, but sign extension |
| 1268 | // is much more likely to be foldable here. |
| 1269 | LargeOps.push_back(getSignExtendExpr(C, SrcType)); |
| 1270 | } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1271 | SmallVector<const SCEV *, 8> LargeMulOps; |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1272 | for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { |
| 1273 | if (const SCEVTruncateExpr *T = |
| 1274 | dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) { |
| 1275 | if (T->getOperand()->getType() != SrcType) { |
| 1276 | Ok = false; |
| 1277 | break; |
| 1278 | } |
| 1279 | LargeMulOps.push_back(T->getOperand()); |
| 1280 | } else if (const SCEVConstant *C = |
| 1281 | dyn_cast<SCEVConstant>(M->getOperand(j))) { |
| 1282 | // This could be either sign or zero extension, but sign extension |
| 1283 | // is much more likely to be foldable here. |
| 1284 | LargeMulOps.push_back(getSignExtendExpr(C, SrcType)); |
| 1285 | } else { |
| 1286 | Ok = false; |
| 1287 | break; |
| 1288 | } |
| 1289 | } |
| 1290 | if (Ok) |
| 1291 | LargeOps.push_back(getMulExpr(LargeMulOps)); |
| 1292 | } else { |
| 1293 | Ok = false; |
| 1294 | break; |
| 1295 | } |
| 1296 | } |
| 1297 | if (Ok) { |
| 1298 | // Evaluate the expression in the larger type. |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1299 | const SCEV *Fold = getAddExpr(LargeOps, HasNUW, HasNSW); |
Dan Gohman | 728c7f3 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1300 | // If it folds to something simple, use it. Otherwise, don't. |
| 1301 | if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold)) |
| 1302 | return getTruncateExpr(Fold, DstType); |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | // Skip past any other cast SCEVs. |
Dan Gohman | f50cd74 | 2007-06-18 19:30:09 +0000 | [diff] [blame] | 1307 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) |
| 1308 | ++Idx; |
| 1309 | |
| 1310 | // If there are add operands they would be next. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1311 | if (Idx < Ops.size()) { |
| 1312 | bool DeletedAdd = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1313 | while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1314 | // If we have an add, expand the add operands onto the end of the operands |
| 1315 | // list. |
| 1316 | Ops.insert(Ops.end(), Add->op_begin(), Add->op_end()); |
| 1317 | Ops.erase(Ops.begin()+Idx); |
| 1318 | DeletedAdd = true; |
| 1319 | } |
| 1320 | |
| 1321 | // If we deleted at least one add, we added operands to the end of the list, |
| 1322 | // and they are not necessarily sorted. Recurse to resort and resimplify |
| 1323 | // any operands we just aquired. |
| 1324 | if (DeletedAdd) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1325 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | // Skip over the add expression until we get to a multiply. |
| 1329 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1330 | ++Idx; |
| 1331 | |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1332 | // Check to see if there are any folding opportunities present with |
| 1333 | // operands multiplied by constant values. |
| 1334 | if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) { |
| 1335 | uint64_t BitWidth = getTypeSizeInBits(Ty); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1336 | DenseMap<const SCEV *, APInt> M; |
| 1337 | SmallVector<const SCEV *, 8> NewOps; |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1338 | APInt AccumulatedConstant(BitWidth, 0); |
| 1339 | if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, |
| 1340 | Ops, APInt(BitWidth, 1), *this)) { |
| 1341 | // Some interesting folding opportunity is present, so its worthwhile to |
| 1342 | // re-generate the operands list. Group the operands by constant scale, |
| 1343 | // to avoid multiplying by the same constant scale multiple times. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1344 | std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists; |
| 1345 | for (SmallVector<const SCEV *, 8>::iterator I = NewOps.begin(), |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1346 | E = NewOps.end(); I != E; ++I) |
| 1347 | MulOpLists[M.find(*I)->second].push_back(*I); |
| 1348 | // Re-generate the operands list. |
| 1349 | Ops.clear(); |
| 1350 | if (AccumulatedConstant != 0) |
| 1351 | Ops.push_back(getConstant(AccumulatedConstant)); |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1352 | for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator |
| 1353 | I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I) |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1354 | if (I->first != 0) |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1355 | Ops.push_back(getMulExpr(getConstant(I->first), |
| 1356 | getAddExpr(I->second))); |
Dan Gohman | bd59d7b | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1357 | if (Ops.empty()) |
| 1358 | return getIntegerSCEV(0, Ty); |
| 1359 | if (Ops.size() == 1) |
| 1360 | return Ops[0]; |
| 1361 | return getAddExpr(Ops); |
| 1362 | } |
| 1363 | } |
| 1364 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1365 | // If we are adding something to a multiply expression, make sure the |
| 1366 | // something is not already an operand of the multiply. If so, merge it into |
| 1367 | // the multiply. |
| 1368 | for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1369 | const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1370 | for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1371 | const SCEV *MulOpSCEV = Mul->getOperand(MulOp); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1372 | for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1373 | if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(Ops[AddOp])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1374 | // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1375 | const SCEV *InnerMul = Mul->getOperand(MulOp == 0); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1376 | if (Mul->getNumOperands() != 2) { |
| 1377 | // If the multiply has more than two operands, we must get the |
| 1378 | // Y*Z term. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1379 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), Mul->op_end()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1380 | MulOps.erase(MulOps.begin()+MulOp); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1381 | InnerMul = getMulExpr(MulOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1382 | } |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1383 | const SCEV *One = getIntegerSCEV(1, Ty); |
| 1384 | const SCEV *AddOne = getAddExpr(InnerMul, One); |
| 1385 | const SCEV *OuterMul = getMulExpr(AddOne, Ops[AddOp]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1386 | if (Ops.size() == 2) return OuterMul; |
| 1387 | if (AddOp < Idx) { |
| 1388 | Ops.erase(Ops.begin()+AddOp); |
| 1389 | Ops.erase(Ops.begin()+Idx-1); |
| 1390 | } else { |
| 1391 | Ops.erase(Ops.begin()+Idx); |
| 1392 | Ops.erase(Ops.begin()+AddOp-1); |
| 1393 | } |
| 1394 | Ops.push_back(OuterMul); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1395 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1396 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1397 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1398 | // Check this multiply against other multiplies being added together. |
| 1399 | for (unsigned OtherMulIdx = Idx+1; |
| 1400 | OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]); |
| 1401 | ++OtherMulIdx) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1402 | const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1403 | // If MulOp occurs in OtherMul, we can fold the two multiplies |
| 1404 | // together. |
| 1405 | for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); |
| 1406 | OMulOp != e; ++OMulOp) |
| 1407 | if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { |
| 1408 | // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E)) |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1409 | const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1410 | if (Mul->getNumOperands() != 2) { |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1411 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), |
| 1412 | Mul->op_end()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1413 | MulOps.erase(MulOps.begin()+MulOp); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1414 | InnerMul1 = getMulExpr(MulOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1415 | } |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1416 | const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1417 | if (OtherMul->getNumOperands() != 2) { |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1418 | SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(), |
| 1419 | OtherMul->op_end()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1420 | MulOps.erase(MulOps.begin()+OMulOp); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1421 | InnerMul2 = getMulExpr(MulOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1422 | } |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1423 | const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2); |
| 1424 | const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1425 | if (Ops.size() == 2) return OuterMul; |
| 1426 | Ops.erase(Ops.begin()+Idx); |
| 1427 | Ops.erase(Ops.begin()+OtherMulIdx-1); |
| 1428 | Ops.push_back(OuterMul); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1429 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | // If there are any add recurrences in the operands list, see if any other |
| 1436 | // added values are loop invariant. If so, we can fold them into the |
| 1437 | // recurrence. |
| 1438 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1439 | ++Idx; |
| 1440 | |
| 1441 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1442 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1443 | // Scan all of the other operands to this add and add them to the vector if |
| 1444 | // they are loop invariant w.r.t. the recurrence. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1445 | SmallVector<const SCEV *, 8> LIOps; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1446 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1447 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1448 | if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { |
| 1449 | LIOps.push_back(Ops[i]); |
| 1450 | Ops.erase(Ops.begin()+i); |
| 1451 | --i; --e; |
| 1452 | } |
| 1453 | |
| 1454 | // If we found some loop invariants, fold them into the recurrence. |
| 1455 | if (!LIOps.empty()) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1456 | // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step} |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1457 | LIOps.push_back(AddRec->getStart()); |
| 1458 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1459 | SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(), |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1460 | AddRec->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1461 | AddRecOps[0] = getAddExpr(LIOps); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1462 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1463 | const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRec->getLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1464 | // If all of the other operands were loop invariant, we are done. |
| 1465 | if (Ops.size() == 1) return NewRec; |
| 1466 | |
| 1467 | // Otherwise, add the folded AddRec by the non-liv parts. |
| 1468 | for (unsigned i = 0;; ++i) |
| 1469 | if (Ops[i] == AddRec) { |
| 1470 | Ops[i] = NewRec; |
| 1471 | break; |
| 1472 | } |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1473 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1477 | // there are multiple AddRec's with the same loop induction variable being |
| 1478 | // added together. If so, we can fold them. |
| 1479 | for (unsigned OtherIdx = Idx+1; |
| 1480 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) |
| 1481 | if (OtherIdx != Idx) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1482 | const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1483 | if (AddRec->getLoop() == OtherAddRec->getLoop()) { |
| 1484 | // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D} |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1485 | SmallVector<const SCEV *, 4> NewOps(AddRec->op_begin(), |
| 1486 | AddRec->op_end()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1487 | for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) { |
| 1488 | if (i >= NewOps.size()) { |
| 1489 | NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i, |
| 1490 | OtherAddRec->op_end()); |
| 1491 | break; |
| 1492 | } |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1493 | NewOps[i] = getAddExpr(NewOps[i], OtherAddRec->getOperand(i)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1494 | } |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1495 | const SCEV *NewAddRec = getAddRecExpr(NewOps, AddRec->getLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1496 | |
| 1497 | if (Ops.size() == 2) return NewAddRec; |
| 1498 | |
| 1499 | Ops.erase(Ops.begin()+Idx); |
| 1500 | Ops.erase(Ops.begin()+OtherIdx-1); |
| 1501 | Ops.push_back(NewAddRec); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1502 | return getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1507 | // next one. |
| 1508 | } |
| 1509 | |
| 1510 | // Okay, it looks like we really DO need an add expr. Check to see if we |
| 1511 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1512 | FoldingSetNodeID ID; |
| 1513 | ID.AddInteger(scAddExpr); |
| 1514 | ID.AddInteger(Ops.size()); |
| 1515 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1516 | ID.AddPointer(Ops[i]); |
| 1517 | void *IP = 0; |
| 1518 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1519 | SCEVAddExpr *S = SCEVAllocator.Allocate<SCEVAddExpr>(); |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1520 | new (S) SCEVAddExpr(ID, Ops); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1521 | UniqueSCEVs.InsertNode(S, IP); |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1522 | if (HasNUW) S->setHasNoUnsignedWrap(true); |
| 1523 | if (HasNSW) S->setHasNoSignedWrap(true); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1524 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1528 | /// getMulExpr - Get a canonical multiply expression, or something simpler if |
| 1529 | /// possible. |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1530 | const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops, |
| 1531 | bool HasNUW, bool HasNSW) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1532 | assert(!Ops.empty() && "Cannot get empty mul!"); |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1533 | #ifndef NDEBUG |
| 1534 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1535 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1536 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1537 | "SCEVMulExpr operand types don't match!"); |
| 1538 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1539 | |
| 1540 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1541 | GroupByComplexity(Ops, LI); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1542 | |
| 1543 | // If there are any constants, fold them together. |
| 1544 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1545 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1546 | |
| 1547 | // C1*(C2+V) -> C1*C2 + C1*V |
| 1548 | if (Ops.size() == 2) |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1549 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1550 | if (Add->getNumOperands() == 2 && |
| 1551 | isa<SCEVConstant>(Add->getOperand(0))) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1552 | return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)), |
| 1553 | getMulExpr(LHSC, Add->getOperand(1))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1554 | |
| 1555 | |
| 1556 | ++Idx; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1557 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1558 | // We found two constants, fold them together! |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1559 | ConstantInt *Fold = ConstantInt::get(getContext(), |
| 1560 | LHSC->getValue()->getValue() * |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1561 | RHSC->getValue()->getValue()); |
| 1562 | Ops[0] = getConstant(Fold); |
| 1563 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1564 | if (Ops.size() == 1) return Ops[0]; |
| 1565 | LHSC = cast<SCEVConstant>(Ops[0]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | // If we are left with a constant one being multiplied, strip it off. |
| 1569 | if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) { |
| 1570 | Ops.erase(Ops.begin()); |
| 1571 | --Idx; |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1572 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1573 | // If we have a multiply of zero, it will always be zero. |
| 1574 | return Ops[0]; |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | // Skip over the add expression until we get to a multiply. |
| 1579 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) |
| 1580 | ++Idx; |
| 1581 | |
| 1582 | if (Ops.size() == 1) |
| 1583 | return Ops[0]; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1584 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1585 | // If there are mul operands inline them all into this expression. |
| 1586 | if (Idx < Ops.size()) { |
| 1587 | bool DeletedMul = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1588 | while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1589 | // If we have an mul, expand the mul operands onto the end of the operands |
| 1590 | // list. |
| 1591 | Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end()); |
| 1592 | Ops.erase(Ops.begin()+Idx); |
| 1593 | DeletedMul = true; |
| 1594 | } |
| 1595 | |
| 1596 | // If we deleted at least one mul, we added operands to the end of the list, |
| 1597 | // and they are not necessarily sorted. Recurse to resort and resimplify |
| 1598 | // any operands we just aquired. |
| 1599 | if (DeletedMul) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1600 | return getMulExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | // If there are any add recurrences in the operands list, see if any other |
| 1604 | // added values are loop invariant. If so, we can fold them into the |
| 1605 | // recurrence. |
| 1606 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) |
| 1607 | ++Idx; |
| 1608 | |
| 1609 | // Scan over all recurrences, trying to fold loop invariants into them. |
| 1610 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { |
| 1611 | // Scan all of the other operands to this mul and add them to the vector if |
| 1612 | // they are loop invariant w.r.t. the recurrence. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1613 | SmallVector<const SCEV *, 8> LIOps; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1614 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1615 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1616 | if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { |
| 1617 | LIOps.push_back(Ops[i]); |
| 1618 | Ops.erase(Ops.begin()+i); |
| 1619 | --i; --e; |
| 1620 | } |
| 1621 | |
| 1622 | // If we found some loop invariants, fold them into the recurrence. |
| 1623 | if (!LIOps.empty()) { |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1624 | // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step} |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1625 | SmallVector<const SCEV *, 4> NewOps; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1626 | NewOps.reserve(AddRec->getNumOperands()); |
| 1627 | if (LIOps.size() == 1) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1628 | const SCEV *Scale = LIOps[0]; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1629 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1630 | NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1631 | } else { |
| 1632 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1633 | SmallVector<const SCEV *, 4> MulOps(LIOps.begin(), LIOps.end()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1634 | MulOps.push_back(AddRec->getOperand(i)); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1635 | NewOps.push_back(getMulExpr(MulOps)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1636 | } |
| 1637 | } |
| 1638 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1639 | const SCEV *NewRec = getAddRecExpr(NewOps, AddRec->getLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1640 | |
| 1641 | // If all of the other operands were loop invariant, we are done. |
| 1642 | if (Ops.size() == 1) return NewRec; |
| 1643 | |
| 1644 | // Otherwise, multiply the folded AddRec by the non-liv parts. |
| 1645 | for (unsigned i = 0;; ++i) |
| 1646 | if (Ops[i] == AddRec) { |
| 1647 | Ops[i] = NewRec; |
| 1648 | break; |
| 1649 | } |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1650 | return getMulExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | // Okay, if there weren't any loop invariants to be folded, check to see if |
| 1654 | // there are multiple AddRec's with the same loop induction variable being |
| 1655 | // multiplied together. If so, we can fold them. |
| 1656 | for (unsigned OtherIdx = Idx+1; |
| 1657 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) |
| 1658 | if (OtherIdx != Idx) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1659 | const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1660 | if (AddRec->getLoop() == OtherAddRec->getLoop()) { |
| 1661 | // F * G --> {A,+,B} * {C,+,D} --> {A*C,+,F*D + G*B + B*D} |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1662 | const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1663 | const SCEV *NewStart = getMulExpr(F->getStart(), |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1664 | G->getStart()); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1665 | const SCEV *B = F->getStepRecurrence(*this); |
| 1666 | const SCEV *D = G->getStepRecurrence(*this); |
| 1667 | const SCEV *NewStep = getAddExpr(getMulExpr(F, D), |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1668 | getMulExpr(G, B), |
| 1669 | getMulExpr(B, D)); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1670 | const SCEV *NewAddRec = getAddRecExpr(NewStart, NewStep, |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1671 | F->getLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1672 | if (Ops.size() == 2) return NewAddRec; |
| 1673 | |
| 1674 | Ops.erase(Ops.begin()+Idx); |
| 1675 | Ops.erase(Ops.begin()+OtherIdx-1); |
| 1676 | Ops.push_back(NewAddRec); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1677 | return getMulExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | // Otherwise couldn't fold anything into this recurrence. Move onto the |
| 1682 | // next one. |
| 1683 | } |
| 1684 | |
| 1685 | // Okay, it looks like we really DO need an mul expr. Check to see if we |
| 1686 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1687 | FoldingSetNodeID ID; |
| 1688 | ID.AddInteger(scMulExpr); |
| 1689 | ID.AddInteger(Ops.size()); |
| 1690 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1691 | ID.AddPointer(Ops[i]); |
| 1692 | void *IP = 0; |
| 1693 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1694 | SCEVMulExpr *S = SCEVAllocator.Allocate<SCEVMulExpr>(); |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1695 | new (S) SCEVMulExpr(ID, Ops); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1696 | UniqueSCEVs.InsertNode(S, IP); |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1697 | if (HasNUW) S->setHasNoUnsignedWrap(true); |
| 1698 | if (HasNSW) S->setHasNoSignedWrap(true); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1699 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Andreas Bolka | 8a11c98 | 2009-08-07 22:55:26 +0000 | [diff] [blame] | 1702 | /// getUDivExpr - Get a canonical unsigned division expression, or something |
| 1703 | /// simpler if possible. |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1704 | const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS, |
| 1705 | const SCEV *RHS) { |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1706 | assert(getEffectiveSCEVType(LHS->getType()) == |
| 1707 | getEffectiveSCEVType(RHS->getType()) && |
| 1708 | "SCEVUDivExpr operand types don't match!"); |
| 1709 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1710 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1711 | if (RHSC->getValue()->equalsInt(1)) |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 1712 | return LHS; // X udiv 1 --> x |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1713 | if (RHSC->isZero()) |
| 1714 | return getIntegerSCEV(0, LHS->getType()); // value is undefined |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1715 | |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1716 | // Determine if the division can be folded into the operands of |
| 1717 | // its operands. |
| 1718 | // TODO: Generalize this to non-constants by using known-bits information. |
| 1719 | const Type *Ty = LHS->getType(); |
| 1720 | unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros(); |
| 1721 | unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ; |
| 1722 | // For non-power-of-two values, effectively round the value up to the |
| 1723 | // nearest power of two. |
| 1724 | if (!RHSC->getValue()->getValue().isPowerOf2()) |
| 1725 | ++MaxShiftAmt; |
| 1726 | const IntegerType *ExtTy = |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1727 | IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt); |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1728 | // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. |
| 1729 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) |
| 1730 | if (const SCEVConstant *Step = |
| 1731 | dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) |
| 1732 | if (!Step->getValue()->getValue() |
| 1733 | .urem(RHSC->getValue()->getValue()) && |
Dan Gohman | b028593 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1734 | getZeroExtendExpr(AR, ExtTy) == |
| 1735 | getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), |
| 1736 | getZeroExtendExpr(Step, ExtTy), |
| 1737 | AR->getLoop())) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1738 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1739 | for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i) |
| 1740 | Operands.push_back(getUDivExpr(AR->getOperand(i), RHS)); |
| 1741 | return getAddRecExpr(Operands, AR->getLoop()); |
| 1742 | } |
| 1743 | // (A*B)/C --> A*(B/C) if safe and B/C can be folded. |
Dan Gohman | b028593 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1744 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1745 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | b028593 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1746 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) |
| 1747 | Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy)); |
| 1748 | if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1749 | // Find an operand that's safely divisible. |
| 1750 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1751 | const SCEV *Op = M->getOperand(i); |
| 1752 | const SCEV *Div = getUDivExpr(Op, RHSC); |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1753 | if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1754 | const SmallVectorImpl<const SCEV *> &MOperands = M->getOperands(); |
| 1755 | Operands = SmallVector<const SCEV *, 4>(MOperands.begin(), |
Dan Gohman | a82752c | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1756 | MOperands.end()); |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1757 | Operands[i] = Div; |
| 1758 | return getMulExpr(Operands); |
| 1759 | } |
| 1760 | } |
Dan Gohman | b028593 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1761 | } |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1762 | // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded. |
Dan Gohman | b028593 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1763 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1764 | SmallVector<const SCEV *, 4> Operands; |
Dan Gohman | b028593 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1765 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) |
| 1766 | Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy)); |
| 1767 | if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { |
| 1768 | Operands.clear(); |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1769 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1770 | const SCEV *Op = getUDivExpr(A->getOperand(i), RHS); |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1771 | if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i)) |
| 1772 | break; |
| 1773 | Operands.push_back(Op); |
| 1774 | } |
| 1775 | if (Operands.size() == A->getNumOperands()) |
| 1776 | return getAddExpr(Operands); |
| 1777 | } |
Dan Gohman | b028593 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1778 | } |
Dan Gohman | 185cf03 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1779 | |
| 1780 | // Fold if both operands are constant. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1781 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1782 | Constant *LHSCV = LHSC->getValue(); |
| 1783 | Constant *RHSCV = RHSC->getValue(); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1784 | return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV, |
Dan Gohman | b8be8b7 | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 1785 | RHSCV))); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1786 | } |
| 1787 | } |
| 1788 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1789 | FoldingSetNodeID ID; |
| 1790 | ID.AddInteger(scUDivExpr); |
| 1791 | ID.AddPointer(LHS); |
| 1792 | ID.AddPointer(RHS); |
| 1793 | void *IP = 0; |
| 1794 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1795 | SCEV *S = SCEVAllocator.Allocate<SCEVUDivExpr>(); |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1796 | new (S) SCEVUDivExpr(ID, LHS, RHS); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1797 | UniqueSCEVs.InsertNode(S, IP); |
| 1798 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1802 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 1803 | /// Simplify the expression as much as possible. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1804 | const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1805 | const SCEV *Step, const Loop *L, |
| 1806 | bool HasNUW, bool HasNSW) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1807 | SmallVector<const SCEV *, 4> Operands; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1808 | Operands.push_back(Start); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1809 | if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step)) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1810 | if (StepChrec->getLoop() == L) { |
| 1811 | Operands.insert(Operands.end(), StepChrec->op_begin(), |
| 1812 | StepChrec->op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1813 | return getAddRecExpr(Operands, L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | Operands.push_back(Step); |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1817 | return getAddRecExpr(Operands, L, HasNUW, HasNSW); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1820 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. |
| 1821 | /// Simplify the expression as much as possible. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1822 | const SCEV * |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1823 | ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands, |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1824 | const Loop *L, |
| 1825 | bool HasNUW, bool HasNSW) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1826 | if (Operands.size() == 1) return Operands[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1827 | #ifndef NDEBUG |
| 1828 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) |
| 1829 | assert(getEffectiveSCEVType(Operands[i]->getType()) == |
| 1830 | getEffectiveSCEVType(Operands[0]->getType()) && |
| 1831 | "SCEVAddRecExpr operand types don't match!"); |
| 1832 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1833 | |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1834 | if (Operands.back()->isZero()) { |
| 1835 | Operands.pop_back(); |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1836 | return getAddRecExpr(Operands, L, HasNUW, HasNSW); // {X,+,0} --> X |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1837 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1838 | |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1839 | // Canonicalize nested AddRecs in by nesting them in order of loop depth. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1840 | if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) { |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 1841 | const Loop *NestedLoop = NestedAR->getLoop(); |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1842 | if (L->getLoopDepth() < NestedLoop->getLoopDepth()) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1843 | SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(), |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 1844 | NestedAR->op_end()); |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1845 | Operands[0] = NestedAR->getStart(); |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 1846 | // AddRecs require their operands be loop-invariant with respect to their |
| 1847 | // loops. Don't perform this transformation if it would break this |
| 1848 | // requirement. |
| 1849 | bool AllInvariant = true; |
| 1850 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 1851 | if (!Operands[i]->isLoopInvariant(L)) { |
| 1852 | AllInvariant = false; |
| 1853 | break; |
| 1854 | } |
| 1855 | if (AllInvariant) { |
| 1856 | NestedOperands[0] = getAddRecExpr(Operands, L); |
| 1857 | AllInvariant = true; |
| 1858 | for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i) |
| 1859 | if (!NestedOperands[i]->isLoopInvariant(NestedLoop)) { |
| 1860 | AllInvariant = false; |
| 1861 | break; |
| 1862 | } |
| 1863 | if (AllInvariant) |
| 1864 | // Ok, both add recurrences are valid after the transformation. |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1865 | return getAddRecExpr(NestedOperands, NestedLoop, HasNUW, HasNSW); |
Dan Gohman | 9a80b45 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 1866 | } |
| 1867 | // Reset Operands to its original state. |
| 1868 | Operands[0] = NestedAR; |
Dan Gohman | d9cc749 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1869 | } |
| 1870 | } |
| 1871 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1872 | FoldingSetNodeID ID; |
| 1873 | ID.AddInteger(scAddRecExpr); |
| 1874 | ID.AddInteger(Operands.size()); |
| 1875 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 1876 | ID.AddPointer(Operands[i]); |
| 1877 | ID.AddPointer(L); |
| 1878 | void *IP = 0; |
| 1879 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1880 | SCEVAddRecExpr *S = SCEVAllocator.Allocate<SCEVAddRecExpr>(); |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1881 | new (S) SCEVAddRecExpr(ID, Operands, L); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1882 | UniqueSCEVs.InsertNode(S, IP); |
Dan Gohman | 3645b01 | 2009-10-09 00:10:36 +0000 | [diff] [blame] | 1883 | if (HasNUW) S->setHasNoUnsignedWrap(true); |
| 1884 | if (HasNSW) S->setHasNoSignedWrap(true); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1885 | return S; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1888 | const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, |
| 1889 | const SCEV *RHS) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1890 | SmallVector<const SCEV *, 2> Ops; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1891 | Ops.push_back(LHS); |
| 1892 | Ops.push_back(RHS); |
| 1893 | return getSMaxExpr(Ops); |
| 1894 | } |
| 1895 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1896 | const SCEV * |
| 1897 | ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1898 | assert(!Ops.empty() && "Cannot get empty smax!"); |
| 1899 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1900 | #ifndef NDEBUG |
| 1901 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1902 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 1903 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 1904 | "SCEVSMaxExpr operand types don't match!"); |
| 1905 | #endif |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1906 | |
| 1907 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1908 | GroupByComplexity(Ops, LI); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1909 | |
| 1910 | // If there are any constants, fold them together. |
| 1911 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1912 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1913 | ++Idx; |
| 1914 | assert(Idx < Ops.size()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1915 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1916 | // We found two constants, fold them together! |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1917 | ConstantInt *Fold = ConstantInt::get(getContext(), |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1918 | APIntOps::smax(LHSC->getValue()->getValue(), |
| 1919 | RHSC->getValue()->getValue())); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1920 | Ops[0] = getConstant(Fold); |
| 1921 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 1922 | if (Ops.size() == 1) return Ops[0]; |
| 1923 | LHSC = cast<SCEVConstant>(Ops[0]); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1926 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1927 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) { |
| 1928 | Ops.erase(Ops.begin()); |
| 1929 | --Idx; |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1930 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) { |
| 1931 | // If we have an smax with a constant maximum-int, it will always be |
| 1932 | // maximum-int. |
| 1933 | return Ops[0]; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | if (Ops.size() == 1) return Ops[0]; |
| 1938 | |
| 1939 | // Find the first SMax |
| 1940 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr) |
| 1941 | ++Idx; |
| 1942 | |
| 1943 | // Check to see if one of the operands is an SMax. If so, expand its operands |
| 1944 | // onto our operand list, and recurse to simplify. |
| 1945 | if (Idx < Ops.size()) { |
| 1946 | bool DeletedSMax = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1947 | while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1948 | Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end()); |
| 1949 | Ops.erase(Ops.begin()+Idx); |
| 1950 | DeletedSMax = true; |
| 1951 | } |
| 1952 | |
| 1953 | if (DeletedSMax) |
| 1954 | return getSMaxExpr(Ops); |
| 1955 | } |
| 1956 | |
| 1957 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 1958 | // so, delete one. Since we sorted the list, these values are required to |
| 1959 | // be adjacent. |
| 1960 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 1961 | if (Ops[i] == Ops[i+1]) { // X smax Y smax Y --> X smax Y |
| 1962 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 1963 | --i; --e; |
| 1964 | } |
| 1965 | |
| 1966 | if (Ops.size() == 1) return Ops[0]; |
| 1967 | |
| 1968 | assert(!Ops.empty() && "Reduced smax down to nothing!"); |
| 1969 | |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1970 | // Okay, it looks like we really DO need an smax expr. Check to see if we |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1971 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1972 | FoldingSetNodeID ID; |
| 1973 | ID.AddInteger(scSMaxExpr); |
| 1974 | ID.AddInteger(Ops.size()); |
| 1975 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1976 | ID.AddPointer(Ops[i]); |
| 1977 | void *IP = 0; |
| 1978 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 1979 | SCEV *S = SCEVAllocator.Allocate<SCEVSMaxExpr>(); |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1980 | new (S) SCEVSMaxExpr(ID, Ops); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1981 | UniqueSCEVs.InsertNode(S, IP); |
| 1982 | return S; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1985 | const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, |
| 1986 | const SCEV *RHS) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1987 | SmallVector<const SCEV *, 2> Ops; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1988 | Ops.push_back(LHS); |
| 1989 | Ops.push_back(RHS); |
| 1990 | return getUMaxExpr(Ops); |
| 1991 | } |
| 1992 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1993 | const SCEV * |
| 1994 | ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1995 | assert(!Ops.empty() && "Cannot get empty umax!"); |
| 1996 | if (Ops.size() == 1) return Ops[0]; |
Dan Gohman | f78a978 | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1997 | #ifndef NDEBUG |
| 1998 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) |
| 1999 | assert(getEffectiveSCEVType(Ops[i]->getType()) == |
| 2000 | getEffectiveSCEVType(Ops[0]->getType()) && |
| 2001 | "SCEVUMaxExpr operand types don't match!"); |
| 2002 | #endif |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2003 | |
| 2004 | // Sort by complexity, this groups all similar expression types together. |
Dan Gohman | 7286130 | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 2005 | GroupByComplexity(Ops, LI); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2006 | |
| 2007 | // If there are any constants, fold them together. |
| 2008 | unsigned Idx = 0; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2009 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2010 | ++Idx; |
| 2011 | assert(Idx < Ops.size()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2012 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2013 | // We found two constants, fold them together! |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2014 | ConstantInt *Fold = ConstantInt::get(getContext(), |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2015 | APIntOps::umax(LHSC->getValue()->getValue(), |
| 2016 | RHSC->getValue()->getValue())); |
| 2017 | Ops[0] = getConstant(Fold); |
| 2018 | Ops.erase(Ops.begin()+1); // Erase the folded element |
| 2019 | if (Ops.size() == 1) return Ops[0]; |
| 2020 | LHSC = cast<SCEVConstant>(Ops[0]); |
| 2021 | } |
| 2022 | |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 2023 | // If we are left with a constant minimum-int, strip it off. |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2024 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) { |
| 2025 | Ops.erase(Ops.begin()); |
| 2026 | --Idx; |
Dan Gohman | e5aceed | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 2027 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) { |
| 2028 | // If we have an umax with a constant maximum-int, it will always be |
| 2029 | // maximum-int. |
| 2030 | return Ops[0]; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | if (Ops.size() == 1) return Ops[0]; |
| 2035 | |
| 2036 | // Find the first UMax |
| 2037 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr) |
| 2038 | ++Idx; |
| 2039 | |
| 2040 | // Check to see if one of the operands is a UMax. If so, expand its operands |
| 2041 | // onto our operand list, and recurse to simplify. |
| 2042 | if (Idx < Ops.size()) { |
| 2043 | bool DeletedUMax = false; |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2044 | while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2045 | Ops.insert(Ops.end(), UMax->op_begin(), UMax->op_end()); |
| 2046 | Ops.erase(Ops.begin()+Idx); |
| 2047 | DeletedUMax = true; |
| 2048 | } |
| 2049 | |
| 2050 | if (DeletedUMax) |
| 2051 | return getUMaxExpr(Ops); |
| 2052 | } |
| 2053 | |
| 2054 | // Okay, check to see if the same value occurs in the operand list twice. If |
| 2055 | // so, delete one. Since we sorted the list, these values are required to |
| 2056 | // be adjacent. |
| 2057 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) |
| 2058 | if (Ops[i] == Ops[i+1]) { // X umax Y umax Y --> X umax Y |
| 2059 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); |
| 2060 | --i; --e; |
| 2061 | } |
| 2062 | |
| 2063 | if (Ops.size() == 1) return Ops[0]; |
| 2064 | |
| 2065 | assert(!Ops.empty() && "Reduced umax down to nothing!"); |
| 2066 | |
| 2067 | // Okay, it looks like we really DO need a umax expr. Check to see if we |
| 2068 | // already have one, otherwise create a new one. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2069 | FoldingSetNodeID ID; |
| 2070 | ID.AddInteger(scUMaxExpr); |
| 2071 | ID.AddInteger(Ops.size()); |
| 2072 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 2073 | ID.AddPointer(Ops[i]); |
| 2074 | void *IP = 0; |
| 2075 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 2076 | SCEV *S = SCEVAllocator.Allocate<SCEVUMaxExpr>(); |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 2077 | new (S) SCEVUMaxExpr(ID, Ops); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2078 | UniqueSCEVs.InsertNode(S, IP); |
| 2079 | return S; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2082 | const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS, |
| 2083 | const SCEV *RHS) { |
Dan Gohman | f9a9a99 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2084 | // ~smax(~x, ~y) == smin(x, y). |
| 2085 | return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 2086 | } |
| 2087 | |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2088 | const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS, |
| 2089 | const SCEV *RHS) { |
Dan Gohman | f9a9a99 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2090 | // ~umax(~x, ~y) == umin(x, y) |
| 2091 | return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); |
| 2092 | } |
| 2093 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2094 | const SCEV *ScalarEvolution::getFieldOffsetExpr(const StructType *STy, |
| 2095 | unsigned FieldNo) { |
| 2096 | // If we have TargetData we can determine the constant offset. |
| 2097 | if (TD) { |
| 2098 | const Type *IntPtrTy = TD->getIntPtrType(getContext()); |
| 2099 | const StructLayout &SL = *TD->getStructLayout(STy); |
| 2100 | uint64_t Offset = SL.getElementOffset(FieldNo); |
| 2101 | return getIntegerSCEV(Offset, IntPtrTy); |
| 2102 | } |
| 2103 | |
| 2104 | // Field 0 is always at offset 0. |
| 2105 | if (FieldNo == 0) { |
| 2106 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy)); |
| 2107 | return getIntegerSCEV(0, Ty); |
| 2108 | } |
| 2109 | |
| 2110 | // Okay, it looks like we really DO need an offsetof expr. Check to see if we |
| 2111 | // already have one, otherwise create a new one. |
| 2112 | FoldingSetNodeID ID; |
| 2113 | ID.AddInteger(scFieldOffset); |
| 2114 | ID.AddPointer(STy); |
| 2115 | ID.AddInteger(FieldNo); |
| 2116 | void *IP = 0; |
| 2117 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 2118 | SCEV *S = SCEVAllocator.Allocate<SCEVFieldOffsetExpr>(); |
| 2119 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy)); |
| 2120 | new (S) SCEVFieldOffsetExpr(ID, Ty, STy, FieldNo); |
| 2121 | UniqueSCEVs.InsertNode(S, IP); |
| 2122 | return S; |
| 2123 | } |
| 2124 | |
| 2125 | const SCEV *ScalarEvolution::getAllocSizeExpr(const Type *AllocTy) { |
| 2126 | // If we have TargetData we can determine the constant size. |
| 2127 | if (TD && AllocTy->isSized()) { |
| 2128 | const Type *IntPtrTy = TD->getIntPtrType(getContext()); |
| 2129 | return getIntegerSCEV(TD->getTypeAllocSize(AllocTy), IntPtrTy); |
| 2130 | } |
| 2131 | |
| 2132 | // Expand an array size into the element size times the number |
| 2133 | // of elements. |
| 2134 | if (const ArrayType *ATy = dyn_cast<ArrayType>(AllocTy)) { |
| 2135 | const SCEV *E = getAllocSizeExpr(ATy->getElementType()); |
| 2136 | return getMulExpr( |
| 2137 | E, getConstant(ConstantInt::get(cast<IntegerType>(E->getType()), |
| 2138 | ATy->getNumElements()))); |
| 2139 | } |
| 2140 | |
| 2141 | // Expand a vector size into the element size times the number |
| 2142 | // of elements. |
| 2143 | if (const VectorType *VTy = dyn_cast<VectorType>(AllocTy)) { |
| 2144 | const SCEV *E = getAllocSizeExpr(VTy->getElementType()); |
| 2145 | return getMulExpr( |
| 2146 | E, getConstant(ConstantInt::get(cast<IntegerType>(E->getType()), |
| 2147 | VTy->getNumElements()))); |
| 2148 | } |
| 2149 | |
| 2150 | // Okay, it looks like we really DO need a sizeof expr. Check to see if we |
| 2151 | // already have one, otherwise create a new one. |
| 2152 | FoldingSetNodeID ID; |
| 2153 | ID.AddInteger(scAllocSize); |
| 2154 | ID.AddPointer(AllocTy); |
| 2155 | void *IP = 0; |
| 2156 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 2157 | SCEV *S = SCEVAllocator.Allocate<SCEVAllocSizeExpr>(); |
| 2158 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy)); |
| 2159 | new (S) SCEVAllocSizeExpr(ID, Ty, AllocTy); |
| 2160 | UniqueSCEVs.InsertNode(S, IP); |
| 2161 | return S; |
| 2162 | } |
| 2163 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2164 | const SCEV *ScalarEvolution::getUnknown(Value *V) { |
Dan Gohman | 6bbcba1 | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2165 | // Don't attempt to do anything other than create a SCEVUnknown object |
| 2166 | // here. createSCEV only calls getUnknown after checking for all other |
| 2167 | // interesting possibilities, and any other code that calls getUnknown |
| 2168 | // is doing so in order to hide a value from SCEV canonicalization. |
| 2169 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2170 | FoldingSetNodeID ID; |
| 2171 | ID.AddInteger(scUnknown); |
| 2172 | ID.AddPointer(V); |
| 2173 | void *IP = 0; |
| 2174 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; |
| 2175 | SCEV *S = SCEVAllocator.Allocate<SCEVUnknown>(); |
Dan Gohman | c050fd9 | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 2176 | new (S) SCEVUnknown(ID, V); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2177 | UniqueSCEVs.InsertNode(S, IP); |
| 2178 | return S; |
Chris Lattner | 0a7f98c | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2181 | //===----------------------------------------------------------------------===// |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2182 | // Basic SCEV Analysis and PHI Idiom Recognition Code |
| 2183 | // |
| 2184 | |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2185 | /// isSCEVable - Test if values of the given type are analyzable within |
| 2186 | /// the SCEV framework. This primarily includes integer types, and it |
| 2187 | /// can optionally include pointer types if the ScalarEvolution class |
| 2188 | /// has access to target-specific information. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2189 | bool ScalarEvolution::isSCEVable(const Type *Ty) const { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2190 | // Integers and pointers are always SCEVable. |
| 2191 | return Ty->isInteger() || isa<PointerType>(Ty); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | /// getTypeSizeInBits - Return the size in bits of the specified type, |
| 2195 | /// for which isSCEVable must return true. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2196 | uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2197 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2198 | |
| 2199 | // If we have a TargetData, use it! |
| 2200 | if (TD) |
| 2201 | return TD->getTypeSizeInBits(Ty); |
| 2202 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2203 | // Integer types have fixed sizes. |
| 2204 | if (Ty->isInteger()) |
| 2205 | return Ty->getPrimitiveSizeInBits(); |
| 2206 | |
| 2207 | // The only other support type is pointer. Without TargetData, conservatively |
| 2208 | // assume pointers are 64-bit. |
| 2209 | assert(isa<PointerType>(Ty) && "isSCEVable permitted a non-SCEVable type!"); |
| 2210 | return 64; |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | /// getEffectiveSCEVType - Return a type with the same bitwidth as |
| 2214 | /// the given type and which represents how SCEV will treat the given |
| 2215 | /// type, for which isSCEVable must return true. For pointer types, |
| 2216 | /// this is the pointer-sized integer type. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2217 | const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2218 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); |
| 2219 | |
| 2220 | if (Ty->isInteger()) |
| 2221 | return Ty; |
| 2222 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2223 | // The only other support type is pointer. |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2224 | assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!"); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2225 | if (TD) return TD->getIntPtrType(getContext()); |
| 2226 | |
| 2227 | // Without TargetData, conservatively assume pointers are 64-bit. |
| 2228 | return Type::getInt64Ty(getContext()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2229 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2230 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2231 | const SCEV *ScalarEvolution::getCouldNotCompute() { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2232 | return &CouldNotCompute; |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2235 | /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the |
| 2236 | /// expression and create a new one. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2237 | const SCEV *ScalarEvolution::getSCEV(Value *V) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2238 | assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2239 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2240 | std::map<SCEVCallbackVH, const SCEV *>::iterator I = Scalars.find(V); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2241 | if (I != Scalars.end()) return I->second; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2242 | const SCEV *S = createSCEV(V); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2243 | Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2244 | return S; |
| 2245 | } |
| 2246 | |
Dan Gohman | 6bbcba1 | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2247 | /// getIntegerSCEV - Given a SCEVable type, create a constant for the |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2248 | /// specified signed integer value and return a SCEV for the constant. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2249 | const SCEV *ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) { |
Dan Gohman | 6bbcba1 | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2250 | const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty)); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2251 | return getConstant(ConstantInt::get(ITy, Val)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V |
| 2255 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2256 | const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) { |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2257 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Owen Anderson | 0a5372e | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2258 | return getConstant( |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2259 | cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue()))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2260 | |
| 2261 | const Type *Ty = V->getType(); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2262 | Ty = getEffectiveSCEVType(Ty); |
Owen Anderson | 73c6b71 | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2263 | return getMulExpr(V, |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2264 | getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty)))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2268 | const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) { |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2269 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) |
Owen Anderson | 73c6b71 | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2270 | return getConstant( |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2271 | cast<ConstantInt>(ConstantExpr::getNot(VC->getValue()))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2272 | |
| 2273 | const Type *Ty = V->getType(); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2274 | Ty = getEffectiveSCEVType(Ty); |
Owen Anderson | 73c6b71 | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2275 | const SCEV *AllOnes = |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2276 | getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2277 | return getMinusSCEV(AllOnes, V); |
| 2278 | } |
| 2279 | |
| 2280 | /// getMinusSCEV - Return a SCEV corresponding to LHS - RHS. |
| 2281 | /// |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2282 | const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, |
| 2283 | const SCEV *RHS) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2284 | // X - Y --> X + -Y |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2285 | return getAddExpr(LHS, getNegativeSCEV(RHS)); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2289 | /// input value to the specified type. If the type must be extended, it is zero |
| 2290 | /// extended. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2291 | const SCEV * |
| 2292 | ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2293 | const Type *Ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2294 | const Type *SrcTy = V->getType(); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2295 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && |
| 2296 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2297 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2298 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2299 | return V; // No conversion |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2300 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2301 | return getTruncateExpr(V, Ty); |
| 2302 | return getZeroExtendExpr(V, Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2306 | /// input value to the specified type. If the type must be extended, it is sign |
| 2307 | /// extended. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2308 | const SCEV * |
| 2309 | ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, |
Nick Lewycky | 5cd28fa | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2310 | const Type *Ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2311 | const Type *SrcTy = V->getType(); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2312 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && |
| 2313 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2314 | "Cannot truncate or zero extend with non-integer arguments!"); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2315 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2316 | return V; // No conversion |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2317 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2318 | return getTruncateExpr(V, Ty); |
| 2319 | return getSignExtendExpr(V, Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2322 | /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the |
| 2323 | /// input value to the specified type. If the type must be extended, it is zero |
| 2324 | /// extended. The conversion must not be narrowing. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2325 | const SCEV * |
| 2326 | ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2327 | const Type *SrcTy = V->getType(); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2328 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && |
| 2329 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2330 | "Cannot noop or zero extend with non-integer arguments!"); |
| 2331 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2332 | "getNoopOrZeroExtend cannot truncate!"); |
| 2333 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2334 | return V; // No conversion |
| 2335 | return getZeroExtendExpr(V, Ty); |
| 2336 | } |
| 2337 | |
| 2338 | /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the |
| 2339 | /// input value to the specified type. If the type must be extended, it is sign |
| 2340 | /// extended. The conversion must not be narrowing. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2341 | const SCEV * |
| 2342 | ScalarEvolution::getNoopOrSignExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2343 | const Type *SrcTy = V->getType(); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2344 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && |
| 2345 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2346 | "Cannot noop or sign extend with non-integer arguments!"); |
| 2347 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2348 | "getNoopOrSignExtend cannot truncate!"); |
| 2349 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2350 | return V; // No conversion |
| 2351 | return getSignExtendExpr(V, Ty); |
| 2352 | } |
| 2353 | |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2354 | /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of |
| 2355 | /// the input value to the specified type. If the type must be extended, |
| 2356 | /// it is extended with unspecified bits. The conversion must not be |
| 2357 | /// narrowing. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2358 | const SCEV * |
| 2359 | ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, const Type *Ty) { |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2360 | const Type *SrcTy = V->getType(); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2361 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && |
| 2362 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | 2ce84c8d | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2363 | "Cannot noop or any extend with non-integer arguments!"); |
| 2364 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && |
| 2365 | "getNoopOrAnyExtend cannot truncate!"); |
| 2366 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2367 | return V; // No conversion |
| 2368 | return getAnyExtendExpr(V, Ty); |
| 2369 | } |
| 2370 | |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2371 | /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the |
| 2372 | /// input value to the specified type. The conversion must not be widening. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2373 | const SCEV * |
| 2374 | ScalarEvolution::getTruncateOrNoop(const SCEV *V, const Type *Ty) { |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2375 | const Type *SrcTy = V->getType(); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2376 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && |
| 2377 | (Ty->isInteger() || isa<PointerType>(Ty)) && |
Dan Gohman | 467c430 | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2378 | "Cannot truncate or noop with non-integer arguments!"); |
| 2379 | assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) && |
| 2380 | "getTruncateOrNoop cannot extend!"); |
| 2381 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) |
| 2382 | return V; // No conversion |
| 2383 | return getTruncateExpr(V, Ty); |
| 2384 | } |
| 2385 | |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2386 | /// getUMaxFromMismatchedTypes - Promote the operands to the wider of |
| 2387 | /// the types using zero-extension, and then perform a umax operation |
| 2388 | /// with them. |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2389 | const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS, |
| 2390 | const SCEV *RHS) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2391 | const SCEV *PromotedLHS = LHS; |
| 2392 | const SCEV *PromotedRHS = RHS; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2393 | |
| 2394 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2395 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2396 | else |
| 2397 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2398 | |
| 2399 | return getUMaxExpr(PromotedLHS, PromotedRHS); |
| 2400 | } |
| 2401 | |
Dan Gohman | c9759e8 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2402 | /// getUMinFromMismatchedTypes - Promote the operands to the wider of |
| 2403 | /// the types using zero-extension, and then perform a umin operation |
| 2404 | /// with them. |
Dan Gohman | 9311ef6 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2405 | const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS, |
| 2406 | const SCEV *RHS) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2407 | const SCEV *PromotedLHS = LHS; |
| 2408 | const SCEV *PromotedRHS = RHS; |
Dan Gohman | c9759e8 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2409 | |
| 2410 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) |
| 2411 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); |
| 2412 | else |
| 2413 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); |
| 2414 | |
| 2415 | return getUMinExpr(PromotedLHS, PromotedRHS); |
| 2416 | } |
| 2417 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2418 | /// PushDefUseChildren - Push users of the given Instruction |
| 2419 | /// onto the given Worklist. |
| 2420 | static void |
| 2421 | PushDefUseChildren(Instruction *I, |
| 2422 | SmallVectorImpl<Instruction *> &Worklist) { |
| 2423 | // Push the def-use children onto the Worklist stack. |
| 2424 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 2425 | UI != UE; ++UI) |
| 2426 | Worklist.push_back(cast<Instruction>(UI)); |
| 2427 | } |
| 2428 | |
| 2429 | /// ForgetSymbolicValue - This looks up computed SCEV values for all |
| 2430 | /// instructions that depend on the given instruction and removes them from |
| 2431 | /// the Scalars map if they reference SymName. This is used during PHI |
| 2432 | /// resolution. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2433 | void |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2434 | ScalarEvolution::ForgetSymbolicName(Instruction *I, const SCEV *SymName) { |
| 2435 | SmallVector<Instruction *, 16> Worklist; |
| 2436 | PushDefUseChildren(I, Worklist); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2437 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2438 | SmallPtrSet<Instruction *, 8> Visited; |
| 2439 | Visited.insert(I); |
| 2440 | while (!Worklist.empty()) { |
| 2441 | Instruction *I = Worklist.pop_back_val(); |
| 2442 | if (!Visited.insert(I)) continue; |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2443 | |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 2444 | std::map<SCEVCallbackVH, const SCEV *>::iterator It = |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2445 | Scalars.find(static_cast<Value *>(I)); |
| 2446 | if (It != Scalars.end()) { |
| 2447 | // Short-circuit the def-use traversal if the symbolic name |
| 2448 | // ceases to appear in expressions. |
| 2449 | if (!It->second->hasOperand(SymName)) |
| 2450 | continue; |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2451 | |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2452 | // SCEVUnknown for a PHI either means that it has an unrecognized |
| 2453 | // structure, or it's a PHI that's in the progress of being computed |
| 2454 | // by createNodeForPHI. In the former case, additional loop trip |
| 2455 | // count information isn't going to change anything. In the later |
| 2456 | // case, createNodeForPHI will perform the necessary updates on its |
| 2457 | // own when it gets to that point. |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 2458 | if (!isa<PHINode>(I) || !isa<SCEVUnknown>(It->second)) { |
| 2459 | ValuesAtScopes.erase(It->second); |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2460 | Scalars.erase(It); |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 2461 | } |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | PushDefUseChildren(I, Worklist); |
| 2465 | } |
Chris Lattner | 4dc534c | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2466 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2467 | |
| 2468 | /// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in |
| 2469 | /// a loop header, making it a potential recurrence, or it doesn't. |
| 2470 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2471 | const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2472 | if (PN->getNumIncomingValues() == 2) // The loops have been canonicalized. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2473 | if (const Loop *L = LI->getLoopFor(PN->getParent())) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2474 | if (L->getHeader() == PN->getParent()) { |
| 2475 | // If it lives in the loop header, it has two incoming values, one |
| 2476 | // from outside the loop, and one from inside. |
| 2477 | unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0)); |
| 2478 | unsigned BackEdge = IncomingEdge^1; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2479 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2480 | // While we are analyzing this PHI node, handle its value symbolically. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2481 | const SCEV *SymbolicName = getUnknown(PN); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2482 | assert(Scalars.find(PN) == Scalars.end() && |
| 2483 | "PHI node already processed?"); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2484 | Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2485 | |
| 2486 | // Using this symbolic name for the PHI, analyze the value coming around |
| 2487 | // the back-edge. |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2488 | Value *BEValueV = PN->getIncomingValue(BackEdge); |
| 2489 | const SCEV *BEValue = getSCEV(BEValueV); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2490 | |
| 2491 | // NOTE: If BEValue is loop invariant, we know that the PHI node just |
| 2492 | // has a special value for the first iteration of the loop. |
| 2493 | |
| 2494 | // If the value coming around the backedge is an add with the symbolic |
| 2495 | // value we just inserted, then we found a simple induction variable! |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2496 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2497 | // If there is a single occurrence of the symbolic value, replace it |
| 2498 | // with a recurrence. |
| 2499 | unsigned FoundIndex = Add->getNumOperands(); |
| 2500 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2501 | if (Add->getOperand(i) == SymbolicName) |
| 2502 | if (FoundIndex == e) { |
| 2503 | FoundIndex = i; |
| 2504 | break; |
| 2505 | } |
| 2506 | |
| 2507 | if (FoundIndex != Add->getNumOperands()) { |
| 2508 | // Create an add with everything but the specified operand. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2509 | SmallVector<const SCEV *, 8> Ops; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2510 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) |
| 2511 | if (i != FoundIndex) |
| 2512 | Ops.push_back(Add->getOperand(i)); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2513 | const SCEV *Accum = getAddExpr(Ops); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2514 | |
| 2515 | // This is not a valid addrec if the step amount is varying each |
| 2516 | // loop iteration, but is not itself an addrec in this loop. |
| 2517 | if (Accum->isLoopInvariant(L) || |
| 2518 | (isa<SCEVAddRecExpr>(Accum) && |
| 2519 | cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) { |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2520 | const SCEV *StartVal = |
| 2521 | getSCEV(PN->getIncomingValue(IncomingEdge)); |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2522 | const SCEVAddRecExpr *PHISCEV = |
| 2523 | cast<SCEVAddRecExpr>(getAddRecExpr(StartVal, Accum, L)); |
| 2524 | |
| 2525 | // If the increment doesn't overflow, then neither the addrec nor the |
| 2526 | // post-increment will overflow. |
| 2527 | if (const AddOperator *OBO = dyn_cast<AddOperator>(BEValueV)) |
| 2528 | if (OBO->getOperand(0) == PN && |
| 2529 | getSCEV(OBO->getOperand(1)) == |
| 2530 | PHISCEV->getStepRecurrence(*this)) { |
| 2531 | const SCEVAddRecExpr *PostInc = PHISCEV->getPostIncExpr(*this); |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 2532 | if (OBO->hasNoUnsignedWrap()) { |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2533 | const_cast<SCEVAddRecExpr *>(PHISCEV) |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 2534 | ->setHasNoUnsignedWrap(true); |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2535 | const_cast<SCEVAddRecExpr *>(PostInc) |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 2536 | ->setHasNoUnsignedWrap(true); |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2537 | } |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 2538 | if (OBO->hasNoSignedWrap()) { |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2539 | const_cast<SCEVAddRecExpr *>(PHISCEV) |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 2540 | ->setHasNoSignedWrap(true); |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2541 | const_cast<SCEVAddRecExpr *>(PostInc) |
Dan Gohman | 5078f84 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 2542 | ->setHasNoSignedWrap(true); |
Dan Gohman | eb490a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2543 | } |
| 2544 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2545 | |
| 2546 | // Okay, for the entire analysis of this edge we assumed the PHI |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2547 | // to be symbolic. We now need to go back and purge all of the |
| 2548 | // entries for the scalars that use the symbolic expression. |
| 2549 | ForgetSymbolicName(PN, SymbolicName); |
| 2550 | Scalars[SCEVCallbackVH(PN, this)] = PHISCEV; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2551 | return PHISCEV; |
| 2552 | } |
| 2553 | } |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2554 | } else if (const SCEVAddRecExpr *AddRec = |
| 2555 | dyn_cast<SCEVAddRecExpr>(BEValue)) { |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2556 | // Otherwise, this could be a loop like this: |
| 2557 | // i = 0; for (j = 1; ..; ++j) { .... i = j; } |
| 2558 | // In this case, j = {1,+,1} and BEValue is j. |
| 2559 | // Because the other in-value of i (0) fits the evolution of BEValue |
| 2560 | // i really is an addrec evolution. |
| 2561 | if (AddRec->getLoop() == L && AddRec->isAffine()) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2562 | const SCEV *StartVal = getSCEV(PN->getIncomingValue(IncomingEdge)); |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2563 | |
| 2564 | // If StartVal = j.start - j.stride, we can use StartVal as the |
| 2565 | // initial step of the addrec evolution. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2566 | if (StartVal == getMinusSCEV(AddRec->getOperand(0), |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 2567 | AddRec->getOperand(1))) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2568 | const SCEV *PHISCEV = |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2569 | getAddRecExpr(StartVal, AddRec->getOperand(1), L); |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2570 | |
| 2571 | // Okay, for the entire analysis of this edge we assumed the PHI |
Dan Gohman | fef8bb2 | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2572 | // to be symbolic. We now need to go back and purge all of the |
| 2573 | // entries for the scalars that use the symbolic expression. |
| 2574 | ForgetSymbolicName(PN, SymbolicName); |
| 2575 | Scalars[SCEVCallbackVH(PN, this)] = PHISCEV; |
Chris Lattner | 97156e7 | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2576 | return PHISCEV; |
| 2577 | } |
| 2578 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | return SymbolicName; |
| 2582 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2583 | |
Dan Gohman | a653fc5 | 2009-07-14 14:06:25 +0000 | [diff] [blame] | 2584 | // It's tempting to recognize PHIs with a unique incoming value, however |
| 2585 | // this leads passes like indvars to break LCSSA form. Fortunately, such |
| 2586 | // PHIs are rare, as instcombine zaps them. |
| 2587 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2588 | // If it's not a loop phi, we can't handle it yet. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2589 | return getUnknown(PN); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2592 | /// createNodeForGEP - Expand GEP instructions into add and multiply |
| 2593 | /// operations. This allows them to be analyzed by regular SCEV code. |
| 2594 | /// |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 2595 | const SCEV *ScalarEvolution::createNodeForGEP(Operator *GEP) { |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2596 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2597 | const Type *IntPtrTy = getEffectiveSCEVType(GEP->getType()); |
Dan Gohman | e810b0d | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2598 | Value *Base = GEP->getOperand(0); |
Dan Gohman | c63a627 | 2009-05-09 00:14:52 +0000 | [diff] [blame] | 2599 | // Don't attempt to analyze GEPs over unsized objects. |
| 2600 | if (!cast<PointerType>(Base->getType())->getElementType()->isSized()) |
| 2601 | return getUnknown(GEP); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2602 | const SCEV *TotalOffset = getIntegerSCEV(0, IntPtrTy); |
Dan Gohman | e810b0d | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2603 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 2604 | for (GetElementPtrInst::op_iterator I = next(GEP->op_begin()), |
| 2605 | E = GEP->op_end(); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2606 | I != E; ++I) { |
| 2607 | Value *Index = *I; |
| 2608 | // Compute the (potentially symbolic) offset in bytes for this index. |
| 2609 | if (const StructType *STy = dyn_cast<StructType>(*GTI++)) { |
| 2610 | // For a struct, add the member offset. |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2611 | unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue(); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2612 | TotalOffset = getAddExpr(TotalOffset, |
| 2613 | getFieldOffsetExpr(STy, FieldNo)); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2614 | } else { |
| 2615 | // For an array, add the element offset, explicitly scaled. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2616 | const SCEV *LocalOffset = getSCEV(Index); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2617 | if (!isa<PointerType>(LocalOffset->getType())) |
| 2618 | // Getelementptr indicies are signed. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2619 | LocalOffset = getTruncateOrSignExtend(LocalOffset, IntPtrTy); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2620 | LocalOffset = getMulExpr(LocalOffset, getAllocSizeExpr(*GTI)); |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2621 | TotalOffset = getAddExpr(TotalOffset, LocalOffset); |
| 2622 | } |
| 2623 | } |
| 2624 | return getAddExpr(getSCEV(Base), TotalOffset); |
| 2625 | } |
| 2626 | |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2627 | /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is |
| 2628 | /// guaranteed to end in (at every loop iteration). It is, at the same time, |
| 2629 | /// the minimum number of times S is divisible by 2. For example, given {4,+,8} |
| 2630 | /// it returns 2. If S is guaranteed to be 0, it returns the bitwidth of S. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2631 | uint32_t |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2632 | ScalarEvolution::GetMinTrailingZeros(const SCEV *S) { |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2633 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Chris Lattner | 8314a0c | 2007-11-23 22:36:49 +0000 | [diff] [blame] | 2634 | return C->getValue()->getValue().countTrailingZeros(); |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2635 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2636 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S)) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2637 | return std::min(GetMinTrailingZeros(T->getOperand()), |
| 2638 | (uint32_t)getTypeSizeInBits(T->getType())); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2639 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2640 | if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) { |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2641 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 2642 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 2643 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2644 | } |
| 2645 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2646 | if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) { |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2647 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); |
| 2648 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? |
| 2649 | getTypeSizeInBits(E->getType()) : OpRes; |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2652 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2653 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2654 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2655 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2656 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2657 | return MinOpRes; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2660 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2661 | // The result is the sum of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2662 | uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0)); |
| 2663 | uint32_t BitWidth = getTypeSizeInBits(M->getType()); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2664 | for (unsigned i = 1, e = M->getNumOperands(); |
| 2665 | SumOpRes != BitWidth && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2666 | SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2667 | BitWidth); |
| 2668 | return SumOpRes; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2669 | } |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2670 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2671 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2672 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2673 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2674 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2675 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2676 | return MinOpRes; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2677 | } |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2678 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2679 | if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) { |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2680 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2681 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2682 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2683 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2684 | return MinOpRes; |
| 2685 | } |
| 2686 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2687 | if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) { |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2688 | // The result is the min of all operands results. |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2689 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2690 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2691 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2692 | return MinOpRes; |
| 2693 | } |
| 2694 | |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2695 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2696 | // For a SCEVUnknown, ask ValueTracking. |
| 2697 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2698 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 2699 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 2700 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones); |
| 2701 | return Zeros.countTrailingOnes(); |
| 2702 | } |
| 2703 | |
| 2704 | // SCEVUDivExpr |
Nick Lewycky | 83bb005 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2705 | return 0; |
Chris Lattner | a17f039 | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2706 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2707 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2708 | /// getUnsignedRange - Determine the unsigned range for a particular SCEV. |
| 2709 | /// |
| 2710 | ConstantRange |
| 2711 | ScalarEvolution::getUnsignedRange(const SCEV *S) { |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2712 | |
| 2713 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2714 | return ConstantRange(C->getValue()->getValue()); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2715 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2716 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 2717 | ConstantRange X = getUnsignedRange(Add->getOperand(0)); |
| 2718 | for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) |
| 2719 | X = X.add(getUnsignedRange(Add->getOperand(i))); |
| 2720 | return X; |
| 2721 | } |
| 2722 | |
| 2723 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { |
| 2724 | ConstantRange X = getUnsignedRange(Mul->getOperand(0)); |
| 2725 | for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) |
| 2726 | X = X.multiply(getUnsignedRange(Mul->getOperand(i))); |
| 2727 | return X; |
| 2728 | } |
| 2729 | |
| 2730 | if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) { |
| 2731 | ConstantRange X = getUnsignedRange(SMax->getOperand(0)); |
| 2732 | for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) |
| 2733 | X = X.smax(getUnsignedRange(SMax->getOperand(i))); |
| 2734 | return X; |
| 2735 | } |
| 2736 | |
| 2737 | if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) { |
| 2738 | ConstantRange X = getUnsignedRange(UMax->getOperand(0)); |
| 2739 | for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) |
| 2740 | X = X.umax(getUnsignedRange(UMax->getOperand(i))); |
| 2741 | return X; |
| 2742 | } |
| 2743 | |
| 2744 | if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { |
| 2745 | ConstantRange X = getUnsignedRange(UDiv->getLHS()); |
| 2746 | ConstantRange Y = getUnsignedRange(UDiv->getRHS()); |
| 2747 | return X.udiv(Y); |
| 2748 | } |
| 2749 | |
| 2750 | if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { |
| 2751 | ConstantRange X = getUnsignedRange(ZExt->getOperand()); |
| 2752 | return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth()); |
| 2753 | } |
| 2754 | |
| 2755 | if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { |
| 2756 | ConstantRange X = getUnsignedRange(SExt->getOperand()); |
| 2757 | return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth()); |
| 2758 | } |
| 2759 | |
| 2760 | if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { |
| 2761 | ConstantRange X = getUnsignedRange(Trunc->getOperand()); |
| 2762 | return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth()); |
| 2763 | } |
| 2764 | |
| 2765 | ConstantRange FullSet(getTypeSizeInBits(S->getType()), true); |
| 2766 | |
| 2767 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { |
| 2768 | const SCEV *T = getBackedgeTakenCount(AddRec->getLoop()); |
| 2769 | const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T); |
| 2770 | if (!Trip) return FullSet; |
| 2771 | |
| 2772 | // TODO: non-affine addrec |
| 2773 | if (AddRec->isAffine()) { |
| 2774 | const Type *Ty = AddRec->getType(); |
| 2775 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop()); |
| 2776 | if (getTypeSizeInBits(MaxBECount->getType()) <= getTypeSizeInBits(Ty)) { |
| 2777 | MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty); |
| 2778 | |
| 2779 | const SCEV *Start = AddRec->getStart(); |
Dan Gohman | a16b576 | 2009-07-21 00:42:47 +0000 | [diff] [blame] | 2780 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2781 | const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this); |
| 2782 | |
| 2783 | // Check for overflow. |
Dan Gohman | a16b576 | 2009-07-21 00:42:47 +0000 | [diff] [blame] | 2784 | // TODO: This is very conservative. |
| 2785 | if (!(Step->isOne() && |
| 2786 | isKnownPredicate(ICmpInst::ICMP_ULT, Start, End)) && |
| 2787 | !(Step->isAllOnesValue() && |
| 2788 | isKnownPredicate(ICmpInst::ICMP_UGT, Start, End))) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2789 | return FullSet; |
| 2790 | |
| 2791 | ConstantRange StartRange = getUnsignedRange(Start); |
| 2792 | ConstantRange EndRange = getUnsignedRange(End); |
| 2793 | APInt Min = APIntOps::umin(StartRange.getUnsignedMin(), |
| 2794 | EndRange.getUnsignedMin()); |
| 2795 | APInt Max = APIntOps::umax(StartRange.getUnsignedMax(), |
| 2796 | EndRange.getUnsignedMax()); |
| 2797 | if (Min.isMinValue() && Max.isMaxValue()) |
Dan Gohman | 0d5bae4 | 2009-07-20 22:41:51 +0000 | [diff] [blame] | 2798 | return FullSet; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2799 | return ConstantRange(Min, Max+1); |
| 2800 | } |
| 2801 | } |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2805 | // For a SCEVUnknown, ask ValueTracking. |
| 2806 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2807 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 2808 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); |
| 2809 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD); |
Dan Gohman | 746f3b1 | 2009-07-20 22:34:18 +0000 | [diff] [blame] | 2810 | if (Ones == ~Zeros + 1) |
| 2811 | return FullSet; |
| 2812 | return ConstantRange(Ones, ~Zeros + 1); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2815 | return FullSet; |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2818 | /// getSignedRange - Determine the signed range for a particular SCEV. |
| 2819 | /// |
| 2820 | ConstantRange |
| 2821 | ScalarEvolution::getSignedRange(const SCEV *S) { |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2822 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2823 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
| 2824 | return ConstantRange(C->getValue()->getValue()); |
| 2825 | |
| 2826 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { |
| 2827 | ConstantRange X = getSignedRange(Add->getOperand(0)); |
| 2828 | for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) |
| 2829 | X = X.add(getSignedRange(Add->getOperand(i))); |
| 2830 | return X; |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2833 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { |
| 2834 | ConstantRange X = getSignedRange(Mul->getOperand(0)); |
| 2835 | for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) |
| 2836 | X = X.multiply(getSignedRange(Mul->getOperand(i))); |
| 2837 | return X; |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2840 | if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) { |
| 2841 | ConstantRange X = getSignedRange(SMax->getOperand(0)); |
| 2842 | for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) |
| 2843 | X = X.smax(getSignedRange(SMax->getOperand(i))); |
| 2844 | return X; |
| 2845 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2846 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2847 | if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) { |
| 2848 | ConstantRange X = getSignedRange(UMax->getOperand(0)); |
| 2849 | for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) |
| 2850 | X = X.umax(getSignedRange(UMax->getOperand(i))); |
| 2851 | return X; |
| 2852 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2853 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2854 | if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { |
| 2855 | ConstantRange X = getSignedRange(UDiv->getLHS()); |
| 2856 | ConstantRange Y = getSignedRange(UDiv->getRHS()); |
| 2857 | return X.udiv(Y); |
| 2858 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2859 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2860 | if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { |
| 2861 | ConstantRange X = getSignedRange(ZExt->getOperand()); |
| 2862 | return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth()); |
| 2863 | } |
| 2864 | |
| 2865 | if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { |
| 2866 | ConstantRange X = getSignedRange(SExt->getOperand()); |
| 2867 | return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth()); |
| 2868 | } |
| 2869 | |
| 2870 | if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { |
| 2871 | ConstantRange X = getSignedRange(Trunc->getOperand()); |
| 2872 | return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth()); |
| 2873 | } |
| 2874 | |
| 2875 | ConstantRange FullSet(getTypeSizeInBits(S->getType()), true); |
| 2876 | |
| 2877 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { |
| 2878 | const SCEV *T = getBackedgeTakenCount(AddRec->getLoop()); |
| 2879 | const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T); |
| 2880 | if (!Trip) return FullSet; |
| 2881 | |
| 2882 | // TODO: non-affine addrec |
| 2883 | if (AddRec->isAffine()) { |
| 2884 | const Type *Ty = AddRec->getType(); |
| 2885 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop()); |
| 2886 | if (getTypeSizeInBits(MaxBECount->getType()) <= getTypeSizeInBits(Ty)) { |
| 2887 | MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty); |
| 2888 | |
| 2889 | const SCEV *Start = AddRec->getStart(); |
| 2890 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
| 2891 | const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this); |
| 2892 | |
| 2893 | // Check for overflow. |
Dan Gohman | a16b576 | 2009-07-21 00:42:47 +0000 | [diff] [blame] | 2894 | // TODO: This is very conservative. |
| 2895 | if (!(Step->isOne() && |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2896 | isKnownPredicate(ICmpInst::ICMP_SLT, Start, End)) && |
Dan Gohman | a16b576 | 2009-07-21 00:42:47 +0000 | [diff] [blame] | 2897 | !(Step->isAllOnesValue() && |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2898 | isKnownPredicate(ICmpInst::ICMP_SGT, Start, End))) |
| 2899 | return FullSet; |
| 2900 | |
| 2901 | ConstantRange StartRange = getSignedRange(Start); |
| 2902 | ConstantRange EndRange = getSignedRange(End); |
| 2903 | APInt Min = APIntOps::smin(StartRange.getSignedMin(), |
| 2904 | EndRange.getSignedMin()); |
| 2905 | APInt Max = APIntOps::smax(StartRange.getSignedMax(), |
| 2906 | EndRange.getSignedMax()); |
| 2907 | if (Min.isMinSignedValue() && Max.isMaxSignedValue()) |
Dan Gohman | c268e7c | 2009-07-21 00:37:45 +0000 | [diff] [blame] | 2908 | return FullSet; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2909 | return ConstantRange(Min, Max+1); |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2910 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2911 | } |
Dan Gohman | 62849c0 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2914 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
| 2915 | // For a SCEVUnknown, ask ValueTracking. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2916 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 2917 | unsigned NS = ComputeNumSignBits(U->getValue(), TD); |
| 2918 | if (NS == 1) |
| 2919 | return FullSet; |
| 2920 | return |
| 2921 | ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1), |
| 2922 | APInt::getSignedMaxValue(BitWidth).ashr(NS - 1)+1); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2925 | return FullSet; |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2928 | /// createSCEV - We know that there is no SCEV for the specified value. |
| 2929 | /// Analyze the expression. |
| 2930 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2931 | const SCEV *ScalarEvolution::createSCEV(Value *V) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2932 | if (!isSCEVable(V->getType())) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2933 | return getUnknown(V); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2934 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2935 | unsigned Opcode = Instruction::UserOp1; |
| 2936 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 2937 | Opcode = I->getOpcode(); |
| 2938 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 2939 | Opcode = CE->getOpcode(); |
Dan Gohman | 6bbcba1 | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2940 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 2941 | return getConstant(CI); |
| 2942 | else if (isa<ConstantPointerNull>(V)) |
| 2943 | return getIntegerSCEV(0, V->getType()); |
| 2944 | else if (isa<UndefValue>(V)) |
| 2945 | return getIntegerSCEV(0, V->getType()); |
Dan Gohman | 2681232 | 2009-08-25 17:49:57 +0000 | [diff] [blame] | 2946 | else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) |
| 2947 | return GA->mayBeOverridden() ? getUnknown(V) : getSCEV(GA->getAliasee()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2948 | else |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2949 | return getUnknown(V); |
Chris Lattner | 2811f2a | 2007-04-02 05:41:38 +0000 | [diff] [blame] | 2950 | |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 2951 | Operator *U = cast<Operator>(V); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2952 | switch (Opcode) { |
Dan Gohman | 7a72195 | 2009-10-09 16:35:06 +0000 | [diff] [blame] | 2953 | case Instruction::Add: |
| 2954 | // Don't transfer the NSW and NUW bits from the Add instruction to the |
| 2955 | // Add expression, because the Instruction may be guarded by control |
| 2956 | // flow and the no-overflow bits may not be valid for the expression in |
| 2957 | // any context. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2958 | return getAddExpr(getSCEV(U->getOperand(0)), |
Dan Gohman | 7a72195 | 2009-10-09 16:35:06 +0000 | [diff] [blame] | 2959 | getSCEV(U->getOperand(1))); |
| 2960 | case Instruction::Mul: |
| 2961 | // Don't transfer the NSW and NUW bits from the Mul instruction to the |
| 2962 | // Mul expression, as with Add. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2963 | return getMulExpr(getSCEV(U->getOperand(0)), |
Dan Gohman | 7a72195 | 2009-10-09 16:35:06 +0000 | [diff] [blame] | 2964 | getSCEV(U->getOperand(1))); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2965 | case Instruction::UDiv: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2966 | return getUDivExpr(getSCEV(U->getOperand(0)), |
| 2967 | getSCEV(U->getOperand(1))); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2968 | case Instruction::Sub: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2969 | return getMinusSCEV(getSCEV(U->getOperand(0)), |
| 2970 | getSCEV(U->getOperand(1))); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2971 | case Instruction::And: |
| 2972 | // For an expression like x&255 that merely masks off the high bits, |
| 2973 | // use zext(trunc(x)) as the SCEV expression. |
| 2974 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 2c73d5f | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2975 | if (CI->isNullValue()) |
| 2976 | return getSCEV(U->getOperand(1)); |
Dan Gohman | d6c3295 | 2009-04-27 01:41:10 +0000 | [diff] [blame] | 2977 | if (CI->isAllOnesValue()) |
| 2978 | return getSCEV(U->getOperand(0)); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2979 | const APInt &A = CI->getValue(); |
Dan Gohman | 61ffa8e | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2980 | |
| 2981 | // Instcombine's ShrinkDemandedConstant may strip bits out of |
| 2982 | // constants, obscuring what would otherwise be a low-bits mask. |
| 2983 | // Use ComputeMaskedBits to compute what ShrinkDemandedConstant |
| 2984 | // knew about to reconstruct a low-bits mask value. |
| 2985 | unsigned LZ = A.countLeadingZeros(); |
| 2986 | unsigned BitWidth = A.getBitWidth(); |
| 2987 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
| 2988 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 2989 | ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD); |
| 2990 | |
| 2991 | APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ); |
| 2992 | |
Dan Gohman | fc3641b | 2009-06-17 23:54:37 +0000 | [diff] [blame] | 2993 | if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask)) |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2994 | return |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2995 | getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2996 | IntegerType::get(getContext(), BitWidth - LZ)), |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2997 | U->getType()); |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2998 | } |
| 2999 | break; |
Dan Gohman | 61ffa8e | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 3000 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3001 | case Instruction::Or: |
| 3002 | // If the RHS of the Or is a constant, we may have something like: |
| 3003 | // X*4+1 which got turned into X*4|1. Handle this as an Add so loop |
| 3004 | // optimizations will transparently handle this case. |
| 3005 | // |
| 3006 | // In order for this transformation to be safe, the LHS must be of the |
| 3007 | // form X*(2^n) and the Or constant must be less than 2^n. |
| 3008 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3009 | const SCEV *LHS = getSCEV(U->getOperand(0)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3010 | const APInt &CIVal = CI->getValue(); |
Dan Gohman | 2c364ad | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 3011 | if (GetMinTrailingZeros(LHS) >= |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 3012 | (CIVal.getBitWidth() - CIVal.countLeadingZeros())) { |
| 3013 | // Build a plain add SCEV. |
| 3014 | const SCEV *S = getAddExpr(LHS, getSCEV(CI)); |
| 3015 | // If the LHS of the add was an addrec and it has no-wrap flags, |
| 3016 | // transfer the no-wrap flags, since an or won't introduce a wrap. |
| 3017 | if (const SCEVAddRecExpr *NewAR = dyn_cast<SCEVAddRecExpr>(S)) { |
| 3018 | const SCEVAddRecExpr *OldAR = cast<SCEVAddRecExpr>(LHS); |
| 3019 | if (OldAR->hasNoUnsignedWrap()) |
| 3020 | const_cast<SCEVAddRecExpr *>(NewAR)->setHasNoUnsignedWrap(true); |
| 3021 | if (OldAR->hasNoSignedWrap()) |
| 3022 | const_cast<SCEVAddRecExpr *>(NewAR)->setHasNoSignedWrap(true); |
| 3023 | } |
| 3024 | return S; |
| 3025 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3026 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3027 | break; |
| 3028 | case Instruction::Xor: |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3029 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3030 | // If the RHS of the xor is a signbit, then this is just an add. |
| 3031 | // Instcombine turns add of signbit into xor as a strength reduction step. |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3032 | if (CI->getValue().isSignBit()) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3033 | return getAddExpr(getSCEV(U->getOperand(0)), |
| 3034 | getSCEV(U->getOperand(1))); |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3035 | |
| 3036 | // If the RHS of xor is -1, then this is a not operation. |
Dan Gohman | 0bac95e | 2009-05-18 16:17:44 +0000 | [diff] [blame] | 3037 | if (CI->isAllOnesValue()) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3038 | return getNotSCEV(getSCEV(U->getOperand(0))); |
Dan Gohman | 10978bd | 2009-05-18 16:29:04 +0000 | [diff] [blame] | 3039 | |
| 3040 | // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask. |
| 3041 | // This is a variant of the check for xor with -1, and it handles |
| 3042 | // the case where instcombine has trimmed non-demanded bits out |
| 3043 | // of an xor with -1. |
| 3044 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0))) |
| 3045 | if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1))) |
| 3046 | if (BO->getOpcode() == Instruction::And && |
| 3047 | LCI->getValue() == CI->getValue()) |
| 3048 | if (const SCEVZeroExtendExpr *Z = |
Dan Gohman | 3034c10 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 3049 | dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) { |
Dan Gohman | 8205283 | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 3050 | const Type *UTy = U->getType(); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3051 | const SCEV *Z0 = Z->getOperand(); |
Dan Gohman | 8205283 | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 3052 | const Type *Z0Ty = Z0->getType(); |
| 3053 | unsigned Z0TySize = getTypeSizeInBits(Z0Ty); |
| 3054 | |
| 3055 | // If C is a low-bits mask, the zero extend is zerving to |
| 3056 | // mask off the high bits. Complement the operand and |
| 3057 | // re-apply the zext. |
| 3058 | if (APIntOps::isMask(Z0TySize, CI->getValue())) |
| 3059 | return getZeroExtendExpr(getNotSCEV(Z0), UTy); |
| 3060 | |
| 3061 | // If C is a single bit, it may be in the sign-bit position |
| 3062 | // before the zero-extend. In this case, represent the xor |
| 3063 | // using an add, which is equivalent, and re-apply the zext. |
| 3064 | APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize); |
| 3065 | if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() && |
| 3066 | Trunc.isSignBit()) |
| 3067 | return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)), |
| 3068 | UTy); |
Dan Gohman | 3034c10 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 3069 | } |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3070 | } |
| 3071 | break; |
| 3072 | |
| 3073 | case Instruction::Shl: |
| 3074 | // Turn shift left of a constant amount into a multiply. |
| 3075 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 3076 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3077 | Constant *X = ConstantInt::get(getContext(), |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3078 | APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3079 | return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3080 | } |
| 3081 | break; |
| 3082 | |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3083 | case Instruction::LShr: |
Nick Lewycky | 789558d | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 3084 | // Turn logical shift right of a constant into a unsigned divide. |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3085 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { |
| 3086 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3087 | Constant *X = ConstantInt::get(getContext(), |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3088 | APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3089 | return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X)); |
Nick Lewycky | 01eaf80 | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3090 | } |
| 3091 | break; |
| 3092 | |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3093 | case Instruction::AShr: |
| 3094 | // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression. |
| 3095 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) |
| 3096 | if (Instruction *L = dyn_cast<Instruction>(U->getOperand(0))) |
| 3097 | if (L->getOpcode() == Instruction::Shl && |
| 3098 | L->getOperand(1) == U->getOperand(1)) { |
Dan Gohman | 2c73d5f | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 3099 | unsigned BitWidth = getTypeSizeInBits(U->getType()); |
| 3100 | uint64_t Amt = BitWidth - CI->getZExtValue(); |
| 3101 | if (Amt == BitWidth) |
| 3102 | return getSCEV(L->getOperand(0)); // shift by zero --> noop |
| 3103 | if (Amt > BitWidth) |
| 3104 | return getIntegerSCEV(0, U->getType()); // value is undefined |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3105 | return |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3106 | getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3107 | IntegerType::get(getContext(), Amt)), |
Dan Gohman | 4ee29af | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3108 | U->getType()); |
| 3109 | } |
| 3110 | break; |
| 3111 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3112 | case Instruction::Trunc: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3113 | return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3114 | |
| 3115 | case Instruction::ZExt: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3116 | return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3117 | |
| 3118 | case Instruction::SExt: |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3119 | return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3120 | |
| 3121 | case Instruction::BitCast: |
| 3122 | // BitCasts are no-op casts so we just eliminate the cast. |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 3123 | if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3124 | return getSCEV(U->getOperand(0)); |
| 3125 | break; |
| 3126 | |
Dan Gohman | f241174 | 2009-07-20 17:43:30 +0000 | [diff] [blame] | 3127 | // It's tempting to handle inttoptr and ptrtoint, however this can |
| 3128 | // lead to pointer expressions which cannot be expanded to GEPs |
| 3129 | // (because they may overflow). For now, the only pointer-typed |
| 3130 | // expressions we handle are GEPs and address literals. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3131 | |
Dan Gohman | 26466c0 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 3132 | case Instruction::GetElementPtr: |
Dan Gohman | fb79160 | 2009-05-08 20:58:38 +0000 | [diff] [blame] | 3133 | return createNodeForGEP(U); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3134 | |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3135 | case Instruction::PHI: |
| 3136 | return createNodeForPHI(cast<PHINode>(U)); |
| 3137 | |
| 3138 | case Instruction::Select: |
| 3139 | // This could be a smax or umax that was lowered earlier. |
| 3140 | // Try to recover it. |
| 3141 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) { |
| 3142 | Value *LHS = ICI->getOperand(0); |
| 3143 | Value *RHS = ICI->getOperand(1); |
| 3144 | switch (ICI->getPredicate()) { |
| 3145 | case ICmpInst::ICMP_SLT: |
| 3146 | case ICmpInst::ICMP_SLE: |
| 3147 | std::swap(LHS, RHS); |
| 3148 | // fall through |
| 3149 | case ICmpInst::ICMP_SGT: |
| 3150 | case ICmpInst::ICMP_SGE: |
| 3151 | if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3152 | return getSMaxExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3153 | else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) |
Dan Gohman | f9a9a99 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 3154 | return getSMinExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3155 | break; |
| 3156 | case ICmpInst::ICMP_ULT: |
| 3157 | case ICmpInst::ICMP_ULE: |
| 3158 | std::swap(LHS, RHS); |
| 3159 | // fall through |
| 3160 | case ICmpInst::ICMP_UGT: |
| 3161 | case ICmpInst::ICMP_UGE: |
| 3162 | if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3163 | return getUMaxExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3164 | else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) |
Dan Gohman | f9a9a99 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 3165 | return getUMinExpr(getSCEV(LHS), getSCEV(RHS)); |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3166 | break; |
Dan Gohman | 30fb512 | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3167 | case ICmpInst::ICMP_NE: |
| 3168 | // n != 0 ? n : 1 -> umax(n, 1) |
| 3169 | if (LHS == U->getOperand(1) && |
| 3170 | isa<ConstantInt>(U->getOperand(2)) && |
| 3171 | cast<ConstantInt>(U->getOperand(2))->isOne() && |
| 3172 | isa<ConstantInt>(RHS) && |
| 3173 | cast<ConstantInt>(RHS)->isZero()) |
| 3174 | return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(2))); |
| 3175 | break; |
| 3176 | case ICmpInst::ICMP_EQ: |
| 3177 | // n == 0 ? 1 : n -> umax(n, 1) |
| 3178 | if (LHS == U->getOperand(2) && |
| 3179 | isa<ConstantInt>(U->getOperand(1)) && |
| 3180 | cast<ConstantInt>(U->getOperand(1))->isOne() && |
| 3181 | isa<ConstantInt>(RHS) && |
| 3182 | cast<ConstantInt>(RHS)->isZero()) |
| 3183 | return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(1))); |
| 3184 | break; |
Dan Gohman | 6c459a2 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3185 | default: |
| 3186 | break; |
| 3187 | } |
| 3188 | } |
| 3189 | |
| 3190 | default: // We cannot analyze this expression. |
| 3191 | break; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3194 | return getUnknown(V); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
| 3197 | |
| 3198 | |
| 3199 | //===----------------------------------------------------------------------===// |
| 3200 | // Iteration Count Computation Code |
| 3201 | // |
| 3202 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3203 | /// getBackedgeTakenCount - If the specified loop has a predictable |
| 3204 | /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute |
| 3205 | /// object. The backedge-taken count is the number of times the loop header |
| 3206 | /// will be branched to from within the loop. This is one less than the |
| 3207 | /// trip count of the loop, since it doesn't count the first iteration, |
| 3208 | /// when the header is branched to from outside the loop. |
| 3209 | /// |
| 3210 | /// Note that it is not valid to call this method on a loop without a |
| 3211 | /// loop-invariant backedge-taken count (see |
| 3212 | /// hasLoopInvariantBackedgeTakenCount). |
| 3213 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3214 | const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3215 | return getBackedgeTakenInfo(L).Exact; |
| 3216 | } |
| 3217 | |
| 3218 | /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except |
| 3219 | /// return the least SCEV value that is known never to be less than the |
| 3220 | /// actual backedge taken count. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3221 | const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3222 | return getBackedgeTakenInfo(L).Max; |
| 3223 | } |
| 3224 | |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3225 | /// PushLoopPHIs - Push PHI nodes in the header of the given loop |
| 3226 | /// onto the given Worklist. |
| 3227 | static void |
| 3228 | PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) { |
| 3229 | BasicBlock *Header = L->getHeader(); |
| 3230 | |
| 3231 | // Push all Loop-header PHIs onto the Worklist stack. |
| 3232 | for (BasicBlock::iterator I = Header->begin(); |
| 3233 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 3234 | Worklist.push_back(PN); |
| 3235 | } |
| 3236 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3237 | const ScalarEvolution::BackedgeTakenInfo & |
| 3238 | ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3239 | // Initially insert a CouldNotCompute for this loop. If the insertion |
| 3240 | // succeeds, procede to actually compute a backedge-taken count and |
| 3241 | // update the value. The temporary CouldNotCompute value tells SCEV |
| 3242 | // code elsewhere that it shouldn't attempt to request a new |
| 3243 | // backedge-taken count, which could result in infinite recursion. |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 3244 | std::pair<std::map<const Loop *, BackedgeTakenInfo>::iterator, bool> Pair = |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3245 | BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute())); |
| 3246 | if (Pair.second) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3247 | BackedgeTakenInfo ItCount = ComputeBackedgeTakenCount(L); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3248 | if (ItCount.Exact != getCouldNotCompute()) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3249 | assert(ItCount.Exact->isLoopInvariant(L) && |
| 3250 | ItCount.Max->isLoopInvariant(L) && |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3251 | "Computed trip count isn't loop invariant for loop!"); |
| 3252 | ++NumTripCountsComputed; |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3253 | |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3254 | // Update the value in the map. |
| 3255 | Pair.first->second = ItCount; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3256 | } else { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3257 | if (ItCount.Max != getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3258 | // Update the value in the map. |
| 3259 | Pair.first->second = ItCount; |
| 3260 | if (isa<PHINode>(L->getHeader()->begin())) |
| 3261 | // Only count loops that have phi nodes as not being computable. |
| 3262 | ++NumTripCountsNotComputed; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3263 | } |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3264 | |
| 3265 | // Now that we know more about the trip count for this loop, forget any |
| 3266 | // existing SCEV values for PHI nodes in this loop since they are only |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3267 | // conservative estimates made without the benefit of trip count |
Dan Gohman | 4c7279a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 3268 | // information. This is similar to the code in forgetLoop, except that |
| 3269 | // it handles SCEVUnknown PHI nodes specially. |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3270 | if (ItCount.hasAnyInfo()) { |
| 3271 | SmallVector<Instruction *, 16> Worklist; |
| 3272 | PushLoopPHIs(L, Worklist); |
| 3273 | |
| 3274 | SmallPtrSet<Instruction *, 8> Visited; |
| 3275 | while (!Worklist.empty()) { |
| 3276 | Instruction *I = Worklist.pop_back_val(); |
| 3277 | if (!Visited.insert(I)) continue; |
| 3278 | |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 3279 | std::map<SCEVCallbackVH, const SCEV *>::iterator It = |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3280 | Scalars.find(static_cast<Value *>(I)); |
| 3281 | if (It != Scalars.end()) { |
| 3282 | // SCEVUnknown for a PHI either means that it has an unrecognized |
| 3283 | // structure, or it's a PHI that's in the progress of being computed |
Dan Gohman | ba70188 | 2009-07-13 22:04:06 +0000 | [diff] [blame] | 3284 | // by createNodeForPHI. In the former case, additional loop trip |
| 3285 | // count information isn't going to change anything. In the later |
| 3286 | // case, createNodeForPHI will perform the necessary updates on its |
| 3287 | // own when it gets to that point. |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 3288 | if (!isa<PHINode>(I) || !isa<SCEVUnknown>(It->second)) { |
| 3289 | ValuesAtScopes.erase(It->second); |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3290 | Scalars.erase(It); |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 3291 | } |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3292 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3293 | ConstantEvolutionLoopExitValue.erase(PN); |
| 3294 | } |
| 3295 | |
| 3296 | PushDefUseChildren(I, Worklist); |
| 3297 | } |
| 3298 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3299 | } |
Dan Gohman | 01ecca2 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3300 | return Pair.first->second; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
Dan Gohman | 4c7279a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 3303 | /// forgetLoop - This method should be called by the client when it has |
| 3304 | /// changed a loop in a way that may effect ScalarEvolution's ability to |
| 3305 | /// compute a trip count, or if the loop is deleted. |
| 3306 | void ScalarEvolution::forgetLoop(const Loop *L) { |
| 3307 | // Drop any stored trip count value. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3308 | BackedgeTakenCounts.erase(L); |
Dan Gohman | fb7d35f | 2009-05-02 17:43:35 +0000 | [diff] [blame] | 3309 | |
Dan Gohman | 4c7279a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 3310 | // Drop information about expressions based on loop-header PHIs. |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3311 | SmallVector<Instruction *, 16> Worklist; |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3312 | PushLoopPHIs(L, Worklist); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3313 | |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3314 | SmallPtrSet<Instruction *, 8> Visited; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3315 | while (!Worklist.empty()) { |
| 3316 | Instruction *I = Worklist.pop_back_val(); |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3317 | if (!Visited.insert(I)) continue; |
| 3318 | |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 3319 | std::map<SCEVCallbackVH, const SCEV *>::iterator It = |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3320 | Scalars.find(static_cast<Value *>(I)); |
| 3321 | if (It != Scalars.end()) { |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 3322 | ValuesAtScopes.erase(It->second); |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3323 | Scalars.erase(It); |
Dan Gohman | 59ae6b9 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3324 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3325 | ConstantEvolutionLoopExitValue.erase(PN); |
| 3326 | } |
| 3327 | |
| 3328 | PushDefUseChildren(I, Worklist); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3329 | } |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 3330 | } |
| 3331 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3332 | /// ComputeBackedgeTakenCount - Compute the number of times the backedge |
| 3333 | /// of the specified loop will execute. |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3334 | ScalarEvolution::BackedgeTakenInfo |
| 3335 | ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 3336 | SmallVector<BasicBlock *, 8> ExitingBlocks; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3337 | L->getExitingBlocks(ExitingBlocks); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3338 | |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3339 | // Examine all exits and pick the most conservative values. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3340 | const SCEV *BECount = getCouldNotCompute(); |
| 3341 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3342 | bool CouldNotComputeBECount = false; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3343 | for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { |
| 3344 | BackedgeTakenInfo NewBTI = |
| 3345 | ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3346 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3347 | if (NewBTI.Exact == getCouldNotCompute()) { |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3348 | // We couldn't compute an exact value for this exit, so |
Dan Gohman | d32f5bf | 2009-06-22 21:10:22 +0000 | [diff] [blame] | 3349 | // we won't be able to compute an exact value for the loop. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3350 | CouldNotComputeBECount = true; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3351 | BECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3352 | } else if (!CouldNotComputeBECount) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3353 | if (BECount == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3354 | BECount = NewBTI.Exact; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3355 | else |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3356 | BECount = getUMinFromMismatchedTypes(BECount, NewBTI.Exact); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3357 | } |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3358 | if (MaxBECount == getCouldNotCompute()) |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3359 | MaxBECount = NewBTI.Max; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3360 | else if (NewBTI.Max != getCouldNotCompute()) |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3361 | MaxBECount = getUMinFromMismatchedTypes(MaxBECount, NewBTI.Max); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3365 | } |
| 3366 | |
| 3367 | /// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge |
| 3368 | /// of the specified loop will execute if it exits via the specified block. |
| 3369 | ScalarEvolution::BackedgeTakenInfo |
| 3370 | ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L, |
| 3371 | BasicBlock *ExitingBlock) { |
| 3372 | |
| 3373 | // Okay, we've chosen an exiting block. See what condition causes us to |
| 3374 | // exit at this block. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3375 | // |
| 3376 | // FIXME: we should be able to handle switch instructions (with a single exit) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3377 | BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3378 | if (ExitBr == 0) return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3379 | assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!"); |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3380 | |
Chris Lattner | 8b0e360 | 2007-01-07 02:24:26 +0000 | [diff] [blame] | 3381 | // At this point, we know we have a conditional branch that determines whether |
| 3382 | // the loop is exited. However, we don't know if the branch is executed each |
| 3383 | // time through the loop. If not, then the execution count of the branch will |
| 3384 | // not be equal to the trip count of the loop. |
| 3385 | // |
| 3386 | // Currently we check for this by checking to see if the Exit branch goes to |
| 3387 | // the loop header. If so, we know it will always execute the same number of |
Chris Lattner | 192e403 | 2007-01-14 01:24:47 +0000 | [diff] [blame] | 3388 | // times as the loop. We also handle the case where the exit block *is* the |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3389 | // loop header. This is common for un-rotated loops. |
| 3390 | // |
| 3391 | // If both of those tests fail, walk up the unique predecessor chain to the |
| 3392 | // header, stopping if there is an edge that doesn't exit the loop. If the |
| 3393 | // header is reached, the execution count of the branch will be equal to the |
| 3394 | // trip count of the loop. |
| 3395 | // |
| 3396 | // More extensive analysis could be done to handle more cases here. |
| 3397 | // |
Chris Lattner | 8b0e360 | 2007-01-07 02:24:26 +0000 | [diff] [blame] | 3398 | if (ExitBr->getSuccessor(0) != L->getHeader() && |
Chris Lattner | 192e403 | 2007-01-14 01:24:47 +0000 | [diff] [blame] | 3399 | ExitBr->getSuccessor(1) != L->getHeader() && |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3400 | ExitBr->getParent() != L->getHeader()) { |
| 3401 | // The simple checks failed, try climbing the unique predecessor chain |
| 3402 | // up to the header. |
| 3403 | bool Ok = false; |
| 3404 | for (BasicBlock *BB = ExitBr->getParent(); BB; ) { |
| 3405 | BasicBlock *Pred = BB->getUniquePredecessor(); |
| 3406 | if (!Pred) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3407 | return getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3408 | TerminatorInst *PredTerm = Pred->getTerminator(); |
| 3409 | for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) { |
| 3410 | BasicBlock *PredSucc = PredTerm->getSuccessor(i); |
| 3411 | if (PredSucc == BB) |
| 3412 | continue; |
| 3413 | // If the predecessor has a successor that isn't BB and isn't |
| 3414 | // outside the loop, assume the worst. |
| 3415 | if (L->contains(PredSucc)) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3416 | return getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3417 | } |
| 3418 | if (Pred == L->getHeader()) { |
| 3419 | Ok = true; |
| 3420 | break; |
| 3421 | } |
| 3422 | BB = Pred; |
| 3423 | } |
| 3424 | if (!Ok) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3425 | return getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
| 3428 | // Procede to the next level to examine the exit condition expression. |
| 3429 | return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(), |
| 3430 | ExitBr->getSuccessor(0), |
| 3431 | ExitBr->getSuccessor(1)); |
| 3432 | } |
| 3433 | |
| 3434 | /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the |
| 3435 | /// backedge of the specified loop will execute if its exit condition |
| 3436 | /// were a conditional branch of ExitCond, TBB, and FBB. |
| 3437 | ScalarEvolution::BackedgeTakenInfo |
| 3438 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L, |
| 3439 | Value *ExitCond, |
| 3440 | BasicBlock *TBB, |
| 3441 | BasicBlock *FBB) { |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3442 | // Check if the controlling expression for this loop is an And or Or. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3443 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) { |
| 3444 | if (BO->getOpcode() == Instruction::And) { |
| 3445 | // Recurse on the operands of the and. |
| 3446 | BackedgeTakenInfo BTI0 = |
| 3447 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 3448 | BackedgeTakenInfo BTI1 = |
| 3449 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3450 | const SCEV *BECount = getCouldNotCompute(); |
| 3451 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3452 | if (L->contains(TBB)) { |
| 3453 | // Both conditions must be true for the loop to continue executing. |
| 3454 | // Choose the less conservative count. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3455 | if (BTI0.Exact == getCouldNotCompute() || |
| 3456 | BTI1.Exact == getCouldNotCompute()) |
| 3457 | BECount = getCouldNotCompute(); |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3458 | else |
| 3459 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3460 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3461 | MaxBECount = BTI1.Max; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3462 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3463 | MaxBECount = BTI0.Max; |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3464 | else |
| 3465 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3466 | } else { |
| 3467 | // Both conditions must be true for the loop to exit. |
| 3468 | assert(L->contains(FBB) && "Loop block has no successor in loop!"); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3469 | if (BTI0.Exact != getCouldNotCompute() && |
| 3470 | BTI1.Exact != getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3471 | BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3472 | if (BTI0.Max != getCouldNotCompute() && |
| 3473 | BTI1.Max != getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3474 | MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max); |
| 3475 | } |
| 3476 | |
| 3477 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3478 | } |
| 3479 | if (BO->getOpcode() == Instruction::Or) { |
| 3480 | // Recurse on the operands of the or. |
| 3481 | BackedgeTakenInfo BTI0 = |
| 3482 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); |
| 3483 | BackedgeTakenInfo BTI1 = |
| 3484 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3485 | const SCEV *BECount = getCouldNotCompute(); |
| 3486 | const SCEV *MaxBECount = getCouldNotCompute(); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3487 | if (L->contains(FBB)) { |
| 3488 | // Both conditions must be false for the loop to continue executing. |
| 3489 | // Choose the less conservative count. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3490 | if (BTI0.Exact == getCouldNotCompute() || |
| 3491 | BTI1.Exact == getCouldNotCompute()) |
| 3492 | BECount = getCouldNotCompute(); |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3493 | else |
| 3494 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3495 | if (BTI0.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3496 | MaxBECount = BTI1.Max; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3497 | else if (BTI1.Max == getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3498 | MaxBECount = BTI0.Max; |
Dan Gohman | 60e9b07 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3499 | else |
| 3500 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3501 | } else { |
| 3502 | // Both conditions must be false for the loop to exit. |
| 3503 | assert(L->contains(TBB) && "Loop block has no successor in loop!"); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3504 | if (BTI0.Exact != getCouldNotCompute() && |
| 3505 | BTI1.Exact != getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3506 | BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3507 | if (BTI0.Max != getCouldNotCompute() && |
| 3508 | BTI1.Max != getCouldNotCompute()) |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3509 | MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max); |
| 3510 | } |
| 3511 | |
| 3512 | return BackedgeTakenInfo(BECount, MaxBECount); |
| 3513 | } |
| 3514 | } |
| 3515 | |
| 3516 | // With an icmp, it may be feasible to compute an exact backedge-taken count. |
| 3517 | // Procede to the next level to examine the icmp. |
| 3518 | if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) |
| 3519 | return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3520 | |
Eli Friedman | 361e54d | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3521 | // If it's not an integer or pointer comparison then compute it the hard way. |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3522 | return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
| 3523 | } |
| 3524 | |
| 3525 | /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the |
| 3526 | /// backedge of the specified loop will execute if its exit condition |
| 3527 | /// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB. |
| 3528 | ScalarEvolution::BackedgeTakenInfo |
| 3529 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L, |
| 3530 | ICmpInst *ExitCond, |
| 3531 | BasicBlock *TBB, |
| 3532 | BasicBlock *FBB) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3533 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3534 | // If the condition was exit on true, convert the condition to exit on false |
| 3535 | ICmpInst::Predicate Cond; |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3536 | if (!L->contains(FBB)) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3537 | Cond = ExitCond->getPredicate(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3538 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3539 | Cond = ExitCond->getInversePredicate(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3540 | |
| 3541 | // Handle common loops like: for (X = "string"; *X; ++X) |
| 3542 | if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0))) |
| 3543 | if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3544 | const SCEV *ItCnt = |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3545 | ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond); |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3546 | if (!isa<SCEVCouldNotCompute>(ItCnt)) { |
| 3547 | unsigned BitWidth = getTypeSizeInBits(ItCnt->getType()); |
| 3548 | return BackedgeTakenInfo(ItCnt, |
| 3549 | isa<SCEVConstant>(ItCnt) ? ItCnt : |
| 3550 | getConstant(APInt::getMaxValue(BitWidth)-1)); |
| 3551 | } |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3554 | const SCEV *LHS = getSCEV(ExitCond->getOperand(0)); |
| 3555 | const SCEV *RHS = getSCEV(ExitCond->getOperand(1)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3556 | |
| 3557 | // Try to evaluate any dependencies out of the loop. |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3558 | LHS = getSCEVAtScope(LHS, L); |
| 3559 | RHS = getSCEVAtScope(RHS, L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3560 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3561 | // At this point, we would like to compute how many iterations of the |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3562 | // loop the predicate will return true for these inputs. |
Dan Gohman | 70ff4cf | 2008-09-16 18:52:57 +0000 | [diff] [blame] | 3563 | if (LHS->isLoopInvariant(L) && !RHS->isLoopInvariant(L)) { |
| 3564 | // If there is a loop-invariant, force it into the RHS. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3565 | std::swap(LHS, RHS); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3566 | Cond = ICmpInst::getSwappedPredicate(Cond); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3567 | } |
| 3568 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3569 | // If we have a comparison of a chrec against a constant, try to use value |
| 3570 | // ranges to answer this query. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3571 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) |
| 3572 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS)) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3573 | if (AddRec->getLoop() == L) { |
Eli Friedman | 361e54d | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3574 | // Form the constant range. |
| 3575 | ConstantRange CompRange( |
| 3576 | ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue())); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3577 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3578 | const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this); |
Eli Friedman | 361e54d | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3579 | if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3580 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3581 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3582 | switch (Cond) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3583 | case ICmpInst::ICMP_NE: { // while (X != Y) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3584 | // Convert to: while (X-Y != 0) |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3585 | const SCEV *TC = HowFarToZero(getMinusSCEV(LHS, RHS), L); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3586 | if (!isa<SCEVCouldNotCompute>(TC)) return TC; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3587 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3588 | } |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 3589 | case ICmpInst::ICMP_EQ: { // while (X == Y) |
| 3590 | // Convert to: while (X-Y == 0) |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3591 | const SCEV *TC = HowFarToNonZero(getMinusSCEV(LHS, RHS), L); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3592 | if (!isa<SCEVCouldNotCompute>(TC)) return TC; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3593 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3594 | } |
| 3595 | case ICmpInst::ICMP_SLT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3596 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true); |
| 3597 | if (BTI.hasAnyInfo()) return BTI; |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 3598 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3599 | } |
| 3600 | case ICmpInst::ICMP_SGT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3601 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 3602 | getNotSCEV(RHS), L, true); |
| 3603 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | d6dac0e | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 3604 | break; |
| 3605 | } |
| 3606 | case ICmpInst::ICMP_ULT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3607 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false); |
| 3608 | if (BTI.hasAnyInfo()) return BTI; |
Nick Lewycky | d6dac0e | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 3609 | break; |
| 3610 | } |
| 3611 | case ICmpInst::ICMP_UGT: { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3612 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), |
| 3613 | getNotSCEV(RHS), L, false); |
| 3614 | if (BTI.hasAnyInfo()) return BTI; |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 3615 | break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3616 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3617 | default: |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 3618 | #if 0 |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3619 | errs() << "ComputeBackedgeTakenCount "; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3620 | if (ExitCond->getOperand(0)->getType()->isUnsigned()) |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3621 | errs() << "[unsigned] "; |
| 3622 | errs() << *LHS << " " |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3623 | << Instruction::getOpcodeName(Instruction::ICmp) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3624 | << " " << *RHS << "\n"; |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 3625 | #endif |
Chris Lattner | e34c0b4 | 2004-04-03 00:43:03 +0000 | [diff] [blame] | 3626 | break; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3627 | } |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3628 | return |
Dan Gohman | a334aa7 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3629 | ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3630 | } |
| 3631 | |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3632 | static ConstantInt * |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3633 | EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, |
| 3634 | ScalarEvolution &SE) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3635 | const SCEV *InVal = SE.getConstant(C); |
| 3636 | const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3637 | assert(isa<SCEVConstant>(Val) && |
| 3638 | "Evaluation of SCEV at constant didn't fold correctly?"); |
| 3639 | return cast<SCEVConstant>(Val)->getValue(); |
| 3640 | } |
| 3641 | |
| 3642 | /// GetAddressedElementFromGlobal - Given a global variable with an initializer |
| 3643 | /// and a GEP expression (missing the pointer index) indexing into it, return |
| 3644 | /// the addressed element of the initializer or null if the index expression is |
| 3645 | /// invalid. |
| 3646 | static Constant * |
Nick Lewycky | c6501b1 | 2009-11-23 03:26:09 +0000 | [diff] [blame] | 3647 | GetAddressedElementFromGlobal(GlobalVariable *GV, |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3648 | const std::vector<ConstantInt*> &Indices) { |
| 3649 | Constant *Init = GV->getInitializer(); |
| 3650 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3651 | uint64_t Idx = Indices[i]->getZExtValue(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3652 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) { |
| 3653 | assert(Idx < CS->getNumOperands() && "Bad struct index!"); |
| 3654 | Init = cast<Constant>(CS->getOperand(Idx)); |
| 3655 | } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) { |
| 3656 | if (Idx >= CA->getNumOperands()) return 0; // Bogus program |
| 3657 | Init = cast<Constant>(CA->getOperand(Idx)); |
| 3658 | } else if (isa<ConstantAggregateZero>(Init)) { |
| 3659 | if (const StructType *STy = dyn_cast<StructType>(Init->getType())) { |
| 3660 | assert(Idx < STy->getNumElements() && "Bad struct index!"); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3661 | Init = Constant::getNullValue(STy->getElementType(Idx)); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3662 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) { |
| 3663 | if (Idx >= ATy->getNumElements()) return 0; // Bogus program |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3664 | Init = Constant::getNullValue(ATy->getElementType()); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3665 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3666 | llvm_unreachable("Unknown constant aggregate type!"); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3667 | } |
| 3668 | return 0; |
| 3669 | } else { |
| 3670 | return 0; // Unknown initializer type |
| 3671 | } |
| 3672 | } |
| 3673 | return Init; |
| 3674 | } |
| 3675 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3676 | /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of |
| 3677 | /// 'icmp op load X, cst', try to see if we can compute the backedge |
| 3678 | /// execution count. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3679 | const SCEV * |
| 3680 | ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount( |
| 3681 | LoadInst *LI, |
| 3682 | Constant *RHS, |
| 3683 | const Loop *L, |
| 3684 | ICmpInst::Predicate predicate) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3685 | if (LI->isVolatile()) return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3686 | |
| 3687 | // Check to see if the loaded pointer is a getelementptr of a global. |
| 3688 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3689 | if (!GEP) return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3690 | |
| 3691 | // Make sure that it is really a constant global we are gepping, with an |
| 3692 | // initializer, and make sure the first IDX is really 0. |
| 3693 | GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); |
Dan Gohman | 8255573 | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 3694 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() || |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3695 | GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) || |
| 3696 | !cast<Constant>(GEP->getOperand(1))->isNullValue()) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3697 | return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3698 | |
| 3699 | // Okay, we allow one non-constant index into the GEP instruction. |
| 3700 | Value *VarIdx = 0; |
| 3701 | std::vector<ConstantInt*> Indexes; |
| 3702 | unsigned VarIdxNum = 0; |
| 3703 | for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) |
| 3704 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 3705 | Indexes.push_back(CI); |
| 3706 | } else if (!isa<ConstantInt>(GEP->getOperand(i))) { |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3707 | if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's. |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3708 | VarIdx = GEP->getOperand(i); |
| 3709 | VarIdxNum = i-2; |
| 3710 | Indexes.push_back(0); |
| 3711 | } |
| 3712 | |
| 3713 | // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant. |
| 3714 | // Check to see if X is a loop variant variable value now. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3715 | const SCEV *Idx = getSCEV(VarIdx); |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3716 | Idx = getSCEVAtScope(Idx, L); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3717 | |
| 3718 | // We can only recognize very limited forms of loop index expressions, in |
| 3719 | // particular, only affine AddRec's like {C1,+,C2}. |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3720 | const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3721 | if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) || |
| 3722 | !isa<SCEVConstant>(IdxExpr->getOperand(0)) || |
| 3723 | !isa<SCEVConstant>(IdxExpr->getOperand(1))) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3724 | return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3725 | |
| 3726 | unsigned MaxSteps = MaxBruteForceIterations; |
| 3727 | for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3728 | ConstantInt *ItCst = ConstantInt::get( |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 3729 | cast<IntegerType>(IdxExpr->getType()), IterationNum); |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3730 | ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3731 | |
| 3732 | // Form the GEP offset. |
| 3733 | Indexes[VarIdxNum] = Val; |
| 3734 | |
Nick Lewycky | c6501b1 | 2009-11-23 03:26:09 +0000 | [diff] [blame] | 3735 | Constant *Result = GetAddressedElementFromGlobal(GV, Indexes); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3736 | if (Result == 0) break; // Cannot compute! |
| 3737 | |
| 3738 | // Evaluate the condition for this iteration. |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3739 | Result = ConstantExpr::getICmp(predicate, Result, RHS); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3740 | if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3741 | if (cast<ConstantInt>(Result)->getValue().isMinValue()) { |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3742 | #if 0 |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3743 | errs() << "\n***\n*** Computed loop count " << *ItCst |
| 3744 | << "\n*** From global " << *GV << "*** BB: " << *L->getHeader() |
| 3745 | << "***\n"; |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3746 | #endif |
| 3747 | ++NumArrayLenItCounts; |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3748 | return getConstant(ItCst); // Found terminating iteration! |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3749 | } |
| 3750 | } |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3751 | return getCouldNotCompute(); |
Chris Lattner | 673e02b | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3752 | } |
| 3753 | |
| 3754 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3755 | /// CanConstantFold - Return true if we can constant fold an instruction of the |
| 3756 | /// specified type, assuming that all operands were constants. |
| 3757 | static bool CanConstantFold(const Instruction *I) { |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3758 | if (isa<BinaryOperator>(I) || isa<CmpInst>(I) || |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3759 | isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I)) |
| 3760 | return true; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3761 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3762 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 3763 | if (const Function *F = CI->getCalledFunction()) |
Dan Gohman | fa9b80e | 2008-01-31 01:05:10 +0000 | [diff] [blame] | 3764 | return canConstantFoldCallTo(F); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3765 | return false; |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3766 | } |
| 3767 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3768 | /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node |
| 3769 | /// in the loop that V is derived from. We allow arbitrary operations along the |
| 3770 | /// way, but the operands of an operation must either be constants or a value |
| 3771 | /// derived from a constant PHI. If this expression does not fit with these |
| 3772 | /// constraints, return null. |
| 3773 | static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) { |
| 3774 | // If this is not an instruction, or if this is an instruction outside of the |
| 3775 | // loop, it can't be derived from a loop PHI. |
| 3776 | Instruction *I = dyn_cast<Instruction>(V); |
Dan Gohman | 92329c7 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 3777 | if (I == 0 || !L->contains(I)) return 0; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3778 | |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 3779 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3780 | if (L->getHeader() == I->getParent()) |
| 3781 | return PN; |
| 3782 | else |
| 3783 | // We don't currently keep track of the control flow needed to evaluate |
| 3784 | // PHIs, so we cannot handle PHIs inside of loops. |
| 3785 | return 0; |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 3786 | } |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3787 | |
| 3788 | // If we won't be able to constant fold this expression even if the operands |
| 3789 | // are constants, return early. |
| 3790 | if (!CanConstantFold(I)) return 0; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3791 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3792 | // Otherwise, we can evaluate this instruction if all of its operands are |
| 3793 | // constant or derived from a PHI node themselves. |
| 3794 | PHINode *PHI = 0; |
| 3795 | for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op) |
| 3796 | if (!(isa<Constant>(I->getOperand(Op)) || |
| 3797 | isa<GlobalValue>(I->getOperand(Op)))) { |
| 3798 | PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L); |
| 3799 | if (P == 0) return 0; // Not evolving from PHI |
| 3800 | if (PHI == 0) |
| 3801 | PHI = P; |
| 3802 | else if (PHI != P) |
| 3803 | return 0; // Evolving from multiple different PHIs. |
| 3804 | } |
| 3805 | |
| 3806 | // This is a expression evolving from a constant PHI! |
| 3807 | return PHI; |
| 3808 | } |
| 3809 | |
| 3810 | /// EvaluateExpression - Given an expression that passes the |
| 3811 | /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node |
| 3812 | /// in the loop has the value PHIVal. If we can't fold this expression for some |
| 3813 | /// reason, return null. |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 3814 | static Constant *EvaluateExpression(Value *V, Constant *PHIVal, |
| 3815 | const TargetData *TD) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3816 | if (isa<PHINode>(V)) return PHIVal; |
Reid Spencer | e840434 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 3817 | if (Constant *C = dyn_cast<Constant>(V)) return C; |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3818 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3819 | Instruction *I = cast<Instruction>(V); |
| 3820 | |
| 3821 | std::vector<Constant*> Operands; |
| 3822 | Operands.resize(I->getNumOperands()); |
| 3823 | |
| 3824 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 3825 | Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal, TD); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3826 | if (Operands[i] == 0) return 0; |
| 3827 | } |
| 3828 | |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3829 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
Chris Lattner | 8f73dea | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 3830 | return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0], |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 3831 | Operands[1], TD); |
Chris Lattner | 8f73dea | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 3832 | return ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 3833 | &Operands[0], Operands.size(), TD); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3834 | } |
| 3835 | |
| 3836 | /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is |
| 3837 | /// in the header of its containing loop, we know the loop executes a |
| 3838 | /// constant number of times, and the PHI node is just a recurrence |
| 3839 | /// involving constants, fold it. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3840 | Constant * |
| 3841 | ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN, |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 3842 | const APInt &BEs, |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3843 | const Loop *L) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3844 | std::map<PHINode*, Constant*>::iterator I = |
| 3845 | ConstantEvolutionLoopExitValue.find(PN); |
| 3846 | if (I != ConstantEvolutionLoopExitValue.end()) |
| 3847 | return I->second; |
| 3848 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3849 | if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations))) |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3850 | return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it. |
| 3851 | |
| 3852 | Constant *&RetVal = ConstantEvolutionLoopExitValue[PN]; |
| 3853 | |
| 3854 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 3855 | // entry must be a constant (coming in from outside of the loop), and the |
| 3856 | // second must be derived from the same PHI. |
| 3857 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 3858 | Constant *StartCST = |
| 3859 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
| 3860 | if (StartCST == 0) |
| 3861 | return RetVal = 0; // Must be a constant. |
| 3862 | |
| 3863 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
| 3864 | PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); |
| 3865 | if (PN2 != PN) |
| 3866 | return RetVal = 0; // Not derived from same PHI. |
| 3867 | |
| 3868 | // Execute the loop symbolically to determine the exit value. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3869 | if (BEs.getActiveBits() >= 32) |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3870 | return RetVal = 0; // More than 2^32-1 iterations?? Not doing it! |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3871 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3872 | unsigned NumIterations = BEs.getZExtValue(); // must be in range |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3873 | unsigned IterationNum = 0; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3874 | for (Constant *PHIVal = StartCST; ; ++IterationNum) { |
| 3875 | if (IterationNum == NumIterations) |
| 3876 | return RetVal = PHIVal; // Got exit value! |
| 3877 | |
| 3878 | // Compute the value of the PHI node for the next iteration. |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 3879 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal, TD); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3880 | if (NextPHI == PHIVal) |
| 3881 | return RetVal = NextPHI; // Stopped evolving! |
| 3882 | if (NextPHI == 0) |
| 3883 | return 0; // Couldn't evaluate! |
| 3884 | PHIVal = NextPHI; |
| 3885 | } |
| 3886 | } |
| 3887 | |
Dan Gohman | 07ad19b | 2009-07-27 16:09:48 +0000 | [diff] [blame] | 3888 | /// ComputeBackedgeTakenCountExhaustively - If the loop is known to execute a |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3889 | /// constant number of times (the condition evolves only from constants), |
| 3890 | /// try to evaluate a few iterations of the loop until we get the exit |
| 3891 | /// condition gets a value of ExitWhen (true or false). If we cannot |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3892 | /// evaluate the trip count of the loop, return getCouldNotCompute(). |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3893 | const SCEV * |
| 3894 | ScalarEvolution::ComputeBackedgeTakenCountExhaustively(const Loop *L, |
| 3895 | Value *Cond, |
| 3896 | bool ExitWhen) { |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3897 | PHINode *PN = getConstantEvolvingPHI(Cond, L); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3898 | if (PN == 0) return getCouldNotCompute(); |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3899 | |
| 3900 | // Since the loop is canonicalized, the PHI node must have two entries. One |
| 3901 | // entry must be a constant (coming in from outside of the loop), and the |
| 3902 | // second must be derived from the same PHI. |
| 3903 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); |
| 3904 | Constant *StartCST = |
| 3905 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3906 | if (StartCST == 0) return getCouldNotCompute(); // Must be a constant. |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3907 | |
| 3908 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); |
| 3909 | PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3910 | if (PN2 != PN) return getCouldNotCompute(); // Not derived from same PHI. |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3911 | |
| 3912 | // Okay, we find a PHI node that defines the trip count of this loop. Execute |
| 3913 | // the loop symbolically to determine when the condition gets a value of |
| 3914 | // "ExitWhen". |
| 3915 | unsigned IterationNum = 0; |
| 3916 | unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis. |
| 3917 | for (Constant *PHIVal = StartCST; |
| 3918 | IterationNum != MaxIterations; ++IterationNum) { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3919 | ConstantInt *CondVal = |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 3920 | dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal, TD)); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3921 | |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3922 | // Couldn't symbolically evaluate. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3923 | if (!CondVal) return getCouldNotCompute(); |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3924 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3925 | if (CondVal->getValue() == uint64_t(ExitWhen)) { |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3926 | ++NumBruteForceTripCountsComputed; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3927 | return getConstant(Type::getInt32Ty(getContext()), IterationNum); |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3928 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3929 | |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3930 | // Compute the value of the PHI node for the next iteration. |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 3931 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal, TD); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3932 | if (NextPHI == 0 || NextPHI == PHIVal) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3933 | return getCouldNotCompute();// Couldn't evaluate or not making progress... |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3934 | PHIVal = NextPHI; |
Chris Lattner | 7980fb9 | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
| 3937 | // Too many iterations were needed to evaluate. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3938 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
Dan Gohman | e7125f4 | 2009-09-03 15:00:26 +0000 | [diff] [blame] | 3941 | /// getSCEVAtScope - Return a SCEV expression for the specified value |
Dan Gohman | 66a7e85 | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 3942 | /// at the specified scope in the program. The L value specifies a loop |
| 3943 | /// nest to evaluate the expression at, where null is the top-level or a |
| 3944 | /// specified loop is immediately inside of the loop. |
| 3945 | /// |
| 3946 | /// This method can be used to compute the exit value for a variable defined |
| 3947 | /// in a loop by querying what the value will hold in the parent loop. |
| 3948 | /// |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3949 | /// In the case that a relevant loop exit value cannot be computed, the |
| 3950 | /// original value V is returned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3951 | const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 3952 | // Check to see if we've folded this expression at this loop before. |
| 3953 | std::map<const Loop *, const SCEV *> &Values = ValuesAtScopes[V]; |
| 3954 | std::pair<std::map<const Loop *, const SCEV *>::iterator, bool> Pair = |
| 3955 | Values.insert(std::make_pair(L, static_cast<const SCEV *>(0))); |
| 3956 | if (!Pair.second) |
| 3957 | return Pair.first->second ? Pair.first->second : V; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3958 | |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 3959 | // Otherwise compute it. |
| 3960 | const SCEV *C = computeSCEVAtScope(V, L); |
Dan Gohman | a5505cb | 2009-08-31 21:58:28 +0000 | [diff] [blame] | 3961 | ValuesAtScopes[V][L] = C; |
Dan Gohman | 4221489 | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 3962 | return C; |
| 3963 | } |
| 3964 | |
| 3965 | const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3966 | if (isa<SCEVConstant>(V)) return V; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3967 | |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 3968 | // If this instruction is evolved from a constant-evolving PHI, compute the |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3969 | // exit value from the loop without using SCEVs. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3970 | if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3971 | if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3972 | const Loop *LI = (*this->LI)[I->getParent()]; |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3973 | if (LI && LI->getParentLoop() == L) // Looking for loop exit value. |
| 3974 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
| 3975 | if (PN->getParent() == LI->getHeader()) { |
| 3976 | // Okay, there is no closed form solution for the PHI node. Check |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3977 | // to see if the loop that contains it has a known backedge-taken |
| 3978 | // count. If so, we may be able to force computation of the exit |
| 3979 | // value. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3980 | const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3981 | if (const SCEVConstant *BTCC = |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3982 | dyn_cast<SCEVConstant>(BackedgeTakenCount)) { |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3983 | // Okay, we know how many times the containing loop executes. If |
| 3984 | // this is a constant evolving PHI node, get the final value at |
| 3985 | // the specified iteration number. |
| 3986 | Constant *RV = getConstantEvolutionLoopExitValue(PN, |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3987 | BTCC->getValue()->getValue(), |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3988 | LI); |
Dan Gohman | 0998796 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3989 | if (RV) return getSCEV(RV); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3990 | } |
| 3991 | } |
| 3992 | |
Reid Spencer | 09906f3 | 2006-12-04 21:33:23 +0000 | [diff] [blame] | 3993 | // Okay, this is an expression that we cannot symbolically evaluate |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3994 | // into a SCEV. Check to see if it's possible to symbolically evaluate |
Reid Spencer | 09906f3 | 2006-12-04 21:33:23 +0000 | [diff] [blame] | 3995 | // the arguments into constants, and if so, try to constant propagate the |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3996 | // result. This is particularly useful for computing loop exit values. |
| 3997 | if (CanConstantFold(I)) { |
| 3998 | std::vector<Constant*> Operands; |
| 3999 | Operands.reserve(I->getNumOperands()); |
| 4000 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 4001 | Value *Op = I->getOperand(i); |
| 4002 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| 4003 | Operands.push_back(C); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4004 | } else { |
Chris Lattner | 42b5e08 | 2007-11-23 08:46:22 +0000 | [diff] [blame] | 4005 | // If any of the operands is non-constant and if they are |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4006 | // non-integer and non-pointer, don't even try to analyze them |
| 4007 | // with scev techniques. |
Dan Gohman | 4acd12a | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 4008 | if (!isSCEVable(Op->getType())) |
Chris Lattner | 42b5e08 | 2007-11-23 08:46:22 +0000 | [diff] [blame] | 4009 | return V; |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4010 | |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 4011 | const SCEV *OpV = getSCEVAtScope(Op, L); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4012 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) { |
Dan Gohman | 4acd12a | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 4013 | Constant *C = SC->getValue(); |
| 4014 | if (C->getType() != Op->getType()) |
| 4015 | C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 4016 | Op->getType(), |
| 4017 | false), |
| 4018 | C, Op->getType()); |
| 4019 | Operands.push_back(C); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4020 | } else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) { |
Dan Gohman | 4acd12a | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 4021 | if (Constant *C = dyn_cast<Constant>(SU->getValue())) { |
| 4022 | if (C->getType() != Op->getType()) |
| 4023 | C = |
| 4024 | ConstantExpr::getCast(CastInst::getCastOpcode(C, false, |
| 4025 | Op->getType(), |
| 4026 | false), |
| 4027 | C, Op->getType()); |
| 4028 | Operands.push_back(C); |
| 4029 | } else |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4030 | return V; |
| 4031 | } else { |
| 4032 | return V; |
| 4033 | } |
| 4034 | } |
| 4035 | } |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4036 | |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 4037 | Constant *C; |
| 4038 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 4039 | C = ConstantFoldCompareInstOperands(CI->getPredicate(), |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4040 | Operands[0], Operands[1], TD); |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 4041 | else |
| 4042 | C = ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
Dan Gohman | 1ba3b6c | 2009-11-09 23:34:17 +0000 | [diff] [blame] | 4043 | &Operands[0], Operands.size(), TD); |
Dan Gohman | 0998796 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 4044 | return getSCEV(C); |
Chris Lattner | 3221ad0 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4045 | } |
| 4046 | } |
| 4047 | |
| 4048 | // This is some other type of SCEVUnknown, just return it. |
| 4049 | return V; |
| 4050 | } |
| 4051 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4052 | if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4053 | // Avoid performing the look-up in the common case where the specified |
| 4054 | // expression has no loop-variant portions. |
| 4055 | for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4056 | const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4057 | if (OpAtScope != Comm->getOperand(i)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4058 | // Okay, at least one of these operands is loop variant but might be |
| 4059 | // foldable. Build a new instance of the folded commutative expression. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4060 | SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(), |
| 4061 | Comm->op_begin()+i); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4062 | NewOps.push_back(OpAtScope); |
| 4063 | |
| 4064 | for (++i; i != e; ++i) { |
| 4065 | OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4066 | NewOps.push_back(OpAtScope); |
| 4067 | } |
| 4068 | if (isa<SCEVAddExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4069 | return getAddExpr(NewOps); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 4070 | if (isa<SCEVMulExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4071 | return getMulExpr(NewOps); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 4072 | if (isa<SCEVSMaxExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4073 | return getSMaxExpr(NewOps); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 4074 | if (isa<SCEVUMaxExpr>(Comm)) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4075 | return getUMaxExpr(NewOps); |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4076 | llvm_unreachable("Unknown commutative SCEV type!"); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4077 | } |
| 4078 | } |
| 4079 | // If we got here, all operands are loop invariant. |
| 4080 | return Comm; |
| 4081 | } |
| 4082 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4083 | if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4084 | const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L); |
| 4085 | const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L); |
Nick Lewycky | 789558d | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 4086 | if (LHS == Div->getLHS() && RHS == Div->getRHS()) |
| 4087 | return Div; // must be loop invariant |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4088 | return getUDivExpr(LHS, RHS); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
| 4091 | // If this is a loop recurrence for a loop that does not contain L, then we |
| 4092 | // are dealing with the final value computed by the loop. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4093 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) { |
Dan Gohman | 92329c7 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 4094 | if (!L || !AddRec->getLoop()->contains(L)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4095 | // To evaluate this recurrence, we need to know how many times the AddRec |
| 4096 | // loop iterates. Compute this now. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4097 | const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4098 | if (BackedgeTakenCount == getCouldNotCompute()) return AddRec; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4099 | |
Eli Friedman | b42a626 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 4100 | // Then, evaluate the AddRec. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4101 | return AddRec->evaluateAtIteration(BackedgeTakenCount, *this); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4102 | } |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 4103 | return AddRec; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4104 | } |
| 4105 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4106 | if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4107 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | eb3948b | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4108 | if (Op == Cast->getOperand()) |
| 4109 | return Cast; // must be loop invariant |
| 4110 | return getZeroExtendExpr(Op, Cast->getType()); |
| 4111 | } |
| 4112 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4113 | if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4114 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | eb3948b | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4115 | if (Op == Cast->getOperand()) |
| 4116 | return Cast; // must be loop invariant |
| 4117 | return getSignExtendExpr(Op, Cast->getType()); |
| 4118 | } |
| 4119 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4120 | if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4121 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); |
Dan Gohman | eb3948b | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4122 | if (Op == Cast->getOperand()) |
| 4123 | return Cast; // must be loop invariant |
| 4124 | return getTruncateExpr(Op, Cast->getType()); |
| 4125 | } |
| 4126 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 4127 | if (isa<SCEVTargetDataConstant>(V)) |
| 4128 | return V; |
| 4129 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4130 | llvm_unreachable("Unknown SCEV type!"); |
Daniel Dunbar | 8c562e2 | 2009-05-18 16:43:04 +0000 | [diff] [blame] | 4131 | return 0; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
Dan Gohman | 66a7e85 | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 4134 | /// getSCEVAtScope - This is a convenience function which does |
| 4135 | /// getSCEVAtScope(getSCEV(V), L). |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4136 | const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4137 | return getSCEVAtScope(getSCEV(V), L); |
| 4138 | } |
| 4139 | |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4140 | /// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the |
| 4141 | /// following equation: |
| 4142 | /// |
| 4143 | /// A * X = B (mod N) |
| 4144 | /// |
| 4145 | /// where N = 2^BW and BW is the common bit width of A and B. The signedness of |
| 4146 | /// A and B isn't important. |
| 4147 | /// |
| 4148 | /// If the equation does not have a solution, SCEVCouldNotCompute is returned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4149 | static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const APInt &B, |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4150 | ScalarEvolution &SE) { |
| 4151 | uint32_t BW = A.getBitWidth(); |
| 4152 | assert(BW == B.getBitWidth() && "Bit widths must be the same."); |
| 4153 | assert(A != 0 && "A must be non-zero."); |
| 4154 | |
| 4155 | // 1. D = gcd(A, N) |
| 4156 | // |
| 4157 | // The gcd of A and N may have only one prime factor: 2. The number of |
| 4158 | // trailing zeros in A is its multiplicity |
| 4159 | uint32_t Mult2 = A.countTrailingZeros(); |
| 4160 | // D = 2^Mult2 |
| 4161 | |
| 4162 | // 2. Check if B is divisible by D. |
| 4163 | // |
| 4164 | // B is divisible by D if and only if the multiplicity of prime factor 2 for B |
| 4165 | // is not less than multiplicity of this prime factor for D. |
| 4166 | if (B.countTrailingZeros() < Mult2) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4167 | return SE.getCouldNotCompute(); |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4168 | |
| 4169 | // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic |
| 4170 | // modulo (N / D). |
| 4171 | // |
| 4172 | // (N / D) may need BW+1 bits in its representation. Hence, we'll use this |
| 4173 | // bit width during computations. |
| 4174 | APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D |
| 4175 | APInt Mod(BW + 1, 0); |
| 4176 | Mod.set(BW - Mult2); // Mod = N / D |
| 4177 | APInt I = AD.multiplicativeInverse(Mod); |
| 4178 | |
| 4179 | // 4. Compute the minimum unsigned root of the equation: |
| 4180 | // I * (B / D) mod (N / D) |
| 4181 | APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod); |
| 4182 | |
| 4183 | // The result is guaranteed to be less than 2^BW so we may truncate it to BW |
| 4184 | // bits. |
| 4185 | return SE.getConstant(Result.trunc(BW)); |
| 4186 | } |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4187 | |
| 4188 | /// SolveQuadraticEquation - Find the roots of the quadratic equation for the |
| 4189 | /// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which |
| 4190 | /// might be the same) or two SCEVCouldNotCompute objects. |
| 4191 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4192 | static std::pair<const SCEV *,const SCEV *> |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4193 | SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4194 | assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4195 | const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0)); |
| 4196 | const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1)); |
| 4197 | const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2)); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4198 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4199 | // We currently can only solve this if the coefficients are constants. |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4200 | if (!LC || !MC || !NC) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4201 | const SCEV *CNC = SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4202 | return std::make_pair(CNC, CNC); |
| 4203 | } |
| 4204 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4205 | uint32_t BitWidth = LC->getValue()->getValue().getBitWidth(); |
Chris Lattner | fe560b8 | 2007-04-15 19:52:49 +0000 | [diff] [blame] | 4206 | const APInt &L = LC->getValue()->getValue(); |
| 4207 | const APInt &M = MC->getValue()->getValue(); |
| 4208 | const APInt &N = NC->getValue()->getValue(); |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4209 | APInt Two(BitWidth, 2); |
| 4210 | APInt Four(BitWidth, 4); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4211 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4212 | { |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4213 | using namespace APIntOps; |
Zhou Sheng | 414de4d | 2007-04-07 17:48:27 +0000 | [diff] [blame] | 4214 | const APInt& C = L; |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4215 | // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C |
| 4216 | // The B coefficient is M-N/2 |
| 4217 | APInt B(M); |
| 4218 | B -= sdiv(N,Two); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4219 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4220 | // The A coefficient is N/2 |
Zhou Sheng | 414de4d | 2007-04-07 17:48:27 +0000 | [diff] [blame] | 4221 | APInt A(N.sdiv(Two)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4222 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4223 | // Compute the B^2-4ac term. |
| 4224 | APInt SqrtTerm(B); |
| 4225 | SqrtTerm *= B; |
| 4226 | SqrtTerm -= Four * (A * C); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4227 | |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4228 | // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest |
| 4229 | // integer value or else APInt::sqrt() will assert. |
| 4230 | APInt SqrtVal(SqrtTerm.sqrt()); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4231 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4232 | // Compute the two solutions for the quadratic formula. |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4233 | // The divisions must be performed as signed divisions. |
| 4234 | APInt NegB(-B); |
Reid Spencer | 3e35c8d | 2007-04-16 02:24:41 +0000 | [diff] [blame] | 4235 | APInt TwoA( A << 1 ); |
Nick Lewycky | 8f4d5eb | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 4236 | if (TwoA.isMinValue()) { |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4237 | const SCEV *CNC = SE.getCouldNotCompute(); |
Nick Lewycky | 8f4d5eb | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 4238 | return std::make_pair(CNC, CNC); |
| 4239 | } |
| 4240 | |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 4241 | LLVMContext &Context = SE.getContext(); |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4242 | |
| 4243 | ConstantInt *Solution1 = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4244 | ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA)); |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4245 | ConstantInt *Solution2 = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4246 | ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA)); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4247 | |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4248 | return std::make_pair(SE.getConstant(Solution1), |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4249 | SE.getConstant(Solution2)); |
Reid Spencer | e8019bb | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4250 | } // end APIntOps namespace |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4251 | } |
| 4252 | |
| 4253 | /// HowFarToZero - Return the number of times a backedge comparing the specified |
Dan Gohman | 86fbf2f | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4254 | /// value to zero will execute. If not computable, return CouldNotCompute. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4255 | const SCEV *ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4256 | // If the value is a constant |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4257 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4258 | // If the value is already zero, the branch will execute zero times. |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 4259 | if (C->getValue()->isZero()) return C; |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4260 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4261 | } |
| 4262 | |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4263 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4264 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4265 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4266 | |
| 4267 | if (AddRec->isAffine()) { |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4268 | // If this is an affine expression, the execution count of this branch is |
| 4269 | // the minimum unsigned root of the following equation: |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4270 | // |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4271 | // Start + Step*N = 0 (mod 2^BW) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4272 | // |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4273 | // equivalent to: |
| 4274 | // |
| 4275 | // Step*N = -Start (mod 2^BW) |
| 4276 | // |
| 4277 | // where BW is the common bit width of Start and Step. |
| 4278 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4279 | // Get the initial value for the loop. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4280 | const SCEV *Start = getSCEVAtScope(AddRec->getStart(), |
| 4281 | L->getParentLoop()); |
| 4282 | const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), |
| 4283 | L->getParentLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4284 | |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4285 | if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) { |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4286 | // For now we handle only constant steps. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4287 | |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4288 | // First, handle unitary steps. |
| 4289 | if (StepC->getValue()->equalsInt(1)) // 1*N = -Start (mod 2^BW), so: |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 4290 | return getNegativeSCEV(Start); // N = -Start (as unsigned) |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4291 | if (StepC->getValue()->isAllOnesValue()) // -1*N = -Start (mod 2^BW), so: |
| 4292 | return Start; // N = Start (as unsigned) |
| 4293 | |
| 4294 | // Then, try to solve the above equation provided that Start is constant. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4295 | if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) |
Wojciech Matyjewicz | de0f238 | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4296 | return SolveLinEquationWithOverflow(StepC->getValue()->getValue(), |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4297 | -StartC->getValue()->getValue(), |
| 4298 | *this); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4299 | } |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4300 | } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4301 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of |
| 4302 | // the quadratic equation to solve it. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4303 | std::pair<const SCEV *,const SCEV *> Roots = SolveQuadraticEquation(AddRec, |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4304 | *this); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4305 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 4306 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4307 | if (R1) { |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 4308 | #if 0 |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 4309 | errs() << "HFTZ: " << *V << " - sol#1: " << *R1 |
| 4310 | << " sol#2: " << *R2 << "\n"; |
Chris Lattner | d18d9dc | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 4311 | #endif |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4312 | // Pick the smallest positive root value. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4313 | if (ConstantInt *CB = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 4314 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4315 | R1->getValue(), R2->getValue()))) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4316 | if (CB->getZExtValue() == false) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4317 | std::swap(R1, R2); // R1 is the minimum root now. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4318 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4319 | // We can only use this value if the chrec ends up with an exact zero |
| 4320 | // value at this index. When solving for "X*X != 5", for example, we |
| 4321 | // should not accept a root of 2. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4322 | const SCEV *Val = AddRec->evaluateAtIteration(R1, *this); |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 4323 | if (Val->isZero()) |
| 4324 | return R1; // We found a quadratic root! |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4325 | } |
| 4326 | } |
| 4327 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4328 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4329 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4330 | } |
| 4331 | |
| 4332 | /// HowFarToNonZero - Return the number of times a backedge checking the |
| 4333 | /// specified value for nonzero will execute. If not computable, return |
Dan Gohman | 86fbf2f | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4334 | /// CouldNotCompute |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4335 | const SCEV *ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4336 | // Loops that look like: while (X == 0) are very strange indeed. We don't |
| 4337 | // handle them yet except for the trivial case. This could be expanded in the |
| 4338 | // future as needed. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4339 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4340 | // If the value is a constant, check to see if it is known to be non-zero |
| 4341 | // already. If so, the backedge will execute zero times. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4342 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
Nick Lewycky | 39442af | 2008-02-21 09:14:53 +0000 | [diff] [blame] | 4343 | if (!C->getValue()->isNullValue()) |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4344 | return getIntegerSCEV(0, C->getType()); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4345 | return getCouldNotCompute(); // Otherwise it will loop infinitely. |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4346 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4347 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4348 | // We could implement others, but I really doubt anyone writes loops like |
| 4349 | // this, and if they did, they would already be constant folded. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4350 | return getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4351 | } |
| 4352 | |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4353 | /// getLoopPredecessor - If the given loop's header has exactly one unique |
| 4354 | /// predecessor outside the loop, return it. Otherwise return null. |
| 4355 | /// |
| 4356 | BasicBlock *ScalarEvolution::getLoopPredecessor(const Loop *L) { |
| 4357 | BasicBlock *Header = L->getHeader(); |
| 4358 | BasicBlock *Pred = 0; |
| 4359 | for (pred_iterator PI = pred_begin(Header), E = pred_end(Header); |
| 4360 | PI != E; ++PI) |
| 4361 | if (!L->contains(*PI)) { |
| 4362 | if (Pred && Pred != *PI) return 0; // Multiple predecessors. |
| 4363 | Pred = *PI; |
| 4364 | } |
| 4365 | return Pred; |
| 4366 | } |
| 4367 | |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4368 | /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB |
| 4369 | /// (which may not be an immediate predecessor) which has exactly one |
| 4370 | /// successor from which BB is reachable, or null if no such block is |
| 4371 | /// found. |
| 4372 | /// |
| 4373 | BasicBlock * |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4374 | ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) { |
Dan Gohman | 3d739fe | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 4375 | // If the block has a unique predecessor, then there is no path from the |
| 4376 | // predecessor to the block that does not go through the direct edge |
| 4377 | // from the predecessor to the block. |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4378 | if (BasicBlock *Pred = BB->getSinglePredecessor()) |
| 4379 | return Pred; |
| 4380 | |
| 4381 | // A loop's header is defined to be a block that dominates the loop. |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4382 | // If the header has a unique predecessor outside the loop, it must be |
| 4383 | // a block that has exactly one successor that can reach the loop. |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4384 | if (Loop *L = LI->getLoopFor(BB)) |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4385 | return getLoopPredecessor(L); |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4386 | |
| 4387 | return 0; |
| 4388 | } |
| 4389 | |
Dan Gohman | 763bad1 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4390 | /// HasSameValue - SCEV structural equivalence is usually sufficient for |
| 4391 | /// testing whether two expressions are equal, however for the purposes of |
| 4392 | /// looking for a condition guarding a loop, it can be useful to be a little |
| 4393 | /// more general, since a front-end may have replicated the controlling |
| 4394 | /// expression. |
| 4395 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4396 | static bool HasSameValue(const SCEV *A, const SCEV *B) { |
Dan Gohman | 763bad1 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4397 | // Quick check to see if they are the same SCEV. |
| 4398 | if (A == B) return true; |
| 4399 | |
| 4400 | // Otherwise, if they're both SCEVUnknown, it's possible that they hold |
| 4401 | // two different instructions with the same value. Check for this case. |
| 4402 | if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A)) |
| 4403 | if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B)) |
| 4404 | if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue())) |
| 4405 | if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue())) |
Dan Gohman | 041de42 | 2009-08-25 17:56:57 +0000 | [diff] [blame] | 4406 | if (AI->isIdenticalTo(BI) && !AI->mayReadFromMemory()) |
Dan Gohman | 763bad1 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4407 | return true; |
| 4408 | |
| 4409 | // Otherwise assume they may have a different value. |
| 4410 | return false; |
| 4411 | } |
| 4412 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4413 | bool ScalarEvolution::isKnownNegative(const SCEV *S) { |
| 4414 | return getSignedRange(S).getSignedMax().isNegative(); |
| 4415 | } |
| 4416 | |
| 4417 | bool ScalarEvolution::isKnownPositive(const SCEV *S) { |
| 4418 | return getSignedRange(S).getSignedMin().isStrictlyPositive(); |
| 4419 | } |
| 4420 | |
| 4421 | bool ScalarEvolution::isKnownNonNegative(const SCEV *S) { |
| 4422 | return !getSignedRange(S).getSignedMin().isNegative(); |
| 4423 | } |
| 4424 | |
| 4425 | bool ScalarEvolution::isKnownNonPositive(const SCEV *S) { |
| 4426 | return !getSignedRange(S).getSignedMax().isStrictlyPositive(); |
| 4427 | } |
| 4428 | |
| 4429 | bool ScalarEvolution::isKnownNonZero(const SCEV *S) { |
| 4430 | return isKnownNegative(S) || isKnownPositive(S); |
| 4431 | } |
| 4432 | |
| 4433 | bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred, |
| 4434 | const SCEV *LHS, const SCEV *RHS) { |
| 4435 | |
| 4436 | if (HasSameValue(LHS, RHS)) |
| 4437 | return ICmpInst::isTrueWhenEqual(Pred); |
| 4438 | |
| 4439 | switch (Pred) { |
| 4440 | default: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4441 | llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4442 | break; |
| 4443 | case ICmpInst::ICMP_SGT: |
| 4444 | Pred = ICmpInst::ICMP_SLT; |
| 4445 | std::swap(LHS, RHS); |
| 4446 | case ICmpInst::ICMP_SLT: { |
| 4447 | ConstantRange LHSRange = getSignedRange(LHS); |
| 4448 | ConstantRange RHSRange = getSignedRange(RHS); |
| 4449 | if (LHSRange.getSignedMax().slt(RHSRange.getSignedMin())) |
| 4450 | return true; |
| 4451 | if (LHSRange.getSignedMin().sge(RHSRange.getSignedMax())) |
| 4452 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4453 | break; |
| 4454 | } |
| 4455 | case ICmpInst::ICMP_SGE: |
| 4456 | Pred = ICmpInst::ICMP_SLE; |
| 4457 | std::swap(LHS, RHS); |
| 4458 | case ICmpInst::ICMP_SLE: { |
| 4459 | ConstantRange LHSRange = getSignedRange(LHS); |
| 4460 | ConstantRange RHSRange = getSignedRange(RHS); |
| 4461 | if (LHSRange.getSignedMax().sle(RHSRange.getSignedMin())) |
| 4462 | return true; |
| 4463 | if (LHSRange.getSignedMin().sgt(RHSRange.getSignedMax())) |
| 4464 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4465 | break; |
| 4466 | } |
| 4467 | case ICmpInst::ICMP_UGT: |
| 4468 | Pred = ICmpInst::ICMP_ULT; |
| 4469 | std::swap(LHS, RHS); |
| 4470 | case ICmpInst::ICMP_ULT: { |
| 4471 | ConstantRange LHSRange = getUnsignedRange(LHS); |
| 4472 | ConstantRange RHSRange = getUnsignedRange(RHS); |
| 4473 | if (LHSRange.getUnsignedMax().ult(RHSRange.getUnsignedMin())) |
| 4474 | return true; |
| 4475 | if (LHSRange.getUnsignedMin().uge(RHSRange.getUnsignedMax())) |
| 4476 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4477 | break; |
| 4478 | } |
| 4479 | case ICmpInst::ICMP_UGE: |
| 4480 | Pred = ICmpInst::ICMP_ULE; |
| 4481 | std::swap(LHS, RHS); |
| 4482 | case ICmpInst::ICMP_ULE: { |
| 4483 | ConstantRange LHSRange = getUnsignedRange(LHS); |
| 4484 | ConstantRange RHSRange = getUnsignedRange(RHS); |
| 4485 | if (LHSRange.getUnsignedMax().ule(RHSRange.getUnsignedMin())) |
| 4486 | return true; |
| 4487 | if (LHSRange.getUnsignedMin().ugt(RHSRange.getUnsignedMax())) |
| 4488 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4489 | break; |
| 4490 | } |
| 4491 | case ICmpInst::ICMP_NE: { |
| 4492 | if (getUnsignedRange(LHS).intersectWith(getUnsignedRange(RHS)).isEmptySet()) |
| 4493 | return true; |
| 4494 | if (getSignedRange(LHS).intersectWith(getSignedRange(RHS)).isEmptySet()) |
| 4495 | return true; |
| 4496 | |
| 4497 | const SCEV *Diff = getMinusSCEV(LHS, RHS); |
| 4498 | if (isKnownNonZero(Diff)) |
| 4499 | return true; |
| 4500 | break; |
| 4501 | } |
| 4502 | case ICmpInst::ICMP_EQ: |
Dan Gohman | f117ed4 | 2009-07-20 23:54:43 +0000 | [diff] [blame] | 4503 | // The check at the top of the function catches the case where |
| 4504 | // the values are known to be equal. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4505 | break; |
| 4506 | } |
| 4507 | return false; |
| 4508 | } |
| 4509 | |
| 4510 | /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is |
| 4511 | /// protected by a conditional between LHS and RHS. This is used to |
| 4512 | /// to eliminate casts. |
| 4513 | bool |
| 4514 | ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L, |
| 4515 | ICmpInst::Predicate Pred, |
| 4516 | const SCEV *LHS, const SCEV *RHS) { |
| 4517 | // Interpret a null as meaning no loop, where there is obviously no guard |
| 4518 | // (interprocedural conditions notwithstanding). |
| 4519 | if (!L) return true; |
| 4520 | |
| 4521 | BasicBlock *Latch = L->getLoopLatch(); |
| 4522 | if (!Latch) |
| 4523 | return false; |
| 4524 | |
| 4525 | BranchInst *LoopContinuePredicate = |
| 4526 | dyn_cast<BranchInst>(Latch->getTerminator()); |
| 4527 | if (!LoopContinuePredicate || |
| 4528 | LoopContinuePredicate->isUnconditional()) |
| 4529 | return false; |
| 4530 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4531 | return isImpliedCond(LoopContinuePredicate->getCondition(), Pred, LHS, RHS, |
| 4532 | LoopContinuePredicate->getSuccessor(0) != L->getHeader()); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | /// isLoopGuardedByCond - Test whether entry to the loop is protected |
| 4536 | /// by a conditional between LHS and RHS. This is used to help avoid max |
| 4537 | /// expressions in loop trip counts, and to eliminate casts. |
| 4538 | bool |
| 4539 | ScalarEvolution::isLoopGuardedByCond(const Loop *L, |
| 4540 | ICmpInst::Predicate Pred, |
| 4541 | const SCEV *LHS, const SCEV *RHS) { |
Dan Gohman | 8ea9452 | 2009-05-18 16:03:58 +0000 | [diff] [blame] | 4542 | // Interpret a null as meaning no loop, where there is obviously no guard |
| 4543 | // (interprocedural conditions notwithstanding). |
| 4544 | if (!L) return false; |
| 4545 | |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4546 | BasicBlock *Predecessor = getLoopPredecessor(L); |
| 4547 | BasicBlock *PredecessorDest = L->getHeader(); |
Nick Lewycky | 59cff12 | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4548 | |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4549 | // Starting at the loop predecessor, climb up the predecessor chain, as long |
| 4550 | // as there are predecessors that can be found that have unique successors |
Dan Gohman | fd6edef | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4551 | // leading to the original header. |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4552 | for (; Predecessor; |
| 4553 | PredecessorDest = Predecessor, |
| 4554 | Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) { |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4555 | |
| 4556 | BranchInst *LoopEntryPredicate = |
Dan Gohman | 859b482 | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4557 | dyn_cast<BranchInst>(Predecessor->getTerminator()); |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4558 | if (!LoopEntryPredicate || |
| 4559 | LoopEntryPredicate->isUnconditional()) |
| 4560 | continue; |
| 4561 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4562 | if (isImpliedCond(LoopEntryPredicate->getCondition(), Pred, LHS, RHS, |
| 4563 | LoopEntryPredicate->getSuccessor(0) != PredecessorDest)) |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4564 | return true; |
Nick Lewycky | 59cff12 | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4565 | } |
| 4566 | |
Dan Gohman | 3837218 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4567 | return false; |
Nick Lewycky | 59cff12 | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4570 | /// isImpliedCond - Test whether the condition described by Pred, LHS, |
| 4571 | /// and RHS is true whenever the given Cond value evaluates to true. |
| 4572 | bool ScalarEvolution::isImpliedCond(Value *CondValue, |
| 4573 | ICmpInst::Predicate Pred, |
| 4574 | const SCEV *LHS, const SCEV *RHS, |
| 4575 | bool Inverse) { |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4576 | // Recursivly handle And and Or conditions. |
| 4577 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CondValue)) { |
| 4578 | if (BO->getOpcode() == Instruction::And) { |
| 4579 | if (!Inverse) |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4580 | return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) || |
| 4581 | isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4582 | } else if (BO->getOpcode() == Instruction::Or) { |
| 4583 | if (Inverse) |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4584 | return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) || |
| 4585 | isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4586 | } |
| 4587 | } |
| 4588 | |
| 4589 | ICmpInst *ICI = dyn_cast<ICmpInst>(CondValue); |
| 4590 | if (!ICI) return false; |
| 4591 | |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4592 | // Bail if the ICmp's operands' types are wider than the needed type |
| 4593 | // before attempting to call getSCEV on them. This avoids infinite |
| 4594 | // recursion, since the analysis of widening casts can require loop |
| 4595 | // exit condition information for overflow checking, which would |
| 4596 | // lead back here. |
| 4597 | if (getTypeSizeInBits(LHS->getType()) < |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4598 | getTypeSizeInBits(ICI->getOperand(0)->getType())) |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4599 | return false; |
| 4600 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4601 | // Now that we found a conditional branch that dominates the loop, check to |
| 4602 | // see if it is the comparison we are looking for. |
| 4603 | ICmpInst::Predicate FoundPred; |
| 4604 | if (Inverse) |
| 4605 | FoundPred = ICI->getInversePredicate(); |
| 4606 | else |
| 4607 | FoundPred = ICI->getPredicate(); |
| 4608 | |
| 4609 | const SCEV *FoundLHS = getSCEV(ICI->getOperand(0)); |
| 4610 | const SCEV *FoundRHS = getSCEV(ICI->getOperand(1)); |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4611 | |
| 4612 | // Balance the types. The case where FoundLHS' type is wider than |
| 4613 | // LHS' type is checked for above. |
| 4614 | if (getTypeSizeInBits(LHS->getType()) > |
| 4615 | getTypeSizeInBits(FoundLHS->getType())) { |
| 4616 | if (CmpInst::isSigned(Pred)) { |
| 4617 | FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType()); |
| 4618 | FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType()); |
| 4619 | } else { |
| 4620 | FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType()); |
| 4621 | FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType()); |
| 4622 | } |
| 4623 | } |
| 4624 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4625 | // Canonicalize the query to match the way instcombine will have |
| 4626 | // canonicalized the comparison. |
| 4627 | // First, put a constant operand on the right. |
| 4628 | if (isa<SCEVConstant>(LHS)) { |
| 4629 | std::swap(LHS, RHS); |
| 4630 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4631 | } |
| 4632 | // Then, canonicalize comparisons with boundary cases. |
| 4633 | if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) { |
| 4634 | const APInt &RA = RC->getValue()->getValue(); |
| 4635 | switch (Pred) { |
| 4636 | default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
| 4637 | case ICmpInst::ICMP_EQ: |
| 4638 | case ICmpInst::ICMP_NE: |
| 4639 | break; |
| 4640 | case ICmpInst::ICMP_UGE: |
| 4641 | if ((RA - 1).isMinValue()) { |
| 4642 | Pred = ICmpInst::ICMP_NE; |
| 4643 | RHS = getConstant(RA - 1); |
| 4644 | break; |
| 4645 | } |
| 4646 | if (RA.isMaxValue()) { |
| 4647 | Pred = ICmpInst::ICMP_EQ; |
| 4648 | break; |
| 4649 | } |
| 4650 | if (RA.isMinValue()) return true; |
| 4651 | break; |
| 4652 | case ICmpInst::ICMP_ULE: |
| 4653 | if ((RA + 1).isMaxValue()) { |
| 4654 | Pred = ICmpInst::ICMP_NE; |
| 4655 | RHS = getConstant(RA + 1); |
| 4656 | break; |
| 4657 | } |
| 4658 | if (RA.isMinValue()) { |
| 4659 | Pred = ICmpInst::ICMP_EQ; |
| 4660 | break; |
| 4661 | } |
| 4662 | if (RA.isMaxValue()) return true; |
| 4663 | break; |
| 4664 | case ICmpInst::ICMP_SGE: |
| 4665 | if ((RA - 1).isMinSignedValue()) { |
| 4666 | Pred = ICmpInst::ICMP_NE; |
| 4667 | RHS = getConstant(RA - 1); |
| 4668 | break; |
| 4669 | } |
| 4670 | if (RA.isMaxSignedValue()) { |
| 4671 | Pred = ICmpInst::ICMP_EQ; |
| 4672 | break; |
| 4673 | } |
| 4674 | if (RA.isMinSignedValue()) return true; |
| 4675 | break; |
| 4676 | case ICmpInst::ICMP_SLE: |
| 4677 | if ((RA + 1).isMaxSignedValue()) { |
| 4678 | Pred = ICmpInst::ICMP_NE; |
| 4679 | RHS = getConstant(RA + 1); |
| 4680 | break; |
| 4681 | } |
| 4682 | if (RA.isMinSignedValue()) { |
| 4683 | Pred = ICmpInst::ICMP_EQ; |
| 4684 | break; |
| 4685 | } |
| 4686 | if (RA.isMaxSignedValue()) return true; |
| 4687 | break; |
| 4688 | case ICmpInst::ICMP_UGT: |
| 4689 | if (RA.isMinValue()) { |
| 4690 | Pred = ICmpInst::ICMP_NE; |
| 4691 | break; |
| 4692 | } |
| 4693 | if ((RA + 1).isMaxValue()) { |
| 4694 | Pred = ICmpInst::ICMP_EQ; |
| 4695 | RHS = getConstant(RA + 1); |
| 4696 | break; |
| 4697 | } |
| 4698 | if (RA.isMaxValue()) return false; |
| 4699 | break; |
| 4700 | case ICmpInst::ICMP_ULT: |
| 4701 | if (RA.isMaxValue()) { |
| 4702 | Pred = ICmpInst::ICMP_NE; |
| 4703 | break; |
| 4704 | } |
| 4705 | if ((RA - 1).isMinValue()) { |
| 4706 | Pred = ICmpInst::ICMP_EQ; |
| 4707 | RHS = getConstant(RA - 1); |
| 4708 | break; |
| 4709 | } |
| 4710 | if (RA.isMinValue()) return false; |
| 4711 | break; |
| 4712 | case ICmpInst::ICMP_SGT: |
| 4713 | if (RA.isMinSignedValue()) { |
| 4714 | Pred = ICmpInst::ICMP_NE; |
| 4715 | break; |
| 4716 | } |
| 4717 | if ((RA + 1).isMaxSignedValue()) { |
| 4718 | Pred = ICmpInst::ICMP_EQ; |
| 4719 | RHS = getConstant(RA + 1); |
| 4720 | break; |
| 4721 | } |
| 4722 | if (RA.isMaxSignedValue()) return false; |
| 4723 | break; |
| 4724 | case ICmpInst::ICMP_SLT: |
| 4725 | if (RA.isMaxSignedValue()) { |
| 4726 | Pred = ICmpInst::ICMP_NE; |
| 4727 | break; |
| 4728 | } |
| 4729 | if ((RA - 1).isMinSignedValue()) { |
| 4730 | Pred = ICmpInst::ICMP_EQ; |
| 4731 | RHS = getConstant(RA - 1); |
| 4732 | break; |
| 4733 | } |
| 4734 | if (RA.isMinSignedValue()) return false; |
| 4735 | break; |
| 4736 | } |
| 4737 | } |
| 4738 | |
| 4739 | // Check to see if we can make the LHS or RHS match. |
| 4740 | if (LHS == FoundRHS || RHS == FoundLHS) { |
| 4741 | if (isa<SCEVConstant>(RHS)) { |
| 4742 | std::swap(FoundLHS, FoundRHS); |
| 4743 | FoundPred = ICmpInst::getSwappedPredicate(FoundPred); |
| 4744 | } else { |
| 4745 | std::swap(LHS, RHS); |
| 4746 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4747 | } |
| 4748 | } |
| 4749 | |
| 4750 | // Check whether the found predicate is the same as the desired predicate. |
| 4751 | if (FoundPred == Pred) |
| 4752 | return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS); |
| 4753 | |
| 4754 | // Check whether swapping the found predicate makes it the same as the |
| 4755 | // desired predicate. |
| 4756 | if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) { |
| 4757 | if (isa<SCEVConstant>(RHS)) |
| 4758 | return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS); |
| 4759 | else |
| 4760 | return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred), |
| 4761 | RHS, LHS, FoundLHS, FoundRHS); |
| 4762 | } |
| 4763 | |
| 4764 | // Check whether the actual condition is beyond sufficient. |
| 4765 | if (FoundPred == ICmpInst::ICMP_EQ) |
| 4766 | if (ICmpInst::isTrueWhenEqual(Pred)) |
| 4767 | if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS)) |
| 4768 | return true; |
| 4769 | if (Pred == ICmpInst::ICMP_NE) |
| 4770 | if (!ICmpInst::isTrueWhenEqual(FoundPred)) |
| 4771 | if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS)) |
| 4772 | return true; |
| 4773 | |
| 4774 | // Otherwise assume the worst. |
| 4775 | return false; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4776 | } |
| 4777 | |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4778 | /// isImpliedCondOperands - Test whether the condition described by Pred, |
| 4779 | /// LHS, and RHS is true whenever the condition desribed by Pred, FoundLHS, |
| 4780 | /// and FoundRHS is true. |
| 4781 | bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred, |
| 4782 | const SCEV *LHS, const SCEV *RHS, |
| 4783 | const SCEV *FoundLHS, |
| 4784 | const SCEV *FoundRHS) { |
| 4785 | return isImpliedCondOperandsHelper(Pred, LHS, RHS, |
| 4786 | FoundLHS, FoundRHS) || |
| 4787 | // ~x < ~y --> x > y |
| 4788 | isImpliedCondOperandsHelper(Pred, LHS, RHS, |
| 4789 | getNotSCEV(FoundRHS), |
| 4790 | getNotSCEV(FoundLHS)); |
| 4791 | } |
| 4792 | |
| 4793 | /// isImpliedCondOperandsHelper - Test whether the condition described by |
| 4794 | /// Pred, LHS, and RHS is true whenever the condition desribed by Pred, |
| 4795 | /// FoundLHS, and FoundRHS is true. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4796 | bool |
Dan Gohman | 0f4b285 | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4797 | ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred, |
| 4798 | const SCEV *LHS, const SCEV *RHS, |
| 4799 | const SCEV *FoundLHS, |
| 4800 | const SCEV *FoundRHS) { |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4801 | switch (Pred) { |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4802 | default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); |
| 4803 | case ICmpInst::ICMP_EQ: |
| 4804 | case ICmpInst::ICMP_NE: |
| 4805 | if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS)) |
| 4806 | return true; |
| 4807 | break; |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4808 | case ICmpInst::ICMP_SLT: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4809 | case ICmpInst::ICMP_SLE: |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4810 | if (isKnownPredicate(ICmpInst::ICMP_SLE, LHS, FoundLHS) && |
| 4811 | isKnownPredicate(ICmpInst::ICMP_SGE, RHS, FoundRHS)) |
| 4812 | return true; |
| 4813 | break; |
| 4814 | case ICmpInst::ICMP_SGT: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4815 | case ICmpInst::ICMP_SGE: |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4816 | if (isKnownPredicate(ICmpInst::ICMP_SGE, LHS, FoundLHS) && |
| 4817 | isKnownPredicate(ICmpInst::ICMP_SLE, RHS, FoundRHS)) |
| 4818 | return true; |
| 4819 | break; |
| 4820 | case ICmpInst::ICMP_ULT: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4821 | case ICmpInst::ICMP_ULE: |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4822 | if (isKnownPredicate(ICmpInst::ICMP_ULE, LHS, FoundLHS) && |
| 4823 | isKnownPredicate(ICmpInst::ICMP_UGE, RHS, FoundRHS)) |
| 4824 | return true; |
| 4825 | break; |
| 4826 | case ICmpInst::ICMP_UGT: |
Dan Gohman | 850f791 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4827 | case ICmpInst::ICMP_UGE: |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4828 | if (isKnownPredicate(ICmpInst::ICMP_UGE, LHS, FoundLHS) && |
| 4829 | isKnownPredicate(ICmpInst::ICMP_ULE, RHS, FoundRHS)) |
| 4830 | return true; |
| 4831 | break; |
| 4832 | } |
| 4833 | |
| 4834 | return false; |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4835 | } |
| 4836 | |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4837 | /// getBECount - Subtract the end and start values and divide by the step, |
| 4838 | /// rounding up, to get the number of times the backedge is executed. Return |
| 4839 | /// CouldNotCompute if an intermediate computation overflows. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4840 | const SCEV *ScalarEvolution::getBECount(const SCEV *Start, |
Dan Gohman | f5074ec | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 4841 | const SCEV *End, |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 4842 | const SCEV *Step, |
| 4843 | bool NoWrap) { |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4844 | const Type *Ty = Start->getType(); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4845 | const SCEV *NegOne = getIntegerSCEV(-1, Ty); |
| 4846 | const SCEV *Diff = getMinusSCEV(End, Start); |
| 4847 | const SCEV *RoundUp = getAddExpr(Step, NegOne); |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4848 | |
| 4849 | // Add an adjustment to the difference between End and Start so that |
| 4850 | // the division will effectively round up. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4851 | const SCEV *Add = getAddExpr(Diff, RoundUp); |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4852 | |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 4853 | if (!NoWrap) { |
| 4854 | // Check Add for unsigned overflow. |
| 4855 | // TODO: More sophisticated things could be done here. |
| 4856 | const Type *WideTy = IntegerType::get(getContext(), |
| 4857 | getTypeSizeInBits(Ty) + 1); |
| 4858 | const SCEV *EDiff = getZeroExtendExpr(Diff, WideTy); |
| 4859 | const SCEV *ERoundUp = getZeroExtendExpr(RoundUp, WideTy); |
| 4860 | const SCEV *OperandExtendedAdd = getAddExpr(EDiff, ERoundUp); |
| 4861 | if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd) |
| 4862 | return getCouldNotCompute(); |
| 4863 | } |
Dan Gohman | 51f53b7 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4864 | |
| 4865 | return getUDivExpr(Add, Step); |
| 4866 | } |
| 4867 | |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4868 | /// HowManyLessThans - Return the number of times a backedge containing the |
| 4869 | /// specified less-than comparison will execute. If not computable, return |
Dan Gohman | 86fbf2f | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4870 | /// CouldNotCompute. |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4871 | ScalarEvolution::BackedgeTakenInfo |
| 4872 | ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS, |
| 4873 | const Loop *L, bool isSigned) { |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4874 | // Only handle: "ADDREC < LoopInvariant". |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4875 | if (!RHS->isLoopInvariant(L)) return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4876 | |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4877 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4878 | if (!AddRec || AddRec->getLoop() != L) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4879 | return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4880 | |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 4881 | // Check to see if we have a flag which makes analysis easy. |
| 4882 | bool NoWrap = isSigned ? AddRec->hasNoSignedWrap() : |
| 4883 | AddRec->hasNoUnsignedWrap(); |
| 4884 | |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4885 | if (AddRec->isAffine()) { |
Nick Lewycky | 789558d | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 4886 | // FORNOW: We only support unit strides. |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4887 | unsigned BitWidth = getTypeSizeInBits(AddRec->getType()); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4888 | const SCEV *Step = AddRec->getStepRecurrence(*this); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4889 | |
| 4890 | // TODO: handle non-constant strides. |
| 4891 | const SCEVConstant *CStep = dyn_cast<SCEVConstant>(Step); |
| 4892 | if (!CStep || CStep->isZero()) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4893 | return getCouldNotCompute(); |
Dan Gohman | 70a1fe7 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 4894 | if (CStep->isOne()) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4895 | // With unit stride, the iteration never steps past the limit value. |
| 4896 | } else if (CStep->getValue()->getValue().isStrictlyPositive()) { |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 4897 | if (NoWrap) { |
| 4898 | // We know the iteration won't step past the maximum value for its type. |
| 4899 | ; |
| 4900 | } else if (const SCEVConstant *CLimit = dyn_cast<SCEVConstant>(RHS)) { |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4901 | // Test whether a positive iteration iteration can step past the limit |
| 4902 | // value and past the maximum value for its type in a single step. |
| 4903 | if (isSigned) { |
| 4904 | APInt Max = APInt::getSignedMaxValue(BitWidth); |
| 4905 | if ((Max - CStep->getValue()->getValue()) |
| 4906 | .slt(CLimit->getValue()->getValue())) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4907 | return getCouldNotCompute(); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4908 | } else { |
| 4909 | APInt Max = APInt::getMaxValue(BitWidth); |
| 4910 | if ((Max - CStep->getValue()->getValue()) |
| 4911 | .ult(CLimit->getValue()->getValue())) |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4912 | return getCouldNotCompute(); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4913 | } |
| 4914 | } else |
| 4915 | // TODO: handle non-constant limit values below. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4916 | return getCouldNotCompute(); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4917 | } else |
| 4918 | // TODO: handle negative strides below. |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4919 | return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4920 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4921 | // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant |
| 4922 | // m. So, we count the number of iterations in which {n,+,s} < m is true. |
| 4923 | // Note that we cannot simply return max(m-n,0)/s because it's not safe to |
Wojciech Matyjewicz | a65ee03 | 2008-02-13 12:21:32 +0000 | [diff] [blame] | 4924 | // treat m-n as signed nor unsigned due to overflow possibility. |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4925 | |
Wojciech Matyjewicz | 3a4cbe2 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4926 | // First, we get the value of the LHS in the first iteration: n |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4927 | const SCEV *Start = AddRec->getOperand(0); |
Wojciech Matyjewicz | 3a4cbe2 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4928 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4929 | // Determine the minimum constant start value. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4930 | const SCEV *MinStart = getConstant(isSigned ? |
| 4931 | getSignedRange(Start).getSignedMin() : |
| 4932 | getUnsignedRange(Start).getUnsignedMin()); |
Wojciech Matyjewicz | 3a4cbe2 | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4933 | |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4934 | // If we know that the condition is true in order to enter the loop, |
| 4935 | // then we know that it will run exactly (m-n)/s times. Otherwise, we |
Dan Gohman | 6c0866c | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 4936 | // only know that it will execute (max(m,n)-n)/s times. In both cases, |
| 4937 | // the division must round up. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4938 | const SCEV *End = RHS; |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4939 | if (!isLoopGuardedByCond(L, |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4940 | isSigned ? ICmpInst::ICMP_SLT : |
| 4941 | ICmpInst::ICMP_ULT, |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4942 | getMinusSCEV(Start, Step), RHS)) |
| 4943 | End = isSigned ? getSMaxExpr(RHS, Start) |
| 4944 | : getUMaxExpr(RHS, Start); |
| 4945 | |
| 4946 | // Determine the maximum constant end value. |
Dan Gohman | 85b05a2 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4947 | const SCEV *MaxEnd = getConstant(isSigned ? |
| 4948 | getSignedRange(End).getSignedMax() : |
| 4949 | getUnsignedRange(End).getUnsignedMax()); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4950 | |
| 4951 | // Finally, we subtract these two values and divide, rounding up, to get |
| 4952 | // the number of times the backedge is executed. |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 4953 | const SCEV *BECount = getBECount(Start, End, Step, NoWrap); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4954 | |
| 4955 | // The maximum backedge count is similar, except using the minimum start |
| 4956 | // value and the maximum end value. |
Dan Gohman | 1f96e67 | 2009-09-17 18:05:20 +0000 | [diff] [blame] | 4957 | const SCEV *MaxBECount = getBECount(MinStart, MaxEnd, Step, NoWrap); |
Dan Gohman | a1af757 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4958 | |
| 4959 | return BackedgeTakenInfo(BECount, MaxBECount); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4960 | } |
| 4961 | |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4962 | return getCouldNotCompute(); |
Chris Lattner | db25de4 | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4965 | /// getNumIterationsInRange - Return the number of iterations of this loop that |
| 4966 | /// produce values in the specified constant range. Another way of looking at |
| 4967 | /// this is that it returns the first iteration number where the value is not in |
| 4968 | /// the condition, thus computing the exit count. If the iteration count can't |
| 4969 | /// be computed, an instance of SCEVCouldNotCompute is returned. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4970 | const SCEV *SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4971 | ScalarEvolution &SE) const { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4972 | if (Range.isFullSet()) // Infinite loop. |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4973 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4974 | |
| 4975 | // If the start is a non-zero constant, shift the range to simplify things. |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4976 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart())) |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 4977 | if (!SC->getValue()->isZero()) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4978 | SmallVector<const SCEV *, 4> Operands(op_begin(), op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4979 | Operands[0] = SE.getIntegerSCEV(0, SC->getType()); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4980 | const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop()); |
Dan Gohman | 622ed67 | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4981 | if (const SCEVAddRecExpr *ShiftedAddRec = |
| 4982 | dyn_cast<SCEVAddRecExpr>(Shifted)) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4983 | return ShiftedAddRec->getNumIterationsInRange( |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4984 | Range.subtract(SC->getValue()->getValue()), SE); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4985 | // This is strange and shouldn't happen. |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4986 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4987 | } |
| 4988 | |
| 4989 | // The only time we can solve this is when we have all constant indices. |
| 4990 | // Otherwise, we cannot determine the overflow conditions. |
| 4991 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
| 4992 | if (!isa<SCEVConstant>(getOperand(i))) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4993 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4994 | |
| 4995 | |
| 4996 | // Okay at this point we know that all elements of the chrec are constants and |
| 4997 | // that the start element is zero. |
| 4998 | |
| 4999 | // First check to see if the range contains zero. If not, the first |
| 5000 | // iteration exits. |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 5001 | unsigned BitWidth = SE.getTypeSizeInBits(getType()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 5002 | if (!Range.contains(APInt(BitWidth, 0))) |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 5003 | return SE.getIntegerSCEV(0, getType()); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5004 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5005 | if (isAffine()) { |
| 5006 | // If this is an affine expression then we have this situation: |
| 5007 | // Solve {0,+,A} in Range === Ax in Range |
| 5008 | |
Nick Lewycky | eefdebe | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 5009 | // We know that zero is in the range. If A is positive then we know that |
| 5010 | // the upper value of the range must be the first possible exit value. |
| 5011 | // If A is negative then the lower of the range is the last possible loop |
| 5012 | // value. Also note that we already checked for a full range. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 5013 | APInt One(BitWidth,1); |
Nick Lewycky | eefdebe | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 5014 | APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue(); |
| 5015 | APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5016 | |
Nick Lewycky | eefdebe | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 5017 | // The exit value should be (End+A)/A. |
Nick Lewycky | 9a2f931 | 2007-09-27 14:12:54 +0000 | [diff] [blame] | 5018 | APInt ExitVal = (End + A).udiv(A); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5019 | ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5020 | |
| 5021 | // Evaluate at the exit value. If we really did fall out of the valid |
| 5022 | // range, then we computed our trip count, otherwise wrap around or other |
| 5023 | // things must have happened. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5024 | ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5025 | if (Range.contains(Val->getValue())) |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5026 | return SE.getCouldNotCompute(); // Something strange happened |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5027 | |
| 5028 | // Ensure that the previous value is in the range. This is a sanity check. |
Reid Spencer | 581b0d4 | 2007-02-28 19:57:34 +0000 | [diff] [blame] | 5029 | assert(Range.contains( |
Dan Gohman | 64a845e | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 5030 | EvaluateConstantChrecAtConstant(this, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5031 | ConstantInt::get(SE.getContext(), ExitVal - One), SE)->getValue()) && |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5032 | "Linear scev computation is off in a bad way!"); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5033 | return SE.getConstant(ExitValue); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5034 | } else if (isQuadratic()) { |
| 5035 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the |
| 5036 | // quadratic equation to solve it. To do this, we must frame our problem in |
| 5037 | // terms of figuring out when zero is crossed, instead of when |
| 5038 | // Range.getUpper() is crossed. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5039 | SmallVector<const SCEV *, 4> NewOps(op_begin(), op_end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5040 | NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper())); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5041 | const SCEV *NewAddRec = SE.getAddRecExpr(NewOps, getLoop()); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5042 | |
| 5043 | // Next, solve the constructed addrec |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5044 | std::pair<const SCEV *,const SCEV *> Roots = |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5045 | SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5046 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); |
| 5047 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5048 | if (R1) { |
| 5049 | // Pick the smallest positive root value. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 5050 | if (ConstantInt *CB = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 5051 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 5052 | R1->getValue(), R2->getValue()))) { |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 5053 | if (CB->getZExtValue() == false) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5054 | std::swap(R1, R2); // R1 is the minimum root now. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5055 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5056 | // Make sure the root is not off by one. The returned iteration should |
| 5057 | // not be in the range, but the previous one should be. When solving |
| 5058 | // for "X*X < 5", for example, we should not return a root of 2. |
| 5059 | ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this, |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5060 | R1->getValue(), |
| 5061 | SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5062 | if (Range.contains(R1Val->getValue())) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5063 | // The next iteration must be out of the range... |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 5064 | ConstantInt *NextVal = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5065 | ConstantInt::get(SE.getContext(), R1->getValue()->getValue()+1); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5066 | |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5067 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5068 | if (!Range.contains(R1Val->getValue())) |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5069 | return SE.getConstant(NextVal); |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5070 | return SE.getCouldNotCompute(); // Something strange happened |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5071 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5072 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5073 | // If R1 was not in the range, then it is a good return value. Make |
| 5074 | // sure that R1-1 WAS in the range though, just in case. |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 5075 | ConstantInt *NextVal = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5076 | ConstantInt::get(SE.getContext(), R1->getValue()->getValue()-1); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5077 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); |
Reid Spencer | a6e8a95 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5078 | if (Range.contains(R1Val->getValue())) |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5079 | return R1; |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5080 | return SE.getCouldNotCompute(); // Something strange happened |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5081 | } |
| 5082 | } |
| 5083 | } |
| 5084 | |
Dan Gohman | f4ccfcb | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5085 | return SE.getCouldNotCompute(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
| 5088 | |
| 5089 | |
| 5090 | //===----------------------------------------------------------------------===// |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5091 | // SCEVCallbackVH Class Implementation |
| 5092 | //===----------------------------------------------------------------------===// |
| 5093 | |
Dan Gohman | 1959b75 | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 5094 | void ScalarEvolution::SCEVCallbackVH::deleted() { |
Dan Gohman | ddf9f99 | 2009-07-13 22:20:53 +0000 | [diff] [blame] | 5095 | assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5096 | if (PHINode *PN = dyn_cast<PHINode>(getValPtr())) |
| 5097 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
| 5098 | SE->Scalars.erase(getValPtr()); |
| 5099 | // this now dangles! |
| 5100 | } |
| 5101 | |
Dan Gohman | 1959b75 | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 5102 | void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *) { |
Dan Gohman | ddf9f99 | 2009-07-13 22:20:53 +0000 | [diff] [blame] | 5103 | assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5104 | |
| 5105 | // Forget all the expressions associated with users of the old value, |
| 5106 | // so that future queries will recompute the expressions using the new |
| 5107 | // value. |
| 5108 | SmallVector<User *, 16> Worklist; |
Dan Gohman | 69fcae9 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5109 | SmallPtrSet<User *, 8> Visited; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5110 | Value *Old = getValPtr(); |
| 5111 | bool DeleteOld = false; |
| 5112 | for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end(); |
| 5113 | UI != UE; ++UI) |
| 5114 | Worklist.push_back(*UI); |
| 5115 | while (!Worklist.empty()) { |
| 5116 | User *U = Worklist.pop_back_val(); |
| 5117 | // Deleting the Old value will cause this to dangle. Postpone |
| 5118 | // that until everything else is done. |
| 5119 | if (U == Old) { |
| 5120 | DeleteOld = true; |
| 5121 | continue; |
| 5122 | } |
Dan Gohman | 69fcae9 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5123 | if (!Visited.insert(U)) |
| 5124 | continue; |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5125 | if (PHINode *PN = dyn_cast<PHINode>(U)) |
| 5126 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
Dan Gohman | 69fcae9 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5127 | SE->Scalars.erase(U); |
| 5128 | for (Value::use_iterator UI = U->use_begin(), UE = U->use_end(); |
| 5129 | UI != UE; ++UI) |
| 5130 | Worklist.push_back(*UI); |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5131 | } |
Dan Gohman | 69fcae9 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5132 | // Delete the Old value if it (indirectly) references itself. |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5133 | if (DeleteOld) { |
| 5134 | if (PHINode *PN = dyn_cast<PHINode>(Old)) |
| 5135 | SE->ConstantEvolutionLoopExitValue.erase(PN); |
| 5136 | SE->Scalars.erase(Old); |
| 5137 | // this now dangles! |
| 5138 | } |
| 5139 | // this may dangle! |
| 5140 | } |
| 5141 | |
Dan Gohman | 1959b75 | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 5142 | ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5143 | : CallbackVH(V), SE(se) {} |
| 5144 | |
| 5145 | //===----------------------------------------------------------------------===// |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5146 | // ScalarEvolution Class Implementation |
| 5147 | //===----------------------------------------------------------------------===// |
| 5148 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5149 | ScalarEvolution::ScalarEvolution() |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5150 | : FunctionPass(&ID) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5151 | } |
| 5152 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5153 | bool ScalarEvolution::runOnFunction(Function &F) { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5154 | this->F = &F; |
| 5155 | LI = &getAnalysis<LoopInfo>(); |
| 5156 | TD = getAnalysisIfAvailable<TargetData>(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5157 | return false; |
| 5158 | } |
| 5159 | |
| 5160 | void ScalarEvolution::releaseMemory() { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5161 | Scalars.clear(); |
| 5162 | BackedgeTakenCounts.clear(); |
| 5163 | ConstantEvolutionLoopExitValue.clear(); |
Dan Gohman | 6bce643 | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 5164 | ValuesAtScopes.clear(); |
Dan Gohman | 1c34375 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5165 | UniqueSCEVs.clear(); |
| 5166 | SCEVAllocator.Reset(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5167 | } |
| 5168 | |
| 5169 | void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const { |
| 5170 | AU.setPreservesAll(); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5171 | AU.addRequiredTransitive<LoopInfo>(); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 5172 | } |
| 5173 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5174 | bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) { |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5175 | return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L)); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5176 | } |
| 5177 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5178 | static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5179 | const Loop *L) { |
| 5180 | // Print all inner loops first |
| 5181 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 5182 | PrintLoopInfo(OS, SE, *I); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5183 | |
Nick Lewycky | aeb5e5c | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 5184 | OS << "Loop " << L->getHeader()->getName() << ": "; |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 5185 | |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 5186 | SmallVector<BasicBlock *, 8> ExitBlocks; |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 5187 | L->getExitBlocks(ExitBlocks); |
| 5188 | if (ExitBlocks.size() != 1) |
Nick Lewycky | aeb5e5c | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 5189 | OS << "<multiple exits> "; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5190 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5191 | if (SE->hasLoopInvariantBackedgeTakenCount(L)) { |
| 5192 | OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5193 | } else { |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5194 | OS << "Unpredictable backedge-taken count. "; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5195 | } |
| 5196 | |
Nick Lewycky | aeb5e5c | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 5197 | OS << "\n"; |
Dan Gohman | aa551ae | 2009-06-24 00:33:16 +0000 | [diff] [blame] | 5198 | OS << "Loop " << L->getHeader()->getName() << ": "; |
| 5199 | |
| 5200 | if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) { |
| 5201 | OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L); |
| 5202 | } else { |
| 5203 | OS << "Unpredictable max backedge-taken count. "; |
| 5204 | } |
| 5205 | |
| 5206 | OS << "\n"; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5207 | } |
| 5208 | |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 5209 | void ScalarEvolution::print(raw_ostream &OS, const Module *) const { |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5210 | // ScalarEvolution's implementaiton of the print method is to print |
| 5211 | // out SCEV values of all instructions that are interesting. Doing |
| 5212 | // this potentially causes it to create new SCEV objects though, |
| 5213 | // which technically conflicts with the const qualifier. This isn't |
Dan Gohman | 1afdc5f | 2009-07-10 20:25:29 +0000 | [diff] [blame] | 5214 | // observable from outside the class though, so casting away the |
| 5215 | // const isn't dangerous. |
Dan Gohman | 5d98491 | 2009-12-18 01:14:11 +0000 | [diff] [blame] | 5216 | ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5217 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5218 | OS << "Classifying expressions for: " << F->getName() << "\n"; |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5219 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) |
Dan Gohman | d9c1c85 | 2009-04-30 01:30:18 +0000 | [diff] [blame] | 5220 | if (isSCEVable(I->getType())) { |
Dan Gohman | c902e13 | 2009-07-13 23:03:05 +0000 | [diff] [blame] | 5221 | OS << *I << '\n'; |
Dan Gohman | 8dae138 | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 5222 | OS << " --> "; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5223 | const SCEV *SV = SE.getSCEV(&*I); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5224 | SV->print(OS); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5225 | |
Dan Gohman | 0c689c5 | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 5226 | const Loop *L = LI->getLoopFor((*I).getParent()); |
| 5227 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5228 | const SCEV *AtUse = SE.getSCEVAtScope(SV, L); |
Dan Gohman | 0c689c5 | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 5229 | if (AtUse != SV) { |
| 5230 | OS << " --> "; |
| 5231 | AtUse->print(OS); |
| 5232 | } |
| 5233 | |
| 5234 | if (L) { |
Dan Gohman | 9e7d988 | 2009-06-18 00:37:45 +0000 | [diff] [blame] | 5235 | OS << "\t\t" "Exits: "; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5236 | const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop()); |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 5237 | if (!ExitValue->isLoopInvariant(L)) { |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5238 | OS << "<<Unknown>>"; |
| 5239 | } else { |
| 5240 | OS << *ExitValue; |
| 5241 | } |
| 5242 | } |
| 5243 | |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5244 | OS << "\n"; |
| 5245 | } |
| 5246 | |
Dan Gohman | f8a8be8 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5247 | OS << "Determining loop execution counts for: " << F->getName() << "\n"; |
| 5248 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
| 5249 | PrintLoopInfo(OS, &SE, *I); |
Chris Lattner | 53e677a | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5250 | } |
Dan Gohman | b7ef729 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 5251 | |