blob: 7389007bf2f97fe9f968677c6ee38edbccf74985 [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
Dan Gohmanc40f17b2009-08-18 16:46:41 +0000324void SCEVFieldOffsetExpr::print(raw_ostream &OS) const {
325 // LLVM struct fields don't have names, so just print the field number.
326 OS << "offsetof(" << *STy << ", " << FieldNo << ")";
327}
328
329void SCEVAllocSizeExpr::print(raw_ostream &OS) const {
330 OS << "sizeof(" << *AllocTy << ")";
331}
332
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000333bool SCEVUnknown::isLoopInvariant(const Loop *L) const {
334 // All non-instruction values are loop invariant. All instructions are loop
335 // invariant if they are not contained in the specified loop.
Dan Gohmana3035a62009-05-20 01:01:24 +0000336 // Instructions are never considered invariant in the function body
337 // (null loop) because they are defined within the "loop".
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000338 if (Instruction *I = dyn_cast<Instruction>(V))
Dan Gohman92329c72009-12-18 01:24:09 +0000339 return L && !L->contains(I);
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000340 return true;
341}
Chris Lattner53e677a2004-04-02 20:23:17 +0000342
Evan Cheng5a6c1a82009-02-17 00:13:06 +0000343bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const {
344 if (Instruction *I = dyn_cast<Instruction>(getValue()))
345 return DT->dominates(I->getParent(), BB);
346 return true;
347}
348
Dan Gohman6e70e312009-09-27 15:26:03 +0000349bool SCEVUnknown::properlyDominates(BasicBlock *BB, DominatorTree *DT) const {
350 if (Instruction *I = dyn_cast<Instruction>(getValue()))
351 return DT->properlyDominates(I->getParent(), BB);
352 return true;
353}
354
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000355const Type *SCEVUnknown::getType() const {
356 return V->getType();
357}
Chris Lattner53e677a2004-04-02 20:23:17 +0000358
Dan Gohmanb7ef7292009-04-21 00:47:46 +0000359void SCEVUnknown::print(raw_ostream &OS) const {
Chris Lattner0a7f98c2004-04-15 15:07:24 +0000360 WriteAsOperand(OS, V, false);
Chris Lattner53e677a2004-04-02 20:23:17 +0000361}
362
Chris Lattner8d741b82004-06-20 06:23:15 +0000363//===----------------------------------------------------------------------===//
364// SCEV Utilities
365//===----------------------------------------------------------------------===//
366
Dan Gohmanc40f17b2009-08-18 16:46:41 +0000367static bool CompareTypes(const Type *A, const Type *B) {
368 if (A->getTypeID() != B->getTypeID())
369 return A->getTypeID() < B->getTypeID();
370 if (const IntegerType *AI = dyn_cast<IntegerType>(A)) {
371 const IntegerType *BI = cast<IntegerType>(B);
372 return AI->getBitWidth() < BI->getBitWidth();
373 }
374 if (const PointerType *AI = dyn_cast<PointerType>(A)) {
375 const PointerType *BI = cast<PointerType>(B);
376 return CompareTypes(AI->getElementType(), BI->getElementType());
377 }
378 if (const ArrayType *AI = dyn_cast<ArrayType>(A)) {
379 const ArrayType *BI = cast<ArrayType>(B);
380 if (AI->getNumElements() != BI->getNumElements())
381 return AI->getNumElements() < BI->getNumElements();
382 return CompareTypes(AI->getElementType(), BI->getElementType());
383 }
384 if (const VectorType *AI = dyn_cast<VectorType>(A)) {
385 const VectorType *BI = cast<VectorType>(B);
386 if (AI->getNumElements() != BI->getNumElements())
387 return AI->getNumElements() < BI->getNumElements();
388 return CompareTypes(AI->getElementType(), BI->getElementType());
389 }
390 if (const StructType *AI = dyn_cast<StructType>(A)) {
391 const StructType *BI = cast<StructType>(B);
392 if (AI->getNumElements() != BI->getNumElements())
393 return AI->getNumElements() < BI->getNumElements();
394 for (unsigned i = 0, e = AI->getNumElements(); i != e; ++i)
395 if (CompareTypes(AI->getElementType(i), BI->getElementType(i)) ||
396 CompareTypes(BI->getElementType(i), AI->getElementType(i)))
397 return CompareTypes(AI->getElementType(i), BI->getElementType(i));
398 }
399 return false;
400}
401
Chris Lattner8d741b82004-06-20 06:23:15 +0000402namespace {
403 /// SCEVComplexityCompare - Return true if the complexity of the LHS is less
404 /// than the complexity of the RHS. This comparator is used to canonicalize
405 /// expressions.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000406 class SCEVComplexityCompare {
Dan Gohman72861302009-05-07 14:39:04 +0000407 LoopInfo *LI;
408 public:
409 explicit SCEVComplexityCompare(LoopInfo *li) : LI(li) {}
410
Dan Gohmanf7b37b22008-04-14 18:23:56 +0000411 bool operator()(const SCEV *LHS, const SCEV *RHS) const {
Dan Gohman42214892009-08-31 21:15:23 +0000412 // Fast-path: SCEVs are uniqued so we can do a quick equality check.
413 if (LHS == RHS)
414 return false;
415
Dan Gohman72861302009-05-07 14:39:04 +0000416 // Primarily, sort the SCEVs by their getSCEVType().
417 if (LHS->getSCEVType() != RHS->getSCEVType())
418 return LHS->getSCEVType() < RHS->getSCEVType();
419
420 // Aside from the getSCEVType() ordering, the particular ordering
421 // isn't very important except that it's beneficial to be consistent,
422 // so that (a + b) and (b + a) don't end up as different expressions.
423
424 // Sort SCEVUnknown values with some loose heuristics. TODO: This is
425 // not as complete as it could be.
426 if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) {
427 const SCEVUnknown *RU = cast<SCEVUnknown>(RHS);
428
Dan Gohman5be18e82009-05-19 02:15:55 +0000429 // Order pointer values after integer values. This helps SCEVExpander
430 // form GEPs.
431 if (isa<PointerType>(LU->getType()) && !isa<PointerType>(RU->getType()))
432 return false;
433 if (isa<PointerType>(RU->getType()) && !isa<PointerType>(LU->getType()))
434 return true;
435
Dan Gohman72861302009-05-07 14:39:04 +0000436 // Compare getValueID values.
437 if (LU->getValue()->getValueID() != RU->getValue()->getValueID())
438 return LU->getValue()->getValueID() < RU->getValue()->getValueID();
439
440 // Sort arguments by their position.
441 if (const Argument *LA = dyn_cast<Argument>(LU->getValue())) {
442 const Argument *RA = cast<Argument>(RU->getValue());
443 return LA->getArgNo() < RA->getArgNo();
444 }
445
446 // For instructions, compare their loop depth, and their opcode.
447 // This is pretty loose.
448 if (Instruction *LV = dyn_cast<Instruction>(LU->getValue())) {
449 Instruction *RV = cast<Instruction>(RU->getValue());
450
451 // Compare loop depths.
452 if (LI->getLoopDepth(LV->getParent()) !=
453 LI->getLoopDepth(RV->getParent()))
454 return LI->getLoopDepth(LV->getParent()) <
455 LI->getLoopDepth(RV->getParent());
456
457 // Compare opcodes.
458 if (LV->getOpcode() != RV->getOpcode())
459 return LV->getOpcode() < RV->getOpcode();
460
461 // Compare the number of operands.
462 if (LV->getNumOperands() != RV->getNumOperands())
463 return LV->getNumOperands() < RV->getNumOperands();
464 }
465
466 return false;
467 }
468
Dan Gohman4dfad292009-06-14 22:51:25 +0000469 // Compare constant values.
470 if (const SCEVConstant *LC = dyn_cast<SCEVConstant>(LHS)) {
471 const SCEVConstant *RC = cast<SCEVConstant>(RHS);
Nick Lewyckyd1ec9892009-07-04 17:24:52 +0000472 if (LC->getValue()->getBitWidth() != RC->getValue()->getBitWidth())
473 return LC->getValue()->getBitWidth() < RC->getValue()->getBitWidth();
Dan Gohman4dfad292009-06-14 22:51:25 +0000474 return LC->getValue()->getValue().ult(RC->getValue()->getValue());
475 }
476
477 // Compare addrec loop depths.
478 if (const SCEVAddRecExpr *LA = dyn_cast<SCEVAddRecExpr>(LHS)) {
479 const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS);
480 if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth())
481 return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth();
482 }
Dan Gohman72861302009-05-07 14:39:04 +0000483
484 // Lexicographically compare n-ary expressions.
485 if (const SCEVNAryExpr *LC = dyn_cast<SCEVNAryExpr>(LHS)) {
486 const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS);
487 for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) {
488 if (i >= RC->getNumOperands())
489 return false;
490 if (operator()(LC->getOperand(i), RC->getOperand(i)))
491 return true;
492 if (operator()(RC->getOperand(i), LC->getOperand(i)))
493 return false;
494 }
495 return LC->getNumOperands() < RC->getNumOperands();
496 }
497
Dan Gohmana6b35e22009-05-07 19:23:21 +0000498 // Lexicographically compare udiv expressions.
499 if (const SCEVUDivExpr *LC = dyn_cast<SCEVUDivExpr>(LHS)) {
500 const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS);
501 if (operator()(LC->getLHS(), RC->getLHS()))
502 return true;
503 if (operator()(RC->getLHS(), LC->getLHS()))
504 return false;
505 if (operator()(LC->getRHS(), RC->getRHS()))
506 return true;
507 if (operator()(RC->getRHS(), LC->getRHS()))
508 return false;
509 return false;
510 }
511
Dan Gohman72861302009-05-07 14:39:04 +0000512 // Compare cast expressions by operand.
513 if (const SCEVCastExpr *LC = dyn_cast<SCEVCastExpr>(LHS)) {
514 const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS);
515 return operator()(LC->getOperand(), RC->getOperand());
516 }
517
Dan Gohmanc40f17b2009-08-18 16:46:41 +0000518 // Compare offsetof expressions.
519 if (const SCEVFieldOffsetExpr *LA = dyn_cast<SCEVFieldOffsetExpr>(LHS)) {
520 const SCEVFieldOffsetExpr *RA = cast<SCEVFieldOffsetExpr>(RHS);
521 if (CompareTypes(LA->getStructType(), RA->getStructType()) ||
522 CompareTypes(RA->getStructType(), LA->getStructType()))
523 return CompareTypes(LA->getStructType(), RA->getStructType());
524 return LA->getFieldNo() < RA->getFieldNo();
525 }
526
527 // Compare sizeof expressions by the allocation type.
528 if (const SCEVAllocSizeExpr *LA = dyn_cast<SCEVAllocSizeExpr>(LHS)) {
529 const SCEVAllocSizeExpr *RA = cast<SCEVAllocSizeExpr>(RHS);
530 return CompareTypes(LA->getAllocType(), RA->getAllocType());
531 }
532
Torok Edwinc23197a2009-07-14 16:55:14 +0000533 llvm_unreachable("Unknown SCEV kind!");
Dan Gohman72861302009-05-07 14:39:04 +0000534 return false;
Chris Lattner8d741b82004-06-20 06:23:15 +0000535 }
536 };
537}
538
539/// GroupByComplexity - Given a list of SCEV objects, order them by their
540/// complexity, and group objects of the same complexity together by value.
541/// When this routine is finished, we know that any duplicates in the vector are
542/// consecutive and that complexity is monotonically increasing.
543///
544/// Note that we go take special precautions to ensure that we get determinstic
545/// results from this routine. In other words, we don't want the results of
546/// this to depend on where the addresses of various SCEV objects happened to
547/// land in memory.
548///
Dan Gohman0bba49c2009-07-07 17:06:11 +0000549static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops,
Dan Gohman72861302009-05-07 14:39:04 +0000550 LoopInfo *LI) {
Chris Lattner8d741b82004-06-20 06:23:15 +0000551 if (Ops.size() < 2) return; // Noop
552 if (Ops.size() == 2) {
553 // This is the common case, which also happens to be trivially simple.
554 // Special case it.
Dan Gohman72861302009-05-07 14:39:04 +0000555 if (SCEVComplexityCompare(LI)(Ops[1], Ops[0]))
Chris Lattner8d741b82004-06-20 06:23:15 +0000556 std::swap(Ops[0], Ops[1]);
557 return;
558 }
559
560 // Do the rough sort by complexity.
Dan Gohman72861302009-05-07 14:39:04 +0000561 std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI));
Chris Lattner8d741b82004-06-20 06:23:15 +0000562
563 // Now that we are sorted by complexity, group elements of the same
564 // complexity. Note that this is, at worst, N^2, but the vector is likely to
565 // be extremely short in practice. Note that we take this approach because we
566 // do not want to depend on the addresses of the objects we are grouping.
Chris Lattner2d584522004-06-20 17:01:44 +0000567 for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
Dan Gohman35738ac2009-05-04 22:30:44 +0000568 const SCEV *S = Ops[i];
Chris Lattner8d741b82004-06-20 06:23:15 +0000569 unsigned Complexity = S->getSCEVType();
570
571 // If there are any objects of the same complexity and same value as this
572 // one, group them.
573 for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
574 if (Ops[j] == S) { // Found a duplicate.
575 // Move it to immediately after i'th element.
576 std::swap(Ops[i+1], Ops[j]);
577 ++i; // no need to rescan it.
Chris Lattner541ad5e2004-06-20 20:32:16 +0000578 if (i == e-2) return; // Done!
Chris Lattner8d741b82004-06-20 06:23:15 +0000579 }
580 }
581 }
582}
583
Chris Lattner53e677a2004-04-02 20:23:17 +0000584
Chris Lattner53e677a2004-04-02 20:23:17 +0000585
586//===----------------------------------------------------------------------===//
587// Simple SCEV method implementations
588//===----------------------------------------------------------------------===//
589
Eli Friedmanb42a6262008-08-04 23:49:06 +0000590/// BinomialCoefficient - Compute BC(It, K). The result has width W.
Dan Gohman6c0866c2009-05-24 23:45:28 +0000591/// Assume, K > 0.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000592static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K,
Dan Gohmanc2b015e2009-07-21 00:38:55 +0000593 ScalarEvolution &SE,
594 const Type* ResultTy) {
Eli Friedmanb42a6262008-08-04 23:49:06 +0000595 // Handle the simplest case efficiently.
596 if (K == 1)
597 return SE.getTruncateOrZeroExtend(It, ResultTy);
598
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000599 // We are using the following formula for BC(It, K):
600 //
601 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K!
602 //
Eli Friedmanb42a6262008-08-04 23:49:06 +0000603 // Suppose, W is the bitwidth of the return value. We must be prepared for
604 // overflow. Hence, we must assure that the result of our computation is
605 // equal to the accurate one modulo 2^W. Unfortunately, division isn't
606 // safe in modular arithmetic.
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000607 //
Eli Friedmanb42a6262008-08-04 23:49:06 +0000608 // However, this code doesn't use exactly that formula; the formula it uses
Dan Gohman64a845e2009-06-24 04:48:43 +0000609 // is something like the following, where T is the number of factors of 2 in
Eli Friedmanb42a6262008-08-04 23:49:06 +0000610 // K! (i.e. trailing zeros in the binary representation of K!), and ^ is
611 // exponentiation:
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000612 //
Eli Friedmanb42a6262008-08-04 23:49:06 +0000613 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T)
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000614 //
Eli Friedmanb42a6262008-08-04 23:49:06 +0000615 // This formula is trivially equivalent to the previous formula. However,
616 // this formula can be implemented much more efficiently. The trick is that
617 // K! / 2^T is odd, and exact division by an odd number *is* safe in modular
618 // arithmetic. To do exact division in modular arithmetic, all we have
619 // to do is multiply by the inverse. Therefore, this step can be done at
620 // width W.
Dan Gohman64a845e2009-06-24 04:48:43 +0000621 //
Eli Friedmanb42a6262008-08-04 23:49:06 +0000622 // The next issue is how to safely do the division by 2^T. The way this
623 // is done is by doing the multiplication step at a width of at least W + T
624 // bits. This way, the bottom W+T bits of the product are accurate. Then,
625 // when we perform the division by 2^T (which is equivalent to a right shift
626 // by T), the bottom W bits are accurate. Extra bits are okay; they'll get
627 // truncated out after the division by 2^T.
628 //
629 // In comparison to just directly using the first formula, this technique
630 // is much more efficient; using the first formula requires W * K bits,
631 // but this formula less than W + K bits. Also, the first formula requires
632 // a division step, whereas this formula only requires multiplies and shifts.
633 //
634 // It doesn't matter whether the subtraction step is done in the calculation
635 // width or the input iteration count's width; if the subtraction overflows,
636 // the result must be zero anyway. We prefer here to do it in the width of
637 // the induction variable because it helps a lot for certain cases; CodeGen
638 // isn't smart enough to ignore the overflow, which leads to much less
639 // efficient code if the width of the subtraction is wider than the native
640 // register width.
641 //
642 // (It's possible to not widen at all by pulling out factors of 2 before
643 // the multiplication; for example, K=2 can be calculated as
644 // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires
645 // extra arithmetic, so it's not an obvious win, and it gets
646 // much more complicated for K > 3.)
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000647
Eli Friedmanb42a6262008-08-04 23:49:06 +0000648 // Protection from insane SCEVs; this bound is conservative,
649 // but it probably doesn't matter.
650 if (K > 1000)
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +0000651 return SE.getCouldNotCompute();
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000652
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000653 unsigned W = SE.getTypeSizeInBits(ResultTy);
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000654
Eli Friedmanb42a6262008-08-04 23:49:06 +0000655 // Calculate K! / 2^T and T; we divide out the factors of two before
656 // multiplying for calculating K! / 2^T to avoid overflow.
657 // Other overflow doesn't matter because we only care about the bottom
658 // W bits of the result.
659 APInt OddFactorial(W, 1);
660 unsigned T = 1;
661 for (unsigned i = 3; i <= K; ++i) {
662 APInt Mult(W, i);
663 unsigned TwoFactors = Mult.countTrailingZeros();
664 T += TwoFactors;
665 Mult = Mult.lshr(TwoFactors);
666 OddFactorial *= Mult;
Chris Lattner53e677a2004-04-02 20:23:17 +0000667 }
Nick Lewycky6f8abf92008-06-13 04:38:55 +0000668
Eli Friedmanb42a6262008-08-04 23:49:06 +0000669 // We need at least W + T bits for the multiplication step
Nick Lewycky237d8732009-01-25 08:16:27 +0000670 unsigned CalculationBits = W + T;
Eli Friedmanb42a6262008-08-04 23:49:06 +0000671
672 // Calcuate 2^T, at width T+W.
673 APInt DivFactor = APInt(CalculationBits, 1).shl(T);
674
675 // Calculate the multiplicative inverse of K! / 2^T;
676 // this multiplication factor will perform the exact division by
677 // K! / 2^T.
678 APInt Mod = APInt::getSignedMinValue(W+1);
679 APInt MultiplyFactor = OddFactorial.zext(W+1);
680 MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod);
681 MultiplyFactor = MultiplyFactor.trunc(W);
682
683 // Calculate the product, at width T+W
Owen Anderson1d0be152009-08-13 21:58:54 +0000684 const IntegerType *CalculationTy = IntegerType::get(SE.getContext(),
685 CalculationBits);
Dan Gohman0bba49c2009-07-07 17:06:11 +0000686 const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy);
Eli Friedmanb42a6262008-08-04 23:49:06 +0000687 for (unsigned i = 1; i != K; ++i) {
Dan Gohman0bba49c2009-07-07 17:06:11 +0000688 const SCEV *S = SE.getMinusSCEV(It, SE.getIntegerSCEV(i, It->getType()));
Eli Friedmanb42a6262008-08-04 23:49:06 +0000689 Dividend = SE.getMulExpr(Dividend,
690 SE.getTruncateOrZeroExtend(S, CalculationTy));
691 }
692
693 // Divide by 2^T
Dan Gohman0bba49c2009-07-07 17:06:11 +0000694 const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor));
Eli Friedmanb42a6262008-08-04 23:49:06 +0000695
696 // Truncate the result, and divide by K! / 2^T.
697
698 return SE.getMulExpr(SE.getConstant(MultiplyFactor),
699 SE.getTruncateOrZeroExtend(DivResult, ResultTy));
Chris Lattner53e677a2004-04-02 20:23:17 +0000700}
701
Chris Lattner53e677a2004-04-02 20:23:17 +0000702/// evaluateAtIteration - Return the value of this chain of recurrences at
703/// the specified iteration number. We can evaluate this recurrence by
704/// multiplying each element in the chain by the binomial coefficient
705/// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as:
706///
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000707/// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3)
Chris Lattner53e677a2004-04-02 20:23:17 +0000708///
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000709/// where BC(It, k) stands for binomial coefficient.
Chris Lattner53e677a2004-04-02 20:23:17 +0000710///
Dan Gohman0bba49c2009-07-07 17:06:11 +0000711const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It,
Dan Gohmanc2b015e2009-07-21 00:38:55 +0000712 ScalarEvolution &SE) const {
Dan Gohman0bba49c2009-07-07 17:06:11 +0000713 const SCEV *Result = getStart();
Chris Lattner53e677a2004-04-02 20:23:17 +0000714 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Wojciech Matyjewicze3320a12008-02-11 11:03:14 +0000715 // The computation is correct in the face of overflow provided that the
716 // multiplication is performed _after_ the evaluation of the binomial
717 // coefficient.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000718 const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType());
Nick Lewyckycb8f1b52008-10-13 03:58:02 +0000719 if (isa<SCEVCouldNotCompute>(Coeff))
720 return Coeff;
721
722 Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff));
Chris Lattner53e677a2004-04-02 20:23:17 +0000723 }
724 return Result;
725}
726
Chris Lattner53e677a2004-04-02 20:23:17 +0000727//===----------------------------------------------------------------------===//
728// SCEV Expression folder implementations
729//===----------------------------------------------------------------------===//
730
Dan Gohman0bba49c2009-07-07 17:06:11 +0000731const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op,
Dan Gohmanf5074ec2009-07-13 22:05:32 +0000732 const Type *Ty) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000733 assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) &&
Dan Gohmanfb17fd22009-04-21 00:55:22 +0000734 "This is not a truncating conversion!");
Dan Gohman10b94792009-05-01 16:44:18 +0000735 assert(isSCEVable(Ty) &&
736 "This is not a conversion to a SCEVable type!");
737 Ty = getEffectiveSCEVType(Ty);
Dan Gohmanfb17fd22009-04-21 00:55:22 +0000738
Dan Gohmanc050fd92009-07-13 20:50:19 +0000739 FoldingSetNodeID ID;
740 ID.AddInteger(scTruncate);
741 ID.AddPointer(Op);
742 ID.AddPointer(Ty);
743 void *IP = 0;
744 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
745
Dan Gohmanc39f44b2009-06-30 20:13:32 +0000746 // Fold if the operand is constant.
Dan Gohman622ed672009-05-04 22:02:23 +0000747 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
Dan Gohmanb8be8b72009-06-24 00:38:39 +0000748 return getConstant(
749 cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty)));
Chris Lattner53e677a2004-04-02 20:23:17 +0000750
Dan Gohman20900ca2009-04-22 16:20:48 +0000751 // trunc(trunc(x)) --> trunc(x)
Dan Gohman622ed672009-05-04 22:02:23 +0000752 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op))
Dan Gohman20900ca2009-04-22 16:20:48 +0000753 return getTruncateExpr(ST->getOperand(), Ty);
754
Nick Lewycky5cd28fa2009-04-23 05:15:08 +0000755 // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing
Dan Gohman622ed672009-05-04 22:02:23 +0000756 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
Nick Lewycky5cd28fa2009-04-23 05:15:08 +0000757 return getTruncateOrSignExtend(SS->getOperand(), Ty);
758
759 // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing
Dan Gohman622ed672009-05-04 22:02:23 +0000760 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
Nick Lewycky5cd28fa2009-04-23 05:15:08 +0000761 return getTruncateOrZeroExtend(SZ->getOperand(), Ty);
762
Dan Gohman6864db62009-06-18 16:24:47 +0000763 // If the input value is a chrec scev, truncate the chrec's operands.
Dan Gohman622ed672009-05-04 22:02:23 +0000764 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
Dan Gohman0bba49c2009-07-07 17:06:11 +0000765 SmallVector<const SCEV *, 4> Operands;
Chris Lattner53e677a2004-04-02 20:23:17 +0000766 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
Dan Gohman728c7f32009-05-08 21:03:19 +0000767 Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty));
768 return getAddRecExpr(Operands, AddRec->getLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +0000769 }
770
Dan Gohmanc050fd92009-07-13 20:50:19 +0000771 // The cast wasn't folded; create an explicit cast node.
772 // Recompute the insert position, as it may have been invalidated.
Dan Gohman1c343752009-06-27 21:21:31 +0000773 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
774 SCEV *S = SCEVAllocator.Allocate<SCEVTruncateExpr>();
Dan Gohmanc050fd92009-07-13 20:50:19 +0000775 new (S) SCEVTruncateExpr(ID, Op, Ty);
Dan Gohman1c343752009-06-27 21:21:31 +0000776 UniqueSCEVs.InsertNode(S, IP);
777 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +0000778}
779
Dan Gohman0bba49c2009-07-07 17:06:11 +0000780const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op,
Dan Gohmanf5074ec2009-07-13 22:05:32 +0000781 const Type *Ty) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000782 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
Dan Gohman8170a682009-04-16 19:25:55 +0000783 "This is not an extending conversion!");
Dan Gohman10b94792009-05-01 16:44:18 +0000784 assert(isSCEVable(Ty) &&
785 "This is not a conversion to a SCEVable type!");
786 Ty = getEffectiveSCEVType(Ty);
Dan Gohman8170a682009-04-16 19:25:55 +0000787
Dan Gohmanc39f44b2009-06-30 20:13:32 +0000788 // Fold if the operand is constant.
Dan Gohman622ed672009-05-04 22:02:23 +0000789 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000790 const Type *IntTy = getEffectiveSCEVType(Ty);
Dan Gohman2d1be872009-04-16 03:18:22 +0000791 Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy);
792 if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty);
Dan Gohmanb8be8b72009-06-24 00:38:39 +0000793 return getConstant(cast<ConstantInt>(C));
Dan Gohman2d1be872009-04-16 03:18:22 +0000794 }
Chris Lattner53e677a2004-04-02 20:23:17 +0000795
Dan Gohman20900ca2009-04-22 16:20:48 +0000796 // zext(zext(x)) --> zext(x)
Dan Gohman622ed672009-05-04 22:02:23 +0000797 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
Dan Gohman20900ca2009-04-22 16:20:48 +0000798 return getZeroExtendExpr(SZ->getOperand(), Ty);
799
Dan Gohman69fbc7f2009-07-13 20:55:53 +0000800 // Before doing any expensive analysis, check to see if we've already
801 // computed a SCEV for this Op and Ty.
802 FoldingSetNodeID ID;
803 ID.AddInteger(scZeroExtend);
804 ID.AddPointer(Op);
805 ID.AddPointer(Ty);
806 void *IP = 0;
807 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
808
Dan Gohman01ecca22009-04-27 20:16:15 +0000809 // If the input value is a chrec scev, and we can prove that the value
Chris Lattner53e677a2004-04-02 20:23:17 +0000810 // did not overflow the old, smaller, value, we can zero extend all of the
Dan Gohman01ecca22009-04-27 20:16:15 +0000811 // operands (often constants). This allows analysis of something like
Chris Lattner53e677a2004-04-02 20:23:17 +0000812 // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
Dan Gohman622ed672009-05-04 22:02:23 +0000813 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
Dan Gohman01ecca22009-04-27 20:16:15 +0000814 if (AR->isAffine()) {
Dan Gohman85b05a22009-07-13 21:35:55 +0000815 const SCEV *Start = AR->getStart();
816 const SCEV *Step = AR->getStepRecurrence(*this);
817 unsigned BitWidth = getTypeSizeInBits(AR->getType());
818 const Loop *L = AR->getLoop();
819
Dan Gohmaneb490a72009-07-25 01:22:26 +0000820 // If we have special knowledge that this addrec won't overflow,
821 // we don't need to do any further analysis.
Dan Gohman5078f842009-08-20 17:11:38 +0000822 if (AR->hasNoUnsignedWrap())
Dan Gohmaneb490a72009-07-25 01:22:26 +0000823 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
824 getZeroExtendExpr(Step, Ty),
825 L);
826
Dan Gohman01ecca22009-04-27 20:16:15 +0000827 // Check whether the backedge-taken count is SCEVCouldNotCompute.
828 // Note that this serves two purposes: It filters out loops that are
829 // simply not analyzable, and it covers the case where this code is
830 // being called from within backedge-taken count analysis, such that
831 // attempting to ask for the backedge-taken count would likely result
832 // in infinite recursion. In the later case, the analysis code will
833 // cope with a conservative value, and it will take care to purge
834 // that value once it has finished.
Dan Gohman85b05a22009-07-13 21:35:55 +0000835 const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
Dan Gohmana1af7572009-04-30 20:47:05 +0000836 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
Dan Gohmanf0aa4852009-04-29 01:54:20 +0000837 // Manually compute the final value for AR, checking for
Dan Gohmanac70cea2009-04-29 22:28:28 +0000838 // overflow.
Dan Gohman01ecca22009-04-27 20:16:15 +0000839
840 // Check whether the backedge-taken count can be losslessly casted to
841 // the addrec's type. The count is always unsigned.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000842 const SCEV *CastedMaxBECount =
Dan Gohmana1af7572009-04-30 20:47:05 +0000843 getTruncateOrZeroExtend(MaxBECount, Start->getType());
Dan Gohman0bba49c2009-07-07 17:06:11 +0000844 const SCEV *RecastedMaxBECount =
Dan Gohman5183cae2009-05-18 15:58:39 +0000845 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
846 if (MaxBECount == RecastedMaxBECount) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000847 const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
Dan Gohmana1af7572009-04-30 20:47:05 +0000848 // Check whether Start+Step*MaxBECount has no unsigned overflow.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000849 const SCEV *ZMul =
Dan Gohmana1af7572009-04-30 20:47:05 +0000850 getMulExpr(CastedMaxBECount,
Dan Gohman01ecca22009-04-27 20:16:15 +0000851 getTruncateOrZeroExtend(Step, Start->getType()));
Dan Gohman0bba49c2009-07-07 17:06:11 +0000852 const SCEV *Add = getAddExpr(Start, ZMul);
853 const SCEV *OperandExtendedAdd =
Dan Gohman5183cae2009-05-18 15:58:39 +0000854 getAddExpr(getZeroExtendExpr(Start, WideTy),
855 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
856 getZeroExtendExpr(Step, WideTy)));
857 if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohmanac70cea2009-04-29 22:28:28 +0000858 // Return the expression with the addrec on the outside.
859 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
860 getZeroExtendExpr(Step, Ty),
Dan Gohman85b05a22009-07-13 21:35:55 +0000861 L);
Dan Gohman01ecca22009-04-27 20:16:15 +0000862
863 // Similar to above, only this time treat the step value as signed.
864 // This covers loops that count down.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000865 const SCEV *SMul =
Dan Gohmana1af7572009-04-30 20:47:05 +0000866 getMulExpr(CastedMaxBECount,
Dan Gohman01ecca22009-04-27 20:16:15 +0000867 getTruncateOrSignExtend(Step, Start->getType()));
Dan Gohmanac70cea2009-04-29 22:28:28 +0000868 Add = getAddExpr(Start, SMul);
Dan Gohman5183cae2009-05-18 15:58:39 +0000869 OperandExtendedAdd =
870 getAddExpr(getZeroExtendExpr(Start, WideTy),
871 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
872 getSignExtendExpr(Step, WideTy)));
873 if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohmanac70cea2009-04-29 22:28:28 +0000874 // Return the expression with the addrec on the outside.
875 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
876 getSignExtendExpr(Step, Ty),
Dan Gohman85b05a22009-07-13 21:35:55 +0000877 L);
878 }
879
880 // If the backedge is guarded by a comparison with the pre-inc value
881 // the addrec is safe. Also, if the entry is guarded by a comparison
882 // with the start value and the backedge is guarded by a comparison
883 // with the post-inc value, the addrec is safe.
884 if (isKnownPositive(Step)) {
885 const SCEV *N = getConstant(APInt::getMinValue(BitWidth) -
886 getUnsignedRange(Step).getUnsignedMax());
887 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) ||
888 (isLoopGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) &&
889 isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT,
890 AR->getPostIncExpr(*this), N)))
891 // Return the expression with the addrec on the outside.
892 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
893 getZeroExtendExpr(Step, Ty),
894 L);
895 } else if (isKnownNegative(Step)) {
896 const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) -
897 getSignedRange(Step).getSignedMin());
898 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) &&
899 (isLoopGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) ||
900 isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT,
901 AR->getPostIncExpr(*this), N)))
902 // Return the expression with the addrec on the outside.
903 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
904 getSignExtendExpr(Step, Ty),
905 L);
Dan Gohman01ecca22009-04-27 20:16:15 +0000906 }
907 }
908 }
Chris Lattner53e677a2004-04-02 20:23:17 +0000909
Dan Gohman69fbc7f2009-07-13 20:55:53 +0000910 // The cast wasn't folded; create an explicit cast node.
911 // Recompute the insert position, as it may have been invalidated.
Dan Gohman1c343752009-06-27 21:21:31 +0000912 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
913 SCEV *S = SCEVAllocator.Allocate<SCEVZeroExtendExpr>();
Dan Gohmanc050fd92009-07-13 20:50:19 +0000914 new (S) SCEVZeroExtendExpr(ID, Op, Ty);
Dan Gohman1c343752009-06-27 21:21:31 +0000915 UniqueSCEVs.InsertNode(S, IP);
916 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +0000917}
918
Dan Gohman0bba49c2009-07-07 17:06:11 +0000919const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op,
Dan Gohmanf5074ec2009-07-13 22:05:32 +0000920 const Type *Ty) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000921 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
Dan Gohmanfb17fd22009-04-21 00:55:22 +0000922 "This is not an extending conversion!");
Dan Gohman10b94792009-05-01 16:44:18 +0000923 assert(isSCEVable(Ty) &&
924 "This is not a conversion to a SCEVable type!");
925 Ty = getEffectiveSCEVType(Ty);
Dan Gohmanfb17fd22009-04-21 00:55:22 +0000926
Dan Gohmanc39f44b2009-06-30 20:13:32 +0000927 // Fold if the operand is constant.
Dan Gohman622ed672009-05-04 22:02:23 +0000928 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000929 const Type *IntTy = getEffectiveSCEVType(Ty);
Dan Gohman2d1be872009-04-16 03:18:22 +0000930 Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy);
931 if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty);
Dan Gohmanb8be8b72009-06-24 00:38:39 +0000932 return getConstant(cast<ConstantInt>(C));
Dan Gohman2d1be872009-04-16 03:18:22 +0000933 }
Dan Gohmand19534a2007-06-15 14:38:12 +0000934
Dan Gohman20900ca2009-04-22 16:20:48 +0000935 // sext(sext(x)) --> sext(x)
Dan Gohman622ed672009-05-04 22:02:23 +0000936 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
Dan Gohman20900ca2009-04-22 16:20:48 +0000937 return getSignExtendExpr(SS->getOperand(), Ty);
938
Dan Gohman69fbc7f2009-07-13 20:55:53 +0000939 // Before doing any expensive analysis, check to see if we've already
940 // computed a SCEV for this Op and Ty.
941 FoldingSetNodeID ID;
942 ID.AddInteger(scSignExtend);
943 ID.AddPointer(Op);
944 ID.AddPointer(Ty);
945 void *IP = 0;
946 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
947
Dan Gohman01ecca22009-04-27 20:16:15 +0000948 // If the input value is a chrec scev, and we can prove that the value
Dan Gohmand19534a2007-06-15 14:38:12 +0000949 // did not overflow the old, smaller, value, we can sign extend all of the
Dan Gohman01ecca22009-04-27 20:16:15 +0000950 // operands (often constants). This allows analysis of something like
Dan Gohmand19534a2007-06-15 14:38:12 +0000951 // this: for (signed char X = 0; X < 100; ++X) { int Y = X; }
Dan Gohman622ed672009-05-04 22:02:23 +0000952 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
Dan Gohman01ecca22009-04-27 20:16:15 +0000953 if (AR->isAffine()) {
Dan Gohman85b05a22009-07-13 21:35:55 +0000954 const SCEV *Start = AR->getStart();
955 const SCEV *Step = AR->getStepRecurrence(*this);
956 unsigned BitWidth = getTypeSizeInBits(AR->getType());
957 const Loop *L = AR->getLoop();
958
Dan Gohmaneb490a72009-07-25 01:22:26 +0000959 // If we have special knowledge that this addrec won't overflow,
960 // we don't need to do any further analysis.
Dan Gohman5078f842009-08-20 17:11:38 +0000961 if (AR->hasNoSignedWrap())
Dan Gohmaneb490a72009-07-25 01:22:26 +0000962 return getAddRecExpr(getSignExtendExpr(Start, Ty),
963 getSignExtendExpr(Step, Ty),
964 L);
965
Dan Gohman01ecca22009-04-27 20:16:15 +0000966 // Check whether the backedge-taken count is SCEVCouldNotCompute.
967 // Note that this serves two purposes: It filters out loops that are
968 // simply not analyzable, and it covers the case where this code is
969 // being called from within backedge-taken count analysis, such that
970 // attempting to ask for the backedge-taken count would likely result
971 // in infinite recursion. In the later case, the analysis code will
972 // cope with a conservative value, and it will take care to purge
973 // that value once it has finished.
Dan Gohman85b05a22009-07-13 21:35:55 +0000974 const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
Dan Gohmana1af7572009-04-30 20:47:05 +0000975 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
Dan Gohmanf0aa4852009-04-29 01:54:20 +0000976 // Manually compute the final value for AR, checking for
Dan Gohmanac70cea2009-04-29 22:28:28 +0000977 // overflow.
Dan Gohman01ecca22009-04-27 20:16:15 +0000978
979 // Check whether the backedge-taken count can be losslessly casted to
Dan Gohmanac70cea2009-04-29 22:28:28 +0000980 // the addrec's type. The count is always unsigned.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000981 const SCEV *CastedMaxBECount =
Dan Gohmana1af7572009-04-30 20:47:05 +0000982 getTruncateOrZeroExtend(MaxBECount, Start->getType());
Dan Gohman0bba49c2009-07-07 17:06:11 +0000983 const SCEV *RecastedMaxBECount =
Dan Gohman5183cae2009-05-18 15:58:39 +0000984 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
985 if (MaxBECount == RecastedMaxBECount) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000986 const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
Dan Gohmana1af7572009-04-30 20:47:05 +0000987 // Check whether Start+Step*MaxBECount has no signed overflow.
Dan Gohman0bba49c2009-07-07 17:06:11 +0000988 const SCEV *SMul =
Dan Gohmana1af7572009-04-30 20:47:05 +0000989 getMulExpr(CastedMaxBECount,
Dan Gohman01ecca22009-04-27 20:16:15 +0000990 getTruncateOrSignExtend(Step, Start->getType()));
Dan Gohman0bba49c2009-07-07 17:06:11 +0000991 const SCEV *Add = getAddExpr(Start, SMul);
992 const SCEV *OperandExtendedAdd =
Dan Gohman5183cae2009-05-18 15:58:39 +0000993 getAddExpr(getSignExtendExpr(Start, WideTy),
994 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
995 getSignExtendExpr(Step, WideTy)));
996 if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohmanac70cea2009-04-29 22:28:28 +0000997 // Return the expression with the addrec on the outside.
998 return getAddRecExpr(getSignExtendExpr(Start, Ty),
999 getSignExtendExpr(Step, Ty),
Dan Gohman85b05a22009-07-13 21:35:55 +00001000 L);
Dan Gohman850f7912009-07-16 17:34:36 +00001001
1002 // Similar to above, only this time treat the step value as unsigned.
1003 // This covers loops that count up with an unsigned step.
1004 const SCEV *UMul =
1005 getMulExpr(CastedMaxBECount,
1006 getTruncateOrZeroExtend(Step, Start->getType()));
1007 Add = getAddExpr(Start, UMul);
1008 OperandExtendedAdd =
Dan Gohman19378d62009-07-25 16:03:30 +00001009 getAddExpr(getSignExtendExpr(Start, WideTy),
Dan Gohman850f7912009-07-16 17:34:36 +00001010 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
1011 getZeroExtendExpr(Step, WideTy)));
Dan Gohman19378d62009-07-25 16:03:30 +00001012 if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohman850f7912009-07-16 17:34:36 +00001013 // Return the expression with the addrec on the outside.
1014 return getAddRecExpr(getSignExtendExpr(Start, Ty),
1015 getZeroExtendExpr(Step, Ty),
1016 L);
Dan Gohman85b05a22009-07-13 21:35:55 +00001017 }
1018
1019 // If the backedge is guarded by a comparison with the pre-inc value
1020 // the addrec is safe. Also, if the entry is guarded by a comparison
1021 // with the start value and the backedge is guarded by a comparison
1022 // with the post-inc value, the addrec is safe.
1023 if (isKnownPositive(Step)) {
1024 const SCEV *N = getConstant(APInt::getSignedMinValue(BitWidth) -
1025 getSignedRange(Step).getSignedMax());
1026 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, AR, N) ||
1027 (isLoopGuardedByCond(L, ICmpInst::ICMP_SLT, Start, N) &&
1028 isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT,
1029 AR->getPostIncExpr(*this), N)))
1030 // Return the expression with the addrec on the outside.
1031 return getAddRecExpr(getSignExtendExpr(Start, Ty),
1032 getSignExtendExpr(Step, Ty),
1033 L);
1034 } else if (isKnownNegative(Step)) {
1035 const SCEV *N = getConstant(APInt::getSignedMaxValue(BitWidth) -
1036 getSignedRange(Step).getSignedMin());
1037 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, AR, N) ||
1038 (isLoopGuardedByCond(L, ICmpInst::ICMP_SGT, Start, N) &&
1039 isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT,
1040 AR->getPostIncExpr(*this), N)))
1041 // Return the expression with the addrec on the outside.
1042 return getAddRecExpr(getSignExtendExpr(Start, Ty),
1043 getSignExtendExpr(Step, Ty),
1044 L);
Dan Gohman01ecca22009-04-27 20:16:15 +00001045 }
1046 }
1047 }
Dan Gohmand19534a2007-06-15 14:38:12 +00001048
Dan Gohman69fbc7f2009-07-13 20:55:53 +00001049 // The cast wasn't folded; create an explicit cast node.
1050 // Recompute the insert position, as it may have been invalidated.
Dan Gohman1c343752009-06-27 21:21:31 +00001051 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1052 SCEV *S = SCEVAllocator.Allocate<SCEVSignExtendExpr>();
Dan Gohmanc050fd92009-07-13 20:50:19 +00001053 new (S) SCEVSignExtendExpr(ID, Op, Ty);
Dan Gohman1c343752009-06-27 21:21:31 +00001054 UniqueSCEVs.InsertNode(S, IP);
1055 return S;
Dan Gohmand19534a2007-06-15 14:38:12 +00001056}
1057
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00001058/// getAnyExtendExpr - Return a SCEV for the given operand extended with
1059/// unspecified bits out to the given type.
1060///
Dan Gohman0bba49c2009-07-07 17:06:11 +00001061const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op,
Dan Gohmanc40f17b2009-08-18 16:46:41 +00001062 const Type *Ty) {
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00001063 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1064 "This is not an extending conversion!");
1065 assert(isSCEVable(Ty) &&
1066 "This is not a conversion to a SCEVable type!");
1067 Ty = getEffectiveSCEVType(Ty);
1068
1069 // Sign-extend negative constants.
1070 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1071 if (SC->getValue()->getValue().isNegative())
1072 return getSignExtendExpr(Op, Ty);
1073
1074 // Peel off a truncate cast.
1075 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001076 const SCEV *NewOp = T->getOperand();
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00001077 if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty))
1078 return getAnyExtendExpr(NewOp, Ty);
1079 return getTruncateOrNoop(NewOp, Ty);
1080 }
1081
1082 // Next try a zext cast. If the cast is folded, use it.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001083 const SCEV *ZExt = getZeroExtendExpr(Op, Ty);
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00001084 if (!isa<SCEVZeroExtendExpr>(ZExt))
1085 return ZExt;
1086
1087 // Next try a sext cast. If the cast is folded, use it.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001088 const SCEV *SExt = getSignExtendExpr(Op, Ty);
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00001089 if (!isa<SCEVSignExtendExpr>(SExt))
1090 return SExt;
1091
1092 // If the expression is obviously signed, use the sext cast value.
1093 if (isa<SCEVSMaxExpr>(Op))
1094 return SExt;
1095
1096 // Absent any other information, use the zext cast value.
1097 return ZExt;
1098}
1099
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001100/// CollectAddOperandsWithScales - Process the given Ops list, which is
1101/// a list of operands to be added under the given scale, update the given
1102/// map. This is a helper function for getAddRecExpr. As an example of
1103/// what it does, given a sequence of operands that would form an add
1104/// expression like this:
1105///
1106/// m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r)
1107///
1108/// where A and B are constants, update the map with these values:
1109///
1110/// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0)
1111///
1112/// and add 13 + A*B*29 to AccumulatedConstant.
1113/// This will allow getAddRecExpr to produce this:
1114///
1115/// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B)
1116///
1117/// This form often exposes folding opportunities that are hidden in
1118/// the original operand list.
1119///
1120/// Return true iff it appears that any interesting folding opportunities
1121/// may be exposed. This helps getAddRecExpr short-circuit extra work in
1122/// the common case where no interesting opportunities are present, and
1123/// is also used as a check to avoid infinite recursion.
1124///
1125static bool
Dan Gohman0bba49c2009-07-07 17:06:11 +00001126CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M,
1127 SmallVector<const SCEV *, 8> &NewOps,
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001128 APInt &AccumulatedConstant,
Dan Gohman0bba49c2009-07-07 17:06:11 +00001129 const SmallVectorImpl<const SCEV *> &Ops,
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001130 const APInt &Scale,
1131 ScalarEvolution &SE) {
1132 bool Interesting = false;
1133
1134 // Iterate over the add operands.
1135 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1136 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]);
1137 if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) {
1138 APInt NewScale =
1139 Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue();
1140 if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) {
1141 // A multiplication of a constant with another add; recurse.
1142 Interesting |=
1143 CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
1144 cast<SCEVAddExpr>(Mul->getOperand(1))
1145 ->getOperands(),
1146 NewScale, SE);
1147 } else {
1148 // A multiplication of a constant with some other value. Update
1149 // the map.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001150 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end());
1151 const SCEV *Key = SE.getMulExpr(MulOps);
1152 std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
Dan Gohman23737e02009-06-29 18:25:52 +00001153 M.insert(std::make_pair(Key, NewScale));
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001154 if (Pair.second) {
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001155 NewOps.push_back(Pair.first->first);
1156 } else {
1157 Pair.first->second += NewScale;
1158 // The map already had an entry for this value, which may indicate
1159 // a folding opportunity.
1160 Interesting = true;
1161 }
1162 }
1163 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
1164 // Pull a buried constant out to the outside.
1165 if (Scale != 1 || AccumulatedConstant != 0 || C->isZero())
1166 Interesting = true;
1167 AccumulatedConstant += Scale * C->getValue()->getValue();
1168 } else {
1169 // An ordinary operand. Update the map.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001170 std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
Dan Gohman23737e02009-06-29 18:25:52 +00001171 M.insert(std::make_pair(Ops[i], Scale));
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001172 if (Pair.second) {
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001173 NewOps.push_back(Pair.first->first);
1174 } else {
1175 Pair.first->second += Scale;
1176 // The map already had an entry for this value, which may indicate
1177 // a folding opportunity.
1178 Interesting = true;
1179 }
1180 }
1181 }
1182
1183 return Interesting;
1184}
1185
1186namespace {
1187 struct APIntCompare {
1188 bool operator()(const APInt &LHS, const APInt &RHS) const {
1189 return LHS.ult(RHS);
1190 }
1191 };
1192}
1193
Dan Gohman6c0866c2009-05-24 23:45:28 +00001194/// getAddExpr - Get a canonical add expression, or something simpler if
1195/// possible.
Dan Gohman3645b012009-10-09 00:10:36 +00001196const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
1197 bool HasNUW, bool HasNSW) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001198 assert(!Ops.empty() && "Cannot get empty add!");
Chris Lattner627018b2004-04-07 16:16:11 +00001199 if (Ops.size() == 1) return Ops[0];
Dan Gohmanf78a9782009-05-18 15:44:58 +00001200#ifndef NDEBUG
1201 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1202 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1203 getEffectiveSCEVType(Ops[0]->getType()) &&
1204 "SCEVAddExpr operand types don't match!");
1205#endif
Chris Lattner53e677a2004-04-02 20:23:17 +00001206
1207 // Sort by complexity, this groups all similar expression types together.
Dan Gohman72861302009-05-07 14:39:04 +00001208 GroupByComplexity(Ops, LI);
Chris Lattner53e677a2004-04-02 20:23:17 +00001209
1210 // If there are any constants, fold them together.
1211 unsigned Idx = 0;
Dan Gohman622ed672009-05-04 22:02:23 +00001212 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001213 ++Idx;
Chris Lattner627018b2004-04-07 16:16:11 +00001214 assert(Idx < Ops.size());
Dan Gohman622ed672009-05-04 22:02:23 +00001215 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001216 // We found two constants, fold them together!
Dan Gohmana82752c2009-06-14 22:47:23 +00001217 Ops[0] = getConstant(LHSC->getValue()->getValue() +
1218 RHSC->getValue()->getValue());
Dan Gohman7f7c4362009-06-14 22:53:57 +00001219 if (Ops.size() == 2) return Ops[0];
Nick Lewycky3e630762008-02-20 06:48:22 +00001220 Ops.erase(Ops.begin()+1); // Erase the folded element
Nick Lewycky3e630762008-02-20 06:48:22 +00001221 LHSC = cast<SCEVConstant>(Ops[0]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001222 }
1223
1224 // If we are left with a constant zero being added, strip it off.
Reid Spencercae57542007-03-02 00:28:52 +00001225 if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001226 Ops.erase(Ops.begin());
1227 --Idx;
1228 }
1229 }
1230
Chris Lattner627018b2004-04-07 16:16:11 +00001231 if (Ops.size() == 1) return Ops[0];
Misha Brukman2b37d7c2005-04-21 21:13:18 +00001232
Chris Lattner53e677a2004-04-02 20:23:17 +00001233 // Okay, check to see if the same value occurs in the operand list twice. If
1234 // so, merge them together into an multiply expression. Since we sorted the
1235 // list, these values are required to be adjacent.
1236 const Type *Ty = Ops[0]->getType();
1237 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
1238 if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2
1239 // Found a match, merge the two values into a multiply, and add any
1240 // remaining values to the result.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001241 const SCEV *Two = getIntegerSCEV(2, Ty);
1242 const SCEV *Mul = getMulExpr(Ops[i], Two);
Chris Lattner53e677a2004-04-02 20:23:17 +00001243 if (Ops.size() == 2)
1244 return Mul;
1245 Ops.erase(Ops.begin()+i, Ops.begin()+i+2);
1246 Ops.push_back(Mul);
Dan Gohman3645b012009-10-09 00:10:36 +00001247 return getAddExpr(Ops, HasNUW, HasNSW);
Chris Lattner53e677a2004-04-02 20:23:17 +00001248 }
1249
Dan Gohman728c7f32009-05-08 21:03:19 +00001250 // Check for truncates. If all the operands are truncated from the same
1251 // type, see if factoring out the truncate would permit the result to be
1252 // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n)
1253 // if the contents of the resulting outer trunc fold to something simple.
1254 for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) {
1255 const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]);
1256 const Type *DstType = Trunc->getType();
1257 const Type *SrcType = Trunc->getOperand()->getType();
Dan Gohman0bba49c2009-07-07 17:06:11 +00001258 SmallVector<const SCEV *, 8> LargeOps;
Dan Gohman728c7f32009-05-08 21:03:19 +00001259 bool Ok = true;
1260 // Check all the operands to see if they can be represented in the
1261 // source type of the truncate.
1262 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1263 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) {
1264 if (T->getOperand()->getType() != SrcType) {
1265 Ok = false;
1266 break;
1267 }
1268 LargeOps.push_back(T->getOperand());
1269 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
1270 // This could be either sign or zero extension, but sign extension
1271 // is much more likely to be foldable here.
1272 LargeOps.push_back(getSignExtendExpr(C, SrcType));
1273 } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001274 SmallVector<const SCEV *, 8> LargeMulOps;
Dan Gohman728c7f32009-05-08 21:03:19 +00001275 for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
1276 if (const SCEVTruncateExpr *T =
1277 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) {
1278 if (T->getOperand()->getType() != SrcType) {
1279 Ok = false;
1280 break;
1281 }
1282 LargeMulOps.push_back(T->getOperand());
1283 } else if (const SCEVConstant *C =
1284 dyn_cast<SCEVConstant>(M->getOperand(j))) {
1285 // This could be either sign or zero extension, but sign extension
1286 // is much more likely to be foldable here.
1287 LargeMulOps.push_back(getSignExtendExpr(C, SrcType));
1288 } else {
1289 Ok = false;
1290 break;
1291 }
1292 }
1293 if (Ok)
1294 LargeOps.push_back(getMulExpr(LargeMulOps));
1295 } else {
1296 Ok = false;
1297 break;
1298 }
1299 }
1300 if (Ok) {
1301 // Evaluate the expression in the larger type.
Dan Gohman3645b012009-10-09 00:10:36 +00001302 const SCEV *Fold = getAddExpr(LargeOps, HasNUW, HasNSW);
Dan Gohman728c7f32009-05-08 21:03:19 +00001303 // If it folds to something simple, use it. Otherwise, don't.
1304 if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold))
1305 return getTruncateExpr(Fold, DstType);
1306 }
1307 }
1308
1309 // Skip past any other cast SCEVs.
Dan Gohmanf50cd742007-06-18 19:30:09 +00001310 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr)
1311 ++Idx;
1312
1313 // If there are add operands they would be next.
Chris Lattner53e677a2004-04-02 20:23:17 +00001314 if (Idx < Ops.size()) {
1315 bool DeletedAdd = false;
Dan Gohman622ed672009-05-04 22:02:23 +00001316 while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001317 // If we have an add, expand the add operands onto the end of the operands
1318 // list.
1319 Ops.insert(Ops.end(), Add->op_begin(), Add->op_end());
1320 Ops.erase(Ops.begin()+Idx);
1321 DeletedAdd = true;
1322 }
1323
1324 // If we deleted at least one add, we added operands to the end of the list,
1325 // and they are not necessarily sorted. Recurse to resort and resimplify
1326 // any operands we just aquired.
1327 if (DeletedAdd)
Dan Gohman246b2562007-10-22 18:31:58 +00001328 return getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001329 }
1330
1331 // Skip over the add expression until we get to a multiply.
1332 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
1333 ++Idx;
1334
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001335 // Check to see if there are any folding opportunities present with
1336 // operands multiplied by constant values.
1337 if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) {
1338 uint64_t BitWidth = getTypeSizeInBits(Ty);
Dan Gohman0bba49c2009-07-07 17:06:11 +00001339 DenseMap<const SCEV *, APInt> M;
1340 SmallVector<const SCEV *, 8> NewOps;
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001341 APInt AccumulatedConstant(BitWidth, 0);
1342 if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
1343 Ops, APInt(BitWidth, 1), *this)) {
1344 // Some interesting folding opportunity is present, so its worthwhile to
1345 // re-generate the operands list. Group the operands by constant scale,
1346 // to avoid multiplying by the same constant scale multiple times.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001347 std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists;
1348 for (SmallVector<const SCEV *, 8>::iterator I = NewOps.begin(),
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001349 E = NewOps.end(); I != E; ++I)
1350 MulOpLists[M.find(*I)->second].push_back(*I);
1351 // Re-generate the operands list.
1352 Ops.clear();
1353 if (AccumulatedConstant != 0)
1354 Ops.push_back(getConstant(AccumulatedConstant));
Dan Gohman64a845e2009-06-24 04:48:43 +00001355 for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator
1356 I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I)
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001357 if (I->first != 0)
Dan Gohman64a845e2009-06-24 04:48:43 +00001358 Ops.push_back(getMulExpr(getConstant(I->first),
1359 getAddExpr(I->second)));
Dan Gohmanbd59d7b2009-06-14 22:58:51 +00001360 if (Ops.empty())
1361 return getIntegerSCEV(0, Ty);
1362 if (Ops.size() == 1)
1363 return Ops[0];
1364 return getAddExpr(Ops);
1365 }
1366 }
1367
Chris Lattner53e677a2004-04-02 20:23:17 +00001368 // If we are adding something to a multiply expression, make sure the
1369 // something is not already an operand of the multiply. If so, merge it into
1370 // the multiply.
1371 for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001372 const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001373 for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001374 const SCEV *MulOpSCEV = Mul->getOperand(MulOp);
Chris Lattner53e677a2004-04-02 20:23:17 +00001375 for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
Dan Gohmana82752c2009-06-14 22:47:23 +00001376 if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(Ops[AddOp])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001377 // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1))
Dan Gohman0bba49c2009-07-07 17:06:11 +00001378 const SCEV *InnerMul = Mul->getOperand(MulOp == 0);
Chris Lattner53e677a2004-04-02 20:23:17 +00001379 if (Mul->getNumOperands() != 2) {
1380 // If the multiply has more than two operands, we must get the
1381 // Y*Z term.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001382 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), Mul->op_end());
Chris Lattner53e677a2004-04-02 20:23:17 +00001383 MulOps.erase(MulOps.begin()+MulOp);
Dan Gohman246b2562007-10-22 18:31:58 +00001384 InnerMul = getMulExpr(MulOps);
Chris Lattner53e677a2004-04-02 20:23:17 +00001385 }
Dan Gohman0bba49c2009-07-07 17:06:11 +00001386 const SCEV *One = getIntegerSCEV(1, Ty);
1387 const SCEV *AddOne = getAddExpr(InnerMul, One);
1388 const SCEV *OuterMul = getMulExpr(AddOne, Ops[AddOp]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001389 if (Ops.size() == 2) return OuterMul;
1390 if (AddOp < Idx) {
1391 Ops.erase(Ops.begin()+AddOp);
1392 Ops.erase(Ops.begin()+Idx-1);
1393 } else {
1394 Ops.erase(Ops.begin()+Idx);
1395 Ops.erase(Ops.begin()+AddOp-1);
1396 }
1397 Ops.push_back(OuterMul);
Dan Gohman246b2562007-10-22 18:31:58 +00001398 return getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001399 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00001400
Chris Lattner53e677a2004-04-02 20:23:17 +00001401 // Check this multiply against other multiplies being added together.
1402 for (unsigned OtherMulIdx = Idx+1;
1403 OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
1404 ++OtherMulIdx) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001405 const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001406 // If MulOp occurs in OtherMul, we can fold the two multiplies
1407 // together.
1408 for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
1409 OMulOp != e; ++OMulOp)
1410 if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
1411 // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
Dan Gohman0bba49c2009-07-07 17:06:11 +00001412 const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0);
Chris Lattner53e677a2004-04-02 20:23:17 +00001413 if (Mul->getNumOperands() != 2) {
Dan Gohman64a845e2009-06-24 04:48:43 +00001414 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
1415 Mul->op_end());
Chris Lattner53e677a2004-04-02 20:23:17 +00001416 MulOps.erase(MulOps.begin()+MulOp);
Dan Gohman246b2562007-10-22 18:31:58 +00001417 InnerMul1 = getMulExpr(MulOps);
Chris Lattner53e677a2004-04-02 20:23:17 +00001418 }
Dan Gohman0bba49c2009-07-07 17:06:11 +00001419 const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0);
Chris Lattner53e677a2004-04-02 20:23:17 +00001420 if (OtherMul->getNumOperands() != 2) {
Dan Gohman64a845e2009-06-24 04:48:43 +00001421 SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(),
1422 OtherMul->op_end());
Chris Lattner53e677a2004-04-02 20:23:17 +00001423 MulOps.erase(MulOps.begin()+OMulOp);
Dan Gohman246b2562007-10-22 18:31:58 +00001424 InnerMul2 = getMulExpr(MulOps);
Chris Lattner53e677a2004-04-02 20:23:17 +00001425 }
Dan Gohman0bba49c2009-07-07 17:06:11 +00001426 const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2);
1427 const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum);
Chris Lattner53e677a2004-04-02 20:23:17 +00001428 if (Ops.size() == 2) return OuterMul;
1429 Ops.erase(Ops.begin()+Idx);
1430 Ops.erase(Ops.begin()+OtherMulIdx-1);
1431 Ops.push_back(OuterMul);
Dan Gohman246b2562007-10-22 18:31:58 +00001432 return getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001433 }
1434 }
1435 }
1436 }
1437
1438 // If there are any add recurrences in the operands list, see if any other
1439 // added values are loop invariant. If so, we can fold them into the
1440 // recurrence.
1441 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
1442 ++Idx;
1443
1444 // Scan over all recurrences, trying to fold loop invariants into them.
1445 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
1446 // Scan all of the other operands to this add and add them to the vector if
1447 // they are loop invariant w.r.t. the recurrence.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001448 SmallVector<const SCEV *, 8> LIOps;
Dan Gohman35738ac2009-05-04 22:30:44 +00001449 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001450 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1451 if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
1452 LIOps.push_back(Ops[i]);
1453 Ops.erase(Ops.begin()+i);
1454 --i; --e;
1455 }
1456
1457 // If we found some loop invariants, fold them into the recurrence.
1458 if (!LIOps.empty()) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001459 // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step}
Chris Lattner53e677a2004-04-02 20:23:17 +00001460 LIOps.push_back(AddRec->getStart());
1461
Dan Gohman0bba49c2009-07-07 17:06:11 +00001462 SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(),
Dan Gohman3a5d4092009-12-18 03:57:04 +00001463 AddRec->op_end());
Dan Gohman246b2562007-10-22 18:31:58 +00001464 AddRecOps[0] = getAddExpr(LIOps);
Chris Lattner53e677a2004-04-02 20:23:17 +00001465
Dan Gohman355b4f32009-12-19 01:46:34 +00001466 // It's tempting to propagate NUW/NSW flags here, but nuw/nsw addition
Dan Gohman59de33e2009-12-18 18:45:31 +00001467 // is not associative so this isn't necessarily safe.
Dan Gohman3a5d4092009-12-18 03:57:04 +00001468 const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRec->getLoop());
Dan Gohman59de33e2009-12-18 18:45:31 +00001469
Chris Lattner53e677a2004-04-02 20:23:17 +00001470 // If all of the other operands were loop invariant, we are done.
1471 if (Ops.size() == 1) return NewRec;
1472
1473 // Otherwise, add the folded AddRec by the non-liv parts.
1474 for (unsigned i = 0;; ++i)
1475 if (Ops[i] == AddRec) {
1476 Ops[i] = NewRec;
1477 break;
1478 }
Dan Gohman246b2562007-10-22 18:31:58 +00001479 return getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001480 }
1481
1482 // Okay, if there weren't any loop invariants to be folded, check to see if
1483 // there are multiple AddRec's with the same loop induction variable being
1484 // added together. If so, we can fold them.
1485 for (unsigned OtherIdx = Idx+1;
1486 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
1487 if (OtherIdx != Idx) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001488 const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001489 if (AddRec->getLoop() == OtherAddRec->getLoop()) {
1490 // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D}
Dan Gohman64a845e2009-06-24 04:48:43 +00001491 SmallVector<const SCEV *, 4> NewOps(AddRec->op_begin(),
1492 AddRec->op_end());
Chris Lattner53e677a2004-04-02 20:23:17 +00001493 for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) {
1494 if (i >= NewOps.size()) {
1495 NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i,
1496 OtherAddRec->op_end());
1497 break;
1498 }
Dan Gohman246b2562007-10-22 18:31:58 +00001499 NewOps[i] = getAddExpr(NewOps[i], OtherAddRec->getOperand(i));
Chris Lattner53e677a2004-04-02 20:23:17 +00001500 }
Dan Gohman0bba49c2009-07-07 17:06:11 +00001501 const SCEV *NewAddRec = getAddRecExpr(NewOps, AddRec->getLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +00001502
1503 if (Ops.size() == 2) return NewAddRec;
1504
1505 Ops.erase(Ops.begin()+Idx);
1506 Ops.erase(Ops.begin()+OtherIdx-1);
1507 Ops.push_back(NewAddRec);
Dan Gohman246b2562007-10-22 18:31:58 +00001508 return getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001509 }
1510 }
1511
1512 // Otherwise couldn't fold anything into this recurrence. Move onto the
1513 // next one.
1514 }
1515
1516 // Okay, it looks like we really DO need an add expr. Check to see if we
1517 // already have one, otherwise create a new one.
Dan Gohman1c343752009-06-27 21:21:31 +00001518 FoldingSetNodeID ID;
1519 ID.AddInteger(scAddExpr);
1520 ID.AddInteger(Ops.size());
1521 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1522 ID.AddPointer(Ops[i]);
1523 void *IP = 0;
1524 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
Dan Gohman3645b012009-10-09 00:10:36 +00001525 SCEVAddExpr *S = SCEVAllocator.Allocate<SCEVAddExpr>();
Dan Gohmanc050fd92009-07-13 20:50:19 +00001526 new (S) SCEVAddExpr(ID, Ops);
Dan Gohman1c343752009-06-27 21:21:31 +00001527 UniqueSCEVs.InsertNode(S, IP);
Dan Gohman3645b012009-10-09 00:10:36 +00001528 if (HasNUW) S->setHasNoUnsignedWrap(true);
1529 if (HasNSW) S->setHasNoSignedWrap(true);
Dan Gohman1c343752009-06-27 21:21:31 +00001530 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +00001531}
1532
Dan Gohman6c0866c2009-05-24 23:45:28 +00001533/// getMulExpr - Get a canonical multiply expression, or something simpler if
1534/// possible.
Dan Gohman3645b012009-10-09 00:10:36 +00001535const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
1536 bool HasNUW, bool HasNSW) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001537 assert(!Ops.empty() && "Cannot get empty mul!");
Dan Gohmanf78a9782009-05-18 15:44:58 +00001538#ifndef NDEBUG
1539 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1540 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1541 getEffectiveSCEVType(Ops[0]->getType()) &&
1542 "SCEVMulExpr operand types don't match!");
1543#endif
Chris Lattner53e677a2004-04-02 20:23:17 +00001544
1545 // Sort by complexity, this groups all similar expression types together.
Dan Gohman72861302009-05-07 14:39:04 +00001546 GroupByComplexity(Ops, LI);
Chris Lattner53e677a2004-04-02 20:23:17 +00001547
1548 // If there are any constants, fold them together.
1549 unsigned Idx = 0;
Dan Gohman622ed672009-05-04 22:02:23 +00001550 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001551
1552 // C1*(C2+V) -> C1*C2 + C1*V
1553 if (Ops.size() == 2)
Dan Gohman622ed672009-05-04 22:02:23 +00001554 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
Chris Lattner53e677a2004-04-02 20:23:17 +00001555 if (Add->getNumOperands() == 2 &&
1556 isa<SCEVConstant>(Add->getOperand(0)))
Dan Gohman246b2562007-10-22 18:31:58 +00001557 return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)),
1558 getMulExpr(LHSC, Add->getOperand(1)));
Chris Lattner53e677a2004-04-02 20:23:17 +00001559
Chris Lattner53e677a2004-04-02 20:23:17 +00001560 ++Idx;
Dan Gohman622ed672009-05-04 22:02:23 +00001561 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001562 // We found two constants, fold them together!
Owen Andersoneed707b2009-07-24 23:12:02 +00001563 ConstantInt *Fold = ConstantInt::get(getContext(),
1564 LHSC->getValue()->getValue() *
Nick Lewycky3e630762008-02-20 06:48:22 +00001565 RHSC->getValue()->getValue());
1566 Ops[0] = getConstant(Fold);
1567 Ops.erase(Ops.begin()+1); // Erase the folded element
1568 if (Ops.size() == 1) return Ops[0];
1569 LHSC = cast<SCEVConstant>(Ops[0]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001570 }
1571
1572 // If we are left with a constant one being multiplied, strip it off.
1573 if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) {
1574 Ops.erase(Ops.begin());
1575 --Idx;
Reid Spencercae57542007-03-02 00:28:52 +00001576 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001577 // If we have a multiply of zero, it will always be zero.
1578 return Ops[0];
1579 }
1580 }
1581
1582 // Skip over the add expression until we get to a multiply.
1583 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
1584 ++Idx;
1585
1586 if (Ops.size() == 1)
1587 return Ops[0];
Misha Brukman2b37d7c2005-04-21 21:13:18 +00001588
Chris Lattner53e677a2004-04-02 20:23:17 +00001589 // If there are mul operands inline them all into this expression.
1590 if (Idx < Ops.size()) {
1591 bool DeletedMul = false;
Dan Gohman622ed672009-05-04 22:02:23 +00001592 while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001593 // If we have an mul, expand the mul operands onto the end of the operands
1594 // list.
1595 Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end());
1596 Ops.erase(Ops.begin()+Idx);
1597 DeletedMul = true;
1598 }
1599
1600 // If we deleted at least one mul, we added operands to the end of the list,
1601 // and they are not necessarily sorted. Recurse to resort and resimplify
1602 // any operands we just aquired.
1603 if (DeletedMul)
Dan Gohman246b2562007-10-22 18:31:58 +00001604 return getMulExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001605 }
1606
1607 // If there are any add recurrences in the operands list, see if any other
1608 // added values are loop invariant. If so, we can fold them into the
1609 // recurrence.
1610 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
1611 ++Idx;
1612
1613 // Scan over all recurrences, trying to fold loop invariants into them.
1614 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
1615 // Scan all of the other operands to this mul and add them to the vector if
1616 // they are loop invariant w.r.t. the recurrence.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001617 SmallVector<const SCEV *, 8> LIOps;
Dan Gohman35738ac2009-05-04 22:30:44 +00001618 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001619 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1620 if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
1621 LIOps.push_back(Ops[i]);
1622 Ops.erase(Ops.begin()+i);
1623 --i; --e;
1624 }
1625
1626 // If we found some loop invariants, fold them into the recurrence.
1627 if (!LIOps.empty()) {
Dan Gohman8dae1382008-09-14 17:21:12 +00001628 // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step}
Dan Gohman0bba49c2009-07-07 17:06:11 +00001629 SmallVector<const SCEV *, 4> NewOps;
Chris Lattner53e677a2004-04-02 20:23:17 +00001630 NewOps.reserve(AddRec->getNumOperands());
1631 if (LIOps.size() == 1) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001632 const SCEV *Scale = LIOps[0];
Chris Lattner53e677a2004-04-02 20:23:17 +00001633 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
Dan Gohman246b2562007-10-22 18:31:58 +00001634 NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i)));
Chris Lattner53e677a2004-04-02 20:23:17 +00001635 } else {
1636 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001637 SmallVector<const SCEV *, 4> MulOps(LIOps.begin(), LIOps.end());
Chris Lattner53e677a2004-04-02 20:23:17 +00001638 MulOps.push_back(AddRec->getOperand(i));
Dan Gohman246b2562007-10-22 18:31:58 +00001639 NewOps.push_back(getMulExpr(MulOps));
Chris Lattner53e677a2004-04-02 20:23:17 +00001640 }
1641 }
1642
Dan Gohman355b4f32009-12-19 01:46:34 +00001643 // It's tempting to propagate the NSW flag here, but nsw multiplication
Dan Gohman59de33e2009-12-18 18:45:31 +00001644 // is not associative so this isn't necessarily safe.
1645 const SCEV *NewRec = getAddRecExpr(NewOps, AddRec->getLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +00001646
1647 // If all of the other operands were loop invariant, we are done.
1648 if (Ops.size() == 1) return NewRec;
1649
1650 // Otherwise, multiply the folded AddRec by the non-liv parts.
1651 for (unsigned i = 0;; ++i)
1652 if (Ops[i] == AddRec) {
1653 Ops[i] = NewRec;
1654 break;
1655 }
Dan Gohman246b2562007-10-22 18:31:58 +00001656 return getMulExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001657 }
1658
1659 // Okay, if there weren't any loop invariants to be folded, check to see if
1660 // there are multiple AddRec's with the same loop induction variable being
1661 // multiplied together. If so, we can fold them.
1662 for (unsigned OtherIdx = Idx+1;
1663 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
1664 if (OtherIdx != Idx) {
Dan Gohman35738ac2009-05-04 22:30:44 +00001665 const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
Chris Lattner53e677a2004-04-02 20:23:17 +00001666 if (AddRec->getLoop() == OtherAddRec->getLoop()) {
1667 // F * G --> {A,+,B} * {C,+,D} --> {A*C,+,F*D + G*B + B*D}
Dan Gohman35738ac2009-05-04 22:30:44 +00001668 const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec;
Dan Gohman0bba49c2009-07-07 17:06:11 +00001669 const SCEV *NewStart = getMulExpr(F->getStart(),
Chris Lattner53e677a2004-04-02 20:23:17 +00001670 G->getStart());
Dan Gohman0bba49c2009-07-07 17:06:11 +00001671 const SCEV *B = F->getStepRecurrence(*this);
1672 const SCEV *D = G->getStepRecurrence(*this);
1673 const SCEV *NewStep = getAddExpr(getMulExpr(F, D),
Dan Gohman246b2562007-10-22 18:31:58 +00001674 getMulExpr(G, B),
1675 getMulExpr(B, D));
Dan Gohman0bba49c2009-07-07 17:06:11 +00001676 const SCEV *NewAddRec = getAddRecExpr(NewStart, NewStep,
Dan Gohman246b2562007-10-22 18:31:58 +00001677 F->getLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +00001678 if (Ops.size() == 2) return NewAddRec;
1679
1680 Ops.erase(Ops.begin()+Idx);
1681 Ops.erase(Ops.begin()+OtherIdx-1);
1682 Ops.push_back(NewAddRec);
Dan Gohman246b2562007-10-22 18:31:58 +00001683 return getMulExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00001684 }
1685 }
1686
1687 // Otherwise couldn't fold anything into this recurrence. Move onto the
1688 // next one.
1689 }
1690
1691 // Okay, it looks like we really DO need an mul expr. Check to see if we
1692 // already have one, otherwise create a new one.
Dan Gohman1c343752009-06-27 21:21:31 +00001693 FoldingSetNodeID ID;
1694 ID.AddInteger(scMulExpr);
1695 ID.AddInteger(Ops.size());
1696 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1697 ID.AddPointer(Ops[i]);
1698 void *IP = 0;
1699 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
Dan Gohman3645b012009-10-09 00:10:36 +00001700 SCEVMulExpr *S = SCEVAllocator.Allocate<SCEVMulExpr>();
Dan Gohmanc050fd92009-07-13 20:50:19 +00001701 new (S) SCEVMulExpr(ID, Ops);
Dan Gohman1c343752009-06-27 21:21:31 +00001702 UniqueSCEVs.InsertNode(S, IP);
Dan Gohman3645b012009-10-09 00:10:36 +00001703 if (HasNUW) S->setHasNoUnsignedWrap(true);
1704 if (HasNSW) S->setHasNoSignedWrap(true);
Dan Gohman1c343752009-06-27 21:21:31 +00001705 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +00001706}
1707
Andreas Bolka8a11c982009-08-07 22:55:26 +00001708/// getUDivExpr - Get a canonical unsigned division expression, or something
1709/// simpler if possible.
Dan Gohman9311ef62009-06-24 14:49:00 +00001710const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
1711 const SCEV *RHS) {
Dan Gohmanf78a9782009-05-18 15:44:58 +00001712 assert(getEffectiveSCEVType(LHS->getType()) ==
1713 getEffectiveSCEVType(RHS->getType()) &&
1714 "SCEVUDivExpr operand types don't match!");
1715
Dan Gohman622ed672009-05-04 22:02:23 +00001716 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001717 if (RHSC->getValue()->equalsInt(1))
Dan Gohman4c0d5d52009-08-20 16:42:55 +00001718 return LHS; // X udiv 1 --> x
Dan Gohman185cf032009-05-08 20:18:49 +00001719 if (RHSC->isZero())
1720 return getIntegerSCEV(0, LHS->getType()); // value is undefined
Chris Lattner53e677a2004-04-02 20:23:17 +00001721
Dan Gohman185cf032009-05-08 20:18:49 +00001722 // Determine if the division can be folded into the operands of
1723 // its operands.
1724 // TODO: Generalize this to non-constants by using known-bits information.
1725 const Type *Ty = LHS->getType();
1726 unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros();
1727 unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ;
1728 // For non-power-of-two values, effectively round the value up to the
1729 // nearest power of two.
1730 if (!RHSC->getValue()->getValue().isPowerOf2())
1731 ++MaxShiftAmt;
1732 const IntegerType *ExtTy =
Owen Anderson1d0be152009-08-13 21:58:54 +00001733 IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt);
Dan Gohman185cf032009-05-08 20:18:49 +00001734 // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
1735 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
1736 if (const SCEVConstant *Step =
1737 dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this)))
1738 if (!Step->getValue()->getValue()
1739 .urem(RHSC->getValue()->getValue()) &&
Dan Gohmanb0285932009-05-08 23:11:16 +00001740 getZeroExtendExpr(AR, ExtTy) ==
1741 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
1742 getZeroExtendExpr(Step, ExtTy),
1743 AR->getLoop())) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001744 SmallVector<const SCEV *, 4> Operands;
Dan Gohman185cf032009-05-08 20:18:49 +00001745 for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i)
1746 Operands.push_back(getUDivExpr(AR->getOperand(i), RHS));
1747 return getAddRecExpr(Operands, AR->getLoop());
1748 }
1749 // (A*B)/C --> A*(B/C) if safe and B/C can be folded.
Dan Gohmanb0285932009-05-08 23:11:16 +00001750 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001751 SmallVector<const SCEV *, 4> Operands;
Dan Gohmanb0285932009-05-08 23:11:16 +00001752 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i)
1753 Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy));
1754 if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands))
Dan Gohman185cf032009-05-08 20:18:49 +00001755 // Find an operand that's safely divisible.
1756 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001757 const SCEV *Op = M->getOperand(i);
1758 const SCEV *Div = getUDivExpr(Op, RHSC);
Dan Gohman185cf032009-05-08 20:18:49 +00001759 if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001760 const SmallVectorImpl<const SCEV *> &MOperands = M->getOperands();
1761 Operands = SmallVector<const SCEV *, 4>(MOperands.begin(),
Dan Gohmana82752c2009-06-14 22:47:23 +00001762 MOperands.end());
Dan Gohman185cf032009-05-08 20:18:49 +00001763 Operands[i] = Div;
1764 return getMulExpr(Operands);
1765 }
1766 }
Dan Gohmanb0285932009-05-08 23:11:16 +00001767 }
Dan Gohman185cf032009-05-08 20:18:49 +00001768 // (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 +00001769 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001770 SmallVector<const SCEV *, 4> Operands;
Dan Gohmanb0285932009-05-08 23:11:16 +00001771 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i)
1772 Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy));
1773 if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) {
1774 Operands.clear();
Dan Gohman185cf032009-05-08 20:18:49 +00001775 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001776 const SCEV *Op = getUDivExpr(A->getOperand(i), RHS);
Dan Gohman185cf032009-05-08 20:18:49 +00001777 if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i))
1778 break;
1779 Operands.push_back(Op);
1780 }
1781 if (Operands.size() == A->getNumOperands())
1782 return getAddExpr(Operands);
1783 }
Dan Gohmanb0285932009-05-08 23:11:16 +00001784 }
Dan Gohman185cf032009-05-08 20:18:49 +00001785
1786 // Fold if both operands are constant.
Dan Gohman622ed672009-05-04 22:02:23 +00001787 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001788 Constant *LHSCV = LHSC->getValue();
1789 Constant *RHSCV = RHSC->getValue();
Owen Andersonbaf3c402009-07-29 18:55:55 +00001790 return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV,
Dan Gohmanb8be8b72009-06-24 00:38:39 +00001791 RHSCV)));
Chris Lattner53e677a2004-04-02 20:23:17 +00001792 }
1793 }
1794
Dan Gohman1c343752009-06-27 21:21:31 +00001795 FoldingSetNodeID ID;
1796 ID.AddInteger(scUDivExpr);
1797 ID.AddPointer(LHS);
1798 ID.AddPointer(RHS);
1799 void *IP = 0;
1800 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1801 SCEV *S = SCEVAllocator.Allocate<SCEVUDivExpr>();
Dan Gohmanc050fd92009-07-13 20:50:19 +00001802 new (S) SCEVUDivExpr(ID, LHS, RHS);
Dan Gohman1c343752009-06-27 21:21:31 +00001803 UniqueSCEVs.InsertNode(S, IP);
1804 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +00001805}
1806
1807
Dan Gohman6c0866c2009-05-24 23:45:28 +00001808/// getAddRecExpr - Get an add recurrence expression for the specified loop.
1809/// Simplify the expression as much as possible.
Dan Gohman0bba49c2009-07-07 17:06:11 +00001810const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start,
Dan Gohman3645b012009-10-09 00:10:36 +00001811 const SCEV *Step, const Loop *L,
1812 bool HasNUW, bool HasNSW) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001813 SmallVector<const SCEV *, 4> Operands;
Chris Lattner53e677a2004-04-02 20:23:17 +00001814 Operands.push_back(Start);
Dan Gohman622ed672009-05-04 22:02:23 +00001815 if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
Chris Lattner53e677a2004-04-02 20:23:17 +00001816 if (StepChrec->getLoop() == L) {
1817 Operands.insert(Operands.end(), StepChrec->op_begin(),
1818 StepChrec->op_end());
Dan Gohman246b2562007-10-22 18:31:58 +00001819 return getAddRecExpr(Operands, L);
Chris Lattner53e677a2004-04-02 20:23:17 +00001820 }
1821
1822 Operands.push_back(Step);
Dan Gohman3645b012009-10-09 00:10:36 +00001823 return getAddRecExpr(Operands, L, HasNUW, HasNSW);
Chris Lattner53e677a2004-04-02 20:23:17 +00001824}
1825
Dan Gohman6c0866c2009-05-24 23:45:28 +00001826/// getAddRecExpr - Get an add recurrence expression for the specified loop.
1827/// Simplify the expression as much as possible.
Dan Gohman64a845e2009-06-24 04:48:43 +00001828const SCEV *
Dan Gohman0bba49c2009-07-07 17:06:11 +00001829ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
Dan Gohman3645b012009-10-09 00:10:36 +00001830 const Loop *L,
1831 bool HasNUW, bool HasNSW) {
Chris Lattner53e677a2004-04-02 20:23:17 +00001832 if (Operands.size() == 1) return Operands[0];
Dan Gohmanf78a9782009-05-18 15:44:58 +00001833#ifndef NDEBUG
1834 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
1835 assert(getEffectiveSCEVType(Operands[i]->getType()) ==
1836 getEffectiveSCEVType(Operands[0]->getType()) &&
1837 "SCEVAddRecExpr operand types don't match!");
1838#endif
Chris Lattner53e677a2004-04-02 20:23:17 +00001839
Dan Gohmancfeb6a42008-06-18 16:23:07 +00001840 if (Operands.back()->isZero()) {
1841 Operands.pop_back();
Dan Gohman3645b012009-10-09 00:10:36 +00001842 return getAddRecExpr(Operands, L, HasNUW, HasNSW); // {X,+,0} --> X
Dan Gohmancfeb6a42008-06-18 16:23:07 +00001843 }
Chris Lattner53e677a2004-04-02 20:23:17 +00001844
Dan Gohmand9cc7492008-08-08 18:33:12 +00001845 // Canonicalize nested AddRecs in by nesting them in order of loop depth.
Dan Gohman622ed672009-05-04 22:02:23 +00001846 if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
Dan Gohman5d984912009-12-18 01:14:11 +00001847 const Loop *NestedLoop = NestedAR->getLoop();
Dan Gohmand9cc7492008-08-08 18:33:12 +00001848 if (L->getLoopDepth() < NestedLoop->getLoopDepth()) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001849 SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(),
Dan Gohman5d984912009-12-18 01:14:11 +00001850 NestedAR->op_end());
Dan Gohmand9cc7492008-08-08 18:33:12 +00001851 Operands[0] = NestedAR->getStart();
Dan Gohman9a80b452009-06-26 22:36:20 +00001852 // AddRecs require their operands be loop-invariant with respect to their
1853 // loops. Don't perform this transformation if it would break this
1854 // requirement.
1855 bool AllInvariant = true;
1856 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
1857 if (!Operands[i]->isLoopInvariant(L)) {
1858 AllInvariant = false;
1859 break;
1860 }
1861 if (AllInvariant) {
1862 NestedOperands[0] = getAddRecExpr(Operands, L);
1863 AllInvariant = true;
1864 for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i)
1865 if (!NestedOperands[i]->isLoopInvariant(NestedLoop)) {
1866 AllInvariant = false;
1867 break;
1868 }
1869 if (AllInvariant)
1870 // Ok, both add recurrences are valid after the transformation.
Dan Gohman3645b012009-10-09 00:10:36 +00001871 return getAddRecExpr(NestedOperands, NestedLoop, HasNUW, HasNSW);
Dan Gohman9a80b452009-06-26 22:36:20 +00001872 }
1873 // Reset Operands to its original state.
1874 Operands[0] = NestedAR;
Dan Gohmand9cc7492008-08-08 18:33:12 +00001875 }
1876 }
1877
Dan Gohman67847532010-01-19 22:27:22 +00001878 // Okay, it looks like we really DO need an addrec expr. Check to see if we
1879 // already have one, otherwise create a new one.
Dan Gohman1c343752009-06-27 21:21:31 +00001880 FoldingSetNodeID ID;
1881 ID.AddInteger(scAddRecExpr);
1882 ID.AddInteger(Operands.size());
1883 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
1884 ID.AddPointer(Operands[i]);
1885 ID.AddPointer(L);
1886 void *IP = 0;
1887 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
Dan Gohman3645b012009-10-09 00:10:36 +00001888 SCEVAddRecExpr *S = SCEVAllocator.Allocate<SCEVAddRecExpr>();
Dan Gohmanc050fd92009-07-13 20:50:19 +00001889 new (S) SCEVAddRecExpr(ID, Operands, L);
Dan Gohman1c343752009-06-27 21:21:31 +00001890 UniqueSCEVs.InsertNode(S, IP);
Dan Gohman3645b012009-10-09 00:10:36 +00001891 if (HasNUW) S->setHasNoUnsignedWrap(true);
1892 if (HasNSW) S->setHasNoSignedWrap(true);
Dan Gohman1c343752009-06-27 21:21:31 +00001893 return S;
Chris Lattner53e677a2004-04-02 20:23:17 +00001894}
1895
Dan Gohman9311ef62009-06-24 14:49:00 +00001896const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS,
1897 const SCEV *RHS) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001898 SmallVector<const SCEV *, 2> Ops;
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001899 Ops.push_back(LHS);
1900 Ops.push_back(RHS);
1901 return getSMaxExpr(Ops);
1902}
1903
Dan Gohman0bba49c2009-07-07 17:06:11 +00001904const SCEV *
1905ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001906 assert(!Ops.empty() && "Cannot get empty smax!");
1907 if (Ops.size() == 1) return Ops[0];
Dan Gohmanf78a9782009-05-18 15:44:58 +00001908#ifndef NDEBUG
1909 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1910 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1911 getEffectiveSCEVType(Ops[0]->getType()) &&
1912 "SCEVSMaxExpr operand types don't match!");
1913#endif
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001914
1915 // Sort by complexity, this groups all similar expression types together.
Dan Gohman72861302009-05-07 14:39:04 +00001916 GroupByComplexity(Ops, LI);
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001917
1918 // If there are any constants, fold them together.
1919 unsigned Idx = 0;
Dan Gohman622ed672009-05-04 22:02:23 +00001920 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001921 ++Idx;
1922 assert(Idx < Ops.size());
Dan Gohman622ed672009-05-04 22:02:23 +00001923 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001924 // We found two constants, fold them together!
Owen Andersoneed707b2009-07-24 23:12:02 +00001925 ConstantInt *Fold = ConstantInt::get(getContext(),
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001926 APIntOps::smax(LHSC->getValue()->getValue(),
1927 RHSC->getValue()->getValue()));
Nick Lewycky3e630762008-02-20 06:48:22 +00001928 Ops[0] = getConstant(Fold);
1929 Ops.erase(Ops.begin()+1); // Erase the folded element
1930 if (Ops.size() == 1) return Ops[0];
1931 LHSC = cast<SCEVConstant>(Ops[0]);
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001932 }
1933
Dan Gohmane5aceed2009-06-24 14:46:22 +00001934 // If we are left with a constant minimum-int, strip it off.
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001935 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) {
1936 Ops.erase(Ops.begin());
1937 --Idx;
Dan Gohmane5aceed2009-06-24 14:46:22 +00001938 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) {
1939 // If we have an smax with a constant maximum-int, it will always be
1940 // maximum-int.
1941 return Ops[0];
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001942 }
1943 }
1944
1945 if (Ops.size() == 1) return Ops[0];
1946
1947 // Find the first SMax
1948 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr)
1949 ++Idx;
1950
1951 // Check to see if one of the operands is an SMax. If so, expand its operands
1952 // onto our operand list, and recurse to simplify.
1953 if (Idx < Ops.size()) {
1954 bool DeletedSMax = false;
Dan Gohman622ed672009-05-04 22:02:23 +00001955 while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) {
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001956 Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end());
1957 Ops.erase(Ops.begin()+Idx);
1958 DeletedSMax = true;
1959 }
1960
1961 if (DeletedSMax)
1962 return getSMaxExpr(Ops);
1963 }
1964
1965 // Okay, check to see if the same value occurs in the operand list twice. If
1966 // so, delete one. Since we sorted the list, these values are required to
1967 // be adjacent.
1968 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
1969 if (Ops[i] == Ops[i+1]) { // X smax Y smax Y --> X smax Y
1970 Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
1971 --i; --e;
1972 }
1973
1974 if (Ops.size() == 1) return Ops[0];
1975
1976 assert(!Ops.empty() && "Reduced smax down to nothing!");
1977
Nick Lewycky3e630762008-02-20 06:48:22 +00001978 // Okay, it looks like we really DO need an smax expr. Check to see if we
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001979 // already have one, otherwise create a new one.
Dan Gohman1c343752009-06-27 21:21:31 +00001980 FoldingSetNodeID ID;
1981 ID.AddInteger(scSMaxExpr);
1982 ID.AddInteger(Ops.size());
1983 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1984 ID.AddPointer(Ops[i]);
1985 void *IP = 0;
1986 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1987 SCEV *S = SCEVAllocator.Allocate<SCEVSMaxExpr>();
Dan Gohmanc050fd92009-07-13 20:50:19 +00001988 new (S) SCEVSMaxExpr(ID, Ops);
Dan Gohman1c343752009-06-27 21:21:31 +00001989 UniqueSCEVs.InsertNode(S, IP);
1990 return S;
Nick Lewyckyc54c5612007-11-25 22:41:31 +00001991}
1992
Dan Gohman9311ef62009-06-24 14:49:00 +00001993const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS,
1994 const SCEV *RHS) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00001995 SmallVector<const SCEV *, 2> Ops;
Nick Lewycky3e630762008-02-20 06:48:22 +00001996 Ops.push_back(LHS);
1997 Ops.push_back(RHS);
1998 return getUMaxExpr(Ops);
1999}
2000
Dan Gohman0bba49c2009-07-07 17:06:11 +00002001const SCEV *
2002ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
Nick Lewycky3e630762008-02-20 06:48:22 +00002003 assert(!Ops.empty() && "Cannot get empty umax!");
2004 if (Ops.size() == 1) return Ops[0];
Dan Gohmanf78a9782009-05-18 15:44:58 +00002005#ifndef NDEBUG
2006 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
2007 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
2008 getEffectiveSCEVType(Ops[0]->getType()) &&
2009 "SCEVUMaxExpr operand types don't match!");
2010#endif
Nick Lewycky3e630762008-02-20 06:48:22 +00002011
2012 // Sort by complexity, this groups all similar expression types together.
Dan Gohman72861302009-05-07 14:39:04 +00002013 GroupByComplexity(Ops, LI);
Nick Lewycky3e630762008-02-20 06:48:22 +00002014
2015 // If there are any constants, fold them together.
2016 unsigned Idx = 0;
Dan Gohman622ed672009-05-04 22:02:23 +00002017 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Nick Lewycky3e630762008-02-20 06:48:22 +00002018 ++Idx;
2019 assert(Idx < Ops.size());
Dan Gohman622ed672009-05-04 22:02:23 +00002020 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Nick Lewycky3e630762008-02-20 06:48:22 +00002021 // We found two constants, fold them together!
Owen Andersoneed707b2009-07-24 23:12:02 +00002022 ConstantInt *Fold = ConstantInt::get(getContext(),
Nick Lewycky3e630762008-02-20 06:48:22 +00002023 APIntOps::umax(LHSC->getValue()->getValue(),
2024 RHSC->getValue()->getValue()));
2025 Ops[0] = getConstant(Fold);
2026 Ops.erase(Ops.begin()+1); // Erase the folded element
2027 if (Ops.size() == 1) return Ops[0];
2028 LHSC = cast<SCEVConstant>(Ops[0]);
2029 }
2030
Dan Gohmane5aceed2009-06-24 14:46:22 +00002031 // If we are left with a constant minimum-int, strip it off.
Nick Lewycky3e630762008-02-20 06:48:22 +00002032 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) {
2033 Ops.erase(Ops.begin());
2034 --Idx;
Dan Gohmane5aceed2009-06-24 14:46:22 +00002035 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) {
2036 // If we have an umax with a constant maximum-int, it will always be
2037 // maximum-int.
2038 return Ops[0];
Nick Lewycky3e630762008-02-20 06:48:22 +00002039 }
2040 }
2041
2042 if (Ops.size() == 1) return Ops[0];
2043
2044 // Find the first UMax
2045 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr)
2046 ++Idx;
2047
2048 // Check to see if one of the operands is a UMax. If so, expand its operands
2049 // onto our operand list, and recurse to simplify.
2050 if (Idx < Ops.size()) {
2051 bool DeletedUMax = false;
Dan Gohman622ed672009-05-04 22:02:23 +00002052 while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) {
Nick Lewycky3e630762008-02-20 06:48:22 +00002053 Ops.insert(Ops.end(), UMax->op_begin(), UMax->op_end());
2054 Ops.erase(Ops.begin()+Idx);
2055 DeletedUMax = true;
2056 }
2057
2058 if (DeletedUMax)
2059 return getUMaxExpr(Ops);
2060 }
2061
2062 // Okay, check to see if the same value occurs in the operand list twice. If
2063 // so, delete one. Since we sorted the list, these values are required to
2064 // be adjacent.
2065 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
2066 if (Ops[i] == Ops[i+1]) { // X umax Y umax Y --> X umax Y
2067 Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
2068 --i; --e;
2069 }
2070
2071 if (Ops.size() == 1) return Ops[0];
2072
2073 assert(!Ops.empty() && "Reduced umax down to nothing!");
2074
2075 // Okay, it looks like we really DO need a umax expr. Check to see if we
2076 // already have one, otherwise create a new one.
Dan Gohman1c343752009-06-27 21:21:31 +00002077 FoldingSetNodeID ID;
2078 ID.AddInteger(scUMaxExpr);
2079 ID.AddInteger(Ops.size());
2080 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2081 ID.AddPointer(Ops[i]);
2082 void *IP = 0;
2083 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2084 SCEV *S = SCEVAllocator.Allocate<SCEVUMaxExpr>();
Dan Gohmanc050fd92009-07-13 20:50:19 +00002085 new (S) SCEVUMaxExpr(ID, Ops);
Dan Gohman1c343752009-06-27 21:21:31 +00002086 UniqueSCEVs.InsertNode(S, IP);
2087 return S;
Nick Lewycky3e630762008-02-20 06:48:22 +00002088}
2089
Dan Gohman9311ef62009-06-24 14:49:00 +00002090const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS,
2091 const SCEV *RHS) {
Dan Gohmanf9a9a992009-06-22 03:18:45 +00002092 // ~smax(~x, ~y) == smin(x, y).
2093 return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
2094}
2095
Dan Gohman9311ef62009-06-24 14:49:00 +00002096const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS,
2097 const SCEV *RHS) {
Dan Gohmanf9a9a992009-06-22 03:18:45 +00002098 // ~umax(~x, ~y) == umin(x, y)
2099 return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
2100}
2101
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002102const SCEV *ScalarEvolution::getFieldOffsetExpr(const StructType *STy,
2103 unsigned FieldNo) {
2104 // If we have TargetData we can determine the constant offset.
2105 if (TD) {
2106 const Type *IntPtrTy = TD->getIntPtrType(getContext());
2107 const StructLayout &SL = *TD->getStructLayout(STy);
2108 uint64_t Offset = SL.getElementOffset(FieldNo);
2109 return getIntegerSCEV(Offset, IntPtrTy);
2110 }
2111
2112 // Field 0 is always at offset 0.
2113 if (FieldNo == 0) {
2114 const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy));
2115 return getIntegerSCEV(0, Ty);
2116 }
2117
2118 // Okay, it looks like we really DO need an offsetof expr. Check to see if we
2119 // already have one, otherwise create a new one.
2120 FoldingSetNodeID ID;
2121 ID.AddInteger(scFieldOffset);
2122 ID.AddPointer(STy);
2123 ID.AddInteger(FieldNo);
2124 void *IP = 0;
2125 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2126 SCEV *S = SCEVAllocator.Allocate<SCEVFieldOffsetExpr>();
2127 const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy));
2128 new (S) SCEVFieldOffsetExpr(ID, Ty, STy, FieldNo);
2129 UniqueSCEVs.InsertNode(S, IP);
2130 return S;
2131}
2132
2133const SCEV *ScalarEvolution::getAllocSizeExpr(const Type *AllocTy) {
2134 // If we have TargetData we can determine the constant size.
2135 if (TD && AllocTy->isSized()) {
2136 const Type *IntPtrTy = TD->getIntPtrType(getContext());
2137 return getIntegerSCEV(TD->getTypeAllocSize(AllocTy), IntPtrTy);
2138 }
2139
2140 // Expand an array size into the element size times the number
2141 // of elements.
2142 if (const ArrayType *ATy = dyn_cast<ArrayType>(AllocTy)) {
2143 const SCEV *E = getAllocSizeExpr(ATy->getElementType());
2144 return getMulExpr(
2145 E, getConstant(ConstantInt::get(cast<IntegerType>(E->getType()),
2146 ATy->getNumElements())));
2147 }
2148
2149 // Expand a vector size into the element size times the number
2150 // of elements.
2151 if (const VectorType *VTy = dyn_cast<VectorType>(AllocTy)) {
2152 const SCEV *E = getAllocSizeExpr(VTy->getElementType());
2153 return getMulExpr(
2154 E, getConstant(ConstantInt::get(cast<IntegerType>(E->getType()),
2155 VTy->getNumElements())));
2156 }
2157
2158 // Okay, it looks like we really DO need a sizeof expr. Check to see if we
2159 // already have one, otherwise create a new one.
2160 FoldingSetNodeID ID;
2161 ID.AddInteger(scAllocSize);
2162 ID.AddPointer(AllocTy);
2163 void *IP = 0;
2164 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2165 SCEV *S = SCEVAllocator.Allocate<SCEVAllocSizeExpr>();
2166 const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy));
2167 new (S) SCEVAllocSizeExpr(ID, Ty, AllocTy);
2168 UniqueSCEVs.InsertNode(S, IP);
2169 return S;
2170}
2171
Dan Gohman0bba49c2009-07-07 17:06:11 +00002172const SCEV *ScalarEvolution::getUnknown(Value *V) {
Dan Gohman6bbcba12009-06-24 00:54:57 +00002173 // Don't attempt to do anything other than create a SCEVUnknown object
2174 // here. createSCEV only calls getUnknown after checking for all other
2175 // interesting possibilities, and any other code that calls getUnknown
2176 // is doing so in order to hide a value from SCEV canonicalization.
2177
Dan Gohman1c343752009-06-27 21:21:31 +00002178 FoldingSetNodeID ID;
2179 ID.AddInteger(scUnknown);
2180 ID.AddPointer(V);
2181 void *IP = 0;
2182 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2183 SCEV *S = SCEVAllocator.Allocate<SCEVUnknown>();
Dan Gohmanc050fd92009-07-13 20:50:19 +00002184 new (S) SCEVUnknown(ID, V);
Dan Gohman1c343752009-06-27 21:21:31 +00002185 UniqueSCEVs.InsertNode(S, IP);
2186 return S;
Chris Lattner0a7f98c2004-04-15 15:07:24 +00002187}
2188
Chris Lattner53e677a2004-04-02 20:23:17 +00002189//===----------------------------------------------------------------------===//
Chris Lattner53e677a2004-04-02 20:23:17 +00002190// Basic SCEV Analysis and PHI Idiom Recognition Code
2191//
2192
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002193/// isSCEVable - Test if values of the given type are analyzable within
2194/// the SCEV framework. This primarily includes integer types, and it
2195/// can optionally include pointer types if the ScalarEvolution class
2196/// has access to target-specific information.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002197bool ScalarEvolution::isSCEVable(const Type *Ty) const {
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002198 // Integers and pointers are always SCEVable.
2199 return Ty->isInteger() || isa<PointerType>(Ty);
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002200}
2201
2202/// getTypeSizeInBits - Return the size in bits of the specified type,
2203/// for which isSCEVable must return true.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002204uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const {
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002205 assert(isSCEVable(Ty) && "Type is not SCEVable!");
2206
2207 // If we have a TargetData, use it!
2208 if (TD)
2209 return TD->getTypeSizeInBits(Ty);
2210
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002211 // Integer types have fixed sizes.
2212 if (Ty->isInteger())
2213 return Ty->getPrimitiveSizeInBits();
2214
2215 // The only other support type is pointer. Without TargetData, conservatively
2216 // assume pointers are 64-bit.
2217 assert(isa<PointerType>(Ty) && "isSCEVable permitted a non-SCEVable type!");
2218 return 64;
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002219}
2220
2221/// getEffectiveSCEVType - Return a type with the same bitwidth as
2222/// the given type and which represents how SCEV will treat the given
2223/// type, for which isSCEVable must return true. For pointer types,
2224/// this is the pointer-sized integer type.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002225const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const {
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002226 assert(isSCEVable(Ty) && "Type is not SCEVable!");
2227
2228 if (Ty->isInteger())
2229 return Ty;
2230
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002231 // The only other support type is pointer.
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002232 assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!");
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002233 if (TD) return TD->getIntPtrType(getContext());
2234
2235 // Without TargetData, conservatively assume pointers are 64-bit.
2236 return Type::getInt64Ty(getContext());
Dan Gohman2d1be872009-04-16 03:18:22 +00002237}
Chris Lattner53e677a2004-04-02 20:23:17 +00002238
Dan Gohman0bba49c2009-07-07 17:06:11 +00002239const SCEV *ScalarEvolution::getCouldNotCompute() {
Dan Gohman1c343752009-06-27 21:21:31 +00002240 return &CouldNotCompute;
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00002241}
2242
Chris Lattner53e677a2004-04-02 20:23:17 +00002243/// getSCEV - Return an existing SCEV if it exists, otherwise analyze the
2244/// expression and create a new one.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002245const SCEV *ScalarEvolution::getSCEV(Value *V) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002246 assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
Chris Lattner53e677a2004-04-02 20:23:17 +00002247
Dan Gohman0bba49c2009-07-07 17:06:11 +00002248 std::map<SCEVCallbackVH, const SCEV *>::iterator I = Scalars.find(V);
Chris Lattner53e677a2004-04-02 20:23:17 +00002249 if (I != Scalars.end()) return I->second;
Dan Gohman0bba49c2009-07-07 17:06:11 +00002250 const SCEV *S = createSCEV(V);
Dan Gohman35738ac2009-05-04 22:30:44 +00002251 Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S));
Chris Lattner53e677a2004-04-02 20:23:17 +00002252 return S;
2253}
2254
Dan Gohman6bbcba12009-06-24 00:54:57 +00002255/// getIntegerSCEV - Given a SCEVable type, create a constant for the
Dan Gohman2d1be872009-04-16 03:18:22 +00002256/// specified signed integer value and return a SCEV for the constant.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002257const SCEV *ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) {
Dan Gohman6bbcba12009-06-24 00:54:57 +00002258 const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty));
Owen Andersoneed707b2009-07-24 23:12:02 +00002259 return getConstant(ConstantInt::get(ITy, Val));
Dan Gohman2d1be872009-04-16 03:18:22 +00002260}
2261
2262/// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
2263///
Dan Gohman0bba49c2009-07-07 17:06:11 +00002264const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) {
Dan Gohman622ed672009-05-04 22:02:23 +00002265 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
Owen Anderson0a5372e2009-07-13 04:09:18 +00002266 return getConstant(
Owen Andersonbaf3c402009-07-29 18:55:55 +00002267 cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue())));
Dan Gohman2d1be872009-04-16 03:18:22 +00002268
2269 const Type *Ty = V->getType();
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002270 Ty = getEffectiveSCEVType(Ty);
Owen Anderson73c6b712009-07-13 20:58:05 +00002271 return getMulExpr(V,
Owen Andersona7235ea2009-07-31 20:28:14 +00002272 getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))));
Dan Gohman2d1be872009-04-16 03:18:22 +00002273}
2274
2275/// getNotSCEV - Return a SCEV corresponding to ~V = -1-V
Dan Gohman0bba49c2009-07-07 17:06:11 +00002276const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) {
Dan Gohman622ed672009-05-04 22:02:23 +00002277 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
Owen Anderson73c6b712009-07-13 20:58:05 +00002278 return getConstant(
Owen Andersonbaf3c402009-07-29 18:55:55 +00002279 cast<ConstantInt>(ConstantExpr::getNot(VC->getValue())));
Dan Gohman2d1be872009-04-16 03:18:22 +00002280
2281 const Type *Ty = V->getType();
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002282 Ty = getEffectiveSCEVType(Ty);
Owen Anderson73c6b712009-07-13 20:58:05 +00002283 const SCEV *AllOnes =
Owen Andersona7235ea2009-07-31 20:28:14 +00002284 getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty)));
Dan Gohman2d1be872009-04-16 03:18:22 +00002285 return getMinusSCEV(AllOnes, V);
2286}
2287
2288/// getMinusSCEV - Return a SCEV corresponding to LHS - RHS.
2289///
Dan Gohman9311ef62009-06-24 14:49:00 +00002290const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS,
2291 const SCEV *RHS) {
Dan Gohman2d1be872009-04-16 03:18:22 +00002292 // X - Y --> X + -Y
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002293 return getAddExpr(LHS, getNegativeSCEV(RHS));
Dan Gohman2d1be872009-04-16 03:18:22 +00002294}
2295
2296/// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the
2297/// input value to the specified type. If the type must be extended, it is zero
2298/// extended.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002299const SCEV *
2300ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V,
Nick Lewycky5cd28fa2009-04-23 05:15:08 +00002301 const Type *Ty) {
Dan Gohman2d1be872009-04-16 03:18:22 +00002302 const Type *SrcTy = V->getType();
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002303 assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2304 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohman2d1be872009-04-16 03:18:22 +00002305 "Cannot truncate or zero extend with non-integer arguments!");
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002306 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
Dan Gohman2d1be872009-04-16 03:18:22 +00002307 return V; // No conversion
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002308 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002309 return getTruncateExpr(V, Ty);
2310 return getZeroExtendExpr(V, Ty);
Dan Gohman2d1be872009-04-16 03:18:22 +00002311}
2312
2313/// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the
2314/// input value to the specified type. If the type must be extended, it is sign
2315/// extended.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002316const SCEV *
2317ScalarEvolution::getTruncateOrSignExtend(const SCEV *V,
Nick Lewycky5cd28fa2009-04-23 05:15:08 +00002318 const Type *Ty) {
Dan Gohman2d1be872009-04-16 03:18:22 +00002319 const Type *SrcTy = V->getType();
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002320 assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2321 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohman2d1be872009-04-16 03:18:22 +00002322 "Cannot truncate or zero extend with non-integer arguments!");
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002323 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
Dan Gohman2d1be872009-04-16 03:18:22 +00002324 return V; // No conversion
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002325 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002326 return getTruncateExpr(V, Ty);
2327 return getSignExtendExpr(V, Ty);
Dan Gohman2d1be872009-04-16 03:18:22 +00002328}
2329
Dan Gohman467c4302009-05-13 03:46:30 +00002330/// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the
2331/// input value to the specified type. If the type must be extended, it is zero
2332/// extended. The conversion must not be narrowing.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002333const SCEV *
2334ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, const Type *Ty) {
Dan Gohman467c4302009-05-13 03:46:30 +00002335 const Type *SrcTy = V->getType();
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002336 assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2337 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohman467c4302009-05-13 03:46:30 +00002338 "Cannot noop or zero extend with non-integer arguments!");
2339 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2340 "getNoopOrZeroExtend cannot truncate!");
2341 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2342 return V; // No conversion
2343 return getZeroExtendExpr(V, Ty);
2344}
2345
2346/// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the
2347/// input value to the specified type. If the type must be extended, it is sign
2348/// extended. The conversion must not be narrowing.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002349const SCEV *
2350ScalarEvolution::getNoopOrSignExtend(const SCEV *V, const Type *Ty) {
Dan Gohman467c4302009-05-13 03:46:30 +00002351 const Type *SrcTy = V->getType();
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002352 assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2353 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohman467c4302009-05-13 03:46:30 +00002354 "Cannot noop or sign extend with non-integer arguments!");
2355 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2356 "getNoopOrSignExtend cannot truncate!");
2357 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2358 return V; // No conversion
2359 return getSignExtendExpr(V, Ty);
2360}
2361
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00002362/// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of
2363/// the input value to the specified type. If the type must be extended,
2364/// it is extended with unspecified bits. The conversion must not be
2365/// narrowing.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002366const SCEV *
2367ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, const Type *Ty) {
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00002368 const Type *SrcTy = V->getType();
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002369 assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2370 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohman2ce84c8d2009-06-13 15:56:47 +00002371 "Cannot noop or any extend with non-integer arguments!");
2372 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2373 "getNoopOrAnyExtend cannot truncate!");
2374 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2375 return V; // No conversion
2376 return getAnyExtendExpr(V, Ty);
2377}
2378
Dan Gohman467c4302009-05-13 03:46:30 +00002379/// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the
2380/// input value to the specified type. The conversion must not be widening.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002381const SCEV *
2382ScalarEvolution::getTruncateOrNoop(const SCEV *V, const Type *Ty) {
Dan Gohman467c4302009-05-13 03:46:30 +00002383 const Type *SrcTy = V->getType();
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002384 assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2385 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohman467c4302009-05-13 03:46:30 +00002386 "Cannot truncate or noop with non-integer arguments!");
2387 assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) &&
2388 "getTruncateOrNoop cannot extend!");
2389 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2390 return V; // No conversion
2391 return getTruncateExpr(V, Ty);
2392}
2393
Dan Gohmana334aa72009-06-22 00:31:57 +00002394/// getUMaxFromMismatchedTypes - Promote the operands to the wider of
2395/// the types using zero-extension, and then perform a umax operation
2396/// with them.
Dan Gohman9311ef62009-06-24 14:49:00 +00002397const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS,
2398 const SCEV *RHS) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00002399 const SCEV *PromotedLHS = LHS;
2400 const SCEV *PromotedRHS = RHS;
Dan Gohmana334aa72009-06-22 00:31:57 +00002401
2402 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
2403 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
2404 else
2405 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
2406
2407 return getUMaxExpr(PromotedLHS, PromotedRHS);
2408}
2409
Dan Gohmanc9759e82009-06-22 15:03:27 +00002410/// getUMinFromMismatchedTypes - Promote the operands to the wider of
2411/// the types using zero-extension, and then perform a umin operation
2412/// with them.
Dan Gohman9311ef62009-06-24 14:49:00 +00002413const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS,
2414 const SCEV *RHS) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00002415 const SCEV *PromotedLHS = LHS;
2416 const SCEV *PromotedRHS = RHS;
Dan Gohmanc9759e82009-06-22 15:03:27 +00002417
2418 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
2419 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
2420 else
2421 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
2422
2423 return getUMinExpr(PromotedLHS, PromotedRHS);
2424}
2425
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002426/// PushDefUseChildren - Push users of the given Instruction
2427/// onto the given Worklist.
2428static void
2429PushDefUseChildren(Instruction *I,
2430 SmallVectorImpl<Instruction *> &Worklist) {
2431 // Push the def-use children onto the Worklist stack.
2432 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
2433 UI != UE; ++UI)
2434 Worklist.push_back(cast<Instruction>(UI));
2435}
2436
2437/// ForgetSymbolicValue - This looks up computed SCEV values for all
2438/// instructions that depend on the given instruction and removes them from
2439/// the Scalars map if they reference SymName. This is used during PHI
2440/// resolution.
Dan Gohman64a845e2009-06-24 04:48:43 +00002441void
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002442ScalarEvolution::ForgetSymbolicName(Instruction *I, const SCEV *SymName) {
2443 SmallVector<Instruction *, 16> Worklist;
2444 PushDefUseChildren(I, Worklist);
Chris Lattner53e677a2004-04-02 20:23:17 +00002445
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002446 SmallPtrSet<Instruction *, 8> Visited;
2447 Visited.insert(I);
2448 while (!Worklist.empty()) {
2449 Instruction *I = Worklist.pop_back_val();
2450 if (!Visited.insert(I)) continue;
Chris Lattner4dc534c2005-02-13 04:37:18 +00002451
Dan Gohman5d984912009-12-18 01:14:11 +00002452 std::map<SCEVCallbackVH, const SCEV *>::iterator It =
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002453 Scalars.find(static_cast<Value *>(I));
2454 if (It != Scalars.end()) {
2455 // Short-circuit the def-use traversal if the symbolic name
2456 // ceases to appear in expressions.
2457 if (!It->second->hasOperand(SymName))
2458 continue;
Chris Lattner4dc534c2005-02-13 04:37:18 +00002459
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002460 // SCEVUnknown for a PHI either means that it has an unrecognized
2461 // structure, or it's a PHI that's in the progress of being computed
2462 // by createNodeForPHI. In the former case, additional loop trip
2463 // count information isn't going to change anything. In the later
2464 // case, createNodeForPHI will perform the necessary updates on its
2465 // own when it gets to that point.
Dan Gohman42214892009-08-31 21:15:23 +00002466 if (!isa<PHINode>(I) || !isa<SCEVUnknown>(It->second)) {
2467 ValuesAtScopes.erase(It->second);
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002468 Scalars.erase(It);
Dan Gohman42214892009-08-31 21:15:23 +00002469 }
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002470 }
2471
2472 PushDefUseChildren(I, Worklist);
2473 }
Chris Lattner4dc534c2005-02-13 04:37:18 +00002474}
Chris Lattner53e677a2004-04-02 20:23:17 +00002475
2476/// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in
2477/// a loop header, making it a potential recurrence, or it doesn't.
2478///
Dan Gohman0bba49c2009-07-07 17:06:11 +00002479const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
Chris Lattner53e677a2004-04-02 20:23:17 +00002480 if (PN->getNumIncomingValues() == 2) // The loops have been canonicalized.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002481 if (const Loop *L = LI->getLoopFor(PN->getParent()))
Chris Lattner53e677a2004-04-02 20:23:17 +00002482 if (L->getHeader() == PN->getParent()) {
2483 // If it lives in the loop header, it has two incoming values, one
2484 // from outside the loop, and one from inside.
2485 unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0));
2486 unsigned BackEdge = IncomingEdge^1;
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002487
Chris Lattner53e677a2004-04-02 20:23:17 +00002488 // While we are analyzing this PHI node, handle its value symbolically.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002489 const SCEV *SymbolicName = getUnknown(PN);
Chris Lattner53e677a2004-04-02 20:23:17 +00002490 assert(Scalars.find(PN) == Scalars.end() &&
2491 "PHI node already processed?");
Dan Gohman35738ac2009-05-04 22:30:44 +00002492 Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName));
Chris Lattner53e677a2004-04-02 20:23:17 +00002493
2494 // Using this symbolic name for the PHI, analyze the value coming around
2495 // the back-edge.
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002496 Value *BEValueV = PN->getIncomingValue(BackEdge);
2497 const SCEV *BEValue = getSCEV(BEValueV);
Chris Lattner53e677a2004-04-02 20:23:17 +00002498
2499 // NOTE: If BEValue is loop invariant, we know that the PHI node just
2500 // has a special value for the first iteration of the loop.
2501
2502 // If the value coming around the backedge is an add with the symbolic
2503 // value we just inserted, then we found a simple induction variable!
Dan Gohman622ed672009-05-04 22:02:23 +00002504 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00002505 // If there is a single occurrence of the symbolic value, replace it
2506 // with a recurrence.
2507 unsigned FoundIndex = Add->getNumOperands();
2508 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
2509 if (Add->getOperand(i) == SymbolicName)
2510 if (FoundIndex == e) {
2511 FoundIndex = i;
2512 break;
2513 }
2514
2515 if (FoundIndex != Add->getNumOperands()) {
2516 // Create an add with everything but the specified operand.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002517 SmallVector<const SCEV *, 8> Ops;
Chris Lattner53e677a2004-04-02 20:23:17 +00002518 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
2519 if (i != FoundIndex)
2520 Ops.push_back(Add->getOperand(i));
Dan Gohman0bba49c2009-07-07 17:06:11 +00002521 const SCEV *Accum = getAddExpr(Ops);
Chris Lattner53e677a2004-04-02 20:23:17 +00002522
2523 // This is not a valid addrec if the step amount is varying each
2524 // loop iteration, but is not itself an addrec in this loop.
2525 if (Accum->isLoopInvariant(L) ||
2526 (isa<SCEVAddRecExpr>(Accum) &&
2527 cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
Dan Gohman64a845e2009-06-24 04:48:43 +00002528 const SCEV *StartVal =
2529 getSCEV(PN->getIncomingValue(IncomingEdge));
Dan Gohmaneb490a72009-07-25 01:22:26 +00002530 const SCEVAddRecExpr *PHISCEV =
2531 cast<SCEVAddRecExpr>(getAddRecExpr(StartVal, Accum, L));
2532
2533 // If the increment doesn't overflow, then neither the addrec nor the
2534 // post-increment will overflow.
2535 if (const AddOperator *OBO = dyn_cast<AddOperator>(BEValueV))
2536 if (OBO->getOperand(0) == PN &&
2537 getSCEV(OBO->getOperand(1)) ==
2538 PHISCEV->getStepRecurrence(*this)) {
2539 const SCEVAddRecExpr *PostInc = PHISCEV->getPostIncExpr(*this);
Dan Gohman5078f842009-08-20 17:11:38 +00002540 if (OBO->hasNoUnsignedWrap()) {
Dan Gohmaneb490a72009-07-25 01:22:26 +00002541 const_cast<SCEVAddRecExpr *>(PHISCEV)
Dan Gohman5078f842009-08-20 17:11:38 +00002542 ->setHasNoUnsignedWrap(true);
Dan Gohmaneb490a72009-07-25 01:22:26 +00002543 const_cast<SCEVAddRecExpr *>(PostInc)
Dan Gohman5078f842009-08-20 17:11:38 +00002544 ->setHasNoUnsignedWrap(true);
Dan Gohmaneb490a72009-07-25 01:22:26 +00002545 }
Dan Gohman5078f842009-08-20 17:11:38 +00002546 if (OBO->hasNoSignedWrap()) {
Dan Gohmaneb490a72009-07-25 01:22:26 +00002547 const_cast<SCEVAddRecExpr *>(PHISCEV)
Dan Gohman5078f842009-08-20 17:11:38 +00002548 ->setHasNoSignedWrap(true);
Dan Gohmaneb490a72009-07-25 01:22:26 +00002549 const_cast<SCEVAddRecExpr *>(PostInc)
Dan Gohman5078f842009-08-20 17:11:38 +00002550 ->setHasNoSignedWrap(true);
Dan Gohmaneb490a72009-07-25 01:22:26 +00002551 }
2552 }
Chris Lattner53e677a2004-04-02 20:23:17 +00002553
2554 // Okay, for the entire analysis of this edge we assumed the PHI
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002555 // to be symbolic. We now need to go back and purge all of the
2556 // entries for the scalars that use the symbolic expression.
2557 ForgetSymbolicName(PN, SymbolicName);
2558 Scalars[SCEVCallbackVH(PN, this)] = PHISCEV;
Chris Lattner53e677a2004-04-02 20:23:17 +00002559 return PHISCEV;
2560 }
2561 }
Dan Gohman622ed672009-05-04 22:02:23 +00002562 } else if (const SCEVAddRecExpr *AddRec =
2563 dyn_cast<SCEVAddRecExpr>(BEValue)) {
Chris Lattner97156e72006-04-26 18:34:07 +00002564 // Otherwise, this could be a loop like this:
2565 // i = 0; for (j = 1; ..; ++j) { .... i = j; }
2566 // In this case, j = {1,+,1} and BEValue is j.
2567 // Because the other in-value of i (0) fits the evolution of BEValue
2568 // i really is an addrec evolution.
2569 if (AddRec->getLoop() == L && AddRec->isAffine()) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00002570 const SCEV *StartVal = getSCEV(PN->getIncomingValue(IncomingEdge));
Chris Lattner97156e72006-04-26 18:34:07 +00002571
2572 // If StartVal = j.start - j.stride, we can use StartVal as the
2573 // initial step of the addrec evolution.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002574 if (StartVal == getMinusSCEV(AddRec->getOperand(0),
Dan Gohman246b2562007-10-22 18:31:58 +00002575 AddRec->getOperand(1))) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00002576 const SCEV *PHISCEV =
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002577 getAddRecExpr(StartVal, AddRec->getOperand(1), L);
Chris Lattner97156e72006-04-26 18:34:07 +00002578
2579 // Okay, for the entire analysis of this edge we assumed the PHI
Dan Gohmanfef8bb22009-07-25 01:13:03 +00002580 // to be symbolic. We now need to go back and purge all of the
2581 // entries for the scalars that use the symbolic expression.
2582 ForgetSymbolicName(PN, SymbolicName);
2583 Scalars[SCEVCallbackVH(PN, this)] = PHISCEV;
Chris Lattner97156e72006-04-26 18:34:07 +00002584 return PHISCEV;
2585 }
2586 }
Chris Lattner53e677a2004-04-02 20:23:17 +00002587 }
2588
2589 return SymbolicName;
2590 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002591
Dan Gohmana653fc52009-07-14 14:06:25 +00002592 // It's tempting to recognize PHIs with a unique incoming value, however
2593 // this leads passes like indvars to break LCSSA form. Fortunately, such
2594 // PHIs are rare, as instcombine zaps them.
2595
Chris Lattner53e677a2004-04-02 20:23:17 +00002596 // If it's not a loop phi, we can't handle it yet.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002597 return getUnknown(PN);
Chris Lattner53e677a2004-04-02 20:23:17 +00002598}
2599
Dan Gohman26466c02009-05-08 20:26:55 +00002600/// createNodeForGEP - Expand GEP instructions into add and multiply
2601/// operations. This allows them to be analyzed by regular SCEV code.
2602///
Dan Gohmand281ed22009-12-18 02:09:29 +00002603const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
Dan Gohman26466c02009-05-08 20:26:55 +00002604
Dan Gohmand281ed22009-12-18 02:09:29 +00002605 bool InBounds = GEP->isInBounds();
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002606 const Type *IntPtrTy = getEffectiveSCEVType(GEP->getType());
Dan Gohmane810b0d2009-05-08 20:36:47 +00002607 Value *Base = GEP->getOperand(0);
Dan Gohmanc63a6272009-05-09 00:14:52 +00002608 // Don't attempt to analyze GEPs over unsized objects.
2609 if (!cast<PointerType>(Base->getType())->getElementType()->isSized())
2610 return getUnknown(GEP);
Dan Gohman0bba49c2009-07-07 17:06:11 +00002611 const SCEV *TotalOffset = getIntegerSCEV(0, IntPtrTy);
Dan Gohmane810b0d2009-05-08 20:36:47 +00002612 gep_type_iterator GTI = gep_type_begin(GEP);
2613 for (GetElementPtrInst::op_iterator I = next(GEP->op_begin()),
2614 E = GEP->op_end();
Dan Gohman26466c02009-05-08 20:26:55 +00002615 I != E; ++I) {
2616 Value *Index = *I;
2617 // Compute the (potentially symbolic) offset in bytes for this index.
2618 if (const StructType *STy = dyn_cast<StructType>(*GTI++)) {
2619 // For a struct, add the member offset.
Dan Gohman26466c02009-05-08 20:26:55 +00002620 unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
Dan Gohmanc40f17b2009-08-18 16:46:41 +00002621 TotalOffset = getAddExpr(TotalOffset,
Dan Gohmand281ed22009-12-18 02:09:29 +00002622 getFieldOffsetExpr(STy, FieldNo),
2623 /*HasNUW=*/false, /*HasNSW=*/InBounds);
Dan Gohman26466c02009-05-08 20:26:55 +00002624 } else {
2625 // For an array, add the element offset, explicitly scaled.
Dan Gohman0bba49c2009-07-07 17:06:11 +00002626 const SCEV *LocalOffset = getSCEV(Index);
Dan Gohman26466c02009-05-08 20:26:55 +00002627 if (!isa<PointerType>(LocalOffset->getType()))
2628 // Getelementptr indicies are signed.
Dan Gohman85b05a22009-07-13 21:35:55 +00002629 LocalOffset = getTruncateOrSignExtend(LocalOffset, IntPtrTy);
Dan Gohmand281ed22009-12-18 02:09:29 +00002630 // Lower "inbounds" GEPs to NSW arithmetic.
Dan Gohmand281ed22009-12-18 02:09:29 +00002631 LocalOffset = getMulExpr(LocalOffset, getAllocSizeExpr(*GTI),
2632 /*HasNUW=*/false, /*HasNSW=*/InBounds);
2633 TotalOffset = getAddExpr(TotalOffset, LocalOffset,
2634 /*HasNUW=*/false, /*HasNSW=*/InBounds);
Dan Gohman26466c02009-05-08 20:26:55 +00002635 }
2636 }
Dan Gohmand281ed22009-12-18 02:09:29 +00002637 return getAddExpr(getSCEV(Base), TotalOffset,
2638 /*HasNUW=*/false, /*HasNSW=*/InBounds);
Dan Gohman26466c02009-05-08 20:26:55 +00002639}
2640
Nick Lewycky83bb0052007-11-22 07:59:40 +00002641/// GetMinTrailingZeros - Determine the minimum number of zero bits that S is
2642/// guaranteed to end in (at every loop iteration). It is, at the same time,
2643/// the minimum number of times S is divisible by 2. For example, given {4,+,8}
2644/// it returns 2. If S is guaranteed to be 0, it returns the bitwidth of S.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002645uint32_t
Dan Gohman0bba49c2009-07-07 17:06:11 +00002646ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
Dan Gohman622ed672009-05-04 22:02:23 +00002647 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
Chris Lattner8314a0c2007-11-23 22:36:49 +00002648 return C->getValue()->getValue().countTrailingZeros();
Chris Lattnera17f0392006-12-12 02:26:09 +00002649
Dan Gohman622ed672009-05-04 22:02:23 +00002650 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S))
Dan Gohman2c364ad2009-06-19 23:29:04 +00002651 return std::min(GetMinTrailingZeros(T->getOperand()),
2652 (uint32_t)getTypeSizeInBits(T->getType()));
Nick Lewycky83bb0052007-11-22 07:59:40 +00002653
Dan Gohman622ed672009-05-04 22:02:23 +00002654 if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) {
Dan Gohman2c364ad2009-06-19 23:29:04 +00002655 uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
2656 return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
2657 getTypeSizeInBits(E->getType()) : OpRes;
Nick Lewycky83bb0052007-11-22 07:59:40 +00002658 }
2659
Dan Gohman622ed672009-05-04 22:02:23 +00002660 if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) {
Dan Gohman2c364ad2009-06-19 23:29:04 +00002661 uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
2662 return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
2663 getTypeSizeInBits(E->getType()) : OpRes;
Nick Lewycky83bb0052007-11-22 07:59:40 +00002664 }
2665
Dan Gohman622ed672009-05-04 22:02:23 +00002666 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
Nick Lewycky83bb0052007-11-22 07:59:40 +00002667 // The result is the min of all operands results.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002668 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
Nick Lewycky83bb0052007-11-22 07:59:40 +00002669 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman2c364ad2009-06-19 23:29:04 +00002670 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
Nick Lewycky83bb0052007-11-22 07:59:40 +00002671 return MinOpRes;
Chris Lattnera17f0392006-12-12 02:26:09 +00002672 }
2673
Dan Gohman622ed672009-05-04 22:02:23 +00002674 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
Nick Lewycky83bb0052007-11-22 07:59:40 +00002675 // The result is the sum of all operands results.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002676 uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0));
2677 uint32_t BitWidth = getTypeSizeInBits(M->getType());
Nick Lewycky83bb0052007-11-22 07:59:40 +00002678 for (unsigned i = 1, e = M->getNumOperands();
2679 SumOpRes != BitWidth && i != e; ++i)
Dan Gohman2c364ad2009-06-19 23:29:04 +00002680 SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)),
Nick Lewycky83bb0052007-11-22 07:59:40 +00002681 BitWidth);
2682 return SumOpRes;
Chris Lattnera17f0392006-12-12 02:26:09 +00002683 }
Nick Lewycky83bb0052007-11-22 07:59:40 +00002684
Dan Gohman622ed672009-05-04 22:02:23 +00002685 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
Nick Lewycky83bb0052007-11-22 07:59:40 +00002686 // The result is the min of all operands results.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002687 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
Nick Lewycky83bb0052007-11-22 07:59:40 +00002688 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman2c364ad2009-06-19 23:29:04 +00002689 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
Nick Lewycky83bb0052007-11-22 07:59:40 +00002690 return MinOpRes;
Chris Lattnera17f0392006-12-12 02:26:09 +00002691 }
Nick Lewycky83bb0052007-11-22 07:59:40 +00002692
Dan Gohman622ed672009-05-04 22:02:23 +00002693 if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) {
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002694 // The result is the min of all operands results.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002695 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002696 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman2c364ad2009-06-19 23:29:04 +00002697 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
Nick Lewyckyc54c5612007-11-25 22:41:31 +00002698 return MinOpRes;
2699 }
2700
Dan Gohman622ed672009-05-04 22:02:23 +00002701 if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) {
Nick Lewycky3e630762008-02-20 06:48:22 +00002702 // The result is the min of all operands results.
Dan Gohman2c364ad2009-06-19 23:29:04 +00002703 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
Nick Lewycky3e630762008-02-20 06:48:22 +00002704 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman2c364ad2009-06-19 23:29:04 +00002705 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
Nick Lewycky3e630762008-02-20 06:48:22 +00002706 return MinOpRes;
2707 }
2708
Dan Gohman2c364ad2009-06-19 23:29:04 +00002709 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2710 // For a SCEVUnknown, ask ValueTracking.
2711 unsigned BitWidth = getTypeSizeInBits(U->getType());
2712 APInt Mask = APInt::getAllOnesValue(BitWidth);
2713 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
2714 ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones);
2715 return Zeros.countTrailingOnes();
2716 }
2717
2718 // SCEVUDivExpr
Nick Lewycky83bb0052007-11-22 07:59:40 +00002719 return 0;
Chris Lattnera17f0392006-12-12 02:26:09 +00002720}
Chris Lattner53e677a2004-04-02 20:23:17 +00002721
Dan Gohman85b05a22009-07-13 21:35:55 +00002722/// getUnsignedRange - Determine the unsigned range for a particular SCEV.
2723///
2724ConstantRange
2725ScalarEvolution::getUnsignedRange(const SCEV *S) {
Dan Gohman2c364ad2009-06-19 23:29:04 +00002726
2727 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
Dan Gohman85b05a22009-07-13 21:35:55 +00002728 return ConstantRange(C->getValue()->getValue());
Dan Gohman2c364ad2009-06-19 23:29:04 +00002729
Dan Gohman85b05a22009-07-13 21:35:55 +00002730 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
2731 ConstantRange X = getUnsignedRange(Add->getOperand(0));
2732 for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
2733 X = X.add(getUnsignedRange(Add->getOperand(i)));
2734 return X;
2735 }
2736
2737 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
2738 ConstantRange X = getUnsignedRange(Mul->getOperand(0));
2739 for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
2740 X = X.multiply(getUnsignedRange(Mul->getOperand(i)));
2741 return X;
2742 }
2743
2744 if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
2745 ConstantRange X = getUnsignedRange(SMax->getOperand(0));
2746 for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
2747 X = X.smax(getUnsignedRange(SMax->getOperand(i)));
2748 return X;
2749 }
2750
2751 if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
2752 ConstantRange X = getUnsignedRange(UMax->getOperand(0));
2753 for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
2754 X = X.umax(getUnsignedRange(UMax->getOperand(i)));
2755 return X;
2756 }
2757
2758 if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
2759 ConstantRange X = getUnsignedRange(UDiv->getLHS());
2760 ConstantRange Y = getUnsignedRange(UDiv->getRHS());
2761 return X.udiv(Y);
2762 }
2763
2764 if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
2765 ConstantRange X = getUnsignedRange(ZExt->getOperand());
2766 return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth());
2767 }
2768
2769 if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
2770 ConstantRange X = getUnsignedRange(SExt->getOperand());
2771 return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth());
2772 }
2773
2774 if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
2775 ConstantRange X = getUnsignedRange(Trunc->getOperand());
2776 return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth());
2777 }
2778
2779 ConstantRange FullSet(getTypeSizeInBits(S->getType()), true);
2780
2781 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
2782 const SCEV *T = getBackedgeTakenCount(AddRec->getLoop());
2783 const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T);
2784 if (!Trip) return FullSet;
2785
2786 // TODO: non-affine addrec
2787 if (AddRec->isAffine()) {
2788 const Type *Ty = AddRec->getType();
2789 const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop());
2790 if (getTypeSizeInBits(MaxBECount->getType()) <= getTypeSizeInBits(Ty)) {
2791 MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty);
2792
2793 const SCEV *Start = AddRec->getStart();
Dan Gohmana16b5762009-07-21 00:42:47 +00002794 const SCEV *Step = AddRec->getStepRecurrence(*this);
Dan Gohman85b05a22009-07-13 21:35:55 +00002795 const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this);
2796
2797 // Check for overflow.
Dan Gohmana16b5762009-07-21 00:42:47 +00002798 // TODO: This is very conservative.
2799 if (!(Step->isOne() &&
2800 isKnownPredicate(ICmpInst::ICMP_ULT, Start, End)) &&
2801 !(Step->isAllOnesValue() &&
2802 isKnownPredicate(ICmpInst::ICMP_UGT, Start, End)))
Dan Gohman85b05a22009-07-13 21:35:55 +00002803 return FullSet;
2804
2805 ConstantRange StartRange = getUnsignedRange(Start);
2806 ConstantRange EndRange = getUnsignedRange(End);
2807 APInt Min = APIntOps::umin(StartRange.getUnsignedMin(),
2808 EndRange.getUnsignedMin());
2809 APInt Max = APIntOps::umax(StartRange.getUnsignedMax(),
2810 EndRange.getUnsignedMax());
2811 if (Min.isMinValue() && Max.isMaxValue())
Dan Gohman0d5bae42009-07-20 22:41:51 +00002812 return FullSet;
Dan Gohman85b05a22009-07-13 21:35:55 +00002813 return ConstantRange(Min, Max+1);
2814 }
2815 }
Dan Gohman2c364ad2009-06-19 23:29:04 +00002816 }
2817
2818 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2819 // For a SCEVUnknown, ask ValueTracking.
2820 unsigned BitWidth = getTypeSizeInBits(U->getType());
2821 APInt Mask = APInt::getAllOnesValue(BitWidth);
2822 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
2823 ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD);
Dan Gohman746f3b12009-07-20 22:34:18 +00002824 if (Ones == ~Zeros + 1)
2825 return FullSet;
2826 return ConstantRange(Ones, ~Zeros + 1);
Dan Gohman2c364ad2009-06-19 23:29:04 +00002827 }
2828
Dan Gohman85b05a22009-07-13 21:35:55 +00002829 return FullSet;
Dan Gohman2c364ad2009-06-19 23:29:04 +00002830}
2831
Dan Gohman85b05a22009-07-13 21:35:55 +00002832/// getSignedRange - Determine the signed range for a particular SCEV.
2833///
2834ConstantRange
2835ScalarEvolution::getSignedRange(const SCEV *S) {
Dan Gohman2c364ad2009-06-19 23:29:04 +00002836
Dan Gohman85b05a22009-07-13 21:35:55 +00002837 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
2838 return ConstantRange(C->getValue()->getValue());
2839
2840 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
2841 ConstantRange X = getSignedRange(Add->getOperand(0));
2842 for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
2843 X = X.add(getSignedRange(Add->getOperand(i)));
2844 return X;
Dan Gohman2c364ad2009-06-19 23:29:04 +00002845 }
2846
Dan Gohman85b05a22009-07-13 21:35:55 +00002847 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
2848 ConstantRange X = getSignedRange(Mul->getOperand(0));
2849 for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
2850 X = X.multiply(getSignedRange(Mul->getOperand(i)));
2851 return X;
Dan Gohman2c364ad2009-06-19 23:29:04 +00002852 }
2853
Dan Gohman85b05a22009-07-13 21:35:55 +00002854 if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
2855 ConstantRange X = getSignedRange(SMax->getOperand(0));
2856 for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
2857 X = X.smax(getSignedRange(SMax->getOperand(i)));
2858 return X;
2859 }
Dan Gohman62849c02009-06-24 01:05:09 +00002860
Dan Gohman85b05a22009-07-13 21:35:55 +00002861 if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
2862 ConstantRange X = getSignedRange(UMax->getOperand(0));
2863 for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
2864 X = X.umax(getSignedRange(UMax->getOperand(i)));
2865 return X;
2866 }
Dan Gohman62849c02009-06-24 01:05:09 +00002867
Dan Gohman85b05a22009-07-13 21:35:55 +00002868 if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
2869 ConstantRange X = getSignedRange(UDiv->getLHS());
2870 ConstantRange Y = getSignedRange(UDiv->getRHS());
2871 return X.udiv(Y);
2872 }
Dan Gohman62849c02009-06-24 01:05:09 +00002873
Dan Gohman85b05a22009-07-13 21:35:55 +00002874 if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
2875 ConstantRange X = getSignedRange(ZExt->getOperand());
2876 return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth());
2877 }
2878
2879 if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
2880 ConstantRange X = getSignedRange(SExt->getOperand());
2881 return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth());
2882 }
2883
2884 if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
2885 ConstantRange X = getSignedRange(Trunc->getOperand());
2886 return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth());
2887 }
2888
2889 ConstantRange FullSet(getTypeSizeInBits(S->getType()), true);
2890
2891 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
2892 const SCEV *T = getBackedgeTakenCount(AddRec->getLoop());
2893 const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T);
2894 if (!Trip) return FullSet;
2895
2896 // TODO: non-affine addrec
2897 if (AddRec->isAffine()) {
2898 const Type *Ty = AddRec->getType();
2899 const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop());
2900 if (getTypeSizeInBits(MaxBECount->getType()) <= getTypeSizeInBits(Ty)) {
2901 MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty);
2902
2903 const SCEV *Start = AddRec->getStart();
2904 const SCEV *Step = AddRec->getStepRecurrence(*this);
2905 const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this);
2906
2907 // Check for overflow.
Dan Gohmana16b5762009-07-21 00:42:47 +00002908 // TODO: This is very conservative.
2909 if (!(Step->isOne() &&
Dan Gohman85b05a22009-07-13 21:35:55 +00002910 isKnownPredicate(ICmpInst::ICMP_SLT, Start, End)) &&
Dan Gohmana16b5762009-07-21 00:42:47 +00002911 !(Step->isAllOnesValue() &&
Dan Gohman85b05a22009-07-13 21:35:55 +00002912 isKnownPredicate(ICmpInst::ICMP_SGT, Start, End)))
2913 return FullSet;
2914
2915 ConstantRange StartRange = getSignedRange(Start);
2916 ConstantRange EndRange = getSignedRange(End);
2917 APInt Min = APIntOps::smin(StartRange.getSignedMin(),
2918 EndRange.getSignedMin());
2919 APInt Max = APIntOps::smax(StartRange.getSignedMax(),
2920 EndRange.getSignedMax());
2921 if (Min.isMinSignedValue() && Max.isMaxSignedValue())
Dan Gohmanc268e7c2009-07-21 00:37:45 +00002922 return FullSet;
Dan Gohman85b05a22009-07-13 21:35:55 +00002923 return ConstantRange(Min, Max+1);
Dan Gohman62849c02009-06-24 01:05:09 +00002924 }
Dan Gohman62849c02009-06-24 01:05:09 +00002925 }
Dan Gohman62849c02009-06-24 01:05:09 +00002926 }
2927
Dan Gohman2c364ad2009-06-19 23:29:04 +00002928 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2929 // For a SCEVUnknown, ask ValueTracking.
Dan Gohman85b05a22009-07-13 21:35:55 +00002930 unsigned BitWidth = getTypeSizeInBits(U->getType());
2931 unsigned NS = ComputeNumSignBits(U->getValue(), TD);
2932 if (NS == 1)
2933 return FullSet;
2934 return
2935 ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1),
2936 APInt::getSignedMaxValue(BitWidth).ashr(NS - 1)+1);
Dan Gohman2c364ad2009-06-19 23:29:04 +00002937 }
2938
Dan Gohman85b05a22009-07-13 21:35:55 +00002939 return FullSet;
Dan Gohman2c364ad2009-06-19 23:29:04 +00002940}
2941
Chris Lattner53e677a2004-04-02 20:23:17 +00002942/// createSCEV - We know that there is no SCEV for the specified value.
2943/// Analyze the expression.
2944///
Dan Gohman0bba49c2009-07-07 17:06:11 +00002945const SCEV *ScalarEvolution::createSCEV(Value *V) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +00002946 if (!isSCEVable(V->getType()))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002947 return getUnknown(V);
Dan Gohman2d1be872009-04-16 03:18:22 +00002948
Dan Gohman6c459a22008-06-22 19:56:46 +00002949 unsigned Opcode = Instruction::UserOp1;
2950 if (Instruction *I = dyn_cast<Instruction>(V))
2951 Opcode = I->getOpcode();
2952 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
2953 Opcode = CE->getOpcode();
Dan Gohman6bbcba12009-06-24 00:54:57 +00002954 else if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
2955 return getConstant(CI);
2956 else if (isa<ConstantPointerNull>(V))
2957 return getIntegerSCEV(0, V->getType());
2958 else if (isa<UndefValue>(V))
2959 return getIntegerSCEV(0, V->getType());
Dan Gohman26812322009-08-25 17:49:57 +00002960 else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
2961 return GA->mayBeOverridden() ? getUnknown(V) : getSCEV(GA->getAliasee());
Dan Gohman6c459a22008-06-22 19:56:46 +00002962 else
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002963 return getUnknown(V);
Chris Lattner2811f2a2007-04-02 05:41:38 +00002964
Dan Gohmanca178902009-07-17 20:47:02 +00002965 Operator *U = cast<Operator>(V);
Dan Gohman6c459a22008-06-22 19:56:46 +00002966 switch (Opcode) {
Dan Gohman7a721952009-10-09 16:35:06 +00002967 case Instruction::Add:
2968 // Don't transfer the NSW and NUW bits from the Add instruction to the
2969 // Add expression, because the Instruction may be guarded by control
2970 // flow and the no-overflow bits may not be valid for the expression in
2971 // any context.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002972 return getAddExpr(getSCEV(U->getOperand(0)),
Dan Gohman7a721952009-10-09 16:35:06 +00002973 getSCEV(U->getOperand(1)));
2974 case Instruction::Mul:
2975 // Don't transfer the NSW and NUW bits from the Mul instruction to the
2976 // Mul expression, as with Add.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002977 return getMulExpr(getSCEV(U->getOperand(0)),
Dan Gohman7a721952009-10-09 16:35:06 +00002978 getSCEV(U->getOperand(1)));
Dan Gohman6c459a22008-06-22 19:56:46 +00002979 case Instruction::UDiv:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002980 return getUDivExpr(getSCEV(U->getOperand(0)),
2981 getSCEV(U->getOperand(1)));
Dan Gohman6c459a22008-06-22 19:56:46 +00002982 case Instruction::Sub:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00002983 return getMinusSCEV(getSCEV(U->getOperand(0)),
2984 getSCEV(U->getOperand(1)));
Dan Gohman4ee29af2009-04-21 02:26:00 +00002985 case Instruction::And:
2986 // For an expression like x&255 that merely masks off the high bits,
2987 // use zext(trunc(x)) as the SCEV expression.
2988 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman2c73d5f2009-04-25 17:05:40 +00002989 if (CI->isNullValue())
2990 return getSCEV(U->getOperand(1));
Dan Gohmand6c32952009-04-27 01:41:10 +00002991 if (CI->isAllOnesValue())
2992 return getSCEV(U->getOperand(0));
Dan Gohman4ee29af2009-04-21 02:26:00 +00002993 const APInt &A = CI->getValue();
Dan Gohman61ffa8e2009-06-16 19:52:01 +00002994
2995 // Instcombine's ShrinkDemandedConstant may strip bits out of
2996 // constants, obscuring what would otherwise be a low-bits mask.
2997 // Use ComputeMaskedBits to compute what ShrinkDemandedConstant
2998 // knew about to reconstruct a low-bits mask value.
2999 unsigned LZ = A.countLeadingZeros();
3000 unsigned BitWidth = A.getBitWidth();
3001 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
3002 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
3003 ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD);
3004
3005 APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ);
3006
Dan Gohmanfc3641b2009-06-17 23:54:37 +00003007 if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask))
Dan Gohman4ee29af2009-04-21 02:26:00 +00003008 return
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003009 getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)),
Owen Anderson1d0be152009-08-13 21:58:54 +00003010 IntegerType::get(getContext(), BitWidth - LZ)),
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003011 U->getType());
Dan Gohman4ee29af2009-04-21 02:26:00 +00003012 }
3013 break;
Dan Gohman61ffa8e2009-06-16 19:52:01 +00003014
Dan Gohman6c459a22008-06-22 19:56:46 +00003015 case Instruction::Or:
3016 // If the RHS of the Or is a constant, we may have something like:
3017 // X*4+1 which got turned into X*4|1. Handle this as an Add so loop
3018 // optimizations will transparently handle this case.
3019 //
3020 // In order for this transformation to be safe, the LHS must be of the
3021 // form X*(2^n) and the Or constant must be less than 2^n.
3022 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00003023 const SCEV *LHS = getSCEV(U->getOperand(0));
Dan Gohman6c459a22008-06-22 19:56:46 +00003024 const APInt &CIVal = CI->getValue();
Dan Gohman2c364ad2009-06-19 23:29:04 +00003025 if (GetMinTrailingZeros(LHS) >=
Dan Gohman1f96e672009-09-17 18:05:20 +00003026 (CIVal.getBitWidth() - CIVal.countLeadingZeros())) {
3027 // Build a plain add SCEV.
3028 const SCEV *S = getAddExpr(LHS, getSCEV(CI));
3029 // If the LHS of the add was an addrec and it has no-wrap flags,
3030 // transfer the no-wrap flags, since an or won't introduce a wrap.
3031 if (const SCEVAddRecExpr *NewAR = dyn_cast<SCEVAddRecExpr>(S)) {
3032 const SCEVAddRecExpr *OldAR = cast<SCEVAddRecExpr>(LHS);
3033 if (OldAR->hasNoUnsignedWrap())
3034 const_cast<SCEVAddRecExpr *>(NewAR)->setHasNoUnsignedWrap(true);
3035 if (OldAR->hasNoSignedWrap())
3036 const_cast<SCEVAddRecExpr *>(NewAR)->setHasNoSignedWrap(true);
3037 }
3038 return S;
3039 }
Chris Lattner53e677a2004-04-02 20:23:17 +00003040 }
Dan Gohman6c459a22008-06-22 19:56:46 +00003041 break;
3042 case Instruction::Xor:
Dan Gohman6c459a22008-06-22 19:56:46 +00003043 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Nick Lewycky01eaf802008-07-07 06:15:49 +00003044 // If the RHS of the xor is a signbit, then this is just an add.
3045 // Instcombine turns add of signbit into xor as a strength reduction step.
Dan Gohman6c459a22008-06-22 19:56:46 +00003046 if (CI->getValue().isSignBit())
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003047 return getAddExpr(getSCEV(U->getOperand(0)),
3048 getSCEV(U->getOperand(1)));
Nick Lewycky01eaf802008-07-07 06:15:49 +00003049
3050 // If the RHS of xor is -1, then this is a not operation.
Dan Gohman0bac95e2009-05-18 16:17:44 +00003051 if (CI->isAllOnesValue())
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003052 return getNotSCEV(getSCEV(U->getOperand(0)));
Dan Gohman10978bd2009-05-18 16:29:04 +00003053
3054 // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask.
3055 // This is a variant of the check for xor with -1, and it handles
3056 // the case where instcombine has trimmed non-demanded bits out
3057 // of an xor with -1.
3058 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0)))
3059 if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1)))
3060 if (BO->getOpcode() == Instruction::And &&
3061 LCI->getValue() == CI->getValue())
3062 if (const SCEVZeroExtendExpr *Z =
Dan Gohman3034c102009-06-17 01:22:39 +00003063 dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) {
Dan Gohman82052832009-06-18 00:00:20 +00003064 const Type *UTy = U->getType();
Dan Gohman0bba49c2009-07-07 17:06:11 +00003065 const SCEV *Z0 = Z->getOperand();
Dan Gohman82052832009-06-18 00:00:20 +00003066 const Type *Z0Ty = Z0->getType();
3067 unsigned Z0TySize = getTypeSizeInBits(Z0Ty);
3068
3069 // If C is a low-bits mask, the zero extend is zerving to
3070 // mask off the high bits. Complement the operand and
3071 // re-apply the zext.
3072 if (APIntOps::isMask(Z0TySize, CI->getValue()))
3073 return getZeroExtendExpr(getNotSCEV(Z0), UTy);
3074
3075 // If C is a single bit, it may be in the sign-bit position
3076 // before the zero-extend. In this case, represent the xor
3077 // using an add, which is equivalent, and re-apply the zext.
3078 APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize);
3079 if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() &&
3080 Trunc.isSignBit())
3081 return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)),
3082 UTy);
Dan Gohman3034c102009-06-17 01:22:39 +00003083 }
Dan Gohman6c459a22008-06-22 19:56:46 +00003084 }
3085 break;
3086
3087 case Instruction::Shl:
3088 // Turn shift left of a constant amount into a multiply.
3089 if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
3090 uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
Owen Andersoneed707b2009-07-24 23:12:02 +00003091 Constant *X = ConstantInt::get(getContext(),
Dan Gohman6c459a22008-06-22 19:56:46 +00003092 APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003093 return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X));
Dan Gohman6c459a22008-06-22 19:56:46 +00003094 }
3095 break;
3096
Nick Lewycky01eaf802008-07-07 06:15:49 +00003097 case Instruction::LShr:
Nick Lewycky789558d2009-01-13 09:18:58 +00003098 // Turn logical shift right of a constant into a unsigned divide.
Nick Lewycky01eaf802008-07-07 06:15:49 +00003099 if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
3100 uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
Owen Andersoneed707b2009-07-24 23:12:02 +00003101 Constant *X = ConstantInt::get(getContext(),
Nick Lewycky01eaf802008-07-07 06:15:49 +00003102 APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003103 return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X));
Nick Lewycky01eaf802008-07-07 06:15:49 +00003104 }
3105 break;
3106
Dan Gohman4ee29af2009-04-21 02:26:00 +00003107 case Instruction::AShr:
3108 // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression.
3109 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1)))
3110 if (Instruction *L = dyn_cast<Instruction>(U->getOperand(0)))
3111 if (L->getOpcode() == Instruction::Shl &&
3112 L->getOperand(1) == U->getOperand(1)) {
Dan Gohman2c73d5f2009-04-25 17:05:40 +00003113 unsigned BitWidth = getTypeSizeInBits(U->getType());
3114 uint64_t Amt = BitWidth - CI->getZExtValue();
3115 if (Amt == BitWidth)
3116 return getSCEV(L->getOperand(0)); // shift by zero --> noop
3117 if (Amt > BitWidth)
3118 return getIntegerSCEV(0, U->getType()); // value is undefined
Dan Gohman4ee29af2009-04-21 02:26:00 +00003119 return
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003120 getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)),
Owen Anderson1d0be152009-08-13 21:58:54 +00003121 IntegerType::get(getContext(), Amt)),
Dan Gohman4ee29af2009-04-21 02:26:00 +00003122 U->getType());
3123 }
3124 break;
3125
Dan Gohman6c459a22008-06-22 19:56:46 +00003126 case Instruction::Trunc:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003127 return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman6c459a22008-06-22 19:56:46 +00003128
3129 case Instruction::ZExt:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003130 return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman6c459a22008-06-22 19:56:46 +00003131
3132 case Instruction::SExt:
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003133 return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman6c459a22008-06-22 19:56:46 +00003134
3135 case Instruction::BitCast:
3136 // BitCasts are no-op casts so we just eliminate the cast.
Dan Gohmanaf79fb52009-04-21 01:07:12 +00003137 if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType()))
Dan Gohman6c459a22008-06-22 19:56:46 +00003138 return getSCEV(U->getOperand(0));
3139 break;
3140
Dan Gohmanf2411742009-07-20 17:43:30 +00003141 // It's tempting to handle inttoptr and ptrtoint, however this can
3142 // lead to pointer expressions which cannot be expanded to GEPs
3143 // (because they may overflow). For now, the only pointer-typed
3144 // expressions we handle are GEPs and address literals.
Dan Gohman2d1be872009-04-16 03:18:22 +00003145
Dan Gohman26466c02009-05-08 20:26:55 +00003146 case Instruction::GetElementPtr:
Dan Gohmand281ed22009-12-18 02:09:29 +00003147 return createNodeForGEP(cast<GEPOperator>(U));
Dan Gohman2d1be872009-04-16 03:18:22 +00003148
Dan Gohman6c459a22008-06-22 19:56:46 +00003149 case Instruction::PHI:
3150 return createNodeForPHI(cast<PHINode>(U));
3151
3152 case Instruction::Select:
3153 // This could be a smax or umax that was lowered earlier.
3154 // Try to recover it.
3155 if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) {
3156 Value *LHS = ICI->getOperand(0);
3157 Value *RHS = ICI->getOperand(1);
3158 switch (ICI->getPredicate()) {
3159 case ICmpInst::ICMP_SLT:
3160 case ICmpInst::ICMP_SLE:
3161 std::swap(LHS, RHS);
3162 // fall through
3163 case ICmpInst::ICMP_SGT:
3164 case ICmpInst::ICMP_SGE:
3165 if (LHS == U->getOperand(1) && RHS == U->getOperand(2))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003166 return getSMaxExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman6c459a22008-06-22 19:56:46 +00003167 else if (LHS == U->getOperand(2) && RHS == U->getOperand(1))
Dan Gohmanf9a9a992009-06-22 03:18:45 +00003168 return getSMinExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman6c459a22008-06-22 19:56:46 +00003169 break;
3170 case ICmpInst::ICMP_ULT:
3171 case ICmpInst::ICMP_ULE:
3172 std::swap(LHS, RHS);
3173 // fall through
3174 case ICmpInst::ICMP_UGT:
3175 case ICmpInst::ICMP_UGE:
3176 if (LHS == U->getOperand(1) && RHS == U->getOperand(2))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003177 return getUMaxExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman6c459a22008-06-22 19:56:46 +00003178 else if (LHS == U->getOperand(2) && RHS == U->getOperand(1))
Dan Gohmanf9a9a992009-06-22 03:18:45 +00003179 return getUMinExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman6c459a22008-06-22 19:56:46 +00003180 break;
Dan Gohman30fb5122009-06-18 20:21:07 +00003181 case ICmpInst::ICMP_NE:
3182 // n != 0 ? n : 1 -> umax(n, 1)
3183 if (LHS == U->getOperand(1) &&
3184 isa<ConstantInt>(U->getOperand(2)) &&
3185 cast<ConstantInt>(U->getOperand(2))->isOne() &&
3186 isa<ConstantInt>(RHS) &&
3187 cast<ConstantInt>(RHS)->isZero())
3188 return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(2)));
3189 break;
3190 case ICmpInst::ICMP_EQ:
3191 // n == 0 ? 1 : n -> umax(n, 1)
3192 if (LHS == U->getOperand(2) &&
3193 isa<ConstantInt>(U->getOperand(1)) &&
3194 cast<ConstantInt>(U->getOperand(1))->isOne() &&
3195 isa<ConstantInt>(RHS) &&
3196 cast<ConstantInt>(RHS)->isZero())
3197 return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(1)));
3198 break;
Dan Gohman6c459a22008-06-22 19:56:46 +00003199 default:
3200 break;
3201 }
3202 }
3203
3204 default: // We cannot analyze this expression.
3205 break;
Chris Lattner53e677a2004-04-02 20:23:17 +00003206 }
3207
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003208 return getUnknown(V);
Chris Lattner53e677a2004-04-02 20:23:17 +00003209}
3210
3211
3212
3213//===----------------------------------------------------------------------===//
3214// Iteration Count Computation Code
3215//
3216
Dan Gohman46bdfb02009-02-24 18:55:53 +00003217/// getBackedgeTakenCount - If the specified loop has a predictable
3218/// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
3219/// object. The backedge-taken count is the number of times the loop header
3220/// will be branched to from within the loop. This is one less than the
3221/// trip count of the loop, since it doesn't count the first iteration,
3222/// when the header is branched to from outside the loop.
3223///
3224/// Note that it is not valid to call this method on a loop without a
3225/// loop-invariant backedge-taken count (see
3226/// hasLoopInvariantBackedgeTakenCount).
3227///
Dan Gohman0bba49c2009-07-07 17:06:11 +00003228const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) {
Dan Gohmana1af7572009-04-30 20:47:05 +00003229 return getBackedgeTakenInfo(L).Exact;
3230}
3231
3232/// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except
3233/// return the least SCEV value that is known never to be less than the
3234/// actual backedge taken count.
Dan Gohman0bba49c2009-07-07 17:06:11 +00003235const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) {
Dan Gohmana1af7572009-04-30 20:47:05 +00003236 return getBackedgeTakenInfo(L).Max;
3237}
3238
Dan Gohman59ae6b92009-07-08 19:23:34 +00003239/// PushLoopPHIs - Push PHI nodes in the header of the given loop
3240/// onto the given Worklist.
3241static void
3242PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) {
3243 BasicBlock *Header = L->getHeader();
3244
3245 // Push all Loop-header PHIs onto the Worklist stack.
3246 for (BasicBlock::iterator I = Header->begin();
3247 PHINode *PN = dyn_cast<PHINode>(I); ++I)
3248 Worklist.push_back(PN);
3249}
3250
Dan Gohmana1af7572009-04-30 20:47:05 +00003251const ScalarEvolution::BackedgeTakenInfo &
3252ScalarEvolution::getBackedgeTakenInfo(const Loop *L) {
Dan Gohman01ecca22009-04-27 20:16:15 +00003253 // Initially insert a CouldNotCompute for this loop. If the insertion
3254 // succeeds, procede to actually compute a backedge-taken count and
3255 // update the value. The temporary CouldNotCompute value tells SCEV
3256 // code elsewhere that it shouldn't attempt to request a new
3257 // backedge-taken count, which could result in infinite recursion.
Dan Gohman5d984912009-12-18 01:14:11 +00003258 std::pair<std::map<const Loop *, BackedgeTakenInfo>::iterator, bool> Pair =
Dan Gohman01ecca22009-04-27 20:16:15 +00003259 BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute()));
3260 if (Pair.second) {
Dan Gohmana1af7572009-04-30 20:47:05 +00003261 BackedgeTakenInfo ItCount = ComputeBackedgeTakenCount(L);
Dan Gohman1c343752009-06-27 21:21:31 +00003262 if (ItCount.Exact != getCouldNotCompute()) {
Dan Gohmana1af7572009-04-30 20:47:05 +00003263 assert(ItCount.Exact->isLoopInvariant(L) &&
3264 ItCount.Max->isLoopInvariant(L) &&
Chris Lattner53e677a2004-04-02 20:23:17 +00003265 "Computed trip count isn't loop invariant for loop!");
3266 ++NumTripCountsComputed;
Dan Gohman01ecca22009-04-27 20:16:15 +00003267
Dan Gohman01ecca22009-04-27 20:16:15 +00003268 // Update the value in the map.
3269 Pair.first->second = ItCount;
Dan Gohmana334aa72009-06-22 00:31:57 +00003270 } else {
Dan Gohman1c343752009-06-27 21:21:31 +00003271 if (ItCount.Max != getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003272 // Update the value in the map.
3273 Pair.first->second = ItCount;
3274 if (isa<PHINode>(L->getHeader()->begin()))
3275 // Only count loops that have phi nodes as not being computable.
3276 ++NumTripCountsNotComputed;
Chris Lattner53e677a2004-04-02 20:23:17 +00003277 }
Dan Gohmana1af7572009-04-30 20:47:05 +00003278
3279 // Now that we know more about the trip count for this loop, forget any
3280 // existing SCEV values for PHI nodes in this loop since they are only
Dan Gohman59ae6b92009-07-08 19:23:34 +00003281 // conservative estimates made without the benefit of trip count
Dan Gohman4c7279a2009-10-31 15:04:55 +00003282 // information. This is similar to the code in forgetLoop, except that
3283 // it handles SCEVUnknown PHI nodes specially.
Dan Gohman59ae6b92009-07-08 19:23:34 +00003284 if (ItCount.hasAnyInfo()) {
3285 SmallVector<Instruction *, 16> Worklist;
3286 PushLoopPHIs(L, Worklist);
3287
3288 SmallPtrSet<Instruction *, 8> Visited;
3289 while (!Worklist.empty()) {
3290 Instruction *I = Worklist.pop_back_val();
3291 if (!Visited.insert(I)) continue;
3292
Dan Gohman5d984912009-12-18 01:14:11 +00003293 std::map<SCEVCallbackVH, const SCEV *>::iterator It =
Dan Gohman59ae6b92009-07-08 19:23:34 +00003294 Scalars.find(static_cast<Value *>(I));
3295 if (It != Scalars.end()) {
3296 // SCEVUnknown for a PHI either means that it has an unrecognized
3297 // structure, or it's a PHI that's in the progress of being computed
Dan Gohmanba701882009-07-13 22:04:06 +00003298 // by createNodeForPHI. In the former case, additional loop trip
3299 // count information isn't going to change anything. In the later
3300 // case, createNodeForPHI will perform the necessary updates on its
3301 // own when it gets to that point.
Dan Gohman42214892009-08-31 21:15:23 +00003302 if (!isa<PHINode>(I) || !isa<SCEVUnknown>(It->second)) {
3303 ValuesAtScopes.erase(It->second);
Dan Gohman59ae6b92009-07-08 19:23:34 +00003304 Scalars.erase(It);
Dan Gohman42214892009-08-31 21:15:23 +00003305 }
Dan Gohman59ae6b92009-07-08 19:23:34 +00003306 if (PHINode *PN = dyn_cast<PHINode>(I))
3307 ConstantEvolutionLoopExitValue.erase(PN);
3308 }
3309
3310 PushDefUseChildren(I, Worklist);
3311 }
3312 }
Chris Lattner53e677a2004-04-02 20:23:17 +00003313 }
Dan Gohman01ecca22009-04-27 20:16:15 +00003314 return Pair.first->second;
Chris Lattner53e677a2004-04-02 20:23:17 +00003315}
3316
Dan Gohman4c7279a2009-10-31 15:04:55 +00003317/// forgetLoop - This method should be called by the client when it has
3318/// changed a loop in a way that may effect ScalarEvolution's ability to
3319/// compute a trip count, or if the loop is deleted.
3320void ScalarEvolution::forgetLoop(const Loop *L) {
3321 // Drop any stored trip count value.
Dan Gohman46bdfb02009-02-24 18:55:53 +00003322 BackedgeTakenCounts.erase(L);
Dan Gohmanfb7d35f2009-05-02 17:43:35 +00003323
Dan Gohman4c7279a2009-10-31 15:04:55 +00003324 // Drop information about expressions based on loop-header PHIs.
Dan Gohman35738ac2009-05-04 22:30:44 +00003325 SmallVector<Instruction *, 16> Worklist;
Dan Gohman59ae6b92009-07-08 19:23:34 +00003326 PushLoopPHIs(L, Worklist);
Dan Gohman35738ac2009-05-04 22:30:44 +00003327
Dan Gohman59ae6b92009-07-08 19:23:34 +00003328 SmallPtrSet<Instruction *, 8> Visited;
Dan Gohman35738ac2009-05-04 22:30:44 +00003329 while (!Worklist.empty()) {
3330 Instruction *I = Worklist.pop_back_val();
Dan Gohman59ae6b92009-07-08 19:23:34 +00003331 if (!Visited.insert(I)) continue;
3332
Dan Gohman5d984912009-12-18 01:14:11 +00003333 std::map<SCEVCallbackVH, const SCEV *>::iterator It =
Dan Gohman59ae6b92009-07-08 19:23:34 +00003334 Scalars.find(static_cast<Value *>(I));
3335 if (It != Scalars.end()) {
Dan Gohman42214892009-08-31 21:15:23 +00003336 ValuesAtScopes.erase(It->second);
Dan Gohman59ae6b92009-07-08 19:23:34 +00003337 Scalars.erase(It);
Dan Gohman59ae6b92009-07-08 19:23:34 +00003338 if (PHINode *PN = dyn_cast<PHINode>(I))
3339 ConstantEvolutionLoopExitValue.erase(PN);
3340 }
3341
3342 PushDefUseChildren(I, Worklist);
Dan Gohman35738ac2009-05-04 22:30:44 +00003343 }
Dan Gohman60f8a632009-02-17 20:49:49 +00003344}
3345
Dan Gohman46bdfb02009-02-24 18:55:53 +00003346/// ComputeBackedgeTakenCount - Compute the number of times the backedge
3347/// of the specified loop will execute.
Dan Gohmana1af7572009-04-30 20:47:05 +00003348ScalarEvolution::BackedgeTakenInfo
3349ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) {
Dan Gohman5d984912009-12-18 01:14:11 +00003350 SmallVector<BasicBlock *, 8> ExitingBlocks;
Dan Gohmana334aa72009-06-22 00:31:57 +00003351 L->getExitingBlocks(ExitingBlocks);
Chris Lattner53e677a2004-04-02 20:23:17 +00003352
Dan Gohmana334aa72009-06-22 00:31:57 +00003353 // Examine all exits and pick the most conservative values.
Dan Gohman0bba49c2009-07-07 17:06:11 +00003354 const SCEV *BECount = getCouldNotCompute();
3355 const SCEV *MaxBECount = getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003356 bool CouldNotComputeBECount = false;
Dan Gohmana334aa72009-06-22 00:31:57 +00003357 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
3358 BackedgeTakenInfo NewBTI =
3359 ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]);
Chris Lattner53e677a2004-04-02 20:23:17 +00003360
Dan Gohman1c343752009-06-27 21:21:31 +00003361 if (NewBTI.Exact == getCouldNotCompute()) {
Dan Gohmana334aa72009-06-22 00:31:57 +00003362 // We couldn't compute an exact value for this exit, so
Dan Gohmand32f5bf2009-06-22 21:10:22 +00003363 // we won't be able to compute an exact value for the loop.
Dan Gohmana334aa72009-06-22 00:31:57 +00003364 CouldNotComputeBECount = true;
Dan Gohman1c343752009-06-27 21:21:31 +00003365 BECount = getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003366 } else if (!CouldNotComputeBECount) {
Dan Gohman1c343752009-06-27 21:21:31 +00003367 if (BECount == getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003368 BECount = NewBTI.Exact;
Dan Gohmana334aa72009-06-22 00:31:57 +00003369 else
Dan Gohman40a5a1b2009-06-24 01:18:18 +00003370 BECount = getUMinFromMismatchedTypes(BECount, NewBTI.Exact);
Dan Gohmana334aa72009-06-22 00:31:57 +00003371 }
Dan Gohman1c343752009-06-27 21:21:31 +00003372 if (MaxBECount == getCouldNotCompute())
Dan Gohman40a5a1b2009-06-24 01:18:18 +00003373 MaxBECount = NewBTI.Max;
Dan Gohman1c343752009-06-27 21:21:31 +00003374 else if (NewBTI.Max != getCouldNotCompute())
Dan Gohman40a5a1b2009-06-24 01:18:18 +00003375 MaxBECount = getUMinFromMismatchedTypes(MaxBECount, NewBTI.Max);
Dan Gohmana334aa72009-06-22 00:31:57 +00003376 }
3377
3378 return BackedgeTakenInfo(BECount, MaxBECount);
3379}
3380
3381/// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge
3382/// of the specified loop will execute if it exits via the specified block.
3383ScalarEvolution::BackedgeTakenInfo
3384ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L,
3385 BasicBlock *ExitingBlock) {
3386
3387 // Okay, we've chosen an exiting block. See what condition causes us to
3388 // exit at this block.
Chris Lattner53e677a2004-04-02 20:23:17 +00003389 //
3390 // FIXME: we should be able to handle switch instructions (with a single exit)
Chris Lattner53e677a2004-04-02 20:23:17 +00003391 BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
Dan Gohman1c343752009-06-27 21:21:31 +00003392 if (ExitBr == 0) return getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00003393 assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!");
Dan Gohman64a845e2009-06-24 04:48:43 +00003394
Chris Lattner8b0e3602007-01-07 02:24:26 +00003395 // At this point, we know we have a conditional branch that determines whether
3396 // the loop is exited. However, we don't know if the branch is executed each
3397 // time through the loop. If not, then the execution count of the branch will
3398 // not be equal to the trip count of the loop.
3399 //
3400 // Currently we check for this by checking to see if the Exit branch goes to
3401 // the loop header. If so, we know it will always execute the same number of
Chris Lattner192e4032007-01-14 01:24:47 +00003402 // times as the loop. We also handle the case where the exit block *is* the
Dan Gohmana334aa72009-06-22 00:31:57 +00003403 // loop header. This is common for un-rotated loops.
3404 //
3405 // If both of those tests fail, walk up the unique predecessor chain to the
3406 // header, stopping if there is an edge that doesn't exit the loop. If the
3407 // header is reached, the execution count of the branch will be equal to the
3408 // trip count of the loop.
3409 //
3410 // More extensive analysis could be done to handle more cases here.
3411 //
Chris Lattner8b0e3602007-01-07 02:24:26 +00003412 if (ExitBr->getSuccessor(0) != L->getHeader() &&
Chris Lattner192e4032007-01-14 01:24:47 +00003413 ExitBr->getSuccessor(1) != L->getHeader() &&
Dan Gohmana334aa72009-06-22 00:31:57 +00003414 ExitBr->getParent() != L->getHeader()) {
3415 // The simple checks failed, try climbing the unique predecessor chain
3416 // up to the header.
3417 bool Ok = false;
3418 for (BasicBlock *BB = ExitBr->getParent(); BB; ) {
3419 BasicBlock *Pred = BB->getUniquePredecessor();
3420 if (!Pred)
Dan Gohman1c343752009-06-27 21:21:31 +00003421 return getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003422 TerminatorInst *PredTerm = Pred->getTerminator();
3423 for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) {
3424 BasicBlock *PredSucc = PredTerm->getSuccessor(i);
3425 if (PredSucc == BB)
3426 continue;
3427 // If the predecessor has a successor that isn't BB and isn't
3428 // outside the loop, assume the worst.
3429 if (L->contains(PredSucc))
Dan Gohman1c343752009-06-27 21:21:31 +00003430 return getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003431 }
3432 if (Pred == L->getHeader()) {
3433 Ok = true;
3434 break;
3435 }
3436 BB = Pred;
3437 }
3438 if (!Ok)
Dan Gohman1c343752009-06-27 21:21:31 +00003439 return getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003440 }
3441
3442 // Procede to the next level to examine the exit condition expression.
3443 return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(),
3444 ExitBr->getSuccessor(0),
3445 ExitBr->getSuccessor(1));
3446}
3447
3448/// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the
3449/// backedge of the specified loop will execute if its exit condition
3450/// were a conditional branch of ExitCond, TBB, and FBB.
3451ScalarEvolution::BackedgeTakenInfo
3452ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L,
3453 Value *ExitCond,
3454 BasicBlock *TBB,
3455 BasicBlock *FBB) {
Dan Gohman40a5a1b2009-06-24 01:18:18 +00003456 // Check if the controlling expression for this loop is an And or Or.
Dan Gohmana334aa72009-06-22 00:31:57 +00003457 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) {
3458 if (BO->getOpcode() == Instruction::And) {
3459 // Recurse on the operands of the and.
3460 BackedgeTakenInfo BTI0 =
3461 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB);
3462 BackedgeTakenInfo BTI1 =
3463 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB);
Dan Gohman0bba49c2009-07-07 17:06:11 +00003464 const SCEV *BECount = getCouldNotCompute();
3465 const SCEV *MaxBECount = getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003466 if (L->contains(TBB)) {
3467 // Both conditions must be true for the loop to continue executing.
3468 // Choose the less conservative count.
Dan Gohman1c343752009-06-27 21:21:31 +00003469 if (BTI0.Exact == getCouldNotCompute() ||
3470 BTI1.Exact == getCouldNotCompute())
3471 BECount = getCouldNotCompute();
Dan Gohman60e9b072009-06-22 15:09:28 +00003472 else
3473 BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
Dan Gohman1c343752009-06-27 21:21:31 +00003474 if (BTI0.Max == getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003475 MaxBECount = BTI1.Max;
Dan Gohman1c343752009-06-27 21:21:31 +00003476 else if (BTI1.Max == getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003477 MaxBECount = BTI0.Max;
Dan Gohman60e9b072009-06-22 15:09:28 +00003478 else
3479 MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max);
Dan Gohmana334aa72009-06-22 00:31:57 +00003480 } else {
3481 // Both conditions must be true for the loop to exit.
3482 assert(L->contains(FBB) && "Loop block has no successor in loop!");
Dan Gohman1c343752009-06-27 21:21:31 +00003483 if (BTI0.Exact != getCouldNotCompute() &&
3484 BTI1.Exact != getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003485 BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
Dan Gohman1c343752009-06-27 21:21:31 +00003486 if (BTI0.Max != getCouldNotCompute() &&
3487 BTI1.Max != getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003488 MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max);
3489 }
3490
3491 return BackedgeTakenInfo(BECount, MaxBECount);
3492 }
3493 if (BO->getOpcode() == Instruction::Or) {
3494 // Recurse on the operands of the or.
3495 BackedgeTakenInfo BTI0 =
3496 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB);
3497 BackedgeTakenInfo BTI1 =
3498 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB);
Dan Gohman0bba49c2009-07-07 17:06:11 +00003499 const SCEV *BECount = getCouldNotCompute();
3500 const SCEV *MaxBECount = getCouldNotCompute();
Dan Gohmana334aa72009-06-22 00:31:57 +00003501 if (L->contains(FBB)) {
3502 // Both conditions must be false for the loop to continue executing.
3503 // Choose the less conservative count.
Dan Gohman1c343752009-06-27 21:21:31 +00003504 if (BTI0.Exact == getCouldNotCompute() ||
3505 BTI1.Exact == getCouldNotCompute())
3506 BECount = getCouldNotCompute();
Dan Gohman60e9b072009-06-22 15:09:28 +00003507 else
3508 BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
Dan Gohman1c343752009-06-27 21:21:31 +00003509 if (BTI0.Max == getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003510 MaxBECount = BTI1.Max;
Dan Gohman1c343752009-06-27 21:21:31 +00003511 else if (BTI1.Max == getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003512 MaxBECount = BTI0.Max;
Dan Gohman60e9b072009-06-22 15:09:28 +00003513 else
3514 MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max);
Dan Gohmana334aa72009-06-22 00:31:57 +00003515 } else {
3516 // Both conditions must be false for the loop to exit.
3517 assert(L->contains(TBB) && "Loop block has no successor in loop!");
Dan Gohman1c343752009-06-27 21:21:31 +00003518 if (BTI0.Exact != getCouldNotCompute() &&
3519 BTI1.Exact != getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003520 BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
Dan Gohman1c343752009-06-27 21:21:31 +00003521 if (BTI0.Max != getCouldNotCompute() &&
3522 BTI1.Max != getCouldNotCompute())
Dan Gohmana334aa72009-06-22 00:31:57 +00003523 MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max);
3524 }
3525
3526 return BackedgeTakenInfo(BECount, MaxBECount);
3527 }
3528 }
3529
3530 // With an icmp, it may be feasible to compute an exact backedge-taken count.
3531 // Procede to the next level to examine the icmp.
3532 if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond))
3533 return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB);
Reid Spencere4d87aa2006-12-23 06:05:41 +00003534
Eli Friedman361e54d2009-05-09 12:32:42 +00003535 // If it's not an integer or pointer comparison then compute it the hard way.
Dan Gohmana334aa72009-06-22 00:31:57 +00003536 return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB));
3537}
3538
3539/// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the
3540/// backedge of the specified loop will execute if its exit condition
3541/// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB.
3542ScalarEvolution::BackedgeTakenInfo
3543ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L,
3544 ICmpInst *ExitCond,
3545 BasicBlock *TBB,
3546 BasicBlock *FBB) {
Chris Lattner53e677a2004-04-02 20:23:17 +00003547
Reid Spencere4d87aa2006-12-23 06:05:41 +00003548 // If the condition was exit on true, convert the condition to exit on false
3549 ICmpInst::Predicate Cond;
Dan Gohmana334aa72009-06-22 00:31:57 +00003550 if (!L->contains(FBB))
Reid Spencere4d87aa2006-12-23 06:05:41 +00003551 Cond = ExitCond->getPredicate();
Chris Lattner673e02b2004-10-12 01:49:27 +00003552 else
Reid Spencere4d87aa2006-12-23 06:05:41 +00003553 Cond = ExitCond->getInversePredicate();
Chris Lattner673e02b2004-10-12 01:49:27 +00003554
3555 // Handle common loops like: for (X = "string"; *X; ++X)
3556 if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0)))
3557 if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00003558 const SCEV *ItCnt =
Dan Gohman46bdfb02009-02-24 18:55:53 +00003559 ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond);
Dan Gohmana334aa72009-06-22 00:31:57 +00003560 if (!isa<SCEVCouldNotCompute>(ItCnt)) {
3561 unsigned BitWidth = getTypeSizeInBits(ItCnt->getType());
3562 return BackedgeTakenInfo(ItCnt,
3563 isa<SCEVConstant>(ItCnt) ? ItCnt :
3564 getConstant(APInt::getMaxValue(BitWidth)-1));
3565 }
Chris Lattner673e02b2004-10-12 01:49:27 +00003566 }
3567
Dan Gohman0bba49c2009-07-07 17:06:11 +00003568 const SCEV *LHS = getSCEV(ExitCond->getOperand(0));
3569 const SCEV *RHS = getSCEV(ExitCond->getOperand(1));
Chris Lattner53e677a2004-04-02 20:23:17 +00003570
3571 // Try to evaluate any dependencies out of the loop.
Dan Gohmand594e6f2009-05-24 23:25:42 +00003572 LHS = getSCEVAtScope(LHS, L);
3573 RHS = getSCEVAtScope(RHS, L);
Chris Lattner53e677a2004-04-02 20:23:17 +00003574
Dan Gohman64a845e2009-06-24 04:48:43 +00003575 // At this point, we would like to compute how many iterations of the
Reid Spencere4d87aa2006-12-23 06:05:41 +00003576 // loop the predicate will return true for these inputs.
Dan Gohman70ff4cf2008-09-16 18:52:57 +00003577 if (LHS->isLoopInvariant(L) && !RHS->isLoopInvariant(L)) {
3578 // If there is a loop-invariant, force it into the RHS.
Chris Lattner53e677a2004-04-02 20:23:17 +00003579 std::swap(LHS, RHS);
Reid Spencere4d87aa2006-12-23 06:05:41 +00003580 Cond = ICmpInst::getSwappedPredicate(Cond);
Chris Lattner53e677a2004-04-02 20:23:17 +00003581 }
3582
Chris Lattner53e677a2004-04-02 20:23:17 +00003583 // If we have a comparison of a chrec against a constant, try to use value
3584 // ranges to answer this query.
Dan Gohman622ed672009-05-04 22:02:23 +00003585 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
3586 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
Chris Lattner53e677a2004-04-02 20:23:17 +00003587 if (AddRec->getLoop() == L) {
Eli Friedman361e54d2009-05-09 12:32:42 +00003588 // Form the constant range.
3589 ConstantRange CompRange(
3590 ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue()));
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003591
Dan Gohman0bba49c2009-07-07 17:06:11 +00003592 const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this);
Eli Friedman361e54d2009-05-09 12:32:42 +00003593 if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
Chris Lattner53e677a2004-04-02 20:23:17 +00003594 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003595
Chris Lattner53e677a2004-04-02 20:23:17 +00003596 switch (Cond) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00003597 case ICmpInst::ICMP_NE: { // while (X != Y)
Chris Lattner53e677a2004-04-02 20:23:17 +00003598 // Convert to: while (X-Y != 0)
Dan Gohman0bba49c2009-07-07 17:06:11 +00003599 const SCEV *TC = HowFarToZero(getMinusSCEV(LHS, RHS), L);
Reid Spencere4d87aa2006-12-23 06:05:41 +00003600 if (!isa<SCEVCouldNotCompute>(TC)) return TC;
Chris Lattner53e677a2004-04-02 20:23:17 +00003601 break;
Reid Spencere4d87aa2006-12-23 06:05:41 +00003602 }
Dan Gohman4c0d5d52009-08-20 16:42:55 +00003603 case ICmpInst::ICMP_EQ: { // while (X == Y)
3604 // Convert to: while (X-Y == 0)
Dan Gohman0bba49c2009-07-07 17:06:11 +00003605 const SCEV *TC = HowFarToNonZero(getMinusSCEV(LHS, RHS), L);
Reid Spencere4d87aa2006-12-23 06:05:41 +00003606 if (!isa<SCEVCouldNotCompute>(TC)) return TC;
Chris Lattner53e677a2004-04-02 20:23:17 +00003607 break;
Reid Spencere4d87aa2006-12-23 06:05:41 +00003608 }
3609 case ICmpInst::ICMP_SLT: {
Dan Gohmana1af7572009-04-30 20:47:05 +00003610 BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true);
3611 if (BTI.hasAnyInfo()) return BTI;
Chris Lattnerdb25de42005-08-15 23:33:51 +00003612 break;
Reid Spencere4d87aa2006-12-23 06:05:41 +00003613 }
3614 case ICmpInst::ICMP_SGT: {
Dan Gohmana1af7572009-04-30 20:47:05 +00003615 BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS),
3616 getNotSCEV(RHS), L, true);
3617 if (BTI.hasAnyInfo()) return BTI;
Nick Lewyckyd6dac0e2007-08-06 19:21:00 +00003618 break;
3619 }
3620 case ICmpInst::ICMP_ULT: {
Dan Gohmana1af7572009-04-30 20:47:05 +00003621 BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false);
3622 if (BTI.hasAnyInfo()) return BTI;
Nick Lewyckyd6dac0e2007-08-06 19:21:00 +00003623 break;
3624 }
3625 case ICmpInst::ICMP_UGT: {
Dan Gohmana1af7572009-04-30 20:47:05 +00003626 BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS),
3627 getNotSCEV(RHS), L, false);
3628 if (BTI.hasAnyInfo()) return BTI;
Chris Lattnerdb25de42005-08-15 23:33:51 +00003629 break;
Reid Spencere4d87aa2006-12-23 06:05:41 +00003630 }
Chris Lattner53e677a2004-04-02 20:23:17 +00003631 default:
Chris Lattnerd18d9dc2004-04-02 20:26:46 +00003632#if 0
David Greene25e0e872009-12-23 22:18:14 +00003633 dbgs() << "ComputeBackedgeTakenCount ";
Chris Lattner53e677a2004-04-02 20:23:17 +00003634 if (ExitCond->getOperand(0)->getType()->isUnsigned())
David Greene25e0e872009-12-23 22:18:14 +00003635 dbgs() << "[unsigned] ";
3636 dbgs() << *LHS << " "
Dan Gohman64a845e2009-06-24 04:48:43 +00003637 << Instruction::getOpcodeName(Instruction::ICmp)
Reid Spencere4d87aa2006-12-23 06:05:41 +00003638 << " " << *RHS << "\n";
Chris Lattnerd18d9dc2004-04-02 20:26:46 +00003639#endif
Chris Lattnere34c0b42004-04-03 00:43:03 +00003640 break;
Chris Lattner53e677a2004-04-02 20:23:17 +00003641 }
Dan Gohman46bdfb02009-02-24 18:55:53 +00003642 return
Dan Gohmana334aa72009-06-22 00:31:57 +00003643 ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB));
Chris Lattner7980fb92004-04-17 18:36:24 +00003644}
3645
Chris Lattner673e02b2004-10-12 01:49:27 +00003646static ConstantInt *
Dan Gohman246b2562007-10-22 18:31:58 +00003647EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C,
3648 ScalarEvolution &SE) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00003649 const SCEV *InVal = SE.getConstant(C);
3650 const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE);
Chris Lattner673e02b2004-10-12 01:49:27 +00003651 assert(isa<SCEVConstant>(Val) &&
3652 "Evaluation of SCEV at constant didn't fold correctly?");
3653 return cast<SCEVConstant>(Val)->getValue();
3654}
3655
3656/// GetAddressedElementFromGlobal - Given a global variable with an initializer
3657/// and a GEP expression (missing the pointer index) indexing into it, return
3658/// the addressed element of the initializer or null if the index expression is
3659/// invalid.
3660static Constant *
Nick Lewyckyc6501b12009-11-23 03:26:09 +00003661GetAddressedElementFromGlobal(GlobalVariable *GV,
Chris Lattner673e02b2004-10-12 01:49:27 +00003662 const std::vector<ConstantInt*> &Indices) {
3663 Constant *Init = GV->getInitializer();
3664 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
Reid Spencerb83eb642006-10-20 07:07:24 +00003665 uint64_t Idx = Indices[i]->getZExtValue();
Chris Lattner673e02b2004-10-12 01:49:27 +00003666 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) {
3667 assert(Idx < CS->getNumOperands() && "Bad struct index!");
3668 Init = cast<Constant>(CS->getOperand(Idx));
3669 } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) {
3670 if (Idx >= CA->getNumOperands()) return 0; // Bogus program
3671 Init = cast<Constant>(CA->getOperand(Idx));
3672 } else if (isa<ConstantAggregateZero>(Init)) {
3673 if (const StructType *STy = dyn_cast<StructType>(Init->getType())) {
3674 assert(Idx < STy->getNumElements() && "Bad struct index!");
Owen Andersona7235ea2009-07-31 20:28:14 +00003675 Init = Constant::getNullValue(STy->getElementType(Idx));
Chris Lattner673e02b2004-10-12 01:49:27 +00003676 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) {
3677 if (Idx >= ATy->getNumElements()) return 0; // Bogus program
Owen Andersona7235ea2009-07-31 20:28:14 +00003678 Init = Constant::getNullValue(ATy->getElementType());
Chris Lattner673e02b2004-10-12 01:49:27 +00003679 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00003680 llvm_unreachable("Unknown constant aggregate type!");
Chris Lattner673e02b2004-10-12 01:49:27 +00003681 }
3682 return 0;
3683 } else {
3684 return 0; // Unknown initializer type
3685 }
3686 }
3687 return Init;
3688}
3689
Dan Gohman46bdfb02009-02-24 18:55:53 +00003690/// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of
3691/// 'icmp op load X, cst', try to see if we can compute the backedge
3692/// execution count.
Dan Gohman64a845e2009-06-24 04:48:43 +00003693const SCEV *
3694ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount(
3695 LoadInst *LI,
3696 Constant *RHS,
3697 const Loop *L,
3698 ICmpInst::Predicate predicate) {
Dan Gohman1c343752009-06-27 21:21:31 +00003699 if (LI->isVolatile()) return getCouldNotCompute();
Chris Lattner673e02b2004-10-12 01:49:27 +00003700
3701 // Check to see if the loaded pointer is a getelementptr of a global.
3702 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0));
Dan Gohman1c343752009-06-27 21:21:31 +00003703 if (!GEP) return getCouldNotCompute();
Chris Lattner673e02b2004-10-12 01:49:27 +00003704
3705 // Make sure that it is really a constant global we are gepping, with an
3706 // initializer, and make sure the first IDX is really 0.
3707 GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
Dan Gohman82555732009-08-19 18:20:44 +00003708 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
Chris Lattner673e02b2004-10-12 01:49:27 +00003709 GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) ||
3710 !cast<Constant>(GEP->getOperand(1))->isNullValue())
Dan Gohman1c343752009-06-27 21:21:31 +00003711 return getCouldNotCompute();
Chris Lattner673e02b2004-10-12 01:49:27 +00003712
3713 // Okay, we allow one non-constant index into the GEP instruction.
3714 Value *VarIdx = 0;
3715 std::vector<ConstantInt*> Indexes;
3716 unsigned VarIdxNum = 0;
3717 for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i)
3718 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
3719 Indexes.push_back(CI);
3720 } else if (!isa<ConstantInt>(GEP->getOperand(i))) {
Dan Gohman1c343752009-06-27 21:21:31 +00003721 if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's.
Chris Lattner673e02b2004-10-12 01:49:27 +00003722 VarIdx = GEP->getOperand(i);
3723 VarIdxNum = i-2;
3724 Indexes.push_back(0);
3725 }
3726
3727 // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant.
3728 // Check to see if X is a loop variant variable value now.
Dan Gohman0bba49c2009-07-07 17:06:11 +00003729 const SCEV *Idx = getSCEV(VarIdx);
Dan Gohmand594e6f2009-05-24 23:25:42 +00003730 Idx = getSCEVAtScope(Idx, L);
Chris Lattner673e02b2004-10-12 01:49:27 +00003731
3732 // We can only recognize very limited forms of loop index expressions, in
3733 // particular, only affine AddRec's like {C1,+,C2}.
Dan Gohman35738ac2009-05-04 22:30:44 +00003734 const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx);
Chris Lattner673e02b2004-10-12 01:49:27 +00003735 if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) ||
3736 !isa<SCEVConstant>(IdxExpr->getOperand(0)) ||
3737 !isa<SCEVConstant>(IdxExpr->getOperand(1)))
Dan Gohman1c343752009-06-27 21:21:31 +00003738 return getCouldNotCompute();
Chris Lattner673e02b2004-10-12 01:49:27 +00003739
3740 unsigned MaxSteps = MaxBruteForceIterations;
3741 for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
Owen Andersoneed707b2009-07-24 23:12:02 +00003742 ConstantInt *ItCst = ConstantInt::get(
Owen Anderson9adc0ab2009-07-14 23:09:55 +00003743 cast<IntegerType>(IdxExpr->getType()), IterationNum);
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003744 ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this);
Chris Lattner673e02b2004-10-12 01:49:27 +00003745
3746 // Form the GEP offset.
3747 Indexes[VarIdxNum] = Val;
3748
Nick Lewyckyc6501b12009-11-23 03:26:09 +00003749 Constant *Result = GetAddressedElementFromGlobal(GV, Indexes);
Chris Lattner673e02b2004-10-12 01:49:27 +00003750 if (Result == 0) break; // Cannot compute!
3751
3752 // Evaluate the condition for this iteration.
Reid Spencere4d87aa2006-12-23 06:05:41 +00003753 Result = ConstantExpr::getICmp(predicate, Result, RHS);
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00003754 if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure
Reid Spencere8019bb2007-03-01 07:25:48 +00003755 if (cast<ConstantInt>(Result)->getValue().isMinValue()) {
Chris Lattner673e02b2004-10-12 01:49:27 +00003756#if 0
David Greene25e0e872009-12-23 22:18:14 +00003757 dbgs() << "\n***\n*** Computed loop count " << *ItCst
Dan Gohmanb7ef7292009-04-21 00:47:46 +00003758 << "\n*** From global " << *GV << "*** BB: " << *L->getHeader()
3759 << "***\n";
Chris Lattner673e02b2004-10-12 01:49:27 +00003760#endif
3761 ++NumArrayLenItCounts;
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003762 return getConstant(ItCst); // Found terminating iteration!
Chris Lattner673e02b2004-10-12 01:49:27 +00003763 }
3764 }
Dan Gohman1c343752009-06-27 21:21:31 +00003765 return getCouldNotCompute();
Chris Lattner673e02b2004-10-12 01:49:27 +00003766}
3767
3768
Chris Lattner3221ad02004-04-17 22:58:41 +00003769/// CanConstantFold - Return true if we can constant fold an instruction of the
3770/// specified type, assuming that all operands were constants.
3771static bool CanConstantFold(const Instruction *I) {
Reid Spencer832254e2007-02-02 02:16:23 +00003772 if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
Chris Lattner3221ad02004-04-17 22:58:41 +00003773 isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I))
3774 return true;
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003775
Chris Lattner3221ad02004-04-17 22:58:41 +00003776 if (const CallInst *CI = dyn_cast<CallInst>(I))
3777 if (const Function *F = CI->getCalledFunction())
Dan Gohmanfa9b80e2008-01-31 01:05:10 +00003778 return canConstantFoldCallTo(F);
Chris Lattner3221ad02004-04-17 22:58:41 +00003779 return false;
Chris Lattner7980fb92004-04-17 18:36:24 +00003780}
3781
Chris Lattner3221ad02004-04-17 22:58:41 +00003782/// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node
3783/// in the loop that V is derived from. We allow arbitrary operations along the
3784/// way, but the operands of an operation must either be constants or a value
3785/// derived from a constant PHI. If this expression does not fit with these
3786/// constraints, return null.
3787static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) {
3788 // If this is not an instruction, or if this is an instruction outside of the
3789 // loop, it can't be derived from a loop PHI.
3790 Instruction *I = dyn_cast<Instruction>(V);
Dan Gohman92329c72009-12-18 01:24:09 +00003791 if (I == 0 || !L->contains(I)) return 0;
Chris Lattner3221ad02004-04-17 22:58:41 +00003792
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +00003793 if (PHINode *PN = dyn_cast<PHINode>(I)) {
Chris Lattner3221ad02004-04-17 22:58:41 +00003794 if (L->getHeader() == I->getParent())
3795 return PN;
3796 else
3797 // We don't currently keep track of the control flow needed to evaluate
3798 // PHIs, so we cannot handle PHIs inside of loops.
3799 return 0;
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +00003800 }
Chris Lattner3221ad02004-04-17 22:58:41 +00003801
3802 // If we won't be able to constant fold this expression even if the operands
3803 // are constants, return early.
3804 if (!CanConstantFold(I)) return 0;
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003805
Chris Lattner3221ad02004-04-17 22:58:41 +00003806 // Otherwise, we can evaluate this instruction if all of its operands are
3807 // constant or derived from a PHI node themselves.
3808 PHINode *PHI = 0;
3809 for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op)
3810 if (!(isa<Constant>(I->getOperand(Op)) ||
3811 isa<GlobalValue>(I->getOperand(Op)))) {
3812 PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L);
3813 if (P == 0) return 0; // Not evolving from PHI
3814 if (PHI == 0)
3815 PHI = P;
3816 else if (PHI != P)
3817 return 0; // Evolving from multiple different PHIs.
3818 }
3819
3820 // This is a expression evolving from a constant PHI!
3821 return PHI;
3822}
3823
3824/// EvaluateExpression - Given an expression that passes the
3825/// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node
3826/// in the loop has the value PHIVal. If we can't fold this expression for some
3827/// reason, return null.
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00003828static Constant *EvaluateExpression(Value *V, Constant *PHIVal,
3829 const TargetData *TD) {
Chris Lattner3221ad02004-04-17 22:58:41 +00003830 if (isa<PHINode>(V)) return PHIVal;
Reid Spencere8404342004-07-18 00:18:30 +00003831 if (Constant *C = dyn_cast<Constant>(V)) return C;
Dan Gohman2d1be872009-04-16 03:18:22 +00003832 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV;
Chris Lattner3221ad02004-04-17 22:58:41 +00003833 Instruction *I = cast<Instruction>(V);
3834
3835 std::vector<Constant*> Operands;
3836 Operands.resize(I->getNumOperands());
3837
3838 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00003839 Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal, TD);
Chris Lattner3221ad02004-04-17 22:58:41 +00003840 if (Operands[i] == 0) return 0;
3841 }
3842
Chris Lattnerf286f6f2007-12-10 22:53:04 +00003843 if (const CmpInst *CI = dyn_cast<CmpInst>(I))
Chris Lattner8f73dea2009-11-09 23:06:58 +00003844 return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0],
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00003845 Operands[1], TD);
Chris Lattner8f73dea2009-11-09 23:06:58 +00003846 return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00003847 &Operands[0], Operands.size(), TD);
Chris Lattner3221ad02004-04-17 22:58:41 +00003848}
3849
3850/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
3851/// in the header of its containing loop, we know the loop executes a
3852/// constant number of times, and the PHI node is just a recurrence
3853/// involving constants, fold it.
Dan Gohman64a845e2009-06-24 04:48:43 +00003854Constant *
3855ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN,
Dan Gohman5d984912009-12-18 01:14:11 +00003856 const APInt &BEs,
Dan Gohman64a845e2009-06-24 04:48:43 +00003857 const Loop *L) {
Chris Lattner3221ad02004-04-17 22:58:41 +00003858 std::map<PHINode*, Constant*>::iterator I =
3859 ConstantEvolutionLoopExitValue.find(PN);
3860 if (I != ConstantEvolutionLoopExitValue.end())
3861 return I->second;
3862
Dan Gohman46bdfb02009-02-24 18:55:53 +00003863 if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations)))
Chris Lattner3221ad02004-04-17 22:58:41 +00003864 return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it.
3865
3866 Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
3867
3868 // Since the loop is canonicalized, the PHI node must have two entries. One
3869 // entry must be a constant (coming in from outside of the loop), and the
3870 // second must be derived from the same PHI.
3871 bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
3872 Constant *StartCST =
3873 dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge));
3874 if (StartCST == 0)
3875 return RetVal = 0; // Must be a constant.
3876
3877 Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
3878 PHINode *PN2 = getConstantEvolvingPHI(BEValue, L);
3879 if (PN2 != PN)
3880 return RetVal = 0; // Not derived from same PHI.
3881
3882 // Execute the loop symbolically to determine the exit value.
Dan Gohman46bdfb02009-02-24 18:55:53 +00003883 if (BEs.getActiveBits() >= 32)
Reid Spencere8019bb2007-03-01 07:25:48 +00003884 return RetVal = 0; // More than 2^32-1 iterations?? Not doing it!
Chris Lattner3221ad02004-04-17 22:58:41 +00003885
Dan Gohman46bdfb02009-02-24 18:55:53 +00003886 unsigned NumIterations = BEs.getZExtValue(); // must be in range
Reid Spencere8019bb2007-03-01 07:25:48 +00003887 unsigned IterationNum = 0;
Chris Lattner3221ad02004-04-17 22:58:41 +00003888 for (Constant *PHIVal = StartCST; ; ++IterationNum) {
3889 if (IterationNum == NumIterations)
3890 return RetVal = PHIVal; // Got exit value!
3891
3892 // Compute the value of the PHI node for the next iteration.
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00003893 Constant *NextPHI = EvaluateExpression(BEValue, PHIVal, TD);
Chris Lattner3221ad02004-04-17 22:58:41 +00003894 if (NextPHI == PHIVal)
3895 return RetVal = NextPHI; // Stopped evolving!
3896 if (NextPHI == 0)
3897 return 0; // Couldn't evaluate!
3898 PHIVal = NextPHI;
3899 }
3900}
3901
Dan Gohman07ad19b2009-07-27 16:09:48 +00003902/// ComputeBackedgeTakenCountExhaustively - If the loop is known to execute a
Chris Lattner7980fb92004-04-17 18:36:24 +00003903/// constant number of times (the condition evolves only from constants),
3904/// try to evaluate a few iterations of the loop until we get the exit
3905/// condition gets a value of ExitWhen (true or false). If we cannot
Dan Gohman1c343752009-06-27 21:21:31 +00003906/// evaluate the trip count of the loop, return getCouldNotCompute().
Dan Gohman64a845e2009-06-24 04:48:43 +00003907const SCEV *
3908ScalarEvolution::ComputeBackedgeTakenCountExhaustively(const Loop *L,
3909 Value *Cond,
3910 bool ExitWhen) {
Chris Lattner7980fb92004-04-17 18:36:24 +00003911 PHINode *PN = getConstantEvolvingPHI(Cond, L);
Dan Gohman1c343752009-06-27 21:21:31 +00003912 if (PN == 0) return getCouldNotCompute();
Chris Lattner7980fb92004-04-17 18:36:24 +00003913
3914 // Since the loop is canonicalized, the PHI node must have two entries. One
3915 // entry must be a constant (coming in from outside of the loop), and the
3916 // second must be derived from the same PHI.
3917 bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
3918 Constant *StartCST =
3919 dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge));
Dan Gohman1c343752009-06-27 21:21:31 +00003920 if (StartCST == 0) return getCouldNotCompute(); // Must be a constant.
Chris Lattner7980fb92004-04-17 18:36:24 +00003921
3922 Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
3923 PHINode *PN2 = getConstantEvolvingPHI(BEValue, L);
Dan Gohman1c343752009-06-27 21:21:31 +00003924 if (PN2 != PN) return getCouldNotCompute(); // Not derived from same PHI.
Chris Lattner7980fb92004-04-17 18:36:24 +00003925
3926 // Okay, we find a PHI node that defines the trip count of this loop. Execute
3927 // the loop symbolically to determine when the condition gets a value of
3928 // "ExitWhen".
3929 unsigned IterationNum = 0;
3930 unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis.
3931 for (Constant *PHIVal = StartCST;
3932 IterationNum != MaxIterations; ++IterationNum) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00003933 ConstantInt *CondVal =
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00003934 dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal, TD));
Chris Lattner3221ad02004-04-17 22:58:41 +00003935
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00003936 // Couldn't symbolically evaluate.
Dan Gohman1c343752009-06-27 21:21:31 +00003937 if (!CondVal) return getCouldNotCompute();
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00003938
Reid Spencere8019bb2007-03-01 07:25:48 +00003939 if (CondVal->getValue() == uint64_t(ExitWhen)) {
Chris Lattner7980fb92004-04-17 18:36:24 +00003940 ++NumBruteForceTripCountsComputed;
Owen Anderson1d0be152009-08-13 21:58:54 +00003941 return getConstant(Type::getInt32Ty(getContext()), IterationNum);
Chris Lattner7980fb92004-04-17 18:36:24 +00003942 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003943
Chris Lattner3221ad02004-04-17 22:58:41 +00003944 // Compute the value of the PHI node for the next iteration.
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00003945 Constant *NextPHI = EvaluateExpression(BEValue, PHIVal, TD);
Chris Lattner3221ad02004-04-17 22:58:41 +00003946 if (NextPHI == 0 || NextPHI == PHIVal)
Dan Gohman1c343752009-06-27 21:21:31 +00003947 return getCouldNotCompute();// Couldn't evaluate or not making progress...
Chris Lattner3221ad02004-04-17 22:58:41 +00003948 PHIVal = NextPHI;
Chris Lattner7980fb92004-04-17 18:36:24 +00003949 }
3950
3951 // Too many iterations were needed to evaluate.
Dan Gohman1c343752009-06-27 21:21:31 +00003952 return getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00003953}
3954
Dan Gohmane7125f42009-09-03 15:00:26 +00003955/// getSCEVAtScope - Return a SCEV expression for the specified value
Dan Gohman66a7e852009-05-08 20:38:54 +00003956/// at the specified scope in the program. The L value specifies a loop
3957/// nest to evaluate the expression at, where null is the top-level or a
3958/// specified loop is immediately inside of the loop.
3959///
3960/// This method can be used to compute the exit value for a variable defined
3961/// in a loop by querying what the value will hold in the parent loop.
3962///
Dan Gohmand594e6f2009-05-24 23:25:42 +00003963/// In the case that a relevant loop exit value cannot be computed, the
3964/// original value V is returned.
Dan Gohman0bba49c2009-07-07 17:06:11 +00003965const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) {
Dan Gohman42214892009-08-31 21:15:23 +00003966 // Check to see if we've folded this expression at this loop before.
3967 std::map<const Loop *, const SCEV *> &Values = ValuesAtScopes[V];
3968 std::pair<std::map<const Loop *, const SCEV *>::iterator, bool> Pair =
3969 Values.insert(std::make_pair(L, static_cast<const SCEV *>(0)));
3970 if (!Pair.second)
3971 return Pair.first->second ? Pair.first->second : V;
Chris Lattner53e677a2004-04-02 20:23:17 +00003972
Dan Gohman42214892009-08-31 21:15:23 +00003973 // Otherwise compute it.
3974 const SCEV *C = computeSCEVAtScope(V, L);
Dan Gohmana5505cb2009-08-31 21:58:28 +00003975 ValuesAtScopes[V][L] = C;
Dan Gohman42214892009-08-31 21:15:23 +00003976 return C;
3977}
3978
3979const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
Chris Lattner3221ad02004-04-17 22:58:41 +00003980 if (isa<SCEVConstant>(V)) return V;
Misha Brukman2b37d7c2005-04-21 21:13:18 +00003981
Nick Lewycky3e630762008-02-20 06:48:22 +00003982 // If this instruction is evolved from a constant-evolving PHI, compute the
Chris Lattner3221ad02004-04-17 22:58:41 +00003983 // exit value from the loop without using SCEVs.
Dan Gohman622ed672009-05-04 22:02:23 +00003984 if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) {
Chris Lattner3221ad02004-04-17 22:58:41 +00003985 if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00003986 const Loop *LI = (*this->LI)[I->getParent()];
Chris Lattner3221ad02004-04-17 22:58:41 +00003987 if (LI && LI->getParentLoop() == L) // Looking for loop exit value.
3988 if (PHINode *PN = dyn_cast<PHINode>(I))
3989 if (PN->getParent() == LI->getHeader()) {
3990 // Okay, there is no closed form solution for the PHI node. Check
Dan Gohman46bdfb02009-02-24 18:55:53 +00003991 // to see if the loop that contains it has a known backedge-taken
3992 // count. If so, we may be able to force computation of the exit
3993 // value.
Dan Gohman0bba49c2009-07-07 17:06:11 +00003994 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI);
Dan Gohman622ed672009-05-04 22:02:23 +00003995 if (const SCEVConstant *BTCC =
Dan Gohman46bdfb02009-02-24 18:55:53 +00003996 dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
Chris Lattner3221ad02004-04-17 22:58:41 +00003997 // Okay, we know how many times the containing loop executes. If
3998 // this is a constant evolving PHI node, get the final value at
3999 // the specified iteration number.
4000 Constant *RV = getConstantEvolutionLoopExitValue(PN,
Dan Gohman46bdfb02009-02-24 18:55:53 +00004001 BTCC->getValue()->getValue(),
Chris Lattner3221ad02004-04-17 22:58:41 +00004002 LI);
Dan Gohman09987962009-06-29 21:31:18 +00004003 if (RV) return getSCEV(RV);
Chris Lattner3221ad02004-04-17 22:58:41 +00004004 }
4005 }
4006
Reid Spencer09906f32006-12-04 21:33:23 +00004007 // Okay, this is an expression that we cannot symbolically evaluate
Chris Lattner3221ad02004-04-17 22:58:41 +00004008 // into a SCEV. Check to see if it's possible to symbolically evaluate
Reid Spencer09906f32006-12-04 21:33:23 +00004009 // the arguments into constants, and if so, try to constant propagate the
Chris Lattner3221ad02004-04-17 22:58:41 +00004010 // result. This is particularly useful for computing loop exit values.
4011 if (CanConstantFold(I)) {
4012 std::vector<Constant*> Operands;
4013 Operands.reserve(I->getNumOperands());
4014 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
4015 Value *Op = I->getOperand(i);
4016 if (Constant *C = dyn_cast<Constant>(Op)) {
4017 Operands.push_back(C);
Chris Lattner3221ad02004-04-17 22:58:41 +00004018 } else {
Chris Lattner42b5e082007-11-23 08:46:22 +00004019 // If any of the operands is non-constant and if they are
Dan Gohman2d1be872009-04-16 03:18:22 +00004020 // non-integer and non-pointer, don't even try to analyze them
4021 // with scev techniques.
Dan Gohman4acd12a2009-04-30 16:40:30 +00004022 if (!isSCEVable(Op->getType()))
Chris Lattner42b5e082007-11-23 08:46:22 +00004023 return V;
Dan Gohman2d1be872009-04-16 03:18:22 +00004024
Dan Gohman5d984912009-12-18 01:14:11 +00004025 const SCEV *OpV = getSCEVAtScope(Op, L);
Dan Gohman622ed672009-05-04 22:02:23 +00004026 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) {
Dan Gohman4acd12a2009-04-30 16:40:30 +00004027 Constant *C = SC->getValue();
4028 if (C->getType() != Op->getType())
4029 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
4030 Op->getType(),
4031 false),
4032 C, Op->getType());
4033 Operands.push_back(C);
Dan Gohman622ed672009-05-04 22:02:23 +00004034 } else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) {
Dan Gohman4acd12a2009-04-30 16:40:30 +00004035 if (Constant *C = dyn_cast<Constant>(SU->getValue())) {
4036 if (C->getType() != Op->getType())
4037 C =
4038 ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
4039 Op->getType(),
4040 false),
4041 C, Op->getType());
4042 Operands.push_back(C);
4043 } else
Chris Lattner3221ad02004-04-17 22:58:41 +00004044 return V;
4045 } else {
4046 return V;
4047 }
4048 }
4049 }
Dan Gohman64a845e2009-06-24 04:48:43 +00004050
Chris Lattnerf286f6f2007-12-10 22:53:04 +00004051 Constant *C;
4052 if (const CmpInst *CI = dyn_cast<CmpInst>(I))
4053 C = ConstantFoldCompareInstOperands(CI->getPredicate(),
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00004054 Operands[0], Operands[1], TD);
Chris Lattnerf286f6f2007-12-10 22:53:04 +00004055 else
4056 C = ConstantFoldInstOperands(I->getOpcode(), I->getType(),
Dan Gohman1ba3b6c2009-11-09 23:34:17 +00004057 &Operands[0], Operands.size(), TD);
Dan Gohman09987962009-06-29 21:31:18 +00004058 return getSCEV(C);
Chris Lattner3221ad02004-04-17 22:58:41 +00004059 }
4060 }
4061
4062 // This is some other type of SCEVUnknown, just return it.
4063 return V;
4064 }
4065
Dan Gohman622ed672009-05-04 22:02:23 +00004066 if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004067 // Avoid performing the look-up in the common case where the specified
4068 // expression has no loop-variant portions.
4069 for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00004070 const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
Chris Lattner53e677a2004-04-02 20:23:17 +00004071 if (OpAtScope != Comm->getOperand(i)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004072 // Okay, at least one of these operands is loop variant but might be
4073 // foldable. Build a new instance of the folded commutative expression.
Dan Gohman64a845e2009-06-24 04:48:43 +00004074 SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(),
4075 Comm->op_begin()+i);
Chris Lattner53e677a2004-04-02 20:23:17 +00004076 NewOps.push_back(OpAtScope);
4077
4078 for (++i; i != e; ++i) {
4079 OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
Chris Lattner53e677a2004-04-02 20:23:17 +00004080 NewOps.push_back(OpAtScope);
4081 }
4082 if (isa<SCEVAddExpr>(Comm))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004083 return getAddExpr(NewOps);
Nick Lewyckyc54c5612007-11-25 22:41:31 +00004084 if (isa<SCEVMulExpr>(Comm))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004085 return getMulExpr(NewOps);
Nick Lewyckyc54c5612007-11-25 22:41:31 +00004086 if (isa<SCEVSMaxExpr>(Comm))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004087 return getSMaxExpr(NewOps);
Nick Lewycky3e630762008-02-20 06:48:22 +00004088 if (isa<SCEVUMaxExpr>(Comm))
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004089 return getUMaxExpr(NewOps);
Torok Edwinc23197a2009-07-14 16:55:14 +00004090 llvm_unreachable("Unknown commutative SCEV type!");
Chris Lattner53e677a2004-04-02 20:23:17 +00004091 }
4092 }
4093 // If we got here, all operands are loop invariant.
4094 return Comm;
4095 }
4096
Dan Gohman622ed672009-05-04 22:02:23 +00004097 if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00004098 const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L);
4099 const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L);
Nick Lewycky789558d2009-01-13 09:18:58 +00004100 if (LHS == Div->getLHS() && RHS == Div->getRHS())
4101 return Div; // must be loop invariant
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004102 return getUDivExpr(LHS, RHS);
Chris Lattner53e677a2004-04-02 20:23:17 +00004103 }
4104
4105 // If this is a loop recurrence for a loop that does not contain L, then we
4106 // are dealing with the final value computed by the loop.
Dan Gohman622ed672009-05-04 22:02:23 +00004107 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
Dan Gohman92329c72009-12-18 01:24:09 +00004108 if (!L || !AddRec->getLoop()->contains(L)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004109 // To evaluate this recurrence, we need to know how many times the AddRec
4110 // loop iterates. Compute this now.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004111 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop());
Dan Gohman1c343752009-06-27 21:21:31 +00004112 if (BackedgeTakenCount == getCouldNotCompute()) return AddRec;
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004113
Eli Friedmanb42a6262008-08-04 23:49:06 +00004114 // Then, evaluate the AddRec.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004115 return AddRec->evaluateAtIteration(BackedgeTakenCount, *this);
Chris Lattner53e677a2004-04-02 20:23:17 +00004116 }
Dan Gohmand594e6f2009-05-24 23:25:42 +00004117 return AddRec;
Chris Lattner53e677a2004-04-02 20:23:17 +00004118 }
4119
Dan Gohman622ed672009-05-04 22:02:23 +00004120 if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00004121 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohmaneb3948b2009-04-29 22:29:01 +00004122 if (Op == Cast->getOperand())
4123 return Cast; // must be loop invariant
4124 return getZeroExtendExpr(Op, Cast->getType());
4125 }
4126
Dan Gohman622ed672009-05-04 22:02:23 +00004127 if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00004128 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohmaneb3948b2009-04-29 22:29:01 +00004129 if (Op == Cast->getOperand())
4130 return Cast; // must be loop invariant
4131 return getSignExtendExpr(Op, Cast->getType());
4132 }
4133
Dan Gohman622ed672009-05-04 22:02:23 +00004134 if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00004135 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohmaneb3948b2009-04-29 22:29:01 +00004136 if (Op == Cast->getOperand())
4137 return Cast; // must be loop invariant
4138 return getTruncateExpr(Op, Cast->getType());
4139 }
4140
Dan Gohmanc40f17b2009-08-18 16:46:41 +00004141 if (isa<SCEVTargetDataConstant>(V))
4142 return V;
4143
Torok Edwinc23197a2009-07-14 16:55:14 +00004144 llvm_unreachable("Unknown SCEV type!");
Daniel Dunbar8c562e22009-05-18 16:43:04 +00004145 return 0;
Chris Lattner53e677a2004-04-02 20:23:17 +00004146}
4147
Dan Gohman66a7e852009-05-08 20:38:54 +00004148/// getSCEVAtScope - This is a convenience function which does
4149/// getSCEVAtScope(getSCEV(V), L).
Dan Gohman0bba49c2009-07-07 17:06:11 +00004150const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004151 return getSCEVAtScope(getSCEV(V), L);
4152}
4153
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004154/// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the
4155/// following equation:
4156///
4157/// A * X = B (mod N)
4158///
4159/// where N = 2^BW and BW is the common bit width of A and B. The signedness of
4160/// A and B isn't important.
4161///
4162/// If the equation does not have a solution, SCEVCouldNotCompute is returned.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004163static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const APInt &B,
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004164 ScalarEvolution &SE) {
4165 uint32_t BW = A.getBitWidth();
4166 assert(BW == B.getBitWidth() && "Bit widths must be the same.");
4167 assert(A != 0 && "A must be non-zero.");
4168
4169 // 1. D = gcd(A, N)
4170 //
4171 // The gcd of A and N may have only one prime factor: 2. The number of
4172 // trailing zeros in A is its multiplicity
4173 uint32_t Mult2 = A.countTrailingZeros();
4174 // D = 2^Mult2
4175
4176 // 2. Check if B is divisible by D.
4177 //
4178 // B is divisible by D if and only if the multiplicity of prime factor 2 for B
4179 // is not less than multiplicity of this prime factor for D.
4180 if (B.countTrailingZeros() < Mult2)
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00004181 return SE.getCouldNotCompute();
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004182
4183 // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic
4184 // modulo (N / D).
4185 //
4186 // (N / D) may need BW+1 bits in its representation. Hence, we'll use this
4187 // bit width during computations.
4188 APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D
4189 APInt Mod(BW + 1, 0);
4190 Mod.set(BW - Mult2); // Mod = N / D
4191 APInt I = AD.multiplicativeInverse(Mod);
4192
4193 // 4. Compute the minimum unsigned root of the equation:
4194 // I * (B / D) mod (N / D)
4195 APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod);
4196
4197 // The result is guaranteed to be less than 2^BW so we may truncate it to BW
4198 // bits.
4199 return SE.getConstant(Result.trunc(BW));
4200}
Chris Lattner53e677a2004-04-02 20:23:17 +00004201
4202/// SolveQuadraticEquation - Find the roots of the quadratic equation for the
4203/// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which
4204/// might be the same) or two SCEVCouldNotCompute objects.
4205///
Dan Gohman0bba49c2009-07-07 17:06:11 +00004206static std::pair<const SCEV *,const SCEV *>
Dan Gohman246b2562007-10-22 18:31:58 +00004207SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004208 assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
Dan Gohman35738ac2009-05-04 22:30:44 +00004209 const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
4210 const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
4211 const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004212
Chris Lattner53e677a2004-04-02 20:23:17 +00004213 // We currently can only solve this if the coefficients are constants.
Reid Spencere8019bb2007-03-01 07:25:48 +00004214 if (!LC || !MC || !NC) {
Dan Gohman35738ac2009-05-04 22:30:44 +00004215 const SCEV *CNC = SE.getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00004216 return std::make_pair(CNC, CNC);
4217 }
4218
Reid Spencere8019bb2007-03-01 07:25:48 +00004219 uint32_t BitWidth = LC->getValue()->getValue().getBitWidth();
Chris Lattnerfe560b82007-04-15 19:52:49 +00004220 const APInt &L = LC->getValue()->getValue();
4221 const APInt &M = MC->getValue()->getValue();
4222 const APInt &N = NC->getValue()->getValue();
Reid Spencere8019bb2007-03-01 07:25:48 +00004223 APInt Two(BitWidth, 2);
4224 APInt Four(BitWidth, 4);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004225
Dan Gohman64a845e2009-06-24 04:48:43 +00004226 {
Reid Spencere8019bb2007-03-01 07:25:48 +00004227 using namespace APIntOps;
Zhou Sheng414de4d2007-04-07 17:48:27 +00004228 const APInt& C = L;
Reid Spencere8019bb2007-03-01 07:25:48 +00004229 // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C
4230 // The B coefficient is M-N/2
4231 APInt B(M);
4232 B -= sdiv(N,Two);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004233
Reid Spencere8019bb2007-03-01 07:25:48 +00004234 // The A coefficient is N/2
Zhou Sheng414de4d2007-04-07 17:48:27 +00004235 APInt A(N.sdiv(Two));
Chris Lattner53e677a2004-04-02 20:23:17 +00004236
Reid Spencere8019bb2007-03-01 07:25:48 +00004237 // Compute the B^2-4ac term.
4238 APInt SqrtTerm(B);
4239 SqrtTerm *= B;
4240 SqrtTerm -= Four * (A * C);
Chris Lattner53e677a2004-04-02 20:23:17 +00004241
Reid Spencere8019bb2007-03-01 07:25:48 +00004242 // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest
4243 // integer value or else APInt::sqrt() will assert.
4244 APInt SqrtVal(SqrtTerm.sqrt());
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004245
Dan Gohman64a845e2009-06-24 04:48:43 +00004246 // Compute the two solutions for the quadratic formula.
Reid Spencere8019bb2007-03-01 07:25:48 +00004247 // The divisions must be performed as signed divisions.
4248 APInt NegB(-B);
Reid Spencer3e35c8d2007-04-16 02:24:41 +00004249 APInt TwoA( A << 1 );
Nick Lewycky8f4d5eb2008-11-03 02:43:49 +00004250 if (TwoA.isMinValue()) {
Dan Gohman35738ac2009-05-04 22:30:44 +00004251 const SCEV *CNC = SE.getCouldNotCompute();
Nick Lewycky8f4d5eb2008-11-03 02:43:49 +00004252 return std::make_pair(CNC, CNC);
4253 }
4254
Owen Andersone922c022009-07-22 00:24:57 +00004255 LLVMContext &Context = SE.getContext();
Owen Anderson76f600b2009-07-06 22:37:39 +00004256
4257 ConstantInt *Solution1 =
Owen Andersoneed707b2009-07-24 23:12:02 +00004258 ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA));
Owen Anderson76f600b2009-07-06 22:37:39 +00004259 ConstantInt *Solution2 =
Owen Andersoneed707b2009-07-24 23:12:02 +00004260 ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA));
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004261
Dan Gohman64a845e2009-06-24 04:48:43 +00004262 return std::make_pair(SE.getConstant(Solution1),
Dan Gohman246b2562007-10-22 18:31:58 +00004263 SE.getConstant(Solution2));
Reid Spencere8019bb2007-03-01 07:25:48 +00004264 } // end APIntOps namespace
Chris Lattner53e677a2004-04-02 20:23:17 +00004265}
4266
4267/// HowFarToZero - Return the number of times a backedge comparing the specified
Dan Gohman86fbf2f2009-06-06 14:37:11 +00004268/// value to zero will execute. If not computable, return CouldNotCompute.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004269const SCEV *ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004270 // If the value is a constant
Dan Gohman622ed672009-05-04 22:02:23 +00004271 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004272 // If the value is already zero, the branch will execute zero times.
Reid Spencercae57542007-03-02 00:28:52 +00004273 if (C->getValue()->isZero()) return C;
Dan Gohman1c343752009-06-27 21:21:31 +00004274 return getCouldNotCompute(); // Otherwise it will loop infinitely.
Chris Lattner53e677a2004-04-02 20:23:17 +00004275 }
4276
Dan Gohman35738ac2009-05-04 22:30:44 +00004277 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V);
Chris Lattner53e677a2004-04-02 20:23:17 +00004278 if (!AddRec || AddRec->getLoop() != L)
Dan Gohman1c343752009-06-27 21:21:31 +00004279 return getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00004280
4281 if (AddRec->isAffine()) {
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004282 // If this is an affine expression, the execution count of this branch is
4283 // the minimum unsigned root of the following equation:
Chris Lattner53e677a2004-04-02 20:23:17 +00004284 //
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004285 // Start + Step*N = 0 (mod 2^BW)
Chris Lattner53e677a2004-04-02 20:23:17 +00004286 //
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004287 // equivalent to:
4288 //
4289 // Step*N = -Start (mod 2^BW)
4290 //
4291 // where BW is the common bit width of Start and Step.
4292
Chris Lattner53e677a2004-04-02 20:23:17 +00004293 // Get the initial value for the loop.
Dan Gohman64a845e2009-06-24 04:48:43 +00004294 const SCEV *Start = getSCEVAtScope(AddRec->getStart(),
4295 L->getParentLoop());
4296 const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1),
4297 L->getParentLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +00004298
Dan Gohman622ed672009-05-04 22:02:23 +00004299 if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) {
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004300 // For now we handle only constant steps.
Chris Lattner53e677a2004-04-02 20:23:17 +00004301
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004302 // First, handle unitary steps.
4303 if (StepC->getValue()->equalsInt(1)) // 1*N = -Start (mod 2^BW), so:
Dan Gohman4c0d5d52009-08-20 16:42:55 +00004304 return getNegativeSCEV(Start); // N = -Start (as unsigned)
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004305 if (StepC->getValue()->isAllOnesValue()) // -1*N = -Start (mod 2^BW), so:
4306 return Start; // N = Start (as unsigned)
4307
4308 // Then, try to solve the above equation provided that Start is constant.
Dan Gohman622ed672009-05-04 22:02:23 +00004309 if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start))
Wojciech Matyjewiczde0f2382008-07-20 15:55:14 +00004310 return SolveLinEquationWithOverflow(StepC->getValue()->getValue(),
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004311 -StartC->getValue()->getValue(),
4312 *this);
Chris Lattner53e677a2004-04-02 20:23:17 +00004313 }
Chris Lattner42a75512007-01-15 02:27:26 +00004314 } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004315 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
4316 // the quadratic equation to solve it.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004317 std::pair<const SCEV *,const SCEV *> Roots = SolveQuadraticEquation(AddRec,
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004318 *this);
Dan Gohman35738ac2009-05-04 22:30:44 +00004319 const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
4320 const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
Chris Lattner53e677a2004-04-02 20:23:17 +00004321 if (R1) {
Chris Lattnerd18d9dc2004-04-02 20:26:46 +00004322#if 0
David Greene25e0e872009-12-23 22:18:14 +00004323 dbgs() << "HFTZ: " << *V << " - sol#1: " << *R1
Dan Gohmanb7ef7292009-04-21 00:47:46 +00004324 << " sol#2: " << *R2 << "\n";
Chris Lattnerd18d9dc2004-04-02 20:26:46 +00004325#endif
Chris Lattner53e677a2004-04-02 20:23:17 +00004326 // Pick the smallest positive root value.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00004327 if (ConstantInt *CB =
Owen Andersonbaf3c402009-07-29 18:55:55 +00004328 dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT,
Reid Spencere4d87aa2006-12-23 06:05:41 +00004329 R1->getValue(), R2->getValue()))) {
Reid Spencer579dca12007-01-12 04:24:46 +00004330 if (CB->getZExtValue() == false)
Chris Lattner53e677a2004-04-02 20:23:17 +00004331 std::swap(R1, R2); // R1 is the minimum root now.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004332
Chris Lattner53e677a2004-04-02 20:23:17 +00004333 // We can only use this value if the chrec ends up with an exact zero
4334 // value at this index. When solving for "X*X != 5", for example, we
4335 // should not accept a root of 2.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004336 const SCEV *Val = AddRec->evaluateAtIteration(R1, *this);
Dan Gohmancfeb6a42008-06-18 16:23:07 +00004337 if (Val->isZero())
4338 return R1; // We found a quadratic root!
Chris Lattner53e677a2004-04-02 20:23:17 +00004339 }
4340 }
4341 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004342
Dan Gohman1c343752009-06-27 21:21:31 +00004343 return getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00004344}
4345
4346/// HowFarToNonZero - Return the number of times a backedge checking the
4347/// specified value for nonzero will execute. If not computable, return
Dan Gohman86fbf2f2009-06-06 14:37:11 +00004348/// CouldNotCompute
Dan Gohman0bba49c2009-07-07 17:06:11 +00004349const SCEV *ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) {
Chris Lattner53e677a2004-04-02 20:23:17 +00004350 // Loops that look like: while (X == 0) are very strange indeed. We don't
4351 // handle them yet except for the trivial case. This could be expanded in the
4352 // future as needed.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004353
Chris Lattner53e677a2004-04-02 20:23:17 +00004354 // If the value is a constant, check to see if it is known to be non-zero
4355 // already. If so, the backedge will execute zero times.
Dan Gohman622ed672009-05-04 22:02:23 +00004356 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
Nick Lewycky39442af2008-02-21 09:14:53 +00004357 if (!C->getValue()->isNullValue())
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004358 return getIntegerSCEV(0, C->getType());
Dan Gohman1c343752009-06-27 21:21:31 +00004359 return getCouldNotCompute(); // Otherwise it will loop infinitely.
Chris Lattner53e677a2004-04-02 20:23:17 +00004360 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00004361
Chris Lattner53e677a2004-04-02 20:23:17 +00004362 // We could implement others, but I really doubt anyone writes loops like
4363 // this, and if they did, they would already be constant folded.
Dan Gohman1c343752009-06-27 21:21:31 +00004364 return getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00004365}
4366
Dan Gohman859b4822009-05-18 15:36:09 +00004367/// getLoopPredecessor - If the given loop's header has exactly one unique
4368/// predecessor outside the loop, return it. Otherwise return null.
4369///
4370BasicBlock *ScalarEvolution::getLoopPredecessor(const Loop *L) {
4371 BasicBlock *Header = L->getHeader();
4372 BasicBlock *Pred = 0;
4373 for (pred_iterator PI = pred_begin(Header), E = pred_end(Header);
4374 PI != E; ++PI)
4375 if (!L->contains(*PI)) {
4376 if (Pred && Pred != *PI) return 0; // Multiple predecessors.
4377 Pred = *PI;
4378 }
4379 return Pred;
4380}
4381
Dan Gohmanfd6edef2008-09-15 22:18:04 +00004382/// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
4383/// (which may not be an immediate predecessor) which has exactly one
4384/// successor from which BB is reachable, or null if no such block is
4385/// found.
4386///
4387BasicBlock *
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004388ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
Dan Gohman3d739fe2009-04-30 20:48:53 +00004389 // If the block has a unique predecessor, then there is no path from the
4390 // predecessor to the block that does not go through the direct edge
4391 // from the predecessor to the block.
Dan Gohmanfd6edef2008-09-15 22:18:04 +00004392 if (BasicBlock *Pred = BB->getSinglePredecessor())
4393 return Pred;
4394
4395 // A loop's header is defined to be a block that dominates the loop.
Dan Gohman859b4822009-05-18 15:36:09 +00004396 // If the header has a unique predecessor outside the loop, it must be
4397 // a block that has exactly one successor that can reach the loop.
Dan Gohmanf8a8be82009-04-21 23:15:49 +00004398 if (Loop *L = LI->getLoopFor(BB))
Dan Gohman859b4822009-05-18 15:36:09 +00004399 return getLoopPredecessor(L);
Dan Gohmanfd6edef2008-09-15 22:18:04 +00004400
4401 return 0;
4402}
4403
Dan Gohman763bad12009-06-20 00:35:32 +00004404/// HasSameValue - SCEV structural equivalence is usually sufficient for
4405/// testing whether two expressions are equal, however for the purposes of
4406/// looking for a condition guarding a loop, it can be useful to be a little
4407/// more general, since a front-end may have replicated the controlling
4408/// expression.
4409///
Dan Gohman0bba49c2009-07-07 17:06:11 +00004410static bool HasSameValue(const SCEV *A, const SCEV *B) {
Dan Gohman763bad12009-06-20 00:35:32 +00004411 // Quick check to see if they are the same SCEV.
4412 if (A == B) return true;
4413
4414 // Otherwise, if they're both SCEVUnknown, it's possible that they hold
4415 // two different instructions with the same value. Check for this case.
4416 if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A))
4417 if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B))
4418 if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue()))
4419 if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue()))
Dan Gohman041de422009-08-25 17:56:57 +00004420 if (AI->isIdenticalTo(BI) && !AI->mayReadFromMemory())
Dan Gohman763bad12009-06-20 00:35:32 +00004421 return true;
4422
4423 // Otherwise assume they may have a different value.
4424 return false;
4425}
4426
Dan Gohman85b05a22009-07-13 21:35:55 +00004427bool ScalarEvolution::isKnownNegative(const SCEV *S) {
4428 return getSignedRange(S).getSignedMax().isNegative();
4429}
4430
4431bool ScalarEvolution::isKnownPositive(const SCEV *S) {
4432 return getSignedRange(S).getSignedMin().isStrictlyPositive();
4433}
4434
4435bool ScalarEvolution::isKnownNonNegative(const SCEV *S) {
4436 return !getSignedRange(S).getSignedMin().isNegative();
4437}
4438
4439bool ScalarEvolution::isKnownNonPositive(const SCEV *S) {
4440 return !getSignedRange(S).getSignedMax().isStrictlyPositive();
4441}
4442
4443bool ScalarEvolution::isKnownNonZero(const SCEV *S) {
4444 return isKnownNegative(S) || isKnownPositive(S);
4445}
4446
4447bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred,
4448 const SCEV *LHS, const SCEV *RHS) {
4449
4450 if (HasSameValue(LHS, RHS))
4451 return ICmpInst::isTrueWhenEqual(Pred);
4452
4453 switch (Pred) {
4454 default:
Dan Gohman850f7912009-07-16 17:34:36 +00004455 llvm_unreachable("Unexpected ICmpInst::Predicate value!");
Dan Gohman85b05a22009-07-13 21:35:55 +00004456 break;
4457 case ICmpInst::ICMP_SGT:
4458 Pred = ICmpInst::ICMP_SLT;
4459 std::swap(LHS, RHS);
4460 case ICmpInst::ICMP_SLT: {
4461 ConstantRange LHSRange = getSignedRange(LHS);
4462 ConstantRange RHSRange = getSignedRange(RHS);
4463 if (LHSRange.getSignedMax().slt(RHSRange.getSignedMin()))
4464 return true;
4465 if (LHSRange.getSignedMin().sge(RHSRange.getSignedMax()))
4466 return false;
Dan Gohman85b05a22009-07-13 21:35:55 +00004467 break;
4468 }
4469 case ICmpInst::ICMP_SGE:
4470 Pred = ICmpInst::ICMP_SLE;
4471 std::swap(LHS, RHS);
4472 case ICmpInst::ICMP_SLE: {
4473 ConstantRange LHSRange = getSignedRange(LHS);
4474 ConstantRange RHSRange = getSignedRange(RHS);
4475 if (LHSRange.getSignedMax().sle(RHSRange.getSignedMin()))
4476 return true;
4477 if (LHSRange.getSignedMin().sgt(RHSRange.getSignedMax()))
4478 return false;
Dan Gohman85b05a22009-07-13 21:35:55 +00004479 break;
4480 }
4481 case ICmpInst::ICMP_UGT:
4482 Pred = ICmpInst::ICMP_ULT;
4483 std::swap(LHS, RHS);
4484 case ICmpInst::ICMP_ULT: {
4485 ConstantRange LHSRange = getUnsignedRange(LHS);
4486 ConstantRange RHSRange = getUnsignedRange(RHS);
4487 if (LHSRange.getUnsignedMax().ult(RHSRange.getUnsignedMin()))
4488 return true;
4489 if (LHSRange.getUnsignedMin().uge(RHSRange.getUnsignedMax()))
4490 return false;
Dan Gohman85b05a22009-07-13 21:35:55 +00004491 break;
4492 }
4493 case ICmpInst::ICMP_UGE:
4494 Pred = ICmpInst::ICMP_ULE;
4495 std::swap(LHS, RHS);
4496 case ICmpInst::ICMP_ULE: {
4497 ConstantRange LHSRange = getUnsignedRange(LHS);
4498 ConstantRange RHSRange = getUnsignedRange(RHS);
4499 if (LHSRange.getUnsignedMax().ule(RHSRange.getUnsignedMin()))
4500 return true;
4501 if (LHSRange.getUnsignedMin().ugt(RHSRange.getUnsignedMax()))
4502 return false;
Dan Gohman85b05a22009-07-13 21:35:55 +00004503 break;
4504 }
4505 case ICmpInst::ICMP_NE: {
4506 if (getUnsignedRange(LHS).intersectWith(getUnsignedRange(RHS)).isEmptySet())
4507 return true;
4508 if (getSignedRange(LHS).intersectWith(getSignedRange(RHS)).isEmptySet())
4509 return true;
4510
4511 const SCEV *Diff = getMinusSCEV(LHS, RHS);
4512 if (isKnownNonZero(Diff))
4513 return true;
4514 break;
4515 }
4516 case ICmpInst::ICMP_EQ:
Dan Gohmanf117ed42009-07-20 23:54:43 +00004517 // The check at the top of the function catches the case where
4518 // the values are known to be equal.
Dan Gohman85b05a22009-07-13 21:35:55 +00004519 break;
4520 }
4521 return false;
4522}
4523
4524/// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
4525/// protected by a conditional between LHS and RHS. This is used to
4526/// to eliminate casts.
4527bool
4528ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L,
4529 ICmpInst::Predicate Pred,
4530 const SCEV *LHS, const SCEV *RHS) {
4531 // Interpret a null as meaning no loop, where there is obviously no guard
4532 // (interprocedural conditions notwithstanding).
4533 if (!L) return true;
4534
4535 BasicBlock *Latch = L->getLoopLatch();
4536 if (!Latch)
4537 return false;
4538
4539 BranchInst *LoopContinuePredicate =
4540 dyn_cast<BranchInst>(Latch->getTerminator());
4541 if (!LoopContinuePredicate ||
4542 LoopContinuePredicate->isUnconditional())
4543 return false;
4544
Dan Gohman0f4b2852009-07-21 23:03:19 +00004545 return isImpliedCond(LoopContinuePredicate->getCondition(), Pred, LHS, RHS,
4546 LoopContinuePredicate->getSuccessor(0) != L->getHeader());
Dan Gohman85b05a22009-07-13 21:35:55 +00004547}
4548
4549/// isLoopGuardedByCond - Test whether entry to the loop is protected
4550/// by a conditional between LHS and RHS. This is used to help avoid max
4551/// expressions in loop trip counts, and to eliminate casts.
4552bool
4553ScalarEvolution::isLoopGuardedByCond(const Loop *L,
4554 ICmpInst::Predicate Pred,
4555 const SCEV *LHS, const SCEV *RHS) {
Dan Gohman8ea94522009-05-18 16:03:58 +00004556 // Interpret a null as meaning no loop, where there is obviously no guard
4557 // (interprocedural conditions notwithstanding).
4558 if (!L) return false;
4559
Dan Gohman859b4822009-05-18 15:36:09 +00004560 BasicBlock *Predecessor = getLoopPredecessor(L);
4561 BasicBlock *PredecessorDest = L->getHeader();
Nick Lewycky59cff122008-07-12 07:41:32 +00004562
Dan Gohman859b4822009-05-18 15:36:09 +00004563 // Starting at the loop predecessor, climb up the predecessor chain, as long
4564 // as there are predecessors that can be found that have unique successors
Dan Gohmanfd6edef2008-09-15 22:18:04 +00004565 // leading to the original header.
Dan Gohman859b4822009-05-18 15:36:09 +00004566 for (; Predecessor;
4567 PredecessorDest = Predecessor,
4568 Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) {
Dan Gohman38372182008-08-12 20:17:31 +00004569
4570 BranchInst *LoopEntryPredicate =
Dan Gohman859b4822009-05-18 15:36:09 +00004571 dyn_cast<BranchInst>(Predecessor->getTerminator());
Dan Gohman38372182008-08-12 20:17:31 +00004572 if (!LoopEntryPredicate ||
4573 LoopEntryPredicate->isUnconditional())
4574 continue;
4575
Dan Gohman0f4b2852009-07-21 23:03:19 +00004576 if (isImpliedCond(LoopEntryPredicate->getCondition(), Pred, LHS, RHS,
4577 LoopEntryPredicate->getSuccessor(0) != PredecessorDest))
Dan Gohman38372182008-08-12 20:17:31 +00004578 return true;
Nick Lewycky59cff122008-07-12 07:41:32 +00004579 }
4580
Dan Gohman38372182008-08-12 20:17:31 +00004581 return false;
Nick Lewycky59cff122008-07-12 07:41:32 +00004582}
4583
Dan Gohman0f4b2852009-07-21 23:03:19 +00004584/// isImpliedCond - Test whether the condition described by Pred, LHS,
4585/// and RHS is true whenever the given Cond value evaluates to true.
4586bool ScalarEvolution::isImpliedCond(Value *CondValue,
4587 ICmpInst::Predicate Pred,
4588 const SCEV *LHS, const SCEV *RHS,
4589 bool Inverse) {
Dan Gohman40a5a1b2009-06-24 01:18:18 +00004590 // Recursivly handle And and Or conditions.
4591 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CondValue)) {
4592 if (BO->getOpcode() == Instruction::And) {
4593 if (!Inverse)
Dan Gohman0f4b2852009-07-21 23:03:19 +00004594 return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) ||
4595 isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse);
Dan Gohman40a5a1b2009-06-24 01:18:18 +00004596 } else if (BO->getOpcode() == Instruction::Or) {
4597 if (Inverse)
Dan Gohman0f4b2852009-07-21 23:03:19 +00004598 return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) ||
4599 isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse);
Dan Gohman40a5a1b2009-06-24 01:18:18 +00004600 }
4601 }
4602
4603 ICmpInst *ICI = dyn_cast<ICmpInst>(CondValue);
4604 if (!ICI) return false;
4605
Dan Gohman85b05a22009-07-13 21:35:55 +00004606 // Bail if the ICmp's operands' types are wider than the needed type
4607 // before attempting to call getSCEV on them. This avoids infinite
4608 // recursion, since the analysis of widening casts can require loop
4609 // exit condition information for overflow checking, which would
4610 // lead back here.
4611 if (getTypeSizeInBits(LHS->getType()) <
Dan Gohman0f4b2852009-07-21 23:03:19 +00004612 getTypeSizeInBits(ICI->getOperand(0)->getType()))
Dan Gohman85b05a22009-07-13 21:35:55 +00004613 return false;
4614
Dan Gohman0f4b2852009-07-21 23:03:19 +00004615 // Now that we found a conditional branch that dominates the loop, check to
4616 // see if it is the comparison we are looking for.
4617 ICmpInst::Predicate FoundPred;
4618 if (Inverse)
4619 FoundPred = ICI->getInversePredicate();
4620 else
4621 FoundPred = ICI->getPredicate();
4622
4623 const SCEV *FoundLHS = getSCEV(ICI->getOperand(0));
4624 const SCEV *FoundRHS = getSCEV(ICI->getOperand(1));
Dan Gohman85b05a22009-07-13 21:35:55 +00004625
4626 // Balance the types. The case where FoundLHS' type is wider than
4627 // LHS' type is checked for above.
4628 if (getTypeSizeInBits(LHS->getType()) >
4629 getTypeSizeInBits(FoundLHS->getType())) {
4630 if (CmpInst::isSigned(Pred)) {
4631 FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType());
4632 FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType());
4633 } else {
4634 FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType());
4635 FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType());
4636 }
4637 }
4638
Dan Gohman0f4b2852009-07-21 23:03:19 +00004639 // Canonicalize the query to match the way instcombine will have
4640 // canonicalized the comparison.
4641 // First, put a constant operand on the right.
4642 if (isa<SCEVConstant>(LHS)) {
4643 std::swap(LHS, RHS);
4644 Pred = ICmpInst::getSwappedPredicate(Pred);
4645 }
4646 // Then, canonicalize comparisons with boundary cases.
4647 if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) {
4648 const APInt &RA = RC->getValue()->getValue();
4649 switch (Pred) {
4650 default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
4651 case ICmpInst::ICMP_EQ:
4652 case ICmpInst::ICMP_NE:
4653 break;
4654 case ICmpInst::ICMP_UGE:
4655 if ((RA - 1).isMinValue()) {
4656 Pred = ICmpInst::ICMP_NE;
4657 RHS = getConstant(RA - 1);
4658 break;
4659 }
4660 if (RA.isMaxValue()) {
4661 Pred = ICmpInst::ICMP_EQ;
4662 break;
4663 }
4664 if (RA.isMinValue()) return true;
4665 break;
4666 case ICmpInst::ICMP_ULE:
4667 if ((RA + 1).isMaxValue()) {
4668 Pred = ICmpInst::ICMP_NE;
4669 RHS = getConstant(RA + 1);
4670 break;
4671 }
4672 if (RA.isMinValue()) {
4673 Pred = ICmpInst::ICMP_EQ;
4674 break;
4675 }
4676 if (RA.isMaxValue()) return true;
4677 break;
4678 case ICmpInst::ICMP_SGE:
4679 if ((RA - 1).isMinSignedValue()) {
4680 Pred = ICmpInst::ICMP_NE;
4681 RHS = getConstant(RA - 1);
4682 break;
4683 }
4684 if (RA.isMaxSignedValue()) {
4685 Pred = ICmpInst::ICMP_EQ;
4686 break;
4687 }
4688 if (RA.isMinSignedValue()) return true;
4689 break;
4690 case ICmpInst::ICMP_SLE:
4691 if ((RA + 1).isMaxSignedValue()) {
4692 Pred = ICmpInst::ICMP_NE;
4693 RHS = getConstant(RA + 1);
4694 break;
4695 }
4696 if (RA.isMinSignedValue()) {
4697 Pred = ICmpInst::ICMP_EQ;
4698 break;
4699 }
4700 if (RA.isMaxSignedValue()) return true;
4701 break;
4702 case ICmpInst::ICMP_UGT:
4703 if (RA.isMinValue()) {
4704 Pred = ICmpInst::ICMP_NE;
4705 break;
4706 }
4707 if ((RA + 1).isMaxValue()) {
4708 Pred = ICmpInst::ICMP_EQ;
4709 RHS = getConstant(RA + 1);
4710 break;
4711 }
4712 if (RA.isMaxValue()) return false;
4713 break;
4714 case ICmpInst::ICMP_ULT:
4715 if (RA.isMaxValue()) {
4716 Pred = ICmpInst::ICMP_NE;
4717 break;
4718 }
4719 if ((RA - 1).isMinValue()) {
4720 Pred = ICmpInst::ICMP_EQ;
4721 RHS = getConstant(RA - 1);
4722 break;
4723 }
4724 if (RA.isMinValue()) return false;
4725 break;
4726 case ICmpInst::ICMP_SGT:
4727 if (RA.isMinSignedValue()) {
4728 Pred = ICmpInst::ICMP_NE;
4729 break;
4730 }
4731 if ((RA + 1).isMaxSignedValue()) {
4732 Pred = ICmpInst::ICMP_EQ;
4733 RHS = getConstant(RA + 1);
4734 break;
4735 }
4736 if (RA.isMaxSignedValue()) return false;
4737 break;
4738 case ICmpInst::ICMP_SLT:
4739 if (RA.isMaxSignedValue()) {
4740 Pred = ICmpInst::ICMP_NE;
4741 break;
4742 }
4743 if ((RA - 1).isMinSignedValue()) {
4744 Pred = ICmpInst::ICMP_EQ;
4745 RHS = getConstant(RA - 1);
4746 break;
4747 }
4748 if (RA.isMinSignedValue()) return false;
4749 break;
4750 }
4751 }
4752
4753 // Check to see if we can make the LHS or RHS match.
4754 if (LHS == FoundRHS || RHS == FoundLHS) {
4755 if (isa<SCEVConstant>(RHS)) {
4756 std::swap(FoundLHS, FoundRHS);
4757 FoundPred = ICmpInst::getSwappedPredicate(FoundPred);
4758 } else {
4759 std::swap(LHS, RHS);
4760 Pred = ICmpInst::getSwappedPredicate(Pred);
4761 }
4762 }
4763
4764 // Check whether the found predicate is the same as the desired predicate.
4765 if (FoundPred == Pred)
4766 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS);
4767
4768 // Check whether swapping the found predicate makes it the same as the
4769 // desired predicate.
4770 if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) {
4771 if (isa<SCEVConstant>(RHS))
4772 return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS);
4773 else
4774 return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred),
4775 RHS, LHS, FoundLHS, FoundRHS);
4776 }
4777
4778 // Check whether the actual condition is beyond sufficient.
4779 if (FoundPred == ICmpInst::ICMP_EQ)
4780 if (ICmpInst::isTrueWhenEqual(Pred))
4781 if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS))
4782 return true;
4783 if (Pred == ICmpInst::ICMP_NE)
4784 if (!ICmpInst::isTrueWhenEqual(FoundPred))
4785 if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS))
4786 return true;
4787
4788 // Otherwise assume the worst.
4789 return false;
Dan Gohman85b05a22009-07-13 21:35:55 +00004790}
4791
Dan Gohman0f4b2852009-07-21 23:03:19 +00004792/// isImpliedCondOperands - Test whether the condition described by Pred,
4793/// LHS, and RHS is true whenever the condition desribed by Pred, FoundLHS,
4794/// and FoundRHS is true.
4795bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred,
4796 const SCEV *LHS, const SCEV *RHS,
4797 const SCEV *FoundLHS,
4798 const SCEV *FoundRHS) {
4799 return isImpliedCondOperandsHelper(Pred, LHS, RHS,
4800 FoundLHS, FoundRHS) ||
4801 // ~x < ~y --> x > y
4802 isImpliedCondOperandsHelper(Pred, LHS, RHS,
4803 getNotSCEV(FoundRHS),
4804 getNotSCEV(FoundLHS));
4805}
4806
4807/// isImpliedCondOperandsHelper - Test whether the condition described by
4808/// Pred, LHS, and RHS is true whenever the condition desribed by Pred,
4809/// FoundLHS, and FoundRHS is true.
Dan Gohman85b05a22009-07-13 21:35:55 +00004810bool
Dan Gohman0f4b2852009-07-21 23:03:19 +00004811ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
4812 const SCEV *LHS, const SCEV *RHS,
4813 const SCEV *FoundLHS,
4814 const SCEV *FoundRHS) {
Dan Gohman85b05a22009-07-13 21:35:55 +00004815 switch (Pred) {
Dan Gohman850f7912009-07-16 17:34:36 +00004816 default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
4817 case ICmpInst::ICMP_EQ:
4818 case ICmpInst::ICMP_NE:
4819 if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS))
4820 return true;
4821 break;
Dan Gohman85b05a22009-07-13 21:35:55 +00004822 case ICmpInst::ICMP_SLT:
Dan Gohman850f7912009-07-16 17:34:36 +00004823 case ICmpInst::ICMP_SLE:
Dan Gohman85b05a22009-07-13 21:35:55 +00004824 if (isKnownPredicate(ICmpInst::ICMP_SLE, LHS, FoundLHS) &&
4825 isKnownPredicate(ICmpInst::ICMP_SGE, RHS, FoundRHS))
4826 return true;
4827 break;
4828 case ICmpInst::ICMP_SGT:
Dan Gohman850f7912009-07-16 17:34:36 +00004829 case ICmpInst::ICMP_SGE:
Dan Gohman85b05a22009-07-13 21:35:55 +00004830 if (isKnownPredicate(ICmpInst::ICMP_SGE, LHS, FoundLHS) &&
4831 isKnownPredicate(ICmpInst::ICMP_SLE, RHS, FoundRHS))
4832 return true;
4833 break;
4834 case ICmpInst::ICMP_ULT:
Dan Gohman850f7912009-07-16 17:34:36 +00004835 case ICmpInst::ICMP_ULE:
Dan Gohman85b05a22009-07-13 21:35:55 +00004836 if (isKnownPredicate(ICmpInst::ICMP_ULE, LHS, FoundLHS) &&
4837 isKnownPredicate(ICmpInst::ICMP_UGE, RHS, FoundRHS))
4838 return true;
4839 break;
4840 case ICmpInst::ICMP_UGT:
Dan Gohman850f7912009-07-16 17:34:36 +00004841 case ICmpInst::ICMP_UGE:
Dan Gohman85b05a22009-07-13 21:35:55 +00004842 if (isKnownPredicate(ICmpInst::ICMP_UGE, LHS, FoundLHS) &&
4843 isKnownPredicate(ICmpInst::ICMP_ULE, RHS, FoundRHS))
4844 return true;
4845 break;
4846 }
4847
4848 return false;
Dan Gohman40a5a1b2009-06-24 01:18:18 +00004849}
4850
Dan Gohman51f53b72009-06-21 23:46:38 +00004851/// getBECount - Subtract the end and start values and divide by the step,
4852/// rounding up, to get the number of times the backedge is executed. Return
4853/// CouldNotCompute if an intermediate computation overflows.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004854const SCEV *ScalarEvolution::getBECount(const SCEV *Start,
Dan Gohmanf5074ec2009-07-13 22:05:32 +00004855 const SCEV *End,
Dan Gohman1f96e672009-09-17 18:05:20 +00004856 const SCEV *Step,
4857 bool NoWrap) {
Dan Gohman51f53b72009-06-21 23:46:38 +00004858 const Type *Ty = Start->getType();
Dan Gohman0bba49c2009-07-07 17:06:11 +00004859 const SCEV *NegOne = getIntegerSCEV(-1, Ty);
4860 const SCEV *Diff = getMinusSCEV(End, Start);
4861 const SCEV *RoundUp = getAddExpr(Step, NegOne);
Dan Gohman51f53b72009-06-21 23:46:38 +00004862
4863 // Add an adjustment to the difference between End and Start so that
4864 // the division will effectively round up.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004865 const SCEV *Add = getAddExpr(Diff, RoundUp);
Dan Gohman51f53b72009-06-21 23:46:38 +00004866
Dan Gohman1f96e672009-09-17 18:05:20 +00004867 if (!NoWrap) {
4868 // Check Add for unsigned overflow.
4869 // TODO: More sophisticated things could be done here.
4870 const Type *WideTy = IntegerType::get(getContext(),
4871 getTypeSizeInBits(Ty) + 1);
4872 const SCEV *EDiff = getZeroExtendExpr(Diff, WideTy);
4873 const SCEV *ERoundUp = getZeroExtendExpr(RoundUp, WideTy);
4874 const SCEV *OperandExtendedAdd = getAddExpr(EDiff, ERoundUp);
4875 if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd)
4876 return getCouldNotCompute();
4877 }
Dan Gohman51f53b72009-06-21 23:46:38 +00004878
4879 return getUDivExpr(Add, Step);
4880}
4881
Chris Lattnerdb25de42005-08-15 23:33:51 +00004882/// HowManyLessThans - Return the number of times a backedge containing the
4883/// specified less-than comparison will execute. If not computable, return
Dan Gohman86fbf2f2009-06-06 14:37:11 +00004884/// CouldNotCompute.
Dan Gohman64a845e2009-06-24 04:48:43 +00004885ScalarEvolution::BackedgeTakenInfo
4886ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
4887 const Loop *L, bool isSigned) {
Chris Lattnerdb25de42005-08-15 23:33:51 +00004888 // Only handle: "ADDREC < LoopInvariant".
Dan Gohman1c343752009-06-27 21:21:31 +00004889 if (!RHS->isLoopInvariant(L)) return getCouldNotCompute();
Chris Lattnerdb25de42005-08-15 23:33:51 +00004890
Dan Gohman35738ac2009-05-04 22:30:44 +00004891 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS);
Chris Lattnerdb25de42005-08-15 23:33:51 +00004892 if (!AddRec || AddRec->getLoop() != L)
Dan Gohman1c343752009-06-27 21:21:31 +00004893 return getCouldNotCompute();
Chris Lattnerdb25de42005-08-15 23:33:51 +00004894
Dan Gohman1f96e672009-09-17 18:05:20 +00004895 // Check to see if we have a flag which makes analysis easy.
4896 bool NoWrap = isSigned ? AddRec->hasNoSignedWrap() :
4897 AddRec->hasNoUnsignedWrap();
4898
Chris Lattnerdb25de42005-08-15 23:33:51 +00004899 if (AddRec->isAffine()) {
Nick Lewycky789558d2009-01-13 09:18:58 +00004900 // FORNOW: We only support unit strides.
Dan Gohmana1af7572009-04-30 20:47:05 +00004901 unsigned BitWidth = getTypeSizeInBits(AddRec->getType());
Dan Gohman0bba49c2009-07-07 17:06:11 +00004902 const SCEV *Step = AddRec->getStepRecurrence(*this);
Dan Gohmana1af7572009-04-30 20:47:05 +00004903
4904 // TODO: handle non-constant strides.
4905 const SCEVConstant *CStep = dyn_cast<SCEVConstant>(Step);
4906 if (!CStep || CStep->isZero())
Dan Gohman1c343752009-06-27 21:21:31 +00004907 return getCouldNotCompute();
Dan Gohman70a1fe72009-05-18 15:22:39 +00004908 if (CStep->isOne()) {
Dan Gohmana1af7572009-04-30 20:47:05 +00004909 // With unit stride, the iteration never steps past the limit value.
4910 } else if (CStep->getValue()->getValue().isStrictlyPositive()) {
Dan Gohman1f96e672009-09-17 18:05:20 +00004911 if (NoWrap) {
4912 // We know the iteration won't step past the maximum value for its type.
4913 ;
4914 } else if (const SCEVConstant *CLimit = dyn_cast<SCEVConstant>(RHS)) {
Dan Gohmana1af7572009-04-30 20:47:05 +00004915 // Test whether a positive iteration iteration can step past the limit
4916 // value and past the maximum value for its type in a single step.
4917 if (isSigned) {
4918 APInt Max = APInt::getSignedMaxValue(BitWidth);
4919 if ((Max - CStep->getValue()->getValue())
4920 .slt(CLimit->getValue()->getValue()))
Dan Gohman1c343752009-06-27 21:21:31 +00004921 return getCouldNotCompute();
Dan Gohmana1af7572009-04-30 20:47:05 +00004922 } else {
4923 APInt Max = APInt::getMaxValue(BitWidth);
4924 if ((Max - CStep->getValue()->getValue())
4925 .ult(CLimit->getValue()->getValue()))
Dan Gohman1c343752009-06-27 21:21:31 +00004926 return getCouldNotCompute();
Dan Gohmana1af7572009-04-30 20:47:05 +00004927 }
4928 } else
4929 // TODO: handle non-constant limit values below.
Dan Gohman1c343752009-06-27 21:21:31 +00004930 return getCouldNotCompute();
Dan Gohmana1af7572009-04-30 20:47:05 +00004931 } else
4932 // TODO: handle negative strides below.
Dan Gohman1c343752009-06-27 21:21:31 +00004933 return getCouldNotCompute();
Chris Lattnerdb25de42005-08-15 23:33:51 +00004934
Dan Gohmana1af7572009-04-30 20:47:05 +00004935 // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant
4936 // m. So, we count the number of iterations in which {n,+,s} < m is true.
4937 // Note that we cannot simply return max(m-n,0)/s because it's not safe to
Wojciech Matyjewicza65ee032008-02-13 12:21:32 +00004938 // treat m-n as signed nor unsigned due to overflow possibility.
Chris Lattnerdb25de42005-08-15 23:33:51 +00004939
Wojciech Matyjewicz3a4cbe22008-02-13 11:51:34 +00004940 // First, we get the value of the LHS in the first iteration: n
Dan Gohman0bba49c2009-07-07 17:06:11 +00004941 const SCEV *Start = AddRec->getOperand(0);
Wojciech Matyjewicz3a4cbe22008-02-13 11:51:34 +00004942
Dan Gohmana1af7572009-04-30 20:47:05 +00004943 // Determine the minimum constant start value.
Dan Gohman85b05a22009-07-13 21:35:55 +00004944 const SCEV *MinStart = getConstant(isSigned ?
4945 getSignedRange(Start).getSignedMin() :
4946 getUnsignedRange(Start).getUnsignedMin());
Wojciech Matyjewicz3a4cbe22008-02-13 11:51:34 +00004947
Dan Gohmana1af7572009-04-30 20:47:05 +00004948 // If we know that the condition is true in order to enter the loop,
4949 // then we know that it will run exactly (m-n)/s times. Otherwise, we
Dan Gohman6c0866c2009-05-24 23:45:28 +00004950 // only know that it will execute (max(m,n)-n)/s times. In both cases,
4951 // the division must round up.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004952 const SCEV *End = RHS;
Dan Gohmana1af7572009-04-30 20:47:05 +00004953 if (!isLoopGuardedByCond(L,
Dan Gohman85b05a22009-07-13 21:35:55 +00004954 isSigned ? ICmpInst::ICMP_SLT :
4955 ICmpInst::ICMP_ULT,
Dan Gohmana1af7572009-04-30 20:47:05 +00004956 getMinusSCEV(Start, Step), RHS))
4957 End = isSigned ? getSMaxExpr(RHS, Start)
4958 : getUMaxExpr(RHS, Start);
4959
4960 // Determine the maximum constant end value.
Dan Gohman85b05a22009-07-13 21:35:55 +00004961 const SCEV *MaxEnd = getConstant(isSigned ?
4962 getSignedRange(End).getSignedMax() :
4963 getUnsignedRange(End).getUnsignedMax());
Dan Gohmana1af7572009-04-30 20:47:05 +00004964
4965 // Finally, we subtract these two values and divide, rounding up, to get
4966 // the number of times the backedge is executed.
Dan Gohman1f96e672009-09-17 18:05:20 +00004967 const SCEV *BECount = getBECount(Start, End, Step, NoWrap);
Dan Gohmana1af7572009-04-30 20:47:05 +00004968
4969 // The maximum backedge count is similar, except using the minimum start
4970 // value and the maximum end value.
Dan Gohman1f96e672009-09-17 18:05:20 +00004971 const SCEV *MaxBECount = getBECount(MinStart, MaxEnd, Step, NoWrap);
Dan Gohmana1af7572009-04-30 20:47:05 +00004972
4973 return BackedgeTakenInfo(BECount, MaxBECount);
Chris Lattnerdb25de42005-08-15 23:33:51 +00004974 }
4975
Dan Gohman1c343752009-06-27 21:21:31 +00004976 return getCouldNotCompute();
Chris Lattnerdb25de42005-08-15 23:33:51 +00004977}
4978
Chris Lattner53e677a2004-04-02 20:23:17 +00004979/// getNumIterationsInRange - Return the number of iterations of this loop that
4980/// produce values in the specified constant range. Another way of looking at
4981/// this is that it returns the first iteration number where the value is not in
4982/// the condition, thus computing the exit count. If the iteration count can't
4983/// be computed, an instance of SCEVCouldNotCompute is returned.
Dan Gohman0bba49c2009-07-07 17:06:11 +00004984const SCEV *SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
Dan Gohman64a845e2009-06-24 04:48:43 +00004985 ScalarEvolution &SE) const {
Chris Lattner53e677a2004-04-02 20:23:17 +00004986 if (Range.isFullSet()) // Infinite loop.
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00004987 return SE.getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00004988
4989 // If the start is a non-zero constant, shift the range to simplify things.
Dan Gohman622ed672009-05-04 22:02:23 +00004990 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart()))
Reid Spencercae57542007-03-02 00:28:52 +00004991 if (!SC->getValue()->isZero()) {
Dan Gohman0bba49c2009-07-07 17:06:11 +00004992 SmallVector<const SCEV *, 4> Operands(op_begin(), op_end());
Dan Gohman246b2562007-10-22 18:31:58 +00004993 Operands[0] = SE.getIntegerSCEV(0, SC->getType());
Dan Gohman0bba49c2009-07-07 17:06:11 +00004994 const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop());
Dan Gohman622ed672009-05-04 22:02:23 +00004995 if (const SCEVAddRecExpr *ShiftedAddRec =
4996 dyn_cast<SCEVAddRecExpr>(Shifted))
Chris Lattner53e677a2004-04-02 20:23:17 +00004997 return ShiftedAddRec->getNumIterationsInRange(
Dan Gohman246b2562007-10-22 18:31:58 +00004998 Range.subtract(SC->getValue()->getValue()), SE);
Chris Lattner53e677a2004-04-02 20:23:17 +00004999 // This is strange and shouldn't happen.
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00005000 return SE.getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00005001 }
5002
5003 // The only time we can solve this is when we have all constant indices.
5004 // Otherwise, we cannot determine the overflow conditions.
5005 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
5006 if (!isa<SCEVConstant>(getOperand(i)))
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00005007 return SE.getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00005008
5009
5010 // Okay at this point we know that all elements of the chrec are constants and
5011 // that the start element is zero.
5012
5013 // First check to see if the range contains zero. If not, the first
5014 // iteration exits.
Dan Gohmanaf79fb52009-04-21 01:07:12 +00005015 unsigned BitWidth = SE.getTypeSizeInBits(getType());
Dan Gohman2d1be872009-04-16 03:18:22 +00005016 if (!Range.contains(APInt(BitWidth, 0)))
Dan Gohman6de29f82009-06-15 22:12:54 +00005017 return SE.getIntegerSCEV(0, getType());
Misha Brukman2b37d7c2005-04-21 21:13:18 +00005018
Chris Lattner53e677a2004-04-02 20:23:17 +00005019 if (isAffine()) {
5020 // If this is an affine expression then we have this situation:
5021 // Solve {0,+,A} in Range === Ax in Range
5022
Nick Lewyckyeefdebe2007-07-16 02:08:00 +00005023 // We know that zero is in the range. If A is positive then we know that
5024 // the upper value of the range must be the first possible exit value.
5025 // If A is negative then the lower of the range is the last possible loop
5026 // value. Also note that we already checked for a full range.
Dan Gohman2d1be872009-04-16 03:18:22 +00005027 APInt One(BitWidth,1);
Nick Lewyckyeefdebe2007-07-16 02:08:00 +00005028 APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue();
5029 APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower();
Chris Lattner53e677a2004-04-02 20:23:17 +00005030
Nick Lewyckyeefdebe2007-07-16 02:08:00 +00005031 // The exit value should be (End+A)/A.
Nick Lewycky9a2f9312007-09-27 14:12:54 +00005032 APInt ExitVal = (End + A).udiv(A);
Owen Andersoneed707b2009-07-24 23:12:02 +00005033 ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal);
Chris Lattner53e677a2004-04-02 20:23:17 +00005034
5035 // Evaluate at the exit value. If we really did fall out of the valid
5036 // range, then we computed our trip count, otherwise wrap around or other
5037 // things must have happened.
Dan Gohman246b2562007-10-22 18:31:58 +00005038 ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE);
Reid Spencera6e8a952007-03-01 07:54:15 +00005039 if (Range.contains(Val->getValue()))
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00005040 return SE.getCouldNotCompute(); // Something strange happened
Chris Lattner53e677a2004-04-02 20:23:17 +00005041
5042 // Ensure that the previous value is in the range. This is a sanity check.
Reid Spencer581b0d42007-02-28 19:57:34 +00005043 assert(Range.contains(
Dan Gohman64a845e2009-06-24 04:48:43 +00005044 EvaluateConstantChrecAtConstant(this,
Owen Andersoneed707b2009-07-24 23:12:02 +00005045 ConstantInt::get(SE.getContext(), ExitVal - One), SE)->getValue()) &&
Chris Lattner53e677a2004-04-02 20:23:17 +00005046 "Linear scev computation is off in a bad way!");
Dan Gohman246b2562007-10-22 18:31:58 +00005047 return SE.getConstant(ExitValue);
Chris Lattner53e677a2004-04-02 20:23:17 +00005048 } else if (isQuadratic()) {
5049 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the
5050 // quadratic equation to solve it. To do this, we must frame our problem in
5051 // terms of figuring out when zero is crossed, instead of when
5052 // Range.getUpper() is crossed.
Dan Gohman0bba49c2009-07-07 17:06:11 +00005053 SmallVector<const SCEV *, 4> NewOps(op_begin(), op_end());
Dan Gohman246b2562007-10-22 18:31:58 +00005054 NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper()));
Dan Gohman0bba49c2009-07-07 17:06:11 +00005055 const SCEV *NewAddRec = SE.getAddRecExpr(NewOps, getLoop());
Chris Lattner53e677a2004-04-02 20:23:17 +00005056
5057 // Next, solve the constructed addrec
Dan Gohman0bba49c2009-07-07 17:06:11 +00005058 std::pair<const SCEV *,const SCEV *> Roots =
Dan Gohman246b2562007-10-22 18:31:58 +00005059 SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE);
Dan Gohman35738ac2009-05-04 22:30:44 +00005060 const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
5061 const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
Chris Lattner53e677a2004-04-02 20:23:17 +00005062 if (R1) {
5063 // Pick the smallest positive root value.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00005064 if (ConstantInt *CB =
Owen Andersonbaf3c402009-07-29 18:55:55 +00005065 dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT,
Owen Anderson76f600b2009-07-06 22:37:39 +00005066 R1->getValue(), R2->getValue()))) {
Reid Spencer579dca12007-01-12 04:24:46 +00005067 if (CB->getZExtValue() == false)
Chris Lattner53e677a2004-04-02 20:23:17 +00005068 std::swap(R1, R2); // R1 is the minimum root now.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00005069
Chris Lattner53e677a2004-04-02 20:23:17 +00005070 // Make sure the root is not off by one. The returned iteration should
5071 // not be in the range, but the previous one should be. When solving
5072 // for "X*X < 5", for example, we should not return a root of 2.
5073 ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this,
Dan Gohman246b2562007-10-22 18:31:58 +00005074 R1->getValue(),
5075 SE);
Reid Spencera6e8a952007-03-01 07:54:15 +00005076 if (Range.contains(R1Val->getValue())) {
Chris Lattner53e677a2004-04-02 20:23:17 +00005077 // The next iteration must be out of the range...
Owen Anderson76f600b2009-07-06 22:37:39 +00005078 ConstantInt *NextVal =
Owen Andersoneed707b2009-07-24 23:12:02 +00005079 ConstantInt::get(SE.getContext(), R1->getValue()->getValue()+1);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00005080
Dan Gohman246b2562007-10-22 18:31:58 +00005081 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
Reid Spencera6e8a952007-03-01 07:54:15 +00005082 if (!Range.contains(R1Val->getValue()))
Dan Gohman246b2562007-10-22 18:31:58 +00005083 return SE.getConstant(NextVal);
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00005084 return SE.getCouldNotCompute(); // Something strange happened
Chris Lattner53e677a2004-04-02 20:23:17 +00005085 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00005086
Chris Lattner53e677a2004-04-02 20:23:17 +00005087 // If R1 was not in the range, then it is a good return value. Make
5088 // sure that R1-1 WAS in the range though, just in case.
Owen Anderson76f600b2009-07-06 22:37:39 +00005089 ConstantInt *NextVal =
Owen Andersoneed707b2009-07-24 23:12:02 +00005090 ConstantInt::get(SE.getContext(), R1->getValue()->getValue()-1);
Dan Gohman246b2562007-10-22 18:31:58 +00005091 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
Reid Spencera6e8a952007-03-01 07:54:15 +00005092 if (Range.contains(R1Val->getValue()))
Chris Lattner53e677a2004-04-02 20:23:17 +00005093 return R1;
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00005094 return SE.getCouldNotCompute(); // Something strange happened
Chris Lattner53e677a2004-04-02 20:23:17 +00005095 }
5096 }
5097 }
5098
Dan Gohmanf4ccfcb2009-04-18 17:58:19 +00005099 return SE.getCouldNotCompute();
Chris Lattner53e677a2004-04-02 20:23:17 +00005100}
5101
5102
5103
5104//===----------------------------------------------------------------------===//
Dan Gohman35738ac2009-05-04 22:30:44 +00005105// SCEVCallbackVH Class Implementation
5106//===----------------------------------------------------------------------===//
5107
Dan Gohman1959b752009-05-19 19:22:47 +00005108void ScalarEvolution::SCEVCallbackVH::deleted() {
Dan Gohmanddf9f992009-07-13 22:20:53 +00005109 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
Dan Gohman35738ac2009-05-04 22:30:44 +00005110 if (PHINode *PN = dyn_cast<PHINode>(getValPtr()))
5111 SE->ConstantEvolutionLoopExitValue.erase(PN);
5112 SE->Scalars.erase(getValPtr());
5113 // this now dangles!
5114}
5115
Dan Gohman1959b752009-05-19 19:22:47 +00005116void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *) {
Dan Gohmanddf9f992009-07-13 22:20:53 +00005117 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
Dan Gohman35738ac2009-05-04 22:30:44 +00005118
5119 // Forget all the expressions associated with users of the old value,
5120 // so that future queries will recompute the expressions using the new
5121 // value.
5122 SmallVector<User *, 16> Worklist;
Dan Gohman69fcae92009-07-14 14:34:04 +00005123 SmallPtrSet<User *, 8> Visited;
Dan Gohman35738ac2009-05-04 22:30:44 +00005124 Value *Old = getValPtr();
5125 bool DeleteOld = false;
5126 for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end();
5127 UI != UE; ++UI)
5128 Worklist.push_back(*UI);
5129 while (!Worklist.empty()) {
5130 User *U = Worklist.pop_back_val();
5131 // Deleting the Old value will cause this to dangle. Postpone
5132 // that until everything else is done.
5133 if (U == Old) {
5134 DeleteOld = true;
5135 continue;
5136 }
Dan Gohman69fcae92009-07-14 14:34:04 +00005137 if (!Visited.insert(U))
5138 continue;
Dan Gohman35738ac2009-05-04 22:30:44 +00005139 if (PHINode *PN = dyn_cast<PHINode>(U))
5140 SE->ConstantEvolutionLoopExitValue.erase(PN);
Dan Gohman69fcae92009-07-14 14:34:04 +00005141 SE->Scalars.erase(U);
5142 for (Value::use_iterator UI = U->use_begin(), UE = U->use_end();
5143 UI != UE; ++UI)
5144 Worklist.push_back(*UI);
Dan Gohman35738ac2009-05-04 22:30:44 +00005145 }
Dan Gohman69fcae92009-07-14 14:34:04 +00005146 // Delete the Old value if it (indirectly) references itself.
Dan Gohman35738ac2009-05-04 22:30:44 +00005147 if (DeleteOld) {
5148 if (PHINode *PN = dyn_cast<PHINode>(Old))
5149 SE->ConstantEvolutionLoopExitValue.erase(PN);
5150 SE->Scalars.erase(Old);
5151 // this now dangles!
5152 }
5153 // this may dangle!
5154}
5155
Dan Gohman1959b752009-05-19 19:22:47 +00005156ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se)
Dan Gohman35738ac2009-05-04 22:30:44 +00005157 : CallbackVH(V), SE(se) {}
5158
5159//===----------------------------------------------------------------------===//
Chris Lattner53e677a2004-04-02 20:23:17 +00005160// ScalarEvolution Class Implementation
5161//===----------------------------------------------------------------------===//
5162
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005163ScalarEvolution::ScalarEvolution()
Dan Gohman1c343752009-06-27 21:21:31 +00005164 : FunctionPass(&ID) {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005165}
5166
Chris Lattner53e677a2004-04-02 20:23:17 +00005167bool ScalarEvolution::runOnFunction(Function &F) {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005168 this->F = &F;
5169 LI = &getAnalysis<LoopInfo>();
Dan Gohman1cd92752010-01-19 22:21:27 +00005170 DT = &getAnalysis<DominatorTree>();
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005171 TD = getAnalysisIfAvailable<TargetData>();
Chris Lattner53e677a2004-04-02 20:23:17 +00005172 return false;
5173}
5174
5175void ScalarEvolution::releaseMemory() {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005176 Scalars.clear();
5177 BackedgeTakenCounts.clear();
5178 ConstantEvolutionLoopExitValue.clear();
Dan Gohman6bce6432009-05-08 20:47:27 +00005179 ValuesAtScopes.clear();
Dan Gohman1c343752009-06-27 21:21:31 +00005180 UniqueSCEVs.clear();
5181 SCEVAllocator.Reset();
Chris Lattner53e677a2004-04-02 20:23:17 +00005182}
5183
5184void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const {
5185 AU.setPreservesAll();
Chris Lattner53e677a2004-04-02 20:23:17 +00005186 AU.addRequiredTransitive<LoopInfo>();
Dan Gohman1cd92752010-01-19 22:21:27 +00005187 AU.addRequiredTransitive<DominatorTree>();
Dan Gohman2d1be872009-04-16 03:18:22 +00005188}
5189
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005190bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) {
Dan Gohman46bdfb02009-02-24 18:55:53 +00005191 return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L));
Chris Lattner53e677a2004-04-02 20:23:17 +00005192}
5193
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005194static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE,
Chris Lattner53e677a2004-04-02 20:23:17 +00005195 const Loop *L) {
5196 // Print all inner loops first
5197 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
5198 PrintLoopInfo(OS, SE, *I);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00005199
Dan Gohman30733292010-01-09 18:17:45 +00005200 OS << "Loop ";
5201 WriteAsOperand(OS, L->getHeader(), /*PrintType=*/false);
5202 OS << ": ";
Chris Lattnerf1ab4b42004-04-18 22:14:10 +00005203
Dan Gohman5d984912009-12-18 01:14:11 +00005204 SmallVector<BasicBlock *, 8> ExitBlocks;
Chris Lattnerf1ab4b42004-04-18 22:14:10 +00005205 L->getExitBlocks(ExitBlocks);
5206 if (ExitBlocks.size() != 1)
Nick Lewyckyaeb5e5c2008-01-02 02:49:20 +00005207 OS << "<multiple exits> ";
Chris Lattner53e677a2004-04-02 20:23:17 +00005208
Dan Gohman46bdfb02009-02-24 18:55:53 +00005209 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
5210 OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L);
Chris Lattner53e677a2004-04-02 20:23:17 +00005211 } else {
Dan Gohman46bdfb02009-02-24 18:55:53 +00005212 OS << "Unpredictable backedge-taken count. ";
Chris Lattner53e677a2004-04-02 20:23:17 +00005213 }
5214
Dan Gohman30733292010-01-09 18:17:45 +00005215 OS << "\n"
5216 "Loop ";
5217 WriteAsOperand(OS, L->getHeader(), /*PrintType=*/false);
5218 OS << ": ";
Dan Gohmanaa551ae2009-06-24 00:33:16 +00005219
5220 if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) {
5221 OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L);
5222 } else {
5223 OS << "Unpredictable max backedge-taken count. ";
5224 }
5225
5226 OS << "\n";
Chris Lattner53e677a2004-04-02 20:23:17 +00005227}
5228
Dan Gohman5d984912009-12-18 01:14:11 +00005229void ScalarEvolution::print(raw_ostream &OS, const Module *) const {
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005230 // ScalarEvolution's implementaiton of the print method is to print
5231 // out SCEV values of all instructions that are interesting. Doing
5232 // this potentially causes it to create new SCEV objects though,
5233 // which technically conflicts with the const qualifier. This isn't
Dan Gohman1afdc5f2009-07-10 20:25:29 +00005234 // observable from outside the class though, so casting away the
5235 // const isn't dangerous.
Dan Gohman5d984912009-12-18 01:14:11 +00005236 ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
Chris Lattner53e677a2004-04-02 20:23:17 +00005237
Dan Gohman30733292010-01-09 18:17:45 +00005238 OS << "Classifying expressions for: ";
5239 WriteAsOperand(OS, F, /*PrintType=*/false);
5240 OS << "\n";
Chris Lattner53e677a2004-04-02 20:23:17 +00005241 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Dan Gohmand9c1c852009-04-30 01:30:18 +00005242 if (isSCEVable(I->getType())) {
Dan Gohmanc902e132009-07-13 23:03:05 +00005243 OS << *I << '\n';
Dan Gohman8dae1382008-09-14 17:21:12 +00005244 OS << " --> ";
Dan Gohman0bba49c2009-07-07 17:06:11 +00005245 const SCEV *SV = SE.getSCEV(&*I);
Chris Lattner53e677a2004-04-02 20:23:17 +00005246 SV->print(OS);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00005247
Dan Gohman0c689c52009-06-19 17:49:54 +00005248 const Loop *L = LI->getLoopFor((*I).getParent());
5249
Dan Gohman0bba49c2009-07-07 17:06:11 +00005250 const SCEV *AtUse = SE.getSCEVAtScope(SV, L);
Dan Gohman0c689c52009-06-19 17:49:54 +00005251 if (AtUse != SV) {
5252 OS << " --> ";
5253 AtUse->print(OS);
5254 }
5255
5256 if (L) {
Dan Gohman9e7d9882009-06-18 00:37:45 +00005257 OS << "\t\t" "Exits: ";
Dan Gohman0bba49c2009-07-07 17:06:11 +00005258 const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop());
Dan Gohmand594e6f2009-05-24 23:25:42 +00005259 if (!ExitValue->isLoopInvariant(L)) {
Chris Lattner53e677a2004-04-02 20:23:17 +00005260 OS << "<<Unknown>>";
5261 } else {
5262 OS << *ExitValue;
5263 }
5264 }
5265
Chris Lattner53e677a2004-04-02 20:23:17 +00005266 OS << "\n";
5267 }
5268
Dan Gohman30733292010-01-09 18:17:45 +00005269 OS << "Determining loop execution counts for: ";
5270 WriteAsOperand(OS, F, /*PrintType=*/false);
5271 OS << "\n";
Dan Gohmanf8a8be82009-04-21 23:15:49 +00005272 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
5273 PrintLoopInfo(OS, &SE, *I);
Chris Lattner53e677a2004-04-02 20:23:17 +00005274}
Dan Gohmanb7ef7292009-04-21 00:47:46 +00005275