blob: 68aa595aa8dd80440eecb9f8663ac7acb8009225 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- ScalarEvolution.cpp - Scalar Evolution Analysis ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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
17// can handle. These classes are reference counted, managed by the SCEVHandle
18// class. We only create one SCEV of a particular shape, so pointer-comparisons
19// for equality are legal.
20//
21// One important aspect of the SCEV objects is that they are never cyclic, even
22// if there is a cycle in the dataflow for an expression (ie, a PHI node). If
23// the PHI node is one of the idioms that we can represent (e.g., a polynomial
24// recurrence) then we represent it directly as a recurrence node, otherwise we
25// represent it as a SCEVUnknown node.
26//
27// In addition to being able to represent expressions of various types, we also
28// have folders that are used to build the *canonical* representation for a
29// particular expression. These folders are capable of using a variety of
30// rewrite rules to simplify the expressions.
31//
32// Once the folders are defined, we can implement the more interesting
33// higher-level code, such as the code that recognizes PHI nodes of various
34// types, computes the execution count of a loop, etc.
35//
36// TODO: We should use these routines and value representations to implement
37// dependence analysis!
38//
39//===----------------------------------------------------------------------===//
40//
41// There are several good references for the techniques used in this analysis.
42//
43// Chains of recurrences -- a method to expedite the evaluation
44// of closed-form functions
45// Olaf Bachmann, Paul S. Wang, Eugene V. Zima
46//
47// On computational properties of chains of recurrences
48// Eugene V. Zima
49//
50// Symbolic Evaluation of Chains of Recurrences for Loop Optimization
51// Robert A. van Engelen
52//
53// Efficient Symbolic Analysis for Optimizing Compilers
54// Robert A. van Engelen
55//
56// Using the chains of recurrences algebra for data dependence testing and
57// induction variable substitution
58// MS Thesis, Johnie Birch
59//
60//===----------------------------------------------------------------------===//
61
62#define DEBUG_TYPE "scalar-evolution"
63#include "llvm/Analysis/ScalarEvolutionExpressions.h"
64#include "llvm/Constants.h"
65#include "llvm/DerivedTypes.h"
66#include "llvm/GlobalVariable.h"
67#include "llvm/Instructions.h"
68#include "llvm/Analysis/ConstantFolding.h"
Evan Cheng98c073b2009-02-17 00:13:06 +000069#include "llvm/Analysis/Dominators.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070#include "llvm/Analysis/LoopInfo.h"
Dan Gohmana7726c32009-06-16 19:52:01 +000071#include "llvm/Analysis/ValueTracking.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072#include "llvm/Assembly/Writer.h"
Dan Gohman01c2ee72009-04-16 03:18:22 +000073#include "llvm/Target/TargetData.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074#include "llvm/Support/CommandLine.h"
75#include "llvm/Support/Compiler.h"
76#include "llvm/Support/ConstantRange.h"
Dan Gohman01c2ee72009-04-16 03:18:22 +000077#include "llvm/Support/GetElementPtrTypeIterator.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078#include "llvm/Support/InstIterator.h"
79#include "llvm/Support/ManagedStatic.h"
80#include "llvm/Support/MathExtras.h"
Dan Gohman13058cc2009-04-21 00:47:46 +000081#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082#include "llvm/ADT/Statistic.h"
Dan Gohman01c2ee72009-04-16 03:18:22 +000083#include "llvm/ADT/STLExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084#include <algorithm>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085using namespace llvm;
86
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087STATISTIC(NumArrayLenItCounts,
88 "Number of trip counts computed with array length");
89STATISTIC(NumTripCountsComputed,
90 "Number of loops with predictable loop counts");
91STATISTIC(NumTripCountsNotComputed,
92 "Number of loops without predictable loop counts");
93STATISTIC(NumBruteForceTripCountsComputed,
94 "Number of loops with trip counts computed by force");
95
Dan Gohman089efff2008-05-13 00:00:25 +000096static cl::opt<unsigned>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden,
98 cl::desc("Maximum number of iterations SCEV will "
99 "symbolically execute a constant derived loop"),
100 cl::init(100));
101
Dan Gohman089efff2008-05-13 00:00:25 +0000102static RegisterPass<ScalarEvolution>
103R("scalar-evolution", "Scalar Evolution Analysis", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104char ScalarEvolution::ID = 0;
105
106//===----------------------------------------------------------------------===//
107// SCEV class definitions
108//===----------------------------------------------------------------------===//
109
110//===----------------------------------------------------------------------===//
111// Implementation of the SCEV class.
112//
113SCEV::~SCEV() {}
114void SCEV::dump() const {
Dan Gohman13058cc2009-04-21 00:47:46 +0000115 print(errs());
116 errs() << '\n';
117}
118
119void SCEV::print(std::ostream &o) const {
120 raw_os_ostream OS(o);
121 print(OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122}
123
Dan Gohman7b560c42008-06-18 16:23:07 +0000124bool SCEV::isZero() const {
125 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
126 return SC->getValue()->isZero();
127 return false;
128}
129
Dan Gohmanf8bc8e82009-05-18 15:22:39 +0000130bool SCEV::isOne() const {
131 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
132 return SC->getValue()->isOne();
133 return false;
134}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135
Owen Andersoncf4e2302009-06-18 22:25:12 +0000136SCEVCouldNotCompute::SCEVCouldNotCompute(const ScalarEvolution* p) :
137 SCEV(scCouldNotCompute, p) {}
Dan Gohmanffd36ba2009-04-21 23:15:49 +0000138SCEVCouldNotCompute::~SCEVCouldNotCompute() {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139
140bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const {
141 assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
142 return false;
143}
144
145const Type *SCEVCouldNotCompute::getType() const {
146 assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
147 return 0;
148}
149
150bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const {
151 assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
152 return false;
153}
154
155SCEVHandle SCEVCouldNotCompute::
156replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
Dan Gohman89f85052007-10-22 18:31:58 +0000157 const SCEVHandle &Conc,
158 ScalarEvolution &SE) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 return this;
160}
161
Dan Gohman13058cc2009-04-21 00:47:46 +0000162void SCEVCouldNotCompute::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 OS << "***COULDNOTCOMPUTE***";
164}
165
166bool SCEVCouldNotCompute::classof(const SCEV *S) {
167 return S->getSCEVType() == scCouldNotCompute;
168}
169
170
171// SCEVConstants - Only allow the creation of one SCEVConstant for any
172// particular value. Don't use a SCEVHandle here, or else the object will
173// never be deleted!
174static ManagedStatic<std::map<ConstantInt*, SCEVConstant*> > SCEVConstants;
175
176
177SCEVConstant::~SCEVConstant() {
178 SCEVConstants->erase(V);
179}
180
Dan Gohman89f85052007-10-22 18:31:58 +0000181SCEVHandle ScalarEvolution::getConstant(ConstantInt *V) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 SCEVConstant *&R = (*SCEVConstants)[V];
Owen Andersoncf4e2302009-06-18 22:25:12 +0000183 if (R == 0) R = new SCEVConstant(V, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 return R;
185}
186
Dan Gohman89f85052007-10-22 18:31:58 +0000187SCEVHandle ScalarEvolution::getConstant(const APInt& Val) {
188 return getConstant(ConstantInt::get(Val));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189}
190
Dan Gohman8fd520a2009-06-15 22:12:54 +0000191SCEVHandle
192ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) {
193 return getConstant(ConstantInt::get(cast<IntegerType>(Ty), V, isSigned));
194}
195
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196const Type *SCEVConstant::getType() const { return V->getType(); }
197
Dan Gohman13058cc2009-04-21 00:47:46 +0000198void SCEVConstant::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 WriteAsOperand(OS, V, false);
200}
201
Dan Gohman2a381532009-04-21 01:25:57 +0000202SCEVCastExpr::SCEVCastExpr(unsigned SCEVTy,
Owen Andersoncf4e2302009-06-18 22:25:12 +0000203 const SCEVHandle &op, const Type *ty,
204 const ScalarEvolution* p)
205 : SCEV(SCEVTy, p), Op(op), Ty(ty) {}
Dan Gohman2a381532009-04-21 01:25:57 +0000206
207SCEVCastExpr::~SCEVCastExpr() {}
208
209bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
210 return Op->dominates(BB, DT);
211}
212
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213// SCEVTruncates - Only allow the creation of one SCEVTruncateExpr for any
214// particular input. Don't use a SCEVHandle here, or else the object will
215// never be deleted!
Dan Gohmanbff6b582009-05-04 22:30:44 +0000216static ManagedStatic<std::map<std::pair<const SCEV*, const Type*>,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 SCEVTruncateExpr*> > SCEVTruncates;
218
Owen Andersoncf4e2302009-06-18 22:25:12 +0000219SCEVTruncateExpr::SCEVTruncateExpr(const SCEVHandle &op, const Type *ty,
220 const ScalarEvolution* p)
221 : SCEVCastExpr(scTruncate, op, ty, p) {
Dan Gohman01c2ee72009-04-16 03:18:22 +0000222 assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
223 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 "Cannot truncate non-integer value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225}
226
227SCEVTruncateExpr::~SCEVTruncateExpr() {
228 SCEVTruncates->erase(std::make_pair(Op, Ty));
229}
230
Dan Gohman13058cc2009-04-21 00:47:46 +0000231void SCEVTruncateExpr::print(raw_ostream &OS) const {
Dan Gohmanc9119222009-04-29 20:27:52 +0000232 OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233}
234
235// SCEVZeroExtends - Only allow the creation of one SCEVZeroExtendExpr for any
236// particular input. Don't use a SCEVHandle here, or else the object will never
237// be deleted!
Dan Gohmanbff6b582009-05-04 22:30:44 +0000238static ManagedStatic<std::map<std::pair<const SCEV*, const Type*>,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 SCEVZeroExtendExpr*> > SCEVZeroExtends;
240
Owen Andersoncf4e2302009-06-18 22:25:12 +0000241SCEVZeroExtendExpr::SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty,
242 const ScalarEvolution* p)
243 : SCEVCastExpr(scZeroExtend, op, ty, p) {
Dan Gohman01c2ee72009-04-16 03:18:22 +0000244 assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
245 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 "Cannot zero extend non-integer value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247}
248
249SCEVZeroExtendExpr::~SCEVZeroExtendExpr() {
250 SCEVZeroExtends->erase(std::make_pair(Op, Ty));
251}
252
Dan Gohman13058cc2009-04-21 00:47:46 +0000253void SCEVZeroExtendExpr::print(raw_ostream &OS) const {
Dan Gohmanc9119222009-04-29 20:27:52 +0000254 OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255}
256
257// SCEVSignExtends - Only allow the creation of one SCEVSignExtendExpr for any
258// particular input. Don't use a SCEVHandle here, or else the object will never
259// be deleted!
Dan Gohmanbff6b582009-05-04 22:30:44 +0000260static ManagedStatic<std::map<std::pair<const SCEV*, const Type*>,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 SCEVSignExtendExpr*> > SCEVSignExtends;
262
Owen Andersoncf4e2302009-06-18 22:25:12 +0000263SCEVSignExtendExpr::SCEVSignExtendExpr(const SCEVHandle &op, const Type *ty,
264 const ScalarEvolution* p)
265 : SCEVCastExpr(scSignExtend, op, ty, p) {
Dan Gohman01c2ee72009-04-16 03:18:22 +0000266 assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
267 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 "Cannot sign extend non-integer value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269}
270
271SCEVSignExtendExpr::~SCEVSignExtendExpr() {
272 SCEVSignExtends->erase(std::make_pair(Op, Ty));
273}
274
Dan Gohman13058cc2009-04-21 00:47:46 +0000275void SCEVSignExtendExpr::print(raw_ostream &OS) const {
Dan Gohmanc9119222009-04-29 20:27:52 +0000276 OS << "(sext " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277}
278
279// SCEVCommExprs - Only allow the creation of one SCEVCommutativeExpr for any
280// particular input. Don't use a SCEVHandle here, or else the object will never
281// be deleted!
Dan Gohmanbff6b582009-05-04 22:30:44 +0000282static ManagedStatic<std::map<std::pair<unsigned, std::vector<const SCEV*> >,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 SCEVCommutativeExpr*> > SCEVCommExprs;
284
285SCEVCommutativeExpr::~SCEVCommutativeExpr() {
Dan Gohmanbff6b582009-05-04 22:30:44 +0000286 std::vector<const SCEV*> SCEVOps(Operands.begin(), Operands.end());
287 SCEVCommExprs->erase(std::make_pair(getSCEVType(), SCEVOps));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288}
289
Dan Gohman13058cc2009-04-21 00:47:46 +0000290void SCEVCommutativeExpr::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 assert(Operands.size() > 1 && "This plus expr shouldn't exist!");
292 const char *OpStr = getOperationStr();
293 OS << "(" << *Operands[0];
294 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
295 OS << OpStr << *Operands[i];
296 OS << ")";
297}
298
299SCEVHandle SCEVCommutativeExpr::
300replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
Dan Gohman89f85052007-10-22 18:31:58 +0000301 const SCEVHandle &Conc,
302 ScalarEvolution &SE) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohman89f85052007-10-22 18:31:58 +0000304 SCEVHandle H =
305 getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 if (H != getOperand(i)) {
Dan Gohman02ff9392009-06-14 22:47:23 +0000307 SmallVector<SCEVHandle, 8> NewOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 NewOps.reserve(getNumOperands());
309 for (unsigned j = 0; j != i; ++j)
310 NewOps.push_back(getOperand(j));
311 NewOps.push_back(H);
312 for (++i; i != e; ++i)
313 NewOps.push_back(getOperand(i)->
Dan Gohman89f85052007-10-22 18:31:58 +0000314 replaceSymbolicValuesWithConcrete(Sym, Conc, SE));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315
316 if (isa<SCEVAddExpr>(this))
Dan Gohman89f85052007-10-22 18:31:58 +0000317 return SE.getAddExpr(NewOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 else if (isa<SCEVMulExpr>(this))
Dan Gohman89f85052007-10-22 18:31:58 +0000319 return SE.getMulExpr(NewOps);
Nick Lewycky711640a2007-11-25 22:41:31 +0000320 else if (isa<SCEVSMaxExpr>(this))
321 return SE.getSMaxExpr(NewOps);
Nick Lewyckye7a24ff2008-02-20 06:48:22 +0000322 else if (isa<SCEVUMaxExpr>(this))
323 return SE.getUMaxExpr(NewOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 else
325 assert(0 && "Unknown commutative expr!");
326 }
327 }
328 return this;
329}
330
Dan Gohman72a8a022009-05-07 14:00:19 +0000331bool SCEVNAryExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
Evan Cheng98c073b2009-02-17 00:13:06 +0000332 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
333 if (!getOperand(i)->dominates(BB, DT))
334 return false;
335 }
336 return true;
337}
338
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000340// SCEVUDivs - Only allow the creation of one SCEVUDivExpr for any particular
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341// input. Don't use a SCEVHandle here, or else the object will never be
342// deleted!
Dan Gohmanbff6b582009-05-04 22:30:44 +0000343static ManagedStatic<std::map<std::pair<const SCEV*, const SCEV*>,
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000344 SCEVUDivExpr*> > SCEVUDivs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000346SCEVUDivExpr::~SCEVUDivExpr() {
347 SCEVUDivs->erase(std::make_pair(LHS, RHS));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348}
349
Evan Cheng98c073b2009-02-17 00:13:06 +0000350bool SCEVUDivExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
351 return LHS->dominates(BB, DT) && RHS->dominates(BB, DT);
352}
353
Dan Gohman13058cc2009-04-21 00:47:46 +0000354void SCEVUDivExpr::print(raw_ostream &OS) const {
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000355 OS << "(" << *LHS << " /u " << *RHS << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356}
357
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000358const Type *SCEVUDivExpr::getType() const {
Dan Gohman140f08f2009-05-26 17:44:05 +0000359 // In most cases the types of LHS and RHS will be the same, but in some
360 // crazy cases one or the other may be a pointer. ScalarEvolution doesn't
361 // depend on the type for correctness, but handling types carefully can
362 // avoid extra casts in the SCEVExpander. The LHS is more likely to be
363 // a pointer type than the RHS, so use the RHS' type here.
364 return RHS->getType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365}
366
367// SCEVAddRecExprs - Only allow the creation of one SCEVAddRecExpr for any
368// particular input. Don't use a SCEVHandle here, or else the object will never
369// be deleted!
Dan Gohmanbff6b582009-05-04 22:30:44 +0000370static ManagedStatic<std::map<std::pair<const Loop *,
371 std::vector<const SCEV*> >,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 SCEVAddRecExpr*> > SCEVAddRecExprs;
373
374SCEVAddRecExpr::~SCEVAddRecExpr() {
Dan Gohmanbff6b582009-05-04 22:30:44 +0000375 std::vector<const SCEV*> SCEVOps(Operands.begin(), Operands.end());
376 SCEVAddRecExprs->erase(std::make_pair(L, SCEVOps));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377}
378
379SCEVHandle SCEVAddRecExpr::
380replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
Dan Gohman89f85052007-10-22 18:31:58 +0000381 const SCEVHandle &Conc,
382 ScalarEvolution &SE) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Dan Gohman89f85052007-10-22 18:31:58 +0000384 SCEVHandle H =
385 getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 if (H != getOperand(i)) {
Dan Gohman02ff9392009-06-14 22:47:23 +0000387 SmallVector<SCEVHandle, 8> NewOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388 NewOps.reserve(getNumOperands());
389 for (unsigned j = 0; j != i; ++j)
390 NewOps.push_back(getOperand(j));
391 NewOps.push_back(H);
392 for (++i; i != e; ++i)
393 NewOps.push_back(getOperand(i)->
Dan Gohman89f85052007-10-22 18:31:58 +0000394 replaceSymbolicValuesWithConcrete(Sym, Conc, SE));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395
Dan Gohman89f85052007-10-22 18:31:58 +0000396 return SE.getAddRecExpr(NewOps, L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 }
398 }
399 return this;
400}
401
402
403bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const {
404 // This recurrence is invariant w.r.t to QueryLoop iff QueryLoop doesn't
405 // contain L and if the start is invariant.
Dan Gohmanae1eaae2009-05-20 01:01:24 +0000406 // Add recurrences are never invariant in the function-body (null loop).
407 return QueryLoop &&
408 !QueryLoop->contains(L->getHeader()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 getOperand(0)->isLoopInvariant(QueryLoop);
410}
411
412
Dan Gohman13058cc2009-04-21 00:47:46 +0000413void SCEVAddRecExpr::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 OS << "{" << *Operands[0];
415 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
416 OS << ",+," << *Operands[i];
417 OS << "}<" << L->getHeader()->getName() + ">";
418}
419
420// SCEVUnknowns - Only allow the creation of one SCEVUnknown for any particular
421// value. Don't use a SCEVHandle here, or else the object will never be
422// deleted!
423static ManagedStatic<std::map<Value*, SCEVUnknown*> > SCEVUnknowns;
424
425SCEVUnknown::~SCEVUnknown() { SCEVUnknowns->erase(V); }
426
427bool SCEVUnknown::isLoopInvariant(const Loop *L) const {
428 // All non-instruction values are loop invariant. All instructions are loop
429 // invariant if they are not contained in the specified loop.
Dan Gohmanae1eaae2009-05-20 01:01:24 +0000430 // Instructions are never considered invariant in the function body
431 // (null loop) because they are defined within the "loop".
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432 if (Instruction *I = dyn_cast<Instruction>(V))
Dan Gohmanae1eaae2009-05-20 01:01:24 +0000433 return L && !L->contains(I->getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 return true;
435}
436
Evan Cheng98c073b2009-02-17 00:13:06 +0000437bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const {
438 if (Instruction *I = dyn_cast<Instruction>(getValue()))
439 return DT->dominates(I->getParent(), BB);
440 return true;
441}
442
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443const Type *SCEVUnknown::getType() const {
444 return V->getType();
445}
446
Dan Gohman13058cc2009-04-21 00:47:46 +0000447void SCEVUnknown::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 WriteAsOperand(OS, V, false);
449}
450
451//===----------------------------------------------------------------------===//
452// SCEV Utilities
453//===----------------------------------------------------------------------===//
454
455namespace {
456 /// SCEVComplexityCompare - Return true if the complexity of the LHS is less
457 /// than the complexity of the RHS. This comparator is used to canonicalize
458 /// expressions.
Dan Gohman5d486452009-05-07 14:39:04 +0000459 class VISIBILITY_HIDDEN SCEVComplexityCompare {
460 LoopInfo *LI;
461 public:
462 explicit SCEVComplexityCompare(LoopInfo *li) : LI(li) {}
463
Dan Gohmanc0c69cf2008-04-14 18:23:56 +0000464 bool operator()(const SCEV *LHS, const SCEV *RHS) const {
Dan Gohman5d486452009-05-07 14:39:04 +0000465 // Primarily, sort the SCEVs by their getSCEVType().
466 if (LHS->getSCEVType() != RHS->getSCEVType())
467 return LHS->getSCEVType() < RHS->getSCEVType();
468
469 // Aside from the getSCEVType() ordering, the particular ordering
470 // isn't very important except that it's beneficial to be consistent,
471 // so that (a + b) and (b + a) don't end up as different expressions.
472
473 // Sort SCEVUnknown values with some loose heuristics. TODO: This is
474 // not as complete as it could be.
475 if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) {
476 const SCEVUnknown *RU = cast<SCEVUnknown>(RHS);
477
Dan Gohmand0c01232009-05-19 02:15:55 +0000478 // Order pointer values after integer values. This helps SCEVExpander
479 // form GEPs.
480 if (isa<PointerType>(LU->getType()) && !isa<PointerType>(RU->getType()))
481 return false;
482 if (isa<PointerType>(RU->getType()) && !isa<PointerType>(LU->getType()))
483 return true;
484
Dan Gohman5d486452009-05-07 14:39:04 +0000485 // Compare getValueID values.
486 if (LU->getValue()->getValueID() != RU->getValue()->getValueID())
487 return LU->getValue()->getValueID() < RU->getValue()->getValueID();
488
489 // Sort arguments by their position.
490 if (const Argument *LA = dyn_cast<Argument>(LU->getValue())) {
491 const Argument *RA = cast<Argument>(RU->getValue());
492 return LA->getArgNo() < RA->getArgNo();
493 }
494
495 // For instructions, compare their loop depth, and their opcode.
496 // This is pretty loose.
497 if (Instruction *LV = dyn_cast<Instruction>(LU->getValue())) {
498 Instruction *RV = cast<Instruction>(RU->getValue());
499
500 // Compare loop depths.
501 if (LI->getLoopDepth(LV->getParent()) !=
502 LI->getLoopDepth(RV->getParent()))
503 return LI->getLoopDepth(LV->getParent()) <
504 LI->getLoopDepth(RV->getParent());
505
506 // Compare opcodes.
507 if (LV->getOpcode() != RV->getOpcode())
508 return LV->getOpcode() < RV->getOpcode();
509
510 // Compare the number of operands.
511 if (LV->getNumOperands() != RV->getNumOperands())
512 return LV->getNumOperands() < RV->getNumOperands();
513 }
514
515 return false;
516 }
517
Dan Gohman56fc8f12009-06-14 22:51:25 +0000518 // Compare constant values.
519 if (const SCEVConstant *LC = dyn_cast<SCEVConstant>(LHS)) {
520 const SCEVConstant *RC = cast<SCEVConstant>(RHS);
521 return LC->getValue()->getValue().ult(RC->getValue()->getValue());
522 }
523
524 // Compare addrec loop depths.
525 if (const SCEVAddRecExpr *LA = dyn_cast<SCEVAddRecExpr>(LHS)) {
526 const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS);
527 if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth())
528 return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth();
529 }
Dan Gohman5d486452009-05-07 14:39:04 +0000530
531 // Lexicographically compare n-ary expressions.
532 if (const SCEVNAryExpr *LC = dyn_cast<SCEVNAryExpr>(LHS)) {
533 const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS);
534 for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) {
535 if (i >= RC->getNumOperands())
536 return false;
537 if (operator()(LC->getOperand(i), RC->getOperand(i)))
538 return true;
539 if (operator()(RC->getOperand(i), LC->getOperand(i)))
540 return false;
541 }
542 return LC->getNumOperands() < RC->getNumOperands();
543 }
544
Dan Gohman6e10db12009-05-07 19:23:21 +0000545 // Lexicographically compare udiv expressions.
546 if (const SCEVUDivExpr *LC = dyn_cast<SCEVUDivExpr>(LHS)) {
547 const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS);
548 if (operator()(LC->getLHS(), RC->getLHS()))
549 return true;
550 if (operator()(RC->getLHS(), LC->getLHS()))
551 return false;
552 if (operator()(LC->getRHS(), RC->getRHS()))
553 return true;
554 if (operator()(RC->getRHS(), LC->getRHS()))
555 return false;
556 return false;
557 }
558
Dan Gohman5d486452009-05-07 14:39:04 +0000559 // Compare cast expressions by operand.
560 if (const SCEVCastExpr *LC = dyn_cast<SCEVCastExpr>(LHS)) {
561 const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS);
562 return operator()(LC->getOperand(), RC->getOperand());
563 }
564
565 assert(0 && "Unknown SCEV kind!");
566 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 }
568 };
569}
570
571/// GroupByComplexity - Given a list of SCEV objects, order them by their
572/// complexity, and group objects of the same complexity together by value.
573/// When this routine is finished, we know that any duplicates in the vector are
574/// consecutive and that complexity is monotonically increasing.
575///
576/// Note that we go take special precautions to ensure that we get determinstic
577/// results from this routine. In other words, we don't want the results of
578/// this to depend on where the addresses of various SCEV objects happened to
579/// land in memory.
580///
Dan Gohman02ff9392009-06-14 22:47:23 +0000581static void GroupByComplexity(SmallVectorImpl<SCEVHandle> &Ops,
Dan Gohman5d486452009-05-07 14:39:04 +0000582 LoopInfo *LI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 if (Ops.size() < 2) return; // Noop
584 if (Ops.size() == 2) {
585 // This is the common case, which also happens to be trivially simple.
586 // Special case it.
Dan Gohman5d486452009-05-07 14:39:04 +0000587 if (SCEVComplexityCompare(LI)(Ops[1], Ops[0]))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 std::swap(Ops[0], Ops[1]);
589 return;
590 }
591
592 // Do the rough sort by complexity.
Dan Gohman5d486452009-05-07 14:39:04 +0000593 std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594
595 // Now that we are sorted by complexity, group elements of the same
596 // complexity. Note that this is, at worst, N^2, but the vector is likely to
597 // be extremely short in practice. Note that we take this approach because we
598 // do not want to depend on the addresses of the objects we are grouping.
599 for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
Dan Gohmanbff6b582009-05-04 22:30:44 +0000600 const SCEV *S = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601 unsigned Complexity = S->getSCEVType();
602
603 // If there are any objects of the same complexity and same value as this
604 // one, group them.
605 for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
606 if (Ops[j] == S) { // Found a duplicate.
607 // Move it to immediately after i'th element.
608 std::swap(Ops[i+1], Ops[j]);
609 ++i; // no need to rescan it.
610 if (i == e-2) return; // Done!
611 }
612 }
613 }
614}
615
616
617
618//===----------------------------------------------------------------------===//
619// Simple SCEV method implementations
620//===----------------------------------------------------------------------===//
621
Eli Friedman7489ec92008-08-04 23:49:06 +0000622/// BinomialCoefficient - Compute BC(It, K). The result has width W.
Dan Gohmanc8a29272009-05-24 23:45:28 +0000623/// Assume, K > 0.
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000624static SCEVHandle BinomialCoefficient(SCEVHandle It, unsigned K,
Eli Friedman7489ec92008-08-04 23:49:06 +0000625 ScalarEvolution &SE,
Dan Gohman01c2ee72009-04-16 03:18:22 +0000626 const Type* ResultTy) {
Eli Friedman7489ec92008-08-04 23:49:06 +0000627 // Handle the simplest case efficiently.
628 if (K == 1)
629 return SE.getTruncateOrZeroExtend(It, ResultTy);
630
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000631 // We are using the following formula for BC(It, K):
632 //
633 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K!
634 //
Eli Friedman7489ec92008-08-04 23:49:06 +0000635 // Suppose, W is the bitwidth of the return value. We must be prepared for
636 // overflow. Hence, we must assure that the result of our computation is
637 // equal to the accurate one modulo 2^W. Unfortunately, division isn't
638 // safe in modular arithmetic.
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000639 //
Eli Friedman7489ec92008-08-04 23:49:06 +0000640 // However, this code doesn't use exactly that formula; the formula it uses
641 // is something like the following, where T is the number of factors of 2 in
642 // K! (i.e. trailing zeros in the binary representation of K!), and ^ is
643 // exponentiation:
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000644 //
Eli Friedman7489ec92008-08-04 23:49:06 +0000645 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T)
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000646 //
Eli Friedman7489ec92008-08-04 23:49:06 +0000647 // This formula is trivially equivalent to the previous formula. However,
648 // this formula can be implemented much more efficiently. The trick is that
649 // K! / 2^T is odd, and exact division by an odd number *is* safe in modular
650 // arithmetic. To do exact division in modular arithmetic, all we have
651 // to do is multiply by the inverse. Therefore, this step can be done at
652 // width W.
653 //
654 // The next issue is how to safely do the division by 2^T. The way this
655 // is done is by doing the multiplication step at a width of at least W + T
656 // bits. This way, the bottom W+T bits of the product are accurate. Then,
657 // when we perform the division by 2^T (which is equivalent to a right shift
658 // by T), the bottom W bits are accurate. Extra bits are okay; they'll get
659 // truncated out after the division by 2^T.
660 //
661 // In comparison to just directly using the first formula, this technique
662 // is much more efficient; using the first formula requires W * K bits,
663 // but this formula less than W + K bits. Also, the first formula requires
664 // a division step, whereas this formula only requires multiplies and shifts.
665 //
666 // It doesn't matter whether the subtraction step is done in the calculation
667 // width or the input iteration count's width; if the subtraction overflows,
668 // the result must be zero anyway. We prefer here to do it in the width of
669 // the induction variable because it helps a lot for certain cases; CodeGen
670 // isn't smart enough to ignore the overflow, which leads to much less
671 // efficient code if the width of the subtraction is wider than the native
672 // register width.
673 //
674 // (It's possible to not widen at all by pulling out factors of 2 before
675 // the multiplication; for example, K=2 can be calculated as
676 // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires
677 // extra arithmetic, so it's not an obvious win, and it gets
678 // much more complicated for K > 3.)
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000679
Eli Friedman7489ec92008-08-04 23:49:06 +0000680 // Protection from insane SCEVs; this bound is conservative,
681 // but it probably doesn't matter.
682 if (K > 1000)
Dan Gohman0ad08b02009-04-18 17:58:19 +0000683 return SE.getCouldNotCompute();
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000684
Dan Gohmanb98c1a32009-04-21 01:07:12 +0000685 unsigned W = SE.getTypeSizeInBits(ResultTy);
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000686
Eli Friedman7489ec92008-08-04 23:49:06 +0000687 // Calculate K! / 2^T and T; we divide out the factors of two before
688 // multiplying for calculating K! / 2^T to avoid overflow.
689 // Other overflow doesn't matter because we only care about the bottom
690 // W bits of the result.
691 APInt OddFactorial(W, 1);
692 unsigned T = 1;
693 for (unsigned i = 3; i <= K; ++i) {
694 APInt Mult(W, i);
695 unsigned TwoFactors = Mult.countTrailingZeros();
696 T += TwoFactors;
697 Mult = Mult.lshr(TwoFactors);
698 OddFactorial *= Mult;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699 }
Nick Lewyckydbaa60a2008-06-13 04:38:55 +0000700
Eli Friedman7489ec92008-08-04 23:49:06 +0000701 // We need at least W + T bits for the multiplication step
nicholas9e3e5fd2009-01-25 08:16:27 +0000702 unsigned CalculationBits = W + T;
Eli Friedman7489ec92008-08-04 23:49:06 +0000703
704 // Calcuate 2^T, at width T+W.
705 APInt DivFactor = APInt(CalculationBits, 1).shl(T);
706
707 // Calculate the multiplicative inverse of K! / 2^T;
708 // this multiplication factor will perform the exact division by
709 // K! / 2^T.
710 APInt Mod = APInt::getSignedMinValue(W+1);
711 APInt MultiplyFactor = OddFactorial.zext(W+1);
712 MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod);
713 MultiplyFactor = MultiplyFactor.trunc(W);
714
715 // Calculate the product, at width T+W
716 const IntegerType *CalculationTy = IntegerType::get(CalculationBits);
717 SCEVHandle Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy);
718 for (unsigned i = 1; i != K; ++i) {
719 SCEVHandle S = SE.getMinusSCEV(It, SE.getIntegerSCEV(i, It->getType()));
720 Dividend = SE.getMulExpr(Dividend,
721 SE.getTruncateOrZeroExtend(S, CalculationTy));
722 }
723
724 // Divide by 2^T
725 SCEVHandle DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor));
726
727 // Truncate the result, and divide by K! / 2^T.
728
729 return SE.getMulExpr(SE.getConstant(MultiplyFactor),
730 SE.getTruncateOrZeroExtend(DivResult, ResultTy));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731}
732
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000733/// evaluateAtIteration - Return the value of this chain of recurrences at
734/// the specified iteration number. We can evaluate this recurrence by
735/// multiplying each element in the chain by the binomial coefficient
736/// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as:
737///
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000738/// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739///
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000740/// where BC(It, k) stands for binomial coefficient.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000741///
Dan Gohman89f85052007-10-22 18:31:58 +0000742SCEVHandle SCEVAddRecExpr::evaluateAtIteration(SCEVHandle It,
743 ScalarEvolution &SE) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744 SCEVHandle Result = getStart();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000745 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000746 // The computation is correct in the face of overflow provided that the
747 // multiplication is performed _after_ the evaluation of the binomial
748 // coefficient.
Dan Gohman01c2ee72009-04-16 03:18:22 +0000749 SCEVHandle Coeff = BinomialCoefficient(It, i, SE, getType());
Nick Lewyckyb6218e02008-10-13 03:58:02 +0000750 if (isa<SCEVCouldNotCompute>(Coeff))
751 return Coeff;
752
753 Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000754 }
755 return Result;
756}
757
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758//===----------------------------------------------------------------------===//
759// SCEV Expression folder implementations
760//===----------------------------------------------------------------------===//
761
Dan Gohman9c8abcc2009-05-01 16:44:56 +0000762SCEVHandle ScalarEvolution::getTruncateExpr(const SCEVHandle &Op,
763 const Type *Ty) {
Dan Gohmanb98c1a32009-04-21 01:07:12 +0000764 assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) &&
Dan Gohmanf62cfe52009-04-21 00:55:22 +0000765 "This is not a truncating conversion!");
Dan Gohman13a51e22009-05-01 16:44:18 +0000766 assert(isSCEVable(Ty) &&
767 "This is not a conversion to a SCEVable type!");
768 Ty = getEffectiveSCEVType(Ty);
Dan Gohmanf62cfe52009-04-21 00:55:22 +0000769
Dan Gohmanc76b5452009-05-04 22:02:23 +0000770 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
Dan Gohman89f85052007-10-22 18:31:58 +0000771 return getUnknown(
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772 ConstantExpr::getTrunc(SC->getValue(), Ty));
773
Dan Gohman1a5c4992009-04-22 16:20:48 +0000774 // trunc(trunc(x)) --> trunc(x)
Dan Gohmanc76b5452009-05-04 22:02:23 +0000775 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op))
Dan Gohman1a5c4992009-04-22 16:20:48 +0000776 return getTruncateExpr(ST->getOperand(), Ty);
777
Nick Lewycky37d04642009-04-23 05:15:08 +0000778 // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing
Dan Gohmanc76b5452009-05-04 22:02:23 +0000779 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
Nick Lewycky37d04642009-04-23 05:15:08 +0000780 return getTruncateOrSignExtend(SS->getOperand(), Ty);
781
782 // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing
Dan Gohmanc76b5452009-05-04 22:02:23 +0000783 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
Nick Lewycky37d04642009-04-23 05:15:08 +0000784 return getTruncateOrZeroExtend(SZ->getOperand(), Ty);
785
Dan Gohman1c0aa2c2009-06-18 16:24:47 +0000786 // If the input value is a chrec scev, truncate the chrec's operands.
Dan Gohmanc76b5452009-05-04 22:02:23 +0000787 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
Dan Gohman02ff9392009-06-14 22:47:23 +0000788 SmallVector<SCEVHandle, 4> Operands;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000789 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
Dan Gohman45b3b542009-05-08 21:03:19 +0000790 Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty));
791 return getAddRecExpr(Operands, AddRec->getLoop());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000792 }
793
794 SCEVTruncateExpr *&Result = (*SCEVTruncates)[std::make_pair(Op, Ty)];
Owen Andersoncf4e2302009-06-18 22:25:12 +0000795 if (Result == 0) Result = new SCEVTruncateExpr(Op, Ty, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796 return Result;
797}
798
Dan Gohman36d40922009-04-16 19:25:55 +0000799SCEVHandle ScalarEvolution::getZeroExtendExpr(const SCEVHandle &Op,
800 const Type *Ty) {
Dan Gohmanb98c1a32009-04-21 01:07:12 +0000801 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
Dan Gohman36d40922009-04-16 19:25:55 +0000802 "This is not an extending conversion!");
Dan Gohman13a51e22009-05-01 16:44:18 +0000803 assert(isSCEVable(Ty) &&
804 "This is not a conversion to a SCEVable type!");
805 Ty = getEffectiveSCEVType(Ty);
Dan Gohman36d40922009-04-16 19:25:55 +0000806
Dan Gohmanc76b5452009-05-04 22:02:23 +0000807 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) {
Dan Gohmanb98c1a32009-04-21 01:07:12 +0000808 const Type *IntTy = getEffectiveSCEVType(Ty);
Dan Gohman01c2ee72009-04-16 03:18:22 +0000809 Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy);
810 if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty);
811 return getUnknown(C);
812 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000813
Dan Gohman1a5c4992009-04-22 16:20:48 +0000814 // zext(zext(x)) --> zext(x)
Dan Gohmanc76b5452009-05-04 22:02:23 +0000815 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
Dan Gohman1a5c4992009-04-22 16:20:48 +0000816 return getZeroExtendExpr(SZ->getOperand(), Ty);
817
Dan Gohmana9dba962009-04-27 20:16:15 +0000818 // If the input value is a chrec scev, and we can prove that the value
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819 // did not overflow the old, smaller, value, we can zero extend all of the
Dan Gohmana9dba962009-04-27 20:16:15 +0000820 // operands (often constants). This allows analysis of something like
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821 // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
Dan Gohmanc76b5452009-05-04 22:02:23 +0000822 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
Dan Gohmana9dba962009-04-27 20:16:15 +0000823 if (AR->isAffine()) {
824 // Check whether the backedge-taken count is SCEVCouldNotCompute.
825 // Note that this serves two purposes: It filters out loops that are
826 // simply not analyzable, and it covers the case where this code is
827 // being called from within backedge-taken count analysis, such that
828 // attempting to ask for the backedge-taken count would likely result
829 // in infinite recursion. In the later case, the analysis code will
830 // cope with a conservative value, and it will take care to purge
831 // that value once it has finished.
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000832 SCEVHandle MaxBECount = getMaxBackedgeTakenCount(AR->getLoop());
833 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
Dan Gohman4ada77f2009-04-29 01:54:20 +0000834 // Manually compute the final value for AR, checking for
Dan Gohman3ded5b22009-04-29 22:28:28 +0000835 // overflow.
Dan Gohmana9dba962009-04-27 20:16:15 +0000836 SCEVHandle Start = AR->getStart();
837 SCEVHandle Step = AR->getStepRecurrence(*this);
838
839 // Check whether the backedge-taken count can be losslessly casted to
840 // the addrec's type. The count is always unsigned.
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000841 SCEVHandle CastedMaxBECount =
842 getTruncateOrZeroExtend(MaxBECount, Start->getType());
Dan Gohman3bb37f52009-05-18 15:58:39 +0000843 SCEVHandle RecastedMaxBECount =
844 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
845 if (MaxBECount == RecastedMaxBECount) {
Dan Gohmana9dba962009-04-27 20:16:15 +0000846 const Type *WideTy =
847 IntegerType::get(getTypeSizeInBits(Start->getType()) * 2);
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000848 // Check whether Start+Step*MaxBECount has no unsigned overflow.
Dan Gohmana9dba962009-04-27 20:16:15 +0000849 SCEVHandle ZMul =
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000850 getMulExpr(CastedMaxBECount,
Dan Gohmana9dba962009-04-27 20:16:15 +0000851 getTruncateOrZeroExtend(Step, Start->getType()));
Dan Gohman3ded5b22009-04-29 22:28:28 +0000852 SCEVHandle Add = getAddExpr(Start, ZMul);
Dan Gohman3bb37f52009-05-18 15:58:39 +0000853 SCEVHandle OperandExtendedAdd =
854 getAddExpr(getZeroExtendExpr(Start, WideTy),
855 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
856 getZeroExtendExpr(Step, WideTy)));
857 if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohman3ded5b22009-04-29 22:28:28 +0000858 // Return the expression with the addrec on the outside.
859 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
860 getZeroExtendExpr(Step, Ty),
861 AR->getLoop());
Dan Gohmana9dba962009-04-27 20:16:15 +0000862
863 // Similar to above, only this time treat the step value as signed.
864 // This covers loops that count down.
865 SCEVHandle SMul =
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000866 getMulExpr(CastedMaxBECount,
Dan Gohmana9dba962009-04-27 20:16:15 +0000867 getTruncateOrSignExtend(Step, Start->getType()));
Dan Gohman3ded5b22009-04-29 22:28:28 +0000868 Add = getAddExpr(Start, SMul);
Dan Gohman3bb37f52009-05-18 15:58:39 +0000869 OperandExtendedAdd =
870 getAddExpr(getZeroExtendExpr(Start, WideTy),
871 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
872 getSignExtendExpr(Step, WideTy)));
873 if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohman3ded5b22009-04-29 22:28:28 +0000874 // Return the expression with the addrec on the outside.
875 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
876 getSignExtendExpr(Step, Ty),
877 AR->getLoop());
Dan Gohmana9dba962009-04-27 20:16:15 +0000878 }
879 }
880 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881
882 SCEVZeroExtendExpr *&Result = (*SCEVZeroExtends)[std::make_pair(Op, Ty)];
Owen Andersoncf4e2302009-06-18 22:25:12 +0000883 if (Result == 0) Result = new SCEVZeroExtendExpr(Op, Ty, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000884 return Result;
885}
886
Dan Gohmana9dba962009-04-27 20:16:15 +0000887SCEVHandle ScalarEvolution::getSignExtendExpr(const SCEVHandle &Op,
888 const Type *Ty) {
Dan Gohmanb98c1a32009-04-21 01:07:12 +0000889 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
Dan Gohmanf62cfe52009-04-21 00:55:22 +0000890 "This is not an extending conversion!");
Dan Gohman13a51e22009-05-01 16:44:18 +0000891 assert(isSCEVable(Ty) &&
892 "This is not a conversion to a SCEVable type!");
893 Ty = getEffectiveSCEVType(Ty);
Dan Gohmanf62cfe52009-04-21 00:55:22 +0000894
Dan Gohmanc76b5452009-05-04 22:02:23 +0000895 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) {
Dan Gohmanb98c1a32009-04-21 01:07:12 +0000896 const Type *IntTy = getEffectiveSCEVType(Ty);
Dan Gohman01c2ee72009-04-16 03:18:22 +0000897 Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy);
898 if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty);
899 return getUnknown(C);
900 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901
Dan Gohman1a5c4992009-04-22 16:20:48 +0000902 // sext(sext(x)) --> sext(x)
Dan Gohmanc76b5452009-05-04 22:02:23 +0000903 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
Dan Gohman1a5c4992009-04-22 16:20:48 +0000904 return getSignExtendExpr(SS->getOperand(), Ty);
905
Dan Gohmana9dba962009-04-27 20:16:15 +0000906 // If the input value is a chrec scev, and we can prove that the value
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000907 // did not overflow the old, smaller, value, we can sign extend all of the
Dan Gohmana9dba962009-04-27 20:16:15 +0000908 // operands (often constants). This allows analysis of something like
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909 // this: for (signed char X = 0; X < 100; ++X) { int Y = X; }
Dan Gohmanc76b5452009-05-04 22:02:23 +0000910 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
Dan Gohmana9dba962009-04-27 20:16:15 +0000911 if (AR->isAffine()) {
912 // Check whether the backedge-taken count is SCEVCouldNotCompute.
913 // Note that this serves two purposes: It filters out loops that are
914 // simply not analyzable, and it covers the case where this code is
915 // being called from within backedge-taken count analysis, such that
916 // attempting to ask for the backedge-taken count would likely result
917 // in infinite recursion. In the later case, the analysis code will
918 // cope with a conservative value, and it will take care to purge
919 // that value once it has finished.
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000920 SCEVHandle MaxBECount = getMaxBackedgeTakenCount(AR->getLoop());
921 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
Dan Gohman4ada77f2009-04-29 01:54:20 +0000922 // Manually compute the final value for AR, checking for
Dan Gohman3ded5b22009-04-29 22:28:28 +0000923 // overflow.
Dan Gohmana9dba962009-04-27 20:16:15 +0000924 SCEVHandle Start = AR->getStart();
925 SCEVHandle Step = AR->getStepRecurrence(*this);
926
927 // Check whether the backedge-taken count can be losslessly casted to
Dan Gohman3ded5b22009-04-29 22:28:28 +0000928 // the addrec's type. The count is always unsigned.
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000929 SCEVHandle CastedMaxBECount =
930 getTruncateOrZeroExtend(MaxBECount, Start->getType());
Dan Gohman3bb37f52009-05-18 15:58:39 +0000931 SCEVHandle RecastedMaxBECount =
932 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
933 if (MaxBECount == RecastedMaxBECount) {
Dan Gohmana9dba962009-04-27 20:16:15 +0000934 const Type *WideTy =
935 IntegerType::get(getTypeSizeInBits(Start->getType()) * 2);
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000936 // Check whether Start+Step*MaxBECount has no signed overflow.
Dan Gohmana9dba962009-04-27 20:16:15 +0000937 SCEVHandle SMul =
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000938 getMulExpr(CastedMaxBECount,
Dan Gohmana9dba962009-04-27 20:16:15 +0000939 getTruncateOrSignExtend(Step, Start->getType()));
Dan Gohman3ded5b22009-04-29 22:28:28 +0000940 SCEVHandle Add = getAddExpr(Start, SMul);
Dan Gohman3bb37f52009-05-18 15:58:39 +0000941 SCEVHandle OperandExtendedAdd =
942 getAddExpr(getSignExtendExpr(Start, WideTy),
943 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
944 getSignExtendExpr(Step, WideTy)));
945 if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohman3ded5b22009-04-29 22:28:28 +0000946 // Return the expression with the addrec on the outside.
947 return getAddRecExpr(getSignExtendExpr(Start, Ty),
948 getSignExtendExpr(Step, Ty),
949 AR->getLoop());
Dan Gohmana9dba962009-04-27 20:16:15 +0000950 }
951 }
952 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000953
954 SCEVSignExtendExpr *&Result = (*SCEVSignExtends)[std::make_pair(Op, Ty)];
Owen Andersoncf4e2302009-06-18 22:25:12 +0000955 if (Result == 0) Result = new SCEVSignExtendExpr(Op, Ty, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000956 return Result;
957}
958
Dan Gohmane1ca7e82009-06-13 15:56:47 +0000959/// getAnyExtendExpr - Return a SCEV for the given operand extended with
960/// unspecified bits out to the given type.
961///
962SCEVHandle ScalarEvolution::getAnyExtendExpr(const SCEVHandle &Op,
963 const Type *Ty) {
964 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
965 "This is not an extending conversion!");
966 assert(isSCEVable(Ty) &&
967 "This is not a conversion to a SCEVable type!");
968 Ty = getEffectiveSCEVType(Ty);
969
970 // Sign-extend negative constants.
971 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
972 if (SC->getValue()->getValue().isNegative())
973 return getSignExtendExpr(Op, Ty);
974
975 // Peel off a truncate cast.
976 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) {
977 SCEVHandle NewOp = T->getOperand();
978 if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty))
979 return getAnyExtendExpr(NewOp, Ty);
980 return getTruncateOrNoop(NewOp, Ty);
981 }
982
983 // Next try a zext cast. If the cast is folded, use it.
984 SCEVHandle ZExt = getZeroExtendExpr(Op, Ty);
985 if (!isa<SCEVZeroExtendExpr>(ZExt))
986 return ZExt;
987
988 // Next try a sext cast. If the cast is folded, use it.
989 SCEVHandle SExt = getSignExtendExpr(Op, Ty);
990 if (!isa<SCEVSignExtendExpr>(SExt))
991 return SExt;
992
993 // If the expression is obviously signed, use the sext cast value.
994 if (isa<SCEVSMaxExpr>(Op))
995 return SExt;
996
997 // Absent any other information, use the zext cast value.
998 return ZExt;
999}
1000
Dan Gohman27bd4cb2009-06-14 22:58:51 +00001001/// CollectAddOperandsWithScales - Process the given Ops list, which is
1002/// a list of operands to be added under the given scale, update the given
1003/// map. This is a helper function for getAddRecExpr. As an example of
1004/// what it does, given a sequence of operands that would form an add
1005/// expression like this:
1006///
1007/// m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r)
1008///
1009/// where A and B are constants, update the map with these values:
1010///
1011/// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0)
1012///
1013/// and add 13 + A*B*29 to AccumulatedConstant.
1014/// This will allow getAddRecExpr to produce this:
1015///
1016/// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B)
1017///
1018/// This form often exposes folding opportunities that are hidden in
1019/// the original operand list.
1020///
1021/// Return true iff it appears that any interesting folding opportunities
1022/// may be exposed. This helps getAddRecExpr short-circuit extra work in
1023/// the common case where no interesting opportunities are present, and
1024/// is also used as a check to avoid infinite recursion.
1025///
1026static bool
1027CollectAddOperandsWithScales(DenseMap<SCEVHandle, APInt> &M,
1028 SmallVector<SCEVHandle, 8> &NewOps,
1029 APInt &AccumulatedConstant,
1030 const SmallVectorImpl<SCEVHandle> &Ops,
1031 const APInt &Scale,
1032 ScalarEvolution &SE) {
1033 bool Interesting = false;
1034
1035 // Iterate over the add operands.
1036 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1037 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]);
1038 if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) {
1039 APInt NewScale =
1040 Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue();
1041 if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) {
1042 // A multiplication of a constant with another add; recurse.
1043 Interesting |=
1044 CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
1045 cast<SCEVAddExpr>(Mul->getOperand(1))
1046 ->getOperands(),
1047 NewScale, SE);
1048 } else {
1049 // A multiplication of a constant with some other value. Update
1050 // the map.
1051 SmallVector<SCEVHandle, 4> MulOps(Mul->op_begin()+1, Mul->op_end());
1052 SCEVHandle Key = SE.getMulExpr(MulOps);
1053 std::pair<DenseMap<SCEVHandle, APInt>::iterator, bool> Pair =
1054 M.insert(std::make_pair(Key, APInt()));
1055 if (Pair.second) {
1056 Pair.first->second = NewScale;
1057 NewOps.push_back(Pair.first->first);
1058 } else {
1059 Pair.first->second += NewScale;
1060 // The map already had an entry for this value, which may indicate
1061 // a folding opportunity.
1062 Interesting = true;
1063 }
1064 }
1065 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
1066 // Pull a buried constant out to the outside.
1067 if (Scale != 1 || AccumulatedConstant != 0 || C->isZero())
1068 Interesting = true;
1069 AccumulatedConstant += Scale * C->getValue()->getValue();
1070 } else {
1071 // An ordinary operand. Update the map.
1072 std::pair<DenseMap<SCEVHandle, APInt>::iterator, bool> Pair =
1073 M.insert(std::make_pair(Ops[i], APInt()));
1074 if (Pair.second) {
1075 Pair.first->second = Scale;
1076 NewOps.push_back(Pair.first->first);
1077 } else {
1078 Pair.first->second += Scale;
1079 // The map already had an entry for this value, which may indicate
1080 // a folding opportunity.
1081 Interesting = true;
1082 }
1083 }
1084 }
1085
1086 return Interesting;
1087}
1088
1089namespace {
1090 struct APIntCompare {
1091 bool operator()(const APInt &LHS, const APInt &RHS) const {
1092 return LHS.ult(RHS);
1093 }
1094 };
1095}
1096
Dan Gohmanc8a29272009-05-24 23:45:28 +00001097/// getAddExpr - Get a canonical add expression, or something simpler if
1098/// possible.
Dan Gohman02ff9392009-06-14 22:47:23 +00001099SCEVHandle ScalarEvolution::getAddExpr(SmallVectorImpl<SCEVHandle> &Ops) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001100 assert(!Ops.empty() && "Cannot get empty add!");
1101 if (Ops.size() == 1) return Ops[0];
Dan Gohmana77b3d42009-05-18 15:44:58 +00001102#ifndef NDEBUG
1103 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1104 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1105 getEffectiveSCEVType(Ops[0]->getType()) &&
1106 "SCEVAddExpr operand types don't match!");
1107#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001108
1109 // Sort by complexity, this groups all similar expression types together.
Dan Gohman5d486452009-05-07 14:39:04 +00001110 GroupByComplexity(Ops, LI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001111
1112 // If there are any constants, fold them together.
1113 unsigned Idx = 0;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001114 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001115 ++Idx;
1116 assert(Idx < Ops.size());
Dan Gohmanc76b5452009-05-04 22:02:23 +00001117 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001118 // We found two constants, fold them together!
Dan Gohman02ff9392009-06-14 22:47:23 +00001119 Ops[0] = getConstant(LHSC->getValue()->getValue() +
1120 RHSC->getValue()->getValue());
Dan Gohman68f23e82009-06-14 22:53:57 +00001121 if (Ops.size() == 2) return Ops[0];
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001122 Ops.erase(Ops.begin()+1); // Erase the folded element
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001123 LHSC = cast<SCEVConstant>(Ops[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001124 }
1125
1126 // If we are left with a constant zero being added, strip it off.
1127 if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
1128 Ops.erase(Ops.begin());
1129 --Idx;
1130 }
1131 }
1132
1133 if (Ops.size() == 1) return Ops[0];
1134
1135 // Okay, check to see if the same value occurs in the operand list twice. If
1136 // so, merge them together into an multiply expression. Since we sorted the
1137 // list, these values are required to be adjacent.
1138 const Type *Ty = Ops[0]->getType();
1139 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
1140 if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2
1141 // Found a match, merge the two values into a multiply, and add any
1142 // remaining values to the result.
Dan Gohman89f85052007-10-22 18:31:58 +00001143 SCEVHandle Two = getIntegerSCEV(2, Ty);
1144 SCEVHandle Mul = getMulExpr(Ops[i], Two);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001145 if (Ops.size() == 2)
1146 return Mul;
1147 Ops.erase(Ops.begin()+i, Ops.begin()+i+2);
1148 Ops.push_back(Mul);
Dan Gohman89f85052007-10-22 18:31:58 +00001149 return getAddExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001150 }
1151
Dan Gohman45b3b542009-05-08 21:03:19 +00001152 // Check for truncates. If all the operands are truncated from the same
1153 // type, see if factoring out the truncate would permit the result to be
1154 // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n)
1155 // if the contents of the resulting outer trunc fold to something simple.
1156 for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) {
1157 const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]);
1158 const Type *DstType = Trunc->getType();
1159 const Type *SrcType = Trunc->getOperand()->getType();
Dan Gohman02ff9392009-06-14 22:47:23 +00001160 SmallVector<SCEVHandle, 8> LargeOps;
Dan Gohman45b3b542009-05-08 21:03:19 +00001161 bool Ok = true;
1162 // Check all the operands to see if they can be represented in the
1163 // source type of the truncate.
1164 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1165 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) {
1166 if (T->getOperand()->getType() != SrcType) {
1167 Ok = false;
1168 break;
1169 }
1170 LargeOps.push_back(T->getOperand());
1171 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
1172 // This could be either sign or zero extension, but sign extension
1173 // is much more likely to be foldable here.
1174 LargeOps.push_back(getSignExtendExpr(C, SrcType));
1175 } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) {
Dan Gohman02ff9392009-06-14 22:47:23 +00001176 SmallVector<SCEVHandle, 8> LargeMulOps;
Dan Gohman45b3b542009-05-08 21:03:19 +00001177 for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
1178 if (const SCEVTruncateExpr *T =
1179 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) {
1180 if (T->getOperand()->getType() != SrcType) {
1181 Ok = false;
1182 break;
1183 }
1184 LargeMulOps.push_back(T->getOperand());
1185 } else if (const SCEVConstant *C =
1186 dyn_cast<SCEVConstant>(M->getOperand(j))) {
1187 // This could be either sign or zero extension, but sign extension
1188 // is much more likely to be foldable here.
1189 LargeMulOps.push_back(getSignExtendExpr(C, SrcType));
1190 } else {
1191 Ok = false;
1192 break;
1193 }
1194 }
1195 if (Ok)
1196 LargeOps.push_back(getMulExpr(LargeMulOps));
1197 } else {
1198 Ok = false;
1199 break;
1200 }
1201 }
1202 if (Ok) {
1203 // Evaluate the expression in the larger type.
1204 SCEVHandle Fold = getAddExpr(LargeOps);
1205 // If it folds to something simple, use it. Otherwise, don't.
1206 if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold))
1207 return getTruncateExpr(Fold, DstType);
1208 }
1209 }
1210
1211 // Skip past any other cast SCEVs.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001212 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr)
1213 ++Idx;
1214
1215 // If there are add operands they would be next.
1216 if (Idx < Ops.size()) {
1217 bool DeletedAdd = false;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001218 while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001219 // If we have an add, expand the add operands onto the end of the operands
1220 // list.
1221 Ops.insert(Ops.end(), Add->op_begin(), Add->op_end());
1222 Ops.erase(Ops.begin()+Idx);
1223 DeletedAdd = true;
1224 }
1225
1226 // If we deleted at least one add, we added operands to the end of the list,
1227 // and they are not necessarily sorted. Recurse to resort and resimplify
1228 // any operands we just aquired.
1229 if (DeletedAdd)
Dan Gohman89f85052007-10-22 18:31:58 +00001230 return getAddExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001231 }
1232
1233 // Skip over the add expression until we get to a multiply.
1234 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
1235 ++Idx;
1236
Dan Gohman27bd4cb2009-06-14 22:58:51 +00001237 // Check to see if there are any folding opportunities present with
1238 // operands multiplied by constant values.
1239 if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) {
1240 uint64_t BitWidth = getTypeSizeInBits(Ty);
1241 DenseMap<SCEVHandle, APInt> M;
1242 SmallVector<SCEVHandle, 8> NewOps;
1243 APInt AccumulatedConstant(BitWidth, 0);
1244 if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
1245 Ops, APInt(BitWidth, 1), *this)) {
1246 // Some interesting folding opportunity is present, so its worthwhile to
1247 // re-generate the operands list. Group the operands by constant scale,
1248 // to avoid multiplying by the same constant scale multiple times.
1249 std::map<APInt, SmallVector<SCEVHandle, 4>, APIntCompare> MulOpLists;
1250 for (SmallVector<SCEVHandle, 8>::iterator I = NewOps.begin(),
1251 E = NewOps.end(); I != E; ++I)
1252 MulOpLists[M.find(*I)->second].push_back(*I);
1253 // Re-generate the operands list.
1254 Ops.clear();
1255 if (AccumulatedConstant != 0)
1256 Ops.push_back(getConstant(AccumulatedConstant));
1257 for (std::map<APInt, SmallVector<SCEVHandle, 4>, APIntCompare>::iterator I =
1258 MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I)
1259 if (I->first != 0)
1260 Ops.push_back(getMulExpr(getConstant(I->first), getAddExpr(I->second)));
1261 if (Ops.empty())
1262 return getIntegerSCEV(0, Ty);
1263 if (Ops.size() == 1)
1264 return Ops[0];
1265 return getAddExpr(Ops);
1266 }
1267 }
1268
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001269 // If we are adding something to a multiply expression, make sure the
1270 // something is not already an operand of the multiply. If so, merge it into
1271 // the multiply.
1272 for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00001273 const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274 for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00001275 const SCEV *MulOpSCEV = Mul->getOperand(MulOp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001276 for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
Dan Gohman02ff9392009-06-14 22:47:23 +00001277 if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(Ops[AddOp])) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001278 // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1))
1279 SCEVHandle InnerMul = Mul->getOperand(MulOp == 0);
1280 if (Mul->getNumOperands() != 2) {
1281 // If the multiply has more than two operands, we must get the
1282 // Y*Z term.
Dan Gohman02ff9392009-06-14 22:47:23 +00001283 SmallVector<SCEVHandle, 4> MulOps(Mul->op_begin(), Mul->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001284 MulOps.erase(MulOps.begin()+MulOp);
Dan Gohman89f85052007-10-22 18:31:58 +00001285 InnerMul = getMulExpr(MulOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001286 }
Dan Gohman89f85052007-10-22 18:31:58 +00001287 SCEVHandle One = getIntegerSCEV(1, Ty);
1288 SCEVHandle AddOne = getAddExpr(InnerMul, One);
1289 SCEVHandle OuterMul = getMulExpr(AddOne, Ops[AddOp]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290 if (Ops.size() == 2) return OuterMul;
1291 if (AddOp < Idx) {
1292 Ops.erase(Ops.begin()+AddOp);
1293 Ops.erase(Ops.begin()+Idx-1);
1294 } else {
1295 Ops.erase(Ops.begin()+Idx);
1296 Ops.erase(Ops.begin()+AddOp-1);
1297 }
1298 Ops.push_back(OuterMul);
Dan Gohman89f85052007-10-22 18:31:58 +00001299 return getAddExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001300 }
1301
1302 // Check this multiply against other multiplies being added together.
1303 for (unsigned OtherMulIdx = Idx+1;
1304 OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
1305 ++OtherMulIdx) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00001306 const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001307 // If MulOp occurs in OtherMul, we can fold the two multiplies
1308 // together.
1309 for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
1310 OMulOp != e; ++OMulOp)
1311 if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
1312 // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
1313 SCEVHandle InnerMul1 = Mul->getOperand(MulOp == 0);
1314 if (Mul->getNumOperands() != 2) {
Dan Gohman02ff9392009-06-14 22:47:23 +00001315 SmallVector<SCEVHandle, 4> MulOps(Mul->op_begin(), Mul->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316 MulOps.erase(MulOps.begin()+MulOp);
Dan Gohman89f85052007-10-22 18:31:58 +00001317 InnerMul1 = getMulExpr(MulOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001318 }
1319 SCEVHandle InnerMul2 = OtherMul->getOperand(OMulOp == 0);
1320 if (OtherMul->getNumOperands() != 2) {
Dan Gohman02ff9392009-06-14 22:47:23 +00001321 SmallVector<SCEVHandle, 4> MulOps(OtherMul->op_begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001322 OtherMul->op_end());
1323 MulOps.erase(MulOps.begin()+OMulOp);
Dan Gohman89f85052007-10-22 18:31:58 +00001324 InnerMul2 = getMulExpr(MulOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001325 }
Dan Gohman89f85052007-10-22 18:31:58 +00001326 SCEVHandle InnerMulSum = getAddExpr(InnerMul1,InnerMul2);
1327 SCEVHandle OuterMul = getMulExpr(MulOpSCEV, InnerMulSum);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001328 if (Ops.size() == 2) return OuterMul;
1329 Ops.erase(Ops.begin()+Idx);
1330 Ops.erase(Ops.begin()+OtherMulIdx-1);
1331 Ops.push_back(OuterMul);
Dan Gohman89f85052007-10-22 18:31:58 +00001332 return getAddExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001333 }
1334 }
1335 }
1336 }
1337
1338 // If there are any add recurrences in the operands list, see if any other
1339 // added values are loop invariant. If so, we can fold them into the
1340 // recurrence.
1341 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
1342 ++Idx;
1343
1344 // Scan over all recurrences, trying to fold loop invariants into them.
1345 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
1346 // Scan all of the other operands to this add and add them to the vector if
1347 // they are loop invariant w.r.t. the recurrence.
Dan Gohman02ff9392009-06-14 22:47:23 +00001348 SmallVector<SCEVHandle, 8> LIOps;
Dan Gohmanbff6b582009-05-04 22:30:44 +00001349 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001350 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1351 if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
1352 LIOps.push_back(Ops[i]);
1353 Ops.erase(Ops.begin()+i);
1354 --i; --e;
1355 }
1356
1357 // If we found some loop invariants, fold them into the recurrence.
1358 if (!LIOps.empty()) {
Dan Gohmanabe991f2008-09-14 17:21:12 +00001359 // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001360 LIOps.push_back(AddRec->getStart());
1361
Dan Gohman02ff9392009-06-14 22:47:23 +00001362 SmallVector<SCEVHandle, 4> AddRecOps(AddRec->op_begin(),
1363 AddRec->op_end());
Dan Gohman89f85052007-10-22 18:31:58 +00001364 AddRecOps[0] = getAddExpr(LIOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001365
Dan Gohman89f85052007-10-22 18:31:58 +00001366 SCEVHandle NewRec = getAddRecExpr(AddRecOps, AddRec->getLoop());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001367 // If all of the other operands were loop invariant, we are done.
1368 if (Ops.size() == 1) return NewRec;
1369
1370 // Otherwise, add the folded AddRec by the non-liv parts.
1371 for (unsigned i = 0;; ++i)
1372 if (Ops[i] == AddRec) {
1373 Ops[i] = NewRec;
1374 break;
1375 }
Dan Gohman89f85052007-10-22 18:31:58 +00001376 return getAddExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001377 }
1378
1379 // Okay, if there weren't any loop invariants to be folded, check to see if
1380 // there are multiple AddRec's with the same loop induction variable being
1381 // added together. If so, we can fold them.
1382 for (unsigned OtherIdx = Idx+1;
1383 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
1384 if (OtherIdx != Idx) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00001385 const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001386 if (AddRec->getLoop() == OtherAddRec->getLoop()) {
1387 // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D}
Dan Gohman02ff9392009-06-14 22:47:23 +00001388 SmallVector<SCEVHandle, 4> NewOps(AddRec->op_begin(), AddRec->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001389 for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) {
1390 if (i >= NewOps.size()) {
1391 NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i,
1392 OtherAddRec->op_end());
1393 break;
1394 }
Dan Gohman89f85052007-10-22 18:31:58 +00001395 NewOps[i] = getAddExpr(NewOps[i], OtherAddRec->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001396 }
Dan Gohman89f85052007-10-22 18:31:58 +00001397 SCEVHandle NewAddRec = getAddRecExpr(NewOps, AddRec->getLoop());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001398
1399 if (Ops.size() == 2) return NewAddRec;
1400
1401 Ops.erase(Ops.begin()+Idx);
1402 Ops.erase(Ops.begin()+OtherIdx-1);
1403 Ops.push_back(NewAddRec);
Dan Gohman89f85052007-10-22 18:31:58 +00001404 return getAddExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001405 }
1406 }
1407
1408 // Otherwise couldn't fold anything into this recurrence. Move onto the
1409 // next one.
1410 }
1411
1412 // Okay, it looks like we really DO need an add expr. Check to see if we
1413 // already have one, otherwise create a new one.
Dan Gohmanbff6b582009-05-04 22:30:44 +00001414 std::vector<const SCEV*> SCEVOps(Ops.begin(), Ops.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001415 SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scAddExpr,
1416 SCEVOps)];
Owen Andersoncf4e2302009-06-18 22:25:12 +00001417 if (Result == 0) Result = new SCEVAddExpr(Ops, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001418 return Result;
1419}
1420
1421
Dan Gohmanc8a29272009-05-24 23:45:28 +00001422/// getMulExpr - Get a canonical multiply expression, or something simpler if
1423/// possible.
Dan Gohman02ff9392009-06-14 22:47:23 +00001424SCEVHandle ScalarEvolution::getMulExpr(SmallVectorImpl<SCEVHandle> &Ops) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001425 assert(!Ops.empty() && "Cannot get empty mul!");
Dan Gohmana77b3d42009-05-18 15:44:58 +00001426#ifndef NDEBUG
1427 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1428 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1429 getEffectiveSCEVType(Ops[0]->getType()) &&
1430 "SCEVMulExpr operand types don't match!");
1431#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001432
1433 // Sort by complexity, this groups all similar expression types together.
Dan Gohman5d486452009-05-07 14:39:04 +00001434 GroupByComplexity(Ops, LI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001435
1436 // If there are any constants, fold them together.
1437 unsigned Idx = 0;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001438 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001439
1440 // C1*(C2+V) -> C1*C2 + C1*V
1441 if (Ops.size() == 2)
Dan Gohmanc76b5452009-05-04 22:02:23 +00001442 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001443 if (Add->getNumOperands() == 2 &&
1444 isa<SCEVConstant>(Add->getOperand(0)))
Dan Gohman89f85052007-10-22 18:31:58 +00001445 return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)),
1446 getMulExpr(LHSC, Add->getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447
1448
1449 ++Idx;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001450 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001451 // We found two constants, fold them together!
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001452 ConstantInt *Fold = ConstantInt::get(LHSC->getValue()->getValue() *
1453 RHSC->getValue()->getValue());
1454 Ops[0] = getConstant(Fold);
1455 Ops.erase(Ops.begin()+1); // Erase the folded element
1456 if (Ops.size() == 1) return Ops[0];
1457 LHSC = cast<SCEVConstant>(Ops[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001458 }
1459
1460 // If we are left with a constant one being multiplied, strip it off.
1461 if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) {
1462 Ops.erase(Ops.begin());
1463 --Idx;
1464 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
1465 // If we have a multiply of zero, it will always be zero.
1466 return Ops[0];
1467 }
1468 }
1469
1470 // Skip over the add expression until we get to a multiply.
1471 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
1472 ++Idx;
1473
1474 if (Ops.size() == 1)
1475 return Ops[0];
1476
1477 // If there are mul operands inline them all into this expression.
1478 if (Idx < Ops.size()) {
1479 bool DeletedMul = false;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001480 while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001481 // If we have an mul, expand the mul operands onto the end of the operands
1482 // list.
1483 Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end());
1484 Ops.erase(Ops.begin()+Idx);
1485 DeletedMul = true;
1486 }
1487
1488 // If we deleted at least one mul, we added operands to the end of the list,
1489 // and they are not necessarily sorted. Recurse to resort and resimplify
1490 // any operands we just aquired.
1491 if (DeletedMul)
Dan Gohman89f85052007-10-22 18:31:58 +00001492 return getMulExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001493 }
1494
1495 // If there are any add recurrences in the operands list, see if any other
1496 // added values are loop invariant. If so, we can fold them into the
1497 // recurrence.
1498 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
1499 ++Idx;
1500
1501 // Scan over all recurrences, trying to fold loop invariants into them.
1502 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
1503 // Scan all of the other operands to this mul and add them to the vector if
1504 // they are loop invariant w.r.t. the recurrence.
Dan Gohman02ff9392009-06-14 22:47:23 +00001505 SmallVector<SCEVHandle, 8> LIOps;
Dan Gohmanbff6b582009-05-04 22:30:44 +00001506 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001507 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1508 if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
1509 LIOps.push_back(Ops[i]);
1510 Ops.erase(Ops.begin()+i);
1511 --i; --e;
1512 }
1513
1514 // If we found some loop invariants, fold them into the recurrence.
1515 if (!LIOps.empty()) {
Dan Gohmanabe991f2008-09-14 17:21:12 +00001516 // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step}
Dan Gohman02ff9392009-06-14 22:47:23 +00001517 SmallVector<SCEVHandle, 4> NewOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518 NewOps.reserve(AddRec->getNumOperands());
1519 if (LIOps.size() == 1) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00001520 const SCEV *Scale = LIOps[0];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001521 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
Dan Gohman89f85052007-10-22 18:31:58 +00001522 NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001523 } else {
1524 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
Dan Gohman02ff9392009-06-14 22:47:23 +00001525 SmallVector<SCEVHandle, 4> MulOps(LIOps.begin(), LIOps.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001526 MulOps.push_back(AddRec->getOperand(i));
Dan Gohman89f85052007-10-22 18:31:58 +00001527 NewOps.push_back(getMulExpr(MulOps));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001528 }
1529 }
1530
Dan Gohman89f85052007-10-22 18:31:58 +00001531 SCEVHandle NewRec = getAddRecExpr(NewOps, AddRec->getLoop());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001532
1533 // If all of the other operands were loop invariant, we are done.
1534 if (Ops.size() == 1) return NewRec;
1535
1536 // Otherwise, multiply the folded AddRec by the non-liv parts.
1537 for (unsigned i = 0;; ++i)
1538 if (Ops[i] == AddRec) {
1539 Ops[i] = NewRec;
1540 break;
1541 }
Dan Gohman89f85052007-10-22 18:31:58 +00001542 return getMulExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001543 }
1544
1545 // Okay, if there weren't any loop invariants to be folded, check to see if
1546 // there are multiple AddRec's with the same loop induction variable being
1547 // multiplied together. If so, we can fold them.
1548 for (unsigned OtherIdx = Idx+1;
1549 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
1550 if (OtherIdx != Idx) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00001551 const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001552 if (AddRec->getLoop() == OtherAddRec->getLoop()) {
1553 // F * G --> {A,+,B} * {C,+,D} --> {A*C,+,F*D + G*B + B*D}
Dan Gohmanbff6b582009-05-04 22:30:44 +00001554 const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec;
Dan Gohman89f85052007-10-22 18:31:58 +00001555 SCEVHandle NewStart = getMulExpr(F->getStart(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001556 G->getStart());
Dan Gohman89f85052007-10-22 18:31:58 +00001557 SCEVHandle B = F->getStepRecurrence(*this);
1558 SCEVHandle D = G->getStepRecurrence(*this);
1559 SCEVHandle NewStep = getAddExpr(getMulExpr(F, D),
1560 getMulExpr(G, B),
1561 getMulExpr(B, D));
1562 SCEVHandle NewAddRec = getAddRecExpr(NewStart, NewStep,
1563 F->getLoop());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001564 if (Ops.size() == 2) return NewAddRec;
1565
1566 Ops.erase(Ops.begin()+Idx);
1567 Ops.erase(Ops.begin()+OtherIdx-1);
1568 Ops.push_back(NewAddRec);
Dan Gohman89f85052007-10-22 18:31:58 +00001569 return getMulExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001570 }
1571 }
1572
1573 // Otherwise couldn't fold anything into this recurrence. Move onto the
1574 // next one.
1575 }
1576
1577 // Okay, it looks like we really DO need an mul expr. Check to see if we
1578 // already have one, otherwise create a new one.
Dan Gohmanbff6b582009-05-04 22:30:44 +00001579 std::vector<const SCEV*> SCEVOps(Ops.begin(), Ops.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001580 SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scMulExpr,
1581 SCEVOps)];
1582 if (Result == 0)
Owen Andersoncf4e2302009-06-18 22:25:12 +00001583 Result = new SCEVMulExpr(Ops, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001584 return Result;
1585}
1586
Dan Gohmanc8a29272009-05-24 23:45:28 +00001587/// getUDivExpr - Get a canonical multiply expression, or something simpler if
1588/// possible.
Dan Gohman77841cd2009-05-04 22:23:18 +00001589SCEVHandle ScalarEvolution::getUDivExpr(const SCEVHandle &LHS,
1590 const SCEVHandle &RHS) {
Dan Gohmana77b3d42009-05-18 15:44:58 +00001591 assert(getEffectiveSCEVType(LHS->getType()) ==
1592 getEffectiveSCEVType(RHS->getType()) &&
1593 "SCEVUDivExpr operand types don't match!");
1594
Dan Gohmanc76b5452009-05-04 22:02:23 +00001595 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001596 if (RHSC->getValue()->equalsInt(1))
Nick Lewycky35b56022009-01-13 09:18:58 +00001597 return LHS; // X udiv 1 --> x
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001598 if (RHSC->isZero())
1599 return getIntegerSCEV(0, LHS->getType()); // value is undefined
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001600
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001601 // Determine if the division can be folded into the operands of
1602 // its operands.
1603 // TODO: Generalize this to non-constants by using known-bits information.
1604 const Type *Ty = LHS->getType();
1605 unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros();
1606 unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ;
1607 // For non-power-of-two values, effectively round the value up to the
1608 // nearest power of two.
1609 if (!RHSC->getValue()->getValue().isPowerOf2())
1610 ++MaxShiftAmt;
1611 const IntegerType *ExtTy =
1612 IntegerType::get(getTypeSizeInBits(Ty) + MaxShiftAmt);
1613 // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
1614 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
1615 if (const SCEVConstant *Step =
1616 dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this)))
1617 if (!Step->getValue()->getValue()
1618 .urem(RHSC->getValue()->getValue()) &&
Dan Gohman14374d32009-05-08 23:11:16 +00001619 getZeroExtendExpr(AR, ExtTy) ==
1620 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
1621 getZeroExtendExpr(Step, ExtTy),
1622 AR->getLoop())) {
Dan Gohman02ff9392009-06-14 22:47:23 +00001623 SmallVector<SCEVHandle, 4> Operands;
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001624 for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i)
1625 Operands.push_back(getUDivExpr(AR->getOperand(i), RHS));
1626 return getAddRecExpr(Operands, AR->getLoop());
1627 }
1628 // (A*B)/C --> A*(B/C) if safe and B/C can be folded.
Dan Gohman14374d32009-05-08 23:11:16 +00001629 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
Dan Gohman02ff9392009-06-14 22:47:23 +00001630 SmallVector<SCEVHandle, 4> Operands;
Dan Gohman14374d32009-05-08 23:11:16 +00001631 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i)
1632 Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy));
1633 if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands))
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001634 // Find an operand that's safely divisible.
1635 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
1636 SCEVHandle Op = M->getOperand(i);
1637 SCEVHandle Div = getUDivExpr(Op, RHSC);
1638 if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) {
Dan Gohman02ff9392009-06-14 22:47:23 +00001639 const SmallVectorImpl<SCEVHandle> &MOperands = M->getOperands();
1640 Operands = SmallVector<SCEVHandle, 4>(MOperands.begin(),
1641 MOperands.end());
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001642 Operands[i] = Div;
1643 return getMulExpr(Operands);
1644 }
1645 }
Dan Gohman14374d32009-05-08 23:11:16 +00001646 }
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001647 // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded.
Dan Gohman14374d32009-05-08 23:11:16 +00001648 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) {
Dan Gohman02ff9392009-06-14 22:47:23 +00001649 SmallVector<SCEVHandle, 4> Operands;
Dan Gohman14374d32009-05-08 23:11:16 +00001650 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i)
1651 Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy));
1652 if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) {
1653 Operands.clear();
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001654 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
1655 SCEVHandle Op = getUDivExpr(A->getOperand(i), RHS);
1656 if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i))
1657 break;
1658 Operands.push_back(Op);
1659 }
1660 if (Operands.size() == A->getNumOperands())
1661 return getAddExpr(Operands);
1662 }
Dan Gohman14374d32009-05-08 23:11:16 +00001663 }
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001664
1665 // Fold if both operands are constant.
Dan Gohmanc76b5452009-05-04 22:02:23 +00001666 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001667 Constant *LHSCV = LHSC->getValue();
1668 Constant *RHSCV = RHSC->getValue();
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +00001669 return getUnknown(ConstantExpr::getUDiv(LHSCV, RHSCV));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001670 }
1671 }
1672
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +00001673 SCEVUDivExpr *&Result = (*SCEVUDivs)[std::make_pair(LHS, RHS)];
Owen Andersoncf4e2302009-06-18 22:25:12 +00001674 if (Result == 0) Result = new SCEVUDivExpr(LHS, RHS, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001675 return Result;
1676}
1677
1678
Dan Gohmanc8a29272009-05-24 23:45:28 +00001679/// getAddRecExpr - Get an add recurrence expression for the specified loop.
1680/// Simplify the expression as much as possible.
Dan Gohman89f85052007-10-22 18:31:58 +00001681SCEVHandle ScalarEvolution::getAddRecExpr(const SCEVHandle &Start,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001682 const SCEVHandle &Step, const Loop *L) {
Dan Gohman02ff9392009-06-14 22:47:23 +00001683 SmallVector<SCEVHandle, 4> Operands;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001684 Operands.push_back(Start);
Dan Gohmanc76b5452009-05-04 22:02:23 +00001685 if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001686 if (StepChrec->getLoop() == L) {
1687 Operands.insert(Operands.end(), StepChrec->op_begin(),
1688 StepChrec->op_end());
Dan Gohman89f85052007-10-22 18:31:58 +00001689 return getAddRecExpr(Operands, L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001690 }
1691
1692 Operands.push_back(Step);
Dan Gohman89f85052007-10-22 18:31:58 +00001693 return getAddRecExpr(Operands, L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001694}
1695
Dan Gohmanc8a29272009-05-24 23:45:28 +00001696/// getAddRecExpr - Get an add recurrence expression for the specified loop.
1697/// Simplify the expression as much as possible.
Dan Gohman02ff9392009-06-14 22:47:23 +00001698SCEVHandle ScalarEvolution::getAddRecExpr(SmallVectorImpl<SCEVHandle> &Operands,
Nick Lewycky37d04642009-04-23 05:15:08 +00001699 const Loop *L) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001700 if (Operands.size() == 1) return Operands[0];
Dan Gohmana77b3d42009-05-18 15:44:58 +00001701#ifndef NDEBUG
1702 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
1703 assert(getEffectiveSCEVType(Operands[i]->getType()) ==
1704 getEffectiveSCEVType(Operands[0]->getType()) &&
1705 "SCEVAddRecExpr operand types don't match!");
1706#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001707
Dan Gohman7b560c42008-06-18 16:23:07 +00001708 if (Operands.back()->isZero()) {
1709 Operands.pop_back();
Dan Gohmanabe991f2008-09-14 17:21:12 +00001710 return getAddRecExpr(Operands, L); // {X,+,0} --> X
Dan Gohman7b560c42008-06-18 16:23:07 +00001711 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001712
Dan Gohman42936882008-08-08 18:33:12 +00001713 // Canonicalize nested AddRecs in by nesting them in order of loop depth.
Dan Gohmanc76b5452009-05-04 22:02:23 +00001714 if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
Dan Gohman42936882008-08-08 18:33:12 +00001715 const Loop* NestedLoop = NestedAR->getLoop();
1716 if (L->getLoopDepth() < NestedLoop->getLoopDepth()) {
Dan Gohman02ff9392009-06-14 22:47:23 +00001717 SmallVector<SCEVHandle, 4> NestedOperands(NestedAR->op_begin(),
1718 NestedAR->op_end());
Dan Gohman42936882008-08-08 18:33:12 +00001719 SCEVHandle NestedARHandle(NestedAR);
1720 Operands[0] = NestedAR->getStart();
1721 NestedOperands[0] = getAddRecExpr(Operands, L);
1722 return getAddRecExpr(NestedOperands, NestedLoop);
1723 }
1724 }
1725
Dan Gohmanbff6b582009-05-04 22:30:44 +00001726 std::vector<const SCEV*> SCEVOps(Operands.begin(), Operands.end());
1727 SCEVAddRecExpr *&Result = (*SCEVAddRecExprs)[std::make_pair(L, SCEVOps)];
Owen Andersoncf4e2302009-06-18 22:25:12 +00001728 if (Result == 0) Result = new SCEVAddRecExpr(Operands, L, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001729 return Result;
1730}
1731
Nick Lewycky711640a2007-11-25 22:41:31 +00001732SCEVHandle ScalarEvolution::getSMaxExpr(const SCEVHandle &LHS,
1733 const SCEVHandle &RHS) {
Dan Gohman02ff9392009-06-14 22:47:23 +00001734 SmallVector<SCEVHandle, 2> Ops;
Nick Lewycky711640a2007-11-25 22:41:31 +00001735 Ops.push_back(LHS);
1736 Ops.push_back(RHS);
1737 return getSMaxExpr(Ops);
1738}
1739
Dan Gohman02ff9392009-06-14 22:47:23 +00001740SCEVHandle
1741ScalarEvolution::getSMaxExpr(SmallVectorImpl<SCEVHandle> &Ops) {
Nick Lewycky711640a2007-11-25 22:41:31 +00001742 assert(!Ops.empty() && "Cannot get empty smax!");
1743 if (Ops.size() == 1) return Ops[0];
Dan Gohmana77b3d42009-05-18 15:44:58 +00001744#ifndef NDEBUG
1745 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1746 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1747 getEffectiveSCEVType(Ops[0]->getType()) &&
1748 "SCEVSMaxExpr operand types don't match!");
1749#endif
Nick Lewycky711640a2007-11-25 22:41:31 +00001750
1751 // Sort by complexity, this groups all similar expression types together.
Dan Gohman5d486452009-05-07 14:39:04 +00001752 GroupByComplexity(Ops, LI);
Nick Lewycky711640a2007-11-25 22:41:31 +00001753
1754 // If there are any constants, fold them together.
1755 unsigned Idx = 0;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001756 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Nick Lewycky711640a2007-11-25 22:41:31 +00001757 ++Idx;
1758 assert(Idx < Ops.size());
Dan Gohmanc76b5452009-05-04 22:02:23 +00001759 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Nick Lewycky711640a2007-11-25 22:41:31 +00001760 // We found two constants, fold them together!
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001761 ConstantInt *Fold = ConstantInt::get(
Nick Lewycky711640a2007-11-25 22:41:31 +00001762 APIntOps::smax(LHSC->getValue()->getValue(),
1763 RHSC->getValue()->getValue()));
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001764 Ops[0] = getConstant(Fold);
1765 Ops.erase(Ops.begin()+1); // Erase the folded element
1766 if (Ops.size() == 1) return Ops[0];
1767 LHSC = cast<SCEVConstant>(Ops[0]);
Nick Lewycky711640a2007-11-25 22:41:31 +00001768 }
1769
1770 // If we are left with a constant -inf, strip it off.
1771 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) {
1772 Ops.erase(Ops.begin());
1773 --Idx;
1774 }
1775 }
1776
1777 if (Ops.size() == 1) return Ops[0];
1778
1779 // Find the first SMax
1780 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr)
1781 ++Idx;
1782
1783 // Check to see if one of the operands is an SMax. If so, expand its operands
1784 // onto our operand list, and recurse to simplify.
1785 if (Idx < Ops.size()) {
1786 bool DeletedSMax = false;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001787 while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) {
Nick Lewycky711640a2007-11-25 22:41:31 +00001788 Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end());
1789 Ops.erase(Ops.begin()+Idx);
1790 DeletedSMax = true;
1791 }
1792
1793 if (DeletedSMax)
1794 return getSMaxExpr(Ops);
1795 }
1796
1797 // Okay, check to see if the same value occurs in the operand list twice. If
1798 // so, delete one. Since we sorted the list, these values are required to
1799 // be adjacent.
1800 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
1801 if (Ops[i] == Ops[i+1]) { // X smax Y smax Y --> X smax Y
1802 Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
1803 --i; --e;
1804 }
1805
1806 if (Ops.size() == 1) return Ops[0];
1807
1808 assert(!Ops.empty() && "Reduced smax down to nothing!");
1809
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001810 // Okay, it looks like we really DO need an smax expr. Check to see if we
Nick Lewycky711640a2007-11-25 22:41:31 +00001811 // already have one, otherwise create a new one.
Dan Gohmanbff6b582009-05-04 22:30:44 +00001812 std::vector<const SCEV*> SCEVOps(Ops.begin(), Ops.end());
Nick Lewycky711640a2007-11-25 22:41:31 +00001813 SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scSMaxExpr,
1814 SCEVOps)];
Owen Andersoncf4e2302009-06-18 22:25:12 +00001815 if (Result == 0) Result = new SCEVSMaxExpr(Ops, this);
Nick Lewycky711640a2007-11-25 22:41:31 +00001816 return Result;
1817}
1818
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001819SCEVHandle ScalarEvolution::getUMaxExpr(const SCEVHandle &LHS,
1820 const SCEVHandle &RHS) {
Dan Gohman02ff9392009-06-14 22:47:23 +00001821 SmallVector<SCEVHandle, 2> Ops;
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001822 Ops.push_back(LHS);
1823 Ops.push_back(RHS);
1824 return getUMaxExpr(Ops);
1825}
1826
Dan Gohman02ff9392009-06-14 22:47:23 +00001827SCEVHandle
1828ScalarEvolution::getUMaxExpr(SmallVectorImpl<SCEVHandle> &Ops) {
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001829 assert(!Ops.empty() && "Cannot get empty umax!");
1830 if (Ops.size() == 1) return Ops[0];
Dan Gohmana77b3d42009-05-18 15:44:58 +00001831#ifndef NDEBUG
1832 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1833 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1834 getEffectiveSCEVType(Ops[0]->getType()) &&
1835 "SCEVUMaxExpr operand types don't match!");
1836#endif
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001837
1838 // Sort by complexity, this groups all similar expression types together.
Dan Gohman5d486452009-05-07 14:39:04 +00001839 GroupByComplexity(Ops, LI);
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001840
1841 // If there are any constants, fold them together.
1842 unsigned Idx = 0;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001843 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001844 ++Idx;
1845 assert(Idx < Ops.size());
Dan Gohmanc76b5452009-05-04 22:02:23 +00001846 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001847 // We found two constants, fold them together!
1848 ConstantInt *Fold = ConstantInt::get(
1849 APIntOps::umax(LHSC->getValue()->getValue(),
1850 RHSC->getValue()->getValue()));
1851 Ops[0] = getConstant(Fold);
1852 Ops.erase(Ops.begin()+1); // Erase the folded element
1853 if (Ops.size() == 1) return Ops[0];
1854 LHSC = cast<SCEVConstant>(Ops[0]);
1855 }
1856
1857 // If we are left with a constant zero, strip it off.
1858 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) {
1859 Ops.erase(Ops.begin());
1860 --Idx;
1861 }
1862 }
1863
1864 if (Ops.size() == 1) return Ops[0];
1865
1866 // Find the first UMax
1867 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr)
1868 ++Idx;
1869
1870 // Check to see if one of the operands is a UMax. If so, expand its operands
1871 // onto our operand list, and recurse to simplify.
1872 if (Idx < Ops.size()) {
1873 bool DeletedUMax = false;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001874 while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) {
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001875 Ops.insert(Ops.end(), UMax->op_begin(), UMax->op_end());
1876 Ops.erase(Ops.begin()+Idx);
1877 DeletedUMax = true;
1878 }
1879
1880 if (DeletedUMax)
1881 return getUMaxExpr(Ops);
1882 }
1883
1884 // Okay, check to see if the same value occurs in the operand list twice. If
1885 // so, delete one. Since we sorted the list, these values are required to
1886 // be adjacent.
1887 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
1888 if (Ops[i] == Ops[i+1]) { // X umax Y umax Y --> X umax Y
1889 Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
1890 --i; --e;
1891 }
1892
1893 if (Ops.size() == 1) return Ops[0];
1894
1895 assert(!Ops.empty() && "Reduced umax down to nothing!");
1896
1897 // Okay, it looks like we really DO need a umax expr. Check to see if we
1898 // already have one, otherwise create a new one.
Dan Gohmanbff6b582009-05-04 22:30:44 +00001899 std::vector<const SCEV*> SCEVOps(Ops.begin(), Ops.end());
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001900 SCEVCommutativeExpr *&Result = (*SCEVCommExprs)[std::make_pair(scUMaxExpr,
1901 SCEVOps)];
Owen Andersoncf4e2302009-06-18 22:25:12 +00001902 if (Result == 0) Result = new SCEVUMaxExpr(Ops, this);
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001903 return Result;
1904}
1905
Dan Gohmand01fff82009-06-22 03:18:45 +00001906SCEVHandle ScalarEvolution::getSMinExpr(const SCEVHandle &LHS,
1907 const SCEVHandle &RHS) {
1908 // ~smax(~x, ~y) == smin(x, y).
1909 return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
1910}
1911
1912SCEVHandle ScalarEvolution::getUMinExpr(const SCEVHandle &LHS,
1913 const SCEVHandle &RHS) {
1914 // ~umax(~x, ~y) == umin(x, y)
1915 return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
1916}
1917
Dan Gohman89f85052007-10-22 18:31:58 +00001918SCEVHandle ScalarEvolution::getUnknown(Value *V) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001919 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
Dan Gohman89f85052007-10-22 18:31:58 +00001920 return getConstant(CI);
Dan Gohman01c2ee72009-04-16 03:18:22 +00001921 if (isa<ConstantPointerNull>(V))
1922 return getIntegerSCEV(0, V->getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001923 SCEVUnknown *&Result = (*SCEVUnknowns)[V];
Owen Andersoncf4e2302009-06-18 22:25:12 +00001924 if (Result == 0) Result = new SCEVUnknown(V, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001925 return Result;
1926}
1927
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001928//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001929// Basic SCEV Analysis and PHI Idiom Recognition Code
1930//
1931
Dan Gohmanb98c1a32009-04-21 01:07:12 +00001932/// isSCEVable - Test if values of the given type are analyzable within
1933/// the SCEV framework. This primarily includes integer types, and it
1934/// can optionally include pointer types if the ScalarEvolution class
1935/// has access to target-specific information.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00001936bool ScalarEvolution::isSCEVable(const Type *Ty) const {
Dan Gohmanb98c1a32009-04-21 01:07:12 +00001937 // Integers are always SCEVable.
1938 if (Ty->isInteger())
1939 return true;
1940
1941 // Pointers are SCEVable if TargetData information is available
1942 // to provide pointer size information.
1943 if (isa<PointerType>(Ty))
1944 return TD != NULL;
1945
1946 // Otherwise it's not SCEVable.
1947 return false;
1948}
1949
1950/// getTypeSizeInBits - Return the size in bits of the specified type,
1951/// for which isSCEVable must return true.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00001952uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const {
Dan Gohmanb98c1a32009-04-21 01:07:12 +00001953 assert(isSCEVable(Ty) && "Type is not SCEVable!");
1954
1955 // If we have a TargetData, use it!
1956 if (TD)
1957 return TD->getTypeSizeInBits(Ty);
1958
1959 // Otherwise, we support only integer types.
1960 assert(Ty->isInteger() && "isSCEVable permitted a non-SCEVable type!");
1961 return Ty->getPrimitiveSizeInBits();
1962}
1963
1964/// getEffectiveSCEVType - Return a type with the same bitwidth as
1965/// the given type and which represents how SCEV will treat the given
1966/// type, for which isSCEVable must return true. For pointer types,
1967/// this is the pointer-sized integer type.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00001968const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const {
Dan Gohmanb98c1a32009-04-21 01:07:12 +00001969 assert(isSCEVable(Ty) && "Type is not SCEVable!");
1970
1971 if (Ty->isInteger())
1972 return Ty;
1973
1974 assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!");
1975 return TD->getIntPtrType();
Dan Gohman01c2ee72009-04-16 03:18:22 +00001976}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001977
Dan Gohmanffd36ba2009-04-21 23:15:49 +00001978SCEVHandle ScalarEvolution::getCouldNotCompute() {
Dan Gohman0c850912009-06-06 14:37:11 +00001979 return CouldNotCompute;
Dan Gohman0ad08b02009-04-18 17:58:19 +00001980}
1981
Dan Gohmand83d4af2009-05-04 22:20:30 +00001982/// hasSCEV - Return true if the SCEV for this value has already been
Edwin Török0e828d62009-05-01 08:33:47 +00001983/// computed.
1984bool ScalarEvolution::hasSCEV(Value *V) const {
1985 return Scalars.count(V);
1986}
1987
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001988/// getSCEV - Return an existing SCEV if it exists, otherwise analyze the
1989/// expression and create a new one.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00001990SCEVHandle ScalarEvolution::getSCEV(Value *V) {
Dan Gohmanb98c1a32009-04-21 01:07:12 +00001991 assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001992
Dan Gohmanbff6b582009-05-04 22:30:44 +00001993 std::map<SCEVCallbackVH, SCEVHandle>::iterator I = Scalars.find(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001994 if (I != Scalars.end()) return I->second;
1995 SCEVHandle S = createSCEV(V);
Dan Gohmanbff6b582009-05-04 22:30:44 +00001996 Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001997 return S;
1998}
1999
Dan Gohman01c2ee72009-04-16 03:18:22 +00002000/// getIntegerSCEV - Given an integer or FP type, create a constant for the
2001/// specified signed integer value and return a SCEV for the constant.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002002SCEVHandle ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) {
2003 Ty = getEffectiveSCEVType(Ty);
Dan Gohman01c2ee72009-04-16 03:18:22 +00002004 Constant *C;
2005 if (Val == 0)
2006 C = Constant::getNullValue(Ty);
2007 else if (Ty->isFloatingPoint())
2008 C = ConstantFP::get(APFloat(Ty==Type::FloatTy ? APFloat::IEEEsingle :
2009 APFloat::IEEEdouble, Val));
2010 else
2011 C = ConstantInt::get(Ty, Val);
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002012 return getUnknown(C);
Dan Gohman01c2ee72009-04-16 03:18:22 +00002013}
2014
2015/// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
2016///
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002017SCEVHandle ScalarEvolution::getNegativeSCEV(const SCEVHandle &V) {
Dan Gohmanc76b5452009-05-04 22:02:23 +00002018 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002019 return getUnknown(ConstantExpr::getNeg(VC->getValue()));
Dan Gohman01c2ee72009-04-16 03:18:22 +00002020
2021 const Type *Ty = V->getType();
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002022 Ty = getEffectiveSCEVType(Ty);
2023 return getMulExpr(V, getConstant(ConstantInt::getAllOnesValue(Ty)));
Dan Gohman01c2ee72009-04-16 03:18:22 +00002024}
2025
2026/// getNotSCEV - Return a SCEV corresponding to ~V = -1-V
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002027SCEVHandle ScalarEvolution::getNotSCEV(const SCEVHandle &V) {
Dan Gohmanc76b5452009-05-04 22:02:23 +00002028 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002029 return getUnknown(ConstantExpr::getNot(VC->getValue()));
Dan Gohman01c2ee72009-04-16 03:18:22 +00002030
2031 const Type *Ty = V->getType();
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002032 Ty = getEffectiveSCEVType(Ty);
2033 SCEVHandle AllOnes = getConstant(ConstantInt::getAllOnesValue(Ty));
Dan Gohman01c2ee72009-04-16 03:18:22 +00002034 return getMinusSCEV(AllOnes, V);
2035}
2036
2037/// getMinusSCEV - Return a SCEV corresponding to LHS - RHS.
2038///
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002039SCEVHandle ScalarEvolution::getMinusSCEV(const SCEVHandle &LHS,
Nick Lewycky37d04642009-04-23 05:15:08 +00002040 const SCEVHandle &RHS) {
Dan Gohman01c2ee72009-04-16 03:18:22 +00002041 // X - Y --> X + -Y
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002042 return getAddExpr(LHS, getNegativeSCEV(RHS));
Dan Gohman01c2ee72009-04-16 03:18:22 +00002043}
2044
2045/// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the
2046/// input value to the specified type. If the type must be extended, it is zero
2047/// extended.
2048SCEVHandle
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002049ScalarEvolution::getTruncateOrZeroExtend(const SCEVHandle &V,
Nick Lewycky37d04642009-04-23 05:15:08 +00002050 const Type *Ty) {
Dan Gohman01c2ee72009-04-16 03:18:22 +00002051 const Type *SrcTy = V->getType();
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002052 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2053 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
Dan Gohman01c2ee72009-04-16 03:18:22 +00002054 "Cannot truncate or zero extend with non-integer arguments!");
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002055 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
Dan Gohman01c2ee72009-04-16 03:18:22 +00002056 return V; // No conversion
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002057 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002058 return getTruncateExpr(V, Ty);
2059 return getZeroExtendExpr(V, Ty);
Dan Gohman01c2ee72009-04-16 03:18:22 +00002060}
2061
2062/// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the
2063/// input value to the specified type. If the type must be extended, it is sign
2064/// extended.
2065SCEVHandle
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002066ScalarEvolution::getTruncateOrSignExtend(const SCEVHandle &V,
Nick Lewycky37d04642009-04-23 05:15:08 +00002067 const Type *Ty) {
Dan Gohman01c2ee72009-04-16 03:18:22 +00002068 const Type *SrcTy = V->getType();
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002069 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2070 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
Dan Gohman01c2ee72009-04-16 03:18:22 +00002071 "Cannot truncate or zero extend with non-integer arguments!");
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002072 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
Dan Gohman01c2ee72009-04-16 03:18:22 +00002073 return V; // No conversion
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002074 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002075 return getTruncateExpr(V, Ty);
2076 return getSignExtendExpr(V, Ty);
Dan Gohman01c2ee72009-04-16 03:18:22 +00002077}
2078
Dan Gohmanac959332009-05-13 03:46:30 +00002079/// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the
2080/// input value to the specified type. If the type must be extended, it is zero
2081/// extended. The conversion must not be narrowing.
2082SCEVHandle
2083ScalarEvolution::getNoopOrZeroExtend(const SCEVHandle &V, const Type *Ty) {
2084 const Type *SrcTy = V->getType();
2085 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2086 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
2087 "Cannot noop or zero extend with non-integer arguments!");
2088 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2089 "getNoopOrZeroExtend cannot truncate!");
2090 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2091 return V; // No conversion
2092 return getZeroExtendExpr(V, Ty);
2093}
2094
2095/// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the
2096/// input value to the specified type. If the type must be extended, it is sign
2097/// extended. The conversion must not be narrowing.
2098SCEVHandle
2099ScalarEvolution::getNoopOrSignExtend(const SCEVHandle &V, const Type *Ty) {
2100 const Type *SrcTy = V->getType();
2101 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2102 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
2103 "Cannot noop or sign extend with non-integer arguments!");
2104 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2105 "getNoopOrSignExtend cannot truncate!");
2106 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2107 return V; // No conversion
2108 return getSignExtendExpr(V, Ty);
2109}
2110
Dan Gohmane1ca7e82009-06-13 15:56:47 +00002111/// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of
2112/// the input value to the specified type. If the type must be extended,
2113/// it is extended with unspecified bits. The conversion must not be
2114/// narrowing.
2115SCEVHandle
2116ScalarEvolution::getNoopOrAnyExtend(const SCEVHandle &V, const Type *Ty) {
2117 const Type *SrcTy = V->getType();
2118 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2119 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
2120 "Cannot noop or any extend with non-integer arguments!");
2121 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2122 "getNoopOrAnyExtend cannot truncate!");
2123 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2124 return V; // No conversion
2125 return getAnyExtendExpr(V, Ty);
2126}
2127
Dan Gohmanac959332009-05-13 03:46:30 +00002128/// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the
2129/// input value to the specified type. The conversion must not be widening.
2130SCEVHandle
2131ScalarEvolution::getTruncateOrNoop(const SCEVHandle &V, const Type *Ty) {
2132 const Type *SrcTy = V->getType();
2133 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2134 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
2135 "Cannot truncate or noop with non-integer arguments!");
2136 assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) &&
2137 "getTruncateOrNoop cannot extend!");
2138 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2139 return V; // No conversion
2140 return getTruncateExpr(V, Ty);
2141}
2142
Dan Gohman8e8b5232009-06-22 00:31:57 +00002143/// getUMaxFromMismatchedTypes - Promote the operands to the wider of
2144/// the types using zero-extension, and then perform a umax operation
2145/// with them.
2146SCEVHandle ScalarEvolution::getUMaxFromMismatchedTypes(const SCEVHandle &LHS,
2147 const SCEVHandle &RHS) {
2148 SCEVHandle PromotedLHS = LHS;
2149 SCEVHandle PromotedRHS = RHS;
2150
2151 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
2152 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
2153 else
2154 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
2155
2156 return getUMaxExpr(PromotedLHS, PromotedRHS);
2157}
2158
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002159/// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value for
2160/// the specified instruction and replaces any references to the symbolic value
2161/// SymName with the specified value. This is used during PHI resolution.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002162void ScalarEvolution::
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002163ReplaceSymbolicValueWithConcrete(Instruction *I, const SCEVHandle &SymName,
2164 const SCEVHandle &NewVal) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00002165 std::map<SCEVCallbackVH, SCEVHandle>::iterator SI =
2166 Scalars.find(SCEVCallbackVH(I, this));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002167 if (SI == Scalars.end()) return;
2168
2169 SCEVHandle NV =
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002170 SI->second->replaceSymbolicValuesWithConcrete(SymName, NewVal, *this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002171 if (NV == SI->second) return; // No change.
2172
2173 SI->second = NV; // Update the scalars map!
2174
2175 // Any instruction values that use this instruction might also need to be
2176 // updated!
2177 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
2178 UI != E; ++UI)
2179 ReplaceSymbolicValueWithConcrete(cast<Instruction>(*UI), SymName, NewVal);
2180}
2181
2182/// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in
2183/// a loop header, making it a potential recurrence, or it doesn't.
2184///
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002185SCEVHandle ScalarEvolution::createNodeForPHI(PHINode *PN) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002186 if (PN->getNumIncomingValues() == 2) // The loops have been canonicalized.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002187 if (const Loop *L = LI->getLoopFor(PN->getParent()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002188 if (L->getHeader() == PN->getParent()) {
2189 // If it lives in the loop header, it has two incoming values, one
2190 // from outside the loop, and one from inside.
2191 unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0));
2192 unsigned BackEdge = IncomingEdge^1;
2193
2194 // While we are analyzing this PHI node, handle its value symbolically.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002195 SCEVHandle SymbolicName = getUnknown(PN);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002196 assert(Scalars.find(PN) == Scalars.end() &&
2197 "PHI node already processed?");
Dan Gohmanbff6b582009-05-04 22:30:44 +00002198 Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002199
2200 // Using this symbolic name for the PHI, analyze the value coming around
2201 // the back-edge.
2202 SCEVHandle BEValue = getSCEV(PN->getIncomingValue(BackEdge));
2203
2204 // NOTE: If BEValue is loop invariant, we know that the PHI node just
2205 // has a special value for the first iteration of the loop.
2206
2207 // If the value coming around the backedge is an add with the symbolic
2208 // value we just inserted, then we found a simple induction variable!
Dan Gohmanc76b5452009-05-04 22:02:23 +00002209 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002210 // If there is a single occurrence of the symbolic value, replace it
2211 // with a recurrence.
2212 unsigned FoundIndex = Add->getNumOperands();
2213 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
2214 if (Add->getOperand(i) == SymbolicName)
2215 if (FoundIndex == e) {
2216 FoundIndex = i;
2217 break;
2218 }
2219
2220 if (FoundIndex != Add->getNumOperands()) {
2221 // Create an add with everything but the specified operand.
Dan Gohman02ff9392009-06-14 22:47:23 +00002222 SmallVector<SCEVHandle, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002223 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
2224 if (i != FoundIndex)
2225 Ops.push_back(Add->getOperand(i));
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002226 SCEVHandle Accum = getAddExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002227
2228 // This is not a valid addrec if the step amount is varying each
2229 // loop iteration, but is not itself an addrec in this loop.
2230 if (Accum->isLoopInvariant(L) ||
2231 (isa<SCEVAddRecExpr>(Accum) &&
2232 cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
2233 SCEVHandle StartVal = getSCEV(PN->getIncomingValue(IncomingEdge));
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002234 SCEVHandle PHISCEV = getAddRecExpr(StartVal, Accum, L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002235
2236 // Okay, for the entire analysis of this edge we assumed the PHI
2237 // to be symbolic. We now need to go back and update all of the
2238 // entries for the scalars that use the PHI (except for the PHI
2239 // itself) to use the new analyzed value instead of the "symbolic"
2240 // value.
2241 ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV);
2242 return PHISCEV;
2243 }
2244 }
Dan Gohmanc76b5452009-05-04 22:02:23 +00002245 } else if (const SCEVAddRecExpr *AddRec =
2246 dyn_cast<SCEVAddRecExpr>(BEValue)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002247 // Otherwise, this could be a loop like this:
2248 // i = 0; for (j = 1; ..; ++j) { .... i = j; }
2249 // In this case, j = {1,+,1} and BEValue is j.
2250 // Because the other in-value of i (0) fits the evolution of BEValue
2251 // i really is an addrec evolution.
2252 if (AddRec->getLoop() == L && AddRec->isAffine()) {
2253 SCEVHandle StartVal = getSCEV(PN->getIncomingValue(IncomingEdge));
2254
2255 // If StartVal = j.start - j.stride, we can use StartVal as the
2256 // initial step of the addrec evolution.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002257 if (StartVal == getMinusSCEV(AddRec->getOperand(0),
Dan Gohman89f85052007-10-22 18:31:58 +00002258 AddRec->getOperand(1))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002259 SCEVHandle PHISCEV =
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002260 getAddRecExpr(StartVal, AddRec->getOperand(1), L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002261
2262 // Okay, for the entire analysis of this edge we assumed the PHI
2263 // to be symbolic. We now need to go back and update all of the
2264 // entries for the scalars that use the PHI (except for the PHI
2265 // itself) to use the new analyzed value instead of the "symbolic"
2266 // value.
2267 ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV);
2268 return PHISCEV;
2269 }
2270 }
2271 }
2272
2273 return SymbolicName;
2274 }
2275
2276 // If it's not a loop phi, we can't handle it yet.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002277 return getUnknown(PN);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002278}
2279
Dan Gohman509cf4d2009-05-08 20:26:55 +00002280/// createNodeForGEP - Expand GEP instructions into add and multiply
2281/// operations. This allows them to be analyzed by regular SCEV code.
2282///
Dan Gohmanca5a39e2009-05-08 20:58:38 +00002283SCEVHandle ScalarEvolution::createNodeForGEP(User *GEP) {
Dan Gohman509cf4d2009-05-08 20:26:55 +00002284
2285 const Type *IntPtrTy = TD->getIntPtrType();
Dan Gohmanc7034fa2009-05-08 20:36:47 +00002286 Value *Base = GEP->getOperand(0);
Dan Gohmand586a4f2009-05-09 00:14:52 +00002287 // Don't attempt to analyze GEPs over unsized objects.
2288 if (!cast<PointerType>(Base->getType())->getElementType()->isSized())
2289 return getUnknown(GEP);
Dan Gohman509cf4d2009-05-08 20:26:55 +00002290 SCEVHandle TotalOffset = getIntegerSCEV(0, IntPtrTy);
Dan Gohmanc7034fa2009-05-08 20:36:47 +00002291 gep_type_iterator GTI = gep_type_begin(GEP);
2292 for (GetElementPtrInst::op_iterator I = next(GEP->op_begin()),
2293 E = GEP->op_end();
Dan Gohman509cf4d2009-05-08 20:26:55 +00002294 I != E; ++I) {
2295 Value *Index = *I;
2296 // Compute the (potentially symbolic) offset in bytes for this index.
2297 if (const StructType *STy = dyn_cast<StructType>(*GTI++)) {
2298 // For a struct, add the member offset.
2299 const StructLayout &SL = *TD->getStructLayout(STy);
2300 unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
2301 uint64_t Offset = SL.getElementOffset(FieldNo);
2302 TotalOffset = getAddExpr(TotalOffset,
2303 getIntegerSCEV(Offset, IntPtrTy));
2304 } else {
2305 // For an array, add the element offset, explicitly scaled.
2306 SCEVHandle LocalOffset = getSCEV(Index);
2307 if (!isa<PointerType>(LocalOffset->getType()))
2308 // Getelementptr indicies are signed.
2309 LocalOffset = getTruncateOrSignExtend(LocalOffset,
2310 IntPtrTy);
2311 LocalOffset =
2312 getMulExpr(LocalOffset,
Duncan Sandsec4f97d2009-05-09 07:06:46 +00002313 getIntegerSCEV(TD->getTypeAllocSize(*GTI),
Dan Gohman509cf4d2009-05-08 20:26:55 +00002314 IntPtrTy));
2315 TotalOffset = getAddExpr(TotalOffset, LocalOffset);
2316 }
2317 }
2318 return getAddExpr(getSCEV(Base), TotalOffset);
2319}
2320
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002321/// GetMinTrailingZeros - Determine the minimum number of zero bits that S is
2322/// guaranteed to end in (at every loop iteration). It is, at the same time,
2323/// the minimum number of times S is divisible by 2. For example, given {4,+,8}
2324/// it returns 2. If S is guaranteed to be 0, it returns the bitwidth of S.
Dan Gohman6e923a72009-06-19 23:29:04 +00002325uint32_t
2326ScalarEvolution::GetMinTrailingZeros(const SCEVHandle &S) {
Dan Gohmanc76b5452009-05-04 22:02:23 +00002327 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
Chris Lattner6ecce2a2007-11-23 22:36:49 +00002328 return C->getValue()->getValue().countTrailingZeros();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002329
Dan Gohmanc76b5452009-05-04 22:02:23 +00002330 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S))
Dan Gohman6e923a72009-06-19 23:29:04 +00002331 return std::min(GetMinTrailingZeros(T->getOperand()),
2332 (uint32_t)getTypeSizeInBits(T->getType()));
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002333
Dan Gohmanc76b5452009-05-04 22:02:23 +00002334 if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) {
Dan Gohman6e923a72009-06-19 23:29:04 +00002335 uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
2336 return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
2337 getTypeSizeInBits(E->getType()) : OpRes;
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002338 }
2339
Dan Gohmanc76b5452009-05-04 22:02:23 +00002340 if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) {
Dan Gohman6e923a72009-06-19 23:29:04 +00002341 uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
2342 return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
2343 getTypeSizeInBits(E->getType()) : OpRes;
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002344 }
2345
Dan Gohmanc76b5452009-05-04 22:02:23 +00002346 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002347 // The result is the min of all operands results.
Dan Gohman6e923a72009-06-19 23:29:04 +00002348 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002349 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman6e923a72009-06-19 23:29:04 +00002350 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002351 return MinOpRes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002352 }
2353
Dan Gohmanc76b5452009-05-04 22:02:23 +00002354 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002355 // The result is the sum of all operands results.
Dan Gohman6e923a72009-06-19 23:29:04 +00002356 uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0));
2357 uint32_t BitWidth = getTypeSizeInBits(M->getType());
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002358 for (unsigned i = 1, e = M->getNumOperands();
2359 SumOpRes != BitWidth && i != e; ++i)
Dan Gohman6e923a72009-06-19 23:29:04 +00002360 SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)),
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002361 BitWidth);
2362 return SumOpRes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002363 }
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002364
Dan Gohmanc76b5452009-05-04 22:02:23 +00002365 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002366 // The result is the min of all operands results.
Dan Gohman6e923a72009-06-19 23:29:04 +00002367 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002368 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman6e923a72009-06-19 23:29:04 +00002369 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002370 return MinOpRes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002371 }
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002372
Dan Gohmanc76b5452009-05-04 22:02:23 +00002373 if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) {
Nick Lewycky711640a2007-11-25 22:41:31 +00002374 // The result is the min of all operands results.
Dan Gohman6e923a72009-06-19 23:29:04 +00002375 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
Nick Lewycky711640a2007-11-25 22:41:31 +00002376 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman6e923a72009-06-19 23:29:04 +00002377 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
Nick Lewycky711640a2007-11-25 22:41:31 +00002378 return MinOpRes;
2379 }
2380
Dan Gohmanc76b5452009-05-04 22:02:23 +00002381 if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) {
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00002382 // The result is the min of all operands results.
Dan Gohman6e923a72009-06-19 23:29:04 +00002383 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00002384 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman6e923a72009-06-19 23:29:04 +00002385 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00002386 return MinOpRes;
2387 }
2388
Dan Gohman6e923a72009-06-19 23:29:04 +00002389 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2390 // For a SCEVUnknown, ask ValueTracking.
2391 unsigned BitWidth = getTypeSizeInBits(U->getType());
2392 APInt Mask = APInt::getAllOnesValue(BitWidth);
2393 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
2394 ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones);
2395 return Zeros.countTrailingOnes();
2396 }
2397
2398 // SCEVUDivExpr
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002399 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002400}
2401
Dan Gohman6e923a72009-06-19 23:29:04 +00002402uint32_t
2403ScalarEvolution::GetMinLeadingZeros(const SCEVHandle &S) {
2404 // TODO: Handle other SCEV expression types here.
2405
2406 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
2407 return C->getValue()->getValue().countLeadingZeros();
2408
2409 if (const SCEVZeroExtendExpr *C = dyn_cast<SCEVZeroExtendExpr>(S)) {
2410 // A zero-extension cast adds zero bits.
2411 return GetMinLeadingZeros(C->getOperand()) +
2412 (getTypeSizeInBits(C->getType()) -
2413 getTypeSizeInBits(C->getOperand()->getType()));
2414 }
2415
2416 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2417 // For a SCEVUnknown, ask ValueTracking.
2418 unsigned BitWidth = getTypeSizeInBits(U->getType());
2419 APInt Mask = APInt::getAllOnesValue(BitWidth);
2420 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
2421 ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD);
2422 return Zeros.countLeadingOnes();
2423 }
2424
2425 return 1;
2426}
2427
2428uint32_t
2429ScalarEvolution::GetMinSignBits(const SCEVHandle &S) {
2430 // TODO: Handle other SCEV expression types here.
2431
2432 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) {
2433 const APInt &A = C->getValue()->getValue();
2434 return A.isNegative() ? A.countLeadingOnes() :
2435 A.countLeadingZeros();
2436 }
2437
2438 if (const SCEVSignExtendExpr *C = dyn_cast<SCEVSignExtendExpr>(S)) {
2439 // A sign-extension cast adds sign bits.
2440 return GetMinSignBits(C->getOperand()) +
2441 (getTypeSizeInBits(C->getType()) -
2442 getTypeSizeInBits(C->getOperand()->getType()));
2443 }
2444
2445 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2446 // For a SCEVUnknown, ask ValueTracking.
2447 return ComputeNumSignBits(U->getValue(), TD);
2448 }
2449
2450 return 1;
2451}
2452
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002453/// createSCEV - We know that there is no SCEV for the specified value.
2454/// Analyze the expression.
2455///
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002456SCEVHandle ScalarEvolution::createSCEV(Value *V) {
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002457 if (!isSCEVable(V->getType()))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002458 return getUnknown(V);
Dan Gohman01c2ee72009-04-16 03:18:22 +00002459
Dan Gohman3996f472008-06-22 19:56:46 +00002460 unsigned Opcode = Instruction::UserOp1;
2461 if (Instruction *I = dyn_cast<Instruction>(V))
2462 Opcode = I->getOpcode();
2463 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
2464 Opcode = CE->getOpcode();
2465 else
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002466 return getUnknown(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002467
Dan Gohman3996f472008-06-22 19:56:46 +00002468 User *U = cast<User>(V);
2469 switch (Opcode) {
2470 case Instruction::Add:
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002471 return getAddExpr(getSCEV(U->getOperand(0)),
2472 getSCEV(U->getOperand(1)));
Dan Gohman3996f472008-06-22 19:56:46 +00002473 case Instruction::Mul:
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002474 return getMulExpr(getSCEV(U->getOperand(0)),
2475 getSCEV(U->getOperand(1)));
Dan Gohman3996f472008-06-22 19:56:46 +00002476 case Instruction::UDiv:
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002477 return getUDivExpr(getSCEV(U->getOperand(0)),
2478 getSCEV(U->getOperand(1)));
Dan Gohman3996f472008-06-22 19:56:46 +00002479 case Instruction::Sub:
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002480 return getMinusSCEV(getSCEV(U->getOperand(0)),
2481 getSCEV(U->getOperand(1)));
Dan Gohman53bf64a2009-04-21 02:26:00 +00002482 case Instruction::And:
2483 // For an expression like x&255 that merely masks off the high bits,
2484 // use zext(trunc(x)) as the SCEV expression.
2485 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman91ae1e72009-04-25 17:05:40 +00002486 if (CI->isNullValue())
2487 return getSCEV(U->getOperand(1));
Dan Gohmanc7ebba12009-04-27 01:41:10 +00002488 if (CI->isAllOnesValue())
2489 return getSCEV(U->getOperand(0));
Dan Gohman53bf64a2009-04-21 02:26:00 +00002490 const APInt &A = CI->getValue();
Dan Gohmana7726c32009-06-16 19:52:01 +00002491
2492 // Instcombine's ShrinkDemandedConstant may strip bits out of
2493 // constants, obscuring what would otherwise be a low-bits mask.
2494 // Use ComputeMaskedBits to compute what ShrinkDemandedConstant
2495 // knew about to reconstruct a low-bits mask value.
2496 unsigned LZ = A.countLeadingZeros();
2497 unsigned BitWidth = A.getBitWidth();
2498 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
2499 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
2500 ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD);
2501
2502 APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ);
2503
Dan Gohmanae1d7dd2009-06-17 23:54:37 +00002504 if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask))
Dan Gohman53bf64a2009-04-21 02:26:00 +00002505 return
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002506 getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)),
Dan Gohmana7726c32009-06-16 19:52:01 +00002507 IntegerType::get(BitWidth - LZ)),
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002508 U->getType());
Dan Gohman53bf64a2009-04-21 02:26:00 +00002509 }
2510 break;
Dan Gohmana7726c32009-06-16 19:52:01 +00002511
Dan Gohman3996f472008-06-22 19:56:46 +00002512 case Instruction::Or:
2513 // If the RHS of the Or is a constant, we may have something like:
2514 // X*4+1 which got turned into X*4|1. Handle this as an Add so loop
2515 // optimizations will transparently handle this case.
2516 //
2517 // In order for this transformation to be safe, the LHS must be of the
2518 // form X*(2^n) and the Or constant must be less than 2^n.
2519 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
2520 SCEVHandle LHS = getSCEV(U->getOperand(0));
2521 const APInt &CIVal = CI->getValue();
Dan Gohman6e923a72009-06-19 23:29:04 +00002522 if (GetMinTrailingZeros(LHS) >=
Dan Gohman3996f472008-06-22 19:56:46 +00002523 (CIVal.getBitWidth() - CIVal.countLeadingZeros()))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002524 return getAddExpr(LHS, getSCEV(U->getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002525 }
Dan Gohman3996f472008-06-22 19:56:46 +00002526 break;
2527 case Instruction::Xor:
Dan Gohman3996f472008-06-22 19:56:46 +00002528 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Nick Lewycky7fd27892008-07-07 06:15:49 +00002529 // If the RHS of the xor is a signbit, then this is just an add.
2530 // Instcombine turns add of signbit into xor as a strength reduction step.
Dan Gohman3996f472008-06-22 19:56:46 +00002531 if (CI->getValue().isSignBit())
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002532 return getAddExpr(getSCEV(U->getOperand(0)),
2533 getSCEV(U->getOperand(1)));
Nick Lewycky7fd27892008-07-07 06:15:49 +00002534
2535 // If the RHS of xor is -1, then this is a not operation.
Dan Gohmanc897f752009-05-18 16:17:44 +00002536 if (CI->isAllOnesValue())
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002537 return getNotSCEV(getSCEV(U->getOperand(0)));
Dan Gohmanfc78cff2009-05-18 16:29:04 +00002538
2539 // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask.
2540 // This is a variant of the check for xor with -1, and it handles
2541 // the case where instcombine has trimmed non-demanded bits out
2542 // of an xor with -1.
2543 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0)))
2544 if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1)))
2545 if (BO->getOpcode() == Instruction::And &&
2546 LCI->getValue() == CI->getValue())
2547 if (const SCEVZeroExtendExpr *Z =
Dan Gohmane49ae432009-06-17 01:22:39 +00002548 dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) {
Dan Gohmaned1d8bb2009-06-18 00:00:20 +00002549 const Type *UTy = U->getType();
2550 SCEVHandle Z0 = Z->getOperand();
2551 const Type *Z0Ty = Z0->getType();
2552 unsigned Z0TySize = getTypeSizeInBits(Z0Ty);
2553
2554 // If C is a low-bits mask, the zero extend is zerving to
2555 // mask off the high bits. Complement the operand and
2556 // re-apply the zext.
2557 if (APIntOps::isMask(Z0TySize, CI->getValue()))
2558 return getZeroExtendExpr(getNotSCEV(Z0), UTy);
2559
2560 // If C is a single bit, it may be in the sign-bit position
2561 // before the zero-extend. In this case, represent the xor
2562 // using an add, which is equivalent, and re-apply the zext.
2563 APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize);
2564 if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() &&
2565 Trunc.isSignBit())
2566 return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)),
2567 UTy);
Dan Gohmane49ae432009-06-17 01:22:39 +00002568 }
Dan Gohman3996f472008-06-22 19:56:46 +00002569 }
2570 break;
2571
2572 case Instruction::Shl:
2573 // Turn shift left of a constant amount into a multiply.
2574 if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
2575 uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
2576 Constant *X = ConstantInt::get(
2577 APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002578 return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X));
Dan Gohman3996f472008-06-22 19:56:46 +00002579 }
2580 break;
2581
Nick Lewycky7fd27892008-07-07 06:15:49 +00002582 case Instruction::LShr:
Nick Lewycky35b56022009-01-13 09:18:58 +00002583 // Turn logical shift right of a constant into a unsigned divide.
Nick Lewycky7fd27892008-07-07 06:15:49 +00002584 if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
2585 uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
2586 Constant *X = ConstantInt::get(
2587 APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002588 return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X));
Nick Lewycky7fd27892008-07-07 06:15:49 +00002589 }
2590 break;
2591
Dan Gohman53bf64a2009-04-21 02:26:00 +00002592 case Instruction::AShr:
2593 // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression.
2594 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1)))
2595 if (Instruction *L = dyn_cast<Instruction>(U->getOperand(0)))
2596 if (L->getOpcode() == Instruction::Shl &&
2597 L->getOperand(1) == U->getOperand(1)) {
Dan Gohman91ae1e72009-04-25 17:05:40 +00002598 unsigned BitWidth = getTypeSizeInBits(U->getType());
2599 uint64_t Amt = BitWidth - CI->getZExtValue();
2600 if (Amt == BitWidth)
2601 return getSCEV(L->getOperand(0)); // shift by zero --> noop
2602 if (Amt > BitWidth)
2603 return getIntegerSCEV(0, U->getType()); // value is undefined
Dan Gohman53bf64a2009-04-21 02:26:00 +00002604 return
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002605 getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)),
Dan Gohman91ae1e72009-04-25 17:05:40 +00002606 IntegerType::get(Amt)),
Dan Gohman53bf64a2009-04-21 02:26:00 +00002607 U->getType());
2608 }
2609 break;
2610
Dan Gohman3996f472008-06-22 19:56:46 +00002611 case Instruction::Trunc:
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002612 return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman3996f472008-06-22 19:56:46 +00002613
2614 case Instruction::ZExt:
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002615 return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman3996f472008-06-22 19:56:46 +00002616
2617 case Instruction::SExt:
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002618 return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman3996f472008-06-22 19:56:46 +00002619
2620 case Instruction::BitCast:
2621 // BitCasts are no-op casts so we just eliminate the cast.
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002622 if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType()))
Dan Gohman3996f472008-06-22 19:56:46 +00002623 return getSCEV(U->getOperand(0));
2624 break;
2625
Dan Gohman01c2ee72009-04-16 03:18:22 +00002626 case Instruction::IntToPtr:
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002627 if (!TD) break; // Without TD we can't analyze pointers.
Dan Gohman01c2ee72009-04-16 03:18:22 +00002628 return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)),
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002629 TD->getIntPtrType());
Dan Gohman01c2ee72009-04-16 03:18:22 +00002630
2631 case Instruction::PtrToInt:
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002632 if (!TD) break; // Without TD we can't analyze pointers.
Dan Gohman01c2ee72009-04-16 03:18:22 +00002633 return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)),
2634 U->getType());
2635
Dan Gohman509cf4d2009-05-08 20:26:55 +00002636 case Instruction::GetElementPtr:
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002637 if (!TD) break; // Without TD we can't analyze pointers.
Dan Gohmanca5a39e2009-05-08 20:58:38 +00002638 return createNodeForGEP(U);
Dan Gohman01c2ee72009-04-16 03:18:22 +00002639
Dan Gohman3996f472008-06-22 19:56:46 +00002640 case Instruction::PHI:
2641 return createNodeForPHI(cast<PHINode>(U));
2642
2643 case Instruction::Select:
2644 // This could be a smax or umax that was lowered earlier.
2645 // Try to recover it.
2646 if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) {
2647 Value *LHS = ICI->getOperand(0);
2648 Value *RHS = ICI->getOperand(1);
2649 switch (ICI->getPredicate()) {
2650 case ICmpInst::ICMP_SLT:
2651 case ICmpInst::ICMP_SLE:
2652 std::swap(LHS, RHS);
2653 // fall through
2654 case ICmpInst::ICMP_SGT:
2655 case ICmpInst::ICMP_SGE:
2656 if (LHS == U->getOperand(1) && RHS == U->getOperand(2))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002657 return getSMaxExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman3996f472008-06-22 19:56:46 +00002658 else if (LHS == U->getOperand(2) && RHS == U->getOperand(1))
Dan Gohmand01fff82009-06-22 03:18:45 +00002659 return getSMinExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman3996f472008-06-22 19:56:46 +00002660 break;
2661 case ICmpInst::ICMP_ULT:
2662 case ICmpInst::ICMP_ULE:
2663 std::swap(LHS, RHS);
2664 // fall through
2665 case ICmpInst::ICMP_UGT:
2666 case ICmpInst::ICMP_UGE:
2667 if (LHS == U->getOperand(1) && RHS == U->getOperand(2))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002668 return getUMaxExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman3996f472008-06-22 19:56:46 +00002669 else if (LHS == U->getOperand(2) && RHS == U->getOperand(1))
Dan Gohmand01fff82009-06-22 03:18:45 +00002670 return getUMinExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman3996f472008-06-22 19:56:46 +00002671 break;
Dan Gohmanf27dc692009-06-18 20:21:07 +00002672 case ICmpInst::ICMP_NE:
2673 // n != 0 ? n : 1 -> umax(n, 1)
2674 if (LHS == U->getOperand(1) &&
2675 isa<ConstantInt>(U->getOperand(2)) &&
2676 cast<ConstantInt>(U->getOperand(2))->isOne() &&
2677 isa<ConstantInt>(RHS) &&
2678 cast<ConstantInt>(RHS)->isZero())
2679 return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(2)));
2680 break;
2681 case ICmpInst::ICMP_EQ:
2682 // n == 0 ? 1 : n -> umax(n, 1)
2683 if (LHS == U->getOperand(2) &&
2684 isa<ConstantInt>(U->getOperand(1)) &&
2685 cast<ConstantInt>(U->getOperand(1))->isOne() &&
2686 isa<ConstantInt>(RHS) &&
2687 cast<ConstantInt>(RHS)->isZero())
2688 return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(1)));
2689 break;
Dan Gohman3996f472008-06-22 19:56:46 +00002690 default:
2691 break;
2692 }
2693 }
2694
2695 default: // We cannot analyze this expression.
2696 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002697 }
2698
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002699 return getUnknown(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002700}
2701
2702
2703
2704//===----------------------------------------------------------------------===//
2705// Iteration Count Computation Code
2706//
2707
Dan Gohman76d5a0d2009-02-24 18:55:53 +00002708/// getBackedgeTakenCount - If the specified loop has a predictable
2709/// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
2710/// object. The backedge-taken count is the number of times the loop header
2711/// will be branched to from within the loop. This is one less than the
2712/// trip count of the loop, since it doesn't count the first iteration,
2713/// when the header is branched to from outside the loop.
2714///
2715/// Note that it is not valid to call this method on a loop without a
2716/// loop-invariant backedge-taken count (see
2717/// hasLoopInvariantBackedgeTakenCount).
2718///
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002719SCEVHandle ScalarEvolution::getBackedgeTakenCount(const Loop *L) {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00002720 return getBackedgeTakenInfo(L).Exact;
2721}
2722
2723/// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except
2724/// return the least SCEV value that is known never to be less than the
2725/// actual backedge taken count.
2726SCEVHandle ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) {
2727 return getBackedgeTakenInfo(L).Max;
2728}
2729
2730const ScalarEvolution::BackedgeTakenInfo &
2731ScalarEvolution::getBackedgeTakenInfo(const Loop *L) {
Dan Gohmana9dba962009-04-27 20:16:15 +00002732 // Initially insert a CouldNotCompute for this loop. If the insertion
2733 // succeeds, procede to actually compute a backedge-taken count and
2734 // update the value. The temporary CouldNotCompute value tells SCEV
2735 // code elsewhere that it shouldn't attempt to request a new
2736 // backedge-taken count, which could result in infinite recursion.
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00002737 std::pair<std::map<const Loop*, BackedgeTakenInfo>::iterator, bool> Pair =
Dan Gohmana9dba962009-04-27 20:16:15 +00002738 BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute()));
2739 if (Pair.second) {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00002740 BackedgeTakenInfo ItCount = ComputeBackedgeTakenCount(L);
Dan Gohman0c850912009-06-06 14:37:11 +00002741 if (ItCount.Exact != CouldNotCompute) {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00002742 assert(ItCount.Exact->isLoopInvariant(L) &&
2743 ItCount.Max->isLoopInvariant(L) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002744 "Computed trip count isn't loop invariant for loop!");
2745 ++NumTripCountsComputed;
Dan Gohmana9dba962009-04-27 20:16:15 +00002746
Dan Gohmana9dba962009-04-27 20:16:15 +00002747 // Update the value in the map.
2748 Pair.first->second = ItCount;
Dan Gohman8e8b5232009-06-22 00:31:57 +00002749 } else {
2750 if (ItCount.Max != CouldNotCompute)
2751 // Update the value in the map.
2752 Pair.first->second = ItCount;
2753 if (isa<PHINode>(L->getHeader()->begin()))
2754 // Only count loops that have phi nodes as not being computable.
2755 ++NumTripCountsNotComputed;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002756 }
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00002757
2758 // Now that we know more about the trip count for this loop, forget any
2759 // existing SCEV values for PHI nodes in this loop since they are only
2760 // conservative estimates made without the benefit
2761 // of trip count information.
2762 if (ItCount.hasAnyInfo())
Dan Gohman94623022009-05-02 17:43:35 +00002763 forgetLoopPHIs(L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002764 }
Dan Gohmana9dba962009-04-27 20:16:15 +00002765 return Pair.first->second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002766}
2767
Dan Gohman76d5a0d2009-02-24 18:55:53 +00002768/// forgetLoopBackedgeTakenCount - This method should be called by the
Dan Gohmanf3a060a2009-02-17 20:49:49 +00002769/// client when it has changed a loop in a way that may effect
Dan Gohman76d5a0d2009-02-24 18:55:53 +00002770/// ScalarEvolution's ability to compute a trip count, or if the loop
2771/// is deleted.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002772void ScalarEvolution::forgetLoopBackedgeTakenCount(const Loop *L) {
Dan Gohman76d5a0d2009-02-24 18:55:53 +00002773 BackedgeTakenCounts.erase(L);
Dan Gohman94623022009-05-02 17:43:35 +00002774 forgetLoopPHIs(L);
2775}
2776
2777/// forgetLoopPHIs - Delete the memoized SCEVs associated with the
2778/// PHI nodes in the given loop. This is used when the trip count of
2779/// the loop may have changed.
2780void ScalarEvolution::forgetLoopPHIs(const Loop *L) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00002781 BasicBlock *Header = L->getHeader();
2782
Dan Gohman9fd4a002009-05-12 01:27:58 +00002783 // Push all Loop-header PHIs onto the Worklist stack, except those
2784 // that are presently represented via a SCEVUnknown. SCEVUnknown for
2785 // a PHI either means that it has an unrecognized structure, or it's
2786 // a PHI that's in the progress of being computed by createNodeForPHI.
2787 // In the former case, additional loop trip count information isn't
2788 // going to change anything. In the later case, createNodeForPHI will
2789 // perform the necessary updates on its own when it gets to that point.
Dan Gohmanbff6b582009-05-04 22:30:44 +00002790 SmallVector<Instruction *, 16> Worklist;
2791 for (BasicBlock::iterator I = Header->begin();
Dan Gohman9fd4a002009-05-12 01:27:58 +00002792 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
2793 std::map<SCEVCallbackVH, SCEVHandle>::iterator It = Scalars.find((Value*)I);
2794 if (It != Scalars.end() && !isa<SCEVUnknown>(It->second))
2795 Worklist.push_back(PN);
2796 }
Dan Gohmanbff6b582009-05-04 22:30:44 +00002797
2798 while (!Worklist.empty()) {
2799 Instruction *I = Worklist.pop_back_val();
2800 if (Scalars.erase(I))
2801 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
2802 UI != UE; ++UI)
2803 Worklist.push_back(cast<Instruction>(UI));
2804 }
Dan Gohmanf3a060a2009-02-17 20:49:49 +00002805}
2806
Dan Gohman76d5a0d2009-02-24 18:55:53 +00002807/// ComputeBackedgeTakenCount - Compute the number of times the backedge
2808/// of the specified loop will execute.
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00002809ScalarEvolution::BackedgeTakenInfo
2810ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) {
Dan Gohman8e8b5232009-06-22 00:31:57 +00002811 SmallVector<BasicBlock*, 8> ExitingBlocks;
2812 L->getExitingBlocks(ExitingBlocks);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002813
Dan Gohman8e8b5232009-06-22 00:31:57 +00002814 // Examine all exits and pick the most conservative values.
2815 SCEVHandle BECount = CouldNotCompute;
2816 SCEVHandle MaxBECount = CouldNotCompute;
2817 bool CouldNotComputeBECount = false;
2818 bool CouldNotComputeMaxBECount = false;
2819 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
2820 BackedgeTakenInfo NewBTI =
2821 ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002822
Dan Gohman8e8b5232009-06-22 00:31:57 +00002823 if (NewBTI.Exact == CouldNotCompute) {
2824 // We couldn't compute an exact value for this exit, so
2825 // we don't be able to compute an exact value for the loop.
2826 CouldNotComputeBECount = true;
2827 BECount = CouldNotCompute;
2828 } else if (!CouldNotComputeBECount) {
2829 if (BECount == CouldNotCompute)
2830 BECount = NewBTI.Exact;
2831 else {
2832 // TODO: More analysis could be done here. For example, a
2833 // loop with a short-circuiting && operator has an exact count
2834 // of the min of both sides.
2835 CouldNotComputeBECount = true;
2836 BECount = CouldNotCompute;
2837 }
2838 }
2839 if (NewBTI.Max == CouldNotCompute) {
2840 // We couldn't compute an maximum value for this exit, so
2841 // we don't be able to compute an maximum value for the loop.
2842 CouldNotComputeMaxBECount = true;
2843 MaxBECount = CouldNotCompute;
2844 } else if (!CouldNotComputeMaxBECount) {
2845 if (MaxBECount == CouldNotCompute)
2846 MaxBECount = NewBTI.Max;
2847 else
2848 MaxBECount = getUMaxFromMismatchedTypes(MaxBECount, NewBTI.Max);
2849 }
2850 }
2851
2852 return BackedgeTakenInfo(BECount, MaxBECount);
2853}
2854
2855/// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge
2856/// of the specified loop will execute if it exits via the specified block.
2857ScalarEvolution::BackedgeTakenInfo
2858ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L,
2859 BasicBlock *ExitingBlock) {
2860
2861 // Okay, we've chosen an exiting block. See what condition causes us to
2862 // exit at this block.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002863 //
2864 // FIXME: we should be able to handle switch instructions (with a single exit)
2865 BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
Dan Gohman0c850912009-06-06 14:37:11 +00002866 if (ExitBr == 0) return CouldNotCompute;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002867 assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!");
2868
2869 // At this point, we know we have a conditional branch that determines whether
2870 // the loop is exited. However, we don't know if the branch is executed each
2871 // time through the loop. If not, then the execution count of the branch will
2872 // not be equal to the trip count of the loop.
2873 //
2874 // Currently we check for this by checking to see if the Exit branch goes to
2875 // the loop header. If so, we know it will always execute the same number of
2876 // times as the loop. We also handle the case where the exit block *is* the
Dan Gohman8e8b5232009-06-22 00:31:57 +00002877 // loop header. This is common for un-rotated loops.
2878 //
2879 // If both of those tests fail, walk up the unique predecessor chain to the
2880 // header, stopping if there is an edge that doesn't exit the loop. If the
2881 // header is reached, the execution count of the branch will be equal to the
2882 // trip count of the loop.
2883 //
2884 // More extensive analysis could be done to handle more cases here.
2885 //
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002886 if (ExitBr->getSuccessor(0) != L->getHeader() &&
2887 ExitBr->getSuccessor(1) != L->getHeader() &&
Dan Gohman8e8b5232009-06-22 00:31:57 +00002888 ExitBr->getParent() != L->getHeader()) {
2889 // The simple checks failed, try climbing the unique predecessor chain
2890 // up to the header.
2891 bool Ok = false;
2892 for (BasicBlock *BB = ExitBr->getParent(); BB; ) {
2893 BasicBlock *Pred = BB->getUniquePredecessor();
2894 if (!Pred)
2895 return CouldNotCompute;
2896 TerminatorInst *PredTerm = Pred->getTerminator();
2897 for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) {
2898 BasicBlock *PredSucc = PredTerm->getSuccessor(i);
2899 if (PredSucc == BB)
2900 continue;
2901 // If the predecessor has a successor that isn't BB and isn't
2902 // outside the loop, assume the worst.
2903 if (L->contains(PredSucc))
2904 return CouldNotCompute;
2905 }
2906 if (Pred == L->getHeader()) {
2907 Ok = true;
2908 break;
2909 }
2910 BB = Pred;
2911 }
2912 if (!Ok)
2913 return CouldNotCompute;
2914 }
2915
2916 // Procede to the next level to examine the exit condition expression.
2917 return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(),
2918 ExitBr->getSuccessor(0),
2919 ExitBr->getSuccessor(1));
2920}
2921
2922/// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the
2923/// backedge of the specified loop will execute if its exit condition
2924/// were a conditional branch of ExitCond, TBB, and FBB.
2925ScalarEvolution::BackedgeTakenInfo
2926ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L,
2927 Value *ExitCond,
2928 BasicBlock *TBB,
2929 BasicBlock *FBB) {
2930 // Check if the controlling expression for this loop is an and or or. In
2931 // such cases, an exact backedge-taken count may be infeasible, but a
2932 // maximum count may still be feasible.
2933 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) {
2934 if (BO->getOpcode() == Instruction::And) {
2935 // Recurse on the operands of the and.
2936 BackedgeTakenInfo BTI0 =
2937 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB);
2938 BackedgeTakenInfo BTI1 =
2939 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB);
2940 SCEVHandle BECount = CouldNotCompute;
2941 SCEVHandle MaxBECount = CouldNotCompute;
2942 if (L->contains(TBB)) {
2943 // Both conditions must be true for the loop to continue executing.
2944 // Choose the less conservative count.
2945 // TODO: Take the minimum of the exact counts.
2946 if (BTI0.Exact == BTI1.Exact)
2947 BECount = BTI0.Exact;
2948 // TODO: Take the minimum of the maximum counts.
2949 if (BTI0.Max == CouldNotCompute)
2950 MaxBECount = BTI1.Max;
2951 else if (BTI1.Max == CouldNotCompute)
2952 MaxBECount = BTI0.Max;
2953 else if (const SCEVConstant *C0 = dyn_cast<SCEVConstant>(BTI0.Max))
2954 if (const SCEVConstant *C1 = dyn_cast<SCEVConstant>(BTI1.Max))
2955 MaxBECount = getConstant(APIntOps::umin(C0->getValue()->getValue(),
2956 C1->getValue()->getValue()));
2957 } else {
2958 // Both conditions must be true for the loop to exit.
2959 assert(L->contains(FBB) && "Loop block has no successor in loop!");
2960 if (BTI0.Exact != CouldNotCompute && BTI1.Exact != CouldNotCompute)
2961 BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
2962 if (BTI0.Max != CouldNotCompute && BTI1.Max != CouldNotCompute)
2963 MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max);
2964 }
2965
2966 return BackedgeTakenInfo(BECount, MaxBECount);
2967 }
2968 if (BO->getOpcode() == Instruction::Or) {
2969 // Recurse on the operands of the or.
2970 BackedgeTakenInfo BTI0 =
2971 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB);
2972 BackedgeTakenInfo BTI1 =
2973 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB);
2974 SCEVHandle BECount = CouldNotCompute;
2975 SCEVHandle MaxBECount = CouldNotCompute;
2976 if (L->contains(FBB)) {
2977 // Both conditions must be false for the loop to continue executing.
2978 // Choose the less conservative count.
2979 // TODO: Take the minimum of the exact counts.
2980 if (BTI0.Exact == BTI1.Exact)
2981 BECount = BTI0.Exact;
2982 // TODO: Take the minimum of the maximum counts.
2983 if (BTI0.Max == CouldNotCompute)
2984 MaxBECount = BTI1.Max;
2985 else if (BTI1.Max == CouldNotCompute)
2986 MaxBECount = BTI0.Max;
2987 else if (const SCEVConstant *C0 = dyn_cast<SCEVConstant>(BTI0.Max))
2988 if (const SCEVConstant *C1 = dyn_cast<SCEVConstant>(BTI1.Max))
2989 MaxBECount = getConstant(APIntOps::umin(C0->getValue()->getValue(),
2990 C1->getValue()->getValue()));
2991 } else {
2992 // Both conditions must be false for the loop to exit.
2993 assert(L->contains(TBB) && "Loop block has no successor in loop!");
2994 if (BTI0.Exact != CouldNotCompute && BTI1.Exact != CouldNotCompute)
2995 BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
2996 if (BTI0.Max != CouldNotCompute && BTI1.Max != CouldNotCompute)
2997 MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max);
2998 }
2999
3000 return BackedgeTakenInfo(BECount, MaxBECount);
3001 }
3002 }
3003
3004 // With an icmp, it may be feasible to compute an exact backedge-taken count.
3005 // Procede to the next level to examine the icmp.
3006 if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond))
3007 return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003008
Eli Friedman459d7292009-05-09 12:32:42 +00003009 // If it's not an integer or pointer comparison then compute it the hard way.
Dan Gohman8e8b5232009-06-22 00:31:57 +00003010 return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB));
3011}
3012
3013/// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the
3014/// backedge of the specified loop will execute if its exit condition
3015/// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB.
3016ScalarEvolution::BackedgeTakenInfo
3017ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L,
3018 ICmpInst *ExitCond,
3019 BasicBlock *TBB,
3020 BasicBlock *FBB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003021
3022 // If the condition was exit on true, convert the condition to exit on false
3023 ICmpInst::Predicate Cond;
Dan Gohman8e8b5232009-06-22 00:31:57 +00003024 if (!L->contains(FBB))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003025 Cond = ExitCond->getPredicate();
3026 else
3027 Cond = ExitCond->getInversePredicate();
3028
3029 // Handle common loops like: for (X = "string"; *X; ++X)
3030 if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0)))
3031 if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) {
3032 SCEVHandle ItCnt =
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003033 ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond);
Dan Gohman8e8b5232009-06-22 00:31:57 +00003034 if (!isa<SCEVCouldNotCompute>(ItCnt)) {
3035 unsigned BitWidth = getTypeSizeInBits(ItCnt->getType());
3036 return BackedgeTakenInfo(ItCnt,
3037 isa<SCEVConstant>(ItCnt) ? ItCnt :
3038 getConstant(APInt::getMaxValue(BitWidth)-1));
3039 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003040 }
3041
3042 SCEVHandle LHS = getSCEV(ExitCond->getOperand(0));
3043 SCEVHandle RHS = getSCEV(ExitCond->getOperand(1));
3044
3045 // Try to evaluate any dependencies out of the loop.
Dan Gohmanaff14d62009-05-24 23:25:42 +00003046 LHS = getSCEVAtScope(LHS, L);
3047 RHS = getSCEVAtScope(RHS, L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003048
3049 // At this point, we would like to compute how many iterations of the
3050 // loop the predicate will return true for these inputs.
Dan Gohman2d96e352008-09-16 18:52:57 +00003051 if (LHS->isLoopInvariant(L) && !RHS->isLoopInvariant(L)) {
3052 // If there is a loop-invariant, force it into the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003053 std::swap(LHS, RHS);
3054 Cond = ICmpInst::getSwappedPredicate(Cond);
3055 }
3056
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003057 // If we have a comparison of a chrec against a constant, try to use value
3058 // ranges to answer this query.
Dan Gohmanc76b5452009-05-04 22:02:23 +00003059 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
3060 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003061 if (AddRec->getLoop() == L) {
Eli Friedman459d7292009-05-09 12:32:42 +00003062 // Form the constant range.
3063 ConstantRange CompRange(
3064 ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003065
Eli Friedman459d7292009-05-09 12:32:42 +00003066 SCEVHandle Ret = AddRec->getNumIterationsInRange(CompRange, *this);
3067 if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003068 }
3069
3070 switch (Cond) {
3071 case ICmpInst::ICMP_NE: { // while (X != Y)
3072 // Convert to: while (X-Y != 0)
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003073 SCEVHandle TC = HowFarToZero(getMinusSCEV(LHS, RHS), L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003074 if (!isa<SCEVCouldNotCompute>(TC)) return TC;
3075 break;
3076 }
3077 case ICmpInst::ICMP_EQ: {
3078 // Convert to: while (X-Y == 0) // while (X == Y)
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003079 SCEVHandle TC = HowFarToNonZero(getMinusSCEV(LHS, RHS), L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003080 if (!isa<SCEVCouldNotCompute>(TC)) return TC;
3081 break;
3082 }
3083 case ICmpInst::ICMP_SLT: {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00003084 BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true);
3085 if (BTI.hasAnyInfo()) return BTI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003086 break;
3087 }
3088 case ICmpInst::ICMP_SGT: {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00003089 BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS),
3090 getNotSCEV(RHS), L, true);
3091 if (BTI.hasAnyInfo()) return BTI;
Nick Lewyckyb7c28942007-08-06 19:21:00 +00003092 break;
3093 }
3094 case ICmpInst::ICMP_ULT: {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00003095 BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false);
3096 if (BTI.hasAnyInfo()) return BTI;
Nick Lewyckyb7c28942007-08-06 19:21:00 +00003097 break;
3098 }
3099 case ICmpInst::ICMP_UGT: {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00003100 BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS),
3101 getNotSCEV(RHS), L, false);
3102 if (BTI.hasAnyInfo()) return BTI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003103 break;
3104 }
3105 default:
3106#if 0
Dan Gohman13058cc2009-04-21 00:47:46 +00003107 errs() << "ComputeBackedgeTakenCount ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003108 if (ExitCond->getOperand(0)->getType()->isUnsigned())
Dan Gohman13058cc2009-04-21 00:47:46 +00003109 errs() << "[unsigned] ";
3110 errs() << *LHS << " "
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003111 << Instruction::getOpcodeName(Instruction::ICmp)
3112 << " " << *RHS << "\n";
3113#endif
3114 break;
3115 }
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003116 return
Dan Gohman8e8b5232009-06-22 00:31:57 +00003117 ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003118}
3119
3120static ConstantInt *
Dan Gohman89f85052007-10-22 18:31:58 +00003121EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C,
3122 ScalarEvolution &SE) {
3123 SCEVHandle InVal = SE.getConstant(C);
3124 SCEVHandle Val = AddRec->evaluateAtIteration(InVal, SE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003125 assert(isa<SCEVConstant>(Val) &&
3126 "Evaluation of SCEV at constant didn't fold correctly?");
3127 return cast<SCEVConstant>(Val)->getValue();
3128}
3129
3130/// GetAddressedElementFromGlobal - Given a global variable with an initializer
3131/// and a GEP expression (missing the pointer index) indexing into it, return
3132/// the addressed element of the initializer or null if the index expression is
3133/// invalid.
3134static Constant *
3135GetAddressedElementFromGlobal(GlobalVariable *GV,
3136 const std::vector<ConstantInt*> &Indices) {
3137 Constant *Init = GV->getInitializer();
3138 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
3139 uint64_t Idx = Indices[i]->getZExtValue();
3140 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) {
3141 assert(Idx < CS->getNumOperands() && "Bad struct index!");
3142 Init = cast<Constant>(CS->getOperand(Idx));
3143 } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) {
3144 if (Idx >= CA->getNumOperands()) return 0; // Bogus program
3145 Init = cast<Constant>(CA->getOperand(Idx));
3146 } else if (isa<ConstantAggregateZero>(Init)) {
3147 if (const StructType *STy = dyn_cast<StructType>(Init->getType())) {
3148 assert(Idx < STy->getNumElements() && "Bad struct index!");
3149 Init = Constant::getNullValue(STy->getElementType(Idx));
3150 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) {
3151 if (Idx >= ATy->getNumElements()) return 0; // Bogus program
3152 Init = Constant::getNullValue(ATy->getElementType());
3153 } else {
3154 assert(0 && "Unknown constant aggregate type!");
3155 }
3156 return 0;
3157 } else {
3158 return 0; // Unknown initializer type
3159 }
3160 }
3161 return Init;
3162}
3163
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003164/// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of
3165/// 'icmp op load X, cst', try to see if we can compute the backedge
3166/// execution count.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003167SCEVHandle ScalarEvolution::
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003168ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI, Constant *RHS,
3169 const Loop *L,
3170 ICmpInst::Predicate predicate) {
Dan Gohman0c850912009-06-06 14:37:11 +00003171 if (LI->isVolatile()) return CouldNotCompute;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003172
3173 // Check to see if the loaded pointer is a getelementptr of a global.
3174 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0));
Dan Gohman0c850912009-06-06 14:37:11 +00003175 if (!GEP) return CouldNotCompute;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003176
3177 // Make sure that it is really a constant global we are gepping, with an
3178 // initializer, and make sure the first IDX is really 0.
3179 GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
3180 if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
3181 GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) ||
3182 !cast<Constant>(GEP->getOperand(1))->isNullValue())
Dan Gohman0c850912009-06-06 14:37:11 +00003183 return CouldNotCompute;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003184
3185 // Okay, we allow one non-constant index into the GEP instruction.
3186 Value *VarIdx = 0;
3187 std::vector<ConstantInt*> Indexes;
3188 unsigned VarIdxNum = 0;
3189 for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i)
3190 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
3191 Indexes.push_back(CI);
3192 } else if (!isa<ConstantInt>(GEP->getOperand(i))) {
Dan Gohman0c850912009-06-06 14:37:11 +00003193 if (VarIdx) return CouldNotCompute; // Multiple non-constant idx's.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003194 VarIdx = GEP->getOperand(i);
3195 VarIdxNum = i-2;
3196 Indexes.push_back(0);
3197 }
3198
3199 // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant.
3200 // Check to see if X is a loop variant variable value now.
3201 SCEVHandle Idx = getSCEV(VarIdx);
Dan Gohmanaff14d62009-05-24 23:25:42 +00003202 Idx = getSCEVAtScope(Idx, L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003203
3204 // We can only recognize very limited forms of loop index expressions, in
3205 // particular, only affine AddRec's like {C1,+,C2}.
Dan Gohmanbff6b582009-05-04 22:30:44 +00003206 const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003207 if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) ||
3208 !isa<SCEVConstant>(IdxExpr->getOperand(0)) ||
3209 !isa<SCEVConstant>(IdxExpr->getOperand(1)))
Dan Gohman0c850912009-06-06 14:37:11 +00003210 return CouldNotCompute;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003211
3212 unsigned MaxSteps = MaxBruteForceIterations;
3213 for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
3214 ConstantInt *ItCst =
Dan Gohman8fd520a2009-06-15 22:12:54 +00003215 ConstantInt::get(cast<IntegerType>(IdxExpr->getType()), IterationNum);
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003216 ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003217
3218 // Form the GEP offset.
3219 Indexes[VarIdxNum] = Val;
3220
3221 Constant *Result = GetAddressedElementFromGlobal(GV, Indexes);
3222 if (Result == 0) break; // Cannot compute!
3223
3224 // Evaluate the condition for this iteration.
3225 Result = ConstantExpr::getICmp(predicate, Result, RHS);
3226 if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure
3227 if (cast<ConstantInt>(Result)->getValue().isMinValue()) {
3228#if 0
Dan Gohman13058cc2009-04-21 00:47:46 +00003229 errs() << "\n***\n*** Computed loop count " << *ItCst
3230 << "\n*** From global " << *GV << "*** BB: " << *L->getHeader()
3231 << "***\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003232#endif
3233 ++NumArrayLenItCounts;
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003234 return getConstant(ItCst); // Found terminating iteration!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003235 }
3236 }
Dan Gohman0c850912009-06-06 14:37:11 +00003237 return CouldNotCompute;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003238}
3239
3240
3241/// CanConstantFold - Return true if we can constant fold an instruction of the
3242/// specified type, assuming that all operands were constants.
3243static bool CanConstantFold(const Instruction *I) {
3244 if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
3245 isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I))
3246 return true;
3247
3248 if (const CallInst *CI = dyn_cast<CallInst>(I))
3249 if (const Function *F = CI->getCalledFunction())
Dan Gohmane6e001f2008-01-31 01:05:10 +00003250 return canConstantFoldCallTo(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003251 return false;
3252}
3253
3254/// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node
3255/// in the loop that V is derived from. We allow arbitrary operations along the
3256/// way, but the operands of an operation must either be constants or a value
3257/// derived from a constant PHI. If this expression does not fit with these
3258/// constraints, return null.
3259static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) {
3260 // If this is not an instruction, or if this is an instruction outside of the
3261 // loop, it can't be derived from a loop PHI.
3262 Instruction *I = dyn_cast<Instruction>(V);
3263 if (I == 0 || !L->contains(I->getParent())) return 0;
3264
Anton Korobeynikov357a27d2008-02-20 11:08:44 +00003265 if (PHINode *PN = dyn_cast<PHINode>(I)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003266 if (L->getHeader() == I->getParent())
3267 return PN;
3268 else
3269 // We don't currently keep track of the control flow needed to evaluate
3270 // PHIs, so we cannot handle PHIs inside of loops.
3271 return 0;
Anton Korobeynikov357a27d2008-02-20 11:08:44 +00003272 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003273
3274 // If we won't be able to constant fold this expression even if the operands
3275 // are constants, return early.
3276 if (!CanConstantFold(I)) return 0;
3277
3278 // Otherwise, we can evaluate this instruction if all of its operands are
3279 // constant or derived from a PHI node themselves.
3280 PHINode *PHI = 0;
3281 for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op)
3282 if (!(isa<Constant>(I->getOperand(Op)) ||
3283 isa<GlobalValue>(I->getOperand(Op)))) {
3284 PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L);
3285 if (P == 0) return 0; // Not evolving from PHI
3286 if (PHI == 0)
3287 PHI = P;
3288 else if (PHI != P)
3289 return 0; // Evolving from multiple different PHIs.
3290 }
3291
3292 // This is a expression evolving from a constant PHI!
3293 return PHI;
3294}
3295
3296/// EvaluateExpression - Given an expression that passes the
3297/// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node
3298/// in the loop has the value PHIVal. If we can't fold this expression for some
3299/// reason, return null.
3300static Constant *EvaluateExpression(Value *V, Constant *PHIVal) {
3301 if (isa<PHINode>(V)) return PHIVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003302 if (Constant *C = dyn_cast<Constant>(V)) return C;
Dan Gohman01c2ee72009-04-16 03:18:22 +00003303 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003304 Instruction *I = cast<Instruction>(V);
3305
3306 std::vector<Constant*> Operands;
3307 Operands.resize(I->getNumOperands());
3308
3309 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
3310 Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal);
3311 if (Operands[i] == 0) return 0;
3312 }
3313
Chris Lattnerd6e56912007-12-10 22:53:04 +00003314 if (const CmpInst *CI = dyn_cast<CmpInst>(I))
3315 return ConstantFoldCompareInstOperands(CI->getPredicate(),
3316 &Operands[0], Operands.size());
3317 else
3318 return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
3319 &Operands[0], Operands.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003320}
3321
3322/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
3323/// in the header of its containing loop, we know the loop executes a
3324/// constant number of times, and the PHI node is just a recurrence
3325/// involving constants, fold it.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003326Constant *ScalarEvolution::
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003327getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs, const Loop *L){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003328 std::map<PHINode*, Constant*>::iterator I =
3329 ConstantEvolutionLoopExitValue.find(PN);
3330 if (I != ConstantEvolutionLoopExitValue.end())
3331 return I->second;
3332
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003333 if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003334 return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it.
3335
3336 Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
3337
3338 // Since the loop is canonicalized, the PHI node must have two entries. One
3339 // entry must be a constant (coming in from outside of the loop), and the
3340 // second must be derived from the same PHI.
3341 bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
3342 Constant *StartCST =
3343 dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge));
3344 if (StartCST == 0)
3345 return RetVal = 0; // Must be a constant.
3346
3347 Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
3348 PHINode *PN2 = getConstantEvolvingPHI(BEValue, L);
3349 if (PN2 != PN)
3350 return RetVal = 0; // Not derived from same PHI.
3351
3352 // Execute the loop symbolically to determine the exit value.
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003353 if (BEs.getActiveBits() >= 32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003354 return RetVal = 0; // More than 2^32-1 iterations?? Not doing it!
3355
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003356 unsigned NumIterations = BEs.getZExtValue(); // must be in range
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003357 unsigned IterationNum = 0;
3358 for (Constant *PHIVal = StartCST; ; ++IterationNum) {
3359 if (IterationNum == NumIterations)
3360 return RetVal = PHIVal; // Got exit value!
3361
3362 // Compute the value of the PHI node for the next iteration.
3363 Constant *NextPHI = EvaluateExpression(BEValue, PHIVal);
3364 if (NextPHI == PHIVal)
3365 return RetVal = NextPHI; // Stopped evolving!
3366 if (NextPHI == 0)
3367 return 0; // Couldn't evaluate!
3368 PHIVal = NextPHI;
3369 }
3370}
3371
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003372/// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute a
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003373/// constant number of times (the condition evolves only from constants),
3374/// try to evaluate a few iterations of the loop until we get the exit
3375/// condition gets a value of ExitWhen (true or false). If we cannot
Dan Gohman0c850912009-06-06 14:37:11 +00003376/// evaluate the trip count of the loop, return CouldNotCompute.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003377SCEVHandle ScalarEvolution::
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003378ComputeBackedgeTakenCountExhaustively(const Loop *L, Value *Cond, bool ExitWhen) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003379 PHINode *PN = getConstantEvolvingPHI(Cond, L);
Dan Gohman0c850912009-06-06 14:37:11 +00003380 if (PN == 0) return CouldNotCompute;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003381
3382 // Since the loop is canonicalized, the PHI node must have two entries. One
3383 // entry must be a constant (coming in from outside of the loop), and the
3384 // second must be derived from the same PHI.
3385 bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
3386 Constant *StartCST =
3387 dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge));
Dan Gohman0c850912009-06-06 14:37:11 +00003388 if (StartCST == 0) return CouldNotCompute; // Must be a constant.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003389
3390 Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
3391 PHINode *PN2 = getConstantEvolvingPHI(BEValue, L);
Dan Gohman0c850912009-06-06 14:37:11 +00003392 if (PN2 != PN) return CouldNotCompute; // Not derived from same PHI.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003393
3394 // Okay, we find a PHI node that defines the trip count of this loop. Execute
3395 // the loop symbolically to determine when the condition gets a value of
3396 // "ExitWhen".
3397 unsigned IterationNum = 0;
3398 unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis.
3399 for (Constant *PHIVal = StartCST;
3400 IterationNum != MaxIterations; ++IterationNum) {
3401 ConstantInt *CondVal =
3402 dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal));
3403
3404 // Couldn't symbolically evaluate.
Dan Gohman0c850912009-06-06 14:37:11 +00003405 if (!CondVal) return CouldNotCompute;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003406
3407 if (CondVal->getValue() == uint64_t(ExitWhen)) {
3408 ConstantEvolutionLoopExitValue[PN] = PHIVal;
3409 ++NumBruteForceTripCountsComputed;
Dan Gohman8fd520a2009-06-15 22:12:54 +00003410 return getConstant(Type::Int32Ty, IterationNum);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003411 }
3412
3413 // Compute the value of the PHI node for the next iteration.
3414 Constant *NextPHI = EvaluateExpression(BEValue, PHIVal);
3415 if (NextPHI == 0 || NextPHI == PHIVal)
Dan Gohman0c850912009-06-06 14:37:11 +00003416 return CouldNotCompute; // Couldn't evaluate or not making progress...
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003417 PHIVal = NextPHI;
3418 }
3419
3420 // Too many iterations were needed to evaluate.
Dan Gohman0c850912009-06-06 14:37:11 +00003421 return CouldNotCompute;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003422}
3423
Dan Gohmandd40e9a2009-05-08 20:38:54 +00003424/// getSCEVAtScope - Return a SCEV expression handle for the specified value
3425/// at the specified scope in the program. The L value specifies a loop
3426/// nest to evaluate the expression at, where null is the top-level or a
3427/// specified loop is immediately inside of the loop.
3428///
3429/// This method can be used to compute the exit value for a variable defined
3430/// in a loop by querying what the value will hold in the parent loop.
3431///
Dan Gohmanaff14d62009-05-24 23:25:42 +00003432/// In the case that a relevant loop exit value cannot be computed, the
3433/// original value V is returned.
Dan Gohmanbff6b582009-05-04 22:30:44 +00003434SCEVHandle ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003435 // FIXME: this should be turned into a virtual method on SCEV!
3436
3437 if (isa<SCEVConstant>(V)) return V;
3438
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00003439 // If this instruction is evolved from a constant-evolving PHI, compute the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003440 // exit value from the loop without using SCEVs.
Dan Gohmanc76b5452009-05-04 22:02:23 +00003441 if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003442 if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) {
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003443 const Loop *LI = (*this->LI)[I->getParent()];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003444 if (LI && LI->getParentLoop() == L) // Looking for loop exit value.
3445 if (PHINode *PN = dyn_cast<PHINode>(I))
3446 if (PN->getParent() == LI->getHeader()) {
3447 // Okay, there is no closed form solution for the PHI node. Check
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003448 // to see if the loop that contains it has a known backedge-taken
3449 // count. If so, we may be able to force computation of the exit
3450 // value.
3451 SCEVHandle BackedgeTakenCount = getBackedgeTakenCount(LI);
Dan Gohmanc76b5452009-05-04 22:02:23 +00003452 if (const SCEVConstant *BTCC =
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003453 dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003454 // Okay, we know how many times the containing loop executes. If
3455 // this is a constant evolving PHI node, get the final value at
3456 // the specified iteration number.
3457 Constant *RV = getConstantEvolutionLoopExitValue(PN,
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003458 BTCC->getValue()->getValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003459 LI);
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003460 if (RV) return getUnknown(RV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003461 }
3462 }
3463
3464 // Okay, this is an expression that we cannot symbolically evaluate
3465 // into a SCEV. Check to see if it's possible to symbolically evaluate
3466 // the arguments into constants, and if so, try to constant propagate the
3467 // result. This is particularly useful for computing loop exit values.
3468 if (CanConstantFold(I)) {
Dan Gohmanda0071e2009-05-08 20:47:27 +00003469 // Check to see if we've folded this instruction at this loop before.
3470 std::map<const Loop *, Constant *> &Values = ValuesAtScopes[I];
3471 std::pair<std::map<const Loop *, Constant *>::iterator, bool> Pair =
3472 Values.insert(std::make_pair(L, static_cast<Constant *>(0)));
3473 if (!Pair.second)
3474 return Pair.first->second ? &*getUnknown(Pair.first->second) : V;
3475
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003476 std::vector<Constant*> Operands;
3477 Operands.reserve(I->getNumOperands());
3478 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
3479 Value *Op = I->getOperand(i);
3480 if (Constant *C = dyn_cast<Constant>(Op)) {
3481 Operands.push_back(C);
3482 } else {
Chris Lattner3fff4642007-11-23 08:46:22 +00003483 // If any of the operands is non-constant and if they are
Dan Gohman01c2ee72009-04-16 03:18:22 +00003484 // non-integer and non-pointer, don't even try to analyze them
3485 // with scev techniques.
Dan Gohman5e4eb762009-04-30 16:40:30 +00003486 if (!isSCEVable(Op->getType()))
Chris Lattner3fff4642007-11-23 08:46:22 +00003487 return V;
Dan Gohman01c2ee72009-04-16 03:18:22 +00003488
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003489 SCEVHandle OpV = getSCEVAtScope(getSCEV(Op), L);
Dan Gohmanc76b5452009-05-04 22:02:23 +00003490 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) {
Dan Gohman5e4eb762009-04-30 16:40:30 +00003491 Constant *C = SC->getValue();
3492 if (C->getType() != Op->getType())
3493 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
3494 Op->getType(),
3495 false),
3496 C, Op->getType());
3497 Operands.push_back(C);
Dan Gohmanc76b5452009-05-04 22:02:23 +00003498 } else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) {
Dan Gohman5e4eb762009-04-30 16:40:30 +00003499 if (Constant *C = dyn_cast<Constant>(SU->getValue())) {
3500 if (C->getType() != Op->getType())
3501 C =
3502 ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
3503 Op->getType(),
3504 false),
3505 C, Op->getType());
3506 Operands.push_back(C);
3507 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003508 return V;
3509 } else {
3510 return V;
3511 }
3512 }
3513 }
Chris Lattnerd6e56912007-12-10 22:53:04 +00003514
3515 Constant *C;
3516 if (const CmpInst *CI = dyn_cast<CmpInst>(I))
3517 C = ConstantFoldCompareInstOperands(CI->getPredicate(),
3518 &Operands[0], Operands.size());
3519 else
3520 C = ConstantFoldInstOperands(I->getOpcode(), I->getType(),
3521 &Operands[0], Operands.size());
Dan Gohmanda0071e2009-05-08 20:47:27 +00003522 Pair.first->second = C;
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003523 return getUnknown(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003524 }
3525 }
3526
3527 // This is some other type of SCEVUnknown, just return it.
3528 return V;
3529 }
3530
Dan Gohmanc76b5452009-05-04 22:02:23 +00003531 if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003532 // Avoid performing the look-up in the common case where the specified
3533 // expression has no loop-variant portions.
3534 for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) {
3535 SCEVHandle OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
3536 if (OpAtScope != Comm->getOperand(i)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003537 // Okay, at least one of these operands is loop variant but might be
3538 // foldable. Build a new instance of the folded commutative expression.
Dan Gohman02ff9392009-06-14 22:47:23 +00003539 SmallVector<SCEVHandle, 8> NewOps(Comm->op_begin(), Comm->op_begin()+i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003540 NewOps.push_back(OpAtScope);
3541
3542 for (++i; i != e; ++i) {
3543 OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003544 NewOps.push_back(OpAtScope);
3545 }
3546 if (isa<SCEVAddExpr>(Comm))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003547 return getAddExpr(NewOps);
Nick Lewycky711640a2007-11-25 22:41:31 +00003548 if (isa<SCEVMulExpr>(Comm))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003549 return getMulExpr(NewOps);
Nick Lewycky711640a2007-11-25 22:41:31 +00003550 if (isa<SCEVSMaxExpr>(Comm))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003551 return getSMaxExpr(NewOps);
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00003552 if (isa<SCEVUMaxExpr>(Comm))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003553 return getUMaxExpr(NewOps);
Nick Lewycky711640a2007-11-25 22:41:31 +00003554 assert(0 && "Unknown commutative SCEV type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003555 }
3556 }
3557 // If we got here, all operands are loop invariant.
3558 return Comm;
3559 }
3560
Dan Gohmanc76b5452009-05-04 22:02:23 +00003561 if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) {
Nick Lewycky35b56022009-01-13 09:18:58 +00003562 SCEVHandle LHS = getSCEVAtScope(Div->getLHS(), L);
Nick Lewycky35b56022009-01-13 09:18:58 +00003563 SCEVHandle RHS = getSCEVAtScope(Div->getRHS(), L);
Nick Lewycky35b56022009-01-13 09:18:58 +00003564 if (LHS == Div->getLHS() && RHS == Div->getRHS())
3565 return Div; // must be loop invariant
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003566 return getUDivExpr(LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003567 }
3568
3569 // If this is a loop recurrence for a loop that does not contain L, then we
3570 // are dealing with the final value computed by the loop.
Dan Gohmanc76b5452009-05-04 22:02:23 +00003571 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003572 if (!L || !AddRec->getLoop()->contains(L->getHeader())) {
3573 // To evaluate this recurrence, we need to know how many times the AddRec
3574 // loop iterates. Compute this now.
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003575 SCEVHandle BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop());
Dan Gohman0c850912009-06-06 14:37:11 +00003576 if (BackedgeTakenCount == CouldNotCompute) return AddRec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003577
Eli Friedman7489ec92008-08-04 23:49:06 +00003578 // Then, evaluate the AddRec.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003579 return AddRec->evaluateAtIteration(BackedgeTakenCount, *this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003580 }
Dan Gohmanaff14d62009-05-24 23:25:42 +00003581 return AddRec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003582 }
3583
Dan Gohmanc76b5452009-05-04 22:02:23 +00003584 if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) {
Dan Gohman78d63c82009-04-29 22:29:01 +00003585 SCEVHandle Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohman78d63c82009-04-29 22:29:01 +00003586 if (Op == Cast->getOperand())
3587 return Cast; // must be loop invariant
3588 return getZeroExtendExpr(Op, Cast->getType());
3589 }
3590
Dan Gohmanc76b5452009-05-04 22:02:23 +00003591 if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) {
Dan Gohman78d63c82009-04-29 22:29:01 +00003592 SCEVHandle Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohman78d63c82009-04-29 22:29:01 +00003593 if (Op == Cast->getOperand())
3594 return Cast; // must be loop invariant
3595 return getSignExtendExpr(Op, Cast->getType());
3596 }
3597
Dan Gohmanc76b5452009-05-04 22:02:23 +00003598 if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) {
Dan Gohman78d63c82009-04-29 22:29:01 +00003599 SCEVHandle Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohman78d63c82009-04-29 22:29:01 +00003600 if (Op == Cast->getOperand())
3601 return Cast; // must be loop invariant
3602 return getTruncateExpr(Op, Cast->getType());
3603 }
3604
3605 assert(0 && "Unknown SCEV type!");
Daniel Dunbara95d96c2009-05-18 16:43:04 +00003606 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003607}
3608
Dan Gohmandd40e9a2009-05-08 20:38:54 +00003609/// getSCEVAtScope - This is a convenience function which does
3610/// getSCEVAtScope(getSCEV(V), L).
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003611SCEVHandle ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) {
3612 return getSCEVAtScope(getSCEV(V), L);
3613}
3614
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00003615/// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the
3616/// following equation:
3617///
3618/// A * X = B (mod N)
3619///
3620/// where N = 2^BW and BW is the common bit width of A and B. The signedness of
3621/// A and B isn't important.
3622///
3623/// If the equation does not have a solution, SCEVCouldNotCompute is returned.
3624static SCEVHandle SolveLinEquationWithOverflow(const APInt &A, const APInt &B,
3625 ScalarEvolution &SE) {
3626 uint32_t BW = A.getBitWidth();
3627 assert(BW == B.getBitWidth() && "Bit widths must be the same.");
3628 assert(A != 0 && "A must be non-zero.");
3629
3630 // 1. D = gcd(A, N)
3631 //
3632 // The gcd of A and N may have only one prime factor: 2. The number of
3633 // trailing zeros in A is its multiplicity
3634 uint32_t Mult2 = A.countTrailingZeros();
3635 // D = 2^Mult2
3636
3637 // 2. Check if B is divisible by D.
3638 //
3639 // B is divisible by D if and only if the multiplicity of prime factor 2 for B
3640 // is not less than multiplicity of this prime factor for D.
3641 if (B.countTrailingZeros() < Mult2)
Dan Gohman0ad08b02009-04-18 17:58:19 +00003642 return SE.getCouldNotCompute();
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00003643
3644 // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic
3645 // modulo (N / D).
3646 //
3647 // (N / D) may need BW+1 bits in its representation. Hence, we'll use this
3648 // bit width during computations.
3649 APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D
3650 APInt Mod(BW + 1, 0);
3651 Mod.set(BW - Mult2); // Mod = N / D
3652 APInt I = AD.multiplicativeInverse(Mod);
3653
3654 // 4. Compute the minimum unsigned root of the equation:
3655 // I * (B / D) mod (N / D)
3656 APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod);
3657
3658 // The result is guaranteed to be less than 2^BW so we may truncate it to BW
3659 // bits.
3660 return SE.getConstant(Result.trunc(BW));
3661}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003662
3663/// SolveQuadraticEquation - Find the roots of the quadratic equation for the
3664/// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which
3665/// might be the same) or two SCEVCouldNotCompute objects.
3666///
3667static std::pair<SCEVHandle,SCEVHandle>
Dan Gohman89f85052007-10-22 18:31:58 +00003668SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003669 assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
Dan Gohmanbff6b582009-05-04 22:30:44 +00003670 const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
3671 const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
3672 const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003673
3674 // We currently can only solve this if the coefficients are constants.
3675 if (!LC || !MC || !NC) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00003676 const SCEV *CNC = SE.getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003677 return std::make_pair(CNC, CNC);
3678 }
3679
3680 uint32_t BitWidth = LC->getValue()->getValue().getBitWidth();
3681 const APInt &L = LC->getValue()->getValue();
3682 const APInt &M = MC->getValue()->getValue();
3683 const APInt &N = NC->getValue()->getValue();
3684 APInt Two(BitWidth, 2);
3685 APInt Four(BitWidth, 4);
3686
3687 {
3688 using namespace APIntOps;
3689 const APInt& C = L;
3690 // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C
3691 // The B coefficient is M-N/2
3692 APInt B(M);
3693 B -= sdiv(N,Two);
3694
3695 // The A coefficient is N/2
3696 APInt A(N.sdiv(Two));
3697
3698 // Compute the B^2-4ac term.
3699 APInt SqrtTerm(B);
3700 SqrtTerm *= B;
3701 SqrtTerm -= Four * (A * C);
3702
3703 // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest
3704 // integer value or else APInt::sqrt() will assert.
3705 APInt SqrtVal(SqrtTerm.sqrt());
3706
3707 // Compute the two solutions for the quadratic formula.
3708 // The divisions must be performed as signed divisions.
3709 APInt NegB(-B);
3710 APInt TwoA( A << 1 );
Nick Lewycky35776692008-11-03 02:43:49 +00003711 if (TwoA.isMinValue()) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00003712 const SCEV *CNC = SE.getCouldNotCompute();
Nick Lewycky35776692008-11-03 02:43:49 +00003713 return std::make_pair(CNC, CNC);
3714 }
3715
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003716 ConstantInt *Solution1 = ConstantInt::get((NegB + SqrtVal).sdiv(TwoA));
3717 ConstantInt *Solution2 = ConstantInt::get((NegB - SqrtVal).sdiv(TwoA));
3718
Dan Gohman89f85052007-10-22 18:31:58 +00003719 return std::make_pair(SE.getConstant(Solution1),
3720 SE.getConstant(Solution2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003721 } // end APIntOps namespace
3722}
3723
3724/// HowFarToZero - Return the number of times a backedge comparing the specified
Dan Gohman0c850912009-06-06 14:37:11 +00003725/// value to zero will execute. If not computable, return CouldNotCompute.
Dan Gohmanbff6b582009-05-04 22:30:44 +00003726SCEVHandle ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003727 // If the value is a constant
Dan Gohmanc76b5452009-05-04 22:02:23 +00003728 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003729 // If the value is already zero, the branch will execute zero times.
3730 if (C->getValue()->isZero()) return C;
Dan Gohman0c850912009-06-06 14:37:11 +00003731 return CouldNotCompute; // Otherwise it will loop infinitely.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003732 }
3733
Dan Gohmanbff6b582009-05-04 22:30:44 +00003734 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003735 if (!AddRec || AddRec->getLoop() != L)
Dan Gohman0c850912009-06-06 14:37:11 +00003736 return CouldNotCompute;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003737
3738 if (AddRec->isAffine()) {
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00003739 // If this is an affine expression, the execution count of this branch is
3740 // the minimum unsigned root of the following equation:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003741 //
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00003742 // Start + Step*N = 0 (mod 2^BW)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003743 //
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00003744 // equivalent to:
3745 //
3746 // Step*N = -Start (mod 2^BW)
3747 //
3748 // where BW is the common bit width of Start and Step.
3749
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003750 // Get the initial value for the loop.
3751 SCEVHandle Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop());
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00003752 SCEVHandle Step = getSCEVAtScope(AddRec->getOperand(1), L->getParentLoop());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003753
Dan Gohmanc76b5452009-05-04 22:02:23 +00003754 if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) {
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00003755 // For now we handle only constant steps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003756
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00003757 // First, handle unitary steps.
3758 if (StepC->getValue()->equalsInt(1)) // 1*N = -Start (mod 2^BW), so:
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003759 return getNegativeSCEV(Start); // N = -Start (as unsigned)
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00003760 if (StepC->getValue()->isAllOnesValue()) // -1*N = -Start (mod 2^BW), so:
3761 return Start; // N = Start (as unsigned)
3762
3763 // Then, try to solve the above equation provided that Start is constant.
Dan Gohmanc76b5452009-05-04 22:02:23 +00003764 if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start))
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00003765 return SolveLinEquationWithOverflow(StepC->getValue()->getValue(),
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003766 -StartC->getValue()->getValue(),
3767 *this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003768 }
3769 } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) {
3770 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
3771 // the quadratic equation to solve it.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003772 std::pair<SCEVHandle,SCEVHandle> Roots = SolveQuadraticEquation(AddRec,
3773 *this);
Dan Gohmanbff6b582009-05-04 22:30:44 +00003774 const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
3775 const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003776 if (R1) {
3777#if 0
Dan Gohman13058cc2009-04-21 00:47:46 +00003778 errs() << "HFTZ: " << *V << " - sol#1: " << *R1
3779 << " sol#2: " << *R2 << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003780#endif
3781 // Pick the smallest positive root value.
3782 if (ConstantInt *CB =
3783 dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT,
3784 R1->getValue(), R2->getValue()))) {
3785 if (CB->getZExtValue() == false)
3786 std::swap(R1, R2); // R1 is the minimum root now.
3787
3788 // We can only use this value if the chrec ends up with an exact zero
3789 // value at this index. When solving for "X*X != 5", for example, we
3790 // should not accept a root of 2.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003791 SCEVHandle Val = AddRec->evaluateAtIteration(R1, *this);
Dan Gohman7b560c42008-06-18 16:23:07 +00003792 if (Val->isZero())
3793 return R1; // We found a quadratic root!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003794 }
3795 }
3796 }
3797
Dan Gohman0c850912009-06-06 14:37:11 +00003798 return CouldNotCompute;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003799}
3800
3801/// HowFarToNonZero - Return the number of times a backedge checking the
3802/// specified value for nonzero will execute. If not computable, return
Dan Gohman0c850912009-06-06 14:37:11 +00003803/// CouldNotCompute
Dan Gohmanbff6b582009-05-04 22:30:44 +00003804SCEVHandle ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003805 // Loops that look like: while (X == 0) are very strange indeed. We don't
3806 // handle them yet except for the trivial case. This could be expanded in the
3807 // future as needed.
3808
3809 // If the value is a constant, check to see if it is known to be non-zero
3810 // already. If so, the backedge will execute zero times.
Dan Gohmanc76b5452009-05-04 22:02:23 +00003811 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
Nick Lewyckyf6805182008-02-21 09:14:53 +00003812 if (!C->getValue()->isNullValue())
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003813 return getIntegerSCEV(0, C->getType());
Dan Gohman0c850912009-06-06 14:37:11 +00003814 return CouldNotCompute; // Otherwise it will loop infinitely.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003815 }
3816
3817 // We could implement others, but I really doubt anyone writes loops like
3818 // this, and if they did, they would already be constant folded.
Dan Gohman0c850912009-06-06 14:37:11 +00003819 return CouldNotCompute;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003820}
3821
Dan Gohmanab157b22009-05-18 15:36:09 +00003822/// getLoopPredecessor - If the given loop's header has exactly one unique
3823/// predecessor outside the loop, return it. Otherwise return null.
3824///
3825BasicBlock *ScalarEvolution::getLoopPredecessor(const Loop *L) {
3826 BasicBlock *Header = L->getHeader();
3827 BasicBlock *Pred = 0;
3828 for (pred_iterator PI = pred_begin(Header), E = pred_end(Header);
3829 PI != E; ++PI)
3830 if (!L->contains(*PI)) {
3831 if (Pred && Pred != *PI) return 0; // Multiple predecessors.
3832 Pred = *PI;
3833 }
3834 return Pred;
3835}
3836
Dan Gohman1cddf972008-09-15 22:18:04 +00003837/// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
3838/// (which may not be an immediate predecessor) which has exactly one
3839/// successor from which BB is reachable, or null if no such block is
3840/// found.
3841///
3842BasicBlock *
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003843ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
Dan Gohman1116ea72009-04-30 20:48:53 +00003844 // If the block has a unique predecessor, then there is no path from the
3845 // predecessor to the block that does not go through the direct edge
3846 // from the predecessor to the block.
Dan Gohman1cddf972008-09-15 22:18:04 +00003847 if (BasicBlock *Pred = BB->getSinglePredecessor())
3848 return Pred;
3849
3850 // A loop's header is defined to be a block that dominates the loop.
Dan Gohmanab157b22009-05-18 15:36:09 +00003851 // If the header has a unique predecessor outside the loop, it must be
3852 // a block that has exactly one successor that can reach the loop.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003853 if (Loop *L = LI->getLoopFor(BB))
Dan Gohmanab157b22009-05-18 15:36:09 +00003854 return getLoopPredecessor(L);
Dan Gohman1cddf972008-09-15 22:18:04 +00003855
3856 return 0;
3857}
3858
Dan Gohmanbc1e3472009-06-20 00:35:32 +00003859/// HasSameValue - SCEV structural equivalence is usually sufficient for
3860/// testing whether two expressions are equal, however for the purposes of
3861/// looking for a condition guarding a loop, it can be useful to be a little
3862/// more general, since a front-end may have replicated the controlling
3863/// expression.
3864///
3865static bool HasSameValue(const SCEVHandle &A, const SCEVHandle &B) {
3866 // Quick check to see if they are the same SCEV.
3867 if (A == B) return true;
3868
3869 // Otherwise, if they're both SCEVUnknown, it's possible that they hold
3870 // two different instructions with the same value. Check for this case.
3871 if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A))
3872 if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B))
3873 if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue()))
3874 if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue()))
3875 if (AI->isIdenticalTo(BI))
3876 return true;
3877
3878 // Otherwise assume they may have a different value.
3879 return false;
3880}
3881
Dan Gohmancacd2012009-02-12 22:19:27 +00003882/// isLoopGuardedByCond - Test whether entry to the loop is protected by
Dan Gohman1116ea72009-04-30 20:48:53 +00003883/// a conditional between LHS and RHS. This is used to help avoid max
3884/// expressions in loop trip counts.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003885bool ScalarEvolution::isLoopGuardedByCond(const Loop *L,
Dan Gohman1116ea72009-04-30 20:48:53 +00003886 ICmpInst::Predicate Pred,
Dan Gohmanbff6b582009-05-04 22:30:44 +00003887 const SCEV *LHS, const SCEV *RHS) {
Dan Gohman8b938182009-05-18 16:03:58 +00003888 // Interpret a null as meaning no loop, where there is obviously no guard
3889 // (interprocedural conditions notwithstanding).
3890 if (!L) return false;
3891
Dan Gohmanab157b22009-05-18 15:36:09 +00003892 BasicBlock *Predecessor = getLoopPredecessor(L);
3893 BasicBlock *PredecessorDest = L->getHeader();
Nick Lewycky1b020bf2008-07-12 07:41:32 +00003894
Dan Gohmanab157b22009-05-18 15:36:09 +00003895 // Starting at the loop predecessor, climb up the predecessor chain, as long
3896 // as there are predecessors that can be found that have unique successors
Dan Gohman1cddf972008-09-15 22:18:04 +00003897 // leading to the original header.
Dan Gohmanab157b22009-05-18 15:36:09 +00003898 for (; Predecessor;
3899 PredecessorDest = Predecessor,
3900 Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) {
Dan Gohmanab678fb2008-08-12 20:17:31 +00003901
3902 BranchInst *LoopEntryPredicate =
Dan Gohmanab157b22009-05-18 15:36:09 +00003903 dyn_cast<BranchInst>(Predecessor->getTerminator());
Dan Gohmanab678fb2008-08-12 20:17:31 +00003904 if (!LoopEntryPredicate ||
3905 LoopEntryPredicate->isUnconditional())
3906 continue;
3907
3908 ICmpInst *ICI = dyn_cast<ICmpInst>(LoopEntryPredicate->getCondition());
3909 if (!ICI) continue;
3910
3911 // Now that we found a conditional branch that dominates the loop, check to
3912 // see if it is the comparison we are looking for.
3913 Value *PreCondLHS = ICI->getOperand(0);
3914 Value *PreCondRHS = ICI->getOperand(1);
3915 ICmpInst::Predicate Cond;
Dan Gohmanab157b22009-05-18 15:36:09 +00003916 if (LoopEntryPredicate->getSuccessor(0) == PredecessorDest)
Dan Gohmanab678fb2008-08-12 20:17:31 +00003917 Cond = ICI->getPredicate();
3918 else
3919 Cond = ICI->getInversePredicate();
3920
Dan Gohmancacd2012009-02-12 22:19:27 +00003921 if (Cond == Pred)
3922 ; // An exact match.
3923 else if (!ICmpInst::isTrueWhenEqual(Cond) && Pred == ICmpInst::ICMP_NE)
3924 ; // The actual condition is beyond sufficient.
3925 else
3926 // Check a few special cases.
3927 switch (Cond) {
3928 case ICmpInst::ICMP_UGT:
3929 if (Pred == ICmpInst::ICMP_ULT) {
3930 std::swap(PreCondLHS, PreCondRHS);
3931 Cond = ICmpInst::ICMP_ULT;
3932 break;
3933 }
3934 continue;
3935 case ICmpInst::ICMP_SGT:
3936 if (Pred == ICmpInst::ICMP_SLT) {
3937 std::swap(PreCondLHS, PreCondRHS);
3938 Cond = ICmpInst::ICMP_SLT;
3939 break;
3940 }
3941 continue;
3942 case ICmpInst::ICMP_NE:
3943 // Expressions like (x >u 0) are often canonicalized to (x != 0),
3944 // so check for this case by checking if the NE is comparing against
3945 // a minimum or maximum constant.
3946 if (!ICmpInst::isTrueWhenEqual(Pred))
3947 if (ConstantInt *CI = dyn_cast<ConstantInt>(PreCondRHS)) {
3948 const APInt &A = CI->getValue();
3949 switch (Pred) {
3950 case ICmpInst::ICMP_SLT:
3951 if (A.isMaxSignedValue()) break;
3952 continue;
3953 case ICmpInst::ICMP_SGT:
3954 if (A.isMinSignedValue()) break;
3955 continue;
3956 case ICmpInst::ICMP_ULT:
3957 if (A.isMaxValue()) break;
3958 continue;
3959 case ICmpInst::ICMP_UGT:
3960 if (A.isMinValue()) break;
3961 continue;
3962 default:
3963 continue;
3964 }
3965 Cond = ICmpInst::ICMP_NE;
3966 // NE is symmetric but the original comparison may not be. Swap
3967 // the operands if necessary so that they match below.
3968 if (isa<SCEVConstant>(LHS))
3969 std::swap(PreCondLHS, PreCondRHS);
3970 break;
3971 }
3972 continue;
3973 default:
3974 // We weren't able to reconcile the condition.
3975 continue;
3976 }
Dan Gohmanab678fb2008-08-12 20:17:31 +00003977
3978 if (!PreCondLHS->getType()->isInteger()) continue;
3979
3980 SCEVHandle PreCondLHSSCEV = getSCEV(PreCondLHS);
3981 SCEVHandle PreCondRHSSCEV = getSCEV(PreCondRHS);
Dan Gohmanbc1e3472009-06-20 00:35:32 +00003982 if ((HasSameValue(LHS, PreCondLHSSCEV) &&
3983 HasSameValue(RHS, PreCondRHSSCEV)) ||
3984 (HasSameValue(LHS, getNotSCEV(PreCondRHSSCEV)) &&
3985 HasSameValue(RHS, getNotSCEV(PreCondLHSSCEV))))
Dan Gohmanab678fb2008-08-12 20:17:31 +00003986 return true;
Nick Lewycky1b020bf2008-07-12 07:41:32 +00003987 }
3988
Dan Gohmanab678fb2008-08-12 20:17:31 +00003989 return false;
Nick Lewycky1b020bf2008-07-12 07:41:32 +00003990}
3991
Dan Gohmand2b62c42009-06-21 23:46:38 +00003992/// getBECount - Subtract the end and start values and divide by the step,
3993/// rounding up, to get the number of times the backedge is executed. Return
3994/// CouldNotCompute if an intermediate computation overflows.
3995SCEVHandle ScalarEvolution::getBECount(const SCEVHandle &Start,
3996 const SCEVHandle &End,
3997 const SCEVHandle &Step) {
3998 const Type *Ty = Start->getType();
3999 SCEVHandle NegOne = getIntegerSCEV(-1, Ty);
4000 SCEVHandle Diff = getMinusSCEV(End, Start);
4001 SCEVHandle RoundUp = getAddExpr(Step, NegOne);
4002
4003 // Add an adjustment to the difference between End and Start so that
4004 // the division will effectively round up.
4005 SCEVHandle Add = getAddExpr(Diff, RoundUp);
4006
4007 // Check Add for unsigned overflow.
4008 // TODO: More sophisticated things could be done here.
4009 const Type *WideTy = IntegerType::get(getTypeSizeInBits(Ty) + 1);
4010 SCEVHandle OperandExtendedAdd =
4011 getAddExpr(getZeroExtendExpr(Diff, WideTy),
4012 getZeroExtendExpr(RoundUp, WideTy));
4013 if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd)
4014 return CouldNotCompute;
4015
4016 return getUDivExpr(Add, Step);
4017}
4018
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004019/// HowManyLessThans - Return the number of times a backedge containing the
4020/// specified less-than comparison will execute. If not computable, return
Dan Gohman0c850912009-06-06 14:37:11 +00004021/// CouldNotCompute.
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004022ScalarEvolution::BackedgeTakenInfo ScalarEvolution::
Dan Gohmanbff6b582009-05-04 22:30:44 +00004023HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
4024 const Loop *L, bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004025 // Only handle: "ADDREC < LoopInvariant".
Dan Gohman0c850912009-06-06 14:37:11 +00004026 if (!RHS->isLoopInvariant(L)) return CouldNotCompute;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004027
Dan Gohmanbff6b582009-05-04 22:30:44 +00004028 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004029 if (!AddRec || AddRec->getLoop() != L)
Dan Gohman0c850912009-06-06 14:37:11 +00004030 return CouldNotCompute;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004031
4032 if (AddRec->isAffine()) {
Nick Lewycky35b56022009-01-13 09:18:58 +00004033 // FORNOW: We only support unit strides.
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004034 unsigned BitWidth = getTypeSizeInBits(AddRec->getType());
4035 SCEVHandle Step = AddRec->getStepRecurrence(*this);
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004036
4037 // TODO: handle non-constant strides.
4038 const SCEVConstant *CStep = dyn_cast<SCEVConstant>(Step);
4039 if (!CStep || CStep->isZero())
Dan Gohman0c850912009-06-06 14:37:11 +00004040 return CouldNotCompute;
Dan Gohmanf8bc8e82009-05-18 15:22:39 +00004041 if (CStep->isOne()) {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004042 // With unit stride, the iteration never steps past the limit value.
4043 } else if (CStep->getValue()->getValue().isStrictlyPositive()) {
4044 if (const SCEVConstant *CLimit = dyn_cast<SCEVConstant>(RHS)) {
4045 // Test whether a positive iteration iteration can step past the limit
4046 // value and past the maximum value for its type in a single step.
4047 if (isSigned) {
4048 APInt Max = APInt::getSignedMaxValue(BitWidth);
4049 if ((Max - CStep->getValue()->getValue())
4050 .slt(CLimit->getValue()->getValue()))
Dan Gohman0c850912009-06-06 14:37:11 +00004051 return CouldNotCompute;
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004052 } else {
4053 APInt Max = APInt::getMaxValue(BitWidth);
4054 if ((Max - CStep->getValue()->getValue())
4055 .ult(CLimit->getValue()->getValue()))
Dan Gohman0c850912009-06-06 14:37:11 +00004056 return CouldNotCompute;
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004057 }
4058 } else
4059 // TODO: handle non-constant limit values below.
Dan Gohman0c850912009-06-06 14:37:11 +00004060 return CouldNotCompute;
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004061 } else
4062 // TODO: handle negative strides below.
Dan Gohman0c850912009-06-06 14:37:11 +00004063 return CouldNotCompute;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004064
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004065 // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant
4066 // m. So, we count the number of iterations in which {n,+,s} < m is true.
4067 // Note that we cannot simply return max(m-n,0)/s because it's not safe to
Wojciech Matyjewicz1377a542008-02-13 12:21:32 +00004068 // treat m-n as signed nor unsigned due to overflow possibility.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004069
Wojciech Matyjewiczebc77b12008-02-13 11:51:34 +00004070 // First, we get the value of the LHS in the first iteration: n
4071 SCEVHandle Start = AddRec->getOperand(0);
4072
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004073 // Determine the minimum constant start value.
4074 SCEVHandle MinStart = isa<SCEVConstant>(Start) ? Start :
4075 getConstant(isSigned ? APInt::getSignedMinValue(BitWidth) :
4076 APInt::getMinValue(BitWidth));
Wojciech Matyjewiczebc77b12008-02-13 11:51:34 +00004077
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004078 // If we know that the condition is true in order to enter the loop,
4079 // then we know that it will run exactly (m-n)/s times. Otherwise, we
Dan Gohmanc8a29272009-05-24 23:45:28 +00004080 // only know that it will execute (max(m,n)-n)/s times. In both cases,
4081 // the division must round up.
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004082 SCEVHandle End = RHS;
4083 if (!isLoopGuardedByCond(L,
4084 isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
4085 getMinusSCEV(Start, Step), RHS))
4086 End = isSigned ? getSMaxExpr(RHS, Start)
4087 : getUMaxExpr(RHS, Start);
4088
4089 // Determine the maximum constant end value.
Dan Gohman92369c32009-06-20 00:32:22 +00004090 SCEVHandle MaxEnd =
4091 isa<SCEVConstant>(End) ? End :
4092 getConstant(isSigned ? APInt::getSignedMaxValue(BitWidth)
4093 .ashr(GetMinSignBits(End) - 1) :
4094 APInt::getMaxValue(BitWidth)
4095 .lshr(GetMinLeadingZeros(End)));
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004096
4097 // Finally, we subtract these two values and divide, rounding up, to get
4098 // the number of times the backedge is executed.
Dan Gohmand2b62c42009-06-21 23:46:38 +00004099 SCEVHandle BECount = getBECount(Start, End, Step);
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004100
4101 // The maximum backedge count is similar, except using the minimum start
4102 // value and the maximum end value.
Dan Gohmand2b62c42009-06-21 23:46:38 +00004103 SCEVHandle MaxBECount = getBECount(MinStart, MaxEnd, Step);;
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004104
4105 return BackedgeTakenInfo(BECount, MaxBECount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004106 }
4107
Dan Gohman0c850912009-06-06 14:37:11 +00004108 return CouldNotCompute;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004109}
4110
4111/// getNumIterationsInRange - Return the number of iterations of this loop that
4112/// produce values in the specified constant range. Another way of looking at
4113/// this is that it returns the first iteration number where the value is not in
4114/// the condition, thus computing the exit count. If the iteration count can't
4115/// be computed, an instance of SCEVCouldNotCompute is returned.
Dan Gohman89f85052007-10-22 18:31:58 +00004116SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
4117 ScalarEvolution &SE) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004118 if (Range.isFullSet()) // Infinite loop.
Dan Gohman0ad08b02009-04-18 17:58:19 +00004119 return SE.getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004120
4121 // If the start is a non-zero constant, shift the range to simplify things.
Dan Gohmanc76b5452009-05-04 22:02:23 +00004122 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004123 if (!SC->getValue()->isZero()) {
Dan Gohman02ff9392009-06-14 22:47:23 +00004124 SmallVector<SCEVHandle, 4> Operands(op_begin(), op_end());
Dan Gohman89f85052007-10-22 18:31:58 +00004125 Operands[0] = SE.getIntegerSCEV(0, SC->getType());
4126 SCEVHandle Shifted = SE.getAddRecExpr(Operands, getLoop());
Dan Gohmanc76b5452009-05-04 22:02:23 +00004127 if (const SCEVAddRecExpr *ShiftedAddRec =
4128 dyn_cast<SCEVAddRecExpr>(Shifted))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004129 return ShiftedAddRec->getNumIterationsInRange(
Dan Gohman89f85052007-10-22 18:31:58 +00004130 Range.subtract(SC->getValue()->getValue()), SE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004131 // This is strange and shouldn't happen.
Dan Gohman0ad08b02009-04-18 17:58:19 +00004132 return SE.getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004133 }
4134
4135 // The only time we can solve this is when we have all constant indices.
4136 // Otherwise, we cannot determine the overflow conditions.
4137 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4138 if (!isa<SCEVConstant>(getOperand(i)))
Dan Gohman0ad08b02009-04-18 17:58:19 +00004139 return SE.getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004140
4141
4142 // Okay at this point we know that all elements of the chrec are constants and
4143 // that the start element is zero.
4144
4145 // First check to see if the range contains zero. If not, the first
4146 // iteration exits.
Dan Gohmanb98c1a32009-04-21 01:07:12 +00004147 unsigned BitWidth = SE.getTypeSizeInBits(getType());
Dan Gohman01c2ee72009-04-16 03:18:22 +00004148 if (!Range.contains(APInt(BitWidth, 0)))
Dan Gohman8fd520a2009-06-15 22:12:54 +00004149 return SE.getIntegerSCEV(0, getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004150
4151 if (isAffine()) {
4152 // If this is an affine expression then we have this situation:
4153 // Solve {0,+,A} in Range === Ax in Range
4154
4155 // We know that zero is in the range. If A is positive then we know that
4156 // the upper value of the range must be the first possible exit value.
4157 // If A is negative then the lower of the range is the last possible loop
4158 // value. Also note that we already checked for a full range.
Dan Gohman01c2ee72009-04-16 03:18:22 +00004159 APInt One(BitWidth,1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004160 APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue();
4161 APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower();
4162
4163 // The exit value should be (End+A)/A.
Nick Lewyckya0facae2007-09-27 14:12:54 +00004164 APInt ExitVal = (End + A).udiv(A);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004165 ConstantInt *ExitValue = ConstantInt::get(ExitVal);
4166
4167 // Evaluate at the exit value. If we really did fall out of the valid
4168 // range, then we computed our trip count, otherwise wrap around or other
4169 // things must have happened.
Dan Gohman89f85052007-10-22 18:31:58 +00004170 ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004171 if (Range.contains(Val->getValue()))
Dan Gohman0ad08b02009-04-18 17:58:19 +00004172 return SE.getCouldNotCompute(); // Something strange happened
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004173
4174 // Ensure that the previous value is in the range. This is a sanity check.
4175 assert(Range.contains(
4176 EvaluateConstantChrecAtConstant(this,
Dan Gohman89f85052007-10-22 18:31:58 +00004177 ConstantInt::get(ExitVal - One), SE)->getValue()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004178 "Linear scev computation is off in a bad way!");
Dan Gohman89f85052007-10-22 18:31:58 +00004179 return SE.getConstant(ExitValue);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004180 } else if (isQuadratic()) {
4181 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the
4182 // quadratic equation to solve it. To do this, we must frame our problem in
4183 // terms of figuring out when zero is crossed, instead of when
4184 // Range.getUpper() is crossed.
Dan Gohman02ff9392009-06-14 22:47:23 +00004185 SmallVector<SCEVHandle, 4> NewOps(op_begin(), op_end());
Dan Gohman89f85052007-10-22 18:31:58 +00004186 NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper()));
4187 SCEVHandle NewAddRec = SE.getAddRecExpr(NewOps, getLoop());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004188
4189 // Next, solve the constructed addrec
4190 std::pair<SCEVHandle,SCEVHandle> Roots =
Dan Gohman89f85052007-10-22 18:31:58 +00004191 SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE);
Dan Gohmanbff6b582009-05-04 22:30:44 +00004192 const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
4193 const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004194 if (R1) {
4195 // Pick the smallest positive root value.
4196 if (ConstantInt *CB =
4197 dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT,
4198 R1->getValue(), R2->getValue()))) {
4199 if (CB->getZExtValue() == false)
4200 std::swap(R1, R2); // R1 is the minimum root now.
4201
4202 // Make sure the root is not off by one. The returned iteration should
4203 // not be in the range, but the previous one should be. When solving
4204 // for "X*X < 5", for example, we should not return a root of 2.
4205 ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this,
Dan Gohman89f85052007-10-22 18:31:58 +00004206 R1->getValue(),
4207 SE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004208 if (Range.contains(R1Val->getValue())) {
4209 // The next iteration must be out of the range...
4210 ConstantInt *NextVal = ConstantInt::get(R1->getValue()->getValue()+1);
4211
Dan Gohman89f85052007-10-22 18:31:58 +00004212 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004213 if (!Range.contains(R1Val->getValue()))
Dan Gohman89f85052007-10-22 18:31:58 +00004214 return SE.getConstant(NextVal);
Dan Gohman0ad08b02009-04-18 17:58:19 +00004215 return SE.getCouldNotCompute(); // Something strange happened
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004216 }
4217
4218 // If R1 was not in the range, then it is a good return value. Make
4219 // sure that R1-1 WAS in the range though, just in case.
4220 ConstantInt *NextVal = ConstantInt::get(R1->getValue()->getValue()-1);
Dan Gohman89f85052007-10-22 18:31:58 +00004221 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004222 if (Range.contains(R1Val->getValue()))
4223 return R1;
Dan Gohman0ad08b02009-04-18 17:58:19 +00004224 return SE.getCouldNotCompute(); // Something strange happened
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004225 }
4226 }
4227 }
4228
Dan Gohman0ad08b02009-04-18 17:58:19 +00004229 return SE.getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004230}
4231
4232
4233
4234//===----------------------------------------------------------------------===//
Dan Gohmanbff6b582009-05-04 22:30:44 +00004235// SCEVCallbackVH Class Implementation
4236//===----------------------------------------------------------------------===//
4237
Dan Gohman999d14e2009-05-19 19:22:47 +00004238void ScalarEvolution::SCEVCallbackVH::deleted() {
Dan Gohmanbff6b582009-05-04 22:30:44 +00004239 assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!");
4240 if (PHINode *PN = dyn_cast<PHINode>(getValPtr()))
4241 SE->ConstantEvolutionLoopExitValue.erase(PN);
Dan Gohmanda0071e2009-05-08 20:47:27 +00004242 if (Instruction *I = dyn_cast<Instruction>(getValPtr()))
4243 SE->ValuesAtScopes.erase(I);
Dan Gohmanbff6b582009-05-04 22:30:44 +00004244 SE->Scalars.erase(getValPtr());
4245 // this now dangles!
4246}
4247
Dan Gohman999d14e2009-05-19 19:22:47 +00004248void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00004249 assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!");
4250
4251 // Forget all the expressions associated with users of the old value,
4252 // so that future queries will recompute the expressions using the new
4253 // value.
4254 SmallVector<User *, 16> Worklist;
4255 Value *Old = getValPtr();
4256 bool DeleteOld = false;
4257 for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end();
4258 UI != UE; ++UI)
4259 Worklist.push_back(*UI);
4260 while (!Worklist.empty()) {
4261 User *U = Worklist.pop_back_val();
4262 // Deleting the Old value will cause this to dangle. Postpone
4263 // that until everything else is done.
4264 if (U == Old) {
4265 DeleteOld = true;
4266 continue;
4267 }
4268 if (PHINode *PN = dyn_cast<PHINode>(U))
4269 SE->ConstantEvolutionLoopExitValue.erase(PN);
Dan Gohmanda0071e2009-05-08 20:47:27 +00004270 if (Instruction *I = dyn_cast<Instruction>(U))
4271 SE->ValuesAtScopes.erase(I);
Dan Gohmanbff6b582009-05-04 22:30:44 +00004272 if (SE->Scalars.erase(U))
4273 for (Value::use_iterator UI = U->use_begin(), UE = U->use_end();
4274 UI != UE; ++UI)
4275 Worklist.push_back(*UI);
4276 }
4277 if (DeleteOld) {
4278 if (PHINode *PN = dyn_cast<PHINode>(Old))
4279 SE->ConstantEvolutionLoopExitValue.erase(PN);
Dan Gohmanda0071e2009-05-08 20:47:27 +00004280 if (Instruction *I = dyn_cast<Instruction>(Old))
4281 SE->ValuesAtScopes.erase(I);
Dan Gohmanbff6b582009-05-04 22:30:44 +00004282 SE->Scalars.erase(Old);
4283 // this now dangles!
4284 }
4285 // this may dangle!
4286}
4287
Dan Gohman999d14e2009-05-19 19:22:47 +00004288ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se)
Dan Gohmanbff6b582009-05-04 22:30:44 +00004289 : CallbackVH(V), SE(se) {}
4290
4291//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004292// ScalarEvolution Class Implementation
4293//===----------------------------------------------------------------------===//
4294
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004295ScalarEvolution::ScalarEvolution()
Owen Andersoncf4e2302009-06-18 22:25:12 +00004296 : FunctionPass(&ID), CouldNotCompute(new SCEVCouldNotCompute(0)) {
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004297}
4298
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004299bool ScalarEvolution::runOnFunction(Function &F) {
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004300 this->F = &F;
4301 LI = &getAnalysis<LoopInfo>();
4302 TD = getAnalysisIfAvailable<TargetData>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004303 return false;
4304}
4305
4306void ScalarEvolution::releaseMemory() {
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004307 Scalars.clear();
4308 BackedgeTakenCounts.clear();
4309 ConstantEvolutionLoopExitValue.clear();
Dan Gohmanda0071e2009-05-08 20:47:27 +00004310 ValuesAtScopes.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004311}
4312
4313void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const {
4314 AU.setPreservesAll();
4315 AU.addRequiredTransitive<LoopInfo>();
Dan Gohman01c2ee72009-04-16 03:18:22 +00004316}
4317
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004318bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) {
Dan Gohman76d5a0d2009-02-24 18:55:53 +00004319 return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004320}
4321
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004322static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004323 const Loop *L) {
4324 // Print all inner loops first
4325 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
4326 PrintLoopInfo(OS, SE, *I);
4327
Nick Lewyckye5da1912008-01-02 02:49:20 +00004328 OS << "Loop " << L->getHeader()->getName() << ": ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004329
Devang Patel02451fa2007-08-21 00:31:24 +00004330 SmallVector<BasicBlock*, 8> ExitBlocks;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004331 L->getExitBlocks(ExitBlocks);
4332 if (ExitBlocks.size() != 1)
Nick Lewyckye5da1912008-01-02 02:49:20 +00004333 OS << "<multiple exits> ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004334
Dan Gohman76d5a0d2009-02-24 18:55:53 +00004335 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
4336 OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004337 } else {
Dan Gohman76d5a0d2009-02-24 18:55:53 +00004338 OS << "Unpredictable backedge-taken count. ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004339 }
4340
Nick Lewyckye5da1912008-01-02 02:49:20 +00004341 OS << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004342}
4343
Dan Gohman13058cc2009-04-21 00:47:46 +00004344void ScalarEvolution::print(raw_ostream &OS, const Module* ) const {
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004345 // ScalarEvolution's implementaiton of the print method is to print
4346 // out SCEV values of all instructions that are interesting. Doing
4347 // this potentially causes it to create new SCEV objects though,
4348 // which technically conflicts with the const qualifier. This isn't
4349 // observable from outside the class though (the hasSCEV function
4350 // notwithstanding), so casting away the const isn't dangerous.
4351 ScalarEvolution &SE = *const_cast<ScalarEvolution*>(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004352
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004353 OS << "Classifying expressions for: " << F->getName() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004354 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Dan Gohman43d37e92009-04-30 01:30:18 +00004355 if (isSCEVable(I->getType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004356 OS << *I;
Dan Gohmanabe991f2008-09-14 17:21:12 +00004357 OS << " --> ";
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004358 SCEVHandle SV = SE.getSCEV(&*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004359 SV->print(OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004360
Dan Gohman8db598a2009-06-19 17:49:54 +00004361 const Loop *L = LI->getLoopFor((*I).getParent());
4362
4363 SCEVHandle AtUse = SE.getSCEVAtScope(SV, L);
4364 if (AtUse != SV) {
4365 OS << " --> ";
4366 AtUse->print(OS);
4367 }
4368
4369 if (L) {
Dan Gohmane5b60842009-06-18 00:37:45 +00004370 OS << "\t\t" "Exits: ";
Dan Gohman8db598a2009-06-19 17:49:54 +00004371 SCEVHandle ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop());
Dan Gohmanaff14d62009-05-24 23:25:42 +00004372 if (!ExitValue->isLoopInvariant(L)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004373 OS << "<<Unknown>>";
4374 } else {
4375 OS << *ExitValue;
4376 }
4377 }
4378
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004379 OS << "\n";
4380 }
4381
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004382 OS << "Determining loop execution counts for: " << F->getName() << "\n";
4383 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
4384 PrintLoopInfo(OS, &SE, *I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004385}
Dan Gohman13058cc2009-04-21 00:47:46 +00004386
4387void ScalarEvolution::print(std::ostream &o, const Module *M) const {
4388 raw_os_ostream OS(o);
4389 print(OS, M);
4390}