blob: c77c7c40ef1a4d366197c33d1333e03808190679 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- ScalarEvolution.cpp - Scalar Evolution Analysis ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the implementation of the scalar evolution analysis
11// engine, which is used primarily to analyze expressions involving induction
12// variables in loops.
13//
14// There are several aspects to this library. First is the representation of
15// scalar expressions, which are represented as subclasses of the SCEV class.
16// These classes are used to represent certain types of subexpressions that we
Dan Gohman161ea032009-07-07 17:06:11 +000017// can handle. These classes are reference counted, managed by the const SCEV *
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018// class. We only create one SCEV of a particular shape, so pointer-comparisons
19// for equality are legal.
20//
21// One important aspect of the SCEV objects is that they are never cyclic, even
22// if there is a cycle in the dataflow for an expression (ie, a PHI node). If
23// the PHI node is one of the idioms that we can represent (e.g., a polynomial
24// recurrence) then we represent it directly as a recurrence node, otherwise we
25// represent it as a SCEVUnknown node.
26//
27// In addition to being able to represent expressions of various types, we also
28// have folders that are used to build the *canonical* representation for a
29// particular expression. These folders are capable of using a variety of
30// rewrite rules to simplify the expressions.
31//
32// Once the folders are defined, we can implement the more interesting
33// higher-level code, such as the code that recognizes PHI nodes of various
34// types, computes the execution count of a loop, etc.
35//
36// TODO: We should use these routines and value representations to implement
37// dependence analysis!
38//
39//===----------------------------------------------------------------------===//
40//
41// There are several good references for the techniques used in this analysis.
42//
43// Chains of recurrences -- a method to expedite the evaluation
44// of closed-form functions
45// Olaf Bachmann, Paul S. Wang, Eugene V. Zima
46//
47// On computational properties of chains of recurrences
48// Eugene V. Zima
49//
50// Symbolic Evaluation of Chains of Recurrences for Loop Optimization
51// Robert A. van Engelen
52//
53// Efficient Symbolic Analysis for Optimizing Compilers
54// Robert A. van Engelen
55//
56// Using the chains of recurrences algebra for data dependence testing and
57// induction variable substitution
58// MS Thesis, Johnie Birch
59//
60//===----------------------------------------------------------------------===//
61
62#define DEBUG_TYPE "scalar-evolution"
63#include "llvm/Analysis/ScalarEvolutionExpressions.h"
64#include "llvm/Constants.h"
65#include "llvm/DerivedTypes.h"
66#include "llvm/GlobalVariable.h"
67#include "llvm/Instructions.h"
Owen Andersone755b092009-07-06 22:37:39 +000068#include "llvm/LLVMContext.h"
Dan Gohman9545fb02009-07-17 20:47:02 +000069#include "llvm/Operator.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070#include "llvm/Analysis/ConstantFolding.h"
Evan Cheng98c073b2009-02-17 00:13:06 +000071#include "llvm/Analysis/Dominators.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072#include "llvm/Analysis/LoopInfo.h"
Dan Gohmana7726c32009-06-16 19:52:01 +000073#include "llvm/Analysis/ValueTracking.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074#include "llvm/Assembly/Writer.h"
Dan Gohman01c2ee72009-04-16 03:18:22 +000075#include "llvm/Target/TargetData.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076#include "llvm/Support/CommandLine.h"
77#include "llvm/Support/Compiler.h"
78#include "llvm/Support/ConstantRange.h"
Edwin Török675d5622009-07-11 20:10:48 +000079#include "llvm/Support/ErrorHandling.h"
Dan Gohman01c2ee72009-04-16 03:18:22 +000080#include "llvm/Support/GetElementPtrTypeIterator.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081#include "llvm/Support/InstIterator.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082#include "llvm/Support/MathExtras.h"
Dan Gohman13058cc2009-04-21 00:47:46 +000083#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084#include "llvm/ADT/Statistic.h"
Dan Gohman01c2ee72009-04-16 03:18:22 +000085#include "llvm/ADT/STLExtras.h"
Dan Gohmanb7d04aa2009-07-08 19:23:34 +000086#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087#include <algorithm>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088using namespace llvm;
89
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Gohman089efff2008-05-13 00:00:25 +000099static cl::opt<unsigned>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden,
101 cl::desc("Maximum number of iterations SCEV will "
Dan Gohman9bc642f2009-06-24 04:48:43 +0000102 "symbolically execute a constant "
103 "derived loop"),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 cl::init(100));
105
Dan Gohman089efff2008-05-13 00:00:25 +0000106static RegisterPass<ScalarEvolution>
107R("scalar-evolution", "Scalar Evolution Analysis", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108char ScalarEvolution::ID = 0;
109
110//===----------------------------------------------------------------------===//
111// SCEV class definitions
112//===----------------------------------------------------------------------===//
113
114//===----------------------------------------------------------------------===//
115// Implementation of the SCEV class.
116//
Dan Gohmanc86c0df2009-06-30 20:13:32 +0000117
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118SCEV::~SCEV() {}
Dan Gohmanc86c0df2009-06-30 20:13:32 +0000119
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120void SCEV::dump() const {
Dan Gohman13058cc2009-04-21 00:47:46 +0000121 print(errs());
122 errs() << '\n';
123}
124
125void SCEV::print(std::ostream &o) const {
126 raw_os_ostream OS(o);
127 print(OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128}
129
Dan Gohman7b560c42008-06-18 16:23:07 +0000130bool SCEV::isZero() const {
131 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
132 return SC->getValue()->isZero();
133 return false;
134}
135
Dan Gohmanf8bc8e82009-05-18 15:22:39 +0000136bool SCEV::isOne() const {
137 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
138 return SC->getValue()->isOne();
139 return false;
140}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141
Dan Gohmanf05118e2009-06-24 00:30:26 +0000142bool SCEV::isAllOnesValue() const {
143 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
144 return SC->getValue()->isAllOnesValue();
145 return false;
146}
147
Owen Andersonb70139d2009-06-22 21:57:23 +0000148SCEVCouldNotCompute::SCEVCouldNotCompute() :
Dan Gohmand43a8282009-07-13 20:50:19 +0000149 SCEV(FoldingSetNodeID(), scCouldNotCompute) {}
Dan Gohmanc6475cb2009-06-27 21:21:31 +0000150
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const {
Edwin Törökbd448e32009-07-14 16:55:14 +0000152 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 return false;
154}
155
156const Type *SCEVCouldNotCompute::getType() const {
Edwin Törökbd448e32009-07-14 16:55:14 +0000157 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 return 0;
159}
160
161bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const {
Edwin Törökbd448e32009-07-14 16:55:14 +0000162 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 return false;
164}
165
Dan Gohman2aa3f042009-07-25 01:13:03 +0000166bool SCEVCouldNotCompute::hasOperand(const SCEV *) const {
167 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
168 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169}
170
Dan Gohman13058cc2009-04-21 00:47:46 +0000171void SCEVCouldNotCompute::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 OS << "***COULDNOTCOMPUTE***";
173}
174
175bool SCEVCouldNotCompute::classof(const SCEV *S) {
176 return S->getSCEVType() == scCouldNotCompute;
177}
178
Dan Gohman161ea032009-07-07 17:06:11 +0000179const SCEV *ScalarEvolution::getConstant(ConstantInt *V) {
Dan Gohmanc6475cb2009-06-27 21:21:31 +0000180 FoldingSetNodeID ID;
181 ID.AddInteger(scConstant);
182 ID.AddPointer(V);
183 void *IP = 0;
184 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
185 SCEV *S = SCEVAllocator.Allocate<SCEVConstant>();
Dan Gohmand43a8282009-07-13 20:50:19 +0000186 new (S) SCEVConstant(ID, V);
Dan Gohmanc6475cb2009-06-27 21:21:31 +0000187 UniqueSCEVs.InsertNode(S, IP);
188 return S;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189}
190
Dan Gohman161ea032009-07-07 17:06:11 +0000191const SCEV *ScalarEvolution::getConstant(const APInt& Val) {
Owen Andersoneacb44d2009-07-24 23:12:02 +0000192 return getConstant(ConstantInt::get(getContext(), Val));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193}
194
Dan Gohman161ea032009-07-07 17:06:11 +0000195const SCEV *
Dan Gohman8fd520a2009-06-15 22:12:54 +0000196ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) {
Owen Anderson9f5b2aa2009-07-14 23:09:55 +0000197 return getConstant(
Owen Andersoneacb44d2009-07-24 23:12:02 +0000198 ConstantInt::get(cast<IntegerType>(Ty), V, isSigned));
Dan Gohman8fd520a2009-06-15 22:12:54 +0000199}
200
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201const Type *SCEVConstant::getType() const { return V->getType(); }
202
Dan Gohman13058cc2009-04-21 00:47:46 +0000203void SCEVConstant::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 WriteAsOperand(OS, V, false);
205}
206
Dan Gohmand43a8282009-07-13 20:50:19 +0000207SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeID &ID,
208 unsigned SCEVTy, const SCEV *op, const Type *ty)
209 : SCEV(ID, SCEVTy), Op(op), Ty(ty) {}
Dan Gohmanc6475cb2009-06-27 21:21:31 +0000210
Dan Gohman2a381532009-04-21 01:25:57 +0000211bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
212 return Op->dominates(BB, DT);
213}
214
Dan Gohmand43a8282009-07-13 20:50:19 +0000215SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeID &ID,
216 const SCEV *op, const Type *ty)
217 : SCEVCastExpr(ID, scTruncate, op, ty) {
Dan Gohman01c2ee72009-04-16 03:18:22 +0000218 assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
219 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220 "Cannot truncate non-integer value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221}
222
Dan Gohman13058cc2009-04-21 00:47:46 +0000223void SCEVTruncateExpr::print(raw_ostream &OS) const {
Dan Gohmanc9119222009-04-29 20:27:52 +0000224 OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225}
226
Dan Gohmand43a8282009-07-13 20:50:19 +0000227SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeID &ID,
228 const SCEV *op, const Type *ty)
229 : SCEVCastExpr(ID, scZeroExtend, op, ty) {
Dan Gohman01c2ee72009-04-16 03:18:22 +0000230 assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
231 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 "Cannot zero extend non-integer value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233}
234
Dan Gohman13058cc2009-04-21 00:47:46 +0000235void SCEVZeroExtendExpr::print(raw_ostream &OS) const {
Dan Gohmanc9119222009-04-29 20:27:52 +0000236 OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237}
238
Dan Gohmand43a8282009-07-13 20:50:19 +0000239SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeID &ID,
240 const SCEV *op, const Type *ty)
241 : SCEVCastExpr(ID, scSignExtend, op, ty) {
Dan Gohman01c2ee72009-04-16 03:18:22 +0000242 assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
243 (Ty->isInteger() || isa<PointerType>(Ty)) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 "Cannot sign extend non-integer value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245}
246
Dan Gohman13058cc2009-04-21 00:47:46 +0000247void SCEVSignExtendExpr::print(raw_ostream &OS) const {
Dan Gohmanc9119222009-04-29 20:27:52 +0000248 OS << "(sext " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249}
250
Dan Gohman13058cc2009-04-21 00:47:46 +0000251void SCEVCommutativeExpr::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 assert(Operands.size() > 1 && "This plus expr shouldn't exist!");
253 const char *OpStr = getOperationStr();
254 OS << "(" << *Operands[0];
255 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
256 OS << OpStr << *Operands[i];
257 OS << ")";
258}
259
Dan Gohman72a8a022009-05-07 14:00:19 +0000260bool SCEVNAryExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
Evan Cheng98c073b2009-02-17 00:13:06 +0000261 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
262 if (!getOperand(i)->dominates(BB, DT))
263 return false;
264 }
265 return true;
266}
267
Evan Cheng98c073b2009-02-17 00:13:06 +0000268bool SCEVUDivExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
269 return LHS->dominates(BB, DT) && RHS->dominates(BB, DT);
270}
271
Dan Gohman13058cc2009-04-21 00:47:46 +0000272void SCEVUDivExpr::print(raw_ostream &OS) const {
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000273 OS << "(" << *LHS << " /u " << *RHS << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274}
275
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000276const Type *SCEVUDivExpr::getType() const {
Dan Gohman140f08f2009-05-26 17:44:05 +0000277 // In most cases the types of LHS and RHS will be the same, but in some
278 // crazy cases one or the other may be a pointer. ScalarEvolution doesn't
279 // depend on the type for correctness, but handling types carefully can
280 // avoid extra casts in the SCEVExpander. The LHS is more likely to be
281 // a pointer type than the RHS, so use the RHS' type here.
282 return RHS->getType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283}
284
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const {
Dan Gohmanae1eaae2009-05-20 01:01:24 +0000286 // Add recurrences are never invariant in the function-body (null loop).
Dan Gohman2d888d82009-06-26 22:17:21 +0000287 if (!QueryLoop)
288 return false;
289
290 // This recurrence is variant w.r.t. QueryLoop if QueryLoop contains L.
291 if (QueryLoop->contains(L->getHeader()))
292 return false;
293
294 // This recurrence is variant w.r.t. QueryLoop if any of its operands
295 // are variant.
296 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
297 if (!getOperand(i)->isLoopInvariant(QueryLoop))
298 return false;
299
300 // Otherwise it's loop-invariant.
301 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302}
303
Dan Gohman13058cc2009-04-21 00:47:46 +0000304void SCEVAddRecExpr::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 OS << "{" << *Operands[0];
306 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
307 OS << ",+," << *Operands[i];
308 OS << "}<" << L->getHeader()->getName() + ">";
309}
310
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311bool SCEVUnknown::isLoopInvariant(const Loop *L) const {
312 // All non-instruction values are loop invariant. All instructions are loop
313 // invariant if they are not contained in the specified loop.
Dan Gohmanae1eaae2009-05-20 01:01:24 +0000314 // Instructions are never considered invariant in the function body
315 // (null loop) because they are defined within the "loop".
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 if (Instruction *I = dyn_cast<Instruction>(V))
Dan Gohmanae1eaae2009-05-20 01:01:24 +0000317 return L && !L->contains(I->getParent());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 return true;
319}
320
Evan Cheng98c073b2009-02-17 00:13:06 +0000321bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const {
322 if (Instruction *I = dyn_cast<Instruction>(getValue()))
323 return DT->dominates(I->getParent(), BB);
324 return true;
325}
326
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327const Type *SCEVUnknown::getType() const {
328 return V->getType();
329}
330
Dan Gohman13058cc2009-04-21 00:47:46 +0000331void SCEVUnknown::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 WriteAsOperand(OS, V, false);
333}
334
335//===----------------------------------------------------------------------===//
336// SCEV Utilities
337//===----------------------------------------------------------------------===//
338
339namespace {
340 /// SCEVComplexityCompare - Return true if the complexity of the LHS is less
341 /// than the complexity of the RHS. This comparator is used to canonicalize
342 /// expressions.
Dan Gohman5d486452009-05-07 14:39:04 +0000343 class VISIBILITY_HIDDEN SCEVComplexityCompare {
344 LoopInfo *LI;
345 public:
346 explicit SCEVComplexityCompare(LoopInfo *li) : LI(li) {}
347
Dan Gohmanc0c69cf2008-04-14 18:23:56 +0000348 bool operator()(const SCEV *LHS, const SCEV *RHS) const {
Dan Gohman5d486452009-05-07 14:39:04 +0000349 // Primarily, sort the SCEVs by their getSCEVType().
350 if (LHS->getSCEVType() != RHS->getSCEVType())
351 return LHS->getSCEVType() < RHS->getSCEVType();
352
353 // Aside from the getSCEVType() ordering, the particular ordering
354 // isn't very important except that it's beneficial to be consistent,
355 // so that (a + b) and (b + a) don't end up as different expressions.
356
357 // Sort SCEVUnknown values with some loose heuristics. TODO: This is
358 // not as complete as it could be.
359 if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) {
360 const SCEVUnknown *RU = cast<SCEVUnknown>(RHS);
361
Dan Gohmand0c01232009-05-19 02:15:55 +0000362 // Order pointer values after integer values. This helps SCEVExpander
363 // form GEPs.
364 if (isa<PointerType>(LU->getType()) && !isa<PointerType>(RU->getType()))
365 return false;
366 if (isa<PointerType>(RU->getType()) && !isa<PointerType>(LU->getType()))
367 return true;
368
Dan Gohman5d486452009-05-07 14:39:04 +0000369 // Compare getValueID values.
370 if (LU->getValue()->getValueID() != RU->getValue()->getValueID())
371 return LU->getValue()->getValueID() < RU->getValue()->getValueID();
372
373 // Sort arguments by their position.
374 if (const Argument *LA = dyn_cast<Argument>(LU->getValue())) {
375 const Argument *RA = cast<Argument>(RU->getValue());
376 return LA->getArgNo() < RA->getArgNo();
377 }
378
379 // For instructions, compare their loop depth, and their opcode.
380 // This is pretty loose.
381 if (Instruction *LV = dyn_cast<Instruction>(LU->getValue())) {
382 Instruction *RV = cast<Instruction>(RU->getValue());
383
384 // Compare loop depths.
385 if (LI->getLoopDepth(LV->getParent()) !=
386 LI->getLoopDepth(RV->getParent()))
387 return LI->getLoopDepth(LV->getParent()) <
388 LI->getLoopDepth(RV->getParent());
389
390 // Compare opcodes.
391 if (LV->getOpcode() != RV->getOpcode())
392 return LV->getOpcode() < RV->getOpcode();
393
394 // Compare the number of operands.
395 if (LV->getNumOperands() != RV->getNumOperands())
396 return LV->getNumOperands() < RV->getNumOperands();
397 }
398
399 return false;
400 }
401
Dan Gohman56fc8f12009-06-14 22:51:25 +0000402 // Compare constant values.
403 if (const SCEVConstant *LC = dyn_cast<SCEVConstant>(LHS)) {
404 const SCEVConstant *RC = cast<SCEVConstant>(RHS);
Nick Lewycky9bb14052009-07-04 17:24:52 +0000405 if (LC->getValue()->getBitWidth() != RC->getValue()->getBitWidth())
406 return LC->getValue()->getBitWidth() < RC->getValue()->getBitWidth();
Dan Gohman56fc8f12009-06-14 22:51:25 +0000407 return LC->getValue()->getValue().ult(RC->getValue()->getValue());
408 }
409
410 // Compare addrec loop depths.
411 if (const SCEVAddRecExpr *LA = dyn_cast<SCEVAddRecExpr>(LHS)) {
412 const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS);
413 if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth())
414 return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth();
415 }
Dan Gohman5d486452009-05-07 14:39:04 +0000416
417 // Lexicographically compare n-ary expressions.
418 if (const SCEVNAryExpr *LC = dyn_cast<SCEVNAryExpr>(LHS)) {
419 const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS);
420 for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) {
421 if (i >= RC->getNumOperands())
422 return false;
423 if (operator()(LC->getOperand(i), RC->getOperand(i)))
424 return true;
425 if (operator()(RC->getOperand(i), LC->getOperand(i)))
426 return false;
427 }
428 return LC->getNumOperands() < RC->getNumOperands();
429 }
430
Dan Gohman6e10db12009-05-07 19:23:21 +0000431 // Lexicographically compare udiv expressions.
432 if (const SCEVUDivExpr *LC = dyn_cast<SCEVUDivExpr>(LHS)) {
433 const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS);
434 if (operator()(LC->getLHS(), RC->getLHS()))
435 return true;
436 if (operator()(RC->getLHS(), LC->getLHS()))
437 return false;
438 if (operator()(LC->getRHS(), RC->getRHS()))
439 return true;
440 if (operator()(RC->getRHS(), LC->getRHS()))
441 return false;
442 return false;
443 }
444
Dan Gohman5d486452009-05-07 14:39:04 +0000445 // Compare cast expressions by operand.
446 if (const SCEVCastExpr *LC = dyn_cast<SCEVCastExpr>(LHS)) {
447 const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS);
448 return operator()(LC->getOperand(), RC->getOperand());
449 }
450
Edwin Törökbd448e32009-07-14 16:55:14 +0000451 llvm_unreachable("Unknown SCEV kind!");
Dan Gohman5d486452009-05-07 14:39:04 +0000452 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 }
454 };
455}
456
457/// GroupByComplexity - Given a list of SCEV objects, order them by their
458/// complexity, and group objects of the same complexity together by value.
459/// When this routine is finished, we know that any duplicates in the vector are
460/// consecutive and that complexity is monotonically increasing.
461///
462/// Note that we go take special precautions to ensure that we get determinstic
463/// results from this routine. In other words, we don't want the results of
464/// this to depend on where the addresses of various SCEV objects happened to
465/// land in memory.
466///
Dan Gohman161ea032009-07-07 17:06:11 +0000467static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops,
Dan Gohman5d486452009-05-07 14:39:04 +0000468 LoopInfo *LI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 if (Ops.size() < 2) return; // Noop
470 if (Ops.size() == 2) {
471 // This is the common case, which also happens to be trivially simple.
472 // Special case it.
Dan Gohman5d486452009-05-07 14:39:04 +0000473 if (SCEVComplexityCompare(LI)(Ops[1], Ops[0]))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 std::swap(Ops[0], Ops[1]);
475 return;
476 }
477
478 // Do the rough sort by complexity.
Dan Gohman5d486452009-05-07 14:39:04 +0000479 std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480
481 // Now that we are sorted by complexity, group elements of the same
482 // complexity. Note that this is, at worst, N^2, but the vector is likely to
483 // be extremely short in practice. Note that we take this approach because we
484 // do not want to depend on the addresses of the objects we are grouping.
485 for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
Dan Gohmanbff6b582009-05-04 22:30:44 +0000486 const SCEV *S = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000487 unsigned Complexity = S->getSCEVType();
488
489 // If there are any objects of the same complexity and same value as this
490 // one, group them.
491 for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
492 if (Ops[j] == S) { // Found a duplicate.
493 // Move it to immediately after i'th element.
494 std::swap(Ops[i+1], Ops[j]);
495 ++i; // no need to rescan it.
496 if (i == e-2) return; // Done!
497 }
498 }
499 }
500}
501
502
503
504//===----------------------------------------------------------------------===//
505// Simple SCEV method implementations
506//===----------------------------------------------------------------------===//
507
Eli Friedman7489ec92008-08-04 23:49:06 +0000508/// BinomialCoefficient - Compute BC(It, K). The result has width W.
Dan Gohmanc8a29272009-05-24 23:45:28 +0000509/// Assume, K > 0.
Dan Gohman161ea032009-07-07 17:06:11 +0000510static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K,
Dan Gohmanf5606fd2009-07-21 00:38:55 +0000511 ScalarEvolution &SE,
512 const Type* ResultTy) {
Eli Friedman7489ec92008-08-04 23:49:06 +0000513 // Handle the simplest case efficiently.
514 if (K == 1)
515 return SE.getTruncateOrZeroExtend(It, ResultTy);
516
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000517 // We are using the following formula for BC(It, K):
518 //
519 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K!
520 //
Eli Friedman7489ec92008-08-04 23:49:06 +0000521 // Suppose, W is the bitwidth of the return value. We must be prepared for
522 // overflow. Hence, we must assure that the result of our computation is
523 // equal to the accurate one modulo 2^W. Unfortunately, division isn't
524 // safe in modular arithmetic.
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000525 //
Eli Friedman7489ec92008-08-04 23:49:06 +0000526 // However, this code doesn't use exactly that formula; the formula it uses
Dan Gohman9bc642f2009-06-24 04:48:43 +0000527 // is something like the following, where T is the number of factors of 2 in
Eli Friedman7489ec92008-08-04 23:49:06 +0000528 // K! (i.e. trailing zeros in the binary representation of K!), and ^ is
529 // exponentiation:
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000530 //
Eli Friedman7489ec92008-08-04 23:49:06 +0000531 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T)
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000532 //
Eli Friedman7489ec92008-08-04 23:49:06 +0000533 // This formula is trivially equivalent to the previous formula. However,
534 // this formula can be implemented much more efficiently. The trick is that
535 // K! / 2^T is odd, and exact division by an odd number *is* safe in modular
536 // arithmetic. To do exact division in modular arithmetic, all we have
537 // to do is multiply by the inverse. Therefore, this step can be done at
538 // width W.
Dan Gohman9bc642f2009-06-24 04:48:43 +0000539 //
Eli Friedman7489ec92008-08-04 23:49:06 +0000540 // The next issue is how to safely do the division by 2^T. The way this
541 // is done is by doing the multiplication step at a width of at least W + T
542 // bits. This way, the bottom W+T bits of the product are accurate. Then,
543 // when we perform the division by 2^T (which is equivalent to a right shift
544 // by T), the bottom W bits are accurate. Extra bits are okay; they'll get
545 // truncated out after the division by 2^T.
546 //
547 // In comparison to just directly using the first formula, this technique
548 // is much more efficient; using the first formula requires W * K bits,
549 // but this formula less than W + K bits. Also, the first formula requires
550 // a division step, whereas this formula only requires multiplies and shifts.
551 //
552 // It doesn't matter whether the subtraction step is done in the calculation
553 // width or the input iteration count's width; if the subtraction overflows,
554 // the result must be zero anyway. We prefer here to do it in the width of
555 // the induction variable because it helps a lot for certain cases; CodeGen
556 // isn't smart enough to ignore the overflow, which leads to much less
557 // efficient code if the width of the subtraction is wider than the native
558 // register width.
559 //
560 // (It's possible to not widen at all by pulling out factors of 2 before
561 // the multiplication; for example, K=2 can be calculated as
562 // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires
563 // extra arithmetic, so it's not an obvious win, and it gets
564 // much more complicated for K > 3.)
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000565
Eli Friedman7489ec92008-08-04 23:49:06 +0000566 // Protection from insane SCEVs; this bound is conservative,
567 // but it probably doesn't matter.
568 if (K > 1000)
Dan Gohman0ad08b02009-04-18 17:58:19 +0000569 return SE.getCouldNotCompute();
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000570
Dan Gohmanb98c1a32009-04-21 01:07:12 +0000571 unsigned W = SE.getTypeSizeInBits(ResultTy);
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000572
Eli Friedman7489ec92008-08-04 23:49:06 +0000573 // Calculate K! / 2^T and T; we divide out the factors of two before
574 // multiplying for calculating K! / 2^T to avoid overflow.
575 // Other overflow doesn't matter because we only care about the bottom
576 // W bits of the result.
577 APInt OddFactorial(W, 1);
578 unsigned T = 1;
579 for (unsigned i = 3; i <= K; ++i) {
580 APInt Mult(W, i);
581 unsigned TwoFactors = Mult.countTrailingZeros();
582 T += TwoFactors;
583 Mult = Mult.lshr(TwoFactors);
584 OddFactorial *= Mult;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585 }
Nick Lewyckydbaa60a2008-06-13 04:38:55 +0000586
Eli Friedman7489ec92008-08-04 23:49:06 +0000587 // We need at least W + T bits for the multiplication step
nicholas9e3e5fd2009-01-25 08:16:27 +0000588 unsigned CalculationBits = W + T;
Eli Friedman7489ec92008-08-04 23:49:06 +0000589
590 // Calcuate 2^T, at width T+W.
591 APInt DivFactor = APInt(CalculationBits, 1).shl(T);
592
593 // Calculate the multiplicative inverse of K! / 2^T;
594 // this multiplication factor will perform the exact division by
595 // K! / 2^T.
596 APInt Mod = APInt::getSignedMinValue(W+1);
597 APInt MultiplyFactor = OddFactorial.zext(W+1);
598 MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod);
599 MultiplyFactor = MultiplyFactor.trunc(W);
600
601 // Calculate the product, at width T+W
602 const IntegerType *CalculationTy = IntegerType::get(CalculationBits);
Dan Gohman161ea032009-07-07 17:06:11 +0000603 const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy);
Eli Friedman7489ec92008-08-04 23:49:06 +0000604 for (unsigned i = 1; i != K; ++i) {
Dan Gohman161ea032009-07-07 17:06:11 +0000605 const SCEV *S = SE.getMinusSCEV(It, SE.getIntegerSCEV(i, It->getType()));
Eli Friedman7489ec92008-08-04 23:49:06 +0000606 Dividend = SE.getMulExpr(Dividend,
607 SE.getTruncateOrZeroExtend(S, CalculationTy));
608 }
609
610 // Divide by 2^T
Dan Gohman161ea032009-07-07 17:06:11 +0000611 const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor));
Eli Friedman7489ec92008-08-04 23:49:06 +0000612
613 // Truncate the result, and divide by K! / 2^T.
614
615 return SE.getMulExpr(SE.getConstant(MultiplyFactor),
616 SE.getTruncateOrZeroExtend(DivResult, ResultTy));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617}
618
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619/// evaluateAtIteration - Return the value of this chain of recurrences at
620/// the specified iteration number. We can evaluate this recurrence by
621/// multiplying each element in the chain by the binomial coefficient
622/// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as:
623///
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000624/// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625///
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000626/// where BC(It, k) stands for binomial coefficient.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627///
Dan Gohman161ea032009-07-07 17:06:11 +0000628const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It,
Dan Gohmanf5606fd2009-07-21 00:38:55 +0000629 ScalarEvolution &SE) const {
Dan Gohman161ea032009-07-07 17:06:11 +0000630 const SCEV *Result = getStart();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Wojciech Matyjewicz2211fec2008-02-11 11:03:14 +0000632 // The computation is correct in the face of overflow provided that the
633 // multiplication is performed _after_ the evaluation of the binomial
634 // coefficient.
Dan Gohman161ea032009-07-07 17:06:11 +0000635 const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType());
Nick Lewyckyb6218e02008-10-13 03:58:02 +0000636 if (isa<SCEVCouldNotCompute>(Coeff))
637 return Coeff;
638
639 Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 }
641 return Result;
642}
643
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644//===----------------------------------------------------------------------===//
645// SCEV Expression folder implementations
646//===----------------------------------------------------------------------===//
647
Dan Gohman161ea032009-07-07 17:06:11 +0000648const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op,
Dan Gohman69eacc72009-07-13 22:05:32 +0000649 const Type *Ty) {
Dan Gohmanb98c1a32009-04-21 01:07:12 +0000650 assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) &&
Dan Gohmanf62cfe52009-04-21 00:55:22 +0000651 "This is not a truncating conversion!");
Dan Gohman13a51e22009-05-01 16:44:18 +0000652 assert(isSCEVable(Ty) &&
653 "This is not a conversion to a SCEVable type!");
654 Ty = getEffectiveSCEVType(Ty);
Dan Gohmanf62cfe52009-04-21 00:55:22 +0000655
Dan Gohmand43a8282009-07-13 20:50:19 +0000656 FoldingSetNodeID ID;
657 ID.AddInteger(scTruncate);
658 ID.AddPointer(Op);
659 ID.AddPointer(Ty);
660 void *IP = 0;
661 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
662
Dan Gohmanc86c0df2009-06-30 20:13:32 +0000663 // Fold if the operand is constant.
Dan Gohmanc76b5452009-05-04 22:02:23 +0000664 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
Dan Gohman55788cf2009-06-24 00:38:39 +0000665 return getConstant(
666 cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667
Dan Gohman1a5c4992009-04-22 16:20:48 +0000668 // trunc(trunc(x)) --> trunc(x)
Dan Gohmanc76b5452009-05-04 22:02:23 +0000669 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op))
Dan Gohman1a5c4992009-04-22 16:20:48 +0000670 return getTruncateExpr(ST->getOperand(), Ty);
671
Nick Lewycky37d04642009-04-23 05:15:08 +0000672 // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing
Dan Gohmanc76b5452009-05-04 22:02:23 +0000673 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
Nick Lewycky37d04642009-04-23 05:15:08 +0000674 return getTruncateOrSignExtend(SS->getOperand(), Ty);
675
676 // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing
Dan Gohmanc76b5452009-05-04 22:02:23 +0000677 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
Nick Lewycky37d04642009-04-23 05:15:08 +0000678 return getTruncateOrZeroExtend(SZ->getOperand(), Ty);
679
Dan Gohman1c0aa2c2009-06-18 16:24:47 +0000680 // If the input value is a chrec scev, truncate the chrec's operands.
Dan Gohmanc76b5452009-05-04 22:02:23 +0000681 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
Dan Gohman161ea032009-07-07 17:06:11 +0000682 SmallVector<const SCEV *, 4> Operands;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
Dan Gohman45b3b542009-05-08 21:03:19 +0000684 Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty));
685 return getAddRecExpr(Operands, AddRec->getLoop());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 }
687
Dan Gohmand43a8282009-07-13 20:50:19 +0000688 // The cast wasn't folded; create an explicit cast node.
689 // Recompute the insert position, as it may have been invalidated.
Dan Gohmanc6475cb2009-06-27 21:21:31 +0000690 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
691 SCEV *S = SCEVAllocator.Allocate<SCEVTruncateExpr>();
Dan Gohmand43a8282009-07-13 20:50:19 +0000692 new (S) SCEVTruncateExpr(ID, Op, Ty);
Dan Gohmanc6475cb2009-06-27 21:21:31 +0000693 UniqueSCEVs.InsertNode(S, IP);
694 return S;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695}
696
Dan Gohman161ea032009-07-07 17:06:11 +0000697const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op,
Dan Gohman69eacc72009-07-13 22:05:32 +0000698 const Type *Ty) {
Dan Gohmanb98c1a32009-04-21 01:07:12 +0000699 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
Dan Gohman36d40922009-04-16 19:25:55 +0000700 "This is not an extending conversion!");
Dan Gohman13a51e22009-05-01 16:44:18 +0000701 assert(isSCEVable(Ty) &&
702 "This is not a conversion to a SCEVable type!");
703 Ty = getEffectiveSCEVType(Ty);
Dan Gohman36d40922009-04-16 19:25:55 +0000704
Dan Gohmanc86c0df2009-06-30 20:13:32 +0000705 // Fold if the operand is constant.
Dan Gohmanc76b5452009-05-04 22:02:23 +0000706 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) {
Dan Gohmanb98c1a32009-04-21 01:07:12 +0000707 const Type *IntTy = getEffectiveSCEVType(Ty);
Dan Gohman01c2ee72009-04-16 03:18:22 +0000708 Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy);
709 if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty);
Dan Gohman55788cf2009-06-24 00:38:39 +0000710 return getConstant(cast<ConstantInt>(C));
Dan Gohman01c2ee72009-04-16 03:18:22 +0000711 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712
Dan Gohman1a5c4992009-04-22 16:20:48 +0000713 // zext(zext(x)) --> zext(x)
Dan Gohmanc76b5452009-05-04 22:02:23 +0000714 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
Dan Gohman1a5c4992009-04-22 16:20:48 +0000715 return getZeroExtendExpr(SZ->getOperand(), Ty);
716
Dan Gohmandb888422009-07-13 20:55:53 +0000717 // Before doing any expensive analysis, check to see if we've already
718 // computed a SCEV for this Op and Ty.
719 FoldingSetNodeID ID;
720 ID.AddInteger(scZeroExtend);
721 ID.AddPointer(Op);
722 ID.AddPointer(Ty);
723 void *IP = 0;
724 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
725
Dan Gohmana9dba962009-04-27 20:16:15 +0000726 // If the input value is a chrec scev, and we can prove that the value
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727 // did not overflow the old, smaller, value, we can zero extend all of the
Dan Gohmana9dba962009-04-27 20:16:15 +0000728 // operands (often constants). This allows analysis of something like
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729 // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
Dan Gohmanc76b5452009-05-04 22:02:23 +0000730 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
Dan Gohmana9dba962009-04-27 20:16:15 +0000731 if (AR->isAffine()) {
Dan Gohman55e2d7e2009-07-13 21:35:55 +0000732 const SCEV *Start = AR->getStart();
733 const SCEV *Step = AR->getStepRecurrence(*this);
734 unsigned BitWidth = getTypeSizeInBits(AR->getType());
735 const Loop *L = AR->getLoop();
736
Dan Gohmana9dba962009-04-27 20:16:15 +0000737 // Check whether the backedge-taken count is SCEVCouldNotCompute.
738 // Note that this serves two purposes: It filters out loops that are
739 // simply not analyzable, and it covers the case where this code is
740 // being called from within backedge-taken count analysis, such that
741 // attempting to ask for the backedge-taken count would likely result
742 // in infinite recursion. In the later case, the analysis code will
743 // cope with a conservative value, and it will take care to purge
744 // that value once it has finished.
Dan Gohman55e2d7e2009-07-13 21:35:55 +0000745 const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000746 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
Dan Gohman4ada77f2009-04-29 01:54:20 +0000747 // Manually compute the final value for AR, checking for
Dan Gohman3ded5b22009-04-29 22:28:28 +0000748 // overflow.
Dan Gohmana9dba962009-04-27 20:16:15 +0000749
750 // Check whether the backedge-taken count can be losslessly casted to
751 // the addrec's type. The count is always unsigned.
Dan Gohman161ea032009-07-07 17:06:11 +0000752 const SCEV *CastedMaxBECount =
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000753 getTruncateOrZeroExtend(MaxBECount, Start->getType());
Dan Gohman161ea032009-07-07 17:06:11 +0000754 const SCEV *RecastedMaxBECount =
Dan Gohman3bb37f52009-05-18 15:58:39 +0000755 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
756 if (MaxBECount == RecastedMaxBECount) {
Dan Gohman55e2d7e2009-07-13 21:35:55 +0000757 const Type *WideTy = IntegerType::get(BitWidth * 2);
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000758 // Check whether Start+Step*MaxBECount has no unsigned overflow.
Dan Gohman161ea032009-07-07 17:06:11 +0000759 const SCEV *ZMul =
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000760 getMulExpr(CastedMaxBECount,
Dan Gohmana9dba962009-04-27 20:16:15 +0000761 getTruncateOrZeroExtend(Step, Start->getType()));
Dan Gohman161ea032009-07-07 17:06:11 +0000762 const SCEV *Add = getAddExpr(Start, ZMul);
763 const SCEV *OperandExtendedAdd =
Dan Gohman3bb37f52009-05-18 15:58:39 +0000764 getAddExpr(getZeroExtendExpr(Start, WideTy),
765 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
766 getZeroExtendExpr(Step, WideTy)));
767 if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohman3ded5b22009-04-29 22:28:28 +0000768 // Return the expression with the addrec on the outside.
769 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
770 getZeroExtendExpr(Step, Ty),
Dan Gohman55e2d7e2009-07-13 21:35:55 +0000771 L);
Dan Gohmana9dba962009-04-27 20:16:15 +0000772
773 // Similar to above, only this time treat the step value as signed.
774 // This covers loops that count down.
Dan Gohman161ea032009-07-07 17:06:11 +0000775 const SCEV *SMul =
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000776 getMulExpr(CastedMaxBECount,
Dan Gohmana9dba962009-04-27 20:16:15 +0000777 getTruncateOrSignExtend(Step, Start->getType()));
Dan Gohman3ded5b22009-04-29 22:28:28 +0000778 Add = getAddExpr(Start, SMul);
Dan Gohman3bb37f52009-05-18 15:58:39 +0000779 OperandExtendedAdd =
780 getAddExpr(getZeroExtendExpr(Start, WideTy),
781 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
782 getSignExtendExpr(Step, WideTy)));
783 if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohman3ded5b22009-04-29 22:28:28 +0000784 // Return the expression with the addrec on the outside.
785 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
786 getSignExtendExpr(Step, Ty),
Dan Gohman55e2d7e2009-07-13 21:35:55 +0000787 L);
788 }
789
790 // If the backedge is guarded by a comparison with the pre-inc value
791 // the addrec is safe. Also, if the entry is guarded by a comparison
792 // with the start value and the backedge is guarded by a comparison
793 // with the post-inc value, the addrec is safe.
794 if (isKnownPositive(Step)) {
795 const SCEV *N = getConstant(APInt::getMinValue(BitWidth) -
796 getUnsignedRange(Step).getUnsignedMax());
797 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) ||
798 (isLoopGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) &&
799 isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT,
800 AR->getPostIncExpr(*this), N)))
801 // Return the expression with the addrec on the outside.
802 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
803 getZeroExtendExpr(Step, Ty),
804 L);
805 } else if (isKnownNegative(Step)) {
806 const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) -
807 getSignedRange(Step).getSignedMin());
808 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) &&
809 (isLoopGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) ||
810 isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT,
811 AR->getPostIncExpr(*this), N)))
812 // Return the expression with the addrec on the outside.
813 return getAddRecExpr(getZeroExtendExpr(Start, Ty),
814 getSignExtendExpr(Step, Ty),
815 L);
Dan Gohmana9dba962009-04-27 20:16:15 +0000816 }
817 }
818 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819
Dan Gohmandb888422009-07-13 20:55:53 +0000820 // The cast wasn't folded; create an explicit cast node.
821 // Recompute the insert position, as it may have been invalidated.
Dan Gohmanc6475cb2009-06-27 21:21:31 +0000822 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
823 SCEV *S = SCEVAllocator.Allocate<SCEVZeroExtendExpr>();
Dan Gohmand43a8282009-07-13 20:50:19 +0000824 new (S) SCEVZeroExtendExpr(ID, Op, Ty);
Dan Gohmanc6475cb2009-06-27 21:21:31 +0000825 UniqueSCEVs.InsertNode(S, IP);
826 return S;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827}
828
Dan Gohman161ea032009-07-07 17:06:11 +0000829const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op,
Dan Gohman69eacc72009-07-13 22:05:32 +0000830 const Type *Ty) {
Dan Gohmanb98c1a32009-04-21 01:07:12 +0000831 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
Dan Gohmanf62cfe52009-04-21 00:55:22 +0000832 "This is not an extending conversion!");
Dan Gohman13a51e22009-05-01 16:44:18 +0000833 assert(isSCEVable(Ty) &&
834 "This is not a conversion to a SCEVable type!");
835 Ty = getEffectiveSCEVType(Ty);
Dan Gohmanf62cfe52009-04-21 00:55:22 +0000836
Dan Gohmanc86c0df2009-06-30 20:13:32 +0000837 // Fold if the operand is constant.
Dan Gohmanc76b5452009-05-04 22:02:23 +0000838 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) {
Dan Gohmanb98c1a32009-04-21 01:07:12 +0000839 const Type *IntTy = getEffectiveSCEVType(Ty);
Dan Gohman01c2ee72009-04-16 03:18:22 +0000840 Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy);
841 if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty);
Dan Gohman55788cf2009-06-24 00:38:39 +0000842 return getConstant(cast<ConstantInt>(C));
Dan Gohman01c2ee72009-04-16 03:18:22 +0000843 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000844
Dan Gohman1a5c4992009-04-22 16:20:48 +0000845 // sext(sext(x)) --> sext(x)
Dan Gohmanc76b5452009-05-04 22:02:23 +0000846 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
Dan Gohman1a5c4992009-04-22 16:20:48 +0000847 return getSignExtendExpr(SS->getOperand(), Ty);
848
Dan Gohmandb888422009-07-13 20:55:53 +0000849 // Before doing any expensive analysis, check to see if we've already
850 // computed a SCEV for this Op and Ty.
851 FoldingSetNodeID ID;
852 ID.AddInteger(scSignExtend);
853 ID.AddPointer(Op);
854 ID.AddPointer(Ty);
855 void *IP = 0;
856 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
857
Dan Gohmana9dba962009-04-27 20:16:15 +0000858 // If the input value is a chrec scev, and we can prove that the value
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 // did not overflow the old, smaller, value, we can sign extend all of the
Dan Gohmana9dba962009-04-27 20:16:15 +0000860 // operands (often constants). This allows analysis of something like
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861 // this: for (signed char X = 0; X < 100; ++X) { int Y = X; }
Dan Gohmanc76b5452009-05-04 22:02:23 +0000862 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
Dan Gohmana9dba962009-04-27 20:16:15 +0000863 if (AR->isAffine()) {
Dan Gohman55e2d7e2009-07-13 21:35:55 +0000864 const SCEV *Start = AR->getStart();
865 const SCEV *Step = AR->getStepRecurrence(*this);
866 unsigned BitWidth = getTypeSizeInBits(AR->getType());
867 const Loop *L = AR->getLoop();
868
Dan Gohmana9dba962009-04-27 20:16:15 +0000869 // Check whether the backedge-taken count is SCEVCouldNotCompute.
870 // Note that this serves two purposes: It filters out loops that are
871 // simply not analyzable, and it covers the case where this code is
872 // being called from within backedge-taken count analysis, such that
873 // attempting to ask for the backedge-taken count would likely result
874 // in infinite recursion. In the later case, the analysis code will
875 // cope with a conservative value, and it will take care to purge
876 // that value once it has finished.
Dan Gohman55e2d7e2009-07-13 21:35:55 +0000877 const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000878 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
Dan Gohman4ada77f2009-04-29 01:54:20 +0000879 // Manually compute the final value for AR, checking for
Dan Gohman3ded5b22009-04-29 22:28:28 +0000880 // overflow.
Dan Gohmana9dba962009-04-27 20:16:15 +0000881
882 // Check whether the backedge-taken count can be losslessly casted to
Dan Gohman3ded5b22009-04-29 22:28:28 +0000883 // the addrec's type. The count is always unsigned.
Dan Gohman161ea032009-07-07 17:06:11 +0000884 const SCEV *CastedMaxBECount =
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000885 getTruncateOrZeroExtend(MaxBECount, Start->getType());
Dan Gohman161ea032009-07-07 17:06:11 +0000886 const SCEV *RecastedMaxBECount =
Dan Gohman3bb37f52009-05-18 15:58:39 +0000887 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
888 if (MaxBECount == RecastedMaxBECount) {
Dan Gohman55e2d7e2009-07-13 21:35:55 +0000889 const Type *WideTy = IntegerType::get(BitWidth * 2);
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000890 // Check whether Start+Step*MaxBECount has no signed overflow.
Dan Gohman161ea032009-07-07 17:06:11 +0000891 const SCEV *SMul =
Dan Gohmanf7d3d25542009-04-30 20:47:05 +0000892 getMulExpr(CastedMaxBECount,
Dan Gohmana9dba962009-04-27 20:16:15 +0000893 getTruncateOrSignExtend(Step, Start->getType()));
Dan Gohman161ea032009-07-07 17:06:11 +0000894 const SCEV *Add = getAddExpr(Start, SMul);
895 const SCEV *OperandExtendedAdd =
Dan Gohman3bb37f52009-05-18 15:58:39 +0000896 getAddExpr(getSignExtendExpr(Start, WideTy),
897 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
898 getSignExtendExpr(Step, WideTy)));
899 if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd)
Dan Gohman3ded5b22009-04-29 22:28:28 +0000900 // Return the expression with the addrec on the outside.
901 return getAddRecExpr(getSignExtendExpr(Start, Ty),
902 getSignExtendExpr(Step, Ty),
Dan Gohman55e2d7e2009-07-13 21:35:55 +0000903 L);
Dan Gohman2d4f5b12009-07-16 17:34:36 +0000904
905 // Similar to above, only this time treat the step value as unsigned.
906 // This covers loops that count up with an unsigned step.
907 const SCEV *UMul =
908 getMulExpr(CastedMaxBECount,
909 getTruncateOrZeroExtend(Step, Start->getType()));
910 Add = getAddExpr(Start, UMul);
911 OperandExtendedAdd =
912 getAddExpr(getZeroExtendExpr(Start, WideTy),
913 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
914 getZeroExtendExpr(Step, WideTy)));
915 if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd)
916 // Return the expression with the addrec on the outside.
917 return getAddRecExpr(getSignExtendExpr(Start, Ty),
918 getZeroExtendExpr(Step, Ty),
919 L);
Dan Gohman55e2d7e2009-07-13 21:35:55 +0000920 }
921
922 // If the backedge is guarded by a comparison with the pre-inc value
923 // the addrec is safe. Also, if the entry is guarded by a comparison
924 // with the start value and the backedge is guarded by a comparison
925 // with the post-inc value, the addrec is safe.
926 if (isKnownPositive(Step)) {
927 const SCEV *N = getConstant(APInt::getSignedMinValue(BitWidth) -
928 getSignedRange(Step).getSignedMax());
929 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, AR, N) ||
930 (isLoopGuardedByCond(L, ICmpInst::ICMP_SLT, Start, N) &&
931 isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT,
932 AR->getPostIncExpr(*this), N)))
933 // Return the expression with the addrec on the outside.
934 return getAddRecExpr(getSignExtendExpr(Start, Ty),
935 getSignExtendExpr(Step, Ty),
936 L);
937 } else if (isKnownNegative(Step)) {
938 const SCEV *N = getConstant(APInt::getSignedMaxValue(BitWidth) -
939 getSignedRange(Step).getSignedMin());
940 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, AR, N) ||
941 (isLoopGuardedByCond(L, ICmpInst::ICMP_SGT, Start, N) &&
942 isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT,
943 AR->getPostIncExpr(*this), N)))
944 // Return the expression with the addrec on the outside.
945 return getAddRecExpr(getSignExtendExpr(Start, Ty),
946 getSignExtendExpr(Step, Ty),
947 L);
Dan Gohmana9dba962009-04-27 20:16:15 +0000948 }
949 }
950 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000951
Dan Gohmandb888422009-07-13 20:55:53 +0000952 // The cast wasn't folded; create an explicit cast node.
953 // Recompute the insert position, as it may have been invalidated.
Dan Gohmanc6475cb2009-06-27 21:21:31 +0000954 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
955 SCEV *S = SCEVAllocator.Allocate<SCEVSignExtendExpr>();
Dan Gohmand43a8282009-07-13 20:50:19 +0000956 new (S) SCEVSignExtendExpr(ID, Op, Ty);
Dan Gohmanc6475cb2009-06-27 21:21:31 +0000957 UniqueSCEVs.InsertNode(S, IP);
958 return S;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000959}
960
Dan Gohmane1ca7e82009-06-13 15:56:47 +0000961/// getAnyExtendExpr - Return a SCEV for the given operand extended with
962/// unspecified bits out to the given type.
963///
Dan Gohman161ea032009-07-07 17:06:11 +0000964const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op,
Dan Gohmane1ca7e82009-06-13 15:56:47 +0000965 const Type *Ty) {
966 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
967 "This is not an extending conversion!");
968 assert(isSCEVable(Ty) &&
969 "This is not a conversion to a SCEVable type!");
970 Ty = getEffectiveSCEVType(Ty);
971
972 // Sign-extend negative constants.
973 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
974 if (SC->getValue()->getValue().isNegative())
975 return getSignExtendExpr(Op, Ty);
976
977 // Peel off a truncate cast.
978 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) {
Dan Gohman161ea032009-07-07 17:06:11 +0000979 const SCEV *NewOp = T->getOperand();
Dan Gohmane1ca7e82009-06-13 15:56:47 +0000980 if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty))
981 return getAnyExtendExpr(NewOp, Ty);
982 return getTruncateOrNoop(NewOp, Ty);
983 }
984
985 // Next try a zext cast. If the cast is folded, use it.
Dan Gohman161ea032009-07-07 17:06:11 +0000986 const SCEV *ZExt = getZeroExtendExpr(Op, Ty);
Dan Gohmane1ca7e82009-06-13 15:56:47 +0000987 if (!isa<SCEVZeroExtendExpr>(ZExt))
988 return ZExt;
989
990 // Next try a sext cast. If the cast is folded, use it.
Dan Gohman161ea032009-07-07 17:06:11 +0000991 const SCEV *SExt = getSignExtendExpr(Op, Ty);
Dan Gohmane1ca7e82009-06-13 15:56:47 +0000992 if (!isa<SCEVSignExtendExpr>(SExt))
993 return SExt;
994
995 // If the expression is obviously signed, use the sext cast value.
996 if (isa<SCEVSMaxExpr>(Op))
997 return SExt;
998
999 // Absent any other information, use the zext cast value.
1000 return ZExt;
1001}
1002
Dan Gohman27bd4cb2009-06-14 22:58:51 +00001003/// CollectAddOperandsWithScales - Process the given Ops list, which is
1004/// a list of operands to be added under the given scale, update the given
1005/// map. This is a helper function for getAddRecExpr. As an example of
1006/// what it does, given a sequence of operands that would form an add
1007/// expression like this:
1008///
1009/// m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r)
1010///
1011/// where A and B are constants, update the map with these values:
1012///
1013/// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0)
1014///
1015/// and add 13 + A*B*29 to AccumulatedConstant.
1016/// This will allow getAddRecExpr to produce this:
1017///
1018/// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B)
1019///
1020/// This form often exposes folding opportunities that are hidden in
1021/// the original operand list.
1022///
1023/// Return true iff it appears that any interesting folding opportunities
1024/// may be exposed. This helps getAddRecExpr short-circuit extra work in
1025/// the common case where no interesting opportunities are present, and
1026/// is also used as a check to avoid infinite recursion.
1027///
1028static bool
Dan Gohman161ea032009-07-07 17:06:11 +00001029CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M,
1030 SmallVector<const SCEV *, 8> &NewOps,
Dan Gohman27bd4cb2009-06-14 22:58:51 +00001031 APInt &AccumulatedConstant,
Dan Gohman161ea032009-07-07 17:06:11 +00001032 const SmallVectorImpl<const SCEV *> &Ops,
Dan Gohman27bd4cb2009-06-14 22:58:51 +00001033 const APInt &Scale,
1034 ScalarEvolution &SE) {
1035 bool Interesting = false;
1036
1037 // Iterate over the add operands.
1038 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1039 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]);
1040 if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) {
1041 APInt NewScale =
1042 Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue();
1043 if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) {
1044 // A multiplication of a constant with another add; recurse.
1045 Interesting |=
1046 CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
1047 cast<SCEVAddExpr>(Mul->getOperand(1))
1048 ->getOperands(),
1049 NewScale, SE);
1050 } else {
1051 // A multiplication of a constant with some other value. Update
1052 // the map.
Dan Gohman161ea032009-07-07 17:06:11 +00001053 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end());
1054 const SCEV *Key = SE.getMulExpr(MulOps);
1055 std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
Dan Gohman3bf01f02009-06-29 18:25:52 +00001056 M.insert(std::make_pair(Key, NewScale));
Dan Gohman27bd4cb2009-06-14 22:58:51 +00001057 if (Pair.second) {
Dan Gohman27bd4cb2009-06-14 22:58:51 +00001058 NewOps.push_back(Pair.first->first);
1059 } else {
1060 Pair.first->second += NewScale;
1061 // The map already had an entry for this value, which may indicate
1062 // a folding opportunity.
1063 Interesting = true;
1064 }
1065 }
1066 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
1067 // Pull a buried constant out to the outside.
1068 if (Scale != 1 || AccumulatedConstant != 0 || C->isZero())
1069 Interesting = true;
1070 AccumulatedConstant += Scale * C->getValue()->getValue();
1071 } else {
1072 // An ordinary operand. Update the map.
Dan Gohman161ea032009-07-07 17:06:11 +00001073 std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
Dan Gohman3bf01f02009-06-29 18:25:52 +00001074 M.insert(std::make_pair(Ops[i], Scale));
Dan Gohman27bd4cb2009-06-14 22:58:51 +00001075 if (Pair.second) {
Dan Gohman27bd4cb2009-06-14 22:58:51 +00001076 NewOps.push_back(Pair.first->first);
1077 } else {
1078 Pair.first->second += Scale;
1079 // The map already had an entry for this value, which may indicate
1080 // a folding opportunity.
1081 Interesting = true;
1082 }
1083 }
1084 }
1085
1086 return Interesting;
1087}
1088
1089namespace {
1090 struct APIntCompare {
1091 bool operator()(const APInt &LHS, const APInt &RHS) const {
1092 return LHS.ult(RHS);
1093 }
1094 };
1095}
1096
Dan Gohmanc8a29272009-05-24 23:45:28 +00001097/// getAddExpr - Get a canonical add expression, or something simpler if
1098/// possible.
Dan Gohman161ea032009-07-07 17:06:11 +00001099const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001100 assert(!Ops.empty() && "Cannot get empty add!");
1101 if (Ops.size() == 1) return Ops[0];
Dan Gohmana77b3d42009-05-18 15:44:58 +00001102#ifndef NDEBUG
1103 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1104 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1105 getEffectiveSCEVType(Ops[0]->getType()) &&
1106 "SCEVAddExpr operand types don't match!");
1107#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001108
1109 // Sort by complexity, this groups all similar expression types together.
Dan Gohman5d486452009-05-07 14:39:04 +00001110 GroupByComplexity(Ops, LI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001111
1112 // If there are any constants, fold them together.
1113 unsigned Idx = 0;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001114 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001115 ++Idx;
1116 assert(Idx < Ops.size());
Dan Gohmanc76b5452009-05-04 22:02:23 +00001117 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001118 // We found two constants, fold them together!
Dan Gohman02ff9392009-06-14 22:47:23 +00001119 Ops[0] = getConstant(LHSC->getValue()->getValue() +
1120 RHSC->getValue()->getValue());
Dan Gohman68f23e82009-06-14 22:53:57 +00001121 if (Ops.size() == 2) return Ops[0];
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001122 Ops.erase(Ops.begin()+1); // Erase the folded element
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001123 LHSC = cast<SCEVConstant>(Ops[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001124 }
1125
1126 // If we are left with a constant zero being added, strip it off.
1127 if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
1128 Ops.erase(Ops.begin());
1129 --Idx;
1130 }
1131 }
1132
1133 if (Ops.size() == 1) return Ops[0];
1134
1135 // Okay, check to see if the same value occurs in the operand list twice. If
1136 // so, merge them together into an multiply expression. Since we sorted the
1137 // list, these values are required to be adjacent.
1138 const Type *Ty = Ops[0]->getType();
1139 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
1140 if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2
1141 // Found a match, merge the two values into a multiply, and add any
1142 // remaining values to the result.
Dan Gohman161ea032009-07-07 17:06:11 +00001143 const SCEV *Two = getIntegerSCEV(2, Ty);
1144 const SCEV *Mul = getMulExpr(Ops[i], Two);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001145 if (Ops.size() == 2)
1146 return Mul;
1147 Ops.erase(Ops.begin()+i, Ops.begin()+i+2);
1148 Ops.push_back(Mul);
Dan Gohman89f85052007-10-22 18:31:58 +00001149 return getAddExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001150 }
1151
Dan Gohman45b3b542009-05-08 21:03:19 +00001152 // Check for truncates. If all the operands are truncated from the same
1153 // type, see if factoring out the truncate would permit the result to be
1154 // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n)
1155 // if the contents of the resulting outer trunc fold to something simple.
1156 for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) {
1157 const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]);
1158 const Type *DstType = Trunc->getType();
1159 const Type *SrcType = Trunc->getOperand()->getType();
Dan Gohman161ea032009-07-07 17:06:11 +00001160 SmallVector<const SCEV *, 8> LargeOps;
Dan Gohman45b3b542009-05-08 21:03:19 +00001161 bool Ok = true;
1162 // Check all the operands to see if they can be represented in the
1163 // source type of the truncate.
1164 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1165 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) {
1166 if (T->getOperand()->getType() != SrcType) {
1167 Ok = false;
1168 break;
1169 }
1170 LargeOps.push_back(T->getOperand());
1171 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
1172 // This could be either sign or zero extension, but sign extension
1173 // is much more likely to be foldable here.
1174 LargeOps.push_back(getSignExtendExpr(C, SrcType));
1175 } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) {
Dan Gohman161ea032009-07-07 17:06:11 +00001176 SmallVector<const SCEV *, 8> LargeMulOps;
Dan Gohman45b3b542009-05-08 21:03:19 +00001177 for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
1178 if (const SCEVTruncateExpr *T =
1179 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) {
1180 if (T->getOperand()->getType() != SrcType) {
1181 Ok = false;
1182 break;
1183 }
1184 LargeMulOps.push_back(T->getOperand());
1185 } else if (const SCEVConstant *C =
1186 dyn_cast<SCEVConstant>(M->getOperand(j))) {
1187 // This could be either sign or zero extension, but sign extension
1188 // is much more likely to be foldable here.
1189 LargeMulOps.push_back(getSignExtendExpr(C, SrcType));
1190 } else {
1191 Ok = false;
1192 break;
1193 }
1194 }
1195 if (Ok)
1196 LargeOps.push_back(getMulExpr(LargeMulOps));
1197 } else {
1198 Ok = false;
1199 break;
1200 }
1201 }
1202 if (Ok) {
1203 // Evaluate the expression in the larger type.
Dan Gohman161ea032009-07-07 17:06:11 +00001204 const SCEV *Fold = getAddExpr(LargeOps);
Dan Gohman45b3b542009-05-08 21:03:19 +00001205 // If it folds to something simple, use it. Otherwise, don't.
1206 if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold))
1207 return getTruncateExpr(Fold, DstType);
1208 }
1209 }
1210
1211 // Skip past any other cast SCEVs.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001212 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr)
1213 ++Idx;
1214
1215 // If there are add operands they would be next.
1216 if (Idx < Ops.size()) {
1217 bool DeletedAdd = false;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001218 while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001219 // If we have an add, expand the add operands onto the end of the operands
1220 // list.
1221 Ops.insert(Ops.end(), Add->op_begin(), Add->op_end());
1222 Ops.erase(Ops.begin()+Idx);
1223 DeletedAdd = true;
1224 }
1225
1226 // If we deleted at least one add, we added operands to the end of the list,
1227 // and they are not necessarily sorted. Recurse to resort and resimplify
1228 // any operands we just aquired.
1229 if (DeletedAdd)
Dan Gohman89f85052007-10-22 18:31:58 +00001230 return getAddExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001231 }
1232
1233 // Skip over the add expression until we get to a multiply.
1234 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
1235 ++Idx;
1236
Dan Gohman27bd4cb2009-06-14 22:58:51 +00001237 // Check to see if there are any folding opportunities present with
1238 // operands multiplied by constant values.
1239 if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) {
1240 uint64_t BitWidth = getTypeSizeInBits(Ty);
Dan Gohman161ea032009-07-07 17:06:11 +00001241 DenseMap<const SCEV *, APInt> M;
1242 SmallVector<const SCEV *, 8> NewOps;
Dan Gohman27bd4cb2009-06-14 22:58:51 +00001243 APInt AccumulatedConstant(BitWidth, 0);
1244 if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
1245 Ops, APInt(BitWidth, 1), *this)) {
1246 // Some interesting folding opportunity is present, so its worthwhile to
1247 // re-generate the operands list. Group the operands by constant scale,
1248 // to avoid multiplying by the same constant scale multiple times.
Dan Gohman161ea032009-07-07 17:06:11 +00001249 std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists;
1250 for (SmallVector<const SCEV *, 8>::iterator I = NewOps.begin(),
Dan Gohman27bd4cb2009-06-14 22:58:51 +00001251 E = NewOps.end(); I != E; ++I)
1252 MulOpLists[M.find(*I)->second].push_back(*I);
1253 // Re-generate the operands list.
1254 Ops.clear();
1255 if (AccumulatedConstant != 0)
1256 Ops.push_back(getConstant(AccumulatedConstant));
Dan Gohman9bc642f2009-06-24 04:48:43 +00001257 for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator
1258 I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I)
Dan Gohman27bd4cb2009-06-14 22:58:51 +00001259 if (I->first != 0)
Dan Gohman9bc642f2009-06-24 04:48:43 +00001260 Ops.push_back(getMulExpr(getConstant(I->first),
1261 getAddExpr(I->second)));
Dan Gohman27bd4cb2009-06-14 22:58:51 +00001262 if (Ops.empty())
1263 return getIntegerSCEV(0, Ty);
1264 if (Ops.size() == 1)
1265 return Ops[0];
1266 return getAddExpr(Ops);
1267 }
1268 }
1269
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001270 // If we are adding something to a multiply expression, make sure the
1271 // something is not already an operand of the multiply. If so, merge it into
1272 // the multiply.
1273 for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00001274 const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001275 for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00001276 const SCEV *MulOpSCEV = Mul->getOperand(MulOp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001277 for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
Dan Gohman02ff9392009-06-14 22:47:23 +00001278 if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(Ops[AddOp])) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001279 // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1))
Dan Gohman161ea032009-07-07 17:06:11 +00001280 const SCEV *InnerMul = Mul->getOperand(MulOp == 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001281 if (Mul->getNumOperands() != 2) {
1282 // If the multiply has more than two operands, we must get the
1283 // Y*Z term.
Dan Gohman161ea032009-07-07 17:06:11 +00001284 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), Mul->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001285 MulOps.erase(MulOps.begin()+MulOp);
Dan Gohman89f85052007-10-22 18:31:58 +00001286 InnerMul = getMulExpr(MulOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001287 }
Dan Gohman161ea032009-07-07 17:06:11 +00001288 const SCEV *One = getIntegerSCEV(1, Ty);
1289 const SCEV *AddOne = getAddExpr(InnerMul, One);
1290 const SCEV *OuterMul = getMulExpr(AddOne, Ops[AddOp]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001291 if (Ops.size() == 2) return OuterMul;
1292 if (AddOp < Idx) {
1293 Ops.erase(Ops.begin()+AddOp);
1294 Ops.erase(Ops.begin()+Idx-1);
1295 } else {
1296 Ops.erase(Ops.begin()+Idx);
1297 Ops.erase(Ops.begin()+AddOp-1);
1298 }
1299 Ops.push_back(OuterMul);
Dan Gohman89f85052007-10-22 18:31:58 +00001300 return getAddExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001301 }
1302
1303 // Check this multiply against other multiplies being added together.
1304 for (unsigned OtherMulIdx = Idx+1;
1305 OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
1306 ++OtherMulIdx) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00001307 const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001308 // If MulOp occurs in OtherMul, we can fold the two multiplies
1309 // together.
1310 for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
1311 OMulOp != e; ++OMulOp)
1312 if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
1313 // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
Dan Gohman161ea032009-07-07 17:06:11 +00001314 const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001315 if (Mul->getNumOperands() != 2) {
Dan Gohman9bc642f2009-06-24 04:48:43 +00001316 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
1317 Mul->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001318 MulOps.erase(MulOps.begin()+MulOp);
Dan Gohman89f85052007-10-22 18:31:58 +00001319 InnerMul1 = getMulExpr(MulOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001320 }
Dan Gohman161ea032009-07-07 17:06:11 +00001321 const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001322 if (OtherMul->getNumOperands() != 2) {
Dan Gohman9bc642f2009-06-24 04:48:43 +00001323 SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(),
1324 OtherMul->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001325 MulOps.erase(MulOps.begin()+OMulOp);
Dan Gohman89f85052007-10-22 18:31:58 +00001326 InnerMul2 = getMulExpr(MulOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001327 }
Dan Gohman161ea032009-07-07 17:06:11 +00001328 const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2);
1329 const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001330 if (Ops.size() == 2) return OuterMul;
1331 Ops.erase(Ops.begin()+Idx);
1332 Ops.erase(Ops.begin()+OtherMulIdx-1);
1333 Ops.push_back(OuterMul);
Dan Gohman89f85052007-10-22 18:31:58 +00001334 return getAddExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001335 }
1336 }
1337 }
1338 }
1339
1340 // If there are any add recurrences in the operands list, see if any other
1341 // added values are loop invariant. If so, we can fold them into the
1342 // recurrence.
1343 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
1344 ++Idx;
1345
1346 // Scan over all recurrences, trying to fold loop invariants into them.
1347 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
1348 // Scan all of the other operands to this add and add them to the vector if
1349 // they are loop invariant w.r.t. the recurrence.
Dan Gohman161ea032009-07-07 17:06:11 +00001350 SmallVector<const SCEV *, 8> LIOps;
Dan Gohmanbff6b582009-05-04 22:30:44 +00001351 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001352 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1353 if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
1354 LIOps.push_back(Ops[i]);
1355 Ops.erase(Ops.begin()+i);
1356 --i; --e;
1357 }
1358
1359 // If we found some loop invariants, fold them into the recurrence.
1360 if (!LIOps.empty()) {
Dan Gohmanabe991f2008-09-14 17:21:12 +00001361 // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001362 LIOps.push_back(AddRec->getStart());
1363
Dan Gohman161ea032009-07-07 17:06:11 +00001364 SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(),
Dan Gohman02ff9392009-06-14 22:47:23 +00001365 AddRec->op_end());
Dan Gohman89f85052007-10-22 18:31:58 +00001366 AddRecOps[0] = getAddExpr(LIOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001367
Dan Gohman161ea032009-07-07 17:06:11 +00001368 const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRec->getLoop());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001369 // If all of the other operands were loop invariant, we are done.
1370 if (Ops.size() == 1) return NewRec;
1371
1372 // Otherwise, add the folded AddRec by the non-liv parts.
1373 for (unsigned i = 0;; ++i)
1374 if (Ops[i] == AddRec) {
1375 Ops[i] = NewRec;
1376 break;
1377 }
Dan Gohman89f85052007-10-22 18:31:58 +00001378 return getAddExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379 }
1380
1381 // Okay, if there weren't any loop invariants to be folded, check to see if
1382 // there are multiple AddRec's with the same loop induction variable being
1383 // added together. If so, we can fold them.
1384 for (unsigned OtherIdx = Idx+1;
1385 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
1386 if (OtherIdx != Idx) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00001387 const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388 if (AddRec->getLoop() == OtherAddRec->getLoop()) {
1389 // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D}
Dan Gohman9bc642f2009-06-24 04:48:43 +00001390 SmallVector<const SCEV *, 4> NewOps(AddRec->op_begin(),
1391 AddRec->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001392 for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) {
1393 if (i >= NewOps.size()) {
1394 NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i,
1395 OtherAddRec->op_end());
1396 break;
1397 }
Dan Gohman89f85052007-10-22 18:31:58 +00001398 NewOps[i] = getAddExpr(NewOps[i], OtherAddRec->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001399 }
Dan Gohman161ea032009-07-07 17:06:11 +00001400 const SCEV *NewAddRec = getAddRecExpr(NewOps, AddRec->getLoop());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001401
1402 if (Ops.size() == 2) return NewAddRec;
1403
1404 Ops.erase(Ops.begin()+Idx);
1405 Ops.erase(Ops.begin()+OtherIdx-1);
1406 Ops.push_back(NewAddRec);
Dan Gohman89f85052007-10-22 18:31:58 +00001407 return getAddExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001408 }
1409 }
1410
1411 // Otherwise couldn't fold anything into this recurrence. Move onto the
1412 // next one.
1413 }
1414
1415 // Okay, it looks like we really DO need an add expr. Check to see if we
1416 // already have one, otherwise create a new one.
Dan Gohmanc6475cb2009-06-27 21:21:31 +00001417 FoldingSetNodeID ID;
1418 ID.AddInteger(scAddExpr);
1419 ID.AddInteger(Ops.size());
1420 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1421 ID.AddPointer(Ops[i]);
1422 void *IP = 0;
1423 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1424 SCEV *S = SCEVAllocator.Allocate<SCEVAddExpr>();
Dan Gohmand43a8282009-07-13 20:50:19 +00001425 new (S) SCEVAddExpr(ID, Ops);
Dan Gohmanc6475cb2009-06-27 21:21:31 +00001426 UniqueSCEVs.InsertNode(S, IP);
1427 return S;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001428}
1429
1430
Dan Gohmanc8a29272009-05-24 23:45:28 +00001431/// getMulExpr - Get a canonical multiply expression, or something simpler if
1432/// possible.
Dan Gohman161ea032009-07-07 17:06:11 +00001433const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001434 assert(!Ops.empty() && "Cannot get empty mul!");
Dan Gohmana77b3d42009-05-18 15:44:58 +00001435#ifndef NDEBUG
1436 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1437 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1438 getEffectiveSCEVType(Ops[0]->getType()) &&
1439 "SCEVMulExpr operand types don't match!");
1440#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001441
1442 // Sort by complexity, this groups all similar expression types together.
Dan Gohman5d486452009-05-07 14:39:04 +00001443 GroupByComplexity(Ops, LI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001444
1445 // If there are any constants, fold them together.
1446 unsigned Idx = 0;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001447 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001448
1449 // C1*(C2+V) -> C1*C2 + C1*V
1450 if (Ops.size() == 2)
Dan Gohmanc76b5452009-05-04 22:02:23 +00001451 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001452 if (Add->getNumOperands() == 2 &&
1453 isa<SCEVConstant>(Add->getOperand(0)))
Dan Gohman89f85052007-10-22 18:31:58 +00001454 return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)),
1455 getMulExpr(LHSC, Add->getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001456
1457
1458 ++Idx;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001459 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001460 // We found two constants, fold them together!
Owen Andersoneacb44d2009-07-24 23:12:02 +00001461 ConstantInt *Fold = ConstantInt::get(getContext(),
1462 LHSC->getValue()->getValue() *
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001463 RHSC->getValue()->getValue());
1464 Ops[0] = getConstant(Fold);
1465 Ops.erase(Ops.begin()+1); // Erase the folded element
1466 if (Ops.size() == 1) return Ops[0];
1467 LHSC = cast<SCEVConstant>(Ops[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001468 }
1469
1470 // If we are left with a constant one being multiplied, strip it off.
1471 if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) {
1472 Ops.erase(Ops.begin());
1473 --Idx;
1474 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
1475 // If we have a multiply of zero, it will always be zero.
1476 return Ops[0];
1477 }
1478 }
1479
1480 // Skip over the add expression until we get to a multiply.
1481 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
1482 ++Idx;
1483
1484 if (Ops.size() == 1)
1485 return Ops[0];
1486
1487 // If there are mul operands inline them all into this expression.
1488 if (Idx < Ops.size()) {
1489 bool DeletedMul = false;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001490 while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491 // If we have an mul, expand the mul operands onto the end of the operands
1492 // list.
1493 Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end());
1494 Ops.erase(Ops.begin()+Idx);
1495 DeletedMul = true;
1496 }
1497
1498 // If we deleted at least one mul, we added operands to the end of the list,
1499 // and they are not necessarily sorted. Recurse to resort and resimplify
1500 // any operands we just aquired.
1501 if (DeletedMul)
Dan Gohman89f85052007-10-22 18:31:58 +00001502 return getMulExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001503 }
1504
1505 // If there are any add recurrences in the operands list, see if any other
1506 // added values are loop invariant. If so, we can fold them into the
1507 // recurrence.
1508 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
1509 ++Idx;
1510
1511 // Scan over all recurrences, trying to fold loop invariants into them.
1512 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
1513 // Scan all of the other operands to this mul and add them to the vector if
1514 // they are loop invariant w.r.t. the recurrence.
Dan Gohman161ea032009-07-07 17:06:11 +00001515 SmallVector<const SCEV *, 8> LIOps;
Dan Gohmanbff6b582009-05-04 22:30:44 +00001516 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001517 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1518 if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
1519 LIOps.push_back(Ops[i]);
1520 Ops.erase(Ops.begin()+i);
1521 --i; --e;
1522 }
1523
1524 // If we found some loop invariants, fold them into the recurrence.
1525 if (!LIOps.empty()) {
Dan Gohmanabe991f2008-09-14 17:21:12 +00001526 // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step}
Dan Gohman161ea032009-07-07 17:06:11 +00001527 SmallVector<const SCEV *, 4> NewOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001528 NewOps.reserve(AddRec->getNumOperands());
1529 if (LIOps.size() == 1) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00001530 const SCEV *Scale = LIOps[0];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001531 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
Dan Gohman89f85052007-10-22 18:31:58 +00001532 NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001533 } else {
1534 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
Dan Gohman161ea032009-07-07 17:06:11 +00001535 SmallVector<const SCEV *, 4> MulOps(LIOps.begin(), LIOps.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001536 MulOps.push_back(AddRec->getOperand(i));
Dan Gohman89f85052007-10-22 18:31:58 +00001537 NewOps.push_back(getMulExpr(MulOps));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001538 }
1539 }
1540
Dan Gohman161ea032009-07-07 17:06:11 +00001541 const SCEV *NewRec = getAddRecExpr(NewOps, AddRec->getLoop());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001542
1543 // If all of the other operands were loop invariant, we are done.
1544 if (Ops.size() == 1) return NewRec;
1545
1546 // Otherwise, multiply the folded AddRec by the non-liv parts.
1547 for (unsigned i = 0;; ++i)
1548 if (Ops[i] == AddRec) {
1549 Ops[i] = NewRec;
1550 break;
1551 }
Dan Gohman89f85052007-10-22 18:31:58 +00001552 return getMulExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001553 }
1554
1555 // Okay, if there weren't any loop invariants to be folded, check to see if
1556 // there are multiple AddRec's with the same loop induction variable being
1557 // multiplied together. If so, we can fold them.
1558 for (unsigned OtherIdx = Idx+1;
1559 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
1560 if (OtherIdx != Idx) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00001561 const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001562 if (AddRec->getLoop() == OtherAddRec->getLoop()) {
1563 // F * G --> {A,+,B} * {C,+,D} --> {A*C,+,F*D + G*B + B*D}
Dan Gohmanbff6b582009-05-04 22:30:44 +00001564 const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec;
Dan Gohman161ea032009-07-07 17:06:11 +00001565 const SCEV *NewStart = getMulExpr(F->getStart(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001566 G->getStart());
Dan Gohman161ea032009-07-07 17:06:11 +00001567 const SCEV *B = F->getStepRecurrence(*this);
1568 const SCEV *D = G->getStepRecurrence(*this);
1569 const SCEV *NewStep = getAddExpr(getMulExpr(F, D),
Dan Gohman89f85052007-10-22 18:31:58 +00001570 getMulExpr(G, B),
1571 getMulExpr(B, D));
Dan Gohman161ea032009-07-07 17:06:11 +00001572 const SCEV *NewAddRec = getAddRecExpr(NewStart, NewStep,
Dan Gohman89f85052007-10-22 18:31:58 +00001573 F->getLoop());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001574 if (Ops.size() == 2) return NewAddRec;
1575
1576 Ops.erase(Ops.begin()+Idx);
1577 Ops.erase(Ops.begin()+OtherIdx-1);
1578 Ops.push_back(NewAddRec);
Dan Gohman89f85052007-10-22 18:31:58 +00001579 return getMulExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001580 }
1581 }
1582
1583 // Otherwise couldn't fold anything into this recurrence. Move onto the
1584 // next one.
1585 }
1586
1587 // Okay, it looks like we really DO need an mul expr. Check to see if we
1588 // already have one, otherwise create a new one.
Dan Gohmanc6475cb2009-06-27 21:21:31 +00001589 FoldingSetNodeID ID;
1590 ID.AddInteger(scMulExpr);
1591 ID.AddInteger(Ops.size());
1592 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1593 ID.AddPointer(Ops[i]);
1594 void *IP = 0;
1595 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1596 SCEV *S = SCEVAllocator.Allocate<SCEVMulExpr>();
Dan Gohmand43a8282009-07-13 20:50:19 +00001597 new (S) SCEVMulExpr(ID, Ops);
Dan Gohmanc6475cb2009-06-27 21:21:31 +00001598 UniqueSCEVs.InsertNode(S, IP);
1599 return S;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001600}
1601
Dan Gohmanc8a29272009-05-24 23:45:28 +00001602/// getUDivExpr - Get a canonical multiply expression, or something simpler if
1603/// possible.
Dan Gohman8c4f20b2009-06-24 14:49:00 +00001604const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
1605 const SCEV *RHS) {
Dan Gohmana77b3d42009-05-18 15:44:58 +00001606 assert(getEffectiveSCEVType(LHS->getType()) ==
1607 getEffectiveSCEVType(RHS->getType()) &&
1608 "SCEVUDivExpr operand types don't match!");
1609
Dan Gohmanc76b5452009-05-04 22:02:23 +00001610 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001611 if (RHSC->getValue()->equalsInt(1))
Nick Lewycky35b56022009-01-13 09:18:58 +00001612 return LHS; // X udiv 1 --> x
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001613 if (RHSC->isZero())
1614 return getIntegerSCEV(0, LHS->getType()); // value is undefined
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001615
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001616 // Determine if the division can be folded into the operands of
1617 // its operands.
1618 // TODO: Generalize this to non-constants by using known-bits information.
1619 const Type *Ty = LHS->getType();
1620 unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros();
1621 unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ;
1622 // For non-power-of-two values, effectively round the value up to the
1623 // nearest power of two.
1624 if (!RHSC->getValue()->getValue().isPowerOf2())
1625 ++MaxShiftAmt;
1626 const IntegerType *ExtTy =
1627 IntegerType::get(getTypeSizeInBits(Ty) + MaxShiftAmt);
1628 // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
1629 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
1630 if (const SCEVConstant *Step =
1631 dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this)))
1632 if (!Step->getValue()->getValue()
1633 .urem(RHSC->getValue()->getValue()) &&
Dan Gohman14374d32009-05-08 23:11:16 +00001634 getZeroExtendExpr(AR, ExtTy) ==
1635 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
1636 getZeroExtendExpr(Step, ExtTy),
1637 AR->getLoop())) {
Dan Gohman161ea032009-07-07 17:06:11 +00001638 SmallVector<const SCEV *, 4> Operands;
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001639 for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i)
1640 Operands.push_back(getUDivExpr(AR->getOperand(i), RHS));
1641 return getAddRecExpr(Operands, AR->getLoop());
1642 }
1643 // (A*B)/C --> A*(B/C) if safe and B/C can be folded.
Dan Gohman14374d32009-05-08 23:11:16 +00001644 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
Dan Gohman161ea032009-07-07 17:06:11 +00001645 SmallVector<const SCEV *, 4> Operands;
Dan Gohman14374d32009-05-08 23:11:16 +00001646 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i)
1647 Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy));
1648 if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands))
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001649 // Find an operand that's safely divisible.
1650 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
Dan Gohman161ea032009-07-07 17:06:11 +00001651 const SCEV *Op = M->getOperand(i);
1652 const SCEV *Div = getUDivExpr(Op, RHSC);
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001653 if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) {
Dan Gohman161ea032009-07-07 17:06:11 +00001654 const SmallVectorImpl<const SCEV *> &MOperands = M->getOperands();
1655 Operands = SmallVector<const SCEV *, 4>(MOperands.begin(),
Dan Gohman02ff9392009-06-14 22:47:23 +00001656 MOperands.end());
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001657 Operands[i] = Div;
1658 return getMulExpr(Operands);
1659 }
1660 }
Dan Gohman14374d32009-05-08 23:11:16 +00001661 }
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001662 // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded.
Dan Gohman14374d32009-05-08 23:11:16 +00001663 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) {
Dan Gohman161ea032009-07-07 17:06:11 +00001664 SmallVector<const SCEV *, 4> Operands;
Dan Gohman14374d32009-05-08 23:11:16 +00001665 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i)
1666 Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy));
1667 if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) {
1668 Operands.clear();
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001669 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
Dan Gohman161ea032009-07-07 17:06:11 +00001670 const SCEV *Op = getUDivExpr(A->getOperand(i), RHS);
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001671 if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i))
1672 break;
1673 Operands.push_back(Op);
1674 }
1675 if (Operands.size() == A->getNumOperands())
1676 return getAddExpr(Operands);
1677 }
Dan Gohman14374d32009-05-08 23:11:16 +00001678 }
Dan Gohmanaf0a1512009-05-08 20:18:49 +00001679
1680 // Fold if both operands are constant.
Dan Gohmanc76b5452009-05-04 22:02:23 +00001681 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001682 Constant *LHSCV = LHSC->getValue();
1683 Constant *RHSCV = RHSC->getValue();
Owen Anderson175b6542009-07-22 00:24:57 +00001684 return getConstant(cast<ConstantInt>(getContext().getConstantExprUDiv(LHSCV,
Dan Gohman55788cf2009-06-24 00:38:39 +00001685 RHSCV)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001686 }
1687 }
1688
Dan Gohmanc6475cb2009-06-27 21:21:31 +00001689 FoldingSetNodeID ID;
1690 ID.AddInteger(scUDivExpr);
1691 ID.AddPointer(LHS);
1692 ID.AddPointer(RHS);
1693 void *IP = 0;
1694 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1695 SCEV *S = SCEVAllocator.Allocate<SCEVUDivExpr>();
Dan Gohmand43a8282009-07-13 20:50:19 +00001696 new (S) SCEVUDivExpr(ID, LHS, RHS);
Dan Gohmanc6475cb2009-06-27 21:21:31 +00001697 UniqueSCEVs.InsertNode(S, IP);
1698 return S;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001699}
1700
1701
Dan Gohmanc8a29272009-05-24 23:45:28 +00001702/// getAddRecExpr - Get an add recurrence expression for the specified loop.
1703/// Simplify the expression as much as possible.
Dan Gohman161ea032009-07-07 17:06:11 +00001704const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start,
Dan Gohman1c4054f2009-07-24 01:03:59 +00001705 const SCEV *Step, const Loop *L) {
Dan Gohman161ea032009-07-07 17:06:11 +00001706 SmallVector<const SCEV *, 4> Operands;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001707 Operands.push_back(Start);
Dan Gohmanc76b5452009-05-04 22:02:23 +00001708 if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001709 if (StepChrec->getLoop() == L) {
1710 Operands.insert(Operands.end(), StepChrec->op_begin(),
1711 StepChrec->op_end());
Dan Gohman89f85052007-10-22 18:31:58 +00001712 return getAddRecExpr(Operands, L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001713 }
1714
1715 Operands.push_back(Step);
Dan Gohman89f85052007-10-22 18:31:58 +00001716 return getAddRecExpr(Operands, L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001717}
1718
Dan Gohmanc8a29272009-05-24 23:45:28 +00001719/// getAddRecExpr - Get an add recurrence expression for the specified loop.
1720/// Simplify the expression as much as possible.
Dan Gohman9bc642f2009-06-24 04:48:43 +00001721const SCEV *
Dan Gohman161ea032009-07-07 17:06:11 +00001722ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
Dan Gohman9bc642f2009-06-24 04:48:43 +00001723 const Loop *L) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001724 if (Operands.size() == 1) return Operands[0];
Dan Gohmana77b3d42009-05-18 15:44:58 +00001725#ifndef NDEBUG
1726 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
1727 assert(getEffectiveSCEVType(Operands[i]->getType()) ==
1728 getEffectiveSCEVType(Operands[0]->getType()) &&
1729 "SCEVAddRecExpr operand types don't match!");
1730#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001731
Dan Gohman7b560c42008-06-18 16:23:07 +00001732 if (Operands.back()->isZero()) {
1733 Operands.pop_back();
Dan Gohmanabe991f2008-09-14 17:21:12 +00001734 return getAddRecExpr(Operands, L); // {X,+,0} --> X
Dan Gohman7b560c42008-06-18 16:23:07 +00001735 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001736
Dan Gohman42936882008-08-08 18:33:12 +00001737 // Canonicalize nested AddRecs in by nesting them in order of loop depth.
Dan Gohmanc76b5452009-05-04 22:02:23 +00001738 if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
Dan Gohman42936882008-08-08 18:33:12 +00001739 const Loop* NestedLoop = NestedAR->getLoop();
1740 if (L->getLoopDepth() < NestedLoop->getLoopDepth()) {
Dan Gohman161ea032009-07-07 17:06:11 +00001741 SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(),
Dan Gohman02ff9392009-06-14 22:47:23 +00001742 NestedAR->op_end());
Dan Gohman42936882008-08-08 18:33:12 +00001743 Operands[0] = NestedAR->getStart();
Dan Gohman08c4c072009-06-26 22:36:20 +00001744 // AddRecs require their operands be loop-invariant with respect to their
1745 // loops. Don't perform this transformation if it would break this
1746 // requirement.
1747 bool AllInvariant = true;
1748 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
1749 if (!Operands[i]->isLoopInvariant(L)) {
1750 AllInvariant = false;
1751 break;
1752 }
1753 if (AllInvariant) {
1754 NestedOperands[0] = getAddRecExpr(Operands, L);
1755 AllInvariant = true;
1756 for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i)
1757 if (!NestedOperands[i]->isLoopInvariant(NestedLoop)) {
1758 AllInvariant = false;
1759 break;
1760 }
1761 if (AllInvariant)
1762 // Ok, both add recurrences are valid after the transformation.
1763 return getAddRecExpr(NestedOperands, NestedLoop);
1764 }
1765 // Reset Operands to its original state.
1766 Operands[0] = NestedAR;
Dan Gohman42936882008-08-08 18:33:12 +00001767 }
1768 }
1769
Dan Gohmanc6475cb2009-06-27 21:21:31 +00001770 FoldingSetNodeID ID;
1771 ID.AddInteger(scAddRecExpr);
1772 ID.AddInteger(Operands.size());
1773 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
1774 ID.AddPointer(Operands[i]);
1775 ID.AddPointer(L);
1776 void *IP = 0;
1777 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1778 SCEV *S = SCEVAllocator.Allocate<SCEVAddRecExpr>();
Dan Gohmand43a8282009-07-13 20:50:19 +00001779 new (S) SCEVAddRecExpr(ID, Operands, L);
Dan Gohmanc6475cb2009-06-27 21:21:31 +00001780 UniqueSCEVs.InsertNode(S, IP);
1781 return S;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001782}
1783
Dan Gohman8c4f20b2009-06-24 14:49:00 +00001784const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS,
1785 const SCEV *RHS) {
Dan Gohman161ea032009-07-07 17:06:11 +00001786 SmallVector<const SCEV *, 2> Ops;
Nick Lewycky711640a2007-11-25 22:41:31 +00001787 Ops.push_back(LHS);
1788 Ops.push_back(RHS);
1789 return getSMaxExpr(Ops);
1790}
1791
Dan Gohman161ea032009-07-07 17:06:11 +00001792const SCEV *
1793ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
Nick Lewycky711640a2007-11-25 22:41:31 +00001794 assert(!Ops.empty() && "Cannot get empty smax!");
1795 if (Ops.size() == 1) return Ops[0];
Dan Gohmana77b3d42009-05-18 15:44:58 +00001796#ifndef NDEBUG
1797 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1798 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1799 getEffectiveSCEVType(Ops[0]->getType()) &&
1800 "SCEVSMaxExpr operand types don't match!");
1801#endif
Nick Lewycky711640a2007-11-25 22:41:31 +00001802
1803 // Sort by complexity, this groups all similar expression types together.
Dan Gohman5d486452009-05-07 14:39:04 +00001804 GroupByComplexity(Ops, LI);
Nick Lewycky711640a2007-11-25 22:41:31 +00001805
1806 // If there are any constants, fold them together.
1807 unsigned Idx = 0;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001808 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Nick Lewycky711640a2007-11-25 22:41:31 +00001809 ++Idx;
1810 assert(Idx < Ops.size());
Dan Gohmanc76b5452009-05-04 22:02:23 +00001811 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Nick Lewycky711640a2007-11-25 22:41:31 +00001812 // We found two constants, fold them together!
Owen Andersoneacb44d2009-07-24 23:12:02 +00001813 ConstantInt *Fold = ConstantInt::get(getContext(),
Nick Lewycky711640a2007-11-25 22:41:31 +00001814 APIntOps::smax(LHSC->getValue()->getValue(),
1815 RHSC->getValue()->getValue()));
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001816 Ops[0] = getConstant(Fold);
1817 Ops.erase(Ops.begin()+1); // Erase the folded element
1818 if (Ops.size() == 1) return Ops[0];
1819 LHSC = cast<SCEVConstant>(Ops[0]);
Nick Lewycky711640a2007-11-25 22:41:31 +00001820 }
1821
Dan Gohmand156c092009-06-24 14:46:22 +00001822 // If we are left with a constant minimum-int, strip it off.
Nick Lewycky711640a2007-11-25 22:41:31 +00001823 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) {
1824 Ops.erase(Ops.begin());
1825 --Idx;
Dan Gohmand156c092009-06-24 14:46:22 +00001826 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) {
1827 // If we have an smax with a constant maximum-int, it will always be
1828 // maximum-int.
1829 return Ops[0];
Nick Lewycky711640a2007-11-25 22:41:31 +00001830 }
1831 }
1832
1833 if (Ops.size() == 1) return Ops[0];
1834
1835 // Find the first SMax
1836 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr)
1837 ++Idx;
1838
1839 // Check to see if one of the operands is an SMax. If so, expand its operands
1840 // onto our operand list, and recurse to simplify.
1841 if (Idx < Ops.size()) {
1842 bool DeletedSMax = false;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001843 while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) {
Nick Lewycky711640a2007-11-25 22:41:31 +00001844 Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end());
1845 Ops.erase(Ops.begin()+Idx);
1846 DeletedSMax = true;
1847 }
1848
1849 if (DeletedSMax)
1850 return getSMaxExpr(Ops);
1851 }
1852
1853 // Okay, check to see if the same value occurs in the operand list twice. If
1854 // so, delete one. Since we sorted the list, these values are required to
1855 // be adjacent.
1856 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
1857 if (Ops[i] == Ops[i+1]) { // X smax Y smax Y --> X smax Y
1858 Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
1859 --i; --e;
1860 }
1861
1862 if (Ops.size() == 1) return Ops[0];
1863
1864 assert(!Ops.empty() && "Reduced smax down to nothing!");
1865
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001866 // Okay, it looks like we really DO need an smax expr. Check to see if we
Nick Lewycky711640a2007-11-25 22:41:31 +00001867 // already have one, otherwise create a new one.
Dan Gohmanc6475cb2009-06-27 21:21:31 +00001868 FoldingSetNodeID ID;
1869 ID.AddInteger(scSMaxExpr);
1870 ID.AddInteger(Ops.size());
1871 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1872 ID.AddPointer(Ops[i]);
1873 void *IP = 0;
1874 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1875 SCEV *S = SCEVAllocator.Allocate<SCEVSMaxExpr>();
Dan Gohmand43a8282009-07-13 20:50:19 +00001876 new (S) SCEVSMaxExpr(ID, Ops);
Dan Gohmanc6475cb2009-06-27 21:21:31 +00001877 UniqueSCEVs.InsertNode(S, IP);
1878 return S;
Nick Lewycky711640a2007-11-25 22:41:31 +00001879}
1880
Dan Gohman8c4f20b2009-06-24 14:49:00 +00001881const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS,
1882 const SCEV *RHS) {
Dan Gohman161ea032009-07-07 17:06:11 +00001883 SmallVector<const SCEV *, 2> Ops;
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001884 Ops.push_back(LHS);
1885 Ops.push_back(RHS);
1886 return getUMaxExpr(Ops);
1887}
1888
Dan Gohman161ea032009-07-07 17:06:11 +00001889const SCEV *
1890ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001891 assert(!Ops.empty() && "Cannot get empty umax!");
1892 if (Ops.size() == 1) return Ops[0];
Dan Gohmana77b3d42009-05-18 15:44:58 +00001893#ifndef NDEBUG
1894 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1895 assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1896 getEffectiveSCEVType(Ops[0]->getType()) &&
1897 "SCEVUMaxExpr operand types don't match!");
1898#endif
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001899
1900 // Sort by complexity, this groups all similar expression types together.
Dan Gohman5d486452009-05-07 14:39:04 +00001901 GroupByComplexity(Ops, LI);
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001902
1903 // If there are any constants, fold them together.
1904 unsigned Idx = 0;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001905 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001906 ++Idx;
1907 assert(Idx < Ops.size());
Dan Gohmanc76b5452009-05-04 22:02:23 +00001908 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001909 // We found two constants, fold them together!
Owen Andersoneacb44d2009-07-24 23:12:02 +00001910 ConstantInt *Fold = ConstantInt::get(getContext(),
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001911 APIntOps::umax(LHSC->getValue()->getValue(),
1912 RHSC->getValue()->getValue()));
1913 Ops[0] = getConstant(Fold);
1914 Ops.erase(Ops.begin()+1); // Erase the folded element
1915 if (Ops.size() == 1) return Ops[0];
1916 LHSC = cast<SCEVConstant>(Ops[0]);
1917 }
1918
Dan Gohmand156c092009-06-24 14:46:22 +00001919 // If we are left with a constant minimum-int, strip it off.
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001920 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) {
1921 Ops.erase(Ops.begin());
1922 --Idx;
Dan Gohmand156c092009-06-24 14:46:22 +00001923 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) {
1924 // If we have an umax with a constant maximum-int, it will always be
1925 // maximum-int.
1926 return Ops[0];
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001927 }
1928 }
1929
1930 if (Ops.size() == 1) return Ops[0];
1931
1932 // Find the first UMax
1933 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr)
1934 ++Idx;
1935
1936 // Check to see if one of the operands is a UMax. If so, expand its operands
1937 // onto our operand list, and recurse to simplify.
1938 if (Idx < Ops.size()) {
1939 bool DeletedUMax = false;
Dan Gohmanc76b5452009-05-04 22:02:23 +00001940 while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) {
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001941 Ops.insert(Ops.end(), UMax->op_begin(), UMax->op_end());
1942 Ops.erase(Ops.begin()+Idx);
1943 DeletedUMax = true;
1944 }
1945
1946 if (DeletedUMax)
1947 return getUMaxExpr(Ops);
1948 }
1949
1950 // Okay, check to see if the same value occurs in the operand list twice. If
1951 // so, delete one. Since we sorted the list, these values are required to
1952 // be adjacent.
1953 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
1954 if (Ops[i] == Ops[i+1]) { // X umax Y umax Y --> X umax Y
1955 Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
1956 --i; --e;
1957 }
1958
1959 if (Ops.size() == 1) return Ops[0];
1960
1961 assert(!Ops.empty() && "Reduced umax down to nothing!");
1962
1963 // Okay, it looks like we really DO need a umax expr. Check to see if we
1964 // already have one, otherwise create a new one.
Dan Gohmanc6475cb2009-06-27 21:21:31 +00001965 FoldingSetNodeID ID;
1966 ID.AddInteger(scUMaxExpr);
1967 ID.AddInteger(Ops.size());
1968 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1969 ID.AddPointer(Ops[i]);
1970 void *IP = 0;
1971 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1972 SCEV *S = SCEVAllocator.Allocate<SCEVUMaxExpr>();
Dan Gohmand43a8282009-07-13 20:50:19 +00001973 new (S) SCEVUMaxExpr(ID, Ops);
Dan Gohmanc6475cb2009-06-27 21:21:31 +00001974 UniqueSCEVs.InsertNode(S, IP);
1975 return S;
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00001976}
1977
Dan Gohman8c4f20b2009-06-24 14:49:00 +00001978const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS,
1979 const SCEV *RHS) {
Dan Gohmand01fff82009-06-22 03:18:45 +00001980 // ~smax(~x, ~y) == smin(x, y).
1981 return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
1982}
1983
Dan Gohman8c4f20b2009-06-24 14:49:00 +00001984const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS,
1985 const SCEV *RHS) {
Dan Gohmand01fff82009-06-22 03:18:45 +00001986 // ~umax(~x, ~y) == umin(x, y)
1987 return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
1988}
1989
Dan Gohman161ea032009-07-07 17:06:11 +00001990const SCEV *ScalarEvolution::getUnknown(Value *V) {
Dan Gohman984c78a2009-06-24 00:54:57 +00001991 // Don't attempt to do anything other than create a SCEVUnknown object
1992 // here. createSCEV only calls getUnknown after checking for all other
1993 // interesting possibilities, and any other code that calls getUnknown
1994 // is doing so in order to hide a value from SCEV canonicalization.
1995
Dan Gohmanc6475cb2009-06-27 21:21:31 +00001996 FoldingSetNodeID ID;
1997 ID.AddInteger(scUnknown);
1998 ID.AddPointer(V);
1999 void *IP = 0;
2000 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2001 SCEV *S = SCEVAllocator.Allocate<SCEVUnknown>();
Dan Gohmand43a8282009-07-13 20:50:19 +00002002 new (S) SCEVUnknown(ID, V);
Dan Gohmanc6475cb2009-06-27 21:21:31 +00002003 UniqueSCEVs.InsertNode(S, IP);
2004 return S;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002005}
2006
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002007//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002008// Basic SCEV Analysis and PHI Idiom Recognition Code
2009//
2010
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002011/// isSCEVable - Test if values of the given type are analyzable within
2012/// the SCEV framework. This primarily includes integer types, and it
2013/// can optionally include pointer types if the ScalarEvolution class
2014/// has access to target-specific information.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002015bool ScalarEvolution::isSCEVable(const Type *Ty) const {
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002016 // Integers are always SCEVable.
2017 if (Ty->isInteger())
2018 return true;
2019
2020 // Pointers are SCEVable if TargetData information is available
2021 // to provide pointer size information.
2022 if (isa<PointerType>(Ty))
2023 return TD != NULL;
2024
2025 // Otherwise it's not SCEVable.
2026 return false;
2027}
2028
2029/// getTypeSizeInBits - Return the size in bits of the specified type,
2030/// for which isSCEVable must return true.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002031uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const {
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002032 assert(isSCEVable(Ty) && "Type is not SCEVable!");
2033
2034 // If we have a TargetData, use it!
2035 if (TD)
2036 return TD->getTypeSizeInBits(Ty);
2037
2038 // Otherwise, we support only integer types.
2039 assert(Ty->isInteger() && "isSCEVable permitted a non-SCEVable type!");
2040 return Ty->getPrimitiveSizeInBits();
2041}
2042
2043/// getEffectiveSCEVType - Return a type with the same bitwidth as
2044/// the given type and which represents how SCEV will treat the given
2045/// type, for which isSCEVable must return true. For pointer types,
2046/// this is the pointer-sized integer type.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002047const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const {
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002048 assert(isSCEVable(Ty) && "Type is not SCEVable!");
2049
2050 if (Ty->isInteger())
2051 return Ty;
2052
2053 assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!");
2054 return TD->getIntPtrType();
Dan Gohman01c2ee72009-04-16 03:18:22 +00002055}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002056
Dan Gohman161ea032009-07-07 17:06:11 +00002057const SCEV *ScalarEvolution::getCouldNotCompute() {
Dan Gohmanc6475cb2009-06-27 21:21:31 +00002058 return &CouldNotCompute;
Dan Gohman0ad08b02009-04-18 17:58:19 +00002059}
2060
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002061/// getSCEV - Return an existing SCEV if it exists, otherwise analyze the
2062/// expression and create a new one.
Dan Gohman161ea032009-07-07 17:06:11 +00002063const SCEV *ScalarEvolution::getSCEV(Value *V) {
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002064 assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002065
Dan Gohman161ea032009-07-07 17:06:11 +00002066 std::map<SCEVCallbackVH, const SCEV *>::iterator I = Scalars.find(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002067 if (I != Scalars.end()) return I->second;
Dan Gohman161ea032009-07-07 17:06:11 +00002068 const SCEV *S = createSCEV(V);
Dan Gohmanbff6b582009-05-04 22:30:44 +00002069 Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002070 return S;
2071}
2072
Dan Gohman984c78a2009-06-24 00:54:57 +00002073/// getIntegerSCEV - Given a SCEVable type, create a constant for the
Dan Gohman01c2ee72009-04-16 03:18:22 +00002074/// specified signed integer value and return a SCEV for the constant.
Dan Gohman161ea032009-07-07 17:06:11 +00002075const SCEV *ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) {
Dan Gohman984c78a2009-06-24 00:54:57 +00002076 const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty));
Owen Andersoneacb44d2009-07-24 23:12:02 +00002077 return getConstant(ConstantInt::get(ITy, Val));
Dan Gohman01c2ee72009-04-16 03:18:22 +00002078}
2079
2080/// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
2081///
Dan Gohman161ea032009-07-07 17:06:11 +00002082const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) {
Dan Gohmanc76b5452009-05-04 22:02:23 +00002083 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
Owen Anderson15b39322009-07-13 04:09:18 +00002084 return getConstant(
Owen Anderson175b6542009-07-22 00:24:57 +00002085 cast<ConstantInt>(getContext().getConstantExprNeg(VC->getValue())));
Dan Gohman01c2ee72009-04-16 03:18:22 +00002086
2087 const Type *Ty = V->getType();
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002088 Ty = getEffectiveSCEVType(Ty);
Owen Anderson035d41d2009-07-13 20:58:05 +00002089 return getMulExpr(V,
Owen Anderson175b6542009-07-22 00:24:57 +00002090 getConstant(cast<ConstantInt>(getContext().getAllOnesValue(Ty))));
Dan Gohman01c2ee72009-04-16 03:18:22 +00002091}
2092
2093/// getNotSCEV - Return a SCEV corresponding to ~V = -1-V
Dan Gohman161ea032009-07-07 17:06:11 +00002094const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) {
Dan Gohmanc76b5452009-05-04 22:02:23 +00002095 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
Owen Anderson035d41d2009-07-13 20:58:05 +00002096 return getConstant(
Owen Anderson175b6542009-07-22 00:24:57 +00002097 cast<ConstantInt>(getContext().getConstantExprNot(VC->getValue())));
Dan Gohman01c2ee72009-04-16 03:18:22 +00002098
2099 const Type *Ty = V->getType();
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002100 Ty = getEffectiveSCEVType(Ty);
Owen Anderson035d41d2009-07-13 20:58:05 +00002101 const SCEV *AllOnes =
Owen Anderson175b6542009-07-22 00:24:57 +00002102 getConstant(cast<ConstantInt>(getContext().getAllOnesValue(Ty)));
Dan Gohman01c2ee72009-04-16 03:18:22 +00002103 return getMinusSCEV(AllOnes, V);
2104}
2105
2106/// getMinusSCEV - Return a SCEV corresponding to LHS - RHS.
2107///
Dan Gohman8c4f20b2009-06-24 14:49:00 +00002108const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS,
2109 const SCEV *RHS) {
Dan Gohman01c2ee72009-04-16 03:18:22 +00002110 // X - Y --> X + -Y
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002111 return getAddExpr(LHS, getNegativeSCEV(RHS));
Dan Gohman01c2ee72009-04-16 03:18:22 +00002112}
2113
2114/// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the
2115/// input value to the specified type. If the type must be extended, it is zero
2116/// extended.
Dan Gohman161ea032009-07-07 17:06:11 +00002117const SCEV *
2118ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V,
Nick Lewycky37d04642009-04-23 05:15:08 +00002119 const Type *Ty) {
Dan Gohman01c2ee72009-04-16 03:18:22 +00002120 const Type *SrcTy = V->getType();
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002121 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2122 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
Dan Gohman01c2ee72009-04-16 03:18:22 +00002123 "Cannot truncate or zero extend with non-integer arguments!");
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002124 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
Dan Gohman01c2ee72009-04-16 03:18:22 +00002125 return V; // No conversion
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002126 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002127 return getTruncateExpr(V, Ty);
2128 return getZeroExtendExpr(V, Ty);
Dan Gohman01c2ee72009-04-16 03:18:22 +00002129}
2130
2131/// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the
2132/// input value to the specified type. If the type must be extended, it is sign
2133/// extended.
Dan Gohman161ea032009-07-07 17:06:11 +00002134const SCEV *
2135ScalarEvolution::getTruncateOrSignExtend(const SCEV *V,
Nick Lewycky37d04642009-04-23 05:15:08 +00002136 const Type *Ty) {
Dan Gohman01c2ee72009-04-16 03:18:22 +00002137 const Type *SrcTy = V->getType();
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002138 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2139 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
Dan Gohman01c2ee72009-04-16 03:18:22 +00002140 "Cannot truncate or zero extend with non-integer arguments!");
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002141 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
Dan Gohman01c2ee72009-04-16 03:18:22 +00002142 return V; // No conversion
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002143 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002144 return getTruncateExpr(V, Ty);
2145 return getSignExtendExpr(V, Ty);
Dan Gohman01c2ee72009-04-16 03:18:22 +00002146}
2147
Dan Gohmanac959332009-05-13 03:46:30 +00002148/// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the
2149/// input value to the specified type. If the type must be extended, it is zero
2150/// extended. The conversion must not be narrowing.
Dan Gohman161ea032009-07-07 17:06:11 +00002151const SCEV *
2152ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, const Type *Ty) {
Dan Gohmanac959332009-05-13 03:46:30 +00002153 const Type *SrcTy = V->getType();
2154 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2155 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
2156 "Cannot noop or zero extend with non-integer arguments!");
2157 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2158 "getNoopOrZeroExtend cannot truncate!");
2159 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2160 return V; // No conversion
2161 return getZeroExtendExpr(V, Ty);
2162}
2163
2164/// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the
2165/// input value to the specified type. If the type must be extended, it is sign
2166/// extended. The conversion must not be narrowing.
Dan Gohman161ea032009-07-07 17:06:11 +00002167const SCEV *
2168ScalarEvolution::getNoopOrSignExtend(const SCEV *V, const Type *Ty) {
Dan Gohmanac959332009-05-13 03:46:30 +00002169 const Type *SrcTy = V->getType();
2170 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2171 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
2172 "Cannot noop or sign extend with non-integer arguments!");
2173 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2174 "getNoopOrSignExtend cannot truncate!");
2175 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2176 return V; // No conversion
2177 return getSignExtendExpr(V, Ty);
2178}
2179
Dan Gohmane1ca7e82009-06-13 15:56:47 +00002180/// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of
2181/// the input value to the specified type. If the type must be extended,
2182/// it is extended with unspecified bits. The conversion must not be
2183/// narrowing.
Dan Gohman161ea032009-07-07 17:06:11 +00002184const SCEV *
2185ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, const Type *Ty) {
Dan Gohmane1ca7e82009-06-13 15:56:47 +00002186 const Type *SrcTy = V->getType();
2187 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2188 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
2189 "Cannot noop or any extend with non-integer arguments!");
2190 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2191 "getNoopOrAnyExtend cannot truncate!");
2192 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2193 return V; // No conversion
2194 return getAnyExtendExpr(V, Ty);
2195}
2196
Dan Gohmanac959332009-05-13 03:46:30 +00002197/// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the
2198/// input value to the specified type. The conversion must not be widening.
Dan Gohman161ea032009-07-07 17:06:11 +00002199const SCEV *
2200ScalarEvolution::getTruncateOrNoop(const SCEV *V, const Type *Ty) {
Dan Gohmanac959332009-05-13 03:46:30 +00002201 const Type *SrcTy = V->getType();
2202 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) &&
2203 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) &&
2204 "Cannot truncate or noop with non-integer arguments!");
2205 assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) &&
2206 "getTruncateOrNoop cannot extend!");
2207 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2208 return V; // No conversion
2209 return getTruncateExpr(V, Ty);
2210}
2211
Dan Gohman8e8b5232009-06-22 00:31:57 +00002212/// getUMaxFromMismatchedTypes - Promote the operands to the wider of
2213/// the types using zero-extension, and then perform a umax operation
2214/// with them.
Dan Gohman8c4f20b2009-06-24 14:49:00 +00002215const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS,
2216 const SCEV *RHS) {
Dan Gohman161ea032009-07-07 17:06:11 +00002217 const SCEV *PromotedLHS = LHS;
2218 const SCEV *PromotedRHS = RHS;
Dan Gohman8e8b5232009-06-22 00:31:57 +00002219
2220 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
2221 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
2222 else
2223 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
2224
2225 return getUMaxExpr(PromotedLHS, PromotedRHS);
2226}
2227
Dan Gohman9e62bb02009-06-22 15:03:27 +00002228/// getUMinFromMismatchedTypes - Promote the operands to the wider of
2229/// the types using zero-extension, and then perform a umin operation
2230/// with them.
Dan Gohman8c4f20b2009-06-24 14:49:00 +00002231const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS,
2232 const SCEV *RHS) {
Dan Gohman161ea032009-07-07 17:06:11 +00002233 const SCEV *PromotedLHS = LHS;
2234 const SCEV *PromotedRHS = RHS;
Dan Gohman9e62bb02009-06-22 15:03:27 +00002235
2236 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
2237 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
2238 else
2239 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
2240
2241 return getUMinExpr(PromotedLHS, PromotedRHS);
2242}
2243
Dan Gohman2aa3f042009-07-25 01:13:03 +00002244/// PushDefUseChildren - Push users of the given Instruction
2245/// onto the given Worklist.
2246static void
2247PushDefUseChildren(Instruction *I,
2248 SmallVectorImpl<Instruction *> &Worklist) {
2249 // Push the def-use children onto the Worklist stack.
2250 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
2251 UI != UE; ++UI)
2252 Worklist.push_back(cast<Instruction>(UI));
2253}
2254
2255/// ForgetSymbolicValue - This looks up computed SCEV values for all
2256/// instructions that depend on the given instruction and removes them from
2257/// the Scalars map if they reference SymName. This is used during PHI
2258/// resolution.
Dan Gohman9bc642f2009-06-24 04:48:43 +00002259void
Dan Gohman2aa3f042009-07-25 01:13:03 +00002260ScalarEvolution::ForgetSymbolicName(Instruction *I, const SCEV *SymName) {
2261 SmallVector<Instruction *, 16> Worklist;
2262 PushDefUseChildren(I, Worklist);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002263
Dan Gohman2aa3f042009-07-25 01:13:03 +00002264 SmallPtrSet<Instruction *, 8> Visited;
2265 Visited.insert(I);
2266 while (!Worklist.empty()) {
2267 Instruction *I = Worklist.pop_back_val();
2268 if (!Visited.insert(I)) continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002269
Dan Gohman2aa3f042009-07-25 01:13:03 +00002270 std::map<SCEVCallbackVH, const SCEV*>::iterator It =
2271 Scalars.find(static_cast<Value *>(I));
2272 if (It != Scalars.end()) {
2273 // Short-circuit the def-use traversal if the symbolic name
2274 // ceases to appear in expressions.
2275 if (!It->second->hasOperand(SymName))
2276 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002277
Dan Gohman2aa3f042009-07-25 01:13:03 +00002278 // SCEVUnknown for a PHI either means that it has an unrecognized
2279 // structure, or it's a PHI that's in the progress of being computed
2280 // by createNodeForPHI. In the former case, additional loop trip
2281 // count information isn't going to change anything. In the later
2282 // case, createNodeForPHI will perform the necessary updates on its
2283 // own when it gets to that point.
2284 if (!isa<PHINode>(I) || !isa<SCEVUnknown>(It->second))
2285 Scalars.erase(It);
2286 ValuesAtScopes.erase(I);
2287 }
2288
2289 PushDefUseChildren(I, Worklist);
2290 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002291}
2292
2293/// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in
2294/// a loop header, making it a potential recurrence, or it doesn't.
2295///
Dan Gohman161ea032009-07-07 17:06:11 +00002296const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002297 if (PN->getNumIncomingValues() == 2) // The loops have been canonicalized.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002298 if (const Loop *L = LI->getLoopFor(PN->getParent()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002299 if (L->getHeader() == PN->getParent()) {
2300 // If it lives in the loop header, it has two incoming values, one
2301 // from outside the loop, and one from inside.
2302 unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0));
2303 unsigned BackEdge = IncomingEdge^1;
2304
2305 // While we are analyzing this PHI node, handle its value symbolically.
Dan Gohman161ea032009-07-07 17:06:11 +00002306 const SCEV *SymbolicName = getUnknown(PN);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002307 assert(Scalars.find(PN) == Scalars.end() &&
2308 "PHI node already processed?");
Dan Gohmanbff6b582009-05-04 22:30:44 +00002309 Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002310
2311 // Using this symbolic name for the PHI, analyze the value coming around
2312 // the back-edge.
Dan Gohman2aa3f042009-07-25 01:13:03 +00002313 Value *BEValueV = PN->getIncomingValue(BackEdge);
2314 const SCEV *BEValue = getSCEV(BEValueV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002315
2316 // NOTE: If BEValue is loop invariant, we know that the PHI node just
2317 // has a special value for the first iteration of the loop.
2318
2319 // If the value coming around the backedge is an add with the symbolic
2320 // value we just inserted, then we found a simple induction variable!
Dan Gohmanc76b5452009-05-04 22:02:23 +00002321 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002322 // If there is a single occurrence of the symbolic value, replace it
2323 // with a recurrence.
2324 unsigned FoundIndex = Add->getNumOperands();
2325 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
2326 if (Add->getOperand(i) == SymbolicName)
2327 if (FoundIndex == e) {
2328 FoundIndex = i;
2329 break;
2330 }
2331
2332 if (FoundIndex != Add->getNumOperands()) {
2333 // Create an add with everything but the specified operand.
Dan Gohman161ea032009-07-07 17:06:11 +00002334 SmallVector<const SCEV *, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002335 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
2336 if (i != FoundIndex)
2337 Ops.push_back(Add->getOperand(i));
Dan Gohman161ea032009-07-07 17:06:11 +00002338 const SCEV *Accum = getAddExpr(Ops);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002339
2340 // This is not a valid addrec if the step amount is varying each
2341 // loop iteration, but is not itself an addrec in this loop.
2342 if (Accum->isLoopInvariant(L) ||
2343 (isa<SCEVAddRecExpr>(Accum) &&
2344 cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
Dan Gohman9bc642f2009-06-24 04:48:43 +00002345 const SCEV *StartVal =
2346 getSCEV(PN->getIncomingValue(IncomingEdge));
2347 const SCEV *PHISCEV =
2348 getAddRecExpr(StartVal, Accum, L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002349
2350 // Okay, for the entire analysis of this edge we assumed the PHI
Dan Gohman2aa3f042009-07-25 01:13:03 +00002351 // to be symbolic. We now need to go back and purge all of the
2352 // entries for the scalars that use the symbolic expression.
2353 ForgetSymbolicName(PN, SymbolicName);
2354 Scalars[SCEVCallbackVH(PN, this)] = PHISCEV;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002355 return PHISCEV;
2356 }
2357 }
Dan Gohmanc76b5452009-05-04 22:02:23 +00002358 } else if (const SCEVAddRecExpr *AddRec =
2359 dyn_cast<SCEVAddRecExpr>(BEValue)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002360 // Otherwise, this could be a loop like this:
2361 // i = 0; for (j = 1; ..; ++j) { .... i = j; }
2362 // In this case, j = {1,+,1} and BEValue is j.
2363 // Because the other in-value of i (0) fits the evolution of BEValue
2364 // i really is an addrec evolution.
2365 if (AddRec->getLoop() == L && AddRec->isAffine()) {
Dan Gohman161ea032009-07-07 17:06:11 +00002366 const SCEV *StartVal = getSCEV(PN->getIncomingValue(IncomingEdge));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002367
2368 // If StartVal = j.start - j.stride, we can use StartVal as the
2369 // initial step of the addrec evolution.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002370 if (StartVal == getMinusSCEV(AddRec->getOperand(0),
Dan Gohman89f85052007-10-22 18:31:58 +00002371 AddRec->getOperand(1))) {
Dan Gohman161ea032009-07-07 17:06:11 +00002372 const SCEV *PHISCEV =
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002373 getAddRecExpr(StartVal, AddRec->getOperand(1), L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002374
2375 // Okay, for the entire analysis of this edge we assumed the PHI
Dan Gohman2aa3f042009-07-25 01:13:03 +00002376 // to be symbolic. We now need to go back and purge all of the
2377 // entries for the scalars that use the symbolic expression.
2378 ForgetSymbolicName(PN, SymbolicName);
2379 Scalars[SCEVCallbackVH(PN, this)] = PHISCEV;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002380 return PHISCEV;
2381 }
2382 }
2383 }
2384
2385 return SymbolicName;
2386 }
2387
Dan Gohman32f35cc2009-07-14 14:06:25 +00002388 // It's tempting to recognize PHIs with a unique incoming value, however
2389 // this leads passes like indvars to break LCSSA form. Fortunately, such
2390 // PHIs are rare, as instcombine zaps them.
2391
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002392 // If it's not a loop phi, we can't handle it yet.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002393 return getUnknown(PN);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002394}
2395
Dan Gohman509cf4d2009-05-08 20:26:55 +00002396/// createNodeForGEP - Expand GEP instructions into add and multiply
2397/// operations. This allows them to be analyzed by regular SCEV code.
2398///
Dan Gohman9545fb02009-07-17 20:47:02 +00002399const SCEV *ScalarEvolution::createNodeForGEP(Operator *GEP) {
Dan Gohman509cf4d2009-05-08 20:26:55 +00002400
2401 const Type *IntPtrTy = TD->getIntPtrType();
Dan Gohmanc7034fa2009-05-08 20:36:47 +00002402 Value *Base = GEP->getOperand(0);
Dan Gohmand586a4f2009-05-09 00:14:52 +00002403 // Don't attempt to analyze GEPs over unsized objects.
2404 if (!cast<PointerType>(Base->getType())->getElementType()->isSized())
2405 return getUnknown(GEP);
Dan Gohman161ea032009-07-07 17:06:11 +00002406 const SCEV *TotalOffset = getIntegerSCEV(0, IntPtrTy);
Dan Gohmanc7034fa2009-05-08 20:36:47 +00002407 gep_type_iterator GTI = gep_type_begin(GEP);
2408 for (GetElementPtrInst::op_iterator I = next(GEP->op_begin()),
2409 E = GEP->op_end();
Dan Gohman509cf4d2009-05-08 20:26:55 +00002410 I != E; ++I) {
2411 Value *Index = *I;
2412 // Compute the (potentially symbolic) offset in bytes for this index.
2413 if (const StructType *STy = dyn_cast<StructType>(*GTI++)) {
2414 // For a struct, add the member offset.
2415 const StructLayout &SL = *TD->getStructLayout(STy);
2416 unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
2417 uint64_t Offset = SL.getElementOffset(FieldNo);
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002418 TotalOffset = getAddExpr(TotalOffset, getIntegerSCEV(Offset, IntPtrTy));
Dan Gohman509cf4d2009-05-08 20:26:55 +00002419 } else {
2420 // For an array, add the element offset, explicitly scaled.
Dan Gohman161ea032009-07-07 17:06:11 +00002421 const SCEV *LocalOffset = getSCEV(Index);
Dan Gohman509cf4d2009-05-08 20:26:55 +00002422 if (!isa<PointerType>(LocalOffset->getType()))
2423 // Getelementptr indicies are signed.
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002424 LocalOffset = getTruncateOrSignExtend(LocalOffset, IntPtrTy);
Dan Gohman509cf4d2009-05-08 20:26:55 +00002425 LocalOffset =
2426 getMulExpr(LocalOffset,
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002427 getIntegerSCEV(TD->getTypeAllocSize(*GTI), IntPtrTy));
Dan Gohman509cf4d2009-05-08 20:26:55 +00002428 TotalOffset = getAddExpr(TotalOffset, LocalOffset);
2429 }
2430 }
2431 return getAddExpr(getSCEV(Base), TotalOffset);
2432}
2433
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002434/// GetMinTrailingZeros - Determine the minimum number of zero bits that S is
2435/// guaranteed to end in (at every loop iteration). It is, at the same time,
2436/// the minimum number of times S is divisible by 2. For example, given {4,+,8}
2437/// it returns 2. If S is guaranteed to be 0, it returns the bitwidth of S.
Dan Gohman6e923a72009-06-19 23:29:04 +00002438uint32_t
Dan Gohman161ea032009-07-07 17:06:11 +00002439ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
Dan Gohmanc76b5452009-05-04 22:02:23 +00002440 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
Chris Lattner6ecce2a2007-11-23 22:36:49 +00002441 return C->getValue()->getValue().countTrailingZeros();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002442
Dan Gohmanc76b5452009-05-04 22:02:23 +00002443 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S))
Dan Gohman6e923a72009-06-19 23:29:04 +00002444 return std::min(GetMinTrailingZeros(T->getOperand()),
2445 (uint32_t)getTypeSizeInBits(T->getType()));
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002446
Dan Gohmanc76b5452009-05-04 22:02:23 +00002447 if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) {
Dan Gohman6e923a72009-06-19 23:29:04 +00002448 uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
2449 return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
2450 getTypeSizeInBits(E->getType()) : OpRes;
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002451 }
2452
Dan Gohmanc76b5452009-05-04 22:02:23 +00002453 if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) {
Dan Gohman6e923a72009-06-19 23:29:04 +00002454 uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
2455 return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
2456 getTypeSizeInBits(E->getType()) : OpRes;
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002457 }
2458
Dan Gohmanc76b5452009-05-04 22:02:23 +00002459 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002460 // The result is the min of all operands results.
Dan Gohman6e923a72009-06-19 23:29:04 +00002461 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002462 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman6e923a72009-06-19 23:29:04 +00002463 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002464 return MinOpRes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002465 }
2466
Dan Gohmanc76b5452009-05-04 22:02:23 +00002467 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002468 // The result is the sum of all operands results.
Dan Gohman6e923a72009-06-19 23:29:04 +00002469 uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0));
2470 uint32_t BitWidth = getTypeSizeInBits(M->getType());
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002471 for (unsigned i = 1, e = M->getNumOperands();
2472 SumOpRes != BitWidth && i != e; ++i)
Dan Gohman6e923a72009-06-19 23:29:04 +00002473 SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)),
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002474 BitWidth);
2475 return SumOpRes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002476 }
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002477
Dan Gohmanc76b5452009-05-04 22:02:23 +00002478 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002479 // The result is the min of all operands results.
Dan Gohman6e923a72009-06-19 23:29:04 +00002480 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002481 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman6e923a72009-06-19 23:29:04 +00002482 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002483 return MinOpRes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002484 }
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002485
Dan Gohmanc76b5452009-05-04 22:02:23 +00002486 if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) {
Nick Lewycky711640a2007-11-25 22:41:31 +00002487 // The result is the min of all operands results.
Dan Gohman6e923a72009-06-19 23:29:04 +00002488 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
Nick Lewycky711640a2007-11-25 22:41:31 +00002489 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman6e923a72009-06-19 23:29:04 +00002490 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
Nick Lewycky711640a2007-11-25 22:41:31 +00002491 return MinOpRes;
2492 }
2493
Dan Gohmanc76b5452009-05-04 22:02:23 +00002494 if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) {
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00002495 // The result is the min of all operands results.
Dan Gohman6e923a72009-06-19 23:29:04 +00002496 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00002497 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohman6e923a72009-06-19 23:29:04 +00002498 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00002499 return MinOpRes;
2500 }
2501
Dan Gohman6e923a72009-06-19 23:29:04 +00002502 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2503 // For a SCEVUnknown, ask ValueTracking.
2504 unsigned BitWidth = getTypeSizeInBits(U->getType());
2505 APInt Mask = APInt::getAllOnesValue(BitWidth);
2506 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
2507 ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones);
2508 return Zeros.countTrailingOnes();
2509 }
2510
2511 // SCEVUDivExpr
Nick Lewycky4cb604b2007-11-22 07:59:40 +00002512 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002513}
2514
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002515/// getUnsignedRange - Determine the unsigned range for a particular SCEV.
2516///
2517ConstantRange
2518ScalarEvolution::getUnsignedRange(const SCEV *S) {
Dan Gohman6e923a72009-06-19 23:29:04 +00002519
2520 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002521 return ConstantRange(C->getValue()->getValue());
Dan Gohman6e923a72009-06-19 23:29:04 +00002522
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002523 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
2524 ConstantRange X = getUnsignedRange(Add->getOperand(0));
2525 for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
2526 X = X.add(getUnsignedRange(Add->getOperand(i)));
2527 return X;
2528 }
2529
2530 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
2531 ConstantRange X = getUnsignedRange(Mul->getOperand(0));
2532 for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
2533 X = X.multiply(getUnsignedRange(Mul->getOperand(i)));
2534 return X;
2535 }
2536
2537 if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
2538 ConstantRange X = getUnsignedRange(SMax->getOperand(0));
2539 for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
2540 X = X.smax(getUnsignedRange(SMax->getOperand(i)));
2541 return X;
2542 }
2543
2544 if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
2545 ConstantRange X = getUnsignedRange(UMax->getOperand(0));
2546 for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
2547 X = X.umax(getUnsignedRange(UMax->getOperand(i)));
2548 return X;
2549 }
2550
2551 if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
2552 ConstantRange X = getUnsignedRange(UDiv->getLHS());
2553 ConstantRange Y = getUnsignedRange(UDiv->getRHS());
2554 return X.udiv(Y);
2555 }
2556
2557 if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
2558 ConstantRange X = getUnsignedRange(ZExt->getOperand());
2559 return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth());
2560 }
2561
2562 if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
2563 ConstantRange X = getUnsignedRange(SExt->getOperand());
2564 return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth());
2565 }
2566
2567 if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
2568 ConstantRange X = getUnsignedRange(Trunc->getOperand());
2569 return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth());
2570 }
2571
2572 ConstantRange FullSet(getTypeSizeInBits(S->getType()), true);
2573
2574 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
2575 const SCEV *T = getBackedgeTakenCount(AddRec->getLoop());
2576 const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T);
2577 if (!Trip) return FullSet;
2578
2579 // TODO: non-affine addrec
2580 if (AddRec->isAffine()) {
2581 const Type *Ty = AddRec->getType();
2582 const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop());
2583 if (getTypeSizeInBits(MaxBECount->getType()) <= getTypeSizeInBits(Ty)) {
2584 MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty);
2585
2586 const SCEV *Start = AddRec->getStart();
Dan Gohman13dca602009-07-21 00:42:47 +00002587 const SCEV *Step = AddRec->getStepRecurrence(*this);
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002588 const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this);
2589
2590 // Check for overflow.
Dan Gohman13dca602009-07-21 00:42:47 +00002591 // TODO: This is very conservative.
2592 if (!(Step->isOne() &&
2593 isKnownPredicate(ICmpInst::ICMP_ULT, Start, End)) &&
2594 !(Step->isAllOnesValue() &&
2595 isKnownPredicate(ICmpInst::ICMP_UGT, Start, End)))
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002596 return FullSet;
2597
2598 ConstantRange StartRange = getUnsignedRange(Start);
2599 ConstantRange EndRange = getUnsignedRange(End);
2600 APInt Min = APIntOps::umin(StartRange.getUnsignedMin(),
2601 EndRange.getUnsignedMin());
2602 APInt Max = APIntOps::umax(StartRange.getUnsignedMax(),
2603 EndRange.getUnsignedMax());
2604 if (Min.isMinValue() && Max.isMaxValue())
Dan Gohman56e18592009-07-20 22:41:51 +00002605 return FullSet;
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002606 return ConstantRange(Min, Max+1);
2607 }
2608 }
Dan Gohman6e923a72009-06-19 23:29:04 +00002609 }
2610
2611 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2612 // For a SCEVUnknown, ask ValueTracking.
2613 unsigned BitWidth = getTypeSizeInBits(U->getType());
2614 APInt Mask = APInt::getAllOnesValue(BitWidth);
2615 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
2616 ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD);
Dan Gohman07620512009-07-20 22:34:18 +00002617 if (Ones == ~Zeros + 1)
2618 return FullSet;
2619 return ConstantRange(Ones, ~Zeros + 1);
Dan Gohman6e923a72009-06-19 23:29:04 +00002620 }
2621
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002622 return FullSet;
Dan Gohman6e923a72009-06-19 23:29:04 +00002623}
2624
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002625/// getSignedRange - Determine the signed range for a particular SCEV.
2626///
2627ConstantRange
2628ScalarEvolution::getSignedRange(const SCEV *S) {
Dan Gohman6e923a72009-06-19 23:29:04 +00002629
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002630 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
2631 return ConstantRange(C->getValue()->getValue());
2632
2633 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
2634 ConstantRange X = getSignedRange(Add->getOperand(0));
2635 for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
2636 X = X.add(getSignedRange(Add->getOperand(i)));
2637 return X;
Dan Gohman6e923a72009-06-19 23:29:04 +00002638 }
2639
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002640 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
2641 ConstantRange X = getSignedRange(Mul->getOperand(0));
2642 for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
2643 X = X.multiply(getSignedRange(Mul->getOperand(i)));
2644 return X;
Dan Gohman6e923a72009-06-19 23:29:04 +00002645 }
2646
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002647 if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
2648 ConstantRange X = getSignedRange(SMax->getOperand(0));
2649 for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
2650 X = X.smax(getSignedRange(SMax->getOperand(i)));
2651 return X;
2652 }
Dan Gohman61e0c4c2009-06-24 01:05:09 +00002653
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002654 if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
2655 ConstantRange X = getSignedRange(UMax->getOperand(0));
2656 for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
2657 X = X.umax(getSignedRange(UMax->getOperand(i)));
2658 return X;
2659 }
Dan Gohman61e0c4c2009-06-24 01:05:09 +00002660
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002661 if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
2662 ConstantRange X = getSignedRange(UDiv->getLHS());
2663 ConstantRange Y = getSignedRange(UDiv->getRHS());
2664 return X.udiv(Y);
2665 }
Dan Gohman61e0c4c2009-06-24 01:05:09 +00002666
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002667 if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
2668 ConstantRange X = getSignedRange(ZExt->getOperand());
2669 return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth());
2670 }
2671
2672 if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
2673 ConstantRange X = getSignedRange(SExt->getOperand());
2674 return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth());
2675 }
2676
2677 if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
2678 ConstantRange X = getSignedRange(Trunc->getOperand());
2679 return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth());
2680 }
2681
2682 ConstantRange FullSet(getTypeSizeInBits(S->getType()), true);
2683
2684 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
2685 const SCEV *T = getBackedgeTakenCount(AddRec->getLoop());
2686 const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T);
2687 if (!Trip) return FullSet;
2688
2689 // TODO: non-affine addrec
2690 if (AddRec->isAffine()) {
2691 const Type *Ty = AddRec->getType();
2692 const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop());
2693 if (getTypeSizeInBits(MaxBECount->getType()) <= getTypeSizeInBits(Ty)) {
2694 MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty);
2695
2696 const SCEV *Start = AddRec->getStart();
2697 const SCEV *Step = AddRec->getStepRecurrence(*this);
2698 const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this);
2699
2700 // Check for overflow.
Dan Gohman13dca602009-07-21 00:42:47 +00002701 // TODO: This is very conservative.
2702 if (!(Step->isOne() &&
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002703 isKnownPredicate(ICmpInst::ICMP_SLT, Start, End)) &&
Dan Gohman13dca602009-07-21 00:42:47 +00002704 !(Step->isAllOnesValue() &&
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002705 isKnownPredicate(ICmpInst::ICMP_SGT, Start, End)))
2706 return FullSet;
2707
2708 ConstantRange StartRange = getSignedRange(Start);
2709 ConstantRange EndRange = getSignedRange(End);
2710 APInt Min = APIntOps::smin(StartRange.getSignedMin(),
2711 EndRange.getSignedMin());
2712 APInt Max = APIntOps::smax(StartRange.getSignedMax(),
2713 EndRange.getSignedMax());
2714 if (Min.isMinSignedValue() && Max.isMaxSignedValue())
Dan Gohmandc87c862009-07-21 00:37:45 +00002715 return FullSet;
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002716 return ConstantRange(Min, Max+1);
Dan Gohman61e0c4c2009-06-24 01:05:09 +00002717 }
Dan Gohman61e0c4c2009-06-24 01:05:09 +00002718 }
Dan Gohman61e0c4c2009-06-24 01:05:09 +00002719 }
2720
Dan Gohman6e923a72009-06-19 23:29:04 +00002721 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2722 // For a SCEVUnknown, ask ValueTracking.
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002723 unsigned BitWidth = getTypeSizeInBits(U->getType());
2724 unsigned NS = ComputeNumSignBits(U->getValue(), TD);
2725 if (NS == 1)
2726 return FullSet;
2727 return
2728 ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1),
2729 APInt::getSignedMaxValue(BitWidth).ashr(NS - 1)+1);
Dan Gohman6e923a72009-06-19 23:29:04 +00002730 }
2731
Dan Gohman55e2d7e2009-07-13 21:35:55 +00002732 return FullSet;
Dan Gohman6e923a72009-06-19 23:29:04 +00002733}
2734
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002735/// createSCEV - We know that there is no SCEV for the specified value.
2736/// Analyze the expression.
2737///
Dan Gohman161ea032009-07-07 17:06:11 +00002738const SCEV *ScalarEvolution::createSCEV(Value *V) {
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002739 if (!isSCEVable(V->getType()))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002740 return getUnknown(V);
Dan Gohman01c2ee72009-04-16 03:18:22 +00002741
Dan Gohman3996f472008-06-22 19:56:46 +00002742 unsigned Opcode = Instruction::UserOp1;
2743 if (Instruction *I = dyn_cast<Instruction>(V))
2744 Opcode = I->getOpcode();
2745 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
2746 Opcode = CE->getOpcode();
Dan Gohman984c78a2009-06-24 00:54:57 +00002747 else if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
2748 return getConstant(CI);
2749 else if (isa<ConstantPointerNull>(V))
2750 return getIntegerSCEV(0, V->getType());
2751 else if (isa<UndefValue>(V))
2752 return getIntegerSCEV(0, V->getType());
Dan Gohman3996f472008-06-22 19:56:46 +00002753 else
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002754 return getUnknown(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002755
Dan Gohman9545fb02009-07-17 20:47:02 +00002756 Operator *U = cast<Operator>(V);
Dan Gohman3996f472008-06-22 19:56:46 +00002757 switch (Opcode) {
2758 case Instruction::Add:
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002759 return getAddExpr(getSCEV(U->getOperand(0)),
2760 getSCEV(U->getOperand(1)));
Dan Gohman3996f472008-06-22 19:56:46 +00002761 case Instruction::Mul:
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002762 return getMulExpr(getSCEV(U->getOperand(0)),
2763 getSCEV(U->getOperand(1)));
Dan Gohman3996f472008-06-22 19:56:46 +00002764 case Instruction::UDiv:
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002765 return getUDivExpr(getSCEV(U->getOperand(0)),
2766 getSCEV(U->getOperand(1)));
Dan Gohman3996f472008-06-22 19:56:46 +00002767 case Instruction::Sub:
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002768 return getMinusSCEV(getSCEV(U->getOperand(0)),
2769 getSCEV(U->getOperand(1)));
Dan Gohman53bf64a2009-04-21 02:26:00 +00002770 case Instruction::And:
2771 // For an expression like x&255 that merely masks off the high bits,
2772 // use zext(trunc(x)) as the SCEV expression.
2773 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman91ae1e72009-04-25 17:05:40 +00002774 if (CI->isNullValue())
2775 return getSCEV(U->getOperand(1));
Dan Gohmanc7ebba12009-04-27 01:41:10 +00002776 if (CI->isAllOnesValue())
2777 return getSCEV(U->getOperand(0));
Dan Gohman53bf64a2009-04-21 02:26:00 +00002778 const APInt &A = CI->getValue();
Dan Gohmana7726c32009-06-16 19:52:01 +00002779
2780 // Instcombine's ShrinkDemandedConstant may strip bits out of
2781 // constants, obscuring what would otherwise be a low-bits mask.
2782 // Use ComputeMaskedBits to compute what ShrinkDemandedConstant
2783 // knew about to reconstruct a low-bits mask value.
2784 unsigned LZ = A.countLeadingZeros();
2785 unsigned BitWidth = A.getBitWidth();
2786 APInt AllOnes = APInt::getAllOnesValue(BitWidth);
2787 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
2788 ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD);
2789
2790 APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ);
2791
Dan Gohmanae1d7dd2009-06-17 23:54:37 +00002792 if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask))
Dan Gohman53bf64a2009-04-21 02:26:00 +00002793 return
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002794 getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)),
Dan Gohmana7726c32009-06-16 19:52:01 +00002795 IntegerType::get(BitWidth - LZ)),
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002796 U->getType());
Dan Gohman53bf64a2009-04-21 02:26:00 +00002797 }
2798 break;
Dan Gohmana7726c32009-06-16 19:52:01 +00002799
Dan Gohman3996f472008-06-22 19:56:46 +00002800 case Instruction::Or:
2801 // If the RHS of the Or is a constant, we may have something like:
2802 // X*4+1 which got turned into X*4|1. Handle this as an Add so loop
2803 // optimizations will transparently handle this case.
2804 //
2805 // In order for this transformation to be safe, the LHS must be of the
2806 // form X*(2^n) and the Or constant must be less than 2^n.
2807 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman161ea032009-07-07 17:06:11 +00002808 const SCEV *LHS = getSCEV(U->getOperand(0));
Dan Gohman3996f472008-06-22 19:56:46 +00002809 const APInt &CIVal = CI->getValue();
Dan Gohman6e923a72009-06-19 23:29:04 +00002810 if (GetMinTrailingZeros(LHS) >=
Dan Gohman3996f472008-06-22 19:56:46 +00002811 (CIVal.getBitWidth() - CIVal.countLeadingZeros()))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002812 return getAddExpr(LHS, getSCEV(U->getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002813 }
Dan Gohman3996f472008-06-22 19:56:46 +00002814 break;
2815 case Instruction::Xor:
Dan Gohman3996f472008-06-22 19:56:46 +00002816 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Nick Lewycky7fd27892008-07-07 06:15:49 +00002817 // If the RHS of the xor is a signbit, then this is just an add.
2818 // Instcombine turns add of signbit into xor as a strength reduction step.
Dan Gohman3996f472008-06-22 19:56:46 +00002819 if (CI->getValue().isSignBit())
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002820 return getAddExpr(getSCEV(U->getOperand(0)),
2821 getSCEV(U->getOperand(1)));
Nick Lewycky7fd27892008-07-07 06:15:49 +00002822
2823 // If the RHS of xor is -1, then this is a not operation.
Dan Gohmanc897f752009-05-18 16:17:44 +00002824 if (CI->isAllOnesValue())
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002825 return getNotSCEV(getSCEV(U->getOperand(0)));
Dan Gohmanfc78cff2009-05-18 16:29:04 +00002826
2827 // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask.
2828 // This is a variant of the check for xor with -1, and it handles
2829 // the case where instcombine has trimmed non-demanded bits out
2830 // of an xor with -1.
2831 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0)))
2832 if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1)))
2833 if (BO->getOpcode() == Instruction::And &&
2834 LCI->getValue() == CI->getValue())
2835 if (const SCEVZeroExtendExpr *Z =
Dan Gohmane49ae432009-06-17 01:22:39 +00002836 dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) {
Dan Gohmaned1d8bb2009-06-18 00:00:20 +00002837 const Type *UTy = U->getType();
Dan Gohman161ea032009-07-07 17:06:11 +00002838 const SCEV *Z0 = Z->getOperand();
Dan Gohmaned1d8bb2009-06-18 00:00:20 +00002839 const Type *Z0Ty = Z0->getType();
2840 unsigned Z0TySize = getTypeSizeInBits(Z0Ty);
2841
2842 // If C is a low-bits mask, the zero extend is zerving to
2843 // mask off the high bits. Complement the operand and
2844 // re-apply the zext.
2845 if (APIntOps::isMask(Z0TySize, CI->getValue()))
2846 return getZeroExtendExpr(getNotSCEV(Z0), UTy);
2847
2848 // If C is a single bit, it may be in the sign-bit position
2849 // before the zero-extend. In this case, represent the xor
2850 // using an add, which is equivalent, and re-apply the zext.
2851 APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize);
2852 if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() &&
2853 Trunc.isSignBit())
2854 return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)),
2855 UTy);
Dan Gohmane49ae432009-06-17 01:22:39 +00002856 }
Dan Gohman3996f472008-06-22 19:56:46 +00002857 }
2858 break;
2859
2860 case Instruction::Shl:
2861 // Turn shift left of a constant amount into a multiply.
2862 if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
2863 uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
Owen Andersoneacb44d2009-07-24 23:12:02 +00002864 Constant *X = ConstantInt::get(getContext(),
Dan Gohman3996f472008-06-22 19:56:46 +00002865 APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002866 return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X));
Dan Gohman3996f472008-06-22 19:56:46 +00002867 }
2868 break;
2869
Nick Lewycky7fd27892008-07-07 06:15:49 +00002870 case Instruction::LShr:
Nick Lewycky35b56022009-01-13 09:18:58 +00002871 // Turn logical shift right of a constant into a unsigned divide.
Nick Lewycky7fd27892008-07-07 06:15:49 +00002872 if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
2873 uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
Owen Andersoneacb44d2009-07-24 23:12:02 +00002874 Constant *X = ConstantInt::get(getContext(),
Nick Lewycky7fd27892008-07-07 06:15:49 +00002875 APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002876 return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X));
Nick Lewycky7fd27892008-07-07 06:15:49 +00002877 }
2878 break;
2879
Dan Gohman53bf64a2009-04-21 02:26:00 +00002880 case Instruction::AShr:
2881 // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression.
2882 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1)))
2883 if (Instruction *L = dyn_cast<Instruction>(U->getOperand(0)))
2884 if (L->getOpcode() == Instruction::Shl &&
2885 L->getOperand(1) == U->getOperand(1)) {
Dan Gohman91ae1e72009-04-25 17:05:40 +00002886 unsigned BitWidth = getTypeSizeInBits(U->getType());
2887 uint64_t Amt = BitWidth - CI->getZExtValue();
2888 if (Amt == BitWidth)
2889 return getSCEV(L->getOperand(0)); // shift by zero --> noop
2890 if (Amt > BitWidth)
2891 return getIntegerSCEV(0, U->getType()); // value is undefined
Dan Gohman53bf64a2009-04-21 02:26:00 +00002892 return
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002893 getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)),
Dan Gohman91ae1e72009-04-25 17:05:40 +00002894 IntegerType::get(Amt)),
Dan Gohman53bf64a2009-04-21 02:26:00 +00002895 U->getType());
2896 }
2897 break;
2898
Dan Gohman3996f472008-06-22 19:56:46 +00002899 case Instruction::Trunc:
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002900 return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman3996f472008-06-22 19:56:46 +00002901
2902 case Instruction::ZExt:
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002903 return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman3996f472008-06-22 19:56:46 +00002904
2905 case Instruction::SExt:
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002906 return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman3996f472008-06-22 19:56:46 +00002907
2908 case Instruction::BitCast:
2909 // BitCasts are no-op casts so we just eliminate the cast.
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002910 if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType()))
Dan Gohman3996f472008-06-22 19:56:46 +00002911 return getSCEV(U->getOperand(0));
2912 break;
2913
Dan Gohman2ec15e62009-07-20 17:43:30 +00002914 // It's tempting to handle inttoptr and ptrtoint, however this can
2915 // lead to pointer expressions which cannot be expanded to GEPs
2916 // (because they may overflow). For now, the only pointer-typed
2917 // expressions we handle are GEPs and address literals.
Dan Gohman01c2ee72009-04-16 03:18:22 +00002918
Dan Gohman509cf4d2009-05-08 20:26:55 +00002919 case Instruction::GetElementPtr:
Dan Gohmanb98c1a32009-04-21 01:07:12 +00002920 if (!TD) break; // Without TD we can't analyze pointers.
Dan Gohmanca5a39e2009-05-08 20:58:38 +00002921 return createNodeForGEP(U);
Dan Gohman01c2ee72009-04-16 03:18:22 +00002922
Dan Gohman3996f472008-06-22 19:56:46 +00002923 case Instruction::PHI:
2924 return createNodeForPHI(cast<PHINode>(U));
2925
2926 case Instruction::Select:
2927 // This could be a smax or umax that was lowered earlier.
2928 // Try to recover it.
2929 if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) {
2930 Value *LHS = ICI->getOperand(0);
2931 Value *RHS = ICI->getOperand(1);
2932 switch (ICI->getPredicate()) {
2933 case ICmpInst::ICMP_SLT:
2934 case ICmpInst::ICMP_SLE:
2935 std::swap(LHS, RHS);
2936 // fall through
2937 case ICmpInst::ICMP_SGT:
2938 case ICmpInst::ICMP_SGE:
2939 if (LHS == U->getOperand(1) && RHS == U->getOperand(2))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002940 return getSMaxExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman3996f472008-06-22 19:56:46 +00002941 else if (LHS == U->getOperand(2) && RHS == U->getOperand(1))
Dan Gohmand01fff82009-06-22 03:18:45 +00002942 return getSMinExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman3996f472008-06-22 19:56:46 +00002943 break;
2944 case ICmpInst::ICMP_ULT:
2945 case ICmpInst::ICMP_ULE:
2946 std::swap(LHS, RHS);
2947 // fall through
2948 case ICmpInst::ICMP_UGT:
2949 case ICmpInst::ICMP_UGE:
2950 if (LHS == U->getOperand(1) && RHS == U->getOperand(2))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002951 return getUMaxExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman3996f472008-06-22 19:56:46 +00002952 else if (LHS == U->getOperand(2) && RHS == U->getOperand(1))
Dan Gohmand01fff82009-06-22 03:18:45 +00002953 return getUMinExpr(getSCEV(LHS), getSCEV(RHS));
Dan Gohman3996f472008-06-22 19:56:46 +00002954 break;
Dan Gohmanf27dc692009-06-18 20:21:07 +00002955 case ICmpInst::ICMP_NE:
2956 // n != 0 ? n : 1 -> umax(n, 1)
2957 if (LHS == U->getOperand(1) &&
2958 isa<ConstantInt>(U->getOperand(2)) &&
2959 cast<ConstantInt>(U->getOperand(2))->isOne() &&
2960 isa<ConstantInt>(RHS) &&
2961 cast<ConstantInt>(RHS)->isZero())
2962 return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(2)));
2963 break;
2964 case ICmpInst::ICMP_EQ:
2965 // n == 0 ? 1 : n -> umax(n, 1)
2966 if (LHS == U->getOperand(2) &&
2967 isa<ConstantInt>(U->getOperand(1)) &&
2968 cast<ConstantInt>(U->getOperand(1))->isOne() &&
2969 isa<ConstantInt>(RHS) &&
2970 cast<ConstantInt>(RHS)->isZero())
2971 return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(1)));
2972 break;
Dan Gohman3996f472008-06-22 19:56:46 +00002973 default:
2974 break;
2975 }
2976 }
2977
2978 default: // We cannot analyze this expression.
2979 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002980 }
2981
Dan Gohmanffd36ba2009-04-21 23:15:49 +00002982 return getUnknown(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002983}
2984
2985
2986
2987//===----------------------------------------------------------------------===//
2988// Iteration Count Computation Code
2989//
2990
Dan Gohman76d5a0d2009-02-24 18:55:53 +00002991/// getBackedgeTakenCount - If the specified loop has a predictable
2992/// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
2993/// object. The backedge-taken count is the number of times the loop header
2994/// will be branched to from within the loop. This is one less than the
2995/// trip count of the loop, since it doesn't count the first iteration,
2996/// when the header is branched to from outside the loop.
2997///
2998/// Note that it is not valid to call this method on a loop without a
2999/// loop-invariant backedge-taken count (see
3000/// hasLoopInvariantBackedgeTakenCount).
3001///
Dan Gohman161ea032009-07-07 17:06:11 +00003002const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00003003 return getBackedgeTakenInfo(L).Exact;
3004}
3005
3006/// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except
3007/// return the least SCEV value that is known never to be less than the
3008/// actual backedge taken count.
Dan Gohman161ea032009-07-07 17:06:11 +00003009const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00003010 return getBackedgeTakenInfo(L).Max;
3011}
3012
Dan Gohmanb7d04aa2009-07-08 19:23:34 +00003013/// PushLoopPHIs - Push PHI nodes in the header of the given loop
3014/// onto the given Worklist.
3015static void
3016PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) {
3017 BasicBlock *Header = L->getHeader();
3018
3019 // Push all Loop-header PHIs onto the Worklist stack.
3020 for (BasicBlock::iterator I = Header->begin();
3021 PHINode *PN = dyn_cast<PHINode>(I); ++I)
3022 Worklist.push_back(PN);
3023}
3024
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00003025const ScalarEvolution::BackedgeTakenInfo &
3026ScalarEvolution::getBackedgeTakenInfo(const Loop *L) {
Dan Gohmana9dba962009-04-27 20:16:15 +00003027 // Initially insert a CouldNotCompute for this loop. If the insertion
3028 // succeeds, procede to actually compute a backedge-taken count and
3029 // update the value. The temporary CouldNotCompute value tells SCEV
3030 // code elsewhere that it shouldn't attempt to request a new
3031 // backedge-taken count, which could result in infinite recursion.
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00003032 std::pair<std::map<const Loop*, BackedgeTakenInfo>::iterator, bool> Pair =
Dan Gohmana9dba962009-04-27 20:16:15 +00003033 BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute()));
3034 if (Pair.second) {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00003035 BackedgeTakenInfo ItCount = ComputeBackedgeTakenCount(L);
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003036 if (ItCount.Exact != getCouldNotCompute()) {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00003037 assert(ItCount.Exact->isLoopInvariant(L) &&
3038 ItCount.Max->isLoopInvariant(L) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003039 "Computed trip count isn't loop invariant for loop!");
3040 ++NumTripCountsComputed;
Dan Gohmana9dba962009-04-27 20:16:15 +00003041
Dan Gohmana9dba962009-04-27 20:16:15 +00003042 // Update the value in the map.
3043 Pair.first->second = ItCount;
Dan Gohman8e8b5232009-06-22 00:31:57 +00003044 } else {
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003045 if (ItCount.Max != getCouldNotCompute())
Dan Gohman8e8b5232009-06-22 00:31:57 +00003046 // Update the value in the map.
3047 Pair.first->second = ItCount;
3048 if (isa<PHINode>(L->getHeader()->begin()))
3049 // Only count loops that have phi nodes as not being computable.
3050 ++NumTripCountsNotComputed;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003051 }
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00003052
3053 // Now that we know more about the trip count for this loop, forget any
3054 // existing SCEV values for PHI nodes in this loop since they are only
Dan Gohmanb7d04aa2009-07-08 19:23:34 +00003055 // conservative estimates made without the benefit of trip count
3056 // information. This is similar to the code in
3057 // forgetLoopBackedgeTakenCount, except that it handles SCEVUnknown PHI
3058 // nodes specially.
3059 if (ItCount.hasAnyInfo()) {
3060 SmallVector<Instruction *, 16> Worklist;
3061 PushLoopPHIs(L, Worklist);
3062
3063 SmallPtrSet<Instruction *, 8> Visited;
3064 while (!Worklist.empty()) {
3065 Instruction *I = Worklist.pop_back_val();
3066 if (!Visited.insert(I)) continue;
3067
3068 std::map<SCEVCallbackVH, const SCEV*>::iterator It =
3069 Scalars.find(static_cast<Value *>(I));
3070 if (It != Scalars.end()) {
3071 // SCEVUnknown for a PHI either means that it has an unrecognized
3072 // structure, or it's a PHI that's in the progress of being computed
Dan Gohman0fa91f32009-07-13 22:04:06 +00003073 // by createNodeForPHI. In the former case, additional loop trip
3074 // count information isn't going to change anything. In the later
3075 // case, createNodeForPHI will perform the necessary updates on its
3076 // own when it gets to that point.
Dan Gohmanb7d04aa2009-07-08 19:23:34 +00003077 if (!isa<PHINode>(I) || !isa<SCEVUnknown>(It->second))
3078 Scalars.erase(It);
3079 ValuesAtScopes.erase(I);
3080 if (PHINode *PN = dyn_cast<PHINode>(I))
3081 ConstantEvolutionLoopExitValue.erase(PN);
3082 }
3083
3084 PushDefUseChildren(I, Worklist);
3085 }
3086 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003087 }
Dan Gohmana9dba962009-04-27 20:16:15 +00003088 return Pair.first->second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003089}
3090
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003091/// forgetLoopBackedgeTakenCount - This method should be called by the
Dan Gohmanf3a060a2009-02-17 20:49:49 +00003092/// client when it has changed a loop in a way that may effect
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003093/// ScalarEvolution's ability to compute a trip count, or if the loop
3094/// is deleted.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003095void ScalarEvolution::forgetLoopBackedgeTakenCount(const Loop *L) {
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003096 BackedgeTakenCounts.erase(L);
Dan Gohman94623022009-05-02 17:43:35 +00003097
Dan Gohmanbff6b582009-05-04 22:30:44 +00003098 SmallVector<Instruction *, 16> Worklist;
Dan Gohmanb7d04aa2009-07-08 19:23:34 +00003099 PushLoopPHIs(L, Worklist);
Dan Gohmanbff6b582009-05-04 22:30:44 +00003100
Dan Gohmanb7d04aa2009-07-08 19:23:34 +00003101 SmallPtrSet<Instruction *, 8> Visited;
Dan Gohmanbff6b582009-05-04 22:30:44 +00003102 while (!Worklist.empty()) {
3103 Instruction *I = Worklist.pop_back_val();
Dan Gohmanb7d04aa2009-07-08 19:23:34 +00003104 if (!Visited.insert(I)) continue;
3105
3106 std::map<SCEVCallbackVH, const SCEV*>::iterator It =
3107 Scalars.find(static_cast<Value *>(I));
3108 if (It != Scalars.end()) {
3109 Scalars.erase(It);
3110 ValuesAtScopes.erase(I);
3111 if (PHINode *PN = dyn_cast<PHINode>(I))
3112 ConstantEvolutionLoopExitValue.erase(PN);
3113 }
3114
3115 PushDefUseChildren(I, Worklist);
Dan Gohmanbff6b582009-05-04 22:30:44 +00003116 }
Dan Gohmanf3a060a2009-02-17 20:49:49 +00003117}
3118
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003119/// ComputeBackedgeTakenCount - Compute the number of times the backedge
3120/// of the specified loop will execute.
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00003121ScalarEvolution::BackedgeTakenInfo
3122ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) {
Dan Gohman8e8b5232009-06-22 00:31:57 +00003123 SmallVector<BasicBlock*, 8> ExitingBlocks;
3124 L->getExitingBlocks(ExitingBlocks);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003125
Dan Gohman8e8b5232009-06-22 00:31:57 +00003126 // Examine all exits and pick the most conservative values.
Dan Gohman161ea032009-07-07 17:06:11 +00003127 const SCEV *BECount = getCouldNotCompute();
3128 const SCEV *MaxBECount = getCouldNotCompute();
Dan Gohman8e8b5232009-06-22 00:31:57 +00003129 bool CouldNotComputeBECount = false;
Dan Gohman8e8b5232009-06-22 00:31:57 +00003130 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
3131 BackedgeTakenInfo NewBTI =
3132 ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003133
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003134 if (NewBTI.Exact == getCouldNotCompute()) {
Dan Gohman8e8b5232009-06-22 00:31:57 +00003135 // We couldn't compute an exact value for this exit, so
Dan Gohmanc6e8c832009-06-22 21:10:22 +00003136 // we won't be able to compute an exact value for the loop.
Dan Gohman8e8b5232009-06-22 00:31:57 +00003137 CouldNotComputeBECount = true;
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003138 BECount = getCouldNotCompute();
Dan Gohman8e8b5232009-06-22 00:31:57 +00003139 } else if (!CouldNotComputeBECount) {
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003140 if (BECount == getCouldNotCompute())
Dan Gohman8e8b5232009-06-22 00:31:57 +00003141 BECount = NewBTI.Exact;
Dan Gohman8e8b5232009-06-22 00:31:57 +00003142 else
Dan Gohman423ed6c2009-06-24 01:18:18 +00003143 BECount = getUMinFromMismatchedTypes(BECount, NewBTI.Exact);
Dan Gohman8e8b5232009-06-22 00:31:57 +00003144 }
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003145 if (MaxBECount == getCouldNotCompute())
Dan Gohman423ed6c2009-06-24 01:18:18 +00003146 MaxBECount = NewBTI.Max;
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003147 else if (NewBTI.Max != getCouldNotCompute())
Dan Gohman423ed6c2009-06-24 01:18:18 +00003148 MaxBECount = getUMinFromMismatchedTypes(MaxBECount, NewBTI.Max);
Dan Gohman8e8b5232009-06-22 00:31:57 +00003149 }
3150
3151 return BackedgeTakenInfo(BECount, MaxBECount);
3152}
3153
3154/// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge
3155/// of the specified loop will execute if it exits via the specified block.
3156ScalarEvolution::BackedgeTakenInfo
3157ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L,
3158 BasicBlock *ExitingBlock) {
3159
3160 // Okay, we've chosen an exiting block. See what condition causes us to
3161 // exit at this block.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003162 //
3163 // FIXME: we should be able to handle switch instructions (with a single exit)
3164 BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003165 if (ExitBr == 0) return getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003166 assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!");
Dan Gohman9bc642f2009-06-24 04:48:43 +00003167
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003168 // At this point, we know we have a conditional branch that determines whether
3169 // the loop is exited. However, we don't know if the branch is executed each
3170 // time through the loop. If not, then the execution count of the branch will
3171 // not be equal to the trip count of the loop.
3172 //
3173 // Currently we check for this by checking to see if the Exit branch goes to
3174 // the loop header. If so, we know it will always execute the same number of
3175 // times as the loop. We also handle the case where the exit block *is* the
Dan Gohman8e8b5232009-06-22 00:31:57 +00003176 // loop header. This is common for un-rotated loops.
3177 //
3178 // If both of those tests fail, walk up the unique predecessor chain to the
3179 // header, stopping if there is an edge that doesn't exit the loop. If the
3180 // header is reached, the execution count of the branch will be equal to the
3181 // trip count of the loop.
3182 //
3183 // More extensive analysis could be done to handle more cases here.
3184 //
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003185 if (ExitBr->getSuccessor(0) != L->getHeader() &&
3186 ExitBr->getSuccessor(1) != L->getHeader() &&
Dan Gohman8e8b5232009-06-22 00:31:57 +00003187 ExitBr->getParent() != L->getHeader()) {
3188 // The simple checks failed, try climbing the unique predecessor chain
3189 // up to the header.
3190 bool Ok = false;
3191 for (BasicBlock *BB = ExitBr->getParent(); BB; ) {
3192 BasicBlock *Pred = BB->getUniquePredecessor();
3193 if (!Pred)
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003194 return getCouldNotCompute();
Dan Gohman8e8b5232009-06-22 00:31:57 +00003195 TerminatorInst *PredTerm = Pred->getTerminator();
3196 for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) {
3197 BasicBlock *PredSucc = PredTerm->getSuccessor(i);
3198 if (PredSucc == BB)
3199 continue;
3200 // If the predecessor has a successor that isn't BB and isn't
3201 // outside the loop, assume the worst.
3202 if (L->contains(PredSucc))
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003203 return getCouldNotCompute();
Dan Gohman8e8b5232009-06-22 00:31:57 +00003204 }
3205 if (Pred == L->getHeader()) {
3206 Ok = true;
3207 break;
3208 }
3209 BB = Pred;
3210 }
3211 if (!Ok)
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003212 return getCouldNotCompute();
Dan Gohman8e8b5232009-06-22 00:31:57 +00003213 }
3214
3215 // Procede to the next level to examine the exit condition expression.
3216 return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(),
3217 ExitBr->getSuccessor(0),
3218 ExitBr->getSuccessor(1));
3219}
3220
3221/// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the
3222/// backedge of the specified loop will execute if its exit condition
3223/// were a conditional branch of ExitCond, TBB, and FBB.
3224ScalarEvolution::BackedgeTakenInfo
3225ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L,
3226 Value *ExitCond,
3227 BasicBlock *TBB,
3228 BasicBlock *FBB) {
Dan Gohman423ed6c2009-06-24 01:18:18 +00003229 // Check if the controlling expression for this loop is an And or Or.
Dan Gohman8e8b5232009-06-22 00:31:57 +00003230 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) {
3231 if (BO->getOpcode() == Instruction::And) {
3232 // Recurse on the operands of the and.
3233 BackedgeTakenInfo BTI0 =
3234 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB);
3235 BackedgeTakenInfo BTI1 =
3236 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB);
Dan Gohman161ea032009-07-07 17:06:11 +00003237 const SCEV *BECount = getCouldNotCompute();
3238 const SCEV *MaxBECount = getCouldNotCompute();
Dan Gohman8e8b5232009-06-22 00:31:57 +00003239 if (L->contains(TBB)) {
3240 // Both conditions must be true for the loop to continue executing.
3241 // Choose the less conservative count.
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003242 if (BTI0.Exact == getCouldNotCompute() ||
3243 BTI1.Exact == getCouldNotCompute())
3244 BECount = getCouldNotCompute();
Dan Gohmanac958b32009-06-22 15:09:28 +00003245 else
3246 BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003247 if (BTI0.Max == getCouldNotCompute())
Dan Gohman8e8b5232009-06-22 00:31:57 +00003248 MaxBECount = BTI1.Max;
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003249 else if (BTI1.Max == getCouldNotCompute())
Dan Gohman8e8b5232009-06-22 00:31:57 +00003250 MaxBECount = BTI0.Max;
Dan Gohmanac958b32009-06-22 15:09:28 +00003251 else
3252 MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max);
Dan Gohman8e8b5232009-06-22 00:31:57 +00003253 } else {
3254 // Both conditions must be true for the loop to exit.
3255 assert(L->contains(FBB) && "Loop block has no successor in loop!");
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003256 if (BTI0.Exact != getCouldNotCompute() &&
3257 BTI1.Exact != getCouldNotCompute())
Dan Gohman8e8b5232009-06-22 00:31:57 +00003258 BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003259 if (BTI0.Max != getCouldNotCompute() &&
3260 BTI1.Max != getCouldNotCompute())
Dan Gohman8e8b5232009-06-22 00:31:57 +00003261 MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max);
3262 }
3263
3264 return BackedgeTakenInfo(BECount, MaxBECount);
3265 }
3266 if (BO->getOpcode() == Instruction::Or) {
3267 // Recurse on the operands of the or.
3268 BackedgeTakenInfo BTI0 =
3269 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB);
3270 BackedgeTakenInfo BTI1 =
3271 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB);
Dan Gohman161ea032009-07-07 17:06:11 +00003272 const SCEV *BECount = getCouldNotCompute();
3273 const SCEV *MaxBECount = getCouldNotCompute();
Dan Gohman8e8b5232009-06-22 00:31:57 +00003274 if (L->contains(FBB)) {
3275 // Both conditions must be false for the loop to continue executing.
3276 // Choose the less conservative count.
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003277 if (BTI0.Exact == getCouldNotCompute() ||
3278 BTI1.Exact == getCouldNotCompute())
3279 BECount = getCouldNotCompute();
Dan Gohmanac958b32009-06-22 15:09:28 +00003280 else
3281 BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003282 if (BTI0.Max == getCouldNotCompute())
Dan Gohman8e8b5232009-06-22 00:31:57 +00003283 MaxBECount = BTI1.Max;
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003284 else if (BTI1.Max == getCouldNotCompute())
Dan Gohman8e8b5232009-06-22 00:31:57 +00003285 MaxBECount = BTI0.Max;
Dan Gohmanac958b32009-06-22 15:09:28 +00003286 else
3287 MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max);
Dan Gohman8e8b5232009-06-22 00:31:57 +00003288 } else {
3289 // Both conditions must be false for the loop to exit.
3290 assert(L->contains(TBB) && "Loop block has no successor in loop!");
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003291 if (BTI0.Exact != getCouldNotCompute() &&
3292 BTI1.Exact != getCouldNotCompute())
Dan Gohman8e8b5232009-06-22 00:31:57 +00003293 BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003294 if (BTI0.Max != getCouldNotCompute() &&
3295 BTI1.Max != getCouldNotCompute())
Dan Gohman8e8b5232009-06-22 00:31:57 +00003296 MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max);
3297 }
3298
3299 return BackedgeTakenInfo(BECount, MaxBECount);
3300 }
3301 }
3302
3303 // With an icmp, it may be feasible to compute an exact backedge-taken count.
3304 // Procede to the next level to examine the icmp.
3305 if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond))
3306 return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003307
Eli Friedman459d7292009-05-09 12:32:42 +00003308 // If it's not an integer or pointer comparison then compute it the hard way.
Dan Gohman8e8b5232009-06-22 00:31:57 +00003309 return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB));
3310}
3311
3312/// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the
3313/// backedge of the specified loop will execute if its exit condition
3314/// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB.
3315ScalarEvolution::BackedgeTakenInfo
3316ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L,
3317 ICmpInst *ExitCond,
3318 BasicBlock *TBB,
3319 BasicBlock *FBB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003320
3321 // If the condition was exit on true, convert the condition to exit on false
3322 ICmpInst::Predicate Cond;
Dan Gohman8e8b5232009-06-22 00:31:57 +00003323 if (!L->contains(FBB))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003324 Cond = ExitCond->getPredicate();
3325 else
3326 Cond = ExitCond->getInversePredicate();
3327
3328 // Handle common loops like: for (X = "string"; *X; ++X)
3329 if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0)))
3330 if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) {
Dan Gohman161ea032009-07-07 17:06:11 +00003331 const SCEV *ItCnt =
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003332 ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond);
Dan Gohman8e8b5232009-06-22 00:31:57 +00003333 if (!isa<SCEVCouldNotCompute>(ItCnt)) {
3334 unsigned BitWidth = getTypeSizeInBits(ItCnt->getType());
3335 return BackedgeTakenInfo(ItCnt,
3336 isa<SCEVConstant>(ItCnt) ? ItCnt :
3337 getConstant(APInt::getMaxValue(BitWidth)-1));
3338 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003339 }
3340
Dan Gohman161ea032009-07-07 17:06:11 +00003341 const SCEV *LHS = getSCEV(ExitCond->getOperand(0));
3342 const SCEV *RHS = getSCEV(ExitCond->getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003343
3344 // Try to evaluate any dependencies out of the loop.
Dan Gohmanaff14d62009-05-24 23:25:42 +00003345 LHS = getSCEVAtScope(LHS, L);
3346 RHS = getSCEVAtScope(RHS, L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003347
Dan Gohman9bc642f2009-06-24 04:48:43 +00003348 // At this point, we would like to compute how many iterations of the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003349 // loop the predicate will return true for these inputs.
Dan Gohman2d96e352008-09-16 18:52:57 +00003350 if (LHS->isLoopInvariant(L) && !RHS->isLoopInvariant(L)) {
3351 // If there is a loop-invariant, force it into the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003352 std::swap(LHS, RHS);
3353 Cond = ICmpInst::getSwappedPredicate(Cond);
3354 }
3355
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003356 // If we have a comparison of a chrec against a constant, try to use value
3357 // ranges to answer this query.
Dan Gohmanc76b5452009-05-04 22:02:23 +00003358 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
3359 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003360 if (AddRec->getLoop() == L) {
Eli Friedman459d7292009-05-09 12:32:42 +00003361 // Form the constant range.
3362 ConstantRange CompRange(
3363 ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003364
Dan Gohman161ea032009-07-07 17:06:11 +00003365 const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this);
Eli Friedman459d7292009-05-09 12:32:42 +00003366 if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003367 }
3368
3369 switch (Cond) {
3370 case ICmpInst::ICMP_NE: { // while (X != Y)
3371 // Convert to: while (X-Y != 0)
Dan Gohman161ea032009-07-07 17:06:11 +00003372 const SCEV *TC = HowFarToZero(getMinusSCEV(LHS, RHS), L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003373 if (!isa<SCEVCouldNotCompute>(TC)) return TC;
3374 break;
3375 }
3376 case ICmpInst::ICMP_EQ: {
3377 // Convert to: while (X-Y == 0) // while (X == Y)
Dan Gohman161ea032009-07-07 17:06:11 +00003378 const SCEV *TC = HowFarToNonZero(getMinusSCEV(LHS, RHS), L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003379 if (!isa<SCEVCouldNotCompute>(TC)) return TC;
3380 break;
3381 }
3382 case ICmpInst::ICMP_SLT: {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00003383 BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true);
3384 if (BTI.hasAnyInfo()) return BTI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003385 break;
3386 }
3387 case ICmpInst::ICMP_SGT: {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00003388 BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS),
3389 getNotSCEV(RHS), L, true);
3390 if (BTI.hasAnyInfo()) return BTI;
Nick Lewyckyb7c28942007-08-06 19:21:00 +00003391 break;
3392 }
3393 case ICmpInst::ICMP_ULT: {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00003394 BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false);
3395 if (BTI.hasAnyInfo()) return BTI;
Nick Lewyckyb7c28942007-08-06 19:21:00 +00003396 break;
3397 }
3398 case ICmpInst::ICMP_UGT: {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00003399 BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS),
3400 getNotSCEV(RHS), L, false);
3401 if (BTI.hasAnyInfo()) return BTI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003402 break;
3403 }
3404 default:
3405#if 0
Dan Gohman13058cc2009-04-21 00:47:46 +00003406 errs() << "ComputeBackedgeTakenCount ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003407 if (ExitCond->getOperand(0)->getType()->isUnsigned())
Dan Gohman13058cc2009-04-21 00:47:46 +00003408 errs() << "[unsigned] ";
3409 errs() << *LHS << " "
Dan Gohman9bc642f2009-06-24 04:48:43 +00003410 << Instruction::getOpcodeName(Instruction::ICmp)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003411 << " " << *RHS << "\n";
3412#endif
3413 break;
3414 }
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003415 return
Dan Gohman8e8b5232009-06-22 00:31:57 +00003416 ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003417}
3418
3419static ConstantInt *
Dan Gohman89f85052007-10-22 18:31:58 +00003420EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C,
3421 ScalarEvolution &SE) {
Dan Gohman161ea032009-07-07 17:06:11 +00003422 const SCEV *InVal = SE.getConstant(C);
3423 const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003424 assert(isa<SCEVConstant>(Val) &&
3425 "Evaluation of SCEV at constant didn't fold correctly?");
3426 return cast<SCEVConstant>(Val)->getValue();
3427}
3428
3429/// GetAddressedElementFromGlobal - Given a global variable with an initializer
3430/// and a GEP expression (missing the pointer index) indexing into it, return
3431/// the addressed element of the initializer or null if the index expression is
3432/// invalid.
3433static Constant *
Owen Anderson175b6542009-07-22 00:24:57 +00003434GetAddressedElementFromGlobal(LLVMContext &Context, GlobalVariable *GV,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003435 const std::vector<ConstantInt*> &Indices) {
3436 Constant *Init = GV->getInitializer();
3437 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
3438 uint64_t Idx = Indices[i]->getZExtValue();
3439 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) {
3440 assert(Idx < CS->getNumOperands() && "Bad struct index!");
3441 Init = cast<Constant>(CS->getOperand(Idx));
3442 } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) {
3443 if (Idx >= CA->getNumOperands()) return 0; // Bogus program
3444 Init = cast<Constant>(CA->getOperand(Idx));
3445 } else if (isa<ConstantAggregateZero>(Init)) {
3446 if (const StructType *STy = dyn_cast<StructType>(Init->getType())) {
3447 assert(Idx < STy->getNumElements() && "Bad struct index!");
Owen Anderson175b6542009-07-22 00:24:57 +00003448 Init = Context.getNullValue(STy->getElementType(Idx));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003449 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) {
3450 if (Idx >= ATy->getNumElements()) return 0; // Bogus program
Owen Anderson175b6542009-07-22 00:24:57 +00003451 Init = Context.getNullValue(ATy->getElementType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003452 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +00003453 llvm_unreachable("Unknown constant aggregate type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003454 }
3455 return 0;
3456 } else {
3457 return 0; // Unknown initializer type
3458 }
3459 }
3460 return Init;
3461}
3462
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003463/// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of
3464/// 'icmp op load X, cst', try to see if we can compute the backedge
3465/// execution count.
Dan Gohman9bc642f2009-06-24 04:48:43 +00003466const SCEV *
3467ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount(
3468 LoadInst *LI,
3469 Constant *RHS,
3470 const Loop *L,
3471 ICmpInst::Predicate predicate) {
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003472 if (LI->isVolatile()) return getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003473
3474 // Check to see if the loaded pointer is a getelementptr of a global.
3475 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0));
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003476 if (!GEP) return getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003477
3478 // Make sure that it is really a constant global we are gepping, with an
3479 // initializer, and make sure the first IDX is really 0.
3480 GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
3481 if (!GV || !GV->isConstant() || !GV->hasInitializer() ||
3482 GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) ||
3483 !cast<Constant>(GEP->getOperand(1))->isNullValue())
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003484 return getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003485
3486 // Okay, we allow one non-constant index into the GEP instruction.
3487 Value *VarIdx = 0;
3488 std::vector<ConstantInt*> Indexes;
3489 unsigned VarIdxNum = 0;
3490 for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i)
3491 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
3492 Indexes.push_back(CI);
3493 } else if (!isa<ConstantInt>(GEP->getOperand(i))) {
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003494 if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003495 VarIdx = GEP->getOperand(i);
3496 VarIdxNum = i-2;
3497 Indexes.push_back(0);
3498 }
3499
3500 // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant.
3501 // Check to see if X is a loop variant variable value now.
Dan Gohman161ea032009-07-07 17:06:11 +00003502 const SCEV *Idx = getSCEV(VarIdx);
Dan Gohmanaff14d62009-05-24 23:25:42 +00003503 Idx = getSCEVAtScope(Idx, L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003504
3505 // We can only recognize very limited forms of loop index expressions, in
3506 // particular, only affine AddRec's like {C1,+,C2}.
Dan Gohmanbff6b582009-05-04 22:30:44 +00003507 const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003508 if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) ||
3509 !isa<SCEVConstant>(IdxExpr->getOperand(0)) ||
3510 !isa<SCEVConstant>(IdxExpr->getOperand(1)))
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003511 return getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003512
3513 unsigned MaxSteps = MaxBruteForceIterations;
3514 for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
Owen Andersoneacb44d2009-07-24 23:12:02 +00003515 ConstantInt *ItCst = ConstantInt::get(
Owen Anderson9f5b2aa2009-07-14 23:09:55 +00003516 cast<IntegerType>(IdxExpr->getType()), IterationNum);
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003517 ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003518
3519 // Form the GEP offset.
3520 Indexes[VarIdxNum] = Val;
3521
Owen Anderson175b6542009-07-22 00:24:57 +00003522 Constant *Result = GetAddressedElementFromGlobal(getContext(), GV, Indexes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003523 if (Result == 0) break; // Cannot compute!
3524
3525 // Evaluate the condition for this iteration.
3526 Result = ConstantExpr::getICmp(predicate, Result, RHS);
3527 if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure
3528 if (cast<ConstantInt>(Result)->getValue().isMinValue()) {
3529#if 0
Dan Gohman13058cc2009-04-21 00:47:46 +00003530 errs() << "\n***\n*** Computed loop count " << *ItCst
3531 << "\n*** From global " << *GV << "*** BB: " << *L->getHeader()
3532 << "***\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003533#endif
3534 ++NumArrayLenItCounts;
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003535 return getConstant(ItCst); // Found terminating iteration!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003536 }
3537 }
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003538 return getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003539}
3540
3541
3542/// CanConstantFold - Return true if we can constant fold an instruction of the
3543/// specified type, assuming that all operands were constants.
3544static bool CanConstantFold(const Instruction *I) {
3545 if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
3546 isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I))
3547 return true;
3548
3549 if (const CallInst *CI = dyn_cast<CallInst>(I))
3550 if (const Function *F = CI->getCalledFunction())
Dan Gohmane6e001f2008-01-31 01:05:10 +00003551 return canConstantFoldCallTo(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003552 return false;
3553}
3554
3555/// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node
3556/// in the loop that V is derived from. We allow arbitrary operations along the
3557/// way, but the operands of an operation must either be constants or a value
3558/// derived from a constant PHI. If this expression does not fit with these
3559/// constraints, return null.
3560static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) {
3561 // If this is not an instruction, or if this is an instruction outside of the
3562 // loop, it can't be derived from a loop PHI.
3563 Instruction *I = dyn_cast<Instruction>(V);
3564 if (I == 0 || !L->contains(I->getParent())) return 0;
3565
Anton Korobeynikov357a27d2008-02-20 11:08:44 +00003566 if (PHINode *PN = dyn_cast<PHINode>(I)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003567 if (L->getHeader() == I->getParent())
3568 return PN;
3569 else
3570 // We don't currently keep track of the control flow needed to evaluate
3571 // PHIs, so we cannot handle PHIs inside of loops.
3572 return 0;
Anton Korobeynikov357a27d2008-02-20 11:08:44 +00003573 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003574
3575 // If we won't be able to constant fold this expression even if the operands
3576 // are constants, return early.
3577 if (!CanConstantFold(I)) return 0;
3578
3579 // Otherwise, we can evaluate this instruction if all of its operands are
3580 // constant or derived from a PHI node themselves.
3581 PHINode *PHI = 0;
3582 for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op)
3583 if (!(isa<Constant>(I->getOperand(Op)) ||
3584 isa<GlobalValue>(I->getOperand(Op)))) {
3585 PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L);
3586 if (P == 0) return 0; // Not evolving from PHI
3587 if (PHI == 0)
3588 PHI = P;
3589 else if (PHI != P)
3590 return 0; // Evolving from multiple different PHIs.
3591 }
3592
3593 // This is a expression evolving from a constant PHI!
3594 return PHI;
3595}
3596
3597/// EvaluateExpression - Given an expression that passes the
3598/// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node
3599/// in the loop has the value PHIVal. If we can't fold this expression for some
3600/// reason, return null.
3601static Constant *EvaluateExpression(Value *V, Constant *PHIVal) {
3602 if (isa<PHINode>(V)) return PHIVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003603 if (Constant *C = dyn_cast<Constant>(V)) return C;
Dan Gohman01c2ee72009-04-16 03:18:22 +00003604 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003605 Instruction *I = cast<Instruction>(V);
Owen Anderson175b6542009-07-22 00:24:57 +00003606 LLVMContext &Context = I->getParent()->getContext();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003607
3608 std::vector<Constant*> Operands;
3609 Operands.resize(I->getNumOperands());
3610
3611 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
3612 Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal);
3613 if (Operands[i] == 0) return 0;
3614 }
3615
Chris Lattnerd6e56912007-12-10 22:53:04 +00003616 if (const CmpInst *CI = dyn_cast<CmpInst>(I))
3617 return ConstantFoldCompareInstOperands(CI->getPredicate(),
Owen Andersond4d90a02009-07-06 18:42:36 +00003618 &Operands[0], Operands.size(),
3619 Context);
Chris Lattnerd6e56912007-12-10 22:53:04 +00003620 else
3621 return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
Owen Andersond4d90a02009-07-06 18:42:36 +00003622 &Operands[0], Operands.size(),
3623 Context);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003624}
3625
3626/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
3627/// in the header of its containing loop, we know the loop executes a
3628/// constant number of times, and the PHI node is just a recurrence
3629/// involving constants, fold it.
Dan Gohman9bc642f2009-06-24 04:48:43 +00003630Constant *
3631ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN,
3632 const APInt& BEs,
3633 const Loop *L) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003634 std::map<PHINode*, Constant*>::iterator I =
3635 ConstantEvolutionLoopExitValue.find(PN);
3636 if (I != ConstantEvolutionLoopExitValue.end())
3637 return I->second;
3638
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003639 if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003640 return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it.
3641
3642 Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
3643
3644 // Since the loop is canonicalized, the PHI node must have two entries. One
3645 // entry must be a constant (coming in from outside of the loop), and the
3646 // second must be derived from the same PHI.
3647 bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
3648 Constant *StartCST =
3649 dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge));
3650 if (StartCST == 0)
3651 return RetVal = 0; // Must be a constant.
3652
3653 Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
3654 PHINode *PN2 = getConstantEvolvingPHI(BEValue, L);
3655 if (PN2 != PN)
3656 return RetVal = 0; // Not derived from same PHI.
3657
3658 // Execute the loop symbolically to determine the exit value.
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003659 if (BEs.getActiveBits() >= 32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003660 return RetVal = 0; // More than 2^32-1 iterations?? Not doing it!
3661
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003662 unsigned NumIterations = BEs.getZExtValue(); // must be in range
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003663 unsigned IterationNum = 0;
3664 for (Constant *PHIVal = StartCST; ; ++IterationNum) {
3665 if (IterationNum == NumIterations)
3666 return RetVal = PHIVal; // Got exit value!
3667
3668 // Compute the value of the PHI node for the next iteration.
3669 Constant *NextPHI = EvaluateExpression(BEValue, PHIVal);
3670 if (NextPHI == PHIVal)
3671 return RetVal = NextPHI; // Stopped evolving!
3672 if (NextPHI == 0)
3673 return 0; // Couldn't evaluate!
3674 PHIVal = NextPHI;
3675 }
3676}
3677
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003678/// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute a
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003679/// constant number of times (the condition evolves only from constants),
3680/// try to evaluate a few iterations of the loop until we get the exit
3681/// condition gets a value of ExitWhen (true or false). If we cannot
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003682/// evaluate the trip count of the loop, return getCouldNotCompute().
Dan Gohman9bc642f2009-06-24 04:48:43 +00003683const SCEV *
3684ScalarEvolution::ComputeBackedgeTakenCountExhaustively(const Loop *L,
3685 Value *Cond,
3686 bool ExitWhen) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003687 PHINode *PN = getConstantEvolvingPHI(Cond, L);
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003688 if (PN == 0) return getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003689
3690 // Since the loop is canonicalized, the PHI node must have two entries. One
3691 // entry must be a constant (coming in from outside of the loop), and the
3692 // second must be derived from the same PHI.
3693 bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
3694 Constant *StartCST =
3695 dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge));
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003696 if (StartCST == 0) return getCouldNotCompute(); // Must be a constant.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003697
3698 Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
3699 PHINode *PN2 = getConstantEvolvingPHI(BEValue, L);
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003700 if (PN2 != PN) return getCouldNotCompute(); // Not derived from same PHI.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003701
3702 // Okay, we find a PHI node that defines the trip count of this loop. Execute
3703 // the loop symbolically to determine when the condition gets a value of
3704 // "ExitWhen".
3705 unsigned IterationNum = 0;
3706 unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis.
3707 for (Constant *PHIVal = StartCST;
3708 IterationNum != MaxIterations; ++IterationNum) {
3709 ConstantInt *CondVal =
3710 dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal));
3711
3712 // Couldn't symbolically evaluate.
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003713 if (!CondVal) return getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003714
3715 if (CondVal->getValue() == uint64_t(ExitWhen)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003716 ++NumBruteForceTripCountsComputed;
Dan Gohman8fd520a2009-06-15 22:12:54 +00003717 return getConstant(Type::Int32Ty, IterationNum);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003718 }
3719
3720 // Compute the value of the PHI node for the next iteration.
3721 Constant *NextPHI = EvaluateExpression(BEValue, PHIVal);
3722 if (NextPHI == 0 || NextPHI == PHIVal)
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003723 return getCouldNotCompute();// Couldn't evaluate or not making progress...
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003724 PHIVal = NextPHI;
3725 }
3726
3727 // Too many iterations were needed to evaluate.
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003728 return getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003729}
3730
Dan Gohmandd40e9a2009-05-08 20:38:54 +00003731/// getSCEVAtScope - Return a SCEV expression handle for the specified value
3732/// at the specified scope in the program. The L value specifies a loop
3733/// nest to evaluate the expression at, where null is the top-level or a
3734/// specified loop is immediately inside of the loop.
3735///
3736/// This method can be used to compute the exit value for a variable defined
3737/// in a loop by querying what the value will hold in the parent loop.
3738///
Dan Gohmanaff14d62009-05-24 23:25:42 +00003739/// In the case that a relevant loop exit value cannot be computed, the
3740/// original value V is returned.
Dan Gohman161ea032009-07-07 17:06:11 +00003741const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003742 // FIXME: this should be turned into a virtual method on SCEV!
3743
3744 if (isa<SCEVConstant>(V)) return V;
3745
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00003746 // If this instruction is evolved from a constant-evolving PHI, compute the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003747 // exit value from the loop without using SCEVs.
Dan Gohmanc76b5452009-05-04 22:02:23 +00003748 if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003749 if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) {
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003750 const Loop *LI = (*this->LI)[I->getParent()];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003751 if (LI && LI->getParentLoop() == L) // Looking for loop exit value.
3752 if (PHINode *PN = dyn_cast<PHINode>(I))
3753 if (PN->getParent() == LI->getHeader()) {
3754 // Okay, there is no closed form solution for the PHI node. Check
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003755 // to see if the loop that contains it has a known backedge-taken
3756 // count. If so, we may be able to force computation of the exit
3757 // value.
Dan Gohman161ea032009-07-07 17:06:11 +00003758 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI);
Dan Gohmanc76b5452009-05-04 22:02:23 +00003759 if (const SCEVConstant *BTCC =
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003760 dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003761 // Okay, we know how many times the containing loop executes. If
3762 // this is a constant evolving PHI node, get the final value at
3763 // the specified iteration number.
3764 Constant *RV = getConstantEvolutionLoopExitValue(PN,
Dan Gohman76d5a0d2009-02-24 18:55:53 +00003765 BTCC->getValue()->getValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003766 LI);
Dan Gohman652caf12009-06-29 21:31:18 +00003767 if (RV) return getSCEV(RV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003768 }
3769 }
3770
3771 // Okay, this is an expression that we cannot symbolically evaluate
3772 // into a SCEV. Check to see if it's possible to symbolically evaluate
3773 // the arguments into constants, and if so, try to constant propagate the
3774 // result. This is particularly useful for computing loop exit values.
3775 if (CanConstantFold(I)) {
Dan Gohmanda0071e2009-05-08 20:47:27 +00003776 // Check to see if we've folded this instruction at this loop before.
3777 std::map<const Loop *, Constant *> &Values = ValuesAtScopes[I];
3778 std::pair<std::map<const Loop *, Constant *>::iterator, bool> Pair =
3779 Values.insert(std::make_pair(L, static_cast<Constant *>(0)));
3780 if (!Pair.second)
Dan Gohman652caf12009-06-29 21:31:18 +00003781 return Pair.first->second ? &*getSCEV(Pair.first->second) : V;
Dan Gohmanda0071e2009-05-08 20:47:27 +00003782
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003783 std::vector<Constant*> Operands;
3784 Operands.reserve(I->getNumOperands());
3785 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
3786 Value *Op = I->getOperand(i);
3787 if (Constant *C = dyn_cast<Constant>(Op)) {
3788 Operands.push_back(C);
3789 } else {
Chris Lattner3fff4642007-11-23 08:46:22 +00003790 // If any of the operands is non-constant and if they are
Dan Gohman01c2ee72009-04-16 03:18:22 +00003791 // non-integer and non-pointer, don't even try to analyze them
3792 // with scev techniques.
Dan Gohman5e4eb762009-04-30 16:40:30 +00003793 if (!isSCEVable(Op->getType()))
Chris Lattner3fff4642007-11-23 08:46:22 +00003794 return V;
Dan Gohman01c2ee72009-04-16 03:18:22 +00003795
Dan Gohman55e2d7e2009-07-13 21:35:55 +00003796 const SCEV* OpV = getSCEVAtScope(Op, L);
Dan Gohmanc76b5452009-05-04 22:02:23 +00003797 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) {
Dan Gohman5e4eb762009-04-30 16:40:30 +00003798 Constant *C = SC->getValue();
3799 if (C->getType() != Op->getType())
3800 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
3801 Op->getType(),
3802 false),
3803 C, Op->getType());
3804 Operands.push_back(C);
Dan Gohmanc76b5452009-05-04 22:02:23 +00003805 } else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) {
Dan Gohman5e4eb762009-04-30 16:40:30 +00003806 if (Constant *C = dyn_cast<Constant>(SU->getValue())) {
3807 if (C->getType() != Op->getType())
3808 C =
3809 ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
3810 Op->getType(),
3811 false),
3812 C, Op->getType());
3813 Operands.push_back(C);
3814 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003815 return V;
3816 } else {
3817 return V;
3818 }
3819 }
3820 }
Dan Gohman9bc642f2009-06-24 04:48:43 +00003821
Chris Lattnerd6e56912007-12-10 22:53:04 +00003822 Constant *C;
3823 if (const CmpInst *CI = dyn_cast<CmpInst>(I))
3824 C = ConstantFoldCompareInstOperands(CI->getPredicate(),
Owen Andersond4d90a02009-07-06 18:42:36 +00003825 &Operands[0], Operands.size(),
Owen Anderson175b6542009-07-22 00:24:57 +00003826 getContext());
Chris Lattnerd6e56912007-12-10 22:53:04 +00003827 else
3828 C = ConstantFoldInstOperands(I->getOpcode(), I->getType(),
Owen Anderson175b6542009-07-22 00:24:57 +00003829 &Operands[0], Operands.size(),
3830 getContext());
Dan Gohmanda0071e2009-05-08 20:47:27 +00003831 Pair.first->second = C;
Dan Gohman652caf12009-06-29 21:31:18 +00003832 return getSCEV(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003833 }
3834 }
3835
3836 // This is some other type of SCEVUnknown, just return it.
3837 return V;
3838 }
3839
Dan Gohmanc76b5452009-05-04 22:02:23 +00003840 if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003841 // Avoid performing the look-up in the common case where the specified
3842 // expression has no loop-variant portions.
3843 for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) {
Dan Gohman161ea032009-07-07 17:06:11 +00003844 const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003845 if (OpAtScope != Comm->getOperand(i)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003846 // Okay, at least one of these operands is loop variant but might be
3847 // foldable. Build a new instance of the folded commutative expression.
Dan Gohman9bc642f2009-06-24 04:48:43 +00003848 SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(),
3849 Comm->op_begin()+i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003850 NewOps.push_back(OpAtScope);
3851
3852 for (++i; i != e; ++i) {
3853 OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003854 NewOps.push_back(OpAtScope);
3855 }
3856 if (isa<SCEVAddExpr>(Comm))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003857 return getAddExpr(NewOps);
Nick Lewycky711640a2007-11-25 22:41:31 +00003858 if (isa<SCEVMulExpr>(Comm))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003859 return getMulExpr(NewOps);
Nick Lewycky711640a2007-11-25 22:41:31 +00003860 if (isa<SCEVSMaxExpr>(Comm))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003861 return getSMaxExpr(NewOps);
Nick Lewyckye7a24ff2008-02-20 06:48:22 +00003862 if (isa<SCEVUMaxExpr>(Comm))
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003863 return getUMaxExpr(NewOps);
Edwin Törökbd448e32009-07-14 16:55:14 +00003864 llvm_unreachable("Unknown commutative SCEV type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003865 }
3866 }
3867 // If we got here, all operands are loop invariant.
3868 return Comm;
3869 }
3870
Dan Gohmanc76b5452009-05-04 22:02:23 +00003871 if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) {
Dan Gohman161ea032009-07-07 17:06:11 +00003872 const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L);
3873 const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L);
Nick Lewycky35b56022009-01-13 09:18:58 +00003874 if (LHS == Div->getLHS() && RHS == Div->getRHS())
3875 return Div; // must be loop invariant
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003876 return getUDivExpr(LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003877 }
3878
3879 // If this is a loop recurrence for a loop that does not contain L, then we
3880 // are dealing with the final value computed by the loop.
Dan Gohmanc76b5452009-05-04 22:02:23 +00003881 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003882 if (!L || !AddRec->getLoop()->contains(L->getHeader())) {
3883 // To evaluate this recurrence, we need to know how many times the AddRec
3884 // loop iterates. Compute this now.
Dan Gohman161ea032009-07-07 17:06:11 +00003885 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop());
Dan Gohmanc6475cb2009-06-27 21:21:31 +00003886 if (BackedgeTakenCount == getCouldNotCompute()) return AddRec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003887
Eli Friedman7489ec92008-08-04 23:49:06 +00003888 // Then, evaluate the AddRec.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003889 return AddRec->evaluateAtIteration(BackedgeTakenCount, *this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003890 }
Dan Gohmanaff14d62009-05-24 23:25:42 +00003891 return AddRec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003892 }
3893
Dan Gohmanc76b5452009-05-04 22:02:23 +00003894 if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) {
Dan Gohman161ea032009-07-07 17:06:11 +00003895 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohman78d63c82009-04-29 22:29:01 +00003896 if (Op == Cast->getOperand())
3897 return Cast; // must be loop invariant
3898 return getZeroExtendExpr(Op, Cast->getType());
3899 }
3900
Dan Gohmanc76b5452009-05-04 22:02:23 +00003901 if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) {
Dan Gohman161ea032009-07-07 17:06:11 +00003902 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohman78d63c82009-04-29 22:29:01 +00003903 if (Op == Cast->getOperand())
3904 return Cast; // must be loop invariant
3905 return getSignExtendExpr(Op, Cast->getType());
3906 }
3907
Dan Gohmanc76b5452009-05-04 22:02:23 +00003908 if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) {
Dan Gohman161ea032009-07-07 17:06:11 +00003909 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohman78d63c82009-04-29 22:29:01 +00003910 if (Op == Cast->getOperand())
3911 return Cast; // must be loop invariant
3912 return getTruncateExpr(Op, Cast->getType());
3913 }
3914
Edwin Törökbd448e32009-07-14 16:55:14 +00003915 llvm_unreachable("Unknown SCEV type!");
Daniel Dunbara95d96c2009-05-18 16:43:04 +00003916 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003917}
3918
Dan Gohmandd40e9a2009-05-08 20:38:54 +00003919/// getSCEVAtScope - This is a convenience function which does
3920/// getSCEVAtScope(getSCEV(V), L).
Dan Gohman161ea032009-07-07 17:06:11 +00003921const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) {
Dan Gohmanffd36ba2009-04-21 23:15:49 +00003922 return getSCEVAtScope(getSCEV(V), L);
3923}
3924
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00003925/// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the
3926/// following equation:
3927///
3928/// A * X = B (mod N)
3929///
3930/// where N = 2^BW and BW is the common bit width of A and B. The signedness of
3931/// A and B isn't important.
3932///
3933/// If the equation does not have a solution, SCEVCouldNotCompute is returned.
Dan Gohman161ea032009-07-07 17:06:11 +00003934static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const APInt &B,
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00003935 ScalarEvolution &SE) {
3936 uint32_t BW = A.getBitWidth();
3937 assert(BW == B.getBitWidth() && "Bit widths must be the same.");
3938 assert(A != 0 && "A must be non-zero.");
3939
3940 // 1. D = gcd(A, N)
3941 //
3942 // The gcd of A and N may have only one prime factor: 2. The number of
3943 // trailing zeros in A is its multiplicity
3944 uint32_t Mult2 = A.countTrailingZeros();
3945 // D = 2^Mult2
3946
3947 // 2. Check if B is divisible by D.
3948 //
3949 // B is divisible by D if and only if the multiplicity of prime factor 2 for B
3950 // is not less than multiplicity of this prime factor for D.
3951 if (B.countTrailingZeros() < Mult2)
Dan Gohman0ad08b02009-04-18 17:58:19 +00003952 return SE.getCouldNotCompute();
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00003953
3954 // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic
3955 // modulo (N / D).
3956 //
3957 // (N / D) may need BW+1 bits in its representation. Hence, we'll use this
3958 // bit width during computations.
3959 APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D
3960 APInt Mod(BW + 1, 0);
3961 Mod.set(BW - Mult2); // Mod = N / D
3962 APInt I = AD.multiplicativeInverse(Mod);
3963
3964 // 4. Compute the minimum unsigned root of the equation:
3965 // I * (B / D) mod (N / D)
3966 APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod);
3967
3968 // The result is guaranteed to be less than 2^BW so we may truncate it to BW
3969 // bits.
3970 return SE.getConstant(Result.trunc(BW));
3971}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003972
3973/// SolveQuadraticEquation - Find the roots of the quadratic equation for the
3974/// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which
3975/// might be the same) or two SCEVCouldNotCompute objects.
3976///
Dan Gohman161ea032009-07-07 17:06:11 +00003977static std::pair<const SCEV *,const SCEV *>
Dan Gohman89f85052007-10-22 18:31:58 +00003978SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003979 assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
Dan Gohmanbff6b582009-05-04 22:30:44 +00003980 const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
3981 const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
3982 const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003983
3984 // We currently can only solve this if the coefficients are constants.
3985 if (!LC || !MC || !NC) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00003986 const SCEV *CNC = SE.getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003987 return std::make_pair(CNC, CNC);
3988 }
3989
3990 uint32_t BitWidth = LC->getValue()->getValue().getBitWidth();
3991 const APInt &L = LC->getValue()->getValue();
3992 const APInt &M = MC->getValue()->getValue();
3993 const APInt &N = NC->getValue()->getValue();
3994 APInt Two(BitWidth, 2);
3995 APInt Four(BitWidth, 4);
3996
Dan Gohman9bc642f2009-06-24 04:48:43 +00003997 {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003998 using namespace APIntOps;
3999 const APInt& C = L;
4000 // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C
4001 // The B coefficient is M-N/2
4002 APInt B(M);
4003 B -= sdiv(N,Two);
4004
4005 // The A coefficient is N/2
4006 APInt A(N.sdiv(Two));
4007
4008 // Compute the B^2-4ac term.
4009 APInt SqrtTerm(B);
4010 SqrtTerm *= B;
4011 SqrtTerm -= Four * (A * C);
4012
4013 // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest
4014 // integer value or else APInt::sqrt() will assert.
4015 APInt SqrtVal(SqrtTerm.sqrt());
4016
Dan Gohman9bc642f2009-06-24 04:48:43 +00004017 // Compute the two solutions for the quadratic formula.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004018 // The divisions must be performed as signed divisions.
4019 APInt NegB(-B);
4020 APInt TwoA( A << 1 );
Nick Lewycky35776692008-11-03 02:43:49 +00004021 if (TwoA.isMinValue()) {
Dan Gohmanbff6b582009-05-04 22:30:44 +00004022 const SCEV *CNC = SE.getCouldNotCompute();
Nick Lewycky35776692008-11-03 02:43:49 +00004023 return std::make_pair(CNC, CNC);
4024 }
4025
Owen Anderson175b6542009-07-22 00:24:57 +00004026 LLVMContext &Context = SE.getContext();
Owen Andersone755b092009-07-06 22:37:39 +00004027
4028 ConstantInt *Solution1 =
Owen Andersoneacb44d2009-07-24 23:12:02 +00004029 ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA));
Owen Andersone755b092009-07-06 22:37:39 +00004030 ConstantInt *Solution2 =
Owen Andersoneacb44d2009-07-24 23:12:02 +00004031 ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004032
Dan Gohman9bc642f2009-06-24 04:48:43 +00004033 return std::make_pair(SE.getConstant(Solution1),
Dan Gohman89f85052007-10-22 18:31:58 +00004034 SE.getConstant(Solution2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004035 } // end APIntOps namespace
4036}
4037
4038/// HowFarToZero - Return the number of times a backedge comparing the specified
Dan Gohman0c850912009-06-06 14:37:11 +00004039/// value to zero will execute. If not computable, return CouldNotCompute.
Dan Gohman161ea032009-07-07 17:06:11 +00004040const SCEV *ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004041 // If the value is a constant
Dan Gohmanc76b5452009-05-04 22:02:23 +00004042 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004043 // If the value is already zero, the branch will execute zero times.
4044 if (C->getValue()->isZero()) return C;
Dan Gohmanc6475cb2009-06-27 21:21:31 +00004045 return getCouldNotCompute(); // Otherwise it will loop infinitely.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004046 }
4047
Dan Gohmanbff6b582009-05-04 22:30:44 +00004048 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004049 if (!AddRec || AddRec->getLoop() != L)
Dan Gohmanc6475cb2009-06-27 21:21:31 +00004050 return getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004051
4052 if (AddRec->isAffine()) {
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00004053 // If this is an affine expression, the execution count of this branch is
4054 // the minimum unsigned root of the following equation:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004055 //
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00004056 // Start + Step*N = 0 (mod 2^BW)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004057 //
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00004058 // equivalent to:
4059 //
4060 // Step*N = -Start (mod 2^BW)
4061 //
4062 // where BW is the common bit width of Start and Step.
4063
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004064 // Get the initial value for the loop.
Dan Gohman9bc642f2009-06-24 04:48:43 +00004065 const SCEV *Start = getSCEVAtScope(AddRec->getStart(),
4066 L->getParentLoop());
4067 const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1),
4068 L->getParentLoop());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004069
Dan Gohmanc76b5452009-05-04 22:02:23 +00004070 if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) {
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00004071 // For now we handle only constant steps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004072
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00004073 // First, handle unitary steps.
4074 if (StepC->getValue()->equalsInt(1)) // 1*N = -Start (mod 2^BW), so:
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004075 return getNegativeSCEV(Start); // N = -Start (as unsigned)
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00004076 if (StepC->getValue()->isAllOnesValue()) // -1*N = -Start (mod 2^BW), so:
4077 return Start; // N = Start (as unsigned)
4078
4079 // Then, try to solve the above equation provided that Start is constant.
Dan Gohmanc76b5452009-05-04 22:02:23 +00004080 if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start))
Wojciech Matyjewicz961b34c2008-07-20 15:55:14 +00004081 return SolveLinEquationWithOverflow(StepC->getValue()->getValue(),
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004082 -StartC->getValue()->getValue(),
4083 *this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004084 }
4085 } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) {
4086 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
4087 // the quadratic equation to solve it.
Dan Gohman161ea032009-07-07 17:06:11 +00004088 std::pair<const SCEV *,const SCEV *> Roots = SolveQuadraticEquation(AddRec,
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004089 *this);
Dan Gohmanbff6b582009-05-04 22:30:44 +00004090 const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
4091 const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004092 if (R1) {
4093#if 0
Dan Gohman13058cc2009-04-21 00:47:46 +00004094 errs() << "HFTZ: " << *V << " - sol#1: " << *R1
4095 << " sol#2: " << *R2 << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004096#endif
4097 // Pick the smallest positive root value.
4098 if (ConstantInt *CB =
Owen Anderson175b6542009-07-22 00:24:57 +00004099 dyn_cast<ConstantInt>(getContext().getConstantExprICmp(ICmpInst::ICMP_ULT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004100 R1->getValue(), R2->getValue()))) {
4101 if (CB->getZExtValue() == false)
4102 std::swap(R1, R2); // R1 is the minimum root now.
4103
4104 // We can only use this value if the chrec ends up with an exact zero
4105 // value at this index. When solving for "X*X != 5", for example, we
4106 // should not accept a root of 2.
Dan Gohman161ea032009-07-07 17:06:11 +00004107 const SCEV *Val = AddRec->evaluateAtIteration(R1, *this);
Dan Gohman7b560c42008-06-18 16:23:07 +00004108 if (Val->isZero())
4109 return R1; // We found a quadratic root!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004110 }
4111 }
4112 }
4113
Dan Gohmanc6475cb2009-06-27 21:21:31 +00004114 return getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004115}
4116
4117/// HowFarToNonZero - Return the number of times a backedge checking the
4118/// specified value for nonzero will execute. If not computable, return
Dan Gohman0c850912009-06-06 14:37:11 +00004119/// CouldNotCompute
Dan Gohman161ea032009-07-07 17:06:11 +00004120const SCEV *ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004121 // Loops that look like: while (X == 0) are very strange indeed. We don't
4122 // handle them yet except for the trivial case. This could be expanded in the
4123 // future as needed.
4124
4125 // If the value is a constant, check to see if it is known to be non-zero
4126 // already. If so, the backedge will execute zero times.
Dan Gohmanc76b5452009-05-04 22:02:23 +00004127 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
Nick Lewyckyf6805182008-02-21 09:14:53 +00004128 if (!C->getValue()->isNullValue())
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004129 return getIntegerSCEV(0, C->getType());
Dan Gohmanc6475cb2009-06-27 21:21:31 +00004130 return getCouldNotCompute(); // Otherwise it will loop infinitely.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004131 }
4132
4133 // We could implement others, but I really doubt anyone writes loops like
4134 // this, and if they did, they would already be constant folded.
Dan Gohmanc6475cb2009-06-27 21:21:31 +00004135 return getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004136}
4137
Dan Gohmanab157b22009-05-18 15:36:09 +00004138/// getLoopPredecessor - If the given loop's header has exactly one unique
4139/// predecessor outside the loop, return it. Otherwise return null.
4140///
4141BasicBlock *ScalarEvolution::getLoopPredecessor(const Loop *L) {
4142 BasicBlock *Header = L->getHeader();
4143 BasicBlock *Pred = 0;
4144 for (pred_iterator PI = pred_begin(Header), E = pred_end(Header);
4145 PI != E; ++PI)
4146 if (!L->contains(*PI)) {
4147 if (Pred && Pred != *PI) return 0; // Multiple predecessors.
4148 Pred = *PI;
4149 }
4150 return Pred;
4151}
4152
Dan Gohman1cddf972008-09-15 22:18:04 +00004153/// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
4154/// (which may not be an immediate predecessor) which has exactly one
4155/// successor from which BB is reachable, or null if no such block is
4156/// found.
4157///
4158BasicBlock *
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004159ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
Dan Gohman1116ea72009-04-30 20:48:53 +00004160 // If the block has a unique predecessor, then there is no path from the
4161 // predecessor to the block that does not go through the direct edge
4162 // from the predecessor to the block.
Dan Gohman1cddf972008-09-15 22:18:04 +00004163 if (BasicBlock *Pred = BB->getSinglePredecessor())
4164 return Pred;
4165
4166 // A loop's header is defined to be a block that dominates the loop.
Dan Gohmanab157b22009-05-18 15:36:09 +00004167 // If the header has a unique predecessor outside the loop, it must be
4168 // a block that has exactly one successor that can reach the loop.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004169 if (Loop *L = LI->getLoopFor(BB))
Dan Gohmanab157b22009-05-18 15:36:09 +00004170 return getLoopPredecessor(L);
Dan Gohman1cddf972008-09-15 22:18:04 +00004171
4172 return 0;
4173}
4174
Dan Gohmanbc1e3472009-06-20 00:35:32 +00004175/// HasSameValue - SCEV structural equivalence is usually sufficient for
4176/// testing whether two expressions are equal, however for the purposes of
4177/// looking for a condition guarding a loop, it can be useful to be a little
4178/// more general, since a front-end may have replicated the controlling
4179/// expression.
4180///
Dan Gohman161ea032009-07-07 17:06:11 +00004181static bool HasSameValue(const SCEV *A, const SCEV *B) {
Dan Gohmanbc1e3472009-06-20 00:35:32 +00004182 // Quick check to see if they are the same SCEV.
4183 if (A == B) return true;
4184
4185 // Otherwise, if they're both SCEVUnknown, it's possible that they hold
4186 // two different instructions with the same value. Check for this case.
4187 if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A))
4188 if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B))
4189 if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue()))
4190 if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue()))
4191 if (AI->isIdenticalTo(BI))
4192 return true;
4193
4194 // Otherwise assume they may have a different value.
4195 return false;
4196}
4197
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004198bool ScalarEvolution::isKnownNegative(const SCEV *S) {
4199 return getSignedRange(S).getSignedMax().isNegative();
4200}
4201
4202bool ScalarEvolution::isKnownPositive(const SCEV *S) {
4203 return getSignedRange(S).getSignedMin().isStrictlyPositive();
4204}
4205
4206bool ScalarEvolution::isKnownNonNegative(const SCEV *S) {
4207 return !getSignedRange(S).getSignedMin().isNegative();
4208}
4209
4210bool ScalarEvolution::isKnownNonPositive(const SCEV *S) {
4211 return !getSignedRange(S).getSignedMax().isStrictlyPositive();
4212}
4213
4214bool ScalarEvolution::isKnownNonZero(const SCEV *S) {
4215 return isKnownNegative(S) || isKnownPositive(S);
4216}
4217
4218bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred,
4219 const SCEV *LHS, const SCEV *RHS) {
4220
4221 if (HasSameValue(LHS, RHS))
4222 return ICmpInst::isTrueWhenEqual(Pred);
4223
4224 switch (Pred) {
4225 default:
Dan Gohman2d4f5b12009-07-16 17:34:36 +00004226 llvm_unreachable("Unexpected ICmpInst::Predicate value!");
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004227 break;
4228 case ICmpInst::ICMP_SGT:
4229 Pred = ICmpInst::ICMP_SLT;
4230 std::swap(LHS, RHS);
4231 case ICmpInst::ICMP_SLT: {
4232 ConstantRange LHSRange = getSignedRange(LHS);
4233 ConstantRange RHSRange = getSignedRange(RHS);
4234 if (LHSRange.getSignedMax().slt(RHSRange.getSignedMin()))
4235 return true;
4236 if (LHSRange.getSignedMin().sge(RHSRange.getSignedMax()))
4237 return false;
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004238 break;
4239 }
4240 case ICmpInst::ICMP_SGE:
4241 Pred = ICmpInst::ICMP_SLE;
4242 std::swap(LHS, RHS);
4243 case ICmpInst::ICMP_SLE: {
4244 ConstantRange LHSRange = getSignedRange(LHS);
4245 ConstantRange RHSRange = getSignedRange(RHS);
4246 if (LHSRange.getSignedMax().sle(RHSRange.getSignedMin()))
4247 return true;
4248 if (LHSRange.getSignedMin().sgt(RHSRange.getSignedMax()))
4249 return false;
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004250 break;
4251 }
4252 case ICmpInst::ICMP_UGT:
4253 Pred = ICmpInst::ICMP_ULT;
4254 std::swap(LHS, RHS);
4255 case ICmpInst::ICMP_ULT: {
4256 ConstantRange LHSRange = getUnsignedRange(LHS);
4257 ConstantRange RHSRange = getUnsignedRange(RHS);
4258 if (LHSRange.getUnsignedMax().ult(RHSRange.getUnsignedMin()))
4259 return true;
4260 if (LHSRange.getUnsignedMin().uge(RHSRange.getUnsignedMax()))
4261 return false;
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004262 break;
4263 }
4264 case ICmpInst::ICMP_UGE:
4265 Pred = ICmpInst::ICMP_ULE;
4266 std::swap(LHS, RHS);
4267 case ICmpInst::ICMP_ULE: {
4268 ConstantRange LHSRange = getUnsignedRange(LHS);
4269 ConstantRange RHSRange = getUnsignedRange(RHS);
4270 if (LHSRange.getUnsignedMax().ule(RHSRange.getUnsignedMin()))
4271 return true;
4272 if (LHSRange.getUnsignedMin().ugt(RHSRange.getUnsignedMax()))
4273 return false;
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004274 break;
4275 }
4276 case ICmpInst::ICMP_NE: {
4277 if (getUnsignedRange(LHS).intersectWith(getUnsignedRange(RHS)).isEmptySet())
4278 return true;
4279 if (getSignedRange(LHS).intersectWith(getSignedRange(RHS)).isEmptySet())
4280 return true;
4281
4282 const SCEV *Diff = getMinusSCEV(LHS, RHS);
4283 if (isKnownNonZero(Diff))
4284 return true;
4285 break;
4286 }
4287 case ICmpInst::ICMP_EQ:
Dan Gohman44e675f2009-07-20 23:54:43 +00004288 // The check at the top of the function catches the case where
4289 // the values are known to be equal.
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004290 break;
4291 }
4292 return false;
4293}
4294
4295/// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
4296/// protected by a conditional between LHS and RHS. This is used to
4297/// to eliminate casts.
4298bool
4299ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L,
4300 ICmpInst::Predicate Pred,
4301 const SCEV *LHS, const SCEV *RHS) {
4302 // Interpret a null as meaning no loop, where there is obviously no guard
4303 // (interprocedural conditions notwithstanding).
4304 if (!L) return true;
4305
4306 BasicBlock *Latch = L->getLoopLatch();
4307 if (!Latch)
4308 return false;
4309
4310 BranchInst *LoopContinuePredicate =
4311 dyn_cast<BranchInst>(Latch->getTerminator());
4312 if (!LoopContinuePredicate ||
4313 LoopContinuePredicate->isUnconditional())
4314 return false;
4315
Dan Gohman920446d2009-07-21 23:03:19 +00004316 return isImpliedCond(LoopContinuePredicate->getCondition(), Pred, LHS, RHS,
4317 LoopContinuePredicate->getSuccessor(0) != L->getHeader());
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004318}
4319
4320/// isLoopGuardedByCond - Test whether entry to the loop is protected
4321/// by a conditional between LHS and RHS. This is used to help avoid max
4322/// expressions in loop trip counts, and to eliminate casts.
4323bool
4324ScalarEvolution::isLoopGuardedByCond(const Loop *L,
4325 ICmpInst::Predicate Pred,
4326 const SCEV *LHS, const SCEV *RHS) {
Dan Gohman8b938182009-05-18 16:03:58 +00004327 // Interpret a null as meaning no loop, where there is obviously no guard
4328 // (interprocedural conditions notwithstanding).
4329 if (!L) return false;
4330
Dan Gohmanab157b22009-05-18 15:36:09 +00004331 BasicBlock *Predecessor = getLoopPredecessor(L);
4332 BasicBlock *PredecessorDest = L->getHeader();
Nick Lewycky1b020bf2008-07-12 07:41:32 +00004333
Dan Gohmanab157b22009-05-18 15:36:09 +00004334 // Starting at the loop predecessor, climb up the predecessor chain, as long
4335 // as there are predecessors that can be found that have unique successors
Dan Gohman1cddf972008-09-15 22:18:04 +00004336 // leading to the original header.
Dan Gohmanab157b22009-05-18 15:36:09 +00004337 for (; Predecessor;
4338 PredecessorDest = Predecessor,
4339 Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) {
Dan Gohmanab678fb2008-08-12 20:17:31 +00004340
4341 BranchInst *LoopEntryPredicate =
Dan Gohmanab157b22009-05-18 15:36:09 +00004342 dyn_cast<BranchInst>(Predecessor->getTerminator());
Dan Gohmanab678fb2008-08-12 20:17:31 +00004343 if (!LoopEntryPredicate ||
4344 LoopEntryPredicate->isUnconditional())
4345 continue;
4346
Dan Gohman920446d2009-07-21 23:03:19 +00004347 if (isImpliedCond(LoopEntryPredicate->getCondition(), Pred, LHS, RHS,
4348 LoopEntryPredicate->getSuccessor(0) != PredecessorDest))
Dan Gohmanab678fb2008-08-12 20:17:31 +00004349 return true;
Nick Lewycky1b020bf2008-07-12 07:41:32 +00004350 }
4351
Dan Gohmanab678fb2008-08-12 20:17:31 +00004352 return false;
Nick Lewycky1b020bf2008-07-12 07:41:32 +00004353}
4354
Dan Gohman920446d2009-07-21 23:03:19 +00004355/// isImpliedCond - Test whether the condition described by Pred, LHS,
4356/// and RHS is true whenever the given Cond value evaluates to true.
4357bool ScalarEvolution::isImpliedCond(Value *CondValue,
4358 ICmpInst::Predicate Pred,
4359 const SCEV *LHS, const SCEV *RHS,
4360 bool Inverse) {
Dan Gohman423ed6c2009-06-24 01:18:18 +00004361 // Recursivly handle And and Or conditions.
4362 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CondValue)) {
4363 if (BO->getOpcode() == Instruction::And) {
4364 if (!Inverse)
Dan Gohman920446d2009-07-21 23:03:19 +00004365 return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) ||
4366 isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse);
Dan Gohman423ed6c2009-06-24 01:18:18 +00004367 } else if (BO->getOpcode() == Instruction::Or) {
4368 if (Inverse)
Dan Gohman920446d2009-07-21 23:03:19 +00004369 return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) ||
4370 isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse);
Dan Gohman423ed6c2009-06-24 01:18:18 +00004371 }
4372 }
4373
4374 ICmpInst *ICI = dyn_cast<ICmpInst>(CondValue);
4375 if (!ICI) return false;
4376
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004377 // Bail if the ICmp's operands' types are wider than the needed type
4378 // before attempting to call getSCEV on them. This avoids infinite
4379 // recursion, since the analysis of widening casts can require loop
4380 // exit condition information for overflow checking, which would
4381 // lead back here.
4382 if (getTypeSizeInBits(LHS->getType()) <
Dan Gohman920446d2009-07-21 23:03:19 +00004383 getTypeSizeInBits(ICI->getOperand(0)->getType()))
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004384 return false;
4385
Dan Gohman920446d2009-07-21 23:03:19 +00004386 // Now that we found a conditional branch that dominates the loop, check to
4387 // see if it is the comparison we are looking for.
4388 ICmpInst::Predicate FoundPred;
4389 if (Inverse)
4390 FoundPred = ICI->getInversePredicate();
4391 else
4392 FoundPred = ICI->getPredicate();
4393
4394 const SCEV *FoundLHS = getSCEV(ICI->getOperand(0));
4395 const SCEV *FoundRHS = getSCEV(ICI->getOperand(1));
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004396
4397 // Balance the types. The case where FoundLHS' type is wider than
4398 // LHS' type is checked for above.
4399 if (getTypeSizeInBits(LHS->getType()) >
4400 getTypeSizeInBits(FoundLHS->getType())) {
4401 if (CmpInst::isSigned(Pred)) {
4402 FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType());
4403 FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType());
4404 } else {
4405 FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType());
4406 FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType());
4407 }
4408 }
4409
Dan Gohman920446d2009-07-21 23:03:19 +00004410 // Canonicalize the query to match the way instcombine will have
4411 // canonicalized the comparison.
4412 // First, put a constant operand on the right.
4413 if (isa<SCEVConstant>(LHS)) {
4414 std::swap(LHS, RHS);
4415 Pred = ICmpInst::getSwappedPredicate(Pred);
4416 }
4417 // Then, canonicalize comparisons with boundary cases.
4418 if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) {
4419 const APInt &RA = RC->getValue()->getValue();
4420 switch (Pred) {
4421 default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
4422 case ICmpInst::ICMP_EQ:
4423 case ICmpInst::ICMP_NE:
4424 break;
4425 case ICmpInst::ICMP_UGE:
4426 if ((RA - 1).isMinValue()) {
4427 Pred = ICmpInst::ICMP_NE;
4428 RHS = getConstant(RA - 1);
4429 break;
4430 }
4431 if (RA.isMaxValue()) {
4432 Pred = ICmpInst::ICMP_EQ;
4433 break;
4434 }
4435 if (RA.isMinValue()) return true;
4436 break;
4437 case ICmpInst::ICMP_ULE:
4438 if ((RA + 1).isMaxValue()) {
4439 Pred = ICmpInst::ICMP_NE;
4440 RHS = getConstant(RA + 1);
4441 break;
4442 }
4443 if (RA.isMinValue()) {
4444 Pred = ICmpInst::ICMP_EQ;
4445 break;
4446 }
4447 if (RA.isMaxValue()) return true;
4448 break;
4449 case ICmpInst::ICMP_SGE:
4450 if ((RA - 1).isMinSignedValue()) {
4451 Pred = ICmpInst::ICMP_NE;
4452 RHS = getConstant(RA - 1);
4453 break;
4454 }
4455 if (RA.isMaxSignedValue()) {
4456 Pred = ICmpInst::ICMP_EQ;
4457 break;
4458 }
4459 if (RA.isMinSignedValue()) return true;
4460 break;
4461 case ICmpInst::ICMP_SLE:
4462 if ((RA + 1).isMaxSignedValue()) {
4463 Pred = ICmpInst::ICMP_NE;
4464 RHS = getConstant(RA + 1);
4465 break;
4466 }
4467 if (RA.isMinSignedValue()) {
4468 Pred = ICmpInst::ICMP_EQ;
4469 break;
4470 }
4471 if (RA.isMaxSignedValue()) return true;
4472 break;
4473 case ICmpInst::ICMP_UGT:
4474 if (RA.isMinValue()) {
4475 Pred = ICmpInst::ICMP_NE;
4476 break;
4477 }
4478 if ((RA + 1).isMaxValue()) {
4479 Pred = ICmpInst::ICMP_EQ;
4480 RHS = getConstant(RA + 1);
4481 break;
4482 }
4483 if (RA.isMaxValue()) return false;
4484 break;
4485 case ICmpInst::ICMP_ULT:
4486 if (RA.isMaxValue()) {
4487 Pred = ICmpInst::ICMP_NE;
4488 break;
4489 }
4490 if ((RA - 1).isMinValue()) {
4491 Pred = ICmpInst::ICMP_EQ;
4492 RHS = getConstant(RA - 1);
4493 break;
4494 }
4495 if (RA.isMinValue()) return false;
4496 break;
4497 case ICmpInst::ICMP_SGT:
4498 if (RA.isMinSignedValue()) {
4499 Pred = ICmpInst::ICMP_NE;
4500 break;
4501 }
4502 if ((RA + 1).isMaxSignedValue()) {
4503 Pred = ICmpInst::ICMP_EQ;
4504 RHS = getConstant(RA + 1);
4505 break;
4506 }
4507 if (RA.isMaxSignedValue()) return false;
4508 break;
4509 case ICmpInst::ICMP_SLT:
4510 if (RA.isMaxSignedValue()) {
4511 Pred = ICmpInst::ICMP_NE;
4512 break;
4513 }
4514 if ((RA - 1).isMinSignedValue()) {
4515 Pred = ICmpInst::ICMP_EQ;
4516 RHS = getConstant(RA - 1);
4517 break;
4518 }
4519 if (RA.isMinSignedValue()) return false;
4520 break;
4521 }
4522 }
4523
4524 // Check to see if we can make the LHS or RHS match.
4525 if (LHS == FoundRHS || RHS == FoundLHS) {
4526 if (isa<SCEVConstant>(RHS)) {
4527 std::swap(FoundLHS, FoundRHS);
4528 FoundPred = ICmpInst::getSwappedPredicate(FoundPred);
4529 } else {
4530 std::swap(LHS, RHS);
4531 Pred = ICmpInst::getSwappedPredicate(Pred);
4532 }
4533 }
4534
4535 // Check whether the found predicate is the same as the desired predicate.
4536 if (FoundPred == Pred)
4537 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS);
4538
4539 // Check whether swapping the found predicate makes it the same as the
4540 // desired predicate.
4541 if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) {
4542 if (isa<SCEVConstant>(RHS))
4543 return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS);
4544 else
4545 return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred),
4546 RHS, LHS, FoundLHS, FoundRHS);
4547 }
4548
4549 // Check whether the actual condition is beyond sufficient.
4550 if (FoundPred == ICmpInst::ICMP_EQ)
4551 if (ICmpInst::isTrueWhenEqual(Pred))
4552 if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS))
4553 return true;
4554 if (Pred == ICmpInst::ICMP_NE)
4555 if (!ICmpInst::isTrueWhenEqual(FoundPred))
4556 if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS))
4557 return true;
4558
4559 // Otherwise assume the worst.
4560 return false;
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004561}
4562
Dan Gohman920446d2009-07-21 23:03:19 +00004563/// isImpliedCondOperands - Test whether the condition described by Pred,
4564/// LHS, and RHS is true whenever the condition desribed by Pred, FoundLHS,
4565/// and FoundRHS is true.
4566bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred,
4567 const SCEV *LHS, const SCEV *RHS,
4568 const SCEV *FoundLHS,
4569 const SCEV *FoundRHS) {
4570 return isImpliedCondOperandsHelper(Pred, LHS, RHS,
4571 FoundLHS, FoundRHS) ||
4572 // ~x < ~y --> x > y
4573 isImpliedCondOperandsHelper(Pred, LHS, RHS,
4574 getNotSCEV(FoundRHS),
4575 getNotSCEV(FoundLHS));
4576}
4577
4578/// isImpliedCondOperandsHelper - Test whether the condition described by
4579/// Pred, LHS, and RHS is true whenever the condition desribed by Pred,
4580/// FoundLHS, and FoundRHS is true.
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004581bool
Dan Gohman920446d2009-07-21 23:03:19 +00004582ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
4583 const SCEV *LHS, const SCEV *RHS,
4584 const SCEV *FoundLHS,
4585 const SCEV *FoundRHS) {
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004586 switch (Pred) {
Dan Gohman2d4f5b12009-07-16 17:34:36 +00004587 default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
4588 case ICmpInst::ICMP_EQ:
4589 case ICmpInst::ICMP_NE:
4590 if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS))
4591 return true;
4592 break;
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004593 case ICmpInst::ICMP_SLT:
Dan Gohman2d4f5b12009-07-16 17:34:36 +00004594 case ICmpInst::ICMP_SLE:
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004595 if (isKnownPredicate(ICmpInst::ICMP_SLE, LHS, FoundLHS) &&
4596 isKnownPredicate(ICmpInst::ICMP_SGE, RHS, FoundRHS))
4597 return true;
4598 break;
4599 case ICmpInst::ICMP_SGT:
Dan Gohman2d4f5b12009-07-16 17:34:36 +00004600 case ICmpInst::ICMP_SGE:
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004601 if (isKnownPredicate(ICmpInst::ICMP_SGE, LHS, FoundLHS) &&
4602 isKnownPredicate(ICmpInst::ICMP_SLE, RHS, FoundRHS))
4603 return true;
4604 break;
4605 case ICmpInst::ICMP_ULT:
Dan Gohman2d4f5b12009-07-16 17:34:36 +00004606 case ICmpInst::ICMP_ULE:
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004607 if (isKnownPredicate(ICmpInst::ICMP_ULE, LHS, FoundLHS) &&
4608 isKnownPredicate(ICmpInst::ICMP_UGE, RHS, FoundRHS))
4609 return true;
4610 break;
4611 case ICmpInst::ICMP_UGT:
Dan Gohman2d4f5b12009-07-16 17:34:36 +00004612 case ICmpInst::ICMP_UGE:
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004613 if (isKnownPredicate(ICmpInst::ICMP_UGE, LHS, FoundLHS) &&
4614 isKnownPredicate(ICmpInst::ICMP_ULE, RHS, FoundRHS))
4615 return true;
4616 break;
4617 }
4618
4619 return false;
Dan Gohman423ed6c2009-06-24 01:18:18 +00004620}
4621
Dan Gohmand2b62c42009-06-21 23:46:38 +00004622/// getBECount - Subtract the end and start values and divide by the step,
4623/// rounding up, to get the number of times the backedge is executed. Return
4624/// CouldNotCompute if an intermediate computation overflows.
Dan Gohman161ea032009-07-07 17:06:11 +00004625const SCEV *ScalarEvolution::getBECount(const SCEV *Start,
Dan Gohman69eacc72009-07-13 22:05:32 +00004626 const SCEV *End,
4627 const SCEV *Step) {
Dan Gohmand2b62c42009-06-21 23:46:38 +00004628 const Type *Ty = Start->getType();
Dan Gohman161ea032009-07-07 17:06:11 +00004629 const SCEV *NegOne = getIntegerSCEV(-1, Ty);
4630 const SCEV *Diff = getMinusSCEV(End, Start);
4631 const SCEV *RoundUp = getAddExpr(Step, NegOne);
Dan Gohmand2b62c42009-06-21 23:46:38 +00004632
4633 // Add an adjustment to the difference between End and Start so that
4634 // the division will effectively round up.
Dan Gohman161ea032009-07-07 17:06:11 +00004635 const SCEV *Add = getAddExpr(Diff, RoundUp);
Dan Gohmand2b62c42009-06-21 23:46:38 +00004636
4637 // Check Add for unsigned overflow.
4638 // TODO: More sophisticated things could be done here.
Owen Anderson175b6542009-07-22 00:24:57 +00004639 const Type *WideTy = getContext().getIntegerType(getTypeSizeInBits(Ty) + 1);
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004640 const SCEV *EDiff = getZeroExtendExpr(Diff, WideTy);
4641 const SCEV *ERoundUp = getZeroExtendExpr(RoundUp, WideTy);
4642 const SCEV *OperandExtendedAdd = getAddExpr(EDiff, ERoundUp);
Dan Gohmand2b62c42009-06-21 23:46:38 +00004643 if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd)
Dan Gohmanc6475cb2009-06-27 21:21:31 +00004644 return getCouldNotCompute();
Dan Gohmand2b62c42009-06-21 23:46:38 +00004645
4646 return getUDivExpr(Add, Step);
4647}
4648
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004649/// HowManyLessThans - Return the number of times a backedge containing the
4650/// specified less-than comparison will execute. If not computable, return
Dan Gohman0c850912009-06-06 14:37:11 +00004651/// CouldNotCompute.
Dan Gohman9bc642f2009-06-24 04:48:43 +00004652ScalarEvolution::BackedgeTakenInfo
4653ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
4654 const Loop *L, bool isSigned) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004655 // Only handle: "ADDREC < LoopInvariant".
Dan Gohmanc6475cb2009-06-27 21:21:31 +00004656 if (!RHS->isLoopInvariant(L)) return getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004657
Dan Gohmanbff6b582009-05-04 22:30:44 +00004658 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004659 if (!AddRec || AddRec->getLoop() != L)
Dan Gohmanc6475cb2009-06-27 21:21:31 +00004660 return getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004661
4662 if (AddRec->isAffine()) {
Nick Lewycky35b56022009-01-13 09:18:58 +00004663 // FORNOW: We only support unit strides.
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004664 unsigned BitWidth = getTypeSizeInBits(AddRec->getType());
Dan Gohman161ea032009-07-07 17:06:11 +00004665 const SCEV *Step = AddRec->getStepRecurrence(*this);
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004666
4667 // TODO: handle non-constant strides.
4668 const SCEVConstant *CStep = dyn_cast<SCEVConstant>(Step);
4669 if (!CStep || CStep->isZero())
Dan Gohmanc6475cb2009-06-27 21:21:31 +00004670 return getCouldNotCompute();
Dan Gohmanf8bc8e82009-05-18 15:22:39 +00004671 if (CStep->isOne()) {
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004672 // With unit stride, the iteration never steps past the limit value.
4673 } else if (CStep->getValue()->getValue().isStrictlyPositive()) {
4674 if (const SCEVConstant *CLimit = dyn_cast<SCEVConstant>(RHS)) {
4675 // Test whether a positive iteration iteration can step past the limit
4676 // value and past the maximum value for its type in a single step.
4677 if (isSigned) {
4678 APInt Max = APInt::getSignedMaxValue(BitWidth);
4679 if ((Max - CStep->getValue()->getValue())
4680 .slt(CLimit->getValue()->getValue()))
Dan Gohmanc6475cb2009-06-27 21:21:31 +00004681 return getCouldNotCompute();
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004682 } else {
4683 APInt Max = APInt::getMaxValue(BitWidth);
4684 if ((Max - CStep->getValue()->getValue())
4685 .ult(CLimit->getValue()->getValue()))
Dan Gohmanc6475cb2009-06-27 21:21:31 +00004686 return getCouldNotCompute();
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004687 }
4688 } else
4689 // TODO: handle non-constant limit values below.
Dan Gohmanc6475cb2009-06-27 21:21:31 +00004690 return getCouldNotCompute();
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004691 } else
4692 // TODO: handle negative strides below.
Dan Gohmanc6475cb2009-06-27 21:21:31 +00004693 return getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004694
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004695 // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant
4696 // m. So, we count the number of iterations in which {n,+,s} < m is true.
4697 // Note that we cannot simply return max(m-n,0)/s because it's not safe to
Wojciech Matyjewicz1377a542008-02-13 12:21:32 +00004698 // treat m-n as signed nor unsigned due to overflow possibility.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004699
Wojciech Matyjewiczebc77b12008-02-13 11:51:34 +00004700 // First, we get the value of the LHS in the first iteration: n
Dan Gohman161ea032009-07-07 17:06:11 +00004701 const SCEV *Start = AddRec->getOperand(0);
Wojciech Matyjewiczebc77b12008-02-13 11:51:34 +00004702
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004703 // Determine the minimum constant start value.
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004704 const SCEV *MinStart = getConstant(isSigned ?
4705 getSignedRange(Start).getSignedMin() :
4706 getUnsignedRange(Start).getUnsignedMin());
Wojciech Matyjewiczebc77b12008-02-13 11:51:34 +00004707
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004708 // If we know that the condition is true in order to enter the loop,
4709 // then we know that it will run exactly (m-n)/s times. Otherwise, we
Dan Gohmanc8a29272009-05-24 23:45:28 +00004710 // only know that it will execute (max(m,n)-n)/s times. In both cases,
4711 // the division must round up.
Dan Gohman161ea032009-07-07 17:06:11 +00004712 const SCEV *End = RHS;
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004713 if (!isLoopGuardedByCond(L,
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004714 isSigned ? ICmpInst::ICMP_SLT :
4715 ICmpInst::ICMP_ULT,
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004716 getMinusSCEV(Start, Step), RHS))
4717 End = isSigned ? getSMaxExpr(RHS, Start)
4718 : getUMaxExpr(RHS, Start);
4719
4720 // Determine the maximum constant end value.
Dan Gohman55e2d7e2009-07-13 21:35:55 +00004721 const SCEV *MaxEnd = getConstant(isSigned ?
4722 getSignedRange(End).getSignedMax() :
4723 getUnsignedRange(End).getUnsignedMax());
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004724
4725 // Finally, we subtract these two values and divide, rounding up, to get
4726 // the number of times the backedge is executed.
Dan Gohman161ea032009-07-07 17:06:11 +00004727 const SCEV *BECount = getBECount(Start, End, Step);
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004728
4729 // The maximum backedge count is similar, except using the minimum start
4730 // value and the maximum end value.
Dan Gohman161ea032009-07-07 17:06:11 +00004731 const SCEV *MaxBECount = getBECount(MinStart, MaxEnd, Step);
Dan Gohmanf7d3d25542009-04-30 20:47:05 +00004732
4733 return BackedgeTakenInfo(BECount, MaxBECount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004734 }
4735
Dan Gohmanc6475cb2009-06-27 21:21:31 +00004736 return getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004737}
4738
4739/// getNumIterationsInRange - Return the number of iterations of this loop that
4740/// produce values in the specified constant range. Another way of looking at
4741/// this is that it returns the first iteration number where the value is not in
4742/// the condition, thus computing the exit count. If the iteration count can't
4743/// be computed, an instance of SCEVCouldNotCompute is returned.
Dan Gohman161ea032009-07-07 17:06:11 +00004744const SCEV *SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
Dan Gohman9bc642f2009-06-24 04:48:43 +00004745 ScalarEvolution &SE) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004746 if (Range.isFullSet()) // Infinite loop.
Dan Gohman0ad08b02009-04-18 17:58:19 +00004747 return SE.getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004748
4749 // If the start is a non-zero constant, shift the range to simplify things.
Dan Gohmanc76b5452009-05-04 22:02:23 +00004750 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004751 if (!SC->getValue()->isZero()) {
Dan Gohman161ea032009-07-07 17:06:11 +00004752 SmallVector<const SCEV *, 4> Operands(op_begin(), op_end());
Dan Gohman89f85052007-10-22 18:31:58 +00004753 Operands[0] = SE.getIntegerSCEV(0, SC->getType());
Dan Gohman161ea032009-07-07 17:06:11 +00004754 const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop());
Dan Gohmanc76b5452009-05-04 22:02:23 +00004755 if (const SCEVAddRecExpr *ShiftedAddRec =
4756 dyn_cast<SCEVAddRecExpr>(Shifted))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004757 return ShiftedAddRec->getNumIterationsInRange(
Dan Gohman89f85052007-10-22 18:31:58 +00004758 Range.subtract(SC->getValue()->getValue()), SE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004759 // This is strange and shouldn't happen.
Dan Gohman0ad08b02009-04-18 17:58:19 +00004760 return SE.getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004761 }
4762
4763 // The only time we can solve this is when we have all constant indices.
4764 // Otherwise, we cannot determine the overflow conditions.
4765 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4766 if (!isa<SCEVConstant>(getOperand(i)))
Dan Gohman0ad08b02009-04-18 17:58:19 +00004767 return SE.getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004768
4769
4770 // Okay at this point we know that all elements of the chrec are constants and
4771 // that the start element is zero.
4772
4773 // First check to see if the range contains zero. If not, the first
4774 // iteration exits.
Dan Gohmanb98c1a32009-04-21 01:07:12 +00004775 unsigned BitWidth = SE.getTypeSizeInBits(getType());
Dan Gohman01c2ee72009-04-16 03:18:22 +00004776 if (!Range.contains(APInt(BitWidth, 0)))
Dan Gohman8fd520a2009-06-15 22:12:54 +00004777 return SE.getIntegerSCEV(0, getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004778
4779 if (isAffine()) {
4780 // If this is an affine expression then we have this situation:
4781 // Solve {0,+,A} in Range === Ax in Range
4782
4783 // We know that zero is in the range. If A is positive then we know that
4784 // the upper value of the range must be the first possible exit value.
4785 // If A is negative then the lower of the range is the last possible loop
4786 // value. Also note that we already checked for a full range.
Dan Gohman01c2ee72009-04-16 03:18:22 +00004787 APInt One(BitWidth,1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004788 APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue();
4789 APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower();
4790
4791 // The exit value should be (End+A)/A.
Nick Lewyckya0facae2007-09-27 14:12:54 +00004792 APInt ExitVal = (End + A).udiv(A);
Owen Andersoneacb44d2009-07-24 23:12:02 +00004793 ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004794
4795 // Evaluate at the exit value. If we really did fall out of the valid
4796 // range, then we computed our trip count, otherwise wrap around or other
4797 // things must have happened.
Dan Gohman89f85052007-10-22 18:31:58 +00004798 ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004799 if (Range.contains(Val->getValue()))
Dan Gohman0ad08b02009-04-18 17:58:19 +00004800 return SE.getCouldNotCompute(); // Something strange happened
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004801
4802 // Ensure that the previous value is in the range. This is a sanity check.
4803 assert(Range.contains(
Dan Gohman9bc642f2009-06-24 04:48:43 +00004804 EvaluateConstantChrecAtConstant(this,
Owen Andersoneacb44d2009-07-24 23:12:02 +00004805 ConstantInt::get(SE.getContext(), ExitVal - One), SE)->getValue()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004806 "Linear scev computation is off in a bad way!");
Dan Gohman89f85052007-10-22 18:31:58 +00004807 return SE.getConstant(ExitValue);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004808 } else if (isQuadratic()) {
4809 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the
4810 // quadratic equation to solve it. To do this, we must frame our problem in
4811 // terms of figuring out when zero is crossed, instead of when
4812 // Range.getUpper() is crossed.
Dan Gohman161ea032009-07-07 17:06:11 +00004813 SmallVector<const SCEV *, 4> NewOps(op_begin(), op_end());
Dan Gohman89f85052007-10-22 18:31:58 +00004814 NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper()));
Dan Gohman161ea032009-07-07 17:06:11 +00004815 const SCEV *NewAddRec = SE.getAddRecExpr(NewOps, getLoop());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004816
4817 // Next, solve the constructed addrec
Dan Gohman161ea032009-07-07 17:06:11 +00004818 std::pair<const SCEV *,const SCEV *> Roots =
Dan Gohman89f85052007-10-22 18:31:58 +00004819 SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE);
Dan Gohmanbff6b582009-05-04 22:30:44 +00004820 const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
4821 const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004822 if (R1) {
4823 // Pick the smallest positive root value.
4824 if (ConstantInt *CB =
Owen Andersone755b092009-07-06 22:37:39 +00004825 dyn_cast<ConstantInt>(
Owen Anderson175b6542009-07-22 00:24:57 +00004826 SE.getContext().getConstantExprICmp(ICmpInst::ICMP_ULT,
Owen Andersone755b092009-07-06 22:37:39 +00004827 R1->getValue(), R2->getValue()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004828 if (CB->getZExtValue() == false)
4829 std::swap(R1, R2); // R1 is the minimum root now.
4830
4831 // Make sure the root is not off by one. The returned iteration should
4832 // not be in the range, but the previous one should be. When solving
4833 // for "X*X < 5", for example, we should not return a root of 2.
4834 ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this,
Dan Gohman89f85052007-10-22 18:31:58 +00004835 R1->getValue(),
4836 SE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004837 if (Range.contains(R1Val->getValue())) {
4838 // The next iteration must be out of the range...
Owen Andersone755b092009-07-06 22:37:39 +00004839 ConstantInt *NextVal =
Owen Andersoneacb44d2009-07-24 23:12:02 +00004840 ConstantInt::get(SE.getContext(), R1->getValue()->getValue()+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004841
Dan Gohman89f85052007-10-22 18:31:58 +00004842 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004843 if (!Range.contains(R1Val->getValue()))
Dan Gohman89f85052007-10-22 18:31:58 +00004844 return SE.getConstant(NextVal);
Dan Gohman0ad08b02009-04-18 17:58:19 +00004845 return SE.getCouldNotCompute(); // Something strange happened
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004846 }
4847
4848 // If R1 was not in the range, then it is a good return value. Make
4849 // sure that R1-1 WAS in the range though, just in case.
Owen Andersone755b092009-07-06 22:37:39 +00004850 ConstantInt *NextVal =
Owen Andersoneacb44d2009-07-24 23:12:02 +00004851 ConstantInt::get(SE.getContext(), R1->getValue()->getValue()-1);
Dan Gohman89f85052007-10-22 18:31:58 +00004852 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004853 if (Range.contains(R1Val->getValue()))
4854 return R1;
Dan Gohman0ad08b02009-04-18 17:58:19 +00004855 return SE.getCouldNotCompute(); // Something strange happened
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004856 }
4857 }
4858 }
4859
Dan Gohman0ad08b02009-04-18 17:58:19 +00004860 return SE.getCouldNotCompute();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004861}
4862
4863
4864
4865//===----------------------------------------------------------------------===//
Dan Gohmanbff6b582009-05-04 22:30:44 +00004866// SCEVCallbackVH Class Implementation
4867//===----------------------------------------------------------------------===//
4868
Dan Gohman999d14e2009-05-19 19:22:47 +00004869void ScalarEvolution::SCEVCallbackVH::deleted() {
Dan Gohman31b69c12009-07-13 22:20:53 +00004870 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
Dan Gohmanbff6b582009-05-04 22:30:44 +00004871 if (PHINode *PN = dyn_cast<PHINode>(getValPtr()))
4872 SE->ConstantEvolutionLoopExitValue.erase(PN);
Dan Gohmanda0071e2009-05-08 20:47:27 +00004873 if (Instruction *I = dyn_cast<Instruction>(getValPtr()))
4874 SE->ValuesAtScopes.erase(I);
Dan Gohmanbff6b582009-05-04 22:30:44 +00004875 SE->Scalars.erase(getValPtr());
4876 // this now dangles!
4877}
4878
Dan Gohman999d14e2009-05-19 19:22:47 +00004879void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *) {
Dan Gohman31b69c12009-07-13 22:20:53 +00004880 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
Dan Gohmanbff6b582009-05-04 22:30:44 +00004881
4882 // Forget all the expressions associated with users of the old value,
4883 // so that future queries will recompute the expressions using the new
4884 // value.
4885 SmallVector<User *, 16> Worklist;
Dan Gohman6b9da312009-07-14 14:34:04 +00004886 SmallPtrSet<User *, 8> Visited;
Dan Gohmanbff6b582009-05-04 22:30:44 +00004887 Value *Old = getValPtr();
4888 bool DeleteOld = false;
4889 for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end();
4890 UI != UE; ++UI)
4891 Worklist.push_back(*UI);
4892 while (!Worklist.empty()) {
4893 User *U = Worklist.pop_back_val();
4894 // Deleting the Old value will cause this to dangle. Postpone
4895 // that until everything else is done.
4896 if (U == Old) {
4897 DeleteOld = true;
4898 continue;
4899 }
Dan Gohman6b9da312009-07-14 14:34:04 +00004900 if (!Visited.insert(U))
4901 continue;
Dan Gohmanbff6b582009-05-04 22:30:44 +00004902 if (PHINode *PN = dyn_cast<PHINode>(U))
4903 SE->ConstantEvolutionLoopExitValue.erase(PN);
Dan Gohmanda0071e2009-05-08 20:47:27 +00004904 if (Instruction *I = dyn_cast<Instruction>(U))
4905 SE->ValuesAtScopes.erase(I);
Dan Gohman6b9da312009-07-14 14:34:04 +00004906 SE->Scalars.erase(U);
4907 for (Value::use_iterator UI = U->use_begin(), UE = U->use_end();
4908 UI != UE; ++UI)
4909 Worklist.push_back(*UI);
Dan Gohmanbff6b582009-05-04 22:30:44 +00004910 }
Dan Gohman6b9da312009-07-14 14:34:04 +00004911 // Delete the Old value if it (indirectly) references itself.
Dan Gohmanbff6b582009-05-04 22:30:44 +00004912 if (DeleteOld) {
4913 if (PHINode *PN = dyn_cast<PHINode>(Old))
4914 SE->ConstantEvolutionLoopExitValue.erase(PN);
Dan Gohmanda0071e2009-05-08 20:47:27 +00004915 if (Instruction *I = dyn_cast<Instruction>(Old))
4916 SE->ValuesAtScopes.erase(I);
Dan Gohmanbff6b582009-05-04 22:30:44 +00004917 SE->Scalars.erase(Old);
4918 // this now dangles!
4919 }
4920 // this may dangle!
4921}
4922
Dan Gohman999d14e2009-05-19 19:22:47 +00004923ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se)
Dan Gohmanbff6b582009-05-04 22:30:44 +00004924 : CallbackVH(V), SE(se) {}
4925
4926//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004927// ScalarEvolution Class Implementation
4928//===----------------------------------------------------------------------===//
4929
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004930ScalarEvolution::ScalarEvolution()
Dan Gohmanc6475cb2009-06-27 21:21:31 +00004931 : FunctionPass(&ID) {
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004932}
4933
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004934bool ScalarEvolution::runOnFunction(Function &F) {
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004935 this->F = &F;
4936 LI = &getAnalysis<LoopInfo>();
4937 TD = getAnalysisIfAvailable<TargetData>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004938 return false;
4939}
4940
4941void ScalarEvolution::releaseMemory() {
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004942 Scalars.clear();
4943 BackedgeTakenCounts.clear();
4944 ConstantEvolutionLoopExitValue.clear();
Dan Gohmanda0071e2009-05-08 20:47:27 +00004945 ValuesAtScopes.clear();
Dan Gohmanc6475cb2009-06-27 21:21:31 +00004946 UniqueSCEVs.clear();
4947 SCEVAllocator.Reset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004948}
4949
4950void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const {
4951 AU.setPreservesAll();
4952 AU.addRequiredTransitive<LoopInfo>();
Dan Gohman01c2ee72009-04-16 03:18:22 +00004953}
4954
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004955bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) {
Dan Gohman76d5a0d2009-02-24 18:55:53 +00004956 return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004957}
4958
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004959static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004960 const Loop *L) {
4961 // Print all inner loops first
4962 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
4963 PrintLoopInfo(OS, SE, *I);
4964
Nick Lewyckye5da1912008-01-02 02:49:20 +00004965 OS << "Loop " << L->getHeader()->getName() << ": ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004966
Devang Patel02451fa2007-08-21 00:31:24 +00004967 SmallVector<BasicBlock*, 8> ExitBlocks;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004968 L->getExitBlocks(ExitBlocks);
4969 if (ExitBlocks.size() != 1)
Nick Lewyckye5da1912008-01-02 02:49:20 +00004970 OS << "<multiple exits> ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004971
Dan Gohman76d5a0d2009-02-24 18:55:53 +00004972 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
4973 OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004974 } else {
Dan Gohman76d5a0d2009-02-24 18:55:53 +00004975 OS << "Unpredictable backedge-taken count. ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004976 }
4977
Nick Lewyckye5da1912008-01-02 02:49:20 +00004978 OS << "\n";
Dan Gohmanb6b9e9e2009-06-24 00:33:16 +00004979 OS << "Loop " << L->getHeader()->getName() << ": ";
4980
4981 if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) {
4982 OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L);
4983 } else {
4984 OS << "Unpredictable max backedge-taken count. ";
4985 }
4986
4987 OS << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004988}
4989
Dan Gohman13058cc2009-04-21 00:47:46 +00004990void ScalarEvolution::print(raw_ostream &OS, const Module* ) const {
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004991 // ScalarEvolution's implementaiton of the print method is to print
4992 // out SCEV values of all instructions that are interesting. Doing
4993 // this potentially causes it to create new SCEV objects though,
4994 // which technically conflicts with the const qualifier. This isn't
Dan Gohmanac2a9d62009-07-10 20:25:29 +00004995 // observable from outside the class though, so casting away the
4996 // const isn't dangerous.
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004997 ScalarEvolution &SE = *const_cast<ScalarEvolution*>(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004998
Dan Gohmanffd36ba2009-04-21 23:15:49 +00004999 OS << "Classifying expressions for: " << F->getName() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005000 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
Dan Gohman43d37e92009-04-30 01:30:18 +00005001 if (isSCEVable(I->getType())) {
Dan Gohman12668ad2009-07-13 23:03:05 +00005002 OS << *I << '\n';
Dan Gohmanabe991f2008-09-14 17:21:12 +00005003 OS << " --> ";
Dan Gohman161ea032009-07-07 17:06:11 +00005004 const SCEV *SV = SE.getSCEV(&*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005005 SV->print(OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005006
Dan Gohman8db598a2009-06-19 17:49:54 +00005007 const Loop *L = LI->getLoopFor((*I).getParent());
5008
Dan Gohman161ea032009-07-07 17:06:11 +00005009 const SCEV *AtUse = SE.getSCEVAtScope(SV, L);
Dan Gohman8db598a2009-06-19 17:49:54 +00005010 if (AtUse != SV) {
5011 OS << " --> ";
5012 AtUse->print(OS);
5013 }
5014
5015 if (L) {
Dan Gohmane5b60842009-06-18 00:37:45 +00005016 OS << "\t\t" "Exits: ";
Dan Gohman161ea032009-07-07 17:06:11 +00005017 const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop());
Dan Gohmanaff14d62009-05-24 23:25:42 +00005018 if (!ExitValue->isLoopInvariant(L)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005019 OS << "<<Unknown>>";
5020 } else {
5021 OS << *ExitValue;
5022 }
5023 }
5024
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005025 OS << "\n";
5026 }
5027
Dan Gohmanffd36ba2009-04-21 23:15:49 +00005028 OS << "Determining loop execution counts for: " << F->getName() << "\n";
5029 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
5030 PrintLoopInfo(OS, &SE, *I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005031}
Dan Gohman13058cc2009-04-21 00:47:46 +00005032
5033void ScalarEvolution::print(std::ostream &o, const Module *M) const {
5034 raw_os_ostream OS(o);
5035 print(OS, M);
5036}