blob: 8d5855986e0de2fc6a7d866f49f347e998a391d5 [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
Dan Gohmanbc3d77a2009-07-25 16:18:07 +000017// can handle. We only create one SCEV of a particular shape, so
18// pointer-comparisons for equality are legal.
Chris Lattner53e677a2004-04-02 20:23:17 +000019//
20// One important aspect of the SCEV objects is that they are never cyclic, even
21// if there is a cycle in the dataflow for an expression (ie, a PHI node). If
22// the PHI node is one of the idioms that we can represent (e.g., a polynomial
23// recurrence) then we represent it directly as a recurrence node, otherwise we
24// represent it as a SCEVUnknown node.
25//
26// In addition to being able to represent expressions of various types, we also
27// have folders that are used to build the *canonical* representation for a
28// particular expression. These folders are capable of using a variety of
29// rewrite rules to simplify the expressions.
Misha Brukman2b37d7c2005-04-21 21:13:18 +000030//
Chris Lattner53e677a2004-04-02 20:23:17 +000031// Once the folders are defined, we can implement the more interesting
32// higher-level code, such as the code that recognizes PHI nodes of various
33// types, computes the execution count of a loop, etc.
34//
Chris Lattner53e677a2004-04-02 20:23:17 +000035// TODO: We should use these routines and value representations to implement
36// dependence analysis!
37//
38//===----------------------------------------------------------------------===//
39//
40// There are several good references for the techniques used in this analysis.
41//
42// Chains of recurrences -- a method to expedite the evaluation
43// of closed-form functions
44// Olaf Bachmann, Paul S. Wang, Eugene V. Zima
45//
46// On computational properties of chains of recurrences
47// Eugene V. Zima
48//
49// Symbolic Evaluation of Chains of Recurrences for Loop Optimization
50// Robert A. van Engelen
51//
52// Efficient Symbolic Analysis for Optimizing Compilers
53// Robert A. van Engelen
54//
55// Using the chains of recurrences algebra for data dependence testing and
56// induction variable substitution
57// MS Thesis, Johnie Birch
58//
59//===----------------------------------------------------------------------===//
60
Chris Lattner3b27d682006-12-19 22:30:33 +000061#define DEBUG_TYPE "scalar-evolution"
Chris Lattner0a7f98c2004-04-15 15:07:24 +000062#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner53e677a2004-04-02 20:23:17 +000063#include "llvm/Constants.h"
64#include "llvm/DerivedTypes.h"
Chris Lattner673e02b2004-10-12 01:49:27 +000065#include "llvm/GlobalVariable.h"
Dan Gohman26812322009-08-25 17:49:57 +000066#include "llvm/GlobalAlias.h"
Chris Lattner53e677a2004-04-02 20:23:17 +000067#include "llvm/Instructions.h"
Owen Anderson76f600b2009-07-06 22:37:39 +000068#include "llvm/LLVMContext.h"
Dan Gohmanca178902009-07-17 20:47:02 +000069#include "llvm/Operator.h"
John Criswella1156432005-10-27 15:54:34 +000070#include "llvm/Analysis/ConstantFolding.h"
Evan Cheng5a6c1a82009-02-17 00:13:06 +000071#include "llvm/Analysis/Dominators.h"
Chris Lattner53e677a2004-04-02 20:23:17 +000072#include "llvm/Analysis/LoopInfo.h"
Dan Gohman61ffa8e2009-06-16 19:52:01 +000073#include "llvm/Analysis/ValueTracking.h"
Chris Lattner53e677a2004-04-02 20:23:17 +000074#include "llvm/Assembly/Writer.h"
Dan Gohman2d1be872009-04-16 03:18:22 +000075#include "llvm/Target/TargetData.h"
Chris Lattner95255282006-06-28 23:17:24 +000076#include "llvm/Support/CommandLine.h"
Chris Lattner53e677a2004-04-02 20:23:17 +000077#include "llvm/Support/ConstantRange.h"
David Greene63c94632009-12-23 22:58:38 +000078#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000079#include "llvm/Support/ErrorHandling.h"
Dan Gohman2d1be872009-04-16 03:18:22 +000080#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner53e677a2004-04-02 20:23:17 +000081#include "llvm/Support/InstIterator.h"
Chris Lattner75de5ab2006-12-19 01:16:02 +000082#include "llvm/Support/MathExtras.h"
Dan Gohmanb7ef7292009-04-21 00:47:46 +000083#include "llvm/Support/raw_ostream.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000084#include "llvm/ADT/Statistic.h"
Dan Gohman2d1be872009-04-16 03:18:22 +000085#include "llvm/ADT/STLExtras.h"
Dan Gohman59ae6b92009-07-08 19:23:34 +000086#include "llvm/ADT/SmallPtrSet.h"
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000087#include <algorithm>
Chris Lattner53e677a2004-04-02 20:23:17 +000088using namespace llvm;
89
Chris Lattner3b27d682006-12-19 22:30:33 +000090STATISTIC(NumArrayLenItCounts,
91 "Number of trip counts computed with array length");
92STATISTIC(NumTripCountsComputed,
93 "Number of loops with predictable loop counts");
94STATISTIC(NumTripCountsNotComputed,
95 "Number of loops without predictable loop counts");
96STATISTIC(NumBruteForceTripCountsComputed,
97 "Number of loops with trip counts computed by force");
98
Dan Gohman844731a2008-05-13 00:00:25 +000099static cl::opt<unsigned>
Chris Lattner3b27d682006-12-19 22:30:33 +0000100MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden,
101 cl::desc("Maximum number of iterations SCEV will "
Dan Gohman64a845e2009-06-24 04:48:43 +0000102 "symbolically execute a constant "
103 "derived loop"),
Chris Lattner3b27d682006-12-19 22:30:33 +0000104 cl::init(100));
105
Dan Gohman844731a2008-05-13 00:00:25 +0000106static RegisterPass<ScalarEvolution>
107R("scalar-evolution", "Scalar Evolution Analysis", false, true);
Devang Patel19974732007-05-03 01:11:54 +0000108char ScalarEvolution::ID = 0;
Chris Lattner53e677a2004-04-02 20:23:17 +0000109
110//===----------------------------------------------------------------------===//
111// SCEV class definitions
112//===----------------------------------------------------------------------===//
113
114//===----------------------------------------------------------------------===//
115// Implementation of the SCEV class.
116//
Dan Gohmanc39f44b2009-06-30 20:13:32 +0000117
Chris Lattner53e677a2004-04-02 20:23:17 +0000118SCEV::~SCEV() {}
Dan Gohmanc39f44b2009-06-30 20:13:32 +0000119
Chris Lattner53e677a2004-04-02 20:23:17 +0000120void SCEV::dump() const {
David Greene25e0e872009-12-23 22:18:14 +0000121 print(dbgs());
122 dbgs() << '\n';
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000123}
124
Dan Gohmancfeb6a42008-06-18 16:23:07 +0000125bool SCEV::isZero() const {
126 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
127 return SC->getValue()->isZero();
128 return false;
129}
130
Dan Gohman70a1fe72009-05-18 15:22:39 +0000131bool SCEV::isOne() const {
132 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
133 return SC->getValue()->isOne();
134 return false;
135}
Chris Lattner53e677a2004-04-02 20:23:17 +0000136
Dan Gohman4d289bf2009-06-24 00:30:26 +0000137bool SCEV::isAllOnesValue() const {
138 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
139 return SC->getValue()->isAllOnesValue();
140 return false;
141}
142
Owen Anderson753ad612009-06-22 21:57:23 +0000143SCEVCouldNotCompute::SCEVCouldNotCompute() :
Dan Gohmanc050fd92009-07-13 20:50:19 +0000144 SCEV(FoldingSetNodeID(), scCouldNotCompute) {}
Dan Gohman1c343752009-06-27 21:21:31 +0000145
Chris Lattner53e677a2004-04-02 20:23:17 +0000146bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const {
Torok Edwinc23197a2009-07-14 16:55:14 +0000147 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
Misha Brukmanbb2aff12004-04-05 19:00:46 +0000148 return false;
Chris Lattner53e677a2004-04-02 20:23:17 +0000149}
150
151const Type *SCEVCouldNotCompute::getType() const {
Torok Edwinc23197a2009-07-14 16:55:14 +0000152 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
Misha Brukmanbb2aff12004-04-05 19:00:46 +0000153 return 0;
Chris Lattner53e677a2004-04-02 20:23:17 +0000154}
155
156bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const {
Torok Edwinc23197a2009-07-14 16:55:14 +0000157 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
Chris Lattner53e677a2004-04-02 20:23:17 +0000158 return false;
159}
160
Dan Gohmanfef8bb22009-07-25 01:13:03 +0000161bool SCEVCouldNotCompute::hasOperand(const SCEV *) const {
162 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
163 return false;
Chris Lattner4dc534c2005-02-13 04:37:18 +0000164}
165
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000166void SCEVCouldNotCompute::print(raw_ostream &OS) const {
Chris Lattner53e677a2004-04-02 20:23:17 +0000167 OS << "***COULDNOTCOMPUTE***";
168}
169
170bool SCEVCouldNotCompute::classof(const SCEV *S) {
171 return S->getSCEVType() == scCouldNotCompute;
172}
173
Dan Gohman0bba49c2009-07-07 17:06:11 +0000174const SCEV *ScalarEvolution::getConstant(ConstantInt *V) {
Dan Gohman1c343752009-06-27 21:21:31 +0000175 FoldingSetNodeID ID;
176 ID.AddInteger(scConstant);
177 ID.AddPointer(V);
178 void *IP = 0;
179 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
180 SCEV *S = SCEVAllocator.Allocate<SCEVConstant>();
Dan Gohmanc050fd92009-07-13 20:50:19 +0000181 new (S) SCEVConstant(ID, V);
Dan Gohman1c343752009-06-27 21:21:31 +0000182 UniqueSCEVs.InsertNode(S, IP);
183 return S;
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000184}
Chris Lattner53e677a2004-04-02 20:23:17 +0000185
Dan Gohman0bba49c2009-07-07 17:06:11 +0000186const SCEV *ScalarEvolution::getConstant(const APInt& Val) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000187 return getConstant(ConstantInt::get(getContext(), Val));
Dan Gohman9a6ae962007-07-09 15:25:17 +0000188}
189
Dan Gohman0bba49c2009-07-07 17:06:11 +0000190const SCEV *
Dan Gohman6de29f82009-06-15 22:12:54 +0000191ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) {
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000192 return getConstant(
Owen Andersoneed707b2009-07-24 23:12:02 +0000193 ConstantInt::get(cast<IntegerType>(Ty), V, isSigned));
Dan Gohman6de29f82009-06-15 22:12:54 +0000194}
195
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000196const Type *SCEVConstant::getType() const { return V->getType(); }
Chris Lattner53e677a2004-04-02 20:23:17 +0000197
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000198void SCEVConstant::print(raw_ostream &OS) const {
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000199 WriteAsOperand(OS, V, false);
200}
Chris Lattner53e677a2004-04-02 20:23:17 +0000201
Dan Gohmanc050fd92009-07-13 20:50:19 +0000202SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeID &ID,
203 unsigned SCEVTy, const SCEV *op, const Type *ty)
204 : SCEV(ID, SCEVTy), Op(op), Ty(ty) {}
Dan Gohman1c343752009-06-27 21:21:31 +0000205
Dan Gohman84923602009-04-21 01:25:57 +0000206bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
207 return Op->dominates(BB, DT);
208}
209
Dan Gohman6e70e312009-09-27 15:26:03 +0000210bool SCEVCastExpr::properlyDominates(BasicBlock *BB, DominatorTree *DT) const {
211 return Op->properlyDominates(BB, DT);
212}
213
Dan Gohmanc050fd92009-07-13 20:50:19 +0000214SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeID &ID,
215 const SCEV *op, const Type *ty)
216 : SCEVCastExpr(ID, scTruncate, op, ty) {
Dan Gohman2d1be872009-04-16 03:18:22 +0000217 assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
218 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000219 "Cannot truncate non-integer value!");
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000220}
Chris Lattner53e677a2004-04-02 20:23:17 +0000221
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000222void SCEVTruncateExpr::print(raw_ostream &OS) const {
Dan Gohman36b8e532009-04-29 20:27:52 +0000223 OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000224}
225
Dan Gohmanc050fd92009-07-13 20:50:19 +0000226SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeID &ID,
227 const SCEV *op, const Type *ty)
228 : SCEVCastExpr(ID, scZeroExtend, op, ty) {
Dan Gohman2d1be872009-04-16 03:18:22 +0000229 assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
230 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000231 "Cannot zero extend non-integer value!");
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000232}
233
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000234void SCEVZeroExtendExpr::print(raw_ostream &OS) const {
Dan Gohman36b8e532009-04-29 20:27:52 +0000235 OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000236}
237
Dan Gohmanc050fd92009-07-13 20:50:19 +0000238SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeID &ID,
239 const SCEV *op, const Type *ty)
240 : SCEVCastExpr(ID, scSignExtend, op, ty) {
Dan Gohman2d1be872009-04-16 03:18:22 +0000241 assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
242 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohmand19534a2007-06-15 14:38:12 +0000243 "Cannot sign extend non-integer value!");
Dan Gohmand19534a2007-06-15 14:38:12 +0000244}
245
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000246void SCEVSignExtendExpr::print(raw_ostream &OS) const {
Dan Gohman36b8e532009-04-29 20:27:52 +0000247 OS << "(sext " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
Dan Gohmand19534a2007-06-15 14:38:12 +0000248}
249
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000250void SCEVCommutativeExpr::print(raw_ostream &OS) const {
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000251 assert(Operands.size() > 1 && "This plus expr shouldn't exist!");
252 const char *OpStr = getOperationStr();
253 OS << "(" << *Operands[0];
254 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
255 OS << OpStr << *Operands[i];
256 OS << ")";
257}
258
Dan Gohmanecb403a2009-05-07 14:00:19 +0000259bool SCEVNAryExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
Evan Cheng5a6c1a82009-02-17 00:13:06 +0000260 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
261 if (!getOperand(i)->dominates(BB, DT))
262 return false;
263 }
264 return true;
265}
266
Dan Gohman6e70e312009-09-27 15:26:03 +0000267bool SCEVNAryExpr::properlyDominates(BasicBlock *BB, DominatorTree *DT) const {
268 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
269 if (!getOperand(i)->properlyDominates(BB, DT))
270 return false;
271 }
272 return true;
273}
274
Evan Cheng5a6c1a82009-02-17 00:13:06 +0000275bool SCEVUDivExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
276 return LHS->dominates(BB, DT) && RHS->dominates(BB, DT);
277}
278
Dan Gohman6e70e312009-09-27 15:26:03 +0000279bool SCEVUDivExpr::properlyDominates(BasicBlock *BB, DominatorTree *DT) const {
280 return LHS->properlyDominates(BB, DT) && RHS->properlyDominates(BB, DT);
281}
282
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000283void SCEVUDivExpr::print(raw_ostream &OS) const {
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000284 OS << "(" << *LHS << " /u " << *RHS << ")";
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000285}
286
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000287const Type *SCEVUDivExpr::getType() const {
Dan Gohman91bb61a2009-05-26 17:44:05 +0000288 // In most cases the types of LHS and RHS will be the same, but in some
289 // crazy cases one or the other may be a pointer. ScalarEvolution doesn't
290 // depend on the type for correctness, but handling types carefully can
291 // avoid extra casts in the SCEVExpander. The LHS is more likely to be
292 // a pointer type than the RHS, so use the RHS' type here.
293 return RHS->getType();
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000294}
295
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000296bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const {
Dan Gohmana3035a62009-05-20 01:01:24 +0000297 // Add recurrences are never invariant in the function-body (null loop).
Dan Gohmane890eea2009-06-26 22:17:21 +0000298 if (!QueryLoop)
299 return false;
300
301 // This recurrence is variant w.r.t. QueryLoop if QueryLoop contains L.
Dan Gohman92329c72009-12-18 01:24:09 +0000302 if (QueryLoop->contains(L))
Dan Gohmane890eea2009-06-26 22:17:21 +0000303 return false;
304
305 // This recurrence is variant w.r.t. QueryLoop if any of its operands
306 // are variant.
307 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
308 if (!getOperand(i)->isLoopInvariant(QueryLoop))
309 return false;
310
311 // Otherwise it's loop-invariant.
312 return true;
Chris Lattner53e677a2004-04-02 20:23:17 +0000313}
314
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000315void SCEVAddRecExpr::print(raw_ostream &OS) const {
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000316 OS << "{" << *Operands[0];
317 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
318 OS << ",+," << *Operands[i];
Dan Gohman30733292010-01-09 18:17:45 +0000319 OS << "}<";
320 WriteAsOperand(OS, L->getHeader(), /*PrintType=*/false);
321 OS << ">";
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000322}
Chris Lattner53e677a2004-04-02 20:23:17 +0000323
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000324bool SCEVUnknown::isLoopInvariant(const Loop *L) const {
325 // All non-instruction values are loop invariant. All instructions are loop
326 // invariant if they are not contained in the specified loop.
Dan Gohmana3035a62009-05-20 01:01:24 +0000327 // Instructions are never considered invariant in the function body
328 // (null loop) because they are defined within the "loop".
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000329 if (Instruction *I = dyn_cast<Instruction>(V))
Dan Gohman92329c72009-12-18 01:24:09 +0000330 return L && !L->contains(I);
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000331 return true;
332}
Chris Lattner53e677a2004-04-02 20:23:17 +0000333
Evan Cheng5a6c1a82009-02-17 00:13:06 +0000334bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const {
335 if (Instruction *I = dyn_cast<Instruction>(getValue()))
336 return DT->dominates(I->getParent(), BB);
337 return true;
338}
339
Dan Gohman6e70e312009-09-27 15:26:03 +0000340bool SCEVUnknown::properlyDominates(BasicBlock *BB, DominatorTree *DT) const {
341 if (Instruction *I = dyn_cast<Instruction>(getValue()))
342 return DT->properlyDominates(I->getParent(), BB);
343 return true;
344}
345
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000346const Type *SCEVUnknown::getType() const {
347 return V->getType();
348}
Chris Lattner53e677a2004-04-02 20:23:17 +0000349
Dan Gohman0f5efe52010-01-28 02:15:55 +0000350bool SCEVUnknown::isSizeOf(const Type *&AllocTy) const {
351 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(V))
352 if (VCE->getOpcode() == Instruction::PtrToInt)
353 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
354 if (CE->getOpcode() == Instruction::GetElementPtr)
355 if (CE->getOperand(0)->isNullValue()) {
356 const Type *Ty =
357 cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
358 if (CE->getNumOperands() == 2)
359 if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(1)))
360 if (CI->isOne()) {
361 AllocTy = Ty;
362 return true;
363 }
364 }
365
366 return false;
367}
368
369bool SCEVUnknown::isAlignOf(const Type *&AllocTy) const {
370 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(V))
371 if (VCE->getOpcode() == Instruction::PtrToInt)
372 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
373 if (CE->getOpcode() == Instruction::GetElementPtr)
374 if (CE->getOperand(0)->isNullValue()) {
375 const Type *Ty =
376 cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
377 if (const StructType *STy = dyn_cast<StructType>(Ty))
Dan Gohman4f8eea82010-02-01 18:27:38 +0000378 if (!STy->isPacked() &&
379 CE->getNumOperands() == 3 &&
Dan Gohman0f5efe52010-01-28 02:15:55 +0000380 CE->getOperand(1)->isNullValue()) {
381 if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(2)))
382 if (CI->isOne() &&
383 STy->getNumElements() == 2 &&
384 STy->getElementType(0)->isInteger(1)) {
385 AllocTy = STy->getElementType(1);
386 return true;
387 }
388 }
389 }
390
391 return false;
392}
393
Dan Gohman4f8eea82010-02-01 18:27:38 +0000394bool SCEVUnknown::isOffsetOf(const Type *&CTy, Constant *&FieldNo) const {
395 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(V))
396 if (VCE->getOpcode() == Instruction::PtrToInt)
397 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
398 if (CE->getOpcode() == Instruction::GetElementPtr &&
399 CE->getNumOperands() == 3 &&
400 CE->getOperand(0)->isNullValue() &&
401 CE->getOperand(1)->isNullValue()) {
402 const Type *Ty =
403 cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
404 // Ignore vector types here so that ScalarEvolutionExpander doesn't
405 // emit getelementptrs that index into vectors.
406 if (isa<StructType>(Ty) || isa<ArrayType>(Ty)) {
407 CTy = Ty;
408 FieldNo = CE->getOperand(2);
409 return true;
410 }
411 }
412
413 return false;
414}
415
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000416void SCEVUnknown::print(raw_ostream &OS) const {
Dan Gohman0f5efe52010-01-28 02:15:55 +0000417 const Type *AllocTy;
418 if (isSizeOf(AllocTy)) {
419 OS << "sizeof(" << *AllocTy << ")";
420 return;
421 }
422 if (isAlignOf(AllocTy)) {
423 OS << "alignof(" << *AllocTy << ")";
424 return;
425 }
426
Dan Gohman4f8eea82010-02-01 18:27:38 +0000427 const Type *CTy;
Dan Gohman0f5efe52010-01-28 02:15:55 +0000428 Constant *FieldNo;
Dan Gohman4f8eea82010-02-01 18:27:38 +0000429 if (isOffsetOf(CTy, FieldNo)) {
430 OS << "offsetof(" << *CTy << ", ";
Dan Gohman0f5efe52010-01-28 02:15:55 +0000431 WriteAsOperand(OS, FieldNo, false);
432 OS << ")";
433 return;
434 }
435
436 // Otherwise just print it normally.
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000437 WriteAsOperand(OS, V, false);
Chris Lattner53e677a2004-04-02 20:23:17 +0000438}
439
Chris Lattner8d741b82004-06-20 06:23:15 +0000440//===----------------------------------------------------------------------===//
441// SCEV Utilities
442//===----------------------------------------------------------------------===//
443
Dan Gohmanc40f17b2009-08-18 16:46:41 +0000444static bool CompareTypes(const Type *A, const Type *B) {
445 if (A->getTypeID() != B->getTypeID())
446 return A->getTypeID() < B->getTypeID();
447 if (const IntegerType *AI = dyn_cast<IntegerType>(A)) {
448 const IntegerType *BI = cast<IntegerType>(B);
449 return AI->getBitWidth() < BI->getBitWidth();
450 }
451 if (const PointerType *AI = dyn_cast<PointerType>(A)) {
452 const PointerType *BI = cast<PointerType>(B);
453 return CompareTypes(AI->getElementType(), BI->getElementType());
454 }
455 if (const ArrayType *AI = dyn_cast<ArrayType>(A)) {
456 const ArrayType *BI = cast<ArrayType>(B);
457 if (AI->getNumElements() != BI->getNumElements())
458 return AI->getNumElements() < BI->getNumElements();
459 return CompareTypes(AI->getElementType(), BI->getElementType());
460 }
461 if (const VectorType *AI = dyn_cast<VectorType>(A)) {
462 const VectorType *BI = cast<VectorType>(B);
463 if (AI->getNumElements() != BI->getNumElements())
464 return AI->getNumElements() < BI->getNumElements();
465 return CompareTypes(AI->getElementType(), BI->getElementType());
466 }
467 if (const StructType *AI = dyn_cast<StructType>(A)) {
468 const StructType *BI = cast<StructType>(B);
469 if (AI->getNumElements() != BI->getNumElements())
470 return AI->getNumElements() < BI->getNumElements();
471 for (unsigned i = 0, e = AI->getNumElements(); i != e; ++i)
472 if (CompareTypes(AI->getElementType(i), BI->getElementType(i)) ||
473 CompareTypes(BI->getElementType(i), AI->getElementType(i)))
474 return CompareTypes(AI->getElementType(i), BI->getElementType(i));
475 }
476 return false;
477}
478
Chris Lattner8d741b82004-06-20 06:23:15 +0000479namespace {
480 /// SCEVComplexityCompare - Return true if the complexity of the LHS is less
481 /// than the complexity of the RHS. This comparator is used to canonicalize
482 /// expressions.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000483 class SCEVComplexityCompare {
Dan Gohman72861302009-05-07 14:39:04 +0000484 LoopInfo *LI;
485 public:
486 explicit SCEVComplexityCompare(LoopInfo *li) : LI(li) {}
487
Dan Gohmanf7b37b22008-04-14 18:23:56 +0000488 bool operator()(const SCEV *LHS, const SCEV *RHS) const {
Dan Gohman42214892009-08-31 21:15:23 +0000489 // Fast-path: SCEVs are uniqued so we can do a quick equality check.
490 if (LHS == RHS)
491 return false;
492
Dan Gohman72861302009-05-07 14:39:04 +0000493 // Primarily, sort the SCEVs by their getSCEVType().
494 if (LHS->getSCEVType() != RHS->getSCEVType())
495 return LHS->getSCEVType() < RHS->getSCEVType();
496
497 // Aside from the getSCEVType() ordering, the particular ordering
498 // isn't very important except that it's beneficial to be consistent,
499 // so that (a + b) and (b + a) don't end up as different expressions.
500
501 // Sort SCEVUnknown values with some loose heuristics. TODO: This is
502 // not as complete as it could be.
503 if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) {
504 const SCEVUnknown *RU = cast<SCEVUnknown>(RHS);
505
Dan Gohman5be18e82009-05-19 02:15:55 +0000506 // Order pointer values after integer values. This helps SCEVExpander
507 // form GEPs.
508 if (isa<PointerType>(LU->getType()) && !isa<PointerType>(RU->getType()))
509 return false;
510 if (isa<PointerType>(RU->getType()) && !isa<PointerType>(LU->getType()))
511 return true;
512
Dan Gohman72861302009-05-07 14:39:04 +0000513 // Compare getValueID values.
514 if (LU->getValue()->getValueID() != RU->getValue()->getValueID())
515 return LU->getValue()->getValueID() < RU->getValue()->getValueID();
516
517 // Sort arguments by their position.
518 if (const Argument *LA = dyn_cast<Argument>(LU->getValue())) {
519 const Argument *RA = cast<Argument>(RU->getValue());
520 return LA->getArgNo() < RA->getArgNo();
521 }
522
523 // For instructions, compare their loop depth, and their opcode.
524 // This is pretty loose.
525 if (Instruction *LV = dyn_cast<Instruction>(LU->getValue())) {
526 Instruction *RV = cast<Instruction>(RU->getValue());
527
528 // Compare loop depths.
529 if (LI->getLoopDepth(LV->getParent()) !=
530 LI->getLoopDepth(RV->getParent()))
531 return LI->getLoopDepth(LV->getParent()) <
532 LI->getLoopDepth(RV->getParent());
533
534 // Compare opcodes.
535 if (LV->getOpcode() != RV->getOpcode())
536 return LV->getOpcode() < RV->getOpcode();
537
538 // Compare the number of operands.
539 if (LV->getNumOperands() != RV->getNumOperands())
540 return LV->getNumOperands() < RV->getNumOperands();
541 }
542
543 return false;
544 }
545
Dan Gohman4dfad292009-06-14 22:51:25 +0000546 // Compare constant values.
547 if (const SCEVConstant *LC = dyn_cast<SCEVConstant>(LHS)) {
548 const SCEVConstant *RC = cast<SCEVConstant>(RHS);
Nick Lewyckyd1ec9892009-07-04 17:24:52 +0000549 if (LC->getValue()->getBitWidth() != RC->getValue()->getBitWidth())
550 return LC->getValue()->getBitWidth() < RC->getValue()->getBitWidth();
Dan Gohman4dfad292009-06-14 22:51:25 +0000551 return LC->getValue()->getValue().ult(RC->getValue()->getValue());
552 }
553
554 // Compare addrec loop depths.
555 if (const SCEVAddRecExpr *LA = dyn_cast<SCEVAddRecExpr>(LHS)) {
556 const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS);
557 if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth())
558 return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth();
559 }
Dan Gohman72861302009-05-07 14:39:04 +0000560
561 // Lexicographically compare n-ary expressions.
562 if (const SCEVNAryExpr *LC = dyn_cast<SCEVNAryExpr>(LHS)) {
563 const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS);
564 for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) {
565 if (i >= RC->getNumOperands())
566 return false;
567 if (operator()(LC->getOperand(i), RC->getOperand(i)))
568 return true;
569 if (operator()(RC->getOperand(i), LC->getOperand(i)))
570 return false;
571 }
572 return LC->getNumOperands() < RC->getNumOperands();
573 }
574
Dan Gohmana6b35e22009-05-07 19:23:21 +0000575 // Lexicographically compare udiv expressions.
576 if (const SCEVUDivExpr *LC = dyn_cast<SCEVUDivExpr>(LHS)) {
577 const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS);
578 if (operator()(LC->getLHS(), RC->getLHS()))
579 return true;
580 if (operator()(RC->getLHS(), LC->getLHS()))
581 return false;
582 if (operator()(LC->getRHS(), RC->getRHS()))
583 return true;
584 if (operator()(RC->getRHS(), LC->getRHS()))
585 return false;
586 return false;
587 }
588
Dan Gohman72861302009-05-07 14:39:04 +0000589 // Compare cast expressions by operand.
590 if (const SCEVCastExpr *LC = dyn_cast<SCEVCastExpr>(LHS)) {
591 const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS);
592 return operator()(LC->getOperand(), RC->getOperand());
593 }
594
Torok Edwinc23197a2009-07-14 16:55:14 +0000595 llvm_unreachable("Unknown SCEV kind!");
Dan Gohman72861302009-05-07 14:39:04 +0000596 return false;
Chris Lattner8d741b82004-06-20 06:23:15 +0000597 }
598 };
599}
600
601/// GroupByComplexity - Given a list of SCEV objects, order them by their
602/// complexity, and group objects of the same complexity together by value.
603/// When this routine is finished, we know that any duplicates in the vector are
604/// consecutive and that complexity is monotonically increasing.
605///
606/// Note that we go take special precautions to ensure that we get determinstic
607/// results from this routine. In other words, we don't want the results of
608/// this to depend on where the addresses of various SCEV objects happened to
609/// land in memory.
610///
Dan Gohman0bba49c2009-07-07 17:06:11 +0000611static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops,
Dan Gohman72861302009-05-07 14:39:04 +0000612 LoopInfo *LI) {
Chris Lattner8d741b82004-06-20 06:23:15 +0000613 if (Ops.size() < 2) return; // Noop
614 if (Ops.size() == 2) {
615 // This is the common case, which also happens to be trivially simple.
616 // Special case it.
Dan Gohman72861302009-05-07 14:39:04 +0000617 if (SCEVComplexityCompare(LI)(Ops[1], Ops[0]))
Chris Lattner8d741b82004-06-20 06:23:15 +0000618 std::swap(Ops[0], Ops[1]);
619 return;
620 }
621
622 // Do the rough sort by complexity.
Dan Gohman72861302009-05-07 14:39:04 +0000623 std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI));
Chris Lattner8d741b82004-06-20 06:23:15 +0000624
625 // Now that we are sorted by complexity, group elements of the same
626 // complexity. Note that this is, at worst, N^2, but the vector is likely to
627 // be extremely short in practice. Note that we take this approach because we
628 // do not want to depend on the addresses of the objects we are grouping.
Chris Lattner2d584522004-06-20 17:01:44 +0000629 for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
Dan Gohman35738ac2009-05-04 22:30:44 +0000630 const SCEV *S = Ops[i];
Chris Lattner8d741b82004-06-20 06:23:15 +0000631 unsigned Complexity = S->getSCEVType();
632
633 // If there are any objects of the same complexity and same value as this
634 // one, group them.
635 for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
636 if (Ops[j] == S) { // Found a duplicate.
637 // Move it to immediately after i'th element.
638 std::swap(Ops[i+1], Ops[j]);
639 ++i; // no need to rescan it.
Chris Lattner541ad5e2004-06-20 20:32:16 +0000640 if (i == e-2) return; // Done!
Chris Lattner8d741b82004-06-20 06:23:15 +0000641 }
642 }
643 }
644}
645
Chris Lattner53e677a2004-04-02 20:23:17 +0000646
Chris Lattner53e677a2004-04-02 20:23:17 +0000647
648//===----------------------------------------------------------------------===//
649// Simple SCEV method implementations
650//===----------------------------------------------------------------------===//
651
Eli Friedmanb42a6262008-08-04 23:49:06 +0000652/// BinomialCoefficient - Compute BC(It, K). The result has width W.
Dan Gohman6c0866c2009-05-24 23:45:28 +0000653/// Assume, K > 0.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000654static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K,
Dan Gohmanc2b015e2009-07-21 00:38:55 +0000655 ScalarEvolution &SE,
656 const Type* ResultTy) {
Eli Friedmanb42a6262008-08-04 23:49:06 +0000657 // Handle the simplest case efficiently.
658 if (K == 1)
659 return SE.getTruncateOrZeroExtend(It, ResultTy);
660
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000661 // We are using the following formula for BC(It, K):
662 //
663 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K!
664 //
Eli Friedmanb42a6262008-08-04 23:49:06 +0000665 // Suppose, W is the bitwidth of the return value. We must be prepared for
666 // overflow. Hence, we must assure that the result of our computation is
667 // equal to the accurate one modulo 2^W. Unfortunately, division isn't
668 // safe in modular arithmetic.
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000669 //
Eli Friedmanb42a6262008-08-04 23:49:06 +0000670 // However, this code doesn't use exactly that formula; the formula it uses
Dan Gohman64a845e2009-06-24 04:48:43 +0000671 // is something like the following, where T is the number of factors of 2 in
Eli Friedmanb42a6262008-08-04 23:49:06 +0000672 // K! (i.e. trailing zeros in the binary representation of K!), and ^ is
673 // exponentiation:
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000674 //
Eli Friedmanb42a6262008-08-04 23:49:06 +0000675 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T)
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000676 //
Eli Friedmanb42a6262008-08-04 23:49:06 +0000677 // This formula is trivially equivalent to the previous formula. However,
678 // this formula can be implemented much more efficiently. The trick is that
679 // K! / 2^T is odd, and exact division by an odd number *is* safe in modular
680 // arithmetic. To do exact division in modular arithmetic, all we have
681 // to do is multiply by the inverse. Therefore, this step can be done at
682 // width W.
Dan Gohman64a845e2009-06-24 04:48:43 +0000683 //
Eli Friedmanb42a6262008-08-04 23:49:06 +0000684 // The next issue is how to safely do the division by 2^T. The way this
685 // is done is by doing the multiplication step at a width of at least W + T
686 // bits. This way, the bottom W+T bits of the product are accurate. Then,
687 // when we perform the division by 2^T (which is equivalent to a right shift
688 // by T), the bottom W bits are accurate. Extra bits are okay; they'll get
689 // truncated out after the division by 2^T.
690 //
691 // In comparison to just directly using the first formula, this technique
692 // is much more efficient; using the first formula requires W * K bits,
693 // but this formula less than W + K bits. Also, the first formula requires
694 // a division step, whereas this formula only requires multiplies and shifts.
695 //
696 // It doesn't matter whether the subtraction step is done in the calculation
697 // width or the input iteration count's width; if the subtraction overflows,
698 // the result must be zero anyway. We prefer here to do it in the width of
699 // the induction variable because it helps a lot for certain cases; CodeGen
700 // isn't smart enough to ignore the overflow, which leads to much less
701 // efficient code if the width of the subtraction is wider than the native
702 // register width.
703 //
704 // (It's possible to not widen at all by pulling out factors of 2 before
705 // the multiplication; for example, K=2 can be calculated as
706 // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires
707 // extra arithmetic, so it's not an obvious win, and it gets
708 // much more complicated for K > 3.)
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000709
Eli Friedmanb42a6262008-08-04 23:49:06 +0000710 // Protection from insane SCEVs; this bound is conservative,
711 // but it probably doesn't matter.
712 if (K > 1000)
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +0000713 return SE.getCouldNotCompute();
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000714
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000715 unsigned W = SE.getTypeSizeInBits(ResultTy);
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000716
Eli Friedmanb42a6262008-08-04 23:49:06 +0000717 // Calculate K! / 2^T and T; we divide out the factors of two before
718 // multiplying for calculating K! / 2^T to avoid overflow.
719 // Other overflow doesn't matter because we only care about the bottom
720 // W bits of the result.
721 APInt OddFactorial(W, 1);
722 unsigned T = 1;
723 for (unsigned i = 3; i <= K; ++i) {
724 APInt Mult(W, i);
725 unsigned TwoFactors = Mult.countTrailingZeros();
726 T += TwoFactors;
727 Mult = Mult.lshr(TwoFactors);
728 OddFactorial *= Mult;
Chris Lattner53e677a2004-04-02 20:23:17 +0000729 }
Nick Lewycky6f8abf92008-06-13 04:38:55 +0000730
Eli Friedmanb42a6262008-08-04 23:49:06 +0000731 // We need at least W + T bits for the multiplication step
Nick Lewycky237d8732009-01-25 08:16:27 +0000732 unsigned CalculationBits = W + T;
Eli Friedmanb42a6262008-08-04 23:49:06 +0000733
734 // Calcuate 2^T, at width T+W.
735 APInt DivFactor = APInt(CalculationBits, 1).shl(T);
736
737 // Calculate the multiplicative inverse of K! / 2^T;
738 // this multiplication factor will perform the exact division by
739 // K! / 2^T.
740 APInt Mod = APInt::getSignedMinValue(W+1);
741 APInt MultiplyFactor = OddFactorial.zext(W+1);
742 MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod);
743 MultiplyFactor = MultiplyFactor.trunc(W);
744
745 // Calculate the product, at width T+W
Owen Anderson1d0be152009-08-13 21:58:54 +0000746 const IntegerType *CalculationTy = IntegerType::get(SE.getContext(),
747 CalculationBits);
Dan Gohman0bba49c2009-07-07 17:06:11 +0000748 const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy);
Eli Friedmanb42a6262008-08-04 23:49:06 +0000749 for (unsigned i = 1; i != K; ++i) {
Dan Gohman0bba49c2009-07-07 17:06:11 +0000750 const SCEV *S = SE.getMinusSCEV(It, SE.getIntegerSCEV(i, It->getType()));
Eli Friedmanb42a6262008-08-04 23:49:06 +0000751 Dividend = SE.getMulExpr(Dividend,
752 SE.getTruncateOrZeroExtend(S, CalculationTy));
753 }
754
755 // Divide by 2^T
Dan Gohman0bba49c2009-07-07 17:06:11 +0000756 const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor));
Eli Friedmanb42a6262008-08-04 23:49:06 +0000757
758 // Truncate the result, and divide by K! / 2^T.
759
760 return SE.getMulExpr(SE.getConstant(MultiplyFactor),
761 SE.getTruncateOrZeroExtend(DivResult, ResultTy));
Chris Lattner53e677a2004-04-02 20:23:17 +0000762}
763
Chris Lattner53e677a2004-04-02 20:23:17 +0000764/// evaluateAtIteration - Return the value of this chain of recurrences at
765/// the specified iteration number. We can evaluate this recurrence by
766/// multiplying each element in the chain by the binomial coefficient
767/// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as:
768///
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000769/// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3)
Chris Lattner53e677a2004-04-02 20:23:17 +0000770///
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000771/// where BC(It, k) stands for binomial coefficient.
Chris Lattner53e677a2004-04-02 20:23:17 +0000772///
Dan Gohman0bba49c2009-07-07 17:06:11 +0000773const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It,
Dan Gohmanc2b015e2009-07-21 00:38:55 +0000774 ScalarEvolution &SE) const {
Dan Gohman0bba49c2009-07-07 17:06:11 +0000775 const SCEV *Result = getStart();
Chris Lattner53e677a2004-04-02 20:23:17 +0000776 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000777 // The computation is correct in the face of overflow provided that the
778 // multiplication is performed _after_ the evaluation of the binomial
779 // coefficient.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000780 const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType());
Nick Lewyckycb8f1b52008-10-13 03:58:02 +0000781 if (isa<SCEVCouldNotCompute>(Coeff))
782 return Coeff;
783
784 Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff));
Chris Lattner53e677a2004-04-02 20:23:17 +0000785 }
786 return Result;
787}
788
Chris Lattner53e677a2004-04-02 20:23:17 +0000789//===----------------------------------------------------------------------===//
790// SCEV Expression folder implementations
791//===----------------------------------------------------------------------===//
792
Dan Gohman0bba49c2009-07-07 17:06:11 +0000793const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op,
Dan Gohmanf5074ec2009-07-13 22:05:32 +0000794 const Type *Ty) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000795 assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) &&
Dan Gohmanfb17fd22009-04-21 00:55:22 +0000796 "This is not a truncating conversion!");
Dan Gohman10b94792009-05-01 16:44:18 +0000797 assert(isSCEVable(Ty) &&
798 "This is not a conversion to a SCEVable type!");
799 Ty = getEffectiveSCEVType(Ty);
Dan Gohmanfb17fd22009-04-21 00:55:22 +0000800
Dan Gohmanc050fd92009-07-13 20:50:19 +0000801 FoldingSetNodeID ID;
802 ID.AddInteger(scTruncate);
803 ID.AddPointer(Op);
804 ID.AddPointer(Ty);
805 void *IP = 0;
806 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
807
Dan Gohmanc39f44b2009-06-30 20:13:32 +0000808 // Fold if the operand is constant.
Dan Gohman622ed672009-05-04 22:02:23 +0000809 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
Dan Gohmanb8be8b72009-06-24 00:38:39 +0000810 return getConstant(
811 cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty)));
Chris Lattner53e677a2004-04-02 20:23:17 +0000812
Dan Gohman20900ca2009-04-22 16:20:48 +0000813 // trunc(trunc(x)) --> trunc(x)
Dan Gohman622ed672009-05-04 22:02:23 +0000814 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op))
Dan Gohman20900ca2009-04-22 16:20:48 +0000815 return getTruncateExpr(ST->getOperand(), Ty);
816
Nick Lewycky5cd28fa2009-04-23 05:15:08 +0000817 // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing
Dan Gohman622ed672009-05-04 22:02:23 +0000818 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
Nick Lewycky5cd28fa2009-04-23 05:15:08 +0000819 return getTruncateOrSignExtend(SS->getOperand(), Ty);
820
821 // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing
Dan Gohman622ed672009-05-04 22:02:23 +0000822 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
Nick Lewycky5cd28fa2009-04-23 05:15:08 +0000823 return getTruncateOrZeroExtend(SZ->getOperand(), Ty);
824
Dan Gohman6864db62009-06-18 16:24:47 +0000825 // If the input value is a chrec scev, truncate the chrec's operands.
Dan Gohman622ed672009-05-04 22:02:23 +0000826 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
Dan Gohman0bba49c2009-07-07 17:06:11 +0000827 SmallVector<const SCEV *, 4> Operands;
Chris Lattner53e677a2004-04-02 20:23:17 +0000828 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
Dan Gohman728c7f32009-05-08 21:03:19 +0000829 Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty));
830 return getAddRecExpr(Operands, AddRec->getLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +0000831 }
832
Dan Gohmanc050fd92009-07-13 20:50:19 +0000833 // The cast wasn't folded; create an explicit cast node.
834 // Recompute the insert position, as it may have been invalidated.
Dan Gohman1c343752009-06-27 21:21:31 +0000835 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
836 SCEV *S = SCEVAllocator.Allocate<SCEVTruncateExpr>();
Dan Gohmanc050fd92009-07-13 20:50:19 +0000837 new (S) SCEVTruncateExpr(ID, Op, Ty);
Dan Gohman1c343752009-06-27 21:21:31 +0000838 UniqueSCEVs.InsertNode(S, IP);
839 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +0000840}
841
Dan Gohman0bba49c2009-07-07 17:06:11 +0000842const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op,
Dan Gohmanf5074ec2009-07-13 22:05:32 +0000843 const Type *Ty) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000844 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
Dan Gohman8170a682009-04-16 19:25:55 +0000845 "This is not an extending conversion!");
Dan Gohman10b94792009-05-01 16:44:18 +0000846 assert(isSCEVable(Ty) &&
847 "This is not a conversion to a SCEVable type!");
848 Ty = getEffectiveSCEVType(Ty);
Dan Gohman8170a682009-04-16 19:25:55 +0000849
Dan Gohmanc39f44b2009-06-30 20:13:32 +0000850 // Fold if the operand is constant.
Dan Gohman622ed672009-05-04 22:02:23 +0000851 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000852 const Type *IntTy = getEffectiveSCEVType(Ty);
Dan Gohman2d1be872009-04-16 03:18:22 +0000853 Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy);
854 if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty);
Dan Gohmanb8be8b72009-06-24 00:38:39 +0000855 return getConstant(cast<ConstantInt>(C));
Dan Gohman2d1be872009-04-16 03:18:22 +0000856 }
Chris Lattner53e677a2004-04-02 20:23:17 +0000857
Dan Gohman20900ca2009-04-22 16:20:48 +0000858 // zext(zext(x)) --> zext(x)
Dan Gohman622ed672009-05-04 22:02:23 +0000859 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
Dan Gohman20900ca2009-04-22 16:20:48 +0000860 return getZeroExtendExpr(SZ->getOperand(), Ty);
861
Dan Gohman69fbc7f2009-07-13 20:55:53 +0000862 // Before doing any expensive analysis, check to see if we've already
863 // computed a SCEV for this Op and Ty.
864 FoldingSetNodeID ID;
865 ID.AddInteger(scZeroExtend);
866 ID.AddPointer(Op);
867 ID.AddPointer(Ty);
868 void *IP = 0;
869 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
870
Dan Gohman01ecca22009-04-27 20:16:15 +0000871 // If the input value is a chrec scev, and we can prove that the value
Chris Lattner53e677a2004-04-02 20:23:17 +0000872 // did not overflow the old, smaller, value, we can zero extend all of the
Dan Gohman01ecca22009-04-27 20:16:15 +0000873 // operands (often constants). This allows analysis of something like
Chris Lattner53e677a2004-04-02 20:23:17 +0000874 // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
Dan Gohman622ed672009-05-04 22:02:23 +0000875 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
Dan Gohman01ecca22009-04-27 20:16:15 +0000876 if (AR->isAffine()) {
Dan Gohman85b05a22009-07-13 21:35:55 +0000877 const SCEV *Start = AR->getStart();
878 const SCEV *Step = AR->getStepRecurrence(*this);
879 unsigned BitWidth = getTypeSizeInBits(AR->getType());
880 const Loop *L = AR->getLoop();
881
Dan Gohmaneb490a72009-07-25 01:22:26 +0000882 // If we have special knowledge that this addrec won't overflow,
883 // we don't need to do any further analysis.
Dan Gohman5078f842009-08-20 17:11:38 +0000884 if (AR->hasNoUnsignedWrap())
Dan Gohmaneb490a72009-07-25 01:22:26 +0000885 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
886 getZeroExtendExpr(Step, Ty),
887 L);
888
Dan Gohman01ecca22009-04-27 20:16:15 +0000889 // Check whether the backedge-taken count is SCEVCouldNotCompute.
890 // Note that this serves two purposes: It filters out loops that are
891 // simply not analyzable, and it covers the case where this code is
892 // being called from within backedge-taken count analysis, such that
893 // attempting to ask for the backedge-taken count would likely result
894 // in infinite recursion. In the later case, the analysis code will
895 // cope with a conservative value, and it will take care to purge
896 // that value once it has finished.
Dan Gohman85b05a22009-07-13 21:35:55 +0000897 const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
Dan Gohmana1af7572009-04-30 20:47:05 +0000898 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
Dan Gohmanf0aa4852009-04-29 01:54:20 +0000899 // Manually compute the final value for AR, checking for
Dan Gohmanac70cea2009-04-29 22:28:28 +0000900 // overflow.
Dan Gohman01ecca22009-04-27 20:16:15 +0000901
902 // Check whether the backedge-taken count can be losslessly casted to
903 // the addrec's type. The count is always unsigned.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000904 const SCEV *CastedMaxBECount =
Dan Gohmana1af7572009-04-30 20:47:05 +0000905 getTruncateOrZeroExtend(MaxBECount, Start->getType());
Dan Gohman0bba49c2009-07-07 17:06:11 +0000906 const SCEV *RecastedMaxBECount =
Dan Gohman5183cae2009-05-18 15:58:39 +0000907 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
908 if (MaxBECount == RecastedMaxBECount) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000909 const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
Dan Gohmana1af7572009-04-30 20:47:05 +0000910 // Check whether Start+Step*MaxBECount has no unsigned overflow.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000911 const SCEV *ZMul =
Dan Gohmana1af7572009-04-30 20:47:05 +0000912 getMulExpr(CastedMaxBECount,
Dan Gohman01ecca22009-04-27 20:16:15 +0000913 getTruncateOrZeroExtend(Step, Start->getType()));
Dan Gohman0bba49c2009-07-07 17:06:11 +0000914 const SCEV *Add = getAddExpr(Start, ZMul);
915 const SCEV *OperandExtendedAdd =
Dan Gohman5183cae2009-05-18 15:58:39 +0000916 getAddExpr(getZeroExtendExpr(Start, WideTy),
917 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
918 getZeroExtendExpr(Step, WideTy)));
919 if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohmanac70cea2009-04-29 22:28:28 +0000920 // Return the expression with the addrec on the outside.
921 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
922 getZeroExtendExpr(Step, Ty),
Dan Gohman85b05a22009-07-13 21:35:55 +0000923 L);
Dan Gohman01ecca22009-04-27 20:16:15 +0000924
925 // Similar to above, only this time treat the step value as signed.
926 // This covers loops that count down.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000927 const SCEV *SMul =
Dan Gohmana1af7572009-04-30 20:47:05 +0000928 getMulExpr(CastedMaxBECount,
Dan Gohman01ecca22009-04-27 20:16:15 +0000929 getTruncateOrSignExtend(Step, Start->getType()));
Dan Gohmanac70cea2009-04-29 22:28:28 +0000930 Add = getAddExpr(Start, SMul);
Dan Gohman5183cae2009-05-18 15:58:39 +0000931 OperandExtendedAdd =
932 getAddExpr(getZeroExtendExpr(Start, WideTy),
933 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
934 getSignExtendExpr(Step, WideTy)));
935 if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohmanac70cea2009-04-29 22:28:28 +0000936 // Return the expression with the addrec on the outside.
937 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
938 getSignExtendExpr(Step, Ty),
Dan Gohman85b05a22009-07-13 21:35:55 +0000939 L);
940 }
941
942 // If the backedge is guarded by a comparison with the pre-inc value
943 // the addrec is safe. Also, if the entry is guarded by a comparison
944 // with the start value and the backedge is guarded by a comparison
945 // with the post-inc value, the addrec is safe.
946 if (isKnownPositive(Step)) {
947 const SCEV *N = getConstant(APInt::getMinValue(BitWidth) -
948 getUnsignedRange(Step).getUnsignedMax());
949 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) ||
950 (isLoopGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) &&
951 isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT,
952 AR->getPostIncExpr(*this), N)))
953 // Return the expression with the addrec on the outside.
954 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
955 getZeroExtendExpr(Step, Ty),
956 L);
957 } else if (isKnownNegative(Step)) {
958 const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) -
959 getSignedRange(Step).getSignedMin());
960 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) &&
961 (isLoopGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) ||
962 isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT,
963 AR->getPostIncExpr(*this), N)))
964 // Return the expression with the addrec on the outside.
965 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
966 getSignExtendExpr(Step, Ty),
967 L);
Dan Gohman01ecca22009-04-27 20:16:15 +0000968 }
969 }
970 }
Chris Lattner53e677a2004-04-02 20:23:17 +0000971
Dan Gohman69fbc7f2009-07-13 20:55:53 +0000972 // The cast wasn't folded; create an explicit cast node.
973 // Recompute the insert position, as it may have been invalidated.
Dan Gohman1c343752009-06-27 21:21:31 +0000974 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
975 SCEV *S = SCEVAllocator.Allocate<SCEVZeroExtendExpr>();
Dan Gohmanc050fd92009-07-13 20:50:19 +0000976 new (S) SCEVZeroExtendExpr(ID, Op, Ty);
Dan Gohman1c343752009-06-27 21:21:31 +0000977 UniqueSCEVs.InsertNode(S, IP);
978 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +0000979}
980
Dan Gohman0bba49c2009-07-07 17:06:11 +0000981const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op,
Dan Gohmanf5074ec2009-07-13 22:05:32 +0000982 const Type *Ty) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000983 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
Dan Gohmanfb17fd22009-04-21 00:55:22 +0000984 "This is not an extending conversion!");
Dan Gohman10b94792009-05-01 16:44:18 +0000985 assert(isSCEVable(Ty) &&
986 "This is not a conversion to a SCEVable type!");
987 Ty = getEffectiveSCEVType(Ty);
Dan Gohmanfb17fd22009-04-21 00:55:22 +0000988
Dan Gohmanc39f44b2009-06-30 20:13:32 +0000989 // Fold if the operand is constant.
Dan Gohman622ed672009-05-04 22:02:23 +0000990 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000991 const Type *IntTy = getEffectiveSCEVType(Ty);
Dan Gohman2d1be872009-04-16 03:18:22 +0000992 Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy);
993 if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty);
Dan Gohmanb8be8b72009-06-24 00:38:39 +0000994 return getConstant(cast<ConstantInt>(C));
Dan Gohman2d1be872009-04-16 03:18:22 +0000995 }
Dan Gohmand19534a2007-06-15 14:38:12 +0000996
Dan Gohman20900ca2009-04-22 16:20:48 +0000997 // sext(sext(x)) --> sext(x)
Dan Gohman622ed672009-05-04 22:02:23 +0000998 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
Dan Gohman20900ca2009-04-22 16:20:48 +0000999 return getSignExtendExpr(SS->getOperand(), Ty);
1000
Dan Gohman69fbc7f2009-07-13 20:55:53 +00001001 // Before doing any expensive analysis, check to see if we've already
1002 // computed a SCEV for this Op and Ty.
1003 FoldingSetNodeID ID;
1004 ID.AddInteger(scSignExtend);
1005 ID.AddPointer(Op);
1006 ID.AddPointer(Ty);
1007 void *IP = 0;
1008 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1009
Dan Gohman01ecca22009-04-27 20:16:15 +00001010 // If the input value is a chrec scev, and we can prove that the value
Dan Gohmand19534a2007-06-15 14:38:12 +00001011 // did not overflow the old, smaller, value, we can sign extend all of the
Dan Gohman01ecca22009-04-27 20:16:15 +00001012 // operands (often constants). This allows analysis of something like
Dan Gohmand19534a2007-06-15 14:38:12 +00001013 // this: for (signed char X = 0; X < 100; ++X) { int Y = X; }
Dan Gohman622ed672009-05-04 22:02:23 +00001014 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
Dan Gohman01ecca22009-04-27 20:16:15 +00001015 if (AR->isAffine()) {
Dan Gohman85b05a22009-07-13 21:35:55 +00001016 const SCEV *Start = AR->getStart();
1017 const SCEV *Step = AR->getStepRecurrence(*this);
1018 unsigned BitWidth = getTypeSizeInBits(AR->getType());
1019 const Loop *L = AR->getLoop();
1020
Dan Gohmaneb490a72009-07-25 01:22:26 +00001021 // If we have special knowledge that this addrec won't overflow,
1022 // we don't need to do any further analysis.
Dan Gohman5078f842009-08-20 17:11:38 +00001023 if (AR->hasNoSignedWrap())
Dan Gohmaneb490a72009-07-25 01:22:26 +00001024 return getAddRecExpr(getSignExtendExpr(Start, Ty),
1025 getSignExtendExpr(Step, Ty),
1026 L);
1027
Dan Gohman01ecca22009-04-27 20:16:15 +00001028 // Check whether the backedge-taken count is SCEVCouldNotCompute.
1029 // Note that this serves two purposes: It filters out loops that are
1030 // simply not analyzable, and it covers the case where this code is
1031 // being called from within backedge-taken count analysis, such that
1032 // attempting to ask for the backedge-taken count would likely result
1033 // in infinite recursion. In the later case, the analysis code will
1034 // cope with a conservative value, and it will take care to purge
1035 // that value once it has finished.
Dan Gohman85b05a22009-07-13 21:35:55 +00001036 const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
Dan Gohmana1af7572009-04-30 20:47:05 +00001037 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
Dan Gohmanf0aa4852009-04-29 01:54:20 +00001038 // Manually compute the final value for AR, checking for
Dan Gohmanac70cea2009-04-29 22:28:28 +00001039 // overflow.
Dan Gohman01ecca22009-04-27 20:16:15 +00001040
1041 // Check whether the backedge-taken count can be losslessly casted to
Dan Gohmanac70cea2009-04-29 22:28:28 +00001042 // the addrec's type. The count is always unsigned.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001043 const SCEV *CastedMaxBECount =
Dan Gohmana1af7572009-04-30 20:47:05 +00001044 getTruncateOrZeroExtend(MaxBECount, Start->getType());
Dan Gohman0bba49c2009-07-07 17:06:11 +00001045 const SCEV *RecastedMaxBECount =
Dan Gohman5183cae2009-05-18 15:58:39 +00001046 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
1047 if (MaxBECount == RecastedMaxBECount) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001048 const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
Dan Gohmana1af7572009-04-30 20:47:05 +00001049 // Check whether Start+Step*MaxBECount has no signed overflow.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001050 const SCEV *SMul =
Dan Gohmana1af7572009-04-30 20:47:05 +00001051 getMulExpr(CastedMaxBECount,
Dan Gohman01ecca22009-04-27 20:16:15 +00001052 getTruncateOrSignExtend(Step, Start->getType()));
Dan Gohman0bba49c2009-07-07 17:06:11 +00001053 const SCEV *Add = getAddExpr(Start, SMul);
1054 const SCEV *OperandExtendedAdd =
Dan Gohman5183cae2009-05-18 15:58:39 +00001055 getAddExpr(getSignExtendExpr(Start, WideTy),
1056 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
1057 getSignExtendExpr(Step, WideTy)));
1058 if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohmanac70cea2009-04-29 22:28:28 +00001059 // Return the expression with the addrec on the outside.
1060 return getAddRecExpr(getSignExtendExpr(Start, Ty),
1061 getSignExtendExpr(Step, Ty),
Dan Gohman85b05a22009-07-13 21:35:55 +00001062 L);
Dan Gohman850f7912009-07-16 17:34:36 +00001063
1064 // Similar to above, only this time treat the step value as unsigned.
1065 // This covers loops that count up with an unsigned step.
1066 const SCEV *UMul =
1067 getMulExpr(CastedMaxBECount,
1068 getTruncateOrZeroExtend(Step, Start->getType()));
1069 Add = getAddExpr(Start, UMul);
1070 OperandExtendedAdd =
Dan Gohman19378d62009-07-25 16:03:30 +00001071 getAddExpr(getSignExtendExpr(Start, WideTy),
Dan Gohman850f7912009-07-16 17:34:36 +00001072 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
1073 getZeroExtendExpr(Step, WideTy)));
Dan Gohman19378d62009-07-25 16:03:30 +00001074 if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohman850f7912009-07-16 17:34:36 +00001075 // Return the expression with the addrec on the outside.
1076 return getAddRecExpr(getSignExtendExpr(Start, Ty),
1077 getZeroExtendExpr(Step, Ty),
1078 L);
Dan Gohman85b05a22009-07-13 21:35:55 +00001079 }
1080
1081 // If the backedge is guarded by a comparison with the pre-inc value
1082 // the addrec is safe. Also, if the entry is guarded by a comparison
1083 // with the start value and the backedge is guarded by a comparison
1084 // with the post-inc value, the addrec is safe.
1085 if (isKnownPositive(Step)) {
1086 const SCEV *N = getConstant(APInt::getSignedMinValue(BitWidth) -
1087 getSignedRange(Step).getSignedMax());
1088 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, AR, N) ||
1089 (isLoopGuardedByCond(L, ICmpInst::ICMP_SLT, Start, N) &&
1090 isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT,
1091 AR->getPostIncExpr(*this), N)))
1092 // Return the expression with the addrec on the outside.
1093 return getAddRecExpr(getSignExtendExpr(Start, Ty),
1094 getSignExtendExpr(Step, Ty),
1095 L);
1096 } else if (isKnownNegative(Step)) {
1097 const SCEV *N = getConstant(APInt::getSignedMaxValue(BitWidth) -
1098 getSignedRange(Step).getSignedMin());
1099 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, AR, N) ||
1100 (isLoopGuardedByCond(L, ICmpInst::ICMP_SGT, Start, N) &&
1101 isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT,
1102 AR->getPostIncExpr(*this), N)))
1103 // Return the expression with the addrec on the outside.
1104 return getAddRecExpr(getSignExtendExpr(Start, Ty),
1105 getSignExtendExpr(Step, Ty),
1106 L);
Dan Gohman01ecca22009-04-27 20:16:15 +00001107 }
1108 }
1109 }
Dan Gohmand19534a2007-06-15 14:38:12 +00001110
Dan Gohman69fbc7f2009-07-13 20:55:53 +00001111 // The cast wasn't folded; create an explicit cast node.
1112 // Recompute the insert position, as it may have been invalidated.
Dan Gohman1c343752009-06-27 21:21:31 +00001113 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1114 SCEV *S = SCEVAllocator.Allocate<SCEVSignExtendExpr>();
Dan Gohmanc050fd92009-07-13 20:50:19 +00001115 new (S) SCEVSignExtendExpr(ID, Op, Ty);
Dan Gohman1c343752009-06-27 21:21:31 +00001116 UniqueSCEVs.InsertNode(S, IP);
1117 return S;
Dan Gohmand19534a2007-06-15 14:38:12 +00001118}
1119
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00001120/// getAnyExtendExpr - Return a SCEV for the given operand extended with
1121/// unspecified bits out to the given type.
1122///
Dan Gohman0bba49c2009-07-07 17:06:11 +00001123const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op,
Dan Gohmanc40f17b2009-08-18 16:46:41 +00001124 const Type *Ty) {
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00001125 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1126 "This is not an extending conversion!");
1127 assert(isSCEVable(Ty) &&
1128 "This is not a conversion to a SCEVable type!");
1129 Ty = getEffectiveSCEVType(Ty);
1130
1131 // Sign-extend negative constants.
1132 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1133 if (SC->getValue()->getValue().isNegative())
1134 return getSignExtendExpr(Op, Ty);
1135
1136 // Peel off a truncate cast.
1137 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001138 const SCEV *NewOp = T->getOperand();
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00001139 if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty))
1140 return getAnyExtendExpr(NewOp, Ty);
1141 return getTruncateOrNoop(NewOp, Ty);
1142 }
1143
1144 // Next try a zext cast. If the cast is folded, use it.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001145 const SCEV *ZExt = getZeroExtendExpr(Op, Ty);
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00001146 if (!isa<SCEVZeroExtendExpr>(ZExt))
1147 return ZExt;
1148
1149 // Next try a sext cast. If the cast is folded, use it.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001150 const SCEV *SExt = getSignExtendExpr(Op, Ty);
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00001151 if (!isa<SCEVSignExtendExpr>(SExt))
1152 return SExt;
1153
Dan Gohmana10756e2010-01-21 02:09:26 +00001154 // Force the cast to be folded into the operands of an addrec.
1155 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) {
1156 SmallVector<const SCEV *, 4> Ops;
1157 for (SCEVAddRecExpr::op_iterator I = AR->op_begin(), E = AR->op_end();
1158 I != E; ++I)
1159 Ops.push_back(getAnyExtendExpr(*I, Ty));
1160 return getAddRecExpr(Ops, AR->getLoop());
1161 }
1162
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00001163 // If the expression is obviously signed, use the sext cast value.
1164 if (isa<SCEVSMaxExpr>(Op))
1165 return SExt;
1166
1167 // Absent any other information, use the zext cast value.
1168 return ZExt;
1169}
1170
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001171/// CollectAddOperandsWithScales - Process the given Ops list, which is
1172/// a list of operands to be added under the given scale, update the given
1173/// map. This is a helper function for getAddRecExpr. As an example of
1174/// what it does, given a sequence of operands that would form an add
1175/// expression like this:
1176///
1177/// m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r)
1178///
1179/// where A and B are constants, update the map with these values:
1180///
1181/// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0)
1182///
1183/// and add 13 + A*B*29 to AccumulatedConstant.
1184/// This will allow getAddRecExpr to produce this:
1185///
1186/// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B)
1187///
1188/// This form often exposes folding opportunities that are hidden in
1189/// the original operand list.
1190///
1191/// Return true iff it appears that any interesting folding opportunities
1192/// may be exposed. This helps getAddRecExpr short-circuit extra work in
1193/// the common case where no interesting opportunities are present, and
1194/// is also used as a check to avoid infinite recursion.
1195///
1196static bool
Dan Gohman0bba49c2009-07-07 17:06:11 +00001197CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M,
1198 SmallVector<const SCEV *, 8> &NewOps,
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001199 APInt &AccumulatedConstant,
Dan Gohman0bba49c2009-07-07 17:06:11 +00001200 const SmallVectorImpl<const SCEV *> &Ops,
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001201 const APInt &Scale,
1202 ScalarEvolution &SE) {
1203 bool Interesting = false;
1204
1205 // Iterate over the add operands.
1206 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1207 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]);
1208 if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) {
1209 APInt NewScale =
1210 Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue();
1211 if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) {
1212 // A multiplication of a constant with another add; recurse.
1213 Interesting |=
1214 CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
1215 cast<SCEVAddExpr>(Mul->getOperand(1))
1216 ->getOperands(),
1217 NewScale, SE);
1218 } else {
1219 // A multiplication of a constant with some other value. Update
1220 // the map.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001221 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end());
1222 const SCEV *Key = SE.getMulExpr(MulOps);
1223 std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
Dan Gohman23737e02009-06-29 18:25:52 +00001224 M.insert(std::make_pair(Key, NewScale));
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001225 if (Pair.second) {
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001226 NewOps.push_back(Pair.first->first);
1227 } else {
1228 Pair.first->second += NewScale;
1229 // The map already had an entry for this value, which may indicate
1230 // a folding opportunity.
1231 Interesting = true;
1232 }
1233 }
1234 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
1235 // Pull a buried constant out to the outside.
1236 if (Scale != 1 || AccumulatedConstant != 0 || C->isZero())
1237 Interesting = true;
1238 AccumulatedConstant += Scale * C->getValue()->getValue();
1239 } else {
1240 // An ordinary operand. Update the map.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001241 std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
Dan Gohman23737e02009-06-29 18:25:52 +00001242 M.insert(std::make_pair(Ops[i], Scale));
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001243 if (Pair.second) {
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001244 NewOps.push_back(Pair.first->first);
1245 } else {
1246 Pair.first->second += Scale;
1247 // The map already had an entry for this value, which may indicate
1248 // a folding opportunity.
1249 Interesting = true;
1250 }
1251 }
1252 }
1253
1254 return Interesting;
1255}
1256
1257namespace {
1258 struct APIntCompare {
1259 bool operator()(const APInt &LHS, const APInt &RHS) const {
1260 return LHS.ult(RHS);
1261 }
1262 };
1263}
1264
Dan Gohman6c0866c2009-05-24 23:45:28 +00001265/// getAddExpr - Get a canonical add expression, or something simpler if
1266/// possible.
Dan Gohman3645b012009-10-09 00:10:36 +00001267const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
1268 bool HasNUW, bool HasNSW) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001269 assert(!Ops.empty() && "Cannot get empty add!");
Chris Lattner627018b2004-04-07 16:16:11 +00001270 if (Ops.size() == 1) return Ops[0];
Dan Gohmanf78a9782009-05-18 15:44:58 +00001271#ifndef NDEBUG
1272 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1273 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1274 getEffectiveSCEVType(Ops[0]->getType()) &&
1275 "SCEVAddExpr operand types don't match!");
1276#endif
Chris Lattner53e677a2004-04-02 20:23:17 +00001277
Dan Gohmana10756e2010-01-21 02:09:26 +00001278 // If HasNSW is true and all the operands are non-negative, infer HasNUW.
1279 if (!HasNUW && HasNSW) {
1280 bool All = true;
1281 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1282 if (!isKnownNonNegative(Ops[i])) {
1283 All = false;
1284 break;
1285 }
1286 if (All) HasNUW = true;
1287 }
1288
Chris Lattner53e677a2004-04-02 20:23:17 +00001289 // Sort by complexity, this groups all similar expression types together.
Dan Gohman72861302009-05-07 14:39:04 +00001290 GroupByComplexity(Ops, LI);
Chris Lattner53e677a2004-04-02 20:23:17 +00001291
1292 // If there are any constants, fold them together.
1293 unsigned Idx = 0;
Dan Gohman622ed672009-05-04 22:02:23 +00001294 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001295 ++Idx;
Chris Lattner627018b2004-04-07 16:16:11 +00001296 assert(Idx < Ops.size());
Dan Gohman622ed672009-05-04 22:02:23 +00001297 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001298 // We found two constants, fold them together!
Dan Gohmana82752c2009-06-14 22:47:23 +00001299 Ops[0] = getConstant(LHSC->getValue()->getValue() +
1300 RHSC->getValue()->getValue());
Dan Gohman7f7c4362009-06-14 22:53:57 +00001301 if (Ops.size() == 2) return Ops[0];
Nick Lewycky3e630762008-02-20 06:48:22 +00001302 Ops.erase(Ops.begin()+1); // Erase the folded element
Nick Lewycky3e630762008-02-20 06:48:22 +00001303 LHSC = cast<SCEVConstant>(Ops[0]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001304 }
1305
1306 // If we are left with a constant zero being added, strip it off.
Reid Spencercae57542007-03-02 00:28:52 +00001307 if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001308 Ops.erase(Ops.begin());
1309 --Idx;
1310 }
1311 }
1312
Chris Lattner627018b2004-04-07 16:16:11 +00001313 if (Ops.size() == 1) return Ops[0];
Misha Brukman2b37d7c2005-04-21 21:13:18 +00001314
Chris Lattner53e677a2004-04-02 20:23:17 +00001315 // Okay, check to see if the same value occurs in the operand list twice. If
1316 // so, merge them together into an multiply expression. Since we sorted the
1317 // list, these values are required to be adjacent.
1318 const Type *Ty = Ops[0]->getType();
1319 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
1320 if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2
1321 // Found a match, merge the two values into a multiply, and add any
1322 // remaining values to the result.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001323 const SCEV *Two = getIntegerSCEV(2, Ty);
1324 const SCEV *Mul = getMulExpr(Ops[i], Two);
Chris Lattner53e677a2004-04-02 20:23:17 +00001325 if (Ops.size() == 2)
1326 return Mul;
1327 Ops.erase(Ops.begin()+i, Ops.begin()+i+2);
1328 Ops.push_back(Mul);
Dan Gohman3645b012009-10-09 00:10:36 +00001329 return getAddExpr(Ops, HasNUW, HasNSW);
Chris Lattner53e677a2004-04-02 20:23:17 +00001330 }
1331
Dan Gohman728c7f32009-05-08 21:03:19 +00001332 // Check for truncates. If all the operands are truncated from the same
1333 // type, see if factoring out the truncate would permit the result to be
1334 // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n)
1335 // if the contents of the resulting outer trunc fold to something simple.
1336 for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) {
1337 const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]);
1338 const Type *DstType = Trunc->getType();
1339 const Type *SrcType = Trunc->getOperand()->getType();
Dan Gohman0bba49c2009-07-07 17:06:11 +00001340 SmallVector<const SCEV *, 8> LargeOps;
Dan Gohman728c7f32009-05-08 21:03:19 +00001341 bool Ok = true;
1342 // Check all the operands to see if they can be represented in the
1343 // source type of the truncate.
1344 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1345 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) {
1346 if (T->getOperand()->getType() != SrcType) {
1347 Ok = false;
1348 break;
1349 }
1350 LargeOps.push_back(T->getOperand());
1351 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
1352 // This could be either sign or zero extension, but sign extension
1353 // is much more likely to be foldable here.
1354 LargeOps.push_back(getSignExtendExpr(C, SrcType));
1355 } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001356 SmallVector<const SCEV *, 8> LargeMulOps;
Dan Gohman728c7f32009-05-08 21:03:19 +00001357 for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
1358 if (const SCEVTruncateExpr *T =
1359 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) {
1360 if (T->getOperand()->getType() != SrcType) {
1361 Ok = false;
1362 break;
1363 }
1364 LargeMulOps.push_back(T->getOperand());
1365 } else if (const SCEVConstant *C =
1366 dyn_cast<SCEVConstant>(M->getOperand(j))) {
1367 // This could be either sign or zero extension, but sign extension
1368 // is much more likely to be foldable here.
1369 LargeMulOps.push_back(getSignExtendExpr(C, SrcType));
1370 } else {
1371 Ok = false;
1372 break;
1373 }
1374 }
1375 if (Ok)
1376 LargeOps.push_back(getMulExpr(LargeMulOps));
1377 } else {
1378 Ok = false;
1379 break;
1380 }
1381 }
1382 if (Ok) {
1383 // Evaluate the expression in the larger type.
Dan Gohman3645b012009-10-09 00:10:36 +00001384 const SCEV *Fold = getAddExpr(LargeOps, HasNUW, HasNSW);
Dan Gohman728c7f32009-05-08 21:03:19 +00001385 // If it folds to something simple, use it. Otherwise, don't.
1386 if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold))
1387 return getTruncateExpr(Fold, DstType);
1388 }
1389 }
1390
1391 // Skip past any other cast SCEVs.
Dan Gohmanf50cd742007-06-18 19:30:09 +00001392 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr)
1393 ++Idx;
1394
1395 // If there are add operands they would be next.
Chris Lattner53e677a2004-04-02 20:23:17 +00001396 if (Idx < Ops.size()) {
1397 bool DeletedAdd = false;
Dan Gohman622ed672009-05-04 22:02:23 +00001398 while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001399 // If we have an add, expand the add operands onto the end of the operands
1400 // list.
1401 Ops.insert(Ops.end(), Add->op_begin(), Add->op_end());
1402 Ops.erase(Ops.begin()+Idx);
1403 DeletedAdd = true;
1404 }
1405
1406 // If we deleted at least one add, we added operands to the end of the list,
1407 // and they are not necessarily sorted. Recurse to resort and resimplify
1408 // any operands we just aquired.
1409 if (DeletedAdd)
Dan Gohman246b2562007-10-22 18:31:58 +00001410 return getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001411 }
1412
1413 // Skip over the add expression until we get to a multiply.
1414 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
1415 ++Idx;
1416
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001417 // Check to see if there are any folding opportunities present with
1418 // operands multiplied by constant values.
1419 if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) {
1420 uint64_t BitWidth = getTypeSizeInBits(Ty);
Dan Gohman0bba49c2009-07-07 17:06:11 +00001421 DenseMap<const SCEV *, APInt> M;
1422 SmallVector<const SCEV *, 8> NewOps;
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001423 APInt AccumulatedConstant(BitWidth, 0);
1424 if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
1425 Ops, APInt(BitWidth, 1), *this)) {
1426 // Some interesting folding opportunity is present, so its worthwhile to
1427 // re-generate the operands list. Group the operands by constant scale,
1428 // to avoid multiplying by the same constant scale multiple times.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001429 std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists;
1430 for (SmallVector<const SCEV *, 8>::iterator I = NewOps.begin(),
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001431 E = NewOps.end(); I != E; ++I)
1432 MulOpLists[M.find(*I)->second].push_back(*I);
1433 // Re-generate the operands list.
1434 Ops.clear();
1435 if (AccumulatedConstant != 0)
1436 Ops.push_back(getConstant(AccumulatedConstant));
Dan Gohman64a845e2009-06-24 04:48:43 +00001437 for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator
1438 I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I)
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001439 if (I->first != 0)
Dan Gohman64a845e2009-06-24 04:48:43 +00001440 Ops.push_back(getMulExpr(getConstant(I->first),
1441 getAddExpr(I->second)));
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001442 if (Ops.empty())
1443 return getIntegerSCEV(0, Ty);
1444 if (Ops.size() == 1)
1445 return Ops[0];
1446 return getAddExpr(Ops);
1447 }
1448 }
1449
Chris Lattner53e677a2004-04-02 20:23:17 +00001450 // If we are adding something to a multiply expression, make sure the
1451 // something is not already an operand of the multiply. If so, merge it into
1452 // the multiply.
1453 for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001454 const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001455 for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001456 const SCEV *MulOpSCEV = Mul->getOperand(MulOp);
Chris Lattner53e677a2004-04-02 20:23:17 +00001457 for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
Dan Gohmana82752c2009-06-14 22:47:23 +00001458 if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(Ops[AddOp])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001459 // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1))
Dan Gohman0bba49c2009-07-07 17:06:11 +00001460 const SCEV *InnerMul = Mul->getOperand(MulOp == 0);
Chris Lattner53e677a2004-04-02 20:23:17 +00001461 if (Mul->getNumOperands() != 2) {
1462 // If the multiply has more than two operands, we must get the
1463 // Y*Z term.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001464 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), Mul->op_end());
Chris Lattner53e677a2004-04-02 20:23:17 +00001465 MulOps.erase(MulOps.begin()+MulOp);
Dan Gohman246b2562007-10-22 18:31:58 +00001466 InnerMul = getMulExpr(MulOps);
Chris Lattner53e677a2004-04-02 20:23:17 +00001467 }
Dan Gohman0bba49c2009-07-07 17:06:11 +00001468 const SCEV *One = getIntegerSCEV(1, Ty);
1469 const SCEV *AddOne = getAddExpr(InnerMul, One);
1470 const SCEV *OuterMul = getMulExpr(AddOne, Ops[AddOp]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001471 if (Ops.size() == 2) return OuterMul;
1472 if (AddOp < Idx) {
1473 Ops.erase(Ops.begin()+AddOp);
1474 Ops.erase(Ops.begin()+Idx-1);
1475 } else {
1476 Ops.erase(Ops.begin()+Idx);
1477 Ops.erase(Ops.begin()+AddOp-1);
1478 }
1479 Ops.push_back(OuterMul);
Dan Gohman246b2562007-10-22 18:31:58 +00001480 return getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001481 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00001482
Chris Lattner53e677a2004-04-02 20:23:17 +00001483 // Check this multiply against other multiplies being added together.
1484 for (unsigned OtherMulIdx = Idx+1;
1485 OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
1486 ++OtherMulIdx) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001487 const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001488 // If MulOp occurs in OtherMul, we can fold the two multiplies
1489 // together.
1490 for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
1491 OMulOp != e; ++OMulOp)
1492 if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
1493 // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
Dan Gohman0bba49c2009-07-07 17:06:11 +00001494 const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0);
Chris Lattner53e677a2004-04-02 20:23:17 +00001495 if (Mul->getNumOperands() != 2) {
Dan Gohman64a845e2009-06-24 04:48:43 +00001496 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
1497 Mul->op_end());
Chris Lattner53e677a2004-04-02 20:23:17 +00001498 MulOps.erase(MulOps.begin()+MulOp);
Dan Gohman246b2562007-10-22 18:31:58 +00001499 InnerMul1 = getMulExpr(MulOps);
Chris Lattner53e677a2004-04-02 20:23:17 +00001500 }
Dan Gohman0bba49c2009-07-07 17:06:11 +00001501 const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0);
Chris Lattner53e677a2004-04-02 20:23:17 +00001502 if (OtherMul->getNumOperands() != 2) {
Dan Gohman64a845e2009-06-24 04:48:43 +00001503 SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(),
1504 OtherMul->op_end());
Chris Lattner53e677a2004-04-02 20:23:17 +00001505 MulOps.erase(MulOps.begin()+OMulOp);
Dan Gohman246b2562007-10-22 18:31:58 +00001506 InnerMul2 = getMulExpr(MulOps);
Chris Lattner53e677a2004-04-02 20:23:17 +00001507 }
Dan Gohman0bba49c2009-07-07 17:06:11 +00001508 const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2);
1509 const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum);
Chris Lattner53e677a2004-04-02 20:23:17 +00001510 if (Ops.size() == 2) return OuterMul;
1511 Ops.erase(Ops.begin()+Idx);
1512 Ops.erase(Ops.begin()+OtherMulIdx-1);
1513 Ops.push_back(OuterMul);
Dan Gohman246b2562007-10-22 18:31:58 +00001514 return getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001515 }
1516 }
1517 }
1518 }
1519
1520 // If there are any add recurrences in the operands list, see if any other
1521 // added values are loop invariant. If so, we can fold them into the
1522 // recurrence.
1523 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
1524 ++Idx;
1525
1526 // Scan over all recurrences, trying to fold loop invariants into them.
1527 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
1528 // Scan all of the other operands to this add and add them to the vector if
1529 // they are loop invariant w.r.t. the recurrence.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001530 SmallVector<const SCEV *, 8> LIOps;
Dan Gohman35738ac2009-05-04 22:30:44 +00001531 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001532 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1533 if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
1534 LIOps.push_back(Ops[i]);
1535 Ops.erase(Ops.begin()+i);
1536 --i; --e;
1537 }
1538
1539 // If we found some loop invariants, fold them into the recurrence.
1540 if (!LIOps.empty()) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001541 // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step}
Chris Lattner53e677a2004-04-02 20:23:17 +00001542 LIOps.push_back(AddRec->getStart());
1543
Dan Gohman0bba49c2009-07-07 17:06:11 +00001544 SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(),
Dan Gohman3a5d4092009-12-18 03:57:04 +00001545 AddRec->op_end());
Dan Gohman246b2562007-10-22 18:31:58 +00001546 AddRecOps[0] = getAddExpr(LIOps);
Chris Lattner53e677a2004-04-02 20:23:17 +00001547
Dan Gohman355b4f32009-12-19 01:46:34 +00001548 // It's tempting to propagate NUW/NSW flags here, but nuw/nsw addition
Dan Gohman59de33e2009-12-18 18:45:31 +00001549 // is not associative so this isn't necessarily safe.
Dan Gohman3a5d4092009-12-18 03:57:04 +00001550 const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRec->getLoop());
Dan Gohman59de33e2009-12-18 18:45:31 +00001551
Chris Lattner53e677a2004-04-02 20:23:17 +00001552 // If all of the other operands were loop invariant, we are done.
1553 if (Ops.size() == 1) return NewRec;
1554
1555 // Otherwise, add the folded AddRec by the non-liv parts.
1556 for (unsigned i = 0;; ++i)
1557 if (Ops[i] == AddRec) {
1558 Ops[i] = NewRec;
1559 break;
1560 }
Dan Gohman246b2562007-10-22 18:31:58 +00001561 return getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001562 }
1563
1564 // Okay, if there weren't any loop invariants to be folded, check to see if
1565 // there are multiple AddRec's with the same loop induction variable being
1566 // added together. If so, we can fold them.
1567 for (unsigned OtherIdx = Idx+1;
1568 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
1569 if (OtherIdx != Idx) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001570 const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001571 if (AddRec->getLoop() == OtherAddRec->getLoop()) {
1572 // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D}
Dan Gohman64a845e2009-06-24 04:48:43 +00001573 SmallVector<const SCEV *, 4> NewOps(AddRec->op_begin(),
1574 AddRec->op_end());
Chris Lattner53e677a2004-04-02 20:23:17 +00001575 for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) {
1576 if (i >= NewOps.size()) {
1577 NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i,
1578 OtherAddRec->op_end());
1579 break;
1580 }
Dan Gohman246b2562007-10-22 18:31:58 +00001581 NewOps[i] = getAddExpr(NewOps[i], OtherAddRec->getOperand(i));
Chris Lattner53e677a2004-04-02 20:23:17 +00001582 }
Dan Gohman0bba49c2009-07-07 17:06:11 +00001583 const SCEV *NewAddRec = getAddRecExpr(NewOps, AddRec->getLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +00001584
1585 if (Ops.size() == 2) return NewAddRec;
1586
1587 Ops.erase(Ops.begin()+Idx);
1588 Ops.erase(Ops.begin()+OtherIdx-1);
1589 Ops.push_back(NewAddRec);
Dan Gohman246b2562007-10-22 18:31:58 +00001590 return getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001591 }
1592 }
1593
1594 // Otherwise couldn't fold anything into this recurrence. Move onto the
1595 // next one.
1596 }
1597
1598 // Okay, it looks like we really DO need an add expr. Check to see if we
1599 // already have one, otherwise create a new one.
Dan Gohman1c343752009-06-27 21:21:31 +00001600 FoldingSetNodeID ID;
1601 ID.AddInteger(scAddExpr);
1602 ID.AddInteger(Ops.size());
1603 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1604 ID.AddPointer(Ops[i]);
1605 void *IP = 0;
Dan Gohmana10756e2010-01-21 02:09:26 +00001606 SCEVAddExpr *S =
1607 static_cast<SCEVAddExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
1608 if (!S) {
1609 S = SCEVAllocator.Allocate<SCEVAddExpr>();
1610 new (S) SCEVAddExpr(ID, Ops);
1611 UniqueSCEVs.InsertNode(S, IP);
1612 }
Dan Gohman3645b012009-10-09 00:10:36 +00001613 if (HasNUW) S->setHasNoUnsignedWrap(true);
1614 if (HasNSW) S->setHasNoSignedWrap(true);
Dan Gohman1c343752009-06-27 21:21:31 +00001615 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +00001616}
1617
Dan Gohman6c0866c2009-05-24 23:45:28 +00001618/// getMulExpr - Get a canonical multiply expression, or something simpler if
1619/// possible.
Dan Gohman3645b012009-10-09 00:10:36 +00001620const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
1621 bool HasNUW, bool HasNSW) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001622 assert(!Ops.empty() && "Cannot get empty mul!");
Dan Gohmana10756e2010-01-21 02:09:26 +00001623 if (Ops.size() == 1) return Ops[0];
Dan Gohmanf78a9782009-05-18 15:44:58 +00001624#ifndef NDEBUG
1625 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1626 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1627 getEffectiveSCEVType(Ops[0]->getType()) &&
1628 "SCEVMulExpr operand types don't match!");
1629#endif
Chris Lattner53e677a2004-04-02 20:23:17 +00001630
Dan Gohmana10756e2010-01-21 02:09:26 +00001631 // If HasNSW is true and all the operands are non-negative, infer HasNUW.
1632 if (!HasNUW && HasNSW) {
1633 bool All = true;
1634 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1635 if (!isKnownNonNegative(Ops[i])) {
1636 All = false;
1637 break;
1638 }
1639 if (All) HasNUW = true;
1640 }
1641
Chris Lattner53e677a2004-04-02 20:23:17 +00001642 // Sort by complexity, this groups all similar expression types together.
Dan Gohman72861302009-05-07 14:39:04 +00001643 GroupByComplexity(Ops, LI);
Chris Lattner53e677a2004-04-02 20:23:17 +00001644
1645 // If there are any constants, fold them together.
1646 unsigned Idx = 0;
Dan Gohman622ed672009-05-04 22:02:23 +00001647 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001648
1649 // C1*(C2+V) -> C1*C2 + C1*V
1650 if (Ops.size() == 2)
Dan Gohman622ed672009-05-04 22:02:23 +00001651 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
Chris Lattner53e677a2004-04-02 20:23:17 +00001652 if (Add->getNumOperands() == 2 &&
1653 isa<SCEVConstant>(Add->getOperand(0)))
Dan Gohman246b2562007-10-22 18:31:58 +00001654 return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)),
1655 getMulExpr(LHSC, Add->getOperand(1)));
Chris Lattner53e677a2004-04-02 20:23:17 +00001656
Chris Lattner53e677a2004-04-02 20:23:17 +00001657 ++Idx;
Dan Gohman622ed672009-05-04 22:02:23 +00001658 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001659 // We found two constants, fold them together!
Owen Andersoneed707b2009-07-24 23:12:02 +00001660 ConstantInt *Fold = ConstantInt::get(getContext(),
1661 LHSC->getValue()->getValue() *
Nick Lewycky3e630762008-02-20 06:48:22 +00001662 RHSC->getValue()->getValue());
1663 Ops[0] = getConstant(Fold);
1664 Ops.erase(Ops.begin()+1); // Erase the folded element
1665 if (Ops.size() == 1) return Ops[0];
1666 LHSC = cast<SCEVConstant>(Ops[0]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001667 }
1668
1669 // If we are left with a constant one being multiplied, strip it off.
1670 if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) {
1671 Ops.erase(Ops.begin());
1672 --Idx;
Reid Spencercae57542007-03-02 00:28:52 +00001673 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001674 // If we have a multiply of zero, it will always be zero.
1675 return Ops[0];
Dan Gohmana10756e2010-01-21 02:09:26 +00001676 } else if (Ops[0]->isAllOnesValue()) {
1677 // If we have a mul by -1 of an add, try distributing the -1 among the
1678 // add operands.
1679 if (Ops.size() == 2)
1680 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) {
1681 SmallVector<const SCEV *, 4> NewOps;
1682 bool AnyFolded = false;
1683 for (SCEVAddRecExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
1684 I != E; ++I) {
1685 const SCEV *Mul = getMulExpr(Ops[0], *I);
1686 if (!isa<SCEVMulExpr>(Mul)) AnyFolded = true;
1687 NewOps.push_back(Mul);
1688 }
1689 if (AnyFolded)
1690 return getAddExpr(NewOps);
1691 }
Chris Lattner53e677a2004-04-02 20:23:17 +00001692 }
1693 }
1694
1695 // Skip over the add expression until we get to a multiply.
1696 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
1697 ++Idx;
1698
1699 if (Ops.size() == 1)
1700 return Ops[0];
Misha Brukman2b37d7c2005-04-21 21:13:18 +00001701
Chris Lattner53e677a2004-04-02 20:23:17 +00001702 // If there are mul operands inline them all into this expression.
1703 if (Idx < Ops.size()) {
1704 bool DeletedMul = false;
Dan Gohman622ed672009-05-04 22:02:23 +00001705 while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001706 // If we have an mul, expand the mul operands onto the end of the operands
1707 // list.
1708 Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end());
1709 Ops.erase(Ops.begin()+Idx);
1710 DeletedMul = true;
1711 }
1712
1713 // If we deleted at least one mul, we added operands to the end of the list,
1714 // and they are not necessarily sorted. Recurse to resort and resimplify
1715 // any operands we just aquired.
1716 if (DeletedMul)
Dan Gohman246b2562007-10-22 18:31:58 +00001717 return getMulExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001718 }
1719
1720 // If there are any add recurrences in the operands list, see if any other
1721 // added values are loop invariant. If so, we can fold them into the
1722 // recurrence.
1723 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
1724 ++Idx;
1725
1726 // Scan over all recurrences, trying to fold loop invariants into them.
1727 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
1728 // Scan all of the other operands to this mul and add them to the vector if
1729 // they are loop invariant w.r.t. the recurrence.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001730 SmallVector<const SCEV *, 8> LIOps;
Dan Gohman35738ac2009-05-04 22:30:44 +00001731 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001732 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1733 if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
1734 LIOps.push_back(Ops[i]);
1735 Ops.erase(Ops.begin()+i);
1736 --i; --e;
1737 }
1738
1739 // If we found some loop invariants, fold them into the recurrence.
1740 if (!LIOps.empty()) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001741 // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step}
Dan Gohman0bba49c2009-07-07 17:06:11 +00001742 SmallVector<const SCEV *, 4> NewOps;
Chris Lattner53e677a2004-04-02 20:23:17 +00001743 NewOps.reserve(AddRec->getNumOperands());
1744 if (LIOps.size() == 1) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001745 const SCEV *Scale = LIOps[0];
Chris Lattner53e677a2004-04-02 20:23:17 +00001746 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
Dan Gohman246b2562007-10-22 18:31:58 +00001747 NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i)));
Chris Lattner53e677a2004-04-02 20:23:17 +00001748 } else {
1749 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001750 SmallVector<const SCEV *, 4> MulOps(LIOps.begin(), LIOps.end());
Chris Lattner53e677a2004-04-02 20:23:17 +00001751 MulOps.push_back(AddRec->getOperand(i));
Dan Gohman246b2562007-10-22 18:31:58 +00001752 NewOps.push_back(getMulExpr(MulOps));
Chris Lattner53e677a2004-04-02 20:23:17 +00001753 }
1754 }
1755
Dan Gohman355b4f32009-12-19 01:46:34 +00001756 // It's tempting to propagate the NSW flag here, but nsw multiplication
Dan Gohman59de33e2009-12-18 18:45:31 +00001757 // is not associative so this isn't necessarily safe.
Dan Gohmana10756e2010-01-21 02:09:26 +00001758 const SCEV *NewRec = getAddRecExpr(NewOps, AddRec->getLoop(),
1759 HasNUW && AddRec->hasNoUnsignedWrap(),
1760 /*HasNSW=*/false);
Chris Lattner53e677a2004-04-02 20:23:17 +00001761
1762 // If all of the other operands were loop invariant, we are done.
1763 if (Ops.size() == 1) return NewRec;
1764
1765 // Otherwise, multiply the folded AddRec by the non-liv parts.
1766 for (unsigned i = 0;; ++i)
1767 if (Ops[i] == AddRec) {
1768 Ops[i] = NewRec;
1769 break;
1770 }
Dan Gohman246b2562007-10-22 18:31:58 +00001771 return getMulExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001772 }
1773
1774 // Okay, if there weren't any loop invariants to be folded, check to see if
1775 // there are multiple AddRec's with the same loop induction variable being
1776 // multiplied together. If so, we can fold them.
1777 for (unsigned OtherIdx = Idx+1;
1778 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
1779 if (OtherIdx != Idx) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001780 const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001781 if (AddRec->getLoop() == OtherAddRec->getLoop()) {
1782 // F * G --> {A,+,B} * {C,+,D} --> {A*C,+,F*D + G*B + B*D}
Dan Gohman35738ac2009-05-04 22:30:44 +00001783 const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec;
Dan Gohman0bba49c2009-07-07 17:06:11 +00001784 const SCEV *NewStart = getMulExpr(F->getStart(),
Chris Lattner53e677a2004-04-02 20:23:17 +00001785 G->getStart());
Dan Gohman0bba49c2009-07-07 17:06:11 +00001786 const SCEV *B = F->getStepRecurrence(*this);
1787 const SCEV *D = G->getStepRecurrence(*this);
1788 const SCEV *NewStep = getAddExpr(getMulExpr(F, D),
Dan Gohman246b2562007-10-22 18:31:58 +00001789 getMulExpr(G, B),
1790 getMulExpr(B, D));
Dan Gohman0bba49c2009-07-07 17:06:11 +00001791 const SCEV *NewAddRec = getAddRecExpr(NewStart, NewStep,
Dan Gohman246b2562007-10-22 18:31:58 +00001792 F->getLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +00001793 if (Ops.size() == 2) return NewAddRec;
1794
1795 Ops.erase(Ops.begin()+Idx);
1796 Ops.erase(Ops.begin()+OtherIdx-1);
1797 Ops.push_back(NewAddRec);
Dan Gohman246b2562007-10-22 18:31:58 +00001798 return getMulExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001799 }
1800 }
1801
1802 // Otherwise couldn't fold anything into this recurrence. Move onto the
1803 // next one.
1804 }
1805
1806 // Okay, it looks like we really DO need an mul expr. Check to see if we
1807 // already have one, otherwise create a new one.
Dan Gohman1c343752009-06-27 21:21:31 +00001808 FoldingSetNodeID ID;
1809 ID.AddInteger(scMulExpr);
1810 ID.AddInteger(Ops.size());
1811 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1812 ID.AddPointer(Ops[i]);
1813 void *IP = 0;
Dan Gohmana10756e2010-01-21 02:09:26 +00001814 SCEVMulExpr *S =
1815 static_cast<SCEVMulExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
1816 if (!S) {
1817 S = SCEVAllocator.Allocate<SCEVMulExpr>();
1818 new (S) SCEVMulExpr(ID, Ops);
1819 UniqueSCEVs.InsertNode(S, IP);
1820 }
Dan Gohman3645b012009-10-09 00:10:36 +00001821 if (HasNUW) S->setHasNoUnsignedWrap(true);
1822 if (HasNSW) S->setHasNoSignedWrap(true);
Dan Gohman1c343752009-06-27 21:21:31 +00001823 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +00001824}
1825
Andreas Bolka8a11c982009-08-07 22:55:26 +00001826/// getUDivExpr - Get a canonical unsigned division expression, or something
1827/// simpler if possible.
Dan Gohman9311ef62009-06-24 14:49:00 +00001828const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
1829 const SCEV *RHS) {
Dan Gohmanf78a9782009-05-18 15:44:58 +00001830 assert(getEffectiveSCEVType(LHS->getType()) ==
1831 getEffectiveSCEVType(RHS->getType()) &&
1832 "SCEVUDivExpr operand types don't match!");
1833
Dan Gohman622ed672009-05-04 22:02:23 +00001834 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001835 if (RHSC->getValue()->equalsInt(1))
Dan Gohman4c0d5d52009-08-20 16:42:55 +00001836 return LHS; // X udiv 1 --> x
Dan Gohman185cf032009-05-08 20:18:49 +00001837 if (RHSC->isZero())
1838 return getIntegerSCEV(0, LHS->getType()); // value is undefined
Chris Lattner53e677a2004-04-02 20:23:17 +00001839
Dan Gohman185cf032009-05-08 20:18:49 +00001840 // Determine if the division can be folded into the operands of
1841 // its operands.
1842 // TODO: Generalize this to non-constants by using known-bits information.
1843 const Type *Ty = LHS->getType();
1844 unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros();
1845 unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ;
1846 // For non-power-of-two values, effectively round the value up to the
1847 // nearest power of two.
1848 if (!RHSC->getValue()->getValue().isPowerOf2())
1849 ++MaxShiftAmt;
1850 const IntegerType *ExtTy =
Owen Anderson1d0be152009-08-13 21:58:54 +00001851 IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt);
Dan Gohman185cf032009-05-08 20:18:49 +00001852 // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
1853 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
1854 if (const SCEVConstant *Step =
1855 dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this)))
1856 if (!Step->getValue()->getValue()
1857 .urem(RHSC->getValue()->getValue()) &&
Dan Gohmanb0285932009-05-08 23:11:16 +00001858 getZeroExtendExpr(AR, ExtTy) ==
1859 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
1860 getZeroExtendExpr(Step, ExtTy),
1861 AR->getLoop())) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001862 SmallVector<const SCEV *, 4> Operands;
Dan Gohman185cf032009-05-08 20:18:49 +00001863 for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i)
1864 Operands.push_back(getUDivExpr(AR->getOperand(i), RHS));
1865 return getAddRecExpr(Operands, AR->getLoop());
1866 }
1867 // (A*B)/C --> A*(B/C) if safe and B/C can be folded.
Dan Gohmanb0285932009-05-08 23:11:16 +00001868 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001869 SmallVector<const SCEV *, 4> Operands;
Dan Gohmanb0285932009-05-08 23:11:16 +00001870 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i)
1871 Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy));
1872 if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands))
Dan Gohman185cf032009-05-08 20:18:49 +00001873 // Find an operand that's safely divisible.
1874 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001875 const SCEV *Op = M->getOperand(i);
1876 const SCEV *Div = getUDivExpr(Op, RHSC);
Dan Gohman185cf032009-05-08 20:18:49 +00001877 if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001878 const SmallVectorImpl<const SCEV *> &MOperands = M->getOperands();
1879 Operands = SmallVector<const SCEV *, 4>(MOperands.begin(),
Dan Gohmana82752c2009-06-14 22:47:23 +00001880 MOperands.end());
Dan Gohman185cf032009-05-08 20:18:49 +00001881 Operands[i] = Div;
1882 return getMulExpr(Operands);
1883 }
1884 }
Dan Gohmanb0285932009-05-08 23:11:16 +00001885 }
Dan Gohman185cf032009-05-08 20:18:49 +00001886 // (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 +00001887 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001888 SmallVector<const SCEV *, 4> Operands;
Dan Gohmanb0285932009-05-08 23:11:16 +00001889 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i)
1890 Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy));
1891 if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) {
1892 Operands.clear();
Dan Gohman185cf032009-05-08 20:18:49 +00001893 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001894 const SCEV *Op = getUDivExpr(A->getOperand(i), RHS);
Dan Gohman185cf032009-05-08 20:18:49 +00001895 if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i))
1896 break;
1897 Operands.push_back(Op);
1898 }
1899 if (Operands.size() == A->getNumOperands())
1900 return getAddExpr(Operands);
1901 }
Dan Gohmanb0285932009-05-08 23:11:16 +00001902 }
Dan Gohman185cf032009-05-08 20:18:49 +00001903
1904 // Fold if both operands are constant.
Dan Gohman622ed672009-05-04 22:02:23 +00001905 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001906 Constant *LHSCV = LHSC->getValue();
1907 Constant *RHSCV = RHSC->getValue();
Owen Andersonbaf3c402009-07-29 18:55:55 +00001908 return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV,
Dan Gohmanb8be8b72009-06-24 00:38:39 +00001909 RHSCV)));
Chris Lattner53e677a2004-04-02 20:23:17 +00001910 }
1911 }
1912
Dan Gohman1c343752009-06-27 21:21:31 +00001913 FoldingSetNodeID ID;
1914 ID.AddInteger(scUDivExpr);
1915 ID.AddPointer(LHS);
1916 ID.AddPointer(RHS);
1917 void *IP = 0;
1918 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1919 SCEV *S = SCEVAllocator.Allocate<SCEVUDivExpr>();
Dan Gohmanc050fd92009-07-13 20:50:19 +00001920 new (S) SCEVUDivExpr(ID, LHS, RHS);
Dan Gohman1c343752009-06-27 21:21:31 +00001921 UniqueSCEVs.InsertNode(S, IP);
1922 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +00001923}
1924
1925
Dan Gohman6c0866c2009-05-24 23:45:28 +00001926/// getAddRecExpr - Get an add recurrence expression for the specified loop.
1927/// Simplify the expression as much as possible.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001928const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start,
Dan Gohman3645b012009-10-09 00:10:36 +00001929 const SCEV *Step, const Loop *L,
1930 bool HasNUW, bool HasNSW) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001931 SmallVector<const SCEV *, 4> Operands;
Chris Lattner53e677a2004-04-02 20:23:17 +00001932 Operands.push_back(Start);
Dan Gohman622ed672009-05-04 22:02:23 +00001933 if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
Chris Lattner53e677a2004-04-02 20:23:17 +00001934 if (StepChrec->getLoop() == L) {
1935 Operands.insert(Operands.end(), StepChrec->op_begin(),
1936 StepChrec->op_end());
Dan Gohman246b2562007-10-22 18:31:58 +00001937 return getAddRecExpr(Operands, L);
Chris Lattner53e677a2004-04-02 20:23:17 +00001938 }
1939
1940 Operands.push_back(Step);
Dan Gohman3645b012009-10-09 00:10:36 +00001941 return getAddRecExpr(Operands, L, HasNUW, HasNSW);
Chris Lattner53e677a2004-04-02 20:23:17 +00001942}
1943
Dan Gohman6c0866c2009-05-24 23:45:28 +00001944/// getAddRecExpr - Get an add recurrence expression for the specified loop.
1945/// Simplify the expression as much as possible.
Dan Gohman64a845e2009-06-24 04:48:43 +00001946const SCEV *
Dan Gohman0bba49c2009-07-07 17:06:11 +00001947ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
Dan Gohman3645b012009-10-09 00:10:36 +00001948 const Loop *L,
1949 bool HasNUW, bool HasNSW) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001950 if (Operands.size() == 1) return Operands[0];
Dan Gohmanf78a9782009-05-18 15:44:58 +00001951#ifndef NDEBUG
1952 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
1953 assert(getEffectiveSCEVType(Operands[i]->getType()) ==
1954 getEffectiveSCEVType(Operands[0]->getType()) &&
1955 "SCEVAddRecExpr operand types don't match!");
1956#endif
Chris Lattner53e677a2004-04-02 20:23:17 +00001957
Dan Gohmancfeb6a42008-06-18 16:23:07 +00001958 if (Operands.back()->isZero()) {
1959 Operands.pop_back();
Dan Gohman3645b012009-10-09 00:10:36 +00001960 return getAddRecExpr(Operands, L, HasNUW, HasNSW); // {X,+,0} --> X
Dan Gohmancfeb6a42008-06-18 16:23:07 +00001961 }
Chris Lattner53e677a2004-04-02 20:23:17 +00001962
Dan Gohmana10756e2010-01-21 02:09:26 +00001963 // If HasNSW is true and all the operands are non-negative, infer HasNUW.
1964 if (!HasNUW && HasNSW) {
1965 bool All = true;
1966 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
1967 if (!isKnownNonNegative(Operands[i])) {
1968 All = false;
1969 break;
1970 }
1971 if (All) HasNUW = true;
1972 }
1973
Dan Gohmand9cc7492008-08-08 18:33:12 +00001974 // Canonicalize nested AddRecs in by nesting them in order of loop depth.
Dan Gohman622ed672009-05-04 22:02:23 +00001975 if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
Dan Gohman5d984912009-12-18 01:14:11 +00001976 const Loop *NestedLoop = NestedAR->getLoop();
Dan Gohmana10756e2010-01-21 02:09:26 +00001977 if (L->contains(NestedLoop->getHeader()) ?
1978 (L->getLoopDepth() < NestedLoop->getLoopDepth()) :
1979 (!NestedLoop->contains(L->getHeader()) &&
1980 DT->dominates(L->getHeader(), NestedLoop->getHeader()))) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001981 SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(),
Dan Gohman5d984912009-12-18 01:14:11 +00001982 NestedAR->op_end());
Dan Gohmand9cc7492008-08-08 18:33:12 +00001983 Operands[0] = NestedAR->getStart();
Dan Gohman9a80b452009-06-26 22:36:20 +00001984 // AddRecs require their operands be loop-invariant with respect to their
1985 // loops. Don't perform this transformation if it would break this
1986 // requirement.
1987 bool AllInvariant = true;
1988 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
1989 if (!Operands[i]->isLoopInvariant(L)) {
1990 AllInvariant = false;
1991 break;
1992 }
1993 if (AllInvariant) {
1994 NestedOperands[0] = getAddRecExpr(Operands, L);
1995 AllInvariant = true;
1996 for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i)
1997 if (!NestedOperands[i]->isLoopInvariant(NestedLoop)) {
1998 AllInvariant = false;
1999 break;
2000 }
2001 if (AllInvariant)
2002 // Ok, both add recurrences are valid after the transformation.
Dan Gohman3645b012009-10-09 00:10:36 +00002003 return getAddRecExpr(NestedOperands, NestedLoop, HasNUW, HasNSW);
Dan Gohman9a80b452009-06-26 22:36:20 +00002004 }
2005 // Reset Operands to its original state.
2006 Operands[0] = NestedAR;
Dan Gohmand9cc7492008-08-08 18:33:12 +00002007 }
2008 }
2009
Dan Gohman67847532010-01-19 22:27:22 +00002010 // Okay, it looks like we really DO need an addrec expr. Check to see if we
2011 // already have one, otherwise create a new one.
Dan Gohman1c343752009-06-27 21:21:31 +00002012 FoldingSetNodeID ID;
2013 ID.AddInteger(scAddRecExpr);
2014 ID.AddInteger(Operands.size());
2015 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
2016 ID.AddPointer(Operands[i]);
2017 ID.AddPointer(L);
2018 void *IP = 0;
Dan Gohmana10756e2010-01-21 02:09:26 +00002019 SCEVAddRecExpr *S =
2020 static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2021 if (!S) {
2022 S = SCEVAllocator.Allocate<SCEVAddRecExpr>();
2023 new (S) SCEVAddRecExpr(ID, Operands, L);
2024 UniqueSCEVs.InsertNode(S, IP);
2025 }
Dan Gohman3645b012009-10-09 00:10:36 +00002026 if (HasNUW) S->setHasNoUnsignedWrap(true);
2027 if (HasNSW) S->setHasNoSignedWrap(true);
Dan Gohman1c343752009-06-27 21:21:31 +00002028 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +00002029}
2030
Dan Gohman9311ef62009-06-24 14:49:00 +00002031const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS,
2032 const SCEV *RHS) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00002033 SmallVector<const SCEV *, 2> Ops;
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002034 Ops.push_back(LHS);
2035 Ops.push_back(RHS);
2036 return getSMaxExpr(Ops);
2037}
2038
Dan Gohman0bba49c2009-07-07 17:06:11 +00002039const SCEV *
2040ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002041 assert(!Ops.empty() && "Cannot get empty smax!");
2042 if (Ops.size() == 1) return Ops[0];
Dan Gohmanf78a9782009-05-18 15:44:58 +00002043#ifndef NDEBUG
2044 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
2045 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
2046 getEffectiveSCEVType(Ops[0]->getType()) &&
2047 "SCEVSMaxExpr operand types don't match!");
2048#endif
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002049
2050 // Sort by complexity, this groups all similar expression types together.
Dan Gohman72861302009-05-07 14:39:04 +00002051 GroupByComplexity(Ops, LI);
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002052
2053 // If there are any constants, fold them together.
2054 unsigned Idx = 0;
Dan Gohman622ed672009-05-04 22:02:23 +00002055 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002056 ++Idx;
2057 assert(Idx < Ops.size());
Dan Gohman622ed672009-05-04 22:02:23 +00002058 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002059 // We found two constants, fold them together!
Owen Andersoneed707b2009-07-24 23:12:02 +00002060 ConstantInt *Fold = ConstantInt::get(getContext(),
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002061 APIntOps::smax(LHSC->getValue()->getValue(),
2062 RHSC->getValue()->getValue()));
Nick Lewycky3e630762008-02-20 06:48:22 +00002063 Ops[0] = getConstant(Fold);
2064 Ops.erase(Ops.begin()+1); // Erase the folded element
2065 if (Ops.size() == 1) return Ops[0];
2066 LHSC = cast<SCEVConstant>(Ops[0]);
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002067 }
2068
Dan Gohmane5aceed2009-06-24 14:46:22 +00002069 // If we are left with a constant minimum-int, strip it off.
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002070 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) {
2071 Ops.erase(Ops.begin());
2072 --Idx;
Dan Gohmane5aceed2009-06-24 14:46:22 +00002073 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) {
2074 // If we have an smax with a constant maximum-int, it will always be
2075 // maximum-int.
2076 return Ops[0];
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002077 }
2078 }
2079
2080 if (Ops.size() == 1) return Ops[0];
2081
2082 // Find the first SMax
2083 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr)
2084 ++Idx;
2085
2086 // Check to see if one of the operands is an SMax. If so, expand its operands
2087 // onto our operand list, and recurse to simplify.
2088 if (Idx < Ops.size()) {
2089 bool DeletedSMax = false;
Dan Gohman622ed672009-05-04 22:02:23 +00002090 while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) {
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002091 Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end());
2092 Ops.erase(Ops.begin()+Idx);
2093 DeletedSMax = true;
2094 }
2095
2096 if (DeletedSMax)
2097 return getSMaxExpr(Ops);
2098 }
2099
2100 // Okay, check to see if the same value occurs in the operand list twice. If
2101 // so, delete one. Since we sorted the list, these values are required to
2102 // be adjacent.
2103 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
2104 if (Ops[i] == Ops[i+1]) { // X smax Y smax Y --> X smax Y
2105 Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
2106 --i; --e;
2107 }
2108
2109 if (Ops.size() == 1) return Ops[0];
2110
2111 assert(!Ops.empty() && "Reduced smax down to nothing!");
2112
Nick Lewycky3e630762008-02-20 06:48:22 +00002113 // Okay, it looks like we really DO need an smax expr. Check to see if we
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002114 // already have one, otherwise create a new one.
Dan Gohman1c343752009-06-27 21:21:31 +00002115 FoldingSetNodeID ID;
2116 ID.AddInteger(scSMaxExpr);
2117 ID.AddInteger(Ops.size());
2118 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2119 ID.AddPointer(Ops[i]);
2120 void *IP = 0;
2121 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2122 SCEV *S = SCEVAllocator.Allocate<SCEVSMaxExpr>();
Dan Gohmanc050fd92009-07-13 20:50:19 +00002123 new (S) SCEVSMaxExpr(ID, Ops);
Dan Gohman1c343752009-06-27 21:21:31 +00002124 UniqueSCEVs.InsertNode(S, IP);
2125 return S;
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002126}
2127
Dan Gohman9311ef62009-06-24 14:49:00 +00002128const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS,
2129 const SCEV *RHS) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00002130 SmallVector<const SCEV *, 2> Ops;
Nick Lewycky3e630762008-02-20 06:48:22 +00002131 Ops.push_back(LHS);
2132 Ops.push_back(RHS);
2133 return getUMaxExpr(Ops);
2134}
2135
Dan Gohman0bba49c2009-07-07 17:06:11 +00002136const SCEV *
2137ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
Nick Lewycky3e630762008-02-20 06:48:22 +00002138 assert(!Ops.empty() && "Cannot get empty umax!");
2139 if (Ops.size() == 1) return Ops[0];
Dan Gohmanf78a9782009-05-18 15:44:58 +00002140#ifndef NDEBUG
2141 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
2142 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
2143 getEffectiveSCEVType(Ops[0]->getType()) &&
2144 "SCEVUMaxExpr operand types don't match!");
2145#endif
Nick Lewycky3e630762008-02-20 06:48:22 +00002146
2147 // Sort by complexity, this groups all similar expression types together.
Dan Gohman72861302009-05-07 14:39:04 +00002148 GroupByComplexity(Ops, LI);
Nick Lewycky3e630762008-02-20 06:48:22 +00002149
2150 // If there are any constants, fold them together.
2151 unsigned Idx = 0;
Dan Gohman622ed672009-05-04 22:02:23 +00002152 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Nick Lewycky3e630762008-02-20 06:48:22 +00002153 ++Idx;
2154 assert(Idx < Ops.size());
Dan Gohman622ed672009-05-04 22:02:23 +00002155 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Nick Lewycky3e630762008-02-20 06:48:22 +00002156 // We found two constants, fold them together!
Owen Andersoneed707b2009-07-24 23:12:02 +00002157 ConstantInt *Fold = ConstantInt::get(getContext(),
Nick Lewycky3e630762008-02-20 06:48:22 +00002158 APIntOps::umax(LHSC->getValue()->getValue(),
2159 RHSC->getValue()->getValue()));
2160 Ops[0] = getConstant(Fold);
2161 Ops.erase(Ops.begin()+1); // Erase the folded element
2162 if (Ops.size() == 1) return Ops[0];
2163 LHSC = cast<SCEVConstant>(Ops[0]);
2164 }
2165
Dan Gohmane5aceed2009-06-24 14:46:22 +00002166 // If we are left with a constant minimum-int, strip it off.
Nick Lewycky3e630762008-02-20 06:48:22 +00002167 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) {
2168 Ops.erase(Ops.begin());
2169 --Idx;
Dan Gohmane5aceed2009-06-24 14:46:22 +00002170 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) {
2171 // If we have an umax with a constant maximum-int, it will always be
2172 // maximum-int.
2173 return Ops[0];
Nick Lewycky3e630762008-02-20 06:48:22 +00002174 }
2175 }
2176
2177 if (Ops.size() == 1) return Ops[0];
2178
2179 // Find the first UMax
2180 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr)
2181 ++Idx;
2182
2183 // Check to see if one of the operands is a UMax. If so, expand its operands
2184 // onto our operand list, and recurse to simplify.
2185 if (Idx < Ops.size()) {
2186 bool DeletedUMax = false;
Dan Gohman622ed672009-05-04 22:02:23 +00002187 while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) {
Nick Lewycky3e630762008-02-20 06:48:22 +00002188 Ops.insert(Ops.end(), UMax->op_begin(), UMax->op_end());
2189 Ops.erase(Ops.begin()+Idx);
2190 DeletedUMax = true;
2191 }
2192
2193 if (DeletedUMax)
2194 return getUMaxExpr(Ops);
2195 }
2196
2197 // Okay, check to see if the same value occurs in the operand list twice. If
2198 // so, delete one. Since we sorted the list, these values are required to
2199 // be adjacent.
2200 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
2201 if (Ops[i] == Ops[i+1]) { // X umax Y umax Y --> X umax Y
2202 Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
2203 --i; --e;
2204 }
2205
2206 if (Ops.size() == 1) return Ops[0];
2207
2208 assert(!Ops.empty() && "Reduced umax down to nothing!");
2209
2210 // Okay, it looks like we really DO need a umax expr. Check to see if we
2211 // already have one, otherwise create a new one.
Dan Gohman1c343752009-06-27 21:21:31 +00002212 FoldingSetNodeID ID;
2213 ID.AddInteger(scUMaxExpr);
2214 ID.AddInteger(Ops.size());
2215 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2216 ID.AddPointer(Ops[i]);
2217 void *IP = 0;
2218 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2219 SCEV *S = SCEVAllocator.Allocate<SCEVUMaxExpr>();
Dan Gohmanc050fd92009-07-13 20:50:19 +00002220 new (S) SCEVUMaxExpr(ID, Ops);
Dan Gohman1c343752009-06-27 21:21:31 +00002221 UniqueSCEVs.InsertNode(S, IP);
2222 return S;
Nick Lewycky3e630762008-02-20 06:48:22 +00002223}
2224
Dan Gohman9311ef62009-06-24 14:49:00 +00002225const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS,
2226 const SCEV *RHS) {
Dan Gohmanf9a9a992009-06-22 03:18:45 +00002227 // ~smax(~x, ~y) == smin(x, y).
2228 return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
2229}
2230
Dan Gohman9311ef62009-06-24 14:49:00 +00002231const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS,
2232 const SCEV *RHS) {
Dan Gohmanf9a9a992009-06-22 03:18:45 +00002233 // ~umax(~x, ~y) == umin(x, y)
2234 return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
2235}
2236
Dan Gohman4f8eea82010-02-01 18:27:38 +00002237const SCEV *ScalarEvolution::getSizeOfExpr(const Type *AllocTy) {
2238 Constant *C = ConstantExpr::getSizeOf(AllocTy);
2239 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
2240 C = ConstantFoldConstantExpression(CE, TD);
2241 const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy));
2242 return getTruncateOrZeroExtend(getSCEV(C), Ty);
2243}
2244
2245const SCEV *ScalarEvolution::getAlignOfExpr(const Type *AllocTy) {
2246 Constant *C = ConstantExpr::getAlignOf(AllocTy);
2247 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
2248 C = ConstantFoldConstantExpression(CE, TD);
2249 const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy));
2250 return getTruncateOrZeroExtend(getSCEV(C), Ty);
2251}
2252
2253const SCEV *ScalarEvolution::getOffsetOfExpr(const StructType *STy,
2254 unsigned FieldNo) {
Dan Gohman0f5efe52010-01-28 02:15:55 +00002255 Constant *C = ConstantExpr::getOffsetOf(STy, FieldNo);
2256 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
2257 C = ConstantFoldConstantExpression(CE, TD);
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002258 const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy));
Dan Gohman0f5efe52010-01-28 02:15:55 +00002259 return getTruncateOrZeroExtend(getSCEV(C), Ty);
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002260}
2261
Dan Gohman4f8eea82010-02-01 18:27:38 +00002262const SCEV *ScalarEvolution::getOffsetOfExpr(const Type *CTy,
2263 Constant *FieldNo) {
2264 Constant *C = ConstantExpr::getOffsetOf(CTy, FieldNo);
Dan Gohman0f5efe52010-01-28 02:15:55 +00002265 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
2266 C = ConstantFoldConstantExpression(CE, TD);
Dan Gohman4f8eea82010-02-01 18:27:38 +00002267 const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(CTy));
Dan Gohman0f5efe52010-01-28 02:15:55 +00002268 return getTruncateOrZeroExtend(getSCEV(C), Ty);
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002269}
2270
Dan Gohman0bba49c2009-07-07 17:06:11 +00002271const SCEV *ScalarEvolution::getUnknown(Value *V) {
Dan Gohman6bbcba12009-06-24 00:54:57 +00002272 // Don't attempt to do anything other than create a SCEVUnknown object
2273 // here. createSCEV only calls getUnknown after checking for all other
2274 // interesting possibilities, and any other code that calls getUnknown
2275 // is doing so in order to hide a value from SCEV canonicalization.
2276
Dan Gohman1c343752009-06-27 21:21:31 +00002277 FoldingSetNodeID ID;
2278 ID.AddInteger(scUnknown);
2279 ID.AddPointer(V);
2280 void *IP = 0;
2281 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2282 SCEV *S = SCEVAllocator.Allocate<SCEVUnknown>();
Dan Gohmanc050fd92009-07-13 20:50:19 +00002283 new (S) SCEVUnknown(ID, V);
Dan Gohman1c343752009-06-27 21:21:31 +00002284 UniqueSCEVs.InsertNode(S, IP);
2285 return S;
Chris Lattner0a7f98c2004-04-15 15:07:24 +00002286}
2287
Chris Lattner53e677a2004-04-02 20:23:17 +00002288//===----------------------------------------------------------------------===//
Chris Lattner53e677a2004-04-02 20:23:17 +00002289// Basic SCEV Analysis and PHI Idiom Recognition Code
2290//
2291
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002292/// isSCEVable - Test if values of the given type are analyzable within
2293/// the SCEV framework. This primarily includes integer types, and it
2294/// can optionally include pointer types if the ScalarEvolution class
2295/// has access to target-specific information.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002296bool ScalarEvolution::isSCEVable(const Type *Ty) const {
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002297 // Integers and pointers are always SCEVable.
2298 return Ty->isInteger() || isa<PointerType>(Ty);
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002299}
2300
2301/// getTypeSizeInBits - Return the size in bits of the specified type,
2302/// for which isSCEVable must return true.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002303uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const {
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002304 assert(isSCEVable(Ty) && "Type is not SCEVable!");
2305
2306 // If we have a TargetData, use it!
2307 if (TD)
2308 return TD->getTypeSizeInBits(Ty);
2309
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002310 // Integer types have fixed sizes.
2311 if (Ty->isInteger())
2312 return Ty->getPrimitiveSizeInBits();
2313
2314 // The only other support type is pointer. Without TargetData, conservatively
2315 // assume pointers are 64-bit.
2316 assert(isa<PointerType>(Ty) && "isSCEVable permitted a non-SCEVable type!");
2317 return 64;
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002318}
2319
2320/// getEffectiveSCEVType - Return a type with the same bitwidth as
2321/// the given type and which represents how SCEV will treat the given
2322/// type, for which isSCEVable must return true. For pointer types,
2323/// this is the pointer-sized integer type.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002324const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const {
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002325 assert(isSCEVable(Ty) && "Type is not SCEVable!");
2326
2327 if (Ty->isInteger())
2328 return Ty;
2329
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002330 // The only other support type is pointer.
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002331 assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!");
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002332 if (TD) return TD->getIntPtrType(getContext());
2333
2334 // Without TargetData, conservatively assume pointers are 64-bit.
2335 return Type::getInt64Ty(getContext());
Dan Gohman2d1be872009-04-16 03:18:22 +00002336}
Chris Lattner53e677a2004-04-02 20:23:17 +00002337
Dan Gohman0bba49c2009-07-07 17:06:11 +00002338const SCEV *ScalarEvolution::getCouldNotCompute() {
Dan Gohman1c343752009-06-27 21:21:31 +00002339 return &CouldNotCompute;
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00002340}
2341
Chris Lattner53e677a2004-04-02 20:23:17 +00002342/// getSCEV - Return an existing SCEV if it exists, otherwise analyze the
2343/// expression and create a new one.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002344const SCEV *ScalarEvolution::getSCEV(Value *V) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002345 assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
Chris Lattner53e677a2004-04-02 20:23:17 +00002346
Dan Gohman0bba49c2009-07-07 17:06:11 +00002347 std::map<SCEVCallbackVH, const SCEV *>::iterator I = Scalars.find(V);
Chris Lattner53e677a2004-04-02 20:23:17 +00002348 if (I != Scalars.end()) return I->second;
Dan Gohman0bba49c2009-07-07 17:06:11 +00002349 const SCEV *S = createSCEV(V);
Dan Gohman35738ac2009-05-04 22:30:44 +00002350 Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S));
Chris Lattner53e677a2004-04-02 20:23:17 +00002351 return S;
2352}
2353
Dan Gohman6bbcba12009-06-24 00:54:57 +00002354/// getIntegerSCEV - Given a SCEVable type, create a constant for the
Dan Gohman2d1be872009-04-16 03:18:22 +00002355/// specified signed integer value and return a SCEV for the constant.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002356const SCEV *ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) {
Dan Gohman6bbcba12009-06-24 00:54:57 +00002357 const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty));
Owen Andersoneed707b2009-07-24 23:12:02 +00002358 return getConstant(ConstantInt::get(ITy, Val));
Dan Gohman2d1be872009-04-16 03:18:22 +00002359}
2360
2361/// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
2362///
Dan Gohman0bba49c2009-07-07 17:06:11 +00002363const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) {
Dan Gohman622ed672009-05-04 22:02:23 +00002364 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
Owen Anderson0a5372e2009-07-13 04:09:18 +00002365 return getConstant(
Owen Andersonbaf3c402009-07-29 18:55:55 +00002366 cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue())));
Dan Gohman2d1be872009-04-16 03:18:22 +00002367
2368 const Type *Ty = V->getType();
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002369 Ty = getEffectiveSCEVType(Ty);
Owen Anderson73c6b712009-07-13 20:58:05 +00002370 return getMulExpr(V,
Owen Andersona7235ea2009-07-31 20:28:14 +00002371 getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))));
Dan Gohman2d1be872009-04-16 03:18:22 +00002372}
2373
2374/// getNotSCEV - Return a SCEV corresponding to ~V = -1-V
Dan Gohman0bba49c2009-07-07 17:06:11 +00002375const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) {
Dan Gohman622ed672009-05-04 22:02:23 +00002376 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
Owen Anderson73c6b712009-07-13 20:58:05 +00002377 return getConstant(
Owen Andersonbaf3c402009-07-29 18:55:55 +00002378 cast<ConstantInt>(ConstantExpr::getNot(VC->getValue())));
Dan Gohman2d1be872009-04-16 03:18:22 +00002379
2380 const Type *Ty = V->getType();
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002381 Ty = getEffectiveSCEVType(Ty);
Owen Anderson73c6b712009-07-13 20:58:05 +00002382 const SCEV *AllOnes =
Owen Andersona7235ea2009-07-31 20:28:14 +00002383 getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty)));
Dan Gohman2d1be872009-04-16 03:18:22 +00002384 return getMinusSCEV(AllOnes, V);
2385}
2386
2387/// getMinusSCEV - Return a SCEV corresponding to LHS - RHS.
2388///
Dan Gohman9311ef62009-06-24 14:49:00 +00002389const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS,
2390 const SCEV *RHS) {
Dan Gohman2d1be872009-04-16 03:18:22 +00002391 // X - Y --> X + -Y
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002392 return getAddExpr(LHS, getNegativeSCEV(RHS));
Dan Gohman2d1be872009-04-16 03:18:22 +00002393}
2394
2395/// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the
2396/// input value to the specified type. If the type must be extended, it is zero
2397/// extended.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002398const SCEV *
2399ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V,
Nick Lewycky5cd28fa2009-04-23 05:15:08 +00002400 const Type *Ty) {
Dan Gohman2d1be872009-04-16 03:18:22 +00002401 const Type *SrcTy = V->getType();
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002402 assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2403 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohman2d1be872009-04-16 03:18:22 +00002404 "Cannot truncate or zero extend with non-integer arguments!");
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002405 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
Dan Gohman2d1be872009-04-16 03:18:22 +00002406 return V; // No conversion
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002407 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002408 return getTruncateExpr(V, Ty);
2409 return getZeroExtendExpr(V, Ty);
Dan Gohman2d1be872009-04-16 03:18:22 +00002410}
2411
2412/// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the
2413/// input value to the specified type. If the type must be extended, it is sign
2414/// extended.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002415const SCEV *
2416ScalarEvolution::getTruncateOrSignExtend(const SCEV *V,
Nick Lewycky5cd28fa2009-04-23 05:15:08 +00002417 const Type *Ty) {
Dan Gohman2d1be872009-04-16 03:18:22 +00002418 const Type *SrcTy = V->getType();
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002419 assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2420 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohman2d1be872009-04-16 03:18:22 +00002421 "Cannot truncate or zero extend with non-integer arguments!");
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002422 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
Dan Gohman2d1be872009-04-16 03:18:22 +00002423 return V; // No conversion
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002424 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002425 return getTruncateExpr(V, Ty);
2426 return getSignExtendExpr(V, Ty);
Dan Gohman2d1be872009-04-16 03:18:22 +00002427}
2428
Dan Gohman467c4302009-05-13 03:46:30 +00002429/// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the
2430/// input value to the specified type. If the type must be extended, it is zero
2431/// extended. The conversion must not be narrowing.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002432const SCEV *
2433ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, const Type *Ty) {
Dan Gohman467c4302009-05-13 03:46:30 +00002434 const Type *SrcTy = V->getType();
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002435 assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2436 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohman467c4302009-05-13 03:46:30 +00002437 "Cannot noop or zero extend with non-integer arguments!");
2438 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2439 "getNoopOrZeroExtend cannot truncate!");
2440 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2441 return V; // No conversion
2442 return getZeroExtendExpr(V, Ty);
2443}
2444
2445/// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the
2446/// input value to the specified type. If the type must be extended, it is sign
2447/// extended. The conversion must not be narrowing.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002448const SCEV *
2449ScalarEvolution::getNoopOrSignExtend(const SCEV *V, const Type *Ty) {
Dan Gohman467c4302009-05-13 03:46:30 +00002450 const Type *SrcTy = V->getType();
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002451 assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2452 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohman467c4302009-05-13 03:46:30 +00002453 "Cannot noop or sign extend with non-integer arguments!");
2454 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2455 "getNoopOrSignExtend cannot truncate!");
2456 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2457 return V; // No conversion
2458 return getSignExtendExpr(V, Ty);
2459}
2460
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00002461/// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of
2462/// the input value to the specified type. If the type must be extended,
2463/// it is extended with unspecified bits. The conversion must not be
2464/// narrowing.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002465const SCEV *
2466ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, const Type *Ty) {
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00002467 const Type *SrcTy = V->getType();
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002468 assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2469 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00002470 "Cannot noop or any extend with non-integer arguments!");
2471 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2472 "getNoopOrAnyExtend cannot truncate!");
2473 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2474 return V; // No conversion
2475 return getAnyExtendExpr(V, Ty);
2476}
2477
Dan Gohman467c4302009-05-13 03:46:30 +00002478/// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the
2479/// input value to the specified type. The conversion must not be widening.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002480const SCEV *
2481ScalarEvolution::getTruncateOrNoop(const SCEV *V, const Type *Ty) {
Dan Gohman467c4302009-05-13 03:46:30 +00002482 const Type *SrcTy = V->getType();
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002483 assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2484 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohman467c4302009-05-13 03:46:30 +00002485 "Cannot truncate or noop with non-integer arguments!");
2486 assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) &&
2487 "getTruncateOrNoop cannot extend!");
2488 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2489 return V; // No conversion
2490 return getTruncateExpr(V, Ty);
2491}
2492
Dan Gohmana334aa72009-06-22 00:31:57 +00002493/// getUMaxFromMismatchedTypes - Promote the operands to the wider of
2494/// the types using zero-extension, and then perform a umax operation
2495/// with them.
Dan Gohman9311ef62009-06-24 14:49:00 +00002496const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS,
2497 const SCEV *RHS) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00002498 const SCEV *PromotedLHS = LHS;
2499 const SCEV *PromotedRHS = RHS;
Dan Gohmana334aa72009-06-22 00:31:57 +00002500
2501 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
2502 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
2503 else
2504 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
2505
2506 return getUMaxExpr(PromotedLHS, PromotedRHS);
2507}
2508
Dan Gohmanc9759e82009-06-22 15:03:27 +00002509/// getUMinFromMismatchedTypes - Promote the operands to the wider of
2510/// the types using zero-extension, and then perform a umin operation
2511/// with them.
Dan Gohman9311ef62009-06-24 14:49:00 +00002512const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS,
2513 const SCEV *RHS) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00002514 const SCEV *PromotedLHS = LHS;
2515 const SCEV *PromotedRHS = RHS;
Dan Gohmanc9759e82009-06-22 15:03:27 +00002516
2517 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
2518 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
2519 else
2520 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
2521
2522 return getUMinExpr(PromotedLHS, PromotedRHS);
2523}
2524
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002525/// PushDefUseChildren - Push users of the given Instruction
2526/// onto the given Worklist.
2527static void
2528PushDefUseChildren(Instruction *I,
2529 SmallVectorImpl<Instruction *> &Worklist) {
2530 // Push the def-use children onto the Worklist stack.
2531 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
2532 UI != UE; ++UI)
2533 Worklist.push_back(cast<Instruction>(UI));
2534}
2535
2536/// ForgetSymbolicValue - This looks up computed SCEV values for all
2537/// instructions that depend on the given instruction and removes them from
2538/// the Scalars map if they reference SymName. This is used during PHI
2539/// resolution.
Dan Gohman64a845e2009-06-24 04:48:43 +00002540void
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002541ScalarEvolution::ForgetSymbolicName(Instruction *I, const SCEV *SymName) {
2542 SmallVector<Instruction *, 16> Worklist;
2543 PushDefUseChildren(I, Worklist);
Chris Lattner53e677a2004-04-02 20:23:17 +00002544
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002545 SmallPtrSet<Instruction *, 8> Visited;
2546 Visited.insert(I);
2547 while (!Worklist.empty()) {
2548 Instruction *I = Worklist.pop_back_val();
2549 if (!Visited.insert(I)) continue;
Chris Lattner4dc534c2005-02-13 04:37:18 +00002550
Dan Gohman5d984912009-12-18 01:14:11 +00002551 std::map<SCEVCallbackVH, const SCEV *>::iterator It =
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002552 Scalars.find(static_cast<Value *>(I));
2553 if (It != Scalars.end()) {
2554 // Short-circuit the def-use traversal if the symbolic name
2555 // ceases to appear in expressions.
2556 if (!It->second->hasOperand(SymName))
2557 continue;
Chris Lattner4dc534c2005-02-13 04:37:18 +00002558
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002559 // SCEVUnknown for a PHI either means that it has an unrecognized
2560 // structure, or it's a PHI that's in the progress of being computed
2561 // by createNodeForPHI. In the former case, additional loop trip
2562 // count information isn't going to change anything. In the later
2563 // case, createNodeForPHI will perform the necessary updates on its
2564 // own when it gets to that point.
Dan Gohman42214892009-08-31 21:15:23 +00002565 if (!isa<PHINode>(I) || !isa<SCEVUnknown>(It->second)) {
2566 ValuesAtScopes.erase(It->second);
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002567 Scalars.erase(It);
Dan Gohman42214892009-08-31 21:15:23 +00002568 }
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002569 }
2570
2571 PushDefUseChildren(I, Worklist);
2572 }
Chris Lattner4dc534c2005-02-13 04:37:18 +00002573}
Chris Lattner53e677a2004-04-02 20:23:17 +00002574
2575/// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in
2576/// a loop header, making it a potential recurrence, or it doesn't.
2577///
Dan Gohman0bba49c2009-07-07 17:06:11 +00002578const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
Chris Lattner53e677a2004-04-02 20:23:17 +00002579 if (PN->getNumIncomingValues() == 2) // The loops have been canonicalized.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002580 if (const Loop *L = LI->getLoopFor(PN->getParent()))
Chris Lattner53e677a2004-04-02 20:23:17 +00002581 if (L->getHeader() == PN->getParent()) {
2582 // If it lives in the loop header, it has two incoming values, one
2583 // from outside the loop, and one from inside.
2584 unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0));
2585 unsigned BackEdge = IncomingEdge^1;
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002586
Chris Lattner53e677a2004-04-02 20:23:17 +00002587 // While we are analyzing this PHI node, handle its value symbolically.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002588 const SCEV *SymbolicName = getUnknown(PN);
Chris Lattner53e677a2004-04-02 20:23:17 +00002589 assert(Scalars.find(PN) == Scalars.end() &&
2590 "PHI node already processed?");
Dan Gohman35738ac2009-05-04 22:30:44 +00002591 Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName));
Chris Lattner53e677a2004-04-02 20:23:17 +00002592
2593 // Using this symbolic name for the PHI, analyze the value coming around
2594 // the back-edge.
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002595 Value *BEValueV = PN->getIncomingValue(BackEdge);
2596 const SCEV *BEValue = getSCEV(BEValueV);
Chris Lattner53e677a2004-04-02 20:23:17 +00002597
2598 // NOTE: If BEValue is loop invariant, we know that the PHI node just
2599 // has a special value for the first iteration of the loop.
2600
2601 // If the value coming around the backedge is an add with the symbolic
2602 // value we just inserted, then we found a simple induction variable!
Dan Gohman622ed672009-05-04 22:02:23 +00002603 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00002604 // If there is a single occurrence of the symbolic value, replace it
2605 // with a recurrence.
2606 unsigned FoundIndex = Add->getNumOperands();
2607 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
2608 if (Add->getOperand(i) == SymbolicName)
2609 if (FoundIndex == e) {
2610 FoundIndex = i;
2611 break;
2612 }
2613
2614 if (FoundIndex != Add->getNumOperands()) {
2615 // Create an add with everything but the specified operand.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002616 SmallVector<const SCEV *, 8> Ops;
Chris Lattner53e677a2004-04-02 20:23:17 +00002617 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
2618 if (i != FoundIndex)
2619 Ops.push_back(Add->getOperand(i));
Dan Gohman0bba49c2009-07-07 17:06:11 +00002620 const SCEV *Accum = getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00002621
2622 // This is not a valid addrec if the step amount is varying each
2623 // loop iteration, but is not itself an addrec in this loop.
2624 if (Accum->isLoopInvariant(L) ||
2625 (isa<SCEVAddRecExpr>(Accum) &&
2626 cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
Dan Gohmana10756e2010-01-21 02:09:26 +00002627 bool HasNUW = false;
2628 bool HasNSW = false;
2629
2630 // If the increment doesn't overflow, then neither the addrec nor
2631 // the post-increment will overflow.
2632 if (const AddOperator *OBO = dyn_cast<AddOperator>(BEValueV)) {
2633 if (OBO->hasNoUnsignedWrap())
2634 HasNUW = true;
2635 if (OBO->hasNoSignedWrap())
2636 HasNSW = true;
2637 }
2638
Dan Gohman64a845e2009-06-24 04:48:43 +00002639 const SCEV *StartVal =
2640 getSCEV(PN->getIncomingValue(IncomingEdge));
Dan Gohmana10756e2010-01-21 02:09:26 +00002641 const SCEV *PHISCEV =
2642 getAddRecExpr(StartVal, Accum, L, HasNUW, HasNSW);
Dan Gohmaneb490a72009-07-25 01:22:26 +00002643
Dan Gohmana10756e2010-01-21 02:09:26 +00002644 // Since the no-wrap flags are on the increment, they apply to the
2645 // post-incremented value as well.
2646 if (Accum->isLoopInvariant(L))
2647 (void)getAddRecExpr(getAddExpr(StartVal, Accum),
2648 Accum, L, HasNUW, HasNSW);
Chris Lattner53e677a2004-04-02 20:23:17 +00002649
2650 // Okay, for the entire analysis of this edge we assumed the PHI
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002651 // to be symbolic. We now need to go back and purge all of the
2652 // entries for the scalars that use the symbolic expression.
2653 ForgetSymbolicName(PN, SymbolicName);
2654 Scalars[SCEVCallbackVH(PN, this)] = PHISCEV;
Chris Lattner53e677a2004-04-02 20:23:17 +00002655 return PHISCEV;
2656 }
2657 }
Dan Gohman622ed672009-05-04 22:02:23 +00002658 } else if (const SCEVAddRecExpr *AddRec =
2659 dyn_cast<SCEVAddRecExpr>(BEValue)) {
Chris Lattner97156e72006-04-26 18:34:07 +00002660 // Otherwise, this could be a loop like this:
2661 // i = 0; for (j = 1; ..; ++j) { .... i = j; }
2662 // In this case, j = {1,+,1} and BEValue is j.
2663 // Because the other in-value of i (0) fits the evolution of BEValue
2664 // i really is an addrec evolution.
2665 if (AddRec->getLoop() == L && AddRec->isAffine()) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00002666 const SCEV *StartVal = getSCEV(PN->getIncomingValue(IncomingEdge));
Chris Lattner97156e72006-04-26 18:34:07 +00002667
2668 // If StartVal = j.start - j.stride, we can use StartVal as the
2669 // initial step of the addrec evolution.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002670 if (StartVal == getMinusSCEV(AddRec->getOperand(0),
Dan Gohman246b2562007-10-22 18:31:58 +00002671 AddRec->getOperand(1))) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00002672 const SCEV *PHISCEV =
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002673 getAddRecExpr(StartVal, AddRec->getOperand(1), L);
Chris Lattner97156e72006-04-26 18:34:07 +00002674
2675 // Okay, for the entire analysis of this edge we assumed the PHI
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002676 // to be symbolic. We now need to go back and purge all of the
2677 // entries for the scalars that use the symbolic expression.
2678 ForgetSymbolicName(PN, SymbolicName);
2679 Scalars[SCEVCallbackVH(PN, this)] = PHISCEV;
Chris Lattner97156e72006-04-26 18:34:07 +00002680 return PHISCEV;
2681 }
2682 }
Chris Lattner53e677a2004-04-02 20:23:17 +00002683 }
2684
2685 return SymbolicName;
2686 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002687
Dan Gohmana653fc52009-07-14 14:06:25 +00002688 // It's tempting to recognize PHIs with a unique incoming value, however
2689 // this leads passes like indvars to break LCSSA form. Fortunately, such
2690 // PHIs are rare, as instcombine zaps them.
2691
Chris Lattner53e677a2004-04-02 20:23:17 +00002692 // If it's not a loop phi, we can't handle it yet.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002693 return getUnknown(PN);
Chris Lattner53e677a2004-04-02 20:23:17 +00002694}
2695
Dan Gohman26466c02009-05-08 20:26:55 +00002696/// createNodeForGEP - Expand GEP instructions into add and multiply
2697/// operations. This allows them to be analyzed by regular SCEV code.
2698///
Dan Gohmand281ed22009-12-18 02:09:29 +00002699const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
Dan Gohman26466c02009-05-08 20:26:55 +00002700
Dan Gohmand281ed22009-12-18 02:09:29 +00002701 bool InBounds = GEP->isInBounds();
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002702 const Type *IntPtrTy = getEffectiveSCEVType(GEP->getType());
Dan Gohmane810b0d2009-05-08 20:36:47 +00002703 Value *Base = GEP->getOperand(0);
Dan Gohmanc63a6272009-05-09 00:14:52 +00002704 // Don't attempt to analyze GEPs over unsized objects.
2705 if (!cast<PointerType>(Base->getType())->getElementType()->isSized())
2706 return getUnknown(GEP);
Dan Gohman0bba49c2009-07-07 17:06:11 +00002707 const SCEV *TotalOffset = getIntegerSCEV(0, IntPtrTy);
Dan Gohmane810b0d2009-05-08 20:36:47 +00002708 gep_type_iterator GTI = gep_type_begin(GEP);
2709 for (GetElementPtrInst::op_iterator I = next(GEP->op_begin()),
2710 E = GEP->op_end();
Dan Gohman26466c02009-05-08 20:26:55 +00002711 I != E; ++I) {
2712 Value *Index = *I;
2713 // Compute the (potentially symbolic) offset in bytes for this index.
2714 if (const StructType *STy = dyn_cast<StructType>(*GTI++)) {
2715 // For a struct, add the member offset.
Dan Gohman26466c02009-05-08 20:26:55 +00002716 unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002717 TotalOffset = getAddExpr(TotalOffset,
Dan Gohman4f8eea82010-02-01 18:27:38 +00002718 getOffsetOfExpr(STy, FieldNo),
Dan Gohmand281ed22009-12-18 02:09:29 +00002719 /*HasNUW=*/false, /*HasNSW=*/InBounds);
Dan Gohman26466c02009-05-08 20:26:55 +00002720 } else {
2721 // For an array, add the element offset, explicitly scaled.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002722 const SCEV *LocalOffset = getSCEV(Index);
Dan Gohman26466c02009-05-08 20:26:55 +00002723 if (!isa<PointerType>(LocalOffset->getType()))
2724 // Getelementptr indicies are signed.
Dan Gohman85b05a22009-07-13 21:35:55 +00002725 LocalOffset = getTruncateOrSignExtend(LocalOffset, IntPtrTy);
Dan Gohmand281ed22009-12-18 02:09:29 +00002726 // Lower "inbounds" GEPs to NSW arithmetic.
Dan Gohman4f8eea82010-02-01 18:27:38 +00002727 LocalOffset = getMulExpr(LocalOffset, getSizeOfExpr(*GTI),
Dan Gohmand281ed22009-12-18 02:09:29 +00002728 /*HasNUW=*/false, /*HasNSW=*/InBounds);
2729 TotalOffset = getAddExpr(TotalOffset, LocalOffset,
2730 /*HasNUW=*/false, /*HasNSW=*/InBounds);
Dan Gohman26466c02009-05-08 20:26:55 +00002731 }
2732 }
Dan Gohmand281ed22009-12-18 02:09:29 +00002733 return getAddExpr(getSCEV(Base), TotalOffset,
2734 /*HasNUW=*/false, /*HasNSW=*/InBounds);
Dan Gohman26466c02009-05-08 20:26:55 +00002735}
2736
Nick Lewycky83bb0052007-11-22 07:59:40 +00002737/// GetMinTrailingZeros - Determine the minimum number of zero bits that S is
2738/// guaranteed to end in (at every loop iteration). It is, at the same time,
2739/// the minimum number of times S is divisible by 2. For example, given {4,+,8}
2740/// it returns 2. If S is guaranteed to be 0, it returns the bitwidth of S.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002741uint32_t
Dan Gohman0bba49c2009-07-07 17:06:11 +00002742ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
Dan Gohman622ed672009-05-04 22:02:23 +00002743 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
Chris Lattner8314a0c2007-11-23 22:36:49 +00002744 return C->getValue()->getValue().countTrailingZeros();
Chris Lattnera17f0392006-12-12 02:26:09 +00002745
Dan Gohman622ed672009-05-04 22:02:23 +00002746 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S))
Dan Gohman2c364ad2009-06-19 23:29:04 +00002747 return std::min(GetMinTrailingZeros(T->getOperand()),
2748 (uint32_t)getTypeSizeInBits(T->getType()));
Nick Lewycky83bb0052007-11-22 07:59:40 +00002749
Dan Gohman622ed672009-05-04 22:02:23 +00002750 if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) {
Dan Gohman2c364ad2009-06-19 23:29:04 +00002751 uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
2752 return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
2753 getTypeSizeInBits(E->getType()) : OpRes;
Nick Lewycky83bb0052007-11-22 07:59:40 +00002754 }
2755
Dan Gohman622ed672009-05-04 22:02:23 +00002756 if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) {
Dan Gohman2c364ad2009-06-19 23:29:04 +00002757 uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
2758 return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
2759 getTypeSizeInBits(E->getType()) : OpRes;
Nick Lewycky83bb0052007-11-22 07:59:40 +00002760 }
2761
Dan Gohman622ed672009-05-04 22:02:23 +00002762 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
Nick Lewycky83bb0052007-11-22 07:59:40 +00002763 // The result is the min of all operands results.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002764 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
Nick Lewycky83bb0052007-11-22 07:59:40 +00002765 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman2c364ad2009-06-19 23:29:04 +00002766 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
Nick Lewycky83bb0052007-11-22 07:59:40 +00002767 return MinOpRes;
Chris Lattnera17f0392006-12-12 02:26:09 +00002768 }
2769
Dan Gohman622ed672009-05-04 22:02:23 +00002770 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
Nick Lewycky83bb0052007-11-22 07:59:40 +00002771 // The result is the sum of all operands results.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002772 uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0));
2773 uint32_t BitWidth = getTypeSizeInBits(M->getType());
Nick Lewycky83bb0052007-11-22 07:59:40 +00002774 for (unsigned i = 1, e = M->getNumOperands();
2775 SumOpRes != BitWidth && i != e; ++i)
Dan Gohman2c364ad2009-06-19 23:29:04 +00002776 SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)),
Nick Lewycky83bb0052007-11-22 07:59:40 +00002777 BitWidth);
2778 return SumOpRes;
Chris Lattnera17f0392006-12-12 02:26:09 +00002779 }
Nick Lewycky83bb0052007-11-22 07:59:40 +00002780
Dan Gohman622ed672009-05-04 22:02:23 +00002781 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
Nick Lewycky83bb0052007-11-22 07:59:40 +00002782 // The result is the min of all operands results.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002783 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
Nick Lewycky83bb0052007-11-22 07:59:40 +00002784 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman2c364ad2009-06-19 23:29:04 +00002785 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
Nick Lewycky83bb0052007-11-22 07:59:40 +00002786 return MinOpRes;
Chris Lattnera17f0392006-12-12 02:26:09 +00002787 }
Nick Lewycky83bb0052007-11-22 07:59:40 +00002788
Dan Gohman622ed672009-05-04 22:02:23 +00002789 if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) {
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002790 // The result is the min of all operands results.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002791 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002792 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman2c364ad2009-06-19 23:29:04 +00002793 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002794 return MinOpRes;
2795 }
2796
Dan Gohman622ed672009-05-04 22:02:23 +00002797 if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) {
Nick Lewycky3e630762008-02-20 06:48:22 +00002798 // The result is the min of all operands results.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002799 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
Nick Lewycky3e630762008-02-20 06:48:22 +00002800 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman2c364ad2009-06-19 23:29:04 +00002801 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
Nick Lewycky3e630762008-02-20 06:48:22 +00002802 return MinOpRes;
2803 }
2804
Dan Gohman2c364ad2009-06-19 23:29:04 +00002805 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2806 // For a SCEVUnknown, ask ValueTracking.
2807 unsigned BitWidth = getTypeSizeInBits(U->getType());
2808 APInt Mask = APInt::getAllOnesValue(BitWidth);
2809 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
2810 ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones);
2811 return Zeros.countTrailingOnes();
2812 }
2813
2814 // SCEVUDivExpr
Nick Lewycky83bb0052007-11-22 07:59:40 +00002815 return 0;
Chris Lattnera17f0392006-12-12 02:26:09 +00002816}
Chris Lattner53e677a2004-04-02 20:23:17 +00002817
Dan Gohman85b05a22009-07-13 21:35:55 +00002818/// getUnsignedRange - Determine the unsigned range for a particular SCEV.
2819///
2820ConstantRange
2821ScalarEvolution::getUnsignedRange(const SCEV *S) {
Dan Gohman2c364ad2009-06-19 23:29:04 +00002822
2823 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
Dan Gohman85b05a22009-07-13 21:35:55 +00002824 return ConstantRange(C->getValue()->getValue());
Dan Gohman2c364ad2009-06-19 23:29:04 +00002825
Dan Gohmanc9c36cb2010-01-26 19:19:05 +00002826 unsigned BitWidth = getTypeSizeInBits(S->getType());
2827 ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true);
2828
2829 // If the value has known zeros, the maximum unsigned value will have those
2830 // known zeros as well.
2831 uint32_t TZ = GetMinTrailingZeros(S);
2832 if (TZ != 0)
2833 ConservativeResult =
2834 ConstantRange(APInt::getMinValue(BitWidth),
2835 APInt::getMaxValue(BitWidth).lshr(TZ).shl(TZ) + 1);
2836
Dan Gohman85b05a22009-07-13 21:35:55 +00002837 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
2838 ConstantRange X = getUnsignedRange(Add->getOperand(0));
2839 for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
2840 X = X.add(getUnsignedRange(Add->getOperand(i)));
Dan Gohmanc9c36cb2010-01-26 19:19:05 +00002841 return ConservativeResult.intersectWith(X);
Dan Gohman85b05a22009-07-13 21:35:55 +00002842 }
2843
2844 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
2845 ConstantRange X = getUnsignedRange(Mul->getOperand(0));
2846 for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
2847 X = X.multiply(getUnsignedRange(Mul->getOperand(i)));
Dan Gohmanc9c36cb2010-01-26 19:19:05 +00002848 return ConservativeResult.intersectWith(X);
Dan Gohman85b05a22009-07-13 21:35:55 +00002849 }
2850
2851 if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
2852 ConstantRange X = getUnsignedRange(SMax->getOperand(0));
2853 for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
2854 X = X.smax(getUnsignedRange(SMax->getOperand(i)));
Dan Gohmanc9c36cb2010-01-26 19:19:05 +00002855 return ConservativeResult.intersectWith(X);
Dan Gohman85b05a22009-07-13 21:35:55 +00002856 }
2857
2858 if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
2859 ConstantRange X = getUnsignedRange(UMax->getOperand(0));
2860 for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
2861 X = X.umax(getUnsignedRange(UMax->getOperand(i)));
Dan Gohmanc9c36cb2010-01-26 19:19:05 +00002862 return ConservativeResult.intersectWith(X);
Dan Gohman85b05a22009-07-13 21:35:55 +00002863 }
2864
2865 if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
2866 ConstantRange X = getUnsignedRange(UDiv->getLHS());
2867 ConstantRange Y = getUnsignedRange(UDiv->getRHS());
Dan Gohmanc9c36cb2010-01-26 19:19:05 +00002868 return ConservativeResult.intersectWith(X.udiv(Y));
Dan Gohman85b05a22009-07-13 21:35:55 +00002869 }
2870
2871 if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
2872 ConstantRange X = getUnsignedRange(ZExt->getOperand());
Dan Gohmanc9c36cb2010-01-26 19:19:05 +00002873 return ConservativeResult.intersectWith(X.zeroExtend(BitWidth));
Dan Gohman85b05a22009-07-13 21:35:55 +00002874 }
2875
2876 if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
2877 ConstantRange X = getUnsignedRange(SExt->getOperand());
Dan Gohmanc9c36cb2010-01-26 19:19:05 +00002878 return ConservativeResult.intersectWith(X.signExtend(BitWidth));
Dan Gohman85b05a22009-07-13 21:35:55 +00002879 }
2880
2881 if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
2882 ConstantRange X = getUnsignedRange(Trunc->getOperand());
Dan Gohmanc9c36cb2010-01-26 19:19:05 +00002883 return ConservativeResult.intersectWith(X.truncate(BitWidth));
Dan Gohman85b05a22009-07-13 21:35:55 +00002884 }
2885
Dan Gohman85b05a22009-07-13 21:35:55 +00002886 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
Dan Gohmana10756e2010-01-21 02:09:26 +00002887 // If there's no unsigned wrap, the value will never be less than its
2888 // initial value.
2889 if (AddRec->hasNoUnsignedWrap())
2890 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(AddRec->getStart()))
2891 ConservativeResult =
2892 ConstantRange(C->getValue()->getValue(),
2893 APInt(getTypeSizeInBits(C->getType()), 0));
Dan Gohman85b05a22009-07-13 21:35:55 +00002894
2895 // TODO: non-affine addrec
Dan Gohmanc9c36cb2010-01-26 19:19:05 +00002896 if (AddRec->isAffine()) {
Dan Gohman85b05a22009-07-13 21:35:55 +00002897 const Type *Ty = AddRec->getType();
2898 const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop());
Dan Gohmanc9c36cb2010-01-26 19:19:05 +00002899 if (!isa<SCEVCouldNotCompute>(MaxBECount) &&
2900 getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) {
Dan Gohman85b05a22009-07-13 21:35:55 +00002901 MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty);
2902
2903 const SCEV *Start = AddRec->getStart();
2904 const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this);
2905
2906 // Check for overflow.
Dan Gohmana10756e2010-01-21 02:09:26 +00002907 if (!AddRec->hasNoUnsignedWrap())
2908 return ConservativeResult;
Dan Gohman85b05a22009-07-13 21:35:55 +00002909
2910 ConstantRange StartRange = getUnsignedRange(Start);
2911 ConstantRange EndRange = getUnsignedRange(End);
2912 APInt Min = APIntOps::umin(StartRange.getUnsignedMin(),
2913 EndRange.getUnsignedMin());
2914 APInt Max = APIntOps::umax(StartRange.getUnsignedMax(),
2915 EndRange.getUnsignedMax());
2916 if (Min.isMinValue() && Max.isMaxValue())
Dan Gohmana10756e2010-01-21 02:09:26 +00002917 return ConservativeResult;
Dan Gohmanc9c36cb2010-01-26 19:19:05 +00002918 return ConservativeResult.intersectWith(ConstantRange(Min, Max+1));
Dan Gohman85b05a22009-07-13 21:35:55 +00002919 }
2920 }
Dan Gohmana10756e2010-01-21 02:09:26 +00002921
2922 return ConservativeResult;
Dan Gohman2c364ad2009-06-19 23:29:04 +00002923 }
2924
2925 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2926 // For a SCEVUnknown, ask ValueTracking.
2927 unsigned BitWidth = getTypeSizeInBits(U->getType());
2928 APInt Mask = APInt::getAllOnesValue(BitWidth);
2929 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
2930 ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD);
Dan Gohman746f3b12009-07-20 22:34:18 +00002931 if (Ones == ~Zeros + 1)
Dan Gohmanc9c36cb2010-01-26 19:19:05 +00002932 return ConservativeResult;
2933 return ConservativeResult.intersectWith(ConstantRange(Ones, ~Zeros + 1));
Dan Gohman2c364ad2009-06-19 23:29:04 +00002934 }
2935
Dan Gohmanc9c36cb2010-01-26 19:19:05 +00002936 return ConservativeResult;
Dan Gohman2c364ad2009-06-19 23:29:04 +00002937}
2938
Dan Gohman85b05a22009-07-13 21:35:55 +00002939/// getSignedRange - Determine the signed range for a particular SCEV.
2940///
2941ConstantRange
2942ScalarEvolution::getSignedRange(const SCEV *S) {
Dan Gohman2c364ad2009-06-19 23:29:04 +00002943
Dan Gohman85b05a22009-07-13 21:35:55 +00002944 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
2945 return ConstantRange(C->getValue()->getValue());
2946
Dan Gohman52fddd32010-01-26 04:40:18 +00002947 unsigned BitWidth = getTypeSizeInBits(S->getType());
2948 ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true);
2949
2950 // If the value has known zeros, the maximum signed value will have those
2951 // known zeros as well.
2952 uint32_t TZ = GetMinTrailingZeros(S);
2953 if (TZ != 0)
2954 ConservativeResult =
2955 ConstantRange(APInt::getSignedMinValue(BitWidth),
2956 APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1);
2957
Dan Gohman85b05a22009-07-13 21:35:55 +00002958 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
2959 ConstantRange X = getSignedRange(Add->getOperand(0));
2960 for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
2961 X = X.add(getSignedRange(Add->getOperand(i)));
Dan Gohman52fddd32010-01-26 04:40:18 +00002962 return ConservativeResult.intersectWith(X);
Dan Gohman2c364ad2009-06-19 23:29:04 +00002963 }
2964
Dan Gohman85b05a22009-07-13 21:35:55 +00002965 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
2966 ConstantRange X = getSignedRange(Mul->getOperand(0));
2967 for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
2968 X = X.multiply(getSignedRange(Mul->getOperand(i)));
Dan Gohman52fddd32010-01-26 04:40:18 +00002969 return ConservativeResult.intersectWith(X);
Dan Gohman2c364ad2009-06-19 23:29:04 +00002970 }
2971
Dan Gohman85b05a22009-07-13 21:35:55 +00002972 if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
2973 ConstantRange X = getSignedRange(SMax->getOperand(0));
2974 for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
2975 X = X.smax(getSignedRange(SMax->getOperand(i)));
Dan Gohman52fddd32010-01-26 04:40:18 +00002976 return ConservativeResult.intersectWith(X);
Dan Gohman85b05a22009-07-13 21:35:55 +00002977 }
Dan Gohman62849c02009-06-24 01:05:09 +00002978
Dan Gohman85b05a22009-07-13 21:35:55 +00002979 if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
2980 ConstantRange X = getSignedRange(UMax->getOperand(0));
2981 for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
2982 X = X.umax(getSignedRange(UMax->getOperand(i)));
Dan Gohman52fddd32010-01-26 04:40:18 +00002983 return ConservativeResult.intersectWith(X);
Dan Gohman85b05a22009-07-13 21:35:55 +00002984 }
Dan Gohman62849c02009-06-24 01:05:09 +00002985
Dan Gohman85b05a22009-07-13 21:35:55 +00002986 if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
2987 ConstantRange X = getSignedRange(UDiv->getLHS());
2988 ConstantRange Y = getSignedRange(UDiv->getRHS());
Dan Gohman52fddd32010-01-26 04:40:18 +00002989 return ConservativeResult.intersectWith(X.udiv(Y));
Dan Gohman85b05a22009-07-13 21:35:55 +00002990 }
Dan Gohman62849c02009-06-24 01:05:09 +00002991
Dan Gohman85b05a22009-07-13 21:35:55 +00002992 if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
2993 ConstantRange X = getSignedRange(ZExt->getOperand());
Dan Gohman52fddd32010-01-26 04:40:18 +00002994 return ConservativeResult.intersectWith(X.zeroExtend(BitWidth));
Dan Gohman85b05a22009-07-13 21:35:55 +00002995 }
2996
2997 if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
2998 ConstantRange X = getSignedRange(SExt->getOperand());
Dan Gohman52fddd32010-01-26 04:40:18 +00002999 return ConservativeResult.intersectWith(X.signExtend(BitWidth));
Dan Gohman85b05a22009-07-13 21:35:55 +00003000 }
3001
3002 if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
3003 ConstantRange X = getSignedRange(Trunc->getOperand());
Dan Gohman52fddd32010-01-26 04:40:18 +00003004 return ConservativeResult.intersectWith(X.truncate(BitWidth));
Dan Gohman85b05a22009-07-13 21:35:55 +00003005 }
3006
Dan Gohman85b05a22009-07-13 21:35:55 +00003007 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
Dan Gohmana10756e2010-01-21 02:09:26 +00003008 // If there's no signed wrap, and all the operands have the same sign or
3009 // zero, the value won't ever change sign.
3010 if (AddRec->hasNoSignedWrap()) {
3011 bool AllNonNeg = true;
3012 bool AllNonPos = true;
3013 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
3014 if (!isKnownNonNegative(AddRec->getOperand(i))) AllNonNeg = false;
3015 if (!isKnownNonPositive(AddRec->getOperand(i))) AllNonPos = false;
3016 }
Dan Gohmana10756e2010-01-21 02:09:26 +00003017 if (AllNonNeg)
Dan Gohman52fddd32010-01-26 04:40:18 +00003018 ConservativeResult = ConservativeResult.intersectWith(
3019 ConstantRange(APInt(BitWidth, 0),
3020 APInt::getSignedMinValue(BitWidth)));
Dan Gohmana10756e2010-01-21 02:09:26 +00003021 else if (AllNonPos)
Dan Gohman52fddd32010-01-26 04:40:18 +00003022 ConservativeResult = ConservativeResult.intersectWith(
3023 ConstantRange(APInt::getSignedMinValue(BitWidth),
3024 APInt(BitWidth, 1)));
Dan Gohmana10756e2010-01-21 02:09:26 +00003025 }
Dan Gohman85b05a22009-07-13 21:35:55 +00003026
3027 // TODO: non-affine addrec
Dan Gohmanc9c36cb2010-01-26 19:19:05 +00003028 if (AddRec->isAffine()) {
Dan Gohman85b05a22009-07-13 21:35:55 +00003029 const Type *Ty = AddRec->getType();
3030 const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop());
Dan Gohmanc9c36cb2010-01-26 19:19:05 +00003031 if (!isa<SCEVCouldNotCompute>(MaxBECount) &&
3032 getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) {
Dan Gohman85b05a22009-07-13 21:35:55 +00003033 MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty);
3034
3035 const SCEV *Start = AddRec->getStart();
Dan Gohman85b05a22009-07-13 21:35:55 +00003036 const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this);
3037
3038 // Check for overflow.
Dan Gohmana10756e2010-01-21 02:09:26 +00003039 if (!AddRec->hasNoSignedWrap())
3040 return ConservativeResult;
Dan Gohman85b05a22009-07-13 21:35:55 +00003041
3042 ConstantRange StartRange = getSignedRange(Start);
3043 ConstantRange EndRange = getSignedRange(End);
3044 APInt Min = APIntOps::smin(StartRange.getSignedMin(),
3045 EndRange.getSignedMin());
3046 APInt Max = APIntOps::smax(StartRange.getSignedMax(),
3047 EndRange.getSignedMax());
3048 if (Min.isMinSignedValue() && Max.isMaxSignedValue())
Dan Gohmana10756e2010-01-21 02:09:26 +00003049 return ConservativeResult;
Dan Gohman52fddd32010-01-26 04:40:18 +00003050 return ConservativeResult.intersectWith(ConstantRange(Min, Max+1));
Dan Gohman62849c02009-06-24 01:05:09 +00003051 }
Dan Gohman62849c02009-06-24 01:05:09 +00003052 }
Dan Gohmana10756e2010-01-21 02:09:26 +00003053
3054 return ConservativeResult;
Dan Gohman62849c02009-06-24 01:05:09 +00003055 }
3056
Dan Gohman2c364ad2009-06-19 23:29:04 +00003057 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
3058 // For a SCEVUnknown, ask ValueTracking.
Dan Gohmana10756e2010-01-21 02:09:26 +00003059 if (!U->getValue()->getType()->isInteger() && !TD)
Dan Gohman52fddd32010-01-26 04:40:18 +00003060 return ConservativeResult;
Dan Gohman85b05a22009-07-13 21:35:55 +00003061 unsigned NS = ComputeNumSignBits(U->getValue(), TD);
3062 if (NS == 1)
Dan Gohman52fddd32010-01-26 04:40:18 +00003063 return ConservativeResult;
3064 return ConservativeResult.intersectWith(
Dan Gohman85b05a22009-07-13 21:35:55 +00003065 ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1),
Dan Gohman52fddd32010-01-26 04:40:18 +00003066 APInt::getSignedMaxValue(BitWidth).ashr(NS - 1)+1));
Dan Gohman2c364ad2009-06-19 23:29:04 +00003067 }
3068
Dan Gohman52fddd32010-01-26 04:40:18 +00003069 return ConservativeResult;
Dan Gohman2c364ad2009-06-19 23:29:04 +00003070}
3071
Chris Lattner53e677a2004-04-02 20:23:17 +00003072/// createSCEV - We know that there is no SCEV for the specified value.
3073/// Analyze the expression.
3074///
Dan Gohman0bba49c2009-07-07 17:06:11 +00003075const SCEV *ScalarEvolution::createSCEV(Value *V) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +00003076 if (!isSCEVable(V->getType()))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003077 return getUnknown(V);
Dan Gohman2d1be872009-04-16 03:18:22 +00003078
Dan Gohman6c459a22008-06-22 19:56:46 +00003079 unsigned Opcode = Instruction::UserOp1;
3080 if (Instruction *I = dyn_cast<Instruction>(V))
3081 Opcode = I->getOpcode();
3082 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
3083 Opcode = CE->getOpcode();
Dan Gohman6bbcba12009-06-24 00:54:57 +00003084 else if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
3085 return getConstant(CI);
3086 else if (isa<ConstantPointerNull>(V))
3087 return getIntegerSCEV(0, V->getType());
3088 else if (isa<UndefValue>(V))
3089 return getIntegerSCEV(0, V->getType());
Dan Gohman26812322009-08-25 17:49:57 +00003090 else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
3091 return GA->mayBeOverridden() ? getUnknown(V) : getSCEV(GA->getAliasee());
Dan Gohman6c459a22008-06-22 19:56:46 +00003092 else
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003093 return getUnknown(V);
Chris Lattner2811f2a2007-04-02 05:41:38 +00003094
Dan Gohmanca178902009-07-17 20:47:02 +00003095 Operator *U = cast<Operator>(V);
Dan Gohman6c459a22008-06-22 19:56:46 +00003096 switch (Opcode) {
Dan Gohman7a721952009-10-09 16:35:06 +00003097 case Instruction::Add:
3098 // Don't transfer the NSW and NUW bits from the Add instruction to the
3099 // Add expression, because the Instruction may be guarded by control
3100 // flow and the no-overflow bits may not be valid for the expression in
3101 // any context.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003102 return getAddExpr(getSCEV(U->getOperand(0)),
Dan Gohman7a721952009-10-09 16:35:06 +00003103 getSCEV(U->getOperand(1)));
3104 case Instruction::Mul:
3105 // Don't transfer the NSW and NUW bits from the Mul instruction to the
3106 // Mul expression, as with Add.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003107 return getMulExpr(getSCEV(U->getOperand(0)),
Dan Gohman7a721952009-10-09 16:35:06 +00003108 getSCEV(U->getOperand(1)));
Dan Gohman6c459a22008-06-22 19:56:46 +00003109 case Instruction::UDiv:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003110 return getUDivExpr(getSCEV(U->getOperand(0)),
3111 getSCEV(U->getOperand(1)));
Dan Gohman6c459a22008-06-22 19:56:46 +00003112 case Instruction::Sub:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003113 return getMinusSCEV(getSCEV(U->getOperand(0)),
3114 getSCEV(U->getOperand(1)));
Dan Gohman4ee29af2009-04-21 02:26:00 +00003115 case Instruction::And:
3116 // For an expression like x&255 that merely masks off the high bits,
3117 // use zext(trunc(x)) as the SCEV expression.
3118 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman2c73d5f2009-04-25 17:05:40 +00003119 if (CI->isNullValue())
3120 return getSCEV(U->getOperand(1));
Dan Gohmand6c32952009-04-27 01:41:10 +00003121 if (CI->isAllOnesValue())
3122 return getSCEV(U->getOperand(0));
Dan Gohman4ee29af2009-04-21 02:26:00 +00003123 const APInt &A = CI->getValue();
Dan Gohman61ffa8e2009-06-16 19:52:01 +00003124
3125 // Instcombine's ShrinkDemandedConstant may strip bits out of
3126 // constants, obscuring what would otherwise be a low-bits mask.
3127 // Use ComputeMaskedBits to compute what ShrinkDemandedConstant
3128 // knew about to reconstruct a low-bits mask value.
3129 unsigned LZ = A.countLeadingZeros();
3130 unsigned BitWidth = A.getBitWidth();
3131 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
3132 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
3133 ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD);
3134
3135 APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ);
3136
Dan Gohmanfc3641b2009-06-17 23:54:37 +00003137 if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask))
Dan Gohman4ee29af2009-04-21 02:26:00 +00003138 return
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003139 getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)),
Owen Anderson1d0be152009-08-13 21:58:54 +00003140 IntegerType::get(getContext(), BitWidth - LZ)),
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003141 U->getType());
Dan Gohman4ee29af2009-04-21 02:26:00 +00003142 }
3143 break;
Dan Gohman61ffa8e2009-06-16 19:52:01 +00003144
Dan Gohman6c459a22008-06-22 19:56:46 +00003145 case Instruction::Or:
3146 // If the RHS of the Or is a constant, we may have something like:
3147 // X*4+1 which got turned into X*4|1. Handle this as an Add so loop
3148 // optimizations will transparently handle this case.
3149 //
3150 // In order for this transformation to be safe, the LHS must be of the
3151 // form X*(2^n) and the Or constant must be less than 2^n.
3152 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00003153 const SCEV *LHS = getSCEV(U->getOperand(0));
Dan Gohman6c459a22008-06-22 19:56:46 +00003154 const APInt &CIVal = CI->getValue();
Dan Gohman2c364ad2009-06-19 23:29:04 +00003155 if (GetMinTrailingZeros(LHS) >=
Dan Gohman1f96e672009-09-17 18:05:20 +00003156 (CIVal.getBitWidth() - CIVal.countLeadingZeros())) {
3157 // Build a plain add SCEV.
3158 const SCEV *S = getAddExpr(LHS, getSCEV(CI));
3159 // If the LHS of the add was an addrec and it has no-wrap flags,
3160 // transfer the no-wrap flags, since an or won't introduce a wrap.
3161 if (const SCEVAddRecExpr *NewAR = dyn_cast<SCEVAddRecExpr>(S)) {
3162 const SCEVAddRecExpr *OldAR = cast<SCEVAddRecExpr>(LHS);
3163 if (OldAR->hasNoUnsignedWrap())
3164 const_cast<SCEVAddRecExpr *>(NewAR)->setHasNoUnsignedWrap(true);
3165 if (OldAR->hasNoSignedWrap())
3166 const_cast<SCEVAddRecExpr *>(NewAR)->setHasNoSignedWrap(true);
3167 }
3168 return S;
3169 }
Chris Lattner53e677a2004-04-02 20:23:17 +00003170 }
Dan Gohman6c459a22008-06-22 19:56:46 +00003171 break;
3172 case Instruction::Xor:
Dan Gohman6c459a22008-06-22 19:56:46 +00003173 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Nick Lewycky01eaf802008-07-07 06:15:49 +00003174 // If the RHS of the xor is a signbit, then this is just an add.
3175 // Instcombine turns add of signbit into xor as a strength reduction step.
Dan Gohman6c459a22008-06-22 19:56:46 +00003176 if (CI->getValue().isSignBit())
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003177 return getAddExpr(getSCEV(U->getOperand(0)),
3178 getSCEV(U->getOperand(1)));
Nick Lewycky01eaf802008-07-07 06:15:49 +00003179
3180 // If the RHS of xor is -1, then this is a not operation.
Dan Gohman0bac95e2009-05-18 16:17:44 +00003181 if (CI->isAllOnesValue())
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003182 return getNotSCEV(getSCEV(U->getOperand(0)));
Dan Gohman10978bd2009-05-18 16:29:04 +00003183
3184 // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask.
3185 // This is a variant of the check for xor with -1, and it handles
3186 // the case where instcombine has trimmed non-demanded bits out
3187 // of an xor with -1.
3188 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0)))
3189 if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1)))
3190 if (BO->getOpcode() == Instruction::And &&
3191 LCI->getValue() == CI->getValue())
3192 if (const SCEVZeroExtendExpr *Z =
Dan Gohman3034c102009-06-17 01:22:39 +00003193 dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) {
Dan Gohman82052832009-06-18 00:00:20 +00003194 const Type *UTy = U->getType();
Dan Gohman0bba49c2009-07-07 17:06:11 +00003195 const SCEV *Z0 = Z->getOperand();
Dan Gohman82052832009-06-18 00:00:20 +00003196 const Type *Z0Ty = Z0->getType();
3197 unsigned Z0TySize = getTypeSizeInBits(Z0Ty);
3198
3199 // If C is a low-bits mask, the zero extend is zerving to
3200 // mask off the high bits. Complement the operand and
3201 // re-apply the zext.
3202 if (APIntOps::isMask(Z0TySize, CI->getValue()))
3203 return getZeroExtendExpr(getNotSCEV(Z0), UTy);
3204
3205 // If C is a single bit, it may be in the sign-bit position
3206 // before the zero-extend. In this case, represent the xor
3207 // using an add, which is equivalent, and re-apply the zext.
3208 APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize);
3209 if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() &&
3210 Trunc.isSignBit())
3211 return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)),
3212 UTy);
Dan Gohman3034c102009-06-17 01:22:39 +00003213 }
Dan Gohman6c459a22008-06-22 19:56:46 +00003214 }
3215 break;
3216
3217 case Instruction::Shl:
3218 // Turn shift left of a constant amount into a multiply.
3219 if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman4f8eea82010-02-01 18:27:38 +00003220 uint32_t BitWidth = cast<IntegerType>(U->getType())->getBitWidth();
Owen Andersoneed707b2009-07-24 23:12:02 +00003221 Constant *X = ConstantInt::get(getContext(),
Dan Gohman6c459a22008-06-22 19:56:46 +00003222 APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003223 return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X));
Dan Gohman6c459a22008-06-22 19:56:46 +00003224 }
3225 break;
3226
Nick Lewycky01eaf802008-07-07 06:15:49 +00003227 case Instruction::LShr:
Nick Lewycky789558d2009-01-13 09:18:58 +00003228 // Turn logical shift right of a constant into a unsigned divide.
Nick Lewycky01eaf802008-07-07 06:15:49 +00003229 if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman4f8eea82010-02-01 18:27:38 +00003230 uint32_t BitWidth = cast<IntegerType>(U->getType())->getBitWidth();
Owen Andersoneed707b2009-07-24 23:12:02 +00003231 Constant *X = ConstantInt::get(getContext(),
Nick Lewycky01eaf802008-07-07 06:15:49 +00003232 APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003233 return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X));
Nick Lewycky01eaf802008-07-07 06:15:49 +00003234 }
3235 break;
3236
Dan Gohman4ee29af2009-04-21 02:26:00 +00003237 case Instruction::AShr:
3238 // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression.
3239 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1)))
3240 if (Instruction *L = dyn_cast<Instruction>(U->getOperand(0)))
3241 if (L->getOpcode() == Instruction::Shl &&
3242 L->getOperand(1) == U->getOperand(1)) {
Dan Gohman2c73d5f2009-04-25 17:05:40 +00003243 unsigned BitWidth = getTypeSizeInBits(U->getType());
3244 uint64_t Amt = BitWidth - CI->getZExtValue();
3245 if (Amt == BitWidth)
3246 return getSCEV(L->getOperand(0)); // shift by zero --> noop
3247 if (Amt > BitWidth)
3248 return getIntegerSCEV(0, U->getType()); // value is undefined
Dan Gohman4ee29af2009-04-21 02:26:00 +00003249 return
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003250 getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)),
Owen Anderson1d0be152009-08-13 21:58:54 +00003251 IntegerType::get(getContext(), Amt)),
Dan Gohman4ee29af2009-04-21 02:26:00 +00003252 U->getType());
3253 }
3254 break;
3255
Dan Gohman6c459a22008-06-22 19:56:46 +00003256 case Instruction::Trunc:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003257 return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman6c459a22008-06-22 19:56:46 +00003258
3259 case Instruction::ZExt:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003260 return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman6c459a22008-06-22 19:56:46 +00003261
3262 case Instruction::SExt:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003263 return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman6c459a22008-06-22 19:56:46 +00003264
3265 case Instruction::BitCast:
3266 // BitCasts are no-op casts so we just eliminate the cast.
Dan Gohmanaf79fb52009-04-21 01:07:12 +00003267 if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType()))
Dan Gohman6c459a22008-06-22 19:56:46 +00003268 return getSCEV(U->getOperand(0));
3269 break;
3270
Dan Gohman4f8eea82010-02-01 18:27:38 +00003271 // It's tempting to handle inttoptr and ptrtoint as no-ops, however this can
3272 // lead to pointer expressions which cannot safely be expanded to GEPs,
3273 // because ScalarEvolution doesn't respect the GEP aliasing rules when
3274 // simplifying integer expressions.
Dan Gohman2d1be872009-04-16 03:18:22 +00003275
Dan Gohman26466c02009-05-08 20:26:55 +00003276 case Instruction::GetElementPtr:
Dan Gohmand281ed22009-12-18 02:09:29 +00003277 return createNodeForGEP(cast<GEPOperator>(U));
Dan Gohman2d1be872009-04-16 03:18:22 +00003278
Dan Gohman6c459a22008-06-22 19:56:46 +00003279 case Instruction::PHI:
3280 return createNodeForPHI(cast<PHINode>(U));
3281
3282 case Instruction::Select:
3283 // This could be a smax or umax that was lowered earlier.
3284 // Try to recover it.
3285 if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) {
3286 Value *LHS = ICI->getOperand(0);
3287 Value *RHS = ICI->getOperand(1);
3288 switch (ICI->getPredicate()) {
3289 case ICmpInst::ICMP_SLT:
3290 case ICmpInst::ICMP_SLE:
3291 std::swap(LHS, RHS);
3292 // fall through
3293 case ICmpInst::ICMP_SGT:
3294 case ICmpInst::ICMP_SGE:
3295 if (LHS == U->getOperand(1) && RHS == U->getOperand(2))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003296 return getSMaxExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman6c459a22008-06-22 19:56:46 +00003297 else if (LHS == U->getOperand(2) && RHS == U->getOperand(1))
Dan Gohmanf9a9a992009-06-22 03:18:45 +00003298 return getSMinExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman6c459a22008-06-22 19:56:46 +00003299 break;
3300 case ICmpInst::ICMP_ULT:
3301 case ICmpInst::ICMP_ULE:
3302 std::swap(LHS, RHS);
3303 // fall through
3304 case ICmpInst::ICMP_UGT:
3305 case ICmpInst::ICMP_UGE:
3306 if (LHS == U->getOperand(1) && RHS == U->getOperand(2))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003307 return getUMaxExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman6c459a22008-06-22 19:56:46 +00003308 else if (LHS == U->getOperand(2) && RHS == U->getOperand(1))
Dan Gohmanf9a9a992009-06-22 03:18:45 +00003309 return getUMinExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman6c459a22008-06-22 19:56:46 +00003310 break;
Dan Gohman30fb5122009-06-18 20:21:07 +00003311 case ICmpInst::ICMP_NE:
3312 // n != 0 ? n : 1 -> umax(n, 1)
3313 if (LHS == U->getOperand(1) &&
3314 isa<ConstantInt>(U->getOperand(2)) &&
3315 cast<ConstantInt>(U->getOperand(2))->isOne() &&
3316 isa<ConstantInt>(RHS) &&
3317 cast<ConstantInt>(RHS)->isZero())
3318 return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(2)));
3319 break;
3320 case ICmpInst::ICMP_EQ:
3321 // n == 0 ? 1 : n -> umax(n, 1)
3322 if (LHS == U->getOperand(2) &&
3323 isa<ConstantInt>(U->getOperand(1)) &&
3324 cast<ConstantInt>(U->getOperand(1))->isOne() &&
3325 isa<ConstantInt>(RHS) &&
3326 cast<ConstantInt>(RHS)->isZero())
3327 return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(1)));
3328 break;
Dan Gohman6c459a22008-06-22 19:56:46 +00003329 default:
3330 break;
3331 }
3332 }
3333
3334 default: // We cannot analyze this expression.
3335 break;
Chris Lattner53e677a2004-04-02 20:23:17 +00003336 }
3337
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003338 return getUnknown(V);
Chris Lattner53e677a2004-04-02 20:23:17 +00003339}
3340
3341
3342
3343//===----------------------------------------------------------------------===//
3344// Iteration Count Computation Code
3345//
3346
Dan Gohman46bdfb02009-02-24 18:55:53 +00003347/// getBackedgeTakenCount - If the specified loop has a predictable
3348/// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
3349/// object. The backedge-taken count is the number of times the loop header
3350/// will be branched to from within the loop. This is one less than the
3351/// trip count of the loop, since it doesn't count the first iteration,
3352/// when the header is branched to from outside the loop.
3353///
3354/// Note that it is not valid to call this method on a loop without a
3355/// loop-invariant backedge-taken count (see
3356/// hasLoopInvariantBackedgeTakenCount).
3357///
Dan Gohman0bba49c2009-07-07 17:06:11 +00003358const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) {
Dan Gohmana1af7572009-04-30 20:47:05 +00003359 return getBackedgeTakenInfo(L).Exact;
3360}
3361
3362/// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except
3363/// return the least SCEV value that is known never to be less than the
3364/// actual backedge taken count.
Dan Gohman0bba49c2009-07-07 17:06:11 +00003365const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) {
Dan Gohmana1af7572009-04-30 20:47:05 +00003366 return getBackedgeTakenInfo(L).Max;
3367}
3368
Dan Gohman59ae6b92009-07-08 19:23:34 +00003369/// PushLoopPHIs - Push PHI nodes in the header of the given loop
3370/// onto the given Worklist.
3371static void
3372PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) {
3373 BasicBlock *Header = L->getHeader();
3374
3375 // Push all Loop-header PHIs onto the Worklist stack.
3376 for (BasicBlock::iterator I = Header->begin();
3377 PHINode *PN = dyn_cast<PHINode>(I); ++I)
3378 Worklist.push_back(PN);
3379}
3380
Dan Gohmana1af7572009-04-30 20:47:05 +00003381const ScalarEvolution::BackedgeTakenInfo &
3382ScalarEvolution::getBackedgeTakenInfo(const Loop *L) {
Dan Gohman01ecca22009-04-27 20:16:15 +00003383 // Initially insert a CouldNotCompute for this loop. If the insertion
3384 // succeeds, procede to actually compute a backedge-taken count and
3385 // update the value. The temporary CouldNotCompute value tells SCEV
3386 // code elsewhere that it shouldn't attempt to request a new
3387 // backedge-taken count, which could result in infinite recursion.
Dan Gohman5d984912009-12-18 01:14:11 +00003388 std::pair<std::map<const Loop *, BackedgeTakenInfo>::iterator, bool> Pair =
Dan Gohman01ecca22009-04-27 20:16:15 +00003389 BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute()));
3390 if (Pair.second) {
Dan Gohman93dacad2010-01-26 16:46:18 +00003391 BackedgeTakenInfo BECount = ComputeBackedgeTakenCount(L);
3392 if (BECount.Exact != getCouldNotCompute()) {
3393 assert(BECount.Exact->isLoopInvariant(L) &&
3394 BECount.Max->isLoopInvariant(L) &&
3395 "Computed backedge-taken count isn't loop invariant for loop!");
Chris Lattner53e677a2004-04-02 20:23:17 +00003396 ++NumTripCountsComputed;
Dan Gohman01ecca22009-04-27 20:16:15 +00003397
Dan Gohman01ecca22009-04-27 20:16:15 +00003398 // Update the value in the map.
Dan Gohman93dacad2010-01-26 16:46:18 +00003399 Pair.first->second = BECount;
Dan Gohmana334aa72009-06-22 00:31:57 +00003400 } else {
Dan Gohman93dacad2010-01-26 16:46:18 +00003401 if (BECount.Max != getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003402 // Update the value in the map.
Dan Gohman93dacad2010-01-26 16:46:18 +00003403 Pair.first->second = BECount;
Dan Gohmana334aa72009-06-22 00:31:57 +00003404 if (isa<PHINode>(L->getHeader()->begin()))
3405 // Only count loops that have phi nodes as not being computable.
3406 ++NumTripCountsNotComputed;
Chris Lattner53e677a2004-04-02 20:23:17 +00003407 }
Dan Gohmana1af7572009-04-30 20:47:05 +00003408
3409 // Now that we know more about the trip count for this loop, forget any
3410 // existing SCEV values for PHI nodes in this loop since they are only
Dan Gohman59ae6b92009-07-08 19:23:34 +00003411 // conservative estimates made without the benefit of trip count
Dan Gohman4c7279a2009-10-31 15:04:55 +00003412 // information. This is similar to the code in forgetLoop, except that
3413 // it handles SCEVUnknown PHI nodes specially.
Dan Gohman93dacad2010-01-26 16:46:18 +00003414 if (BECount.hasAnyInfo()) {
Dan Gohman59ae6b92009-07-08 19:23:34 +00003415 SmallVector<Instruction *, 16> Worklist;
3416 PushLoopPHIs(L, Worklist);
3417
3418 SmallPtrSet<Instruction *, 8> Visited;
3419 while (!Worklist.empty()) {
3420 Instruction *I = Worklist.pop_back_val();
3421 if (!Visited.insert(I)) continue;
3422
Dan Gohman5d984912009-12-18 01:14:11 +00003423 std::map<SCEVCallbackVH, const SCEV *>::iterator It =
Dan Gohman59ae6b92009-07-08 19:23:34 +00003424 Scalars.find(static_cast<Value *>(I));
3425 if (It != Scalars.end()) {
3426 // SCEVUnknown for a PHI either means that it has an unrecognized
3427 // structure, or it's a PHI that's in the progress of being computed
Dan Gohmanba701882009-07-13 22:04:06 +00003428 // by createNodeForPHI. In the former case, additional loop trip
3429 // count information isn't going to change anything. In the later
3430 // case, createNodeForPHI will perform the necessary updates on its
3431 // own when it gets to that point.
Dan Gohman42214892009-08-31 21:15:23 +00003432 if (!isa<PHINode>(I) || !isa<SCEVUnknown>(It->second)) {
3433 ValuesAtScopes.erase(It->second);
Dan Gohman59ae6b92009-07-08 19:23:34 +00003434 Scalars.erase(It);
Dan Gohman42214892009-08-31 21:15:23 +00003435 }
Dan Gohman59ae6b92009-07-08 19:23:34 +00003436 if (PHINode *PN = dyn_cast<PHINode>(I))
3437 ConstantEvolutionLoopExitValue.erase(PN);
3438 }
3439
3440 PushDefUseChildren(I, Worklist);
3441 }
3442 }
Chris Lattner53e677a2004-04-02 20:23:17 +00003443 }
Dan Gohman01ecca22009-04-27 20:16:15 +00003444 return Pair.first->second;
Chris Lattner53e677a2004-04-02 20:23:17 +00003445}
3446
Dan Gohman4c7279a2009-10-31 15:04:55 +00003447/// forgetLoop - This method should be called by the client when it has
3448/// changed a loop in a way that may effect ScalarEvolution's ability to
3449/// compute a trip count, or if the loop is deleted.
3450void ScalarEvolution::forgetLoop(const Loop *L) {
3451 // Drop any stored trip count value.
Dan Gohman46bdfb02009-02-24 18:55:53 +00003452 BackedgeTakenCounts.erase(L);
Dan Gohmanfb7d35f2009-05-02 17:43:35 +00003453
Dan Gohman4c7279a2009-10-31 15:04:55 +00003454 // Drop information about expressions based on loop-header PHIs.
Dan Gohman35738ac2009-05-04 22:30:44 +00003455 SmallVector<Instruction *, 16> Worklist;
Dan Gohman59ae6b92009-07-08 19:23:34 +00003456 PushLoopPHIs(L, Worklist);
Dan Gohman35738ac2009-05-04 22:30:44 +00003457
Dan Gohman59ae6b92009-07-08 19:23:34 +00003458 SmallPtrSet<Instruction *, 8> Visited;
Dan Gohman35738ac2009-05-04 22:30:44 +00003459 while (!Worklist.empty()) {
3460 Instruction *I = Worklist.pop_back_val();
Dan Gohman59ae6b92009-07-08 19:23:34 +00003461 if (!Visited.insert(I)) continue;
3462
Dan Gohman5d984912009-12-18 01:14:11 +00003463 std::map<SCEVCallbackVH, const SCEV *>::iterator It =
Dan Gohman59ae6b92009-07-08 19:23:34 +00003464 Scalars.find(static_cast<Value *>(I));
3465 if (It != Scalars.end()) {
Dan Gohman42214892009-08-31 21:15:23 +00003466 ValuesAtScopes.erase(It->second);
Dan Gohman59ae6b92009-07-08 19:23:34 +00003467 Scalars.erase(It);
Dan Gohman59ae6b92009-07-08 19:23:34 +00003468 if (PHINode *PN = dyn_cast<PHINode>(I))
3469 ConstantEvolutionLoopExitValue.erase(PN);
3470 }
3471
3472 PushDefUseChildren(I, Worklist);
Dan Gohman35738ac2009-05-04 22:30:44 +00003473 }
Dan Gohman60f8a632009-02-17 20:49:49 +00003474}
3475
Dan Gohman46bdfb02009-02-24 18:55:53 +00003476/// ComputeBackedgeTakenCount - Compute the number of times the backedge
3477/// of the specified loop will execute.
Dan Gohmana1af7572009-04-30 20:47:05 +00003478ScalarEvolution::BackedgeTakenInfo
3479ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) {
Dan Gohman5d984912009-12-18 01:14:11 +00003480 SmallVector<BasicBlock *, 8> ExitingBlocks;
Dan Gohmana334aa72009-06-22 00:31:57 +00003481 L->getExitingBlocks(ExitingBlocks);
Chris Lattner53e677a2004-04-02 20:23:17 +00003482
Dan Gohmana334aa72009-06-22 00:31:57 +00003483 // Examine all exits and pick the most conservative values.
Dan Gohman0bba49c2009-07-07 17:06:11 +00003484 const SCEV *BECount = getCouldNotCompute();
3485 const SCEV *MaxBECount = getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003486 bool CouldNotComputeBECount = false;
Dan Gohmana334aa72009-06-22 00:31:57 +00003487 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
3488 BackedgeTakenInfo NewBTI =
3489 ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]);
Chris Lattner53e677a2004-04-02 20:23:17 +00003490
Dan Gohman1c343752009-06-27 21:21:31 +00003491 if (NewBTI.Exact == getCouldNotCompute()) {
Dan Gohmana334aa72009-06-22 00:31:57 +00003492 // We couldn't compute an exact value for this exit, so
Dan Gohmand32f5bf2009-06-22 21:10:22 +00003493 // we won't be able to compute an exact value for the loop.
Dan Gohmana334aa72009-06-22 00:31:57 +00003494 CouldNotComputeBECount = true;
Dan Gohman1c343752009-06-27 21:21:31 +00003495 BECount = getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003496 } else if (!CouldNotComputeBECount) {
Dan Gohman1c343752009-06-27 21:21:31 +00003497 if (BECount == getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003498 BECount = NewBTI.Exact;
Dan Gohmana334aa72009-06-22 00:31:57 +00003499 else
Dan Gohman40a5a1b2009-06-24 01:18:18 +00003500 BECount = getUMinFromMismatchedTypes(BECount, NewBTI.Exact);
Dan Gohmana334aa72009-06-22 00:31:57 +00003501 }
Dan Gohman1c343752009-06-27 21:21:31 +00003502 if (MaxBECount == getCouldNotCompute())
Dan Gohman40a5a1b2009-06-24 01:18:18 +00003503 MaxBECount = NewBTI.Max;
Dan Gohman1c343752009-06-27 21:21:31 +00003504 else if (NewBTI.Max != getCouldNotCompute())
Dan Gohman40a5a1b2009-06-24 01:18:18 +00003505 MaxBECount = getUMinFromMismatchedTypes(MaxBECount, NewBTI.Max);
Dan Gohmana334aa72009-06-22 00:31:57 +00003506 }
3507
3508 return BackedgeTakenInfo(BECount, MaxBECount);
3509}
3510
3511/// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge
3512/// of the specified loop will execute if it exits via the specified block.
3513ScalarEvolution::BackedgeTakenInfo
3514ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L,
3515 BasicBlock *ExitingBlock) {
3516
3517 // Okay, we've chosen an exiting block. See what condition causes us to
3518 // exit at this block.
Chris Lattner53e677a2004-04-02 20:23:17 +00003519 //
3520 // FIXME: we should be able to handle switch instructions (with a single exit)
Chris Lattner53e677a2004-04-02 20:23:17 +00003521 BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
Dan Gohman1c343752009-06-27 21:21:31 +00003522 if (ExitBr == 0) return getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00003523 assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!");
Dan Gohman64a845e2009-06-24 04:48:43 +00003524
Chris Lattner8b0e3602007-01-07 02:24:26 +00003525 // At this point, we know we have a conditional branch that determines whether
3526 // the loop is exited. However, we don't know if the branch is executed each
3527 // time through the loop. If not, then the execution count of the branch will
3528 // not be equal to the trip count of the loop.
3529 //
3530 // Currently we check for this by checking to see if the Exit branch goes to
3531 // the loop header. If so, we know it will always execute the same number of
Chris Lattner192e4032007-01-14 01:24:47 +00003532 // times as the loop. We also handle the case where the exit block *is* the
Dan Gohmana334aa72009-06-22 00:31:57 +00003533 // loop header. This is common for un-rotated loops.
3534 //
3535 // If both of those tests fail, walk up the unique predecessor chain to the
3536 // header, stopping if there is an edge that doesn't exit the loop. If the
3537 // header is reached, the execution count of the branch will be equal to the
3538 // trip count of the loop.
3539 //
3540 // More extensive analysis could be done to handle more cases here.
3541 //
Chris Lattner8b0e3602007-01-07 02:24:26 +00003542 if (ExitBr->getSuccessor(0) != L->getHeader() &&
Chris Lattner192e4032007-01-14 01:24:47 +00003543 ExitBr->getSuccessor(1) != L->getHeader() &&
Dan Gohmana334aa72009-06-22 00:31:57 +00003544 ExitBr->getParent() != L->getHeader()) {
3545 // The simple checks failed, try climbing the unique predecessor chain
3546 // up to the header.
3547 bool Ok = false;
3548 for (BasicBlock *BB = ExitBr->getParent(); BB; ) {
3549 BasicBlock *Pred = BB->getUniquePredecessor();
3550 if (!Pred)
Dan Gohman1c343752009-06-27 21:21:31 +00003551 return getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003552 TerminatorInst *PredTerm = Pred->getTerminator();
3553 for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) {
3554 BasicBlock *PredSucc = PredTerm->getSuccessor(i);
3555 if (PredSucc == BB)
3556 continue;
3557 // If the predecessor has a successor that isn't BB and isn't
3558 // outside the loop, assume the worst.
3559 if (L->contains(PredSucc))
Dan Gohman1c343752009-06-27 21:21:31 +00003560 return getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003561 }
3562 if (Pred == L->getHeader()) {
3563 Ok = true;
3564 break;
3565 }
3566 BB = Pred;
3567 }
3568 if (!Ok)
Dan Gohman1c343752009-06-27 21:21:31 +00003569 return getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003570 }
3571
3572 // Procede to the next level to examine the exit condition expression.
3573 return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(),
3574 ExitBr->getSuccessor(0),
3575 ExitBr->getSuccessor(1));
3576}
3577
3578/// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the
3579/// backedge of the specified loop will execute if its exit condition
3580/// were a conditional branch of ExitCond, TBB, and FBB.
3581ScalarEvolution::BackedgeTakenInfo
3582ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L,
3583 Value *ExitCond,
3584 BasicBlock *TBB,
3585 BasicBlock *FBB) {
Dan Gohman40a5a1b2009-06-24 01:18:18 +00003586 // Check if the controlling expression for this loop is an And or Or.
Dan Gohmana334aa72009-06-22 00:31:57 +00003587 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) {
3588 if (BO->getOpcode() == Instruction::And) {
3589 // Recurse on the operands of the and.
3590 BackedgeTakenInfo BTI0 =
3591 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB);
3592 BackedgeTakenInfo BTI1 =
3593 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB);
Dan Gohman0bba49c2009-07-07 17:06:11 +00003594 const SCEV *BECount = getCouldNotCompute();
3595 const SCEV *MaxBECount = getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003596 if (L->contains(TBB)) {
3597 // Both conditions must be true for the loop to continue executing.
3598 // Choose the less conservative count.
Dan Gohman1c343752009-06-27 21:21:31 +00003599 if (BTI0.Exact == getCouldNotCompute() ||
3600 BTI1.Exact == getCouldNotCompute())
3601 BECount = getCouldNotCompute();
Dan Gohman60e9b072009-06-22 15:09:28 +00003602 else
3603 BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
Dan Gohman1c343752009-06-27 21:21:31 +00003604 if (BTI0.Max == getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003605 MaxBECount = BTI1.Max;
Dan Gohman1c343752009-06-27 21:21:31 +00003606 else if (BTI1.Max == getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003607 MaxBECount = BTI0.Max;
Dan Gohman60e9b072009-06-22 15:09:28 +00003608 else
3609 MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max);
Dan Gohmana334aa72009-06-22 00:31:57 +00003610 } else {
3611 // Both conditions must be true for the loop to exit.
3612 assert(L->contains(FBB) && "Loop block has no successor in loop!");
Dan Gohman1c343752009-06-27 21:21:31 +00003613 if (BTI0.Exact != getCouldNotCompute() &&
3614 BTI1.Exact != getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003615 BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
Dan Gohman1c343752009-06-27 21:21:31 +00003616 if (BTI0.Max != getCouldNotCompute() &&
3617 BTI1.Max != getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003618 MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max);
3619 }
3620
3621 return BackedgeTakenInfo(BECount, MaxBECount);
3622 }
3623 if (BO->getOpcode() == Instruction::Or) {
3624 // Recurse on the operands of the or.
3625 BackedgeTakenInfo BTI0 =
3626 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB);
3627 BackedgeTakenInfo BTI1 =
3628 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB);
Dan Gohman0bba49c2009-07-07 17:06:11 +00003629 const SCEV *BECount = getCouldNotCompute();
3630 const SCEV *MaxBECount = getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003631 if (L->contains(FBB)) {
3632 // Both conditions must be false for the loop to continue executing.
3633 // Choose the less conservative count.
Dan Gohman1c343752009-06-27 21:21:31 +00003634 if (BTI0.Exact == getCouldNotCompute() ||
3635 BTI1.Exact == getCouldNotCompute())
3636 BECount = getCouldNotCompute();
Dan Gohman60e9b072009-06-22 15:09:28 +00003637 else
3638 BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
Dan Gohman1c343752009-06-27 21:21:31 +00003639 if (BTI0.Max == getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003640 MaxBECount = BTI1.Max;
Dan Gohman1c343752009-06-27 21:21:31 +00003641 else if (BTI1.Max == getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003642 MaxBECount = BTI0.Max;
Dan Gohman60e9b072009-06-22 15:09:28 +00003643 else
3644 MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max);
Dan Gohmana334aa72009-06-22 00:31:57 +00003645 } else {
3646 // Both conditions must be false for the loop to exit.
3647 assert(L->contains(TBB) && "Loop block has no successor in loop!");
Dan Gohman1c343752009-06-27 21:21:31 +00003648 if (BTI0.Exact != getCouldNotCompute() &&
3649 BTI1.Exact != getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003650 BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
Dan Gohman1c343752009-06-27 21:21:31 +00003651 if (BTI0.Max != getCouldNotCompute() &&
3652 BTI1.Max != getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003653 MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max);
3654 }
3655
3656 return BackedgeTakenInfo(BECount, MaxBECount);
3657 }
3658 }
3659
3660 // With an icmp, it may be feasible to compute an exact backedge-taken count.
3661 // Procede to the next level to examine the icmp.
3662 if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond))
3663 return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB);
Reid Spencere4d87aa2006-12-23 06:05:41 +00003664
Eli Friedman361e54d2009-05-09 12:32:42 +00003665 // If it's not an integer or pointer comparison then compute it the hard way.
Dan Gohmana334aa72009-06-22 00:31:57 +00003666 return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB));
3667}
3668
3669/// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the
3670/// backedge of the specified loop will execute if its exit condition
3671/// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB.
3672ScalarEvolution::BackedgeTakenInfo
3673ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L,
3674 ICmpInst *ExitCond,
3675 BasicBlock *TBB,
3676 BasicBlock *FBB) {
Chris Lattner53e677a2004-04-02 20:23:17 +00003677
Reid Spencere4d87aa2006-12-23 06:05:41 +00003678 // If the condition was exit on true, convert the condition to exit on false
3679 ICmpInst::Predicate Cond;
Dan Gohmana334aa72009-06-22 00:31:57 +00003680 if (!L->contains(FBB))
Reid Spencere4d87aa2006-12-23 06:05:41 +00003681 Cond = ExitCond->getPredicate();
Chris Lattner673e02b2004-10-12 01:49:27 +00003682 else
Reid Spencere4d87aa2006-12-23 06:05:41 +00003683 Cond = ExitCond->getInversePredicate();
Chris Lattner673e02b2004-10-12 01:49:27 +00003684
3685 // Handle common loops like: for (X = "string"; *X; ++X)
3686 if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0)))
3687 if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00003688 const SCEV *ItCnt =
Dan Gohman46bdfb02009-02-24 18:55:53 +00003689 ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond);
Dan Gohmana334aa72009-06-22 00:31:57 +00003690 if (!isa<SCEVCouldNotCompute>(ItCnt)) {
3691 unsigned BitWidth = getTypeSizeInBits(ItCnt->getType());
3692 return BackedgeTakenInfo(ItCnt,
3693 isa<SCEVConstant>(ItCnt) ? ItCnt :
3694 getConstant(APInt::getMaxValue(BitWidth)-1));
3695 }
Chris Lattner673e02b2004-10-12 01:49:27 +00003696 }
3697
Dan Gohman0bba49c2009-07-07 17:06:11 +00003698 const SCEV *LHS = getSCEV(ExitCond->getOperand(0));
3699 const SCEV *RHS = getSCEV(ExitCond->getOperand(1));
Chris Lattner53e677a2004-04-02 20:23:17 +00003700
3701 // Try to evaluate any dependencies out of the loop.
Dan Gohmand594e6f2009-05-24 23:25:42 +00003702 LHS = getSCEVAtScope(LHS, L);
3703 RHS = getSCEVAtScope(RHS, L);
Chris Lattner53e677a2004-04-02 20:23:17 +00003704
Dan Gohman64a845e2009-06-24 04:48:43 +00003705 // At this point, we would like to compute how many iterations of the
Reid Spencere4d87aa2006-12-23 06:05:41 +00003706 // loop the predicate will return true for these inputs.
Dan Gohman70ff4cf2008-09-16 18:52:57 +00003707 if (LHS->isLoopInvariant(L) && !RHS->isLoopInvariant(L)) {
3708 // If there is a loop-invariant, force it into the RHS.
Chris Lattner53e677a2004-04-02 20:23:17 +00003709 std::swap(LHS, RHS);
Reid Spencere4d87aa2006-12-23 06:05:41 +00003710 Cond = ICmpInst::getSwappedPredicate(Cond);
Chris Lattner53e677a2004-04-02 20:23:17 +00003711 }
3712
Chris Lattner53e677a2004-04-02 20:23:17 +00003713 // If we have a comparison of a chrec against a constant, try to use value
3714 // ranges to answer this query.
Dan Gohman622ed672009-05-04 22:02:23 +00003715 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
3716 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
Chris Lattner53e677a2004-04-02 20:23:17 +00003717 if (AddRec->getLoop() == L) {
Eli Friedman361e54d2009-05-09 12:32:42 +00003718 // Form the constant range.
3719 ConstantRange CompRange(
3720 ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue()));
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003721
Dan Gohman0bba49c2009-07-07 17:06:11 +00003722 const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this);
Eli Friedman361e54d2009-05-09 12:32:42 +00003723 if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
Chris Lattner53e677a2004-04-02 20:23:17 +00003724 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003725
Chris Lattner53e677a2004-04-02 20:23:17 +00003726 switch (Cond) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00003727 case ICmpInst::ICMP_NE: { // while (X != Y)
Chris Lattner53e677a2004-04-02 20:23:17 +00003728 // Convert to: while (X-Y != 0)
Dan Gohman0bba49c2009-07-07 17:06:11 +00003729 const SCEV *TC = HowFarToZero(getMinusSCEV(LHS, RHS), L);
Reid Spencere4d87aa2006-12-23 06:05:41 +00003730 if (!isa<SCEVCouldNotCompute>(TC)) return TC;
Chris Lattner53e677a2004-04-02 20:23:17 +00003731 break;
Reid Spencere4d87aa2006-12-23 06:05:41 +00003732 }
Dan Gohman4c0d5d52009-08-20 16:42:55 +00003733 case ICmpInst::ICMP_EQ: { // while (X == Y)
3734 // Convert to: while (X-Y == 0)
Dan Gohman0bba49c2009-07-07 17:06:11 +00003735 const SCEV *TC = HowFarToNonZero(getMinusSCEV(LHS, RHS), L);
Reid Spencere4d87aa2006-12-23 06:05:41 +00003736 if (!isa<SCEVCouldNotCompute>(TC)) return TC;
Chris Lattner53e677a2004-04-02 20:23:17 +00003737 break;
Reid Spencere4d87aa2006-12-23 06:05:41 +00003738 }
3739 case ICmpInst::ICMP_SLT: {
Dan Gohmana1af7572009-04-30 20:47:05 +00003740 BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true);
3741 if (BTI.hasAnyInfo()) return BTI;
Chris Lattnerdb25de42005-08-15 23:33:51 +00003742 break;
Reid Spencere4d87aa2006-12-23 06:05:41 +00003743 }
3744 case ICmpInst::ICMP_SGT: {
Dan Gohmana1af7572009-04-30 20:47:05 +00003745 BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS),
3746 getNotSCEV(RHS), L, true);
3747 if (BTI.hasAnyInfo()) return BTI;
Nick Lewyckyd6dac0e2007-08-06 19:21:00 +00003748 break;
3749 }
3750 case ICmpInst::ICMP_ULT: {
Dan Gohmana1af7572009-04-30 20:47:05 +00003751 BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false);
3752 if (BTI.hasAnyInfo()) return BTI;
Nick Lewyckyd6dac0e2007-08-06 19:21:00 +00003753 break;
3754 }
3755 case ICmpInst::ICMP_UGT: {
Dan Gohmana1af7572009-04-30 20:47:05 +00003756 BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS),
3757 getNotSCEV(RHS), L, false);
3758 if (BTI.hasAnyInfo()) return BTI;
Chris Lattnerdb25de42005-08-15 23:33:51 +00003759 break;
Reid Spencere4d87aa2006-12-23 06:05:41 +00003760 }
Chris Lattner53e677a2004-04-02 20:23:17 +00003761 default:
Chris Lattnerd18d9dc2004-04-02 20:26:46 +00003762#if 0
David Greene25e0e872009-12-23 22:18:14 +00003763 dbgs() << "ComputeBackedgeTakenCount ";
Chris Lattner53e677a2004-04-02 20:23:17 +00003764 if (ExitCond->getOperand(0)->getType()->isUnsigned())
David Greene25e0e872009-12-23 22:18:14 +00003765 dbgs() << "[unsigned] ";
3766 dbgs() << *LHS << " "
Dan Gohman64a845e2009-06-24 04:48:43 +00003767 << Instruction::getOpcodeName(Instruction::ICmp)
Reid Spencere4d87aa2006-12-23 06:05:41 +00003768 << " " << *RHS << "\n";
Chris Lattnerd18d9dc2004-04-02 20:26:46 +00003769#endif
Chris Lattnere34c0b42004-04-03 00:43:03 +00003770 break;
Chris Lattner53e677a2004-04-02 20:23:17 +00003771 }
Dan Gohman46bdfb02009-02-24 18:55:53 +00003772 return
Dan Gohmana334aa72009-06-22 00:31:57 +00003773 ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB));
Chris Lattner7980fb92004-04-17 18:36:24 +00003774}
3775
Chris Lattner673e02b2004-10-12 01:49:27 +00003776static ConstantInt *
Dan Gohman246b2562007-10-22 18:31:58 +00003777EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C,
3778 ScalarEvolution &SE) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00003779 const SCEV *InVal = SE.getConstant(C);
3780 const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE);
Chris Lattner673e02b2004-10-12 01:49:27 +00003781 assert(isa<SCEVConstant>(Val) &&
3782 "Evaluation of SCEV at constant didn't fold correctly?");
3783 return cast<SCEVConstant>(Val)->getValue();
3784}
3785
3786/// GetAddressedElementFromGlobal - Given a global variable with an initializer
3787/// and a GEP expression (missing the pointer index) indexing into it, return
3788/// the addressed element of the initializer or null if the index expression is
3789/// invalid.
3790static Constant *
Nick Lewyckyc6501b12009-11-23 03:26:09 +00003791GetAddressedElementFromGlobal(GlobalVariable *GV,
Chris Lattner673e02b2004-10-12 01:49:27 +00003792 const std::vector<ConstantInt*> &Indices) {
3793 Constant *Init = GV->getInitializer();
3794 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
Reid Spencerb83eb642006-10-20 07:07:24 +00003795 uint64_t Idx = Indices[i]->getZExtValue();
Chris Lattner673e02b2004-10-12 01:49:27 +00003796 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) {
3797 assert(Idx < CS->getNumOperands() && "Bad struct index!");
3798 Init = cast<Constant>(CS->getOperand(Idx));
3799 } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) {
3800 if (Idx >= CA->getNumOperands()) return 0; // Bogus program
3801 Init = cast<Constant>(CA->getOperand(Idx));
3802 } else if (isa<ConstantAggregateZero>(Init)) {
3803 if (const StructType *STy = dyn_cast<StructType>(Init->getType())) {
3804 assert(Idx < STy->getNumElements() && "Bad struct index!");
Owen Andersona7235ea2009-07-31 20:28:14 +00003805 Init = Constant::getNullValue(STy->getElementType(Idx));
Chris Lattner673e02b2004-10-12 01:49:27 +00003806 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) {
3807 if (Idx >= ATy->getNumElements()) return 0; // Bogus program
Owen Andersona7235ea2009-07-31 20:28:14 +00003808 Init = Constant::getNullValue(ATy->getElementType());
Chris Lattner673e02b2004-10-12 01:49:27 +00003809 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003810 llvm_unreachable("Unknown constant aggregate type!");
Chris Lattner673e02b2004-10-12 01:49:27 +00003811 }
3812 return 0;
3813 } else {
3814 return 0; // Unknown initializer type
3815 }
3816 }
3817 return Init;
3818}
3819
Dan Gohman46bdfb02009-02-24 18:55:53 +00003820/// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of
3821/// 'icmp op load X, cst', try to see if we can compute the backedge
3822/// execution count.
Dan Gohman64a845e2009-06-24 04:48:43 +00003823const SCEV *
3824ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount(
3825 LoadInst *LI,
3826 Constant *RHS,
3827 const Loop *L,
3828 ICmpInst::Predicate predicate) {
Dan Gohman1c343752009-06-27 21:21:31 +00003829 if (LI->isVolatile()) return getCouldNotCompute();
Chris Lattner673e02b2004-10-12 01:49:27 +00003830
3831 // Check to see if the loaded pointer is a getelementptr of a global.
3832 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0));
Dan Gohman1c343752009-06-27 21:21:31 +00003833 if (!GEP) return getCouldNotCompute();
Chris Lattner673e02b2004-10-12 01:49:27 +00003834
3835 // Make sure that it is really a constant global we are gepping, with an
3836 // initializer, and make sure the first IDX is really 0.
3837 GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
Dan Gohman82555732009-08-19 18:20:44 +00003838 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
Chris Lattner673e02b2004-10-12 01:49:27 +00003839 GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) ||
3840 !cast<Constant>(GEP->getOperand(1))->isNullValue())
Dan Gohman1c343752009-06-27 21:21:31 +00003841 return getCouldNotCompute();
Chris Lattner673e02b2004-10-12 01:49:27 +00003842
3843 // Okay, we allow one non-constant index into the GEP instruction.
3844 Value *VarIdx = 0;
3845 std::vector<ConstantInt*> Indexes;
3846 unsigned VarIdxNum = 0;
3847 for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i)
3848 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
3849 Indexes.push_back(CI);
3850 } else if (!isa<ConstantInt>(GEP->getOperand(i))) {
Dan Gohman1c343752009-06-27 21:21:31 +00003851 if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's.
Chris Lattner673e02b2004-10-12 01:49:27 +00003852 VarIdx = GEP->getOperand(i);
3853 VarIdxNum = i-2;
3854 Indexes.push_back(0);
3855 }
3856
3857 // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant.
3858 // Check to see if X is a loop variant variable value now.
Dan Gohman0bba49c2009-07-07 17:06:11 +00003859 const SCEV *Idx = getSCEV(VarIdx);
Dan Gohmand594e6f2009-05-24 23:25:42 +00003860 Idx = getSCEVAtScope(Idx, L);
Chris Lattner673e02b2004-10-12 01:49:27 +00003861
3862 // We can only recognize very limited forms of loop index expressions, in
3863 // particular, only affine AddRec's like {C1,+,C2}.
Dan Gohman35738ac2009-05-04 22:30:44 +00003864 const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx);
Chris Lattner673e02b2004-10-12 01:49:27 +00003865 if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) ||
3866 !isa<SCEVConstant>(IdxExpr->getOperand(0)) ||
3867 !isa<SCEVConstant>(IdxExpr->getOperand(1)))
Dan Gohman1c343752009-06-27 21:21:31 +00003868 return getCouldNotCompute();
Chris Lattner673e02b2004-10-12 01:49:27 +00003869
3870 unsigned MaxSteps = MaxBruteForceIterations;
3871 for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
Owen Andersoneed707b2009-07-24 23:12:02 +00003872 ConstantInt *ItCst = ConstantInt::get(
Owen Anderson9adc0ab2009-07-14 23:09:55 +00003873 cast<IntegerType>(IdxExpr->getType()), IterationNum);
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003874 ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this);
Chris Lattner673e02b2004-10-12 01:49:27 +00003875
3876 // Form the GEP offset.
3877 Indexes[VarIdxNum] = Val;
3878
Nick Lewyckyc6501b12009-11-23 03:26:09 +00003879 Constant *Result = GetAddressedElementFromGlobal(GV, Indexes);
Chris Lattner673e02b2004-10-12 01:49:27 +00003880 if (Result == 0) break; // Cannot compute!
3881
3882 // Evaluate the condition for this iteration.
Reid Spencere4d87aa2006-12-23 06:05:41 +00003883 Result = ConstantExpr::getICmp(predicate, Result, RHS);
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00003884 if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure
Reid Spencere8019bb2007-03-01 07:25:48 +00003885 if (cast<ConstantInt>(Result)->getValue().isMinValue()) {
Chris Lattner673e02b2004-10-12 01:49:27 +00003886#if 0
David Greene25e0e872009-12-23 22:18:14 +00003887 dbgs() << "\n***\n*** Computed loop count " << *ItCst
Dan Gohmanb7ef7292009-04-21 00:47:46 +00003888 << "\n*** From global " << *GV << "*** BB: " << *L->getHeader()
3889 << "***\n";
Chris Lattner673e02b2004-10-12 01:49:27 +00003890#endif
3891 ++NumArrayLenItCounts;
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003892 return getConstant(ItCst); // Found terminating iteration!
Chris Lattner673e02b2004-10-12 01:49:27 +00003893 }
3894 }
Dan Gohman1c343752009-06-27 21:21:31 +00003895 return getCouldNotCompute();
Chris Lattner673e02b2004-10-12 01:49:27 +00003896}
3897
3898
Chris Lattner3221ad02004-04-17 22:58:41 +00003899/// CanConstantFold - Return true if we can constant fold an instruction of the
3900/// specified type, assuming that all operands were constants.
3901static bool CanConstantFold(const Instruction *I) {
Reid Spencer832254e2007-02-02 02:16:23 +00003902 if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
Chris Lattner3221ad02004-04-17 22:58:41 +00003903 isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I))
3904 return true;
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003905
Chris Lattner3221ad02004-04-17 22:58:41 +00003906 if (const CallInst *CI = dyn_cast<CallInst>(I))
3907 if (const Function *F = CI->getCalledFunction())
Dan Gohmanfa9b80e2008-01-31 01:05:10 +00003908 return canConstantFoldCallTo(F);
Chris Lattner3221ad02004-04-17 22:58:41 +00003909 return false;
Chris Lattner7980fb92004-04-17 18:36:24 +00003910}
3911
Chris Lattner3221ad02004-04-17 22:58:41 +00003912/// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node
3913/// in the loop that V is derived from. We allow arbitrary operations along the
3914/// way, but the operands of an operation must either be constants or a value
3915/// derived from a constant PHI. If this expression does not fit with these
3916/// constraints, return null.
3917static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) {
3918 // If this is not an instruction, or if this is an instruction outside of the
3919 // loop, it can't be derived from a loop PHI.
3920 Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman92329c72009-12-18 01:24:09 +00003921 if (I == 0 || !L->contains(I)) return 0;
Chris Lattner3221ad02004-04-17 22:58:41 +00003922
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +00003923 if (PHINode *PN = dyn_cast<PHINode>(I)) {
Chris Lattner3221ad02004-04-17 22:58:41 +00003924 if (L->getHeader() == I->getParent())
3925 return PN;
3926 else
3927 // We don't currently keep track of the control flow needed to evaluate
3928 // PHIs, so we cannot handle PHIs inside of loops.
3929 return 0;
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +00003930 }
Chris Lattner3221ad02004-04-17 22:58:41 +00003931
3932 // If we won't be able to constant fold this expression even if the operands
3933 // are constants, return early.
3934 if (!CanConstantFold(I)) return 0;
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003935
Chris Lattner3221ad02004-04-17 22:58:41 +00003936 // Otherwise, we can evaluate this instruction if all of its operands are
3937 // constant or derived from a PHI node themselves.
3938 PHINode *PHI = 0;
3939 for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op)
3940 if (!(isa<Constant>(I->getOperand(Op)) ||
3941 isa<GlobalValue>(I->getOperand(Op)))) {
3942 PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L);
3943 if (P == 0) return 0; // Not evolving from PHI
3944 if (PHI == 0)
3945 PHI = P;
3946 else if (PHI != P)
3947 return 0; // Evolving from multiple different PHIs.
3948 }
3949
3950 // This is a expression evolving from a constant PHI!
3951 return PHI;
3952}
3953
3954/// EvaluateExpression - Given an expression that passes the
3955/// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node
3956/// in the loop has the value PHIVal. If we can't fold this expression for some
3957/// reason, return null.
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00003958static Constant *EvaluateExpression(Value *V, Constant *PHIVal,
3959 const TargetData *TD) {
Chris Lattner3221ad02004-04-17 22:58:41 +00003960 if (isa<PHINode>(V)) return PHIVal;
Reid Spencere8404342004-07-18 00:18:30 +00003961 if (Constant *C = dyn_cast<Constant>(V)) return C;
Dan Gohman2d1be872009-04-16 03:18:22 +00003962 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV;
Chris Lattner3221ad02004-04-17 22:58:41 +00003963 Instruction *I = cast<Instruction>(V);
3964
3965 std::vector<Constant*> Operands;
3966 Operands.resize(I->getNumOperands());
3967
3968 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00003969 Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal, TD);
Chris Lattner3221ad02004-04-17 22:58:41 +00003970 if (Operands[i] == 0) return 0;
3971 }
3972
Chris Lattnerf286f6f2007-12-10 22:53:04 +00003973 if (const CmpInst *CI = dyn_cast<CmpInst>(I))
Chris Lattner8f73dea2009-11-09 23:06:58 +00003974 return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0],
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00003975 Operands[1], TD);
Chris Lattner8f73dea2009-11-09 23:06:58 +00003976 return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00003977 &Operands[0], Operands.size(), TD);
Chris Lattner3221ad02004-04-17 22:58:41 +00003978}
3979
3980/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
3981/// in the header of its containing loop, we know the loop executes a
3982/// constant number of times, and the PHI node is just a recurrence
3983/// involving constants, fold it.
Dan Gohman64a845e2009-06-24 04:48:43 +00003984Constant *
3985ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN,
Dan Gohman5d984912009-12-18 01:14:11 +00003986 const APInt &BEs,
Dan Gohman64a845e2009-06-24 04:48:43 +00003987 const Loop *L) {
Chris Lattner3221ad02004-04-17 22:58:41 +00003988 std::map<PHINode*, Constant*>::iterator I =
3989 ConstantEvolutionLoopExitValue.find(PN);
3990 if (I != ConstantEvolutionLoopExitValue.end())
3991 return I->second;
3992
Dan Gohman46bdfb02009-02-24 18:55:53 +00003993 if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations)))
Chris Lattner3221ad02004-04-17 22:58:41 +00003994 return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it.
3995
3996 Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
3997
3998 // Since the loop is canonicalized, the PHI node must have two entries. One
3999 // entry must be a constant (coming in from outside of the loop), and the
4000 // second must be derived from the same PHI.
4001 bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
4002 Constant *StartCST =
4003 dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge));
4004 if (StartCST == 0)
4005 return RetVal = 0; // Must be a constant.
4006
4007 Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
4008 PHINode *PN2 = getConstantEvolvingPHI(BEValue, L);
4009 if (PN2 != PN)
4010 return RetVal = 0; // Not derived from same PHI.
4011
4012 // Execute the loop symbolically to determine the exit value.
Dan Gohman46bdfb02009-02-24 18:55:53 +00004013 if (BEs.getActiveBits() >= 32)
Reid Spencere8019bb2007-03-01 07:25:48 +00004014 return RetVal = 0; // More than 2^32-1 iterations?? Not doing it!
Chris Lattner3221ad02004-04-17 22:58:41 +00004015
Dan Gohman46bdfb02009-02-24 18:55:53 +00004016 unsigned NumIterations = BEs.getZExtValue(); // must be in range
Reid Spencere8019bb2007-03-01 07:25:48 +00004017 unsigned IterationNum = 0;
Chris Lattner3221ad02004-04-17 22:58:41 +00004018 for (Constant *PHIVal = StartCST; ; ++IterationNum) {
4019 if (IterationNum == NumIterations)
4020 return RetVal = PHIVal; // Got exit value!
4021
4022 // Compute the value of the PHI node for the next iteration.
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00004023 Constant *NextPHI = EvaluateExpression(BEValue, PHIVal, TD);
Chris Lattner3221ad02004-04-17 22:58:41 +00004024 if (NextPHI == PHIVal)
4025 return RetVal = NextPHI; // Stopped evolving!
4026 if (NextPHI == 0)
4027 return 0; // Couldn't evaluate!
4028 PHIVal = NextPHI;
4029 }
4030}
4031
Dan Gohman07ad19b2009-07-27 16:09:48 +00004032/// ComputeBackedgeTakenCountExhaustively - If the loop is known to execute a
Chris Lattner7980fb92004-04-17 18:36:24 +00004033/// constant number of times (the condition evolves only from constants),
4034/// try to evaluate a few iterations of the loop until we get the exit
4035/// condition gets a value of ExitWhen (true or false). If we cannot
Dan Gohman1c343752009-06-27 21:21:31 +00004036/// evaluate the trip count of the loop, return getCouldNotCompute().
Dan Gohman64a845e2009-06-24 04:48:43 +00004037const SCEV *
4038ScalarEvolution::ComputeBackedgeTakenCountExhaustively(const Loop *L,
4039 Value *Cond,
4040 bool ExitWhen) {
Chris Lattner7980fb92004-04-17 18:36:24 +00004041 PHINode *PN = getConstantEvolvingPHI(Cond, L);
Dan Gohman1c343752009-06-27 21:21:31 +00004042 if (PN == 0) return getCouldNotCompute();
Chris Lattner7980fb92004-04-17 18:36:24 +00004043
4044 // Since the loop is canonicalized, the PHI node must have two entries. One
4045 // entry must be a constant (coming in from outside of the loop), and the
4046 // second must be derived from the same PHI.
4047 bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
4048 Constant *StartCST =
4049 dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge));
Dan Gohman1c343752009-06-27 21:21:31 +00004050 if (StartCST == 0) return getCouldNotCompute(); // Must be a constant.
Chris Lattner7980fb92004-04-17 18:36:24 +00004051
4052 Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
4053 PHINode *PN2 = getConstantEvolvingPHI(BEValue, L);
Dan Gohman1c343752009-06-27 21:21:31 +00004054 if (PN2 != PN) return getCouldNotCompute(); // Not derived from same PHI.
Chris Lattner7980fb92004-04-17 18:36:24 +00004055
4056 // Okay, we find a PHI node that defines the trip count of this loop. Execute
4057 // the loop symbolically to determine when the condition gets a value of
4058 // "ExitWhen".
4059 unsigned IterationNum = 0;
4060 unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis.
4061 for (Constant *PHIVal = StartCST;
4062 IterationNum != MaxIterations; ++IterationNum) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00004063 ConstantInt *CondVal =
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00004064 dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal, TD));
Chris Lattner3221ad02004-04-17 22:58:41 +00004065
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00004066 // Couldn't symbolically evaluate.
Dan Gohman1c343752009-06-27 21:21:31 +00004067 if (!CondVal) return getCouldNotCompute();
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00004068
Reid Spencere8019bb2007-03-01 07:25:48 +00004069 if (CondVal->getValue() == uint64_t(ExitWhen)) {
Chris Lattner7980fb92004-04-17 18:36:24 +00004070 ++NumBruteForceTripCountsComputed;
Owen Anderson1d0be152009-08-13 21:58:54 +00004071 return getConstant(Type::getInt32Ty(getContext()), IterationNum);
Chris Lattner7980fb92004-04-17 18:36:24 +00004072 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004073
Chris Lattner3221ad02004-04-17 22:58:41 +00004074 // Compute the value of the PHI node for the next iteration.
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00004075 Constant *NextPHI = EvaluateExpression(BEValue, PHIVal, TD);
Chris Lattner3221ad02004-04-17 22:58:41 +00004076 if (NextPHI == 0 || NextPHI == PHIVal)
Dan Gohman1c343752009-06-27 21:21:31 +00004077 return getCouldNotCompute();// Couldn't evaluate or not making progress...
Chris Lattner3221ad02004-04-17 22:58:41 +00004078 PHIVal = NextPHI;
Chris Lattner7980fb92004-04-17 18:36:24 +00004079 }
4080
4081 // Too many iterations were needed to evaluate.
Dan Gohman1c343752009-06-27 21:21:31 +00004082 return getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00004083}
4084
Dan Gohmane7125f42009-09-03 15:00:26 +00004085/// getSCEVAtScope - Return a SCEV expression for the specified value
Dan Gohman66a7e852009-05-08 20:38:54 +00004086/// at the specified scope in the program. The L value specifies a loop
4087/// nest to evaluate the expression at, where null is the top-level or a
4088/// specified loop is immediately inside of the loop.
4089///
4090/// This method can be used to compute the exit value for a variable defined
4091/// in a loop by querying what the value will hold in the parent loop.
4092///
Dan Gohmand594e6f2009-05-24 23:25:42 +00004093/// In the case that a relevant loop exit value cannot be computed, the
4094/// original value V is returned.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004095const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) {
Dan Gohman42214892009-08-31 21:15:23 +00004096 // Check to see if we've folded this expression at this loop before.
4097 std::map<const Loop *, const SCEV *> &Values = ValuesAtScopes[V];
4098 std::pair<std::map<const Loop *, const SCEV *>::iterator, bool> Pair =
4099 Values.insert(std::make_pair(L, static_cast<const SCEV *>(0)));
4100 if (!Pair.second)
4101 return Pair.first->second ? Pair.first->second : V;
Chris Lattner53e677a2004-04-02 20:23:17 +00004102
Dan Gohman42214892009-08-31 21:15:23 +00004103 // Otherwise compute it.
4104 const SCEV *C = computeSCEVAtScope(V, L);
Dan Gohmana5505cb2009-08-31 21:58:28 +00004105 ValuesAtScopes[V][L] = C;
Dan Gohman42214892009-08-31 21:15:23 +00004106 return C;
4107}
4108
4109const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
Chris Lattner3221ad02004-04-17 22:58:41 +00004110 if (isa<SCEVConstant>(V)) return V;
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004111
Nick Lewycky3e630762008-02-20 06:48:22 +00004112 // If this instruction is evolved from a constant-evolving PHI, compute the
Chris Lattner3221ad02004-04-17 22:58:41 +00004113 // exit value from the loop without using SCEVs.
Dan Gohman622ed672009-05-04 22:02:23 +00004114 if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) {
Chris Lattner3221ad02004-04-17 22:58:41 +00004115 if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004116 const Loop *LI = (*this->LI)[I->getParent()];
Chris Lattner3221ad02004-04-17 22:58:41 +00004117 if (LI && LI->getParentLoop() == L) // Looking for loop exit value.
4118 if (PHINode *PN = dyn_cast<PHINode>(I))
4119 if (PN->getParent() == LI->getHeader()) {
4120 // Okay, there is no closed form solution for the PHI node. Check
Dan Gohman46bdfb02009-02-24 18:55:53 +00004121 // to see if the loop that contains it has a known backedge-taken
4122 // count. If so, we may be able to force computation of the exit
4123 // value.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004124 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI);
Dan Gohman622ed672009-05-04 22:02:23 +00004125 if (const SCEVConstant *BTCC =
Dan Gohman46bdfb02009-02-24 18:55:53 +00004126 dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
Chris Lattner3221ad02004-04-17 22:58:41 +00004127 // Okay, we know how many times the containing loop executes. If
4128 // this is a constant evolving PHI node, get the final value at
4129 // the specified iteration number.
4130 Constant *RV = getConstantEvolutionLoopExitValue(PN,
Dan Gohman46bdfb02009-02-24 18:55:53 +00004131 BTCC->getValue()->getValue(),
Chris Lattner3221ad02004-04-17 22:58:41 +00004132 LI);
Dan Gohman09987962009-06-29 21:31:18 +00004133 if (RV) return getSCEV(RV);
Chris Lattner3221ad02004-04-17 22:58:41 +00004134 }
4135 }
4136
Reid Spencer09906f32006-12-04 21:33:23 +00004137 // Okay, this is an expression that we cannot symbolically evaluate
Chris Lattner3221ad02004-04-17 22:58:41 +00004138 // into a SCEV. Check to see if it's possible to symbolically evaluate
Reid Spencer09906f32006-12-04 21:33:23 +00004139 // the arguments into constants, and if so, try to constant propagate the
Chris Lattner3221ad02004-04-17 22:58:41 +00004140 // result. This is particularly useful for computing loop exit values.
4141 if (CanConstantFold(I)) {
4142 std::vector<Constant*> Operands;
4143 Operands.reserve(I->getNumOperands());
4144 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
4145 Value *Op = I->getOperand(i);
4146 if (Constant *C = dyn_cast<Constant>(Op)) {
4147 Operands.push_back(C);
Chris Lattner3221ad02004-04-17 22:58:41 +00004148 } else {
Chris Lattner42b5e082007-11-23 08:46:22 +00004149 // If any of the operands is non-constant and if they are
Dan Gohman2d1be872009-04-16 03:18:22 +00004150 // non-integer and non-pointer, don't even try to analyze them
4151 // with scev techniques.
Dan Gohman4acd12a2009-04-30 16:40:30 +00004152 if (!isSCEVable(Op->getType()))
Chris Lattner42b5e082007-11-23 08:46:22 +00004153 return V;
Dan Gohman2d1be872009-04-16 03:18:22 +00004154
Dan Gohman5d984912009-12-18 01:14:11 +00004155 const SCEV *OpV = getSCEVAtScope(Op, L);
Dan Gohman622ed672009-05-04 22:02:23 +00004156 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) {
Dan Gohman4acd12a2009-04-30 16:40:30 +00004157 Constant *C = SC->getValue();
4158 if (C->getType() != Op->getType())
4159 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
4160 Op->getType(),
4161 false),
4162 C, Op->getType());
4163 Operands.push_back(C);
Dan Gohman622ed672009-05-04 22:02:23 +00004164 } else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) {
Dan Gohman4acd12a2009-04-30 16:40:30 +00004165 if (Constant *C = dyn_cast<Constant>(SU->getValue())) {
4166 if (C->getType() != Op->getType())
4167 C =
4168 ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
4169 Op->getType(),
4170 false),
4171 C, Op->getType());
4172 Operands.push_back(C);
4173 } else
Chris Lattner3221ad02004-04-17 22:58:41 +00004174 return V;
4175 } else {
4176 return V;
4177 }
4178 }
4179 }
Dan Gohman64a845e2009-06-24 04:48:43 +00004180
Chris Lattnerf286f6f2007-12-10 22:53:04 +00004181 Constant *C;
4182 if (const CmpInst *CI = dyn_cast<CmpInst>(I))
4183 C = ConstantFoldCompareInstOperands(CI->getPredicate(),
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00004184 Operands[0], Operands[1], TD);
Chris Lattnerf286f6f2007-12-10 22:53:04 +00004185 else
4186 C = ConstantFoldInstOperands(I->getOpcode(), I->getType(),
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00004187 &Operands[0], Operands.size(), TD);
Dan Gohman09987962009-06-29 21:31:18 +00004188 return getSCEV(C);
Chris Lattner3221ad02004-04-17 22:58:41 +00004189 }
4190 }
4191
4192 // This is some other type of SCEVUnknown, just return it.
4193 return V;
4194 }
4195
Dan Gohman622ed672009-05-04 22:02:23 +00004196 if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004197 // Avoid performing the look-up in the common case where the specified
4198 // expression has no loop-variant portions.
4199 for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00004200 const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
Chris Lattner53e677a2004-04-02 20:23:17 +00004201 if (OpAtScope != Comm->getOperand(i)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004202 // Okay, at least one of these operands is loop variant but might be
4203 // foldable. Build a new instance of the folded commutative expression.
Dan Gohman64a845e2009-06-24 04:48:43 +00004204 SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(),
4205 Comm->op_begin()+i);
Chris Lattner53e677a2004-04-02 20:23:17 +00004206 NewOps.push_back(OpAtScope);
4207
4208 for (++i; i != e; ++i) {
4209 OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
Chris Lattner53e677a2004-04-02 20:23:17 +00004210 NewOps.push_back(OpAtScope);
4211 }
4212 if (isa<SCEVAddExpr>(Comm))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004213 return getAddExpr(NewOps);
Nick Lewyckyc54c5612007-11-25 22:41:31 +00004214 if (isa<SCEVMulExpr>(Comm))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004215 return getMulExpr(NewOps);
Nick Lewyckyc54c5612007-11-25 22:41:31 +00004216 if (isa<SCEVSMaxExpr>(Comm))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004217 return getSMaxExpr(NewOps);
Nick Lewycky3e630762008-02-20 06:48:22 +00004218 if (isa<SCEVUMaxExpr>(Comm))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004219 return getUMaxExpr(NewOps);
Torok Edwinc23197a2009-07-14 16:55:14 +00004220 llvm_unreachable("Unknown commutative SCEV type!");
Chris Lattner53e677a2004-04-02 20:23:17 +00004221 }
4222 }
4223 // If we got here, all operands are loop invariant.
4224 return Comm;
4225 }
4226
Dan Gohman622ed672009-05-04 22:02:23 +00004227 if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00004228 const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L);
4229 const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L);
Nick Lewycky789558d2009-01-13 09:18:58 +00004230 if (LHS == Div->getLHS() && RHS == Div->getRHS())
4231 return Div; // must be loop invariant
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004232 return getUDivExpr(LHS, RHS);
Chris Lattner53e677a2004-04-02 20:23:17 +00004233 }
4234
4235 // If this is a loop recurrence for a loop that does not contain L, then we
4236 // are dealing with the final value computed by the loop.
Dan Gohman622ed672009-05-04 22:02:23 +00004237 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
Dan Gohman92329c72009-12-18 01:24:09 +00004238 if (!L || !AddRec->getLoop()->contains(L)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004239 // To evaluate this recurrence, we need to know how many times the AddRec
4240 // loop iterates. Compute this now.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004241 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop());
Dan Gohman1c343752009-06-27 21:21:31 +00004242 if (BackedgeTakenCount == getCouldNotCompute()) return AddRec;
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004243
Eli Friedmanb42a6262008-08-04 23:49:06 +00004244 // Then, evaluate the AddRec.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004245 return AddRec->evaluateAtIteration(BackedgeTakenCount, *this);
Chris Lattner53e677a2004-04-02 20:23:17 +00004246 }
Dan Gohmand594e6f2009-05-24 23:25:42 +00004247 return AddRec;
Chris Lattner53e677a2004-04-02 20:23:17 +00004248 }
4249
Dan Gohman622ed672009-05-04 22:02:23 +00004250 if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00004251 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohmaneb3948b2009-04-29 22:29:01 +00004252 if (Op == Cast->getOperand())
4253 return Cast; // must be loop invariant
4254 return getZeroExtendExpr(Op, Cast->getType());
4255 }
4256
Dan Gohman622ed672009-05-04 22:02:23 +00004257 if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00004258 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohmaneb3948b2009-04-29 22:29:01 +00004259 if (Op == Cast->getOperand())
4260 return Cast; // must be loop invariant
4261 return getSignExtendExpr(Op, Cast->getType());
4262 }
4263
Dan Gohman622ed672009-05-04 22:02:23 +00004264 if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00004265 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohmaneb3948b2009-04-29 22:29:01 +00004266 if (Op == Cast->getOperand())
4267 return Cast; // must be loop invariant
4268 return getTruncateExpr(Op, Cast->getType());
4269 }
4270
Torok Edwinc23197a2009-07-14 16:55:14 +00004271 llvm_unreachable("Unknown SCEV type!");
Daniel Dunbar8c562e22009-05-18 16:43:04 +00004272 return 0;
Chris Lattner53e677a2004-04-02 20:23:17 +00004273}
4274
Dan Gohman66a7e852009-05-08 20:38:54 +00004275/// getSCEVAtScope - This is a convenience function which does
4276/// getSCEVAtScope(getSCEV(V), L).
Dan Gohman0bba49c2009-07-07 17:06:11 +00004277const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004278 return getSCEVAtScope(getSCEV(V), L);
4279}
4280
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004281/// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the
4282/// following equation:
4283///
4284/// A * X = B (mod N)
4285///
4286/// where N = 2^BW and BW is the common bit width of A and B. The signedness of
4287/// A and B isn't important.
4288///
4289/// If the equation does not have a solution, SCEVCouldNotCompute is returned.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004290static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const APInt &B,
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004291 ScalarEvolution &SE) {
4292 uint32_t BW = A.getBitWidth();
4293 assert(BW == B.getBitWidth() && "Bit widths must be the same.");
4294 assert(A != 0 && "A must be non-zero.");
4295
4296 // 1. D = gcd(A, N)
4297 //
4298 // The gcd of A and N may have only one prime factor: 2. The number of
4299 // trailing zeros in A is its multiplicity
4300 uint32_t Mult2 = A.countTrailingZeros();
4301 // D = 2^Mult2
4302
4303 // 2. Check if B is divisible by D.
4304 //
4305 // B is divisible by D if and only if the multiplicity of prime factor 2 for B
4306 // is not less than multiplicity of this prime factor for D.
4307 if (B.countTrailingZeros() < Mult2)
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00004308 return SE.getCouldNotCompute();
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004309
4310 // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic
4311 // modulo (N / D).
4312 //
4313 // (N / D) may need BW+1 bits in its representation. Hence, we'll use this
4314 // bit width during computations.
4315 APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D
4316 APInt Mod(BW + 1, 0);
4317 Mod.set(BW - Mult2); // Mod = N / D
4318 APInt I = AD.multiplicativeInverse(Mod);
4319
4320 // 4. Compute the minimum unsigned root of the equation:
4321 // I * (B / D) mod (N / D)
4322 APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod);
4323
4324 // The result is guaranteed to be less than 2^BW so we may truncate it to BW
4325 // bits.
4326 return SE.getConstant(Result.trunc(BW));
4327}
Chris Lattner53e677a2004-04-02 20:23:17 +00004328
4329/// SolveQuadraticEquation - Find the roots of the quadratic equation for the
4330/// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which
4331/// might be the same) or two SCEVCouldNotCompute objects.
4332///
Dan Gohman0bba49c2009-07-07 17:06:11 +00004333static std::pair<const SCEV *,const SCEV *>
Dan Gohman246b2562007-10-22 18:31:58 +00004334SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004335 assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
Dan Gohman35738ac2009-05-04 22:30:44 +00004336 const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
4337 const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
4338 const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004339
Chris Lattner53e677a2004-04-02 20:23:17 +00004340 // We currently can only solve this if the coefficients are constants.
Reid Spencere8019bb2007-03-01 07:25:48 +00004341 if (!LC || !MC || !NC) {
Dan Gohman35738ac2009-05-04 22:30:44 +00004342 const SCEV *CNC = SE.getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00004343 return std::make_pair(CNC, CNC);
4344 }
4345
Reid Spencere8019bb2007-03-01 07:25:48 +00004346 uint32_t BitWidth = LC->getValue()->getValue().getBitWidth();
Chris Lattnerfe560b82007-04-15 19:52:49 +00004347 const APInt &L = LC->getValue()->getValue();
4348 const APInt &M = MC->getValue()->getValue();
4349 const APInt &N = NC->getValue()->getValue();
Reid Spencere8019bb2007-03-01 07:25:48 +00004350 APInt Two(BitWidth, 2);
4351 APInt Four(BitWidth, 4);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004352
Dan Gohman64a845e2009-06-24 04:48:43 +00004353 {
Reid Spencere8019bb2007-03-01 07:25:48 +00004354 using namespace APIntOps;
Zhou Sheng414de4d2007-04-07 17:48:27 +00004355 const APInt& C = L;
Reid Spencere8019bb2007-03-01 07:25:48 +00004356 // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C
4357 // The B coefficient is M-N/2
4358 APInt B(M);
4359 B -= sdiv(N,Two);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004360
Reid Spencere8019bb2007-03-01 07:25:48 +00004361 // The A coefficient is N/2
Zhou Sheng414de4d2007-04-07 17:48:27 +00004362 APInt A(N.sdiv(Two));
Chris Lattner53e677a2004-04-02 20:23:17 +00004363
Reid Spencere8019bb2007-03-01 07:25:48 +00004364 // Compute the B^2-4ac term.
4365 APInt SqrtTerm(B);
4366 SqrtTerm *= B;
4367 SqrtTerm -= Four * (A * C);
Chris Lattner53e677a2004-04-02 20:23:17 +00004368
Reid Spencere8019bb2007-03-01 07:25:48 +00004369 // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest
4370 // integer value or else APInt::sqrt() will assert.
4371 APInt SqrtVal(SqrtTerm.sqrt());
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004372
Dan Gohman64a845e2009-06-24 04:48:43 +00004373 // Compute the two solutions for the quadratic formula.
Reid Spencere8019bb2007-03-01 07:25:48 +00004374 // The divisions must be performed as signed divisions.
4375 APInt NegB(-B);
Reid Spencer3e35c8d2007-04-16 02:24:41 +00004376 APInt TwoA( A << 1 );
Nick Lewycky8f4d5eb2008-11-03 02:43:49 +00004377 if (TwoA.isMinValue()) {
Dan Gohman35738ac2009-05-04 22:30:44 +00004378 const SCEV *CNC = SE.getCouldNotCompute();
Nick Lewycky8f4d5eb2008-11-03 02:43:49 +00004379 return std::make_pair(CNC, CNC);
4380 }
4381
Owen Andersone922c022009-07-22 00:24:57 +00004382 LLVMContext &Context = SE.getContext();
Owen Anderson76f600b2009-07-06 22:37:39 +00004383
4384 ConstantInt *Solution1 =
Owen Andersoneed707b2009-07-24 23:12:02 +00004385 ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA));
Owen Anderson76f600b2009-07-06 22:37:39 +00004386 ConstantInt *Solution2 =
Owen Andersoneed707b2009-07-24 23:12:02 +00004387 ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA));
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004388
Dan Gohman64a845e2009-06-24 04:48:43 +00004389 return std::make_pair(SE.getConstant(Solution1),
Dan Gohman246b2562007-10-22 18:31:58 +00004390 SE.getConstant(Solution2));
Reid Spencere8019bb2007-03-01 07:25:48 +00004391 } // end APIntOps namespace
Chris Lattner53e677a2004-04-02 20:23:17 +00004392}
4393
4394/// HowFarToZero - Return the number of times a backedge comparing the specified
Dan Gohman86fbf2f2009-06-06 14:37:11 +00004395/// value to zero will execute. If not computable, return CouldNotCompute.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004396const SCEV *ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004397 // If the value is a constant
Dan Gohman622ed672009-05-04 22:02:23 +00004398 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004399 // If the value is already zero, the branch will execute zero times.
Reid Spencercae57542007-03-02 00:28:52 +00004400 if (C->getValue()->isZero()) return C;
Dan Gohman1c343752009-06-27 21:21:31 +00004401 return getCouldNotCompute(); // Otherwise it will loop infinitely.
Chris Lattner53e677a2004-04-02 20:23:17 +00004402 }
4403
Dan Gohman35738ac2009-05-04 22:30:44 +00004404 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V);
Chris Lattner53e677a2004-04-02 20:23:17 +00004405 if (!AddRec || AddRec->getLoop() != L)
Dan Gohman1c343752009-06-27 21:21:31 +00004406 return getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00004407
4408 if (AddRec->isAffine()) {
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004409 // If this is an affine expression, the execution count of this branch is
4410 // the minimum unsigned root of the following equation:
Chris Lattner53e677a2004-04-02 20:23:17 +00004411 //
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004412 // Start + Step*N = 0 (mod 2^BW)
Chris Lattner53e677a2004-04-02 20:23:17 +00004413 //
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004414 // equivalent to:
4415 //
4416 // Step*N = -Start (mod 2^BW)
4417 //
4418 // where BW is the common bit width of Start and Step.
4419
Chris Lattner53e677a2004-04-02 20:23:17 +00004420 // Get the initial value for the loop.
Dan Gohman64a845e2009-06-24 04:48:43 +00004421 const SCEV *Start = getSCEVAtScope(AddRec->getStart(),
4422 L->getParentLoop());
4423 const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1),
4424 L->getParentLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +00004425
Dan Gohman622ed672009-05-04 22:02:23 +00004426 if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) {
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004427 // For now we handle only constant steps.
Chris Lattner53e677a2004-04-02 20:23:17 +00004428
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004429 // First, handle unitary steps.
4430 if (StepC->getValue()->equalsInt(1)) // 1*N = -Start (mod 2^BW), so:
Dan Gohman4c0d5d52009-08-20 16:42:55 +00004431 return getNegativeSCEV(Start); // N = -Start (as unsigned)
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004432 if (StepC->getValue()->isAllOnesValue()) // -1*N = -Start (mod 2^BW), so:
4433 return Start; // N = Start (as unsigned)
4434
4435 // Then, try to solve the above equation provided that Start is constant.
Dan Gohman622ed672009-05-04 22:02:23 +00004436 if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start))
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004437 return SolveLinEquationWithOverflow(StepC->getValue()->getValue(),
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004438 -StartC->getValue()->getValue(),
4439 *this);
Chris Lattner53e677a2004-04-02 20:23:17 +00004440 }
Chris Lattner42a75512007-01-15 02:27:26 +00004441 } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004442 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
4443 // the quadratic equation to solve it.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004444 std::pair<const SCEV *,const SCEV *> Roots = SolveQuadraticEquation(AddRec,
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004445 *this);
Dan Gohman35738ac2009-05-04 22:30:44 +00004446 const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
4447 const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
Chris Lattner53e677a2004-04-02 20:23:17 +00004448 if (R1) {
Chris Lattnerd18d9dc2004-04-02 20:26:46 +00004449#if 0
David Greene25e0e872009-12-23 22:18:14 +00004450 dbgs() << "HFTZ: " << *V << " - sol#1: " << *R1
Dan Gohmanb7ef7292009-04-21 00:47:46 +00004451 << " sol#2: " << *R2 << "\n";
Chris Lattnerd18d9dc2004-04-02 20:26:46 +00004452#endif
Chris Lattner53e677a2004-04-02 20:23:17 +00004453 // Pick the smallest positive root value.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00004454 if (ConstantInt *CB =
Owen Andersonbaf3c402009-07-29 18:55:55 +00004455 dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT,
Reid Spencere4d87aa2006-12-23 06:05:41 +00004456 R1->getValue(), R2->getValue()))) {
Reid Spencer579dca12007-01-12 04:24:46 +00004457 if (CB->getZExtValue() == false)
Chris Lattner53e677a2004-04-02 20:23:17 +00004458 std::swap(R1, R2); // R1 is the minimum root now.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004459
Chris Lattner53e677a2004-04-02 20:23:17 +00004460 // We can only use this value if the chrec ends up with an exact zero
4461 // value at this index. When solving for "X*X != 5", for example, we
4462 // should not accept a root of 2.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004463 const SCEV *Val = AddRec->evaluateAtIteration(R1, *this);
Dan Gohmancfeb6a42008-06-18 16:23:07 +00004464 if (Val->isZero())
4465 return R1; // We found a quadratic root!
Chris Lattner53e677a2004-04-02 20:23:17 +00004466 }
4467 }
4468 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004469
Dan Gohman1c343752009-06-27 21:21:31 +00004470 return getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00004471}
4472
4473/// HowFarToNonZero - Return the number of times a backedge checking the
4474/// specified value for nonzero will execute. If not computable, return
Dan Gohman86fbf2f2009-06-06 14:37:11 +00004475/// CouldNotCompute
Dan Gohman0bba49c2009-07-07 17:06:11 +00004476const SCEV *ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004477 // Loops that look like: while (X == 0) are very strange indeed. We don't
4478 // handle them yet except for the trivial case. This could be expanded in the
4479 // future as needed.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004480
Chris Lattner53e677a2004-04-02 20:23:17 +00004481 // If the value is a constant, check to see if it is known to be non-zero
4482 // already. If so, the backedge will execute zero times.
Dan Gohman622ed672009-05-04 22:02:23 +00004483 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
Nick Lewycky39442af2008-02-21 09:14:53 +00004484 if (!C->getValue()->isNullValue())
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004485 return getIntegerSCEV(0, C->getType());
Dan Gohman1c343752009-06-27 21:21:31 +00004486 return getCouldNotCompute(); // Otherwise it will loop infinitely.
Chris Lattner53e677a2004-04-02 20:23:17 +00004487 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004488
Chris Lattner53e677a2004-04-02 20:23:17 +00004489 // We could implement others, but I really doubt anyone writes loops like
4490 // this, and if they did, they would already be constant folded.
Dan Gohman1c343752009-06-27 21:21:31 +00004491 return getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00004492}
4493
Dan Gohman859b4822009-05-18 15:36:09 +00004494/// getLoopPredecessor - If the given loop's header has exactly one unique
4495/// predecessor outside the loop, return it. Otherwise return null.
4496///
4497BasicBlock *ScalarEvolution::getLoopPredecessor(const Loop *L) {
4498 BasicBlock *Header = L->getHeader();
4499 BasicBlock *Pred = 0;
4500 for (pred_iterator PI = pred_begin(Header), E = pred_end(Header);
4501 PI != E; ++PI)
4502 if (!L->contains(*PI)) {
4503 if (Pred && Pred != *PI) return 0; // Multiple predecessors.
4504 Pred = *PI;
4505 }
4506 return Pred;
4507}
4508
Dan Gohmanfd6edef2008-09-15 22:18:04 +00004509/// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
4510/// (which may not be an immediate predecessor) which has exactly one
4511/// successor from which BB is reachable, or null if no such block is
4512/// found.
4513///
4514BasicBlock *
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004515ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
Dan Gohman3d739fe2009-04-30 20:48:53 +00004516 // If the block has a unique predecessor, then there is no path from the
4517 // predecessor to the block that does not go through the direct edge
4518 // from the predecessor to the block.
Dan Gohmanfd6edef2008-09-15 22:18:04 +00004519 if (BasicBlock *Pred = BB->getSinglePredecessor())
4520 return Pred;
4521
4522 // A loop's header is defined to be a block that dominates the loop.
Dan Gohman859b4822009-05-18 15:36:09 +00004523 // If the header has a unique predecessor outside the loop, it must be
4524 // a block that has exactly one successor that can reach the loop.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004525 if (Loop *L = LI->getLoopFor(BB))
Dan Gohman859b4822009-05-18 15:36:09 +00004526 return getLoopPredecessor(L);
Dan Gohmanfd6edef2008-09-15 22:18:04 +00004527
4528 return 0;
4529}
4530
Dan Gohman763bad12009-06-20 00:35:32 +00004531/// HasSameValue - SCEV structural equivalence is usually sufficient for
4532/// testing whether two expressions are equal, however for the purposes of
4533/// looking for a condition guarding a loop, it can be useful to be a little
4534/// more general, since a front-end may have replicated the controlling
4535/// expression.
4536///
Dan Gohman0bba49c2009-07-07 17:06:11 +00004537static bool HasSameValue(const SCEV *A, const SCEV *B) {
Dan Gohman763bad12009-06-20 00:35:32 +00004538 // Quick check to see if they are the same SCEV.
4539 if (A == B) return true;
4540
4541 // Otherwise, if they're both SCEVUnknown, it's possible that they hold
4542 // two different instructions with the same value. Check for this case.
4543 if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A))
4544 if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B))
4545 if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue()))
4546 if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue()))
Dan Gohman041de422009-08-25 17:56:57 +00004547 if (AI->isIdenticalTo(BI) && !AI->mayReadFromMemory())
Dan Gohman763bad12009-06-20 00:35:32 +00004548 return true;
4549
4550 // Otherwise assume they may have a different value.
4551 return false;
4552}
4553
Dan Gohman85b05a22009-07-13 21:35:55 +00004554bool ScalarEvolution::isKnownNegative(const SCEV *S) {
4555 return getSignedRange(S).getSignedMax().isNegative();
4556}
4557
4558bool ScalarEvolution::isKnownPositive(const SCEV *S) {
4559 return getSignedRange(S).getSignedMin().isStrictlyPositive();
4560}
4561
4562bool ScalarEvolution::isKnownNonNegative(const SCEV *S) {
4563 return !getSignedRange(S).getSignedMin().isNegative();
4564}
4565
4566bool ScalarEvolution::isKnownNonPositive(const SCEV *S) {
4567 return !getSignedRange(S).getSignedMax().isStrictlyPositive();
4568}
4569
4570bool ScalarEvolution::isKnownNonZero(const SCEV *S) {
4571 return isKnownNegative(S) || isKnownPositive(S);
4572}
4573
4574bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred,
4575 const SCEV *LHS, const SCEV *RHS) {
4576
4577 if (HasSameValue(LHS, RHS))
4578 return ICmpInst::isTrueWhenEqual(Pred);
4579
4580 switch (Pred) {
4581 default:
Dan Gohman850f7912009-07-16 17:34:36 +00004582 llvm_unreachable("Unexpected ICmpInst::Predicate value!");
Dan Gohman85b05a22009-07-13 21:35:55 +00004583 break;
4584 case ICmpInst::ICMP_SGT:
4585 Pred = ICmpInst::ICMP_SLT;
4586 std::swap(LHS, RHS);
4587 case ICmpInst::ICMP_SLT: {
4588 ConstantRange LHSRange = getSignedRange(LHS);
4589 ConstantRange RHSRange = getSignedRange(RHS);
4590 if (LHSRange.getSignedMax().slt(RHSRange.getSignedMin()))
4591 return true;
4592 if (LHSRange.getSignedMin().sge(RHSRange.getSignedMax()))
4593 return false;
Dan Gohman85b05a22009-07-13 21:35:55 +00004594 break;
4595 }
4596 case ICmpInst::ICMP_SGE:
4597 Pred = ICmpInst::ICMP_SLE;
4598 std::swap(LHS, RHS);
4599 case ICmpInst::ICMP_SLE: {
4600 ConstantRange LHSRange = getSignedRange(LHS);
4601 ConstantRange RHSRange = getSignedRange(RHS);
4602 if (LHSRange.getSignedMax().sle(RHSRange.getSignedMin()))
4603 return true;
4604 if (LHSRange.getSignedMin().sgt(RHSRange.getSignedMax()))
4605 return false;
Dan Gohman85b05a22009-07-13 21:35:55 +00004606 break;
4607 }
4608 case ICmpInst::ICMP_UGT:
4609 Pred = ICmpInst::ICMP_ULT;
4610 std::swap(LHS, RHS);
4611 case ICmpInst::ICMP_ULT: {
4612 ConstantRange LHSRange = getUnsignedRange(LHS);
4613 ConstantRange RHSRange = getUnsignedRange(RHS);
4614 if (LHSRange.getUnsignedMax().ult(RHSRange.getUnsignedMin()))
4615 return true;
4616 if (LHSRange.getUnsignedMin().uge(RHSRange.getUnsignedMax()))
4617 return false;
Dan Gohman85b05a22009-07-13 21:35:55 +00004618 break;
4619 }
4620 case ICmpInst::ICMP_UGE:
4621 Pred = ICmpInst::ICMP_ULE;
4622 std::swap(LHS, RHS);
4623 case ICmpInst::ICMP_ULE: {
4624 ConstantRange LHSRange = getUnsignedRange(LHS);
4625 ConstantRange RHSRange = getUnsignedRange(RHS);
4626 if (LHSRange.getUnsignedMax().ule(RHSRange.getUnsignedMin()))
4627 return true;
4628 if (LHSRange.getUnsignedMin().ugt(RHSRange.getUnsignedMax()))
4629 return false;
Dan Gohman85b05a22009-07-13 21:35:55 +00004630 break;
4631 }
4632 case ICmpInst::ICMP_NE: {
4633 if (getUnsignedRange(LHS).intersectWith(getUnsignedRange(RHS)).isEmptySet())
4634 return true;
4635 if (getSignedRange(LHS).intersectWith(getSignedRange(RHS)).isEmptySet())
4636 return true;
4637
4638 const SCEV *Diff = getMinusSCEV(LHS, RHS);
4639 if (isKnownNonZero(Diff))
4640 return true;
4641 break;
4642 }
4643 case ICmpInst::ICMP_EQ:
Dan Gohmanf117ed42009-07-20 23:54:43 +00004644 // The check at the top of the function catches the case where
4645 // the values are known to be equal.
Dan Gohman85b05a22009-07-13 21:35:55 +00004646 break;
4647 }
4648 return false;
4649}
4650
4651/// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
4652/// protected by a conditional between LHS and RHS. This is used to
4653/// to eliminate casts.
4654bool
4655ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L,
4656 ICmpInst::Predicate Pred,
4657 const SCEV *LHS, const SCEV *RHS) {
4658 // Interpret a null as meaning no loop, where there is obviously no guard
4659 // (interprocedural conditions notwithstanding).
4660 if (!L) return true;
4661
4662 BasicBlock *Latch = L->getLoopLatch();
4663 if (!Latch)
4664 return false;
4665
4666 BranchInst *LoopContinuePredicate =
4667 dyn_cast<BranchInst>(Latch->getTerminator());
4668 if (!LoopContinuePredicate ||
4669 LoopContinuePredicate->isUnconditional())
4670 return false;
4671
Dan Gohman0f4b2852009-07-21 23:03:19 +00004672 return isImpliedCond(LoopContinuePredicate->getCondition(), Pred, LHS, RHS,
4673 LoopContinuePredicate->getSuccessor(0) != L->getHeader());
Dan Gohman85b05a22009-07-13 21:35:55 +00004674}
4675
4676/// isLoopGuardedByCond - Test whether entry to the loop is protected
4677/// by a conditional between LHS and RHS. This is used to help avoid max
4678/// expressions in loop trip counts, and to eliminate casts.
4679bool
4680ScalarEvolution::isLoopGuardedByCond(const Loop *L,
4681 ICmpInst::Predicate Pred,
4682 const SCEV *LHS, const SCEV *RHS) {
Dan Gohman8ea94522009-05-18 16:03:58 +00004683 // Interpret a null as meaning no loop, where there is obviously no guard
4684 // (interprocedural conditions notwithstanding).
4685 if (!L) return false;
4686
Dan Gohman859b4822009-05-18 15:36:09 +00004687 BasicBlock *Predecessor = getLoopPredecessor(L);
4688 BasicBlock *PredecessorDest = L->getHeader();
Nick Lewycky59cff122008-07-12 07:41:32 +00004689
Dan Gohman859b4822009-05-18 15:36:09 +00004690 // Starting at the loop predecessor, climb up the predecessor chain, as long
4691 // as there are predecessors that can be found that have unique successors
Dan Gohmanfd6edef2008-09-15 22:18:04 +00004692 // leading to the original header.
Dan Gohman859b4822009-05-18 15:36:09 +00004693 for (; Predecessor;
4694 PredecessorDest = Predecessor,
4695 Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) {
Dan Gohman38372182008-08-12 20:17:31 +00004696
4697 BranchInst *LoopEntryPredicate =
Dan Gohman859b4822009-05-18 15:36:09 +00004698 dyn_cast<BranchInst>(Predecessor->getTerminator());
Dan Gohman38372182008-08-12 20:17:31 +00004699 if (!LoopEntryPredicate ||
4700 LoopEntryPredicate->isUnconditional())
4701 continue;
4702
Dan Gohman0f4b2852009-07-21 23:03:19 +00004703 if (isImpliedCond(LoopEntryPredicate->getCondition(), Pred, LHS, RHS,
4704 LoopEntryPredicate->getSuccessor(0) != PredecessorDest))
Dan Gohman38372182008-08-12 20:17:31 +00004705 return true;
Nick Lewycky59cff122008-07-12 07:41:32 +00004706 }
4707
Dan Gohman38372182008-08-12 20:17:31 +00004708 return false;
Nick Lewycky59cff122008-07-12 07:41:32 +00004709}
4710
Dan Gohman0f4b2852009-07-21 23:03:19 +00004711/// isImpliedCond - Test whether the condition described by Pred, LHS,
4712/// and RHS is true whenever the given Cond value evaluates to true.
4713bool ScalarEvolution::isImpliedCond(Value *CondValue,
4714 ICmpInst::Predicate Pred,
4715 const SCEV *LHS, const SCEV *RHS,
4716 bool Inverse) {
Dan Gohman40a5a1b2009-06-24 01:18:18 +00004717 // Recursivly handle And and Or conditions.
4718 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CondValue)) {
4719 if (BO->getOpcode() == Instruction::And) {
4720 if (!Inverse)
Dan Gohman0f4b2852009-07-21 23:03:19 +00004721 return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) ||
4722 isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse);
Dan Gohman40a5a1b2009-06-24 01:18:18 +00004723 } else if (BO->getOpcode() == Instruction::Or) {
4724 if (Inverse)
Dan Gohman0f4b2852009-07-21 23:03:19 +00004725 return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) ||
4726 isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse);
Dan Gohman40a5a1b2009-06-24 01:18:18 +00004727 }
4728 }
4729
4730 ICmpInst *ICI = dyn_cast<ICmpInst>(CondValue);
4731 if (!ICI) return false;
4732
Dan Gohman85b05a22009-07-13 21:35:55 +00004733 // Bail if the ICmp's operands' types are wider than the needed type
4734 // before attempting to call getSCEV on them. This avoids infinite
4735 // recursion, since the analysis of widening casts can require loop
4736 // exit condition information for overflow checking, which would
4737 // lead back here.
4738 if (getTypeSizeInBits(LHS->getType()) <
Dan Gohman0f4b2852009-07-21 23:03:19 +00004739 getTypeSizeInBits(ICI->getOperand(0)->getType()))
Dan Gohman85b05a22009-07-13 21:35:55 +00004740 return false;
4741
Dan Gohman0f4b2852009-07-21 23:03:19 +00004742 // Now that we found a conditional branch that dominates the loop, check to
4743 // see if it is the comparison we are looking for.
4744 ICmpInst::Predicate FoundPred;
4745 if (Inverse)
4746 FoundPred = ICI->getInversePredicate();
4747 else
4748 FoundPred = ICI->getPredicate();
4749
4750 const SCEV *FoundLHS = getSCEV(ICI->getOperand(0));
4751 const SCEV *FoundRHS = getSCEV(ICI->getOperand(1));
Dan Gohman85b05a22009-07-13 21:35:55 +00004752
4753 // Balance the types. The case where FoundLHS' type is wider than
4754 // LHS' type is checked for above.
4755 if (getTypeSizeInBits(LHS->getType()) >
4756 getTypeSizeInBits(FoundLHS->getType())) {
4757 if (CmpInst::isSigned(Pred)) {
4758 FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType());
4759 FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType());
4760 } else {
4761 FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType());
4762 FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType());
4763 }
4764 }
4765
Dan Gohman0f4b2852009-07-21 23:03:19 +00004766 // Canonicalize the query to match the way instcombine will have
4767 // canonicalized the comparison.
4768 // First, put a constant operand on the right.
4769 if (isa<SCEVConstant>(LHS)) {
4770 std::swap(LHS, RHS);
4771 Pred = ICmpInst::getSwappedPredicate(Pred);
4772 }
4773 // Then, canonicalize comparisons with boundary cases.
4774 if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) {
4775 const APInt &RA = RC->getValue()->getValue();
4776 switch (Pred) {
4777 default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
4778 case ICmpInst::ICMP_EQ:
4779 case ICmpInst::ICMP_NE:
4780 break;
4781 case ICmpInst::ICMP_UGE:
4782 if ((RA - 1).isMinValue()) {
4783 Pred = ICmpInst::ICMP_NE;
4784 RHS = getConstant(RA - 1);
4785 break;
4786 }
4787 if (RA.isMaxValue()) {
4788 Pred = ICmpInst::ICMP_EQ;
4789 break;
4790 }
4791 if (RA.isMinValue()) return true;
4792 break;
4793 case ICmpInst::ICMP_ULE:
4794 if ((RA + 1).isMaxValue()) {
4795 Pred = ICmpInst::ICMP_NE;
4796 RHS = getConstant(RA + 1);
4797 break;
4798 }
4799 if (RA.isMinValue()) {
4800 Pred = ICmpInst::ICMP_EQ;
4801 break;
4802 }
4803 if (RA.isMaxValue()) return true;
4804 break;
4805 case ICmpInst::ICMP_SGE:
4806 if ((RA - 1).isMinSignedValue()) {
4807 Pred = ICmpInst::ICMP_NE;
4808 RHS = getConstant(RA - 1);
4809 break;
4810 }
4811 if (RA.isMaxSignedValue()) {
4812 Pred = ICmpInst::ICMP_EQ;
4813 break;
4814 }
4815 if (RA.isMinSignedValue()) return true;
4816 break;
4817 case ICmpInst::ICMP_SLE:
4818 if ((RA + 1).isMaxSignedValue()) {
4819 Pred = ICmpInst::ICMP_NE;
4820 RHS = getConstant(RA + 1);
4821 break;
4822 }
4823 if (RA.isMinSignedValue()) {
4824 Pred = ICmpInst::ICMP_EQ;
4825 break;
4826 }
4827 if (RA.isMaxSignedValue()) return true;
4828 break;
4829 case ICmpInst::ICMP_UGT:
4830 if (RA.isMinValue()) {
4831 Pred = ICmpInst::ICMP_NE;
4832 break;
4833 }
4834 if ((RA + 1).isMaxValue()) {
4835 Pred = ICmpInst::ICMP_EQ;
4836 RHS = getConstant(RA + 1);
4837 break;
4838 }
4839 if (RA.isMaxValue()) return false;
4840 break;
4841 case ICmpInst::ICMP_ULT:
4842 if (RA.isMaxValue()) {
4843 Pred = ICmpInst::ICMP_NE;
4844 break;
4845 }
4846 if ((RA - 1).isMinValue()) {
4847 Pred = ICmpInst::ICMP_EQ;
4848 RHS = getConstant(RA - 1);
4849 break;
4850 }
4851 if (RA.isMinValue()) return false;
4852 break;
4853 case ICmpInst::ICMP_SGT:
4854 if (RA.isMinSignedValue()) {
4855 Pred = ICmpInst::ICMP_NE;
4856 break;
4857 }
4858 if ((RA + 1).isMaxSignedValue()) {
4859 Pred = ICmpInst::ICMP_EQ;
4860 RHS = getConstant(RA + 1);
4861 break;
4862 }
4863 if (RA.isMaxSignedValue()) return false;
4864 break;
4865 case ICmpInst::ICMP_SLT:
4866 if (RA.isMaxSignedValue()) {
4867 Pred = ICmpInst::ICMP_NE;
4868 break;
4869 }
4870 if ((RA - 1).isMinSignedValue()) {
4871 Pred = ICmpInst::ICMP_EQ;
4872 RHS = getConstant(RA - 1);
4873 break;
4874 }
4875 if (RA.isMinSignedValue()) return false;
4876 break;
4877 }
4878 }
4879
4880 // Check to see if we can make the LHS or RHS match.
4881 if (LHS == FoundRHS || RHS == FoundLHS) {
4882 if (isa<SCEVConstant>(RHS)) {
4883 std::swap(FoundLHS, FoundRHS);
4884 FoundPred = ICmpInst::getSwappedPredicate(FoundPred);
4885 } else {
4886 std::swap(LHS, RHS);
4887 Pred = ICmpInst::getSwappedPredicate(Pred);
4888 }
4889 }
4890
4891 // Check whether the found predicate is the same as the desired predicate.
4892 if (FoundPred == Pred)
4893 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS);
4894
4895 // Check whether swapping the found predicate makes it the same as the
4896 // desired predicate.
4897 if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) {
4898 if (isa<SCEVConstant>(RHS))
4899 return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS);
4900 else
4901 return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred),
4902 RHS, LHS, FoundLHS, FoundRHS);
4903 }
4904
4905 // Check whether the actual condition is beyond sufficient.
4906 if (FoundPred == ICmpInst::ICMP_EQ)
4907 if (ICmpInst::isTrueWhenEqual(Pred))
4908 if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS))
4909 return true;
4910 if (Pred == ICmpInst::ICMP_NE)
4911 if (!ICmpInst::isTrueWhenEqual(FoundPred))
4912 if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS))
4913 return true;
4914
4915 // Otherwise assume the worst.
4916 return false;
Dan Gohman85b05a22009-07-13 21:35:55 +00004917}
4918
Dan Gohman0f4b2852009-07-21 23:03:19 +00004919/// isImpliedCondOperands - Test whether the condition described by Pred,
4920/// LHS, and RHS is true whenever the condition desribed by Pred, FoundLHS,
4921/// and FoundRHS is true.
4922bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred,
4923 const SCEV *LHS, const SCEV *RHS,
4924 const SCEV *FoundLHS,
4925 const SCEV *FoundRHS) {
4926 return isImpliedCondOperandsHelper(Pred, LHS, RHS,
4927 FoundLHS, FoundRHS) ||
4928 // ~x < ~y --> x > y
4929 isImpliedCondOperandsHelper(Pred, LHS, RHS,
4930 getNotSCEV(FoundRHS),
4931 getNotSCEV(FoundLHS));
4932}
4933
4934/// isImpliedCondOperandsHelper - Test whether the condition described by
4935/// Pred, LHS, and RHS is true whenever the condition desribed by Pred,
4936/// FoundLHS, and FoundRHS is true.
Dan Gohman85b05a22009-07-13 21:35:55 +00004937bool
Dan Gohman0f4b2852009-07-21 23:03:19 +00004938ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
4939 const SCEV *LHS, const SCEV *RHS,
4940 const SCEV *FoundLHS,
4941 const SCEV *FoundRHS) {
Dan Gohman85b05a22009-07-13 21:35:55 +00004942 switch (Pred) {
Dan Gohman850f7912009-07-16 17:34:36 +00004943 default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
4944 case ICmpInst::ICMP_EQ:
4945 case ICmpInst::ICMP_NE:
4946 if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS))
4947 return true;
4948 break;
Dan Gohman85b05a22009-07-13 21:35:55 +00004949 case ICmpInst::ICMP_SLT:
Dan Gohman850f7912009-07-16 17:34:36 +00004950 case ICmpInst::ICMP_SLE:
Dan Gohman85b05a22009-07-13 21:35:55 +00004951 if (isKnownPredicate(ICmpInst::ICMP_SLE, LHS, FoundLHS) &&
4952 isKnownPredicate(ICmpInst::ICMP_SGE, RHS, FoundRHS))
4953 return true;
4954 break;
4955 case ICmpInst::ICMP_SGT:
Dan Gohman850f7912009-07-16 17:34:36 +00004956 case ICmpInst::ICMP_SGE:
Dan Gohman85b05a22009-07-13 21:35:55 +00004957 if (isKnownPredicate(ICmpInst::ICMP_SGE, LHS, FoundLHS) &&
4958 isKnownPredicate(ICmpInst::ICMP_SLE, RHS, FoundRHS))
4959 return true;
4960 break;
4961 case ICmpInst::ICMP_ULT:
Dan Gohman850f7912009-07-16 17:34:36 +00004962 case ICmpInst::ICMP_ULE:
Dan Gohman85b05a22009-07-13 21:35:55 +00004963 if (isKnownPredicate(ICmpInst::ICMP_ULE, LHS, FoundLHS) &&
4964 isKnownPredicate(ICmpInst::ICMP_UGE, RHS, FoundRHS))
4965 return true;
4966 break;
4967 case ICmpInst::ICMP_UGT:
Dan Gohman850f7912009-07-16 17:34:36 +00004968 case ICmpInst::ICMP_UGE:
Dan Gohman85b05a22009-07-13 21:35:55 +00004969 if (isKnownPredicate(ICmpInst::ICMP_UGE, LHS, FoundLHS) &&
4970 isKnownPredicate(ICmpInst::ICMP_ULE, RHS, FoundRHS))
4971 return true;
4972 break;
4973 }
4974
4975 return false;
Dan Gohman40a5a1b2009-06-24 01:18:18 +00004976}
4977
Dan Gohman51f53b72009-06-21 23:46:38 +00004978/// getBECount - Subtract the end and start values and divide by the step,
4979/// rounding up, to get the number of times the backedge is executed. Return
4980/// CouldNotCompute if an intermediate computation overflows.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004981const SCEV *ScalarEvolution::getBECount(const SCEV *Start,
Dan Gohmanf5074ec2009-07-13 22:05:32 +00004982 const SCEV *End,
Dan Gohman1f96e672009-09-17 18:05:20 +00004983 const SCEV *Step,
4984 bool NoWrap) {
Dan Gohman52fddd32010-01-26 04:40:18 +00004985 assert(!isKnownNegative(Step) &&
4986 "This code doesn't handle negative strides yet!");
4987
Dan Gohman51f53b72009-06-21 23:46:38 +00004988 const Type *Ty = Start->getType();
Dan Gohman0bba49c2009-07-07 17:06:11 +00004989 const SCEV *NegOne = getIntegerSCEV(-1, Ty);
4990 const SCEV *Diff = getMinusSCEV(End, Start);
4991 const SCEV *RoundUp = getAddExpr(Step, NegOne);
Dan Gohman51f53b72009-06-21 23:46:38 +00004992
4993 // Add an adjustment to the difference between End and Start so that
4994 // the division will effectively round up.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004995 const SCEV *Add = getAddExpr(Diff, RoundUp);
Dan Gohman51f53b72009-06-21 23:46:38 +00004996
Dan Gohman1f96e672009-09-17 18:05:20 +00004997 if (!NoWrap) {
4998 // Check Add for unsigned overflow.
4999 // TODO: More sophisticated things could be done here.
5000 const Type *WideTy = IntegerType::get(getContext(),
5001 getTypeSizeInBits(Ty) + 1);
5002 const SCEV *EDiff = getZeroExtendExpr(Diff, WideTy);
5003 const SCEV *ERoundUp = getZeroExtendExpr(RoundUp, WideTy);
5004 const SCEV *OperandExtendedAdd = getAddExpr(EDiff, ERoundUp);
5005 if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd)
5006 return getCouldNotCompute();
5007 }
Dan Gohman51f53b72009-06-21 23:46:38 +00005008
5009 return getUDivExpr(Add, Step);
5010}
5011
Chris Lattnerdb25de42005-08-15 23:33:51 +00005012/// HowManyLessThans - Return the number of times a backedge containing the
5013/// specified less-than comparison will execute. If not computable, return
Dan Gohman86fbf2f2009-06-06 14:37:11 +00005014/// CouldNotCompute.
Dan Gohman64a845e2009-06-24 04:48:43 +00005015ScalarEvolution::BackedgeTakenInfo
5016ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
5017 const Loop *L, bool isSigned) {
Chris Lattnerdb25de42005-08-15 23:33:51 +00005018 // Only handle: "ADDREC < LoopInvariant".
Dan Gohman1c343752009-06-27 21:21:31 +00005019 if (!RHS->isLoopInvariant(L)) return getCouldNotCompute();
Chris Lattnerdb25de42005-08-15 23:33:51 +00005020
Dan Gohman35738ac2009-05-04 22:30:44 +00005021 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS);
Chris Lattnerdb25de42005-08-15 23:33:51 +00005022 if (!AddRec || AddRec->getLoop() != L)
Dan Gohman1c343752009-06-27 21:21:31 +00005023 return getCouldNotCompute();
Chris Lattnerdb25de42005-08-15 23:33:51 +00005024
Dan Gohman1f96e672009-09-17 18:05:20 +00005025 // Check to see if we have a flag which makes analysis easy.
5026 bool NoWrap = isSigned ? AddRec->hasNoSignedWrap() :
5027 AddRec->hasNoUnsignedWrap();
5028
Chris Lattnerdb25de42005-08-15 23:33:51 +00005029 if (AddRec->isAffine()) {
Dan Gohmana1af7572009-04-30 20:47:05 +00005030 unsigned BitWidth = getTypeSizeInBits(AddRec->getType());
Dan Gohman0bba49c2009-07-07 17:06:11 +00005031 const SCEV *Step = AddRec->getStepRecurrence(*this);
Dan Gohmana1af7572009-04-30 20:47:05 +00005032
Dan Gohman52fddd32010-01-26 04:40:18 +00005033 if (Step->isZero())
Dan Gohman1c343752009-06-27 21:21:31 +00005034 return getCouldNotCompute();
Dan Gohman52fddd32010-01-26 04:40:18 +00005035 if (Step->isOne()) {
Dan Gohmana1af7572009-04-30 20:47:05 +00005036 // With unit stride, the iteration never steps past the limit value.
Dan Gohman52fddd32010-01-26 04:40:18 +00005037 } else if (isKnownPositive(Step)) {
5038 // Test whether a positive iteration iteration can step past the limit
5039 // value and past the maximum value for its type in a single step.
5040 // Note that it's not sufficient to check NoWrap here, because even
5041 // though the value after a wrap is undefined, it's not undefined
5042 // behavior, so if wrap does occur, the loop could either terminate or
Dan Gohman155eec72010-01-26 18:32:54 +00005043 // loop infinitely, but in either case, the loop is guaranteed to
Dan Gohman52fddd32010-01-26 04:40:18 +00005044 // iterate at least until the iteration where the wrapping occurs.
5045 const SCEV *One = getIntegerSCEV(1, Step->getType());
5046 if (isSigned) {
5047 APInt Max = APInt::getSignedMaxValue(BitWidth);
5048 if ((Max - getSignedRange(getMinusSCEV(Step, One)).getSignedMax())
5049 .slt(getSignedRange(RHS).getSignedMax()))
5050 return getCouldNotCompute();
5051 } else {
5052 APInt Max = APInt::getMaxValue(BitWidth);
5053 if ((Max - getUnsignedRange(getMinusSCEV(Step, One)).getUnsignedMax())
5054 .ult(getUnsignedRange(RHS).getUnsignedMax()))
5055 return getCouldNotCompute();
5056 }
Dan Gohmana1af7572009-04-30 20:47:05 +00005057 } else
Dan Gohman52fddd32010-01-26 04:40:18 +00005058 // TODO: Handle negative strides here and below.
Dan Gohman1c343752009-06-27 21:21:31 +00005059 return getCouldNotCompute();
Chris Lattnerdb25de42005-08-15 23:33:51 +00005060
Dan Gohmana1af7572009-04-30 20:47:05 +00005061 // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant
5062 // m. So, we count the number of iterations in which {n,+,s} < m is true.
5063 // Note that we cannot simply return max(m-n,0)/s because it's not safe to
Wojciech Matyjewicza65ee032008-02-13 12:21:32 +00005064 // treat m-n as signed nor unsigned due to overflow possibility.
Chris Lattnerdb25de42005-08-15 23:33:51 +00005065
Wojciech Matyjewicz3a4cbe22008-02-13 11:51:34 +00005066 // First, we get the value of the LHS in the first iteration: n
Dan Gohman0bba49c2009-07-07 17:06:11 +00005067 const SCEV *Start = AddRec->getOperand(0);
Wojciech Matyjewicz3a4cbe22008-02-13 11:51:34 +00005068
Dan Gohmana1af7572009-04-30 20:47:05 +00005069 // Determine the minimum constant start value.
Dan Gohman85b05a22009-07-13 21:35:55 +00005070 const SCEV *MinStart = getConstant(isSigned ?
5071 getSignedRange(Start).getSignedMin() :
5072 getUnsignedRange(Start).getUnsignedMin());
Wojciech Matyjewicz3a4cbe22008-02-13 11:51:34 +00005073
Dan Gohmana1af7572009-04-30 20:47:05 +00005074 // If we know that the condition is true in order to enter the loop,
5075 // then we know that it will run exactly (m-n)/s times. Otherwise, we
Dan Gohman6c0866c2009-05-24 23:45:28 +00005076 // only know that it will execute (max(m,n)-n)/s times. In both cases,
5077 // the division must round up.
Dan Gohman0bba49c2009-07-07 17:06:11 +00005078 const SCEV *End = RHS;
Dan Gohmana1af7572009-04-30 20:47:05 +00005079 if (!isLoopGuardedByCond(L,
Dan Gohman85b05a22009-07-13 21:35:55 +00005080 isSigned ? ICmpInst::ICMP_SLT :
5081 ICmpInst::ICMP_ULT,
Dan Gohmana1af7572009-04-30 20:47:05 +00005082 getMinusSCEV(Start, Step), RHS))
5083 End = isSigned ? getSMaxExpr(RHS, Start)
5084 : getUMaxExpr(RHS, Start);
5085
5086 // Determine the maximum constant end value.
Dan Gohman85b05a22009-07-13 21:35:55 +00005087 const SCEV *MaxEnd = getConstant(isSigned ?
5088 getSignedRange(End).getSignedMax() :
5089 getUnsignedRange(End).getUnsignedMax());
Dan Gohmana1af7572009-04-30 20:47:05 +00005090
Dan Gohman52fddd32010-01-26 04:40:18 +00005091 // If MaxEnd is within a step of the maximum integer value in its type,
5092 // adjust it down to the minimum value which would produce the same effect.
5093 // This allows the subsequent ceiling divison of (N+(step-1))/step to
5094 // compute the correct value.
5095 const SCEV *StepMinusOne = getMinusSCEV(Step,
5096 getIntegerSCEV(1, Step->getType()));
5097 MaxEnd = isSigned ?
5098 getSMinExpr(MaxEnd,
5099 getMinusSCEV(getConstant(APInt::getSignedMaxValue(BitWidth)),
5100 StepMinusOne)) :
5101 getUMinExpr(MaxEnd,
5102 getMinusSCEV(getConstant(APInt::getMaxValue(BitWidth)),
5103 StepMinusOne));
5104
Dan Gohmana1af7572009-04-30 20:47:05 +00005105 // Finally, we subtract these two values and divide, rounding up, to get
5106 // the number of times the backedge is executed.
Dan Gohman1f96e672009-09-17 18:05:20 +00005107 const SCEV *BECount = getBECount(Start, End, Step, NoWrap);
Dan Gohmana1af7572009-04-30 20:47:05 +00005108
5109 // The maximum backedge count is similar, except using the minimum start
5110 // value and the maximum end value.
Dan Gohman1f96e672009-09-17 18:05:20 +00005111 const SCEV *MaxBECount = getBECount(MinStart, MaxEnd, Step, NoWrap);
Dan Gohmana1af7572009-04-30 20:47:05 +00005112
5113 return BackedgeTakenInfo(BECount, MaxBECount);
Chris Lattnerdb25de42005-08-15 23:33:51 +00005114 }
5115
Dan Gohman1c343752009-06-27 21:21:31 +00005116 return getCouldNotCompute();
Chris Lattnerdb25de42005-08-15 23:33:51 +00005117}
5118
Chris Lattner53e677a2004-04-02 20:23:17 +00005119/// getNumIterationsInRange - Return the number of iterations of this loop that
5120/// produce values in the specified constant range. Another way of looking at
5121/// this is that it returns the first iteration number where the value is not in
5122/// the condition, thus computing the exit count. If the iteration count can't
5123/// be computed, an instance of SCEVCouldNotCompute is returned.
Dan Gohman0bba49c2009-07-07 17:06:11 +00005124const SCEV *SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
Dan Gohman64a845e2009-06-24 04:48:43 +00005125 ScalarEvolution &SE) const {
Chris Lattner53e677a2004-04-02 20:23:17 +00005126 if (Range.isFullSet()) // Infinite loop.
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00005127 return SE.getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00005128
5129 // If the start is a non-zero constant, shift the range to simplify things.
Dan Gohman622ed672009-05-04 22:02:23 +00005130 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart()))
Reid Spencercae57542007-03-02 00:28:52 +00005131 if (!SC->getValue()->isZero()) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00005132 SmallVector<const SCEV *, 4> Operands(op_begin(), op_end());
Dan Gohman246b2562007-10-22 18:31:58 +00005133 Operands[0] = SE.getIntegerSCEV(0, SC->getType());
Dan Gohman0bba49c2009-07-07 17:06:11 +00005134 const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop());
Dan Gohman622ed672009-05-04 22:02:23 +00005135 if (const SCEVAddRecExpr *ShiftedAddRec =
5136 dyn_cast<SCEVAddRecExpr>(Shifted))
Chris Lattner53e677a2004-04-02 20:23:17 +00005137 return ShiftedAddRec->getNumIterationsInRange(
Dan Gohman246b2562007-10-22 18:31:58 +00005138 Range.subtract(SC->getValue()->getValue()), SE);
Chris Lattner53e677a2004-04-02 20:23:17 +00005139 // This is strange and shouldn't happen.
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00005140 return SE.getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00005141 }
5142
5143 // The only time we can solve this is when we have all constant indices.
5144 // Otherwise, we cannot determine the overflow conditions.
5145 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
5146 if (!isa<SCEVConstant>(getOperand(i)))
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00005147 return SE.getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00005148
5149
5150 // Okay at this point we know that all elements of the chrec are constants and
5151 // that the start element is zero.
5152
5153 // First check to see if the range contains zero. If not, the first
5154 // iteration exits.
Dan Gohmanaf79fb52009-04-21 01:07:12 +00005155 unsigned BitWidth = SE.getTypeSizeInBits(getType());
Dan Gohman2d1be872009-04-16 03:18:22 +00005156 if (!Range.contains(APInt(BitWidth, 0)))
Dan Gohman6de29f82009-06-15 22:12:54 +00005157 return SE.getIntegerSCEV(0, getType());
Misha Brukman2b37d7c2005-04-21 21:13:18 +00005158
Chris Lattner53e677a2004-04-02 20:23:17 +00005159 if (isAffine()) {
5160 // If this is an affine expression then we have this situation:
5161 // Solve {0,+,A} in Range === Ax in Range
5162
Nick Lewyckyeefdebe2007-07-16 02:08:00 +00005163 // We know that zero is in the range. If A is positive then we know that
5164 // the upper value of the range must be the first possible exit value.
5165 // If A is negative then the lower of the range is the last possible loop
5166 // value. Also note that we already checked for a full range.
Dan Gohman2d1be872009-04-16 03:18:22 +00005167 APInt One(BitWidth,1);
Nick Lewyckyeefdebe2007-07-16 02:08:00 +00005168 APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue();
5169 APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower();
Chris Lattner53e677a2004-04-02 20:23:17 +00005170
Nick Lewyckyeefdebe2007-07-16 02:08:00 +00005171 // The exit value should be (End+A)/A.
Nick Lewycky9a2f9312007-09-27 14:12:54 +00005172 APInt ExitVal = (End + A).udiv(A);
Owen Andersoneed707b2009-07-24 23:12:02 +00005173 ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal);
Chris Lattner53e677a2004-04-02 20:23:17 +00005174
5175 // Evaluate at the exit value. If we really did fall out of the valid
5176 // range, then we computed our trip count, otherwise wrap around or other
5177 // things must have happened.
Dan Gohman246b2562007-10-22 18:31:58 +00005178 ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE);
Reid Spencera6e8a952007-03-01 07:54:15 +00005179 if (Range.contains(Val->getValue()))
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00005180 return SE.getCouldNotCompute(); // Something strange happened
Chris Lattner53e677a2004-04-02 20:23:17 +00005181
5182 // Ensure that the previous value is in the range. This is a sanity check.
Reid Spencer581b0d42007-02-28 19:57:34 +00005183 assert(Range.contains(
Dan Gohman64a845e2009-06-24 04:48:43 +00005184 EvaluateConstantChrecAtConstant(this,
Owen Andersoneed707b2009-07-24 23:12:02 +00005185 ConstantInt::get(SE.getContext(), ExitVal - One), SE)->getValue()) &&
Chris Lattner53e677a2004-04-02 20:23:17 +00005186 "Linear scev computation is off in a bad way!");
Dan Gohman246b2562007-10-22 18:31:58 +00005187 return SE.getConstant(ExitValue);
Chris Lattner53e677a2004-04-02 20:23:17 +00005188 } else if (isQuadratic()) {
5189 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the
5190 // quadratic equation to solve it. To do this, we must frame our problem in
5191 // terms of figuring out when zero is crossed, instead of when
5192 // Range.getUpper() is crossed.
Dan Gohman0bba49c2009-07-07 17:06:11 +00005193 SmallVector<const SCEV *, 4> NewOps(op_begin(), op_end());
Dan Gohman246b2562007-10-22 18:31:58 +00005194 NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper()));
Dan Gohman0bba49c2009-07-07 17:06:11 +00005195 const SCEV *NewAddRec = SE.getAddRecExpr(NewOps, getLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +00005196
5197 // Next, solve the constructed addrec
Dan Gohman0bba49c2009-07-07 17:06:11 +00005198 std::pair<const SCEV *,const SCEV *> Roots =
Dan Gohman246b2562007-10-22 18:31:58 +00005199 SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE);
Dan Gohman35738ac2009-05-04 22:30:44 +00005200 const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
5201 const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
Chris Lattner53e677a2004-04-02 20:23:17 +00005202 if (R1) {
5203 // Pick the smallest positive root value.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00005204 if (ConstantInt *CB =
Owen Andersonbaf3c402009-07-29 18:55:55 +00005205 dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT,
Owen Anderson76f600b2009-07-06 22:37:39 +00005206 R1->getValue(), R2->getValue()))) {
Reid Spencer579dca12007-01-12 04:24:46 +00005207 if (CB->getZExtValue() == false)
Chris Lattner53e677a2004-04-02 20:23:17 +00005208 std::swap(R1, R2); // R1 is the minimum root now.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00005209
Chris Lattner53e677a2004-04-02 20:23:17 +00005210 // Make sure the root is not off by one. The returned iteration should
5211 // not be in the range, but the previous one should be. When solving
5212 // for "X*X < 5", for example, we should not return a root of 2.
5213 ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this,
Dan Gohman246b2562007-10-22 18:31:58 +00005214 R1->getValue(),
5215 SE);
Reid Spencera6e8a952007-03-01 07:54:15 +00005216 if (Range.contains(R1Val->getValue())) {
Chris Lattner53e677a2004-04-02 20:23:17 +00005217 // The next iteration must be out of the range...
Owen Anderson76f600b2009-07-06 22:37:39 +00005218 ConstantInt *NextVal =
Owen Andersoneed707b2009-07-24 23:12:02 +00005219 ConstantInt::get(SE.getContext(), R1->getValue()->getValue()+1);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00005220
Dan Gohman246b2562007-10-22 18:31:58 +00005221 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
Reid Spencera6e8a952007-03-01 07:54:15 +00005222 if (!Range.contains(R1Val->getValue()))
Dan Gohman246b2562007-10-22 18:31:58 +00005223 return SE.getConstant(NextVal);
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00005224 return SE.getCouldNotCompute(); // Something strange happened
Chris Lattner53e677a2004-04-02 20:23:17 +00005225 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00005226
Chris Lattner53e677a2004-04-02 20:23:17 +00005227 // If R1 was not in the range, then it is a good return value. Make
5228 // sure that R1-1 WAS in the range though, just in case.
Owen Anderson76f600b2009-07-06 22:37:39 +00005229 ConstantInt *NextVal =
Owen Andersoneed707b2009-07-24 23:12:02 +00005230 ConstantInt::get(SE.getContext(), R1->getValue()->getValue()-1);
Dan Gohman246b2562007-10-22 18:31:58 +00005231 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
Reid Spencera6e8a952007-03-01 07:54:15 +00005232 if (Range.contains(R1Val->getValue()))
Chris Lattner53e677a2004-04-02 20:23:17 +00005233 return R1;
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00005234 return SE.getCouldNotCompute(); // Something strange happened
Chris Lattner53e677a2004-04-02 20:23:17 +00005235 }
5236 }
5237 }
5238
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00005239 return SE.getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00005240}
5241
5242
5243
5244//===----------------------------------------------------------------------===//
Dan Gohman35738ac2009-05-04 22:30:44 +00005245// SCEVCallbackVH Class Implementation
5246//===----------------------------------------------------------------------===//
5247
Dan Gohman1959b752009-05-19 19:22:47 +00005248void ScalarEvolution::SCEVCallbackVH::deleted() {
Dan Gohmanddf9f992009-07-13 22:20:53 +00005249 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
Dan Gohman35738ac2009-05-04 22:30:44 +00005250 if (PHINode *PN = dyn_cast<PHINode>(getValPtr()))
5251 SE->ConstantEvolutionLoopExitValue.erase(PN);
5252 SE->Scalars.erase(getValPtr());
5253 // this now dangles!
5254}
5255
Dan Gohman1959b752009-05-19 19:22:47 +00005256void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *) {
Dan Gohmanddf9f992009-07-13 22:20:53 +00005257 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
Dan Gohman35738ac2009-05-04 22:30:44 +00005258
5259 // Forget all the expressions associated with users of the old value,
5260 // so that future queries will recompute the expressions using the new
5261 // value.
5262 SmallVector<User *, 16> Worklist;
Dan Gohman69fcae92009-07-14 14:34:04 +00005263 SmallPtrSet<User *, 8> Visited;
Dan Gohman35738ac2009-05-04 22:30:44 +00005264 Value *Old = getValPtr();
5265 bool DeleteOld = false;
5266 for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end();
5267 UI != UE; ++UI)
5268 Worklist.push_back(*UI);
5269 while (!Worklist.empty()) {
5270 User *U = Worklist.pop_back_val();
5271 // Deleting the Old value will cause this to dangle. Postpone
5272 // that until everything else is done.
5273 if (U == Old) {
5274 DeleteOld = true;
5275 continue;
5276 }
Dan Gohman69fcae92009-07-14 14:34:04 +00005277 if (!Visited.insert(U))
5278 continue;
Dan Gohman35738ac2009-05-04 22:30:44 +00005279 if (PHINode *PN = dyn_cast<PHINode>(U))
5280 SE->ConstantEvolutionLoopExitValue.erase(PN);
Dan Gohman69fcae92009-07-14 14:34:04 +00005281 SE->Scalars.erase(U);
5282 for (Value::use_iterator UI = U->use_begin(), UE = U->use_end();
5283 UI != UE; ++UI)
5284 Worklist.push_back(*UI);
Dan Gohman35738ac2009-05-04 22:30:44 +00005285 }
Dan Gohman69fcae92009-07-14 14:34:04 +00005286 // Delete the Old value if it (indirectly) references itself.
Dan Gohman35738ac2009-05-04 22:30:44 +00005287 if (DeleteOld) {
5288 if (PHINode *PN = dyn_cast<PHINode>(Old))
5289 SE->ConstantEvolutionLoopExitValue.erase(PN);
5290 SE->Scalars.erase(Old);
5291 // this now dangles!
5292 }
5293 // this may dangle!
5294}
5295
Dan Gohman1959b752009-05-19 19:22:47 +00005296ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se)
Dan Gohman35738ac2009-05-04 22:30:44 +00005297 : CallbackVH(V), SE(se) {}
5298
5299//===----------------------------------------------------------------------===//
Chris Lattner53e677a2004-04-02 20:23:17 +00005300// ScalarEvolution Class Implementation
5301//===----------------------------------------------------------------------===//
5302
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005303ScalarEvolution::ScalarEvolution()
Dan Gohman1c343752009-06-27 21:21:31 +00005304 : FunctionPass(&ID) {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005305}
5306
Chris Lattner53e677a2004-04-02 20:23:17 +00005307bool ScalarEvolution::runOnFunction(Function &F) {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005308 this->F = &F;
5309 LI = &getAnalysis<LoopInfo>();
Dan Gohman1cd92752010-01-19 22:21:27 +00005310 DT = &getAnalysis<DominatorTree>();
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005311 TD = getAnalysisIfAvailable<TargetData>();
Chris Lattner53e677a2004-04-02 20:23:17 +00005312 return false;
5313}
5314
5315void ScalarEvolution::releaseMemory() {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005316 Scalars.clear();
5317 BackedgeTakenCounts.clear();
5318 ConstantEvolutionLoopExitValue.clear();
Dan Gohman6bce6432009-05-08 20:47:27 +00005319 ValuesAtScopes.clear();
Dan Gohman1c343752009-06-27 21:21:31 +00005320 UniqueSCEVs.clear();
5321 SCEVAllocator.Reset();
Chris Lattner53e677a2004-04-02 20:23:17 +00005322}
5323
5324void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const {
5325 AU.setPreservesAll();
Chris Lattner53e677a2004-04-02 20:23:17 +00005326 AU.addRequiredTransitive<LoopInfo>();
Dan Gohman1cd92752010-01-19 22:21:27 +00005327 AU.addRequiredTransitive<DominatorTree>();
Dan Gohman2d1be872009-04-16 03:18:22 +00005328}
5329
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005330bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) {
Dan Gohman46bdfb02009-02-24 18:55:53 +00005331 return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L));
Chris Lattner53e677a2004-04-02 20:23:17 +00005332}
5333
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005334static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE,
Chris Lattner53e677a2004-04-02 20:23:17 +00005335 const Loop *L) {
5336 // Print all inner loops first
5337 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
5338 PrintLoopInfo(OS, SE, *I);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00005339
Dan Gohman30733292010-01-09 18:17:45 +00005340 OS << "Loop ";
5341 WriteAsOperand(OS, L->getHeader(), /*PrintType=*/false);
5342 OS << ": ";
Chris Lattnerf1ab4b42004-04-18 22:14:10 +00005343
Dan Gohman5d984912009-12-18 01:14:11 +00005344 SmallVector<BasicBlock *, 8> ExitBlocks;
Chris Lattnerf1ab4b42004-04-18 22:14:10 +00005345 L->getExitBlocks(ExitBlocks);
5346 if (ExitBlocks.size() != 1)
Nick Lewyckyaeb5e5c2008-01-02 02:49:20 +00005347 OS << "<multiple exits> ";
Chris Lattner53e677a2004-04-02 20:23:17 +00005348
Dan Gohman46bdfb02009-02-24 18:55:53 +00005349 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
5350 OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L);
Chris Lattner53e677a2004-04-02 20:23:17 +00005351 } else {
Dan Gohman46bdfb02009-02-24 18:55:53 +00005352 OS << "Unpredictable backedge-taken count. ";
Chris Lattner53e677a2004-04-02 20:23:17 +00005353 }
5354
Dan Gohman30733292010-01-09 18:17:45 +00005355 OS << "\n"
5356 "Loop ";
5357 WriteAsOperand(OS, L->getHeader(), /*PrintType=*/false);
5358 OS << ": ";
Dan Gohmanaa551ae2009-06-24 00:33:16 +00005359
5360 if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) {
5361 OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L);
5362 } else {
5363 OS << "Unpredictable max backedge-taken count. ";
5364 }
5365
5366 OS << "\n";
Chris Lattner53e677a2004-04-02 20:23:17 +00005367}
5368
Dan Gohman5d984912009-12-18 01:14:11 +00005369void ScalarEvolution::print(raw_ostream &OS, const Module *) const {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005370 // ScalarEvolution's implementaiton of the print method is to print
5371 // out SCEV values of all instructions that are interesting. Doing
5372 // this potentially causes it to create new SCEV objects though,
5373 // which technically conflicts with the const qualifier. This isn't
Dan Gohman1afdc5f2009-07-10 20:25:29 +00005374 // observable from outside the class though, so casting away the
5375 // const isn't dangerous.
Dan Gohman5d984912009-12-18 01:14:11 +00005376 ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
Chris Lattner53e677a2004-04-02 20:23:17 +00005377
Dan Gohman30733292010-01-09 18:17:45 +00005378 OS << "Classifying expressions for: ";
5379 WriteAsOperand(OS, F, /*PrintType=*/false);
5380 OS << "\n";
Chris Lattner53e677a2004-04-02 20:23:17 +00005381 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Dan Gohmand9c1c852009-04-30 01:30:18 +00005382 if (isSCEVable(I->getType())) {
Dan Gohmanc902e132009-07-13 23:03:05 +00005383 OS << *I << '\n';
Dan Gohman8dae1382008-09-14 17:21:12 +00005384 OS << " --> ";
Dan Gohman0bba49c2009-07-07 17:06:11 +00005385 const SCEV *SV = SE.getSCEV(&*I);
Chris Lattner53e677a2004-04-02 20:23:17 +00005386 SV->print(OS);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00005387
Dan Gohman0c689c52009-06-19 17:49:54 +00005388 const Loop *L = LI->getLoopFor((*I).getParent());
5389
Dan Gohman0bba49c2009-07-07 17:06:11 +00005390 const SCEV *AtUse = SE.getSCEVAtScope(SV, L);
Dan Gohman0c689c52009-06-19 17:49:54 +00005391 if (AtUse != SV) {
5392 OS << " --> ";
5393 AtUse->print(OS);
5394 }
5395
5396 if (L) {
Dan Gohman9e7d9882009-06-18 00:37:45 +00005397 OS << "\t\t" "Exits: ";
Dan Gohman0bba49c2009-07-07 17:06:11 +00005398 const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop());
Dan Gohmand594e6f2009-05-24 23:25:42 +00005399 if (!ExitValue->isLoopInvariant(L)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00005400 OS << "<<Unknown>>";
5401 } else {
5402 OS << *ExitValue;
5403 }
5404 }
5405
Chris Lattner53e677a2004-04-02 20:23:17 +00005406 OS << "\n";
5407 }
5408
Dan Gohman30733292010-01-09 18:17:45 +00005409 OS << "Determining loop execution counts for: ";
5410 WriteAsOperand(OS, F, /*PrintType=*/false);
5411 OS << "\n";
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005412 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
5413 PrintLoopInfo(OS, &SE, *I);
Chris Lattner53e677a2004-04-02 20:23:17 +00005414}
Dan Gohmanb7ef7292009-04-21 00:47:46 +00005415