| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1 | //===- ScalarEvolution.cpp - Scalar Evolution Analysis ----------*- C++ -*-===// | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | f3ebc3f | 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 | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // | 
| Chris Lattner | d934c70 | 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 | ef2ae2c | 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 | d934c70 | 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 | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 30 | // | 
| Chris Lattner | d934c70 | 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 | d934c70 | 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 | 57ef942 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 61 | #define DEBUG_TYPE "scalar-evolution" | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 62 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 63 | #include "llvm/Constants.h" | 
|  | 64 | #include "llvm/DerivedTypes.h" | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 65 | #include "llvm/GlobalVariable.h" | 
| Dan Gohman | f161e06e | 2009-08-25 17:49:57 +0000 | [diff] [blame] | 66 | #include "llvm/GlobalAlias.h" | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 67 | #include "llvm/Instructions.h" | 
| Owen Anderson | f1f1743 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 68 | #include "llvm/LLVMContext.h" | 
| Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 69 | #include "llvm/Operator.h" | 
| John Criswell | fe5f33b | 2005-10-27 15:54:34 +0000 | [diff] [blame] | 70 | #include "llvm/Analysis/ConstantFolding.h" | 
| Evan Cheng | 161861d | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 71 | #include "llvm/Analysis/Dominators.h" | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 72 | #include "llvm/Analysis/LoopInfo.h" | 
| Dan Gohman | 1ee696d | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 73 | #include "llvm/Analysis/ValueTracking.h" | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 74 | #include "llvm/Assembly/Writer.h" | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 75 | #include "llvm/Target/TargetData.h" | 
| Chris Lattner | 996795b | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 76 | #include "llvm/Support/CommandLine.h" | 
| Chris Lattner | 538c6eb | 2006-10-04 21:49:37 +0000 | [diff] [blame] | 77 | #include "llvm/Support/Compiler.h" | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 78 | #include "llvm/Support/ConstantRange.h" | 
| Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 79 | #include "llvm/Support/ErrorHandling.h" | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 80 | #include "llvm/Support/GetElementPtrTypeIterator.h" | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 81 | #include "llvm/Support/InstIterator.h" | 
| Chris Lattner | 0a1e993 | 2006-12-19 01:16:02 +0000 | [diff] [blame] | 82 | #include "llvm/Support/MathExtras.h" | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 83 | #include "llvm/Support/raw_ostream.h" | 
| Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 84 | #include "llvm/ADT/Statistic.h" | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 85 | #include "llvm/ADT/STLExtras.h" | 
| Dan Gohman | dc19104 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 86 | #include "llvm/ADT/SmallPtrSet.h" | 
| Alkis Evlogimenos | a5c04ee | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 87 | #include <algorithm> | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 88 | using namespace llvm; | 
|  | 89 |  | 
| Chris Lattner | 57ef942 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 90 | STATISTIC(NumArrayLenItCounts, | 
|  | 91 | "Number of trip counts computed with array length"); | 
|  | 92 | STATISTIC(NumTripCountsComputed, | 
|  | 93 | "Number of loops with predictable loop counts"); | 
|  | 94 | STATISTIC(NumTripCountsNotComputed, | 
|  | 95 | "Number of loops without predictable loop counts"); | 
|  | 96 | STATISTIC(NumBruteForceTripCountsComputed, | 
|  | 97 | "Number of loops with trip counts computed by force"); | 
|  | 98 |  | 
| Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 99 | static cl::opt<unsigned> | 
| Chris Lattner | 57ef942 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 100 | MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, | 
|  | 101 | cl::desc("Maximum number of iterations SCEV will " | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 102 | "symbolically execute a constant " | 
|  | 103 | "derived loop"), | 
| Chris Lattner | 57ef942 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 104 | cl::init(100)); | 
|  | 105 |  | 
| Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 106 | static RegisterPass<ScalarEvolution> | 
|  | 107 | R("scalar-evolution", "Scalar Evolution Analysis", false, true); | 
| Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 108 | char ScalarEvolution::ID = 0; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 109 |  | 
|  | 110 | //===----------------------------------------------------------------------===// | 
|  | 111 | //                           SCEV class definitions | 
|  | 112 | //===----------------------------------------------------------------------===// | 
|  | 113 |  | 
|  | 114 | //===----------------------------------------------------------------------===// | 
|  | 115 | // Implementation of the SCEV class. | 
|  | 116 | // | 
| Dan Gohman | 3423e72 | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 117 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 118 | SCEV::~SCEV() {} | 
| Dan Gohman | 3423e72 | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 119 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 120 | void SCEV::dump() const { | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 121 | print(errs()); | 
|  | 122 | errs() << '\n'; | 
|  | 123 | } | 
|  | 124 |  | 
| Dan Gohman | be928e3 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 125 | bool SCEV::isZero() const { | 
|  | 126 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) | 
|  | 127 | return SC->getValue()->isZero(); | 
|  | 128 | return false; | 
|  | 129 | } | 
|  | 130 |  | 
| Dan Gohman | ba7f6d8 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 131 | bool SCEV::isOne() const { | 
|  | 132 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) | 
|  | 133 | return SC->getValue()->isOne(); | 
|  | 134 | return false; | 
|  | 135 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 136 |  | 
| Dan Gohman | 18a96bb | 2009-06-24 00:30:26 +0000 | [diff] [blame] | 137 | bool SCEV::isAllOnesValue() const { | 
|  | 138 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) | 
|  | 139 | return SC->getValue()->isAllOnesValue(); | 
|  | 140 | return false; | 
|  | 141 | } | 
|  | 142 |  | 
| Owen Anderson | 04052ec | 2009-06-22 21:57:23 +0000 | [diff] [blame] | 143 | SCEVCouldNotCompute::SCEVCouldNotCompute() : | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 144 | SCEV(FoldingSetNodeID(), scCouldNotCompute) {} | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 145 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 146 | bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const { | 
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 147 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); | 
| Misha Brukman | 5ebc25c | 2004-04-05 19:00:46 +0000 | [diff] [blame] | 148 | return false; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
|  | 151 | const Type *SCEVCouldNotCompute::getType() const { | 
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 152 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); | 
| Misha Brukman | 5ebc25c | 2004-04-05 19:00:46 +0000 | [diff] [blame] | 153 | return 0; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 154 | } | 
|  | 155 |  | 
|  | 156 | bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const { | 
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 157 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 158 | return false; | 
|  | 159 | } | 
|  | 160 |  | 
| Dan Gohman | 0b89dff | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 161 | bool SCEVCouldNotCompute::hasOperand(const SCEV *) const { | 
|  | 162 | llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); | 
|  | 163 | return false; | 
| Chris Lattner | 7b0fbe7 | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 164 | } | 
|  | 165 |  | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 166 | void SCEVCouldNotCompute::print(raw_ostream &OS) const { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 167 | OS << "***COULDNOTCOMPUTE***"; | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | bool SCEVCouldNotCompute::classof(const SCEV *S) { | 
|  | 171 | return S->getSCEVType() == scCouldNotCompute; | 
|  | 172 | } | 
|  | 173 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 174 | const SCEV *ScalarEvolution::getConstant(ConstantInt *V) { | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 175 | FoldingSetNodeID ID; | 
|  | 176 | ID.AddInteger(scConstant); | 
|  | 177 | ID.AddPointer(V); | 
|  | 178 | void *IP = 0; | 
|  | 179 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; | 
|  | 180 | SCEV *S = SCEVAllocator.Allocate<SCEVConstant>(); | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 181 | new (S) SCEVConstant(ID, V); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 182 | UniqueSCEVs.InsertNode(S, IP); | 
|  | 183 | return S; | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 184 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 185 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 186 | const SCEV *ScalarEvolution::getConstant(const APInt& Val) { | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 187 | return getConstant(ConstantInt::get(getContext(), Val)); | 
| Dan Gohman | 0a76e7f | 2007-07-09 15:25:17 +0000 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 190 | const SCEV * | 
| Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 191 | ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) { | 
| Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 192 | return getConstant( | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 193 | ConstantInt::get(cast<IntegerType>(Ty), V, isSigned)); | 
| Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 194 | } | 
|  | 195 |  | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 196 | const Type *SCEVConstant::getType() const { return V->getType(); } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 197 |  | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 198 | void SCEVConstant::print(raw_ostream &OS) const { | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 199 | WriteAsOperand(OS, V, false); | 
|  | 200 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 201 |  | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 202 | SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeID &ID, | 
|  | 203 | unsigned SCEVTy, const SCEV *op, const Type *ty) | 
|  | 204 | : SCEV(ID, SCEVTy), Op(op), Ty(ty) {} | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 205 |  | 
| Dan Gohman | 4860db6 | 2009-04-21 01:25:57 +0000 | [diff] [blame] | 206 | bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { | 
|  | 207 | return Op->dominates(BB, DT); | 
|  | 208 | } | 
|  | 209 |  | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 210 | SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeID &ID, | 
|  | 211 | const SCEV *op, const Type *ty) | 
|  | 212 | : SCEVCastExpr(ID, scTruncate, op, ty) { | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 213 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && | 
|  | 214 | (Ty->isInteger() || isa<PointerType>(Ty)) && | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 215 | "Cannot truncate non-integer value!"); | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 216 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 217 |  | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 218 | void SCEVTruncateExpr::print(raw_ostream &OS) const { | 
| Dan Gohman | d9b11b2 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 219 | OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 220 | } | 
|  | 221 |  | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 222 | SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeID &ID, | 
|  | 223 | const SCEV *op, const Type *ty) | 
|  | 224 | : SCEVCastExpr(ID, scZeroExtend, op, ty) { | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 225 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && | 
|  | 226 | (Ty->isInteger() || isa<PointerType>(Ty)) && | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 227 | "Cannot zero extend non-integer value!"); | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 228 | } | 
|  | 229 |  | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 230 | void SCEVZeroExtendExpr::print(raw_ostream &OS) const { | 
| Dan Gohman | d9b11b2 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 231 | OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 232 | } | 
|  | 233 |  | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 234 | SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeID &ID, | 
|  | 235 | const SCEV *op, const Type *ty) | 
|  | 236 | : SCEVCastExpr(ID, scSignExtend, op, ty) { | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 237 | assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && | 
|  | 238 | (Ty->isInteger() || isa<PointerType>(Ty)) && | 
| Dan Gohman | cb9e09a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 239 | "Cannot sign extend non-integer value!"); | 
| Dan Gohman | cb9e09a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 240 | } | 
|  | 241 |  | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 242 | void SCEVSignExtendExpr::print(raw_ostream &OS) const { | 
| Dan Gohman | d9b11b2 | 2009-04-29 20:27:52 +0000 | [diff] [blame] | 243 | OS << "(sext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; | 
| Dan Gohman | cb9e09a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 244 | } | 
|  | 245 |  | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 246 | void SCEVCommutativeExpr::print(raw_ostream &OS) const { | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 247 | assert(Operands.size() > 1 && "This plus expr shouldn't exist!"); | 
|  | 248 | const char *OpStr = getOperationStr(); | 
|  | 249 | OS << "(" << *Operands[0]; | 
|  | 250 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) | 
|  | 251 | OS << OpStr << *Operands[i]; | 
|  | 252 | OS << ")"; | 
|  | 253 | } | 
|  | 254 |  | 
| Dan Gohman | c6bb55b | 2009-05-07 14:00:19 +0000 | [diff] [blame] | 255 | bool SCEVNAryExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { | 
| Evan Cheng | 161861d | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 256 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { | 
|  | 257 | if (!getOperand(i)->dominates(BB, DT)) | 
|  | 258 | return false; | 
|  | 259 | } | 
|  | 260 | return true; | 
|  | 261 | } | 
|  | 262 |  | 
| Evan Cheng | 161861d | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 263 | bool SCEVUDivExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { | 
|  | 264 | return LHS->dominates(BB, DT) && RHS->dominates(BB, DT); | 
|  | 265 | } | 
|  | 266 |  | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 267 | void SCEVUDivExpr::print(raw_ostream &OS) const { | 
| Wojciech Matyjewicz | d2d9764 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 268 | OS << "(" << *LHS << " /u " << *RHS << ")"; | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 269 | } | 
|  | 270 |  | 
| Wojciech Matyjewicz | d2d9764 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 271 | const Type *SCEVUDivExpr::getType() const { | 
| Dan Gohman | 2a6606c | 2009-05-26 17:44:05 +0000 | [diff] [blame] | 272 | // In most cases the types of LHS and RHS will be the same, but in some | 
|  | 273 | // crazy cases one or the other may be a pointer. ScalarEvolution doesn't | 
|  | 274 | // depend on the type for correctness, but handling types carefully can | 
|  | 275 | // avoid extra casts in the SCEVExpander. The LHS is more likely to be | 
|  | 276 | // a pointer type than the RHS, so use the RHS' type here. | 
|  | 277 | return RHS->getType(); | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 278 | } | 
|  | 279 |  | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 280 | bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const { | 
| Dan Gohman | 9cbf850 | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 281 | // Add recurrences are never invariant in the function-body (null loop). | 
| Dan Gohman | 06a4e27 | 2009-06-26 22:17:21 +0000 | [diff] [blame] | 282 | if (!QueryLoop) | 
|  | 283 | return false; | 
|  | 284 |  | 
|  | 285 | // This recurrence is variant w.r.t. QueryLoop if QueryLoop contains L. | 
|  | 286 | if (QueryLoop->contains(L->getHeader())) | 
|  | 287 | return false; | 
|  | 288 |  | 
|  | 289 | // This recurrence is variant w.r.t. QueryLoop if any of its operands | 
|  | 290 | // are variant. | 
|  | 291 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) | 
|  | 292 | if (!getOperand(i)->isLoopInvariant(QueryLoop)) | 
|  | 293 | return false; | 
|  | 294 |  | 
|  | 295 | // Otherwise it's loop-invariant. | 
|  | 296 | return true; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 297 | } | 
|  | 298 |  | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 299 | void SCEVAddRecExpr::print(raw_ostream &OS) const { | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 300 | OS << "{" << *Operands[0]; | 
|  | 301 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) | 
|  | 302 | OS << ",+," << *Operands[i]; | 
|  | 303 | OS << "}<" << L->getHeader()->getName() + ">"; | 
|  | 304 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 305 |  | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 306 | void SCEVFieldOffsetExpr::print(raw_ostream &OS) const { | 
|  | 307 | // LLVM struct fields don't have names, so just print the field number. | 
|  | 308 | OS << "offsetof(" << *STy << ", " << FieldNo << ")"; | 
|  | 309 | } | 
|  | 310 |  | 
|  | 311 | void SCEVAllocSizeExpr::print(raw_ostream &OS) const { | 
|  | 312 | OS << "sizeof(" << *AllocTy << ")"; | 
|  | 313 | } | 
|  | 314 |  | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 315 | bool SCEVUnknown::isLoopInvariant(const Loop *L) const { | 
|  | 316 | // All non-instruction values are loop invariant.  All instructions are loop | 
|  | 317 | // invariant if they are not contained in the specified loop. | 
| Dan Gohman | 9cbf850 | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 318 | // Instructions are never considered invariant in the function body | 
|  | 319 | // (null loop) because they are defined within the "loop". | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 320 | if (Instruction *I = dyn_cast<Instruction>(V)) | 
| Dan Gohman | 9cbf850 | 2009-05-20 01:01:24 +0000 | [diff] [blame] | 321 | return L && !L->contains(I->getParent()); | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 322 | return true; | 
|  | 323 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 324 |  | 
| Evan Cheng | 161861d | 2009-02-17 00:13:06 +0000 | [diff] [blame] | 325 | bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const { | 
|  | 326 | if (Instruction *I = dyn_cast<Instruction>(getValue())) | 
|  | 327 | return DT->dominates(I->getParent(), BB); | 
|  | 328 | return true; | 
|  | 329 | } | 
|  | 330 |  | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 331 | const Type *SCEVUnknown::getType() const { | 
|  | 332 | return V->getType(); | 
|  | 333 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 334 |  | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 335 | void SCEVUnknown::print(raw_ostream &OS) const { | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 336 | WriteAsOperand(OS, V, false); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 337 | } | 
|  | 338 |  | 
| Chris Lattner | eb3e840 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 339 | //===----------------------------------------------------------------------===// | 
|  | 340 | //                               SCEV Utilities | 
|  | 341 | //===----------------------------------------------------------------------===// | 
|  | 342 |  | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 343 | static bool CompareTypes(const Type *A, const Type *B) { | 
|  | 344 | if (A->getTypeID() != B->getTypeID()) | 
|  | 345 | return A->getTypeID() < B->getTypeID(); | 
|  | 346 | if (const IntegerType *AI = dyn_cast<IntegerType>(A)) { | 
|  | 347 | const IntegerType *BI = cast<IntegerType>(B); | 
|  | 348 | return AI->getBitWidth() < BI->getBitWidth(); | 
|  | 349 | } | 
|  | 350 | if (const PointerType *AI = dyn_cast<PointerType>(A)) { | 
|  | 351 | const PointerType *BI = cast<PointerType>(B); | 
|  | 352 | return CompareTypes(AI->getElementType(), BI->getElementType()); | 
|  | 353 | } | 
|  | 354 | if (const ArrayType *AI = dyn_cast<ArrayType>(A)) { | 
|  | 355 | const ArrayType *BI = cast<ArrayType>(B); | 
|  | 356 | if (AI->getNumElements() != BI->getNumElements()) | 
|  | 357 | return AI->getNumElements() < BI->getNumElements(); | 
|  | 358 | return CompareTypes(AI->getElementType(), BI->getElementType()); | 
|  | 359 | } | 
|  | 360 | if (const VectorType *AI = dyn_cast<VectorType>(A)) { | 
|  | 361 | const VectorType *BI = cast<VectorType>(B); | 
|  | 362 | if (AI->getNumElements() != BI->getNumElements()) | 
|  | 363 | return AI->getNumElements() < BI->getNumElements(); | 
|  | 364 | return CompareTypes(AI->getElementType(), BI->getElementType()); | 
|  | 365 | } | 
|  | 366 | if (const StructType *AI = dyn_cast<StructType>(A)) { | 
|  | 367 | const StructType *BI = cast<StructType>(B); | 
|  | 368 | if (AI->getNumElements() != BI->getNumElements()) | 
|  | 369 | return AI->getNumElements() < BI->getNumElements(); | 
|  | 370 | for (unsigned i = 0, e = AI->getNumElements(); i != e; ++i) | 
|  | 371 | if (CompareTypes(AI->getElementType(i), BI->getElementType(i)) || | 
|  | 372 | CompareTypes(BI->getElementType(i), AI->getElementType(i))) | 
|  | 373 | return CompareTypes(AI->getElementType(i), BI->getElementType(i)); | 
|  | 374 | } | 
|  | 375 | return false; | 
|  | 376 | } | 
|  | 377 |  | 
| Chris Lattner | eb3e840 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 378 | namespace { | 
|  | 379 | /// SCEVComplexityCompare - Return true if the complexity of the LHS is less | 
|  | 380 | /// than the complexity of the RHS.  This comparator is used to canonicalize | 
|  | 381 | /// expressions. | 
| Dan Gohman | 9ba542c | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 382 | class VISIBILITY_HIDDEN SCEVComplexityCompare { | 
|  | 383 | LoopInfo *LI; | 
|  | 384 | public: | 
|  | 385 | explicit SCEVComplexityCompare(LoopInfo *li) : LI(li) {} | 
|  | 386 |  | 
| Dan Gohman | 5e6ce7b | 2008-04-14 18:23:56 +0000 | [diff] [blame] | 387 | bool operator()(const SCEV *LHS, const SCEV *RHS) const { | 
| Dan Gohman | cc2f1eb | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 388 | // Fast-path: SCEVs are uniqued so we can do a quick equality check. | 
|  | 389 | if (LHS == RHS) | 
|  | 390 | return false; | 
|  | 391 |  | 
| Dan Gohman | 9ba542c | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 392 | // Primarily, sort the SCEVs by their getSCEVType(). | 
|  | 393 | if (LHS->getSCEVType() != RHS->getSCEVType()) | 
|  | 394 | return LHS->getSCEVType() < RHS->getSCEVType(); | 
|  | 395 |  | 
|  | 396 | // Aside from the getSCEVType() ordering, the particular ordering | 
|  | 397 | // isn't very important except that it's beneficial to be consistent, | 
|  | 398 | // so that (a + b) and (b + a) don't end up as different expressions. | 
|  | 399 |  | 
|  | 400 | // Sort SCEVUnknown values with some loose heuristics. TODO: This is | 
|  | 401 | // not as complete as it could be. | 
|  | 402 | if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) { | 
|  | 403 | const SCEVUnknown *RU = cast<SCEVUnknown>(RHS); | 
|  | 404 |  | 
| Dan Gohman | 2649491 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 405 | // Order pointer values after integer values. This helps SCEVExpander | 
|  | 406 | // form GEPs. | 
|  | 407 | if (isa<PointerType>(LU->getType()) && !isa<PointerType>(RU->getType())) | 
|  | 408 | return false; | 
|  | 409 | if (isa<PointerType>(RU->getType()) && !isa<PointerType>(LU->getType())) | 
|  | 410 | return true; | 
|  | 411 |  | 
| Dan Gohman | 9ba542c | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 412 | // Compare getValueID values. | 
|  | 413 | if (LU->getValue()->getValueID() != RU->getValue()->getValueID()) | 
|  | 414 | return LU->getValue()->getValueID() < RU->getValue()->getValueID(); | 
|  | 415 |  | 
|  | 416 | // Sort arguments by their position. | 
|  | 417 | if (const Argument *LA = dyn_cast<Argument>(LU->getValue())) { | 
|  | 418 | const Argument *RA = cast<Argument>(RU->getValue()); | 
|  | 419 | return LA->getArgNo() < RA->getArgNo(); | 
|  | 420 | } | 
|  | 421 |  | 
|  | 422 | // For instructions, compare their loop depth, and their opcode. | 
|  | 423 | // This is pretty loose. | 
|  | 424 | if (Instruction *LV = dyn_cast<Instruction>(LU->getValue())) { | 
|  | 425 | Instruction *RV = cast<Instruction>(RU->getValue()); | 
|  | 426 |  | 
|  | 427 | // Compare loop depths. | 
|  | 428 | if (LI->getLoopDepth(LV->getParent()) != | 
|  | 429 | LI->getLoopDepth(RV->getParent())) | 
|  | 430 | return LI->getLoopDepth(LV->getParent()) < | 
|  | 431 | LI->getLoopDepth(RV->getParent()); | 
|  | 432 |  | 
|  | 433 | // Compare opcodes. | 
|  | 434 | if (LV->getOpcode() != RV->getOpcode()) | 
|  | 435 | return LV->getOpcode() < RV->getOpcode(); | 
|  | 436 |  | 
|  | 437 | // Compare the number of operands. | 
|  | 438 | if (LV->getNumOperands() != RV->getNumOperands()) | 
|  | 439 | return LV->getNumOperands() < RV->getNumOperands(); | 
|  | 440 | } | 
|  | 441 |  | 
|  | 442 | return false; | 
|  | 443 | } | 
|  | 444 |  | 
| Dan Gohman | 862b7d9 | 2009-06-14 22:51:25 +0000 | [diff] [blame] | 445 | // Compare constant values. | 
|  | 446 | if (const SCEVConstant *LC = dyn_cast<SCEVConstant>(LHS)) { | 
|  | 447 | const SCEVConstant *RC = cast<SCEVConstant>(RHS); | 
| Nick Lewycky | 3292908 | 2009-07-04 17:24:52 +0000 | [diff] [blame] | 448 | if (LC->getValue()->getBitWidth() != RC->getValue()->getBitWidth()) | 
|  | 449 | return LC->getValue()->getBitWidth() < RC->getValue()->getBitWidth(); | 
| Dan Gohman | 862b7d9 | 2009-06-14 22:51:25 +0000 | [diff] [blame] | 450 | return LC->getValue()->getValue().ult(RC->getValue()->getValue()); | 
|  | 451 | } | 
|  | 452 |  | 
|  | 453 | // Compare addrec loop depths. | 
|  | 454 | if (const SCEVAddRecExpr *LA = dyn_cast<SCEVAddRecExpr>(LHS)) { | 
|  | 455 | const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS); | 
|  | 456 | if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth()) | 
|  | 457 | return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth(); | 
|  | 458 | } | 
| Dan Gohman | 9ba542c | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 459 |  | 
|  | 460 | // Lexicographically compare n-ary expressions. | 
|  | 461 | if (const SCEVNAryExpr *LC = dyn_cast<SCEVNAryExpr>(LHS)) { | 
|  | 462 | const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS); | 
|  | 463 | for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) { | 
|  | 464 | if (i >= RC->getNumOperands()) | 
|  | 465 | return false; | 
|  | 466 | if (operator()(LC->getOperand(i), RC->getOperand(i))) | 
|  | 467 | return true; | 
|  | 468 | if (operator()(RC->getOperand(i), LC->getOperand(i))) | 
|  | 469 | return false; | 
|  | 470 | } | 
|  | 471 | return LC->getNumOperands() < RC->getNumOperands(); | 
|  | 472 | } | 
|  | 473 |  | 
| Dan Gohman | 64f756b | 2009-05-07 19:23:21 +0000 | [diff] [blame] | 474 | // Lexicographically compare udiv expressions. | 
|  | 475 | if (const SCEVUDivExpr *LC = dyn_cast<SCEVUDivExpr>(LHS)) { | 
|  | 476 | const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS); | 
|  | 477 | if (operator()(LC->getLHS(), RC->getLHS())) | 
|  | 478 | return true; | 
|  | 479 | if (operator()(RC->getLHS(), LC->getLHS())) | 
|  | 480 | return false; | 
|  | 481 | if (operator()(LC->getRHS(), RC->getRHS())) | 
|  | 482 | return true; | 
|  | 483 | if (operator()(RC->getRHS(), LC->getRHS())) | 
|  | 484 | return false; | 
|  | 485 | return false; | 
|  | 486 | } | 
|  | 487 |  | 
| Dan Gohman | 9ba542c | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 488 | // Compare cast expressions by operand. | 
|  | 489 | if (const SCEVCastExpr *LC = dyn_cast<SCEVCastExpr>(LHS)) { | 
|  | 490 | const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS); | 
|  | 491 | return operator()(LC->getOperand(), RC->getOperand()); | 
|  | 492 | } | 
|  | 493 |  | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 494 | // Compare offsetof expressions. | 
|  | 495 | if (const SCEVFieldOffsetExpr *LA = dyn_cast<SCEVFieldOffsetExpr>(LHS)) { | 
|  | 496 | const SCEVFieldOffsetExpr *RA = cast<SCEVFieldOffsetExpr>(RHS); | 
|  | 497 | if (CompareTypes(LA->getStructType(), RA->getStructType()) || | 
|  | 498 | CompareTypes(RA->getStructType(), LA->getStructType())) | 
|  | 499 | return CompareTypes(LA->getStructType(), RA->getStructType()); | 
|  | 500 | return LA->getFieldNo() < RA->getFieldNo(); | 
|  | 501 | } | 
|  | 502 |  | 
|  | 503 | // Compare sizeof expressions by the allocation type. | 
|  | 504 | if (const SCEVAllocSizeExpr *LA = dyn_cast<SCEVAllocSizeExpr>(LHS)) { | 
|  | 505 | const SCEVAllocSizeExpr *RA = cast<SCEVAllocSizeExpr>(RHS); | 
|  | 506 | return CompareTypes(LA->getAllocType(), RA->getAllocType()); | 
|  | 507 | } | 
|  | 508 |  | 
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 509 | llvm_unreachable("Unknown SCEV kind!"); | 
| Dan Gohman | 9ba542c | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 510 | return false; | 
| Chris Lattner | eb3e840 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 511 | } | 
|  | 512 | }; | 
|  | 513 | } | 
|  | 514 |  | 
|  | 515 | /// GroupByComplexity - Given a list of SCEV objects, order them by their | 
|  | 516 | /// complexity, and group objects of the same complexity together by value. | 
|  | 517 | /// When this routine is finished, we know that any duplicates in the vector are | 
|  | 518 | /// consecutive and that complexity is monotonically increasing. | 
|  | 519 | /// | 
|  | 520 | /// Note that we go take special precautions to ensure that we get determinstic | 
|  | 521 | /// results from this routine.  In other words, we don't want the results of | 
|  | 522 | /// this to depend on where the addresses of various SCEV objects happened to | 
|  | 523 | /// land in memory. | 
|  | 524 | /// | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 525 | static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops, | 
| Dan Gohman | 9ba542c | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 526 | LoopInfo *LI) { | 
| Chris Lattner | eb3e840 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 527 | if (Ops.size() < 2) return;  // Noop | 
|  | 528 | if (Ops.size() == 2) { | 
|  | 529 | // This is the common case, which also happens to be trivially simple. | 
|  | 530 | // Special case it. | 
| Dan Gohman | 9ba542c | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 531 | if (SCEVComplexityCompare(LI)(Ops[1], Ops[0])) | 
| Chris Lattner | eb3e840 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 532 | std::swap(Ops[0], Ops[1]); | 
|  | 533 | return; | 
|  | 534 | } | 
|  | 535 |  | 
|  | 536 | // Do the rough sort by complexity. | 
| Dan Gohman | 9ba542c | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 537 | std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI)); | 
| Chris Lattner | eb3e840 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 538 |  | 
|  | 539 | // Now that we are sorted by complexity, group elements of the same | 
|  | 540 | // complexity.  Note that this is, at worst, N^2, but the vector is likely to | 
|  | 541 | // be extremely short in practice.  Note that we take this approach because we | 
|  | 542 | // do not want to depend on the addresses of the objects we are grouping. | 
| Chris Lattner | 6bfca8f | 2004-06-20 17:01:44 +0000 | [diff] [blame] | 543 | for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 544 | const SCEV *S = Ops[i]; | 
| Chris Lattner | eb3e840 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 545 | unsigned Complexity = S->getSCEVType(); | 
|  | 546 |  | 
|  | 547 | // If there are any objects of the same complexity and same value as this | 
|  | 548 | // one, group them. | 
|  | 549 | for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { | 
|  | 550 | if (Ops[j] == S) { // Found a duplicate. | 
|  | 551 | // Move it to immediately after i'th element. | 
|  | 552 | std::swap(Ops[i+1], Ops[j]); | 
|  | 553 | ++i;   // no need to rescan it. | 
| Chris Lattner | baaed7e | 2004-06-20 20:32:16 +0000 | [diff] [blame] | 554 | if (i == e-2) return;  // Done! | 
| Chris Lattner | eb3e840 | 2004-06-20 06:23:15 +0000 | [diff] [blame] | 555 | } | 
|  | 556 | } | 
|  | 557 | } | 
|  | 558 | } | 
|  | 559 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 560 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 561 |  | 
|  | 562 | //===----------------------------------------------------------------------===// | 
|  | 563 | //                      Simple SCEV method implementations | 
|  | 564 | //===----------------------------------------------------------------------===// | 
|  | 565 |  | 
| Eli Friedman | 61f6762 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 566 | /// BinomialCoefficient - Compute BC(It, K).  The result has width W. | 
| Dan Gohman | 4d5435d | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 567 | /// Assume, K > 0. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 568 | static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K, | 
| Dan Gohman | 32291b1 | 2009-07-21 00:38:55 +0000 | [diff] [blame] | 569 | ScalarEvolution &SE, | 
|  | 570 | const Type* ResultTy) { | 
| Eli Friedman | 61f6762 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 571 | // Handle the simplest case efficiently. | 
|  | 572 | if (K == 1) | 
|  | 573 | return SE.getTruncateOrZeroExtend(It, ResultTy); | 
|  | 574 |  | 
| Wojciech Matyjewicz | d2d9764 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 575 | // We are using the following formula for BC(It, K): | 
|  | 576 | // | 
|  | 577 | //   BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! | 
|  | 578 | // | 
| Eli Friedman | 61f6762 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 579 | // Suppose, W is the bitwidth of the return value.  We must be prepared for | 
|  | 580 | // overflow.  Hence, we must assure that the result of our computation is | 
|  | 581 | // equal to the accurate one modulo 2^W.  Unfortunately, division isn't | 
|  | 582 | // safe in modular arithmetic. | 
| Wojciech Matyjewicz | d2d9764 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 583 | // | 
| Eli Friedman | 61f6762 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 584 | // However, this code doesn't use exactly that formula; the formula it uses | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 585 | // is something like the following, where T is the number of factors of 2 in | 
| Eli Friedman | 61f6762 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 586 | // K! (i.e. trailing zeros in the binary representation of K!), and ^ is | 
|  | 587 | // exponentiation: | 
| Wojciech Matyjewicz | d2d9764 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 588 | // | 
| Eli Friedman | 61f6762 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 589 | //   BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T) | 
| Wojciech Matyjewicz | d2d9764 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 590 | // | 
| Eli Friedman | 61f6762 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 591 | // This formula is trivially equivalent to the previous formula.  However, | 
|  | 592 | // this formula can be implemented much more efficiently.  The trick is that | 
|  | 593 | // K! / 2^T is odd, and exact division by an odd number *is* safe in modular | 
|  | 594 | // arithmetic.  To do exact division in modular arithmetic, all we have | 
|  | 595 | // to do is multiply by the inverse.  Therefore, this step can be done at | 
|  | 596 | // width W. | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 597 | // | 
| Eli Friedman | 61f6762 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 598 | // The next issue is how to safely do the division by 2^T.  The way this | 
|  | 599 | // is done is by doing the multiplication step at a width of at least W + T | 
|  | 600 | // bits.  This way, the bottom W+T bits of the product are accurate. Then, | 
|  | 601 | // when we perform the division by 2^T (which is equivalent to a right shift | 
|  | 602 | // by T), the bottom W bits are accurate.  Extra bits are okay; they'll get | 
|  | 603 | // truncated out after the division by 2^T. | 
|  | 604 | // | 
|  | 605 | // In comparison to just directly using the first formula, this technique | 
|  | 606 | // is much more efficient; using the first formula requires W * K bits, | 
|  | 607 | // but this formula less than W + K bits. Also, the first formula requires | 
|  | 608 | // a division step, whereas this formula only requires multiplies and shifts. | 
|  | 609 | // | 
|  | 610 | // It doesn't matter whether the subtraction step is done in the calculation | 
|  | 611 | // width or the input iteration count's width; if the subtraction overflows, | 
|  | 612 | // the result must be zero anyway.  We prefer here to do it in the width of | 
|  | 613 | // the induction variable because it helps a lot for certain cases; CodeGen | 
|  | 614 | // isn't smart enough to ignore the overflow, which leads to much less | 
|  | 615 | // efficient code if the width of the subtraction is wider than the native | 
|  | 616 | // register width. | 
|  | 617 | // | 
|  | 618 | // (It's possible to not widen at all by pulling out factors of 2 before | 
|  | 619 | // the multiplication; for example, K=2 can be calculated as | 
|  | 620 | // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires | 
|  | 621 | // extra arithmetic, so it's not an obvious win, and it gets | 
|  | 622 | // much more complicated for K > 3.) | 
| Wojciech Matyjewicz | d2d9764 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 623 |  | 
| Eli Friedman | 61f6762 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 624 | // Protection from insane SCEVs; this bound is conservative, | 
|  | 625 | // but it probably doesn't matter. | 
|  | 626 | if (K > 1000) | 
| Dan Gohman | 31efa30 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 627 | return SE.getCouldNotCompute(); | 
| Wojciech Matyjewicz | d2d9764 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 628 |  | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 629 | unsigned W = SE.getTypeSizeInBits(ResultTy); | 
| Wojciech Matyjewicz | d2d9764 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 630 |  | 
| Eli Friedman | 61f6762 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 631 | // Calculate K! / 2^T and T; we divide out the factors of two before | 
|  | 632 | // multiplying for calculating K! / 2^T to avoid overflow. | 
|  | 633 | // Other overflow doesn't matter because we only care about the bottom | 
|  | 634 | // W bits of the result. | 
|  | 635 | APInt OddFactorial(W, 1); | 
|  | 636 | unsigned T = 1; | 
|  | 637 | for (unsigned i = 3; i <= K; ++i) { | 
|  | 638 | APInt Mult(W, i); | 
|  | 639 | unsigned TwoFactors = Mult.countTrailingZeros(); | 
|  | 640 | T += TwoFactors; | 
|  | 641 | Mult = Mult.lshr(TwoFactors); | 
|  | 642 | OddFactorial *= Mult; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 643 | } | 
| Nick Lewycky | ed169d5 | 2008-06-13 04:38:55 +0000 | [diff] [blame] | 644 |  | 
| Eli Friedman | 61f6762 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 645 | // We need at least W + T bits for the multiplication step | 
| Nick Lewycky | 21add8f | 2009-01-25 08:16:27 +0000 | [diff] [blame] | 646 | unsigned CalculationBits = W + T; | 
| Eli Friedman | 61f6762 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 647 |  | 
|  | 648 | // Calcuate 2^T, at width T+W. | 
|  | 649 | APInt DivFactor = APInt(CalculationBits, 1).shl(T); | 
|  | 650 |  | 
|  | 651 | // Calculate the multiplicative inverse of K! / 2^T; | 
|  | 652 | // this multiplication factor will perform the exact division by | 
|  | 653 | // K! / 2^T. | 
|  | 654 | APInt Mod = APInt::getSignedMinValue(W+1); | 
|  | 655 | APInt MultiplyFactor = OddFactorial.zext(W+1); | 
|  | 656 | MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); | 
|  | 657 | MultiplyFactor = MultiplyFactor.trunc(W); | 
|  | 658 |  | 
|  | 659 | // Calculate the product, at width T+W | 
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 660 | const IntegerType *CalculationTy = IntegerType::get(SE.getContext(), | 
|  | 661 | CalculationBits); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 662 | const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); | 
| Eli Friedman | 61f6762 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 663 | for (unsigned i = 1; i != K; ++i) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 664 | const SCEV *S = SE.getMinusSCEV(It, SE.getIntegerSCEV(i, It->getType())); | 
| Eli Friedman | 61f6762 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 665 | Dividend = SE.getMulExpr(Dividend, | 
|  | 666 | SE.getTruncateOrZeroExtend(S, CalculationTy)); | 
|  | 667 | } | 
|  | 668 |  | 
|  | 669 | // Divide by 2^T | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 670 | const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); | 
| Eli Friedman | 61f6762 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 671 |  | 
|  | 672 | // Truncate the result, and divide by K! / 2^T. | 
|  | 673 |  | 
|  | 674 | return SE.getMulExpr(SE.getConstant(MultiplyFactor), | 
|  | 675 | SE.getTruncateOrZeroExtend(DivResult, ResultTy)); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 676 | } | 
|  | 677 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 678 | /// evaluateAtIteration - Return the value of this chain of recurrences at | 
|  | 679 | /// the specified iteration number.  We can evaluate this recurrence by | 
|  | 680 | /// multiplying each element in the chain by the binomial coefficient | 
|  | 681 | /// corresponding to it.  In other words, we can evaluate {A,+,B,+,C,+,D} as: | 
|  | 682 | /// | 
| Wojciech Matyjewicz | d2d9764 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 683 | ///   A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3) | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 684 | /// | 
| Wojciech Matyjewicz | d2d9764 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 685 | /// where BC(It, k) stands for binomial coefficient. | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 686 | /// | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 687 | const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It, | 
| Dan Gohman | 32291b1 | 2009-07-21 00:38:55 +0000 | [diff] [blame] | 688 | ScalarEvolution &SE) const { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 689 | const SCEV *Result = getStart(); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 690 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { | 
| Wojciech Matyjewicz | d2d9764 | 2008-02-11 11:03:14 +0000 | [diff] [blame] | 691 | // The computation is correct in the face of overflow provided that the | 
|  | 692 | // multiplication is performed _after_ the evaluation of the binomial | 
|  | 693 | // coefficient. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 694 | const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType()); | 
| Nick Lewycky | 707663e | 2008-10-13 03:58:02 +0000 | [diff] [blame] | 695 | if (isa<SCEVCouldNotCompute>(Coeff)) | 
|  | 696 | return Coeff; | 
|  | 697 |  | 
|  | 698 | Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff)); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 699 | } | 
|  | 700 | return Result; | 
|  | 701 | } | 
|  | 702 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 703 | //===----------------------------------------------------------------------===// | 
|  | 704 | //                    SCEV Expression folder implementations | 
|  | 705 | //===----------------------------------------------------------------------===// | 
|  | 706 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 707 | const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, | 
| Dan Gohman | fc76994 | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 708 | const Type *Ty) { | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 709 | assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && | 
| Dan Gohman | 413e91f | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 710 | "This is not a truncating conversion!"); | 
| Dan Gohman | 194e42c | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 711 | assert(isSCEVable(Ty) && | 
|  | 712 | "This is not a conversion to a SCEVable type!"); | 
|  | 713 | Ty = getEffectiveSCEVType(Ty); | 
| Dan Gohman | 413e91f | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 714 |  | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 715 | FoldingSetNodeID ID; | 
|  | 716 | ID.AddInteger(scTruncate); | 
|  | 717 | ID.AddPointer(Op); | 
|  | 718 | ID.AddPointer(Ty); | 
|  | 719 | void *IP = 0; | 
|  | 720 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; | 
|  | 721 |  | 
| Dan Gohman | 3423e72 | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 722 | // Fold if the operand is constant. | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 723 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) | 
| Dan Gohman | 8d7576e | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 724 | return getConstant( | 
|  | 725 | cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty))); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 726 |  | 
| Dan Gohman | 79af854 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 727 | // trunc(trunc(x)) --> trunc(x) | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 728 | if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) | 
| Dan Gohman | 79af854 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 729 | return getTruncateExpr(ST->getOperand(), Ty); | 
|  | 730 |  | 
| Nick Lewycky | b4d9f7a | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 731 | // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 732 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) | 
| Nick Lewycky | b4d9f7a | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 733 | return getTruncateOrSignExtend(SS->getOperand(), Ty); | 
|  | 734 |  | 
|  | 735 | // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 736 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) | 
| Nick Lewycky | b4d9f7a | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 737 | return getTruncateOrZeroExtend(SZ->getOperand(), Ty); | 
|  | 738 |  | 
| Dan Gohman | 5a728c9 | 2009-06-18 16:24:47 +0000 | [diff] [blame] | 739 | // If the input value is a chrec scev, truncate the chrec's operands. | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 740 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 741 | SmallVector<const SCEV *, 4> Operands; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 742 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) | 
| Dan Gohman | 2e55cc5 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 743 | Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty)); | 
|  | 744 | return getAddRecExpr(Operands, AddRec->getLoop()); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 745 | } | 
|  | 746 |  | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 747 | // The cast wasn't folded; create an explicit cast node. | 
|  | 748 | // Recompute the insert position, as it may have been invalidated. | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 749 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; | 
|  | 750 | SCEV *S = SCEVAllocator.Allocate<SCEVTruncateExpr>(); | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 751 | new (S) SCEVTruncateExpr(ID, Op, Ty); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 752 | UniqueSCEVs.InsertNode(S, IP); | 
|  | 753 | return S; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 754 | } | 
|  | 755 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 756 | const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op, | 
| Dan Gohman | fc76994 | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 757 | const Type *Ty) { | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 758 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && | 
| Dan Gohman | c1c2ba7 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 759 | "This is not an extending conversion!"); | 
| Dan Gohman | 194e42c | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 760 | assert(isSCEVable(Ty) && | 
|  | 761 | "This is not a conversion to a SCEVable type!"); | 
|  | 762 | Ty = getEffectiveSCEVType(Ty); | 
| Dan Gohman | c1c2ba7 | 2009-04-16 19:25:55 +0000 | [diff] [blame] | 763 |  | 
| Dan Gohman | 3423e72 | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 764 | // Fold if the operand is constant. | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 765 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 766 | const Type *IntTy = getEffectiveSCEVType(Ty); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 767 | Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy); | 
|  | 768 | if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); | 
| Dan Gohman | 8d7576e | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 769 | return getConstant(cast<ConstantInt>(C)); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 770 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 771 |  | 
| Dan Gohman | 79af854 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 772 | // zext(zext(x)) --> zext(x) | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 773 | if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) | 
| Dan Gohman | 79af854 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 774 | return getZeroExtendExpr(SZ->getOperand(), Ty); | 
|  | 775 |  | 
| Dan Gohman | 74a0ba1 | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 776 | // Before doing any expensive analysis, check to see if we've already | 
|  | 777 | // computed a SCEV for this Op and Ty. | 
|  | 778 | FoldingSetNodeID ID; | 
|  | 779 | ID.AddInteger(scZeroExtend); | 
|  | 780 | ID.AddPointer(Op); | 
|  | 781 | ID.AddPointer(Ty); | 
|  | 782 | void *IP = 0; | 
|  | 783 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; | 
|  | 784 |  | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 785 | // If the input value is a chrec scev, and we can prove that the value | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 786 | // did not overflow the old, smaller, value, we can zero extend all of the | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 787 | // operands (often constants).  This allows analysis of something like | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 788 | // this:  for (unsigned char X = 0; X < 100; ++X) { int Y = X; } | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 789 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 790 | if (AR->isAffine()) { | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 791 | const SCEV *Start = AR->getStart(); | 
|  | 792 | const SCEV *Step = AR->getStepRecurrence(*this); | 
|  | 793 | unsigned BitWidth = getTypeSizeInBits(AR->getType()); | 
|  | 794 | const Loop *L = AR->getLoop(); | 
|  | 795 |  | 
| Dan Gohman | 62ef6a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 796 | // If we have special knowledge that this addrec won't overflow, | 
|  | 797 | // we don't need to do any further analysis. | 
| Dan Gohman | 16f5415 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 798 | if (AR->hasNoUnsignedWrap()) | 
| Dan Gohman | 62ef6a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 799 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), | 
|  | 800 | getZeroExtendExpr(Step, Ty), | 
|  | 801 | L); | 
|  | 802 |  | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 803 | // Check whether the backedge-taken count is SCEVCouldNotCompute. | 
|  | 804 | // Note that this serves two purposes: It filters out loops that are | 
|  | 805 | // simply not analyzable, and it covers the case where this code is | 
|  | 806 | // being called from within backedge-taken count analysis, such that | 
|  | 807 | // attempting to ask for the backedge-taken count would likely result | 
|  | 808 | // in infinite recursion. In the later case, the analysis code will | 
|  | 809 | // cope with a conservative value, and it will take care to purge | 
|  | 810 | // that value once it has finished. | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 811 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 812 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { | 
| Dan Gohman | 95c5b0e | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 813 | // Manually compute the final value for AR, checking for | 
| Dan Gohman | 494dac3 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 814 | // overflow. | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 815 |  | 
|  | 816 | // Check whether the backedge-taken count can be losslessly casted to | 
|  | 817 | // the addrec's type. The count is always unsigned. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 818 | const SCEV *CastedMaxBECount = | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 819 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 820 | const SCEV *RecastedMaxBECount = | 
| Dan Gohman | 4fc3668 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 821 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); | 
|  | 822 | if (MaxBECount == RecastedMaxBECount) { | 
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 823 | const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 824 | // Check whether Start+Step*MaxBECount has no unsigned overflow. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 825 | const SCEV *ZMul = | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 826 | getMulExpr(CastedMaxBECount, | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 827 | getTruncateOrZeroExtend(Step, Start->getType())); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 828 | const SCEV *Add = getAddExpr(Start, ZMul); | 
|  | 829 | const SCEV *OperandExtendedAdd = | 
| Dan Gohman | 4fc3668 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 830 | getAddExpr(getZeroExtendExpr(Start, WideTy), | 
|  | 831 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), | 
|  | 832 | getZeroExtendExpr(Step, WideTy))); | 
|  | 833 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) | 
| Dan Gohman | 494dac3 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 834 | // Return the expression with the addrec on the outside. | 
|  | 835 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), | 
|  | 836 | getZeroExtendExpr(Step, Ty), | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 837 | L); | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 838 |  | 
|  | 839 | // Similar to above, only this time treat the step value as signed. | 
|  | 840 | // This covers loops that count down. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 841 | const SCEV *SMul = | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 842 | getMulExpr(CastedMaxBECount, | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 843 | getTruncateOrSignExtend(Step, Start->getType())); | 
| Dan Gohman | 494dac3 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 844 | Add = getAddExpr(Start, SMul); | 
| Dan Gohman | 4fc3668 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 845 | OperandExtendedAdd = | 
|  | 846 | getAddExpr(getZeroExtendExpr(Start, WideTy), | 
|  | 847 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), | 
|  | 848 | getSignExtendExpr(Step, WideTy))); | 
|  | 849 | if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) | 
| Dan Gohman | 494dac3 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 850 | // Return the expression with the addrec on the outside. | 
|  | 851 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), | 
|  | 852 | getSignExtendExpr(Step, Ty), | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 853 | L); | 
|  | 854 | } | 
|  | 855 |  | 
|  | 856 | // If the backedge is guarded by a comparison with the pre-inc value | 
|  | 857 | // the addrec is safe. Also, if the entry is guarded by a comparison | 
|  | 858 | // with the start value and the backedge is guarded by a comparison | 
|  | 859 | // with the post-inc value, the addrec is safe. | 
|  | 860 | if (isKnownPositive(Step)) { | 
|  | 861 | const SCEV *N = getConstant(APInt::getMinValue(BitWidth) - | 
|  | 862 | getUnsignedRange(Step).getUnsignedMax()); | 
|  | 863 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) || | 
|  | 864 | (isLoopGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) && | 
|  | 865 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, | 
|  | 866 | AR->getPostIncExpr(*this), N))) | 
|  | 867 | // Return the expression with the addrec on the outside. | 
|  | 868 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), | 
|  | 869 | getZeroExtendExpr(Step, Ty), | 
|  | 870 | L); | 
|  | 871 | } else if (isKnownNegative(Step)) { | 
|  | 872 | const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) - | 
|  | 873 | getSignedRange(Step).getSignedMin()); | 
|  | 874 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) && | 
|  | 875 | (isLoopGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) || | 
|  | 876 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, | 
|  | 877 | AR->getPostIncExpr(*this), N))) | 
|  | 878 | // Return the expression with the addrec on the outside. | 
|  | 879 | return getAddRecExpr(getZeroExtendExpr(Start, Ty), | 
|  | 880 | getSignExtendExpr(Step, Ty), | 
|  | 881 | L); | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 882 | } | 
|  | 883 | } | 
|  | 884 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 885 |  | 
| Dan Gohman | 74a0ba1 | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 886 | // The cast wasn't folded; create an explicit cast node. | 
|  | 887 | // Recompute the insert position, as it may have been invalidated. | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 888 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; | 
|  | 889 | SCEV *S = SCEVAllocator.Allocate<SCEVZeroExtendExpr>(); | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 890 | new (S) SCEVZeroExtendExpr(ID, Op, Ty); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 891 | UniqueSCEVs.InsertNode(S, IP); | 
|  | 892 | return S; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 893 | } | 
|  | 894 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 895 | const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op, | 
| Dan Gohman | fc76994 | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 896 | const Type *Ty) { | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 897 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && | 
| Dan Gohman | 413e91f | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 898 | "This is not an extending conversion!"); | 
| Dan Gohman | 194e42c | 2009-05-01 16:44:18 +0000 | [diff] [blame] | 899 | assert(isSCEVable(Ty) && | 
|  | 900 | "This is not a conversion to a SCEVable type!"); | 
|  | 901 | Ty = getEffectiveSCEVType(Ty); | 
| Dan Gohman | 413e91f | 2009-04-21 00:55:22 +0000 | [diff] [blame] | 902 |  | 
| Dan Gohman | 3423e72 | 2009-06-30 20:13:32 +0000 | [diff] [blame] | 903 | // Fold if the operand is constant. | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 904 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 905 | const Type *IntTy = getEffectiveSCEVType(Ty); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 906 | Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy); | 
|  | 907 | if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); | 
| Dan Gohman | 8d7576e | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 908 | return getConstant(cast<ConstantInt>(C)); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 909 | } | 
| Dan Gohman | cb9e09a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 910 |  | 
| Dan Gohman | 79af854 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 911 | // sext(sext(x)) --> sext(x) | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 912 | if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) | 
| Dan Gohman | 79af854 | 2009-04-22 16:20:48 +0000 | [diff] [blame] | 913 | return getSignExtendExpr(SS->getOperand(), Ty); | 
|  | 914 |  | 
| Dan Gohman | 74a0ba1 | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 915 | // Before doing any expensive analysis, check to see if we've already | 
|  | 916 | // computed a SCEV for this Op and Ty. | 
|  | 917 | FoldingSetNodeID ID; | 
|  | 918 | ID.AddInteger(scSignExtend); | 
|  | 919 | ID.AddPointer(Op); | 
|  | 920 | ID.AddPointer(Ty); | 
|  | 921 | void *IP = 0; | 
|  | 922 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; | 
|  | 923 |  | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 924 | // If the input value is a chrec scev, and we can prove that the value | 
| Dan Gohman | cb9e09a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 925 | // did not overflow the old, smaller, value, we can sign extend all of the | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 926 | // operands (often constants).  This allows analysis of something like | 
| Dan Gohman | cb9e09a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 927 | // this:  for (signed char X = 0; X < 100; ++X) { int Y = X; } | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 928 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 929 | if (AR->isAffine()) { | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 930 | const SCEV *Start = AR->getStart(); | 
|  | 931 | const SCEV *Step = AR->getStepRecurrence(*this); | 
|  | 932 | unsigned BitWidth = getTypeSizeInBits(AR->getType()); | 
|  | 933 | const Loop *L = AR->getLoop(); | 
|  | 934 |  | 
| Dan Gohman | 62ef6a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 935 | // If we have special knowledge that this addrec won't overflow, | 
|  | 936 | // we don't need to do any further analysis. | 
| Dan Gohman | 16f5415 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 937 | if (AR->hasNoSignedWrap()) | 
| Dan Gohman | 62ef6a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 938 | return getAddRecExpr(getSignExtendExpr(Start, Ty), | 
|  | 939 | getSignExtendExpr(Step, Ty), | 
|  | 940 | L); | 
|  | 941 |  | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 942 | // Check whether the backedge-taken count is SCEVCouldNotCompute. | 
|  | 943 | // Note that this serves two purposes: It filters out loops that are | 
|  | 944 | // simply not analyzable, and it covers the case where this code is | 
|  | 945 | // being called from within backedge-taken count analysis, such that | 
|  | 946 | // attempting to ask for the backedge-taken count would likely result | 
|  | 947 | // in infinite recursion. In the later case, the analysis code will | 
|  | 948 | // cope with a conservative value, and it will take care to purge | 
|  | 949 | // that value once it has finished. | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 950 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 951 | if (!isa<SCEVCouldNotCompute>(MaxBECount)) { | 
| Dan Gohman | 95c5b0e | 2009-04-29 01:54:20 +0000 | [diff] [blame] | 952 | // Manually compute the final value for AR, checking for | 
| Dan Gohman | 494dac3 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 953 | // overflow. | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 954 |  | 
|  | 955 | // Check whether the backedge-taken count can be losslessly casted to | 
| Dan Gohman | 494dac3 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 956 | // the addrec's type. The count is always unsigned. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 957 | const SCEV *CastedMaxBECount = | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 958 | getTruncateOrZeroExtend(MaxBECount, Start->getType()); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 959 | const SCEV *RecastedMaxBECount = | 
| Dan Gohman | 4fc3668 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 960 | getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); | 
|  | 961 | if (MaxBECount == RecastedMaxBECount) { | 
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 962 | const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 963 | // Check whether Start+Step*MaxBECount has no signed overflow. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 964 | const SCEV *SMul = | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 965 | getMulExpr(CastedMaxBECount, | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 966 | getTruncateOrSignExtend(Step, Start->getType())); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 967 | const SCEV *Add = getAddExpr(Start, SMul); | 
|  | 968 | const SCEV *OperandExtendedAdd = | 
| Dan Gohman | 4fc3668 | 2009-05-18 15:58:39 +0000 | [diff] [blame] | 969 | getAddExpr(getSignExtendExpr(Start, WideTy), | 
|  | 970 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), | 
|  | 971 | getSignExtendExpr(Step, WideTy))); | 
|  | 972 | if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) | 
| Dan Gohman | 494dac3 | 2009-04-29 22:28:28 +0000 | [diff] [blame] | 973 | // Return the expression with the addrec on the outside. | 
|  | 974 | return getAddRecExpr(getSignExtendExpr(Start, Ty), | 
|  | 975 | getSignExtendExpr(Step, Ty), | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 976 | L); | 
| Dan Gohman | 8c129d7 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 977 |  | 
|  | 978 | // Similar to above, only this time treat the step value as unsigned. | 
|  | 979 | // This covers loops that count up with an unsigned step. | 
|  | 980 | const SCEV *UMul = | 
|  | 981 | getMulExpr(CastedMaxBECount, | 
|  | 982 | getTruncateOrZeroExtend(Step, Start->getType())); | 
|  | 983 | Add = getAddExpr(Start, UMul); | 
|  | 984 | OperandExtendedAdd = | 
| Dan Gohman | 534d66a | 2009-07-25 16:03:30 +0000 | [diff] [blame] | 985 | getAddExpr(getSignExtendExpr(Start, WideTy), | 
| Dan Gohman | 8c129d7 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 986 | getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), | 
|  | 987 | getZeroExtendExpr(Step, WideTy))); | 
| Dan Gohman | 534d66a | 2009-07-25 16:03:30 +0000 | [diff] [blame] | 988 | if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) | 
| Dan Gohman | 8c129d7 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 989 | // Return the expression with the addrec on the outside. | 
|  | 990 | return getAddRecExpr(getSignExtendExpr(Start, Ty), | 
|  | 991 | getZeroExtendExpr(Step, Ty), | 
|  | 992 | L); | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 993 | } | 
|  | 994 |  | 
|  | 995 | // If the backedge is guarded by a comparison with the pre-inc value | 
|  | 996 | // the addrec is safe. Also, if the entry is guarded by a comparison | 
|  | 997 | // with the start value and the backedge is guarded by a comparison | 
|  | 998 | // with the post-inc value, the addrec is safe. | 
|  | 999 | if (isKnownPositive(Step)) { | 
|  | 1000 | const SCEV *N = getConstant(APInt::getSignedMinValue(BitWidth) - | 
|  | 1001 | getSignedRange(Step).getSignedMax()); | 
|  | 1002 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, AR, N) || | 
|  | 1003 | (isLoopGuardedByCond(L, ICmpInst::ICMP_SLT, Start, N) && | 
|  | 1004 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, | 
|  | 1005 | AR->getPostIncExpr(*this), N))) | 
|  | 1006 | // Return the expression with the addrec on the outside. | 
|  | 1007 | return getAddRecExpr(getSignExtendExpr(Start, Ty), | 
|  | 1008 | getSignExtendExpr(Step, Ty), | 
|  | 1009 | L); | 
|  | 1010 | } else if (isKnownNegative(Step)) { | 
|  | 1011 | const SCEV *N = getConstant(APInt::getSignedMaxValue(BitWidth) - | 
|  | 1012 | getSignedRange(Step).getSignedMin()); | 
|  | 1013 | if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, AR, N) || | 
|  | 1014 | (isLoopGuardedByCond(L, ICmpInst::ICMP_SGT, Start, N) && | 
|  | 1015 | isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, | 
|  | 1016 | AR->getPostIncExpr(*this), N))) | 
|  | 1017 | // Return the expression with the addrec on the outside. | 
|  | 1018 | return getAddRecExpr(getSignExtendExpr(Start, Ty), | 
|  | 1019 | getSignExtendExpr(Step, Ty), | 
|  | 1020 | L); | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 1021 | } | 
|  | 1022 | } | 
|  | 1023 | } | 
| Dan Gohman | cb9e09a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 1024 |  | 
| Dan Gohman | 74a0ba1 | 2009-07-13 20:55:53 +0000 | [diff] [blame] | 1025 | // The cast wasn't folded; create an explicit cast node. | 
|  | 1026 | // Recompute the insert position, as it may have been invalidated. | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1027 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; | 
|  | 1028 | SCEV *S = SCEVAllocator.Allocate<SCEVSignExtendExpr>(); | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1029 | new (S) SCEVSignExtendExpr(ID, Op, Ty); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1030 | UniqueSCEVs.InsertNode(S, IP); | 
|  | 1031 | return S; | 
| Dan Gohman | cb9e09a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 1032 | } | 
|  | 1033 |  | 
| Dan Gohman | 8db2edc | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1034 | /// getAnyExtendExpr - Return a SCEV for the given operand extended with | 
|  | 1035 | /// unspecified bits out to the given type. | 
|  | 1036 | /// | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1037 | const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op, | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 1038 | const Type *Ty) { | 
| Dan Gohman | 8db2edc | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1039 | assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && | 
|  | 1040 | "This is not an extending conversion!"); | 
|  | 1041 | assert(isSCEVable(Ty) && | 
|  | 1042 | "This is not a conversion to a SCEVable type!"); | 
|  | 1043 | Ty = getEffectiveSCEVType(Ty); | 
|  | 1044 |  | 
|  | 1045 | // Sign-extend negative constants. | 
|  | 1046 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) | 
|  | 1047 | if (SC->getValue()->getValue().isNegative()) | 
|  | 1048 | return getSignExtendExpr(Op, Ty); | 
|  | 1049 |  | 
|  | 1050 | // Peel off a truncate cast. | 
|  | 1051 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1052 | const SCEV *NewOp = T->getOperand(); | 
| Dan Gohman | 8db2edc | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1053 | if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty)) | 
|  | 1054 | return getAnyExtendExpr(NewOp, Ty); | 
|  | 1055 | return getTruncateOrNoop(NewOp, Ty); | 
|  | 1056 | } | 
|  | 1057 |  | 
|  | 1058 | // Next try a zext cast. If the cast is folded, use it. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1059 | const SCEV *ZExt = getZeroExtendExpr(Op, Ty); | 
| Dan Gohman | 8db2edc | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1060 | if (!isa<SCEVZeroExtendExpr>(ZExt)) | 
|  | 1061 | return ZExt; | 
|  | 1062 |  | 
|  | 1063 | // Next try a sext cast. If the cast is folded, use it. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1064 | const SCEV *SExt = getSignExtendExpr(Op, Ty); | 
| Dan Gohman | 8db2edc | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 1065 | if (!isa<SCEVSignExtendExpr>(SExt)) | 
|  | 1066 | return SExt; | 
|  | 1067 |  | 
|  | 1068 | // If the expression is obviously signed, use the sext cast value. | 
|  | 1069 | if (isa<SCEVSMaxExpr>(Op)) | 
|  | 1070 | return SExt; | 
|  | 1071 |  | 
|  | 1072 | // Absent any other information, use the zext cast value. | 
|  | 1073 | return ZExt; | 
|  | 1074 | } | 
|  | 1075 |  | 
| Dan Gohman | 038d02e | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1076 | /// CollectAddOperandsWithScales - Process the given Ops list, which is | 
|  | 1077 | /// a list of operands to be added under the given scale, update the given | 
|  | 1078 | /// map. This is a helper function for getAddRecExpr. As an example of | 
|  | 1079 | /// what it does, given a sequence of operands that would form an add | 
|  | 1080 | /// expression like this: | 
|  | 1081 | /// | 
|  | 1082 | ///    m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r) | 
|  | 1083 | /// | 
|  | 1084 | /// where A and B are constants, update the map with these values: | 
|  | 1085 | /// | 
|  | 1086 | ///    (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0) | 
|  | 1087 | /// | 
|  | 1088 | /// and add 13 + A*B*29 to AccumulatedConstant. | 
|  | 1089 | /// This will allow getAddRecExpr to produce this: | 
|  | 1090 | /// | 
|  | 1091 | ///    13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B) | 
|  | 1092 | /// | 
|  | 1093 | /// This form often exposes folding opportunities that are hidden in | 
|  | 1094 | /// the original operand list. | 
|  | 1095 | /// | 
|  | 1096 | /// Return true iff it appears that any interesting folding opportunities | 
|  | 1097 | /// may be exposed. This helps getAddRecExpr short-circuit extra work in | 
|  | 1098 | /// the common case where no interesting opportunities are present, and | 
|  | 1099 | /// is also used as a check to avoid infinite recursion. | 
|  | 1100 | /// | 
|  | 1101 | static bool | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1102 | CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M, | 
|  | 1103 | SmallVector<const SCEV *, 8> &NewOps, | 
| Dan Gohman | 038d02e | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1104 | APInt &AccumulatedConstant, | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1105 | const SmallVectorImpl<const SCEV *> &Ops, | 
| Dan Gohman | 038d02e | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1106 | const APInt &Scale, | 
|  | 1107 | ScalarEvolution &SE) { | 
|  | 1108 | bool Interesting = false; | 
|  | 1109 |  | 
|  | 1110 | // Iterate over the add operands. | 
|  | 1111 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { | 
|  | 1112 | const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]); | 
|  | 1113 | if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) { | 
|  | 1114 | APInt NewScale = | 
|  | 1115 | Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue(); | 
|  | 1116 | if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) { | 
|  | 1117 | // A multiplication of a constant with another add; recurse. | 
|  | 1118 | Interesting |= | 
|  | 1119 | CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, | 
|  | 1120 | cast<SCEVAddExpr>(Mul->getOperand(1)) | 
|  | 1121 | ->getOperands(), | 
|  | 1122 | NewScale, SE); | 
|  | 1123 | } else { | 
|  | 1124 | // A multiplication of a constant with some other value. Update | 
|  | 1125 | // the map. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1126 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end()); | 
|  | 1127 | const SCEV *Key = SE.getMulExpr(MulOps); | 
|  | 1128 | std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = | 
| Dan Gohman | e00beaa | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1129 | M.insert(std::make_pair(Key, NewScale)); | 
| Dan Gohman | 038d02e | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1130 | if (Pair.second) { | 
| Dan Gohman | 038d02e | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1131 | NewOps.push_back(Pair.first->first); | 
|  | 1132 | } else { | 
|  | 1133 | Pair.first->second += NewScale; | 
|  | 1134 | // The map already had an entry for this value, which may indicate | 
|  | 1135 | // a folding opportunity. | 
|  | 1136 | Interesting = true; | 
|  | 1137 | } | 
|  | 1138 | } | 
|  | 1139 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { | 
|  | 1140 | // Pull a buried constant out to the outside. | 
|  | 1141 | if (Scale != 1 || AccumulatedConstant != 0 || C->isZero()) | 
|  | 1142 | Interesting = true; | 
|  | 1143 | AccumulatedConstant += Scale * C->getValue()->getValue(); | 
|  | 1144 | } else { | 
|  | 1145 | // An ordinary operand. Update the map. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1146 | std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = | 
| Dan Gohman | e00beaa | 2009-06-29 18:25:52 +0000 | [diff] [blame] | 1147 | M.insert(std::make_pair(Ops[i], Scale)); | 
| Dan Gohman | 038d02e | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1148 | if (Pair.second) { | 
| Dan Gohman | 038d02e | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1149 | NewOps.push_back(Pair.first->first); | 
|  | 1150 | } else { | 
|  | 1151 | Pair.first->second += Scale; | 
|  | 1152 | // The map already had an entry for this value, which may indicate | 
|  | 1153 | // a folding opportunity. | 
|  | 1154 | Interesting = true; | 
|  | 1155 | } | 
|  | 1156 | } | 
|  | 1157 | } | 
|  | 1158 |  | 
|  | 1159 | return Interesting; | 
|  | 1160 | } | 
|  | 1161 |  | 
|  | 1162 | namespace { | 
|  | 1163 | struct APIntCompare { | 
|  | 1164 | bool operator()(const APInt &LHS, const APInt &RHS) const { | 
|  | 1165 | return LHS.ult(RHS); | 
|  | 1166 | } | 
|  | 1167 | }; | 
|  | 1168 | } | 
|  | 1169 |  | 
| Dan Gohman | 4d5435d | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1170 | /// getAddExpr - Get a canonical add expression, or something simpler if | 
|  | 1171 | /// possible. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1172 | const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1173 | assert(!Ops.empty() && "Cannot get empty add!"); | 
| Chris Lattner | 74498e1 | 2004-04-07 16:16:11 +0000 | [diff] [blame] | 1174 | if (Ops.size() == 1) return Ops[0]; | 
| Dan Gohman | d33f36e | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1175 | #ifndef NDEBUG | 
|  | 1176 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) | 
|  | 1177 | assert(getEffectiveSCEVType(Ops[i]->getType()) == | 
|  | 1178 | getEffectiveSCEVType(Ops[0]->getType()) && | 
|  | 1179 | "SCEVAddExpr operand types don't match!"); | 
|  | 1180 | #endif | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1181 |  | 
|  | 1182 | // Sort by complexity, this groups all similar expression types together. | 
| Dan Gohman | 9ba542c | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1183 | GroupByComplexity(Ops, LI); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1184 |  | 
|  | 1185 | // If there are any constants, fold them together. | 
|  | 1186 | unsigned Idx = 0; | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1187 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1188 | ++Idx; | 
| Chris Lattner | 74498e1 | 2004-04-07 16:16:11 +0000 | [diff] [blame] | 1189 | assert(Idx < Ops.size()); | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1190 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1191 | // We found two constants, fold them together! | 
| Dan Gohman | 0652fd5 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1192 | Ops[0] = getConstant(LHSC->getValue()->getValue() + | 
|  | 1193 | RHSC->getValue()->getValue()); | 
| Dan Gohman | 011cf68 | 2009-06-14 22:53:57 +0000 | [diff] [blame] | 1194 | if (Ops.size() == 2) return Ops[0]; | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1195 | Ops.erase(Ops.begin()+1);  // Erase the folded element | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1196 | LHSC = cast<SCEVConstant>(Ops[0]); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1197 | } | 
|  | 1198 |  | 
|  | 1199 | // If we are left with a constant zero being added, strip it off. | 
| Reid Spencer | 2e54a15 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1200 | if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1201 | Ops.erase(Ops.begin()); | 
|  | 1202 | --Idx; | 
|  | 1203 | } | 
|  | 1204 | } | 
|  | 1205 |  | 
| Chris Lattner | 74498e1 | 2004-04-07 16:16:11 +0000 | [diff] [blame] | 1206 | if (Ops.size() == 1) return Ops[0]; | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1207 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1208 | // Okay, check to see if the same value occurs in the operand list twice.  If | 
|  | 1209 | // so, merge them together into an multiply expression.  Since we sorted the | 
|  | 1210 | // list, these values are required to be adjacent. | 
|  | 1211 | const Type *Ty = Ops[0]->getType(); | 
|  | 1212 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) | 
|  | 1213 | if (Ops[i] == Ops[i+1]) {      //  X + Y + Y  -->  X + Y*2 | 
|  | 1214 | // Found a match, merge the two values into a multiply, and add any | 
|  | 1215 | // remaining values to the result. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1216 | const SCEV *Two = getIntegerSCEV(2, Ty); | 
|  | 1217 | const SCEV *Mul = getMulExpr(Ops[i], Two); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1218 | if (Ops.size() == 2) | 
|  | 1219 | return Mul; | 
|  | 1220 | Ops.erase(Ops.begin()+i, Ops.begin()+i+2); | 
|  | 1221 | Ops.push_back(Mul); | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1222 | return getAddExpr(Ops); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1223 | } | 
|  | 1224 |  | 
| Dan Gohman | 2e55cc5 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1225 | // Check for truncates. If all the operands are truncated from the same | 
|  | 1226 | // type, see if factoring out the truncate would permit the result to be | 
|  | 1227 | // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n) | 
|  | 1228 | // if the contents of the resulting outer trunc fold to something simple. | 
|  | 1229 | for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) { | 
|  | 1230 | const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]); | 
|  | 1231 | const Type *DstType = Trunc->getType(); | 
|  | 1232 | const Type *SrcType = Trunc->getOperand()->getType(); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1233 | SmallVector<const SCEV *, 8> LargeOps; | 
| Dan Gohman | 2e55cc5 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1234 | bool Ok = true; | 
|  | 1235 | // Check all the operands to see if they can be represented in the | 
|  | 1236 | // source type of the truncate. | 
|  | 1237 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { | 
|  | 1238 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) { | 
|  | 1239 | if (T->getOperand()->getType() != SrcType) { | 
|  | 1240 | Ok = false; | 
|  | 1241 | break; | 
|  | 1242 | } | 
|  | 1243 | LargeOps.push_back(T->getOperand()); | 
|  | 1244 | } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { | 
|  | 1245 | // This could be either sign or zero extension, but sign extension | 
|  | 1246 | // is much more likely to be foldable here. | 
|  | 1247 | LargeOps.push_back(getSignExtendExpr(C, SrcType)); | 
|  | 1248 | } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1249 | SmallVector<const SCEV *, 8> LargeMulOps; | 
| Dan Gohman | 2e55cc5 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1250 | for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { | 
|  | 1251 | if (const SCEVTruncateExpr *T = | 
|  | 1252 | dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) { | 
|  | 1253 | if (T->getOperand()->getType() != SrcType) { | 
|  | 1254 | Ok = false; | 
|  | 1255 | break; | 
|  | 1256 | } | 
|  | 1257 | LargeMulOps.push_back(T->getOperand()); | 
|  | 1258 | } else if (const SCEVConstant *C = | 
|  | 1259 | dyn_cast<SCEVConstant>(M->getOperand(j))) { | 
|  | 1260 | // This could be either sign or zero extension, but sign extension | 
|  | 1261 | // is much more likely to be foldable here. | 
|  | 1262 | LargeMulOps.push_back(getSignExtendExpr(C, SrcType)); | 
|  | 1263 | } else { | 
|  | 1264 | Ok = false; | 
|  | 1265 | break; | 
|  | 1266 | } | 
|  | 1267 | } | 
|  | 1268 | if (Ok) | 
|  | 1269 | LargeOps.push_back(getMulExpr(LargeMulOps)); | 
|  | 1270 | } else { | 
|  | 1271 | Ok = false; | 
|  | 1272 | break; | 
|  | 1273 | } | 
|  | 1274 | } | 
|  | 1275 | if (Ok) { | 
|  | 1276 | // Evaluate the expression in the larger type. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1277 | const SCEV *Fold = getAddExpr(LargeOps); | 
| Dan Gohman | 2e55cc5 | 2009-05-08 21:03:19 +0000 | [diff] [blame] | 1278 | // If it folds to something simple, use it. Otherwise, don't. | 
|  | 1279 | if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold)) | 
|  | 1280 | return getTruncateExpr(Fold, DstType); | 
|  | 1281 | } | 
|  | 1282 | } | 
|  | 1283 |  | 
|  | 1284 | // Skip past any other cast SCEVs. | 
| Dan Gohman | eed125f | 2007-06-18 19:30:09 +0000 | [diff] [blame] | 1285 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) | 
|  | 1286 | ++Idx; | 
|  | 1287 |  | 
|  | 1288 | // If there are add operands they would be next. | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1289 | if (Idx < Ops.size()) { | 
|  | 1290 | bool DeletedAdd = false; | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1291 | while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1292 | // If we have an add, expand the add operands onto the end of the operands | 
|  | 1293 | // list. | 
|  | 1294 | Ops.insert(Ops.end(), Add->op_begin(), Add->op_end()); | 
|  | 1295 | Ops.erase(Ops.begin()+Idx); | 
|  | 1296 | DeletedAdd = true; | 
|  | 1297 | } | 
|  | 1298 |  | 
|  | 1299 | // If we deleted at least one add, we added operands to the end of the list, | 
|  | 1300 | // and they are not necessarily sorted.  Recurse to resort and resimplify | 
|  | 1301 | // any operands we just aquired. | 
|  | 1302 | if (DeletedAdd) | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1303 | return getAddExpr(Ops); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1304 | } | 
|  | 1305 |  | 
|  | 1306 | // Skip over the add expression until we get to a multiply. | 
|  | 1307 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) | 
|  | 1308 | ++Idx; | 
|  | 1309 |  | 
| Dan Gohman | 038d02e | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1310 | // Check to see if there are any folding opportunities present with | 
|  | 1311 | // operands multiplied by constant values. | 
|  | 1312 | if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) { | 
|  | 1313 | uint64_t BitWidth = getTypeSizeInBits(Ty); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1314 | DenseMap<const SCEV *, APInt> M; | 
|  | 1315 | SmallVector<const SCEV *, 8> NewOps; | 
| Dan Gohman | 038d02e | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1316 | APInt AccumulatedConstant(BitWidth, 0); | 
|  | 1317 | if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, | 
|  | 1318 | Ops, APInt(BitWidth, 1), *this)) { | 
|  | 1319 | // Some interesting folding opportunity is present, so its worthwhile to | 
|  | 1320 | // re-generate the operands list. Group the operands by constant scale, | 
|  | 1321 | // to avoid multiplying by the same constant scale multiple times. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1322 | std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists; | 
|  | 1323 | for (SmallVector<const SCEV *, 8>::iterator I = NewOps.begin(), | 
| Dan Gohman | 038d02e | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1324 | E = NewOps.end(); I != E; ++I) | 
|  | 1325 | MulOpLists[M.find(*I)->second].push_back(*I); | 
|  | 1326 | // Re-generate the operands list. | 
|  | 1327 | Ops.clear(); | 
|  | 1328 | if (AccumulatedConstant != 0) | 
|  | 1329 | Ops.push_back(getConstant(AccumulatedConstant)); | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1330 | for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator | 
|  | 1331 | I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I) | 
| Dan Gohman | 038d02e | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1332 | if (I->first != 0) | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1333 | Ops.push_back(getMulExpr(getConstant(I->first), | 
|  | 1334 | getAddExpr(I->second))); | 
| Dan Gohman | 038d02e | 2009-06-14 22:58:51 +0000 | [diff] [blame] | 1335 | if (Ops.empty()) | 
|  | 1336 | return getIntegerSCEV(0, Ty); | 
|  | 1337 | if (Ops.size() == 1) | 
|  | 1338 | return Ops[0]; | 
|  | 1339 | return getAddExpr(Ops); | 
|  | 1340 | } | 
|  | 1341 | } | 
|  | 1342 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1343 | // If we are adding something to a multiply expression, make sure the | 
|  | 1344 | // something is not already an operand of the multiply.  If so, merge it into | 
|  | 1345 | // the multiply. | 
|  | 1346 | for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) { | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1347 | const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1348 | for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1349 | const SCEV *MulOpSCEV = Mul->getOperand(MulOp); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1350 | for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) | 
| Dan Gohman | 0652fd5 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1351 | if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(Ops[AddOp])) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1352 | // Fold W + X + (X * Y * Z)  -->  W + (X * ((Y*Z)+1)) | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1353 | const SCEV *InnerMul = Mul->getOperand(MulOp == 0); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1354 | if (Mul->getNumOperands() != 2) { | 
|  | 1355 | // If the multiply has more than two operands, we must get the | 
|  | 1356 | // Y*Z term. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1357 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), Mul->op_end()); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1358 | MulOps.erase(MulOps.begin()+MulOp); | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1359 | InnerMul = getMulExpr(MulOps); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1360 | } | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1361 | const SCEV *One = getIntegerSCEV(1, Ty); | 
|  | 1362 | const SCEV *AddOne = getAddExpr(InnerMul, One); | 
|  | 1363 | const SCEV *OuterMul = getMulExpr(AddOne, Ops[AddOp]); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1364 | if (Ops.size() == 2) return OuterMul; | 
|  | 1365 | if (AddOp < Idx) { | 
|  | 1366 | Ops.erase(Ops.begin()+AddOp); | 
|  | 1367 | Ops.erase(Ops.begin()+Idx-1); | 
|  | 1368 | } else { | 
|  | 1369 | Ops.erase(Ops.begin()+Idx); | 
|  | 1370 | Ops.erase(Ops.begin()+AddOp-1); | 
|  | 1371 | } | 
|  | 1372 | Ops.push_back(OuterMul); | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1373 | return getAddExpr(Ops); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1374 | } | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1375 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1376 | // Check this multiply against other multiplies being added together. | 
|  | 1377 | for (unsigned OtherMulIdx = Idx+1; | 
|  | 1378 | OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]); | 
|  | 1379 | ++OtherMulIdx) { | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1380 | const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1381 | // If MulOp occurs in OtherMul, we can fold the two multiplies | 
|  | 1382 | // together. | 
|  | 1383 | for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); | 
|  | 1384 | OMulOp != e; ++OMulOp) | 
|  | 1385 | if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { | 
|  | 1386 | // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E)) | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1387 | const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1388 | if (Mul->getNumOperands() != 2) { | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1389 | SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), | 
|  | 1390 | Mul->op_end()); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1391 | MulOps.erase(MulOps.begin()+MulOp); | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1392 | InnerMul1 = getMulExpr(MulOps); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1393 | } | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1394 | const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1395 | if (OtherMul->getNumOperands() != 2) { | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1396 | SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(), | 
|  | 1397 | OtherMul->op_end()); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1398 | MulOps.erase(MulOps.begin()+OMulOp); | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1399 | InnerMul2 = getMulExpr(MulOps); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1400 | } | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1401 | const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2); | 
|  | 1402 | const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1403 | if (Ops.size() == 2) return OuterMul; | 
|  | 1404 | Ops.erase(Ops.begin()+Idx); | 
|  | 1405 | Ops.erase(Ops.begin()+OtherMulIdx-1); | 
|  | 1406 | Ops.push_back(OuterMul); | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1407 | return getAddExpr(Ops); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1408 | } | 
|  | 1409 | } | 
|  | 1410 | } | 
|  | 1411 | } | 
|  | 1412 |  | 
|  | 1413 | // If there are any add recurrences in the operands list, see if any other | 
|  | 1414 | // added values are loop invariant.  If so, we can fold them into the | 
|  | 1415 | // recurrence. | 
|  | 1416 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) | 
|  | 1417 | ++Idx; | 
|  | 1418 |  | 
|  | 1419 | // Scan over all recurrences, trying to fold loop invariants into them. | 
|  | 1420 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { | 
|  | 1421 | // Scan all of the other operands to this add and add them to the vector if | 
|  | 1422 | // they are loop invariant w.r.t. the recurrence. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1423 | SmallVector<const SCEV *, 8> LIOps; | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1424 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1425 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) | 
|  | 1426 | if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { | 
|  | 1427 | LIOps.push_back(Ops[i]); | 
|  | 1428 | Ops.erase(Ops.begin()+i); | 
|  | 1429 | --i; --e; | 
|  | 1430 | } | 
|  | 1431 |  | 
|  | 1432 | // If we found some loop invariants, fold them into the recurrence. | 
|  | 1433 | if (!LIOps.empty()) { | 
| Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1434 | //  NLI + LI + {Start,+,Step}  -->  NLI + {LI+Start,+,Step} | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1435 | LIOps.push_back(AddRec->getStart()); | 
|  | 1436 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1437 | SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(), | 
| Dan Gohman | 0652fd5 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1438 | AddRec->op_end()); | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1439 | AddRecOps[0] = getAddExpr(LIOps); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1440 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1441 | const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRec->getLoop()); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1442 | // If all of the other operands were loop invariant, we are done. | 
|  | 1443 | if (Ops.size() == 1) return NewRec; | 
|  | 1444 |  | 
|  | 1445 | // Otherwise, add the folded AddRec by the non-liv parts. | 
|  | 1446 | for (unsigned i = 0;; ++i) | 
|  | 1447 | if (Ops[i] == AddRec) { | 
|  | 1448 | Ops[i] = NewRec; | 
|  | 1449 | break; | 
|  | 1450 | } | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1451 | return getAddExpr(Ops); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1452 | } | 
|  | 1453 |  | 
|  | 1454 | // Okay, if there weren't any loop invariants to be folded, check to see if | 
|  | 1455 | // there are multiple AddRec's with the same loop induction variable being | 
|  | 1456 | // added together.  If so, we can fold them. | 
|  | 1457 | for (unsigned OtherIdx = Idx+1; | 
|  | 1458 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) | 
|  | 1459 | if (OtherIdx != Idx) { | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1460 | const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1461 | if (AddRec->getLoop() == OtherAddRec->getLoop()) { | 
|  | 1462 | // Other + {A,+,B} + {C,+,D}  -->  Other + {A+C,+,B+D} | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1463 | SmallVector<const SCEV *, 4> NewOps(AddRec->op_begin(), | 
|  | 1464 | AddRec->op_end()); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1465 | for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) { | 
|  | 1466 | if (i >= NewOps.size()) { | 
|  | 1467 | NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i, | 
|  | 1468 | OtherAddRec->op_end()); | 
|  | 1469 | break; | 
|  | 1470 | } | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1471 | NewOps[i] = getAddExpr(NewOps[i], OtherAddRec->getOperand(i)); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1472 | } | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1473 | const SCEV *NewAddRec = getAddRecExpr(NewOps, AddRec->getLoop()); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1474 |  | 
|  | 1475 | if (Ops.size() == 2) return NewAddRec; | 
|  | 1476 |  | 
|  | 1477 | Ops.erase(Ops.begin()+Idx); | 
|  | 1478 | Ops.erase(Ops.begin()+OtherIdx-1); | 
|  | 1479 | Ops.push_back(NewAddRec); | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1480 | return getAddExpr(Ops); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1481 | } | 
|  | 1482 | } | 
|  | 1483 |  | 
|  | 1484 | // Otherwise couldn't fold anything into this recurrence.  Move onto the | 
|  | 1485 | // next one. | 
|  | 1486 | } | 
|  | 1487 |  | 
|  | 1488 | // Okay, it looks like we really DO need an add expr.  Check to see if we | 
|  | 1489 | // already have one, otherwise create a new one. | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1490 | FoldingSetNodeID ID; | 
|  | 1491 | ID.AddInteger(scAddExpr); | 
|  | 1492 | ID.AddInteger(Ops.size()); | 
|  | 1493 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) | 
|  | 1494 | ID.AddPointer(Ops[i]); | 
|  | 1495 | void *IP = 0; | 
|  | 1496 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; | 
|  | 1497 | SCEV *S = SCEVAllocator.Allocate<SCEVAddExpr>(); | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1498 | new (S) SCEVAddExpr(ID, Ops); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1499 | UniqueSCEVs.InsertNode(S, IP); | 
|  | 1500 | return S; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1501 | } | 
|  | 1502 |  | 
|  | 1503 |  | 
| Dan Gohman | 4d5435d | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1504 | /// getMulExpr - Get a canonical multiply expression, or something simpler if | 
|  | 1505 | /// possible. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1506 | const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1507 | assert(!Ops.empty() && "Cannot get empty mul!"); | 
| Dan Gohman | d33f36e | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1508 | #ifndef NDEBUG | 
|  | 1509 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) | 
|  | 1510 | assert(getEffectiveSCEVType(Ops[i]->getType()) == | 
|  | 1511 | getEffectiveSCEVType(Ops[0]->getType()) && | 
|  | 1512 | "SCEVMulExpr operand types don't match!"); | 
|  | 1513 | #endif | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1514 |  | 
|  | 1515 | // Sort by complexity, this groups all similar expression types together. | 
| Dan Gohman | 9ba542c | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1516 | GroupByComplexity(Ops, LI); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1517 |  | 
|  | 1518 | // If there are any constants, fold them together. | 
|  | 1519 | unsigned Idx = 0; | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1520 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1521 |  | 
|  | 1522 | // C1*(C2+V) -> C1*C2 + C1*V | 
|  | 1523 | if (Ops.size() == 2) | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1524 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1525 | if (Add->getNumOperands() == 2 && | 
|  | 1526 | isa<SCEVConstant>(Add->getOperand(0))) | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1527 | return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)), | 
|  | 1528 | getMulExpr(LHSC, Add->getOperand(1))); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1529 |  | 
|  | 1530 |  | 
|  | 1531 | ++Idx; | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1532 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1533 | // We found two constants, fold them together! | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1534 | ConstantInt *Fold = ConstantInt::get(getContext(), | 
|  | 1535 | LHSC->getValue()->getValue() * | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1536 | RHSC->getValue()->getValue()); | 
|  | 1537 | Ops[0] = getConstant(Fold); | 
|  | 1538 | Ops.erase(Ops.begin()+1);  // Erase the folded element | 
|  | 1539 | if (Ops.size() == 1) return Ops[0]; | 
|  | 1540 | LHSC = cast<SCEVConstant>(Ops[0]); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1541 | } | 
|  | 1542 |  | 
|  | 1543 | // If we are left with a constant one being multiplied, strip it off. | 
|  | 1544 | if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) { | 
|  | 1545 | Ops.erase(Ops.begin()); | 
|  | 1546 | --Idx; | 
| Reid Spencer | 2e54a15 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1547 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1548 | // If we have a multiply of zero, it will always be zero. | 
|  | 1549 | return Ops[0]; | 
|  | 1550 | } | 
|  | 1551 | } | 
|  | 1552 |  | 
|  | 1553 | // Skip over the add expression until we get to a multiply. | 
|  | 1554 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) | 
|  | 1555 | ++Idx; | 
|  | 1556 |  | 
|  | 1557 | if (Ops.size() == 1) | 
|  | 1558 | return Ops[0]; | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 1559 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1560 | // If there are mul operands inline them all into this expression. | 
|  | 1561 | if (Idx < Ops.size()) { | 
|  | 1562 | bool DeletedMul = false; | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1563 | while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1564 | // If we have an mul, expand the mul operands onto the end of the operands | 
|  | 1565 | // list. | 
|  | 1566 | Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end()); | 
|  | 1567 | Ops.erase(Ops.begin()+Idx); | 
|  | 1568 | DeletedMul = true; | 
|  | 1569 | } | 
|  | 1570 |  | 
|  | 1571 | // If we deleted at least one mul, we added operands to the end of the list, | 
|  | 1572 | // and they are not necessarily sorted.  Recurse to resort and resimplify | 
|  | 1573 | // any operands we just aquired. | 
|  | 1574 | if (DeletedMul) | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1575 | return getMulExpr(Ops); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1576 | } | 
|  | 1577 |  | 
|  | 1578 | // If there are any add recurrences in the operands list, see if any other | 
|  | 1579 | // added values are loop invariant.  If so, we can fold them into the | 
|  | 1580 | // recurrence. | 
|  | 1581 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) | 
|  | 1582 | ++Idx; | 
|  | 1583 |  | 
|  | 1584 | // Scan over all recurrences, trying to fold loop invariants into them. | 
|  | 1585 | for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { | 
|  | 1586 | // Scan all of the other operands to this mul and add them to the vector if | 
|  | 1587 | // they are loop invariant w.r.t. the recurrence. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1588 | SmallVector<const SCEV *, 8> LIOps; | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1589 | const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1590 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) | 
|  | 1591 | if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { | 
|  | 1592 | LIOps.push_back(Ops[i]); | 
|  | 1593 | Ops.erase(Ops.begin()+i); | 
|  | 1594 | --i; --e; | 
|  | 1595 | } | 
|  | 1596 |  | 
|  | 1597 | // If we found some loop invariants, fold them into the recurrence. | 
|  | 1598 | if (!LIOps.empty()) { | 
| Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1599 | //  NLI * LI * {Start,+,Step}  -->  NLI * {LI*Start,+,LI*Step} | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1600 | SmallVector<const SCEV *, 4> NewOps; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1601 | NewOps.reserve(AddRec->getNumOperands()); | 
|  | 1602 | if (LIOps.size() == 1) { | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1603 | const SCEV *Scale = LIOps[0]; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1604 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1605 | NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i))); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1606 | } else { | 
|  | 1607 | for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1608 | SmallVector<const SCEV *, 4> MulOps(LIOps.begin(), LIOps.end()); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1609 | MulOps.push_back(AddRec->getOperand(i)); | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1610 | NewOps.push_back(getMulExpr(MulOps)); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1611 | } | 
|  | 1612 | } | 
|  | 1613 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1614 | const SCEV *NewRec = getAddRecExpr(NewOps, AddRec->getLoop()); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1615 |  | 
|  | 1616 | // If all of the other operands were loop invariant, we are done. | 
|  | 1617 | if (Ops.size() == 1) return NewRec; | 
|  | 1618 |  | 
|  | 1619 | // Otherwise, multiply the folded AddRec by the non-liv parts. | 
|  | 1620 | for (unsigned i = 0;; ++i) | 
|  | 1621 | if (Ops[i] == AddRec) { | 
|  | 1622 | Ops[i] = NewRec; | 
|  | 1623 | break; | 
|  | 1624 | } | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1625 | return getMulExpr(Ops); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1626 | } | 
|  | 1627 |  | 
|  | 1628 | // Okay, if there weren't any loop invariants to be folded, check to see if | 
|  | 1629 | // there are multiple AddRec's with the same loop induction variable being | 
|  | 1630 | // multiplied together.  If so, we can fold them. | 
|  | 1631 | for (unsigned OtherIdx = Idx+1; | 
|  | 1632 | OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) | 
|  | 1633 | if (OtherIdx != Idx) { | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1634 | const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1635 | if (AddRec->getLoop() == OtherAddRec->getLoop()) { | 
|  | 1636 | // F * G  -->  {A,+,B} * {C,+,D}  -->  {A*C,+,F*D + G*B + B*D} | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 1637 | const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec; | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1638 | const SCEV *NewStart = getMulExpr(F->getStart(), | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1639 | G->getStart()); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1640 | const SCEV *B = F->getStepRecurrence(*this); | 
|  | 1641 | const SCEV *D = G->getStepRecurrence(*this); | 
|  | 1642 | const SCEV *NewStep = getAddExpr(getMulExpr(F, D), | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1643 | getMulExpr(G, B), | 
|  | 1644 | getMulExpr(B, D)); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1645 | const SCEV *NewAddRec = getAddRecExpr(NewStart, NewStep, | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1646 | F->getLoop()); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1647 | if (Ops.size() == 2) return NewAddRec; | 
|  | 1648 |  | 
|  | 1649 | Ops.erase(Ops.begin()+Idx); | 
|  | 1650 | Ops.erase(Ops.begin()+OtherIdx-1); | 
|  | 1651 | Ops.push_back(NewAddRec); | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1652 | return getMulExpr(Ops); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1653 | } | 
|  | 1654 | } | 
|  | 1655 |  | 
|  | 1656 | // Otherwise couldn't fold anything into this recurrence.  Move onto the | 
|  | 1657 | // next one. | 
|  | 1658 | } | 
|  | 1659 |  | 
|  | 1660 | // Okay, it looks like we really DO need an mul expr.  Check to see if we | 
|  | 1661 | // already have one, otherwise create a new one. | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1662 | FoldingSetNodeID ID; | 
|  | 1663 | ID.AddInteger(scMulExpr); | 
|  | 1664 | ID.AddInteger(Ops.size()); | 
|  | 1665 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) | 
|  | 1666 | ID.AddPointer(Ops[i]); | 
|  | 1667 | void *IP = 0; | 
|  | 1668 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; | 
|  | 1669 | SCEV *S = SCEVAllocator.Allocate<SCEVMulExpr>(); | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1670 | new (S) SCEVMulExpr(ID, Ops); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1671 | UniqueSCEVs.InsertNode(S, IP); | 
|  | 1672 | return S; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1673 | } | 
|  | 1674 |  | 
| Andreas Bolka | 7a5c8db | 2009-08-07 22:55:26 +0000 | [diff] [blame] | 1675 | /// getUDivExpr - Get a canonical unsigned division expression, or something | 
|  | 1676 | /// simpler if possible. | 
| Dan Gohman | abd1709 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1677 | const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS, | 
|  | 1678 | const SCEV *RHS) { | 
| Dan Gohman | d33f36e | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1679 | assert(getEffectiveSCEVType(LHS->getType()) == | 
|  | 1680 | getEffectiveSCEVType(RHS->getType()) && | 
|  | 1681 | "SCEVUDivExpr operand types don't match!"); | 
|  | 1682 |  | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1683 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1684 | if (RHSC->getValue()->equalsInt(1)) | 
| Dan Gohman | 8a8ad7d | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 1685 | return LHS;                               // X udiv 1 --> x | 
| Dan Gohman | c3a3cb4 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1686 | if (RHSC->isZero()) | 
|  | 1687 | return getIntegerSCEV(0, LHS->getType()); // value is undefined | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1688 |  | 
| Dan Gohman | c3a3cb4 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1689 | // Determine if the division can be folded into the operands of | 
|  | 1690 | // its operands. | 
|  | 1691 | // TODO: Generalize this to non-constants by using known-bits information. | 
|  | 1692 | const Type *Ty = LHS->getType(); | 
|  | 1693 | unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros(); | 
|  | 1694 | unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ; | 
|  | 1695 | // For non-power-of-two values, effectively round the value up to the | 
|  | 1696 | // nearest power of two. | 
|  | 1697 | if (!RHSC->getValue()->getValue().isPowerOf2()) | 
|  | 1698 | ++MaxShiftAmt; | 
|  | 1699 | const IntegerType *ExtTy = | 
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1700 | IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt); | 
| Dan Gohman | c3a3cb4 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1701 | // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. | 
|  | 1702 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) | 
|  | 1703 | if (const SCEVConstant *Step = | 
|  | 1704 | dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) | 
|  | 1705 | if (!Step->getValue()->getValue() | 
|  | 1706 | .urem(RHSC->getValue()->getValue()) && | 
| Dan Gohman | 35dc9b6 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1707 | getZeroExtendExpr(AR, ExtTy) == | 
|  | 1708 | getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), | 
|  | 1709 | getZeroExtendExpr(Step, ExtTy), | 
|  | 1710 | AR->getLoop())) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1711 | SmallVector<const SCEV *, 4> Operands; | 
| Dan Gohman | c3a3cb4 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1712 | for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i) | 
|  | 1713 | Operands.push_back(getUDivExpr(AR->getOperand(i), RHS)); | 
|  | 1714 | return getAddRecExpr(Operands, AR->getLoop()); | 
|  | 1715 | } | 
|  | 1716 | // (A*B)/C --> A*(B/C) if safe and B/C can be folded. | 
| Dan Gohman | 35dc9b6 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1717 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1718 | SmallVector<const SCEV *, 4> Operands; | 
| Dan Gohman | 35dc9b6 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1719 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) | 
|  | 1720 | Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy)); | 
|  | 1721 | if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) | 
| Dan Gohman | c3a3cb4 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1722 | // Find an operand that's safely divisible. | 
|  | 1723 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1724 | const SCEV *Op = M->getOperand(i); | 
|  | 1725 | const SCEV *Div = getUDivExpr(Op, RHSC); | 
| Dan Gohman | c3a3cb4 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1726 | if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1727 | const SmallVectorImpl<const SCEV *> &MOperands = M->getOperands(); | 
|  | 1728 | Operands = SmallVector<const SCEV *, 4>(MOperands.begin(), | 
| Dan Gohman | 0652fd5 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1729 | MOperands.end()); | 
| Dan Gohman | c3a3cb4 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1730 | Operands[i] = Div; | 
|  | 1731 | return getMulExpr(Operands); | 
|  | 1732 | } | 
|  | 1733 | } | 
| Dan Gohman | 35dc9b6 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1734 | } | 
| Dan Gohman | c3a3cb4 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1735 | // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded. | 
| Dan Gohman | 35dc9b6 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1736 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1737 | SmallVector<const SCEV *, 4> Operands; | 
| Dan Gohman | 35dc9b6 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1738 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) | 
|  | 1739 | Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy)); | 
|  | 1740 | if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { | 
|  | 1741 | Operands.clear(); | 
| Dan Gohman | c3a3cb4 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1742 | for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1743 | const SCEV *Op = getUDivExpr(A->getOperand(i), RHS); | 
| Dan Gohman | c3a3cb4 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1744 | if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i)) | 
|  | 1745 | break; | 
|  | 1746 | Operands.push_back(Op); | 
|  | 1747 | } | 
|  | 1748 | if (Operands.size() == A->getNumOperands()) | 
|  | 1749 | return getAddExpr(Operands); | 
|  | 1750 | } | 
| Dan Gohman | 35dc9b6 | 2009-05-08 23:11:16 +0000 | [diff] [blame] | 1751 | } | 
| Dan Gohman | c3a3cb4 | 2009-05-08 20:18:49 +0000 | [diff] [blame] | 1752 |  | 
|  | 1753 | // Fold if both operands are constant. | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1754 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1755 | Constant *LHSCV = LHSC->getValue(); | 
|  | 1756 | Constant *RHSCV = RHSC->getValue(); | 
| Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1757 | return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV, | 
| Dan Gohman | 8d7576e | 2009-06-24 00:38:39 +0000 | [diff] [blame] | 1758 | RHSCV))); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1759 | } | 
|  | 1760 | } | 
|  | 1761 |  | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1762 | FoldingSetNodeID ID; | 
|  | 1763 | ID.AddInteger(scUDivExpr); | 
|  | 1764 | ID.AddPointer(LHS); | 
|  | 1765 | ID.AddPointer(RHS); | 
|  | 1766 | void *IP = 0; | 
|  | 1767 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; | 
|  | 1768 | SCEV *S = SCEVAllocator.Allocate<SCEVUDivExpr>(); | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1769 | new (S) SCEVUDivExpr(ID, LHS, RHS); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1770 | UniqueSCEVs.InsertNode(S, IP); | 
|  | 1771 | return S; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1772 | } | 
|  | 1773 |  | 
|  | 1774 |  | 
| Dan Gohman | 4d5435d | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1775 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. | 
|  | 1776 | /// Simplify the expression as much as possible. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1777 | const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, | 
| Dan Gohman | 5ece69a | 2009-07-24 01:03:59 +0000 | [diff] [blame] | 1778 | const SCEV *Step, const Loop *L) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1779 | SmallVector<const SCEV *, 4> Operands; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1780 | Operands.push_back(Start); | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1781 | if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step)) | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1782 | if (StepChrec->getLoop() == L) { | 
|  | 1783 | Operands.insert(Operands.end(), StepChrec->op_begin(), | 
|  | 1784 | StepChrec->op_end()); | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1785 | return getAddRecExpr(Operands, L); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1786 | } | 
|  | 1787 |  | 
|  | 1788 | Operands.push_back(Step); | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 1789 | return getAddRecExpr(Operands, L); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1790 | } | 
|  | 1791 |  | 
| Dan Gohman | 4d5435d | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 1792 | /// getAddRecExpr - Get an add recurrence expression for the specified loop. | 
|  | 1793 | /// Simplify the expression as much as possible. | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1794 | const SCEV * | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1795 | ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands, | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 1796 | const Loop *L) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1797 | if (Operands.size() == 1) return Operands[0]; | 
| Dan Gohman | d33f36e | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1798 | #ifndef NDEBUG | 
|  | 1799 | for (unsigned i = 1, e = Operands.size(); i != e; ++i) | 
|  | 1800 | assert(getEffectiveSCEVType(Operands[i]->getType()) == | 
|  | 1801 | getEffectiveSCEVType(Operands[0]->getType()) && | 
|  | 1802 | "SCEVAddRecExpr operand types don't match!"); | 
|  | 1803 | #endif | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1804 |  | 
| Dan Gohman | be928e3 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1805 | if (Operands.back()->isZero()) { | 
|  | 1806 | Operands.pop_back(); | 
| Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 1807 | return getAddRecExpr(Operands, L);             // {X,+,0}  -->  X | 
| Dan Gohman | be928e3 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 1808 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1809 |  | 
| Dan Gohman | 223a5d2 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1810 | // Canonicalize nested AddRecs in by nesting them in order of loop depth. | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1811 | if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) { | 
| Dan Gohman | 223a5d2 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1812 | const Loop* NestedLoop = NestedAR->getLoop(); | 
|  | 1813 | if (L->getLoopDepth() < NestedLoop->getLoopDepth()) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1814 | SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(), | 
| Dan Gohman | 0652fd5 | 2009-06-14 22:47:23 +0000 | [diff] [blame] | 1815 | NestedAR->op_end()); | 
| Dan Gohman | 223a5d2 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1816 | Operands[0] = NestedAR->getStart(); | 
| Dan Gohman | cc030b7 | 2009-06-26 22:36:20 +0000 | [diff] [blame] | 1817 | // AddRecs require their operands be loop-invariant with respect to their | 
|  | 1818 | // loops. Don't perform this transformation if it would break this | 
|  | 1819 | // requirement. | 
|  | 1820 | bool AllInvariant = true; | 
|  | 1821 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) | 
|  | 1822 | if (!Operands[i]->isLoopInvariant(L)) { | 
|  | 1823 | AllInvariant = false; | 
|  | 1824 | break; | 
|  | 1825 | } | 
|  | 1826 | if (AllInvariant) { | 
|  | 1827 | NestedOperands[0] = getAddRecExpr(Operands, L); | 
|  | 1828 | AllInvariant = true; | 
|  | 1829 | for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i) | 
|  | 1830 | if (!NestedOperands[i]->isLoopInvariant(NestedLoop)) { | 
|  | 1831 | AllInvariant = false; | 
|  | 1832 | break; | 
|  | 1833 | } | 
|  | 1834 | if (AllInvariant) | 
|  | 1835 | // Ok, both add recurrences are valid after the transformation. | 
|  | 1836 | return getAddRecExpr(NestedOperands, NestedLoop); | 
|  | 1837 | } | 
|  | 1838 | // Reset Operands to its original state. | 
|  | 1839 | Operands[0] = NestedAR; | 
| Dan Gohman | 223a5d2 | 2008-08-08 18:33:12 +0000 | [diff] [blame] | 1840 | } | 
|  | 1841 | } | 
|  | 1842 |  | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1843 | FoldingSetNodeID ID; | 
|  | 1844 | ID.AddInteger(scAddRecExpr); | 
|  | 1845 | ID.AddInteger(Operands.size()); | 
|  | 1846 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) | 
|  | 1847 | ID.AddPointer(Operands[i]); | 
|  | 1848 | ID.AddPointer(L); | 
|  | 1849 | void *IP = 0; | 
|  | 1850 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; | 
|  | 1851 | SCEV *S = SCEVAllocator.Allocate<SCEVAddRecExpr>(); | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1852 | new (S) SCEVAddRecExpr(ID, Operands, L); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1853 | UniqueSCEVs.InsertNode(S, IP); | 
|  | 1854 | return S; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 1855 | } | 
|  | 1856 |  | 
| Dan Gohman | abd1709 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1857 | const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, | 
|  | 1858 | const SCEV *RHS) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1859 | SmallVector<const SCEV *, 2> Ops; | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1860 | Ops.push_back(LHS); | 
|  | 1861 | Ops.push_back(RHS); | 
|  | 1862 | return getSMaxExpr(Ops); | 
|  | 1863 | } | 
|  | 1864 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1865 | const SCEV * | 
|  | 1866 | ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1867 | assert(!Ops.empty() && "Cannot get empty smax!"); | 
|  | 1868 | if (Ops.size() == 1) return Ops[0]; | 
| Dan Gohman | d33f36e | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1869 | #ifndef NDEBUG | 
|  | 1870 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) | 
|  | 1871 | assert(getEffectiveSCEVType(Ops[i]->getType()) == | 
|  | 1872 | getEffectiveSCEVType(Ops[0]->getType()) && | 
|  | 1873 | "SCEVSMaxExpr operand types don't match!"); | 
|  | 1874 | #endif | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1875 |  | 
|  | 1876 | // Sort by complexity, this groups all similar expression types together. | 
| Dan Gohman | 9ba542c | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1877 | GroupByComplexity(Ops, LI); | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1878 |  | 
|  | 1879 | // If there are any constants, fold them together. | 
|  | 1880 | unsigned Idx = 0; | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1881 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1882 | ++Idx; | 
|  | 1883 | assert(Idx < Ops.size()); | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1884 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1885 | // We found two constants, fold them together! | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1886 | ConstantInt *Fold = ConstantInt::get(getContext(), | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1887 | APIntOps::smax(LHSC->getValue()->getValue(), | 
|  | 1888 | RHSC->getValue()->getValue())); | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1889 | Ops[0] = getConstant(Fold); | 
|  | 1890 | Ops.erase(Ops.begin()+1);  // Erase the folded element | 
|  | 1891 | if (Ops.size() == 1) return Ops[0]; | 
|  | 1892 | LHSC = cast<SCEVConstant>(Ops[0]); | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1893 | } | 
|  | 1894 |  | 
| Dan Gohman | f57bdb7 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1895 | // If we are left with a constant minimum-int, strip it off. | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1896 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) { | 
|  | 1897 | Ops.erase(Ops.begin()); | 
|  | 1898 | --Idx; | 
| Dan Gohman | f57bdb7 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1899 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) { | 
|  | 1900 | // If we have an smax with a constant maximum-int, it will always be | 
|  | 1901 | // maximum-int. | 
|  | 1902 | return Ops[0]; | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1903 | } | 
|  | 1904 | } | 
|  | 1905 |  | 
|  | 1906 | if (Ops.size() == 1) return Ops[0]; | 
|  | 1907 |  | 
|  | 1908 | // Find the first SMax | 
|  | 1909 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr) | 
|  | 1910 | ++Idx; | 
|  | 1911 |  | 
|  | 1912 | // Check to see if one of the operands is an SMax. If so, expand its operands | 
|  | 1913 | // onto our operand list, and recurse to simplify. | 
|  | 1914 | if (Idx < Ops.size()) { | 
|  | 1915 | bool DeletedSMax = false; | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1916 | while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) { | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1917 | Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end()); | 
|  | 1918 | Ops.erase(Ops.begin()+Idx); | 
|  | 1919 | DeletedSMax = true; | 
|  | 1920 | } | 
|  | 1921 |  | 
|  | 1922 | if (DeletedSMax) | 
|  | 1923 | return getSMaxExpr(Ops); | 
|  | 1924 | } | 
|  | 1925 |  | 
|  | 1926 | // Okay, check to see if the same value occurs in the operand list twice.  If | 
|  | 1927 | // so, delete one.  Since we sorted the list, these values are required to | 
|  | 1928 | // be adjacent. | 
|  | 1929 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) | 
|  | 1930 | if (Ops[i] == Ops[i+1]) {      //  X smax Y smax Y  -->  X smax Y | 
|  | 1931 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); | 
|  | 1932 | --i; --e; | 
|  | 1933 | } | 
|  | 1934 |  | 
|  | 1935 | if (Ops.size() == 1) return Ops[0]; | 
|  | 1936 |  | 
|  | 1937 | assert(!Ops.empty() && "Reduced smax down to nothing!"); | 
|  | 1938 |  | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1939 | // Okay, it looks like we really DO need an smax expr.  Check to see if we | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1940 | // already have one, otherwise create a new one. | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1941 | FoldingSetNodeID ID; | 
|  | 1942 | ID.AddInteger(scSMaxExpr); | 
|  | 1943 | ID.AddInteger(Ops.size()); | 
|  | 1944 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) | 
|  | 1945 | ID.AddPointer(Ops[i]); | 
|  | 1946 | void *IP = 0; | 
|  | 1947 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; | 
|  | 1948 | SCEV *S = SCEVAllocator.Allocate<SCEVSMaxExpr>(); | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 1949 | new (S) SCEVSMaxExpr(ID, Ops); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 1950 | UniqueSCEVs.InsertNode(S, IP); | 
|  | 1951 | return S; | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 1952 | } | 
|  | 1953 |  | 
| Dan Gohman | abd1709 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 1954 | const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, | 
|  | 1955 | const SCEV *RHS) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1956 | SmallVector<const SCEV *, 2> Ops; | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1957 | Ops.push_back(LHS); | 
|  | 1958 | Ops.push_back(RHS); | 
|  | 1959 | return getUMaxExpr(Ops); | 
|  | 1960 | } | 
|  | 1961 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1962 | const SCEV * | 
|  | 1963 | ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1964 | assert(!Ops.empty() && "Cannot get empty umax!"); | 
|  | 1965 | if (Ops.size() == 1) return Ops[0]; | 
| Dan Gohman | d33f36e | 2009-05-18 15:44:58 +0000 | [diff] [blame] | 1966 | #ifndef NDEBUG | 
|  | 1967 | for (unsigned i = 1, e = Ops.size(); i != e; ++i) | 
|  | 1968 | assert(getEffectiveSCEVType(Ops[i]->getType()) == | 
|  | 1969 | getEffectiveSCEVType(Ops[0]->getType()) && | 
|  | 1970 | "SCEVUMaxExpr operand types don't match!"); | 
|  | 1971 | #endif | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1972 |  | 
|  | 1973 | // Sort by complexity, this groups all similar expression types together. | 
| Dan Gohman | 9ba542c | 2009-05-07 14:39:04 +0000 | [diff] [blame] | 1974 | GroupByComplexity(Ops, LI); | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1975 |  | 
|  | 1976 | // If there are any constants, fold them together. | 
|  | 1977 | unsigned Idx = 0; | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1978 | if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1979 | ++Idx; | 
|  | 1980 | assert(Idx < Ops.size()); | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 1981 | while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1982 | // We found two constants, fold them together! | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1983 | ConstantInt *Fold = ConstantInt::get(getContext(), | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1984 | APIntOps::umax(LHSC->getValue()->getValue(), | 
|  | 1985 | RHSC->getValue()->getValue())); | 
|  | 1986 | Ops[0] = getConstant(Fold); | 
|  | 1987 | Ops.erase(Ops.begin()+1);  // Erase the folded element | 
|  | 1988 | if (Ops.size() == 1) return Ops[0]; | 
|  | 1989 | LHSC = cast<SCEVConstant>(Ops[0]); | 
|  | 1990 | } | 
|  | 1991 |  | 
| Dan Gohman | f57bdb7 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1992 | // If we are left with a constant minimum-int, strip it off. | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1993 | if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) { | 
|  | 1994 | Ops.erase(Ops.begin()); | 
|  | 1995 | --Idx; | 
| Dan Gohman | f57bdb7 | 2009-06-24 14:46:22 +0000 | [diff] [blame] | 1996 | } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) { | 
|  | 1997 | // If we have an umax with a constant maximum-int, it will always be | 
|  | 1998 | // maximum-int. | 
|  | 1999 | return Ops[0]; | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2000 | } | 
|  | 2001 | } | 
|  | 2002 |  | 
|  | 2003 | if (Ops.size() == 1) return Ops[0]; | 
|  | 2004 |  | 
|  | 2005 | // Find the first UMax | 
|  | 2006 | while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr) | 
|  | 2007 | ++Idx; | 
|  | 2008 |  | 
|  | 2009 | // Check to see if one of the operands is a UMax. If so, expand its operands | 
|  | 2010 | // onto our operand list, and recurse to simplify. | 
|  | 2011 | if (Idx < Ops.size()) { | 
|  | 2012 | bool DeletedUMax = false; | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2013 | while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) { | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2014 | Ops.insert(Ops.end(), UMax->op_begin(), UMax->op_end()); | 
|  | 2015 | Ops.erase(Ops.begin()+Idx); | 
|  | 2016 | DeletedUMax = true; | 
|  | 2017 | } | 
|  | 2018 |  | 
|  | 2019 | if (DeletedUMax) | 
|  | 2020 | return getUMaxExpr(Ops); | 
|  | 2021 | } | 
|  | 2022 |  | 
|  | 2023 | // Okay, check to see if the same value occurs in the operand list twice.  If | 
|  | 2024 | // so, delete one.  Since we sorted the list, these values are required to | 
|  | 2025 | // be adjacent. | 
|  | 2026 | for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) | 
|  | 2027 | if (Ops[i] == Ops[i+1]) {      //  X umax Y umax Y  -->  X umax Y | 
|  | 2028 | Ops.erase(Ops.begin()+i, Ops.begin()+i+1); | 
|  | 2029 | --i; --e; | 
|  | 2030 | } | 
|  | 2031 |  | 
|  | 2032 | if (Ops.size() == 1) return Ops[0]; | 
|  | 2033 |  | 
|  | 2034 | assert(!Ops.empty() && "Reduced umax down to nothing!"); | 
|  | 2035 |  | 
|  | 2036 | // Okay, it looks like we really DO need a umax expr.  Check to see if we | 
|  | 2037 | // already have one, otherwise create a new one. | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2038 | FoldingSetNodeID ID; | 
|  | 2039 | ID.AddInteger(scUMaxExpr); | 
|  | 2040 | ID.AddInteger(Ops.size()); | 
|  | 2041 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) | 
|  | 2042 | ID.AddPointer(Ops[i]); | 
|  | 2043 | void *IP = 0; | 
|  | 2044 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; | 
|  | 2045 | SCEV *S = SCEVAllocator.Allocate<SCEVUMaxExpr>(); | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 2046 | new (S) SCEVUMaxExpr(ID, Ops); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2047 | UniqueSCEVs.InsertNode(S, IP); | 
|  | 2048 | return S; | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2049 | } | 
|  | 2050 |  | 
| Dan Gohman | abd1709 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2051 | const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS, | 
|  | 2052 | const SCEV *RHS) { | 
| Dan Gohman | 692b468 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2053 | // ~smax(~x, ~y) == smin(x, y). | 
|  | 2054 | return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); | 
|  | 2055 | } | 
|  | 2056 |  | 
| Dan Gohman | abd1709 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2057 | const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS, | 
|  | 2058 | const SCEV *RHS) { | 
| Dan Gohman | 692b468 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 2059 | // ~umax(~x, ~y) == umin(x, y) | 
|  | 2060 | return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); | 
|  | 2061 | } | 
|  | 2062 |  | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2063 | const SCEV *ScalarEvolution::getFieldOffsetExpr(const StructType *STy, | 
|  | 2064 | unsigned FieldNo) { | 
|  | 2065 | // If we have TargetData we can determine the constant offset. | 
|  | 2066 | if (TD) { | 
|  | 2067 | const Type *IntPtrTy = TD->getIntPtrType(getContext()); | 
|  | 2068 | const StructLayout &SL = *TD->getStructLayout(STy); | 
|  | 2069 | uint64_t Offset = SL.getElementOffset(FieldNo); | 
|  | 2070 | return getIntegerSCEV(Offset, IntPtrTy); | 
|  | 2071 | } | 
|  | 2072 |  | 
|  | 2073 | // Field 0 is always at offset 0. | 
|  | 2074 | if (FieldNo == 0) { | 
|  | 2075 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy)); | 
|  | 2076 | return getIntegerSCEV(0, Ty); | 
|  | 2077 | } | 
|  | 2078 |  | 
|  | 2079 | // Okay, it looks like we really DO need an offsetof expr.  Check to see if we | 
|  | 2080 | // already have one, otherwise create a new one. | 
|  | 2081 | FoldingSetNodeID ID; | 
|  | 2082 | ID.AddInteger(scFieldOffset); | 
|  | 2083 | ID.AddPointer(STy); | 
|  | 2084 | ID.AddInteger(FieldNo); | 
|  | 2085 | void *IP = 0; | 
|  | 2086 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; | 
|  | 2087 | SCEV *S = SCEVAllocator.Allocate<SCEVFieldOffsetExpr>(); | 
|  | 2088 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy)); | 
|  | 2089 | new (S) SCEVFieldOffsetExpr(ID, Ty, STy, FieldNo); | 
|  | 2090 | UniqueSCEVs.InsertNode(S, IP); | 
|  | 2091 | return S; | 
|  | 2092 | } | 
|  | 2093 |  | 
|  | 2094 | const SCEV *ScalarEvolution::getAllocSizeExpr(const Type *AllocTy) { | 
|  | 2095 | // If we have TargetData we can determine the constant size. | 
|  | 2096 | if (TD && AllocTy->isSized()) { | 
|  | 2097 | const Type *IntPtrTy = TD->getIntPtrType(getContext()); | 
|  | 2098 | return getIntegerSCEV(TD->getTypeAllocSize(AllocTy), IntPtrTy); | 
|  | 2099 | } | 
|  | 2100 |  | 
|  | 2101 | // Expand an array size into the element size times the number | 
|  | 2102 | // of elements. | 
|  | 2103 | if (const ArrayType *ATy = dyn_cast<ArrayType>(AllocTy)) { | 
|  | 2104 | const SCEV *E = getAllocSizeExpr(ATy->getElementType()); | 
|  | 2105 | return getMulExpr( | 
|  | 2106 | E, getConstant(ConstantInt::get(cast<IntegerType>(E->getType()), | 
|  | 2107 | ATy->getNumElements()))); | 
|  | 2108 | } | 
|  | 2109 |  | 
|  | 2110 | // Expand a vector size into the element size times the number | 
|  | 2111 | // of elements. | 
|  | 2112 | if (const VectorType *VTy = dyn_cast<VectorType>(AllocTy)) { | 
|  | 2113 | const SCEV *E = getAllocSizeExpr(VTy->getElementType()); | 
|  | 2114 | return getMulExpr( | 
|  | 2115 | E, getConstant(ConstantInt::get(cast<IntegerType>(E->getType()), | 
|  | 2116 | VTy->getNumElements()))); | 
|  | 2117 | } | 
|  | 2118 |  | 
|  | 2119 | // Okay, it looks like we really DO need a sizeof expr.  Check to see if we | 
|  | 2120 | // already have one, otherwise create a new one. | 
|  | 2121 | FoldingSetNodeID ID; | 
|  | 2122 | ID.AddInteger(scAllocSize); | 
|  | 2123 | ID.AddPointer(AllocTy); | 
|  | 2124 | void *IP = 0; | 
|  | 2125 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; | 
|  | 2126 | SCEV *S = SCEVAllocator.Allocate<SCEVAllocSizeExpr>(); | 
|  | 2127 | const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy)); | 
|  | 2128 | new (S) SCEVAllocSizeExpr(ID, Ty, AllocTy); | 
|  | 2129 | UniqueSCEVs.InsertNode(S, IP); | 
|  | 2130 | return S; | 
|  | 2131 | } | 
|  | 2132 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2133 | const SCEV *ScalarEvolution::getUnknown(Value *V) { | 
| Dan Gohman | f436bac | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2134 | // Don't attempt to do anything other than create a SCEVUnknown object | 
|  | 2135 | // here.  createSCEV only calls getUnknown after checking for all other | 
|  | 2136 | // interesting possibilities, and any other code that calls getUnknown | 
|  | 2137 | // is doing so in order to hide a value from SCEV canonicalization. | 
|  | 2138 |  | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2139 | FoldingSetNodeID ID; | 
|  | 2140 | ID.AddInteger(scUnknown); | 
|  | 2141 | ID.AddPointer(V); | 
|  | 2142 | void *IP = 0; | 
|  | 2143 | if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; | 
|  | 2144 | SCEV *S = SCEVAllocator.Allocate<SCEVUnknown>(); | 
| Dan Gohman | 3a302cb | 2009-07-13 20:50:19 +0000 | [diff] [blame] | 2145 | new (S) SCEVUnknown(ID, V); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2146 | UniqueSCEVs.InsertNode(S, IP); | 
|  | 2147 | return S; | 
| Chris Lattner | b4f681b | 2004-04-15 15:07:24 +0000 | [diff] [blame] | 2148 | } | 
|  | 2149 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2150 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2151 | //            Basic SCEV Analysis and PHI Idiom Recognition Code | 
|  | 2152 | // | 
|  | 2153 |  | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2154 | /// isSCEVable - Test if values of the given type are analyzable within | 
|  | 2155 | /// the SCEV framework. This primarily includes integer types, and it | 
|  | 2156 | /// can optionally include pointer types if the ScalarEvolution class | 
|  | 2157 | /// has access to target-specific information. | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2158 | bool ScalarEvolution::isSCEVable(const Type *Ty) const { | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2159 | // Integers and pointers are always SCEVable. | 
|  | 2160 | return Ty->isInteger() || isa<PointerType>(Ty); | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2161 | } | 
|  | 2162 |  | 
|  | 2163 | /// getTypeSizeInBits - Return the size in bits of the specified type, | 
|  | 2164 | /// for which isSCEVable must return true. | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2165 | uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const { | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2166 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); | 
|  | 2167 |  | 
|  | 2168 | // If we have a TargetData, use it! | 
|  | 2169 | if (TD) | 
|  | 2170 | return TD->getTypeSizeInBits(Ty); | 
|  | 2171 |  | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2172 | // Integer types have fixed sizes. | 
|  | 2173 | if (Ty->isInteger()) | 
|  | 2174 | return Ty->getPrimitiveSizeInBits(); | 
|  | 2175 |  | 
|  | 2176 | // The only other support type is pointer. Without TargetData, conservatively | 
|  | 2177 | // assume pointers are 64-bit. | 
|  | 2178 | assert(isa<PointerType>(Ty) && "isSCEVable permitted a non-SCEVable type!"); | 
|  | 2179 | return 64; | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2180 | } | 
|  | 2181 |  | 
|  | 2182 | /// getEffectiveSCEVType - Return a type with the same bitwidth as | 
|  | 2183 | /// the given type and which represents how SCEV will treat the given | 
|  | 2184 | /// type, for which isSCEVable must return true. For pointer types, | 
|  | 2185 | /// this is the pointer-sized integer type. | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2186 | const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const { | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2187 | assert(isSCEVable(Ty) && "Type is not SCEVable!"); | 
|  | 2188 |  | 
|  | 2189 | if (Ty->isInteger()) | 
|  | 2190 | return Ty; | 
|  | 2191 |  | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2192 | // The only other support type is pointer. | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2193 | assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!"); | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2194 | if (TD) return TD->getIntPtrType(getContext()); | 
|  | 2195 |  | 
|  | 2196 | // Without TargetData, conservatively assume pointers are 64-bit. | 
|  | 2197 | return Type::getInt64Ty(getContext()); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2198 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2199 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2200 | const SCEV *ScalarEvolution::getCouldNotCompute() { | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 2201 | return &CouldNotCompute; | 
| Dan Gohman | 31efa30 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 2202 | } | 
|  | 2203 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2204 | /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the | 
|  | 2205 | /// expression and create a new one. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2206 | const SCEV *ScalarEvolution::getSCEV(Value *V) { | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2207 | assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2208 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2209 | std::map<SCEVCallbackVH, const SCEV *>::iterator I = Scalars.find(V); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2210 | if (I != Scalars.end()) return I->second; | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2211 | const SCEV *S = createSCEV(V); | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2212 | Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S)); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2213 | return S; | 
|  | 2214 | } | 
|  | 2215 |  | 
| Dan Gohman | f436bac | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2216 | /// getIntegerSCEV - Given a SCEVable type, create a constant for the | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2217 | /// specified signed integer value and return a SCEV for the constant. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2218 | const SCEV *ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) { | 
| Dan Gohman | f436bac | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2219 | const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty)); | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2220 | return getConstant(ConstantInt::get(ITy, Val)); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2221 | } | 
|  | 2222 |  | 
|  | 2223 | /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V | 
|  | 2224 | /// | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2225 | const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) { | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2226 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) | 
| Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2227 | return getConstant( | 
| Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2228 | cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue()))); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2229 |  | 
|  | 2230 | const Type *Ty = V->getType(); | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2231 | Ty = getEffectiveSCEVType(Ty); | 
| Owen Anderson | 542619e | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2232 | return getMulExpr(V, | 
| Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2233 | getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty)))); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2234 | } | 
|  | 2235 |  | 
|  | 2236 | /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2237 | const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) { | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2238 | if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) | 
| Owen Anderson | 542619e | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2239 | return getConstant( | 
| Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2240 | cast<ConstantInt>(ConstantExpr::getNot(VC->getValue()))); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2241 |  | 
|  | 2242 | const Type *Ty = V->getType(); | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2243 | Ty = getEffectiveSCEVType(Ty); | 
| Owen Anderson | 542619e | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2244 | const SCEV *AllOnes = | 
| Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2245 | getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2246 | return getMinusSCEV(AllOnes, V); | 
|  | 2247 | } | 
|  | 2248 |  | 
|  | 2249 | /// getMinusSCEV - Return a SCEV corresponding to LHS - RHS. | 
|  | 2250 | /// | 
| Dan Gohman | abd1709 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2251 | const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, | 
|  | 2252 | const SCEV *RHS) { | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2253 | // X - Y --> X + -Y | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2254 | return getAddExpr(LHS, getNegativeSCEV(RHS)); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2255 | } | 
|  | 2256 |  | 
|  | 2257 | /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the | 
|  | 2258 | /// input value to the specified type.  If the type must be extended, it is zero | 
|  | 2259 | /// extended. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2260 | const SCEV * | 
|  | 2261 | ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, | 
| Nick Lewycky | b4d9f7a | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2262 | const Type *Ty) { | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2263 | const Type *SrcTy = V->getType(); | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2264 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && | 
|  | 2265 | (Ty->isInteger() || isa<PointerType>(Ty)) && | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2266 | "Cannot truncate or zero extend with non-integer arguments!"); | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2267 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2268 | return V;  // No conversion | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2269 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2270 | return getTruncateExpr(V, Ty); | 
|  | 2271 | return getZeroExtendExpr(V, Ty); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2272 | } | 
|  | 2273 |  | 
|  | 2274 | /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the | 
|  | 2275 | /// input value to the specified type.  If the type must be extended, it is sign | 
|  | 2276 | /// extended. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2277 | const SCEV * | 
|  | 2278 | ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, | 
| Nick Lewycky | b4d9f7a | 2009-04-23 05:15:08 +0000 | [diff] [blame] | 2279 | const Type *Ty) { | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2280 | const Type *SrcTy = V->getType(); | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2281 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && | 
|  | 2282 | (Ty->isInteger() || isa<PointerType>(Ty)) && | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2283 | "Cannot truncate or zero extend with non-integer arguments!"); | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2284 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2285 | return V;  // No conversion | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2286 | if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2287 | return getTruncateExpr(V, Ty); | 
|  | 2288 | return getSignExtendExpr(V, Ty); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2289 | } | 
|  | 2290 |  | 
| Dan Gohman | e712a2f | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2291 | /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the | 
|  | 2292 | /// input value to the specified type.  If the type must be extended, it is zero | 
|  | 2293 | /// extended.  The conversion must not be narrowing. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2294 | const SCEV * | 
|  | 2295 | ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, const Type *Ty) { | 
| Dan Gohman | e712a2f | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2296 | const Type *SrcTy = V->getType(); | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2297 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && | 
|  | 2298 | (Ty->isInteger() || isa<PointerType>(Ty)) && | 
| Dan Gohman | e712a2f | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2299 | "Cannot noop or zero extend with non-integer arguments!"); | 
|  | 2300 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && | 
|  | 2301 | "getNoopOrZeroExtend cannot truncate!"); | 
|  | 2302 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) | 
|  | 2303 | return V;  // No conversion | 
|  | 2304 | return getZeroExtendExpr(V, Ty); | 
|  | 2305 | } | 
|  | 2306 |  | 
|  | 2307 | /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the | 
|  | 2308 | /// input value to the specified type.  If the type must be extended, it is sign | 
|  | 2309 | /// extended.  The conversion must not be narrowing. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2310 | const SCEV * | 
|  | 2311 | ScalarEvolution::getNoopOrSignExtend(const SCEV *V, const Type *Ty) { | 
| Dan Gohman | e712a2f | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2312 | const Type *SrcTy = V->getType(); | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2313 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && | 
|  | 2314 | (Ty->isInteger() || isa<PointerType>(Ty)) && | 
| Dan Gohman | e712a2f | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2315 | "Cannot noop or sign extend with non-integer arguments!"); | 
|  | 2316 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && | 
|  | 2317 | "getNoopOrSignExtend cannot truncate!"); | 
|  | 2318 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) | 
|  | 2319 | return V;  // No conversion | 
|  | 2320 | return getSignExtendExpr(V, Ty); | 
|  | 2321 | } | 
|  | 2322 |  | 
| Dan Gohman | 8db2edc | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2323 | /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of | 
|  | 2324 | /// the input value to the specified type. If the type must be extended, | 
|  | 2325 | /// it is extended with unspecified bits. The conversion must not be | 
|  | 2326 | /// narrowing. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2327 | const SCEV * | 
|  | 2328 | ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, const Type *Ty) { | 
| Dan Gohman | 8db2edc | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2329 | const Type *SrcTy = V->getType(); | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2330 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && | 
|  | 2331 | (Ty->isInteger() || isa<PointerType>(Ty)) && | 
| Dan Gohman | 8db2edc | 2009-06-13 15:56:47 +0000 | [diff] [blame] | 2332 | "Cannot noop or any extend with non-integer arguments!"); | 
|  | 2333 | assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && | 
|  | 2334 | "getNoopOrAnyExtend cannot truncate!"); | 
|  | 2335 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) | 
|  | 2336 | return V;  // No conversion | 
|  | 2337 | return getAnyExtendExpr(V, Ty); | 
|  | 2338 | } | 
|  | 2339 |  | 
| Dan Gohman | e712a2f | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2340 | /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the | 
|  | 2341 | /// input value to the specified type.  The conversion must not be widening. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2342 | const SCEV * | 
|  | 2343 | ScalarEvolution::getTruncateOrNoop(const SCEV *V, const Type *Ty) { | 
| Dan Gohman | e712a2f | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2344 | const Type *SrcTy = V->getType(); | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2345 | assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) && | 
|  | 2346 | (Ty->isInteger() || isa<PointerType>(Ty)) && | 
| Dan Gohman | e712a2f | 2009-05-13 03:46:30 +0000 | [diff] [blame] | 2347 | "Cannot truncate or noop with non-integer arguments!"); | 
|  | 2348 | assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) && | 
|  | 2349 | "getTruncateOrNoop cannot extend!"); | 
|  | 2350 | if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) | 
|  | 2351 | return V;  // No conversion | 
|  | 2352 | return getTruncateExpr(V, Ty); | 
|  | 2353 | } | 
|  | 2354 |  | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2355 | /// getUMaxFromMismatchedTypes - Promote the operands to the wider of | 
|  | 2356 | /// the types using zero-extension, and then perform a umax operation | 
|  | 2357 | /// with them. | 
| Dan Gohman | abd1709 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2358 | const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS, | 
|  | 2359 | const SCEV *RHS) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2360 | const SCEV *PromotedLHS = LHS; | 
|  | 2361 | const SCEV *PromotedRHS = RHS; | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 2362 |  | 
|  | 2363 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) | 
|  | 2364 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); | 
|  | 2365 | else | 
|  | 2366 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); | 
|  | 2367 |  | 
|  | 2368 | return getUMaxExpr(PromotedLHS, PromotedRHS); | 
|  | 2369 | } | 
|  | 2370 |  | 
| Dan Gohman | 2bc2230 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2371 | /// getUMinFromMismatchedTypes - Promote the operands to the wider of | 
|  | 2372 | /// the types using zero-extension, and then perform a umin operation | 
|  | 2373 | /// with them. | 
| Dan Gohman | abd1709 | 2009-06-24 14:49:00 +0000 | [diff] [blame] | 2374 | const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS, | 
|  | 2375 | const SCEV *RHS) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2376 | const SCEV *PromotedLHS = LHS; | 
|  | 2377 | const SCEV *PromotedRHS = RHS; | 
| Dan Gohman | 2bc2230 | 2009-06-22 15:03:27 +0000 | [diff] [blame] | 2378 |  | 
|  | 2379 | if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) | 
|  | 2380 | PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); | 
|  | 2381 | else | 
|  | 2382 | PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); | 
|  | 2383 |  | 
|  | 2384 | return getUMinExpr(PromotedLHS, PromotedRHS); | 
|  | 2385 | } | 
|  | 2386 |  | 
| Dan Gohman | 0b89dff | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2387 | /// PushDefUseChildren - Push users of the given Instruction | 
|  | 2388 | /// onto the given Worklist. | 
|  | 2389 | static void | 
|  | 2390 | PushDefUseChildren(Instruction *I, | 
|  | 2391 | SmallVectorImpl<Instruction *> &Worklist) { | 
|  | 2392 | // Push the def-use children onto the Worklist stack. | 
|  | 2393 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); | 
|  | 2394 | UI != UE; ++UI) | 
|  | 2395 | Worklist.push_back(cast<Instruction>(UI)); | 
|  | 2396 | } | 
|  | 2397 |  | 
|  | 2398 | /// ForgetSymbolicValue - This looks up computed SCEV values for all | 
|  | 2399 | /// instructions that depend on the given instruction and removes them from | 
|  | 2400 | /// the Scalars map if they reference SymName. This is used during PHI | 
|  | 2401 | /// resolution. | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2402 | void | 
| Dan Gohman | 0b89dff | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2403 | ScalarEvolution::ForgetSymbolicName(Instruction *I, const SCEV *SymName) { | 
|  | 2404 | SmallVector<Instruction *, 16> Worklist; | 
|  | 2405 | PushDefUseChildren(I, Worklist); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2406 |  | 
| Dan Gohman | 0b89dff | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2407 | SmallPtrSet<Instruction *, 8> Visited; | 
|  | 2408 | Visited.insert(I); | 
|  | 2409 | while (!Worklist.empty()) { | 
|  | 2410 | Instruction *I = Worklist.pop_back_val(); | 
|  | 2411 | if (!Visited.insert(I)) continue; | 
| Chris Lattner | 7b0fbe7 | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2412 |  | 
| Dan Gohman | 0b89dff | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2413 | std::map<SCEVCallbackVH, const SCEV*>::iterator It = | 
|  | 2414 | Scalars.find(static_cast<Value *>(I)); | 
|  | 2415 | if (It != Scalars.end()) { | 
|  | 2416 | // Short-circuit the def-use traversal if the symbolic name | 
|  | 2417 | // ceases to appear in expressions. | 
|  | 2418 | if (!It->second->hasOperand(SymName)) | 
|  | 2419 | continue; | 
| Chris Lattner | 7b0fbe7 | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2420 |  | 
| Dan Gohman | 0b89dff | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2421 | // SCEVUnknown for a PHI either means that it has an unrecognized | 
|  | 2422 | // structure, or it's a PHI that's in the progress of being computed | 
|  | 2423 | // by createNodeForPHI.  In the former case, additional loop trip | 
|  | 2424 | // count information isn't going to change anything. In the later | 
|  | 2425 | // case, createNodeForPHI will perform the necessary updates on its | 
|  | 2426 | // own when it gets to that point. | 
| Dan Gohman | cc2f1eb | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 2427 | if (!isa<PHINode>(I) || !isa<SCEVUnknown>(It->second)) { | 
|  | 2428 | ValuesAtScopes.erase(It->second); | 
| Dan Gohman | 0b89dff | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2429 | Scalars.erase(It); | 
| Dan Gohman | cc2f1eb | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 2430 | } | 
| Dan Gohman | 0b89dff | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2431 | } | 
|  | 2432 |  | 
|  | 2433 | PushDefUseChildren(I, Worklist); | 
|  | 2434 | } | 
| Chris Lattner | 7b0fbe7 | 2005-02-13 04:37:18 +0000 | [diff] [blame] | 2435 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2436 |  | 
|  | 2437 | /// createNodeForPHI - PHI nodes have two cases.  Either the PHI node exists in | 
|  | 2438 | /// a loop header, making it a potential recurrence, or it doesn't. | 
|  | 2439 | /// | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2440 | const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2441 | if (PN->getNumIncomingValues() == 2)  // The loops have been canonicalized. | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2442 | if (const Loop *L = LI->getLoopFor(PN->getParent())) | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2443 | if (L->getHeader() == PN->getParent()) { | 
|  | 2444 | // If it lives in the loop header, it has two incoming values, one | 
|  | 2445 | // from outside the loop, and one from inside. | 
|  | 2446 | unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0)); | 
|  | 2447 | unsigned BackEdge     = IncomingEdge^1; | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2448 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2449 | // While we are analyzing this PHI node, handle its value symbolically. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2450 | const SCEV *SymbolicName = getUnknown(PN); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2451 | assert(Scalars.find(PN) == Scalars.end() && | 
|  | 2452 | "PHI node already processed?"); | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 2453 | Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName)); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2454 |  | 
|  | 2455 | // Using this symbolic name for the PHI, analyze the value coming around | 
|  | 2456 | // the back-edge. | 
| Dan Gohman | 0b89dff | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2457 | Value *BEValueV = PN->getIncomingValue(BackEdge); | 
|  | 2458 | const SCEV *BEValue = getSCEV(BEValueV); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2459 |  | 
|  | 2460 | // NOTE: If BEValue is loop invariant, we know that the PHI node just | 
|  | 2461 | // has a special value for the first iteration of the loop. | 
|  | 2462 |  | 
|  | 2463 | // If the value coming around the backedge is an add with the symbolic | 
|  | 2464 | // value we just inserted, then we found a simple induction variable! | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2465 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2466 | // If there is a single occurrence of the symbolic value, replace it | 
|  | 2467 | // with a recurrence. | 
|  | 2468 | unsigned FoundIndex = Add->getNumOperands(); | 
|  | 2469 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) | 
|  | 2470 | if (Add->getOperand(i) == SymbolicName) | 
|  | 2471 | if (FoundIndex == e) { | 
|  | 2472 | FoundIndex = i; | 
|  | 2473 | break; | 
|  | 2474 | } | 
|  | 2475 |  | 
|  | 2476 | if (FoundIndex != Add->getNumOperands()) { | 
|  | 2477 | // Create an add with everything but the specified operand. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2478 | SmallVector<const SCEV *, 8> Ops; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2479 | for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) | 
|  | 2480 | if (i != FoundIndex) | 
|  | 2481 | Ops.push_back(Add->getOperand(i)); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2482 | const SCEV *Accum = getAddExpr(Ops); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2483 |  | 
|  | 2484 | // This is not a valid addrec if the step amount is varying each | 
|  | 2485 | // loop iteration, but is not itself an addrec in this loop. | 
|  | 2486 | if (Accum->isLoopInvariant(L) || | 
|  | 2487 | (isa<SCEVAddRecExpr>(Accum) && | 
|  | 2488 | cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) { | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 2489 | const SCEV *StartVal = | 
|  | 2490 | getSCEV(PN->getIncomingValue(IncomingEdge)); | 
| Dan Gohman | 62ef6a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2491 | const SCEVAddRecExpr *PHISCEV = | 
|  | 2492 | cast<SCEVAddRecExpr>(getAddRecExpr(StartVal, Accum, L)); | 
|  | 2493 |  | 
|  | 2494 | // If the increment doesn't overflow, then neither the addrec nor the | 
|  | 2495 | // post-increment will overflow. | 
|  | 2496 | if (const AddOperator *OBO = dyn_cast<AddOperator>(BEValueV)) | 
|  | 2497 | if (OBO->getOperand(0) == PN && | 
|  | 2498 | getSCEV(OBO->getOperand(1)) == | 
|  | 2499 | PHISCEV->getStepRecurrence(*this)) { | 
|  | 2500 | const SCEVAddRecExpr *PostInc = PHISCEV->getPostIncExpr(*this); | 
| Dan Gohman | 16f5415 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 2501 | if (OBO->hasNoUnsignedWrap()) { | 
| Dan Gohman | 62ef6a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2502 | const_cast<SCEVAddRecExpr *>(PHISCEV) | 
| Dan Gohman | 16f5415 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 2503 | ->setHasNoUnsignedWrap(true); | 
| Dan Gohman | 62ef6a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2504 | const_cast<SCEVAddRecExpr *>(PostInc) | 
| Dan Gohman | 16f5415 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 2505 | ->setHasNoUnsignedWrap(true); | 
| Dan Gohman | 62ef6a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2506 | } | 
| Dan Gohman | 16f5415 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 2507 | if (OBO->hasNoSignedWrap()) { | 
| Dan Gohman | 62ef6a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2508 | const_cast<SCEVAddRecExpr *>(PHISCEV) | 
| Dan Gohman | 16f5415 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 2509 | ->setHasNoSignedWrap(true); | 
| Dan Gohman | 62ef6a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2510 | const_cast<SCEVAddRecExpr *>(PostInc) | 
| Dan Gohman | 16f5415 | 2009-08-20 17:11:38 +0000 | [diff] [blame] | 2511 | ->setHasNoSignedWrap(true); | 
| Dan Gohman | 62ef6a7 | 2009-07-25 01:22:26 +0000 | [diff] [blame] | 2512 | } | 
|  | 2513 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2514 |  | 
|  | 2515 | // Okay, for the entire analysis of this edge we assumed the PHI | 
| Dan Gohman | 0b89dff | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2516 | // to be symbolic.  We now need to go back and purge all of the | 
|  | 2517 | // entries for the scalars that use the symbolic expression. | 
|  | 2518 | ForgetSymbolicName(PN, SymbolicName); | 
|  | 2519 | Scalars[SCEVCallbackVH(PN, this)] = PHISCEV; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2520 | return PHISCEV; | 
|  | 2521 | } | 
|  | 2522 | } | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2523 | } else if (const SCEVAddRecExpr *AddRec = | 
|  | 2524 | dyn_cast<SCEVAddRecExpr>(BEValue)) { | 
| Chris Lattner | e8cbdbf | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2525 | // Otherwise, this could be a loop like this: | 
|  | 2526 | //     i = 0;  for (j = 1; ..; ++j) { ....  i = j; } | 
|  | 2527 | // In this case, j = {1,+,1}  and BEValue is j. | 
|  | 2528 | // Because the other in-value of i (0) fits the evolution of BEValue | 
|  | 2529 | // i really is an addrec evolution. | 
|  | 2530 | if (AddRec->getLoop() == L && AddRec->isAffine()) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2531 | const SCEV *StartVal = getSCEV(PN->getIncomingValue(IncomingEdge)); | 
| Chris Lattner | e8cbdbf | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2532 |  | 
|  | 2533 | // If StartVal = j.start - j.stride, we can use StartVal as the | 
|  | 2534 | // initial step of the addrec evolution. | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2535 | if (StartVal == getMinusSCEV(AddRec->getOperand(0), | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 2536 | AddRec->getOperand(1))) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2537 | const SCEV *PHISCEV = | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2538 | getAddRecExpr(StartVal, AddRec->getOperand(1), L); | 
| Chris Lattner | e8cbdbf | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2539 |  | 
|  | 2540 | // Okay, for the entire analysis of this edge we assumed the PHI | 
| Dan Gohman | 0b89dff | 2009-07-25 01:13:03 +0000 | [diff] [blame] | 2541 | // to be symbolic.  We now need to go back and purge all of the | 
|  | 2542 | // entries for the scalars that use the symbolic expression. | 
|  | 2543 | ForgetSymbolicName(PN, SymbolicName); | 
|  | 2544 | Scalars[SCEVCallbackVH(PN, this)] = PHISCEV; | 
| Chris Lattner | e8cbdbf | 2006-04-26 18:34:07 +0000 | [diff] [blame] | 2545 | return PHISCEV; | 
|  | 2546 | } | 
|  | 2547 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2548 | } | 
|  | 2549 |  | 
|  | 2550 | return SymbolicName; | 
|  | 2551 | } | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2552 |  | 
| Dan Gohman | 92b4c7f3 | 2009-07-14 14:06:25 +0000 | [diff] [blame] | 2553 | // It's tempting to recognize PHIs with a unique incoming value, however | 
|  | 2554 | // this leads passes like indvars to break LCSSA form. Fortunately, such | 
|  | 2555 | // PHIs are rare, as instcombine zaps them. | 
|  | 2556 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2557 | // If it's not a loop phi, we can't handle it yet. | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2558 | return getUnknown(PN); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2559 | } | 
|  | 2560 |  | 
| Dan Gohman | ee750d1 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2561 | /// createNodeForGEP - Expand GEP instructions into add and multiply | 
|  | 2562 | /// operations. This allows them to be analyzed by regular SCEV code. | 
|  | 2563 | /// | 
| Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 2564 | const SCEV *ScalarEvolution::createNodeForGEP(Operator *GEP) { | 
| Dan Gohman | ee750d1 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2565 |  | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2566 | const Type *IntPtrTy = getEffectiveSCEVType(GEP->getType()); | 
| Dan Gohman | 2173bd3 | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2567 | Value *Base = GEP->getOperand(0); | 
| Dan Gohman | 30f24fe | 2009-05-09 00:14:52 +0000 | [diff] [blame] | 2568 | // Don't attempt to analyze GEPs over unsized objects. | 
|  | 2569 | if (!cast<PointerType>(Base->getType())->getElementType()->isSized()) | 
|  | 2570 | return getUnknown(GEP); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2571 | const SCEV *TotalOffset = getIntegerSCEV(0, IntPtrTy); | 
| Dan Gohman | 2173bd3 | 2009-05-08 20:36:47 +0000 | [diff] [blame] | 2572 | gep_type_iterator GTI = gep_type_begin(GEP); | 
|  | 2573 | for (GetElementPtrInst::op_iterator I = next(GEP->op_begin()), | 
|  | 2574 | E = GEP->op_end(); | 
| Dan Gohman | ee750d1 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2575 | I != E; ++I) { | 
|  | 2576 | Value *Index = *I; | 
|  | 2577 | // Compute the (potentially symbolic) offset in bytes for this index. | 
|  | 2578 | if (const StructType *STy = dyn_cast<StructType>(*GTI++)) { | 
|  | 2579 | // For a struct, add the member offset. | 
| Dan Gohman | ee750d1 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2580 | unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue(); | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2581 | TotalOffset = getAddExpr(TotalOffset, | 
|  | 2582 | getFieldOffsetExpr(STy, FieldNo)); | 
| Dan Gohman | ee750d1 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2583 | } else { | 
|  | 2584 | // For an array, add the element offset, explicitly scaled. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2585 | const SCEV *LocalOffset = getSCEV(Index); | 
| Dan Gohman | ee750d1 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2586 | if (!isa<PointerType>(LocalOffset->getType())) | 
|  | 2587 | // Getelementptr indicies are signed. | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2588 | LocalOffset = getTruncateOrSignExtend(LocalOffset, IntPtrTy); | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 2589 | LocalOffset = getMulExpr(LocalOffset, getAllocSizeExpr(*GTI)); | 
| Dan Gohman | ee750d1 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 2590 | TotalOffset = getAddExpr(TotalOffset, LocalOffset); | 
|  | 2591 | } | 
|  | 2592 | } | 
|  | 2593 | return getAddExpr(getSCEV(Base), TotalOffset); | 
|  | 2594 | } | 
|  | 2595 |  | 
| Nick Lewycky | 3783b46 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2596 | /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is | 
|  | 2597 | /// guaranteed to end in (at every loop iteration).  It is, at the same time, | 
|  | 2598 | /// the minimum number of times S is divisible by 2.  For example, given {4,+,8} | 
|  | 2599 | /// it returns 2.  If S is guaranteed to be 0, it returns the bitwidth of S. | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2600 | uint32_t | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2601 | ScalarEvolution::GetMinTrailingZeros(const SCEV *S) { | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2602 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) | 
| Chris Lattner | 69ec1ec | 2007-11-23 22:36:49 +0000 | [diff] [blame] | 2603 | return C->getValue()->getValue().countTrailingZeros(); | 
| Chris Lattner | 49b090e | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2604 |  | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2605 | if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S)) | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2606 | return std::min(GetMinTrailingZeros(T->getOperand()), | 
|  | 2607 | (uint32_t)getTypeSizeInBits(T->getType())); | 
| Nick Lewycky | 3783b46 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2608 |  | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2609 | if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) { | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2610 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); | 
|  | 2611 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? | 
|  | 2612 | getTypeSizeInBits(E->getType()) : OpRes; | 
| Nick Lewycky | 3783b46 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2613 | } | 
|  | 2614 |  | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2615 | if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) { | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2616 | uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); | 
|  | 2617 | return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? | 
|  | 2618 | getTypeSizeInBits(E->getType()) : OpRes; | 
| Nick Lewycky | 3783b46 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2619 | } | 
|  | 2620 |  | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2621 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { | 
| Nick Lewycky | 3783b46 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2622 | // The result is the min of all operands results. | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2623 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); | 
| Nick Lewycky | 3783b46 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2624 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2625 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); | 
| Nick Lewycky | 3783b46 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2626 | return MinOpRes; | 
| Chris Lattner | 49b090e | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2627 | } | 
|  | 2628 |  | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2629 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { | 
| Nick Lewycky | 3783b46 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2630 | // The result is the sum of all operands results. | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2631 | uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0)); | 
|  | 2632 | uint32_t BitWidth = getTypeSizeInBits(M->getType()); | 
| Nick Lewycky | 3783b46 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2633 | for (unsigned i = 1, e = M->getNumOperands(); | 
|  | 2634 | SumOpRes != BitWidth && i != e; ++i) | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2635 | SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), | 
| Nick Lewycky | 3783b46 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2636 | BitWidth); | 
|  | 2637 | return SumOpRes; | 
| Chris Lattner | 49b090e | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2638 | } | 
| Nick Lewycky | 3783b46 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2639 |  | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2640 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { | 
| Nick Lewycky | 3783b46 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2641 | // The result is the min of all operands results. | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2642 | uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); | 
| Nick Lewycky | 3783b46 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2643 | for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2644 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); | 
| Nick Lewycky | 3783b46 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2645 | return MinOpRes; | 
| Chris Lattner | 49b090e | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2646 | } | 
| Nick Lewycky | 3783b46 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2647 |  | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2648 | if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) { | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2649 | // The result is the min of all operands results. | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2650 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2651 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2652 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 2653 | return MinOpRes; | 
|  | 2654 | } | 
|  | 2655 |  | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 2656 | if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) { | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2657 | // The result is the min of all operands results. | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2658 | uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2659 | for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2660 | MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 2661 | return MinOpRes; | 
|  | 2662 | } | 
|  | 2663 |  | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2664 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { | 
|  | 2665 | // For a SCEVUnknown, ask ValueTracking. | 
|  | 2666 | unsigned BitWidth = getTypeSizeInBits(U->getType()); | 
|  | 2667 | APInt Mask = APInt::getAllOnesValue(BitWidth); | 
|  | 2668 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); | 
|  | 2669 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones); | 
|  | 2670 | return Zeros.countTrailingOnes(); | 
|  | 2671 | } | 
|  | 2672 |  | 
|  | 2673 | // SCEVUDivExpr | 
| Nick Lewycky | 3783b46 | 2007-11-22 07:59:40 +0000 | [diff] [blame] | 2674 | return 0; | 
| Chris Lattner | 49b090e | 2006-12-12 02:26:09 +0000 | [diff] [blame] | 2675 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2676 |  | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2677 | /// getUnsignedRange - Determine the unsigned range for a particular SCEV. | 
|  | 2678 | /// | 
|  | 2679 | ConstantRange | 
|  | 2680 | ScalarEvolution::getUnsignedRange(const SCEV *S) { | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2681 |  | 
|  | 2682 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2683 | return ConstantRange(C->getValue()->getValue()); | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2684 |  | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2685 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { | 
|  | 2686 | ConstantRange X = getUnsignedRange(Add->getOperand(0)); | 
|  | 2687 | for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) | 
|  | 2688 | X = X.add(getUnsignedRange(Add->getOperand(i))); | 
|  | 2689 | return X; | 
|  | 2690 | } | 
|  | 2691 |  | 
|  | 2692 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { | 
|  | 2693 | ConstantRange X = getUnsignedRange(Mul->getOperand(0)); | 
|  | 2694 | for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) | 
|  | 2695 | X = X.multiply(getUnsignedRange(Mul->getOperand(i))); | 
|  | 2696 | return X; | 
|  | 2697 | } | 
|  | 2698 |  | 
|  | 2699 | if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) { | 
|  | 2700 | ConstantRange X = getUnsignedRange(SMax->getOperand(0)); | 
|  | 2701 | for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) | 
|  | 2702 | X = X.smax(getUnsignedRange(SMax->getOperand(i))); | 
|  | 2703 | return X; | 
|  | 2704 | } | 
|  | 2705 |  | 
|  | 2706 | if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) { | 
|  | 2707 | ConstantRange X = getUnsignedRange(UMax->getOperand(0)); | 
|  | 2708 | for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) | 
|  | 2709 | X = X.umax(getUnsignedRange(UMax->getOperand(i))); | 
|  | 2710 | return X; | 
|  | 2711 | } | 
|  | 2712 |  | 
|  | 2713 | if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { | 
|  | 2714 | ConstantRange X = getUnsignedRange(UDiv->getLHS()); | 
|  | 2715 | ConstantRange Y = getUnsignedRange(UDiv->getRHS()); | 
|  | 2716 | return X.udiv(Y); | 
|  | 2717 | } | 
|  | 2718 |  | 
|  | 2719 | if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { | 
|  | 2720 | ConstantRange X = getUnsignedRange(ZExt->getOperand()); | 
|  | 2721 | return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth()); | 
|  | 2722 | } | 
|  | 2723 |  | 
|  | 2724 | if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { | 
|  | 2725 | ConstantRange X = getUnsignedRange(SExt->getOperand()); | 
|  | 2726 | return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth()); | 
|  | 2727 | } | 
|  | 2728 |  | 
|  | 2729 | if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { | 
|  | 2730 | ConstantRange X = getUnsignedRange(Trunc->getOperand()); | 
|  | 2731 | return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth()); | 
|  | 2732 | } | 
|  | 2733 |  | 
|  | 2734 | ConstantRange FullSet(getTypeSizeInBits(S->getType()), true); | 
|  | 2735 |  | 
|  | 2736 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { | 
|  | 2737 | const SCEV *T = getBackedgeTakenCount(AddRec->getLoop()); | 
|  | 2738 | const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T); | 
|  | 2739 | if (!Trip) return FullSet; | 
|  | 2740 |  | 
|  | 2741 | // TODO: non-affine addrec | 
|  | 2742 | if (AddRec->isAffine()) { | 
|  | 2743 | const Type *Ty = AddRec->getType(); | 
|  | 2744 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop()); | 
|  | 2745 | if (getTypeSizeInBits(MaxBECount->getType()) <= getTypeSizeInBits(Ty)) { | 
|  | 2746 | MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty); | 
|  | 2747 |  | 
|  | 2748 | const SCEV *Start = AddRec->getStart(); | 
| Dan Gohman | 75dced0 | 2009-07-21 00:42:47 +0000 | [diff] [blame] | 2749 | const SCEV *Step = AddRec->getStepRecurrence(*this); | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2750 | const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this); | 
|  | 2751 |  | 
|  | 2752 | // Check for overflow. | 
| Dan Gohman | 75dced0 | 2009-07-21 00:42:47 +0000 | [diff] [blame] | 2753 | // TODO: This is very conservative. | 
|  | 2754 | if (!(Step->isOne() && | 
|  | 2755 | isKnownPredicate(ICmpInst::ICMP_ULT, Start, End)) && | 
|  | 2756 | !(Step->isAllOnesValue() && | 
|  | 2757 | isKnownPredicate(ICmpInst::ICMP_UGT, Start, End))) | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2758 | return FullSet; | 
|  | 2759 |  | 
|  | 2760 | ConstantRange StartRange = getUnsignedRange(Start); | 
|  | 2761 | ConstantRange EndRange = getUnsignedRange(End); | 
|  | 2762 | APInt Min = APIntOps::umin(StartRange.getUnsignedMin(), | 
|  | 2763 | EndRange.getUnsignedMin()); | 
|  | 2764 | APInt Max = APIntOps::umax(StartRange.getUnsignedMax(), | 
|  | 2765 | EndRange.getUnsignedMax()); | 
|  | 2766 | if (Min.isMinValue() && Max.isMaxValue()) | 
| Dan Gohman | d231d78 | 2009-07-20 22:41:51 +0000 | [diff] [blame] | 2767 | return FullSet; | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2768 | return ConstantRange(Min, Max+1); | 
|  | 2769 | } | 
|  | 2770 | } | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2771 | } | 
|  | 2772 |  | 
|  | 2773 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { | 
|  | 2774 | // For a SCEVUnknown, ask ValueTracking. | 
|  | 2775 | unsigned BitWidth = getTypeSizeInBits(U->getType()); | 
|  | 2776 | APInt Mask = APInt::getAllOnesValue(BitWidth); | 
|  | 2777 | APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); | 
|  | 2778 | ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD); | 
| Dan Gohman | 1a7ab94 | 2009-07-20 22:34:18 +0000 | [diff] [blame] | 2779 | if (Ones == ~Zeros + 1) | 
|  | 2780 | return FullSet; | 
|  | 2781 | return ConstantRange(Ones, ~Zeros + 1); | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2782 | } | 
|  | 2783 |  | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2784 | return FullSet; | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2785 | } | 
|  | 2786 |  | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2787 | /// getSignedRange - Determine the signed range for a particular SCEV. | 
|  | 2788 | /// | 
|  | 2789 | ConstantRange | 
|  | 2790 | ScalarEvolution::getSignedRange(const SCEV *S) { | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2791 |  | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2792 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) | 
|  | 2793 | return ConstantRange(C->getValue()->getValue()); | 
|  | 2794 |  | 
|  | 2795 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { | 
|  | 2796 | ConstantRange X = getSignedRange(Add->getOperand(0)); | 
|  | 2797 | for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) | 
|  | 2798 | X = X.add(getSignedRange(Add->getOperand(i))); | 
|  | 2799 | return X; | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2800 | } | 
|  | 2801 |  | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2802 | if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { | 
|  | 2803 | ConstantRange X = getSignedRange(Mul->getOperand(0)); | 
|  | 2804 | for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) | 
|  | 2805 | X = X.multiply(getSignedRange(Mul->getOperand(i))); | 
|  | 2806 | return X; | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2807 | } | 
|  | 2808 |  | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2809 | if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) { | 
|  | 2810 | ConstantRange X = getSignedRange(SMax->getOperand(0)); | 
|  | 2811 | for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) | 
|  | 2812 | X = X.smax(getSignedRange(SMax->getOperand(i))); | 
|  | 2813 | return X; | 
|  | 2814 | } | 
| Dan Gohman | d261d27 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2815 |  | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2816 | if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) { | 
|  | 2817 | ConstantRange X = getSignedRange(UMax->getOperand(0)); | 
|  | 2818 | for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) | 
|  | 2819 | X = X.umax(getSignedRange(UMax->getOperand(i))); | 
|  | 2820 | return X; | 
|  | 2821 | } | 
| Dan Gohman | d261d27 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2822 |  | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2823 | if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { | 
|  | 2824 | ConstantRange X = getSignedRange(UDiv->getLHS()); | 
|  | 2825 | ConstantRange Y = getSignedRange(UDiv->getRHS()); | 
|  | 2826 | return X.udiv(Y); | 
|  | 2827 | } | 
| Dan Gohman | d261d27 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2828 |  | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2829 | if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { | 
|  | 2830 | ConstantRange X = getSignedRange(ZExt->getOperand()); | 
|  | 2831 | return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth()); | 
|  | 2832 | } | 
|  | 2833 |  | 
|  | 2834 | if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { | 
|  | 2835 | ConstantRange X = getSignedRange(SExt->getOperand()); | 
|  | 2836 | return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth()); | 
|  | 2837 | } | 
|  | 2838 |  | 
|  | 2839 | if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { | 
|  | 2840 | ConstantRange X = getSignedRange(Trunc->getOperand()); | 
|  | 2841 | return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth()); | 
|  | 2842 | } | 
|  | 2843 |  | 
|  | 2844 | ConstantRange FullSet(getTypeSizeInBits(S->getType()), true); | 
|  | 2845 |  | 
|  | 2846 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { | 
|  | 2847 | const SCEV *T = getBackedgeTakenCount(AddRec->getLoop()); | 
|  | 2848 | const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T); | 
|  | 2849 | if (!Trip) return FullSet; | 
|  | 2850 |  | 
|  | 2851 | // TODO: non-affine addrec | 
|  | 2852 | if (AddRec->isAffine()) { | 
|  | 2853 | const Type *Ty = AddRec->getType(); | 
|  | 2854 | const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop()); | 
|  | 2855 | if (getTypeSizeInBits(MaxBECount->getType()) <= getTypeSizeInBits(Ty)) { | 
|  | 2856 | MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty); | 
|  | 2857 |  | 
|  | 2858 | const SCEV *Start = AddRec->getStart(); | 
|  | 2859 | const SCEV *Step = AddRec->getStepRecurrence(*this); | 
|  | 2860 | const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this); | 
|  | 2861 |  | 
|  | 2862 | // Check for overflow. | 
| Dan Gohman | 75dced0 | 2009-07-21 00:42:47 +0000 | [diff] [blame] | 2863 | // TODO: This is very conservative. | 
|  | 2864 | if (!(Step->isOne() && | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2865 | isKnownPredicate(ICmpInst::ICMP_SLT, Start, End)) && | 
| Dan Gohman | 75dced0 | 2009-07-21 00:42:47 +0000 | [diff] [blame] | 2866 | !(Step->isAllOnesValue() && | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2867 | isKnownPredicate(ICmpInst::ICMP_SGT, Start, End))) | 
|  | 2868 | return FullSet; | 
|  | 2869 |  | 
|  | 2870 | ConstantRange StartRange = getSignedRange(Start); | 
|  | 2871 | ConstantRange EndRange = getSignedRange(End); | 
|  | 2872 | APInt Min = APIntOps::smin(StartRange.getSignedMin(), | 
|  | 2873 | EndRange.getSignedMin()); | 
|  | 2874 | APInt Max = APIntOps::smax(StartRange.getSignedMax(), | 
|  | 2875 | EndRange.getSignedMax()); | 
|  | 2876 | if (Min.isMinSignedValue() && Max.isMaxSignedValue()) | 
| Dan Gohman | d571c37 | 2009-07-21 00:37:45 +0000 | [diff] [blame] | 2877 | return FullSet; | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2878 | return ConstantRange(Min, Max+1); | 
| Dan Gohman | d261d27 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2879 | } | 
| Dan Gohman | d261d27 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2880 | } | 
| Dan Gohman | d261d27 | 2009-06-24 01:05:09 +0000 | [diff] [blame] | 2881 | } | 
|  | 2882 |  | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2883 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { | 
|  | 2884 | // For a SCEVUnknown, ask ValueTracking. | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2885 | unsigned BitWidth = getTypeSizeInBits(U->getType()); | 
|  | 2886 | unsigned NS = ComputeNumSignBits(U->getValue(), TD); | 
|  | 2887 | if (NS == 1) | 
|  | 2888 | return FullSet; | 
|  | 2889 | return | 
|  | 2890 | ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1), | 
|  | 2891 | APInt::getSignedMaxValue(BitWidth).ashr(NS - 1)+1); | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2892 | } | 
|  | 2893 |  | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 2894 | return FullSet; | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2895 | } | 
|  | 2896 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2897 | /// createSCEV - We know that there is no SCEV for the specified value. | 
|  | 2898 | /// Analyze the expression. | 
|  | 2899 | /// | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2900 | const SCEV *ScalarEvolution::createSCEV(Value *V) { | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 2901 | if (!isSCEVable(V->getType())) | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2902 | return getUnknown(V); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 2903 |  | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2904 | unsigned Opcode = Instruction::UserOp1; | 
|  | 2905 | if (Instruction *I = dyn_cast<Instruction>(V)) | 
|  | 2906 | Opcode = I->getOpcode(); | 
|  | 2907 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) | 
|  | 2908 | Opcode = CE->getOpcode(); | 
| Dan Gohman | f436bac | 2009-06-24 00:54:57 +0000 | [diff] [blame] | 2909 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) | 
|  | 2910 | return getConstant(CI); | 
|  | 2911 | else if (isa<ConstantPointerNull>(V)) | 
|  | 2912 | return getIntegerSCEV(0, V->getType()); | 
|  | 2913 | else if (isa<UndefValue>(V)) | 
|  | 2914 | return getIntegerSCEV(0, V->getType()); | 
| Dan Gohman | f161e06e | 2009-08-25 17:49:57 +0000 | [diff] [blame] | 2915 | else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) | 
|  | 2916 | return GA->mayBeOverridden() ? getUnknown(V) : getSCEV(GA->getAliasee()); | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2917 | else | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2918 | return getUnknown(V); | 
| Chris Lattner | a3e0bb4 | 2007-04-02 05:41:38 +0000 | [diff] [blame] | 2919 |  | 
| Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 2920 | Operator *U = cast<Operator>(V); | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2921 | switch (Opcode) { | 
|  | 2922 | case Instruction::Add: | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2923 | return getAddExpr(getSCEV(U->getOperand(0)), | 
|  | 2924 | getSCEV(U->getOperand(1))); | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2925 | case Instruction::Mul: | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2926 | return getMulExpr(getSCEV(U->getOperand(0)), | 
|  | 2927 | getSCEV(U->getOperand(1))); | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2928 | case Instruction::UDiv: | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2929 | return getUDivExpr(getSCEV(U->getOperand(0)), | 
|  | 2930 | getSCEV(U->getOperand(1))); | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2931 | case Instruction::Sub: | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2932 | return getMinusSCEV(getSCEV(U->getOperand(0)), | 
|  | 2933 | getSCEV(U->getOperand(1))); | 
| Dan Gohman | 0ec0537 | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2934 | case Instruction::And: | 
|  | 2935 | // For an expression like x&255 that merely masks off the high bits, | 
|  | 2936 | // use zext(trunc(x)) as the SCEV expression. | 
|  | 2937 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { | 
| Dan Gohman | df19948 | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 2938 | if (CI->isNullValue()) | 
|  | 2939 | return getSCEV(U->getOperand(1)); | 
| Dan Gohman | 05c1d37 | 2009-04-27 01:41:10 +0000 | [diff] [blame] | 2940 | if (CI->isAllOnesValue()) | 
|  | 2941 | return getSCEV(U->getOperand(0)); | 
| Dan Gohman | 0ec0537 | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2942 | const APInt &A = CI->getValue(); | 
| Dan Gohman | 1ee696d | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2943 |  | 
|  | 2944 | // Instcombine's ShrinkDemandedConstant may strip bits out of | 
|  | 2945 | // constants, obscuring what would otherwise be a low-bits mask. | 
|  | 2946 | // Use ComputeMaskedBits to compute what ShrinkDemandedConstant | 
|  | 2947 | // knew about to reconstruct a low-bits mask value. | 
|  | 2948 | unsigned LZ = A.countLeadingZeros(); | 
|  | 2949 | unsigned BitWidth = A.getBitWidth(); | 
|  | 2950 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); | 
|  | 2951 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); | 
|  | 2952 | ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD); | 
|  | 2953 |  | 
|  | 2954 | APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ); | 
|  | 2955 |  | 
| Dan Gohman | 4d8723d | 2009-06-17 23:54:37 +0000 | [diff] [blame] | 2956 | if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask)) | 
| Dan Gohman | 0ec0537 | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2957 | return | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2958 | getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)), | 
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2959 | IntegerType::get(getContext(), BitWidth - LZ)), | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2960 | U->getType()); | 
| Dan Gohman | 0ec0537 | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 2961 | } | 
|  | 2962 | break; | 
| Dan Gohman | 1ee696d | 2009-06-16 19:52:01 +0000 | [diff] [blame] | 2963 |  | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2964 | case Instruction::Or: | 
|  | 2965 | // If the RHS of the Or is a constant, we may have something like: | 
|  | 2966 | // X*4+1 which got turned into X*4|1.  Handle this as an Add so loop | 
|  | 2967 | // optimizations will transparently handle this case. | 
|  | 2968 | // | 
|  | 2969 | // In order for this transformation to be safe, the LHS must be of the | 
|  | 2970 | // form X*(2^n) and the Or constant must be less than 2^n. | 
|  | 2971 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 2972 | const SCEV *LHS = getSCEV(U->getOperand(0)); | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2973 | const APInt &CIVal = CI->getValue(); | 
| Dan Gohman | c702fc0 | 2009-06-19 23:29:04 +0000 | [diff] [blame] | 2974 | if (GetMinTrailingZeros(LHS) >= | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2975 | (CIVal.getBitWidth() - CIVal.countLeadingZeros())) | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2976 | return getAddExpr(LHS, getSCEV(U->getOperand(1))); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 2977 | } | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2978 | break; | 
|  | 2979 | case Instruction::Xor: | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2980 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { | 
| Nick Lewycky | f5c547d | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2981 | // If the RHS of the xor is a signbit, then this is just an add. | 
|  | 2982 | // Instcombine turns add of signbit into xor as a strength reduction step. | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 2983 | if (CI->getValue().isSignBit()) | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2984 | return getAddExpr(getSCEV(U->getOperand(0)), | 
|  | 2985 | getSCEV(U->getOperand(1))); | 
| Nick Lewycky | f5c547d | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 2986 |  | 
|  | 2987 | // If the RHS of xor is -1, then this is a not operation. | 
| Dan Gohman | d277a1e | 2009-05-18 16:17:44 +0000 | [diff] [blame] | 2988 | if (CI->isAllOnesValue()) | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 2989 | return getNotSCEV(getSCEV(U->getOperand(0))); | 
| Dan Gohman | 6350296e | 2009-05-18 16:29:04 +0000 | [diff] [blame] | 2990 |  | 
|  | 2991 | // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask. | 
|  | 2992 | // This is a variant of the check for xor with -1, and it handles | 
|  | 2993 | // the case where instcombine has trimmed non-demanded bits out | 
|  | 2994 | // of an xor with -1. | 
|  | 2995 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0))) | 
|  | 2996 | if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1))) | 
|  | 2997 | if (BO->getOpcode() == Instruction::And && | 
|  | 2998 | LCI->getValue() == CI->getValue()) | 
|  | 2999 | if (const SCEVZeroExtendExpr *Z = | 
| Dan Gohman | b50f5a4 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 3000 | dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) { | 
| Dan Gohman | eddf771 | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 3001 | const Type *UTy = U->getType(); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3002 | const SCEV *Z0 = Z->getOperand(); | 
| Dan Gohman | eddf771 | 2009-06-18 00:00:20 +0000 | [diff] [blame] | 3003 | const Type *Z0Ty = Z0->getType(); | 
|  | 3004 | unsigned Z0TySize = getTypeSizeInBits(Z0Ty); | 
|  | 3005 |  | 
|  | 3006 | // If C is a low-bits mask, the zero extend is zerving to | 
|  | 3007 | // mask off the high bits. Complement the operand and | 
|  | 3008 | // re-apply the zext. | 
|  | 3009 | if (APIntOps::isMask(Z0TySize, CI->getValue())) | 
|  | 3010 | return getZeroExtendExpr(getNotSCEV(Z0), UTy); | 
|  | 3011 |  | 
|  | 3012 | // If C is a single bit, it may be in the sign-bit position | 
|  | 3013 | // before the zero-extend. In this case, represent the xor | 
|  | 3014 | // using an add, which is equivalent, and re-apply the zext. | 
|  | 3015 | APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize); | 
|  | 3016 | if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() && | 
|  | 3017 | Trunc.isSignBit()) | 
|  | 3018 | return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)), | 
|  | 3019 | UTy); | 
| Dan Gohman | b50f5a4 | 2009-06-17 01:22:39 +0000 | [diff] [blame] | 3020 | } | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3021 | } | 
|  | 3022 | break; | 
|  | 3023 |  | 
|  | 3024 | case Instruction::Shl: | 
|  | 3025 | // Turn shift left of a constant amount into a multiply. | 
|  | 3026 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { | 
|  | 3027 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3028 | Constant *X = ConstantInt::get(getContext(), | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3029 | APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3030 | return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X)); | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3031 | } | 
|  | 3032 | break; | 
|  | 3033 |  | 
| Nick Lewycky | f5c547d | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3034 | case Instruction::LShr: | 
| Nick Lewycky | 5234830 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 3035 | // Turn logical shift right of a constant into a unsigned divide. | 
| Nick Lewycky | f5c547d | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3036 | if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { | 
|  | 3037 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3038 | Constant *X = ConstantInt::get(getContext(), | 
| Nick Lewycky | f5c547d | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3039 | APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3040 | return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X)); | 
| Nick Lewycky | f5c547d | 2008-07-07 06:15:49 +0000 | [diff] [blame] | 3041 | } | 
|  | 3042 | break; | 
|  | 3043 |  | 
| Dan Gohman | 0ec0537 | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3044 | case Instruction::AShr: | 
|  | 3045 | // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression. | 
|  | 3046 | if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) | 
|  | 3047 | if (Instruction *L = dyn_cast<Instruction>(U->getOperand(0))) | 
|  | 3048 | if (L->getOpcode() == Instruction::Shl && | 
|  | 3049 | L->getOperand(1) == U->getOperand(1)) { | 
| Dan Gohman | df19948 | 2009-04-25 17:05:40 +0000 | [diff] [blame] | 3050 | unsigned BitWidth = getTypeSizeInBits(U->getType()); | 
|  | 3051 | uint64_t Amt = BitWidth - CI->getZExtValue(); | 
|  | 3052 | if (Amt == BitWidth) | 
|  | 3053 | return getSCEV(L->getOperand(0));       // shift by zero --> noop | 
|  | 3054 | if (Amt > BitWidth) | 
|  | 3055 | return getIntegerSCEV(0, U->getType()); // value is undefined | 
| Dan Gohman | 0ec0537 | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3056 | return | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3057 | getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)), | 
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3058 | IntegerType::get(getContext(), Amt)), | 
| Dan Gohman | 0ec0537 | 2009-04-21 02:26:00 +0000 | [diff] [blame] | 3059 | U->getType()); | 
|  | 3060 | } | 
|  | 3061 | break; | 
|  | 3062 |  | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3063 | case Instruction::Trunc: | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3064 | return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType()); | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3065 |  | 
|  | 3066 | case Instruction::ZExt: | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3067 | return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3068 |  | 
|  | 3069 | case Instruction::SExt: | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3070 | return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3071 |  | 
|  | 3072 | case Instruction::BitCast: | 
|  | 3073 | // BitCasts are no-op casts so we just eliminate the cast. | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 3074 | if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3075 | return getSCEV(U->getOperand(0)); | 
|  | 3076 | break; | 
|  | 3077 |  | 
| Dan Gohman | 33a3fd0 | 2009-07-20 17:43:30 +0000 | [diff] [blame] | 3078 | // It's tempting to handle inttoptr and ptrtoint, however this can | 
|  | 3079 | // lead to pointer expressions which cannot be expanded to GEPs | 
|  | 3080 | // (because they may overflow). For now, the only pointer-typed | 
|  | 3081 | // expressions we handle are GEPs and address literals. | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3082 |  | 
| Dan Gohman | ee750d1 | 2009-05-08 20:26:55 +0000 | [diff] [blame] | 3083 | case Instruction::GetElementPtr: | 
| Dan Gohman | c05bb94 | 2009-05-08 20:58:38 +0000 | [diff] [blame] | 3084 | return createNodeForGEP(U); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3085 |  | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3086 | case Instruction::PHI: | 
|  | 3087 | return createNodeForPHI(cast<PHINode>(U)); | 
|  | 3088 |  | 
|  | 3089 | case Instruction::Select: | 
|  | 3090 | // This could be a smax or umax that was lowered earlier. | 
|  | 3091 | // Try to recover it. | 
|  | 3092 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) { | 
|  | 3093 | Value *LHS = ICI->getOperand(0); | 
|  | 3094 | Value *RHS = ICI->getOperand(1); | 
|  | 3095 | switch (ICI->getPredicate()) { | 
|  | 3096 | case ICmpInst::ICMP_SLT: | 
|  | 3097 | case ICmpInst::ICMP_SLE: | 
|  | 3098 | std::swap(LHS, RHS); | 
|  | 3099 | // fall through | 
|  | 3100 | case ICmpInst::ICMP_SGT: | 
|  | 3101 | case ICmpInst::ICMP_SGE: | 
|  | 3102 | if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3103 | return getSMaxExpr(getSCEV(LHS), getSCEV(RHS)); | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3104 | else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) | 
| Dan Gohman | 692b468 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 3105 | return getSMinExpr(getSCEV(LHS), getSCEV(RHS)); | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3106 | break; | 
|  | 3107 | case ICmpInst::ICMP_ULT: | 
|  | 3108 | case ICmpInst::ICMP_ULE: | 
|  | 3109 | std::swap(LHS, RHS); | 
|  | 3110 | // fall through | 
|  | 3111 | case ICmpInst::ICMP_UGT: | 
|  | 3112 | case ICmpInst::ICMP_UGE: | 
|  | 3113 | if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3114 | return getUMaxExpr(getSCEV(LHS), getSCEV(RHS)); | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3115 | else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) | 
| Dan Gohman | 692b468 | 2009-06-22 03:18:45 +0000 | [diff] [blame] | 3116 | return getUMinExpr(getSCEV(LHS), getSCEV(RHS)); | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3117 | break; | 
| Dan Gohman | 4d3c3cf | 2009-06-18 20:21:07 +0000 | [diff] [blame] | 3118 | case ICmpInst::ICMP_NE: | 
|  | 3119 | // n != 0 ? n : 1  ->  umax(n, 1) | 
|  | 3120 | if (LHS == U->getOperand(1) && | 
|  | 3121 | isa<ConstantInt>(U->getOperand(2)) && | 
|  | 3122 | cast<ConstantInt>(U->getOperand(2))->isOne() && | 
|  | 3123 | isa<ConstantInt>(RHS) && | 
|  | 3124 | cast<ConstantInt>(RHS)->isZero()) | 
|  | 3125 | return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(2))); | 
|  | 3126 | break; | 
|  | 3127 | case ICmpInst::ICMP_EQ: | 
|  | 3128 | // n == 0 ? 1 : n  ->  umax(n, 1) | 
|  | 3129 | if (LHS == U->getOperand(2) && | 
|  | 3130 | isa<ConstantInt>(U->getOperand(1)) && | 
|  | 3131 | cast<ConstantInt>(U->getOperand(1))->isOne() && | 
|  | 3132 | isa<ConstantInt>(RHS) && | 
|  | 3133 | cast<ConstantInt>(RHS)->isZero()) | 
|  | 3134 | return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(1))); | 
|  | 3135 | break; | 
| Dan Gohman | 05e8973 | 2008-06-22 19:56:46 +0000 | [diff] [blame] | 3136 | default: | 
|  | 3137 | break; | 
|  | 3138 | } | 
|  | 3139 | } | 
|  | 3140 |  | 
|  | 3141 | default: // We cannot analyze this expression. | 
|  | 3142 | break; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3143 | } | 
|  | 3144 |  | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3145 | return getUnknown(V); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3146 | } | 
|  | 3147 |  | 
|  | 3148 |  | 
|  | 3149 |  | 
|  | 3150 | //===----------------------------------------------------------------------===// | 
|  | 3151 | //                   Iteration Count Computation Code | 
|  | 3152 | // | 
|  | 3153 |  | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3154 | /// getBackedgeTakenCount - If the specified loop has a predictable | 
|  | 3155 | /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute | 
|  | 3156 | /// object. The backedge-taken count is the number of times the loop header | 
|  | 3157 | /// will be branched to from within the loop. This is one less than the | 
|  | 3158 | /// trip count of the loop, since it doesn't count the first iteration, | 
|  | 3159 | /// when the header is branched to from outside the loop. | 
|  | 3160 | /// | 
|  | 3161 | /// Note that it is not valid to call this method on a loop without a | 
|  | 3162 | /// loop-invariant backedge-taken count (see | 
|  | 3163 | /// hasLoopInvariantBackedgeTakenCount). | 
|  | 3164 | /// | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3165 | const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) { | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3166 | return getBackedgeTakenInfo(L).Exact; | 
|  | 3167 | } | 
|  | 3168 |  | 
|  | 3169 | /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except | 
|  | 3170 | /// return the least SCEV value that is known never to be less than the | 
|  | 3171 | /// actual backedge taken count. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3172 | const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) { | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3173 | return getBackedgeTakenInfo(L).Max; | 
|  | 3174 | } | 
|  | 3175 |  | 
| Dan Gohman | dc19104 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3176 | /// PushLoopPHIs - Push PHI nodes in the header of the given loop | 
|  | 3177 | /// onto the given Worklist. | 
|  | 3178 | static void | 
|  | 3179 | PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) { | 
|  | 3180 | BasicBlock *Header = L->getHeader(); | 
|  | 3181 |  | 
|  | 3182 | // Push all Loop-header PHIs onto the Worklist stack. | 
|  | 3183 | for (BasicBlock::iterator I = Header->begin(); | 
|  | 3184 | PHINode *PN = dyn_cast<PHINode>(I); ++I) | 
|  | 3185 | Worklist.push_back(PN); | 
|  | 3186 | } | 
|  | 3187 |  | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3188 | const ScalarEvolution::BackedgeTakenInfo & | 
|  | 3189 | ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3190 | // Initially insert a CouldNotCompute for this loop. If the insertion | 
|  | 3191 | // succeeds, procede to actually compute a backedge-taken count and | 
|  | 3192 | // update the value. The temporary CouldNotCompute value tells SCEV | 
|  | 3193 | // code elsewhere that it shouldn't attempt to request a new | 
|  | 3194 | // backedge-taken count, which could result in infinite recursion. | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3195 | std::pair<std::map<const Loop*, BackedgeTakenInfo>::iterator, bool> Pair = | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3196 | BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute())); | 
|  | 3197 | if (Pair.second) { | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3198 | BackedgeTakenInfo ItCount = ComputeBackedgeTakenCount(L); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3199 | if (ItCount.Exact != getCouldNotCompute()) { | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3200 | assert(ItCount.Exact->isLoopInvariant(L) && | 
|  | 3201 | ItCount.Max->isLoopInvariant(L) && | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3202 | "Computed trip count isn't loop invariant for loop!"); | 
|  | 3203 | ++NumTripCountsComputed; | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3204 |  | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3205 | // Update the value in the map. | 
|  | 3206 | Pair.first->second = ItCount; | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3207 | } else { | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3208 | if (ItCount.Max != getCouldNotCompute()) | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3209 | // Update the value in the map. | 
|  | 3210 | Pair.first->second = ItCount; | 
|  | 3211 | if (isa<PHINode>(L->getHeader()->begin())) | 
|  | 3212 | // Only count loops that have phi nodes as not being computable. | 
|  | 3213 | ++NumTripCountsNotComputed; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3214 | } | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3215 |  | 
|  | 3216 | // Now that we know more about the trip count for this loop, forget any | 
|  | 3217 | // existing SCEV values for PHI nodes in this loop since they are only | 
| Dan Gohman | dc19104 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3218 | // conservative estimates made without the benefit of trip count | 
|  | 3219 | // information. This is similar to the code in | 
|  | 3220 | // forgetLoopBackedgeTakenCount, except that it handles SCEVUnknown PHI | 
|  | 3221 | // nodes specially. | 
|  | 3222 | if (ItCount.hasAnyInfo()) { | 
|  | 3223 | SmallVector<Instruction *, 16> Worklist; | 
|  | 3224 | PushLoopPHIs(L, Worklist); | 
|  | 3225 |  | 
|  | 3226 | SmallPtrSet<Instruction *, 8> Visited; | 
|  | 3227 | while (!Worklist.empty()) { | 
|  | 3228 | Instruction *I = Worklist.pop_back_val(); | 
|  | 3229 | if (!Visited.insert(I)) continue; | 
|  | 3230 |  | 
|  | 3231 | std::map<SCEVCallbackVH, const SCEV*>::iterator It = | 
|  | 3232 | Scalars.find(static_cast<Value *>(I)); | 
|  | 3233 | if (It != Scalars.end()) { | 
|  | 3234 | // SCEVUnknown for a PHI either means that it has an unrecognized | 
|  | 3235 | // structure, or it's a PHI that's in the progress of being computed | 
| Dan Gohman | e6b4bab | 2009-07-13 22:04:06 +0000 | [diff] [blame] | 3236 | // by createNodeForPHI.  In the former case, additional loop trip | 
|  | 3237 | // count information isn't going to change anything. In the later | 
|  | 3238 | // case, createNodeForPHI will perform the necessary updates on its | 
|  | 3239 | // own when it gets to that point. | 
| Dan Gohman | cc2f1eb | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 3240 | if (!isa<PHINode>(I) || !isa<SCEVUnknown>(It->second)) { | 
|  | 3241 | ValuesAtScopes.erase(It->second); | 
| Dan Gohman | dc19104 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3242 | Scalars.erase(It); | 
| Dan Gohman | cc2f1eb | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 3243 | } | 
| Dan Gohman | dc19104 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3244 | if (PHINode *PN = dyn_cast<PHINode>(I)) | 
|  | 3245 | ConstantEvolutionLoopExitValue.erase(PN); | 
|  | 3246 | } | 
|  | 3247 |  | 
|  | 3248 | PushDefUseChildren(I, Worklist); | 
|  | 3249 | } | 
|  | 3250 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3251 | } | 
| Dan Gohman | 7646637 | 2009-04-27 20:16:15 +0000 | [diff] [blame] | 3252 | return Pair.first->second; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3253 | } | 
|  | 3254 |  | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3255 | /// forgetLoopBackedgeTakenCount - This method should be called by the | 
| Dan Gohman | 4330034 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 3256 | /// client when it has changed a loop in a way that may effect | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3257 | /// ScalarEvolution's ability to compute a trip count, or if the loop | 
|  | 3258 | /// is deleted. | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3259 | void ScalarEvolution::forgetLoopBackedgeTakenCount(const Loop *L) { | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3260 | BackedgeTakenCounts.erase(L); | 
| Dan Gohman | f150572 | 2009-05-02 17:43:35 +0000 | [diff] [blame] | 3261 |  | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3262 | SmallVector<Instruction *, 16> Worklist; | 
| Dan Gohman | dc19104 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3263 | PushLoopPHIs(L, Worklist); | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3264 |  | 
| Dan Gohman | dc19104 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3265 | SmallPtrSet<Instruction *, 8> Visited; | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3266 | while (!Worklist.empty()) { | 
|  | 3267 | Instruction *I = Worklist.pop_back_val(); | 
| Dan Gohman | dc19104 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3268 | if (!Visited.insert(I)) continue; | 
|  | 3269 |  | 
|  | 3270 | std::map<SCEVCallbackVH, const SCEV*>::iterator It = | 
|  | 3271 | Scalars.find(static_cast<Value *>(I)); | 
|  | 3272 | if (It != Scalars.end()) { | 
| Dan Gohman | cc2f1eb | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 3273 | ValuesAtScopes.erase(It->second); | 
| Dan Gohman | dc19104 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3274 | Scalars.erase(It); | 
| Dan Gohman | dc19104 | 2009-07-08 19:23:34 +0000 | [diff] [blame] | 3275 | if (PHINode *PN = dyn_cast<PHINode>(I)) | 
|  | 3276 | ConstantEvolutionLoopExitValue.erase(PN); | 
|  | 3277 | } | 
|  | 3278 |  | 
|  | 3279 | PushDefUseChildren(I, Worklist); | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3280 | } | 
| Dan Gohman | 4330034 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 3281 | } | 
|  | 3282 |  | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3283 | /// ComputeBackedgeTakenCount - Compute the number of times the backedge | 
|  | 3284 | /// of the specified loop will execute. | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3285 | ScalarEvolution::BackedgeTakenInfo | 
|  | 3286 | ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) { | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3287 | SmallVector<BasicBlock*, 8> ExitingBlocks; | 
|  | 3288 | L->getExitingBlocks(ExitingBlocks); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3289 |  | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3290 | // Examine all exits and pick the most conservative values. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3291 | const SCEV *BECount = getCouldNotCompute(); | 
|  | 3292 | const SCEV *MaxBECount = getCouldNotCompute(); | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3293 | bool CouldNotComputeBECount = false; | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3294 | for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { | 
|  | 3295 | BackedgeTakenInfo NewBTI = | 
|  | 3296 | ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3297 |  | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3298 | if (NewBTI.Exact == getCouldNotCompute()) { | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3299 | // We couldn't compute an exact value for this exit, so | 
| Dan Gohman | 8885b37 | 2009-06-22 21:10:22 +0000 | [diff] [blame] | 3300 | // we won't be able to compute an exact value for the loop. | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3301 | CouldNotComputeBECount = true; | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3302 | BECount = getCouldNotCompute(); | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3303 | } else if (!CouldNotComputeBECount) { | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3304 | if (BECount == getCouldNotCompute()) | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3305 | BECount = NewBTI.Exact; | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3306 | else | 
| Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3307 | BECount = getUMinFromMismatchedTypes(BECount, NewBTI.Exact); | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3308 | } | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3309 | if (MaxBECount == getCouldNotCompute()) | 
| Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3310 | MaxBECount = NewBTI.Max; | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3311 | else if (NewBTI.Max != getCouldNotCompute()) | 
| Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3312 | MaxBECount = getUMinFromMismatchedTypes(MaxBECount, NewBTI.Max); | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3313 | } | 
|  | 3314 |  | 
|  | 3315 | return BackedgeTakenInfo(BECount, MaxBECount); | 
|  | 3316 | } | 
|  | 3317 |  | 
|  | 3318 | /// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge | 
|  | 3319 | /// of the specified loop will execute if it exits via the specified block. | 
|  | 3320 | ScalarEvolution::BackedgeTakenInfo | 
|  | 3321 | ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L, | 
|  | 3322 | BasicBlock *ExitingBlock) { | 
|  | 3323 |  | 
|  | 3324 | // Okay, we've chosen an exiting block.  See what condition causes us to | 
|  | 3325 | // exit at this block. | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3326 | // | 
|  | 3327 | // FIXME: we should be able to handle switch instructions (with a single exit) | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3328 | BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3329 | if (ExitBr == 0) return getCouldNotCompute(); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3330 | assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!"); | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3331 |  | 
| Chris Lattner | 1895485 | 2007-01-07 02:24:26 +0000 | [diff] [blame] | 3332 | // At this point, we know we have a conditional branch that determines whether | 
|  | 3333 | // the loop is exited.  However, we don't know if the branch is executed each | 
|  | 3334 | // time through the loop.  If not, then the execution count of the branch will | 
|  | 3335 | // not be equal to the trip count of the loop. | 
|  | 3336 | // | 
|  | 3337 | // Currently we check for this by checking to see if the Exit branch goes to | 
|  | 3338 | // the loop header.  If so, we know it will always execute the same number of | 
| Chris Lattner | 5a55476 | 2007-01-14 01:24:47 +0000 | [diff] [blame] | 3339 | // times as the loop.  We also handle the case where the exit block *is* the | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3340 | // loop header.  This is common for un-rotated loops. | 
|  | 3341 | // | 
|  | 3342 | // If both of those tests fail, walk up the unique predecessor chain to the | 
|  | 3343 | // header, stopping if there is an edge that doesn't exit the loop. If the | 
|  | 3344 | // header is reached, the execution count of the branch will be equal to the | 
|  | 3345 | // trip count of the loop. | 
|  | 3346 | // | 
|  | 3347 | //  More extensive analysis could be done to handle more cases here. | 
|  | 3348 | // | 
| Chris Lattner | 1895485 | 2007-01-07 02:24:26 +0000 | [diff] [blame] | 3349 | if (ExitBr->getSuccessor(0) != L->getHeader() && | 
| Chris Lattner | 5a55476 | 2007-01-14 01:24:47 +0000 | [diff] [blame] | 3350 | ExitBr->getSuccessor(1) != L->getHeader() && | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3351 | ExitBr->getParent() != L->getHeader()) { | 
|  | 3352 | // The simple checks failed, try climbing the unique predecessor chain | 
|  | 3353 | // up to the header. | 
|  | 3354 | bool Ok = false; | 
|  | 3355 | for (BasicBlock *BB = ExitBr->getParent(); BB; ) { | 
|  | 3356 | BasicBlock *Pred = BB->getUniquePredecessor(); | 
|  | 3357 | if (!Pred) | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3358 | return getCouldNotCompute(); | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3359 | TerminatorInst *PredTerm = Pred->getTerminator(); | 
|  | 3360 | for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) { | 
|  | 3361 | BasicBlock *PredSucc = PredTerm->getSuccessor(i); | 
|  | 3362 | if (PredSucc == BB) | 
|  | 3363 | continue; | 
|  | 3364 | // If the predecessor has a successor that isn't BB and isn't | 
|  | 3365 | // outside the loop, assume the worst. | 
|  | 3366 | if (L->contains(PredSucc)) | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3367 | return getCouldNotCompute(); | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3368 | } | 
|  | 3369 | if (Pred == L->getHeader()) { | 
|  | 3370 | Ok = true; | 
|  | 3371 | break; | 
|  | 3372 | } | 
|  | 3373 | BB = Pred; | 
|  | 3374 | } | 
|  | 3375 | if (!Ok) | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3376 | return getCouldNotCompute(); | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3377 | } | 
|  | 3378 |  | 
|  | 3379 | // Procede to the next level to examine the exit condition expression. | 
|  | 3380 | return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(), | 
|  | 3381 | ExitBr->getSuccessor(0), | 
|  | 3382 | ExitBr->getSuccessor(1)); | 
|  | 3383 | } | 
|  | 3384 |  | 
|  | 3385 | /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the | 
|  | 3386 | /// backedge of the specified loop will execute if its exit condition | 
|  | 3387 | /// were a conditional branch of ExitCond, TBB, and FBB. | 
|  | 3388 | ScalarEvolution::BackedgeTakenInfo | 
|  | 3389 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L, | 
|  | 3390 | Value *ExitCond, | 
|  | 3391 | BasicBlock *TBB, | 
|  | 3392 | BasicBlock *FBB) { | 
| Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 3393 | // Check if the controlling expression for this loop is an And or Or. | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3394 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) { | 
|  | 3395 | if (BO->getOpcode() == Instruction::And) { | 
|  | 3396 | // Recurse on the operands of the and. | 
|  | 3397 | BackedgeTakenInfo BTI0 = | 
|  | 3398 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); | 
|  | 3399 | BackedgeTakenInfo BTI1 = | 
|  | 3400 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3401 | const SCEV *BECount = getCouldNotCompute(); | 
|  | 3402 | const SCEV *MaxBECount = getCouldNotCompute(); | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3403 | if (L->contains(TBB)) { | 
|  | 3404 | // Both conditions must be true for the loop to continue executing. | 
|  | 3405 | // Choose the less conservative count. | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3406 | if (BTI0.Exact == getCouldNotCompute() || | 
|  | 3407 | BTI1.Exact == getCouldNotCompute()) | 
|  | 3408 | BECount = getCouldNotCompute(); | 
| Dan Gohman | ed62738 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3409 | else | 
|  | 3410 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3411 | if (BTI0.Max == getCouldNotCompute()) | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3412 | MaxBECount = BTI1.Max; | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3413 | else if (BTI1.Max == getCouldNotCompute()) | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3414 | MaxBECount = BTI0.Max; | 
| Dan Gohman | ed62738 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3415 | else | 
|  | 3416 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3417 | } else { | 
|  | 3418 | // Both conditions must be true for the loop to exit. | 
|  | 3419 | assert(L->contains(FBB) && "Loop block has no successor in loop!"); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3420 | if (BTI0.Exact != getCouldNotCompute() && | 
|  | 3421 | BTI1.Exact != getCouldNotCompute()) | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3422 | BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3423 | if (BTI0.Max != getCouldNotCompute() && | 
|  | 3424 | BTI1.Max != getCouldNotCompute()) | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3425 | MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max); | 
|  | 3426 | } | 
|  | 3427 |  | 
|  | 3428 | return BackedgeTakenInfo(BECount, MaxBECount); | 
|  | 3429 | } | 
|  | 3430 | if (BO->getOpcode() == Instruction::Or) { | 
|  | 3431 | // Recurse on the operands of the or. | 
|  | 3432 | BackedgeTakenInfo BTI0 = | 
|  | 3433 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); | 
|  | 3434 | BackedgeTakenInfo BTI1 = | 
|  | 3435 | ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3436 | const SCEV *BECount = getCouldNotCompute(); | 
|  | 3437 | const SCEV *MaxBECount = getCouldNotCompute(); | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3438 | if (L->contains(FBB)) { | 
|  | 3439 | // Both conditions must be false for the loop to continue executing. | 
|  | 3440 | // Choose the less conservative count. | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3441 | if (BTI0.Exact == getCouldNotCompute() || | 
|  | 3442 | BTI1.Exact == getCouldNotCompute()) | 
|  | 3443 | BECount = getCouldNotCompute(); | 
| Dan Gohman | ed62738 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3444 | else | 
|  | 3445 | BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3446 | if (BTI0.Max == getCouldNotCompute()) | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3447 | MaxBECount = BTI1.Max; | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3448 | else if (BTI1.Max == getCouldNotCompute()) | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3449 | MaxBECount = BTI0.Max; | 
| Dan Gohman | ed62738 | 2009-06-22 15:09:28 +0000 | [diff] [blame] | 3450 | else | 
|  | 3451 | MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3452 | } else { | 
|  | 3453 | // Both conditions must be false for the loop to exit. | 
|  | 3454 | assert(L->contains(TBB) && "Loop block has no successor in loop!"); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3455 | if (BTI0.Exact != getCouldNotCompute() && | 
|  | 3456 | BTI1.Exact != getCouldNotCompute()) | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3457 | BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3458 | if (BTI0.Max != getCouldNotCompute() && | 
|  | 3459 | BTI1.Max != getCouldNotCompute()) | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3460 | MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max); | 
|  | 3461 | } | 
|  | 3462 |  | 
|  | 3463 | return BackedgeTakenInfo(BECount, MaxBECount); | 
|  | 3464 | } | 
|  | 3465 | } | 
|  | 3466 |  | 
|  | 3467 | // With an icmp, it may be feasible to compute an exact backedge-taken count. | 
|  | 3468 | // Procede to the next level to examine the icmp. | 
|  | 3469 | if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) | 
|  | 3470 | return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3471 |  | 
| Eli Friedman | ebf98b0 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3472 | // If it's not an integer or pointer comparison then compute it the hard way. | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3473 | return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); | 
|  | 3474 | } | 
|  | 3475 |  | 
|  | 3476 | /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the | 
|  | 3477 | /// backedge of the specified loop will execute if its exit condition | 
|  | 3478 | /// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB. | 
|  | 3479 | ScalarEvolution::BackedgeTakenInfo | 
|  | 3480 | ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L, | 
|  | 3481 | ICmpInst *ExitCond, | 
|  | 3482 | BasicBlock *TBB, | 
|  | 3483 | BasicBlock *FBB) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3484 |  | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3485 | // If the condition was exit on true, convert the condition to exit on false | 
|  | 3486 | ICmpInst::Predicate Cond; | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3487 | if (!L->contains(FBB)) | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3488 | Cond = ExitCond->getPredicate(); | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3489 | else | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3490 | Cond = ExitCond->getInversePredicate(); | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3491 |  | 
|  | 3492 | // Handle common loops like: for (X = "string"; *X; ++X) | 
|  | 3493 | if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0))) | 
|  | 3494 | if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3495 | const SCEV *ItCnt = | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3496 | ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond); | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3497 | if (!isa<SCEVCouldNotCompute>(ItCnt)) { | 
|  | 3498 | unsigned BitWidth = getTypeSizeInBits(ItCnt->getType()); | 
|  | 3499 | return BackedgeTakenInfo(ItCnt, | 
|  | 3500 | isa<SCEVConstant>(ItCnt) ? ItCnt : | 
|  | 3501 | getConstant(APInt::getMaxValue(BitWidth)-1)); | 
|  | 3502 | } | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3503 | } | 
|  | 3504 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3505 | const SCEV *LHS = getSCEV(ExitCond->getOperand(0)); | 
|  | 3506 | const SCEV *RHS = getSCEV(ExitCond->getOperand(1)); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3507 |  | 
|  | 3508 | // Try to evaluate any dependencies out of the loop. | 
| Dan Gohman | 8ca0885 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3509 | LHS = getSCEVAtScope(LHS, L); | 
|  | 3510 | RHS = getSCEVAtScope(RHS, L); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3511 |  | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3512 | // At this point, we would like to compute how many iterations of the | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3513 | // loop the predicate will return true for these inputs. | 
| Dan Gohman | dc5f5cb | 2008-09-16 18:52:57 +0000 | [diff] [blame] | 3514 | if (LHS->isLoopInvariant(L) && !RHS->isLoopInvariant(L)) { | 
|  | 3515 | // If there is a loop-invariant, force it into the RHS. | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3516 | std::swap(LHS, RHS); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3517 | Cond = ICmpInst::getSwappedPredicate(Cond); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3518 | } | 
|  | 3519 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3520 | // If we have a comparison of a chrec against a constant, try to use value | 
|  | 3521 | // ranges to answer this query. | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3522 | if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) | 
|  | 3523 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS)) | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3524 | if (AddRec->getLoop() == L) { | 
| Eli Friedman | ebf98b0 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3525 | // Form the constant range. | 
|  | 3526 | ConstantRange CompRange( | 
|  | 3527 | ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue())); | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3528 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3529 | const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this); | 
| Eli Friedman | ebf98b0 | 2009-05-09 12:32:42 +0000 | [diff] [blame] | 3530 | if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3531 | } | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3532 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3533 | switch (Cond) { | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3534 | case ICmpInst::ICMP_NE: {                     // while (X != Y) | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3535 | // Convert to: while (X-Y != 0) | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3536 | const SCEV *TC = HowFarToZero(getMinusSCEV(LHS, RHS), L); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3537 | if (!isa<SCEVCouldNotCompute>(TC)) return TC; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3538 | break; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3539 | } | 
| Dan Gohman | 8a8ad7d | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 3540 | case ICmpInst::ICMP_EQ: {                     // while (X == Y) | 
|  | 3541 | // Convert to: while (X-Y == 0) | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3542 | const SCEV *TC = HowFarToNonZero(getMinusSCEV(LHS, RHS), L); | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3543 | if (!isa<SCEVCouldNotCompute>(TC)) return TC; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3544 | break; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3545 | } | 
|  | 3546 | case ICmpInst::ICMP_SLT: { | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3547 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true); | 
|  | 3548 | if (BTI.hasAnyInfo()) return BTI; | 
| Chris Lattner | 587a75b | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 3549 | break; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3550 | } | 
|  | 3551 | case ICmpInst::ICMP_SGT: { | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3552 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), | 
|  | 3553 | getNotSCEV(RHS), L, true); | 
|  | 3554 | if (BTI.hasAnyInfo()) return BTI; | 
| Nick Lewycky | 96606ce | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 3555 | break; | 
|  | 3556 | } | 
|  | 3557 | case ICmpInst::ICMP_ULT: { | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3558 | BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false); | 
|  | 3559 | if (BTI.hasAnyInfo()) return BTI; | 
| Nick Lewycky | 96606ce | 2007-08-06 19:21:00 +0000 | [diff] [blame] | 3560 | break; | 
|  | 3561 | } | 
|  | 3562 | case ICmpInst::ICMP_UGT: { | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 3563 | BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), | 
|  | 3564 | getNotSCEV(RHS), L, false); | 
|  | 3565 | if (BTI.hasAnyInfo()) return BTI; | 
| Chris Lattner | 587a75b | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 3566 | break; | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3567 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3568 | default: | 
| Chris Lattner | 0916921 | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 3569 | #if 0 | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3570 | errs() << "ComputeBackedgeTakenCount "; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3571 | if (ExitCond->getOperand(0)->getType()->isUnsigned()) | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3572 | errs() << "[unsigned] "; | 
|  | 3573 | errs() << *LHS << "   " | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3574 | << Instruction::getOpcodeName(Instruction::ICmp) | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3575 | << "   " << *RHS << "\n"; | 
| Chris Lattner | 0916921 | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 3576 | #endif | 
| Chris Lattner | 0defaa1 | 2004-04-03 00:43:03 +0000 | [diff] [blame] | 3577 | break; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3578 | } | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3579 | return | 
| Dan Gohman | 96212b6 | 2009-06-22 00:31:57 +0000 | [diff] [blame] | 3580 | ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); | 
| Chris Lattner | 4021d1a | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3581 | } | 
|  | 3582 |  | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3583 | static ConstantInt * | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 3584 | EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, | 
|  | 3585 | ScalarEvolution &SE) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3586 | const SCEV *InVal = SE.getConstant(C); | 
|  | 3587 | const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE); | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3588 | assert(isa<SCEVConstant>(Val) && | 
|  | 3589 | "Evaluation of SCEV at constant didn't fold correctly?"); | 
|  | 3590 | return cast<SCEVConstant>(Val)->getValue(); | 
|  | 3591 | } | 
|  | 3592 |  | 
|  | 3593 | /// GetAddressedElementFromGlobal - Given a global variable with an initializer | 
|  | 3594 | /// and a GEP expression (missing the pointer index) indexing into it, return | 
|  | 3595 | /// the addressed element of the initializer or null if the index expression is | 
|  | 3596 | /// invalid. | 
|  | 3597 | static Constant * | 
| Owen Anderson | 47db941 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3598 | GetAddressedElementFromGlobal(LLVMContext &Context, GlobalVariable *GV, | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3599 | const std::vector<ConstantInt*> &Indices) { | 
|  | 3600 | Constant *Init = GV->getInitializer(); | 
|  | 3601 | for (unsigned i = 0, e = Indices.size(); i != e; ++i) { | 
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3602 | uint64_t Idx = Indices[i]->getZExtValue(); | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3603 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) { | 
|  | 3604 | assert(Idx < CS->getNumOperands() && "Bad struct index!"); | 
|  | 3605 | Init = cast<Constant>(CS->getOperand(Idx)); | 
|  | 3606 | } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) { | 
|  | 3607 | if (Idx >= CA->getNumOperands()) return 0;  // Bogus program | 
|  | 3608 | Init = cast<Constant>(CA->getOperand(Idx)); | 
|  | 3609 | } else if (isa<ConstantAggregateZero>(Init)) { | 
|  | 3610 | if (const StructType *STy = dyn_cast<StructType>(Init->getType())) { | 
|  | 3611 | assert(Idx < STy->getNumElements() && "Bad struct index!"); | 
| Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3612 | Init = Constant::getNullValue(STy->getElementType(Idx)); | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3613 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) { | 
|  | 3614 | if (Idx >= ATy->getNumElements()) return 0;  // Bogus program | 
| Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 3615 | Init = Constant::getNullValue(ATy->getElementType()); | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3616 | } else { | 
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3617 | llvm_unreachable("Unknown constant aggregate type!"); | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3618 | } | 
|  | 3619 | return 0; | 
|  | 3620 | } else { | 
|  | 3621 | return 0; // Unknown initializer type | 
|  | 3622 | } | 
|  | 3623 | } | 
|  | 3624 | return Init; | 
|  | 3625 | } | 
|  | 3626 |  | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3627 | /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of | 
|  | 3628 | /// 'icmp op load X, cst', try to see if we can compute the backedge | 
|  | 3629 | /// execution count. | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3630 | const SCEV * | 
|  | 3631 | ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount( | 
|  | 3632 | LoadInst *LI, | 
|  | 3633 | Constant *RHS, | 
|  | 3634 | const Loop *L, | 
|  | 3635 | ICmpInst::Predicate predicate) { | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3636 | if (LI->isVolatile()) return getCouldNotCompute(); | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3637 |  | 
|  | 3638 | // Check to see if the loaded pointer is a getelementptr of a global. | 
|  | 3639 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3640 | if (!GEP) return getCouldNotCompute(); | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3641 |  | 
|  | 3642 | // Make sure that it is really a constant global we are gepping, with an | 
|  | 3643 | // initializer, and make sure the first IDX is really 0. | 
|  | 3644 | GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); | 
| Dan Gohman | 5d5bc6d | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 3645 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() || | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3646 | GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) || | 
|  | 3647 | !cast<Constant>(GEP->getOperand(1))->isNullValue()) | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3648 | return getCouldNotCompute(); | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3649 |  | 
|  | 3650 | // Okay, we allow one non-constant index into the GEP instruction. | 
|  | 3651 | Value *VarIdx = 0; | 
|  | 3652 | std::vector<ConstantInt*> Indexes; | 
|  | 3653 | unsigned VarIdxNum = 0; | 
|  | 3654 | for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) | 
|  | 3655 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { | 
|  | 3656 | Indexes.push_back(CI); | 
|  | 3657 | } else if (!isa<ConstantInt>(GEP->getOperand(i))) { | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3658 | if (VarIdx) return getCouldNotCompute();  // Multiple non-constant idx's. | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3659 | VarIdx = GEP->getOperand(i); | 
|  | 3660 | VarIdxNum = i-2; | 
|  | 3661 | Indexes.push_back(0); | 
|  | 3662 | } | 
|  | 3663 |  | 
|  | 3664 | // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant. | 
|  | 3665 | // Check to see if X is a loop variant variable value now. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3666 | const SCEV *Idx = getSCEV(VarIdx); | 
| Dan Gohman | 8ca0885 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3667 | Idx = getSCEVAtScope(Idx, L); | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3668 |  | 
|  | 3669 | // We can only recognize very limited forms of loop index expressions, in | 
|  | 3670 | // particular, only affine AddRec's like {C1,+,C2}. | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 3671 | const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx); | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3672 | if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) || | 
|  | 3673 | !isa<SCEVConstant>(IdxExpr->getOperand(0)) || | 
|  | 3674 | !isa<SCEVConstant>(IdxExpr->getOperand(1))) | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3675 | return getCouldNotCompute(); | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3676 |  | 
|  | 3677 | unsigned MaxSteps = MaxBruteForceIterations; | 
|  | 3678 | for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) { | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 3679 | ConstantInt *ItCst = ConstantInt::get( | 
| Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 3680 | cast<IntegerType>(IdxExpr->getType()), IterationNum); | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3681 | ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this); | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3682 |  | 
|  | 3683 | // Form the GEP offset. | 
|  | 3684 | Indexes[VarIdxNum] = Val; | 
|  | 3685 |  | 
| Owen Anderson | 47db941 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3686 | Constant *Result = GetAddressedElementFromGlobal(getContext(), GV, Indexes); | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3687 | if (Result == 0) break;  // Cannot compute! | 
|  | 3688 |  | 
|  | 3689 | // Evaluate the condition for this iteration. | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3690 | Result = ConstantExpr::getICmp(predicate, Result, RHS); | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3691 | if (!isa<ConstantInt>(Result)) break;  // Couldn't decide for sure | 
| Reid Spencer | 983e3b3 | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3692 | if (cast<ConstantInt>(Result)->getValue().isMinValue()) { | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3693 | #if 0 | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 3694 | errs() << "\n***\n*** Computed loop count " << *ItCst | 
|  | 3695 | << "\n*** From global " << *GV << "*** BB: " << *L->getHeader() | 
|  | 3696 | << "***\n"; | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3697 | #endif | 
|  | 3698 | ++NumArrayLenItCounts; | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3699 | return getConstant(ItCst);   // Found terminating iteration! | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3700 | } | 
|  | 3701 | } | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3702 | return getCouldNotCompute(); | 
| Chris Lattner | ec901cc | 2004-10-12 01:49:27 +0000 | [diff] [blame] | 3703 | } | 
|  | 3704 |  | 
|  | 3705 |  | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3706 | /// CanConstantFold - Return true if we can constant fold an instruction of the | 
|  | 3707 | /// specified type, assuming that all operands were constants. | 
|  | 3708 | static bool CanConstantFold(const Instruction *I) { | 
| Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 3709 | if (isa<BinaryOperator>(I) || isa<CmpInst>(I) || | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3710 | isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I)) | 
|  | 3711 | return true; | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3712 |  | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3713 | if (const CallInst *CI = dyn_cast<CallInst>(I)) | 
|  | 3714 | if (const Function *F = CI->getCalledFunction()) | 
| Dan Gohman | a65951f | 2008-01-31 01:05:10 +0000 | [diff] [blame] | 3715 | return canConstantFoldCallTo(F); | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3716 | return false; | 
| Chris Lattner | 4021d1a | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3717 | } | 
|  | 3718 |  | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3719 | /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node | 
|  | 3720 | /// in the loop that V is derived from.  We allow arbitrary operations along the | 
|  | 3721 | /// way, but the operands of an operation must either be constants or a value | 
|  | 3722 | /// derived from a constant PHI.  If this expression does not fit with these | 
|  | 3723 | /// constraints, return null. | 
|  | 3724 | static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) { | 
|  | 3725 | // If this is not an instruction, or if this is an instruction outside of the | 
|  | 3726 | // loop, it can't be derived from a loop PHI. | 
|  | 3727 | Instruction *I = dyn_cast<Instruction>(V); | 
|  | 3728 | if (I == 0 || !L->contains(I->getParent())) return 0; | 
|  | 3729 |  | 
| Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 3730 | if (PHINode *PN = dyn_cast<PHINode>(I)) { | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3731 | if (L->getHeader() == I->getParent()) | 
|  | 3732 | return PN; | 
|  | 3733 | else | 
|  | 3734 | // We don't currently keep track of the control flow needed to evaluate | 
|  | 3735 | // PHIs, so we cannot handle PHIs inside of loops. | 
|  | 3736 | return 0; | 
| Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 3737 | } | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3738 |  | 
|  | 3739 | // If we won't be able to constant fold this expression even if the operands | 
|  | 3740 | // are constants, return early. | 
|  | 3741 | if (!CanConstantFold(I)) return 0; | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3742 |  | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3743 | // Otherwise, we can evaluate this instruction if all of its operands are | 
|  | 3744 | // constant or derived from a PHI node themselves. | 
|  | 3745 | PHINode *PHI = 0; | 
|  | 3746 | for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op) | 
|  | 3747 | if (!(isa<Constant>(I->getOperand(Op)) || | 
|  | 3748 | isa<GlobalValue>(I->getOperand(Op)))) { | 
|  | 3749 | PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L); | 
|  | 3750 | if (P == 0) return 0;  // Not evolving from PHI | 
|  | 3751 | if (PHI == 0) | 
|  | 3752 | PHI = P; | 
|  | 3753 | else if (PHI != P) | 
|  | 3754 | return 0;  // Evolving from multiple different PHIs. | 
|  | 3755 | } | 
|  | 3756 |  | 
|  | 3757 | // This is a expression evolving from a constant PHI! | 
|  | 3758 | return PHI; | 
|  | 3759 | } | 
|  | 3760 |  | 
|  | 3761 | /// EvaluateExpression - Given an expression that passes the | 
|  | 3762 | /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node | 
|  | 3763 | /// in the loop has the value PHIVal.  If we can't fold this expression for some | 
|  | 3764 | /// reason, return null. | 
|  | 3765 | static Constant *EvaluateExpression(Value *V, Constant *PHIVal) { | 
|  | 3766 | if (isa<PHINode>(V)) return PHIVal; | 
| Reid Spencer | 30d69a5 | 2004-07-18 00:18:30 +0000 | [diff] [blame] | 3767 | if (Constant *C = dyn_cast<Constant>(V)) return C; | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3768 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV; | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3769 | Instruction *I = cast<Instruction>(V); | 
| Owen Anderson | 47db941 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3770 | LLVMContext &Context = I->getParent()->getContext(); | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3771 |  | 
|  | 3772 | std::vector<Constant*> Operands; | 
|  | 3773 | Operands.resize(I->getNumOperands()); | 
|  | 3774 |  | 
|  | 3775 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { | 
|  | 3776 | Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal); | 
|  | 3777 | if (Operands[i] == 0) return 0; | 
|  | 3778 | } | 
|  | 3779 |  | 
| Chris Lattner | d2265b4 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3780 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) | 
|  | 3781 | return ConstantFoldCompareInstOperands(CI->getPredicate(), | 
| Owen Anderson | 39f00cc | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 3782 | &Operands[0], Operands.size(), | 
|  | 3783 | Context); | 
| Chris Lattner | d2265b4 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3784 | else | 
|  | 3785 | return ConstantFoldInstOperands(I->getOpcode(), I->getType(), | 
| Owen Anderson | 39f00cc | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 3786 | &Operands[0], Operands.size(), | 
|  | 3787 | Context); | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3788 | } | 
|  | 3789 |  | 
|  | 3790 | /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is | 
|  | 3791 | /// in the header of its containing loop, we know the loop executes a | 
|  | 3792 | /// constant number of times, and the PHI node is just a recurrence | 
|  | 3793 | /// involving constants, fold it. | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3794 | Constant * | 
|  | 3795 | ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN, | 
|  | 3796 | const APInt& BEs, | 
|  | 3797 | const Loop *L) { | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3798 | std::map<PHINode*, Constant*>::iterator I = | 
|  | 3799 | ConstantEvolutionLoopExitValue.find(PN); | 
|  | 3800 | if (I != ConstantEvolutionLoopExitValue.end()) | 
|  | 3801 | return I->second; | 
|  | 3802 |  | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3803 | if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations))) | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3804 | return ConstantEvolutionLoopExitValue[PN] = 0;  // Not going to evaluate it. | 
|  | 3805 |  | 
|  | 3806 | Constant *&RetVal = ConstantEvolutionLoopExitValue[PN]; | 
|  | 3807 |  | 
|  | 3808 | // Since the loop is canonicalized, the PHI node must have two entries.  One | 
|  | 3809 | // entry must be a constant (coming in from outside of the loop), and the | 
|  | 3810 | // second must be derived from the same PHI. | 
|  | 3811 | bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); | 
|  | 3812 | Constant *StartCST = | 
|  | 3813 | dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); | 
|  | 3814 | if (StartCST == 0) | 
|  | 3815 | return RetVal = 0;  // Must be a constant. | 
|  | 3816 |  | 
|  | 3817 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); | 
|  | 3818 | PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); | 
|  | 3819 | if (PN2 != PN) | 
|  | 3820 | return RetVal = 0;  // Not derived from same PHI. | 
|  | 3821 |  | 
|  | 3822 | // Execute the loop symbolically to determine the exit value. | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3823 | if (BEs.getActiveBits() >= 32) | 
| Reid Spencer | 983e3b3 | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3824 | return RetVal = 0; // More than 2^32-1 iterations?? Not doing it! | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3825 |  | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3826 | unsigned NumIterations = BEs.getZExtValue(); // must be in range | 
| Reid Spencer | 983e3b3 | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3827 | unsigned IterationNum = 0; | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3828 | for (Constant *PHIVal = StartCST; ; ++IterationNum) { | 
|  | 3829 | if (IterationNum == NumIterations) | 
|  | 3830 | return RetVal = PHIVal;  // Got exit value! | 
|  | 3831 |  | 
|  | 3832 | // Compute the value of the PHI node for the next iteration. | 
|  | 3833 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal); | 
|  | 3834 | if (NextPHI == PHIVal) | 
|  | 3835 | return RetVal = NextPHI;  // Stopped evolving! | 
|  | 3836 | if (NextPHI == 0) | 
|  | 3837 | return 0;        // Couldn't evaluate! | 
|  | 3838 | PHIVal = NextPHI; | 
|  | 3839 | } | 
|  | 3840 | } | 
|  | 3841 |  | 
| Dan Gohman | 169ef13 | 2009-07-27 16:09:48 +0000 | [diff] [blame] | 3842 | /// ComputeBackedgeTakenCountExhaustively - If the loop is known to execute a | 
| Chris Lattner | 4021d1a | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3843 | /// constant number of times (the condition evolves only from constants), | 
|  | 3844 | /// try to evaluate a few iterations of the loop until we get the exit | 
|  | 3845 | /// condition gets a value of ExitWhen (true or false).  If we cannot | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3846 | /// evaluate the trip count of the loop, return getCouldNotCompute(). | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3847 | const SCEV * | 
|  | 3848 | ScalarEvolution::ComputeBackedgeTakenCountExhaustively(const Loop *L, | 
|  | 3849 | Value *Cond, | 
|  | 3850 | bool ExitWhen) { | 
| Chris Lattner | 4021d1a | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3851 | PHINode *PN = getConstantEvolvingPHI(Cond, L); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3852 | if (PN == 0) return getCouldNotCompute(); | 
| Chris Lattner | 4021d1a | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 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)); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3860 | if (StartCST == 0) return getCouldNotCompute();  // Must be a constant. | 
| Chris Lattner | 4021d1a | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3861 |  | 
|  | 3862 | Value *BEValue = PN->getIncomingValue(SecondIsBackedge); | 
|  | 3863 | PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3864 | if (PN2 != PN) return getCouldNotCompute();  // Not derived from same PHI. | 
| Chris Lattner | 4021d1a | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3865 |  | 
|  | 3866 | // Okay, we find a PHI node that defines the trip count of this loop.  Execute | 
|  | 3867 | // the loop symbolically to determine when the condition gets a value of | 
|  | 3868 | // "ExitWhen". | 
|  | 3869 | unsigned IterationNum = 0; | 
|  | 3870 | unsigned MaxIterations = MaxBruteForceIterations;   // Limit analysis. | 
|  | 3871 | for (Constant *PHIVal = StartCST; | 
|  | 3872 | IterationNum != MaxIterations; ++IterationNum) { | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3873 | ConstantInt *CondVal = | 
|  | 3874 | dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal)); | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3875 |  | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3876 | // Couldn't symbolically evaluate. | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3877 | if (!CondVal) return getCouldNotCompute(); | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 3878 |  | 
| Reid Spencer | 983e3b3 | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 3879 | if (CondVal->getValue() == uint64_t(ExitWhen)) { | 
| Chris Lattner | 4021d1a | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3880 | ++NumBruteForceTripCountsComputed; | 
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3881 | return getConstant(Type::getInt32Ty(getContext()), IterationNum); | 
| Chris Lattner | 4021d1a | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3882 | } | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3883 |  | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3884 | // Compute the value of the PHI node for the next iteration. | 
|  | 3885 | Constant *NextPHI = EvaluateExpression(BEValue, PHIVal); | 
|  | 3886 | if (NextPHI == 0 || NextPHI == PHIVal) | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3887 | return getCouldNotCompute();// Couldn't evaluate or not making progress... | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3888 | PHIVal = NextPHI; | 
| Chris Lattner | 4021d1a | 2004-04-17 18:36:24 +0000 | [diff] [blame] | 3889 | } | 
|  | 3890 |  | 
|  | 3891 | // Too many iterations were needed to evaluate. | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 3892 | return getCouldNotCompute(); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3893 | } | 
|  | 3894 |  | 
| Dan Gohman | 237d9e5 | 2009-09-03 15:00:26 +0000 | [diff] [blame] | 3895 | /// getSCEVAtScope - Return a SCEV expression for the specified value | 
| Dan Gohman | b81f47d | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 3896 | /// at the specified scope in the program.  The L value specifies a loop | 
|  | 3897 | /// nest to evaluate the expression at, where null is the top-level or a | 
|  | 3898 | /// specified loop is immediately inside of the loop. | 
|  | 3899 | /// | 
|  | 3900 | /// This method can be used to compute the exit value for a variable defined | 
|  | 3901 | /// in a loop by querying what the value will hold in the parent loop. | 
|  | 3902 | /// | 
| Dan Gohman | 8ca0885 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 3903 | /// In the case that a relevant loop exit value cannot be computed, the | 
|  | 3904 | /// original value V is returned. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3905 | const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { | 
| Dan Gohman | cc2f1eb | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 3906 | // Check to see if we've folded this expression at this loop before. | 
|  | 3907 | std::map<const Loop *, const SCEV *> &Values = ValuesAtScopes[V]; | 
|  | 3908 | std::pair<std::map<const Loop *, const SCEV *>::iterator, bool> Pair = | 
|  | 3909 | Values.insert(std::make_pair(L, static_cast<const SCEV *>(0))); | 
|  | 3910 | if (!Pair.second) | 
|  | 3911 | return Pair.first->second ? Pair.first->second : V; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 3912 |  | 
| Dan Gohman | cc2f1eb | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 3913 | // Otherwise compute it. | 
|  | 3914 | const SCEV *C = computeSCEVAtScope(V, L); | 
| Dan Gohman | dba696a | 2009-08-31 21:58:28 +0000 | [diff] [blame] | 3915 | ValuesAtScopes[V][L] = C; | 
| Dan Gohman | cc2f1eb | 2009-08-31 21:15:23 +0000 | [diff] [blame] | 3916 | return C; | 
|  | 3917 | } | 
|  | 3918 |  | 
|  | 3919 | const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) { | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3920 | if (isa<SCEVConstant>(V)) return V; | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 3921 |  | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 3922 | // If this instruction is evolved from a constant-evolving PHI, compute the | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3923 | // exit value from the loop without using SCEVs. | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3924 | if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) { | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3925 | if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) { | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 3926 | const Loop *LI = (*this->LI)[I->getParent()]; | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3927 | if (LI && LI->getParentLoop() == L)  // Looking for loop exit value. | 
|  | 3928 | if (PHINode *PN = dyn_cast<PHINode>(I)) | 
|  | 3929 | if (PN->getParent() == LI->getHeader()) { | 
|  | 3930 | // Okay, there is no closed form solution for the PHI node.  Check | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3931 | // to see if the loop that contains it has a known backedge-taken | 
|  | 3932 | // count.  If so, we may be able to force computation of the exit | 
|  | 3933 | // value. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 3934 | const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI); | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3935 | if (const SCEVConstant *BTCC = | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3936 | dyn_cast<SCEVConstant>(BackedgeTakenCount)) { | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3937 | // Okay, we know how many times the containing loop executes.  If | 
|  | 3938 | // this is a constant evolving PHI node, get the final value at | 
|  | 3939 | // the specified iteration number. | 
|  | 3940 | Constant *RV = getConstantEvolutionLoopExitValue(PN, | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 3941 | BTCC->getValue()->getValue(), | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3942 | LI); | 
| Dan Gohman | 9d203c6 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 3943 | if (RV) return getSCEV(RV); | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3944 | } | 
|  | 3945 | } | 
|  | 3946 |  | 
| Reid Spencer | e6328ca | 2006-12-04 21:33:23 +0000 | [diff] [blame] | 3947 | // Okay, this is an expression that we cannot symbolically evaluate | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3948 | // into a SCEV.  Check to see if it's possible to symbolically evaluate | 
| Reid Spencer | e6328ca | 2006-12-04 21:33:23 +0000 | [diff] [blame] | 3949 | // the arguments into constants, and if so, try to constant propagate the | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3950 | // result.  This is particularly useful for computing loop exit values. | 
|  | 3951 | if (CanConstantFold(I)) { | 
|  | 3952 | std::vector<Constant*> Operands; | 
|  | 3953 | Operands.reserve(I->getNumOperands()); | 
|  | 3954 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { | 
|  | 3955 | Value *Op = I->getOperand(i); | 
|  | 3956 | if (Constant *C = dyn_cast<Constant>(Op)) { | 
|  | 3957 | Operands.push_back(C); | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3958 | } else { | 
| Chris Lattner | a8fbde3 | 2007-11-23 08:46:22 +0000 | [diff] [blame] | 3959 | // If any of the operands is non-constant and if they are | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3960 | // non-integer and non-pointer, don't even try to analyze them | 
|  | 3961 | // with scev techniques. | 
| Dan Gohman | 4bafc42 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3962 | if (!isSCEVable(Op->getType())) | 
| Chris Lattner | a8fbde3 | 2007-11-23 08:46:22 +0000 | [diff] [blame] | 3963 | return V; | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 3964 |  | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 3965 | const SCEV* OpV = getSCEVAtScope(Op, L); | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3966 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) { | 
| Dan Gohman | 4bafc42 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3967 | Constant *C = SC->getValue(); | 
|  | 3968 | if (C->getType() != Op->getType()) | 
|  | 3969 | C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, | 
|  | 3970 | Op->getType(), | 
|  | 3971 | false), | 
|  | 3972 | C, Op->getType()); | 
|  | 3973 | Operands.push_back(C); | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 3974 | } else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) { | 
| Dan Gohman | 4bafc42 | 2009-04-30 16:40:30 +0000 | [diff] [blame] | 3975 | if (Constant *C = dyn_cast<Constant>(SU->getValue())) { | 
|  | 3976 | if (C->getType() != Op->getType()) | 
|  | 3977 | C = | 
|  | 3978 | ConstantExpr::getCast(CastInst::getCastOpcode(C, false, | 
|  | 3979 | Op->getType(), | 
|  | 3980 | false), | 
|  | 3981 | C, Op->getType()); | 
|  | 3982 | Operands.push_back(C); | 
|  | 3983 | } else | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 3984 | return V; | 
|  | 3985 | } else { | 
|  | 3986 | return V; | 
|  | 3987 | } | 
|  | 3988 | } | 
|  | 3989 | } | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 3990 |  | 
| Chris Lattner | d2265b4 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3991 | Constant *C; | 
|  | 3992 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) | 
|  | 3993 | C = ConstantFoldCompareInstOperands(CI->getPredicate(), | 
| Owen Anderson | 39f00cc | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 3994 | &Operands[0], Operands.size(), | 
| Owen Anderson | 47db941 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3995 | getContext()); | 
| Chris Lattner | d2265b4 | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 3996 | else | 
|  | 3997 | C = ConstantFoldInstOperands(I->getOpcode(), I->getType(), | 
| Dan Gohman | 8a8ad7d | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 3998 | &Operands[0], Operands.size(), | 
| Owen Anderson | 47db941 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 3999 | getContext()); | 
| Dan Gohman | 9d203c6 | 2009-06-29 21:31:18 +0000 | [diff] [blame] | 4000 | return getSCEV(C); | 
| Chris Lattner | dd73047 | 2004-04-17 22:58:41 +0000 | [diff] [blame] | 4001 | } | 
|  | 4002 | } | 
|  | 4003 |  | 
|  | 4004 | // This is some other type of SCEVUnknown, just return it. | 
|  | 4005 | return V; | 
|  | 4006 | } | 
|  | 4007 |  | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4008 | if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4009 | // Avoid performing the look-up in the common case where the specified | 
|  | 4010 | // expression has no loop-variant portions. | 
|  | 4011 | for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4012 | const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4013 | if (OpAtScope != Comm->getOperand(i)) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4014 | // Okay, at least one of these operands is loop variant but might be | 
|  | 4015 | // foldable.  Build a new instance of the folded commutative expression. | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4016 | SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(), | 
|  | 4017 | Comm->op_begin()+i); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4018 | NewOps.push_back(OpAtScope); | 
|  | 4019 |  | 
|  | 4020 | for (++i; i != e; ++i) { | 
|  | 4021 | OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4022 | NewOps.push_back(OpAtScope); | 
|  | 4023 | } | 
|  | 4024 | if (isa<SCEVAddExpr>(Comm)) | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4025 | return getAddExpr(NewOps); | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 4026 | if (isa<SCEVMulExpr>(Comm)) | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4027 | return getMulExpr(NewOps); | 
| Nick Lewycky | cdb7e54 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 4028 | if (isa<SCEVSMaxExpr>(Comm)) | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4029 | return getSMaxExpr(NewOps); | 
| Nick Lewycky | 1c44ebc | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 4030 | if (isa<SCEVUMaxExpr>(Comm)) | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4031 | return getUMaxExpr(NewOps); | 
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4032 | llvm_unreachable("Unknown commutative SCEV type!"); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4033 | } | 
|  | 4034 | } | 
|  | 4035 | // If we got here, all operands are loop invariant. | 
|  | 4036 | return Comm; | 
|  | 4037 | } | 
|  | 4038 |  | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4039 | if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4040 | const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L); | 
|  | 4041 | const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L); | 
| Nick Lewycky | 5234830 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 4042 | if (LHS == Div->getLHS() && RHS == Div->getRHS()) | 
|  | 4043 | return Div;   // must be loop invariant | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4044 | return getUDivExpr(LHS, RHS); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4045 | } | 
|  | 4046 |  | 
|  | 4047 | // If this is a loop recurrence for a loop that does not contain L, then we | 
|  | 4048 | // are dealing with the final value computed by the loop. | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4049 | if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4050 | if (!L || !AddRec->getLoop()->contains(L->getHeader())) { | 
|  | 4051 | // To evaluate this recurrence, we need to know how many times the AddRec | 
|  | 4052 | // loop iterates.  Compute this now. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4053 | const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop()); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4054 | if (BackedgeTakenCount == getCouldNotCompute()) return AddRec; | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4055 |  | 
| Eli Friedman | 61f6762 | 2008-08-04 23:49:06 +0000 | [diff] [blame] | 4056 | // Then, evaluate the AddRec. | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4057 | return AddRec->evaluateAtIteration(BackedgeTakenCount, *this); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4058 | } | 
| Dan Gohman | 8ca0885 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 4059 | return AddRec; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4060 | } | 
|  | 4061 |  | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4062 | if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4063 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); | 
| Dan Gohman | 0098d01 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4064 | if (Op == Cast->getOperand()) | 
|  | 4065 | return Cast;  // must be loop invariant | 
|  | 4066 | return getZeroExtendExpr(Op, Cast->getType()); | 
|  | 4067 | } | 
|  | 4068 |  | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4069 | if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4070 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); | 
| Dan Gohman | 0098d01 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4071 | if (Op == Cast->getOperand()) | 
|  | 4072 | return Cast;  // must be loop invariant | 
|  | 4073 | return getSignExtendExpr(Op, Cast->getType()); | 
|  | 4074 | } | 
|  | 4075 |  | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4076 | if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4077 | const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); | 
| Dan Gohman | 0098d01 | 2009-04-29 22:29:01 +0000 | [diff] [blame] | 4078 | if (Op == Cast->getOperand()) | 
|  | 4079 | return Cast;  // must be loop invariant | 
|  | 4080 | return getTruncateExpr(Op, Cast->getType()); | 
|  | 4081 | } | 
|  | 4082 |  | 
| Dan Gohman | bf2a9ae | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 4083 | if (isa<SCEVTargetDataConstant>(V)) | 
|  | 4084 | return V; | 
|  | 4085 |  | 
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4086 | llvm_unreachable("Unknown SCEV type!"); | 
| Daniel Dunbar | a8c1658 | 2009-05-18 16:43:04 +0000 | [diff] [blame] | 4087 | return 0; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4088 | } | 
|  | 4089 |  | 
| Dan Gohman | b81f47d | 2009-05-08 20:38:54 +0000 | [diff] [blame] | 4090 | /// getSCEVAtScope - This is a convenience function which does | 
|  | 4091 | /// getSCEVAtScope(getSCEV(V), L). | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4092 | const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) { | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4093 | return getSCEVAtScope(getSCEV(V), L); | 
|  | 4094 | } | 
|  | 4095 |  | 
| Wojciech Matyjewicz | f0d21cd | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4096 | /// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the | 
|  | 4097 | /// following equation: | 
|  | 4098 | /// | 
|  | 4099 | ///     A * X = B (mod N) | 
|  | 4100 | /// | 
|  | 4101 | /// where N = 2^BW and BW is the common bit width of A and B. The signedness of | 
|  | 4102 | /// A and B isn't important. | 
|  | 4103 | /// | 
|  | 4104 | /// If the equation does not have a solution, SCEVCouldNotCompute is returned. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4105 | static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const APInt &B, | 
| Wojciech Matyjewicz | f0d21cd | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4106 | ScalarEvolution &SE) { | 
|  | 4107 | uint32_t BW = A.getBitWidth(); | 
|  | 4108 | assert(BW == B.getBitWidth() && "Bit widths must be the same."); | 
|  | 4109 | assert(A != 0 && "A must be non-zero."); | 
|  | 4110 |  | 
|  | 4111 | // 1. D = gcd(A, N) | 
|  | 4112 | // | 
|  | 4113 | // The gcd of A and N may have only one prime factor: 2. The number of | 
|  | 4114 | // trailing zeros in A is its multiplicity | 
|  | 4115 | uint32_t Mult2 = A.countTrailingZeros(); | 
|  | 4116 | // D = 2^Mult2 | 
|  | 4117 |  | 
|  | 4118 | // 2. Check if B is divisible by D. | 
|  | 4119 | // | 
|  | 4120 | // B is divisible by D if and only if the multiplicity of prime factor 2 for B | 
|  | 4121 | // is not less than multiplicity of this prime factor for D. | 
|  | 4122 | if (B.countTrailingZeros() < Mult2) | 
| Dan Gohman | 31efa30 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4123 | return SE.getCouldNotCompute(); | 
| Wojciech Matyjewicz | f0d21cd | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4124 |  | 
|  | 4125 | // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic | 
|  | 4126 | // modulo (N / D). | 
|  | 4127 | // | 
|  | 4128 | // (N / D) may need BW+1 bits in its representation.  Hence, we'll use this | 
|  | 4129 | // bit width during computations. | 
|  | 4130 | APInt AD = A.lshr(Mult2).zext(BW + 1);  // AD = A / D | 
|  | 4131 | APInt Mod(BW + 1, 0); | 
|  | 4132 | Mod.set(BW - Mult2);  // Mod = N / D | 
|  | 4133 | APInt I = AD.multiplicativeInverse(Mod); | 
|  | 4134 |  | 
|  | 4135 | // 4. Compute the minimum unsigned root of the equation: | 
|  | 4136 | // I * (B / D) mod (N / D) | 
|  | 4137 | APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod); | 
|  | 4138 |  | 
|  | 4139 | // The result is guaranteed to be less than 2^BW so we may truncate it to BW | 
|  | 4140 | // bits. | 
|  | 4141 | return SE.getConstant(Result.trunc(BW)); | 
|  | 4142 | } | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4143 |  | 
|  | 4144 | /// SolveQuadraticEquation - Find the roots of the quadratic equation for the | 
|  | 4145 | /// given quadratic chrec {L,+,M,+,N}.  This returns either the two roots (which | 
|  | 4146 | /// might be the same) or two SCEVCouldNotCompute objects. | 
|  | 4147 | /// | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4148 | static std::pair<const SCEV *,const SCEV *> | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4149 | SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4150 | assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4151 | const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0)); | 
|  | 4152 | const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1)); | 
|  | 4153 | const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2)); | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4154 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4155 | // We currently can only solve this if the coefficients are constants. | 
| Reid Spencer | 983e3b3 | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4156 | if (!LC || !MC || !NC) { | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4157 | const SCEV *CNC = SE.getCouldNotCompute(); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4158 | return std::make_pair(CNC, CNC); | 
|  | 4159 | } | 
|  | 4160 |  | 
| Reid Spencer | 983e3b3 | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4161 | uint32_t BitWidth = LC->getValue()->getValue().getBitWidth(); | 
| Chris Lattner | cad61e8 | 2007-04-15 19:52:49 +0000 | [diff] [blame] | 4162 | const APInt &L = LC->getValue()->getValue(); | 
|  | 4163 | const APInt &M = MC->getValue()->getValue(); | 
|  | 4164 | const APInt &N = NC->getValue()->getValue(); | 
| Reid Spencer | 983e3b3 | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4165 | APInt Two(BitWidth, 2); | 
|  | 4166 | APInt Four(BitWidth, 4); | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4167 |  | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4168 | { | 
| Reid Spencer | 983e3b3 | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4169 | using namespace APIntOps; | 
| Zhou Sheng | 2852d99 | 2007-04-07 17:48:27 +0000 | [diff] [blame] | 4170 | const APInt& C = L; | 
| Reid Spencer | 983e3b3 | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4171 | // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C | 
|  | 4172 | // The B coefficient is M-N/2 | 
|  | 4173 | APInt B(M); | 
|  | 4174 | B -= sdiv(N,Two); | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4175 |  | 
| Reid Spencer | 983e3b3 | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4176 | // The A coefficient is N/2 | 
| Zhou Sheng | 2852d99 | 2007-04-07 17:48:27 +0000 | [diff] [blame] | 4177 | APInt A(N.sdiv(Two)); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4178 |  | 
| Reid Spencer | 983e3b3 | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4179 | // Compute the B^2-4ac term. | 
|  | 4180 | APInt SqrtTerm(B); | 
|  | 4181 | SqrtTerm *= B; | 
|  | 4182 | SqrtTerm -= Four * (A * C); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4183 |  | 
| Reid Spencer | 983e3b3 | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4184 | // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest | 
|  | 4185 | // integer value or else APInt::sqrt() will assert. | 
|  | 4186 | APInt SqrtVal(SqrtTerm.sqrt()); | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4187 |  | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4188 | // Compute the two solutions for the quadratic formula. | 
| Reid Spencer | 983e3b3 | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4189 | // The divisions must be performed as signed divisions. | 
|  | 4190 | APInt NegB(-B); | 
| Reid Spencer | a3cfb8a | 2007-04-16 02:24:41 +0000 | [diff] [blame] | 4191 | APInt TwoA( A << 1 ); | 
| Nick Lewycky | 7b14e20 | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 4192 | if (TwoA.isMinValue()) { | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4193 | const SCEV *CNC = SE.getCouldNotCompute(); | 
| Nick Lewycky | 7b14e20 | 2008-11-03 02:43:49 +0000 | [diff] [blame] | 4194 | return std::make_pair(CNC, CNC); | 
|  | 4195 | } | 
|  | 4196 |  | 
| Owen Anderson | 47db941 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 4197 | LLVMContext &Context = SE.getContext(); | 
| Owen Anderson | f1f1743 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4198 |  | 
|  | 4199 | ConstantInt *Solution1 = | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4200 | ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA)); | 
| Owen Anderson | f1f1743 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4201 | ConstantInt *Solution2 = | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4202 | ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA)); | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4203 |  | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4204 | return std::make_pair(SE.getConstant(Solution1), | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4205 | SE.getConstant(Solution2)); | 
| Reid Spencer | 983e3b3 | 2007-03-01 07:25:48 +0000 | [diff] [blame] | 4206 | } // end APIntOps namespace | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4207 | } | 
|  | 4208 |  | 
|  | 4209 | /// HowFarToZero - Return the number of times a backedge comparing the specified | 
| Dan Gohman | 4c720c0 | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4210 | /// value to zero will execute.  If not computable, return CouldNotCompute. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4211 | const SCEV *ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4212 | // If the value is a constant | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4213 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4214 | // If the value is already zero, the branch will execute zero times. | 
| Reid Spencer | 2e54a15 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 4215 | if (C->getValue()->isZero()) return C; | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4216 | return getCouldNotCompute();  // Otherwise it will loop infinitely. | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4217 | } | 
|  | 4218 |  | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4219 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4220 | if (!AddRec || AddRec->getLoop() != L) | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4221 | return getCouldNotCompute(); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4222 |  | 
|  | 4223 | if (AddRec->isAffine()) { | 
| Wojciech Matyjewicz | f0d21cd | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4224 | // If this is an affine expression, the execution count of this branch is | 
|  | 4225 | // the minimum unsigned root of the following equation: | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4226 | // | 
| Wojciech Matyjewicz | f0d21cd | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4227 | //     Start + Step*N = 0 (mod 2^BW) | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4228 | // | 
| Wojciech Matyjewicz | f0d21cd | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4229 | // equivalent to: | 
|  | 4230 | // | 
|  | 4231 | //             Step*N = -Start (mod 2^BW) | 
|  | 4232 | // | 
|  | 4233 | // where BW is the common bit width of Start and Step. | 
|  | 4234 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4235 | // Get the initial value for the loop. | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4236 | const SCEV *Start = getSCEVAtScope(AddRec->getStart(), | 
|  | 4237 | L->getParentLoop()); | 
|  | 4238 | const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), | 
|  | 4239 | L->getParentLoop()); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4240 |  | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4241 | if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) { | 
| Wojciech Matyjewicz | f0d21cd | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4242 | // For now we handle only constant steps. | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4243 |  | 
| Wojciech Matyjewicz | f0d21cd | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4244 | // First, handle unitary steps. | 
|  | 4245 | if (StepC->getValue()->equalsInt(1))      // 1*N = -Start (mod 2^BW), so: | 
| Dan Gohman | 8a8ad7d | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 4246 | return getNegativeSCEV(Start);          //   N = -Start (as unsigned) | 
| Wojciech Matyjewicz | f0d21cd | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4247 | if (StepC->getValue()->isAllOnesValue())  // -1*N = -Start (mod 2^BW), so: | 
|  | 4248 | return Start;                           //    N = Start (as unsigned) | 
|  | 4249 |  | 
|  | 4250 | // Then, try to solve the above equation provided that Start is constant. | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4251 | if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) | 
| Wojciech Matyjewicz | f0d21cd | 2008-07-20 15:55:14 +0000 | [diff] [blame] | 4252 | return SolveLinEquationWithOverflow(StepC->getValue()->getValue(), | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4253 | -StartC->getValue()->getValue(), | 
|  | 4254 | *this); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4255 | } | 
| Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 4256 | } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4257 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of | 
|  | 4258 | // the quadratic equation to solve it. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4259 | std::pair<const SCEV *,const SCEV *> Roots = SolveQuadraticEquation(AddRec, | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4260 | *this); | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4261 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); | 
|  | 4262 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4263 | if (R1) { | 
| Chris Lattner | 0916921 | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 4264 | #if 0 | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 4265 | errs() << "HFTZ: " << *V << " - sol#1: " << *R1 | 
|  | 4266 | << "  sol#2: " << *R2 << "\n"; | 
| Chris Lattner | 0916921 | 2004-04-02 20:26:46 +0000 | [diff] [blame] | 4267 | #endif | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4268 | // Pick the smallest positive root value. | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4269 | if (ConstantInt *CB = | 
| Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 4270 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, | 
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 4271 | R1->getValue(), R2->getValue()))) { | 
| Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4272 | if (CB->getZExtValue() == false) | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4273 | std::swap(R1, R2);   // R1 is the minimum root now. | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4274 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4275 | // We can only use this value if the chrec ends up with an exact zero | 
|  | 4276 | // value at this index.  When solving for "X*X != 5", for example, we | 
|  | 4277 | // should not accept a root of 2. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4278 | const SCEV *Val = AddRec->evaluateAtIteration(R1, *this); | 
| Dan Gohman | be928e3 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 4279 | if (Val->isZero()) | 
|  | 4280 | return R1;  // We found a quadratic root! | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4281 | } | 
|  | 4282 | } | 
|  | 4283 | } | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4284 |  | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4285 | return getCouldNotCompute(); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4286 | } | 
|  | 4287 |  | 
|  | 4288 | /// HowFarToNonZero - Return the number of times a backedge checking the | 
|  | 4289 | /// specified value for nonzero will execute.  If not computable, return | 
| Dan Gohman | 4c720c0 | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4290 | /// CouldNotCompute | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4291 | const SCEV *ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4292 | // Loops that look like: while (X == 0) are very strange indeed.  We don't | 
|  | 4293 | // handle them yet except for the trivial case.  This could be expanded in the | 
|  | 4294 | // future as needed. | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4295 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4296 | // If the value is a constant, check to see if it is known to be non-zero | 
|  | 4297 | // already.  If so, the backedge will execute zero times. | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4298 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { | 
| Nick Lewycky | 5a3db14 | 2008-02-21 09:14:53 +0000 | [diff] [blame] | 4299 | if (!C->getValue()->isNullValue()) | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4300 | return getIntegerSCEV(0, C->getType()); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4301 | return getCouldNotCompute();  // Otherwise it will loop infinitely. | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4302 | } | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4303 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4304 | // We could implement others, but I really doubt anyone writes loops like | 
|  | 4305 | // this, and if they did, they would already be constant folded. | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4306 | return getCouldNotCompute(); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4307 | } | 
|  | 4308 |  | 
| Dan Gohman | 8c77f1a | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4309 | /// getLoopPredecessor - If the given loop's header has exactly one unique | 
|  | 4310 | /// predecessor outside the loop, return it. Otherwise return null. | 
|  | 4311 | /// | 
|  | 4312 | BasicBlock *ScalarEvolution::getLoopPredecessor(const Loop *L) { | 
|  | 4313 | BasicBlock *Header = L->getHeader(); | 
|  | 4314 | BasicBlock *Pred = 0; | 
|  | 4315 | for (pred_iterator PI = pred_begin(Header), E = pred_end(Header); | 
|  | 4316 | PI != E; ++PI) | 
|  | 4317 | if (!L->contains(*PI)) { | 
|  | 4318 | if (Pred && Pred != *PI) return 0; // Multiple predecessors. | 
|  | 4319 | Pred = *PI; | 
|  | 4320 | } | 
|  | 4321 | return Pred; | 
|  | 4322 | } | 
|  | 4323 |  | 
| Dan Gohman | f9081a2 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4324 | /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB | 
|  | 4325 | /// (which may not be an immediate predecessor) which has exactly one | 
|  | 4326 | /// successor from which BB is reachable, or null if no such block is | 
|  | 4327 | /// found. | 
|  | 4328 | /// | 
|  | 4329 | BasicBlock * | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4330 | ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) { | 
| Dan Gohman | fa066ef | 2009-04-30 20:48:53 +0000 | [diff] [blame] | 4331 | // If the block has a unique predecessor, then there is no path from the | 
|  | 4332 | // predecessor to the block that does not go through the direct edge | 
|  | 4333 | // from the predecessor to the block. | 
| Dan Gohman | f9081a2 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4334 | if (BasicBlock *Pred = BB->getSinglePredecessor()) | 
|  | 4335 | return Pred; | 
|  | 4336 |  | 
|  | 4337 | // A loop's header is defined to be a block that dominates the loop. | 
| Dan Gohman | 8c77f1a | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4338 | // If the header has a unique predecessor outside the loop, it must be | 
|  | 4339 | // a block that has exactly one successor that can reach the loop. | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 4340 | if (Loop *L = LI->getLoopFor(BB)) | 
| Dan Gohman | 8c77f1a | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4341 | return getLoopPredecessor(L); | 
| Dan Gohman | f9081a2 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4342 |  | 
|  | 4343 | return 0; | 
|  | 4344 | } | 
|  | 4345 |  | 
| Dan Gohman | 450f4e0 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4346 | /// HasSameValue - SCEV structural equivalence is usually sufficient for | 
|  | 4347 | /// testing whether two expressions are equal, however for the purposes of | 
|  | 4348 | /// looking for a condition guarding a loop, it can be useful to be a little | 
|  | 4349 | /// more general, since a front-end may have replicated the controlling | 
|  | 4350 | /// expression. | 
|  | 4351 | /// | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4352 | static bool HasSameValue(const SCEV *A, const SCEV *B) { | 
| Dan Gohman | 450f4e0 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4353 | // Quick check to see if they are the same SCEV. | 
|  | 4354 | if (A == B) return true; | 
|  | 4355 |  | 
|  | 4356 | // Otherwise, if they're both SCEVUnknown, it's possible that they hold | 
|  | 4357 | // two different instructions with the same value. Check for this case. | 
|  | 4358 | if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A)) | 
|  | 4359 | if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B)) | 
|  | 4360 | if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue())) | 
|  | 4361 | if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue())) | 
| Dan Gohman | 2d08556 | 2009-08-25 17:56:57 +0000 | [diff] [blame] | 4362 | if (AI->isIdenticalTo(BI) && !AI->mayReadFromMemory()) | 
| Dan Gohman | 450f4e0 | 2009-06-20 00:35:32 +0000 | [diff] [blame] | 4363 | return true; | 
|  | 4364 |  | 
|  | 4365 | // Otherwise assume they may have a different value. | 
|  | 4366 | return false; | 
|  | 4367 | } | 
|  | 4368 |  | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4369 | bool ScalarEvolution::isKnownNegative(const SCEV *S) { | 
|  | 4370 | return getSignedRange(S).getSignedMax().isNegative(); | 
|  | 4371 | } | 
|  | 4372 |  | 
|  | 4373 | bool ScalarEvolution::isKnownPositive(const SCEV *S) { | 
|  | 4374 | return getSignedRange(S).getSignedMin().isStrictlyPositive(); | 
|  | 4375 | } | 
|  | 4376 |  | 
|  | 4377 | bool ScalarEvolution::isKnownNonNegative(const SCEV *S) { | 
|  | 4378 | return !getSignedRange(S).getSignedMin().isNegative(); | 
|  | 4379 | } | 
|  | 4380 |  | 
|  | 4381 | bool ScalarEvolution::isKnownNonPositive(const SCEV *S) { | 
|  | 4382 | return !getSignedRange(S).getSignedMax().isStrictlyPositive(); | 
|  | 4383 | } | 
|  | 4384 |  | 
|  | 4385 | bool ScalarEvolution::isKnownNonZero(const SCEV *S) { | 
|  | 4386 | return isKnownNegative(S) || isKnownPositive(S); | 
|  | 4387 | } | 
|  | 4388 |  | 
|  | 4389 | bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred, | 
|  | 4390 | const SCEV *LHS, const SCEV *RHS) { | 
|  | 4391 |  | 
|  | 4392 | if (HasSameValue(LHS, RHS)) | 
|  | 4393 | return ICmpInst::isTrueWhenEqual(Pred); | 
|  | 4394 |  | 
|  | 4395 | switch (Pred) { | 
|  | 4396 | default: | 
| Dan Gohman | 8c129d7 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4397 | llvm_unreachable("Unexpected ICmpInst::Predicate value!"); | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4398 | break; | 
|  | 4399 | case ICmpInst::ICMP_SGT: | 
|  | 4400 | Pred = ICmpInst::ICMP_SLT; | 
|  | 4401 | std::swap(LHS, RHS); | 
|  | 4402 | case ICmpInst::ICMP_SLT: { | 
|  | 4403 | ConstantRange LHSRange = getSignedRange(LHS); | 
|  | 4404 | ConstantRange RHSRange = getSignedRange(RHS); | 
|  | 4405 | if (LHSRange.getSignedMax().slt(RHSRange.getSignedMin())) | 
|  | 4406 | return true; | 
|  | 4407 | if (LHSRange.getSignedMin().sge(RHSRange.getSignedMax())) | 
|  | 4408 | return false; | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4409 | break; | 
|  | 4410 | } | 
|  | 4411 | case ICmpInst::ICMP_SGE: | 
|  | 4412 | Pred = ICmpInst::ICMP_SLE; | 
|  | 4413 | std::swap(LHS, RHS); | 
|  | 4414 | case ICmpInst::ICMP_SLE: { | 
|  | 4415 | ConstantRange LHSRange = getSignedRange(LHS); | 
|  | 4416 | ConstantRange RHSRange = getSignedRange(RHS); | 
|  | 4417 | if (LHSRange.getSignedMax().sle(RHSRange.getSignedMin())) | 
|  | 4418 | return true; | 
|  | 4419 | if (LHSRange.getSignedMin().sgt(RHSRange.getSignedMax())) | 
|  | 4420 | return false; | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4421 | break; | 
|  | 4422 | } | 
|  | 4423 | case ICmpInst::ICMP_UGT: | 
|  | 4424 | Pred = ICmpInst::ICMP_ULT; | 
|  | 4425 | std::swap(LHS, RHS); | 
|  | 4426 | case ICmpInst::ICMP_ULT: { | 
|  | 4427 | ConstantRange LHSRange = getUnsignedRange(LHS); | 
|  | 4428 | ConstantRange RHSRange = getUnsignedRange(RHS); | 
|  | 4429 | if (LHSRange.getUnsignedMax().ult(RHSRange.getUnsignedMin())) | 
|  | 4430 | return true; | 
|  | 4431 | if (LHSRange.getUnsignedMin().uge(RHSRange.getUnsignedMax())) | 
|  | 4432 | return false; | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4433 | break; | 
|  | 4434 | } | 
|  | 4435 | case ICmpInst::ICMP_UGE: | 
|  | 4436 | Pred = ICmpInst::ICMP_ULE; | 
|  | 4437 | std::swap(LHS, RHS); | 
|  | 4438 | case ICmpInst::ICMP_ULE: { | 
|  | 4439 | ConstantRange LHSRange = getUnsignedRange(LHS); | 
|  | 4440 | ConstantRange RHSRange = getUnsignedRange(RHS); | 
|  | 4441 | if (LHSRange.getUnsignedMax().ule(RHSRange.getUnsignedMin())) | 
|  | 4442 | return true; | 
|  | 4443 | if (LHSRange.getUnsignedMin().ugt(RHSRange.getUnsignedMax())) | 
|  | 4444 | return false; | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4445 | break; | 
|  | 4446 | } | 
|  | 4447 | case ICmpInst::ICMP_NE: { | 
|  | 4448 | if (getUnsignedRange(LHS).intersectWith(getUnsignedRange(RHS)).isEmptySet()) | 
|  | 4449 | return true; | 
|  | 4450 | if (getSignedRange(LHS).intersectWith(getSignedRange(RHS)).isEmptySet()) | 
|  | 4451 | return true; | 
|  | 4452 |  | 
|  | 4453 | const SCEV *Diff = getMinusSCEV(LHS, RHS); | 
|  | 4454 | if (isKnownNonZero(Diff)) | 
|  | 4455 | return true; | 
|  | 4456 | break; | 
|  | 4457 | } | 
|  | 4458 | case ICmpInst::ICMP_EQ: | 
| Dan Gohman | 3439262 | 2009-07-20 23:54:43 +0000 | [diff] [blame] | 4459 | // The check at the top of the function catches the case where | 
|  | 4460 | // the values are known to be equal. | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4461 | break; | 
|  | 4462 | } | 
|  | 4463 | return false; | 
|  | 4464 | } | 
|  | 4465 |  | 
|  | 4466 | /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is | 
|  | 4467 | /// protected by a conditional between LHS and RHS.  This is used to | 
|  | 4468 | /// to eliminate casts. | 
|  | 4469 | bool | 
|  | 4470 | ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L, | 
|  | 4471 | ICmpInst::Predicate Pred, | 
|  | 4472 | const SCEV *LHS, const SCEV *RHS) { | 
|  | 4473 | // Interpret a null as meaning no loop, where there is obviously no guard | 
|  | 4474 | // (interprocedural conditions notwithstanding). | 
|  | 4475 | if (!L) return true; | 
|  | 4476 |  | 
|  | 4477 | BasicBlock *Latch = L->getLoopLatch(); | 
|  | 4478 | if (!Latch) | 
|  | 4479 | return false; | 
|  | 4480 |  | 
|  | 4481 | BranchInst *LoopContinuePredicate = | 
|  | 4482 | dyn_cast<BranchInst>(Latch->getTerminator()); | 
|  | 4483 | if (!LoopContinuePredicate || | 
|  | 4484 | LoopContinuePredicate->isUnconditional()) | 
|  | 4485 | return false; | 
|  | 4486 |  | 
| Dan Gohman | 430f0cc | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4487 | return isImpliedCond(LoopContinuePredicate->getCondition(), Pred, LHS, RHS, | 
|  | 4488 | LoopContinuePredicate->getSuccessor(0) != L->getHeader()); | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4489 | } | 
|  | 4490 |  | 
|  | 4491 | /// isLoopGuardedByCond - Test whether entry to the loop is protected | 
|  | 4492 | /// by a conditional between LHS and RHS.  This is used to help avoid max | 
|  | 4493 | /// expressions in loop trip counts, and to eliminate casts. | 
|  | 4494 | bool | 
|  | 4495 | ScalarEvolution::isLoopGuardedByCond(const Loop *L, | 
|  | 4496 | ICmpInst::Predicate Pred, | 
|  | 4497 | const SCEV *LHS, const SCEV *RHS) { | 
| Dan Gohman | 9cf09f8 | 2009-05-18 16:03:58 +0000 | [diff] [blame] | 4498 | // Interpret a null as meaning no loop, where there is obviously no guard | 
|  | 4499 | // (interprocedural conditions notwithstanding). | 
|  | 4500 | if (!L) return false; | 
|  | 4501 |  | 
| Dan Gohman | 8c77f1a | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4502 | BasicBlock *Predecessor = getLoopPredecessor(L); | 
|  | 4503 | BasicBlock *PredecessorDest = L->getHeader(); | 
| Nick Lewycky | b5688cc | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4504 |  | 
| Dan Gohman | 8c77f1a | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4505 | // Starting at the loop predecessor, climb up the predecessor chain, as long | 
|  | 4506 | // as there are predecessors that can be found that have unique successors | 
| Dan Gohman | f9081a2 | 2008-09-15 22:18:04 +0000 | [diff] [blame] | 4507 | // leading to the original header. | 
| Dan Gohman | 8c77f1a | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4508 | for (; Predecessor; | 
|  | 4509 | PredecessorDest = Predecessor, | 
|  | 4510 | Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) { | 
| Dan Gohman | 2a62fd9 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4511 |  | 
|  | 4512 | BranchInst *LoopEntryPredicate = | 
| Dan Gohman | 8c77f1a | 2009-05-18 15:36:09 +0000 | [diff] [blame] | 4513 | dyn_cast<BranchInst>(Predecessor->getTerminator()); | 
| Dan Gohman | 2a62fd9 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4514 | if (!LoopEntryPredicate || | 
|  | 4515 | LoopEntryPredicate->isUnconditional()) | 
|  | 4516 | continue; | 
|  | 4517 |  | 
| Dan Gohman | 430f0cc | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4518 | if (isImpliedCond(LoopEntryPredicate->getCondition(), Pred, LHS, RHS, | 
|  | 4519 | LoopEntryPredicate->getSuccessor(0) != PredecessorDest)) | 
| Dan Gohman | 2a62fd9 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4520 | return true; | 
| Nick Lewycky | b5688cc | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4521 | } | 
|  | 4522 |  | 
| Dan Gohman | 2a62fd9 | 2008-08-12 20:17:31 +0000 | [diff] [blame] | 4523 | return false; | 
| Nick Lewycky | b5688cc | 2008-07-12 07:41:32 +0000 | [diff] [blame] | 4524 | } | 
|  | 4525 |  | 
| Dan Gohman | 430f0cc | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4526 | /// isImpliedCond - Test whether the condition described by Pred, LHS, | 
|  | 4527 | /// and RHS is true whenever the given Cond value evaluates to true. | 
|  | 4528 | bool ScalarEvolution::isImpliedCond(Value *CondValue, | 
|  | 4529 | ICmpInst::Predicate Pred, | 
|  | 4530 | const SCEV *LHS, const SCEV *RHS, | 
|  | 4531 | bool Inverse) { | 
| Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4532 | // Recursivly handle And and Or conditions. | 
|  | 4533 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CondValue)) { | 
|  | 4534 | if (BO->getOpcode() == Instruction::And) { | 
|  | 4535 | if (!Inverse) | 
| Dan Gohman | 430f0cc | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4536 | return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) || | 
|  | 4537 | isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse); | 
| Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4538 | } else if (BO->getOpcode() == Instruction::Or) { | 
|  | 4539 | if (Inverse) | 
| Dan Gohman | 430f0cc | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4540 | return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) || | 
|  | 4541 | isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse); | 
| Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4542 | } | 
|  | 4543 | } | 
|  | 4544 |  | 
|  | 4545 | ICmpInst *ICI = dyn_cast<ICmpInst>(CondValue); | 
|  | 4546 | if (!ICI) return false; | 
|  | 4547 |  | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4548 | // Bail if the ICmp's operands' types are wider than the needed type | 
|  | 4549 | // before attempting to call getSCEV on them. This avoids infinite | 
|  | 4550 | // recursion, since the analysis of widening casts can require loop | 
|  | 4551 | // exit condition information for overflow checking, which would | 
|  | 4552 | // lead back here. | 
|  | 4553 | if (getTypeSizeInBits(LHS->getType()) < | 
| Dan Gohman | 430f0cc | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4554 | getTypeSizeInBits(ICI->getOperand(0)->getType())) | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4555 | return false; | 
|  | 4556 |  | 
| Dan Gohman | 430f0cc | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4557 | // Now that we found a conditional branch that dominates the loop, check to | 
|  | 4558 | // see if it is the comparison we are looking for. | 
|  | 4559 | ICmpInst::Predicate FoundPred; | 
|  | 4560 | if (Inverse) | 
|  | 4561 | FoundPred = ICI->getInversePredicate(); | 
|  | 4562 | else | 
|  | 4563 | FoundPred = ICI->getPredicate(); | 
|  | 4564 |  | 
|  | 4565 | const SCEV *FoundLHS = getSCEV(ICI->getOperand(0)); | 
|  | 4566 | const SCEV *FoundRHS = getSCEV(ICI->getOperand(1)); | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4567 |  | 
|  | 4568 | // Balance the types. The case where FoundLHS' type is wider than | 
|  | 4569 | // LHS' type is checked for above. | 
|  | 4570 | if (getTypeSizeInBits(LHS->getType()) > | 
|  | 4571 | getTypeSizeInBits(FoundLHS->getType())) { | 
|  | 4572 | if (CmpInst::isSigned(Pred)) { | 
|  | 4573 | FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType()); | 
|  | 4574 | FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType()); | 
|  | 4575 | } else { | 
|  | 4576 | FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType()); | 
|  | 4577 | FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType()); | 
|  | 4578 | } | 
|  | 4579 | } | 
|  | 4580 |  | 
| Dan Gohman | 430f0cc | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4581 | // Canonicalize the query to match the way instcombine will have | 
|  | 4582 | // canonicalized the comparison. | 
|  | 4583 | // First, put a constant operand on the right. | 
|  | 4584 | if (isa<SCEVConstant>(LHS)) { | 
|  | 4585 | std::swap(LHS, RHS); | 
|  | 4586 | Pred = ICmpInst::getSwappedPredicate(Pred); | 
|  | 4587 | } | 
|  | 4588 | // Then, canonicalize comparisons with boundary cases. | 
|  | 4589 | if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) { | 
|  | 4590 | const APInt &RA = RC->getValue()->getValue(); | 
|  | 4591 | switch (Pred) { | 
|  | 4592 | default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); | 
|  | 4593 | case ICmpInst::ICMP_EQ: | 
|  | 4594 | case ICmpInst::ICMP_NE: | 
|  | 4595 | break; | 
|  | 4596 | case ICmpInst::ICMP_UGE: | 
|  | 4597 | if ((RA - 1).isMinValue()) { | 
|  | 4598 | Pred = ICmpInst::ICMP_NE; | 
|  | 4599 | RHS = getConstant(RA - 1); | 
|  | 4600 | break; | 
|  | 4601 | } | 
|  | 4602 | if (RA.isMaxValue()) { | 
|  | 4603 | Pred = ICmpInst::ICMP_EQ; | 
|  | 4604 | break; | 
|  | 4605 | } | 
|  | 4606 | if (RA.isMinValue()) return true; | 
|  | 4607 | break; | 
|  | 4608 | case ICmpInst::ICMP_ULE: | 
|  | 4609 | if ((RA + 1).isMaxValue()) { | 
|  | 4610 | Pred = ICmpInst::ICMP_NE; | 
|  | 4611 | RHS = getConstant(RA + 1); | 
|  | 4612 | break; | 
|  | 4613 | } | 
|  | 4614 | if (RA.isMinValue()) { | 
|  | 4615 | Pred = ICmpInst::ICMP_EQ; | 
|  | 4616 | break; | 
|  | 4617 | } | 
|  | 4618 | if (RA.isMaxValue()) return true; | 
|  | 4619 | break; | 
|  | 4620 | case ICmpInst::ICMP_SGE: | 
|  | 4621 | if ((RA - 1).isMinSignedValue()) { | 
|  | 4622 | Pred = ICmpInst::ICMP_NE; | 
|  | 4623 | RHS = getConstant(RA - 1); | 
|  | 4624 | break; | 
|  | 4625 | } | 
|  | 4626 | if (RA.isMaxSignedValue()) { | 
|  | 4627 | Pred = ICmpInst::ICMP_EQ; | 
|  | 4628 | break; | 
|  | 4629 | } | 
|  | 4630 | if (RA.isMinSignedValue()) return true; | 
|  | 4631 | break; | 
|  | 4632 | case ICmpInst::ICMP_SLE: | 
|  | 4633 | if ((RA + 1).isMaxSignedValue()) { | 
|  | 4634 | Pred = ICmpInst::ICMP_NE; | 
|  | 4635 | RHS = getConstant(RA + 1); | 
|  | 4636 | break; | 
|  | 4637 | } | 
|  | 4638 | if (RA.isMinSignedValue()) { | 
|  | 4639 | Pred = ICmpInst::ICMP_EQ; | 
|  | 4640 | break; | 
|  | 4641 | } | 
|  | 4642 | if (RA.isMaxSignedValue()) return true; | 
|  | 4643 | break; | 
|  | 4644 | case ICmpInst::ICMP_UGT: | 
|  | 4645 | if (RA.isMinValue()) { | 
|  | 4646 | Pred = ICmpInst::ICMP_NE; | 
|  | 4647 | break; | 
|  | 4648 | } | 
|  | 4649 | if ((RA + 1).isMaxValue()) { | 
|  | 4650 | Pred = ICmpInst::ICMP_EQ; | 
|  | 4651 | RHS = getConstant(RA + 1); | 
|  | 4652 | break; | 
|  | 4653 | } | 
|  | 4654 | if (RA.isMaxValue()) return false; | 
|  | 4655 | break; | 
|  | 4656 | case ICmpInst::ICMP_ULT: | 
|  | 4657 | if (RA.isMaxValue()) { | 
|  | 4658 | Pred = ICmpInst::ICMP_NE; | 
|  | 4659 | break; | 
|  | 4660 | } | 
|  | 4661 | if ((RA - 1).isMinValue()) { | 
|  | 4662 | Pred = ICmpInst::ICMP_EQ; | 
|  | 4663 | RHS = getConstant(RA - 1); | 
|  | 4664 | break; | 
|  | 4665 | } | 
|  | 4666 | if (RA.isMinValue()) return false; | 
|  | 4667 | break; | 
|  | 4668 | case ICmpInst::ICMP_SGT: | 
|  | 4669 | if (RA.isMinSignedValue()) { | 
|  | 4670 | Pred = ICmpInst::ICMP_NE; | 
|  | 4671 | break; | 
|  | 4672 | } | 
|  | 4673 | if ((RA + 1).isMaxSignedValue()) { | 
|  | 4674 | Pred = ICmpInst::ICMP_EQ; | 
|  | 4675 | RHS = getConstant(RA + 1); | 
|  | 4676 | break; | 
|  | 4677 | } | 
|  | 4678 | if (RA.isMaxSignedValue()) return false; | 
|  | 4679 | break; | 
|  | 4680 | case ICmpInst::ICMP_SLT: | 
|  | 4681 | if (RA.isMaxSignedValue()) { | 
|  | 4682 | Pred = ICmpInst::ICMP_NE; | 
|  | 4683 | break; | 
|  | 4684 | } | 
|  | 4685 | if ((RA - 1).isMinSignedValue()) { | 
|  | 4686 | Pred = ICmpInst::ICMP_EQ; | 
|  | 4687 | RHS = getConstant(RA - 1); | 
|  | 4688 | break; | 
|  | 4689 | } | 
|  | 4690 | if (RA.isMinSignedValue()) return false; | 
|  | 4691 | break; | 
|  | 4692 | } | 
|  | 4693 | } | 
|  | 4694 |  | 
|  | 4695 | // Check to see if we can make the LHS or RHS match. | 
|  | 4696 | if (LHS == FoundRHS || RHS == FoundLHS) { | 
|  | 4697 | if (isa<SCEVConstant>(RHS)) { | 
|  | 4698 | std::swap(FoundLHS, FoundRHS); | 
|  | 4699 | FoundPred = ICmpInst::getSwappedPredicate(FoundPred); | 
|  | 4700 | } else { | 
|  | 4701 | std::swap(LHS, RHS); | 
|  | 4702 | Pred = ICmpInst::getSwappedPredicate(Pred); | 
|  | 4703 | } | 
|  | 4704 | } | 
|  | 4705 |  | 
|  | 4706 | // Check whether the found predicate is the same as the desired predicate. | 
|  | 4707 | if (FoundPred == Pred) | 
|  | 4708 | return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS); | 
|  | 4709 |  | 
|  | 4710 | // Check whether swapping the found predicate makes it the same as the | 
|  | 4711 | // desired predicate. | 
|  | 4712 | if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) { | 
|  | 4713 | if (isa<SCEVConstant>(RHS)) | 
|  | 4714 | return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS); | 
|  | 4715 | else | 
|  | 4716 | return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred), | 
|  | 4717 | RHS, LHS, FoundLHS, FoundRHS); | 
|  | 4718 | } | 
|  | 4719 |  | 
|  | 4720 | // Check whether the actual condition is beyond sufficient. | 
|  | 4721 | if (FoundPred == ICmpInst::ICMP_EQ) | 
|  | 4722 | if (ICmpInst::isTrueWhenEqual(Pred)) | 
|  | 4723 | if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS)) | 
|  | 4724 | return true; | 
|  | 4725 | if (Pred == ICmpInst::ICMP_NE) | 
|  | 4726 | if (!ICmpInst::isTrueWhenEqual(FoundPred)) | 
|  | 4727 | if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS)) | 
|  | 4728 | return true; | 
|  | 4729 |  | 
|  | 4730 | // Otherwise assume the worst. | 
|  | 4731 | return false; | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4732 | } | 
|  | 4733 |  | 
| Dan Gohman | 430f0cc | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4734 | /// isImpliedCondOperands - Test whether the condition described by Pred, | 
|  | 4735 | /// LHS, and RHS is true whenever the condition desribed by Pred, FoundLHS, | 
|  | 4736 | /// and FoundRHS is true. | 
|  | 4737 | bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred, | 
|  | 4738 | const SCEV *LHS, const SCEV *RHS, | 
|  | 4739 | const SCEV *FoundLHS, | 
|  | 4740 | const SCEV *FoundRHS) { | 
|  | 4741 | return isImpliedCondOperandsHelper(Pred, LHS, RHS, | 
|  | 4742 | FoundLHS, FoundRHS) || | 
|  | 4743 | // ~x < ~y --> x > y | 
|  | 4744 | isImpliedCondOperandsHelper(Pred, LHS, RHS, | 
|  | 4745 | getNotSCEV(FoundRHS), | 
|  | 4746 | getNotSCEV(FoundLHS)); | 
|  | 4747 | } | 
|  | 4748 |  | 
|  | 4749 | /// isImpliedCondOperandsHelper - Test whether the condition described by | 
|  | 4750 | /// Pred, LHS, and RHS is true whenever the condition desribed by Pred, | 
|  | 4751 | /// FoundLHS, and FoundRHS is true. | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4752 | bool | 
| Dan Gohman | 430f0cc | 2009-07-21 23:03:19 +0000 | [diff] [blame] | 4753 | ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred, | 
|  | 4754 | const SCEV *LHS, const SCEV *RHS, | 
|  | 4755 | const SCEV *FoundLHS, | 
|  | 4756 | const SCEV *FoundRHS) { | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4757 | switch (Pred) { | 
| Dan Gohman | 8c129d7 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4758 | default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); | 
|  | 4759 | case ICmpInst::ICMP_EQ: | 
|  | 4760 | case ICmpInst::ICMP_NE: | 
|  | 4761 | if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS)) | 
|  | 4762 | return true; | 
|  | 4763 | break; | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4764 | case ICmpInst::ICMP_SLT: | 
| Dan Gohman | 8c129d7 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4765 | case ICmpInst::ICMP_SLE: | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4766 | if (isKnownPredicate(ICmpInst::ICMP_SLE, LHS, FoundLHS) && | 
|  | 4767 | isKnownPredicate(ICmpInst::ICMP_SGE, RHS, FoundRHS)) | 
|  | 4768 | return true; | 
|  | 4769 | break; | 
|  | 4770 | case ICmpInst::ICMP_SGT: | 
| Dan Gohman | 8c129d7 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4771 | case ICmpInst::ICMP_SGE: | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4772 | if (isKnownPredicate(ICmpInst::ICMP_SGE, LHS, FoundLHS) && | 
|  | 4773 | isKnownPredicate(ICmpInst::ICMP_SLE, RHS, FoundRHS)) | 
|  | 4774 | return true; | 
|  | 4775 | break; | 
|  | 4776 | case ICmpInst::ICMP_ULT: | 
| Dan Gohman | 8c129d7 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4777 | case ICmpInst::ICMP_ULE: | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4778 | if (isKnownPredicate(ICmpInst::ICMP_ULE, LHS, FoundLHS) && | 
|  | 4779 | isKnownPredicate(ICmpInst::ICMP_UGE, RHS, FoundRHS)) | 
|  | 4780 | return true; | 
|  | 4781 | break; | 
|  | 4782 | case ICmpInst::ICMP_UGT: | 
| Dan Gohman | 8c129d7 | 2009-07-16 17:34:36 +0000 | [diff] [blame] | 4783 | case ICmpInst::ICMP_UGE: | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4784 | if (isKnownPredicate(ICmpInst::ICMP_UGE, LHS, FoundLHS) && | 
|  | 4785 | isKnownPredicate(ICmpInst::ICMP_ULE, RHS, FoundRHS)) | 
|  | 4786 | return true; | 
|  | 4787 | break; | 
|  | 4788 | } | 
|  | 4789 |  | 
|  | 4790 | return false; | 
| Dan Gohman | f19aeec | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 4791 | } | 
|  | 4792 |  | 
| Dan Gohman | 0104842 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4793 | /// getBECount - Subtract the end and start values and divide by the step, | 
|  | 4794 | /// rounding up, to get the number of times the backedge is executed. Return | 
|  | 4795 | /// CouldNotCompute if an intermediate computation overflows. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4796 | const SCEV *ScalarEvolution::getBECount(const SCEV *Start, | 
| Dan Gohman | fc76994 | 2009-07-13 22:05:32 +0000 | [diff] [blame] | 4797 | const SCEV *End, | 
|  | 4798 | const SCEV *Step) { | 
| Dan Gohman | 0104842 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4799 | const Type *Ty = Start->getType(); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4800 | const SCEV *NegOne = getIntegerSCEV(-1, Ty); | 
|  | 4801 | const SCEV *Diff = getMinusSCEV(End, Start); | 
|  | 4802 | const SCEV *RoundUp = getAddExpr(Step, NegOne); | 
| Dan Gohman | 0104842 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4803 |  | 
|  | 4804 | // Add an adjustment to the difference between End and Start so that | 
|  | 4805 | // the division will effectively round up. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4806 | const SCEV *Add = getAddExpr(Diff, RoundUp); | 
| Dan Gohman | 0104842 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4807 |  | 
|  | 4808 | // Check Add for unsigned overflow. | 
|  | 4809 | // TODO: More sophisticated things could be done here. | 
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 4810 | const Type *WideTy = IntegerType::get(getContext(), | 
|  | 4811 | getTypeSizeInBits(Ty) + 1); | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4812 | const SCEV *EDiff = getZeroExtendExpr(Diff, WideTy); | 
|  | 4813 | const SCEV *ERoundUp = getZeroExtendExpr(RoundUp, WideTy); | 
|  | 4814 | const SCEV *OperandExtendedAdd = getAddExpr(EDiff, ERoundUp); | 
| Dan Gohman | 0104842 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4815 | if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd) | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4816 | return getCouldNotCompute(); | 
| Dan Gohman | 0104842 | 2009-06-21 23:46:38 +0000 | [diff] [blame] | 4817 |  | 
|  | 4818 | return getUDivExpr(Add, Step); | 
|  | 4819 | } | 
|  | 4820 |  | 
| Chris Lattner | 587a75b | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4821 | /// HowManyLessThans - Return the number of times a backedge containing the | 
|  | 4822 | /// specified less-than comparison will execute.  If not computable, return | 
| Dan Gohman | 4c720c0 | 2009-06-06 14:37:11 +0000 | [diff] [blame] | 4823 | /// CouldNotCompute. | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4824 | ScalarEvolution::BackedgeTakenInfo | 
|  | 4825 | ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS, | 
|  | 4826 | const Loop *L, bool isSigned) { | 
| Chris Lattner | 587a75b | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4827 | // Only handle:  "ADDREC < LoopInvariant". | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4828 | if (!RHS->isLoopInvariant(L)) return getCouldNotCompute(); | 
| Chris Lattner | 587a75b | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4829 |  | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4830 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS); | 
| Chris Lattner | 587a75b | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4831 | if (!AddRec || AddRec->getLoop() != L) | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4832 | return getCouldNotCompute(); | 
| Chris Lattner | 587a75b | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4833 |  | 
|  | 4834 | if (AddRec->isAffine()) { | 
| Nick Lewycky | 5234830 | 2009-01-13 09:18:58 +0000 | [diff] [blame] | 4835 | // FORNOW: We only support unit strides. | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4836 | unsigned BitWidth = getTypeSizeInBits(AddRec->getType()); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4837 | const SCEV *Step = AddRec->getStepRecurrence(*this); | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4838 |  | 
|  | 4839 | // TODO: handle non-constant strides. | 
|  | 4840 | const SCEVConstant *CStep = dyn_cast<SCEVConstant>(Step); | 
|  | 4841 | if (!CStep || CStep->isZero()) | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4842 | return getCouldNotCompute(); | 
| Dan Gohman | ba7f6d8 | 2009-05-18 15:22:39 +0000 | [diff] [blame] | 4843 | if (CStep->isOne()) { | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4844 | // With unit stride, the iteration never steps past the limit value. | 
|  | 4845 | } else if (CStep->getValue()->getValue().isStrictlyPositive()) { | 
|  | 4846 | if (const SCEVConstant *CLimit = dyn_cast<SCEVConstant>(RHS)) { | 
|  | 4847 | // Test whether a positive iteration iteration can step past the limit | 
|  | 4848 | // value and past the maximum value for its type in a single step. | 
|  | 4849 | if (isSigned) { | 
|  | 4850 | APInt Max = APInt::getSignedMaxValue(BitWidth); | 
|  | 4851 | if ((Max - CStep->getValue()->getValue()) | 
|  | 4852 | .slt(CLimit->getValue()->getValue())) | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4853 | return getCouldNotCompute(); | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4854 | } else { | 
|  | 4855 | APInt Max = APInt::getMaxValue(BitWidth); | 
|  | 4856 | if ((Max - CStep->getValue()->getValue()) | 
|  | 4857 | .ult(CLimit->getValue()->getValue())) | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4858 | return getCouldNotCompute(); | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4859 | } | 
|  | 4860 | } else | 
|  | 4861 | // TODO: handle non-constant limit values below. | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4862 | return getCouldNotCompute(); | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4863 | } else | 
|  | 4864 | // TODO: handle negative strides below. | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4865 | return getCouldNotCompute(); | 
| Chris Lattner | 587a75b | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4866 |  | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4867 | // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant | 
|  | 4868 | // m.  So, we count the number of iterations in which {n,+,s} < m is true. | 
|  | 4869 | // Note that we cannot simply return max(m-n,0)/s because it's not safe to | 
| Wojciech Matyjewicz | 0e411f6 | 2008-02-13 12:21:32 +0000 | [diff] [blame] | 4870 | // treat m-n as signed nor unsigned due to overflow possibility. | 
| Chris Lattner | 587a75b | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4871 |  | 
| Wojciech Matyjewicz | 35545fd | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4872 | // First, we get the value of the LHS in the first iteration: n | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4873 | const SCEV *Start = AddRec->getOperand(0); | 
| Wojciech Matyjewicz | 35545fd | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4874 |  | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4875 | // Determine the minimum constant start value. | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4876 | const SCEV *MinStart = getConstant(isSigned ? | 
|  | 4877 | getSignedRange(Start).getSignedMin() : | 
|  | 4878 | getUnsignedRange(Start).getUnsignedMin()); | 
| Wojciech Matyjewicz | 35545fd | 2008-02-13 11:51:34 +0000 | [diff] [blame] | 4879 |  | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4880 | // If we know that the condition is true in order to enter the loop, | 
|  | 4881 | // then we know that it will run exactly (m-n)/s times. Otherwise, we | 
| Dan Gohman | 4d5435d | 2009-05-24 23:45:28 +0000 | [diff] [blame] | 4882 | // only know that it will execute (max(m,n)-n)/s times. In both cases, | 
|  | 4883 | // the division must round up. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4884 | const SCEV *End = RHS; | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4885 | if (!isLoopGuardedByCond(L, | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4886 | isSigned ? ICmpInst::ICMP_SLT : | 
|  | 4887 | ICmpInst::ICMP_ULT, | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4888 | getMinusSCEV(Start, Step), RHS)) | 
|  | 4889 | End = isSigned ? getSMaxExpr(RHS, Start) | 
|  | 4890 | : getUMaxExpr(RHS, Start); | 
|  | 4891 |  | 
|  | 4892 | // Determine the maximum constant end value. | 
| Dan Gohman | e65c917 | 2009-07-13 21:35:55 +0000 | [diff] [blame] | 4893 | const SCEV *MaxEnd = getConstant(isSigned ? | 
|  | 4894 | getSignedRange(End).getSignedMax() : | 
|  | 4895 | getUnsignedRange(End).getUnsignedMax()); | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4896 |  | 
|  | 4897 | // Finally, we subtract these two values and divide, rounding up, to get | 
|  | 4898 | // the number of times the backedge is executed. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4899 | const SCEV *BECount = getBECount(Start, End, Step); | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4900 |  | 
|  | 4901 | // The maximum backedge count is similar, except using the minimum start | 
|  | 4902 | // value and the maximum end value. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4903 | const SCEV *MaxBECount = getBECount(MinStart, MaxEnd, Step); | 
| Dan Gohman | 2b8da35 | 2009-04-30 20:47:05 +0000 | [diff] [blame] | 4904 |  | 
|  | 4905 | return BackedgeTakenInfo(BECount, MaxBECount); | 
| Chris Lattner | 587a75b | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4906 | } | 
|  | 4907 |  | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 4908 | return getCouldNotCompute(); | 
| Chris Lattner | 587a75b | 2005-08-15 23:33:51 +0000 | [diff] [blame] | 4909 | } | 
|  | 4910 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4911 | /// getNumIterationsInRange - Return the number of iterations of this loop that | 
|  | 4912 | /// produce values in the specified constant range.  Another way of looking at | 
|  | 4913 | /// this is that it returns the first iteration number where the value is not in | 
|  | 4914 | /// the condition, thus computing the exit count. If the iteration count can't | 
|  | 4915 | /// be computed, an instance of SCEVCouldNotCompute is returned. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4916 | const SCEV *SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4917 | ScalarEvolution &SE) const { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4918 | if (Range.isFullSet())  // Infinite loop. | 
| Dan Gohman | 31efa30 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4919 | return SE.getCouldNotCompute(); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4920 |  | 
|  | 4921 | // If the start is a non-zero constant, shift the range to simplify things. | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4922 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart())) | 
| Reid Spencer | 2e54a15 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 4923 | if (!SC->getValue()->isZero()) { | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4924 | SmallVector<const SCEV *, 4> Operands(op_begin(), op_end()); | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4925 | Operands[0] = SE.getIntegerSCEV(0, SC->getType()); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4926 | const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop()); | 
| Dan Gohman | a30370b | 2009-05-04 22:02:23 +0000 | [diff] [blame] | 4927 | if (const SCEVAddRecExpr *ShiftedAddRec = | 
|  | 4928 | dyn_cast<SCEVAddRecExpr>(Shifted)) | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4929 | return ShiftedAddRec->getNumIterationsInRange( | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4930 | Range.subtract(SC->getValue()->getValue()), SE); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4931 | // This is strange and shouldn't happen. | 
| Dan Gohman | 31efa30 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4932 | return SE.getCouldNotCompute(); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4933 | } | 
|  | 4934 |  | 
|  | 4935 | // The only time we can solve this is when we have all constant indices. | 
|  | 4936 | // Otherwise, we cannot determine the overflow conditions. | 
|  | 4937 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) | 
|  | 4938 | if (!isa<SCEVConstant>(getOperand(i))) | 
| Dan Gohman | 31efa30 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4939 | return SE.getCouldNotCompute(); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4940 |  | 
|  | 4941 |  | 
|  | 4942 | // Okay at this point we know that all elements of the chrec are constants and | 
|  | 4943 | // that the start element is zero. | 
|  | 4944 |  | 
|  | 4945 | // First check to see if the range contains zero.  If not, the first | 
|  | 4946 | // iteration exits. | 
| Dan Gohman | b397e1a | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 4947 | unsigned BitWidth = SE.getTypeSizeInBits(getType()); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4948 | if (!Range.contains(APInt(BitWidth, 0))) | 
| Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 4949 | return SE.getIntegerSCEV(0, getType()); | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 4950 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4951 | if (isAffine()) { | 
|  | 4952 | // If this is an affine expression then we have this situation: | 
|  | 4953 | //   Solve {0,+,A} in Range  ===  Ax in Range | 
|  | 4954 |  | 
| Nick Lewycky | 5246026 | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 4955 | // We know that zero is in the range.  If A is positive then we know that | 
|  | 4956 | // the upper value of the range must be the first possible exit value. | 
|  | 4957 | // If A is negative then the lower of the range is the last possible loop | 
|  | 4958 | // value.  Also note that we already checked for a full range. | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 4959 | APInt One(BitWidth,1); | 
| Nick Lewycky | 5246026 | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 4960 | APInt A     = cast<SCEVConstant>(getOperand(1))->getValue()->getValue(); | 
|  | 4961 | APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower(); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4962 |  | 
| Nick Lewycky | 5246026 | 2007-07-16 02:08:00 +0000 | [diff] [blame] | 4963 | // The exit value should be (End+A)/A. | 
| Nick Lewycky | 3934961 | 2007-09-27 14:12:54 +0000 | [diff] [blame] | 4964 | APInt ExitVal = (End + A).udiv(A); | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4965 | ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4966 |  | 
|  | 4967 | // Evaluate at the exit value.  If we really did fall out of the valid | 
|  | 4968 | // range, then we computed our trip count, otherwise wrap around or other | 
|  | 4969 | // things must have happened. | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4970 | ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE); | 
| Reid Spencer | 6a44033 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 4971 | if (Range.contains(Val->getValue())) | 
| Dan Gohman | 31efa30 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 4972 | return SE.getCouldNotCompute();  // Something strange happened | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4973 |  | 
|  | 4974 | // Ensure that the previous value is in the range.  This is a sanity check. | 
| Reid Spencer | 3a7e9d8 | 2007-02-28 19:57:34 +0000 | [diff] [blame] | 4975 | assert(Range.contains( | 
| Dan Gohman | ce973df | 2009-06-24 04:48:43 +0000 | [diff] [blame] | 4976 | EvaluateConstantChrecAtConstant(this, | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 4977 | ConstantInt::get(SE.getContext(), ExitVal - One), SE)->getValue()) && | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4978 | "Linear scev computation is off in a bad way!"); | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4979 | return SE.getConstant(ExitValue); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4980 | } else if (isQuadratic()) { | 
|  | 4981 | // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the | 
|  | 4982 | // quadratic equation to solve it.  To do this, we must frame our problem in | 
|  | 4983 | // terms of figuring out when zero is crossed, instead of when | 
|  | 4984 | // Range.getUpper() is crossed. | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4985 | SmallVector<const SCEV *, 4> NewOps(op_begin(), op_end()); | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4986 | NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper())); | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4987 | const SCEV *NewAddRec = SE.getAddRecExpr(NewOps, getLoop()); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4988 |  | 
|  | 4989 | // Next, solve the constructed addrec | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 4990 | std::pair<const SCEV *,const SCEV *> Roots = | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 4991 | SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE); | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 4992 | const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); | 
|  | 4993 | const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 4994 | if (R1) { | 
|  | 4995 | // Pick the smallest positive root value. | 
| Zhou Sheng | 75b871f | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 4996 | if (ConstantInt *CB = | 
| Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 4997 | dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, | 
| Owen Anderson | f1f1743 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 4998 | R1->getValue(), R2->getValue()))) { | 
| Reid Spencer | cddc9df | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 4999 | if (CB->getZExtValue() == false) | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5000 | std::swap(R1, R2);   // R1 is the minimum root now. | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5001 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5002 | // Make sure the root is not off by one.  The returned iteration should | 
|  | 5003 | // not be in the range, but the previous one should be.  When solving | 
|  | 5004 | // for "X*X < 5", for example, we should not return a root of 2. | 
|  | 5005 | ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this, | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5006 | R1->getValue(), | 
|  | 5007 | SE); | 
| Reid Spencer | 6a44033 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5008 | if (Range.contains(R1Val->getValue())) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5009 | // The next iteration must be out of the range... | 
| Owen Anderson | f1f1743 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 5010 | ConstantInt *NextVal = | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5011 | ConstantInt::get(SE.getContext(), R1->getValue()->getValue()+1); | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5012 |  | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5013 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); | 
| Reid Spencer | 6a44033 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5014 | if (!Range.contains(R1Val->getValue())) | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5015 | return SE.getConstant(NextVal); | 
| Dan Gohman | 31efa30 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5016 | return SE.getCouldNotCompute();  // Something strange happened | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5017 | } | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5018 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5019 | // If R1 was not in the range, then it is a good return value.  Make | 
|  | 5020 | // sure that R1-1 WAS in the range though, just in case. | 
| Owen Anderson | f1f1743 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 5021 | ConstantInt *NextVal = | 
| Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 5022 | ConstantInt::get(SE.getContext(), R1->getValue()->getValue()-1); | 
| Dan Gohman | a37eaf2 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 5023 | R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); | 
| Reid Spencer | 6a44033 | 2007-03-01 07:54:15 +0000 | [diff] [blame] | 5024 | if (Range.contains(R1Val->getValue())) | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5025 | return R1; | 
| Dan Gohman | 31efa30 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5026 | return SE.getCouldNotCompute();  // Something strange happened | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5027 | } | 
|  | 5028 | } | 
|  | 5029 | } | 
|  | 5030 |  | 
| Dan Gohman | 31efa30 | 2009-04-18 17:58:19 +0000 | [diff] [blame] | 5031 | return SE.getCouldNotCompute(); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5032 | } | 
|  | 5033 |  | 
|  | 5034 |  | 
|  | 5035 |  | 
|  | 5036 | //===----------------------------------------------------------------------===// | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5037 | //                   SCEVCallbackVH Class Implementation | 
|  | 5038 | //===----------------------------------------------------------------------===// | 
|  | 5039 |  | 
| Dan Gohman | d33a090 | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 5040 | void ScalarEvolution::SCEVCallbackVH::deleted() { | 
| Dan Gohman | dd707af | 2009-07-13 22:20:53 +0000 | [diff] [blame] | 5041 | assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5042 | if (PHINode *PN = dyn_cast<PHINode>(getValPtr())) | 
|  | 5043 | SE->ConstantEvolutionLoopExitValue.erase(PN); | 
|  | 5044 | SE->Scalars.erase(getValPtr()); | 
|  | 5045 | // this now dangles! | 
|  | 5046 | } | 
|  | 5047 |  | 
| Dan Gohman | d33a090 | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 5048 | void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *) { | 
| Dan Gohman | dd707af | 2009-07-13 22:20:53 +0000 | [diff] [blame] | 5049 | assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5050 |  | 
|  | 5051 | // Forget all the expressions associated with users of the old value, | 
|  | 5052 | // so that future queries will recompute the expressions using the new | 
|  | 5053 | // value. | 
|  | 5054 | SmallVector<User *, 16> Worklist; | 
| Dan Gohman | f34f863 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5055 | SmallPtrSet<User *, 8> Visited; | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5056 | Value *Old = getValPtr(); | 
|  | 5057 | bool DeleteOld = false; | 
|  | 5058 | for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end(); | 
|  | 5059 | UI != UE; ++UI) | 
|  | 5060 | Worklist.push_back(*UI); | 
|  | 5061 | while (!Worklist.empty()) { | 
|  | 5062 | User *U = Worklist.pop_back_val(); | 
|  | 5063 | // Deleting the Old value will cause this to dangle. Postpone | 
|  | 5064 | // that until everything else is done. | 
|  | 5065 | if (U == Old) { | 
|  | 5066 | DeleteOld = true; | 
|  | 5067 | continue; | 
|  | 5068 | } | 
| Dan Gohman | f34f863 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5069 | if (!Visited.insert(U)) | 
|  | 5070 | continue; | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5071 | if (PHINode *PN = dyn_cast<PHINode>(U)) | 
|  | 5072 | SE->ConstantEvolutionLoopExitValue.erase(PN); | 
| Dan Gohman | f34f863 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5073 | SE->Scalars.erase(U); | 
|  | 5074 | for (Value::use_iterator UI = U->use_begin(), UE = U->use_end(); | 
|  | 5075 | UI != UE; ++UI) | 
|  | 5076 | Worklist.push_back(*UI); | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5077 | } | 
| Dan Gohman | f34f863 | 2009-07-14 14:34:04 +0000 | [diff] [blame] | 5078 | // Delete the Old value if it (indirectly) references itself. | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5079 | if (DeleteOld) { | 
|  | 5080 | if (PHINode *PN = dyn_cast<PHINode>(Old)) | 
|  | 5081 | SE->ConstantEvolutionLoopExitValue.erase(PN); | 
|  | 5082 | SE->Scalars.erase(Old); | 
|  | 5083 | // this now dangles! | 
|  | 5084 | } | 
|  | 5085 | // this may dangle! | 
|  | 5086 | } | 
|  | 5087 |  | 
| Dan Gohman | d33a090 | 2009-05-19 19:22:47 +0000 | [diff] [blame] | 5088 | ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) | 
| Dan Gohman | 48f8222 | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 5089 | : CallbackVH(V), SE(se) {} | 
|  | 5090 |  | 
|  | 5091 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5092 | //                   ScalarEvolution Class Implementation | 
|  | 5093 | //===----------------------------------------------------------------------===// | 
|  | 5094 |  | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5095 | ScalarEvolution::ScalarEvolution() | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5096 | : FunctionPass(&ID) { | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5097 | } | 
|  | 5098 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5099 | bool ScalarEvolution::runOnFunction(Function &F) { | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5100 | this->F = &F; | 
|  | 5101 | LI = &getAnalysis<LoopInfo>(); | 
|  | 5102 | TD = getAnalysisIfAvailable<TargetData>(); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5103 | return false; | 
|  | 5104 | } | 
|  | 5105 |  | 
|  | 5106 | void ScalarEvolution::releaseMemory() { | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5107 | Scalars.clear(); | 
|  | 5108 | BackedgeTakenCounts.clear(); | 
|  | 5109 | ConstantEvolutionLoopExitValue.clear(); | 
| Dan Gohman | 5122d61 | 2009-05-08 20:47:27 +0000 | [diff] [blame] | 5110 | ValuesAtScopes.clear(); | 
| Dan Gohman | c5c85c0 | 2009-06-27 21:21:31 +0000 | [diff] [blame] | 5111 | UniqueSCEVs.clear(); | 
|  | 5112 | SCEVAllocator.Reset(); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5113 | } | 
|  | 5114 |  | 
|  | 5115 | void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const { | 
|  | 5116 | AU.setPreservesAll(); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5117 | AU.addRequiredTransitive<LoopInfo>(); | 
| Dan Gohman | 0a40ad9 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 5118 | } | 
|  | 5119 |  | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5120 | bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) { | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5121 | return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L)); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5122 | } | 
|  | 5123 |  | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5124 | static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5125 | const Loop *L) { | 
|  | 5126 | // Print all inner loops first | 
|  | 5127 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) | 
|  | 5128 | PrintLoopInfo(OS, SE, *I); | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5129 |  | 
| Nick Lewycky | d1200b0 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 5130 | OS << "Loop " << L->getHeader()->getName() << ": "; | 
| Chris Lattner | d72c3eb | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 5131 |  | 
| Devang Patel | b5933bb | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 5132 | SmallVector<BasicBlock*, 8> ExitBlocks; | 
| Chris Lattner | d72c3eb | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 5133 | L->getExitBlocks(ExitBlocks); | 
|  | 5134 | if (ExitBlocks.size() != 1) | 
| Nick Lewycky | d1200b0 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 5135 | OS << "<multiple exits> "; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5136 |  | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5137 | if (SE->hasLoopInvariantBackedgeTakenCount(L)) { | 
|  | 5138 | OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5139 | } else { | 
| Dan Gohman | 0bddac1 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 5140 | OS << "Unpredictable backedge-taken count. "; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5141 | } | 
|  | 5142 |  | 
| Nick Lewycky | d1200b0 | 2008-01-02 02:49:20 +0000 | [diff] [blame] | 5143 | OS << "\n"; | 
| Dan Gohman | 6994293 | 2009-06-24 00:33:16 +0000 | [diff] [blame] | 5144 | OS << "Loop " << L->getHeader()->getName() << ": "; | 
|  | 5145 |  | 
|  | 5146 | if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) { | 
|  | 5147 | OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L); | 
|  | 5148 | } else { | 
|  | 5149 | OS << "Unpredictable max backedge-taken count. "; | 
|  | 5150 | } | 
|  | 5151 |  | 
|  | 5152 | OS << "\n"; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5153 | } | 
|  | 5154 |  | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 5155 | void ScalarEvolution::print(raw_ostream &OS, const Module* ) const { | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5156 | // ScalarEvolution's implementaiton of the print method is to print | 
|  | 5157 | // out SCEV values of all instructions that are interesting. Doing | 
|  | 5158 | // this potentially causes it to create new SCEV objects though, | 
|  | 5159 | // which technically conflicts with the const qualifier. This isn't | 
| Dan Gohman | 028e615 | 2009-07-10 20:25:29 +0000 | [diff] [blame] | 5160 | // observable from outside the class though, so casting away the | 
|  | 5161 | // const isn't dangerous. | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5162 | ScalarEvolution &SE = *const_cast<ScalarEvolution*>(this); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5163 |  | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5164 | OS << "Classifying expressions for: " << F->getName() << "\n"; | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5165 | for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) | 
| Dan Gohman | 7216b9d | 2009-04-30 01:30:18 +0000 | [diff] [blame] | 5166 | if (isSCEVable(I->getType())) { | 
| Dan Gohman | fda3c4a | 2009-07-13 23:03:05 +0000 | [diff] [blame] | 5167 | OS << *I << '\n'; | 
| Dan Gohman | 81313fd | 2008-09-14 17:21:12 +0000 | [diff] [blame] | 5168 | OS << "  -->  "; | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5169 | const SCEV *SV = SE.getSCEV(&*I); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5170 | SV->print(OS); | 
| Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 5171 |  | 
| Dan Gohman | b9063a8 | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 5172 | const Loop *L = LI->getLoopFor((*I).getParent()); | 
|  | 5173 |  | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5174 | const SCEV *AtUse = SE.getSCEVAtScope(SV, L); | 
| Dan Gohman | b9063a8 | 2009-06-19 17:49:54 +0000 | [diff] [blame] | 5175 | if (AtUse != SV) { | 
|  | 5176 | OS << "  -->  "; | 
|  | 5177 | AtUse->print(OS); | 
|  | 5178 | } | 
|  | 5179 |  | 
|  | 5180 | if (L) { | 
| Dan Gohman | 94c468f | 2009-06-18 00:37:45 +0000 | [diff] [blame] | 5181 | OS << "\t\t" "Exits: "; | 
| Dan Gohman | af75234 | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 5182 | const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop()); | 
| Dan Gohman | 8ca0885 | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 5183 | if (!ExitValue->isLoopInvariant(L)) { | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5184 | OS << "<<Unknown>>"; | 
|  | 5185 | } else { | 
|  | 5186 | OS << *ExitValue; | 
|  | 5187 | } | 
|  | 5188 | } | 
|  | 5189 |  | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5190 | OS << "\n"; | 
|  | 5191 | } | 
|  | 5192 |  | 
| Dan Gohman | c8e2362 | 2009-04-21 23:15:49 +0000 | [diff] [blame] | 5193 | OS << "Determining loop execution counts for: " << F->getName() << "\n"; | 
|  | 5194 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) | 
|  | 5195 | PrintLoopInfo(OS, &SE, *I); | 
| Chris Lattner | d934c70 | 2004-04-02 20:23:17 +0000 | [diff] [blame] | 5196 | } | 
| Dan Gohman | e20f824 | 2009-04-21 00:47:46 +0000 | [diff] [blame] | 5197 |  |