blob: 4d9a3cefd61c8b88e7cfc1603d003cbdefaecb89 [file] [log] [blame]
Chris Lattner53e677a2004-04-02 20:23:17 +00001//===- ScalarEvolution.cpp - Scalar Evolution Analysis ----------*- C++ -*-===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Chris Lattner53e677a2004-04-02 20:23:17 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
Chris Lattner53e677a2004-04-02 20:23:17 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains the implementation of the scalar evolution analysis
11// engine, which is used primarily to analyze expressions involving induction
12// variables in loops.
13//
14// There are several aspects to this library. First is the representation of
15// scalar expressions, which are represented as subclasses of the SCEV class.
16// These classes are used to represent certain types of subexpressions that we
Owen Anderson372b46c2009-06-22 21:39:50 +000017// can handle. These classes are reference counted, managed by the const SCEV*
Chris Lattner53e677a2004-04-02 20:23:17 +000018// class. We only create one SCEV of a particular shape, so pointer-comparisons
19// for equality are legal.
20//
21// One important aspect of the SCEV objects is that they are never cyclic, even
22// if there is a cycle in the dataflow for an expression (ie, a PHI node). If
23// the PHI node is one of the idioms that we can represent (e.g., a polynomial
24// recurrence) then we represent it directly as a recurrence node, otherwise we
25// represent it as a SCEVUnknown node.
26//
27// In addition to being able to represent expressions of various types, we also
28// have folders that are used to build the *canonical* representation for a
29// particular expression. These folders are capable of using a variety of
30// rewrite rules to simplify the expressions.
Misha Brukman2b37d7c2005-04-21 21:13:18 +000031//
Chris Lattner53e677a2004-04-02 20:23:17 +000032// Once the folders are defined, we can implement the more interesting
33// higher-level code, such as the code that recognizes PHI nodes of various
34// types, computes the execution count of a loop, etc.
35//
Chris Lattner53e677a2004-04-02 20:23:17 +000036// TODO: We should use these routines and value representations to implement
37// dependence analysis!
38//
39//===----------------------------------------------------------------------===//
40//
41// There are several good references for the techniques used in this analysis.
42//
43// Chains of recurrences -- a method to expedite the evaluation
44// of closed-form functions
45// Olaf Bachmann, Paul S. Wang, Eugene V. Zima
46//
47// On computational properties of chains of recurrences
48// Eugene V. Zima
49//
50// Symbolic Evaluation of Chains of Recurrences for Loop Optimization
51// Robert A. van Engelen
52//
53// Efficient Symbolic Analysis for Optimizing Compilers
54// Robert A. van Engelen
55//
56// Using the chains of recurrences algebra for data dependence testing and
57// induction variable substitution
58// MS Thesis, Johnie Birch
59//
60//===----------------------------------------------------------------------===//
61
Chris Lattner3b27d682006-12-19 22:30:33 +000062#define DEBUG_TYPE "scalar-evolution"
Chris Lattner0a7f98c2004-04-15 15:07:24 +000063#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner53e677a2004-04-02 20:23:17 +000064#include "llvm/Constants.h"
65#include "llvm/DerivedTypes.h"
Chris Lattner673e02b2004-10-12 01:49:27 +000066#include "llvm/GlobalVariable.h"
Chris Lattner53e677a2004-04-02 20:23:17 +000067#include "llvm/Instructions.h"
John Criswella1156432005-10-27 15:54:34 +000068#include "llvm/Analysis/ConstantFolding.h"
Evan Cheng5a6c1a82009-02-17 00:13:06 +000069#include "llvm/Analysis/Dominators.h"
Chris Lattner53e677a2004-04-02 20:23:17 +000070#include "llvm/Analysis/LoopInfo.h"
Dan Gohman61ffa8e2009-06-16 19:52:01 +000071#include "llvm/Analysis/ValueTracking.h"
Chris Lattner53e677a2004-04-02 20:23:17 +000072#include "llvm/Assembly/Writer.h"
Dan Gohman2d1be872009-04-16 03:18:22 +000073#include "llvm/Target/TargetData.h"
Chris Lattner95255282006-06-28 23:17:24 +000074#include "llvm/Support/CommandLine.h"
Chris Lattnerb3364092006-10-04 21:49:37 +000075#include "llvm/Support/Compiler.h"
Chris Lattner53e677a2004-04-02 20:23:17 +000076#include "llvm/Support/ConstantRange.h"
Dan Gohman2d1be872009-04-16 03:18:22 +000077#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner53e677a2004-04-02 20:23:17 +000078#include "llvm/Support/InstIterator.h"
Chris Lattner75de5ab2006-12-19 01:16:02 +000079#include "llvm/Support/MathExtras.h"
Dan Gohmanb7ef7292009-04-21 00:47:46 +000080#include "llvm/Support/raw_ostream.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000081#include "llvm/ADT/Statistic.h"
Dan Gohman2d1be872009-04-16 03:18:22 +000082#include "llvm/ADT/STLExtras.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000083#include <algorithm>
Chris Lattner53e677a2004-04-02 20:23:17 +000084using namespace llvm;
85
Chris Lattner3b27d682006-12-19 22:30:33 +000086STATISTIC(NumArrayLenItCounts,
87 "Number of trip counts computed with array length");
88STATISTIC(NumTripCountsComputed,
89 "Number of loops with predictable loop counts");
90STATISTIC(NumTripCountsNotComputed,
91 "Number of loops without predictable loop counts");
92STATISTIC(NumBruteForceTripCountsComputed,
93 "Number of loops with trip counts computed by force");
94
Dan Gohman844731a2008-05-13 00:00:25 +000095static cl::opt<unsigned>
Chris Lattner3b27d682006-12-19 22:30:33 +000096MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden,
97 cl::desc("Maximum number of iterations SCEV will "
Dan Gohman64a845e2009-06-24 04:48:43 +000098 "symbolically execute a constant "
99 "derived loop"),
Chris Lattner3b27d682006-12-19 22:30:33 +0000100 cl::init(100));
101
Dan Gohman844731a2008-05-13 00:00:25 +0000102static RegisterPass<ScalarEvolution>
103R("scalar-evolution", "Scalar Evolution Analysis", false, true);
Devang Patel19974732007-05-03 01:11:54 +0000104char ScalarEvolution::ID = 0;
Chris Lattner53e677a2004-04-02 20:23:17 +0000105
106//===----------------------------------------------------------------------===//
107// SCEV class definitions
108//===----------------------------------------------------------------------===//
109
110//===----------------------------------------------------------------------===//
111// Implementation of the SCEV class.
112//
Dan Gohmanc39f44b2009-06-30 20:13:32 +0000113
Chris Lattner53e677a2004-04-02 20:23:17 +0000114SCEV::~SCEV() {}
Dan Gohmanc39f44b2009-06-30 20:13:32 +0000115
Chris Lattner53e677a2004-04-02 20:23:17 +0000116void SCEV::dump() const {
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000117 print(errs());
118 errs() << '\n';
119}
120
121void SCEV::print(std::ostream &o) const {
122 raw_os_ostream OS(o);
123 print(OS);
Chris Lattner53e677a2004-04-02 20:23:17 +0000124}
125
Dan Gohmancfeb6a42008-06-18 16:23:07 +0000126bool SCEV::isZero() const {
127 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
128 return SC->getValue()->isZero();
129 return false;
130}
131
Dan Gohman70a1fe72009-05-18 15:22:39 +0000132bool SCEV::isOne() const {
133 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
134 return SC->getValue()->isOne();
135 return false;
136}
Chris Lattner53e677a2004-04-02 20:23:17 +0000137
Dan Gohman4d289bf2009-06-24 00:30:26 +0000138bool SCEV::isAllOnesValue() const {
139 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
140 return SC->getValue()->isAllOnesValue();
141 return false;
142}
143
Owen Anderson753ad612009-06-22 21:57:23 +0000144SCEVCouldNotCompute::SCEVCouldNotCompute() :
145 SCEV(scCouldNotCompute) {}
Chris Lattner53e677a2004-04-02 20:23:17 +0000146
Dan Gohman1c343752009-06-27 21:21:31 +0000147void SCEVCouldNotCompute::Profile(FoldingSetNodeID &ID) const {
148 assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
149}
150
Chris Lattner53e677a2004-04-02 20:23:17 +0000151bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const {
152 assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
Misha Brukmanbb2aff12004-04-05 19:00:46 +0000153 return false;
Chris Lattner53e677a2004-04-02 20:23:17 +0000154}
155
156const Type *SCEVCouldNotCompute::getType() const {
157 assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
Misha Brukmanbb2aff12004-04-05 19:00:46 +0000158 return 0;
Chris Lattner53e677a2004-04-02 20:23:17 +0000159}
160
161bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const {
162 assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
163 return false;
164}
165
Dan Gohman64a845e2009-06-24 04:48:43 +0000166const SCEV *
167SCEVCouldNotCompute::replaceSymbolicValuesWithConcrete(
168 const SCEV *Sym,
169 const SCEV *Conc,
170 ScalarEvolution &SE) const {
Chris Lattner4dc534c2005-02-13 04:37:18 +0000171 return this;
172}
173
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000174void SCEVCouldNotCompute::print(raw_ostream &OS) const {
Chris Lattner53e677a2004-04-02 20:23:17 +0000175 OS << "***COULDNOTCOMPUTE***";
176}
177
178bool SCEVCouldNotCompute::classof(const SCEV *S) {
179 return S->getSCEVType() == scCouldNotCompute;
180}
181
Owen Anderson372b46c2009-06-22 21:39:50 +0000182const SCEV* ScalarEvolution::getConstant(ConstantInt *V) {
Dan Gohman1c343752009-06-27 21:21:31 +0000183 FoldingSetNodeID ID;
184 ID.AddInteger(scConstant);
185 ID.AddPointer(V);
186 void *IP = 0;
187 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
188 SCEV *S = SCEVAllocator.Allocate<SCEVConstant>();
189 new (S) SCEVConstant(V);
190 UniqueSCEVs.InsertNode(S, IP);
191 return S;
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000192}
Chris Lattner53e677a2004-04-02 20:23:17 +0000193
Owen Anderson372b46c2009-06-22 21:39:50 +0000194const SCEV* ScalarEvolution::getConstant(const APInt& Val) {
Dan Gohman246b2562007-10-22 18:31:58 +0000195 return getConstant(ConstantInt::get(Val));
Dan Gohman9a6ae962007-07-09 15:25:17 +0000196}
197
Owen Anderson372b46c2009-06-22 21:39:50 +0000198const SCEV*
Dan Gohman6de29f82009-06-15 22:12:54 +0000199ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) {
200 return getConstant(ConstantInt::get(cast<IntegerType>(Ty), V, isSigned));
201}
202
Dan Gohman1c343752009-06-27 21:21:31 +0000203void SCEVConstant::Profile(FoldingSetNodeID &ID) const {
204 ID.AddInteger(scConstant);
205 ID.AddPointer(V);
206}
207
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000208const Type *SCEVConstant::getType() const { return V->getType(); }
Chris Lattner53e677a2004-04-02 20:23:17 +0000209
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000210void SCEVConstant::print(raw_ostream &OS) const {
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000211 WriteAsOperand(OS, V, false);
212}
Chris Lattner53e677a2004-04-02 20:23:17 +0000213
Dan Gohman84923602009-04-21 01:25:57 +0000214SCEVCastExpr::SCEVCastExpr(unsigned SCEVTy,
Owen Anderson753ad612009-06-22 21:57:23 +0000215 const SCEV* op, const Type *ty)
216 : SCEV(SCEVTy), Op(op), Ty(ty) {}
Dan Gohman84923602009-04-21 01:25:57 +0000217
Dan Gohman1c343752009-06-27 21:21:31 +0000218void SCEVCastExpr::Profile(FoldingSetNodeID &ID) const {
219 ID.AddInteger(getSCEVType());
220 ID.AddPointer(Op);
221 ID.AddPointer(Ty);
222}
223
Dan Gohman84923602009-04-21 01:25:57 +0000224bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
225 return Op->dominates(BB, DT);
226}
227
Owen Anderson753ad612009-06-22 21:57:23 +0000228SCEVTruncateExpr::SCEVTruncateExpr(const SCEV* op, const Type *ty)
229 : SCEVCastExpr(scTruncate, op, ty) {
Dan Gohman2d1be872009-04-16 03:18:22 +0000230 assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
231 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000232 "Cannot truncate non-integer value!");
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000233}
Chris Lattner53e677a2004-04-02 20:23:17 +0000234
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000235void SCEVTruncateExpr::print(raw_ostream &OS) const {
Dan Gohman36b8e532009-04-29 20:27:52 +0000236 OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000237}
238
Owen Anderson753ad612009-06-22 21:57:23 +0000239SCEVZeroExtendExpr::SCEVZeroExtendExpr(const SCEV* op, const Type *ty)
240 : SCEVCastExpr(scZeroExtend, op, ty) {
Dan Gohman2d1be872009-04-16 03:18:22 +0000241 assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
242 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000243 "Cannot zero extend non-integer value!");
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000244}
245
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000246void SCEVZeroExtendExpr::print(raw_ostream &OS) const {
Dan Gohman36b8e532009-04-29 20:27:52 +0000247 OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000248}
249
Owen Anderson753ad612009-06-22 21:57:23 +0000250SCEVSignExtendExpr::SCEVSignExtendExpr(const SCEV* op, const Type *ty)
251 : SCEVCastExpr(scSignExtend, op, ty) {
Dan Gohman2d1be872009-04-16 03:18:22 +0000252 assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
253 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohmand19534a2007-06-15 14:38:12 +0000254 "Cannot sign extend non-integer value!");
Dan Gohmand19534a2007-06-15 14:38:12 +0000255}
256
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000257void SCEVSignExtendExpr::print(raw_ostream &OS) const {
Dan Gohman36b8e532009-04-29 20:27:52 +0000258 OS << "(sext " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
Dan Gohmand19534a2007-06-15 14:38:12 +0000259}
260
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000261void SCEVCommutativeExpr::print(raw_ostream &OS) const {
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000262 assert(Operands.size() > 1 && "This plus expr shouldn't exist!");
263 const char *OpStr = getOperationStr();
264 OS << "(" << *Operands[0];
265 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
266 OS << OpStr << *Operands[i];
267 OS << ")";
268}
269
Dan Gohman64a845e2009-06-24 04:48:43 +0000270const SCEV *
271SCEVCommutativeExpr::replaceSymbolicValuesWithConcrete(
272 const SCEV *Sym,
273 const SCEV *Conc,
274 ScalarEvolution &SE) const {
Chris Lattner4dc534c2005-02-13 04:37:18 +0000275 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Owen Anderson372b46c2009-06-22 21:39:50 +0000276 const SCEV* H =
Dan Gohman246b2562007-10-22 18:31:58 +0000277 getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
Chris Lattner4dc534c2005-02-13 04:37:18 +0000278 if (H != getOperand(i)) {
Owen Anderson372b46c2009-06-22 21:39:50 +0000279 SmallVector<const SCEV*, 8> NewOps;
Chris Lattner4dc534c2005-02-13 04:37:18 +0000280 NewOps.reserve(getNumOperands());
281 for (unsigned j = 0; j != i; ++j)
282 NewOps.push_back(getOperand(j));
283 NewOps.push_back(H);
284 for (++i; i != e; ++i)
285 NewOps.push_back(getOperand(i)->
Dan Gohman246b2562007-10-22 18:31:58 +0000286 replaceSymbolicValuesWithConcrete(Sym, Conc, SE));
Chris Lattner4dc534c2005-02-13 04:37:18 +0000287
288 if (isa<SCEVAddExpr>(this))
Dan Gohman246b2562007-10-22 18:31:58 +0000289 return SE.getAddExpr(NewOps);
Chris Lattner4dc534c2005-02-13 04:37:18 +0000290 else if (isa<SCEVMulExpr>(this))
Dan Gohman246b2562007-10-22 18:31:58 +0000291 return SE.getMulExpr(NewOps);
Nick Lewyckyc54c5612007-11-25 22:41:31 +0000292 else if (isa<SCEVSMaxExpr>(this))
293 return SE.getSMaxExpr(NewOps);
Nick Lewycky3e630762008-02-20 06:48:22 +0000294 else if (isa<SCEVUMaxExpr>(this))
295 return SE.getUMaxExpr(NewOps);
Chris Lattner4dc534c2005-02-13 04:37:18 +0000296 else
297 assert(0 && "Unknown commutative expr!");
298 }
299 }
300 return this;
301}
302
Dan Gohman1c343752009-06-27 21:21:31 +0000303void SCEVNAryExpr::Profile(FoldingSetNodeID &ID) const {
304 ID.AddInteger(getSCEVType());
305 ID.AddInteger(Operands.size());
306 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
307 ID.AddPointer(Operands[i]);
308}
309
Dan Gohmanecb403a2009-05-07 14:00:19 +0000310bool SCEVNAryExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
Evan Cheng5a6c1a82009-02-17 00:13:06 +0000311 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
312 if (!getOperand(i)->dominates(BB, DT))
313 return false;
314 }
315 return true;
316}
317
Dan Gohman1c343752009-06-27 21:21:31 +0000318void SCEVUDivExpr::Profile(FoldingSetNodeID &ID) const {
319 ID.AddInteger(scUDivExpr);
320 ID.AddPointer(LHS);
321 ID.AddPointer(RHS);
322}
323
Evan Cheng5a6c1a82009-02-17 00:13:06 +0000324bool SCEVUDivExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
325 return LHS->dominates(BB, DT) && RHS->dominates(BB, DT);
326}
327
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000328void SCEVUDivExpr::print(raw_ostream &OS) const {
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000329 OS << "(" << *LHS << " /u " << *RHS << ")";
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000330}
331
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000332const Type *SCEVUDivExpr::getType() const {
Dan Gohman91bb61a2009-05-26 17:44:05 +0000333 // In most cases the types of LHS and RHS will be the same, but in some
334 // crazy cases one or the other may be a pointer. ScalarEvolution doesn't
335 // depend on the type for correctness, but handling types carefully can
336 // avoid extra casts in the SCEVExpander. The LHS is more likely to be
337 // a pointer type than the RHS, so use the RHS' type here.
338 return RHS->getType();
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000339}
340
Dan Gohman1c343752009-06-27 21:21:31 +0000341void SCEVAddRecExpr::Profile(FoldingSetNodeID &ID) const {
342 ID.AddInteger(scAddRecExpr);
343 ID.AddInteger(Operands.size());
344 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
345 ID.AddPointer(Operands[i]);
346 ID.AddPointer(L);
347}
348
Dan Gohman64a845e2009-06-24 04:48:43 +0000349const SCEV *
350SCEVAddRecExpr::replaceSymbolicValuesWithConcrete(const SCEV *Sym,
351 const SCEV *Conc,
352 ScalarEvolution &SE) const {
Chris Lattner4dc534c2005-02-13 04:37:18 +0000353 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Owen Anderson372b46c2009-06-22 21:39:50 +0000354 const SCEV* H =
Dan Gohman246b2562007-10-22 18:31:58 +0000355 getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
Chris Lattner4dc534c2005-02-13 04:37:18 +0000356 if (H != getOperand(i)) {
Owen Anderson372b46c2009-06-22 21:39:50 +0000357 SmallVector<const SCEV*, 8> NewOps;
Chris Lattner4dc534c2005-02-13 04:37:18 +0000358 NewOps.reserve(getNumOperands());
359 for (unsigned j = 0; j != i; ++j)
360 NewOps.push_back(getOperand(j));
361 NewOps.push_back(H);
362 for (++i; i != e; ++i)
363 NewOps.push_back(getOperand(i)->
Dan Gohman246b2562007-10-22 18:31:58 +0000364 replaceSymbolicValuesWithConcrete(Sym, Conc, SE));
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000365
Dan Gohman246b2562007-10-22 18:31:58 +0000366 return SE.getAddRecExpr(NewOps, L);
Chris Lattner4dc534c2005-02-13 04:37:18 +0000367 }
368 }
369 return this;
370}
371
372
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000373bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const {
Dan Gohmana3035a62009-05-20 01:01:24 +0000374 // Add recurrences are never invariant in the function-body (null loop).
Dan Gohmane890eea2009-06-26 22:17:21 +0000375 if (!QueryLoop)
376 return false;
377
378 // This recurrence is variant w.r.t. QueryLoop if QueryLoop contains L.
379 if (QueryLoop->contains(L->getHeader()))
380 return false;
381
382 // This recurrence is variant w.r.t. QueryLoop if any of its operands
383 // are variant.
384 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
385 if (!getOperand(i)->isLoopInvariant(QueryLoop))
386 return false;
387
388 // Otherwise it's loop-invariant.
389 return true;
Chris Lattner53e677a2004-04-02 20:23:17 +0000390}
391
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000392void SCEVAddRecExpr::print(raw_ostream &OS) const {
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000393 OS << "{" << *Operands[0];
394 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
395 OS << ",+," << *Operands[i];
396 OS << "}<" << L->getHeader()->getName() + ">";
397}
Chris Lattner53e677a2004-04-02 20:23:17 +0000398
Dan Gohman1c343752009-06-27 21:21:31 +0000399void SCEVUnknown::Profile(FoldingSetNodeID &ID) const {
400 ID.AddInteger(scUnknown);
401 ID.AddPointer(V);
402}
403
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000404bool SCEVUnknown::isLoopInvariant(const Loop *L) const {
405 // All non-instruction values are loop invariant. All instructions are loop
406 // invariant if they are not contained in the specified loop.
Dan Gohmana3035a62009-05-20 01:01:24 +0000407 // Instructions are never considered invariant in the function body
408 // (null loop) because they are defined within the "loop".
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000409 if (Instruction *I = dyn_cast<Instruction>(V))
Dan Gohmana3035a62009-05-20 01:01:24 +0000410 return L && !L->contains(I->getParent());
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000411 return true;
412}
Chris Lattner53e677a2004-04-02 20:23:17 +0000413
Evan Cheng5a6c1a82009-02-17 00:13:06 +0000414bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const {
415 if (Instruction *I = dyn_cast<Instruction>(getValue()))
416 return DT->dominates(I->getParent(), BB);
417 return true;
418}
419
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000420const Type *SCEVUnknown::getType() const {
421 return V->getType();
422}
Chris Lattner53e677a2004-04-02 20:23:17 +0000423
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000424void SCEVUnknown::print(raw_ostream &OS) const {
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000425 WriteAsOperand(OS, V, false);
Chris Lattner53e677a2004-04-02 20:23:17 +0000426}
427
Chris Lattner8d741b82004-06-20 06:23:15 +0000428//===----------------------------------------------------------------------===//
429// SCEV Utilities
430//===----------------------------------------------------------------------===//
431
432namespace {
433 /// SCEVComplexityCompare - Return true if the complexity of the LHS is less
434 /// than the complexity of the RHS. This comparator is used to canonicalize
435 /// expressions.
Dan Gohman72861302009-05-07 14:39:04 +0000436 class VISIBILITY_HIDDEN SCEVComplexityCompare {
437 LoopInfo *LI;
438 public:
439 explicit SCEVComplexityCompare(LoopInfo *li) : LI(li) {}
440
Dan Gohmanf7b37b22008-04-14 18:23:56 +0000441 bool operator()(const SCEV *LHS, const SCEV *RHS) const {
Dan Gohman72861302009-05-07 14:39:04 +0000442 // Primarily, sort the SCEVs by their getSCEVType().
443 if (LHS->getSCEVType() != RHS->getSCEVType())
444 return LHS->getSCEVType() < RHS->getSCEVType();
445
446 // Aside from the getSCEVType() ordering, the particular ordering
447 // isn't very important except that it's beneficial to be consistent,
448 // so that (a + b) and (b + a) don't end up as different expressions.
449
450 // Sort SCEVUnknown values with some loose heuristics. TODO: This is
451 // not as complete as it could be.
452 if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) {
453 const SCEVUnknown *RU = cast<SCEVUnknown>(RHS);
454
Dan Gohman5be18e82009-05-19 02:15:55 +0000455 // Order pointer values after integer values. This helps SCEVExpander
456 // form GEPs.
457 if (isa<PointerType>(LU->getType()) && !isa<PointerType>(RU->getType()))
458 return false;
459 if (isa<PointerType>(RU->getType()) && !isa<PointerType>(LU->getType()))
460 return true;
461
Dan Gohman72861302009-05-07 14:39:04 +0000462 // Compare getValueID values.
463 if (LU->getValue()->getValueID() != RU->getValue()->getValueID())
464 return LU->getValue()->getValueID() < RU->getValue()->getValueID();
465
466 // Sort arguments by their position.
467 if (const Argument *LA = dyn_cast<Argument>(LU->getValue())) {
468 const Argument *RA = cast<Argument>(RU->getValue());
469 return LA->getArgNo() < RA->getArgNo();
470 }
471
472 // For instructions, compare their loop depth, and their opcode.
473 // This is pretty loose.
474 if (Instruction *LV = dyn_cast<Instruction>(LU->getValue())) {
475 Instruction *RV = cast<Instruction>(RU->getValue());
476
477 // Compare loop depths.
478 if (LI->getLoopDepth(LV->getParent()) !=
479 LI->getLoopDepth(RV->getParent()))
480 return LI->getLoopDepth(LV->getParent()) <
481 LI->getLoopDepth(RV->getParent());
482
483 // Compare opcodes.
484 if (LV->getOpcode() != RV->getOpcode())
485 return LV->getOpcode() < RV->getOpcode();
486
487 // Compare the number of operands.
488 if (LV->getNumOperands() != RV->getNumOperands())
489 return LV->getNumOperands() < RV->getNumOperands();
490 }
491
492 return false;
493 }
494
Dan Gohman4dfad292009-06-14 22:51:25 +0000495 // Compare constant values.
496 if (const SCEVConstant *LC = dyn_cast<SCEVConstant>(LHS)) {
497 const SCEVConstant *RC = cast<SCEVConstant>(RHS);
Nick Lewyckyd1ec9892009-07-04 17:24:52 +0000498 if (LC->getValue()->getBitWidth() != RC->getValue()->getBitWidth())
499 return LC->getValue()->getBitWidth() < RC->getValue()->getBitWidth();
Dan Gohman4dfad292009-06-14 22:51:25 +0000500 return LC->getValue()->getValue().ult(RC->getValue()->getValue());
501 }
502
503 // Compare addrec loop depths.
504 if (const SCEVAddRecExpr *LA = dyn_cast<SCEVAddRecExpr>(LHS)) {
505 const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS);
506 if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth())
507 return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth();
508 }
Dan Gohman72861302009-05-07 14:39:04 +0000509
510 // Lexicographically compare n-ary expressions.
511 if (const SCEVNAryExpr *LC = dyn_cast<SCEVNAryExpr>(LHS)) {
512 const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS);
513 for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) {
514 if (i >= RC->getNumOperands())
515 return false;
516 if (operator()(LC->getOperand(i), RC->getOperand(i)))
517 return true;
518 if (operator()(RC->getOperand(i), LC->getOperand(i)))
519 return false;
520 }
521 return LC->getNumOperands() < RC->getNumOperands();
522 }
523
Dan Gohmana6b35e22009-05-07 19:23:21 +0000524 // Lexicographically compare udiv expressions.
525 if (const SCEVUDivExpr *LC = dyn_cast<SCEVUDivExpr>(LHS)) {
526 const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS);
527 if (operator()(LC->getLHS(), RC->getLHS()))
528 return true;
529 if (operator()(RC->getLHS(), LC->getLHS()))
530 return false;
531 if (operator()(LC->getRHS(), RC->getRHS()))
532 return true;
533 if (operator()(RC->getRHS(), LC->getRHS()))
534 return false;
535 return false;
536 }
537
Dan Gohman72861302009-05-07 14:39:04 +0000538 // Compare cast expressions by operand.
539 if (const SCEVCastExpr *LC = dyn_cast<SCEVCastExpr>(LHS)) {
540 const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS);
541 return operator()(LC->getOperand(), RC->getOperand());
542 }
543
544 assert(0 && "Unknown SCEV kind!");
545 return false;
Chris Lattner8d741b82004-06-20 06:23:15 +0000546 }
547 };
548}
549
550/// GroupByComplexity - Given a list of SCEV objects, order them by their
551/// complexity, and group objects of the same complexity together by value.
552/// When this routine is finished, we know that any duplicates in the vector are
553/// consecutive and that complexity is monotonically increasing.
554///
555/// Note that we go take special precautions to ensure that we get determinstic
556/// results from this routine. In other words, we don't want the results of
557/// this to depend on where the addresses of various SCEV objects happened to
558/// land in memory.
559///
Owen Anderson372b46c2009-06-22 21:39:50 +0000560static void GroupByComplexity(SmallVectorImpl<const SCEV*> &Ops,
Dan Gohman72861302009-05-07 14:39:04 +0000561 LoopInfo *LI) {
Chris Lattner8d741b82004-06-20 06:23:15 +0000562 if (Ops.size() < 2) return; // Noop
563 if (Ops.size() == 2) {
564 // This is the common case, which also happens to be trivially simple.
565 // Special case it.
Dan Gohman72861302009-05-07 14:39:04 +0000566 if (SCEVComplexityCompare(LI)(Ops[1], Ops[0]))
Chris Lattner8d741b82004-06-20 06:23:15 +0000567 std::swap(Ops[0], Ops[1]);
568 return;
569 }
570
571 // Do the rough sort by complexity.
Dan Gohman72861302009-05-07 14:39:04 +0000572 std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI));
Chris Lattner8d741b82004-06-20 06:23:15 +0000573
574 // Now that we are sorted by complexity, group elements of the same
575 // complexity. Note that this is, at worst, N^2, but the vector is likely to
576 // be extremely short in practice. Note that we take this approach because we
577 // do not want to depend on the addresses of the objects we are grouping.
Chris Lattner2d584522004-06-20 17:01:44 +0000578 for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
Dan Gohman35738ac2009-05-04 22:30:44 +0000579 const SCEV *S = Ops[i];
Chris Lattner8d741b82004-06-20 06:23:15 +0000580 unsigned Complexity = S->getSCEVType();
581
582 // If there are any objects of the same complexity and same value as this
583 // one, group them.
584 for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
585 if (Ops[j] == S) { // Found a duplicate.
586 // Move it to immediately after i'th element.
587 std::swap(Ops[i+1], Ops[j]);
588 ++i; // no need to rescan it.
Chris Lattner541ad5e2004-06-20 20:32:16 +0000589 if (i == e-2) return; // Done!
Chris Lattner8d741b82004-06-20 06:23:15 +0000590 }
591 }
592 }
593}
594
Chris Lattner53e677a2004-04-02 20:23:17 +0000595
Chris Lattner53e677a2004-04-02 20:23:17 +0000596
597//===----------------------------------------------------------------------===//
598// Simple SCEV method implementations
599//===----------------------------------------------------------------------===//
600
Eli Friedmanb42a6262008-08-04 23:49:06 +0000601/// BinomialCoefficient - Compute BC(It, K). The result has width W.
Dan Gohman6c0866c2009-05-24 23:45:28 +0000602/// Assume, K > 0.
Owen Anderson372b46c2009-06-22 21:39:50 +0000603static const SCEV* BinomialCoefficient(const SCEV* It, unsigned K,
Eli Friedmanb42a6262008-08-04 23:49:06 +0000604 ScalarEvolution &SE,
Dan Gohman2d1be872009-04-16 03:18:22 +0000605 const Type* ResultTy) {
Eli Friedmanb42a6262008-08-04 23:49:06 +0000606 // Handle the simplest case efficiently.
607 if (K == 1)
608 return SE.getTruncateOrZeroExtend(It, ResultTy);
609
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000610 // We are using the following formula for BC(It, K):
611 //
612 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K!
613 //
Eli Friedmanb42a6262008-08-04 23:49:06 +0000614 // Suppose, W is the bitwidth of the return value. We must be prepared for
615 // overflow. Hence, we must assure that the result of our computation is
616 // equal to the accurate one modulo 2^W. Unfortunately, division isn't
617 // safe in modular arithmetic.
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000618 //
Eli Friedmanb42a6262008-08-04 23:49:06 +0000619 // However, this code doesn't use exactly that formula; the formula it uses
Dan Gohman64a845e2009-06-24 04:48:43 +0000620 // is something like the following, where T is the number of factors of 2 in
Eli Friedmanb42a6262008-08-04 23:49:06 +0000621 // K! (i.e. trailing zeros in the binary representation of K!), and ^ is
622 // exponentiation:
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000623 //
Eli Friedmanb42a6262008-08-04 23:49:06 +0000624 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T)
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000625 //
Eli Friedmanb42a6262008-08-04 23:49:06 +0000626 // This formula is trivially equivalent to the previous formula. However,
627 // this formula can be implemented much more efficiently. The trick is that
628 // K! / 2^T is odd, and exact division by an odd number *is* safe in modular
629 // arithmetic. To do exact division in modular arithmetic, all we have
630 // to do is multiply by the inverse. Therefore, this step can be done at
631 // width W.
Dan Gohman64a845e2009-06-24 04:48:43 +0000632 //
Eli Friedmanb42a6262008-08-04 23:49:06 +0000633 // The next issue is how to safely do the division by 2^T. The way this
634 // is done is by doing the multiplication step at a width of at least W + T
635 // bits. This way, the bottom W+T bits of the product are accurate. Then,
636 // when we perform the division by 2^T (which is equivalent to a right shift
637 // by T), the bottom W bits are accurate. Extra bits are okay; they'll get
638 // truncated out after the division by 2^T.
639 //
640 // In comparison to just directly using the first formula, this technique
641 // is much more efficient; using the first formula requires W * K bits,
642 // but this formula less than W + K bits. Also, the first formula requires
643 // a division step, whereas this formula only requires multiplies and shifts.
644 //
645 // It doesn't matter whether the subtraction step is done in the calculation
646 // width or the input iteration count's width; if the subtraction overflows,
647 // the result must be zero anyway. We prefer here to do it in the width of
648 // the induction variable because it helps a lot for certain cases; CodeGen
649 // isn't smart enough to ignore the overflow, which leads to much less
650 // efficient code if the width of the subtraction is wider than the native
651 // register width.
652 //
653 // (It's possible to not widen at all by pulling out factors of 2 before
654 // the multiplication; for example, K=2 can be calculated as
655 // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires
656 // extra arithmetic, so it's not an obvious win, and it gets
657 // much more complicated for K > 3.)
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000658
Eli Friedmanb42a6262008-08-04 23:49:06 +0000659 // Protection from insane SCEVs; this bound is conservative,
660 // but it probably doesn't matter.
661 if (K > 1000)
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +0000662 return SE.getCouldNotCompute();
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000663
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000664 unsigned W = SE.getTypeSizeInBits(ResultTy);
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000665
Eli Friedmanb42a6262008-08-04 23:49:06 +0000666 // Calculate K! / 2^T and T; we divide out the factors of two before
667 // multiplying for calculating K! / 2^T to avoid overflow.
668 // Other overflow doesn't matter because we only care about the bottom
669 // W bits of the result.
670 APInt OddFactorial(W, 1);
671 unsigned T = 1;
672 for (unsigned i = 3; i <= K; ++i) {
673 APInt Mult(W, i);
674 unsigned TwoFactors = Mult.countTrailingZeros();
675 T += TwoFactors;
676 Mult = Mult.lshr(TwoFactors);
677 OddFactorial *= Mult;
Chris Lattner53e677a2004-04-02 20:23:17 +0000678 }
Nick Lewycky6f8abf92008-06-13 04:38:55 +0000679
Eli Friedmanb42a6262008-08-04 23:49:06 +0000680 // We need at least W + T bits for the multiplication step
Nick Lewycky237d8732009-01-25 08:16:27 +0000681 unsigned CalculationBits = W + T;
Eli Friedmanb42a6262008-08-04 23:49:06 +0000682
683 // Calcuate 2^T, at width T+W.
684 APInt DivFactor = APInt(CalculationBits, 1).shl(T);
685
686 // Calculate the multiplicative inverse of K! / 2^T;
687 // this multiplication factor will perform the exact division by
688 // K! / 2^T.
689 APInt Mod = APInt::getSignedMinValue(W+1);
690 APInt MultiplyFactor = OddFactorial.zext(W+1);
691 MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod);
692 MultiplyFactor = MultiplyFactor.trunc(W);
693
694 // Calculate the product, at width T+W
695 const IntegerType *CalculationTy = IntegerType::get(CalculationBits);
Owen Anderson372b46c2009-06-22 21:39:50 +0000696 const SCEV* Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy);
Eli Friedmanb42a6262008-08-04 23:49:06 +0000697 for (unsigned i = 1; i != K; ++i) {
Owen Anderson372b46c2009-06-22 21:39:50 +0000698 const SCEV* S = SE.getMinusSCEV(It, SE.getIntegerSCEV(i, It->getType()));
Eli Friedmanb42a6262008-08-04 23:49:06 +0000699 Dividend = SE.getMulExpr(Dividend,
700 SE.getTruncateOrZeroExtend(S, CalculationTy));
701 }
702
703 // Divide by 2^T
Owen Anderson372b46c2009-06-22 21:39:50 +0000704 const SCEV* DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor));
Eli Friedmanb42a6262008-08-04 23:49:06 +0000705
706 // Truncate the result, and divide by K! / 2^T.
707
708 return SE.getMulExpr(SE.getConstant(MultiplyFactor),
709 SE.getTruncateOrZeroExtend(DivResult, ResultTy));
Chris Lattner53e677a2004-04-02 20:23:17 +0000710}
711
Chris Lattner53e677a2004-04-02 20:23:17 +0000712/// evaluateAtIteration - Return the value of this chain of recurrences at
713/// the specified iteration number. We can evaluate this recurrence by
714/// multiplying each element in the chain by the binomial coefficient
715/// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as:
716///
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000717/// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3)
Chris Lattner53e677a2004-04-02 20:23:17 +0000718///
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000719/// where BC(It, k) stands for binomial coefficient.
Chris Lattner53e677a2004-04-02 20:23:17 +0000720///
Owen Anderson372b46c2009-06-22 21:39:50 +0000721const SCEV* SCEVAddRecExpr::evaluateAtIteration(const SCEV* It,
Dan Gohman246b2562007-10-22 18:31:58 +0000722 ScalarEvolution &SE) const {
Owen Anderson372b46c2009-06-22 21:39:50 +0000723 const SCEV* Result = getStart();
Chris Lattner53e677a2004-04-02 20:23:17 +0000724 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000725 // The computation is correct in the face of overflow provided that the
726 // multiplication is performed _after_ the evaluation of the binomial
727 // coefficient.
Owen Anderson372b46c2009-06-22 21:39:50 +0000728 const SCEV* Coeff = BinomialCoefficient(It, i, SE, getType());
Nick Lewyckycb8f1b52008-10-13 03:58:02 +0000729 if (isa<SCEVCouldNotCompute>(Coeff))
730 return Coeff;
731
732 Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff));
Chris Lattner53e677a2004-04-02 20:23:17 +0000733 }
734 return Result;
735}
736
Chris Lattner53e677a2004-04-02 20:23:17 +0000737//===----------------------------------------------------------------------===//
738// SCEV Expression folder implementations
739//===----------------------------------------------------------------------===//
740
Owen Anderson372b46c2009-06-22 21:39:50 +0000741const SCEV* ScalarEvolution::getTruncateExpr(const SCEV* Op,
Dan Gohman99243b32009-05-01 16:44:56 +0000742 const Type *Ty) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000743 assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) &&
Dan Gohmanfb17fd22009-04-21 00:55:22 +0000744 "This is not a truncating conversion!");
Dan Gohman10b94792009-05-01 16:44:18 +0000745 assert(isSCEVable(Ty) &&
746 "This is not a conversion to a SCEVable type!");
747 Ty = getEffectiveSCEVType(Ty);
Dan Gohmanfb17fd22009-04-21 00:55:22 +0000748
Dan Gohmanc39f44b2009-06-30 20:13:32 +0000749 // Fold if the operand is constant.
Dan Gohman622ed672009-05-04 22:02:23 +0000750 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
Dan Gohmanb8be8b72009-06-24 00:38:39 +0000751 return getConstant(
752 cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty)));
Chris Lattner53e677a2004-04-02 20:23:17 +0000753
Dan Gohman20900ca2009-04-22 16:20:48 +0000754 // trunc(trunc(x)) --> trunc(x)
Dan Gohman622ed672009-05-04 22:02:23 +0000755 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op))
Dan Gohman20900ca2009-04-22 16:20:48 +0000756 return getTruncateExpr(ST->getOperand(), Ty);
757
Nick Lewycky5cd28fa2009-04-23 05:15:08 +0000758 // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing
Dan Gohman622ed672009-05-04 22:02:23 +0000759 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
Nick Lewycky5cd28fa2009-04-23 05:15:08 +0000760 return getTruncateOrSignExtend(SS->getOperand(), Ty);
761
762 // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing
Dan Gohman622ed672009-05-04 22:02:23 +0000763 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
Nick Lewycky5cd28fa2009-04-23 05:15:08 +0000764 return getTruncateOrZeroExtend(SZ->getOperand(), Ty);
765
Dan Gohman6864db62009-06-18 16:24:47 +0000766 // If the input value is a chrec scev, truncate the chrec's operands.
Dan Gohman622ed672009-05-04 22:02:23 +0000767 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
Owen Anderson372b46c2009-06-22 21:39:50 +0000768 SmallVector<const SCEV*, 4> Operands;
Chris Lattner53e677a2004-04-02 20:23:17 +0000769 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
Dan Gohman728c7f32009-05-08 21:03:19 +0000770 Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty));
771 return getAddRecExpr(Operands, AddRec->getLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +0000772 }
773
Dan Gohman1c343752009-06-27 21:21:31 +0000774 FoldingSetNodeID ID;
775 ID.AddInteger(scTruncate);
776 ID.AddPointer(Op);
777 ID.AddPointer(Ty);
778 void *IP = 0;
779 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
780 SCEV *S = SCEVAllocator.Allocate<SCEVTruncateExpr>();
781 new (S) SCEVTruncateExpr(Op, Ty);
782 UniqueSCEVs.InsertNode(S, IP);
783 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +0000784}
785
Owen Anderson372b46c2009-06-22 21:39:50 +0000786const SCEV* ScalarEvolution::getZeroExtendExpr(const SCEV* Op,
Dan Gohman8170a682009-04-16 19:25:55 +0000787 const Type *Ty) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000788 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
Dan Gohman8170a682009-04-16 19:25:55 +0000789 "This is not an extending conversion!");
Dan Gohman10b94792009-05-01 16:44:18 +0000790 assert(isSCEVable(Ty) &&
791 "This is not a conversion to a SCEVable type!");
792 Ty = getEffectiveSCEVType(Ty);
Dan Gohman8170a682009-04-16 19:25:55 +0000793
Dan Gohmanc39f44b2009-06-30 20:13:32 +0000794 // Fold if the operand is constant.
Dan Gohman622ed672009-05-04 22:02:23 +0000795 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000796 const Type *IntTy = getEffectiveSCEVType(Ty);
Dan Gohman2d1be872009-04-16 03:18:22 +0000797 Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy);
798 if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty);
Dan Gohmanb8be8b72009-06-24 00:38:39 +0000799 return getConstant(cast<ConstantInt>(C));
Dan Gohman2d1be872009-04-16 03:18:22 +0000800 }
Chris Lattner53e677a2004-04-02 20:23:17 +0000801
Dan Gohman20900ca2009-04-22 16:20:48 +0000802 // zext(zext(x)) --> zext(x)
Dan Gohman622ed672009-05-04 22:02:23 +0000803 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
Dan Gohman20900ca2009-04-22 16:20:48 +0000804 return getZeroExtendExpr(SZ->getOperand(), Ty);
805
Dan Gohman01ecca22009-04-27 20:16:15 +0000806 // If the input value is a chrec scev, and we can prove that the value
Chris Lattner53e677a2004-04-02 20:23:17 +0000807 // did not overflow the old, smaller, value, we can zero extend all of the
Dan Gohman01ecca22009-04-27 20:16:15 +0000808 // operands (often constants). This allows analysis of something like
Chris Lattner53e677a2004-04-02 20:23:17 +0000809 // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
Dan Gohman622ed672009-05-04 22:02:23 +0000810 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
Dan Gohman01ecca22009-04-27 20:16:15 +0000811 if (AR->isAffine()) {
812 // Check whether the backedge-taken count is SCEVCouldNotCompute.
813 // Note that this serves two purposes: It filters out loops that are
814 // simply not analyzable, and it covers the case where this code is
815 // being called from within backedge-taken count analysis, such that
816 // attempting to ask for the backedge-taken count would likely result
817 // in infinite recursion. In the later case, the analysis code will
818 // cope with a conservative value, and it will take care to purge
819 // that value once it has finished.
Owen Anderson372b46c2009-06-22 21:39:50 +0000820 const SCEV* MaxBECount = getMaxBackedgeTakenCount(AR->getLoop());
Dan Gohmana1af7572009-04-30 20:47:05 +0000821 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
Dan Gohmanf0aa4852009-04-29 01:54:20 +0000822 // Manually compute the final value for AR, checking for
Dan Gohmanac70cea2009-04-29 22:28:28 +0000823 // overflow.
Owen Anderson372b46c2009-06-22 21:39:50 +0000824 const SCEV* Start = AR->getStart();
825 const SCEV* Step = AR->getStepRecurrence(*this);
Dan Gohman01ecca22009-04-27 20:16:15 +0000826
827 // Check whether the backedge-taken count can be losslessly casted to
828 // the addrec's type. The count is always unsigned.
Owen Anderson372b46c2009-06-22 21:39:50 +0000829 const SCEV* CastedMaxBECount =
Dan Gohmana1af7572009-04-30 20:47:05 +0000830 getTruncateOrZeroExtend(MaxBECount, Start->getType());
Owen Anderson372b46c2009-06-22 21:39:50 +0000831 const SCEV* RecastedMaxBECount =
Dan Gohman5183cae2009-05-18 15:58:39 +0000832 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
833 if (MaxBECount == RecastedMaxBECount) {
Dan Gohman01ecca22009-04-27 20:16:15 +0000834 const Type *WideTy =
835 IntegerType::get(getTypeSizeInBits(Start->getType()) * 2);
Dan Gohmana1af7572009-04-30 20:47:05 +0000836 // Check whether Start+Step*MaxBECount has no unsigned overflow.
Owen Anderson372b46c2009-06-22 21:39:50 +0000837 const SCEV* ZMul =
Dan Gohmana1af7572009-04-30 20:47:05 +0000838 getMulExpr(CastedMaxBECount,
Dan Gohman01ecca22009-04-27 20:16:15 +0000839 getTruncateOrZeroExtend(Step, Start->getType()));
Owen Anderson372b46c2009-06-22 21:39:50 +0000840 const SCEV* Add = getAddExpr(Start, ZMul);
841 const SCEV* OperandExtendedAdd =
Dan Gohman5183cae2009-05-18 15:58:39 +0000842 getAddExpr(getZeroExtendExpr(Start, WideTy),
843 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
844 getZeroExtendExpr(Step, WideTy)));
845 if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohmanac70cea2009-04-29 22:28:28 +0000846 // Return the expression with the addrec on the outside.
847 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
848 getZeroExtendExpr(Step, Ty),
849 AR->getLoop());
Dan Gohman01ecca22009-04-27 20:16:15 +0000850
851 // Similar to above, only this time treat the step value as signed.
852 // This covers loops that count down.
Owen Anderson372b46c2009-06-22 21:39:50 +0000853 const SCEV* SMul =
Dan Gohmana1af7572009-04-30 20:47:05 +0000854 getMulExpr(CastedMaxBECount,
Dan Gohman01ecca22009-04-27 20:16:15 +0000855 getTruncateOrSignExtend(Step, Start->getType()));
Dan Gohmanac70cea2009-04-29 22:28:28 +0000856 Add = getAddExpr(Start, SMul);
Dan Gohman5183cae2009-05-18 15:58:39 +0000857 OperandExtendedAdd =
858 getAddExpr(getZeroExtendExpr(Start, WideTy),
859 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
860 getSignExtendExpr(Step, WideTy)));
861 if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohmanac70cea2009-04-29 22:28:28 +0000862 // Return the expression with the addrec on the outside.
863 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
864 getSignExtendExpr(Step, Ty),
865 AR->getLoop());
Dan Gohman01ecca22009-04-27 20:16:15 +0000866 }
867 }
868 }
Chris Lattner53e677a2004-04-02 20:23:17 +0000869
Dan Gohman1c343752009-06-27 21:21:31 +0000870 FoldingSetNodeID ID;
871 ID.AddInteger(scZeroExtend);
872 ID.AddPointer(Op);
873 ID.AddPointer(Ty);
874 void *IP = 0;
875 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
876 SCEV *S = SCEVAllocator.Allocate<SCEVZeroExtendExpr>();
877 new (S) SCEVZeroExtendExpr(Op, Ty);
878 UniqueSCEVs.InsertNode(S, IP);
879 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +0000880}
881
Owen Anderson372b46c2009-06-22 21:39:50 +0000882const SCEV* ScalarEvolution::getSignExtendExpr(const SCEV* Op,
Dan Gohman01ecca22009-04-27 20:16:15 +0000883 const Type *Ty) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000884 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
Dan Gohmanfb17fd22009-04-21 00:55:22 +0000885 "This is not an extending conversion!");
Dan Gohman10b94792009-05-01 16:44:18 +0000886 assert(isSCEVable(Ty) &&
887 "This is not a conversion to a SCEVable type!");
888 Ty = getEffectiveSCEVType(Ty);
Dan Gohmanfb17fd22009-04-21 00:55:22 +0000889
Dan Gohmanc39f44b2009-06-30 20:13:32 +0000890 // Fold if the operand is constant.
Dan Gohman622ed672009-05-04 22:02:23 +0000891 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000892 const Type *IntTy = getEffectiveSCEVType(Ty);
Dan Gohman2d1be872009-04-16 03:18:22 +0000893 Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy);
894 if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty);
Dan Gohmanb8be8b72009-06-24 00:38:39 +0000895 return getConstant(cast<ConstantInt>(C));
Dan Gohman2d1be872009-04-16 03:18:22 +0000896 }
Dan Gohmand19534a2007-06-15 14:38:12 +0000897
Dan Gohman20900ca2009-04-22 16:20:48 +0000898 // sext(sext(x)) --> sext(x)
Dan Gohman622ed672009-05-04 22:02:23 +0000899 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
Dan Gohman20900ca2009-04-22 16:20:48 +0000900 return getSignExtendExpr(SS->getOperand(), Ty);
901
Dan Gohman01ecca22009-04-27 20:16:15 +0000902 // If the input value is a chrec scev, and we can prove that the value
Dan Gohmand19534a2007-06-15 14:38:12 +0000903 // did not overflow the old, smaller, value, we can sign extend all of the
Dan Gohman01ecca22009-04-27 20:16:15 +0000904 // operands (often constants). This allows analysis of something like
Dan Gohmand19534a2007-06-15 14:38:12 +0000905 // this: for (signed char X = 0; X < 100; ++X) { int Y = X; }
Dan Gohman622ed672009-05-04 22:02:23 +0000906 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
Dan Gohman01ecca22009-04-27 20:16:15 +0000907 if (AR->isAffine()) {
908 // Check whether the backedge-taken count is SCEVCouldNotCompute.
909 // Note that this serves two purposes: It filters out loops that are
910 // simply not analyzable, and it covers the case where this code is
911 // being called from within backedge-taken count analysis, such that
912 // attempting to ask for the backedge-taken count would likely result
913 // in infinite recursion. In the later case, the analysis code will
914 // cope with a conservative value, and it will take care to purge
915 // that value once it has finished.
Owen Anderson372b46c2009-06-22 21:39:50 +0000916 const SCEV* MaxBECount = getMaxBackedgeTakenCount(AR->getLoop());
Dan Gohmana1af7572009-04-30 20:47:05 +0000917 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
Dan Gohmanf0aa4852009-04-29 01:54:20 +0000918 // Manually compute the final value for AR, checking for
Dan Gohmanac70cea2009-04-29 22:28:28 +0000919 // overflow.
Owen Anderson372b46c2009-06-22 21:39:50 +0000920 const SCEV* Start = AR->getStart();
921 const SCEV* Step = AR->getStepRecurrence(*this);
Dan Gohman01ecca22009-04-27 20:16:15 +0000922
923 // Check whether the backedge-taken count can be losslessly casted to
Dan Gohmanac70cea2009-04-29 22:28:28 +0000924 // the addrec's type. The count is always unsigned.
Owen Anderson372b46c2009-06-22 21:39:50 +0000925 const SCEV* CastedMaxBECount =
Dan Gohmana1af7572009-04-30 20:47:05 +0000926 getTruncateOrZeroExtend(MaxBECount, Start->getType());
Owen Anderson372b46c2009-06-22 21:39:50 +0000927 const SCEV* RecastedMaxBECount =
Dan Gohman5183cae2009-05-18 15:58:39 +0000928 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
929 if (MaxBECount == RecastedMaxBECount) {
Dan Gohman01ecca22009-04-27 20:16:15 +0000930 const Type *WideTy =
931 IntegerType::get(getTypeSizeInBits(Start->getType()) * 2);
Dan Gohmana1af7572009-04-30 20:47:05 +0000932 // Check whether Start+Step*MaxBECount has no signed overflow.
Owen Anderson372b46c2009-06-22 21:39:50 +0000933 const SCEV* SMul =
Dan Gohmana1af7572009-04-30 20:47:05 +0000934 getMulExpr(CastedMaxBECount,
Dan Gohman01ecca22009-04-27 20:16:15 +0000935 getTruncateOrSignExtend(Step, Start->getType()));
Owen Anderson372b46c2009-06-22 21:39:50 +0000936 const SCEV* Add = getAddExpr(Start, SMul);
937 const SCEV* OperandExtendedAdd =
Dan Gohman5183cae2009-05-18 15:58:39 +0000938 getAddExpr(getSignExtendExpr(Start, WideTy),
939 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
940 getSignExtendExpr(Step, WideTy)));
941 if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohmanac70cea2009-04-29 22:28:28 +0000942 // Return the expression with the addrec on the outside.
943 return getAddRecExpr(getSignExtendExpr(Start, Ty),
944 getSignExtendExpr(Step, Ty),
945 AR->getLoop());
Dan Gohman01ecca22009-04-27 20:16:15 +0000946 }
947 }
948 }
Dan Gohmand19534a2007-06-15 14:38:12 +0000949
Dan Gohman1c343752009-06-27 21:21:31 +0000950 FoldingSetNodeID ID;
951 ID.AddInteger(scSignExtend);
952 ID.AddPointer(Op);
953 ID.AddPointer(Ty);
954 void *IP = 0;
955 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
956 SCEV *S = SCEVAllocator.Allocate<SCEVSignExtendExpr>();
957 new (S) SCEVSignExtendExpr(Op, Ty);
958 UniqueSCEVs.InsertNode(S, IP);
959 return S;
Dan Gohmand19534a2007-06-15 14:38:12 +0000960}
961
Dan Gohman2ce84c8d2009-06-13 15:56:47 +0000962/// getAnyExtendExpr - Return a SCEV for the given operand extended with
963/// unspecified bits out to the given type.
964///
Owen Anderson372b46c2009-06-22 21:39:50 +0000965const SCEV* ScalarEvolution::getAnyExtendExpr(const SCEV* Op,
Dan Gohman2ce84c8d2009-06-13 15:56:47 +0000966 const Type *Ty) {
967 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
968 "This is not an extending conversion!");
969 assert(isSCEVable(Ty) &&
970 "This is not a conversion to a SCEVable type!");
971 Ty = getEffectiveSCEVType(Ty);
972
973 // Sign-extend negative constants.
974 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
975 if (SC->getValue()->getValue().isNegative())
976 return getSignExtendExpr(Op, Ty);
977
978 // Peel off a truncate cast.
979 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) {
Owen Anderson372b46c2009-06-22 21:39:50 +0000980 const SCEV* NewOp = T->getOperand();
Dan Gohman2ce84c8d2009-06-13 15:56:47 +0000981 if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty))
982 return getAnyExtendExpr(NewOp, Ty);
983 return getTruncateOrNoop(NewOp, Ty);
984 }
985
986 // Next try a zext cast. If the cast is folded, use it.
Owen Anderson372b46c2009-06-22 21:39:50 +0000987 const SCEV* ZExt = getZeroExtendExpr(Op, Ty);
Dan Gohman2ce84c8d2009-06-13 15:56:47 +0000988 if (!isa<SCEVZeroExtendExpr>(ZExt))
989 return ZExt;
990
991 // Next try a sext cast. If the cast is folded, use it.
Owen Anderson372b46c2009-06-22 21:39:50 +0000992 const SCEV* SExt = getSignExtendExpr(Op, Ty);
Dan Gohman2ce84c8d2009-06-13 15:56:47 +0000993 if (!isa<SCEVSignExtendExpr>(SExt))
994 return SExt;
995
996 // If the expression is obviously signed, use the sext cast value.
997 if (isa<SCEVSMaxExpr>(Op))
998 return SExt;
999
1000 // Absent any other information, use the zext cast value.
1001 return ZExt;
1002}
1003
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001004/// CollectAddOperandsWithScales - Process the given Ops list, which is
1005/// a list of operands to be added under the given scale, update the given
1006/// map. This is a helper function for getAddRecExpr. As an example of
1007/// what it does, given a sequence of operands that would form an add
1008/// expression like this:
1009///
1010/// m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r)
1011///
1012/// where A and B are constants, update the map with these values:
1013///
1014/// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0)
1015///
1016/// and add 13 + A*B*29 to AccumulatedConstant.
1017/// This will allow getAddRecExpr to produce this:
1018///
1019/// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B)
1020///
1021/// This form often exposes folding opportunities that are hidden in
1022/// the original operand list.
1023///
1024/// Return true iff it appears that any interesting folding opportunities
1025/// may be exposed. This helps getAddRecExpr short-circuit extra work in
1026/// the common case where no interesting opportunities are present, and
1027/// is also used as a check to avoid infinite recursion.
1028///
1029static bool
Owen Anderson372b46c2009-06-22 21:39:50 +00001030CollectAddOperandsWithScales(DenseMap<const SCEV*, APInt> &M,
1031 SmallVector<const SCEV*, 8> &NewOps,
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001032 APInt &AccumulatedConstant,
Owen Anderson372b46c2009-06-22 21:39:50 +00001033 const SmallVectorImpl<const SCEV*> &Ops,
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001034 const APInt &Scale,
1035 ScalarEvolution &SE) {
1036 bool Interesting = false;
1037
1038 // Iterate over the add operands.
1039 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1040 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]);
1041 if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) {
1042 APInt NewScale =
1043 Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue();
1044 if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) {
1045 // A multiplication of a constant with another add; recurse.
1046 Interesting |=
1047 CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
1048 cast<SCEVAddExpr>(Mul->getOperand(1))
1049 ->getOperands(),
1050 NewScale, SE);
1051 } else {
1052 // A multiplication of a constant with some other value. Update
1053 // the map.
Owen Anderson372b46c2009-06-22 21:39:50 +00001054 SmallVector<const SCEV*, 4> MulOps(Mul->op_begin()+1, Mul->op_end());
1055 const SCEV* Key = SE.getMulExpr(MulOps);
1056 std::pair<DenseMap<const SCEV*, APInt>::iterator, bool> Pair =
Dan Gohman23737e02009-06-29 18:25:52 +00001057 M.insert(std::make_pair(Key, NewScale));
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001058 if (Pair.second) {
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001059 NewOps.push_back(Pair.first->first);
1060 } else {
1061 Pair.first->second += NewScale;
1062 // The map already had an entry for this value, which may indicate
1063 // a folding opportunity.
1064 Interesting = true;
1065 }
1066 }
1067 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
1068 // Pull a buried constant out to the outside.
1069 if (Scale != 1 || AccumulatedConstant != 0 || C->isZero())
1070 Interesting = true;
1071 AccumulatedConstant += Scale * C->getValue()->getValue();
1072 } else {
1073 // An ordinary operand. Update the map.
Owen Anderson372b46c2009-06-22 21:39:50 +00001074 std::pair<DenseMap<const SCEV*, APInt>::iterator, bool> Pair =
Dan Gohman23737e02009-06-29 18:25:52 +00001075 M.insert(std::make_pair(Ops[i], Scale));
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001076 if (Pair.second) {
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001077 NewOps.push_back(Pair.first->first);
1078 } else {
1079 Pair.first->second += Scale;
1080 // The map already had an entry for this value, which may indicate
1081 // a folding opportunity.
1082 Interesting = true;
1083 }
1084 }
1085 }
1086
1087 return Interesting;
1088}
1089
1090namespace {
1091 struct APIntCompare {
1092 bool operator()(const APInt &LHS, const APInt &RHS) const {
1093 return LHS.ult(RHS);
1094 }
1095 };
1096}
1097
Dan Gohman6c0866c2009-05-24 23:45:28 +00001098/// getAddExpr - Get a canonical add expression, or something simpler if
1099/// possible.
Owen Anderson372b46c2009-06-22 21:39:50 +00001100const SCEV* ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV*> &Ops) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001101 assert(!Ops.empty() && "Cannot get empty add!");
Chris Lattner627018b2004-04-07 16:16:11 +00001102 if (Ops.size() == 1) return Ops[0];
Dan Gohmanf78a9782009-05-18 15:44:58 +00001103#ifndef NDEBUG
1104 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1105 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1106 getEffectiveSCEVType(Ops[0]->getType()) &&
1107 "SCEVAddExpr operand types don't match!");
1108#endif
Chris Lattner53e677a2004-04-02 20:23:17 +00001109
1110 // Sort by complexity, this groups all similar expression types together.
Dan Gohman72861302009-05-07 14:39:04 +00001111 GroupByComplexity(Ops, LI);
Chris Lattner53e677a2004-04-02 20:23:17 +00001112
1113 // If there are any constants, fold them together.
1114 unsigned Idx = 0;
Dan Gohman622ed672009-05-04 22:02:23 +00001115 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001116 ++Idx;
Chris Lattner627018b2004-04-07 16:16:11 +00001117 assert(Idx < Ops.size());
Dan Gohman622ed672009-05-04 22:02:23 +00001118 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001119 // We found two constants, fold them together!
Dan Gohmana82752c2009-06-14 22:47:23 +00001120 Ops[0] = getConstant(LHSC->getValue()->getValue() +
1121 RHSC->getValue()->getValue());
Dan Gohman7f7c4362009-06-14 22:53:57 +00001122 if (Ops.size() == 2) return Ops[0];
Nick Lewycky3e630762008-02-20 06:48:22 +00001123 Ops.erase(Ops.begin()+1); // Erase the folded element
Nick Lewycky3e630762008-02-20 06:48:22 +00001124 LHSC = cast<SCEVConstant>(Ops[0]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001125 }
1126
1127 // If we are left with a constant zero being added, strip it off.
Reid Spencercae57542007-03-02 00:28:52 +00001128 if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001129 Ops.erase(Ops.begin());
1130 --Idx;
1131 }
1132 }
1133
Chris Lattner627018b2004-04-07 16:16:11 +00001134 if (Ops.size() == 1) return Ops[0];
Misha Brukman2b37d7c2005-04-21 21:13:18 +00001135
Chris Lattner53e677a2004-04-02 20:23:17 +00001136 // Okay, check to see if the same value occurs in the operand list twice. If
1137 // so, merge them together into an multiply expression. Since we sorted the
1138 // list, these values are required to be adjacent.
1139 const Type *Ty = Ops[0]->getType();
1140 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
1141 if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2
1142 // Found a match, merge the two values into a multiply, and add any
1143 // remaining values to the result.
Owen Anderson372b46c2009-06-22 21:39:50 +00001144 const SCEV* Two = getIntegerSCEV(2, Ty);
1145 const SCEV* Mul = getMulExpr(Ops[i], Two);
Chris Lattner53e677a2004-04-02 20:23:17 +00001146 if (Ops.size() == 2)
1147 return Mul;
1148 Ops.erase(Ops.begin()+i, Ops.begin()+i+2);
1149 Ops.push_back(Mul);
Dan Gohman246b2562007-10-22 18:31:58 +00001150 return getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001151 }
1152
Dan Gohman728c7f32009-05-08 21:03:19 +00001153 // Check for truncates. If all the operands are truncated from the same
1154 // type, see if factoring out the truncate would permit the result to be
1155 // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n)
1156 // if the contents of the resulting outer trunc fold to something simple.
1157 for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) {
1158 const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]);
1159 const Type *DstType = Trunc->getType();
1160 const Type *SrcType = Trunc->getOperand()->getType();
Owen Anderson372b46c2009-06-22 21:39:50 +00001161 SmallVector<const SCEV*, 8> LargeOps;
Dan Gohman728c7f32009-05-08 21:03:19 +00001162 bool Ok = true;
1163 // Check all the operands to see if they can be represented in the
1164 // source type of the truncate.
1165 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1166 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) {
1167 if (T->getOperand()->getType() != SrcType) {
1168 Ok = false;
1169 break;
1170 }
1171 LargeOps.push_back(T->getOperand());
1172 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
1173 // This could be either sign or zero extension, but sign extension
1174 // is much more likely to be foldable here.
1175 LargeOps.push_back(getSignExtendExpr(C, SrcType));
1176 } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) {
Owen Anderson372b46c2009-06-22 21:39:50 +00001177 SmallVector<const SCEV*, 8> LargeMulOps;
Dan Gohman728c7f32009-05-08 21:03:19 +00001178 for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
1179 if (const SCEVTruncateExpr *T =
1180 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) {
1181 if (T->getOperand()->getType() != SrcType) {
1182 Ok = false;
1183 break;
1184 }
1185 LargeMulOps.push_back(T->getOperand());
1186 } else if (const SCEVConstant *C =
1187 dyn_cast<SCEVConstant>(M->getOperand(j))) {
1188 // This could be either sign or zero extension, but sign extension
1189 // is much more likely to be foldable here.
1190 LargeMulOps.push_back(getSignExtendExpr(C, SrcType));
1191 } else {
1192 Ok = false;
1193 break;
1194 }
1195 }
1196 if (Ok)
1197 LargeOps.push_back(getMulExpr(LargeMulOps));
1198 } else {
1199 Ok = false;
1200 break;
1201 }
1202 }
1203 if (Ok) {
1204 // Evaluate the expression in the larger type.
Owen Anderson372b46c2009-06-22 21:39:50 +00001205 const SCEV* Fold = getAddExpr(LargeOps);
Dan Gohman728c7f32009-05-08 21:03:19 +00001206 // If it folds to something simple, use it. Otherwise, don't.
1207 if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold))
1208 return getTruncateExpr(Fold, DstType);
1209 }
1210 }
1211
1212 // Skip past any other cast SCEVs.
Dan Gohmanf50cd742007-06-18 19:30:09 +00001213 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr)
1214 ++Idx;
1215
1216 // If there are add operands they would be next.
Chris Lattner53e677a2004-04-02 20:23:17 +00001217 if (Idx < Ops.size()) {
1218 bool DeletedAdd = false;
Dan Gohman622ed672009-05-04 22:02:23 +00001219 while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001220 // If we have an add, expand the add operands onto the end of the operands
1221 // list.
1222 Ops.insert(Ops.end(), Add->op_begin(), Add->op_end());
1223 Ops.erase(Ops.begin()+Idx);
1224 DeletedAdd = true;
1225 }
1226
1227 // If we deleted at least one add, we added operands to the end of the list,
1228 // and they are not necessarily sorted. Recurse to resort and resimplify
1229 // any operands we just aquired.
1230 if (DeletedAdd)
Dan Gohman246b2562007-10-22 18:31:58 +00001231 return getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001232 }
1233
1234 // Skip over the add expression until we get to a multiply.
1235 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
1236 ++Idx;
1237
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001238 // Check to see if there are any folding opportunities present with
1239 // operands multiplied by constant values.
1240 if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) {
1241 uint64_t BitWidth = getTypeSizeInBits(Ty);
Owen Anderson372b46c2009-06-22 21:39:50 +00001242 DenseMap<const SCEV*, APInt> M;
1243 SmallVector<const SCEV*, 8> NewOps;
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001244 APInt AccumulatedConstant(BitWidth, 0);
1245 if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
1246 Ops, APInt(BitWidth, 1), *this)) {
1247 // Some interesting folding opportunity is present, so its worthwhile to
1248 // re-generate the operands list. Group the operands by constant scale,
1249 // to avoid multiplying by the same constant scale multiple times.
Owen Anderson372b46c2009-06-22 21:39:50 +00001250 std::map<APInt, SmallVector<const SCEV*, 4>, APIntCompare> MulOpLists;
1251 for (SmallVector<const SCEV*, 8>::iterator I = NewOps.begin(),
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001252 E = NewOps.end(); I != E; ++I)
1253 MulOpLists[M.find(*I)->second].push_back(*I);
1254 // Re-generate the operands list.
1255 Ops.clear();
1256 if (AccumulatedConstant != 0)
1257 Ops.push_back(getConstant(AccumulatedConstant));
Dan Gohman64a845e2009-06-24 04:48:43 +00001258 for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator
1259 I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I)
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001260 if (I->first != 0)
Dan Gohman64a845e2009-06-24 04:48:43 +00001261 Ops.push_back(getMulExpr(getConstant(I->first),
1262 getAddExpr(I->second)));
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001263 if (Ops.empty())
1264 return getIntegerSCEV(0, Ty);
1265 if (Ops.size() == 1)
1266 return Ops[0];
1267 return getAddExpr(Ops);
1268 }
1269 }
1270
Chris Lattner53e677a2004-04-02 20:23:17 +00001271 // If we are adding something to a multiply expression, make sure the
1272 // something is not already an operand of the multiply. If so, merge it into
1273 // the multiply.
1274 for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001275 const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001276 for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001277 const SCEV *MulOpSCEV = Mul->getOperand(MulOp);
Chris Lattner53e677a2004-04-02 20:23:17 +00001278 for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
Dan Gohmana82752c2009-06-14 22:47:23 +00001279 if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(Ops[AddOp])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001280 // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1))
Owen Anderson372b46c2009-06-22 21:39:50 +00001281 const SCEV* InnerMul = Mul->getOperand(MulOp == 0);
Chris Lattner53e677a2004-04-02 20:23:17 +00001282 if (Mul->getNumOperands() != 2) {
1283 // If the multiply has more than two operands, we must get the
1284 // Y*Z term.
Owen Anderson372b46c2009-06-22 21:39:50 +00001285 SmallVector<const SCEV*, 4> MulOps(Mul->op_begin(), Mul->op_end());
Chris Lattner53e677a2004-04-02 20:23:17 +00001286 MulOps.erase(MulOps.begin()+MulOp);
Dan Gohman246b2562007-10-22 18:31:58 +00001287 InnerMul = getMulExpr(MulOps);
Chris Lattner53e677a2004-04-02 20:23:17 +00001288 }
Owen Anderson372b46c2009-06-22 21:39:50 +00001289 const SCEV* One = getIntegerSCEV(1, Ty);
1290 const SCEV* AddOne = getAddExpr(InnerMul, One);
1291 const SCEV* OuterMul = getMulExpr(AddOne, Ops[AddOp]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001292 if (Ops.size() == 2) return OuterMul;
1293 if (AddOp < Idx) {
1294 Ops.erase(Ops.begin()+AddOp);
1295 Ops.erase(Ops.begin()+Idx-1);
1296 } else {
1297 Ops.erase(Ops.begin()+Idx);
1298 Ops.erase(Ops.begin()+AddOp-1);
1299 }
1300 Ops.push_back(OuterMul);
Dan Gohman246b2562007-10-22 18:31:58 +00001301 return getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001302 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00001303
Chris Lattner53e677a2004-04-02 20:23:17 +00001304 // Check this multiply against other multiplies being added together.
1305 for (unsigned OtherMulIdx = Idx+1;
1306 OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
1307 ++OtherMulIdx) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001308 const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001309 // If MulOp occurs in OtherMul, we can fold the two multiplies
1310 // together.
1311 for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
1312 OMulOp != e; ++OMulOp)
1313 if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
1314 // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
Owen Anderson372b46c2009-06-22 21:39:50 +00001315 const SCEV* InnerMul1 = Mul->getOperand(MulOp == 0);
Chris Lattner53e677a2004-04-02 20:23:17 +00001316 if (Mul->getNumOperands() != 2) {
Dan Gohman64a845e2009-06-24 04:48:43 +00001317 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
1318 Mul->op_end());
Chris Lattner53e677a2004-04-02 20:23:17 +00001319 MulOps.erase(MulOps.begin()+MulOp);
Dan Gohman246b2562007-10-22 18:31:58 +00001320 InnerMul1 = getMulExpr(MulOps);
Chris Lattner53e677a2004-04-02 20:23:17 +00001321 }
Owen Anderson372b46c2009-06-22 21:39:50 +00001322 const SCEV* InnerMul2 = OtherMul->getOperand(OMulOp == 0);
Chris Lattner53e677a2004-04-02 20:23:17 +00001323 if (OtherMul->getNumOperands() != 2) {
Dan Gohman64a845e2009-06-24 04:48:43 +00001324 SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(),
1325 OtherMul->op_end());
Chris Lattner53e677a2004-04-02 20:23:17 +00001326 MulOps.erase(MulOps.begin()+OMulOp);
Dan Gohman246b2562007-10-22 18:31:58 +00001327 InnerMul2 = getMulExpr(MulOps);
Chris Lattner53e677a2004-04-02 20:23:17 +00001328 }
Owen Anderson372b46c2009-06-22 21:39:50 +00001329 const SCEV* InnerMulSum = getAddExpr(InnerMul1,InnerMul2);
1330 const SCEV* OuterMul = getMulExpr(MulOpSCEV, InnerMulSum);
Chris Lattner53e677a2004-04-02 20:23:17 +00001331 if (Ops.size() == 2) return OuterMul;
1332 Ops.erase(Ops.begin()+Idx);
1333 Ops.erase(Ops.begin()+OtherMulIdx-1);
1334 Ops.push_back(OuterMul);
Dan Gohman246b2562007-10-22 18:31:58 +00001335 return getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001336 }
1337 }
1338 }
1339 }
1340
1341 // If there are any add recurrences in the operands list, see if any other
1342 // added values are loop invariant. If so, we can fold them into the
1343 // recurrence.
1344 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
1345 ++Idx;
1346
1347 // Scan over all recurrences, trying to fold loop invariants into them.
1348 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
1349 // Scan all of the other operands to this add and add them to the vector if
1350 // they are loop invariant w.r.t. the recurrence.
Owen Anderson372b46c2009-06-22 21:39:50 +00001351 SmallVector<const SCEV*, 8> LIOps;
Dan Gohman35738ac2009-05-04 22:30:44 +00001352 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001353 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1354 if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
1355 LIOps.push_back(Ops[i]);
1356 Ops.erase(Ops.begin()+i);
1357 --i; --e;
1358 }
1359
1360 // If we found some loop invariants, fold them into the recurrence.
1361 if (!LIOps.empty()) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001362 // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step}
Chris Lattner53e677a2004-04-02 20:23:17 +00001363 LIOps.push_back(AddRec->getStart());
1364
Owen Anderson372b46c2009-06-22 21:39:50 +00001365 SmallVector<const SCEV*, 4> AddRecOps(AddRec->op_begin(),
Dan Gohmana82752c2009-06-14 22:47:23 +00001366 AddRec->op_end());
Dan Gohman246b2562007-10-22 18:31:58 +00001367 AddRecOps[0] = getAddExpr(LIOps);
Chris Lattner53e677a2004-04-02 20:23:17 +00001368
Owen Anderson372b46c2009-06-22 21:39:50 +00001369 const SCEV* NewRec = getAddRecExpr(AddRecOps, AddRec->getLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +00001370 // If all of the other operands were loop invariant, we are done.
1371 if (Ops.size() == 1) return NewRec;
1372
1373 // Otherwise, add the folded AddRec by the non-liv parts.
1374 for (unsigned i = 0;; ++i)
1375 if (Ops[i] == AddRec) {
1376 Ops[i] = NewRec;
1377 break;
1378 }
Dan Gohman246b2562007-10-22 18:31:58 +00001379 return getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001380 }
1381
1382 // Okay, if there weren't any loop invariants to be folded, check to see if
1383 // there are multiple AddRec's with the same loop induction variable being
1384 // added together. If so, we can fold them.
1385 for (unsigned OtherIdx = Idx+1;
1386 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
1387 if (OtherIdx != Idx) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001388 const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001389 if (AddRec->getLoop() == OtherAddRec->getLoop()) {
1390 // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D}
Dan Gohman64a845e2009-06-24 04:48:43 +00001391 SmallVector<const SCEV *, 4> NewOps(AddRec->op_begin(),
1392 AddRec->op_end());
Chris Lattner53e677a2004-04-02 20:23:17 +00001393 for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) {
1394 if (i >= NewOps.size()) {
1395 NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i,
1396 OtherAddRec->op_end());
1397 break;
1398 }
Dan Gohman246b2562007-10-22 18:31:58 +00001399 NewOps[i] = getAddExpr(NewOps[i], OtherAddRec->getOperand(i));
Chris Lattner53e677a2004-04-02 20:23:17 +00001400 }
Owen Anderson372b46c2009-06-22 21:39:50 +00001401 const SCEV* NewAddRec = getAddRecExpr(NewOps, AddRec->getLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +00001402
1403 if (Ops.size() == 2) return NewAddRec;
1404
1405 Ops.erase(Ops.begin()+Idx);
1406 Ops.erase(Ops.begin()+OtherIdx-1);
1407 Ops.push_back(NewAddRec);
Dan Gohman246b2562007-10-22 18:31:58 +00001408 return getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001409 }
1410 }
1411
1412 // Otherwise couldn't fold anything into this recurrence. Move onto the
1413 // next one.
1414 }
1415
1416 // Okay, it looks like we really DO need an add expr. Check to see if we
1417 // already have one, otherwise create a new one.
Dan Gohman1c343752009-06-27 21:21:31 +00001418 FoldingSetNodeID ID;
1419 ID.AddInteger(scAddExpr);
1420 ID.AddInteger(Ops.size());
1421 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1422 ID.AddPointer(Ops[i]);
1423 void *IP = 0;
1424 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1425 SCEV *S = SCEVAllocator.Allocate<SCEVAddExpr>();
1426 new (S) SCEVAddExpr(Ops);
1427 UniqueSCEVs.InsertNode(S, IP);
1428 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +00001429}
1430
1431
Dan Gohman6c0866c2009-05-24 23:45:28 +00001432/// getMulExpr - Get a canonical multiply expression, or something simpler if
1433/// possible.
Owen Anderson372b46c2009-06-22 21:39:50 +00001434const SCEV* ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV*> &Ops) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001435 assert(!Ops.empty() && "Cannot get empty mul!");
Dan Gohmanf78a9782009-05-18 15:44:58 +00001436#ifndef NDEBUG
1437 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1438 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1439 getEffectiveSCEVType(Ops[0]->getType()) &&
1440 "SCEVMulExpr operand types don't match!");
1441#endif
Chris Lattner53e677a2004-04-02 20:23:17 +00001442
1443 // Sort by complexity, this groups all similar expression types together.
Dan Gohman72861302009-05-07 14:39:04 +00001444 GroupByComplexity(Ops, LI);
Chris Lattner53e677a2004-04-02 20:23:17 +00001445
1446 // If there are any constants, fold them together.
1447 unsigned Idx = 0;
Dan Gohman622ed672009-05-04 22:02:23 +00001448 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001449
1450 // C1*(C2+V) -> C1*C2 + C1*V
1451 if (Ops.size() == 2)
Dan Gohman622ed672009-05-04 22:02:23 +00001452 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
Chris Lattner53e677a2004-04-02 20:23:17 +00001453 if (Add->getNumOperands() == 2 &&
1454 isa<SCEVConstant>(Add->getOperand(0)))
Dan Gohman246b2562007-10-22 18:31:58 +00001455 return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)),
1456 getMulExpr(LHSC, Add->getOperand(1)));
Chris Lattner53e677a2004-04-02 20:23:17 +00001457
1458
1459 ++Idx;
Dan Gohman622ed672009-05-04 22:02:23 +00001460 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001461 // We found two constants, fold them together!
Dan Gohman64a845e2009-06-24 04:48:43 +00001462 ConstantInt *Fold = ConstantInt::get(LHSC->getValue()->getValue() *
Nick Lewycky3e630762008-02-20 06:48:22 +00001463 RHSC->getValue()->getValue());
1464 Ops[0] = getConstant(Fold);
1465 Ops.erase(Ops.begin()+1); // Erase the folded element
1466 if (Ops.size() == 1) return Ops[0];
1467 LHSC = cast<SCEVConstant>(Ops[0]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001468 }
1469
1470 // If we are left with a constant one being multiplied, strip it off.
1471 if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) {
1472 Ops.erase(Ops.begin());
1473 --Idx;
Reid Spencercae57542007-03-02 00:28:52 +00001474 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001475 // If we have a multiply of zero, it will always be zero.
1476 return Ops[0];
1477 }
1478 }
1479
1480 // Skip over the add expression until we get to a multiply.
1481 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
1482 ++Idx;
1483
1484 if (Ops.size() == 1)
1485 return Ops[0];
Misha Brukman2b37d7c2005-04-21 21:13:18 +00001486
Chris Lattner53e677a2004-04-02 20:23:17 +00001487 // If there are mul operands inline them all into this expression.
1488 if (Idx < Ops.size()) {
1489 bool DeletedMul = false;
Dan Gohman622ed672009-05-04 22:02:23 +00001490 while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001491 // If we have an mul, expand the mul operands onto the end of the operands
1492 // list.
1493 Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end());
1494 Ops.erase(Ops.begin()+Idx);
1495 DeletedMul = true;
1496 }
1497
1498 // If we deleted at least one mul, we added operands to the end of the list,
1499 // and they are not necessarily sorted. Recurse to resort and resimplify
1500 // any operands we just aquired.
1501 if (DeletedMul)
Dan Gohman246b2562007-10-22 18:31:58 +00001502 return getMulExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001503 }
1504
1505 // If there are any add recurrences in the operands list, see if any other
1506 // added values are loop invariant. If so, we can fold them into the
1507 // recurrence.
1508 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
1509 ++Idx;
1510
1511 // Scan over all recurrences, trying to fold loop invariants into them.
1512 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
1513 // Scan all of the other operands to this mul and add them to the vector if
1514 // they are loop invariant w.r.t. the recurrence.
Owen Anderson372b46c2009-06-22 21:39:50 +00001515 SmallVector<const SCEV*, 8> LIOps;
Dan Gohman35738ac2009-05-04 22:30:44 +00001516 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001517 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1518 if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
1519 LIOps.push_back(Ops[i]);
1520 Ops.erase(Ops.begin()+i);
1521 --i; --e;
1522 }
1523
1524 // If we found some loop invariants, fold them into the recurrence.
1525 if (!LIOps.empty()) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001526 // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step}
Owen Anderson372b46c2009-06-22 21:39:50 +00001527 SmallVector<const SCEV*, 4> NewOps;
Chris Lattner53e677a2004-04-02 20:23:17 +00001528 NewOps.reserve(AddRec->getNumOperands());
1529 if (LIOps.size() == 1) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001530 const SCEV *Scale = LIOps[0];
Chris Lattner53e677a2004-04-02 20:23:17 +00001531 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
Dan Gohman246b2562007-10-22 18:31:58 +00001532 NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i)));
Chris Lattner53e677a2004-04-02 20:23:17 +00001533 } else {
1534 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
Owen Anderson372b46c2009-06-22 21:39:50 +00001535 SmallVector<const SCEV*, 4> MulOps(LIOps.begin(), LIOps.end());
Chris Lattner53e677a2004-04-02 20:23:17 +00001536 MulOps.push_back(AddRec->getOperand(i));
Dan Gohman246b2562007-10-22 18:31:58 +00001537 NewOps.push_back(getMulExpr(MulOps));
Chris Lattner53e677a2004-04-02 20:23:17 +00001538 }
1539 }
1540
Owen Anderson372b46c2009-06-22 21:39:50 +00001541 const SCEV* NewRec = getAddRecExpr(NewOps, AddRec->getLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +00001542
1543 // If all of the other operands were loop invariant, we are done.
1544 if (Ops.size() == 1) return NewRec;
1545
1546 // Otherwise, multiply the folded AddRec by the non-liv parts.
1547 for (unsigned i = 0;; ++i)
1548 if (Ops[i] == AddRec) {
1549 Ops[i] = NewRec;
1550 break;
1551 }
Dan Gohman246b2562007-10-22 18:31:58 +00001552 return getMulExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001553 }
1554
1555 // Okay, if there weren't any loop invariants to be folded, check to see if
1556 // there are multiple AddRec's with the same loop induction variable being
1557 // multiplied together. If so, we can fold them.
1558 for (unsigned OtherIdx = Idx+1;
1559 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
1560 if (OtherIdx != Idx) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001561 const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001562 if (AddRec->getLoop() == OtherAddRec->getLoop()) {
1563 // F * G --> {A,+,B} * {C,+,D} --> {A*C,+,F*D + G*B + B*D}
Dan Gohman35738ac2009-05-04 22:30:44 +00001564 const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec;
Owen Anderson372b46c2009-06-22 21:39:50 +00001565 const SCEV* NewStart = getMulExpr(F->getStart(),
Chris Lattner53e677a2004-04-02 20:23:17 +00001566 G->getStart());
Owen Anderson372b46c2009-06-22 21:39:50 +00001567 const SCEV* B = F->getStepRecurrence(*this);
1568 const SCEV* D = G->getStepRecurrence(*this);
1569 const SCEV* NewStep = getAddExpr(getMulExpr(F, D),
Dan Gohman246b2562007-10-22 18:31:58 +00001570 getMulExpr(G, B),
1571 getMulExpr(B, D));
Owen Anderson372b46c2009-06-22 21:39:50 +00001572 const SCEV* NewAddRec = getAddRecExpr(NewStart, NewStep,
Dan Gohman246b2562007-10-22 18:31:58 +00001573 F->getLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +00001574 if (Ops.size() == 2) return NewAddRec;
1575
1576 Ops.erase(Ops.begin()+Idx);
1577 Ops.erase(Ops.begin()+OtherIdx-1);
1578 Ops.push_back(NewAddRec);
Dan Gohman246b2562007-10-22 18:31:58 +00001579 return getMulExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001580 }
1581 }
1582
1583 // Otherwise couldn't fold anything into this recurrence. Move onto the
1584 // next one.
1585 }
1586
1587 // Okay, it looks like we really DO need an mul expr. Check to see if we
1588 // already have one, otherwise create a new one.
Dan Gohman1c343752009-06-27 21:21:31 +00001589 FoldingSetNodeID ID;
1590 ID.AddInteger(scMulExpr);
1591 ID.AddInteger(Ops.size());
1592 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1593 ID.AddPointer(Ops[i]);
1594 void *IP = 0;
1595 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1596 SCEV *S = SCEVAllocator.Allocate<SCEVMulExpr>();
1597 new (S) SCEVMulExpr(Ops);
1598 UniqueSCEVs.InsertNode(S, IP);
1599 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +00001600}
1601
Dan Gohman6c0866c2009-05-24 23:45:28 +00001602/// getUDivExpr - Get a canonical multiply expression, or something simpler if
1603/// possible.
Dan Gohman9311ef62009-06-24 14:49:00 +00001604const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
1605 const SCEV *RHS) {
Dan Gohmanf78a9782009-05-18 15:44:58 +00001606 assert(getEffectiveSCEVType(LHS->getType()) ==
1607 getEffectiveSCEVType(RHS->getType()) &&
1608 "SCEVUDivExpr operand types don't match!");
1609
Dan Gohman622ed672009-05-04 22:02:23 +00001610 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001611 if (RHSC->getValue()->equalsInt(1))
Nick Lewycky789558d2009-01-13 09:18:58 +00001612 return LHS; // X udiv 1 --> x
Dan Gohman185cf032009-05-08 20:18:49 +00001613 if (RHSC->isZero())
1614 return getIntegerSCEV(0, LHS->getType()); // value is undefined
Chris Lattner53e677a2004-04-02 20:23:17 +00001615
Dan Gohman185cf032009-05-08 20:18:49 +00001616 // Determine if the division can be folded into the operands of
1617 // its operands.
1618 // TODO: Generalize this to non-constants by using known-bits information.
1619 const Type *Ty = LHS->getType();
1620 unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros();
1621 unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ;
1622 // For non-power-of-two values, effectively round the value up to the
1623 // nearest power of two.
1624 if (!RHSC->getValue()->getValue().isPowerOf2())
1625 ++MaxShiftAmt;
1626 const IntegerType *ExtTy =
1627 IntegerType::get(getTypeSizeInBits(Ty) + MaxShiftAmt);
1628 // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
1629 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
1630 if (const SCEVConstant *Step =
1631 dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this)))
1632 if (!Step->getValue()->getValue()
1633 .urem(RHSC->getValue()->getValue()) &&
Dan Gohmanb0285932009-05-08 23:11:16 +00001634 getZeroExtendExpr(AR, ExtTy) ==
1635 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
1636 getZeroExtendExpr(Step, ExtTy),
1637 AR->getLoop())) {
Owen Anderson372b46c2009-06-22 21:39:50 +00001638 SmallVector<const SCEV*, 4> Operands;
Dan Gohman185cf032009-05-08 20:18:49 +00001639 for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i)
1640 Operands.push_back(getUDivExpr(AR->getOperand(i), RHS));
1641 return getAddRecExpr(Operands, AR->getLoop());
1642 }
1643 // (A*B)/C --> A*(B/C) if safe and B/C can be folded.
Dan Gohmanb0285932009-05-08 23:11:16 +00001644 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
Owen Anderson372b46c2009-06-22 21:39:50 +00001645 SmallVector<const SCEV*, 4> Operands;
Dan Gohmanb0285932009-05-08 23:11:16 +00001646 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i)
1647 Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy));
1648 if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands))
Dan Gohman185cf032009-05-08 20:18:49 +00001649 // Find an operand that's safely divisible.
1650 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
Owen Anderson372b46c2009-06-22 21:39:50 +00001651 const SCEV* Op = M->getOperand(i);
1652 const SCEV* Div = getUDivExpr(Op, RHSC);
Dan Gohman185cf032009-05-08 20:18:49 +00001653 if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) {
Owen Anderson372b46c2009-06-22 21:39:50 +00001654 const SmallVectorImpl<const SCEV*> &MOperands = M->getOperands();
1655 Operands = SmallVector<const SCEV*, 4>(MOperands.begin(),
Dan Gohmana82752c2009-06-14 22:47:23 +00001656 MOperands.end());
Dan Gohman185cf032009-05-08 20:18:49 +00001657 Operands[i] = Div;
1658 return getMulExpr(Operands);
1659 }
1660 }
Dan Gohmanb0285932009-05-08 23:11:16 +00001661 }
Dan Gohman185cf032009-05-08 20:18:49 +00001662 // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded.
Dan Gohmanb0285932009-05-08 23:11:16 +00001663 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) {
Owen Anderson372b46c2009-06-22 21:39:50 +00001664 SmallVector<const SCEV*, 4> Operands;
Dan Gohmanb0285932009-05-08 23:11:16 +00001665 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i)
1666 Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy));
1667 if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) {
1668 Operands.clear();
Dan Gohman185cf032009-05-08 20:18:49 +00001669 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
Owen Anderson372b46c2009-06-22 21:39:50 +00001670 const SCEV* Op = getUDivExpr(A->getOperand(i), RHS);
Dan Gohman185cf032009-05-08 20:18:49 +00001671 if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i))
1672 break;
1673 Operands.push_back(Op);
1674 }
1675 if (Operands.size() == A->getNumOperands())
1676 return getAddExpr(Operands);
1677 }
Dan Gohmanb0285932009-05-08 23:11:16 +00001678 }
Dan Gohman185cf032009-05-08 20:18:49 +00001679
1680 // Fold if both operands are constant.
Dan Gohman622ed672009-05-04 22:02:23 +00001681 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001682 Constant *LHSCV = LHSC->getValue();
1683 Constant *RHSCV = RHSC->getValue();
Dan Gohmanb8be8b72009-06-24 00:38:39 +00001684 return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV,
1685 RHSCV)));
Chris Lattner53e677a2004-04-02 20:23:17 +00001686 }
1687 }
1688
Dan Gohman1c343752009-06-27 21:21:31 +00001689 FoldingSetNodeID ID;
1690 ID.AddInteger(scUDivExpr);
1691 ID.AddPointer(LHS);
1692 ID.AddPointer(RHS);
1693 void *IP = 0;
1694 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1695 SCEV *S = SCEVAllocator.Allocate<SCEVUDivExpr>();
1696 new (S) SCEVUDivExpr(LHS, RHS);
1697 UniqueSCEVs.InsertNode(S, IP);
1698 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +00001699}
1700
1701
Dan Gohman6c0866c2009-05-24 23:45:28 +00001702/// getAddRecExpr - Get an add recurrence expression for the specified loop.
1703/// Simplify the expression as much as possible.
Owen Anderson372b46c2009-06-22 21:39:50 +00001704const SCEV* ScalarEvolution::getAddRecExpr(const SCEV* Start,
1705 const SCEV* Step, const Loop *L) {
1706 SmallVector<const SCEV*, 4> Operands;
Chris Lattner53e677a2004-04-02 20:23:17 +00001707 Operands.push_back(Start);
Dan Gohman622ed672009-05-04 22:02:23 +00001708 if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
Chris Lattner53e677a2004-04-02 20:23:17 +00001709 if (StepChrec->getLoop() == L) {
1710 Operands.insert(Operands.end(), StepChrec->op_begin(),
1711 StepChrec->op_end());
Dan Gohman246b2562007-10-22 18:31:58 +00001712 return getAddRecExpr(Operands, L);
Chris Lattner53e677a2004-04-02 20:23:17 +00001713 }
1714
1715 Operands.push_back(Step);
Dan Gohman246b2562007-10-22 18:31:58 +00001716 return getAddRecExpr(Operands, L);
Chris Lattner53e677a2004-04-02 20:23:17 +00001717}
1718
Dan Gohman6c0866c2009-05-24 23:45:28 +00001719/// getAddRecExpr - Get an add recurrence expression for the specified loop.
1720/// Simplify the expression as much as possible.
Dan Gohman64a845e2009-06-24 04:48:43 +00001721const SCEV *
1722ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV*> &Operands,
1723 const Loop *L) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001724 if (Operands.size() == 1) return Operands[0];
Dan Gohmanf78a9782009-05-18 15:44:58 +00001725#ifndef NDEBUG
1726 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
1727 assert(getEffectiveSCEVType(Operands[i]->getType()) ==
1728 getEffectiveSCEVType(Operands[0]->getType()) &&
1729 "SCEVAddRecExpr operand types don't match!");
1730#endif
Chris Lattner53e677a2004-04-02 20:23:17 +00001731
Dan Gohmancfeb6a42008-06-18 16:23:07 +00001732 if (Operands.back()->isZero()) {
1733 Operands.pop_back();
Dan Gohman8dae1382008-09-14 17:21:12 +00001734 return getAddRecExpr(Operands, L); // {X,+,0} --> X
Dan Gohmancfeb6a42008-06-18 16:23:07 +00001735 }
Chris Lattner53e677a2004-04-02 20:23:17 +00001736
Dan Gohmand9cc7492008-08-08 18:33:12 +00001737 // Canonicalize nested AddRecs in by nesting them in order of loop depth.
Dan Gohman622ed672009-05-04 22:02:23 +00001738 if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
Dan Gohmand9cc7492008-08-08 18:33:12 +00001739 const Loop* NestedLoop = NestedAR->getLoop();
1740 if (L->getLoopDepth() < NestedLoop->getLoopDepth()) {
Owen Anderson372b46c2009-06-22 21:39:50 +00001741 SmallVector<const SCEV*, 4> NestedOperands(NestedAR->op_begin(),
Dan Gohmana82752c2009-06-14 22:47:23 +00001742 NestedAR->op_end());
Dan Gohmand9cc7492008-08-08 18:33:12 +00001743 Operands[0] = NestedAR->getStart();
Dan Gohman9a80b452009-06-26 22:36:20 +00001744 // AddRecs require their operands be loop-invariant with respect to their
1745 // loops. Don't perform this transformation if it would break this
1746 // requirement.
1747 bool AllInvariant = true;
1748 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
1749 if (!Operands[i]->isLoopInvariant(L)) {
1750 AllInvariant = false;
1751 break;
1752 }
1753 if (AllInvariant) {
1754 NestedOperands[0] = getAddRecExpr(Operands, L);
1755 AllInvariant = true;
1756 for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i)
1757 if (!NestedOperands[i]->isLoopInvariant(NestedLoop)) {
1758 AllInvariant = false;
1759 break;
1760 }
1761 if (AllInvariant)
1762 // Ok, both add recurrences are valid after the transformation.
1763 return getAddRecExpr(NestedOperands, NestedLoop);
1764 }
1765 // Reset Operands to its original state.
1766 Operands[0] = NestedAR;
Dan Gohmand9cc7492008-08-08 18:33:12 +00001767 }
1768 }
1769
Dan Gohman1c343752009-06-27 21:21:31 +00001770 FoldingSetNodeID ID;
1771 ID.AddInteger(scAddRecExpr);
1772 ID.AddInteger(Operands.size());
1773 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
1774 ID.AddPointer(Operands[i]);
1775 ID.AddPointer(L);
1776 void *IP = 0;
1777 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1778 SCEV *S = SCEVAllocator.Allocate<SCEVAddRecExpr>();
1779 new (S) SCEVAddRecExpr(Operands, L);
1780 UniqueSCEVs.InsertNode(S, IP);
1781 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +00001782}
1783
Dan Gohman9311ef62009-06-24 14:49:00 +00001784const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS,
1785 const SCEV *RHS) {
Owen Anderson372b46c2009-06-22 21:39:50 +00001786 SmallVector<const SCEV*, 2> Ops;
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001787 Ops.push_back(LHS);
1788 Ops.push_back(RHS);
1789 return getSMaxExpr(Ops);
1790}
1791
Owen Anderson372b46c2009-06-22 21:39:50 +00001792const SCEV*
1793ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV*> &Ops) {
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001794 assert(!Ops.empty() && "Cannot get empty smax!");
1795 if (Ops.size() == 1) return Ops[0];
Dan Gohmanf78a9782009-05-18 15:44:58 +00001796#ifndef NDEBUG
1797 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1798 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1799 getEffectiveSCEVType(Ops[0]->getType()) &&
1800 "SCEVSMaxExpr operand types don't match!");
1801#endif
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001802
1803 // Sort by complexity, this groups all similar expression types together.
Dan Gohman72861302009-05-07 14:39:04 +00001804 GroupByComplexity(Ops, LI);
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001805
1806 // If there are any constants, fold them together.
1807 unsigned Idx = 0;
Dan Gohman622ed672009-05-04 22:02:23 +00001808 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001809 ++Idx;
1810 assert(Idx < Ops.size());
Dan Gohman622ed672009-05-04 22:02:23 +00001811 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001812 // We found two constants, fold them together!
Nick Lewycky3e630762008-02-20 06:48:22 +00001813 ConstantInt *Fold = ConstantInt::get(
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001814 APIntOps::smax(LHSC->getValue()->getValue(),
1815 RHSC->getValue()->getValue()));
Nick Lewycky3e630762008-02-20 06:48:22 +00001816 Ops[0] = getConstant(Fold);
1817 Ops.erase(Ops.begin()+1); // Erase the folded element
1818 if (Ops.size() == 1) return Ops[0];
1819 LHSC = cast<SCEVConstant>(Ops[0]);
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001820 }
1821
Dan Gohmane5aceed2009-06-24 14:46:22 +00001822 // If we are left with a constant minimum-int, strip it off.
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001823 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) {
1824 Ops.erase(Ops.begin());
1825 --Idx;
Dan Gohmane5aceed2009-06-24 14:46:22 +00001826 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) {
1827 // If we have an smax with a constant maximum-int, it will always be
1828 // maximum-int.
1829 return Ops[0];
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001830 }
1831 }
1832
1833 if (Ops.size() == 1) return Ops[0];
1834
1835 // Find the first SMax
1836 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr)
1837 ++Idx;
1838
1839 // Check to see if one of the operands is an SMax. If so, expand its operands
1840 // onto our operand list, and recurse to simplify.
1841 if (Idx < Ops.size()) {
1842 bool DeletedSMax = false;
Dan Gohman622ed672009-05-04 22:02:23 +00001843 while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) {
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001844 Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end());
1845 Ops.erase(Ops.begin()+Idx);
1846 DeletedSMax = true;
1847 }
1848
1849 if (DeletedSMax)
1850 return getSMaxExpr(Ops);
1851 }
1852
1853 // Okay, check to see if the same value occurs in the operand list twice. If
1854 // so, delete one. Since we sorted the list, these values are required to
1855 // be adjacent.
1856 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
1857 if (Ops[i] == Ops[i+1]) { // X smax Y smax Y --> X smax Y
1858 Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
1859 --i; --e;
1860 }
1861
1862 if (Ops.size() == 1) return Ops[0];
1863
1864 assert(!Ops.empty() && "Reduced smax down to nothing!");
1865
Nick Lewycky3e630762008-02-20 06:48:22 +00001866 // Okay, it looks like we really DO need an smax expr. Check to see if we
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001867 // already have one, otherwise create a new one.
Dan Gohman1c343752009-06-27 21:21:31 +00001868 FoldingSetNodeID ID;
1869 ID.AddInteger(scSMaxExpr);
1870 ID.AddInteger(Ops.size());
1871 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1872 ID.AddPointer(Ops[i]);
1873 void *IP = 0;
1874 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1875 SCEV *S = SCEVAllocator.Allocate<SCEVSMaxExpr>();
1876 new (S) SCEVSMaxExpr(Ops);
1877 UniqueSCEVs.InsertNode(S, IP);
1878 return S;
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001879}
1880
Dan Gohman9311ef62009-06-24 14:49:00 +00001881const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS,
1882 const SCEV *RHS) {
Owen Anderson372b46c2009-06-22 21:39:50 +00001883 SmallVector<const SCEV*, 2> Ops;
Nick Lewycky3e630762008-02-20 06:48:22 +00001884 Ops.push_back(LHS);
1885 Ops.push_back(RHS);
1886 return getUMaxExpr(Ops);
1887}
1888
Owen Anderson372b46c2009-06-22 21:39:50 +00001889const SCEV*
1890ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV*> &Ops) {
Nick Lewycky3e630762008-02-20 06:48:22 +00001891 assert(!Ops.empty() && "Cannot get empty umax!");
1892 if (Ops.size() == 1) return Ops[0];
Dan Gohmanf78a9782009-05-18 15:44:58 +00001893#ifndef NDEBUG
1894 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1895 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1896 getEffectiveSCEVType(Ops[0]->getType()) &&
1897 "SCEVUMaxExpr operand types don't match!");
1898#endif
Nick Lewycky3e630762008-02-20 06:48:22 +00001899
1900 // Sort by complexity, this groups all similar expression types together.
Dan Gohman72861302009-05-07 14:39:04 +00001901 GroupByComplexity(Ops, LI);
Nick Lewycky3e630762008-02-20 06:48:22 +00001902
1903 // If there are any constants, fold them together.
1904 unsigned Idx = 0;
Dan Gohman622ed672009-05-04 22:02:23 +00001905 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Nick Lewycky3e630762008-02-20 06:48:22 +00001906 ++Idx;
1907 assert(Idx < Ops.size());
Dan Gohman622ed672009-05-04 22:02:23 +00001908 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Nick Lewycky3e630762008-02-20 06:48:22 +00001909 // We found two constants, fold them together!
1910 ConstantInt *Fold = ConstantInt::get(
1911 APIntOps::umax(LHSC->getValue()->getValue(),
1912 RHSC->getValue()->getValue()));
1913 Ops[0] = getConstant(Fold);
1914 Ops.erase(Ops.begin()+1); // Erase the folded element
1915 if (Ops.size() == 1) return Ops[0];
1916 LHSC = cast<SCEVConstant>(Ops[0]);
1917 }
1918
Dan Gohmane5aceed2009-06-24 14:46:22 +00001919 // If we are left with a constant minimum-int, strip it off.
Nick Lewycky3e630762008-02-20 06:48:22 +00001920 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) {
1921 Ops.erase(Ops.begin());
1922 --Idx;
Dan Gohmane5aceed2009-06-24 14:46:22 +00001923 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) {
1924 // If we have an umax with a constant maximum-int, it will always be
1925 // maximum-int.
1926 return Ops[0];
Nick Lewycky3e630762008-02-20 06:48:22 +00001927 }
1928 }
1929
1930 if (Ops.size() == 1) return Ops[0];
1931
1932 // Find the first UMax
1933 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr)
1934 ++Idx;
1935
1936 // Check to see if one of the operands is a UMax. If so, expand its operands
1937 // onto our operand list, and recurse to simplify.
1938 if (Idx < Ops.size()) {
1939 bool DeletedUMax = false;
Dan Gohman622ed672009-05-04 22:02:23 +00001940 while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) {
Nick Lewycky3e630762008-02-20 06:48:22 +00001941 Ops.insert(Ops.end(), UMax->op_begin(), UMax->op_end());
1942 Ops.erase(Ops.begin()+Idx);
1943 DeletedUMax = true;
1944 }
1945
1946 if (DeletedUMax)
1947 return getUMaxExpr(Ops);
1948 }
1949
1950 // Okay, check to see if the same value occurs in the operand list twice. If
1951 // so, delete one. Since we sorted the list, these values are required to
1952 // be adjacent.
1953 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
1954 if (Ops[i] == Ops[i+1]) { // X umax Y umax Y --> X umax Y
1955 Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
1956 --i; --e;
1957 }
1958
1959 if (Ops.size() == 1) return Ops[0];
1960
1961 assert(!Ops.empty() && "Reduced umax down to nothing!");
1962
1963 // Okay, it looks like we really DO need a umax expr. Check to see if we
1964 // already have one, otherwise create a new one.
Dan Gohman1c343752009-06-27 21:21:31 +00001965 FoldingSetNodeID ID;
1966 ID.AddInteger(scUMaxExpr);
1967 ID.AddInteger(Ops.size());
1968 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1969 ID.AddPointer(Ops[i]);
1970 void *IP = 0;
1971 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1972 SCEV *S = SCEVAllocator.Allocate<SCEVUMaxExpr>();
1973 new (S) SCEVUMaxExpr(Ops);
1974 UniqueSCEVs.InsertNode(S, IP);
1975 return S;
Nick Lewycky3e630762008-02-20 06:48:22 +00001976}
1977
Dan Gohman9311ef62009-06-24 14:49:00 +00001978const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS,
1979 const SCEV *RHS) {
Dan Gohmanf9a9a992009-06-22 03:18:45 +00001980 // ~smax(~x, ~y) == smin(x, y).
1981 return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
1982}
1983
Dan Gohman9311ef62009-06-24 14:49:00 +00001984const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS,
1985 const SCEV *RHS) {
Dan Gohmanf9a9a992009-06-22 03:18:45 +00001986 // ~umax(~x, ~y) == umin(x, y)
1987 return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
1988}
1989
Owen Anderson372b46c2009-06-22 21:39:50 +00001990const SCEV* ScalarEvolution::getUnknown(Value *V) {
Dan Gohman6bbcba12009-06-24 00:54:57 +00001991 // Don't attempt to do anything other than create a SCEVUnknown object
1992 // here. createSCEV only calls getUnknown after checking for all other
1993 // interesting possibilities, and any other code that calls getUnknown
1994 // is doing so in order to hide a value from SCEV canonicalization.
1995
Dan Gohman1c343752009-06-27 21:21:31 +00001996 FoldingSetNodeID ID;
1997 ID.AddInteger(scUnknown);
1998 ID.AddPointer(V);
1999 void *IP = 0;
2000 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2001 SCEV *S = SCEVAllocator.Allocate<SCEVUnknown>();
2002 new (S) SCEVUnknown(V);
2003 UniqueSCEVs.InsertNode(S, IP);
2004 return S;
Chris Lattner0a7f98c2004-04-15 15:07:24 +00002005}
2006
Chris Lattner53e677a2004-04-02 20:23:17 +00002007//===----------------------------------------------------------------------===//
Chris Lattner53e677a2004-04-02 20:23:17 +00002008// Basic SCEV Analysis and PHI Idiom Recognition Code
2009//
2010
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002011/// isSCEVable - Test if values of the given type are analyzable within
2012/// the SCEV framework. This primarily includes integer types, and it
2013/// can optionally include pointer types if the ScalarEvolution class
2014/// has access to target-specific information.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002015bool ScalarEvolution::isSCEVable(const Type *Ty) const {
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002016 // Integers are always SCEVable.
2017 if (Ty->isInteger())
2018 return true;
2019
2020 // Pointers are SCEVable if TargetData information is available
2021 // to provide pointer size information.
2022 if (isa<PointerType>(Ty))
2023 return TD != NULL;
2024
2025 // Otherwise it's not SCEVable.
2026 return false;
2027}
2028
2029/// getTypeSizeInBits - Return the size in bits of the specified type,
2030/// for which isSCEVable must return true.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002031uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const {
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002032 assert(isSCEVable(Ty) && "Type is not SCEVable!");
2033
2034 // If we have a TargetData, use it!
2035 if (TD)
2036 return TD->getTypeSizeInBits(Ty);
2037
2038 // Otherwise, we support only integer types.
2039 assert(Ty->isInteger() && "isSCEVable permitted a non-SCEVable type!");
2040 return Ty->getPrimitiveSizeInBits();
2041}
2042
2043/// getEffectiveSCEVType - Return a type with the same bitwidth as
2044/// the given type and which represents how SCEV will treat the given
2045/// type, for which isSCEVable must return true. For pointer types,
2046/// this is the pointer-sized integer type.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002047const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const {
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002048 assert(isSCEVable(Ty) && "Type is not SCEVable!");
2049
2050 if (Ty->isInteger())
2051 return Ty;
2052
2053 assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!");
2054 return TD->getIntPtrType();
Dan Gohman2d1be872009-04-16 03:18:22 +00002055}
Chris Lattner53e677a2004-04-02 20:23:17 +00002056
Owen Anderson372b46c2009-06-22 21:39:50 +00002057const SCEV* ScalarEvolution::getCouldNotCompute() {
Dan Gohman1c343752009-06-27 21:21:31 +00002058 return &CouldNotCompute;
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00002059}
2060
Dan Gohman92fa56e2009-05-04 22:20:30 +00002061/// hasSCEV - Return true if the SCEV for this value has already been
Torok Edwine3d12852009-05-01 08:33:47 +00002062/// computed.
2063bool ScalarEvolution::hasSCEV(Value *V) const {
2064 return Scalars.count(V);
2065}
2066
Chris Lattner53e677a2004-04-02 20:23:17 +00002067/// getSCEV - Return an existing SCEV if it exists, otherwise analyze the
2068/// expression and create a new one.
Owen Anderson372b46c2009-06-22 21:39:50 +00002069const SCEV* ScalarEvolution::getSCEV(Value *V) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002070 assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
Chris Lattner53e677a2004-04-02 20:23:17 +00002071
Owen Anderson372b46c2009-06-22 21:39:50 +00002072 std::map<SCEVCallbackVH, const SCEV*>::iterator I = Scalars.find(V);
Chris Lattner53e677a2004-04-02 20:23:17 +00002073 if (I != Scalars.end()) return I->second;
Owen Anderson372b46c2009-06-22 21:39:50 +00002074 const SCEV* S = createSCEV(V);
Dan Gohman35738ac2009-05-04 22:30:44 +00002075 Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S));
Chris Lattner53e677a2004-04-02 20:23:17 +00002076 return S;
2077}
2078
Dan Gohman6bbcba12009-06-24 00:54:57 +00002079/// getIntegerSCEV - Given a SCEVable type, create a constant for the
Dan Gohman2d1be872009-04-16 03:18:22 +00002080/// specified signed integer value and return a SCEV for the constant.
Owen Anderson372b46c2009-06-22 21:39:50 +00002081const SCEV* ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) {
Dan Gohman6bbcba12009-06-24 00:54:57 +00002082 const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty));
2083 return getConstant(ConstantInt::get(ITy, Val));
Dan Gohman2d1be872009-04-16 03:18:22 +00002084}
2085
2086/// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
2087///
Owen Anderson372b46c2009-06-22 21:39:50 +00002088const SCEV* ScalarEvolution::getNegativeSCEV(const SCEV* V) {
Dan Gohman622ed672009-05-04 22:02:23 +00002089 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
Dan Gohmanb8be8b72009-06-24 00:38:39 +00002090 return getConstant(cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue())));
Dan Gohman2d1be872009-04-16 03:18:22 +00002091
2092 const Type *Ty = V->getType();
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002093 Ty = getEffectiveSCEVType(Ty);
2094 return getMulExpr(V, getConstant(ConstantInt::getAllOnesValue(Ty)));
Dan Gohman2d1be872009-04-16 03:18:22 +00002095}
2096
2097/// getNotSCEV - Return a SCEV corresponding to ~V = -1-V
Owen Anderson372b46c2009-06-22 21:39:50 +00002098const SCEV* ScalarEvolution::getNotSCEV(const SCEV* V) {
Dan Gohman622ed672009-05-04 22:02:23 +00002099 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
Dan Gohmanb8be8b72009-06-24 00:38:39 +00002100 return getConstant(cast<ConstantInt>(ConstantExpr::getNot(VC->getValue())));
Dan Gohman2d1be872009-04-16 03:18:22 +00002101
2102 const Type *Ty = V->getType();
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002103 Ty = getEffectiveSCEVType(Ty);
Owen Anderson372b46c2009-06-22 21:39:50 +00002104 const SCEV* AllOnes = getConstant(ConstantInt::getAllOnesValue(Ty));
Dan Gohman2d1be872009-04-16 03:18:22 +00002105 return getMinusSCEV(AllOnes, V);
2106}
2107
2108/// getMinusSCEV - Return a SCEV corresponding to LHS - RHS.
2109///
Dan Gohman9311ef62009-06-24 14:49:00 +00002110const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS,
2111 const SCEV *RHS) {
Dan Gohman2d1be872009-04-16 03:18:22 +00002112 // X - Y --> X + -Y
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002113 return getAddExpr(LHS, getNegativeSCEV(RHS));
Dan Gohman2d1be872009-04-16 03:18:22 +00002114}
2115
2116/// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the
2117/// input value to the specified type. If the type must be extended, it is zero
2118/// extended.
Owen Anderson372b46c2009-06-22 21:39:50 +00002119const SCEV*
2120ScalarEvolution::getTruncateOrZeroExtend(const SCEV* V,
Nick Lewycky5cd28fa2009-04-23 05:15:08 +00002121 const Type *Ty) {
Dan Gohman2d1be872009-04-16 03:18:22 +00002122 const Type *SrcTy = V->getType();
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002123 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2124 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
Dan Gohman2d1be872009-04-16 03:18:22 +00002125 "Cannot truncate or zero extend with non-integer arguments!");
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002126 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
Dan Gohman2d1be872009-04-16 03:18:22 +00002127 return V; // No conversion
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002128 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002129 return getTruncateExpr(V, Ty);
2130 return getZeroExtendExpr(V, Ty);
Dan Gohman2d1be872009-04-16 03:18:22 +00002131}
2132
2133/// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the
2134/// input value to the specified type. If the type must be extended, it is sign
2135/// extended.
Owen Anderson372b46c2009-06-22 21:39:50 +00002136const SCEV*
2137ScalarEvolution::getTruncateOrSignExtend(const SCEV* V,
Nick Lewycky5cd28fa2009-04-23 05:15:08 +00002138 const Type *Ty) {
Dan Gohman2d1be872009-04-16 03:18:22 +00002139 const Type *SrcTy = V->getType();
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002140 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2141 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
Dan Gohman2d1be872009-04-16 03:18:22 +00002142 "Cannot truncate or zero extend with non-integer arguments!");
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002143 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
Dan Gohman2d1be872009-04-16 03:18:22 +00002144 return V; // No conversion
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002145 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002146 return getTruncateExpr(V, Ty);
2147 return getSignExtendExpr(V, Ty);
Dan Gohman2d1be872009-04-16 03:18:22 +00002148}
2149
Dan Gohman467c4302009-05-13 03:46:30 +00002150/// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the
2151/// input value to the specified type. If the type must be extended, it is zero
2152/// extended. The conversion must not be narrowing.
Owen Anderson372b46c2009-06-22 21:39:50 +00002153const SCEV*
2154ScalarEvolution::getNoopOrZeroExtend(const SCEV* V, const Type *Ty) {
Dan Gohman467c4302009-05-13 03:46:30 +00002155 const Type *SrcTy = V->getType();
2156 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2157 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
2158 "Cannot noop or zero extend with non-integer arguments!");
2159 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2160 "getNoopOrZeroExtend cannot truncate!");
2161 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2162 return V; // No conversion
2163 return getZeroExtendExpr(V, Ty);
2164}
2165
2166/// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the
2167/// input value to the specified type. If the type must be extended, it is sign
2168/// extended. The conversion must not be narrowing.
Owen Anderson372b46c2009-06-22 21:39:50 +00002169const SCEV*
2170ScalarEvolution::getNoopOrSignExtend(const SCEV* V, const Type *Ty) {
Dan Gohman467c4302009-05-13 03:46:30 +00002171 const Type *SrcTy = V->getType();
2172 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2173 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
2174 "Cannot noop or sign extend with non-integer arguments!");
2175 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2176 "getNoopOrSignExtend cannot truncate!");
2177 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2178 return V; // No conversion
2179 return getSignExtendExpr(V, Ty);
2180}
2181
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00002182/// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of
2183/// the input value to the specified type. If the type must be extended,
2184/// it is extended with unspecified bits. The conversion must not be
2185/// narrowing.
Owen Anderson372b46c2009-06-22 21:39:50 +00002186const SCEV*
2187ScalarEvolution::getNoopOrAnyExtend(const SCEV* V, const Type *Ty) {
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00002188 const Type *SrcTy = V->getType();
2189 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2190 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
2191 "Cannot noop or any extend with non-integer arguments!");
2192 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2193 "getNoopOrAnyExtend cannot truncate!");
2194 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2195 return V; // No conversion
2196 return getAnyExtendExpr(V, Ty);
2197}
2198
Dan Gohman467c4302009-05-13 03:46:30 +00002199/// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the
2200/// input value to the specified type. The conversion must not be widening.
Owen Anderson372b46c2009-06-22 21:39:50 +00002201const SCEV*
2202ScalarEvolution::getTruncateOrNoop(const SCEV* V, const Type *Ty) {
Dan Gohman467c4302009-05-13 03:46:30 +00002203 const Type *SrcTy = V->getType();
2204 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2205 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
2206 "Cannot truncate or noop with non-integer arguments!");
2207 assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) &&
2208 "getTruncateOrNoop cannot extend!");
2209 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2210 return V; // No conversion
2211 return getTruncateExpr(V, Ty);
2212}
2213
Dan Gohmana334aa72009-06-22 00:31:57 +00002214/// getUMaxFromMismatchedTypes - Promote the operands to the wider of
2215/// the types using zero-extension, and then perform a umax operation
2216/// with them.
Dan Gohman9311ef62009-06-24 14:49:00 +00002217const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS,
2218 const SCEV *RHS) {
Owen Anderson372b46c2009-06-22 21:39:50 +00002219 const SCEV* PromotedLHS = LHS;
2220 const SCEV* PromotedRHS = RHS;
Dan Gohmana334aa72009-06-22 00:31:57 +00002221
2222 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
2223 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
2224 else
2225 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
2226
2227 return getUMaxExpr(PromotedLHS, PromotedRHS);
2228}
2229
Dan Gohmanc9759e82009-06-22 15:03:27 +00002230/// getUMinFromMismatchedTypes - Promote the operands to the wider of
2231/// the types using zero-extension, and then perform a umin operation
2232/// with them.
Dan Gohman9311ef62009-06-24 14:49:00 +00002233const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS,
2234 const SCEV *RHS) {
Owen Anderson372b46c2009-06-22 21:39:50 +00002235 const SCEV* PromotedLHS = LHS;
2236 const SCEV* PromotedRHS = RHS;
Dan Gohmanc9759e82009-06-22 15:03:27 +00002237
2238 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
2239 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
2240 else
2241 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
2242
2243 return getUMinExpr(PromotedLHS, PromotedRHS);
2244}
2245
Chris Lattner4dc534c2005-02-13 04:37:18 +00002246/// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value for
2247/// the specified instruction and replaces any references to the symbolic value
2248/// SymName with the specified value. This is used during PHI resolution.
Dan Gohman64a845e2009-06-24 04:48:43 +00002249void
2250ScalarEvolution::ReplaceSymbolicValueWithConcrete(Instruction *I,
2251 const SCEV *SymName,
2252 const SCEV *NewVal) {
Owen Anderson372b46c2009-06-22 21:39:50 +00002253 std::map<SCEVCallbackVH, const SCEV*>::iterator SI =
Dan Gohman35738ac2009-05-04 22:30:44 +00002254 Scalars.find(SCEVCallbackVH(I, this));
Chris Lattner4dc534c2005-02-13 04:37:18 +00002255 if (SI == Scalars.end()) return;
Chris Lattner53e677a2004-04-02 20:23:17 +00002256
Owen Anderson372b46c2009-06-22 21:39:50 +00002257 const SCEV* NV =
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002258 SI->second->replaceSymbolicValuesWithConcrete(SymName, NewVal, *this);
Chris Lattner4dc534c2005-02-13 04:37:18 +00002259 if (NV == SI->second) return; // No change.
2260
2261 SI->second = NV; // Update the scalars map!
2262
2263 // Any instruction values that use this instruction might also need to be
2264 // updated!
2265 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
2266 UI != E; ++UI)
2267 ReplaceSymbolicValueWithConcrete(cast<Instruction>(*UI), SymName, NewVal);
2268}
Chris Lattner53e677a2004-04-02 20:23:17 +00002269
2270/// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in
2271/// a loop header, making it a potential recurrence, or it doesn't.
2272///
Owen Anderson372b46c2009-06-22 21:39:50 +00002273const SCEV* ScalarEvolution::createNodeForPHI(PHINode *PN) {
Chris Lattner53e677a2004-04-02 20:23:17 +00002274 if (PN->getNumIncomingValues() == 2) // The loops have been canonicalized.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002275 if (const Loop *L = LI->getLoopFor(PN->getParent()))
Chris Lattner53e677a2004-04-02 20:23:17 +00002276 if (L->getHeader() == PN->getParent()) {
2277 // If it lives in the loop header, it has two incoming values, one
2278 // from outside the loop, and one from inside.
2279 unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0));
2280 unsigned BackEdge = IncomingEdge^1;
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002281
Chris Lattner53e677a2004-04-02 20:23:17 +00002282 // While we are analyzing this PHI node, handle its value symbolically.
Owen Anderson372b46c2009-06-22 21:39:50 +00002283 const SCEV* SymbolicName = getUnknown(PN);
Chris Lattner53e677a2004-04-02 20:23:17 +00002284 assert(Scalars.find(PN) == Scalars.end() &&
2285 "PHI node already processed?");
Dan Gohman35738ac2009-05-04 22:30:44 +00002286 Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName));
Chris Lattner53e677a2004-04-02 20:23:17 +00002287
2288 // Using this symbolic name for the PHI, analyze the value coming around
2289 // the back-edge.
Owen Anderson372b46c2009-06-22 21:39:50 +00002290 const SCEV* BEValue = getSCEV(PN->getIncomingValue(BackEdge));
Chris Lattner53e677a2004-04-02 20:23:17 +00002291
2292 // NOTE: If BEValue is loop invariant, we know that the PHI node just
2293 // has a special value for the first iteration of the loop.
2294
2295 // If the value coming around the backedge is an add with the symbolic
2296 // value we just inserted, then we found a simple induction variable!
Dan Gohman622ed672009-05-04 22:02:23 +00002297 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00002298 // If there is a single occurrence of the symbolic value, replace it
2299 // with a recurrence.
2300 unsigned FoundIndex = Add->getNumOperands();
2301 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
2302 if (Add->getOperand(i) == SymbolicName)
2303 if (FoundIndex == e) {
2304 FoundIndex = i;
2305 break;
2306 }
2307
2308 if (FoundIndex != Add->getNumOperands()) {
2309 // Create an add with everything but the specified operand.
Owen Anderson372b46c2009-06-22 21:39:50 +00002310 SmallVector<const SCEV*, 8> Ops;
Chris Lattner53e677a2004-04-02 20:23:17 +00002311 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
2312 if (i != FoundIndex)
2313 Ops.push_back(Add->getOperand(i));
Owen Anderson372b46c2009-06-22 21:39:50 +00002314 const SCEV* Accum = getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00002315
2316 // This is not a valid addrec if the step amount is varying each
2317 // loop iteration, but is not itself an addrec in this loop.
2318 if (Accum->isLoopInvariant(L) ||
2319 (isa<SCEVAddRecExpr>(Accum) &&
2320 cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
Dan Gohman64a845e2009-06-24 04:48:43 +00002321 const SCEV *StartVal =
2322 getSCEV(PN->getIncomingValue(IncomingEdge));
2323 const SCEV *PHISCEV =
2324 getAddRecExpr(StartVal, Accum, L);
Chris Lattner53e677a2004-04-02 20:23:17 +00002325
2326 // Okay, for the entire analysis of this edge we assumed the PHI
2327 // to be symbolic. We now need to go back and update all of the
2328 // entries for the scalars that use the PHI (except for the PHI
2329 // itself) to use the new analyzed value instead of the "symbolic"
2330 // value.
Chris Lattner4dc534c2005-02-13 04:37:18 +00002331 ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV);
Chris Lattner53e677a2004-04-02 20:23:17 +00002332 return PHISCEV;
2333 }
2334 }
Dan Gohman622ed672009-05-04 22:02:23 +00002335 } else if (const SCEVAddRecExpr *AddRec =
2336 dyn_cast<SCEVAddRecExpr>(BEValue)) {
Chris Lattner97156e72006-04-26 18:34:07 +00002337 // Otherwise, this could be a loop like this:
2338 // i = 0; for (j = 1; ..; ++j) { .... i = j; }
2339 // In this case, j = {1,+,1} and BEValue is j.
2340 // Because the other in-value of i (0) fits the evolution of BEValue
2341 // i really is an addrec evolution.
2342 if (AddRec->getLoop() == L && AddRec->isAffine()) {
Owen Anderson372b46c2009-06-22 21:39:50 +00002343 const SCEV* StartVal = getSCEV(PN->getIncomingValue(IncomingEdge));
Chris Lattner97156e72006-04-26 18:34:07 +00002344
2345 // If StartVal = j.start - j.stride, we can use StartVal as the
2346 // initial step of the addrec evolution.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002347 if (StartVal == getMinusSCEV(AddRec->getOperand(0),
Dan Gohman246b2562007-10-22 18:31:58 +00002348 AddRec->getOperand(1))) {
Dan Gohman64a845e2009-06-24 04:48:43 +00002349 const SCEV* PHISCEV =
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002350 getAddRecExpr(StartVal, AddRec->getOperand(1), L);
Chris Lattner97156e72006-04-26 18:34:07 +00002351
2352 // Okay, for the entire analysis of this edge we assumed the PHI
2353 // to be symbolic. We now need to go back and update all of the
2354 // entries for the scalars that use the PHI (except for the PHI
2355 // itself) to use the new analyzed value instead of the "symbolic"
2356 // value.
2357 ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV);
2358 return PHISCEV;
2359 }
2360 }
Chris Lattner53e677a2004-04-02 20:23:17 +00002361 }
2362
2363 return SymbolicName;
2364 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002365
Chris Lattner53e677a2004-04-02 20:23:17 +00002366 // If it's not a loop phi, we can't handle it yet.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002367 return getUnknown(PN);
Chris Lattner53e677a2004-04-02 20:23:17 +00002368}
2369
Dan Gohman26466c02009-05-08 20:26:55 +00002370/// createNodeForGEP - Expand GEP instructions into add and multiply
2371/// operations. This allows them to be analyzed by regular SCEV code.
2372///
Owen Anderson372b46c2009-06-22 21:39:50 +00002373const SCEV* ScalarEvolution::createNodeForGEP(User *GEP) {
Dan Gohman26466c02009-05-08 20:26:55 +00002374
2375 const Type *IntPtrTy = TD->getIntPtrType();
Dan Gohmane810b0d2009-05-08 20:36:47 +00002376 Value *Base = GEP->getOperand(0);
Dan Gohmanc63a6272009-05-09 00:14:52 +00002377 // Don't attempt to analyze GEPs over unsized objects.
2378 if (!cast<PointerType>(Base->getType())->getElementType()->isSized())
2379 return getUnknown(GEP);
Owen Anderson372b46c2009-06-22 21:39:50 +00002380 const SCEV* TotalOffset = getIntegerSCEV(0, IntPtrTy);
Dan Gohmane810b0d2009-05-08 20:36:47 +00002381 gep_type_iterator GTI = gep_type_begin(GEP);
2382 for (GetElementPtrInst::op_iterator I = next(GEP->op_begin()),
2383 E = GEP->op_end();
Dan Gohman26466c02009-05-08 20:26:55 +00002384 I != E; ++I) {
2385 Value *Index = *I;
2386 // Compute the (potentially symbolic) offset in bytes for this index.
2387 if (const StructType *STy = dyn_cast<StructType>(*GTI++)) {
2388 // For a struct, add the member offset.
2389 const StructLayout &SL = *TD->getStructLayout(STy);
2390 unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
2391 uint64_t Offset = SL.getElementOffset(FieldNo);
2392 TotalOffset = getAddExpr(TotalOffset,
2393 getIntegerSCEV(Offset, IntPtrTy));
2394 } else {
2395 // For an array, add the element offset, explicitly scaled.
Owen Anderson372b46c2009-06-22 21:39:50 +00002396 const SCEV* LocalOffset = getSCEV(Index);
Dan Gohman26466c02009-05-08 20:26:55 +00002397 if (!isa<PointerType>(LocalOffset->getType()))
2398 // Getelementptr indicies are signed.
2399 LocalOffset = getTruncateOrSignExtend(LocalOffset,
2400 IntPtrTy);
2401 LocalOffset =
2402 getMulExpr(LocalOffset,
Duncan Sands777d2302009-05-09 07:06:46 +00002403 getIntegerSCEV(TD->getTypeAllocSize(*GTI),
Dan Gohman26466c02009-05-08 20:26:55 +00002404 IntPtrTy));
2405 TotalOffset = getAddExpr(TotalOffset, LocalOffset);
2406 }
2407 }
2408 return getAddExpr(getSCEV(Base), TotalOffset);
2409}
2410
Nick Lewycky83bb0052007-11-22 07:59:40 +00002411/// GetMinTrailingZeros - Determine the minimum number of zero bits that S is
2412/// guaranteed to end in (at every loop iteration). It is, at the same time,
2413/// the minimum number of times S is divisible by 2. For example, given {4,+,8}
2414/// it returns 2. If S is guaranteed to be 0, it returns the bitwidth of S.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002415uint32_t
Owen Anderson372b46c2009-06-22 21:39:50 +00002416ScalarEvolution::GetMinTrailingZeros(const SCEV* S) {
Dan Gohman622ed672009-05-04 22:02:23 +00002417 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
Chris Lattner8314a0c2007-11-23 22:36:49 +00002418 return C->getValue()->getValue().countTrailingZeros();
Chris Lattnera17f0392006-12-12 02:26:09 +00002419
Dan Gohman622ed672009-05-04 22:02:23 +00002420 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S))
Dan Gohman2c364ad2009-06-19 23:29:04 +00002421 return std::min(GetMinTrailingZeros(T->getOperand()),
2422 (uint32_t)getTypeSizeInBits(T->getType()));
Nick Lewycky83bb0052007-11-22 07:59:40 +00002423
Dan Gohman622ed672009-05-04 22:02:23 +00002424 if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) {
Dan Gohman2c364ad2009-06-19 23:29:04 +00002425 uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
2426 return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
2427 getTypeSizeInBits(E->getType()) : OpRes;
Nick Lewycky83bb0052007-11-22 07:59:40 +00002428 }
2429
Dan Gohman622ed672009-05-04 22:02:23 +00002430 if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) {
Dan Gohman2c364ad2009-06-19 23:29:04 +00002431 uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
2432 return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
2433 getTypeSizeInBits(E->getType()) : OpRes;
Nick Lewycky83bb0052007-11-22 07:59:40 +00002434 }
2435
Dan Gohman622ed672009-05-04 22:02:23 +00002436 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
Nick Lewycky83bb0052007-11-22 07:59:40 +00002437 // The result is the min of all operands results.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002438 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
Nick Lewycky83bb0052007-11-22 07:59:40 +00002439 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman2c364ad2009-06-19 23:29:04 +00002440 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
Nick Lewycky83bb0052007-11-22 07:59:40 +00002441 return MinOpRes;
Chris Lattnera17f0392006-12-12 02:26:09 +00002442 }
2443
Dan Gohman622ed672009-05-04 22:02:23 +00002444 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
Nick Lewycky83bb0052007-11-22 07:59:40 +00002445 // The result is the sum of all operands results.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002446 uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0));
2447 uint32_t BitWidth = getTypeSizeInBits(M->getType());
Nick Lewycky83bb0052007-11-22 07:59:40 +00002448 for (unsigned i = 1, e = M->getNumOperands();
2449 SumOpRes != BitWidth && i != e; ++i)
Dan Gohman2c364ad2009-06-19 23:29:04 +00002450 SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)),
Nick Lewycky83bb0052007-11-22 07:59:40 +00002451 BitWidth);
2452 return SumOpRes;
Chris Lattnera17f0392006-12-12 02:26:09 +00002453 }
Nick Lewycky83bb0052007-11-22 07:59:40 +00002454
Dan Gohman622ed672009-05-04 22:02:23 +00002455 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
Nick Lewycky83bb0052007-11-22 07:59:40 +00002456 // The result is the min of all operands results.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002457 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
Nick Lewycky83bb0052007-11-22 07:59:40 +00002458 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman2c364ad2009-06-19 23:29:04 +00002459 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
Nick Lewycky83bb0052007-11-22 07:59:40 +00002460 return MinOpRes;
Chris Lattnera17f0392006-12-12 02:26:09 +00002461 }
Nick Lewycky83bb0052007-11-22 07:59:40 +00002462
Dan Gohman622ed672009-05-04 22:02:23 +00002463 if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) {
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002464 // The result is the min of all operands results.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002465 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002466 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman2c364ad2009-06-19 23:29:04 +00002467 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002468 return MinOpRes;
2469 }
2470
Dan Gohman622ed672009-05-04 22:02:23 +00002471 if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) {
Nick Lewycky3e630762008-02-20 06:48:22 +00002472 // The result is the min of all operands results.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002473 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
Nick Lewycky3e630762008-02-20 06:48:22 +00002474 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman2c364ad2009-06-19 23:29:04 +00002475 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
Nick Lewycky3e630762008-02-20 06:48:22 +00002476 return MinOpRes;
2477 }
2478
Dan Gohman2c364ad2009-06-19 23:29:04 +00002479 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2480 // For a SCEVUnknown, ask ValueTracking.
2481 unsigned BitWidth = getTypeSizeInBits(U->getType());
2482 APInt Mask = APInt::getAllOnesValue(BitWidth);
2483 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
2484 ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones);
2485 return Zeros.countTrailingOnes();
2486 }
2487
2488 // SCEVUDivExpr
Nick Lewycky83bb0052007-11-22 07:59:40 +00002489 return 0;
Chris Lattnera17f0392006-12-12 02:26:09 +00002490}
Chris Lattner53e677a2004-04-02 20:23:17 +00002491
Dan Gohman2c364ad2009-06-19 23:29:04 +00002492uint32_t
Owen Anderson372b46c2009-06-22 21:39:50 +00002493ScalarEvolution::GetMinLeadingZeros(const SCEV* S) {
Dan Gohman2c364ad2009-06-19 23:29:04 +00002494 // TODO: Handle other SCEV expression types here.
2495
2496 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
2497 return C->getValue()->getValue().countLeadingZeros();
2498
2499 if (const SCEVZeroExtendExpr *C = dyn_cast<SCEVZeroExtendExpr>(S)) {
2500 // A zero-extension cast adds zero bits.
2501 return GetMinLeadingZeros(C->getOperand()) +
2502 (getTypeSizeInBits(C->getType()) -
2503 getTypeSizeInBits(C->getOperand()->getType()));
2504 }
2505
2506 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2507 // For a SCEVUnknown, ask ValueTracking.
2508 unsigned BitWidth = getTypeSizeInBits(U->getType());
2509 APInt Mask = APInt::getAllOnesValue(BitWidth);
2510 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
2511 ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD);
2512 return Zeros.countLeadingOnes();
2513 }
2514
2515 return 1;
2516}
2517
2518uint32_t
Owen Anderson372b46c2009-06-22 21:39:50 +00002519ScalarEvolution::GetMinSignBits(const SCEV* S) {
Dan Gohman2c364ad2009-06-19 23:29:04 +00002520 // TODO: Handle other SCEV expression types here.
2521
2522 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) {
2523 const APInt &A = C->getValue()->getValue();
2524 return A.isNegative() ? A.countLeadingOnes() :
2525 A.countLeadingZeros();
2526 }
2527
2528 if (const SCEVSignExtendExpr *C = dyn_cast<SCEVSignExtendExpr>(S)) {
2529 // A sign-extension cast adds sign bits.
2530 return GetMinSignBits(C->getOperand()) +
2531 (getTypeSizeInBits(C->getType()) -
2532 getTypeSizeInBits(C->getOperand()->getType()));
2533 }
2534
Dan Gohman62849c02009-06-24 01:05:09 +00002535 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
2536 unsigned BitWidth = getTypeSizeInBits(A->getType());
2537
2538 // Special case decrementing a value (ADD X, -1):
2539 if (const SCEVConstant *CRHS = dyn_cast<SCEVConstant>(A->getOperand(0)))
2540 if (CRHS->isAllOnesValue()) {
2541 SmallVector<const SCEV *, 4> OtherOps(A->op_begin() + 1, A->op_end());
2542 const SCEV *OtherOpsAdd = getAddExpr(OtherOps);
2543 unsigned LZ = GetMinLeadingZeros(OtherOpsAdd);
2544
2545 // If the input is known to be 0 or 1, the output is 0/-1, which is all
2546 // sign bits set.
2547 if (LZ == BitWidth - 1)
2548 return BitWidth;
2549
2550 // If we are subtracting one from a positive number, there is no carry
2551 // out of the result.
2552 if (LZ > 0)
2553 return GetMinSignBits(OtherOpsAdd);
2554 }
2555
2556 // Add can have at most one carry bit. Thus we know that the output
2557 // is, at worst, one more bit than the inputs.
2558 unsigned Min = BitWidth;
2559 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
2560 unsigned N = GetMinSignBits(A->getOperand(i));
2561 Min = std::min(Min, N) - 1;
2562 if (Min == 0) return 1;
2563 }
2564 return 1;
2565 }
2566
Dan Gohman2c364ad2009-06-19 23:29:04 +00002567 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2568 // For a SCEVUnknown, ask ValueTracking.
2569 return ComputeNumSignBits(U->getValue(), TD);
2570 }
2571
2572 return 1;
2573}
2574
Chris Lattner53e677a2004-04-02 20:23:17 +00002575/// createSCEV - We know that there is no SCEV for the specified value.
2576/// Analyze the expression.
2577///
Owen Anderson372b46c2009-06-22 21:39:50 +00002578const SCEV* ScalarEvolution::createSCEV(Value *V) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002579 if (!isSCEVable(V->getType()))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002580 return getUnknown(V);
Dan Gohman2d1be872009-04-16 03:18:22 +00002581
Dan Gohman6c459a22008-06-22 19:56:46 +00002582 unsigned Opcode = Instruction::UserOp1;
2583 if (Instruction *I = dyn_cast<Instruction>(V))
2584 Opcode = I->getOpcode();
2585 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
2586 Opcode = CE->getOpcode();
Dan Gohman6bbcba12009-06-24 00:54:57 +00002587 else if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
2588 return getConstant(CI);
2589 else if (isa<ConstantPointerNull>(V))
2590 return getIntegerSCEV(0, V->getType());
2591 else if (isa<UndefValue>(V))
2592 return getIntegerSCEV(0, V->getType());
Dan Gohman6c459a22008-06-22 19:56:46 +00002593 else
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002594 return getUnknown(V);
Chris Lattner2811f2a2007-04-02 05:41:38 +00002595
Dan Gohman6c459a22008-06-22 19:56:46 +00002596 User *U = cast<User>(V);
2597 switch (Opcode) {
2598 case Instruction::Add:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002599 return getAddExpr(getSCEV(U->getOperand(0)),
2600 getSCEV(U->getOperand(1)));
Dan Gohman6c459a22008-06-22 19:56:46 +00002601 case Instruction::Mul:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002602 return getMulExpr(getSCEV(U->getOperand(0)),
2603 getSCEV(U->getOperand(1)));
Dan Gohman6c459a22008-06-22 19:56:46 +00002604 case Instruction::UDiv:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002605 return getUDivExpr(getSCEV(U->getOperand(0)),
2606 getSCEV(U->getOperand(1)));
Dan Gohman6c459a22008-06-22 19:56:46 +00002607 case Instruction::Sub:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002608 return getMinusSCEV(getSCEV(U->getOperand(0)),
2609 getSCEV(U->getOperand(1)));
Dan Gohman4ee29af2009-04-21 02:26:00 +00002610 case Instruction::And:
2611 // For an expression like x&255 that merely masks off the high bits,
2612 // use zext(trunc(x)) as the SCEV expression.
2613 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman2c73d5f2009-04-25 17:05:40 +00002614 if (CI->isNullValue())
2615 return getSCEV(U->getOperand(1));
Dan Gohmand6c32952009-04-27 01:41:10 +00002616 if (CI->isAllOnesValue())
2617 return getSCEV(U->getOperand(0));
Dan Gohman4ee29af2009-04-21 02:26:00 +00002618 const APInt &A = CI->getValue();
Dan Gohman61ffa8e2009-06-16 19:52:01 +00002619
2620 // Instcombine's ShrinkDemandedConstant may strip bits out of
2621 // constants, obscuring what would otherwise be a low-bits mask.
2622 // Use ComputeMaskedBits to compute what ShrinkDemandedConstant
2623 // knew about to reconstruct a low-bits mask value.
2624 unsigned LZ = A.countLeadingZeros();
2625 unsigned BitWidth = A.getBitWidth();
2626 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
2627 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
2628 ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD);
2629
2630 APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ);
2631
Dan Gohmanfc3641b2009-06-17 23:54:37 +00002632 if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask))
Dan Gohman4ee29af2009-04-21 02:26:00 +00002633 return
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002634 getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)),
Dan Gohman61ffa8e2009-06-16 19:52:01 +00002635 IntegerType::get(BitWidth - LZ)),
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002636 U->getType());
Dan Gohman4ee29af2009-04-21 02:26:00 +00002637 }
2638 break;
Dan Gohman61ffa8e2009-06-16 19:52:01 +00002639
Dan Gohman6c459a22008-06-22 19:56:46 +00002640 case Instruction::Or:
2641 // If the RHS of the Or is a constant, we may have something like:
2642 // X*4+1 which got turned into X*4|1. Handle this as an Add so loop
2643 // optimizations will transparently handle this case.
2644 //
2645 // In order for this transformation to be safe, the LHS must be of the
2646 // form X*(2^n) and the Or constant must be less than 2^n.
2647 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Owen Anderson372b46c2009-06-22 21:39:50 +00002648 const SCEV* LHS = getSCEV(U->getOperand(0));
Dan Gohman6c459a22008-06-22 19:56:46 +00002649 const APInt &CIVal = CI->getValue();
Dan Gohman2c364ad2009-06-19 23:29:04 +00002650 if (GetMinTrailingZeros(LHS) >=
Dan Gohman6c459a22008-06-22 19:56:46 +00002651 (CIVal.getBitWidth() - CIVal.countLeadingZeros()))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002652 return getAddExpr(LHS, getSCEV(U->getOperand(1)));
Chris Lattner53e677a2004-04-02 20:23:17 +00002653 }
Dan Gohman6c459a22008-06-22 19:56:46 +00002654 break;
2655 case Instruction::Xor:
Dan Gohman6c459a22008-06-22 19:56:46 +00002656 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Nick Lewycky01eaf802008-07-07 06:15:49 +00002657 // If the RHS of the xor is a signbit, then this is just an add.
2658 // Instcombine turns add of signbit into xor as a strength reduction step.
Dan Gohman6c459a22008-06-22 19:56:46 +00002659 if (CI->getValue().isSignBit())
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002660 return getAddExpr(getSCEV(U->getOperand(0)),
2661 getSCEV(U->getOperand(1)));
Nick Lewycky01eaf802008-07-07 06:15:49 +00002662
2663 // If the RHS of xor is -1, then this is a not operation.
Dan Gohman0bac95e2009-05-18 16:17:44 +00002664 if (CI->isAllOnesValue())
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002665 return getNotSCEV(getSCEV(U->getOperand(0)));
Dan Gohman10978bd2009-05-18 16:29:04 +00002666
2667 // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask.
2668 // This is a variant of the check for xor with -1, and it handles
2669 // the case where instcombine has trimmed non-demanded bits out
2670 // of an xor with -1.
2671 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0)))
2672 if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1)))
2673 if (BO->getOpcode() == Instruction::And &&
2674 LCI->getValue() == CI->getValue())
2675 if (const SCEVZeroExtendExpr *Z =
Dan Gohman3034c102009-06-17 01:22:39 +00002676 dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) {
Dan Gohman82052832009-06-18 00:00:20 +00002677 const Type *UTy = U->getType();
Owen Anderson372b46c2009-06-22 21:39:50 +00002678 const SCEV* Z0 = Z->getOperand();
Dan Gohman82052832009-06-18 00:00:20 +00002679 const Type *Z0Ty = Z0->getType();
2680 unsigned Z0TySize = getTypeSizeInBits(Z0Ty);
2681
2682 // If C is a low-bits mask, the zero extend is zerving to
2683 // mask off the high bits. Complement the operand and
2684 // re-apply the zext.
2685 if (APIntOps::isMask(Z0TySize, CI->getValue()))
2686 return getZeroExtendExpr(getNotSCEV(Z0), UTy);
2687
2688 // If C is a single bit, it may be in the sign-bit position
2689 // before the zero-extend. In this case, represent the xor
2690 // using an add, which is equivalent, and re-apply the zext.
2691 APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize);
2692 if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() &&
2693 Trunc.isSignBit())
2694 return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)),
2695 UTy);
Dan Gohman3034c102009-06-17 01:22:39 +00002696 }
Dan Gohman6c459a22008-06-22 19:56:46 +00002697 }
2698 break;
2699
2700 case Instruction::Shl:
2701 // Turn shift left of a constant amount into a multiply.
2702 if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
2703 uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
2704 Constant *X = ConstantInt::get(
2705 APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002706 return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X));
Dan Gohman6c459a22008-06-22 19:56:46 +00002707 }
2708 break;
2709
Nick Lewycky01eaf802008-07-07 06:15:49 +00002710 case Instruction::LShr:
Nick Lewycky789558d2009-01-13 09:18:58 +00002711 // Turn logical shift right of a constant into a unsigned divide.
Nick Lewycky01eaf802008-07-07 06:15:49 +00002712 if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
2713 uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
2714 Constant *X = ConstantInt::get(
2715 APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002716 return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X));
Nick Lewycky01eaf802008-07-07 06:15:49 +00002717 }
2718 break;
2719
Dan Gohman4ee29af2009-04-21 02:26:00 +00002720 case Instruction::AShr:
2721 // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression.
2722 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1)))
2723 if (Instruction *L = dyn_cast<Instruction>(U->getOperand(0)))
2724 if (L->getOpcode() == Instruction::Shl &&
2725 L->getOperand(1) == U->getOperand(1)) {
Dan Gohman2c73d5f2009-04-25 17:05:40 +00002726 unsigned BitWidth = getTypeSizeInBits(U->getType());
2727 uint64_t Amt = BitWidth - CI->getZExtValue();
2728 if (Amt == BitWidth)
2729 return getSCEV(L->getOperand(0)); // shift by zero --> noop
2730 if (Amt > BitWidth)
2731 return getIntegerSCEV(0, U->getType()); // value is undefined
Dan Gohman4ee29af2009-04-21 02:26:00 +00002732 return
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002733 getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)),
Dan Gohman2c73d5f2009-04-25 17:05:40 +00002734 IntegerType::get(Amt)),
Dan Gohman4ee29af2009-04-21 02:26:00 +00002735 U->getType());
2736 }
2737 break;
2738
Dan Gohman6c459a22008-06-22 19:56:46 +00002739 case Instruction::Trunc:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002740 return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman6c459a22008-06-22 19:56:46 +00002741
2742 case Instruction::ZExt:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002743 return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman6c459a22008-06-22 19:56:46 +00002744
2745 case Instruction::SExt:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002746 return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman6c459a22008-06-22 19:56:46 +00002747
2748 case Instruction::BitCast:
2749 // BitCasts are no-op casts so we just eliminate the cast.
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002750 if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType()))
Dan Gohman6c459a22008-06-22 19:56:46 +00002751 return getSCEV(U->getOperand(0));
2752 break;
2753
Dan Gohman2d1be872009-04-16 03:18:22 +00002754 case Instruction::IntToPtr:
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002755 if (!TD) break; // Without TD we can't analyze pointers.
Dan Gohman2d1be872009-04-16 03:18:22 +00002756 return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)),
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002757 TD->getIntPtrType());
Dan Gohman2d1be872009-04-16 03:18:22 +00002758
2759 case Instruction::PtrToInt:
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002760 if (!TD) break; // Without TD we can't analyze pointers.
Dan Gohman2d1be872009-04-16 03:18:22 +00002761 return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)),
2762 U->getType());
2763
Dan Gohman26466c02009-05-08 20:26:55 +00002764 case Instruction::GetElementPtr:
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002765 if (!TD) break; // Without TD we can't analyze pointers.
Dan Gohmanfb791602009-05-08 20:58:38 +00002766 return createNodeForGEP(U);
Dan Gohman2d1be872009-04-16 03:18:22 +00002767
Dan Gohman6c459a22008-06-22 19:56:46 +00002768 case Instruction::PHI:
2769 return createNodeForPHI(cast<PHINode>(U));
2770
2771 case Instruction::Select:
2772 // This could be a smax or umax that was lowered earlier.
2773 // Try to recover it.
2774 if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) {
2775 Value *LHS = ICI->getOperand(0);
2776 Value *RHS = ICI->getOperand(1);
2777 switch (ICI->getPredicate()) {
2778 case ICmpInst::ICMP_SLT:
2779 case ICmpInst::ICMP_SLE:
2780 std::swap(LHS, RHS);
2781 // fall through
2782 case ICmpInst::ICMP_SGT:
2783 case ICmpInst::ICMP_SGE:
2784 if (LHS == U->getOperand(1) && RHS == U->getOperand(2))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002785 return getSMaxExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman6c459a22008-06-22 19:56:46 +00002786 else if (LHS == U->getOperand(2) && RHS == U->getOperand(1))
Dan Gohmanf9a9a992009-06-22 03:18:45 +00002787 return getSMinExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman6c459a22008-06-22 19:56:46 +00002788 break;
2789 case ICmpInst::ICMP_ULT:
2790 case ICmpInst::ICMP_ULE:
2791 std::swap(LHS, RHS);
2792 // fall through
2793 case ICmpInst::ICMP_UGT:
2794 case ICmpInst::ICMP_UGE:
2795 if (LHS == U->getOperand(1) && RHS == U->getOperand(2))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002796 return getUMaxExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman6c459a22008-06-22 19:56:46 +00002797 else if (LHS == U->getOperand(2) && RHS == U->getOperand(1))
Dan Gohmanf9a9a992009-06-22 03:18:45 +00002798 return getUMinExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman6c459a22008-06-22 19:56:46 +00002799 break;
Dan Gohman30fb5122009-06-18 20:21:07 +00002800 case ICmpInst::ICMP_NE:
2801 // n != 0 ? n : 1 -> umax(n, 1)
2802 if (LHS == U->getOperand(1) &&
2803 isa<ConstantInt>(U->getOperand(2)) &&
2804 cast<ConstantInt>(U->getOperand(2))->isOne() &&
2805 isa<ConstantInt>(RHS) &&
2806 cast<ConstantInt>(RHS)->isZero())
2807 return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(2)));
2808 break;
2809 case ICmpInst::ICMP_EQ:
2810 // n == 0 ? 1 : n -> umax(n, 1)
2811 if (LHS == U->getOperand(2) &&
2812 isa<ConstantInt>(U->getOperand(1)) &&
2813 cast<ConstantInt>(U->getOperand(1))->isOne() &&
2814 isa<ConstantInt>(RHS) &&
2815 cast<ConstantInt>(RHS)->isZero())
2816 return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(1)));
2817 break;
Dan Gohman6c459a22008-06-22 19:56:46 +00002818 default:
2819 break;
2820 }
2821 }
2822
2823 default: // We cannot analyze this expression.
2824 break;
Chris Lattner53e677a2004-04-02 20:23:17 +00002825 }
2826
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002827 return getUnknown(V);
Chris Lattner53e677a2004-04-02 20:23:17 +00002828}
2829
2830
2831
2832//===----------------------------------------------------------------------===//
2833// Iteration Count Computation Code
2834//
2835
Dan Gohman46bdfb02009-02-24 18:55:53 +00002836/// getBackedgeTakenCount - If the specified loop has a predictable
2837/// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
2838/// object. The backedge-taken count is the number of times the loop header
2839/// will be branched to from within the loop. This is one less than the
2840/// trip count of the loop, since it doesn't count the first iteration,
2841/// when the header is branched to from outside the loop.
2842///
2843/// Note that it is not valid to call this method on a loop without a
2844/// loop-invariant backedge-taken count (see
2845/// hasLoopInvariantBackedgeTakenCount).
2846///
Owen Anderson372b46c2009-06-22 21:39:50 +00002847const SCEV* ScalarEvolution::getBackedgeTakenCount(const Loop *L) {
Dan Gohmana1af7572009-04-30 20:47:05 +00002848 return getBackedgeTakenInfo(L).Exact;
2849}
2850
2851/// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except
2852/// return the least SCEV value that is known never to be less than the
2853/// actual backedge taken count.
Owen Anderson372b46c2009-06-22 21:39:50 +00002854const SCEV* ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) {
Dan Gohmana1af7572009-04-30 20:47:05 +00002855 return getBackedgeTakenInfo(L).Max;
2856}
2857
2858const ScalarEvolution::BackedgeTakenInfo &
2859ScalarEvolution::getBackedgeTakenInfo(const Loop *L) {
Dan Gohman01ecca22009-04-27 20:16:15 +00002860 // Initially insert a CouldNotCompute for this loop. If the insertion
2861 // succeeds, procede to actually compute a backedge-taken count and
2862 // update the value. The temporary CouldNotCompute value tells SCEV
2863 // code elsewhere that it shouldn't attempt to request a new
2864 // backedge-taken count, which could result in infinite recursion.
Dan Gohmana1af7572009-04-30 20:47:05 +00002865 std::pair<std::map<const Loop*, BackedgeTakenInfo>::iterator, bool> Pair =
Dan Gohman01ecca22009-04-27 20:16:15 +00002866 BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute()));
2867 if (Pair.second) {
Dan Gohmana1af7572009-04-30 20:47:05 +00002868 BackedgeTakenInfo ItCount = ComputeBackedgeTakenCount(L);
Dan Gohman1c343752009-06-27 21:21:31 +00002869 if (ItCount.Exact != getCouldNotCompute()) {
Dan Gohmana1af7572009-04-30 20:47:05 +00002870 assert(ItCount.Exact->isLoopInvariant(L) &&
2871 ItCount.Max->isLoopInvariant(L) &&
Chris Lattner53e677a2004-04-02 20:23:17 +00002872 "Computed trip count isn't loop invariant for loop!");
2873 ++NumTripCountsComputed;
Dan Gohman01ecca22009-04-27 20:16:15 +00002874
Dan Gohman01ecca22009-04-27 20:16:15 +00002875 // Update the value in the map.
2876 Pair.first->second = ItCount;
Dan Gohmana334aa72009-06-22 00:31:57 +00002877 } else {
Dan Gohman1c343752009-06-27 21:21:31 +00002878 if (ItCount.Max != getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00002879 // Update the value in the map.
2880 Pair.first->second = ItCount;
2881 if (isa<PHINode>(L->getHeader()->begin()))
2882 // Only count loops that have phi nodes as not being computable.
2883 ++NumTripCountsNotComputed;
Chris Lattner53e677a2004-04-02 20:23:17 +00002884 }
Dan Gohmana1af7572009-04-30 20:47:05 +00002885
2886 // Now that we know more about the trip count for this loop, forget any
2887 // existing SCEV values for PHI nodes in this loop since they are only
2888 // conservative estimates made without the benefit
2889 // of trip count information.
2890 if (ItCount.hasAnyInfo())
Dan Gohmanfb7d35f2009-05-02 17:43:35 +00002891 forgetLoopPHIs(L);
Chris Lattner53e677a2004-04-02 20:23:17 +00002892 }
Dan Gohman01ecca22009-04-27 20:16:15 +00002893 return Pair.first->second;
Chris Lattner53e677a2004-04-02 20:23:17 +00002894}
2895
Dan Gohman46bdfb02009-02-24 18:55:53 +00002896/// forgetLoopBackedgeTakenCount - This method should be called by the
Dan Gohman60f8a632009-02-17 20:49:49 +00002897/// client when it has changed a loop in a way that may effect
Dan Gohman46bdfb02009-02-24 18:55:53 +00002898/// ScalarEvolution's ability to compute a trip count, or if the loop
2899/// is deleted.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002900void ScalarEvolution::forgetLoopBackedgeTakenCount(const Loop *L) {
Dan Gohman46bdfb02009-02-24 18:55:53 +00002901 BackedgeTakenCounts.erase(L);
Dan Gohmanfb7d35f2009-05-02 17:43:35 +00002902 forgetLoopPHIs(L);
2903}
2904
2905/// forgetLoopPHIs - Delete the memoized SCEVs associated with the
2906/// PHI nodes in the given loop. This is used when the trip count of
2907/// the loop may have changed.
2908void ScalarEvolution::forgetLoopPHIs(const Loop *L) {
Dan Gohman35738ac2009-05-04 22:30:44 +00002909 BasicBlock *Header = L->getHeader();
2910
Dan Gohmanefb9fbf2009-05-12 01:27:58 +00002911 // Push all Loop-header PHIs onto the Worklist stack, except those
2912 // that are presently represented via a SCEVUnknown. SCEVUnknown for
2913 // a PHI either means that it has an unrecognized structure, or it's
2914 // a PHI that's in the progress of being computed by createNodeForPHI.
2915 // In the former case, additional loop trip count information isn't
2916 // going to change anything. In the later case, createNodeForPHI will
2917 // perform the necessary updates on its own when it gets to that point.
Dan Gohman35738ac2009-05-04 22:30:44 +00002918 SmallVector<Instruction *, 16> Worklist;
2919 for (BasicBlock::iterator I = Header->begin();
Dan Gohmanefb9fbf2009-05-12 01:27:58 +00002920 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohman64a845e2009-06-24 04:48:43 +00002921 std::map<SCEVCallbackVH, const SCEV*>::iterator It =
2922 Scalars.find((Value*)I);
Dan Gohmanefb9fbf2009-05-12 01:27:58 +00002923 if (It != Scalars.end() && !isa<SCEVUnknown>(It->second))
2924 Worklist.push_back(PN);
2925 }
Dan Gohman35738ac2009-05-04 22:30:44 +00002926
2927 while (!Worklist.empty()) {
2928 Instruction *I = Worklist.pop_back_val();
2929 if (Scalars.erase(I))
2930 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
2931 UI != UE; ++UI)
2932 Worklist.push_back(cast<Instruction>(UI));
2933 }
Dan Gohman60f8a632009-02-17 20:49:49 +00002934}
2935
Dan Gohman46bdfb02009-02-24 18:55:53 +00002936/// ComputeBackedgeTakenCount - Compute the number of times the backedge
2937/// of the specified loop will execute.
Dan Gohmana1af7572009-04-30 20:47:05 +00002938ScalarEvolution::BackedgeTakenInfo
2939ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) {
Dan Gohmana334aa72009-06-22 00:31:57 +00002940 SmallVector<BasicBlock*, 8> ExitingBlocks;
2941 L->getExitingBlocks(ExitingBlocks);
Chris Lattner53e677a2004-04-02 20:23:17 +00002942
Dan Gohmana334aa72009-06-22 00:31:57 +00002943 // Examine all exits and pick the most conservative values.
Dan Gohman1c343752009-06-27 21:21:31 +00002944 const SCEV* BECount = getCouldNotCompute();
2945 const SCEV* MaxBECount = getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00002946 bool CouldNotComputeBECount = false;
Dan Gohmana334aa72009-06-22 00:31:57 +00002947 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
2948 BackedgeTakenInfo NewBTI =
2949 ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]);
Chris Lattner53e677a2004-04-02 20:23:17 +00002950
Dan Gohman1c343752009-06-27 21:21:31 +00002951 if (NewBTI.Exact == getCouldNotCompute()) {
Dan Gohmana334aa72009-06-22 00:31:57 +00002952 // We couldn't compute an exact value for this exit, so
Dan Gohmand32f5bf2009-06-22 21:10:22 +00002953 // we won't be able to compute an exact value for the loop.
Dan Gohmana334aa72009-06-22 00:31:57 +00002954 CouldNotComputeBECount = true;
Dan Gohman1c343752009-06-27 21:21:31 +00002955 BECount = getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00002956 } else if (!CouldNotComputeBECount) {
Dan Gohman1c343752009-06-27 21:21:31 +00002957 if (BECount == getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00002958 BECount = NewBTI.Exact;
Dan Gohmana334aa72009-06-22 00:31:57 +00002959 else
Dan Gohman40a5a1b2009-06-24 01:18:18 +00002960 BECount = getUMinFromMismatchedTypes(BECount, NewBTI.Exact);
Dan Gohmana334aa72009-06-22 00:31:57 +00002961 }
Dan Gohman1c343752009-06-27 21:21:31 +00002962 if (MaxBECount == getCouldNotCompute())
Dan Gohman40a5a1b2009-06-24 01:18:18 +00002963 MaxBECount = NewBTI.Max;
Dan Gohman1c343752009-06-27 21:21:31 +00002964 else if (NewBTI.Max != getCouldNotCompute())
Dan Gohman40a5a1b2009-06-24 01:18:18 +00002965 MaxBECount = getUMinFromMismatchedTypes(MaxBECount, NewBTI.Max);
Dan Gohmana334aa72009-06-22 00:31:57 +00002966 }
2967
2968 return BackedgeTakenInfo(BECount, MaxBECount);
2969}
2970
2971/// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge
2972/// of the specified loop will execute if it exits via the specified block.
2973ScalarEvolution::BackedgeTakenInfo
2974ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L,
2975 BasicBlock *ExitingBlock) {
2976
2977 // Okay, we've chosen an exiting block. See what condition causes us to
2978 // exit at this block.
Chris Lattner53e677a2004-04-02 20:23:17 +00002979 //
2980 // FIXME: we should be able to handle switch instructions (with a single exit)
Chris Lattner53e677a2004-04-02 20:23:17 +00002981 BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
Dan Gohman1c343752009-06-27 21:21:31 +00002982 if (ExitBr == 0) return getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00002983 assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!");
Dan Gohman64a845e2009-06-24 04:48:43 +00002984
Chris Lattner8b0e3602007-01-07 02:24:26 +00002985 // At this point, we know we have a conditional branch that determines whether
2986 // the loop is exited. However, we don't know if the branch is executed each
2987 // time through the loop. If not, then the execution count of the branch will
2988 // not be equal to the trip count of the loop.
2989 //
2990 // Currently we check for this by checking to see if the Exit branch goes to
2991 // the loop header. If so, we know it will always execute the same number of
Chris Lattner192e4032007-01-14 01:24:47 +00002992 // times as the loop. We also handle the case where the exit block *is* the
Dan Gohmana334aa72009-06-22 00:31:57 +00002993 // loop header. This is common for un-rotated loops.
2994 //
2995 // If both of those tests fail, walk up the unique predecessor chain to the
2996 // header, stopping if there is an edge that doesn't exit the loop. If the
2997 // header is reached, the execution count of the branch will be equal to the
2998 // trip count of the loop.
2999 //
3000 // More extensive analysis could be done to handle more cases here.
3001 //
Chris Lattner8b0e3602007-01-07 02:24:26 +00003002 if (ExitBr->getSuccessor(0) != L->getHeader() &&
Chris Lattner192e4032007-01-14 01:24:47 +00003003 ExitBr->getSuccessor(1) != L->getHeader() &&
Dan Gohmana334aa72009-06-22 00:31:57 +00003004 ExitBr->getParent() != L->getHeader()) {
3005 // The simple checks failed, try climbing the unique predecessor chain
3006 // up to the header.
3007 bool Ok = false;
3008 for (BasicBlock *BB = ExitBr->getParent(); BB; ) {
3009 BasicBlock *Pred = BB->getUniquePredecessor();
3010 if (!Pred)
Dan Gohman1c343752009-06-27 21:21:31 +00003011 return getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003012 TerminatorInst *PredTerm = Pred->getTerminator();
3013 for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) {
3014 BasicBlock *PredSucc = PredTerm->getSuccessor(i);
3015 if (PredSucc == BB)
3016 continue;
3017 // If the predecessor has a successor that isn't BB and isn't
3018 // outside the loop, assume the worst.
3019 if (L->contains(PredSucc))
Dan Gohman1c343752009-06-27 21:21:31 +00003020 return getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003021 }
3022 if (Pred == L->getHeader()) {
3023 Ok = true;
3024 break;
3025 }
3026 BB = Pred;
3027 }
3028 if (!Ok)
Dan Gohman1c343752009-06-27 21:21:31 +00003029 return getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003030 }
3031
3032 // Procede to the next level to examine the exit condition expression.
3033 return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(),
3034 ExitBr->getSuccessor(0),
3035 ExitBr->getSuccessor(1));
3036}
3037
3038/// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the
3039/// backedge of the specified loop will execute if its exit condition
3040/// were a conditional branch of ExitCond, TBB, and FBB.
3041ScalarEvolution::BackedgeTakenInfo
3042ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L,
3043 Value *ExitCond,
3044 BasicBlock *TBB,
3045 BasicBlock *FBB) {
Dan Gohman40a5a1b2009-06-24 01:18:18 +00003046 // Check if the controlling expression for this loop is an And or Or.
Dan Gohmana334aa72009-06-22 00:31:57 +00003047 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) {
3048 if (BO->getOpcode() == Instruction::And) {
3049 // Recurse on the operands of the and.
3050 BackedgeTakenInfo BTI0 =
3051 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB);
3052 BackedgeTakenInfo BTI1 =
3053 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB);
Dan Gohman1c343752009-06-27 21:21:31 +00003054 const SCEV* BECount = getCouldNotCompute();
3055 const SCEV* MaxBECount = getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003056 if (L->contains(TBB)) {
3057 // Both conditions must be true for the loop to continue executing.
3058 // Choose the less conservative count.
Dan Gohman1c343752009-06-27 21:21:31 +00003059 if (BTI0.Exact == getCouldNotCompute() ||
3060 BTI1.Exact == getCouldNotCompute())
3061 BECount = getCouldNotCompute();
Dan Gohman60e9b072009-06-22 15:09:28 +00003062 else
3063 BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
Dan Gohman1c343752009-06-27 21:21:31 +00003064 if (BTI0.Max == getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003065 MaxBECount = BTI1.Max;
Dan Gohman1c343752009-06-27 21:21:31 +00003066 else if (BTI1.Max == getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003067 MaxBECount = BTI0.Max;
Dan Gohman60e9b072009-06-22 15:09:28 +00003068 else
3069 MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max);
Dan Gohmana334aa72009-06-22 00:31:57 +00003070 } else {
3071 // Both conditions must be true for the loop to exit.
3072 assert(L->contains(FBB) && "Loop block has no successor in loop!");
Dan Gohman1c343752009-06-27 21:21:31 +00003073 if (BTI0.Exact != getCouldNotCompute() &&
3074 BTI1.Exact != getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003075 BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
Dan Gohman1c343752009-06-27 21:21:31 +00003076 if (BTI0.Max != getCouldNotCompute() &&
3077 BTI1.Max != getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003078 MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max);
3079 }
3080
3081 return BackedgeTakenInfo(BECount, MaxBECount);
3082 }
3083 if (BO->getOpcode() == Instruction::Or) {
3084 // Recurse on the operands of the or.
3085 BackedgeTakenInfo BTI0 =
3086 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB);
3087 BackedgeTakenInfo BTI1 =
3088 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB);
Dan Gohman1c343752009-06-27 21:21:31 +00003089 const SCEV* BECount = getCouldNotCompute();
3090 const SCEV* MaxBECount = getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003091 if (L->contains(FBB)) {
3092 // Both conditions must be false for the loop to continue executing.
3093 // Choose the less conservative count.
Dan Gohman1c343752009-06-27 21:21:31 +00003094 if (BTI0.Exact == getCouldNotCompute() ||
3095 BTI1.Exact == getCouldNotCompute())
3096 BECount = getCouldNotCompute();
Dan Gohman60e9b072009-06-22 15:09:28 +00003097 else
3098 BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
Dan Gohman1c343752009-06-27 21:21:31 +00003099 if (BTI0.Max == getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003100 MaxBECount = BTI1.Max;
Dan Gohman1c343752009-06-27 21:21:31 +00003101 else if (BTI1.Max == getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003102 MaxBECount = BTI0.Max;
Dan Gohman60e9b072009-06-22 15:09:28 +00003103 else
3104 MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max);
Dan Gohmana334aa72009-06-22 00:31:57 +00003105 } else {
3106 // Both conditions must be false for the loop to exit.
3107 assert(L->contains(TBB) && "Loop block has no successor in loop!");
Dan Gohman1c343752009-06-27 21:21:31 +00003108 if (BTI0.Exact != getCouldNotCompute() &&
3109 BTI1.Exact != getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003110 BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
Dan Gohman1c343752009-06-27 21:21:31 +00003111 if (BTI0.Max != getCouldNotCompute() &&
3112 BTI1.Max != getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003113 MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max);
3114 }
3115
3116 return BackedgeTakenInfo(BECount, MaxBECount);
3117 }
3118 }
3119
3120 // With an icmp, it may be feasible to compute an exact backedge-taken count.
3121 // Procede to the next level to examine the icmp.
3122 if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond))
3123 return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB);
Reid Spencere4d87aa2006-12-23 06:05:41 +00003124
Eli Friedman361e54d2009-05-09 12:32:42 +00003125 // If it's not an integer or pointer comparison then compute it the hard way.
Dan Gohmana334aa72009-06-22 00:31:57 +00003126 return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB));
3127}
3128
3129/// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the
3130/// backedge of the specified loop will execute if its exit condition
3131/// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB.
3132ScalarEvolution::BackedgeTakenInfo
3133ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L,
3134 ICmpInst *ExitCond,
3135 BasicBlock *TBB,
3136 BasicBlock *FBB) {
Chris Lattner53e677a2004-04-02 20:23:17 +00003137
Reid Spencere4d87aa2006-12-23 06:05:41 +00003138 // If the condition was exit on true, convert the condition to exit on false
3139 ICmpInst::Predicate Cond;
Dan Gohmana334aa72009-06-22 00:31:57 +00003140 if (!L->contains(FBB))
Reid Spencere4d87aa2006-12-23 06:05:41 +00003141 Cond = ExitCond->getPredicate();
Chris Lattner673e02b2004-10-12 01:49:27 +00003142 else
Reid Spencere4d87aa2006-12-23 06:05:41 +00003143 Cond = ExitCond->getInversePredicate();
Chris Lattner673e02b2004-10-12 01:49:27 +00003144
3145 // Handle common loops like: for (X = "string"; *X; ++X)
3146 if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0)))
3147 if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) {
Owen Anderson372b46c2009-06-22 21:39:50 +00003148 const SCEV* ItCnt =
Dan Gohman46bdfb02009-02-24 18:55:53 +00003149 ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond);
Dan Gohmana334aa72009-06-22 00:31:57 +00003150 if (!isa<SCEVCouldNotCompute>(ItCnt)) {
3151 unsigned BitWidth = getTypeSizeInBits(ItCnt->getType());
3152 return BackedgeTakenInfo(ItCnt,
3153 isa<SCEVConstant>(ItCnt) ? ItCnt :
3154 getConstant(APInt::getMaxValue(BitWidth)-1));
3155 }
Chris Lattner673e02b2004-10-12 01:49:27 +00003156 }
3157
Owen Anderson372b46c2009-06-22 21:39:50 +00003158 const SCEV* LHS = getSCEV(ExitCond->getOperand(0));
3159 const SCEV* RHS = getSCEV(ExitCond->getOperand(1));
Chris Lattner53e677a2004-04-02 20:23:17 +00003160
3161 // Try to evaluate any dependencies out of the loop.
Dan Gohmand594e6f2009-05-24 23:25:42 +00003162 LHS = getSCEVAtScope(LHS, L);
3163 RHS = getSCEVAtScope(RHS, L);
Chris Lattner53e677a2004-04-02 20:23:17 +00003164
Dan Gohman64a845e2009-06-24 04:48:43 +00003165 // At this point, we would like to compute how many iterations of the
Reid Spencere4d87aa2006-12-23 06:05:41 +00003166 // loop the predicate will return true for these inputs.
Dan Gohman70ff4cf2008-09-16 18:52:57 +00003167 if (LHS->isLoopInvariant(L) && !RHS->isLoopInvariant(L)) {
3168 // If there is a loop-invariant, force it into the RHS.
Chris Lattner53e677a2004-04-02 20:23:17 +00003169 std::swap(LHS, RHS);
Reid Spencere4d87aa2006-12-23 06:05:41 +00003170 Cond = ICmpInst::getSwappedPredicate(Cond);
Chris Lattner53e677a2004-04-02 20:23:17 +00003171 }
3172
Chris Lattner53e677a2004-04-02 20:23:17 +00003173 // If we have a comparison of a chrec against a constant, try to use value
3174 // ranges to answer this query.
Dan Gohman622ed672009-05-04 22:02:23 +00003175 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
3176 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
Chris Lattner53e677a2004-04-02 20:23:17 +00003177 if (AddRec->getLoop() == L) {
Eli Friedman361e54d2009-05-09 12:32:42 +00003178 // Form the constant range.
3179 ConstantRange CompRange(
3180 ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue()));
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003181
Owen Anderson372b46c2009-06-22 21:39:50 +00003182 const SCEV* Ret = AddRec->getNumIterationsInRange(CompRange, *this);
Eli Friedman361e54d2009-05-09 12:32:42 +00003183 if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
Chris Lattner53e677a2004-04-02 20:23:17 +00003184 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003185
Chris Lattner53e677a2004-04-02 20:23:17 +00003186 switch (Cond) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00003187 case ICmpInst::ICMP_NE: { // while (X != Y)
Chris Lattner53e677a2004-04-02 20:23:17 +00003188 // Convert to: while (X-Y != 0)
Owen Anderson372b46c2009-06-22 21:39:50 +00003189 const SCEV* TC = HowFarToZero(getMinusSCEV(LHS, RHS), L);
Reid Spencere4d87aa2006-12-23 06:05:41 +00003190 if (!isa<SCEVCouldNotCompute>(TC)) return TC;
Chris Lattner53e677a2004-04-02 20:23:17 +00003191 break;
Reid Spencere4d87aa2006-12-23 06:05:41 +00003192 }
3193 case ICmpInst::ICMP_EQ: {
Chris Lattner53e677a2004-04-02 20:23:17 +00003194 // Convert to: while (X-Y == 0) // while (X == Y)
Owen Anderson372b46c2009-06-22 21:39:50 +00003195 const SCEV* TC = HowFarToNonZero(getMinusSCEV(LHS, RHS), L);
Reid Spencere4d87aa2006-12-23 06:05:41 +00003196 if (!isa<SCEVCouldNotCompute>(TC)) return TC;
Chris Lattner53e677a2004-04-02 20:23:17 +00003197 break;
Reid Spencere4d87aa2006-12-23 06:05:41 +00003198 }
3199 case ICmpInst::ICMP_SLT: {
Dan Gohmana1af7572009-04-30 20:47:05 +00003200 BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true);
3201 if (BTI.hasAnyInfo()) return BTI;
Chris Lattnerdb25de42005-08-15 23:33:51 +00003202 break;
Reid Spencere4d87aa2006-12-23 06:05:41 +00003203 }
3204 case ICmpInst::ICMP_SGT: {
Dan Gohmana1af7572009-04-30 20:47:05 +00003205 BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS),
3206 getNotSCEV(RHS), L, true);
3207 if (BTI.hasAnyInfo()) return BTI;
Nick Lewyckyd6dac0e2007-08-06 19:21:00 +00003208 break;
3209 }
3210 case ICmpInst::ICMP_ULT: {
Dan Gohmana1af7572009-04-30 20:47:05 +00003211 BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false);
3212 if (BTI.hasAnyInfo()) return BTI;
Nick Lewyckyd6dac0e2007-08-06 19:21:00 +00003213 break;
3214 }
3215 case ICmpInst::ICMP_UGT: {
Dan Gohmana1af7572009-04-30 20:47:05 +00003216 BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS),
3217 getNotSCEV(RHS), L, false);
3218 if (BTI.hasAnyInfo()) return BTI;
Chris Lattnerdb25de42005-08-15 23:33:51 +00003219 break;
Reid Spencere4d87aa2006-12-23 06:05:41 +00003220 }
Chris Lattner53e677a2004-04-02 20:23:17 +00003221 default:
Chris Lattnerd18d9dc2004-04-02 20:26:46 +00003222#if 0
Dan Gohmanb7ef7292009-04-21 00:47:46 +00003223 errs() << "ComputeBackedgeTakenCount ";
Chris Lattner53e677a2004-04-02 20:23:17 +00003224 if (ExitCond->getOperand(0)->getType()->isUnsigned())
Dan Gohmanb7ef7292009-04-21 00:47:46 +00003225 errs() << "[unsigned] ";
3226 errs() << *LHS << " "
Dan Gohman64a845e2009-06-24 04:48:43 +00003227 << Instruction::getOpcodeName(Instruction::ICmp)
Reid Spencere4d87aa2006-12-23 06:05:41 +00003228 << " " << *RHS << "\n";
Chris Lattnerd18d9dc2004-04-02 20:26:46 +00003229#endif
Chris Lattnere34c0b42004-04-03 00:43:03 +00003230 break;
Chris Lattner53e677a2004-04-02 20:23:17 +00003231 }
Dan Gohman46bdfb02009-02-24 18:55:53 +00003232 return
Dan Gohmana334aa72009-06-22 00:31:57 +00003233 ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB));
Chris Lattner7980fb92004-04-17 18:36:24 +00003234}
3235
Chris Lattner673e02b2004-10-12 01:49:27 +00003236static ConstantInt *
Dan Gohman246b2562007-10-22 18:31:58 +00003237EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C,
3238 ScalarEvolution &SE) {
Owen Anderson372b46c2009-06-22 21:39:50 +00003239 const SCEV* InVal = SE.getConstant(C);
3240 const SCEV* Val = AddRec->evaluateAtIteration(InVal, SE);
Chris Lattner673e02b2004-10-12 01:49:27 +00003241 assert(isa<SCEVConstant>(Val) &&
3242 "Evaluation of SCEV at constant didn't fold correctly?");
3243 return cast<SCEVConstant>(Val)->getValue();
3244}
3245
3246/// GetAddressedElementFromGlobal - Given a global variable with an initializer
3247/// and a GEP expression (missing the pointer index) indexing into it, return
3248/// the addressed element of the initializer or null if the index expression is
3249/// invalid.
3250static Constant *
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003251GetAddressedElementFromGlobal(GlobalVariable *GV,
Chris Lattner673e02b2004-10-12 01:49:27 +00003252 const std::vector<ConstantInt*> &Indices) {
3253 Constant *Init = GV->getInitializer();
3254 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
Reid Spencerb83eb642006-10-20 07:07:24 +00003255 uint64_t Idx = Indices[i]->getZExtValue();
Chris Lattner673e02b2004-10-12 01:49:27 +00003256 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) {
3257 assert(Idx < CS->getNumOperands() && "Bad struct index!");
3258 Init = cast<Constant>(CS->getOperand(Idx));
3259 } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) {
3260 if (Idx >= CA->getNumOperands()) return 0; // Bogus program
3261 Init = cast<Constant>(CA->getOperand(Idx));
3262 } else if (isa<ConstantAggregateZero>(Init)) {
3263 if (const StructType *STy = dyn_cast<StructType>(Init->getType())) {
3264 assert(Idx < STy->getNumElements() && "Bad struct index!");
3265 Init = Constant::getNullValue(STy->getElementType(Idx));
3266 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) {
3267 if (Idx >= ATy->getNumElements()) return 0; // Bogus program
3268 Init = Constant::getNullValue(ATy->getElementType());
3269 } else {
3270 assert(0 && "Unknown constant aggregate type!");
3271 }
3272 return 0;
3273 } else {
3274 return 0; // Unknown initializer type
3275 }
3276 }
3277 return Init;
3278}
3279
Dan Gohman46bdfb02009-02-24 18:55:53 +00003280/// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of
3281/// 'icmp op load X, cst', try to see if we can compute the backedge
3282/// execution count.
Dan Gohman64a845e2009-06-24 04:48:43 +00003283const SCEV *
3284ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount(
3285 LoadInst *LI,
3286 Constant *RHS,
3287 const Loop *L,
3288 ICmpInst::Predicate predicate) {
Dan Gohman1c343752009-06-27 21:21:31 +00003289 if (LI->isVolatile()) return getCouldNotCompute();
Chris Lattner673e02b2004-10-12 01:49:27 +00003290
3291 // Check to see if the loaded pointer is a getelementptr of a global.
3292 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0));
Dan Gohman1c343752009-06-27 21:21:31 +00003293 if (!GEP) return getCouldNotCompute();
Chris Lattner673e02b2004-10-12 01:49:27 +00003294
3295 // Make sure that it is really a constant global we are gepping, with an
3296 // initializer, and make sure the first IDX is really 0.
3297 GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
3298 if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
3299 GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) ||
3300 !cast<Constant>(GEP->getOperand(1))->isNullValue())
Dan Gohman1c343752009-06-27 21:21:31 +00003301 return getCouldNotCompute();
Chris Lattner673e02b2004-10-12 01:49:27 +00003302
3303 // Okay, we allow one non-constant index into the GEP instruction.
3304 Value *VarIdx = 0;
3305 std::vector<ConstantInt*> Indexes;
3306 unsigned VarIdxNum = 0;
3307 for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i)
3308 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
3309 Indexes.push_back(CI);
3310 } else if (!isa<ConstantInt>(GEP->getOperand(i))) {
Dan Gohman1c343752009-06-27 21:21:31 +00003311 if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's.
Chris Lattner673e02b2004-10-12 01:49:27 +00003312 VarIdx = GEP->getOperand(i);
3313 VarIdxNum = i-2;
3314 Indexes.push_back(0);
3315 }
3316
3317 // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant.
3318 // Check to see if X is a loop variant variable value now.
Owen Anderson372b46c2009-06-22 21:39:50 +00003319 const SCEV* Idx = getSCEV(VarIdx);
Dan Gohmand594e6f2009-05-24 23:25:42 +00003320 Idx = getSCEVAtScope(Idx, L);
Chris Lattner673e02b2004-10-12 01:49:27 +00003321
3322 // We can only recognize very limited forms of loop index expressions, in
3323 // particular, only affine AddRec's like {C1,+,C2}.
Dan Gohman35738ac2009-05-04 22:30:44 +00003324 const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx);
Chris Lattner673e02b2004-10-12 01:49:27 +00003325 if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) ||
3326 !isa<SCEVConstant>(IdxExpr->getOperand(0)) ||
3327 !isa<SCEVConstant>(IdxExpr->getOperand(1)))
Dan Gohman1c343752009-06-27 21:21:31 +00003328 return getCouldNotCompute();
Chris Lattner673e02b2004-10-12 01:49:27 +00003329
3330 unsigned MaxSteps = MaxBruteForceIterations;
3331 for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
Reid Spencerb83eb642006-10-20 07:07:24 +00003332 ConstantInt *ItCst =
Dan Gohman6de29f82009-06-15 22:12:54 +00003333 ConstantInt::get(cast<IntegerType>(IdxExpr->getType()), IterationNum);
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003334 ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this);
Chris Lattner673e02b2004-10-12 01:49:27 +00003335
3336 // Form the GEP offset.
3337 Indexes[VarIdxNum] = Val;
3338
3339 Constant *Result = GetAddressedElementFromGlobal(GV, Indexes);
3340 if (Result == 0) break; // Cannot compute!
3341
3342 // Evaluate the condition for this iteration.
Reid Spencere4d87aa2006-12-23 06:05:41 +00003343 Result = ConstantExpr::getICmp(predicate, Result, RHS);
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00003344 if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure
Reid Spencere8019bb2007-03-01 07:25:48 +00003345 if (cast<ConstantInt>(Result)->getValue().isMinValue()) {
Chris Lattner673e02b2004-10-12 01:49:27 +00003346#if 0
Dan Gohmanb7ef7292009-04-21 00:47:46 +00003347 errs() << "\n***\n*** Computed loop count " << *ItCst
3348 << "\n*** From global " << *GV << "*** BB: " << *L->getHeader()
3349 << "***\n";
Chris Lattner673e02b2004-10-12 01:49:27 +00003350#endif
3351 ++NumArrayLenItCounts;
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003352 return getConstant(ItCst); // Found terminating iteration!
Chris Lattner673e02b2004-10-12 01:49:27 +00003353 }
3354 }
Dan Gohman1c343752009-06-27 21:21:31 +00003355 return getCouldNotCompute();
Chris Lattner673e02b2004-10-12 01:49:27 +00003356}
3357
3358
Chris Lattner3221ad02004-04-17 22:58:41 +00003359/// CanConstantFold - Return true if we can constant fold an instruction of the
3360/// specified type, assuming that all operands were constants.
3361static bool CanConstantFold(const Instruction *I) {
Reid Spencer832254e2007-02-02 02:16:23 +00003362 if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
Chris Lattner3221ad02004-04-17 22:58:41 +00003363 isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I))
3364 return true;
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003365
Chris Lattner3221ad02004-04-17 22:58:41 +00003366 if (const CallInst *CI = dyn_cast<CallInst>(I))
3367 if (const Function *F = CI->getCalledFunction())
Dan Gohmanfa9b80e2008-01-31 01:05:10 +00003368 return canConstantFoldCallTo(F);
Chris Lattner3221ad02004-04-17 22:58:41 +00003369 return false;
Chris Lattner7980fb92004-04-17 18:36:24 +00003370}
3371
Chris Lattner3221ad02004-04-17 22:58:41 +00003372/// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node
3373/// in the loop that V is derived from. We allow arbitrary operations along the
3374/// way, but the operands of an operation must either be constants or a value
3375/// derived from a constant PHI. If this expression does not fit with these
3376/// constraints, return null.
3377static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) {
3378 // If this is not an instruction, or if this is an instruction outside of the
3379 // loop, it can't be derived from a loop PHI.
3380 Instruction *I = dyn_cast<Instruction>(V);
3381 if (I == 0 || !L->contains(I->getParent())) return 0;
3382
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +00003383 if (PHINode *PN = dyn_cast<PHINode>(I)) {
Chris Lattner3221ad02004-04-17 22:58:41 +00003384 if (L->getHeader() == I->getParent())
3385 return PN;
3386 else
3387 // We don't currently keep track of the control flow needed to evaluate
3388 // PHIs, so we cannot handle PHIs inside of loops.
3389 return 0;
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +00003390 }
Chris Lattner3221ad02004-04-17 22:58:41 +00003391
3392 // If we won't be able to constant fold this expression even if the operands
3393 // are constants, return early.
3394 if (!CanConstantFold(I)) return 0;
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003395
Chris Lattner3221ad02004-04-17 22:58:41 +00003396 // Otherwise, we can evaluate this instruction if all of its operands are
3397 // constant or derived from a PHI node themselves.
3398 PHINode *PHI = 0;
3399 for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op)
3400 if (!(isa<Constant>(I->getOperand(Op)) ||
3401 isa<GlobalValue>(I->getOperand(Op)))) {
3402 PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L);
3403 if (P == 0) return 0; // Not evolving from PHI
3404 if (PHI == 0)
3405 PHI = P;
3406 else if (PHI != P)
3407 return 0; // Evolving from multiple different PHIs.
3408 }
3409
3410 // This is a expression evolving from a constant PHI!
3411 return PHI;
3412}
3413
3414/// EvaluateExpression - Given an expression that passes the
3415/// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node
3416/// in the loop has the value PHIVal. If we can't fold this expression for some
3417/// reason, return null.
3418static Constant *EvaluateExpression(Value *V, Constant *PHIVal) {
3419 if (isa<PHINode>(V)) return PHIVal;
Reid Spencere8404342004-07-18 00:18:30 +00003420 if (Constant *C = dyn_cast<Constant>(V)) return C;
Dan Gohman2d1be872009-04-16 03:18:22 +00003421 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV;
Chris Lattner3221ad02004-04-17 22:58:41 +00003422 Instruction *I = cast<Instruction>(V);
Owen Anderson50895512009-07-06 18:42:36 +00003423 LLVMContext* Context = I->getParent()->getContext();
Chris Lattner3221ad02004-04-17 22:58:41 +00003424
3425 std::vector<Constant*> Operands;
3426 Operands.resize(I->getNumOperands());
3427
3428 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
3429 Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal);
3430 if (Operands[i] == 0) return 0;
3431 }
3432
Chris Lattnerf286f6f2007-12-10 22:53:04 +00003433 if (const CmpInst *CI = dyn_cast<CmpInst>(I))
3434 return ConstantFoldCompareInstOperands(CI->getPredicate(),
Owen Anderson50895512009-07-06 18:42:36 +00003435 &Operands[0], Operands.size(),
3436 Context);
Chris Lattnerf286f6f2007-12-10 22:53:04 +00003437 else
3438 return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
Owen Anderson50895512009-07-06 18:42:36 +00003439 &Operands[0], Operands.size(),
3440 Context);
Chris Lattner3221ad02004-04-17 22:58:41 +00003441}
3442
3443/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
3444/// in the header of its containing loop, we know the loop executes a
3445/// constant number of times, and the PHI node is just a recurrence
3446/// involving constants, fold it.
Dan Gohman64a845e2009-06-24 04:48:43 +00003447Constant *
3448ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN,
3449 const APInt& BEs,
3450 const Loop *L) {
Chris Lattner3221ad02004-04-17 22:58:41 +00003451 std::map<PHINode*, Constant*>::iterator I =
3452 ConstantEvolutionLoopExitValue.find(PN);
3453 if (I != ConstantEvolutionLoopExitValue.end())
3454 return I->second;
3455
Dan Gohman46bdfb02009-02-24 18:55:53 +00003456 if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations)))
Chris Lattner3221ad02004-04-17 22:58:41 +00003457 return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it.
3458
3459 Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
3460
3461 // Since the loop is canonicalized, the PHI node must have two entries. One
3462 // entry must be a constant (coming in from outside of the loop), and the
3463 // second must be derived from the same PHI.
3464 bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
3465 Constant *StartCST =
3466 dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge));
3467 if (StartCST == 0)
3468 return RetVal = 0; // Must be a constant.
3469
3470 Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
3471 PHINode *PN2 = getConstantEvolvingPHI(BEValue, L);
3472 if (PN2 != PN)
3473 return RetVal = 0; // Not derived from same PHI.
3474
3475 // Execute the loop symbolically to determine the exit value.
Dan Gohman46bdfb02009-02-24 18:55:53 +00003476 if (BEs.getActiveBits() >= 32)
Reid Spencere8019bb2007-03-01 07:25:48 +00003477 return RetVal = 0; // More than 2^32-1 iterations?? Not doing it!
Chris Lattner3221ad02004-04-17 22:58:41 +00003478
Dan Gohman46bdfb02009-02-24 18:55:53 +00003479 unsigned NumIterations = BEs.getZExtValue(); // must be in range
Reid Spencere8019bb2007-03-01 07:25:48 +00003480 unsigned IterationNum = 0;
Chris Lattner3221ad02004-04-17 22:58:41 +00003481 for (Constant *PHIVal = StartCST; ; ++IterationNum) {
3482 if (IterationNum == NumIterations)
3483 return RetVal = PHIVal; // Got exit value!
3484
3485 // Compute the value of the PHI node for the next iteration.
3486 Constant *NextPHI = EvaluateExpression(BEValue, PHIVal);
3487 if (NextPHI == PHIVal)
3488 return RetVal = NextPHI; // Stopped evolving!
3489 if (NextPHI == 0)
3490 return 0; // Couldn't evaluate!
3491 PHIVal = NextPHI;
3492 }
3493}
3494
Dan Gohman46bdfb02009-02-24 18:55:53 +00003495/// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute a
Chris Lattner7980fb92004-04-17 18:36:24 +00003496/// constant number of times (the condition evolves only from constants),
3497/// try to evaluate a few iterations of the loop until we get the exit
3498/// condition gets a value of ExitWhen (true or false). If we cannot
Dan Gohman1c343752009-06-27 21:21:31 +00003499/// evaluate the trip count of the loop, return getCouldNotCompute().
Dan Gohman64a845e2009-06-24 04:48:43 +00003500const SCEV *
3501ScalarEvolution::ComputeBackedgeTakenCountExhaustively(const Loop *L,
3502 Value *Cond,
3503 bool ExitWhen) {
Chris Lattner7980fb92004-04-17 18:36:24 +00003504 PHINode *PN = getConstantEvolvingPHI(Cond, L);
Dan Gohman1c343752009-06-27 21:21:31 +00003505 if (PN == 0) return getCouldNotCompute();
Chris Lattner7980fb92004-04-17 18:36:24 +00003506
3507 // Since the loop is canonicalized, the PHI node must have two entries. One
3508 // entry must be a constant (coming in from outside of the loop), and the
3509 // second must be derived from the same PHI.
3510 bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
3511 Constant *StartCST =
3512 dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge));
Dan Gohman1c343752009-06-27 21:21:31 +00003513 if (StartCST == 0) return getCouldNotCompute(); // Must be a constant.
Chris Lattner7980fb92004-04-17 18:36:24 +00003514
3515 Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
3516 PHINode *PN2 = getConstantEvolvingPHI(BEValue, L);
Dan Gohman1c343752009-06-27 21:21:31 +00003517 if (PN2 != PN) return getCouldNotCompute(); // Not derived from same PHI.
Chris Lattner7980fb92004-04-17 18:36:24 +00003518
3519 // Okay, we find a PHI node that defines the trip count of this loop. Execute
3520 // the loop symbolically to determine when the condition gets a value of
3521 // "ExitWhen".
3522 unsigned IterationNum = 0;
3523 unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis.
3524 for (Constant *PHIVal = StartCST;
3525 IterationNum != MaxIterations; ++IterationNum) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00003526 ConstantInt *CondVal =
3527 dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal));
Chris Lattner3221ad02004-04-17 22:58:41 +00003528
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00003529 // Couldn't symbolically evaluate.
Dan Gohman1c343752009-06-27 21:21:31 +00003530 if (!CondVal) return getCouldNotCompute();
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00003531
Reid Spencere8019bb2007-03-01 07:25:48 +00003532 if (CondVal->getValue() == uint64_t(ExitWhen)) {
Chris Lattner7980fb92004-04-17 18:36:24 +00003533 ++NumBruteForceTripCountsComputed;
Dan Gohman6de29f82009-06-15 22:12:54 +00003534 return getConstant(Type::Int32Ty, IterationNum);
Chris Lattner7980fb92004-04-17 18:36:24 +00003535 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003536
Chris Lattner3221ad02004-04-17 22:58:41 +00003537 // Compute the value of the PHI node for the next iteration.
3538 Constant *NextPHI = EvaluateExpression(BEValue, PHIVal);
3539 if (NextPHI == 0 || NextPHI == PHIVal)
Dan Gohman1c343752009-06-27 21:21:31 +00003540 return getCouldNotCompute();// Couldn't evaluate or not making progress...
Chris Lattner3221ad02004-04-17 22:58:41 +00003541 PHIVal = NextPHI;
Chris Lattner7980fb92004-04-17 18:36:24 +00003542 }
3543
3544 // Too many iterations were needed to evaluate.
Dan Gohman1c343752009-06-27 21:21:31 +00003545 return getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00003546}
3547
Dan Gohman66a7e852009-05-08 20:38:54 +00003548/// getSCEVAtScope - Return a SCEV expression handle for the specified value
3549/// at the specified scope in the program. The L value specifies a loop
3550/// nest to evaluate the expression at, where null is the top-level or a
3551/// specified loop is immediately inside of the loop.
3552///
3553/// This method can be used to compute the exit value for a variable defined
3554/// in a loop by querying what the value will hold in the parent loop.
3555///
Dan Gohmand594e6f2009-05-24 23:25:42 +00003556/// In the case that a relevant loop exit value cannot be computed, the
3557/// original value V is returned.
Owen Anderson372b46c2009-06-22 21:39:50 +00003558const SCEV* ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) {
Chris Lattner53e677a2004-04-02 20:23:17 +00003559 // FIXME: this should be turned into a virtual method on SCEV!
3560
Chris Lattner3221ad02004-04-17 22:58:41 +00003561 if (isa<SCEVConstant>(V)) return V;
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003562
Nick Lewycky3e630762008-02-20 06:48:22 +00003563 // If this instruction is evolved from a constant-evolving PHI, compute the
Chris Lattner3221ad02004-04-17 22:58:41 +00003564 // exit value from the loop without using SCEVs.
Dan Gohman622ed672009-05-04 22:02:23 +00003565 if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) {
Chris Lattner3221ad02004-04-17 22:58:41 +00003566 if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003567 const Loop *LI = (*this->LI)[I->getParent()];
Chris Lattner3221ad02004-04-17 22:58:41 +00003568 if (LI && LI->getParentLoop() == L) // Looking for loop exit value.
3569 if (PHINode *PN = dyn_cast<PHINode>(I))
3570 if (PN->getParent() == LI->getHeader()) {
3571 // Okay, there is no closed form solution for the PHI node. Check
Dan Gohman46bdfb02009-02-24 18:55:53 +00003572 // to see if the loop that contains it has a known backedge-taken
3573 // count. If so, we may be able to force computation of the exit
3574 // value.
Owen Anderson372b46c2009-06-22 21:39:50 +00003575 const SCEV* BackedgeTakenCount = getBackedgeTakenCount(LI);
Dan Gohman622ed672009-05-04 22:02:23 +00003576 if (const SCEVConstant *BTCC =
Dan Gohman46bdfb02009-02-24 18:55:53 +00003577 dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
Chris Lattner3221ad02004-04-17 22:58:41 +00003578 // Okay, we know how many times the containing loop executes. If
3579 // this is a constant evolving PHI node, get the final value at
3580 // the specified iteration number.
3581 Constant *RV = getConstantEvolutionLoopExitValue(PN,
Dan Gohman46bdfb02009-02-24 18:55:53 +00003582 BTCC->getValue()->getValue(),
Chris Lattner3221ad02004-04-17 22:58:41 +00003583 LI);
Dan Gohman09987962009-06-29 21:31:18 +00003584 if (RV) return getSCEV(RV);
Chris Lattner3221ad02004-04-17 22:58:41 +00003585 }
3586 }
3587
Reid Spencer09906f32006-12-04 21:33:23 +00003588 // Okay, this is an expression that we cannot symbolically evaluate
Chris Lattner3221ad02004-04-17 22:58:41 +00003589 // into a SCEV. Check to see if it's possible to symbolically evaluate
Reid Spencer09906f32006-12-04 21:33:23 +00003590 // the arguments into constants, and if so, try to constant propagate the
Chris Lattner3221ad02004-04-17 22:58:41 +00003591 // result. This is particularly useful for computing loop exit values.
3592 if (CanConstantFold(I)) {
Dan Gohman6bce6432009-05-08 20:47:27 +00003593 // Check to see if we've folded this instruction at this loop before.
3594 std::map<const Loop *, Constant *> &Values = ValuesAtScopes[I];
3595 std::pair<std::map<const Loop *, Constant *>::iterator, bool> Pair =
3596 Values.insert(std::make_pair(L, static_cast<Constant *>(0)));
3597 if (!Pair.second)
Dan Gohman09987962009-06-29 21:31:18 +00003598 return Pair.first->second ? &*getSCEV(Pair.first->second) : V;
Dan Gohman6bce6432009-05-08 20:47:27 +00003599
Chris Lattner3221ad02004-04-17 22:58:41 +00003600 std::vector<Constant*> Operands;
3601 Operands.reserve(I->getNumOperands());
3602 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
3603 Value *Op = I->getOperand(i);
3604 if (Constant *C = dyn_cast<Constant>(Op)) {
3605 Operands.push_back(C);
Chris Lattner3221ad02004-04-17 22:58:41 +00003606 } else {
Chris Lattner42b5e082007-11-23 08:46:22 +00003607 // If any of the operands is non-constant and if they are
Dan Gohman2d1be872009-04-16 03:18:22 +00003608 // non-integer and non-pointer, don't even try to analyze them
3609 // with scev techniques.
Dan Gohman4acd12a2009-04-30 16:40:30 +00003610 if (!isSCEVable(Op->getType()))
Chris Lattner42b5e082007-11-23 08:46:22 +00003611 return V;
Dan Gohman2d1be872009-04-16 03:18:22 +00003612
Owen Anderson372b46c2009-06-22 21:39:50 +00003613 const SCEV* OpV = getSCEVAtScope(getSCEV(Op), L);
Dan Gohman622ed672009-05-04 22:02:23 +00003614 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) {
Dan Gohman4acd12a2009-04-30 16:40:30 +00003615 Constant *C = SC->getValue();
3616 if (C->getType() != Op->getType())
3617 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
3618 Op->getType(),
3619 false),
3620 C, Op->getType());
3621 Operands.push_back(C);
Dan Gohman622ed672009-05-04 22:02:23 +00003622 } else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) {
Dan Gohman4acd12a2009-04-30 16:40:30 +00003623 if (Constant *C = dyn_cast<Constant>(SU->getValue())) {
3624 if (C->getType() != Op->getType())
3625 C =
3626 ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
3627 Op->getType(),
3628 false),
3629 C, Op->getType());
3630 Operands.push_back(C);
3631 } else
Chris Lattner3221ad02004-04-17 22:58:41 +00003632 return V;
3633 } else {
3634 return V;
3635 }
3636 }
3637 }
Dan Gohman64a845e2009-06-24 04:48:43 +00003638
Chris Lattnerf286f6f2007-12-10 22:53:04 +00003639 Constant *C;
3640 if (const CmpInst *CI = dyn_cast<CmpInst>(I))
3641 C = ConstantFoldCompareInstOperands(CI->getPredicate(),
Owen Anderson50895512009-07-06 18:42:36 +00003642 &Operands[0], Operands.size(),
3643 Context);
Chris Lattnerf286f6f2007-12-10 22:53:04 +00003644 else
3645 C = ConstantFoldInstOperands(I->getOpcode(), I->getType(),
Owen Anderson50895512009-07-06 18:42:36 +00003646 &Operands[0], Operands.size(), Context);
Dan Gohman6bce6432009-05-08 20:47:27 +00003647 Pair.first->second = C;
Dan Gohman09987962009-06-29 21:31:18 +00003648 return getSCEV(C);
Chris Lattner3221ad02004-04-17 22:58:41 +00003649 }
3650 }
3651
3652 // This is some other type of SCEVUnknown, just return it.
3653 return V;
3654 }
3655
Dan Gohman622ed672009-05-04 22:02:23 +00003656 if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00003657 // Avoid performing the look-up in the common case where the specified
3658 // expression has no loop-variant portions.
3659 for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) {
Owen Anderson372b46c2009-06-22 21:39:50 +00003660 const SCEV* OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
Chris Lattner53e677a2004-04-02 20:23:17 +00003661 if (OpAtScope != Comm->getOperand(i)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00003662 // Okay, at least one of these operands is loop variant but might be
3663 // foldable. Build a new instance of the folded commutative expression.
Dan Gohman64a845e2009-06-24 04:48:43 +00003664 SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(),
3665 Comm->op_begin()+i);
Chris Lattner53e677a2004-04-02 20:23:17 +00003666 NewOps.push_back(OpAtScope);
3667
3668 for (++i; i != e; ++i) {
3669 OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
Chris Lattner53e677a2004-04-02 20:23:17 +00003670 NewOps.push_back(OpAtScope);
3671 }
3672 if (isa<SCEVAddExpr>(Comm))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003673 return getAddExpr(NewOps);
Nick Lewyckyc54c5612007-11-25 22:41:31 +00003674 if (isa<SCEVMulExpr>(Comm))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003675 return getMulExpr(NewOps);
Nick Lewyckyc54c5612007-11-25 22:41:31 +00003676 if (isa<SCEVSMaxExpr>(Comm))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003677 return getSMaxExpr(NewOps);
Nick Lewycky3e630762008-02-20 06:48:22 +00003678 if (isa<SCEVUMaxExpr>(Comm))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003679 return getUMaxExpr(NewOps);
Nick Lewyckyc54c5612007-11-25 22:41:31 +00003680 assert(0 && "Unknown commutative SCEV type!");
Chris Lattner53e677a2004-04-02 20:23:17 +00003681 }
3682 }
3683 // If we got here, all operands are loop invariant.
3684 return Comm;
3685 }
3686
Dan Gohman622ed672009-05-04 22:02:23 +00003687 if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) {
Owen Anderson372b46c2009-06-22 21:39:50 +00003688 const SCEV* LHS = getSCEVAtScope(Div->getLHS(), L);
3689 const SCEV* RHS = getSCEVAtScope(Div->getRHS(), L);
Nick Lewycky789558d2009-01-13 09:18:58 +00003690 if (LHS == Div->getLHS() && RHS == Div->getRHS())
3691 return Div; // must be loop invariant
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003692 return getUDivExpr(LHS, RHS);
Chris Lattner53e677a2004-04-02 20:23:17 +00003693 }
3694
3695 // If this is a loop recurrence for a loop that does not contain L, then we
3696 // are dealing with the final value computed by the loop.
Dan Gohman622ed672009-05-04 22:02:23 +00003697 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00003698 if (!L || !AddRec->getLoop()->contains(L->getHeader())) {
3699 // To evaluate this recurrence, we need to know how many times the AddRec
3700 // loop iterates. Compute this now.
Owen Anderson372b46c2009-06-22 21:39:50 +00003701 const SCEV* BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop());
Dan Gohman1c343752009-06-27 21:21:31 +00003702 if (BackedgeTakenCount == getCouldNotCompute()) return AddRec;
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003703
Eli Friedmanb42a6262008-08-04 23:49:06 +00003704 // Then, evaluate the AddRec.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003705 return AddRec->evaluateAtIteration(BackedgeTakenCount, *this);
Chris Lattner53e677a2004-04-02 20:23:17 +00003706 }
Dan Gohmand594e6f2009-05-24 23:25:42 +00003707 return AddRec;
Chris Lattner53e677a2004-04-02 20:23:17 +00003708 }
3709
Dan Gohman622ed672009-05-04 22:02:23 +00003710 if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) {
Owen Anderson372b46c2009-06-22 21:39:50 +00003711 const SCEV* Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohmaneb3948b2009-04-29 22:29:01 +00003712 if (Op == Cast->getOperand())
3713 return Cast; // must be loop invariant
3714 return getZeroExtendExpr(Op, Cast->getType());
3715 }
3716
Dan Gohman622ed672009-05-04 22:02:23 +00003717 if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) {
Owen Anderson372b46c2009-06-22 21:39:50 +00003718 const SCEV* Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohmaneb3948b2009-04-29 22:29:01 +00003719 if (Op == Cast->getOperand())
3720 return Cast; // must be loop invariant
3721 return getSignExtendExpr(Op, Cast->getType());
3722 }
3723
Dan Gohman622ed672009-05-04 22:02:23 +00003724 if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) {
Owen Anderson372b46c2009-06-22 21:39:50 +00003725 const SCEV* Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohmaneb3948b2009-04-29 22:29:01 +00003726 if (Op == Cast->getOperand())
3727 return Cast; // must be loop invariant
3728 return getTruncateExpr(Op, Cast->getType());
3729 }
3730
3731 assert(0 && "Unknown SCEV type!");
Daniel Dunbar8c562e22009-05-18 16:43:04 +00003732 return 0;
Chris Lattner53e677a2004-04-02 20:23:17 +00003733}
3734
Dan Gohman66a7e852009-05-08 20:38:54 +00003735/// getSCEVAtScope - This is a convenience function which does
3736/// getSCEVAtScope(getSCEV(V), L).
Owen Anderson372b46c2009-06-22 21:39:50 +00003737const SCEV* ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003738 return getSCEVAtScope(getSCEV(V), L);
3739}
3740
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00003741/// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the
3742/// following equation:
3743///
3744/// A * X = B (mod N)
3745///
3746/// where N = 2^BW and BW is the common bit width of A and B. The signedness of
3747/// A and B isn't important.
3748///
3749/// If the equation does not have a solution, SCEVCouldNotCompute is returned.
Owen Anderson372b46c2009-06-22 21:39:50 +00003750static const SCEV* SolveLinEquationWithOverflow(const APInt &A, const APInt &B,
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00003751 ScalarEvolution &SE) {
3752 uint32_t BW = A.getBitWidth();
3753 assert(BW == B.getBitWidth() && "Bit widths must be the same.");
3754 assert(A != 0 && "A must be non-zero.");
3755
3756 // 1. D = gcd(A, N)
3757 //
3758 // The gcd of A and N may have only one prime factor: 2. The number of
3759 // trailing zeros in A is its multiplicity
3760 uint32_t Mult2 = A.countTrailingZeros();
3761 // D = 2^Mult2
3762
3763 // 2. Check if B is divisible by D.
3764 //
3765 // B is divisible by D if and only if the multiplicity of prime factor 2 for B
3766 // is not less than multiplicity of this prime factor for D.
3767 if (B.countTrailingZeros() < Mult2)
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00003768 return SE.getCouldNotCompute();
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00003769
3770 // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic
3771 // modulo (N / D).
3772 //
3773 // (N / D) may need BW+1 bits in its representation. Hence, we'll use this
3774 // bit width during computations.
3775 APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D
3776 APInt Mod(BW + 1, 0);
3777 Mod.set(BW - Mult2); // Mod = N / D
3778 APInt I = AD.multiplicativeInverse(Mod);
3779
3780 // 4. Compute the minimum unsigned root of the equation:
3781 // I * (B / D) mod (N / D)
3782 APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod);
3783
3784 // The result is guaranteed to be less than 2^BW so we may truncate it to BW
3785 // bits.
3786 return SE.getConstant(Result.trunc(BW));
3787}
Chris Lattner53e677a2004-04-02 20:23:17 +00003788
3789/// SolveQuadraticEquation - Find the roots of the quadratic equation for the
3790/// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which
3791/// might be the same) or two SCEVCouldNotCompute objects.
3792///
Owen Anderson372b46c2009-06-22 21:39:50 +00003793static std::pair<const SCEV*,const SCEV*>
Dan Gohman246b2562007-10-22 18:31:58 +00003794SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) {
Chris Lattner53e677a2004-04-02 20:23:17 +00003795 assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
Dan Gohman35738ac2009-05-04 22:30:44 +00003796 const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
3797 const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
3798 const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003799
Chris Lattner53e677a2004-04-02 20:23:17 +00003800 // We currently can only solve this if the coefficients are constants.
Reid Spencere8019bb2007-03-01 07:25:48 +00003801 if (!LC || !MC || !NC) {
Dan Gohman35738ac2009-05-04 22:30:44 +00003802 const SCEV *CNC = SE.getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00003803 return std::make_pair(CNC, CNC);
3804 }
3805
Reid Spencere8019bb2007-03-01 07:25:48 +00003806 uint32_t BitWidth = LC->getValue()->getValue().getBitWidth();
Chris Lattnerfe560b82007-04-15 19:52:49 +00003807 const APInt &L = LC->getValue()->getValue();
3808 const APInt &M = MC->getValue()->getValue();
3809 const APInt &N = NC->getValue()->getValue();
Reid Spencere8019bb2007-03-01 07:25:48 +00003810 APInt Two(BitWidth, 2);
3811 APInt Four(BitWidth, 4);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003812
Dan Gohman64a845e2009-06-24 04:48:43 +00003813 {
Reid Spencere8019bb2007-03-01 07:25:48 +00003814 using namespace APIntOps;
Zhou Sheng414de4d2007-04-07 17:48:27 +00003815 const APInt& C = L;
Reid Spencere8019bb2007-03-01 07:25:48 +00003816 // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C
3817 // The B coefficient is M-N/2
3818 APInt B(M);
3819 B -= sdiv(N,Two);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003820
Reid Spencere8019bb2007-03-01 07:25:48 +00003821 // The A coefficient is N/2
Zhou Sheng414de4d2007-04-07 17:48:27 +00003822 APInt A(N.sdiv(Two));
Chris Lattner53e677a2004-04-02 20:23:17 +00003823
Reid Spencere8019bb2007-03-01 07:25:48 +00003824 // Compute the B^2-4ac term.
3825 APInt SqrtTerm(B);
3826 SqrtTerm *= B;
3827 SqrtTerm -= Four * (A * C);
Chris Lattner53e677a2004-04-02 20:23:17 +00003828
Reid Spencere8019bb2007-03-01 07:25:48 +00003829 // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest
3830 // integer value or else APInt::sqrt() will assert.
3831 APInt SqrtVal(SqrtTerm.sqrt());
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003832
Dan Gohman64a845e2009-06-24 04:48:43 +00003833 // Compute the two solutions for the quadratic formula.
Reid Spencere8019bb2007-03-01 07:25:48 +00003834 // The divisions must be performed as signed divisions.
3835 APInt NegB(-B);
Reid Spencer3e35c8d2007-04-16 02:24:41 +00003836 APInt TwoA( A << 1 );
Nick Lewycky8f4d5eb2008-11-03 02:43:49 +00003837 if (TwoA.isMinValue()) {
Dan Gohman35738ac2009-05-04 22:30:44 +00003838 const SCEV *CNC = SE.getCouldNotCompute();
Nick Lewycky8f4d5eb2008-11-03 02:43:49 +00003839 return std::make_pair(CNC, CNC);
3840 }
3841
Reid Spencere8019bb2007-03-01 07:25:48 +00003842 ConstantInt *Solution1 = ConstantInt::get((NegB + SqrtVal).sdiv(TwoA));
3843 ConstantInt *Solution2 = ConstantInt::get((NegB - SqrtVal).sdiv(TwoA));
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003844
Dan Gohman64a845e2009-06-24 04:48:43 +00003845 return std::make_pair(SE.getConstant(Solution1),
Dan Gohman246b2562007-10-22 18:31:58 +00003846 SE.getConstant(Solution2));
Reid Spencere8019bb2007-03-01 07:25:48 +00003847 } // end APIntOps namespace
Chris Lattner53e677a2004-04-02 20:23:17 +00003848}
3849
3850/// HowFarToZero - Return the number of times a backedge comparing the specified
Dan Gohman86fbf2f2009-06-06 14:37:11 +00003851/// value to zero will execute. If not computable, return CouldNotCompute.
Owen Anderson372b46c2009-06-22 21:39:50 +00003852const SCEV* ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) {
Chris Lattner53e677a2004-04-02 20:23:17 +00003853 // If the value is a constant
Dan Gohman622ed672009-05-04 22:02:23 +00003854 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00003855 // If the value is already zero, the branch will execute zero times.
Reid Spencercae57542007-03-02 00:28:52 +00003856 if (C->getValue()->isZero()) return C;
Dan Gohman1c343752009-06-27 21:21:31 +00003857 return getCouldNotCompute(); // Otherwise it will loop infinitely.
Chris Lattner53e677a2004-04-02 20:23:17 +00003858 }
3859
Dan Gohman35738ac2009-05-04 22:30:44 +00003860 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V);
Chris Lattner53e677a2004-04-02 20:23:17 +00003861 if (!AddRec || AddRec->getLoop() != L)
Dan Gohman1c343752009-06-27 21:21:31 +00003862 return getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00003863
3864 if (AddRec->isAffine()) {
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00003865 // If this is an affine expression, the execution count of this branch is
3866 // the minimum unsigned root of the following equation:
Chris Lattner53e677a2004-04-02 20:23:17 +00003867 //
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00003868 // Start + Step*N = 0 (mod 2^BW)
Chris Lattner53e677a2004-04-02 20:23:17 +00003869 //
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00003870 // equivalent to:
3871 //
3872 // Step*N = -Start (mod 2^BW)
3873 //
3874 // where BW is the common bit width of Start and Step.
3875
Chris Lattner53e677a2004-04-02 20:23:17 +00003876 // Get the initial value for the loop.
Dan Gohman64a845e2009-06-24 04:48:43 +00003877 const SCEV *Start = getSCEVAtScope(AddRec->getStart(),
3878 L->getParentLoop());
3879 const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1),
3880 L->getParentLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +00003881
Dan Gohman622ed672009-05-04 22:02:23 +00003882 if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) {
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00003883 // For now we handle only constant steps.
Chris Lattner53e677a2004-04-02 20:23:17 +00003884
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00003885 // First, handle unitary steps.
3886 if (StepC->getValue()->equalsInt(1)) // 1*N = -Start (mod 2^BW), so:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003887 return getNegativeSCEV(Start); // N = -Start (as unsigned)
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00003888 if (StepC->getValue()->isAllOnesValue()) // -1*N = -Start (mod 2^BW), so:
3889 return Start; // N = Start (as unsigned)
3890
3891 // Then, try to solve the above equation provided that Start is constant.
Dan Gohman622ed672009-05-04 22:02:23 +00003892 if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start))
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00003893 return SolveLinEquationWithOverflow(StepC->getValue()->getValue(),
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003894 -StartC->getValue()->getValue(),
3895 *this);
Chris Lattner53e677a2004-04-02 20:23:17 +00003896 }
Chris Lattner42a75512007-01-15 02:27:26 +00003897 } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) {
Chris Lattner53e677a2004-04-02 20:23:17 +00003898 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
3899 // the quadratic equation to solve it.
Owen Anderson372b46c2009-06-22 21:39:50 +00003900 std::pair<const SCEV*,const SCEV*> Roots = SolveQuadraticEquation(AddRec,
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003901 *this);
Dan Gohman35738ac2009-05-04 22:30:44 +00003902 const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
3903 const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
Chris Lattner53e677a2004-04-02 20:23:17 +00003904 if (R1) {
Chris Lattnerd18d9dc2004-04-02 20:26:46 +00003905#if 0
Dan Gohmanb7ef7292009-04-21 00:47:46 +00003906 errs() << "HFTZ: " << *V << " - sol#1: " << *R1
3907 << " sol#2: " << *R2 << "\n";
Chris Lattnerd18d9dc2004-04-02 20:26:46 +00003908#endif
Chris Lattner53e677a2004-04-02 20:23:17 +00003909 // Pick the smallest positive root value.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00003910 if (ConstantInt *CB =
Dan Gohman64a845e2009-06-24 04:48:43 +00003911 dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT,
Reid Spencere4d87aa2006-12-23 06:05:41 +00003912 R1->getValue(), R2->getValue()))) {
Reid Spencer579dca12007-01-12 04:24:46 +00003913 if (CB->getZExtValue() == false)
Chris Lattner53e677a2004-04-02 20:23:17 +00003914 std::swap(R1, R2); // R1 is the minimum root now.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003915
Chris Lattner53e677a2004-04-02 20:23:17 +00003916 // We can only use this value if the chrec ends up with an exact zero
3917 // value at this index. When solving for "X*X != 5", for example, we
3918 // should not accept a root of 2.
Owen Anderson372b46c2009-06-22 21:39:50 +00003919 const SCEV* Val = AddRec->evaluateAtIteration(R1, *this);
Dan Gohmancfeb6a42008-06-18 16:23:07 +00003920 if (Val->isZero())
3921 return R1; // We found a quadratic root!
Chris Lattner53e677a2004-04-02 20:23:17 +00003922 }
3923 }
3924 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003925
Dan Gohman1c343752009-06-27 21:21:31 +00003926 return getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00003927}
3928
3929/// HowFarToNonZero - Return the number of times a backedge checking the
3930/// specified value for nonzero will execute. If not computable, return
Dan Gohman86fbf2f2009-06-06 14:37:11 +00003931/// CouldNotCompute
Owen Anderson372b46c2009-06-22 21:39:50 +00003932const SCEV* ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) {
Chris Lattner53e677a2004-04-02 20:23:17 +00003933 // Loops that look like: while (X == 0) are very strange indeed. We don't
3934 // handle them yet except for the trivial case. This could be expanded in the
3935 // future as needed.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003936
Chris Lattner53e677a2004-04-02 20:23:17 +00003937 // If the value is a constant, check to see if it is known to be non-zero
3938 // already. If so, the backedge will execute zero times.
Dan Gohman622ed672009-05-04 22:02:23 +00003939 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
Nick Lewycky39442af2008-02-21 09:14:53 +00003940 if (!C->getValue()->isNullValue())
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003941 return getIntegerSCEV(0, C->getType());
Dan Gohman1c343752009-06-27 21:21:31 +00003942 return getCouldNotCompute(); // Otherwise it will loop infinitely.
Chris Lattner53e677a2004-04-02 20:23:17 +00003943 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003944
Chris Lattner53e677a2004-04-02 20:23:17 +00003945 // We could implement others, but I really doubt anyone writes loops like
3946 // this, and if they did, they would already be constant folded.
Dan Gohman1c343752009-06-27 21:21:31 +00003947 return getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00003948}
3949
Dan Gohman859b4822009-05-18 15:36:09 +00003950/// getLoopPredecessor - If the given loop's header has exactly one unique
3951/// predecessor outside the loop, return it. Otherwise return null.
3952///
3953BasicBlock *ScalarEvolution::getLoopPredecessor(const Loop *L) {
3954 BasicBlock *Header = L->getHeader();
3955 BasicBlock *Pred = 0;
3956 for (pred_iterator PI = pred_begin(Header), E = pred_end(Header);
3957 PI != E; ++PI)
3958 if (!L->contains(*PI)) {
3959 if (Pred && Pred != *PI) return 0; // Multiple predecessors.
3960 Pred = *PI;
3961 }
3962 return Pred;
3963}
3964
Dan Gohmanfd6edef2008-09-15 22:18:04 +00003965/// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
3966/// (which may not be an immediate predecessor) which has exactly one
3967/// successor from which BB is reachable, or null if no such block is
3968/// found.
3969///
3970BasicBlock *
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003971ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
Dan Gohman3d739fe2009-04-30 20:48:53 +00003972 // If the block has a unique predecessor, then there is no path from the
3973 // predecessor to the block that does not go through the direct edge
3974 // from the predecessor to the block.
Dan Gohmanfd6edef2008-09-15 22:18:04 +00003975 if (BasicBlock *Pred = BB->getSinglePredecessor())
3976 return Pred;
3977
3978 // A loop's header is defined to be a block that dominates the loop.
Dan Gohman859b4822009-05-18 15:36:09 +00003979 // If the header has a unique predecessor outside the loop, it must be
3980 // a block that has exactly one successor that can reach the loop.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003981 if (Loop *L = LI->getLoopFor(BB))
Dan Gohman859b4822009-05-18 15:36:09 +00003982 return getLoopPredecessor(L);
Dan Gohmanfd6edef2008-09-15 22:18:04 +00003983
3984 return 0;
3985}
3986
Dan Gohman763bad12009-06-20 00:35:32 +00003987/// HasSameValue - SCEV structural equivalence is usually sufficient for
3988/// testing whether two expressions are equal, however for the purposes of
3989/// looking for a condition guarding a loop, it can be useful to be a little
3990/// more general, since a front-end may have replicated the controlling
3991/// expression.
3992///
Owen Anderson372b46c2009-06-22 21:39:50 +00003993static bool HasSameValue(const SCEV* A, const SCEV* B) {
Dan Gohman763bad12009-06-20 00:35:32 +00003994 // Quick check to see if they are the same SCEV.
3995 if (A == B) return true;
3996
3997 // Otherwise, if they're both SCEVUnknown, it's possible that they hold
3998 // two different instructions with the same value. Check for this case.
3999 if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A))
4000 if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B))
4001 if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue()))
4002 if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue()))
4003 if (AI->isIdenticalTo(BI))
4004 return true;
4005
4006 // Otherwise assume they may have a different value.
4007 return false;
4008}
4009
Dan Gohmanc2390b12009-02-12 22:19:27 +00004010/// isLoopGuardedByCond - Test whether entry to the loop is protected by
Dan Gohman3d739fe2009-04-30 20:48:53 +00004011/// a conditional between LHS and RHS. This is used to help avoid max
4012/// expressions in loop trip counts.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004013bool ScalarEvolution::isLoopGuardedByCond(const Loop *L,
Dan Gohman3d739fe2009-04-30 20:48:53 +00004014 ICmpInst::Predicate Pred,
Dan Gohman35738ac2009-05-04 22:30:44 +00004015 const SCEV *LHS, const SCEV *RHS) {
Dan Gohman8ea94522009-05-18 16:03:58 +00004016 // Interpret a null as meaning no loop, where there is obviously no guard
4017 // (interprocedural conditions notwithstanding).
4018 if (!L) return false;
4019
Dan Gohman859b4822009-05-18 15:36:09 +00004020 BasicBlock *Predecessor = getLoopPredecessor(L);
4021 BasicBlock *PredecessorDest = L->getHeader();
Nick Lewycky59cff122008-07-12 07:41:32 +00004022
Dan Gohman859b4822009-05-18 15:36:09 +00004023 // Starting at the loop predecessor, climb up the predecessor chain, as long
4024 // as there are predecessors that can be found that have unique successors
Dan Gohmanfd6edef2008-09-15 22:18:04 +00004025 // leading to the original header.
Dan Gohman859b4822009-05-18 15:36:09 +00004026 for (; Predecessor;
4027 PredecessorDest = Predecessor,
4028 Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) {
Dan Gohman38372182008-08-12 20:17:31 +00004029
4030 BranchInst *LoopEntryPredicate =
Dan Gohman859b4822009-05-18 15:36:09 +00004031 dyn_cast<BranchInst>(Predecessor->getTerminator());
Dan Gohman38372182008-08-12 20:17:31 +00004032 if (!LoopEntryPredicate ||
4033 LoopEntryPredicate->isUnconditional())
4034 continue;
4035
Dan Gohman40a5a1b2009-06-24 01:18:18 +00004036 if (isNecessaryCond(LoopEntryPredicate->getCondition(), Pred, LHS, RHS,
4037 LoopEntryPredicate->getSuccessor(0) != PredecessorDest))
Dan Gohman38372182008-08-12 20:17:31 +00004038 return true;
Nick Lewycky59cff122008-07-12 07:41:32 +00004039 }
4040
Dan Gohman38372182008-08-12 20:17:31 +00004041 return false;
Nick Lewycky59cff122008-07-12 07:41:32 +00004042}
4043
Dan Gohman40a5a1b2009-06-24 01:18:18 +00004044/// isNecessaryCond - Test whether the given CondValue value is a condition
4045/// which is at least as strict as the one described by Pred, LHS, and RHS.
4046bool ScalarEvolution::isNecessaryCond(Value *CondValue,
4047 ICmpInst::Predicate Pred,
4048 const SCEV *LHS, const SCEV *RHS,
4049 bool Inverse) {
4050 // Recursivly handle And and Or conditions.
4051 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CondValue)) {
4052 if (BO->getOpcode() == Instruction::And) {
4053 if (!Inverse)
4054 return isNecessaryCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) ||
4055 isNecessaryCond(BO->getOperand(1), Pred, LHS, RHS, Inverse);
4056 } else if (BO->getOpcode() == Instruction::Or) {
4057 if (Inverse)
4058 return isNecessaryCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) ||
4059 isNecessaryCond(BO->getOperand(1), Pred, LHS, RHS, Inverse);
4060 }
4061 }
4062
4063 ICmpInst *ICI = dyn_cast<ICmpInst>(CondValue);
4064 if (!ICI) return false;
4065
4066 // Now that we found a conditional branch that dominates the loop, check to
4067 // see if it is the comparison we are looking for.
4068 Value *PreCondLHS = ICI->getOperand(0);
4069 Value *PreCondRHS = ICI->getOperand(1);
4070 ICmpInst::Predicate Cond;
4071 if (Inverse)
4072 Cond = ICI->getInversePredicate();
4073 else
4074 Cond = ICI->getPredicate();
4075
4076 if (Cond == Pred)
4077 ; // An exact match.
4078 else if (!ICmpInst::isTrueWhenEqual(Cond) && Pred == ICmpInst::ICMP_NE)
4079 ; // The actual condition is beyond sufficient.
4080 else
4081 // Check a few special cases.
4082 switch (Cond) {
4083 case ICmpInst::ICMP_UGT:
4084 if (Pred == ICmpInst::ICMP_ULT) {
4085 std::swap(PreCondLHS, PreCondRHS);
4086 Cond = ICmpInst::ICMP_ULT;
4087 break;
4088 }
4089 return false;
4090 case ICmpInst::ICMP_SGT:
4091 if (Pred == ICmpInst::ICMP_SLT) {
4092 std::swap(PreCondLHS, PreCondRHS);
4093 Cond = ICmpInst::ICMP_SLT;
4094 break;
4095 }
4096 return false;
4097 case ICmpInst::ICMP_NE:
4098 // Expressions like (x >u 0) are often canonicalized to (x != 0),
4099 // so check for this case by checking if the NE is comparing against
4100 // a minimum or maximum constant.
4101 if (!ICmpInst::isTrueWhenEqual(Pred))
4102 if (ConstantInt *CI = dyn_cast<ConstantInt>(PreCondRHS)) {
4103 const APInt &A = CI->getValue();
4104 switch (Pred) {
4105 case ICmpInst::ICMP_SLT:
4106 if (A.isMaxSignedValue()) break;
4107 return false;
4108 case ICmpInst::ICMP_SGT:
4109 if (A.isMinSignedValue()) break;
4110 return false;
4111 case ICmpInst::ICMP_ULT:
4112 if (A.isMaxValue()) break;
4113 return false;
4114 case ICmpInst::ICMP_UGT:
4115 if (A.isMinValue()) break;
4116 return false;
4117 default:
4118 return false;
4119 }
4120 Cond = ICmpInst::ICMP_NE;
4121 // NE is symmetric but the original comparison may not be. Swap
4122 // the operands if necessary so that they match below.
4123 if (isa<SCEVConstant>(LHS))
4124 std::swap(PreCondLHS, PreCondRHS);
4125 break;
4126 }
4127 return false;
4128 default:
4129 // We weren't able to reconcile the condition.
4130 return false;
4131 }
4132
4133 if (!PreCondLHS->getType()->isInteger()) return false;
4134
4135 const SCEV *PreCondLHSSCEV = getSCEV(PreCondLHS);
4136 const SCEV *PreCondRHSSCEV = getSCEV(PreCondRHS);
4137 return (HasSameValue(LHS, PreCondLHSSCEV) &&
4138 HasSameValue(RHS, PreCondRHSSCEV)) ||
4139 (HasSameValue(LHS, getNotSCEV(PreCondRHSSCEV)) &&
4140 HasSameValue(RHS, getNotSCEV(PreCondLHSSCEV)));
4141}
4142
Dan Gohman51f53b72009-06-21 23:46:38 +00004143/// getBECount - Subtract the end and start values and divide by the step,
4144/// rounding up, to get the number of times the backedge is executed. Return
4145/// CouldNotCompute if an intermediate computation overflows.
Owen Anderson372b46c2009-06-22 21:39:50 +00004146const SCEV* ScalarEvolution::getBECount(const SCEV* Start,
4147 const SCEV* End,
4148 const SCEV* Step) {
Dan Gohman51f53b72009-06-21 23:46:38 +00004149 const Type *Ty = Start->getType();
Owen Anderson372b46c2009-06-22 21:39:50 +00004150 const SCEV* NegOne = getIntegerSCEV(-1, Ty);
4151 const SCEV* Diff = getMinusSCEV(End, Start);
4152 const SCEV* RoundUp = getAddExpr(Step, NegOne);
Dan Gohman51f53b72009-06-21 23:46:38 +00004153
4154 // Add an adjustment to the difference between End and Start so that
4155 // the division will effectively round up.
Owen Anderson372b46c2009-06-22 21:39:50 +00004156 const SCEV* Add = getAddExpr(Diff, RoundUp);
Dan Gohman51f53b72009-06-21 23:46:38 +00004157
4158 // Check Add for unsigned overflow.
4159 // TODO: More sophisticated things could be done here.
4160 const Type *WideTy = IntegerType::get(getTypeSizeInBits(Ty) + 1);
Owen Anderson372b46c2009-06-22 21:39:50 +00004161 const SCEV* OperandExtendedAdd =
Dan Gohman51f53b72009-06-21 23:46:38 +00004162 getAddExpr(getZeroExtendExpr(Diff, WideTy),
4163 getZeroExtendExpr(RoundUp, WideTy));
4164 if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd)
Dan Gohman1c343752009-06-27 21:21:31 +00004165 return getCouldNotCompute();
Dan Gohman51f53b72009-06-21 23:46:38 +00004166
4167 return getUDivExpr(Add, Step);
4168}
4169
Chris Lattnerdb25de42005-08-15 23:33:51 +00004170/// HowManyLessThans - Return the number of times a backedge containing the
4171/// specified less-than comparison will execute. If not computable, return
Dan Gohman86fbf2f2009-06-06 14:37:11 +00004172/// CouldNotCompute.
Dan Gohman64a845e2009-06-24 04:48:43 +00004173ScalarEvolution::BackedgeTakenInfo
4174ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
4175 const Loop *L, bool isSigned) {
Chris Lattnerdb25de42005-08-15 23:33:51 +00004176 // Only handle: "ADDREC < LoopInvariant".
Dan Gohman1c343752009-06-27 21:21:31 +00004177 if (!RHS->isLoopInvariant(L)) return getCouldNotCompute();
Chris Lattnerdb25de42005-08-15 23:33:51 +00004178
Dan Gohman35738ac2009-05-04 22:30:44 +00004179 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS);
Chris Lattnerdb25de42005-08-15 23:33:51 +00004180 if (!AddRec || AddRec->getLoop() != L)
Dan Gohman1c343752009-06-27 21:21:31 +00004181 return getCouldNotCompute();
Chris Lattnerdb25de42005-08-15 23:33:51 +00004182
4183 if (AddRec->isAffine()) {
Nick Lewycky789558d2009-01-13 09:18:58 +00004184 // FORNOW: We only support unit strides.
Dan Gohmana1af7572009-04-30 20:47:05 +00004185 unsigned BitWidth = getTypeSizeInBits(AddRec->getType());
Owen Anderson372b46c2009-06-22 21:39:50 +00004186 const SCEV* Step = AddRec->getStepRecurrence(*this);
Dan Gohmana1af7572009-04-30 20:47:05 +00004187
4188 // TODO: handle non-constant strides.
4189 const SCEVConstant *CStep = dyn_cast<SCEVConstant>(Step);
4190 if (!CStep || CStep->isZero())
Dan Gohman1c343752009-06-27 21:21:31 +00004191 return getCouldNotCompute();
Dan Gohman70a1fe72009-05-18 15:22:39 +00004192 if (CStep->isOne()) {
Dan Gohmana1af7572009-04-30 20:47:05 +00004193 // With unit stride, the iteration never steps past the limit value.
4194 } else if (CStep->getValue()->getValue().isStrictlyPositive()) {
4195 if (const SCEVConstant *CLimit = dyn_cast<SCEVConstant>(RHS)) {
4196 // Test whether a positive iteration iteration can step past the limit
4197 // value and past the maximum value for its type in a single step.
4198 if (isSigned) {
4199 APInt Max = APInt::getSignedMaxValue(BitWidth);
4200 if ((Max - CStep->getValue()->getValue())
4201 .slt(CLimit->getValue()->getValue()))
Dan Gohman1c343752009-06-27 21:21:31 +00004202 return getCouldNotCompute();
Dan Gohmana1af7572009-04-30 20:47:05 +00004203 } else {
4204 APInt Max = APInt::getMaxValue(BitWidth);
4205 if ((Max - CStep->getValue()->getValue())
4206 .ult(CLimit->getValue()->getValue()))
Dan Gohman1c343752009-06-27 21:21:31 +00004207 return getCouldNotCompute();
Dan Gohmana1af7572009-04-30 20:47:05 +00004208 }
4209 } else
4210 // TODO: handle non-constant limit values below.
Dan Gohman1c343752009-06-27 21:21:31 +00004211 return getCouldNotCompute();
Dan Gohmana1af7572009-04-30 20:47:05 +00004212 } else
4213 // TODO: handle negative strides below.
Dan Gohman1c343752009-06-27 21:21:31 +00004214 return getCouldNotCompute();
Chris Lattnerdb25de42005-08-15 23:33:51 +00004215
Dan Gohmana1af7572009-04-30 20:47:05 +00004216 // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant
4217 // m. So, we count the number of iterations in which {n,+,s} < m is true.
4218 // Note that we cannot simply return max(m-n,0)/s because it's not safe to
Wojciech Matyjewicza65ee032008-02-13 12:21:32 +00004219 // treat m-n as signed nor unsigned due to overflow possibility.
Chris Lattnerdb25de42005-08-15 23:33:51 +00004220
Wojciech Matyjewicz3a4cbe22008-02-13 11:51:34 +00004221 // First, we get the value of the LHS in the first iteration: n
Owen Anderson372b46c2009-06-22 21:39:50 +00004222 const SCEV* Start = AddRec->getOperand(0);
Wojciech Matyjewicz3a4cbe22008-02-13 11:51:34 +00004223
Dan Gohmana1af7572009-04-30 20:47:05 +00004224 // Determine the minimum constant start value.
Dan Gohman64a845e2009-06-24 04:48:43 +00004225 const SCEV *MinStart = isa<SCEVConstant>(Start) ? Start :
Dan Gohmana1af7572009-04-30 20:47:05 +00004226 getConstant(isSigned ? APInt::getSignedMinValue(BitWidth) :
4227 APInt::getMinValue(BitWidth));
Wojciech Matyjewicz3a4cbe22008-02-13 11:51:34 +00004228
Dan Gohmana1af7572009-04-30 20:47:05 +00004229 // If we know that the condition is true in order to enter the loop,
4230 // then we know that it will run exactly (m-n)/s times. Otherwise, we
Dan Gohman6c0866c2009-05-24 23:45:28 +00004231 // only know that it will execute (max(m,n)-n)/s times. In both cases,
4232 // the division must round up.
Owen Anderson372b46c2009-06-22 21:39:50 +00004233 const SCEV* End = RHS;
Dan Gohmana1af7572009-04-30 20:47:05 +00004234 if (!isLoopGuardedByCond(L,
4235 isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
4236 getMinusSCEV(Start, Step), RHS))
4237 End = isSigned ? getSMaxExpr(RHS, Start)
4238 : getUMaxExpr(RHS, Start);
4239
4240 // Determine the maximum constant end value.
Owen Anderson372b46c2009-06-22 21:39:50 +00004241 const SCEV* MaxEnd =
Dan Gohman3964acc2009-06-20 00:32:22 +00004242 isa<SCEVConstant>(End) ? End :
4243 getConstant(isSigned ? APInt::getSignedMaxValue(BitWidth)
4244 .ashr(GetMinSignBits(End) - 1) :
4245 APInt::getMaxValue(BitWidth)
4246 .lshr(GetMinLeadingZeros(End)));
Dan Gohmana1af7572009-04-30 20:47:05 +00004247
4248 // Finally, we subtract these two values and divide, rounding up, to get
4249 // the number of times the backedge is executed.
Owen Anderson372b46c2009-06-22 21:39:50 +00004250 const SCEV* BECount = getBECount(Start, End, Step);
Dan Gohmana1af7572009-04-30 20:47:05 +00004251
4252 // The maximum backedge count is similar, except using the minimum start
4253 // value and the maximum end value.
Dan Gohmanc39f44b2009-06-30 20:13:32 +00004254 const SCEV* MaxBECount = getBECount(MinStart, MaxEnd, Step);
Dan Gohmana1af7572009-04-30 20:47:05 +00004255
4256 return BackedgeTakenInfo(BECount, MaxBECount);
Chris Lattnerdb25de42005-08-15 23:33:51 +00004257 }
4258
Dan Gohman1c343752009-06-27 21:21:31 +00004259 return getCouldNotCompute();
Chris Lattnerdb25de42005-08-15 23:33:51 +00004260}
4261
Chris Lattner53e677a2004-04-02 20:23:17 +00004262/// getNumIterationsInRange - Return the number of iterations of this loop that
4263/// produce values in the specified constant range. Another way of looking at
4264/// this is that it returns the first iteration number where the value is not in
4265/// the condition, thus computing the exit count. If the iteration count can't
4266/// be computed, an instance of SCEVCouldNotCompute is returned.
Owen Anderson372b46c2009-06-22 21:39:50 +00004267const SCEV* SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
Dan Gohman64a845e2009-06-24 04:48:43 +00004268 ScalarEvolution &SE) const {
Chris Lattner53e677a2004-04-02 20:23:17 +00004269 if (Range.isFullSet()) // Infinite loop.
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00004270 return SE.getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00004271
4272 // If the start is a non-zero constant, shift the range to simplify things.
Dan Gohman622ed672009-05-04 22:02:23 +00004273 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart()))
Reid Spencercae57542007-03-02 00:28:52 +00004274 if (!SC->getValue()->isZero()) {
Owen Anderson372b46c2009-06-22 21:39:50 +00004275 SmallVector<const SCEV*, 4> Operands(op_begin(), op_end());
Dan Gohman246b2562007-10-22 18:31:58 +00004276 Operands[0] = SE.getIntegerSCEV(0, SC->getType());
Owen Anderson372b46c2009-06-22 21:39:50 +00004277 const SCEV* Shifted = SE.getAddRecExpr(Operands, getLoop());
Dan Gohman622ed672009-05-04 22:02:23 +00004278 if (const SCEVAddRecExpr *ShiftedAddRec =
4279 dyn_cast<SCEVAddRecExpr>(Shifted))
Chris Lattner53e677a2004-04-02 20:23:17 +00004280 return ShiftedAddRec->getNumIterationsInRange(
Dan Gohman246b2562007-10-22 18:31:58 +00004281 Range.subtract(SC->getValue()->getValue()), SE);
Chris Lattner53e677a2004-04-02 20:23:17 +00004282 // This is strange and shouldn't happen.
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00004283 return SE.getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00004284 }
4285
4286 // The only time we can solve this is when we have all constant indices.
4287 // Otherwise, we cannot determine the overflow conditions.
4288 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4289 if (!isa<SCEVConstant>(getOperand(i)))
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00004290 return SE.getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00004291
4292
4293 // Okay at this point we know that all elements of the chrec are constants and
4294 // that the start element is zero.
4295
4296 // First check to see if the range contains zero. If not, the first
4297 // iteration exits.
Dan Gohmanaf79fb52009-04-21 01:07:12 +00004298 unsigned BitWidth = SE.getTypeSizeInBits(getType());
Dan Gohman2d1be872009-04-16 03:18:22 +00004299 if (!Range.contains(APInt(BitWidth, 0)))
Dan Gohman6de29f82009-06-15 22:12:54 +00004300 return SE.getIntegerSCEV(0, getType());
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004301
Chris Lattner53e677a2004-04-02 20:23:17 +00004302 if (isAffine()) {
4303 // If this is an affine expression then we have this situation:
4304 // Solve {0,+,A} in Range === Ax in Range
4305
Nick Lewyckyeefdebe2007-07-16 02:08:00 +00004306 // We know that zero is in the range. If A is positive then we know that
4307 // the upper value of the range must be the first possible exit value.
4308 // If A is negative then the lower of the range is the last possible loop
4309 // value. Also note that we already checked for a full range.
Dan Gohman2d1be872009-04-16 03:18:22 +00004310 APInt One(BitWidth,1);
Nick Lewyckyeefdebe2007-07-16 02:08:00 +00004311 APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue();
4312 APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower();
Chris Lattner53e677a2004-04-02 20:23:17 +00004313
Nick Lewyckyeefdebe2007-07-16 02:08:00 +00004314 // The exit value should be (End+A)/A.
Nick Lewycky9a2f9312007-09-27 14:12:54 +00004315 APInt ExitVal = (End + A).udiv(A);
Reid Spencerc7cd7a02007-03-01 19:32:33 +00004316 ConstantInt *ExitValue = ConstantInt::get(ExitVal);
Chris Lattner53e677a2004-04-02 20:23:17 +00004317
4318 // Evaluate at the exit value. If we really did fall out of the valid
4319 // range, then we computed our trip count, otherwise wrap around or other
4320 // things must have happened.
Dan Gohman246b2562007-10-22 18:31:58 +00004321 ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE);
Reid Spencera6e8a952007-03-01 07:54:15 +00004322 if (Range.contains(Val->getValue()))
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00004323 return SE.getCouldNotCompute(); // Something strange happened
Chris Lattner53e677a2004-04-02 20:23:17 +00004324
4325 // Ensure that the previous value is in the range. This is a sanity check.
Reid Spencer581b0d42007-02-28 19:57:34 +00004326 assert(Range.contains(
Dan Gohman64a845e2009-06-24 04:48:43 +00004327 EvaluateConstantChrecAtConstant(this,
Dan Gohman246b2562007-10-22 18:31:58 +00004328 ConstantInt::get(ExitVal - One), SE)->getValue()) &&
Chris Lattner53e677a2004-04-02 20:23:17 +00004329 "Linear scev computation is off in a bad way!");
Dan Gohman246b2562007-10-22 18:31:58 +00004330 return SE.getConstant(ExitValue);
Chris Lattner53e677a2004-04-02 20:23:17 +00004331 } else if (isQuadratic()) {
4332 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the
4333 // quadratic equation to solve it. To do this, we must frame our problem in
4334 // terms of figuring out when zero is crossed, instead of when
4335 // Range.getUpper() is crossed.
Owen Anderson372b46c2009-06-22 21:39:50 +00004336 SmallVector<const SCEV*, 4> NewOps(op_begin(), op_end());
Dan Gohman246b2562007-10-22 18:31:58 +00004337 NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper()));
Owen Anderson372b46c2009-06-22 21:39:50 +00004338 const SCEV* NewAddRec = SE.getAddRecExpr(NewOps, getLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +00004339
4340 // Next, solve the constructed addrec
Owen Anderson372b46c2009-06-22 21:39:50 +00004341 std::pair<const SCEV*,const SCEV*> Roots =
Dan Gohman246b2562007-10-22 18:31:58 +00004342 SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE);
Dan Gohman35738ac2009-05-04 22:30:44 +00004343 const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
4344 const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
Chris Lattner53e677a2004-04-02 20:23:17 +00004345 if (R1) {
4346 // Pick the smallest positive root value.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00004347 if (ConstantInt *CB =
Dan Gohman64a845e2009-06-24 04:48:43 +00004348 dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT,
Reid Spencere4d87aa2006-12-23 06:05:41 +00004349 R1->getValue(), R2->getValue()))) {
Reid Spencer579dca12007-01-12 04:24:46 +00004350 if (CB->getZExtValue() == false)
Chris Lattner53e677a2004-04-02 20:23:17 +00004351 std::swap(R1, R2); // R1 is the minimum root now.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004352
Chris Lattner53e677a2004-04-02 20:23:17 +00004353 // Make sure the root is not off by one. The returned iteration should
4354 // not be in the range, but the previous one should be. When solving
4355 // for "X*X < 5", for example, we should not return a root of 2.
4356 ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this,
Dan Gohman246b2562007-10-22 18:31:58 +00004357 R1->getValue(),
4358 SE);
Reid Spencera6e8a952007-03-01 07:54:15 +00004359 if (Range.contains(R1Val->getValue())) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004360 // The next iteration must be out of the range...
Dan Gohman9a6ae962007-07-09 15:25:17 +00004361 ConstantInt *NextVal = ConstantInt::get(R1->getValue()->getValue()+1);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004362
Dan Gohman246b2562007-10-22 18:31:58 +00004363 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
Reid Spencera6e8a952007-03-01 07:54:15 +00004364 if (!Range.contains(R1Val->getValue()))
Dan Gohman246b2562007-10-22 18:31:58 +00004365 return SE.getConstant(NextVal);
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00004366 return SE.getCouldNotCompute(); // Something strange happened
Chris Lattner53e677a2004-04-02 20:23:17 +00004367 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004368
Chris Lattner53e677a2004-04-02 20:23:17 +00004369 // If R1 was not in the range, then it is a good return value. Make
4370 // sure that R1-1 WAS in the range though, just in case.
Dan Gohman9a6ae962007-07-09 15:25:17 +00004371 ConstantInt *NextVal = ConstantInt::get(R1->getValue()->getValue()-1);
Dan Gohman246b2562007-10-22 18:31:58 +00004372 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
Reid Spencera6e8a952007-03-01 07:54:15 +00004373 if (Range.contains(R1Val->getValue()))
Chris Lattner53e677a2004-04-02 20:23:17 +00004374 return R1;
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00004375 return SE.getCouldNotCompute(); // Something strange happened
Chris Lattner53e677a2004-04-02 20:23:17 +00004376 }
4377 }
4378 }
4379
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00004380 return SE.getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00004381}
4382
4383
4384
4385//===----------------------------------------------------------------------===//
Dan Gohman35738ac2009-05-04 22:30:44 +00004386// SCEVCallbackVH Class Implementation
4387//===----------------------------------------------------------------------===//
4388
Dan Gohman1959b752009-05-19 19:22:47 +00004389void ScalarEvolution::SCEVCallbackVH::deleted() {
Dan Gohman35738ac2009-05-04 22:30:44 +00004390 assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!");
4391 if (PHINode *PN = dyn_cast<PHINode>(getValPtr()))
4392 SE->ConstantEvolutionLoopExitValue.erase(PN);
Dan Gohman6bce6432009-05-08 20:47:27 +00004393 if (Instruction *I = dyn_cast<Instruction>(getValPtr()))
4394 SE->ValuesAtScopes.erase(I);
Dan Gohman35738ac2009-05-04 22:30:44 +00004395 SE->Scalars.erase(getValPtr());
4396 // this now dangles!
4397}
4398
Dan Gohman1959b752009-05-19 19:22:47 +00004399void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *) {
Dan Gohman35738ac2009-05-04 22:30:44 +00004400 assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!");
4401
4402 // Forget all the expressions associated with users of the old value,
4403 // so that future queries will recompute the expressions using the new
4404 // value.
4405 SmallVector<User *, 16> Worklist;
4406 Value *Old = getValPtr();
4407 bool DeleteOld = false;
4408 for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end();
4409 UI != UE; ++UI)
4410 Worklist.push_back(*UI);
4411 while (!Worklist.empty()) {
4412 User *U = Worklist.pop_back_val();
4413 // Deleting the Old value will cause this to dangle. Postpone
4414 // that until everything else is done.
4415 if (U == Old) {
4416 DeleteOld = true;
4417 continue;
4418 }
4419 if (PHINode *PN = dyn_cast<PHINode>(U))
4420 SE->ConstantEvolutionLoopExitValue.erase(PN);
Dan Gohman6bce6432009-05-08 20:47:27 +00004421 if (Instruction *I = dyn_cast<Instruction>(U))
4422 SE->ValuesAtScopes.erase(I);
Dan Gohman35738ac2009-05-04 22:30:44 +00004423 if (SE->Scalars.erase(U))
4424 for (Value::use_iterator UI = U->use_begin(), UE = U->use_end();
4425 UI != UE; ++UI)
4426 Worklist.push_back(*UI);
4427 }
4428 if (DeleteOld) {
4429 if (PHINode *PN = dyn_cast<PHINode>(Old))
4430 SE->ConstantEvolutionLoopExitValue.erase(PN);
Dan Gohman6bce6432009-05-08 20:47:27 +00004431 if (Instruction *I = dyn_cast<Instruction>(Old))
4432 SE->ValuesAtScopes.erase(I);
Dan Gohman35738ac2009-05-04 22:30:44 +00004433 SE->Scalars.erase(Old);
4434 // this now dangles!
4435 }
4436 // this may dangle!
4437}
4438
Dan Gohman1959b752009-05-19 19:22:47 +00004439ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se)
Dan Gohman35738ac2009-05-04 22:30:44 +00004440 : CallbackVH(V), SE(se) {}
4441
4442//===----------------------------------------------------------------------===//
Chris Lattner53e677a2004-04-02 20:23:17 +00004443// ScalarEvolution Class Implementation
4444//===----------------------------------------------------------------------===//
4445
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004446ScalarEvolution::ScalarEvolution()
Dan Gohman1c343752009-06-27 21:21:31 +00004447 : FunctionPass(&ID) {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004448}
4449
Chris Lattner53e677a2004-04-02 20:23:17 +00004450bool ScalarEvolution::runOnFunction(Function &F) {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004451 this->F = &F;
4452 LI = &getAnalysis<LoopInfo>();
4453 TD = getAnalysisIfAvailable<TargetData>();
Chris Lattner53e677a2004-04-02 20:23:17 +00004454 return false;
4455}
4456
4457void ScalarEvolution::releaseMemory() {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004458 Scalars.clear();
4459 BackedgeTakenCounts.clear();
4460 ConstantEvolutionLoopExitValue.clear();
Dan Gohman6bce6432009-05-08 20:47:27 +00004461 ValuesAtScopes.clear();
Dan Gohman1c343752009-06-27 21:21:31 +00004462 UniqueSCEVs.clear();
4463 SCEVAllocator.Reset();
Chris Lattner53e677a2004-04-02 20:23:17 +00004464}
4465
4466void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const {
4467 AU.setPreservesAll();
Chris Lattner53e677a2004-04-02 20:23:17 +00004468 AU.addRequiredTransitive<LoopInfo>();
Dan Gohman2d1be872009-04-16 03:18:22 +00004469}
4470
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004471bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) {
Dan Gohman46bdfb02009-02-24 18:55:53 +00004472 return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L));
Chris Lattner53e677a2004-04-02 20:23:17 +00004473}
4474
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004475static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE,
Chris Lattner53e677a2004-04-02 20:23:17 +00004476 const Loop *L) {
4477 // Print all inner loops first
4478 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
4479 PrintLoopInfo(OS, SE, *I);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004480
Nick Lewyckyaeb5e5c2008-01-02 02:49:20 +00004481 OS << "Loop " << L->getHeader()->getName() << ": ";
Chris Lattnerf1ab4b42004-04-18 22:14:10 +00004482
Devang Patelb7211a22007-08-21 00:31:24 +00004483 SmallVector<BasicBlock*, 8> ExitBlocks;
Chris Lattnerf1ab4b42004-04-18 22:14:10 +00004484 L->getExitBlocks(ExitBlocks);
4485 if (ExitBlocks.size() != 1)
Nick Lewyckyaeb5e5c2008-01-02 02:49:20 +00004486 OS << "<multiple exits> ";
Chris Lattner53e677a2004-04-02 20:23:17 +00004487
Dan Gohman46bdfb02009-02-24 18:55:53 +00004488 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
4489 OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L);
Chris Lattner53e677a2004-04-02 20:23:17 +00004490 } else {
Dan Gohman46bdfb02009-02-24 18:55:53 +00004491 OS << "Unpredictable backedge-taken count. ";
Chris Lattner53e677a2004-04-02 20:23:17 +00004492 }
4493
Nick Lewyckyaeb5e5c2008-01-02 02:49:20 +00004494 OS << "\n";
Dan Gohmanaa551ae2009-06-24 00:33:16 +00004495 OS << "Loop " << L->getHeader()->getName() << ": ";
4496
4497 if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) {
4498 OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L);
4499 } else {
4500 OS << "Unpredictable max backedge-taken count. ";
4501 }
4502
4503 OS << "\n";
Chris Lattner53e677a2004-04-02 20:23:17 +00004504}
4505
Dan Gohmanb7ef7292009-04-21 00:47:46 +00004506void ScalarEvolution::print(raw_ostream &OS, const Module* ) const {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004507 // ScalarEvolution's implementaiton of the print method is to print
4508 // out SCEV values of all instructions that are interesting. Doing
4509 // this potentially causes it to create new SCEV objects though,
4510 // which technically conflicts with the const qualifier. This isn't
4511 // observable from outside the class though (the hasSCEV function
4512 // notwithstanding), so casting away the const isn't dangerous.
4513 ScalarEvolution &SE = *const_cast<ScalarEvolution*>(this);
Chris Lattner53e677a2004-04-02 20:23:17 +00004514
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004515 OS << "Classifying expressions for: " << F->getName() << "\n";
Chris Lattner53e677a2004-04-02 20:23:17 +00004516 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Dan Gohmand9c1c852009-04-30 01:30:18 +00004517 if (isSCEVable(I->getType())) {
Chris Lattner6ffe5512004-04-27 15:13:33 +00004518 OS << *I;
Dan Gohman8dae1382008-09-14 17:21:12 +00004519 OS << " --> ";
Owen Anderson372b46c2009-06-22 21:39:50 +00004520 const SCEV* SV = SE.getSCEV(&*I);
Chris Lattner53e677a2004-04-02 20:23:17 +00004521 SV->print(OS);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004522
Dan Gohman0c689c52009-06-19 17:49:54 +00004523 const Loop *L = LI->getLoopFor((*I).getParent());
4524
Owen Anderson372b46c2009-06-22 21:39:50 +00004525 const SCEV* AtUse = SE.getSCEVAtScope(SV, L);
Dan Gohman0c689c52009-06-19 17:49:54 +00004526 if (AtUse != SV) {
4527 OS << " --> ";
4528 AtUse->print(OS);
4529 }
4530
4531 if (L) {
Dan Gohman9e7d9882009-06-18 00:37:45 +00004532 OS << "\t\t" "Exits: ";
Owen Anderson372b46c2009-06-22 21:39:50 +00004533 const SCEV* ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop());
Dan Gohmand594e6f2009-05-24 23:25:42 +00004534 if (!ExitValue->isLoopInvariant(L)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004535 OS << "<<Unknown>>";
4536 } else {
4537 OS << *ExitValue;
4538 }
4539 }
4540
Chris Lattner53e677a2004-04-02 20:23:17 +00004541 OS << "\n";
4542 }
4543
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004544 OS << "Determining loop execution counts for: " << F->getName() << "\n";
4545 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
4546 PrintLoopInfo(OS, &SE, *I);
Chris Lattner53e677a2004-04-02 20:23:17 +00004547}
Dan Gohmanb7ef7292009-04-21 00:47:46 +00004548
4549void ScalarEvolution::print(std::ostream &o, const Module *M) const {
4550 raw_os_ostream OS(o);
4551 print(OS, M);
4552}