blob: f34549ae52b47e2ef90bf16a9db79698f68e14e1 [file] [log] [blame]
Nick Lewycky97756402014-09-01 05:17:15 +00001//===- ScalarEvolution.cpp - Scalar Evolution Analysis --------------------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
Chris Lattnerd934c702004-04-02 20:23:17 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman01808ca2005-04-21 21:13:18 +00007//
Chris Lattnerd934c702004-04-02 20:23:17 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains the implementation of the scalar evolution analysis
11// engine, which is used primarily to analyze expressions involving induction
12// variables in loops.
13//
14// There are several aspects to this library. First is the representation of
15// scalar expressions, which are represented as subclasses of the SCEV class.
16// These classes are used to represent certain types of subexpressions that we
Dan Gohmanef2ae2c2009-07-25 16:18:07 +000017// can handle. We only create one SCEV of a particular shape, so
18// pointer-comparisons for equality are legal.
Chris Lattnerd934c702004-04-02 20:23:17 +000019//
20// One important aspect of the SCEV objects is that they are never cyclic, even
21// if there is a cycle in the dataflow for an expression (ie, a PHI node). If
22// the PHI node is one of the idioms that we can represent (e.g., a polynomial
23// recurrence) then we represent it directly as a recurrence node, otherwise we
24// represent it as a SCEVUnknown node.
25//
26// In addition to being able to represent expressions of various types, we also
27// have folders that are used to build the *canonical* representation for a
28// particular expression. These folders are capable of using a variety of
29// rewrite rules to simplify the expressions.
Misha Brukman01808ca2005-04-21 21:13:18 +000030//
Chris Lattnerd934c702004-04-02 20:23:17 +000031// Once the folders are defined, we can implement the more interesting
32// higher-level code, such as the code that recognizes PHI nodes of various
33// types, computes the execution count of a loop, etc.
34//
Chris Lattnerd934c702004-04-02 20:23:17 +000035// TODO: We should use these routines and value representations to implement
36// dependence analysis!
37//
38//===----------------------------------------------------------------------===//
39//
40// There are several good references for the techniques used in this analysis.
41//
42// Chains of recurrences -- a method to expedite the evaluation
43// of closed-form functions
44// Olaf Bachmann, Paul S. Wang, Eugene V. Zima
45//
46// On computational properties of chains of recurrences
47// Eugene V. Zima
48//
49// Symbolic Evaluation of Chains of Recurrences for Loop Optimization
50// Robert A. van Engelen
51//
52// Efficient Symbolic Analysis for Optimizing Compilers
53// Robert A. van Engelen
54//
55// Using the chains of recurrences algebra for data dependence testing and
56// induction variable substitution
57// MS Thesis, Johnie Birch
58//
59//===----------------------------------------------------------------------===//
60
Chandler Carruthed0881b2012-12-03 16:50:05 +000061#include "llvm/Analysis/ScalarEvolution.h"
Eugene Zelenkobe709f22017-08-18 23:51:26 +000062#include "llvm/ADT/APInt.h"
63#include "llvm/ADT/ArrayRef.h"
64#include "llvm/ADT/DenseMap.h"
65#include "llvm/ADT/DepthFirstIterator.h"
Max Kazantsevcf9b1b22017-11-28 07:48:12 +000066#include "llvm/ADT/EquivalenceClasses.h"
Eugene Zelenkobe709f22017-08-18 23:51:26 +000067#include "llvm/ADT/FoldingSet.h"
68#include "llvm/ADT/None.h"
Sanjoy Das1f05c512014-10-10 21:22:34 +000069#include "llvm/ADT/Optional.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000070#include "llvm/ADT/STLExtras.h"
Sanjoy Dasc46bceb2016-09-27 18:01:42 +000071#include "llvm/ADT/ScopeExit.h"
Sanjoy Das17078692016-10-31 03:32:43 +000072#include "llvm/ADT/Sequence.h"
Eugene Zelenkobe709f22017-08-18 23:51:26 +000073#include "llvm/ADT/SetVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000074#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenkobe709f22017-08-18 23:51:26 +000075#include "llvm/ADT/SmallSet.h"
76#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000077#include "llvm/ADT/Statistic.h"
Eugene Zelenkobe709f22017-08-18 23:51:26 +000078#include "llvm/ADT/StringRef.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000079#include "llvm/Analysis/AssumptionCache.h"
John Criswellfe5f33b2005-10-27 15:54:34 +000080#include "llvm/Analysis/ConstantFolding.h"
Duncan Sandsd06f50e2010-11-17 04:18:45 +000081#include "llvm/Analysis/InstructionSimplify.h"
Chris Lattnerd934c702004-04-02 20:23:17 +000082#include "llvm/Analysis/LoopInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000083#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000084#include "llvm/Analysis/TargetLibraryInfo.h"
Dan Gohman1ee696d2009-06-16 19:52:01 +000085#include "llvm/Analysis/ValueTracking.h"
Eugene Zelenkobe709f22017-08-18 23:51:26 +000086#include "llvm/IR/Argument.h"
87#include "llvm/IR/BasicBlock.h"
88#include "llvm/IR/CFG.h"
89#include "llvm/IR/CallSite.h"
90#include "llvm/IR/Constant.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000091#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000092#include "llvm/IR/Constants.h"
93#include "llvm/IR/DataLayout.h"
94#include "llvm/IR/DerivedTypes.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000095#include "llvm/IR/Dominators.h"
Eugene Zelenkobe709f22017-08-18 23:51:26 +000096#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000097#include "llvm/IR/GlobalAlias.h"
Eugene Zelenkobe709f22017-08-18 23:51:26 +000098#include "llvm/IR/GlobalValue.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000099#include "llvm/IR/GlobalVariable.h"
Chandler Carruth83948572014-03-04 10:30:26 +0000100#include "llvm/IR/InstIterator.h"
Eugene Zelenkobe709f22017-08-18 23:51:26 +0000101#include "llvm/IR/InstrTypes.h"
102#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +0000103#include "llvm/IR/Instructions.h"
Eugene Zelenkobe709f22017-08-18 23:51:26 +0000104#include "llvm/IR/IntrinsicInst.h"
105#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +0000106#include "llvm/IR/LLVMContext.h"
Sanjoy Das1f05c512014-10-10 21:22:34 +0000107#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +0000108#include "llvm/IR/Operator.h"
Sanjoy Dasc88f5d32015-10-28 21:27:14 +0000109#include "llvm/IR/PatternMatch.h"
Eugene Zelenkobe709f22017-08-18 23:51:26 +0000110#include "llvm/IR/Type.h"
111#include "llvm/IR/Use.h"
112#include "llvm/IR/User.h"
113#include "llvm/IR/Value.h"
114#include "llvm/Pass.h"
115#include "llvm/Support/Casting.h"
Chris Lattner996795b2006-06-28 23:17:24 +0000116#include "llvm/Support/CommandLine.h"
Eugene Zelenkobe709f22017-08-18 23:51:26 +0000117#include "llvm/Support/Compiler.h"
David Greene2330f782009-12-23 22:58:38 +0000118#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +0000119#include "llvm/Support/ErrorHandling.h"
Craig Topperb45eabc2017-04-26 16:39:58 +0000120#include "llvm/Support/KnownBits.h"
Sanjoy Das5d9a8cb2015-09-22 00:10:57 +0000121#include "llvm/Support/SaveAndRestore.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +0000122#include "llvm/Support/raw_ostream.h"
Alkis Evlogimenosa5c04ee2004-09-03 18:19:51 +0000123#include <algorithm>
Eugene Zelenkobe709f22017-08-18 23:51:26 +0000124#include <cassert>
125#include <climits>
126#include <cstddef>
127#include <cstdint>
128#include <cstdlib>
129#include <map>
130#include <memory>
131#include <tuple>
132#include <utility>
133#include <vector>
134
Chris Lattnerd934c702004-04-02 20:23:17 +0000135using namespace llvm;
136
Chandler Carruthf1221bd2014-04-22 02:48:03 +0000137#define DEBUG_TYPE "scalar-evolution"
138
Chris Lattner57ef9422006-12-19 22:30:33 +0000139STATISTIC(NumArrayLenItCounts,
140 "Number of trip counts computed with array length");
141STATISTIC(NumTripCountsComputed,
142 "Number of loops with predictable loop counts");
143STATISTIC(NumTripCountsNotComputed,
144 "Number of loops without predictable loop counts");
145STATISTIC(NumBruteForceTripCountsComputed,
146 "Number of loops with trip counts computed by force");
147
Dan Gohmand78c4002008-05-13 00:00:25 +0000148static cl::opt<unsigned>
Chris Lattner57ef9422006-12-19 22:30:33 +0000149MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden,
150 cl::desc("Maximum number of iterations SCEV will "
Dan Gohmance973df2009-06-24 04:48:43 +0000151 "symbolically execute a constant "
152 "derived loop"),
Chris Lattner57ef9422006-12-19 22:30:33 +0000153 cl::init(100));
154
Filipe Cabecinhas0da99372016-04-29 15:22:48 +0000155// FIXME: Enable this with EXPENSIVE_CHECKS when the test suite is clean.
Zachary Turner8065f0b2017-12-01 00:53:10 +0000156static cl::opt<bool> VerifySCEV(
157 "verify-scev", cl::Hidden,
158 cl::desc("Verify ScalarEvolution's backedge taken counts (slow)"));
Sanjoy Das0cdcdf02017-04-24 02:35:19 +0000159static cl::opt<bool>
Zachary Turner8065f0b2017-12-01 00:53:10 +0000160 VerifySCEVMap("verify-scev-maps", cl::Hidden,
Jeroen Ketemae48e3932016-04-12 23:21:46 +0000161 cl::desc("Verify no dangling value in ScalarEvolution's "
Wei Mia49559b2016-02-04 01:27:38 +0000162 "ExprValueMap (slow)"));
Benjamin Kramer214935e2012-10-26 17:31:32 +0000163
Li Huangfcfe8cd2016-10-20 21:38:39 +0000164static cl::opt<unsigned> MulOpsInlineThreshold(
165 "scev-mulops-inline-threshold", cl::Hidden,
166 cl::desc("Threshold for inlining multiplication operands into a SCEV"),
Max Kazantseveac01d42017-06-21 07:28:13 +0000167 cl::init(32));
Li Huangfcfe8cd2016-10-20 21:38:39 +0000168
Daniil Fukalovb09dac52017-01-26 13:33:17 +0000169static cl::opt<unsigned> AddOpsInlineThreshold(
170 "scev-addops-inline-threshold", cl::Hidden,
Max Kazantsev0bcf6ec2017-06-20 08:37:31 +0000171 cl::desc("Threshold for inlining addition operands into a SCEV"),
Daniil Fukalovb09dac52017-01-26 13:33:17 +0000172 cl::init(500));
173
Sanjoy Das1bd479d2017-03-05 23:49:17 +0000174static cl::opt<unsigned> MaxSCEVCompareDepth(
175 "scalar-evolution-max-scev-compare-depth", cl::Hidden,
176 cl::desc("Maximum depth of recursive SCEV complexity comparisons"),
177 cl::init(32));
178
Max Kazantsev2e44d292017-03-31 12:05:30 +0000179static cl::opt<unsigned> MaxSCEVOperationsImplicationDepth(
180 "scalar-evolution-max-scev-operations-implication-depth", cl::Hidden,
181 cl::desc("Maximum depth of recursive SCEV operations implication analysis"),
182 cl::init(2));
183
Sanjoy Das1bd479d2017-03-05 23:49:17 +0000184static cl::opt<unsigned> MaxValueCompareDepth(
185 "scalar-evolution-max-value-compare-depth", cl::Hidden,
186 cl::desc("Maximum depth of recursive value complexity comparisons"),
187 cl::init(2));
Daniil Fukalov4c3322c2016-11-17 16:07:52 +0000188
Daniil Fukalov6378bdb2017-02-06 12:38:06 +0000189static cl::opt<unsigned>
Max Kazantsevdc803662017-06-15 11:48:21 +0000190 MaxArithDepth("scalar-evolution-max-arith-depth", cl::Hidden,
191 cl::desc("Maximum depth of recursive arithmetics"),
192 cl::init(32));
Daniil Fukalov6378bdb2017-02-06 12:38:06 +0000193
Michael Liao468fb742017-01-13 18:28:30 +0000194static cl::opt<unsigned> MaxConstantEvolvingDepth(
195 "scalar-evolution-max-constant-evolving-depth", cl::Hidden,
196 cl::desc("Maximum depth of recursive constant evolving"), cl::init(32));
197
Max Kazantsev8d0322e2017-06-30 05:04:09 +0000198static cl::opt<unsigned>
199 MaxExtDepth("scalar-evolution-max-ext-depth", cl::Hidden,
200 cl::desc("Maximum depth of recursive SExt/ZExt"),
201 cl::init(8));
202
Max Kazantsev0e9e0792017-07-23 15:40:19 +0000203static cl::opt<unsigned>
204 MaxAddRecSize("scalar-evolution-max-add-rec-size", cl::Hidden,
205 cl::desc("Max coefficients in AddRec during evolving"),
206 cl::init(16));
207
Chris Lattnerd934c702004-04-02 20:23:17 +0000208//===----------------------------------------------------------------------===//
209// SCEV class definitions
210//===----------------------------------------------------------------------===//
211
212//===----------------------------------------------------------------------===//
213// Implementation of the SCEV class.
214//
Dan Gohman3423e722009-06-30 20:13:32 +0000215
Aaron Ballman615eb472017-10-15 14:32:27 +0000216#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun8c209aa2017-01-28 02:02:38 +0000217LLVM_DUMP_METHOD void SCEV::dump() const {
Davide Italiano2071f4c2015-10-25 19:55:24 +0000218 print(dbgs());
219 dbgs() << '\n';
220}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000221#endif
Davide Italiano2071f4c2015-10-25 19:55:24 +0000222
Dan Gohman534749b2010-11-17 22:27:42 +0000223void SCEV::print(raw_ostream &OS) const {
Benjamin Kramer987b8502014-02-11 19:02:55 +0000224 switch (static_cast<SCEVTypes>(getSCEVType())) {
Dan Gohman534749b2010-11-17 22:27:42 +0000225 case scConstant:
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000226 cast<SCEVConstant>(this)->getValue()->printAsOperand(OS, false);
Dan Gohman534749b2010-11-17 22:27:42 +0000227 return;
228 case scTruncate: {
229 const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(this);
230 const SCEV *Op = Trunc->getOperand();
231 OS << "(trunc " << *Op->getType() << " " << *Op << " to "
232 << *Trunc->getType() << ")";
233 return;
234 }
235 case scZeroExtend: {
236 const SCEVZeroExtendExpr *ZExt = cast<SCEVZeroExtendExpr>(this);
237 const SCEV *Op = ZExt->getOperand();
238 OS << "(zext " << *Op->getType() << " " << *Op << " to "
239 << *ZExt->getType() << ")";
240 return;
241 }
242 case scSignExtend: {
243 const SCEVSignExtendExpr *SExt = cast<SCEVSignExtendExpr>(this);
244 const SCEV *Op = SExt->getOperand();
245 OS << "(sext " << *Op->getType() << " " << *Op << " to "
246 << *SExt->getType() << ")";
247 return;
248 }
249 case scAddRecExpr: {
250 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(this);
251 OS << "{" << *AR->getOperand(0);
252 for (unsigned i = 1, e = AR->getNumOperands(); i != e; ++i)
253 OS << ",+," << *AR->getOperand(i);
254 OS << "}<";
Sanjoy Das76c48e02016-02-04 18:21:54 +0000255 if (AR->hasNoUnsignedWrap())
Chris Lattnera337f5e2011-01-09 02:16:18 +0000256 OS << "nuw><";
Sanjoy Das76c48e02016-02-04 18:21:54 +0000257 if (AR->hasNoSignedWrap())
Chris Lattnera337f5e2011-01-09 02:16:18 +0000258 OS << "nsw><";
Sanjoy Das76c48e02016-02-04 18:21:54 +0000259 if (AR->hasNoSelfWrap() &&
Andrew Trick8b55b732011-03-14 16:50:06 +0000260 !AR->getNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW)))
261 OS << "nw><";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000262 AR->getLoop()->getHeader()->printAsOperand(OS, /*PrintType=*/false);
Dan Gohman534749b2010-11-17 22:27:42 +0000263 OS << ">";
264 return;
265 }
266 case scAddExpr:
267 case scMulExpr:
268 case scUMaxExpr:
269 case scSMaxExpr: {
270 const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(this);
Craig Topper9f008862014-04-15 04:59:12 +0000271 const char *OpStr = nullptr;
Dan Gohman534749b2010-11-17 22:27:42 +0000272 switch (NAry->getSCEVType()) {
273 case scAddExpr: OpStr = " + "; break;
274 case scMulExpr: OpStr = " * "; break;
275 case scUMaxExpr: OpStr = " umax "; break;
276 case scSMaxExpr: OpStr = " smax "; break;
277 }
278 OS << "(";
279 for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end();
280 I != E; ++I) {
281 OS << **I;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000282 if (std::next(I) != E)
Dan Gohman534749b2010-11-17 22:27:42 +0000283 OS << OpStr;
284 }
285 OS << ")";
Andrew Trickd912a5b2011-11-29 02:06:35 +0000286 switch (NAry->getSCEVType()) {
287 case scAddExpr:
288 case scMulExpr:
Sanjoy Das76c48e02016-02-04 18:21:54 +0000289 if (NAry->hasNoUnsignedWrap())
Andrew Trickd912a5b2011-11-29 02:06:35 +0000290 OS << "<nuw>";
Sanjoy Das76c48e02016-02-04 18:21:54 +0000291 if (NAry->hasNoSignedWrap())
Andrew Trickd912a5b2011-11-29 02:06:35 +0000292 OS << "<nsw>";
293 }
Dan Gohman534749b2010-11-17 22:27:42 +0000294 return;
295 }
296 case scUDivExpr: {
297 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(this);
298 OS << "(" << *UDiv->getLHS() << " /u " << *UDiv->getRHS() << ")";
299 return;
300 }
301 case scUnknown: {
302 const SCEVUnknown *U = cast<SCEVUnknown>(this);
Chris Lattner229907c2011-07-18 04:54:35 +0000303 Type *AllocTy;
Dan Gohman534749b2010-11-17 22:27:42 +0000304 if (U->isSizeOf(AllocTy)) {
305 OS << "sizeof(" << *AllocTy << ")";
306 return;
307 }
308 if (U->isAlignOf(AllocTy)) {
309 OS << "alignof(" << *AllocTy << ")";
310 return;
311 }
Andrew Trick2a3b7162011-03-09 17:23:39 +0000312
Chris Lattner229907c2011-07-18 04:54:35 +0000313 Type *CTy;
Dan Gohman534749b2010-11-17 22:27:42 +0000314 Constant *FieldNo;
315 if (U->isOffsetOf(CTy, FieldNo)) {
316 OS << "offsetof(" << *CTy << ", ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000317 FieldNo->printAsOperand(OS, false);
Dan Gohman534749b2010-11-17 22:27:42 +0000318 OS << ")";
319 return;
320 }
Andrew Trick2a3b7162011-03-09 17:23:39 +0000321
Dan Gohman534749b2010-11-17 22:27:42 +0000322 // Otherwise just print it normally.
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000323 U->getValue()->printAsOperand(OS, false);
Dan Gohman534749b2010-11-17 22:27:42 +0000324 return;
325 }
326 case scCouldNotCompute:
327 OS << "***COULDNOTCOMPUTE***";
328 return;
Dan Gohman534749b2010-11-17 22:27:42 +0000329 }
330 llvm_unreachable("Unknown SCEV kind!");
331}
332
Chris Lattner229907c2011-07-18 04:54:35 +0000333Type *SCEV::getType() const {
Benjamin Kramer987b8502014-02-11 19:02:55 +0000334 switch (static_cast<SCEVTypes>(getSCEVType())) {
Dan Gohman534749b2010-11-17 22:27:42 +0000335 case scConstant:
336 return cast<SCEVConstant>(this)->getType();
337 case scTruncate:
338 case scZeroExtend:
339 case scSignExtend:
340 return cast<SCEVCastExpr>(this)->getType();
341 case scAddRecExpr:
342 case scMulExpr:
343 case scUMaxExpr:
344 case scSMaxExpr:
345 return cast<SCEVNAryExpr>(this)->getType();
346 case scAddExpr:
347 return cast<SCEVAddExpr>(this)->getType();
348 case scUDivExpr:
349 return cast<SCEVUDivExpr>(this)->getType();
350 case scUnknown:
351 return cast<SCEVUnknown>(this)->getType();
352 case scCouldNotCompute:
353 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
Dan Gohman534749b2010-11-17 22:27:42 +0000354 }
Benjamin Kramer987b8502014-02-11 19:02:55 +0000355 llvm_unreachable("Unknown SCEV kind!");
Dan Gohman534749b2010-11-17 22:27:42 +0000356}
357
Dan Gohmanbe928e32008-06-18 16:23:07 +0000358bool SCEV::isZero() const {
359 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
360 return SC->getValue()->isZero();
361 return false;
362}
363
Dan Gohmanba7f6d82009-05-18 15:22:39 +0000364bool SCEV::isOne() const {
365 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
366 return SC->getValue()->isOne();
367 return false;
368}
Chris Lattnerd934c702004-04-02 20:23:17 +0000369
Dan Gohman18a96bb2009-06-24 00:30:26 +0000370bool SCEV::isAllOnesValue() const {
371 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
Craig Topper79ab6432017-07-06 18:39:47 +0000372 return SC->getValue()->isMinusOne();
Dan Gohman18a96bb2009-06-24 00:30:26 +0000373 return false;
374}
375
Andrew Trick881a7762012-01-07 00:27:31 +0000376bool SCEV::isNonConstantNegative() const {
377 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(this);
378 if (!Mul) return false;
379
380 // If there is a constant factor, it will be first.
381 const SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0));
382 if (!SC) return false;
383
384 // Return true if the value is negative, this matches things like (-42 * V).
Sanjoy Das0de2fec2015-12-17 20:28:46 +0000385 return SC->getAPInt().isNegative();
Andrew Trick881a7762012-01-07 00:27:31 +0000386}
387
Owen Anderson04052ec2009-06-22 21:57:23 +0000388SCEVCouldNotCompute::SCEVCouldNotCompute() :
Dan Gohman24ceda82010-06-18 19:54:20 +0000389 SCEV(FoldingSetNodeIDRef(), scCouldNotCompute) {}
Dan Gohmanc5c85c02009-06-27 21:21:31 +0000390
Chris Lattnerd934c702004-04-02 20:23:17 +0000391bool SCEVCouldNotCompute::classof(const SCEV *S) {
392 return S->getSCEVType() == scCouldNotCompute;
393}
394
Dan Gohmanaf752342009-07-07 17:06:11 +0000395const SCEV *ScalarEvolution::getConstant(ConstantInt *V) {
Dan Gohmanc5c85c02009-06-27 21:21:31 +0000396 FoldingSetNodeID ID;
397 ID.AddInteger(scConstant);
398 ID.AddPointer(V);
Craig Topper9f008862014-04-15 04:59:12 +0000399 void *IP = nullptr;
Dan Gohmanc5c85c02009-06-27 21:21:31 +0000400 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
Dan Gohman24ceda82010-06-18 19:54:20 +0000401 SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V);
Dan Gohmanc5c85c02009-06-27 21:21:31 +0000402 UniqueSCEVs.InsertNode(S, IP);
403 return S;
Chris Lattnerb4f681b2004-04-15 15:07:24 +0000404}
Chris Lattnerd934c702004-04-02 20:23:17 +0000405
Nick Lewycky31eaca52014-01-27 10:04:03 +0000406const SCEV *ScalarEvolution::getConstant(const APInt &Val) {
Owen Andersonedb4a702009-07-24 23:12:02 +0000407 return getConstant(ConstantInt::get(getContext(), Val));
Dan Gohman0a76e7f2007-07-09 15:25:17 +0000408}
409
Dan Gohmanaf752342009-07-07 17:06:11 +0000410const SCEV *
Chris Lattner229907c2011-07-18 04:54:35 +0000411ScalarEvolution::getConstant(Type *Ty, uint64_t V, bool isSigned) {
412 IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty));
Dan Gohmana029cbe2010-04-21 16:04:04 +0000413 return getConstant(ConstantInt::get(ITy, V, isSigned));
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000414}
415
Dan Gohman24ceda82010-06-18 19:54:20 +0000416SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID,
Chris Lattner229907c2011-07-18 04:54:35 +0000417 unsigned SCEVTy, const SCEV *op, Type *ty)
Dan Gohman24ceda82010-06-18 19:54:20 +0000418 : SCEV(ID, SCEVTy), Op(op), Ty(ty) {}
Dan Gohmanc5c85c02009-06-27 21:21:31 +0000419
Dan Gohman24ceda82010-06-18 19:54:20 +0000420SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID,
Chris Lattner229907c2011-07-18 04:54:35 +0000421 const SCEV *op, Type *ty)
Dan Gohman24ceda82010-06-18 19:54:20 +0000422 : SCEVCastExpr(ID, scTruncate, op, ty) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000423 assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) &&
424 (Ty->isIntegerTy() || Ty->isPointerTy()) &&
Chris Lattnerb4f681b2004-04-15 15:07:24 +0000425 "Cannot truncate non-integer value!");
Chris Lattnerb4f681b2004-04-15 15:07:24 +0000426}
Chris Lattnerd934c702004-04-02 20:23:17 +0000427
Dan Gohman24ceda82010-06-18 19:54:20 +0000428SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
Chris Lattner229907c2011-07-18 04:54:35 +0000429 const SCEV *op, Type *ty)
Dan Gohman24ceda82010-06-18 19:54:20 +0000430 : SCEVCastExpr(ID, scZeroExtend, op, ty) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000431 assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) &&
432 (Ty->isIntegerTy() || Ty->isPointerTy()) &&
Chris Lattnerb4f681b2004-04-15 15:07:24 +0000433 "Cannot zero extend non-integer value!");
Chris Lattnerb4f681b2004-04-15 15:07:24 +0000434}
435
Dan Gohman24ceda82010-06-18 19:54:20 +0000436SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
Chris Lattner229907c2011-07-18 04:54:35 +0000437 const SCEV *op, Type *ty)
Dan Gohman24ceda82010-06-18 19:54:20 +0000438 : SCEVCastExpr(ID, scSignExtend, op, ty) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000439 assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) &&
440 (Ty->isIntegerTy() || Ty->isPointerTy()) &&
Dan Gohmancb9e09a2007-06-15 14:38:12 +0000441 "Cannot sign extend non-integer value!");
Dan Gohmancb9e09a2007-06-15 14:38:12 +0000442}
443
Dan Gohman7cac9572010-08-02 23:49:30 +0000444void SCEVUnknown::deleted() {
Dan Gohman761065e2010-11-17 02:44:44 +0000445 // Clear this SCEVUnknown from various maps.
Dan Gohman7e6b3932010-11-17 23:28:48 +0000446 SE->forgetMemoizedResults(this);
Dan Gohman7cac9572010-08-02 23:49:30 +0000447
448 // Remove this SCEVUnknown from the uniquing map.
449 SE->UniqueSCEVs.RemoveNode(this);
450
451 // Release the value.
Craig Topper9f008862014-04-15 04:59:12 +0000452 setValPtr(nullptr);
Dan Gohman7cac9572010-08-02 23:49:30 +0000453}
454
455void SCEVUnknown::allUsesReplacedWith(Value *New) {
Dan Gohman7cac9572010-08-02 23:49:30 +0000456 // Remove this SCEVUnknown from the uniquing map.
457 SE->UniqueSCEVs.RemoveNode(this);
458
459 // Update this SCEVUnknown to point to the new value. This is needed
460 // because there may still be outstanding SCEVs which still point to
461 // this SCEVUnknown.
462 setValPtr(New);
463}
464
Chris Lattner229907c2011-07-18 04:54:35 +0000465bool SCEVUnknown::isSizeOf(Type *&AllocTy) const {
Dan Gohman7cac9572010-08-02 23:49:30 +0000466 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
Dan Gohmancf913832010-01-28 02:15:55 +0000467 if (VCE->getOpcode() == Instruction::PtrToInt)
468 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
Dan Gohman7e5f1b22010-02-02 01:38:49 +0000469 if (CE->getOpcode() == Instruction::GetElementPtr &&
470 CE->getOperand(0)->isNullValue() &&
471 CE->getNumOperands() == 2)
472 if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(1)))
473 if (CI->isOne()) {
474 AllocTy = cast<PointerType>(CE->getOperand(0)->getType())
475 ->getElementType();
476 return true;
477 }
Dan Gohmancf913832010-01-28 02:15:55 +0000478
479 return false;
480}
481
Chris Lattner229907c2011-07-18 04:54:35 +0000482bool SCEVUnknown::isAlignOf(Type *&AllocTy) const {
Dan Gohman7cac9572010-08-02 23:49:30 +0000483 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
Dan Gohmancf913832010-01-28 02:15:55 +0000484 if (VCE->getOpcode() == Instruction::PtrToInt)
485 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
Dan Gohman7e5f1b22010-02-02 01:38:49 +0000486 if (CE->getOpcode() == Instruction::GetElementPtr &&
487 CE->getOperand(0)->isNullValue()) {
Chris Lattner229907c2011-07-18 04:54:35 +0000488 Type *Ty =
Dan Gohman7e5f1b22010-02-02 01:38:49 +0000489 cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
Chris Lattner229907c2011-07-18 04:54:35 +0000490 if (StructType *STy = dyn_cast<StructType>(Ty))
Dan Gohman7e5f1b22010-02-02 01:38:49 +0000491 if (!STy->isPacked() &&
492 CE->getNumOperands() == 3 &&
493 CE->getOperand(1)->isNullValue()) {
494 if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(2)))
495 if (CI->isOne() &&
496 STy->getNumElements() == 2 &&
Duncan Sands9dff9be2010-02-15 16:12:20 +0000497 STy->getElementType(0)->isIntegerTy(1)) {
Dan Gohman7e5f1b22010-02-02 01:38:49 +0000498 AllocTy = STy->getElementType(1);
499 return true;
500 }
501 }
502 }
Dan Gohmancf913832010-01-28 02:15:55 +0000503
504 return false;
505}
506
Chris Lattner229907c2011-07-18 04:54:35 +0000507bool SCEVUnknown::isOffsetOf(Type *&CTy, Constant *&FieldNo) const {
Dan Gohman7cac9572010-08-02 23:49:30 +0000508 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
Dan Gohmane5e1b7b2010-02-01 18:27:38 +0000509 if (VCE->getOpcode() == Instruction::PtrToInt)
510 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
511 if (CE->getOpcode() == Instruction::GetElementPtr &&
512 CE->getNumOperands() == 3 &&
513 CE->getOperand(0)->isNullValue() &&
514 CE->getOperand(1)->isNullValue()) {
Chris Lattner229907c2011-07-18 04:54:35 +0000515 Type *Ty =
Dan Gohmane5e1b7b2010-02-01 18:27:38 +0000516 cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
517 // Ignore vector types here so that ScalarEvolutionExpander doesn't
518 // emit getelementptrs that index into vectors.
Duncan Sands19d0b472010-02-16 11:11:14 +0000519 if (Ty->isStructTy() || Ty->isArrayTy()) {
Dan Gohmane5e1b7b2010-02-01 18:27:38 +0000520 CTy = Ty;
521 FieldNo = CE->getOperand(2);
522 return true;
523 }
524 }
525
526 return false;
527}
528
Chris Lattnereb3e8402004-06-20 06:23:15 +0000529//===----------------------------------------------------------------------===//
530// SCEV Utilities
531//===----------------------------------------------------------------------===//
532
Sanjoy Das17078692016-10-31 03:32:43 +0000533/// Compare the two values \p LV and \p RV in terms of their "complexity" where
534/// "complexity" is a partial (and somewhat ad-hoc) relation used to order
535/// operands in SCEV expressions. \p EqCache is a set of pairs of values that
536/// have been previously deemed to be "equally complex" by this routine. It is
537/// intended to avoid exponential time complexity in cases like:
538///
539/// %a = f(%x, %y)
540/// %b = f(%a, %a)
541/// %c = f(%b, %b)
542///
543/// %d = f(%x, %y)
544/// %e = f(%d, %d)
545/// %f = f(%e, %e)
546///
547/// CompareValueComplexity(%f, %c)
548///
549/// Since we do not continue running this routine on expression trees once we
550/// have seen unequal values, there is no need to track them in the cache.
551static int
Max Kazantsev1c66ae62017-12-06 08:58:16 +0000552CompareValueComplexity(EquivalenceClasses<const Value *> &EqCacheValue,
Sanjoy Das17078692016-10-31 03:32:43 +0000553 const LoopInfo *const LI, Value *LV, Value *RV,
Daniil Fukalov4c3322c2016-11-17 16:07:52 +0000554 unsigned Depth) {
Max Kazantsev1c66ae62017-12-06 08:58:16 +0000555 if (Depth > MaxValueCompareDepth || EqCacheValue.isEquivalent(LV, RV))
Sanjoy Das507dd402016-10-18 17:45:16 +0000556 return 0;
557
Sanjoy Das9cd877a2016-10-18 17:45:13 +0000558 // Order pointer values after integer values. This helps SCEVExpander form
559 // GEPs.
560 bool LIsPointer = LV->getType()->isPointerTy(),
561 RIsPointer = RV->getType()->isPointerTy();
562 if (LIsPointer != RIsPointer)
563 return (int)LIsPointer - (int)RIsPointer;
564
565 // Compare getValueID values.
566 unsigned LID = LV->getValueID(), RID = RV->getValueID();
567 if (LID != RID)
568 return (int)LID - (int)RID;
569
570 // Sort arguments by their position.
Sanjoy Dasb4830a82016-10-30 23:52:53 +0000571 if (const auto *LA = dyn_cast<Argument>(LV)) {
572 const auto *RA = cast<Argument>(RV);
Sanjoy Das9cd877a2016-10-18 17:45:13 +0000573 unsigned LArgNo = LA->getArgNo(), RArgNo = RA->getArgNo();
574 return (int)LArgNo - (int)RArgNo;
575 }
576
Sanjoy Das299e6722016-10-30 23:52:56 +0000577 if (const auto *LGV = dyn_cast<GlobalValue>(LV)) {
578 const auto *RGV = cast<GlobalValue>(RV);
579
580 const auto IsGVNameSemantic = [&](const GlobalValue *GV) {
581 auto LT = GV->getLinkage();
582 return !(GlobalValue::isPrivateLinkage(LT) ||
583 GlobalValue::isInternalLinkage(LT));
584 };
585
586 // Use the names to distinguish the two values, but only if the
587 // names are semantically important.
588 if (IsGVNameSemantic(LGV) && IsGVNameSemantic(RGV))
589 return LGV->getName().compare(RGV->getName());
590 }
591
Sanjoy Das9cd877a2016-10-18 17:45:13 +0000592 // For instructions, compare their loop depth, and their operand count. This
593 // is pretty loose.
Sanjoy Dasb4830a82016-10-30 23:52:53 +0000594 if (const auto *LInst = dyn_cast<Instruction>(LV)) {
595 const auto *RInst = cast<Instruction>(RV);
Sanjoy Das9cd877a2016-10-18 17:45:13 +0000596
597 // Compare loop depths.
598 const BasicBlock *LParent = LInst->getParent(),
599 *RParent = RInst->getParent();
600 if (LParent != RParent) {
601 unsigned LDepth = LI->getLoopDepth(LParent),
602 RDepth = LI->getLoopDepth(RParent);
603 if (LDepth != RDepth)
604 return (int)LDepth - (int)RDepth;
605 }
606
607 // Compare the number of operands.
608 unsigned LNumOps = LInst->getNumOperands(),
609 RNumOps = RInst->getNumOperands();
Sanjoy Das17078692016-10-31 03:32:43 +0000610 if (LNumOps != RNumOps)
Sanjoy Das507dd402016-10-18 17:45:16 +0000611 return (int)LNumOps - (int)RNumOps;
612
Sanjoy Das17078692016-10-31 03:32:43 +0000613 for (unsigned Idx : seq(0u, LNumOps)) {
614 int Result =
Max Kazantsev1c66ae62017-12-06 08:58:16 +0000615 CompareValueComplexity(EqCacheValue, LI, LInst->getOperand(Idx),
Daniil Fukalov4c3322c2016-11-17 16:07:52 +0000616 RInst->getOperand(Idx), Depth + 1);
Sanjoy Das17078692016-10-31 03:32:43 +0000617 if (Result != 0)
Daniil Fukalove8703982016-11-16 16:41:40 +0000618 return Result;
Sanjoy Das17078692016-10-31 03:32:43 +0000619 }
Sanjoy Das9cd877a2016-10-18 17:45:13 +0000620 }
621
Max Kazantsev1c66ae62017-12-06 08:58:16 +0000622 EqCacheValue.unionSets(LV, RV);
Sanjoy Das9cd877a2016-10-18 17:45:13 +0000623 return 0;
624}
625
Sanjoy Das237c8452016-09-27 18:01:48 +0000626// Return negative, zero, or positive, if LHS is less than, equal to, or greater
627// than RHS, respectively. A three-way result allows recursive comparisons to be
628// more efficient.
Daniil Fukalov4c3322c2016-11-17 16:07:52 +0000629static int CompareSCEVComplexity(
Max Kazantsevcf9b1b22017-11-28 07:48:12 +0000630 EquivalenceClasses<const SCEV *> &EqCacheSCEV,
Max Kazantsev1c66ae62017-12-06 08:58:16 +0000631 EquivalenceClasses<const Value *> &EqCacheValue,
Daniil Fukalov4c3322c2016-11-17 16:07:52 +0000632 const LoopInfo *const LI, const SCEV *LHS, const SCEV *RHS,
Max Kazantsevb09b5db2017-05-16 07:27:06 +0000633 DominatorTree &DT, unsigned Depth = 0) {
Sanjoy Das237c8452016-09-27 18:01:48 +0000634 // Fast-path: SCEVs are uniqued so we can do a quick equality check.
635 if (LHS == RHS)
636 return 0;
Dan Gohman9ba542c2009-05-07 14:39:04 +0000637
Sanjoy Das237c8452016-09-27 18:01:48 +0000638 // Primarily, sort the SCEVs by their getSCEVType().
639 unsigned LType = LHS->getSCEVType(), RType = RHS->getSCEVType();
640 if (LType != RType)
641 return (int)LType - (int)RType;
Dan Gohman27065672010-08-27 15:26:01 +0000642
Max Kazantsevcf9b1b22017-11-28 07:48:12 +0000643 if (Depth > MaxSCEVCompareDepth || EqCacheSCEV.isEquivalent(LHS, RHS))
Daniil Fukalov4c3322c2016-11-17 16:07:52 +0000644 return 0;
Sanjoy Das237c8452016-09-27 18:01:48 +0000645 // Aside from the getSCEVType() ordering, the particular ordering
646 // isn't very important except that it's beneficial to be consistent,
647 // so that (a + b) and (b + a) don't end up as different expressions.
648 switch (static_cast<SCEVTypes>(LType)) {
649 case scUnknown: {
650 const SCEVUnknown *LU = cast<SCEVUnknown>(LHS);
651 const SCEVUnknown *RU = cast<SCEVUnknown>(RHS);
Dan Gohmancc2f1eb2009-08-31 21:15:23 +0000652
Max Kazantsev1c66ae62017-12-06 08:58:16 +0000653 int X = CompareValueComplexity(EqCacheValue, LI, LU->getValue(),
654 RU->getValue(), Depth + 1);
Daniil Fukalov4c3322c2016-11-17 16:07:52 +0000655 if (X == 0)
Max Kazantsevcf9b1b22017-11-28 07:48:12 +0000656 EqCacheSCEV.unionSets(LHS, RHS);
Daniil Fukalov4c3322c2016-11-17 16:07:52 +0000657 return X;
Sanjoy Das237c8452016-09-27 18:01:48 +0000658 }
Sanjoy Das7881abd2015-12-08 04:32:51 +0000659
Sanjoy Das237c8452016-09-27 18:01:48 +0000660 case scConstant: {
661 const SCEVConstant *LC = cast<SCEVConstant>(LHS);
662 const SCEVConstant *RC = cast<SCEVConstant>(RHS);
663
664 // Compare constant values.
665 const APInt &LA = LC->getAPInt();
666 const APInt &RA = RC->getAPInt();
667 unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.getBitWidth();
668 if (LBitWidth != RBitWidth)
669 return (int)LBitWidth - (int)RBitWidth;
670 return LA.ult(RA) ? -1 : 1;
671 }
672
673 case scAddRecExpr: {
674 const SCEVAddRecExpr *LA = cast<SCEVAddRecExpr>(LHS);
675 const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS);
676
Max Kazantsev4c7f2932017-05-17 04:09:14 +0000677 // There is always a dominance between two recs that are used by one SCEV,
678 // so we can safely sort recs by loop header dominance. We require such
679 // order in getAddExpr.
Sanjoy Das237c8452016-09-27 18:01:48 +0000680 const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop();
681 if (LLoop != RLoop) {
Max Kazantsevb09b5db2017-05-16 07:27:06 +0000682 const BasicBlock *LHead = LLoop->getHeader(), *RHead = RLoop->getHeader();
683 assert(LHead != RHead && "Two loops share the same header?");
684 if (DT.dominates(LHead, RHead))
685 return 1;
Max Kazantsev4c7f2932017-05-17 04:09:14 +0000686 else
687 assert(DT.dominates(RHead, LHead) &&
688 "No dominance between recurrences used by one SCEV?");
689 return -1;
Sanjoy Das237c8452016-09-27 18:01:48 +0000690 }
691
692 // Addrec complexity grows with operand count.
693 unsigned LNumOps = LA->getNumOperands(), RNumOps = RA->getNumOperands();
694 if (LNumOps != RNumOps)
695 return (int)LNumOps - (int)RNumOps;
696
Max Kazantsevd4f59872017-12-06 12:44:56 +0000697 // Compare NoWrap flags.
698 if (LA->getNoWrapFlags() != RA->getNoWrapFlags())
699 return (int)LA->getNoWrapFlags() - (int)RA->getNoWrapFlags();
700
Sanjoy Das237c8452016-09-27 18:01:48 +0000701 // Lexicographically compare.
702 for (unsigned i = 0; i != LNumOps; ++i) {
Max Kazantsev1c66ae62017-12-06 08:58:16 +0000703 int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI,
704 LA->getOperand(i), RA->getOperand(i), DT,
705 Depth + 1);
Sanjoy Das7881abd2015-12-08 04:32:51 +0000706 if (X != 0)
707 return X;
Sanjoy Das7881abd2015-12-08 04:32:51 +0000708 }
Max Kazantsevcf9b1b22017-11-28 07:48:12 +0000709 EqCacheSCEV.unionSets(LHS, RHS);
Sanjoy Das237c8452016-09-27 18:01:48 +0000710 return 0;
Sanjoy Das7881abd2015-12-08 04:32:51 +0000711 }
Sanjoy Das237c8452016-09-27 18:01:48 +0000712
713 case scAddExpr:
714 case scMulExpr:
715 case scSMaxExpr:
716 case scUMaxExpr: {
717 const SCEVNAryExpr *LC = cast<SCEVNAryExpr>(LHS);
718 const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS);
719
720 // Lexicographically compare n-ary expressions.
721 unsigned LNumOps = LC->getNumOperands(), RNumOps = RC->getNumOperands();
722 if (LNumOps != RNumOps)
723 return (int)LNumOps - (int)RNumOps;
724
Max Kazantsevd4f59872017-12-06 12:44:56 +0000725 // Compare NoWrap flags.
726 if (LC->getNoWrapFlags() != RC->getNoWrapFlags())
727 return (int)LC->getNoWrapFlags() - (int)RC->getNoWrapFlags();
728
Sanjoy Das237c8452016-09-27 18:01:48 +0000729 for (unsigned i = 0; i != LNumOps; ++i) {
Max Kazantsev1c66ae62017-12-06 08:58:16 +0000730 int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI,
731 LC->getOperand(i), RC->getOperand(i), DT,
732 Depth + 1);
Sanjoy Das237c8452016-09-27 18:01:48 +0000733 if (X != 0)
734 return X;
735 }
Max Kazantsevcf9b1b22017-11-28 07:48:12 +0000736 EqCacheSCEV.unionSets(LHS, RHS);
Daniil Fukalov4c3322c2016-11-17 16:07:52 +0000737 return 0;
Sanjoy Das237c8452016-09-27 18:01:48 +0000738 }
739
740 case scUDivExpr: {
741 const SCEVUDivExpr *LC = cast<SCEVUDivExpr>(LHS);
742 const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS);
743
744 // Lexicographically compare udiv expressions.
Max Kazantsev1c66ae62017-12-06 08:58:16 +0000745 int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getLHS(),
746 RC->getLHS(), DT, Depth + 1);
Sanjoy Das237c8452016-09-27 18:01:48 +0000747 if (X != 0)
748 return X;
Max Kazantsev1c66ae62017-12-06 08:58:16 +0000749 X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getRHS(),
750 RC->getRHS(), DT, Depth + 1);
Daniil Fukalov4c3322c2016-11-17 16:07:52 +0000751 if (X == 0)
Max Kazantsevcf9b1b22017-11-28 07:48:12 +0000752 EqCacheSCEV.unionSets(LHS, RHS);
Daniil Fukalov4c3322c2016-11-17 16:07:52 +0000753 return X;
Sanjoy Das237c8452016-09-27 18:01:48 +0000754 }
755
756 case scTruncate:
757 case scZeroExtend:
758 case scSignExtend: {
759 const SCEVCastExpr *LC = cast<SCEVCastExpr>(LHS);
760 const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS);
761
762 // Compare cast expressions by operand.
Max Kazantsev1c66ae62017-12-06 08:58:16 +0000763 int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI,
764 LC->getOperand(), RC->getOperand(), DT,
765 Depth + 1);
Daniil Fukalov4c3322c2016-11-17 16:07:52 +0000766 if (X == 0)
Max Kazantsevcf9b1b22017-11-28 07:48:12 +0000767 EqCacheSCEV.unionSets(LHS, RHS);
Daniil Fukalov4c3322c2016-11-17 16:07:52 +0000768 return X;
Sanjoy Das237c8452016-09-27 18:01:48 +0000769 }
770
771 case scCouldNotCompute:
772 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
773 }
774 llvm_unreachable("Unknown SCEV kind!");
775}
Chris Lattnereb3e8402004-06-20 06:23:15 +0000776
Sanjoy Dasf8570812016-05-29 00:38:22 +0000777/// Given a list of SCEV objects, order them by their complexity, and group
778/// objects of the same complexity together by value. When this routine is
779/// finished, we know that any duplicates in the vector are consecutive and that
780/// complexity is monotonically increasing.
Chris Lattnereb3e8402004-06-20 06:23:15 +0000781///
Dan Gohman8b0a4192010-03-01 17:49:51 +0000782/// Note that we go take special precautions to ensure that we get deterministic
Chris Lattnereb3e8402004-06-20 06:23:15 +0000783/// results from this routine. In other words, we don't want the results of
784/// this to depend on where the addresses of various SCEV objects happened to
785/// land in memory.
Dan Gohmanaf752342009-07-07 17:06:11 +0000786static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops,
Max Kazantsevb09b5db2017-05-16 07:27:06 +0000787 LoopInfo *LI, DominatorTree &DT) {
Chris Lattnereb3e8402004-06-20 06:23:15 +0000788 if (Ops.size() < 2) return; // Noop
Daniil Fukalov4c3322c2016-11-17 16:07:52 +0000789
Max Kazantsev1c66ae62017-12-06 08:58:16 +0000790 EquivalenceClasses<const SCEV *> EqCacheSCEV;
791 EquivalenceClasses<const Value *> EqCacheValue;
Chris Lattnereb3e8402004-06-20 06:23:15 +0000792 if (Ops.size() == 2) {
793 // This is the common case, which also happens to be trivially simple.
794 // Special case it.
Dan Gohman7712d292010-08-29 15:07:13 +0000795 const SCEV *&LHS = Ops[0], *&RHS = Ops[1];
Max Kazantsev1c66ae62017-12-06 08:58:16 +0000796 if (CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, RHS, LHS, DT) < 0)
Dan Gohman7712d292010-08-29 15:07:13 +0000797 std::swap(LHS, RHS);
Chris Lattnereb3e8402004-06-20 06:23:15 +0000798 return;
799 }
800
Dan Gohman24ceda82010-06-18 19:54:20 +0000801 // Do the rough sort by complexity.
Sanjoy Das237c8452016-09-27 18:01:48 +0000802 std::stable_sort(Ops.begin(), Ops.end(),
Max Kazantsev1c66ae62017-12-06 08:58:16 +0000803 [&](const SCEV *LHS, const SCEV *RHS) {
804 return CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI,
805 LHS, RHS, DT) < 0;
Sanjoy Das237c8452016-09-27 18:01:48 +0000806 });
Dan Gohman24ceda82010-06-18 19:54:20 +0000807
808 // Now that we are sorted by complexity, group elements of the same
809 // complexity. Note that this is, at worst, N^2, but the vector is likely to
810 // be extremely short in practice. Note that we take this approach because we
811 // do not want to depend on the addresses of the objects we are grouping.
812 for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
813 const SCEV *S = Ops[i];
814 unsigned Complexity = S->getSCEVType();
815
816 // If there are any objects of the same complexity and same value as this
817 // one, group them.
818 for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
819 if (Ops[j] == S) { // Found a duplicate.
820 // Move it to immediately after i'th element.
821 std::swap(Ops[i+1], Ops[j]);
822 ++i; // no need to rescan it.
823 if (i == e-2) return; // Done!
824 }
825 }
826 }
Chris Lattnereb3e8402004-06-20 06:23:15 +0000827}
828
Mark Heffernan2beab5f2014-10-10 17:39:11 +0000829// Returns the size of the SCEV S.
830static inline int sizeOfSCEV(const SCEV *S) {
Sanjoy Das7d752672015-12-08 04:32:54 +0000831 struct FindSCEVSize {
Eugene Zelenkobe709f22017-08-18 23:51:26 +0000832 int Size = 0;
833
834 FindSCEVSize() = default;
Sanjoy Das7d752672015-12-08 04:32:54 +0000835
836 bool follow(const SCEV *S) {
837 ++Size;
838 // Keep looking at all operands of S.
839 return true;
840 }
Eugene Zelenkobe709f22017-08-18 23:51:26 +0000841
Sanjoy Das7d752672015-12-08 04:32:54 +0000842 bool isDone() const {
843 return false;
844 }
845 };
846
Mark Heffernan2beab5f2014-10-10 17:39:11 +0000847 FindSCEVSize F;
848 SCEVTraversal<FindSCEVSize> ST(F);
849 ST.visitAll(S);
850 return F.Size;
851}
852
853namespace {
854
David Majnemer4e879362014-12-14 09:12:33 +0000855struct SCEVDivision : public SCEVVisitor<SCEVDivision, void> {
Mark Heffernan2beab5f2014-10-10 17:39:11 +0000856public:
857 // Computes the Quotient and Remainder of the division of Numerator by
858 // Denominator.
859 static void divide(ScalarEvolution &SE, const SCEV *Numerator,
860 const SCEV *Denominator, const SCEV **Quotient,
861 const SCEV **Remainder) {
862 assert(Numerator && Denominator && "Uninitialized SCEV");
863
David Majnemer4e879362014-12-14 09:12:33 +0000864 SCEVDivision D(SE, Numerator, Denominator);
Mark Heffernan2beab5f2014-10-10 17:39:11 +0000865
866 // Check for the trivial case here to avoid having to check for it in the
867 // rest of the code.
868 if (Numerator == Denominator) {
869 *Quotient = D.One;
870 *Remainder = D.Zero;
871 return;
872 }
873
874 if (Numerator->isZero()) {
875 *Quotient = D.Zero;
876 *Remainder = D.Zero;
877 return;
878 }
879
Brendon Cahoona57cc8b2015-04-20 16:03:28 +0000880 // A simple case when N/1. The quotient is N.
881 if (Denominator->isOne()) {
882 *Quotient = Numerator;
883 *Remainder = D.Zero;
884 return;
885 }
886
Mark Heffernan2beab5f2014-10-10 17:39:11 +0000887 // Split the Denominator when it is a product.
Sanjoy Dasb277a422016-06-15 06:53:55 +0000888 if (const SCEVMulExpr *T = dyn_cast<SCEVMulExpr>(Denominator)) {
Mark Heffernan2beab5f2014-10-10 17:39:11 +0000889 const SCEV *Q, *R;
890 *Quotient = Numerator;
891 for (const SCEV *Op : T->operands()) {
892 divide(SE, *Quotient, Op, &Q, &R);
893 *Quotient = Q;
894
895 // Bail out when the Numerator is not divisible by one of the terms of
896 // the Denominator.
897 if (!R->isZero()) {
898 *Quotient = D.Zero;
899 *Remainder = Numerator;
900 return;
901 }
902 }
903 *Remainder = D.Zero;
904 return;
905 }
906
907 D.visit(Numerator);
908 *Quotient = D.Quotient;
909 *Remainder = D.Remainder;
910 }
911
Mark Heffernan2beab5f2014-10-10 17:39:11 +0000912 // Except in the trivial case described above, we do not know how to divide
913 // Expr by Denominator for the following functions with empty implementation.
914 void visitTruncateExpr(const SCEVTruncateExpr *Numerator) {}
915 void visitZeroExtendExpr(const SCEVZeroExtendExpr *Numerator) {}
916 void visitSignExtendExpr(const SCEVSignExtendExpr *Numerator) {}
917 void visitUDivExpr(const SCEVUDivExpr *Numerator) {}
918 void visitSMaxExpr(const SCEVSMaxExpr *Numerator) {}
919 void visitUMaxExpr(const SCEVUMaxExpr *Numerator) {}
920 void visitUnknown(const SCEVUnknown *Numerator) {}
921 void visitCouldNotCompute(const SCEVCouldNotCompute *Numerator) {}
922
David Majnemer4e879362014-12-14 09:12:33 +0000923 void visitConstant(const SCEVConstant *Numerator) {
924 if (const SCEVConstant *D = dyn_cast<SCEVConstant>(Denominator)) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +0000925 APInt NumeratorVal = Numerator->getAPInt();
926 APInt DenominatorVal = D->getAPInt();
David Majnemer4e879362014-12-14 09:12:33 +0000927 uint32_t NumeratorBW = NumeratorVal.getBitWidth();
928 uint32_t DenominatorBW = DenominatorVal.getBitWidth();
929
930 if (NumeratorBW > DenominatorBW)
931 DenominatorVal = DenominatorVal.sext(NumeratorBW);
932 else if (NumeratorBW < DenominatorBW)
933 NumeratorVal = NumeratorVal.sext(DenominatorBW);
934
935 APInt QuotientVal(NumeratorVal.getBitWidth(), 0);
936 APInt RemainderVal(NumeratorVal.getBitWidth(), 0);
937 APInt::sdivrem(NumeratorVal, DenominatorVal, QuotientVal, RemainderVal);
938 Quotient = SE.getConstant(QuotientVal);
939 Remainder = SE.getConstant(RemainderVal);
940 return;
941 }
942 }
943
Mark Heffernan2beab5f2014-10-10 17:39:11 +0000944 void visitAddRecExpr(const SCEVAddRecExpr *Numerator) {
945 const SCEV *StartQ, *StartR, *StepQ, *StepR;
Matthew Simpsonddb4d972015-09-10 18:12:47 +0000946 if (!Numerator->isAffine())
947 return cannotDivide(Numerator);
Mark Heffernan2beab5f2014-10-10 17:39:11 +0000948 divide(SE, Numerator->getStart(), Denominator, &StartQ, &StartR);
949 divide(SE, Numerator->getStepRecurrence(SE), Denominator, &StepQ, &StepR);
Brendon Cahoonf9751ad2015-04-22 15:06:40 +0000950 // Bail out if the types do not match.
951 Type *Ty = Denominator->getType();
952 if (Ty != StartQ->getType() || Ty != StartR->getType() ||
Matthew Simpsonddb4d972015-09-10 18:12:47 +0000953 Ty != StepQ->getType() || Ty != StepR->getType())
954 return cannotDivide(Numerator);
Mark Heffernan2beab5f2014-10-10 17:39:11 +0000955 Quotient = SE.getAddRecExpr(StartQ, StepQ, Numerator->getLoop(),
956 Numerator->getNoWrapFlags());
957 Remainder = SE.getAddRecExpr(StartR, StepR, Numerator->getLoop(),
958 Numerator->getNoWrapFlags());
959 }
960
961 void visitAddExpr(const SCEVAddExpr *Numerator) {
962 SmallVector<const SCEV *, 2> Qs, Rs;
963 Type *Ty = Denominator->getType();
964
965 for (const SCEV *Op : Numerator->operands()) {
966 const SCEV *Q, *R;
967 divide(SE, Op, Denominator, &Q, &R);
968
969 // Bail out if types do not match.
Matthew Simpsonddb4d972015-09-10 18:12:47 +0000970 if (Ty != Q->getType() || Ty != R->getType())
971 return cannotDivide(Numerator);
Mark Heffernan2beab5f2014-10-10 17:39:11 +0000972
973 Qs.push_back(Q);
974 Rs.push_back(R);
975 }
976
977 if (Qs.size() == 1) {
978 Quotient = Qs[0];
979 Remainder = Rs[0];
980 return;
981 }
982
983 Quotient = SE.getAddExpr(Qs);
984 Remainder = SE.getAddExpr(Rs);
985 }
986
987 void visitMulExpr(const SCEVMulExpr *Numerator) {
988 SmallVector<const SCEV *, 2> Qs;
989 Type *Ty = Denominator->getType();
990
991 bool FoundDenominatorTerm = false;
992 for (const SCEV *Op : Numerator->operands()) {
993 // Bail out if types do not match.
Matthew Simpsonddb4d972015-09-10 18:12:47 +0000994 if (Ty != Op->getType())
995 return cannotDivide(Numerator);
Mark Heffernan2beab5f2014-10-10 17:39:11 +0000996
997 if (FoundDenominatorTerm) {
998 Qs.push_back(Op);
999 continue;
1000 }
1001
1002 // Check whether Denominator divides one of the product operands.
1003 const SCEV *Q, *R;
1004 divide(SE, Op, Denominator, &Q, &R);
1005 if (!R->isZero()) {
1006 Qs.push_back(Op);
1007 continue;
1008 }
1009
1010 // Bail out if types do not match.
Matthew Simpsonddb4d972015-09-10 18:12:47 +00001011 if (Ty != Q->getType())
1012 return cannotDivide(Numerator);
Mark Heffernan2beab5f2014-10-10 17:39:11 +00001013
1014 FoundDenominatorTerm = true;
1015 Qs.push_back(Q);
1016 }
1017
1018 if (FoundDenominatorTerm) {
1019 Remainder = Zero;
1020 if (Qs.size() == 1)
1021 Quotient = Qs[0];
1022 else
1023 Quotient = SE.getMulExpr(Qs);
1024 return;
1025 }
1026
Matthew Simpsonddb4d972015-09-10 18:12:47 +00001027 if (!isa<SCEVUnknown>(Denominator))
1028 return cannotDivide(Numerator);
Mark Heffernan2beab5f2014-10-10 17:39:11 +00001029
1030 // The Remainder is obtained by replacing Denominator by 0 in Numerator.
1031 ValueToValueMap RewriteMap;
1032 RewriteMap[cast<SCEVUnknown>(Denominator)->getValue()] =
1033 cast<SCEVConstant>(Zero)->getValue();
1034 Remainder = SCEVParameterRewriter::rewrite(Numerator, SE, RewriteMap, true);
1035
1036 if (Remainder->isZero()) {
1037 // The Quotient is obtained by replacing Denominator by 1 in Numerator.
1038 RewriteMap[cast<SCEVUnknown>(Denominator)->getValue()] =
1039 cast<SCEVConstant>(One)->getValue();
1040 Quotient =
1041 SCEVParameterRewriter::rewrite(Numerator, SE, RewriteMap, true);
1042 return;
1043 }
1044
1045 // Quotient is (Numerator - Remainder) divided by Denominator.
1046 const SCEV *Q, *R;
1047 const SCEV *Diff = SE.getMinusSCEV(Numerator, Remainder);
Matthew Simpsonddb4d972015-09-10 18:12:47 +00001048 // This SCEV does not seem to simplify: fail the division here.
1049 if (sizeOfSCEV(Diff) > sizeOfSCEV(Numerator))
1050 return cannotDivide(Numerator);
Mark Heffernan2beab5f2014-10-10 17:39:11 +00001051 divide(SE, Diff, Denominator, &Q, &R);
Matthew Simpsonddb4d972015-09-10 18:12:47 +00001052 if (R != Zero)
1053 return cannotDivide(Numerator);
Mark Heffernan2beab5f2014-10-10 17:39:11 +00001054 Quotient = Q;
1055 }
1056
1057private:
David Majnemer5d2670c2014-11-17 11:27:45 +00001058 SCEVDivision(ScalarEvolution &S, const SCEV *Numerator,
1059 const SCEV *Denominator)
1060 : SE(S), Denominator(Denominator) {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00001061 Zero = SE.getZero(Denominator->getType());
1062 One = SE.getOne(Denominator->getType());
David Majnemer5d2670c2014-11-17 11:27:45 +00001063
Matthew Simpsonddb4d972015-09-10 18:12:47 +00001064 // We generally do not know how to divide Expr by Denominator. We
1065 // initialize the division to a "cannot divide" state to simplify the rest
1066 // of the code.
1067 cannotDivide(Numerator);
1068 }
1069
1070 // Convenience function for giving up on the division. We set the quotient to
1071 // be equal to zero and the remainder to be equal to the numerator.
1072 void cannotDivide(const SCEV *Numerator) {
David Majnemer5d2670c2014-11-17 11:27:45 +00001073 Quotient = Zero;
1074 Remainder = Numerator;
1075 }
1076
Mark Heffernan2beab5f2014-10-10 17:39:11 +00001077 ScalarEvolution &SE;
1078 const SCEV *Denominator, *Quotient, *Remainder, *Zero, *One;
David Majnemer32b8ccf2014-11-16 20:35:19 +00001079};
1080
Eugene Zelenkobe709f22017-08-18 23:51:26 +00001081} // end anonymous namespace
Mark Heffernan2beab5f2014-10-10 17:39:11 +00001082
Chris Lattnerd934c702004-04-02 20:23:17 +00001083//===----------------------------------------------------------------------===//
1084// Simple SCEV method implementations
1085//===----------------------------------------------------------------------===//
1086
Sanjoy Dasf8570812016-05-29 00:38:22 +00001087/// Compute BC(It, K). The result has width W. Assume, K > 0.
Dan Gohmanaf752342009-07-07 17:06:11 +00001088static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K,
Dan Gohman32291b12009-07-21 00:38:55 +00001089 ScalarEvolution &SE,
Nick Lewycky702cf1e2011-09-06 06:39:54 +00001090 Type *ResultTy) {
Eli Friedman61f67622008-08-04 23:49:06 +00001091 // Handle the simplest case efficiently.
1092 if (K == 1)
1093 return SE.getTruncateOrZeroExtend(It, ResultTy);
1094
Wojciech Matyjewiczd2d97642008-02-11 11:03:14 +00001095 // We are using the following formula for BC(It, K):
1096 //
1097 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K!
1098 //
Eli Friedman61f67622008-08-04 23:49:06 +00001099 // Suppose, W is the bitwidth of the return value. We must be prepared for
1100 // overflow. Hence, we must assure that the result of our computation is
1101 // equal to the accurate one modulo 2^W. Unfortunately, division isn't
1102 // safe in modular arithmetic.
Wojciech Matyjewiczd2d97642008-02-11 11:03:14 +00001103 //
Eli Friedman61f67622008-08-04 23:49:06 +00001104 // However, this code doesn't use exactly that formula; the formula it uses
Dan Gohmance973df2009-06-24 04:48:43 +00001105 // is something like the following, where T is the number of factors of 2 in
Eli Friedman61f67622008-08-04 23:49:06 +00001106 // K! (i.e. trailing zeros in the binary representation of K!), and ^ is
1107 // exponentiation:
Wojciech Matyjewiczd2d97642008-02-11 11:03:14 +00001108 //
Eli Friedman61f67622008-08-04 23:49:06 +00001109 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T)
Wojciech Matyjewiczd2d97642008-02-11 11:03:14 +00001110 //
Eli Friedman61f67622008-08-04 23:49:06 +00001111 // This formula is trivially equivalent to the previous formula. However,
1112 // this formula can be implemented much more efficiently. The trick is that
1113 // K! / 2^T is odd, and exact division by an odd number *is* safe in modular
1114 // arithmetic. To do exact division in modular arithmetic, all we have
1115 // to do is multiply by the inverse. Therefore, this step can be done at
1116 // width W.
Dan Gohmance973df2009-06-24 04:48:43 +00001117 //
Eli Friedman61f67622008-08-04 23:49:06 +00001118 // The next issue is how to safely do the division by 2^T. The way this
1119 // is done is by doing the multiplication step at a width of at least W + T
1120 // bits. This way, the bottom W+T bits of the product are accurate. Then,
1121 // when we perform the division by 2^T (which is equivalent to a right shift
1122 // by T), the bottom W bits are accurate. Extra bits are okay; they'll get
1123 // truncated out after the division by 2^T.
1124 //
1125 // In comparison to just directly using the first formula, this technique
1126 // is much more efficient; using the first formula requires W * K bits,
1127 // but this formula less than W + K bits. Also, the first formula requires
1128 // a division step, whereas this formula only requires multiplies and shifts.
1129 //
1130 // It doesn't matter whether the subtraction step is done in the calculation
1131 // width or the input iteration count's width; if the subtraction overflows,
1132 // the result must be zero anyway. We prefer here to do it in the width of
1133 // the induction variable because it helps a lot for certain cases; CodeGen
1134 // isn't smart enough to ignore the overflow, which leads to much less
1135 // efficient code if the width of the subtraction is wider than the native
1136 // register width.
1137 //
1138 // (It's possible to not widen at all by pulling out factors of 2 before
1139 // the multiplication; for example, K=2 can be calculated as
1140 // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires
1141 // extra arithmetic, so it's not an obvious win, and it gets
1142 // much more complicated for K > 3.)
Wojciech Matyjewiczd2d97642008-02-11 11:03:14 +00001143
Eli Friedman61f67622008-08-04 23:49:06 +00001144 // Protection from insane SCEVs; this bound is conservative,
1145 // but it probably doesn't matter.
1146 if (K > 1000)
Dan Gohman31efa302009-04-18 17:58:19 +00001147 return SE.getCouldNotCompute();
Wojciech Matyjewiczd2d97642008-02-11 11:03:14 +00001148
Dan Gohmanb397e1a2009-04-21 01:07:12 +00001149 unsigned W = SE.getTypeSizeInBits(ResultTy);
Wojciech Matyjewiczd2d97642008-02-11 11:03:14 +00001150
Eli Friedman61f67622008-08-04 23:49:06 +00001151 // Calculate K! / 2^T and T; we divide out the factors of two before
1152 // multiplying for calculating K! / 2^T to avoid overflow.
1153 // Other overflow doesn't matter because we only care about the bottom
1154 // W bits of the result.
1155 APInt OddFactorial(W, 1);
1156 unsigned T = 1;
1157 for (unsigned i = 3; i <= K; ++i) {
1158 APInt Mult(W, i);
1159 unsigned TwoFactors = Mult.countTrailingZeros();
1160 T += TwoFactors;
Craig Topperfc947bc2017-04-18 17:14:21 +00001161 Mult.lshrInPlace(TwoFactors);
Eli Friedman61f67622008-08-04 23:49:06 +00001162 OddFactorial *= Mult;
Chris Lattnerd934c702004-04-02 20:23:17 +00001163 }
Nick Lewyckyed169d52008-06-13 04:38:55 +00001164
Eli Friedman61f67622008-08-04 23:49:06 +00001165 // We need at least W + T bits for the multiplication step
Nick Lewycky21add8f2009-01-25 08:16:27 +00001166 unsigned CalculationBits = W + T;
Eli Friedman61f67622008-08-04 23:49:06 +00001167
Dan Gohman8b0a4192010-03-01 17:49:51 +00001168 // Calculate 2^T, at width T+W.
Benjamin Kramerfc3ea6f2013-07-11 16:05:50 +00001169 APInt DivFactor = APInt::getOneBitSet(CalculationBits, T);
Eli Friedman61f67622008-08-04 23:49:06 +00001170
1171 // Calculate the multiplicative inverse of K! / 2^T;
1172 // this multiplication factor will perform the exact division by
1173 // K! / 2^T.
1174 APInt Mod = APInt::getSignedMinValue(W+1);
1175 APInt MultiplyFactor = OddFactorial.zext(W+1);
1176 MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod);
1177 MultiplyFactor = MultiplyFactor.trunc(W);
1178
1179 // Calculate the product, at width T+W
Chris Lattner229907c2011-07-18 04:54:35 +00001180 IntegerType *CalculationTy = IntegerType::get(SE.getContext(),
Owen Anderson55f1c092009-08-13 21:58:54 +00001181 CalculationBits);
Dan Gohmanaf752342009-07-07 17:06:11 +00001182 const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy);
Eli Friedman61f67622008-08-04 23:49:06 +00001183 for (unsigned i = 1; i != K; ++i) {
Dan Gohman1d2ded72010-05-03 22:09:21 +00001184 const SCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i));
Eli Friedman61f67622008-08-04 23:49:06 +00001185 Dividend = SE.getMulExpr(Dividend,
1186 SE.getTruncateOrZeroExtend(S, CalculationTy));
1187 }
1188
1189 // Divide by 2^T
Dan Gohmanaf752342009-07-07 17:06:11 +00001190 const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor));
Eli Friedman61f67622008-08-04 23:49:06 +00001191
1192 // Truncate the result, and divide by K! / 2^T.
1193
1194 return SE.getMulExpr(SE.getConstant(MultiplyFactor),
1195 SE.getTruncateOrZeroExtend(DivResult, ResultTy));
Chris Lattnerd934c702004-04-02 20:23:17 +00001196}
1197
Sanjoy Dasf8570812016-05-29 00:38:22 +00001198/// Return the value of this chain of recurrences at the specified iteration
1199/// number. We can evaluate this recurrence by multiplying each element in the
1200/// chain by the binomial coefficient corresponding to it. In other words, we
1201/// can evaluate {A,+,B,+,C,+,D} as:
Chris Lattnerd934c702004-04-02 20:23:17 +00001202///
Wojciech Matyjewiczd2d97642008-02-11 11:03:14 +00001203/// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3)
Chris Lattnerd934c702004-04-02 20:23:17 +00001204///
Wojciech Matyjewiczd2d97642008-02-11 11:03:14 +00001205/// where BC(It, k) stands for binomial coefficient.
Dan Gohmanaf752342009-07-07 17:06:11 +00001206const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It,
Dan Gohman32291b12009-07-21 00:38:55 +00001207 ScalarEvolution &SE) const {
Dan Gohmanaf752342009-07-07 17:06:11 +00001208 const SCEV *Result = getStart();
Chris Lattnerd934c702004-04-02 20:23:17 +00001209 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
Wojciech Matyjewiczd2d97642008-02-11 11:03:14 +00001210 // The computation is correct in the face of overflow provided that the
1211 // multiplication is performed _after_ the evaluation of the binomial
1212 // coefficient.
Dan Gohmanaf752342009-07-07 17:06:11 +00001213 const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType());
Nick Lewycky707663e2008-10-13 03:58:02 +00001214 if (isa<SCEVCouldNotCompute>(Coeff))
1215 return Coeff;
1216
1217 Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff));
Chris Lattnerd934c702004-04-02 20:23:17 +00001218 }
1219 return Result;
1220}
1221
Chris Lattnerd934c702004-04-02 20:23:17 +00001222//===----------------------------------------------------------------------===//
1223// SCEV Expression folder implementations
1224//===----------------------------------------------------------------------===//
1225
Dan Gohmanaf752342009-07-07 17:06:11 +00001226const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op,
Chris Lattner229907c2011-07-18 04:54:35 +00001227 Type *Ty) {
Dan Gohmanb397e1a2009-04-21 01:07:12 +00001228 assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) &&
Dan Gohman413e91f2009-04-21 00:55:22 +00001229 "This is not a truncating conversion!");
Dan Gohman194e42c2009-05-01 16:44:18 +00001230 assert(isSCEVable(Ty) &&
1231 "This is not a conversion to a SCEVable type!");
1232 Ty = getEffectiveSCEVType(Ty);
Dan Gohman413e91f2009-04-21 00:55:22 +00001233
Dan Gohman3a302cb2009-07-13 20:50:19 +00001234 FoldingSetNodeID ID;
1235 ID.AddInteger(scTruncate);
1236 ID.AddPointer(Op);
1237 ID.AddPointer(Ty);
Craig Topper9f008862014-04-15 04:59:12 +00001238 void *IP = nullptr;
Dan Gohman3a302cb2009-07-13 20:50:19 +00001239 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1240
Dan Gohman3423e722009-06-30 20:13:32 +00001241 // Fold if the operand is constant.
Dan Gohmana30370b2009-05-04 22:02:23 +00001242 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
Dan Gohman8d7576e2009-06-24 00:38:39 +00001243 return getConstant(
Nuno Lopesab5c9242012-05-15 15:44:38 +00001244 cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty)));
Chris Lattnerd934c702004-04-02 20:23:17 +00001245
Dan Gohman79af8542009-04-22 16:20:48 +00001246 // trunc(trunc(x)) --> trunc(x)
Dan Gohmana30370b2009-05-04 22:02:23 +00001247 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op))
Dan Gohman79af8542009-04-22 16:20:48 +00001248 return getTruncateExpr(ST->getOperand(), Ty);
1249
Nick Lewyckyb4d9f7a2009-04-23 05:15:08 +00001250 // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing
Dan Gohmana30370b2009-05-04 22:02:23 +00001251 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
Nick Lewyckyb4d9f7a2009-04-23 05:15:08 +00001252 return getTruncateOrSignExtend(SS->getOperand(), Ty);
1253
1254 // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing
Dan Gohmana30370b2009-05-04 22:02:23 +00001255 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
Nick Lewyckyb4d9f7a2009-04-23 05:15:08 +00001256 return getTruncateOrZeroExtend(SZ->getOperand(), Ty);
1257
Nick Lewycky5143f0f2011-01-19 16:59:46 +00001258 // trunc(x1+x2+...+xN) --> trunc(x1)+trunc(x2)+...+trunc(xN) if we can
Nick Lewycky2ce28322015-03-20 02:52:23 +00001259 // eliminate all the truncates, or we replace other casts with truncates.
Nick Lewycky5143f0f2011-01-19 16:59:46 +00001260 if (const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Op)) {
1261 SmallVector<const SCEV *, 4> Operands;
1262 bool hasTrunc = false;
1263 for (unsigned i = 0, e = SA->getNumOperands(); i != e && !hasTrunc; ++i) {
1264 const SCEV *S = getTruncateExpr(SA->getOperand(i), Ty);
Nick Lewyckybe8af482015-03-20 02:25:00 +00001265 if (!isa<SCEVCastExpr>(SA->getOperand(i)))
1266 hasTrunc = isa<SCEVTruncateExpr>(S);
Nick Lewycky5143f0f2011-01-19 16:59:46 +00001267 Operands.push_back(S);
1268 }
1269 if (!hasTrunc)
Andrew Trick8b55b732011-03-14 16:50:06 +00001270 return getAddExpr(Operands);
Serguei Katkovedf3c822017-12-27 07:15:23 +00001271 // In spite we checked in the beginning that ID is not in the cache,
1272 // it is possible that during recursion and different modification
1273 // ID came to cache, so if we found it, just return it.
1274 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
1275 return S;
Nick Lewycky5143f0f2011-01-19 16:59:46 +00001276 }
1277
Nick Lewycky5c901f32011-01-19 18:56:00 +00001278 // trunc(x1*x2*...*xN) --> trunc(x1)*trunc(x2)*...*trunc(xN) if we can
Nick Lewyckybe8af482015-03-20 02:25:00 +00001279 // eliminate all the truncates, or we replace other casts with truncates.
Nick Lewycky5c901f32011-01-19 18:56:00 +00001280 if (const SCEVMulExpr *SM = dyn_cast<SCEVMulExpr>(Op)) {
1281 SmallVector<const SCEV *, 4> Operands;
1282 bool hasTrunc = false;
1283 for (unsigned i = 0, e = SM->getNumOperands(); i != e && !hasTrunc; ++i) {
1284 const SCEV *S = getTruncateExpr(SM->getOperand(i), Ty);
Nick Lewyckybe8af482015-03-20 02:25:00 +00001285 if (!isa<SCEVCastExpr>(SM->getOperand(i)))
1286 hasTrunc = isa<SCEVTruncateExpr>(S);
Nick Lewycky5c901f32011-01-19 18:56:00 +00001287 Operands.push_back(S);
1288 }
1289 if (!hasTrunc)
Andrew Trick8b55b732011-03-14 16:50:06 +00001290 return getMulExpr(Operands);
Serguei Katkovedf3c822017-12-27 07:15:23 +00001291 // In spite we checked in the beginning that ID is not in the cache,
1292 // it is possible that during recursion and different modification
1293 // ID came to cache, so if we found it, just return it.
1294 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
1295 return S;
Nick Lewycky5c901f32011-01-19 18:56:00 +00001296 }
1297
Dan Gohman5a728c92009-06-18 16:24:47 +00001298 // If the input value is a chrec scev, truncate the chrec's operands.
Dan Gohmana30370b2009-05-04 22:02:23 +00001299 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
Dan Gohmanaf752342009-07-07 17:06:11 +00001300 SmallVector<const SCEV *, 4> Operands;
Sanjoy Dasd9f6d332015-10-18 00:29:16 +00001301 for (const SCEV *Op : AddRec->operands())
1302 Operands.push_back(getTruncateExpr(Op, Ty));
Andrew Trick8b55b732011-03-14 16:50:06 +00001303 return getAddRecExpr(Operands, AddRec->getLoop(), SCEV::FlagAnyWrap);
Chris Lattnerd934c702004-04-02 20:23:17 +00001304 }
1305
Dan Gohman89dd42a2010-06-25 18:47:08 +00001306 // The cast wasn't folded; create an explicit cast node. We can reuse
1307 // the existing insert position since if we get here, we won't have
1308 // made any changes which would invalidate it.
Dan Gohman01c65a22010-03-18 18:49:47 +00001309 SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator),
1310 Op, Ty);
Dan Gohmanc5c85c02009-06-27 21:21:31 +00001311 UniqueSCEVs.InsertNode(S, IP);
Sanjoy Dase6b995f2017-10-13 05:50:52 +00001312 addToLoopUseLists(S);
Dan Gohmanc5c85c02009-06-27 21:21:31 +00001313 return S;
Chris Lattnerd934c702004-04-02 20:23:17 +00001314}
1315
Sanjoy Das4153f472015-02-18 01:47:07 +00001316// Get the limit of a recurrence such that incrementing by Step cannot cause
1317// signed overflow as long as the value of the recurrence within the
1318// loop does not exceed this limit before incrementing.
1319static const SCEV *getSignedOverflowLimitForStep(const SCEV *Step,
1320 ICmpInst::Predicate *Pred,
1321 ScalarEvolution *SE) {
1322 unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
1323 if (SE->isKnownPositive(Step)) {
1324 *Pred = ICmpInst::ICMP_SLT;
1325 return SE->getConstant(APInt::getSignedMinValue(BitWidth) -
Craig Topper01020392017-06-24 23:34:50 +00001326 SE->getSignedRangeMax(Step));
Sanjoy Das4153f472015-02-18 01:47:07 +00001327 }
1328 if (SE->isKnownNegative(Step)) {
1329 *Pred = ICmpInst::ICMP_SGT;
1330 return SE->getConstant(APInt::getSignedMaxValue(BitWidth) -
Craig Topper01020392017-06-24 23:34:50 +00001331 SE->getSignedRangeMin(Step));
Sanjoy Das4153f472015-02-18 01:47:07 +00001332 }
1333 return nullptr;
1334}
1335
1336// Get the limit of a recurrence such that incrementing by Step cannot cause
1337// unsigned overflow as long as the value of the recurrence within the loop does
1338// not exceed this limit before incrementing.
1339static const SCEV *getUnsignedOverflowLimitForStep(const SCEV *Step,
1340 ICmpInst::Predicate *Pred,
1341 ScalarEvolution *SE) {
1342 unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
1343 *Pred = ICmpInst::ICMP_ULT;
1344
1345 return SE->getConstant(APInt::getMinValue(BitWidth) -
Craig Topper01020392017-06-24 23:34:50 +00001346 SE->getUnsignedRangeMax(Step));
Sanjoy Das4153f472015-02-18 01:47:07 +00001347}
1348
1349namespace {
1350
1351struct ExtendOpTraitsBase {
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001352 typedef const SCEV *(ScalarEvolution::*GetExtendExprTy)(const SCEV *, Type *,
1353 unsigned);
Sanjoy Das4153f472015-02-18 01:47:07 +00001354};
1355
1356// Used to make code generic over signed and unsigned overflow.
1357template <typename ExtendOp> struct ExtendOpTraits {
1358 // Members present:
1359 //
1360 // static const SCEV::NoWrapFlags WrapType;
1361 //
1362 // static const ExtendOpTraitsBase::GetExtendExprTy GetExtendExpr;
1363 //
1364 // static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1365 // ICmpInst::Predicate *Pred,
1366 // ScalarEvolution *SE);
1367};
1368
1369template <>
1370struct ExtendOpTraits<SCEVSignExtendExpr> : public ExtendOpTraitsBase {
1371 static const SCEV::NoWrapFlags WrapType = SCEV::FlagNSW;
1372
1373 static const GetExtendExprTy GetExtendExpr;
1374
1375 static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1376 ICmpInst::Predicate *Pred,
1377 ScalarEvolution *SE) {
1378 return getSignedOverflowLimitForStep(Step, Pred, SE);
1379 }
1380};
1381
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001382const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1383 SCEVSignExtendExpr>::GetExtendExpr = &ScalarEvolution::getSignExtendExpr;
Sanjoy Das4153f472015-02-18 01:47:07 +00001384
1385template <>
1386struct ExtendOpTraits<SCEVZeroExtendExpr> : public ExtendOpTraitsBase {
1387 static const SCEV::NoWrapFlags WrapType = SCEV::FlagNUW;
1388
1389 static const GetExtendExprTy GetExtendExpr;
1390
1391 static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1392 ICmpInst::Predicate *Pred,
1393 ScalarEvolution *SE) {
1394 return getUnsignedOverflowLimitForStep(Step, Pred, SE);
1395 }
1396};
1397
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001398const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1399 SCEVZeroExtendExpr>::GetExtendExpr = &ScalarEvolution::getZeroExtendExpr;
Eugene Zelenkobe709f22017-08-18 23:51:26 +00001400
1401} // end anonymous namespace
Sanjoy Das4153f472015-02-18 01:47:07 +00001402
1403// The recurrence AR has been shown to have no signed/unsigned wrap or something
1404// close to it. Typically, if we can prove NSW/NUW for AR, then we can just as
1405// easily prove NSW/NUW for its preincrement or postincrement sibling. This
1406// allows normalizing a sign/zero extended AddRec as such: {sext/zext(Step +
1407// Start),+,Step} => {(Step + sext/zext(Start),+,Step} As a result, the
1408// expression "Step + sext/zext(PreIncAR)" is congruent with
1409// "sext/zext(PostIncAR)"
1410template <typename ExtendOpTy>
1411static const SCEV *getPreStartForExtend(const SCEVAddRecExpr *AR, Type *Ty,
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001412 ScalarEvolution *SE, unsigned Depth) {
Sanjoy Das4153f472015-02-18 01:47:07 +00001413 auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1414 auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1415
1416 const Loop *L = AR->getLoop();
1417 const SCEV *Start = AR->getStart();
1418 const SCEV *Step = AR->getStepRecurrence(*SE);
1419
1420 // Check for a simple looking step prior to loop entry.
1421 const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Start);
1422 if (!SA)
1423 return nullptr;
1424
1425 // Create an AddExpr for "PreStart" after subtracting Step. Full SCEV
1426 // subtraction is expensive. For this purpose, perform a quick and dirty
1427 // difference, by checking for Step in the operand list.
1428 SmallVector<const SCEV *, 4> DiffOps;
1429 for (const SCEV *Op : SA->operands())
1430 if (Op != Step)
1431 DiffOps.push_back(Op);
1432
1433 if (DiffOps.size() == SA->getNumOperands())
1434 return nullptr;
1435
1436 // Try to prove `WrapType` (SCEV::FlagNSW or SCEV::FlagNUW) on `PreStart` +
1437 // `Step`:
1438
1439 // 1. NSW/NUW flags on the step increment.
Sanjoy Das0714e3e2015-10-23 06:33:47 +00001440 auto PreStartFlags =
1441 ScalarEvolution::maskFlags(SA->getNoWrapFlags(), SCEV::FlagNUW);
1442 const SCEV *PreStart = SE->getAddExpr(DiffOps, PreStartFlags);
Sanjoy Das4153f472015-02-18 01:47:07 +00001443 const SCEVAddRecExpr *PreAR = dyn_cast<SCEVAddRecExpr>(
1444 SE->getAddRecExpr(PreStart, Step, L, SCEV::FlagAnyWrap));
1445
Sanjoy Dasb14010d2015-02-24 01:02:42 +00001446 // "{S,+,X} is <nsw>/<nuw>" and "the backedge is taken at least once" implies
1447 // "S+X does not sign/unsign-overflow".
Sanjoy Das4153f472015-02-18 01:47:07 +00001448 //
1449
Sanjoy Dasb14010d2015-02-24 01:02:42 +00001450 const SCEV *BECount = SE->getBackedgeTakenCount(L);
1451 if (PreAR && PreAR->getNoWrapFlags(WrapType) &&
1452 !isa<SCEVCouldNotCompute>(BECount) && SE->isKnownPositive(BECount))
Sanjoy Das4153f472015-02-18 01:47:07 +00001453 return PreStart;
1454
1455 // 2. Direct overflow check on the step operation's expression.
1456 unsigned BitWidth = SE->getTypeSizeInBits(AR->getType());
1457 Type *WideTy = IntegerType::get(SE->getContext(), BitWidth * 2);
1458 const SCEV *OperandExtendedStart =
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001459 SE->getAddExpr((SE->*GetExtendExpr)(PreStart, WideTy, Depth),
1460 (SE->*GetExtendExpr)(Step, WideTy, Depth));
1461 if ((SE->*GetExtendExpr)(Start, WideTy, Depth) == OperandExtendedStart) {
Sanjoy Das4153f472015-02-18 01:47:07 +00001462 if (PreAR && AR->getNoWrapFlags(WrapType)) {
1463 // If we know `AR` == {`PreStart`+`Step`,+,`Step`} is `WrapType` (FlagNSW
1464 // or FlagNUW) and that `PreStart` + `Step` is `WrapType` too, then
1465 // `PreAR` == {`PreStart`,+,`Step`} is also `WrapType`. Cache this fact.
1466 const_cast<SCEVAddRecExpr *>(PreAR)->setNoWrapFlags(WrapType);
1467 }
1468 return PreStart;
1469 }
1470
1471 // 3. Loop precondition.
1472 ICmpInst::Predicate Pred;
1473 const SCEV *OverflowLimit =
1474 ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(Step, &Pred, SE);
1475
1476 if (OverflowLimit &&
Sanjoy Dasd295f2c2015-10-18 00:29:27 +00001477 SE->isLoopEntryGuardedByCond(L, Pred, PreStart, OverflowLimit))
Sanjoy Das4153f472015-02-18 01:47:07 +00001478 return PreStart;
Sanjoy Dasd295f2c2015-10-18 00:29:27 +00001479
Sanjoy Das4153f472015-02-18 01:47:07 +00001480 return nullptr;
1481}
1482
1483// Get the normalized zero or sign extended expression for this AddRec's Start.
1484template <typename ExtendOpTy>
1485static const SCEV *getExtendAddRecStart(const SCEVAddRecExpr *AR, Type *Ty,
Wei Mi8c405332017-04-17 20:40:05 +00001486 ScalarEvolution *SE,
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001487 unsigned Depth) {
Sanjoy Das4153f472015-02-18 01:47:07 +00001488 auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1489
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001490 const SCEV *PreStart = getPreStartForExtend<ExtendOpTy>(AR, Ty, SE, Depth);
Sanjoy Das4153f472015-02-18 01:47:07 +00001491 if (!PreStart)
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001492 return (SE->*GetExtendExpr)(AR->getStart(), Ty, Depth);
Sanjoy Das4153f472015-02-18 01:47:07 +00001493
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001494 return SE->getAddExpr((SE->*GetExtendExpr)(AR->getStepRecurrence(*SE), Ty,
1495 Depth),
1496 (SE->*GetExtendExpr)(PreStart, Ty, Depth));
Sanjoy Das4153f472015-02-18 01:47:07 +00001497}
1498
Sanjoy Das9e2c5012015-03-04 22:24:17 +00001499// Try to prove away overflow by looking at "nearby" add recurrences. A
1500// motivating example for this rule: if we know `{0,+,4}` is `ult` `-1` and it
1501// does not itself wrap then we can conclude that `{1,+,4}` is `nuw`.
1502//
1503// Formally:
1504//
1505// {S,+,X} == {S-T,+,X} + T
1506// => Ext({S,+,X}) == Ext({S-T,+,X} + T)
1507//
1508// If ({S-T,+,X} + T) does not overflow ... (1)
1509//
1510// RHS == Ext({S-T,+,X} + T) == Ext({S-T,+,X}) + Ext(T)
1511//
1512// If {S-T,+,X} does not overflow ... (2)
1513//
1514// RHS == Ext({S-T,+,X}) + Ext(T) == {Ext(S-T),+,Ext(X)} + Ext(T)
1515// == {Ext(S-T)+Ext(T),+,Ext(X)}
1516//
1517// If (S-T)+T does not overflow ... (3)
1518//
1519// RHS == {Ext(S-T)+Ext(T),+,Ext(X)} == {Ext(S-T+T),+,Ext(X)}
1520// == {Ext(S),+,Ext(X)} == LHS
1521//
1522// Thus, if (1), (2) and (3) are true for some T, then
1523// Ext({S,+,X}) == {Ext(S),+,Ext(X)}
1524//
1525// (3) is implied by (1) -- "(S-T)+T does not overflow" is simply "({S-T,+,X}+T)
1526// does not overflow" restricted to the 0th iteration. Therefore we only need
1527// to check for (1) and (2).
1528//
1529// In the current context, S is `Start`, X is `Step`, Ext is `ExtendOpTy` and T
1530// is `Delta` (defined below).
Sanjoy Das9e2c5012015-03-04 22:24:17 +00001531template <typename ExtendOpTy>
1532bool ScalarEvolution::proveNoWrapByVaryingStart(const SCEV *Start,
1533 const SCEV *Step,
1534 const Loop *L) {
1535 auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1536
1537 // We restrict `Start` to a constant to prevent SCEV from spending too much
1538 // time here. It is correct (but more expensive) to continue with a
1539 // non-constant `Start` and do a general SCEV subtraction to compute
1540 // `PreStart` below.
Sanjoy Das9e2c5012015-03-04 22:24:17 +00001541 const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start);
1542 if (!StartC)
1543 return false;
1544
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001545 APInt StartAI = StartC->getAPInt();
Sanjoy Das9e2c5012015-03-04 22:24:17 +00001546
1547 for (unsigned Delta : {-2, -1, 1, 2}) {
1548 const SCEV *PreStart = getConstant(StartAI - Delta);
1549
Sanjoy Das42801102015-10-23 06:57:21 +00001550 FoldingSetNodeID ID;
1551 ID.AddInteger(scAddRecExpr);
1552 ID.AddPointer(PreStart);
1553 ID.AddPointer(Step);
1554 ID.AddPointer(L);
1555 void *IP = nullptr;
1556 const auto *PreAR =
1557 static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
1558
Sanjoy Das9e2c5012015-03-04 22:24:17 +00001559 // Give up if we don't already have the add recurrence we need because
1560 // actually constructing an add recurrence is relatively expensive.
Sanjoy Das9e2c5012015-03-04 22:24:17 +00001561 if (PreAR && PreAR->getNoWrapFlags(WrapType)) { // proves (2)
1562 const SCEV *DeltaS = getConstant(StartC->getType(), Delta);
1563 ICmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
1564 const SCEV *Limit = ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(
1565 DeltaS, &Pred, this);
1566 if (Limit && isKnownPredicate(Pred, PreAR, Limit)) // proves (1)
1567 return true;
1568 }
1569 }
1570
1571 return false;
1572}
1573
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001574const SCEV *
1575ScalarEvolution::getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
Dan Gohmanb397e1a2009-04-21 01:07:12 +00001576 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
Dan Gohmanc1c2ba72009-04-16 19:25:55 +00001577 "This is not an extending conversion!");
Dan Gohman194e42c2009-05-01 16:44:18 +00001578 assert(isSCEVable(Ty) &&
1579 "This is not a conversion to a SCEVable type!");
1580 Ty = getEffectiveSCEVType(Ty);
Dan Gohmanc1c2ba72009-04-16 19:25:55 +00001581
Dan Gohman3423e722009-06-30 20:13:32 +00001582 // Fold if the operand is constant.
Dan Gohman5235cc22010-06-24 16:47:03 +00001583 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1584 return getConstant(
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001585 cast<ConstantInt>(ConstantExpr::getZExt(SC->getValue(), Ty)));
Chris Lattnerd934c702004-04-02 20:23:17 +00001586
Dan Gohman79af8542009-04-22 16:20:48 +00001587 // zext(zext(x)) --> zext(x)
Dan Gohmana30370b2009-05-04 22:02:23 +00001588 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001589 return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1);
Dan Gohman79af8542009-04-22 16:20:48 +00001590
Dan Gohman74a0ba12009-07-13 20:55:53 +00001591 // Before doing any expensive analysis, check to see if we've already
1592 // computed a SCEV for this Op and Ty.
1593 FoldingSetNodeID ID;
1594 ID.AddInteger(scZeroExtend);
1595 ID.AddPointer(Op);
1596 ID.AddPointer(Ty);
Craig Topper9f008862014-04-15 04:59:12 +00001597 void *IP = nullptr;
Dan Gohman74a0ba12009-07-13 20:55:53 +00001598 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001599 if (Depth > MaxExtDepth) {
1600 SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
1601 Op, Ty);
1602 UniqueSCEVs.InsertNode(S, IP);
Sanjoy Dase6b995f2017-10-13 05:50:52 +00001603 addToLoopUseLists(S);
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001604 return S;
1605 }
Dan Gohman74a0ba12009-07-13 20:55:53 +00001606
Nick Lewyckybc98f5b2011-01-23 06:20:19 +00001607 // zext(trunc(x)) --> zext(x) or x or trunc(x)
1608 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
1609 // It's possible the bits taken off by the truncate were all zero bits. If
1610 // so, we should be able to simplify this further.
1611 const SCEV *X = ST->getOperand();
1612 ConstantRange CR = getUnsignedRange(X);
Nick Lewyckybc98f5b2011-01-23 06:20:19 +00001613 unsigned TruncBits = getTypeSizeInBits(ST->getType());
1614 unsigned NewBits = getTypeSizeInBits(Ty);
1615 if (CR.truncate(TruncBits).zeroExtend(NewBits).contains(
Nick Lewyckyd4192f72011-01-23 20:06:05 +00001616 CR.zextOrTrunc(NewBits)))
1617 return getTruncateOrZeroExtend(X, Ty);
Nick Lewyckybc98f5b2011-01-23 06:20:19 +00001618 }
1619
Dan Gohman76466372009-04-27 20:16:15 +00001620 // If the input value is a chrec scev, and we can prove that the value
Chris Lattnerd934c702004-04-02 20:23:17 +00001621 // did not overflow the old, smaller, value, we can zero extend all of the
Dan Gohman76466372009-04-27 20:16:15 +00001622 // operands (often constants). This allows analysis of something like
Chris Lattnerd934c702004-04-02 20:23:17 +00001623 // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
Dan Gohmana30370b2009-05-04 22:02:23 +00001624 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
Dan Gohman76466372009-04-27 20:16:15 +00001625 if (AR->isAffine()) {
Dan Gohmane65c9172009-07-13 21:35:55 +00001626 const SCEV *Start = AR->getStart();
1627 const SCEV *Step = AR->getStepRecurrence(*this);
1628 unsigned BitWidth = getTypeSizeInBits(AR->getType());
1629 const Loop *L = AR->getLoop();
1630
Sanjoy Das724f5cf2016-03-03 18:31:29 +00001631 if (!AR->hasNoUnsignedWrap()) {
1632 auto NewFlags = proveNoWrapViaConstantRanges(AR);
1633 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(NewFlags);
1634 }
1635
Dan Gohman62ef6a72009-07-25 01:22:26 +00001636 // If we have special knowledge that this addrec won't overflow,
1637 // we don't need to do any further analysis.
Sanjoy Das76c48e02016-02-04 18:21:54 +00001638 if (AR->hasNoUnsignedWrap())
Sanjoy Das4153f472015-02-18 01:47:07 +00001639 return getAddRecExpr(
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001640 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, Depth + 1),
1641 getZeroExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
Dan Gohman62ef6a72009-07-25 01:22:26 +00001642
Dan Gohman76466372009-04-27 20:16:15 +00001643 // Check whether the backedge-taken count is SCEVCouldNotCompute.
1644 // Note that this serves two purposes: It filters out loops that are
1645 // simply not analyzable, and it covers the case where this code is
1646 // being called from within backedge-taken count analysis, such that
1647 // attempting to ask for the backedge-taken count would likely result
1648 // in infinite recursion. In the later case, the analysis code will
1649 // cope with a conservative value, and it will take care to purge
1650 // that value once it has finished.
Dan Gohmane65c9172009-07-13 21:35:55 +00001651 const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
Dan Gohman2b8da352009-04-30 20:47:05 +00001652 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
Dan Gohman95c5b0e2009-04-29 01:54:20 +00001653 // Manually compute the final value for AR, checking for
Dan Gohman494dac32009-04-29 22:28:28 +00001654 // overflow.
Dan Gohman76466372009-04-27 20:16:15 +00001655
1656 // Check whether the backedge-taken count can be losslessly casted to
1657 // the addrec's type. The count is always unsigned.
Dan Gohmanaf752342009-07-07 17:06:11 +00001658 const SCEV *CastedMaxBECount =
Dan Gohman2b8da352009-04-30 20:47:05 +00001659 getTruncateOrZeroExtend(MaxBECount, Start->getType());
Dan Gohmanaf752342009-07-07 17:06:11 +00001660 const SCEV *RecastedMaxBECount =
Dan Gohman4fc36682009-05-18 15:58:39 +00001661 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
1662 if (MaxBECount == RecastedMaxBECount) {
Chris Lattner229907c2011-07-18 04:54:35 +00001663 Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
Dan Gohman2b8da352009-04-30 20:47:05 +00001664 // Check whether Start+Step*MaxBECount has no unsigned overflow.
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001665 const SCEV *ZMul = getMulExpr(CastedMaxBECount, Step,
1666 SCEV::FlagAnyWrap, Depth + 1);
1667 const SCEV *ZAdd = getZeroExtendExpr(getAddExpr(Start, ZMul,
1668 SCEV::FlagAnyWrap,
1669 Depth + 1),
1670 WideTy, Depth + 1);
1671 const SCEV *WideStart = getZeroExtendExpr(Start, WideTy, Depth + 1);
Nuno Lopesc2a170e2012-05-15 20:20:14 +00001672 const SCEV *WideMaxBECount =
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001673 getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1);
1674 const SCEV *OperandExtendedAdd =
1675 getAddExpr(WideStart,
1676 getMulExpr(WideMaxBECount,
1677 getZeroExtendExpr(Step, WideTy, Depth + 1),
1678 SCEV::FlagAnyWrap, Depth + 1),
1679 SCEV::FlagAnyWrap, Depth + 1);
Nuno Lopesc2a170e2012-05-15 20:20:14 +00001680 if (ZAdd == OperandExtendedAdd) {
Andrew Trickf6b01ff2011-03-15 00:37:00 +00001681 // Cache knowledge of AR NUW, which is propagated to this AddRec.
1682 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
Dan Gohman494dac32009-04-29 22:28:28 +00001683 // Return the expression with the addrec on the outside.
Sanjoy Das4153f472015-02-18 01:47:07 +00001684 return getAddRecExpr(
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001685 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
1686 Depth + 1),
1687 getZeroExtendExpr(Step, Ty, Depth + 1), L,
Wei Mi8c405332017-04-17 20:40:05 +00001688 AR->getNoWrapFlags());
Andrew Trickf6b01ff2011-03-15 00:37:00 +00001689 }
Dan Gohman76466372009-04-27 20:16:15 +00001690 // Similar to above, only this time treat the step value as signed.
1691 // This covers loops that count down.
Dan Gohman4fc36682009-05-18 15:58:39 +00001692 OperandExtendedAdd =
Nuno Lopesc2a170e2012-05-15 20:20:14 +00001693 getAddExpr(WideStart,
1694 getMulExpr(WideMaxBECount,
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001695 getSignExtendExpr(Step, WideTy, Depth + 1),
1696 SCEV::FlagAnyWrap, Depth + 1),
1697 SCEV::FlagAnyWrap, Depth + 1);
Nuno Lopesc2a170e2012-05-15 20:20:14 +00001698 if (ZAdd == OperandExtendedAdd) {
Andrew Trickf6b01ff2011-03-15 00:37:00 +00001699 // Cache knowledge of AR NW, which is propagated to this AddRec.
1700 // Negative step causes unsigned wrap, but it still can't self-wrap.
1701 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
Dan Gohman494dac32009-04-29 22:28:28 +00001702 // Return the expression with the addrec on the outside.
Sanjoy Das4153f472015-02-18 01:47:07 +00001703 return getAddRecExpr(
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001704 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
1705 Depth + 1),
1706 getSignExtendExpr(Step, Ty, Depth + 1), L,
1707 AR->getNoWrapFlags());
Andrew Trickf6b01ff2011-03-15 00:37:00 +00001708 }
Dan Gohmane65c9172009-07-13 21:35:55 +00001709 }
Sanjoy Dasf5d40d52016-05-17 17:51:14 +00001710 }
Dan Gohmane65c9172009-07-13 21:35:55 +00001711
Sanjoy Dasf5d40d52016-05-17 17:51:14 +00001712 // Normally, in the cases we can prove no-overflow via a
1713 // backedge guarding condition, we can also compute a backedge
1714 // taken count for the loop. The exceptions are assumptions and
1715 // guards present in the loop -- SCEV is not great at exploiting
1716 // these to compute max backedge taken counts, but can still use
1717 // these to prove lack of overflow. Use this fact to avoid
1718 // doing extra work that may not pay off.
1719 if (!isa<SCEVCouldNotCompute>(MaxBECount) || HasGuards ||
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001720 !AC.assumptions().empty()) {
Sanjoy Dasf5d40d52016-05-17 17:51:14 +00001721 // If the backedge is guarded by a comparison with the pre-inc
1722 // value the addrec is safe. Also, if the entry is guarded by
1723 // a comparison with the start value and the backedge is
1724 // guarded by a comparison with the post-inc value, the addrec
1725 // is safe.
Dan Gohmane65c9172009-07-13 21:35:55 +00001726 if (isKnownPositive(Step)) {
1727 const SCEV *N = getConstant(APInt::getMinValue(BitWidth) -
Craig Topper01020392017-06-24 23:34:50 +00001728 getUnsignedRangeMax(Step));
Dan Gohmane65c9172009-07-13 21:35:55 +00001729 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) ||
Dan Gohmanb50349a2010-04-11 19:27:13 +00001730 (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) &&
Dan Gohmane65c9172009-07-13 21:35:55 +00001731 isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT,
Andrew Trickf6b01ff2011-03-15 00:37:00 +00001732 AR->getPostIncExpr(*this), N))) {
Sanjoy Dasf5d40d52016-05-17 17:51:14 +00001733 // Cache knowledge of AR NUW, which is propagated to this
1734 // AddRec.
Andrew Trickf6b01ff2011-03-15 00:37:00 +00001735 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
Dan Gohmane65c9172009-07-13 21:35:55 +00001736 // Return the expression with the addrec on the outside.
Sanjoy Das4153f472015-02-18 01:47:07 +00001737 return getAddRecExpr(
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001738 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
1739 Depth + 1),
1740 getZeroExtendExpr(Step, Ty, Depth + 1), L,
Wei Mi8c405332017-04-17 20:40:05 +00001741 AR->getNoWrapFlags());
Andrew Trickf6b01ff2011-03-15 00:37:00 +00001742 }
Dan Gohmane65c9172009-07-13 21:35:55 +00001743 } else if (isKnownNegative(Step)) {
1744 const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) -
Craig Topper01020392017-06-24 23:34:50 +00001745 getSignedRangeMin(Step));
Dan Gohman5f18c542010-05-04 01:11:15 +00001746 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) ||
1747 (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) &&
Dan Gohmane65c9172009-07-13 21:35:55 +00001748 isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT,
Andrew Trickf6b01ff2011-03-15 00:37:00 +00001749 AR->getPostIncExpr(*this), N))) {
Sanjoy Dasf5d40d52016-05-17 17:51:14 +00001750 // Cache knowledge of AR NW, which is propagated to this
1751 // AddRec. Negative step causes unsigned wrap, but it
1752 // still can't self-wrap.
Andrew Trickf6b01ff2011-03-15 00:37:00 +00001753 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
1754 // Return the expression with the addrec on the outside.
Sanjoy Das4153f472015-02-18 01:47:07 +00001755 return getAddRecExpr(
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001756 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
1757 Depth + 1),
1758 getSignExtendExpr(Step, Ty, Depth + 1), L,
1759 AR->getNoWrapFlags());
Andrew Trickf6b01ff2011-03-15 00:37:00 +00001760 }
Dan Gohman76466372009-04-27 20:16:15 +00001761 }
1762 }
Sanjoy Das9e2c5012015-03-04 22:24:17 +00001763
1764 if (proveNoWrapByVaryingStart<SCEVZeroExtendExpr>(Start, Step, L)) {
1765 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
1766 return getAddRecExpr(
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001767 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, Depth + 1),
1768 getZeroExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
Sanjoy Das9e2c5012015-03-04 22:24:17 +00001769 }
Dan Gohman76466372009-04-27 20:16:15 +00001770 }
Chris Lattnerd934c702004-04-02 20:23:17 +00001771
Sanjoy Daseeca9f62015-10-22 19:57:38 +00001772 if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) {
1773 // zext((A + B + ...)<nuw>) --> (zext(A) + zext(B) + ...)<nuw>
Sanjoy Das76c48e02016-02-04 18:21:54 +00001774 if (SA->hasNoUnsignedWrap()) {
Sanjoy Daseeca9f62015-10-22 19:57:38 +00001775 // If the addition does not unsign overflow then we can, by definition,
1776 // commute the zero extension with the addition operation.
1777 SmallVector<const SCEV *, 4> Ops;
1778 for (const auto *Op : SA->operands())
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001779 Ops.push_back(getZeroExtendExpr(Op, Ty, Depth + 1));
1780 return getAddExpr(Ops, SCEV::FlagNUW, Depth + 1);
Sanjoy Daseeca9f62015-10-22 19:57:38 +00001781 }
1782 }
1783
Dan Gohman74a0ba12009-07-13 20:55:53 +00001784 // The cast wasn't folded; create an explicit cast node.
1785 // Recompute the insert position, as it may have been invalidated.
Dan Gohmanc5c85c02009-06-27 21:21:31 +00001786 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
Dan Gohman01c65a22010-03-18 18:49:47 +00001787 SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
1788 Op, Ty);
Dan Gohmanc5c85c02009-06-27 21:21:31 +00001789 UniqueSCEVs.InsertNode(S, IP);
Sanjoy Dase6b995f2017-10-13 05:50:52 +00001790 addToLoopUseLists(S);
Dan Gohmanc5c85c02009-06-27 21:21:31 +00001791 return S;
Chris Lattnerd934c702004-04-02 20:23:17 +00001792}
1793
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001794const SCEV *
1795ScalarEvolution::getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
Dan Gohmanb397e1a2009-04-21 01:07:12 +00001796 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
Dan Gohman413e91f2009-04-21 00:55:22 +00001797 "This is not an extending conversion!");
Dan Gohman194e42c2009-05-01 16:44:18 +00001798 assert(isSCEVable(Ty) &&
1799 "This is not a conversion to a SCEVable type!");
1800 Ty = getEffectiveSCEVType(Ty);
Dan Gohman413e91f2009-04-21 00:55:22 +00001801
Dan Gohman3423e722009-06-30 20:13:32 +00001802 // Fold if the operand is constant.
Dan Gohman5235cc22010-06-24 16:47:03 +00001803 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1804 return getConstant(
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001805 cast<ConstantInt>(ConstantExpr::getSExt(SC->getValue(), Ty)));
Dan Gohmancb9e09a2007-06-15 14:38:12 +00001806
Dan Gohman79af8542009-04-22 16:20:48 +00001807 // sext(sext(x)) --> sext(x)
Dan Gohmana30370b2009-05-04 22:02:23 +00001808 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001809 return getSignExtendExpr(SS->getOperand(), Ty, Depth + 1);
Dan Gohman79af8542009-04-22 16:20:48 +00001810
Nick Lewyckye9ea75e2011-01-19 15:56:12 +00001811 // sext(zext(x)) --> zext(x)
1812 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001813 return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1);
Nick Lewyckye9ea75e2011-01-19 15:56:12 +00001814
Dan Gohman74a0ba12009-07-13 20:55:53 +00001815 // Before doing any expensive analysis, check to see if we've already
1816 // computed a SCEV for this Op and Ty.
1817 FoldingSetNodeID ID;
1818 ID.AddInteger(scSignExtend);
1819 ID.AddPointer(Op);
1820 ID.AddPointer(Ty);
Craig Topper9f008862014-04-15 04:59:12 +00001821 void *IP = nullptr;
Dan Gohman74a0ba12009-07-13 20:55:53 +00001822 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001823 // Limit recursion depth.
1824 if (Depth > MaxExtDepth) {
1825 SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
1826 Op, Ty);
1827 UniqueSCEVs.InsertNode(S, IP);
Sanjoy Dase6b995f2017-10-13 05:50:52 +00001828 addToLoopUseLists(S);
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001829 return S;
1830 }
Dan Gohman74a0ba12009-07-13 20:55:53 +00001831
Nick Lewyckybc98f5b2011-01-23 06:20:19 +00001832 // sext(trunc(x)) --> sext(x) or x or trunc(x)
1833 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
1834 // It's possible the bits taken off by the truncate were all sign bits. If
1835 // so, we should be able to simplify this further.
1836 const SCEV *X = ST->getOperand();
1837 ConstantRange CR = getSignedRange(X);
Nick Lewyckybc98f5b2011-01-23 06:20:19 +00001838 unsigned TruncBits = getTypeSizeInBits(ST->getType());
1839 unsigned NewBits = getTypeSizeInBits(Ty);
1840 if (CR.truncate(TruncBits).signExtend(NewBits).contains(
Nick Lewyckyd4192f72011-01-23 20:06:05 +00001841 CR.sextOrTrunc(NewBits)))
1842 return getTruncateOrSignExtend(X, Ty);
Nick Lewyckybc98f5b2011-01-23 06:20:19 +00001843 }
1844
Michael Zolotukhind4c72462014-05-24 08:09:57 +00001845 // sext(C1 + (C2 * x)) --> C1 + sext(C2 * x) if C1 < C2
Sanjoy Das1195dbe2015-10-08 03:45:58 +00001846 if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) {
Michael Zolotukhind4c72462014-05-24 08:09:57 +00001847 if (SA->getNumOperands() == 2) {
Sanjoy Das1195dbe2015-10-08 03:45:58 +00001848 auto *SC1 = dyn_cast<SCEVConstant>(SA->getOperand(0));
1849 auto *SMul = dyn_cast<SCEVMulExpr>(SA->getOperand(1));
Michael Zolotukhind4c72462014-05-24 08:09:57 +00001850 if (SMul && SC1) {
Sanjoy Das1195dbe2015-10-08 03:45:58 +00001851 if (auto *SC2 = dyn_cast<SCEVConstant>(SMul->getOperand(0))) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00001852 const APInt &C1 = SC1->getAPInt();
1853 const APInt &C2 = SC2->getAPInt();
Michael Zolotukhind4c72462014-05-24 08:09:57 +00001854 if (C1.isStrictlyPositive() && C2.isStrictlyPositive() &&
Michael Zolotukhin265dfa42014-05-26 14:49:46 +00001855 C2.ugt(C1) && C2.isPowerOf2())
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001856 return getAddExpr(getSignExtendExpr(SC1, Ty, Depth + 1),
1857 getSignExtendExpr(SMul, Ty, Depth + 1),
1858 SCEV::FlagAnyWrap, Depth + 1);
Michael Zolotukhind4c72462014-05-24 08:09:57 +00001859 }
1860 }
1861 }
Sanjoy Dasa060e602015-10-22 19:57:25 +00001862
1863 // sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw>
Sanjoy Das76c48e02016-02-04 18:21:54 +00001864 if (SA->hasNoSignedWrap()) {
Sanjoy Dasa060e602015-10-22 19:57:25 +00001865 // If the addition does not sign overflow then we can, by definition,
1866 // commute the sign extension with the addition operation.
1867 SmallVector<const SCEV *, 4> Ops;
1868 for (const auto *Op : SA->operands())
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001869 Ops.push_back(getSignExtendExpr(Op, Ty, Depth + 1));
1870 return getAddExpr(Ops, SCEV::FlagNSW, Depth + 1);
Sanjoy Dasa060e602015-10-22 19:57:25 +00001871 }
Michael Zolotukhind4c72462014-05-24 08:09:57 +00001872 }
Dan Gohman76466372009-04-27 20:16:15 +00001873 // If the input value is a chrec scev, and we can prove that the value
Dan Gohmancb9e09a2007-06-15 14:38:12 +00001874 // did not overflow the old, smaller, value, we can sign extend all of the
Dan Gohman76466372009-04-27 20:16:15 +00001875 // operands (often constants). This allows analysis of something like
Dan Gohmancb9e09a2007-06-15 14:38:12 +00001876 // this: for (signed char X = 0; X < 100; ++X) { int Y = X; }
Dan Gohmana30370b2009-05-04 22:02:23 +00001877 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
Dan Gohman76466372009-04-27 20:16:15 +00001878 if (AR->isAffine()) {
Dan Gohmane65c9172009-07-13 21:35:55 +00001879 const SCEV *Start = AR->getStart();
1880 const SCEV *Step = AR->getStepRecurrence(*this);
1881 unsigned BitWidth = getTypeSizeInBits(AR->getType());
1882 const Loop *L = AR->getLoop();
1883
Sanjoy Das724f5cf2016-03-03 18:31:29 +00001884 if (!AR->hasNoSignedWrap()) {
1885 auto NewFlags = proveNoWrapViaConstantRanges(AR);
1886 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(NewFlags);
1887 }
1888
Dan Gohman62ef6a72009-07-25 01:22:26 +00001889 // If we have special knowledge that this addrec won't overflow,
1890 // we don't need to do any further analysis.
Sanjoy Das76c48e02016-02-04 18:21:54 +00001891 if (AR->hasNoSignedWrap())
Sanjoy Das4153f472015-02-18 01:47:07 +00001892 return getAddRecExpr(
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001893 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1),
1894 getSignExtendExpr(Step, Ty, Depth + 1), L, SCEV::FlagNSW);
Dan Gohman62ef6a72009-07-25 01:22:26 +00001895
Dan Gohman76466372009-04-27 20:16:15 +00001896 // Check whether the backedge-taken count is SCEVCouldNotCompute.
1897 // Note that this serves two purposes: It filters out loops that are
1898 // simply not analyzable, and it covers the case where this code is
1899 // being called from within backedge-taken count analysis, such that
1900 // attempting to ask for the backedge-taken count would likely result
1901 // in infinite recursion. In the later case, the analysis code will
1902 // cope with a conservative value, and it will take care to purge
1903 // that value once it has finished.
Dan Gohmane65c9172009-07-13 21:35:55 +00001904 const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
Dan Gohman2b8da352009-04-30 20:47:05 +00001905 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
Dan Gohman95c5b0e2009-04-29 01:54:20 +00001906 // Manually compute the final value for AR, checking for
Dan Gohman494dac32009-04-29 22:28:28 +00001907 // overflow.
Dan Gohman76466372009-04-27 20:16:15 +00001908
1909 // Check whether the backedge-taken count can be losslessly casted to
Dan Gohman494dac32009-04-29 22:28:28 +00001910 // the addrec's type. The count is always unsigned.
Dan Gohmanaf752342009-07-07 17:06:11 +00001911 const SCEV *CastedMaxBECount =
Dan Gohman2b8da352009-04-30 20:47:05 +00001912 getTruncateOrZeroExtend(MaxBECount, Start->getType());
Dan Gohmanaf752342009-07-07 17:06:11 +00001913 const SCEV *RecastedMaxBECount =
Dan Gohman4fc36682009-05-18 15:58:39 +00001914 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
1915 if (MaxBECount == RecastedMaxBECount) {
Chris Lattner229907c2011-07-18 04:54:35 +00001916 Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
Dan Gohman2b8da352009-04-30 20:47:05 +00001917 // Check whether Start+Step*MaxBECount has no signed overflow.
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001918 const SCEV *SMul = getMulExpr(CastedMaxBECount, Step,
1919 SCEV::FlagAnyWrap, Depth + 1);
1920 const SCEV *SAdd = getSignExtendExpr(getAddExpr(Start, SMul,
1921 SCEV::FlagAnyWrap,
1922 Depth + 1),
1923 WideTy, Depth + 1);
1924 const SCEV *WideStart = getSignExtendExpr(Start, WideTy, Depth + 1);
Nuno Lopesc2a170e2012-05-15 20:20:14 +00001925 const SCEV *WideMaxBECount =
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001926 getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1);
1927 const SCEV *OperandExtendedAdd =
1928 getAddExpr(WideStart,
1929 getMulExpr(WideMaxBECount,
1930 getSignExtendExpr(Step, WideTy, Depth + 1),
1931 SCEV::FlagAnyWrap, Depth + 1),
1932 SCEV::FlagAnyWrap, Depth + 1);
Nuno Lopesc2a170e2012-05-15 20:20:14 +00001933 if (SAdd == OperandExtendedAdd) {
Andrew Trickf6b01ff2011-03-15 00:37:00 +00001934 // Cache knowledge of AR NSW, which is propagated to this AddRec.
1935 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
Dan Gohman494dac32009-04-29 22:28:28 +00001936 // Return the expression with the addrec on the outside.
Sanjoy Das4153f472015-02-18 01:47:07 +00001937 return getAddRecExpr(
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001938 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this,
1939 Depth + 1),
1940 getSignExtendExpr(Step, Ty, Depth + 1), L,
Wei Mi8c405332017-04-17 20:40:05 +00001941 AR->getNoWrapFlags());
Andrew Trickf6b01ff2011-03-15 00:37:00 +00001942 }
Dan Gohman8c129d72009-07-16 17:34:36 +00001943 // Similar to above, only this time treat the step value as unsigned.
1944 // This covers loops that count up with an unsigned step.
Dan Gohman8c129d72009-07-16 17:34:36 +00001945 OperandExtendedAdd =
Nuno Lopesc2a170e2012-05-15 20:20:14 +00001946 getAddExpr(WideStart,
1947 getMulExpr(WideMaxBECount,
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001948 getZeroExtendExpr(Step, WideTy, Depth + 1),
1949 SCEV::FlagAnyWrap, Depth + 1),
1950 SCEV::FlagAnyWrap, Depth + 1);
Nuno Lopesc2a170e2012-05-15 20:20:14 +00001951 if (SAdd == OperandExtendedAdd) {
Sanjoy Dasbf5d8702015-02-09 18:34:55 +00001952 // If AR wraps around then
1953 //
1954 // abs(Step) * MaxBECount > unsigned-max(AR->getType())
1955 // => SAdd != OperandExtendedAdd
1956 //
1957 // Thus (AR is not NW => SAdd != OperandExtendedAdd) <=>
1958 // (SAdd == OperandExtendedAdd => AR is NW)
1959
1960 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
1961
Dan Gohman8c129d72009-07-16 17:34:36 +00001962 // Return the expression with the addrec on the outside.
Sanjoy Das4153f472015-02-18 01:47:07 +00001963 return getAddRecExpr(
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001964 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this,
1965 Depth + 1),
1966 getZeroExtendExpr(Step, Ty, Depth + 1), L,
1967 AR->getNoWrapFlags());
Andrew Trickf6b01ff2011-03-15 00:37:00 +00001968 }
Dan Gohmane65c9172009-07-13 21:35:55 +00001969 }
Sanjoy Das787c2462016-05-11 17:41:26 +00001970 }
Dan Gohmane65c9172009-07-13 21:35:55 +00001971
Sanjoy Das787c2462016-05-11 17:41:26 +00001972 // Normally, in the cases we can prove no-overflow via a
1973 // backedge guarding condition, we can also compute a backedge
1974 // taken count for the loop. The exceptions are assumptions and
1975 // guards present in the loop -- SCEV is not great at exploiting
1976 // these to compute max backedge taken counts, but can still use
1977 // these to prove lack of overflow. Use this fact to avoid
1978 // doing extra work that may not pay off.
1979
1980 if (!isa<SCEVCouldNotCompute>(MaxBECount) || HasGuards ||
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001981 !AC.assumptions().empty()) {
Sanjoy Das787c2462016-05-11 17:41:26 +00001982 // If the backedge is guarded by a comparison with the pre-inc
1983 // value the addrec is safe. Also, if the entry is guarded by
1984 // a comparison with the start value and the backedge is
1985 // guarded by a comparison with the post-inc value, the addrec
1986 // is safe.
Andrew Trick812276e2011-05-31 21:17:47 +00001987 ICmpInst::Predicate Pred;
Sanjoy Das4153f472015-02-18 01:47:07 +00001988 const SCEV *OverflowLimit =
1989 getSignedOverflowLimitForStep(Step, &Pred, this);
Andrew Trick812276e2011-05-31 21:17:47 +00001990 if (OverflowLimit &&
1991 (isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) ||
1992 (isLoopEntryGuardedByCond(L, Pred, Start, OverflowLimit) &&
1993 isLoopBackedgeGuardedByCond(L, Pred, AR->getPostIncExpr(*this),
1994 OverflowLimit)))) {
1995 // Cache knowledge of AR NSW, then propagate NSW to the wide AddRec.
1996 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
Sanjoy Das4153f472015-02-18 01:47:07 +00001997 return getAddRecExpr(
Max Kazantsev8d0322e2017-06-30 05:04:09 +00001998 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1),
1999 getSignExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
Dan Gohman76466372009-04-27 20:16:15 +00002000 }
2001 }
Sanjoy Das787c2462016-05-11 17:41:26 +00002002
Michael Zolotukhind4c72462014-05-24 08:09:57 +00002003 // If Start and Step are constants, check if we can apply this
2004 // transformation:
2005 // sext{C1,+,C2} --> C1 + sext{0,+,C2} if C1 < C2
Sanjoy Das1195dbe2015-10-08 03:45:58 +00002006 auto *SC1 = dyn_cast<SCEVConstant>(Start);
2007 auto *SC2 = dyn_cast<SCEVConstant>(Step);
Michael Zolotukhind4c72462014-05-24 08:09:57 +00002008 if (SC1 && SC2) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002009 const APInt &C1 = SC1->getAPInt();
2010 const APInt &C2 = SC2->getAPInt();
Michael Zolotukhin265dfa42014-05-26 14:49:46 +00002011 if (C1.isStrictlyPositive() && C2.isStrictlyPositive() && C2.ugt(C1) &&
2012 C2.isPowerOf2()) {
Max Kazantsev8d0322e2017-06-30 05:04:09 +00002013 Start = getSignExtendExpr(Start, Ty, Depth + 1);
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002014 const SCEV *NewAR = getAddRecExpr(getZero(AR->getType()), Step, L,
2015 AR->getNoWrapFlags());
Max Kazantsev8d0322e2017-06-30 05:04:09 +00002016 return getAddExpr(Start, getSignExtendExpr(NewAR, Ty, Depth + 1),
2017 SCEV::FlagAnyWrap, Depth + 1);
Michael Zolotukhind4c72462014-05-24 08:09:57 +00002018 }
2019 }
Sanjoy Das9e2c5012015-03-04 22:24:17 +00002020
2021 if (proveNoWrapByVaryingStart<SCEVSignExtendExpr>(Start, Step, L)) {
2022 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
2023 return getAddRecExpr(
Max Kazantsev8d0322e2017-06-30 05:04:09 +00002024 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1),
2025 getSignExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
Sanjoy Das9e2c5012015-03-04 22:24:17 +00002026 }
Dan Gohman76466372009-04-27 20:16:15 +00002027 }
Dan Gohmancb9e09a2007-06-15 14:38:12 +00002028
Sanjoy Das11ef6062016-03-03 18:31:23 +00002029 // If the input value is provably positive and we could not simplify
2030 // away the sext build a zext instead.
2031 if (isKnownNonNegative(Op))
Max Kazantsev8d0322e2017-06-30 05:04:09 +00002032 return getZeroExtendExpr(Op, Ty, Depth + 1);
Sanjoy Das11ef6062016-03-03 18:31:23 +00002033
Dan Gohman74a0ba12009-07-13 20:55:53 +00002034 // The cast wasn't folded; create an explicit cast node.
2035 // Recompute the insert position, as it may have been invalidated.
Dan Gohmanc5c85c02009-06-27 21:21:31 +00002036 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
Dan Gohman01c65a22010-03-18 18:49:47 +00002037 SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
2038 Op, Ty);
Dan Gohmanc5c85c02009-06-27 21:21:31 +00002039 UniqueSCEVs.InsertNode(S, IP);
Sanjoy Dase6b995f2017-10-13 05:50:52 +00002040 addToLoopUseLists(S);
Dan Gohmanc5c85c02009-06-27 21:21:31 +00002041 return S;
Dan Gohmancb9e09a2007-06-15 14:38:12 +00002042}
2043
Dan Gohman8db2edc2009-06-13 15:56:47 +00002044/// getAnyExtendExpr - Return a SCEV for the given operand extended with
2045/// unspecified bits out to the given type.
Dan Gohmanaf752342009-07-07 17:06:11 +00002046const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op,
Chris Lattner229907c2011-07-18 04:54:35 +00002047 Type *Ty) {
Dan Gohman8db2edc2009-06-13 15:56:47 +00002048 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
2049 "This is not an extending conversion!");
2050 assert(isSCEVable(Ty) &&
2051 "This is not a conversion to a SCEVable type!");
2052 Ty = getEffectiveSCEVType(Ty);
2053
2054 // Sign-extend negative constants.
2055 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002056 if (SC->getAPInt().isNegative())
Dan Gohman8db2edc2009-06-13 15:56:47 +00002057 return getSignExtendExpr(Op, Ty);
2058
2059 // Peel off a truncate cast.
2060 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) {
Dan Gohmanaf752342009-07-07 17:06:11 +00002061 const SCEV *NewOp = T->getOperand();
Dan Gohman8db2edc2009-06-13 15:56:47 +00002062 if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty))
2063 return getAnyExtendExpr(NewOp, Ty);
2064 return getTruncateOrNoop(NewOp, Ty);
2065 }
2066
2067 // Next try a zext cast. If the cast is folded, use it.
Dan Gohmanaf752342009-07-07 17:06:11 +00002068 const SCEV *ZExt = getZeroExtendExpr(Op, Ty);
Dan Gohman8db2edc2009-06-13 15:56:47 +00002069 if (!isa<SCEVZeroExtendExpr>(ZExt))
2070 return ZExt;
2071
2072 // Next try a sext cast. If the cast is folded, use it.
Dan Gohmanaf752342009-07-07 17:06:11 +00002073 const SCEV *SExt = getSignExtendExpr(Op, Ty);
Dan Gohman8db2edc2009-06-13 15:56:47 +00002074 if (!isa<SCEVSignExtendExpr>(SExt))
2075 return SExt;
2076
Dan Gohman51ad99d2010-01-21 02:09:26 +00002077 // Force the cast to be folded into the operands of an addrec.
2078 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) {
2079 SmallVector<const SCEV *, 4> Ops;
Tobias Grosser924221c2014-05-07 06:07:47 +00002080 for (const SCEV *Op : AR->operands())
2081 Ops.push_back(getAnyExtendExpr(Op, Ty));
Andrew Trickf6b01ff2011-03-15 00:37:00 +00002082 return getAddRecExpr(Ops, AR->getLoop(), SCEV::FlagNW);
Dan Gohman51ad99d2010-01-21 02:09:26 +00002083 }
2084
Dan Gohman8db2edc2009-06-13 15:56:47 +00002085 // If the expression is obviously signed, use the sext cast value.
2086 if (isa<SCEVSMaxExpr>(Op))
2087 return SExt;
2088
2089 // Absent any other information, use the zext cast value.
2090 return ZExt;
2091}
2092
Sanjoy Dasf8570812016-05-29 00:38:22 +00002093/// Process the given Ops list, which is a list of operands to be added under
2094/// the given scale, update the given map. This is a helper function for
2095/// getAddRecExpr. As an example of what it does, given a sequence of operands
2096/// that would form an add expression like this:
Dan Gohman038d02e2009-06-14 22:58:51 +00002097///
Tobias Grosserba49e422014-03-05 10:37:17 +00002098/// m + n + 13 + (A * (o + p + (B * (q + m + 29)))) + r + (-1 * r)
Dan Gohman038d02e2009-06-14 22:58:51 +00002099///
2100/// where A and B are constants, update the map with these values:
2101///
2102/// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0)
2103///
2104/// and add 13 + A*B*29 to AccumulatedConstant.
2105/// This will allow getAddRecExpr to produce this:
2106///
2107/// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B)
2108///
2109/// This form often exposes folding opportunities that are hidden in
2110/// the original operand list.
2111///
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002112/// Return true iff it appears that any interesting folding opportunities
Dan Gohman038d02e2009-06-14 22:58:51 +00002113/// may be exposed. This helps getAddRecExpr short-circuit extra work in
2114/// the common case where no interesting opportunities are present, and
2115/// is also used as a check to avoid infinite recursion.
Dan Gohman038d02e2009-06-14 22:58:51 +00002116static bool
Dan Gohmanaf752342009-07-07 17:06:11 +00002117CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M,
Craig Topper2cd5ff82013-07-11 16:22:38 +00002118 SmallVectorImpl<const SCEV *> &NewOps,
Dan Gohman038d02e2009-06-14 22:58:51 +00002119 APInt &AccumulatedConstant,
Dan Gohman00524492010-03-18 01:17:13 +00002120 const SCEV *const *Ops, size_t NumOperands,
Dan Gohman038d02e2009-06-14 22:58:51 +00002121 const APInt &Scale,
2122 ScalarEvolution &SE) {
2123 bool Interesting = false;
2124
Dan Gohman45073042010-06-18 19:12:32 +00002125 // Iterate over the add operands. They are sorted, with constants first.
2126 unsigned i = 0;
2127 while (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
2128 ++i;
2129 // Pull a buried constant out to the outside.
2130 if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero())
2131 Interesting = true;
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002132 AccumulatedConstant += Scale * C->getAPInt();
Dan Gohman45073042010-06-18 19:12:32 +00002133 }
2134
2135 // Next comes everything else. We're especially interested in multiplies
2136 // here, but they're in the middle, so just visit the rest with one loop.
2137 for (; i != NumOperands; ++i) {
Dan Gohman038d02e2009-06-14 22:58:51 +00002138 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]);
2139 if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) {
2140 APInt NewScale =
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002141 Scale * cast<SCEVConstant>(Mul->getOperand(0))->getAPInt();
Dan Gohman038d02e2009-06-14 22:58:51 +00002142 if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) {
2143 // A multiplication of a constant with another add; recurse.
Dan Gohman00524492010-03-18 01:17:13 +00002144 const SCEVAddExpr *Add = cast<SCEVAddExpr>(Mul->getOperand(1));
Dan Gohman038d02e2009-06-14 22:58:51 +00002145 Interesting |=
2146 CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
Dan Gohman00524492010-03-18 01:17:13 +00002147 Add->op_begin(), Add->getNumOperands(),
Dan Gohman038d02e2009-06-14 22:58:51 +00002148 NewScale, SE);
2149 } else {
2150 // A multiplication of a constant with some other value. Update
2151 // the map.
Dan Gohmanaf752342009-07-07 17:06:11 +00002152 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end());
2153 const SCEV *Key = SE.getMulExpr(MulOps);
Sanjoy Dasc42f7cc2016-02-20 01:35:56 +00002154 auto Pair = M.insert({Key, NewScale});
Dan Gohman038d02e2009-06-14 22:58:51 +00002155 if (Pair.second) {
Dan Gohman038d02e2009-06-14 22:58:51 +00002156 NewOps.push_back(Pair.first->first);
2157 } else {
2158 Pair.first->second += NewScale;
2159 // The map already had an entry for this value, which may indicate
2160 // a folding opportunity.
2161 Interesting = true;
2162 }
2163 }
Dan Gohman038d02e2009-06-14 22:58:51 +00002164 } else {
2165 // An ordinary operand. Update the map.
Dan Gohmanaf752342009-07-07 17:06:11 +00002166 std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
Sanjoy Dasc42f7cc2016-02-20 01:35:56 +00002167 M.insert({Ops[i], Scale});
Dan Gohman038d02e2009-06-14 22:58:51 +00002168 if (Pair.second) {
Dan Gohman038d02e2009-06-14 22:58:51 +00002169 NewOps.push_back(Pair.first->first);
2170 } else {
2171 Pair.first->second += Scale;
2172 // The map already had an entry for this value, which may indicate
2173 // a folding opportunity.
2174 Interesting = true;
2175 }
2176 }
2177 }
2178
2179 return Interesting;
2180}
2181
Sanjoy Das81401d42015-01-10 23:41:24 +00002182// We're trying to construct a SCEV of type `Type' with `Ops' as operands and
2183// `OldFlags' as can't-wrap behavior. Infer a more aggressive set of
2184// can't-overflow flags for the operation if possible.
2185static SCEV::NoWrapFlags
2186StrengthenNoWrapFlags(ScalarEvolution *SE, SCEVTypes Type,
2187 const SmallVectorImpl<const SCEV *> &Ops,
Sanjoy Das8f274152015-10-22 19:57:19 +00002188 SCEV::NoWrapFlags Flags) {
Sanjoy Das81401d42015-01-10 23:41:24 +00002189 using namespace std::placeholders;
Eugene Zelenkobe709f22017-08-18 23:51:26 +00002190
2191 using OBO = OverflowingBinaryOperator;
Sanjoy Das81401d42015-01-10 23:41:24 +00002192
2193 bool CanAnalyze =
2194 Type == scAddExpr || Type == scAddRecExpr || Type == scMulExpr;
2195 (void)CanAnalyze;
2196 assert(CanAnalyze && "don't call from other places!");
2197
2198 int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW;
2199 SCEV::NoWrapFlags SignOrUnsignWrap =
Sanjoy Das8f274152015-10-22 19:57:19 +00002200 ScalarEvolution::maskFlags(Flags, SignOrUnsignMask);
Sanjoy Das81401d42015-01-10 23:41:24 +00002201
2202 // If FlagNSW is true and all the operands are non-negative, infer FlagNUW.
Sanjoy Das9b0015f2015-11-29 23:40:57 +00002203 auto IsKnownNonNegative = [&](const SCEV *S) {
2204 return SE->isKnownNonNegative(S);
2205 };
Sanjoy Das81401d42015-01-10 23:41:24 +00002206
Sanjoy Das3b827c72015-11-29 23:40:53 +00002207 if (SignOrUnsignWrap == SCEV::FlagNSW && all_of(Ops, IsKnownNonNegative))
Sanjoy Das8f274152015-10-22 19:57:19 +00002208 Flags =
2209 ScalarEvolution::setFlags(Flags, (SCEV::NoWrapFlags)SignOrUnsignMask);
Sanjoy Das81401d42015-01-10 23:41:24 +00002210
Sanjoy Das8f274152015-10-22 19:57:19 +00002211 SignOrUnsignWrap = ScalarEvolution::maskFlags(Flags, SignOrUnsignMask);
2212
2213 if (SignOrUnsignWrap != SignOrUnsignMask && Type == scAddExpr &&
2214 Ops.size() == 2 && isa<SCEVConstant>(Ops[0])) {
2215
2216 // (A + C) --> (A + C)<nsw> if the addition does not sign overflow
2217 // (A + C) --> (A + C)<nuw> if the addition does not unsign overflow
2218
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002219 const APInt &C = cast<SCEVConstant>(Ops[0])->getAPInt();
Sanjoy Das8f274152015-10-22 19:57:19 +00002220 if (!(SignOrUnsignWrap & SCEV::FlagNSW)) {
Sanjoy Das5079f622016-02-22 16:13:02 +00002221 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
2222 Instruction::Add, C, OBO::NoSignedWrap);
Sanjoy Das8f274152015-10-22 19:57:19 +00002223 if (NSWRegion.contains(SE->getSignedRange(Ops[1])))
2224 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW);
2225 }
2226 if (!(SignOrUnsignWrap & SCEV::FlagNUW)) {
Sanjoy Das5079f622016-02-22 16:13:02 +00002227 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
2228 Instruction::Add, C, OBO::NoUnsignedWrap);
Sanjoy Das8f274152015-10-22 19:57:19 +00002229 if (NUWRegion.contains(SE->getUnsignedRange(Ops[1])))
2230 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
2231 }
2232 }
2233
2234 return Flags;
Sanjoy Das81401d42015-01-10 23:41:24 +00002235}
2236
Max Kazantsevd8fe3eb2017-05-30 10:54:58 +00002237bool ScalarEvolution::isAvailableAtLoopEntry(const SCEV *S, const Loop *L) {
Max Kazantsev41450322017-05-26 06:47:04 +00002238 if (!isLoopInvariant(S, L))
2239 return false;
2240 // If a value depends on a SCEVUnknown which is defined after the loop, we
2241 // conservatively assume that we cannot calculate it at the loop's entry.
2242 struct FindDominatedSCEVUnknown {
2243 bool Found = false;
2244 const Loop *L;
2245 DominatorTree &DT;
2246 LoopInfo &LI;
2247
2248 FindDominatedSCEVUnknown(const Loop *L, DominatorTree &DT, LoopInfo &LI)
2249 : L(L), DT(DT), LI(LI) {}
2250
2251 bool checkSCEVUnknown(const SCEVUnknown *SU) {
2252 if (auto *I = dyn_cast<Instruction>(SU->getValue())) {
2253 if (DT.dominates(L->getHeader(), I->getParent()))
2254 Found = true;
2255 else
2256 assert(DT.dominates(I->getParent(), L->getHeader()) &&
2257 "No dominance relationship between SCEV and loop?");
2258 }
2259 return false;
2260 }
2261
2262 bool follow(const SCEV *S) {
2263 switch (static_cast<SCEVTypes>(S->getSCEVType())) {
2264 case scConstant:
2265 return false;
2266 case scAddRecExpr:
2267 case scTruncate:
2268 case scZeroExtend:
2269 case scSignExtend:
2270 case scAddExpr:
2271 case scMulExpr:
2272 case scUMaxExpr:
2273 case scSMaxExpr:
2274 case scUDivExpr:
2275 return true;
2276 case scUnknown:
2277 return checkSCEVUnknown(cast<SCEVUnknown>(S));
2278 case scCouldNotCompute:
2279 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
2280 }
2281 return false;
2282 }
2283
2284 bool isDone() { return Found; }
2285 };
2286
2287 FindDominatedSCEVUnknown FSU(L, DT, LI);
2288 SCEVTraversal<FindDominatedSCEVUnknown> ST(FSU);
2289 ST.visitAll(S);
2290 return !FSU.Found;
2291}
2292
Sanjoy Dasf8570812016-05-29 00:38:22 +00002293/// Get a canonical add expression, or something simpler if possible.
Dan Gohman816fe0a2009-10-09 00:10:36 +00002294const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
Daniil Fukalov6378bdb2017-02-06 12:38:06 +00002295 SCEV::NoWrapFlags Flags,
2296 unsigned Depth) {
Andrew Trick8b55b732011-03-14 16:50:06 +00002297 assert(!(Flags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
2298 "only nuw or nsw allowed");
Chris Lattnerd934c702004-04-02 20:23:17 +00002299 assert(!Ops.empty() && "Cannot get empty add!");
Chris Lattner74498e12004-04-07 16:16:11 +00002300 if (Ops.size() == 1) return Ops[0];
Dan Gohmand33f36e2009-05-18 15:44:58 +00002301#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +00002302 Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
Dan Gohmand33f36e2009-05-18 15:44:58 +00002303 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
Dan Gohman9136d9f2010-06-18 19:09:27 +00002304 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
Dan Gohmand33f36e2009-05-18 15:44:58 +00002305 "SCEVAddExpr operand types don't match!");
2306#endif
Chris Lattnerd934c702004-04-02 20:23:17 +00002307
2308 // Sort by complexity, this groups all similar expression types together.
Max Kazantsevb09b5db2017-05-16 07:27:06 +00002309 GroupByComplexity(Ops, &LI, DT);
Chris Lattnerd934c702004-04-02 20:23:17 +00002310
Sanjoy Das64895612015-10-09 02:44:45 +00002311 Flags = StrengthenNoWrapFlags(this, scAddExpr, Ops, Flags);
2312
Chris Lattnerd934c702004-04-02 20:23:17 +00002313 // If there are any constants, fold them together.
2314 unsigned Idx = 0;
Dan Gohmana30370b2009-05-04 22:02:23 +00002315 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Chris Lattnerd934c702004-04-02 20:23:17 +00002316 ++Idx;
Chris Lattner74498e12004-04-07 16:16:11 +00002317 assert(Idx < Ops.size());
Dan Gohmana30370b2009-05-04 22:02:23 +00002318 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Chris Lattnerd934c702004-04-02 20:23:17 +00002319 // We found two constants, fold them together!
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002320 Ops[0] = getConstant(LHSC->getAPInt() + RHSC->getAPInt());
Dan Gohman011cf682009-06-14 22:53:57 +00002321 if (Ops.size() == 2) return Ops[0];
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00002322 Ops.erase(Ops.begin()+1); // Erase the folded element
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00002323 LHSC = cast<SCEVConstant>(Ops[0]);
Chris Lattnerd934c702004-04-02 20:23:17 +00002324 }
2325
2326 // If we are left with a constant zero being added, strip it off.
Dan Gohmanebbd05f2010-04-12 23:08:18 +00002327 if (LHSC->getValue()->isZero()) {
Chris Lattnerd934c702004-04-02 20:23:17 +00002328 Ops.erase(Ops.begin());
2329 --Idx;
2330 }
Chris Lattnerd934c702004-04-02 20:23:17 +00002331
Dan Gohmanebbd05f2010-04-12 23:08:18 +00002332 if (Ops.size() == 1) return Ops[0];
2333 }
Misha Brukman01808ca2005-04-21 21:13:18 +00002334
Max Kazantsevdc803662017-06-15 11:48:21 +00002335 // Limit recursion calls depth.
2336 if (Depth > MaxArithDepth)
Daniil Fukalov6378bdb2017-02-06 12:38:06 +00002337 return getOrCreateAddExpr(Ops, Flags);
2338
Dan Gohman15871f22010-08-27 21:39:59 +00002339 // Okay, check to see if the same value occurs in the operand list more than
Reid Kleckner30422ee2016-12-12 18:52:32 +00002340 // once. If so, merge them together into an multiply expression. Since we
Dan Gohman15871f22010-08-27 21:39:59 +00002341 // sorted the list, these values are required to be adjacent.
Chris Lattner229907c2011-07-18 04:54:35 +00002342 Type *Ty = Ops[0]->getType();
Dan Gohmane67b2872010-08-12 14:46:54 +00002343 bool FoundMatch = false;
Dan Gohman15871f22010-08-27 21:39:59 +00002344 for (unsigned i = 0, e = Ops.size(); i != e-1; ++i)
Chris Lattnerd934c702004-04-02 20:23:17 +00002345 if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2
Dan Gohman15871f22010-08-27 21:39:59 +00002346 // Scan ahead to count how many equal operands there are.
2347 unsigned Count = 2;
2348 while (i+Count != e && Ops[i+Count] == Ops[i])
2349 ++Count;
2350 // Merge the values into a multiply.
2351 const SCEV *Scale = getConstant(Ty, Count);
Max Kazantsevdc803662017-06-15 11:48:21 +00002352 const SCEV *Mul = getMulExpr(Scale, Ops[i], SCEV::FlagAnyWrap, Depth + 1);
Dan Gohman15871f22010-08-27 21:39:59 +00002353 if (Ops.size() == Count)
Chris Lattnerd934c702004-04-02 20:23:17 +00002354 return Mul;
Dan Gohmane67b2872010-08-12 14:46:54 +00002355 Ops[i] = Mul;
Dan Gohman15871f22010-08-27 21:39:59 +00002356 Ops.erase(Ops.begin()+i+1, Ops.begin()+i+Count);
Dan Gohmanfe22f1d2010-08-28 00:39:27 +00002357 --i; e -= Count - 1;
Dan Gohmane67b2872010-08-12 14:46:54 +00002358 FoundMatch = true;
Chris Lattnerd934c702004-04-02 20:23:17 +00002359 }
Dan Gohmane67b2872010-08-12 14:46:54 +00002360 if (FoundMatch)
Andrew Trick8b55b732011-03-14 16:50:06 +00002361 return getAddExpr(Ops, Flags);
Chris Lattnerd934c702004-04-02 20:23:17 +00002362
Dan Gohman2e55cc52009-05-08 21:03:19 +00002363 // Check for truncates. If all the operands are truncated from the same
2364 // type, see if factoring out the truncate would permit the result to be
Daniel Neilson1341ac22017-09-22 15:47:57 +00002365 // folded. eg., n*trunc(x) + m*trunc(y) --> trunc(trunc(m)*x + trunc(n)*y)
Dan Gohman2e55cc52009-05-08 21:03:19 +00002366 // if the contents of the resulting outer trunc fold to something simple.
Daniel Neilson1341ac22017-09-22 15:47:57 +00002367 auto FindTruncSrcType = [&]() -> Type * {
2368 // We're ultimately looking to fold an addrec of truncs and muls of only
2369 // constants and truncs, so if we find any other types of SCEV
2370 // as operands of the addrec then we bail and return nullptr here.
2371 // Otherwise, we return the type of the operand of a trunc that we find.
2372 if (auto *T = dyn_cast<SCEVTruncateExpr>(Ops[Idx]))
2373 return T->getOperand()->getType();
2374 if (const auto *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
2375 const auto *LastOp = Mul->getOperand(Mul->getNumOperands() - 1);
2376 if (const auto *T = dyn_cast<SCEVTruncateExpr>(LastOp))
2377 return T->getOperand()->getType();
2378 }
2379 return nullptr;
2380 };
2381 if (auto *SrcType = FindTruncSrcType()) {
Dan Gohmanaf752342009-07-07 17:06:11 +00002382 SmallVector<const SCEV *, 8> LargeOps;
Dan Gohman2e55cc52009-05-08 21:03:19 +00002383 bool Ok = true;
2384 // Check all the operands to see if they can be represented in the
2385 // source type of the truncate.
2386 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
2387 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) {
2388 if (T->getOperand()->getType() != SrcType) {
2389 Ok = false;
2390 break;
2391 }
2392 LargeOps.push_back(T->getOperand());
2393 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
Dan Gohmanff3174e2010-04-23 01:51:29 +00002394 LargeOps.push_back(getAnyExtendExpr(C, SrcType));
Dan Gohman2e55cc52009-05-08 21:03:19 +00002395 } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) {
Dan Gohmanaf752342009-07-07 17:06:11 +00002396 SmallVector<const SCEV *, 8> LargeMulOps;
Dan Gohman2e55cc52009-05-08 21:03:19 +00002397 for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
2398 if (const SCEVTruncateExpr *T =
2399 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) {
2400 if (T->getOperand()->getType() != SrcType) {
2401 Ok = false;
2402 break;
2403 }
2404 LargeMulOps.push_back(T->getOperand());
Sanjoy Das63914592015-10-18 00:29:20 +00002405 } else if (const auto *C = dyn_cast<SCEVConstant>(M->getOperand(j))) {
Dan Gohmanff3174e2010-04-23 01:51:29 +00002406 LargeMulOps.push_back(getAnyExtendExpr(C, SrcType));
Dan Gohman2e55cc52009-05-08 21:03:19 +00002407 } else {
2408 Ok = false;
2409 break;
2410 }
2411 }
2412 if (Ok)
Max Kazantsevdc803662017-06-15 11:48:21 +00002413 LargeOps.push_back(getMulExpr(LargeMulOps, SCEV::FlagAnyWrap, Depth + 1));
Dan Gohman2e55cc52009-05-08 21:03:19 +00002414 } else {
2415 Ok = false;
2416 break;
2417 }
2418 }
2419 if (Ok) {
2420 // Evaluate the expression in the larger type.
Daniil Fukalov6378bdb2017-02-06 12:38:06 +00002421 const SCEV *Fold = getAddExpr(LargeOps, Flags, Depth + 1);
Dan Gohman2e55cc52009-05-08 21:03:19 +00002422 // If it folds to something simple, use it. Otherwise, don't.
2423 if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold))
Daniel Neilson1341ac22017-09-22 15:47:57 +00002424 return getTruncateExpr(Fold, Ty);
Dan Gohman2e55cc52009-05-08 21:03:19 +00002425 }
2426 }
2427
2428 // Skip past any other cast SCEVs.
Dan Gohmaneed125f2007-06-18 19:30:09 +00002429 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr)
2430 ++Idx;
2431
2432 // If there are add operands they would be next.
Chris Lattnerd934c702004-04-02 20:23:17 +00002433 if (Idx < Ops.size()) {
2434 bool DeletedAdd = false;
Dan Gohmana30370b2009-05-04 22:02:23 +00002435 while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
Daniil Fukalovb09dac52017-01-26 13:33:17 +00002436 if (Ops.size() > AddOpsInlineThreshold ||
2437 Add->getNumOperands() > AddOpsInlineThreshold)
2438 break;
Chris Lattnerd934c702004-04-02 20:23:17 +00002439 // If we have an add, expand the add operands onto the end of the operands
2440 // list.
Chris Lattnerd934c702004-04-02 20:23:17 +00002441 Ops.erase(Ops.begin()+Idx);
Dan Gohmandd41bba2010-06-21 19:47:52 +00002442 Ops.append(Add->op_begin(), Add->op_end());
Chris Lattnerd934c702004-04-02 20:23:17 +00002443 DeletedAdd = true;
2444 }
2445
2446 // If we deleted at least one add, we added operands to the end of the list,
2447 // and they are not necessarily sorted. Recurse to resort and resimplify
Dan Gohman8b0a4192010-03-01 17:49:51 +00002448 // any operands we just acquired.
Chris Lattnerd934c702004-04-02 20:23:17 +00002449 if (DeletedAdd)
Daniil Fukalov6378bdb2017-02-06 12:38:06 +00002450 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
Chris Lattnerd934c702004-04-02 20:23:17 +00002451 }
2452
2453 // Skip over the add expression until we get to a multiply.
2454 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
2455 ++Idx;
2456
Dan Gohman038d02e2009-06-14 22:58:51 +00002457 // Check to see if there are any folding opportunities present with
2458 // operands multiplied by constant values.
2459 if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) {
2460 uint64_t BitWidth = getTypeSizeInBits(Ty);
Dan Gohmanaf752342009-07-07 17:06:11 +00002461 DenseMap<const SCEV *, APInt> M;
2462 SmallVector<const SCEV *, 8> NewOps;
Dan Gohman038d02e2009-06-14 22:58:51 +00002463 APInt AccumulatedConstant(BitWidth, 0);
2464 if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
Dan Gohman00524492010-03-18 01:17:13 +00002465 Ops.data(), Ops.size(),
2466 APInt(BitWidth, 1), *this)) {
Sanjoy Das7d752672015-12-08 04:32:54 +00002467 struct APIntCompare {
2468 bool operator()(const APInt &LHS, const APInt &RHS) const {
2469 return LHS.ult(RHS);
2470 }
2471 };
2472
Dan Gohman038d02e2009-06-14 22:58:51 +00002473 // Some interesting folding opportunity is present, so its worthwhile to
2474 // re-generate the operands list. Group the operands by constant scale,
2475 // to avoid multiplying by the same constant scale multiple times.
Dan Gohmanaf752342009-07-07 17:06:11 +00002476 std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists;
Sanjoy Dasf25d25a2015-10-31 23:21:32 +00002477 for (const SCEV *NewOp : NewOps)
2478 MulOpLists[M.find(NewOp)->second].push_back(NewOp);
Dan Gohman038d02e2009-06-14 22:58:51 +00002479 // Re-generate the operands list.
2480 Ops.clear();
2481 if (AccumulatedConstant != 0)
2482 Ops.push_back(getConstant(AccumulatedConstant));
Sanjoy Dasf25d25a2015-10-31 23:21:32 +00002483 for (auto &MulOp : MulOpLists)
2484 if (MulOp.first != 0)
Daniil Fukalov6378bdb2017-02-06 12:38:06 +00002485 Ops.push_back(getMulExpr(
2486 getConstant(MulOp.first),
Max Kazantsevdc803662017-06-15 11:48:21 +00002487 getAddExpr(MulOp.second, SCEV::FlagAnyWrap, Depth + 1),
2488 SCEV::FlagAnyWrap, Depth + 1));
Dan Gohman038d02e2009-06-14 22:58:51 +00002489 if (Ops.empty())
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002490 return getZero(Ty);
Dan Gohman038d02e2009-06-14 22:58:51 +00002491 if (Ops.size() == 1)
2492 return Ops[0];
Daniil Fukalov6378bdb2017-02-06 12:38:06 +00002493 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
Dan Gohman038d02e2009-06-14 22:58:51 +00002494 }
2495 }
2496
Chris Lattnerd934c702004-04-02 20:23:17 +00002497 // If we are adding something to a multiply expression, make sure the
2498 // something is not already an operand of the multiply. If so, merge it into
2499 // the multiply.
2500 for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
Dan Gohman48f82222009-05-04 22:30:44 +00002501 const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
Chris Lattnerd934c702004-04-02 20:23:17 +00002502 for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
Dan Gohman48f82222009-05-04 22:30:44 +00002503 const SCEV *MulOpSCEV = Mul->getOperand(MulOp);
Dan Gohman157847f2010-08-12 14:52:55 +00002504 if (isa<SCEVConstant>(MulOpSCEV))
2505 continue;
Chris Lattnerd934c702004-04-02 20:23:17 +00002506 for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
Dan Gohman157847f2010-08-12 14:52:55 +00002507 if (MulOpSCEV == Ops[AddOp]) {
Chris Lattnerd934c702004-04-02 20:23:17 +00002508 // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1))
Dan Gohmanaf752342009-07-07 17:06:11 +00002509 const SCEV *InnerMul = Mul->getOperand(MulOp == 0);
Chris Lattnerd934c702004-04-02 20:23:17 +00002510 if (Mul->getNumOperands() != 2) {
2511 // If the multiply has more than two operands, we must get the
2512 // Y*Z term.
Dan Gohman797a1db2010-08-16 16:57:24 +00002513 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
2514 Mul->op_begin()+MulOp);
2515 MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end());
Max Kazantsevdc803662017-06-15 11:48:21 +00002516 InnerMul = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1);
Chris Lattnerd934c702004-04-02 20:23:17 +00002517 }
Daniil Fukalov6378bdb2017-02-06 12:38:06 +00002518 SmallVector<const SCEV *, 2> TwoOps = {getOne(Ty), InnerMul};
2519 const SCEV *AddOne = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1);
Max Kazantsevdc803662017-06-15 11:48:21 +00002520 const SCEV *OuterMul = getMulExpr(AddOne, MulOpSCEV,
2521 SCEV::FlagAnyWrap, Depth + 1);
Chris Lattnerd934c702004-04-02 20:23:17 +00002522 if (Ops.size() == 2) return OuterMul;
2523 if (AddOp < Idx) {
2524 Ops.erase(Ops.begin()+AddOp);
2525 Ops.erase(Ops.begin()+Idx-1);
2526 } else {
2527 Ops.erase(Ops.begin()+Idx);
2528 Ops.erase(Ops.begin()+AddOp-1);
2529 }
2530 Ops.push_back(OuterMul);
Daniil Fukalov6378bdb2017-02-06 12:38:06 +00002531 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
Chris Lattnerd934c702004-04-02 20:23:17 +00002532 }
Misha Brukman01808ca2005-04-21 21:13:18 +00002533
Chris Lattnerd934c702004-04-02 20:23:17 +00002534 // Check this multiply against other multiplies being added together.
2535 for (unsigned OtherMulIdx = Idx+1;
2536 OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
2537 ++OtherMulIdx) {
Dan Gohman48f82222009-05-04 22:30:44 +00002538 const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
Chris Lattnerd934c702004-04-02 20:23:17 +00002539 // If MulOp occurs in OtherMul, we can fold the two multiplies
2540 // together.
2541 for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
2542 OMulOp != e; ++OMulOp)
2543 if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
2544 // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
Dan Gohmanaf752342009-07-07 17:06:11 +00002545 const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0);
Chris Lattnerd934c702004-04-02 20:23:17 +00002546 if (Mul->getNumOperands() != 2) {
Dan Gohmance973df2009-06-24 04:48:43 +00002547 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
Dan Gohman797a1db2010-08-16 16:57:24 +00002548 Mul->op_begin()+MulOp);
2549 MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end());
Max Kazantsevdc803662017-06-15 11:48:21 +00002550 InnerMul1 = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1);
Chris Lattnerd934c702004-04-02 20:23:17 +00002551 }
Dan Gohmanaf752342009-07-07 17:06:11 +00002552 const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0);
Chris Lattnerd934c702004-04-02 20:23:17 +00002553 if (OtherMul->getNumOperands() != 2) {
Dan Gohmance973df2009-06-24 04:48:43 +00002554 SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(),
Dan Gohman797a1db2010-08-16 16:57:24 +00002555 OtherMul->op_begin()+OMulOp);
2556 MulOps.append(OtherMul->op_begin()+OMulOp+1, OtherMul->op_end());
Max Kazantsevdc803662017-06-15 11:48:21 +00002557 InnerMul2 = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1);
Chris Lattnerd934c702004-04-02 20:23:17 +00002558 }
Daniil Fukalov6378bdb2017-02-06 12:38:06 +00002559 SmallVector<const SCEV *, 2> TwoOps = {InnerMul1, InnerMul2};
2560 const SCEV *InnerMulSum =
2561 getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1);
Max Kazantsevdc803662017-06-15 11:48:21 +00002562 const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum,
2563 SCEV::FlagAnyWrap, Depth + 1);
Chris Lattnerd934c702004-04-02 20:23:17 +00002564 if (Ops.size() == 2) return OuterMul;
Dan Gohmanaabfc522010-08-31 22:50:31 +00002565 Ops.erase(Ops.begin()+Idx);
2566 Ops.erase(Ops.begin()+OtherMulIdx-1);
2567 Ops.push_back(OuterMul);
Daniil Fukalov6378bdb2017-02-06 12:38:06 +00002568 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
Chris Lattnerd934c702004-04-02 20:23:17 +00002569 }
2570 }
2571 }
2572 }
2573
2574 // If there are any add recurrences in the operands list, see if any other
2575 // added values are loop invariant. If so, we can fold them into the
2576 // recurrence.
2577 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
2578 ++Idx;
2579
2580 // Scan over all recurrences, trying to fold loop invariants into them.
2581 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
2582 // Scan all of the other operands to this add and add them to the vector if
2583 // they are loop invariant w.r.t. the recurrence.
Dan Gohmanaf752342009-07-07 17:06:11 +00002584 SmallVector<const SCEV *, 8> LIOps;
Dan Gohman48f82222009-05-04 22:30:44 +00002585 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
Dan Gohmanebbd05f2010-04-12 23:08:18 +00002586 const Loop *AddRecLoop = AddRec->getLoop();
Chris Lattnerd934c702004-04-02 20:23:17 +00002587 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
Max Kazantsevd8fe3eb2017-05-30 10:54:58 +00002588 if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) {
Chris Lattnerd934c702004-04-02 20:23:17 +00002589 LIOps.push_back(Ops[i]);
2590 Ops.erase(Ops.begin()+i);
2591 --i; --e;
2592 }
2593
2594 // If we found some loop invariants, fold them into the recurrence.
2595 if (!LIOps.empty()) {
Dan Gohman81313fd2008-09-14 17:21:12 +00002596 // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step}
Chris Lattnerd934c702004-04-02 20:23:17 +00002597 LIOps.push_back(AddRec->getStart());
2598
Dan Gohmanaf752342009-07-07 17:06:11 +00002599 SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(),
Dan Gohman7a2dab82009-12-18 03:57:04 +00002600 AddRec->op_end());
Oleg Ranevskyyeb4ecca2016-05-25 13:01:33 +00002601 // This follows from the fact that the no-wrap flags on the outer add
2602 // expression are applicable on the 0th iteration, when the add recurrence
2603 // will be equal to its start value.
Daniil Fukalov6378bdb2017-02-06 12:38:06 +00002604 AddRecOps[0] = getAddExpr(LIOps, Flags, Depth + 1);
Chris Lattnerd934c702004-04-02 20:23:17 +00002605
Dan Gohman16206132010-06-30 07:16:37 +00002606 // Build the new addrec. Propagate the NUW and NSW flags if both the
Eric Christopher23bf3ba2011-01-11 09:02:09 +00002607 // outer add and the inner addrec are guaranteed to have no overflow.
Andrew Trickf6b01ff2011-03-15 00:37:00 +00002608 // Always propagate NW.
2609 Flags = AddRec->getNoWrapFlags(setFlags(Flags, SCEV::FlagNW));
Andrew Trick8b55b732011-03-14 16:50:06 +00002610 const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRecLoop, Flags);
Dan Gohman51f13052009-12-18 18:45:31 +00002611
Chris Lattnerd934c702004-04-02 20:23:17 +00002612 // If all of the other operands were loop invariant, we are done.
2613 if (Ops.size() == 1) return NewRec;
2614
Nick Lewyckydb66b822011-09-06 05:08:09 +00002615 // Otherwise, add the folded AddRec by the non-invariant parts.
Chris Lattnerd934c702004-04-02 20:23:17 +00002616 for (unsigned i = 0;; ++i)
2617 if (Ops[i] == AddRec) {
2618 Ops[i] = NewRec;
2619 break;
2620 }
Daniil Fukalov6378bdb2017-02-06 12:38:06 +00002621 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
Chris Lattnerd934c702004-04-02 20:23:17 +00002622 }
2623
2624 // Okay, if there weren't any loop invariants to be folded, check to see if
2625 // there are multiple AddRec's with the same loop induction variable being
2626 // added together. If so, we can fold them.
2627 for (unsigned OtherIdx = Idx+1;
Dan Gohmanc866bf42010-08-27 20:45:56 +00002628 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
Max Kazantsevb09b5db2017-05-16 07:27:06 +00002629 ++OtherIdx) {
2630 // We expect the AddRecExpr's to be sorted in reverse dominance order,
2631 // so that the 1st found AddRecExpr is dominated by all others.
2632 assert(DT.dominates(
2633 cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()->getHeader(),
2634 AddRec->getLoop()->getHeader()) &&
2635 "AddRecExprs are not sorted in reverse dominance order?");
Dan Gohmanc866bf42010-08-27 20:45:56 +00002636 if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) {
2637 // Other + {A,+,B}<L> + {C,+,D}<L> --> Other + {A+C,+,B+D}<L>
2638 SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(),
2639 AddRec->op_end());
2640 for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
Max Kazantsevb67d3442017-05-17 03:58:42 +00002641 ++OtherIdx) {
2642 const auto *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
2643 if (OtherAddRec->getLoop() == AddRecLoop) {
2644 for (unsigned i = 0, e = OtherAddRec->getNumOperands();
2645 i != e; ++i) {
2646 if (i >= AddRecOps.size()) {
2647 AddRecOps.append(OtherAddRec->op_begin()+i,
2648 OtherAddRec->op_end());
2649 break;
Dan Gohmanc866bf42010-08-27 20:45:56 +00002650 }
Max Kazantsevb67d3442017-05-17 03:58:42 +00002651 SmallVector<const SCEV *, 2> TwoOps = {
2652 AddRecOps[i], OtherAddRec->getOperand(i)};
2653 AddRecOps[i] = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1);
Chris Lattnerd934c702004-04-02 20:23:17 +00002654 }
Max Kazantsevb67d3442017-05-17 03:58:42 +00002655 Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
2656 }
2657 }
Andrew Trick8b55b732011-03-14 16:50:06 +00002658 // Step size has changed, so we cannot guarantee no self-wraparound.
2659 Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop, SCEV::FlagAnyWrap);
Daniil Fukalov6378bdb2017-02-06 12:38:06 +00002660 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
Chris Lattnerd934c702004-04-02 20:23:17 +00002661 }
Max Kazantsevb09b5db2017-05-16 07:27:06 +00002662 }
Chris Lattnerd934c702004-04-02 20:23:17 +00002663
2664 // Otherwise couldn't fold anything into this recurrence. Move onto the
2665 // next one.
2666 }
2667
2668 // Okay, it looks like we really DO need an add expr. Check to see if we
2669 // already have one, otherwise create a new one.
Daniil Fukalov6378bdb2017-02-06 12:38:06 +00002670 return getOrCreateAddExpr(Ops, Flags);
2671}
2672
2673const SCEV *
2674ScalarEvolution::getOrCreateAddExpr(SmallVectorImpl<const SCEV *> &Ops,
2675 SCEV::NoWrapFlags Flags) {
Dan Gohmanc5c85c02009-06-27 21:21:31 +00002676 FoldingSetNodeID ID;
2677 ID.AddInteger(scAddExpr);
Javed Absarda30c302017-11-16 13:49:27 +00002678 for (const SCEV *Op : Ops)
2679 ID.AddPointer(Op);
Craig Topper9f008862014-04-15 04:59:12 +00002680 void *IP = nullptr;
Dan Gohman51ad99d2010-01-21 02:09:26 +00002681 SCEVAddExpr *S =
Daniil Fukalov6378bdb2017-02-06 12:38:06 +00002682 static_cast<SCEVAddExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
Dan Gohman51ad99d2010-01-21 02:09:26 +00002683 if (!S) {
Dan Gohman00524492010-03-18 01:17:13 +00002684 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
2685 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
Daniil Fukalov6378bdb2017-02-06 12:38:06 +00002686 S = new (SCEVAllocator)
2687 SCEVAddExpr(ID.Intern(SCEVAllocator), O, Ops.size());
Dan Gohman51ad99d2010-01-21 02:09:26 +00002688 UniqueSCEVs.InsertNode(S, IP);
Sanjoy Dase6b995f2017-10-13 05:50:52 +00002689 addToLoopUseLists(S);
Dan Gohman51ad99d2010-01-21 02:09:26 +00002690 }
Andrew Trick8b55b732011-03-14 16:50:06 +00002691 S->setNoWrapFlags(Flags);
Dan Gohmanc5c85c02009-06-27 21:21:31 +00002692 return S;
Chris Lattnerd934c702004-04-02 20:23:17 +00002693}
2694
Max Kazantsevdc803662017-06-15 11:48:21 +00002695const SCEV *
2696ScalarEvolution::getOrCreateMulExpr(SmallVectorImpl<const SCEV *> &Ops,
2697 SCEV::NoWrapFlags Flags) {
2698 FoldingSetNodeID ID;
2699 ID.AddInteger(scMulExpr);
2700 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2701 ID.AddPointer(Ops[i]);
2702 void *IP = nullptr;
2703 SCEVMulExpr *S =
2704 static_cast<SCEVMulExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2705 if (!S) {
2706 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
2707 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
2708 S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator),
2709 O, Ops.size());
2710 UniqueSCEVs.InsertNode(S, IP);
Sanjoy Dase6b995f2017-10-13 05:50:52 +00002711 addToLoopUseLists(S);
Max Kazantsevdc803662017-06-15 11:48:21 +00002712 }
2713 S->setNoWrapFlags(Flags);
2714 return S;
2715}
2716
Nick Lewycky287682e2011-10-04 06:51:26 +00002717static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow) {
2718 uint64_t k = i*j;
2719 if (j > 1 && k / j != i) Overflow = true;
2720 return k;
2721}
2722
2723/// Compute the result of "n choose k", the binomial coefficient. If an
2724/// intermediate computation overflows, Overflow will be set and the return will
Benjamin Kramerbde91762012-06-02 10:20:22 +00002725/// be garbage. Overflow is not cleared on absence of overflow.
Nick Lewycky287682e2011-10-04 06:51:26 +00002726static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow) {
2727 // We use the multiplicative formula:
2728 // n(n-1)(n-2)...(n-(k-1)) / k(k-1)(k-2)...1 .
2729 // At each iteration, we take the n-th term of the numeral and divide by the
2730 // (k-n)th term of the denominator. This division will always produce an
2731 // integral result, and helps reduce the chance of overflow in the
2732 // intermediate computations. However, we can still overflow even when the
2733 // final result would fit.
2734
2735 if (n == 0 || n == k) return 1;
2736 if (k > n) return 0;
2737
2738 if (k > n/2)
2739 k = n-k;
2740
2741 uint64_t r = 1;
2742 for (uint64_t i = 1; i <= k; ++i) {
2743 r = umul_ov(r, n-(i-1), Overflow);
2744 r /= i;
2745 }
2746 return r;
2747}
2748
Nick Lewycky05044c22014-12-06 00:45:50 +00002749/// Determine if any of the operands in this SCEV are a constant or if
2750/// any of the add or multiply expressions in this SCEV contain a constant.
Max Kazantsevfa496952017-07-28 06:42:15 +00002751static bool containsConstantInAddMulChain(const SCEV *StartExpr) {
2752 struct FindConstantInAddMulChain {
2753 bool FoundConstant = false;
Nick Lewycky05044c22014-12-06 00:45:50 +00002754
Max Kazantsevfa496952017-07-28 06:42:15 +00002755 bool follow(const SCEV *S) {
2756 FoundConstant |= isa<SCEVConstant>(S);
2757 return isa<SCEVAddExpr>(S) || isa<SCEVMulExpr>(S);
Nick Lewycky05044c22014-12-06 00:45:50 +00002758 }
Eugene Zelenkobe709f22017-08-18 23:51:26 +00002759
Max Kazantsevfa496952017-07-28 06:42:15 +00002760 bool isDone() const {
2761 return FoundConstant;
2762 }
2763 };
2764
2765 FindConstantInAddMulChain F;
2766 SCEVTraversal<FindConstantInAddMulChain> ST(F);
2767 ST.visitAll(StartExpr);
2768 return F.FoundConstant;
Nick Lewycky05044c22014-12-06 00:45:50 +00002769}
2770
Sanjoy Dasf8570812016-05-29 00:38:22 +00002771/// Get a canonical multiply expression, or something simpler if possible.
Dan Gohman816fe0a2009-10-09 00:10:36 +00002772const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
Max Kazantsevdc803662017-06-15 11:48:21 +00002773 SCEV::NoWrapFlags Flags,
2774 unsigned Depth) {
Andrew Trick8b55b732011-03-14 16:50:06 +00002775 assert(Flags == maskFlags(Flags, SCEV::FlagNUW | SCEV::FlagNSW) &&
2776 "only nuw or nsw allowed");
Chris Lattnerd934c702004-04-02 20:23:17 +00002777 assert(!Ops.empty() && "Cannot get empty mul!");
Dan Gohman51ad99d2010-01-21 02:09:26 +00002778 if (Ops.size() == 1) return Ops[0];
Dan Gohmand33f36e2009-05-18 15:44:58 +00002779#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +00002780 Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
Dan Gohmand33f36e2009-05-18 15:44:58 +00002781 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
Dan Gohmanb6c773e2010-08-16 16:13:54 +00002782 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
Dan Gohmand33f36e2009-05-18 15:44:58 +00002783 "SCEVMulExpr operand types don't match!");
2784#endif
Chris Lattnerd934c702004-04-02 20:23:17 +00002785
2786 // Sort by complexity, this groups all similar expression types together.
Max Kazantsevb09b5db2017-05-16 07:27:06 +00002787 GroupByComplexity(Ops, &LI, DT);
Chris Lattnerd934c702004-04-02 20:23:17 +00002788
Sanjoy Das64895612015-10-09 02:44:45 +00002789 Flags = StrengthenNoWrapFlags(this, scMulExpr, Ops, Flags);
2790
Max Kazantsevdc803662017-06-15 11:48:21 +00002791 // Limit recursion calls depth.
2792 if (Depth > MaxArithDepth)
2793 return getOrCreateMulExpr(Ops, Flags);
2794
Chris Lattnerd934c702004-04-02 20:23:17 +00002795 // If there are any constants, fold them together.
2796 unsigned Idx = 0;
Dan Gohmana30370b2009-05-04 22:02:23 +00002797 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Chris Lattnerd934c702004-04-02 20:23:17 +00002798
2799 // C1*(C2+V) -> C1*C2 + C1*V
2800 if (Ops.size() == 2)
Nick Lewycky05044c22014-12-06 00:45:50 +00002801 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
2802 // If any of Add's ops are Adds or Muls with a constant,
2803 // apply this transformation as well.
2804 if (Add->getNumOperands() == 2)
Max Kazantsevfa496952017-07-28 06:42:15 +00002805 // TODO: There are some cases where this transformation is not
2806 // profitable, for example:
2807 // Add = (C0 + X) * Y + Z.
2808 // Maybe the scope of this transformation should be narrowed down.
2809 if (containsConstantInAddMulChain(Add))
Max Kazantsevdc803662017-06-15 11:48:21 +00002810 return getAddExpr(getMulExpr(LHSC, Add->getOperand(0),
2811 SCEV::FlagAnyWrap, Depth + 1),
2812 getMulExpr(LHSC, Add->getOperand(1),
2813 SCEV::FlagAnyWrap, Depth + 1),
2814 SCEV::FlagAnyWrap, Depth + 1);
Chris Lattnerd934c702004-04-02 20:23:17 +00002815
Chris Lattnerd934c702004-04-02 20:23:17 +00002816 ++Idx;
Dan Gohmana30370b2009-05-04 22:02:23 +00002817 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Chris Lattnerd934c702004-04-02 20:23:17 +00002818 // We found two constants, fold them together!
Sanjoy Das0de2fec2015-12-17 20:28:46 +00002819 ConstantInt *Fold =
2820 ConstantInt::get(getContext(), LHSC->getAPInt() * RHSC->getAPInt());
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00002821 Ops[0] = getConstant(Fold);
2822 Ops.erase(Ops.begin()+1); // Erase the folded element
2823 if (Ops.size() == 1) return Ops[0];
2824 LHSC = cast<SCEVConstant>(Ops[0]);
Chris Lattnerd934c702004-04-02 20:23:17 +00002825 }
2826
2827 // If we are left with a constant one being multiplied, strip it off.
Craig Topperca2c8762017-07-06 18:39:49 +00002828 if (cast<SCEVConstant>(Ops[0])->getValue()->isOne()) {
Chris Lattnerd934c702004-04-02 20:23:17 +00002829 Ops.erase(Ops.begin());
2830 --Idx;
Reid Spencer2e54a152007-03-02 00:28:52 +00002831 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
Chris Lattnerd934c702004-04-02 20:23:17 +00002832 // If we have a multiply of zero, it will always be zero.
2833 return Ops[0];
Dan Gohman51ad99d2010-01-21 02:09:26 +00002834 } else if (Ops[0]->isAllOnesValue()) {
2835 // If we have a mul by -1 of an add, try distributing the -1 among the
2836 // add operands.
Andrew Trick8b55b732011-03-14 16:50:06 +00002837 if (Ops.size() == 2) {
Dan Gohman51ad99d2010-01-21 02:09:26 +00002838 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) {
2839 SmallVector<const SCEV *, 4> NewOps;
2840 bool AnyFolded = false;
Sanjoy Dasd87e4352015-12-08 22:53:36 +00002841 for (const SCEV *AddOp : Add->operands()) {
Max Kazantsevdc803662017-06-15 11:48:21 +00002842 const SCEV *Mul = getMulExpr(Ops[0], AddOp, SCEV::FlagAnyWrap,
2843 Depth + 1);
Dan Gohman51ad99d2010-01-21 02:09:26 +00002844 if (!isa<SCEVMulExpr>(Mul)) AnyFolded = true;
2845 NewOps.push_back(Mul);
2846 }
2847 if (AnyFolded)
Max Kazantsevdc803662017-06-15 11:48:21 +00002848 return getAddExpr(NewOps, SCEV::FlagAnyWrap, Depth + 1);
Sanjoy Das63914592015-10-18 00:29:20 +00002849 } else if (const auto *AddRec = dyn_cast<SCEVAddRecExpr>(Ops[1])) {
Andrew Tricke92dcce2011-03-14 17:38:54 +00002850 // Negation preserves a recurrence's no self-wrap property.
2851 SmallVector<const SCEV *, 4> Operands;
Sanjoy Dasd87e4352015-12-08 22:53:36 +00002852 for (const SCEV *AddRecOp : AddRec->operands())
Max Kazantsevdc803662017-06-15 11:48:21 +00002853 Operands.push_back(getMulExpr(Ops[0], AddRecOp, SCEV::FlagAnyWrap,
2854 Depth + 1));
Sanjoy Dasd87e4352015-12-08 22:53:36 +00002855
Andrew Tricke92dcce2011-03-14 17:38:54 +00002856 return getAddRecExpr(Operands, AddRec->getLoop(),
2857 AddRec->getNoWrapFlags(SCEV::FlagNW));
2858 }
Andrew Trick8b55b732011-03-14 16:50:06 +00002859 }
Chris Lattnerd934c702004-04-02 20:23:17 +00002860 }
Dan Gohmanfe4b2912010-04-13 16:49:23 +00002861
2862 if (Ops.size() == 1)
2863 return Ops[0];
Chris Lattnerd934c702004-04-02 20:23:17 +00002864 }
2865
2866 // Skip over the add expression until we get to a multiply.
2867 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
2868 ++Idx;
2869
Chris Lattnerd934c702004-04-02 20:23:17 +00002870 // If there are mul operands inline them all into this expression.
2871 if (Idx < Ops.size()) {
2872 bool DeletedMul = false;
Dan Gohmana30370b2009-05-04 22:02:23 +00002873 while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
Li Huangfcfe8cd2016-10-20 21:38:39 +00002874 if (Ops.size() > MulOpsInlineThreshold)
2875 break;
Max Kazantsevdc803662017-06-15 11:48:21 +00002876 // If we have an mul, expand the mul operands onto the end of the
2877 // operands list.
Chris Lattnerd934c702004-04-02 20:23:17 +00002878 Ops.erase(Ops.begin()+Idx);
Dan Gohmandd41bba2010-06-21 19:47:52 +00002879 Ops.append(Mul->op_begin(), Mul->op_end());
Chris Lattnerd934c702004-04-02 20:23:17 +00002880 DeletedMul = true;
2881 }
2882
Max Kazantsevdc803662017-06-15 11:48:21 +00002883 // If we deleted at least one mul, we added operands to the end of the
2884 // list, and they are not necessarily sorted. Recurse to resort and
2885 // resimplify any operands we just acquired.
Chris Lattnerd934c702004-04-02 20:23:17 +00002886 if (DeletedMul)
Max Kazantsevdc803662017-06-15 11:48:21 +00002887 return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
Chris Lattnerd934c702004-04-02 20:23:17 +00002888 }
2889
2890 // If there are any add recurrences in the operands list, see if any other
2891 // added values are loop invariant. If so, we can fold them into the
2892 // recurrence.
2893 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
2894 ++Idx;
2895
2896 // Scan over all recurrences, trying to fold loop invariants into them.
2897 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
Max Kazantsevdc803662017-06-15 11:48:21 +00002898 // Scan all of the other operands to this mul and add them to the vector
2899 // if they are loop invariant w.r.t. the recurrence.
Dan Gohmanaf752342009-07-07 17:06:11 +00002900 SmallVector<const SCEV *, 8> LIOps;
Dan Gohman48f82222009-05-04 22:30:44 +00002901 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
Dan Gohman0f2de012010-08-29 14:55:19 +00002902 const Loop *AddRecLoop = AddRec->getLoop();
Chris Lattnerd934c702004-04-02 20:23:17 +00002903 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
Max Kazantsevd8fe3eb2017-05-30 10:54:58 +00002904 if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) {
Chris Lattnerd934c702004-04-02 20:23:17 +00002905 LIOps.push_back(Ops[i]);
2906 Ops.erase(Ops.begin()+i);
2907 --i; --e;
2908 }
2909
2910 // If we found some loop invariants, fold them into the recurrence.
2911 if (!LIOps.empty()) {
Dan Gohman81313fd2008-09-14 17:21:12 +00002912 // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step}
Dan Gohmanaf752342009-07-07 17:06:11 +00002913 SmallVector<const SCEV *, 4> NewOps;
Chris Lattnerd934c702004-04-02 20:23:17 +00002914 NewOps.reserve(AddRec->getNumOperands());
Max Kazantsevdc803662017-06-15 11:48:21 +00002915 const SCEV *Scale = getMulExpr(LIOps, SCEV::FlagAnyWrap, Depth + 1);
Dan Gohman8f5954f2010-06-17 23:34:09 +00002916 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
Max Kazantsevdc803662017-06-15 11:48:21 +00002917 NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i),
2918 SCEV::FlagAnyWrap, Depth + 1));
Chris Lattnerd934c702004-04-02 20:23:17 +00002919
Dan Gohman16206132010-06-30 07:16:37 +00002920 // Build the new addrec. Propagate the NUW and NSW flags if both the
2921 // outer mul and the inner addrec are guaranteed to have no overflow.
Andrew Trick8b55b732011-03-14 16:50:06 +00002922 //
2923 // No self-wrap cannot be guaranteed after changing the step size, but
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00002924 // will be inferred if either NUW or NSW is true.
Andrew Trick8b55b732011-03-14 16:50:06 +00002925 Flags = AddRec->getNoWrapFlags(clearFlags(Flags, SCEV::FlagNW));
2926 const SCEV *NewRec = getAddRecExpr(NewOps, AddRecLoop, Flags);
Chris Lattnerd934c702004-04-02 20:23:17 +00002927
2928 // If all of the other operands were loop invariant, we are done.
2929 if (Ops.size() == 1) return NewRec;
2930
Nick Lewyckydb66b822011-09-06 05:08:09 +00002931 // Otherwise, multiply the folded AddRec by the non-invariant parts.
Chris Lattnerd934c702004-04-02 20:23:17 +00002932 for (unsigned i = 0;; ++i)
2933 if (Ops[i] == AddRec) {
2934 Ops[i] = NewRec;
2935 break;
2936 }
Max Kazantsevdc803662017-06-15 11:48:21 +00002937 return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
Chris Lattnerd934c702004-04-02 20:23:17 +00002938 }
2939
Max Kazantsevdc803662017-06-15 11:48:21 +00002940 // Okay, if there weren't any loop invariants to be folded, check to see
2941 // if there are multiple AddRec's with the same loop induction variable
2942 // being multiplied together. If so, we can fold them.
Nick Lewycky97756402014-09-01 05:17:15 +00002943
2944 // {A1,+,A2,+,...,+,An}<L> * {B1,+,B2,+,...,+,Bn}<L>
2945 // = {x=1 in [ sum y=x..2x [ sum z=max(y-x, y-n)..min(x,n) [
2946 // choose(x, 2x)*choose(2x-y, x-z)*A_{y-z}*B_z
2947 // ]]],+,...up to x=2n}.
2948 // Note that the arguments to choose() are always integers with values
2949 // known at compile time, never SCEV objects.
2950 //
2951 // The implementation avoids pointless extra computations when the two
2952 // addrec's are of different length (mathematically, it's equivalent to
2953 // an infinite stream of zeros on the right).
2954 bool OpsModified = false;
Chris Lattnerd934c702004-04-02 20:23:17 +00002955 for (unsigned OtherIdx = Idx+1;
Nick Lewycky97756402014-09-01 05:17:15 +00002956 OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
Nick Lewyckye0aa54b2011-09-06 21:42:18 +00002957 ++OtherIdx) {
Nick Lewycky97756402014-09-01 05:17:15 +00002958 const SCEVAddRecExpr *OtherAddRec =
2959 dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx]);
2960 if (!OtherAddRec || OtherAddRec->getLoop() != AddRecLoop)
Andrew Trick946f76b2012-05-30 03:35:17 +00002961 continue;
2962
Max Kazantsev0e9e0792017-07-23 15:40:19 +00002963 // Limit max number of arguments to avoid creation of unreasonably big
2964 // SCEVAddRecs with very complex operands.
2965 if (AddRec->getNumOperands() + OtherAddRec->getNumOperands() - 1 >
2966 MaxAddRecSize)
2967 continue;
2968
Nick Lewycky97756402014-09-01 05:17:15 +00002969 bool Overflow = false;
2970 Type *Ty = AddRec->getType();
2971 bool LargerThan64Bits = getTypeSizeInBits(Ty) > 64;
2972 SmallVector<const SCEV*, 7> AddRecOps;
2973 for (int x = 0, xe = AddRec->getNumOperands() +
2974 OtherAddRec->getNumOperands() - 1; x != xe && !Overflow; ++x) {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00002975 const SCEV *Term = getZero(Ty);
Nick Lewycky97756402014-09-01 05:17:15 +00002976 for (int y = x, ye = 2*x+1; y != ye && !Overflow; ++y) {
2977 uint64_t Coeff1 = Choose(x, 2*x - y, Overflow);
2978 for (int z = std::max(y-x, y-(int)AddRec->getNumOperands()+1),
2979 ze = std::min(x+1, (int)OtherAddRec->getNumOperands());
2980 z < ze && !Overflow; ++z) {
2981 uint64_t Coeff2 = Choose(2*x - y, x-z, Overflow);
2982 uint64_t Coeff;
2983 if (LargerThan64Bits)
2984 Coeff = umul_ov(Coeff1, Coeff2, Overflow);
2985 else
2986 Coeff = Coeff1*Coeff2;
2987 const SCEV *CoeffTerm = getConstant(Ty, Coeff);
2988 const SCEV *Term1 = AddRec->getOperand(y-z);
2989 const SCEV *Term2 = OtherAddRec->getOperand(z);
Max Kazantsevdc803662017-06-15 11:48:21 +00002990 Term = getAddExpr(Term, getMulExpr(CoeffTerm, Term1, Term2,
2991 SCEV::FlagAnyWrap, Depth + 1),
2992 SCEV::FlagAnyWrap, Depth + 1);
Andrew Trick946f76b2012-05-30 03:35:17 +00002993 }
Andrew Trick946f76b2012-05-30 03:35:17 +00002994 }
Nick Lewycky97756402014-09-01 05:17:15 +00002995 AddRecOps.push_back(Term);
Chris Lattnerd934c702004-04-02 20:23:17 +00002996 }
Nick Lewycky97756402014-09-01 05:17:15 +00002997 if (!Overflow) {
2998 const SCEV *NewAddRec = getAddRecExpr(AddRecOps, AddRec->getLoop(),
2999 SCEV::FlagAnyWrap);
3000 if (Ops.size() == 2) return NewAddRec;
3001 Ops[Idx] = NewAddRec;
3002 Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
3003 OpsModified = true;
3004 AddRec = dyn_cast<SCEVAddRecExpr>(NewAddRec);
3005 if (!AddRec)
3006 break;
3007 }
Nick Lewyckye0aa54b2011-09-06 21:42:18 +00003008 }
Nick Lewycky97756402014-09-01 05:17:15 +00003009 if (OpsModified)
Max Kazantsevdc803662017-06-15 11:48:21 +00003010 return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
Chris Lattnerd934c702004-04-02 20:23:17 +00003011
3012 // Otherwise couldn't fold anything into this recurrence. Move onto the
3013 // next one.
3014 }
3015
3016 // Okay, it looks like we really DO need an mul expr. Check to see if we
3017 // already have one, otherwise create a new one.
Max Kazantsevdc803662017-06-15 11:48:21 +00003018 return getOrCreateMulExpr(Ops, Flags);
Chris Lattnerd934c702004-04-02 20:23:17 +00003019}
3020
Alexandre Isoard405728f2017-09-01 14:59:59 +00003021/// Represents an unsigned remainder expression based on unsigned division.
3022const SCEV *ScalarEvolution::getURemExpr(const SCEV *LHS,
3023 const SCEV *RHS) {
3024 assert(getEffectiveSCEVType(LHS->getType()) ==
3025 getEffectiveSCEVType(RHS->getType()) &&
3026 "SCEVURemExpr operand types don't match!");
3027
3028 // Short-circuit easy cases
3029 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
3030 // If constant is one, the result is trivial
3031 if (RHSC->getValue()->isOne())
3032 return getZero(LHS->getType()); // X urem 1 --> 0
3033
3034 // If constant is a power of two, fold into a zext(trunc(LHS)).
3035 if (RHSC->getAPInt().isPowerOf2()) {
3036 Type *FullTy = LHS->getType();
3037 Type *TruncTy =
3038 IntegerType::get(getContext(), RHSC->getAPInt().logBase2());
3039 return getZeroExtendExpr(getTruncateExpr(LHS, TruncTy), FullTy);
3040 }
3041 }
3042
3043 // Fallback to %a == %x urem %y == %x -<nuw> ((%x udiv %y) *<nuw> %y)
3044 const SCEV *UDiv = getUDivExpr(LHS, RHS);
3045 const SCEV *Mult = getMulExpr(UDiv, RHS, SCEV::FlagNUW);
3046 return getMinusSCEV(LHS, Mult, SCEV::FlagNUW);
3047}
3048
Sanjoy Dasf8570812016-05-29 00:38:22 +00003049/// Get a canonical unsigned division expression, or something simpler if
3050/// possible.
Dan Gohmanabd17092009-06-24 14:49:00 +00003051const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
3052 const SCEV *RHS) {
Dan Gohmand33f36e2009-05-18 15:44:58 +00003053 assert(getEffectiveSCEVType(LHS->getType()) ==
3054 getEffectiveSCEVType(RHS->getType()) &&
3055 "SCEVUDivExpr operand types don't match!");
3056
Dan Gohmana30370b2009-05-04 22:02:23 +00003057 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
Craig Topperca2c8762017-07-06 18:39:49 +00003058 if (RHSC->getValue()->isOne())
Dan Gohman8a8ad7d2009-08-20 16:42:55 +00003059 return LHS; // X udiv 1 --> x
Dan Gohmanacd700a2010-04-22 01:35:11 +00003060 // If the denominator is zero, the result of the udiv is undefined. Don't
3061 // try to analyze it, because the resolution chosen here may differ from
3062 // the resolution chosen in other parts of the compiler.
3063 if (!RHSC->getValue()->isZero()) {
3064 // Determine if the division can be folded into the operands of
3065 // its operands.
3066 // TODO: Generalize this to non-constants by using known-bits information.
Chris Lattner229907c2011-07-18 04:54:35 +00003067 Type *Ty = LHS->getType();
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003068 unsigned LZ = RHSC->getAPInt().countLeadingZeros();
Dan Gohmandb764c62010-08-04 19:52:50 +00003069 unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1;
Dan Gohmanacd700a2010-04-22 01:35:11 +00003070 // For non-power-of-two values, effectively round the value up to the
3071 // nearest power of two.
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003072 if (!RHSC->getAPInt().isPowerOf2())
Dan Gohmanacd700a2010-04-22 01:35:11 +00003073 ++MaxShiftAmt;
Chris Lattner229907c2011-07-18 04:54:35 +00003074 IntegerType *ExtTy =
Dan Gohmanacd700a2010-04-22 01:35:11 +00003075 IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt);
Dan Gohmanacd700a2010-04-22 01:35:11 +00003076 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
3077 if (const SCEVConstant *Step =
Andrew Trick6d45a012011-08-06 07:00:37 +00003078 dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) {
3079 // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003080 const APInt &StepInt = Step->getAPInt();
3081 const APInt &DivInt = RHSC->getAPInt();
Andrew Trick6d45a012011-08-06 07:00:37 +00003082 if (!StepInt.urem(DivInt) &&
Dan Gohmanacd700a2010-04-22 01:35:11 +00003083 getZeroExtendExpr(AR, ExtTy) ==
3084 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
3085 getZeroExtendExpr(Step, ExtTy),
Andrew Trick8b55b732011-03-14 16:50:06 +00003086 AR->getLoop(), SCEV::FlagAnyWrap)) {
Dan Gohmanacd700a2010-04-22 01:35:11 +00003087 SmallVector<const SCEV *, 4> Operands;
Sanjoy Dasd9f6d332015-10-18 00:29:16 +00003088 for (const SCEV *Op : AR->operands())
3089 Operands.push_back(getUDivExpr(Op, RHS));
3090 return getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagNW);
Dan Gohmanc3a3cb42009-05-08 20:18:49 +00003091 }
Andrew Trick6d45a012011-08-06 07:00:37 +00003092 /// Get a canonical UDivExpr for a recurrence.
3093 /// {X,+,N}/C => {Y,+,N}/C where Y=X-(X%N). Safe when C%N=0.
3094 // We can currently only fold X%N if X is constant.
3095 const SCEVConstant *StartC = dyn_cast<SCEVConstant>(AR->getStart());
3096 if (StartC && !DivInt.urem(StepInt) &&
3097 getZeroExtendExpr(AR, ExtTy) ==
3098 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
3099 getZeroExtendExpr(Step, ExtTy),
3100 AR->getLoop(), SCEV::FlagAnyWrap)) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003101 const APInt &StartInt = StartC->getAPInt();
Andrew Trick6d45a012011-08-06 07:00:37 +00003102 const APInt &StartRem = StartInt.urem(StepInt);
3103 if (StartRem != 0)
3104 LHS = getAddRecExpr(getConstant(StartInt - StartRem), Step,
3105 AR->getLoop(), SCEV::FlagNW);
3106 }
3107 }
Dan Gohmanacd700a2010-04-22 01:35:11 +00003108 // (A*B)/C --> A*(B/C) if safe and B/C can be folded.
3109 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
3110 SmallVector<const SCEV *, 4> Operands;
Sanjoy Dasd9f6d332015-10-18 00:29:16 +00003111 for (const SCEV *Op : M->operands())
3112 Operands.push_back(getZeroExtendExpr(Op, ExtTy));
Dan Gohmanacd700a2010-04-22 01:35:11 +00003113 if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands))
3114 // Find an operand that's safely divisible.
3115 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
3116 const SCEV *Op = M->getOperand(i);
3117 const SCEV *Div = getUDivExpr(Op, RHSC);
3118 if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) {
3119 Operands = SmallVector<const SCEV *, 4>(M->op_begin(),
3120 M->op_end());
3121 Operands[i] = Div;
3122 return getMulExpr(Operands);
3123 }
3124 }
Dan Gohmanc3a3cb42009-05-08 20:18:49 +00003125 }
Dan Gohmanacd700a2010-04-22 01:35:11 +00003126 // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded.
Andrew Trick7d1eea82011-04-27 18:17:36 +00003127 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(LHS)) {
Dan Gohmanacd700a2010-04-22 01:35:11 +00003128 SmallVector<const SCEV *, 4> Operands;
Sanjoy Dasd9f6d332015-10-18 00:29:16 +00003129 for (const SCEV *Op : A->operands())
3130 Operands.push_back(getZeroExtendExpr(Op, ExtTy));
Dan Gohmanacd700a2010-04-22 01:35:11 +00003131 if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) {
3132 Operands.clear();
3133 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
3134 const SCEV *Op = getUDivExpr(A->getOperand(i), RHS);
3135 if (isa<SCEVUDivExpr>(Op) ||
3136 getMulExpr(Op, RHS) != A->getOperand(i))
3137 break;
3138 Operands.push_back(Op);
3139 }
3140 if (Operands.size() == A->getNumOperands())
3141 return getAddExpr(Operands);
3142 }
3143 }
Dan Gohmanc3a3cb42009-05-08 20:18:49 +00003144
Dan Gohmanacd700a2010-04-22 01:35:11 +00003145 // Fold if both operands are constant.
3146 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
3147 Constant *LHSCV = LHSC->getValue();
3148 Constant *RHSCV = RHSC->getValue();
3149 return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV,
3150 RHSCV)));
3151 }
Chris Lattnerd934c702004-04-02 20:23:17 +00003152 }
3153 }
3154
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003155 FoldingSetNodeID ID;
3156 ID.AddInteger(scUDivExpr);
3157 ID.AddPointer(LHS);
3158 ID.AddPointer(RHS);
Craig Topper9f008862014-04-15 04:59:12 +00003159 void *IP = nullptr;
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003160 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
Dan Gohman01c65a22010-03-18 18:49:47 +00003161 SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator),
3162 LHS, RHS);
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003163 UniqueSCEVs.InsertNode(S, IP);
Sanjoy Dase6b995f2017-10-13 05:50:52 +00003164 addToLoopUseLists(S);
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003165 return S;
Chris Lattnerd934c702004-04-02 20:23:17 +00003166}
3167
Nick Lewycky31eaca52014-01-27 10:04:03 +00003168static const APInt gcd(const SCEVConstant *C1, const SCEVConstant *C2) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003169 APInt A = C1->getAPInt().abs();
3170 APInt B = C2->getAPInt().abs();
Nick Lewycky31eaca52014-01-27 10:04:03 +00003171 uint32_t ABW = A.getBitWidth();
3172 uint32_t BBW = B.getBitWidth();
3173
3174 if (ABW > BBW)
3175 B = B.zext(ABW);
3176 else if (ABW < BBW)
3177 A = A.zext(BBW);
3178
Craig Topper69f1af22017-05-06 05:22:56 +00003179 return APIntOps::GreatestCommonDivisor(std::move(A), std::move(B));
Nick Lewycky31eaca52014-01-27 10:04:03 +00003180}
3181
Sanjoy Dasf8570812016-05-29 00:38:22 +00003182/// Get a canonical unsigned division expression, or something simpler if
3183/// possible. There is no representation for an exact udiv in SCEV IR, but we
3184/// can attempt to remove factors from the LHS and RHS. We can't do this when
3185/// it's not exact because the udiv may be clearing bits.
Nick Lewycky31eaca52014-01-27 10:04:03 +00003186const SCEV *ScalarEvolution::getUDivExactExpr(const SCEV *LHS,
3187 const SCEV *RHS) {
3188 // TODO: we could try to find factors in all sorts of things, but for now we
3189 // just deal with u/exact (multiply, constant). See SCEVDivision towards the
3190 // end of this file for inspiration.
3191
3192 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(LHS);
Eli Friedmanf1f49c82017-01-18 23:56:42 +00003193 if (!Mul || !Mul->hasNoUnsignedWrap())
Nick Lewycky31eaca52014-01-27 10:04:03 +00003194 return getUDivExpr(LHS, RHS);
3195
3196 if (const SCEVConstant *RHSCst = dyn_cast<SCEVConstant>(RHS)) {
3197 // If the mulexpr multiplies by a constant, then that constant must be the
3198 // first element of the mulexpr.
Sanjoy Das63914592015-10-18 00:29:20 +00003199 if (const auto *LHSCst = dyn_cast<SCEVConstant>(Mul->getOperand(0))) {
Nick Lewycky31eaca52014-01-27 10:04:03 +00003200 if (LHSCst == RHSCst) {
3201 SmallVector<const SCEV *, 2> Operands;
3202 Operands.append(Mul->op_begin() + 1, Mul->op_end());
3203 return getMulExpr(Operands);
3204 }
3205
3206 // We can't just assume that LHSCst divides RHSCst cleanly, it could be
3207 // that there's a factor provided by one of the other terms. We need to
3208 // check.
3209 APInt Factor = gcd(LHSCst, RHSCst);
3210 if (!Factor.isIntN(1)) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003211 LHSCst =
3212 cast<SCEVConstant>(getConstant(LHSCst->getAPInt().udiv(Factor)));
3213 RHSCst =
3214 cast<SCEVConstant>(getConstant(RHSCst->getAPInt().udiv(Factor)));
Nick Lewycky31eaca52014-01-27 10:04:03 +00003215 SmallVector<const SCEV *, 2> Operands;
3216 Operands.push_back(LHSCst);
3217 Operands.append(Mul->op_begin() + 1, Mul->op_end());
3218 LHS = getMulExpr(Operands);
3219 RHS = RHSCst;
Nick Lewycky629199c2014-01-27 10:47:44 +00003220 Mul = dyn_cast<SCEVMulExpr>(LHS);
3221 if (!Mul)
3222 return getUDivExactExpr(LHS, RHS);
Nick Lewycky31eaca52014-01-27 10:04:03 +00003223 }
3224 }
3225 }
3226
3227 for (int i = 0, e = Mul->getNumOperands(); i != e; ++i) {
3228 if (Mul->getOperand(i) == RHS) {
3229 SmallVector<const SCEV *, 2> Operands;
3230 Operands.append(Mul->op_begin(), Mul->op_begin() + i);
3231 Operands.append(Mul->op_begin() + i + 1, Mul->op_end());
3232 return getMulExpr(Operands);
3233 }
3234 }
3235
3236 return getUDivExpr(LHS, RHS);
3237}
Chris Lattnerd934c702004-04-02 20:23:17 +00003238
Sanjoy Dasf8570812016-05-29 00:38:22 +00003239/// Get an add recurrence expression for the specified loop. Simplify the
3240/// expression as much as possible.
Andrew Trick8b55b732011-03-14 16:50:06 +00003241const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, const SCEV *Step,
3242 const Loop *L,
3243 SCEV::NoWrapFlags Flags) {
Dan Gohmanaf752342009-07-07 17:06:11 +00003244 SmallVector<const SCEV *, 4> Operands;
Chris Lattnerd934c702004-04-02 20:23:17 +00003245 Operands.push_back(Start);
Dan Gohmana30370b2009-05-04 22:02:23 +00003246 if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
Chris Lattnerd934c702004-04-02 20:23:17 +00003247 if (StepChrec->getLoop() == L) {
Dan Gohmandd41bba2010-06-21 19:47:52 +00003248 Operands.append(StepChrec->op_begin(), StepChrec->op_end());
Andrew Trickf6b01ff2011-03-15 00:37:00 +00003249 return getAddRecExpr(Operands, L, maskFlags(Flags, SCEV::FlagNW));
Chris Lattnerd934c702004-04-02 20:23:17 +00003250 }
3251
3252 Operands.push_back(Step);
Andrew Trick8b55b732011-03-14 16:50:06 +00003253 return getAddRecExpr(Operands, L, Flags);
Chris Lattnerd934c702004-04-02 20:23:17 +00003254}
3255
Sanjoy Dasf8570812016-05-29 00:38:22 +00003256/// Get an add recurrence expression for the specified loop. Simplify the
3257/// expression as much as possible.
Dan Gohmance973df2009-06-24 04:48:43 +00003258const SCEV *
Dan Gohmanaf752342009-07-07 17:06:11 +00003259ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
Andrew Trick8b55b732011-03-14 16:50:06 +00003260 const Loop *L, SCEV::NoWrapFlags Flags) {
Chris Lattnerd934c702004-04-02 20:23:17 +00003261 if (Operands.size() == 1) return Operands[0];
Dan Gohmand33f36e2009-05-18 15:44:58 +00003262#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +00003263 Type *ETy = getEffectiveSCEVType(Operands[0]->getType());
Dan Gohmand33f36e2009-05-18 15:44:58 +00003264 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
Dan Gohmanb6c773e2010-08-16 16:13:54 +00003265 assert(getEffectiveSCEVType(Operands[i]->getType()) == ETy &&
Dan Gohmand33f36e2009-05-18 15:44:58 +00003266 "SCEVAddRecExpr operand types don't match!");
Dan Gohmand3a32ae2010-11-17 20:48:38 +00003267 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
Dan Gohmanafd6db92010-11-17 21:23:15 +00003268 assert(isLoopInvariant(Operands[i], L) &&
Dan Gohmand3a32ae2010-11-17 20:48:38 +00003269 "SCEVAddRecExpr operand is not loop-invariant!");
Dan Gohmand33f36e2009-05-18 15:44:58 +00003270#endif
Chris Lattnerd934c702004-04-02 20:23:17 +00003271
Dan Gohmanbe928e32008-06-18 16:23:07 +00003272 if (Operands.back()->isZero()) {
3273 Operands.pop_back();
Andrew Trick8b55b732011-03-14 16:50:06 +00003274 return getAddRecExpr(Operands, L, SCEV::FlagAnyWrap); // {X,+,0} --> X
Dan Gohmanbe928e32008-06-18 16:23:07 +00003275 }
Chris Lattnerd934c702004-04-02 20:23:17 +00003276
Dan Gohmancf9c64e2010-02-19 18:49:22 +00003277 // It's tempting to want to call getMaxBackedgeTakenCount count here and
3278 // use that information to infer NUW and NSW flags. However, computing a
3279 // BE count requires calling getAddRecExpr, so we may not yet have a
3280 // meaningful BE count at this point (and if we don't, we'd be stuck
3281 // with a SCEVCouldNotCompute as the cached BE count).
3282
Sanjoy Das81401d42015-01-10 23:41:24 +00003283 Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags);
Dan Gohman51ad99d2010-01-21 02:09:26 +00003284
Dan Gohman223a5d22008-08-08 18:33:12 +00003285 // Canonicalize nested AddRecs in by nesting them in order of loop depth.
Dan Gohmana30370b2009-05-04 22:02:23 +00003286 if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
Dan Gohmancb0efec2009-12-18 01:14:11 +00003287 const Loop *NestedLoop = NestedAR->getLoop();
Chandler Carruth2f1fd162015-08-17 02:08:17 +00003288 if (L->contains(NestedLoop)
3289 ? (L->getLoopDepth() < NestedLoop->getLoopDepth())
3290 : (!NestedLoop->contains(L) &&
3291 DT.dominates(L->getHeader(), NestedLoop->getHeader()))) {
Dan Gohmanaf752342009-07-07 17:06:11 +00003292 SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(),
Dan Gohmancb0efec2009-12-18 01:14:11 +00003293 NestedAR->op_end());
Dan Gohman223a5d22008-08-08 18:33:12 +00003294 Operands[0] = NestedAR->getStart();
Dan Gohmancc030b72009-06-26 22:36:20 +00003295 // AddRecs require their operands be loop-invariant with respect to their
3296 // loops. Don't perform this transformation if it would break this
3297 // requirement.
Sanjoy Das3b827c72015-11-29 23:40:53 +00003298 bool AllInvariant = all_of(
3299 Operands, [&](const SCEV *Op) { return isLoopInvariant(Op, L); });
Sanjoy Dasf07d2a72015-10-18 00:29:23 +00003300
Dan Gohmancc030b72009-06-26 22:36:20 +00003301 if (AllInvariant) {
Andrew Trick8b55b732011-03-14 16:50:06 +00003302 // Create a recurrence for the outer loop with the same step size.
3303 //
Andrew Trick8b55b732011-03-14 16:50:06 +00003304 // The outer recurrence keeps its NW flag but only keeps NUW/NSW if the
3305 // inner recurrence has the same property.
Andrew Trickf6b01ff2011-03-15 00:37:00 +00003306 SCEV::NoWrapFlags OuterFlags =
3307 maskFlags(Flags, SCEV::FlagNW | NestedAR->getNoWrapFlags());
Andrew Trick8b55b732011-03-14 16:50:06 +00003308
3309 NestedOperands[0] = getAddRecExpr(Operands, L, OuterFlags);
Sanjoy Das3b827c72015-11-29 23:40:53 +00003310 AllInvariant = all_of(NestedOperands, [&](const SCEV *Op) {
3311 return isLoopInvariant(Op, NestedLoop);
3312 });
Sanjoy Dasf07d2a72015-10-18 00:29:23 +00003313
Andrew Trick8b55b732011-03-14 16:50:06 +00003314 if (AllInvariant) {
Dan Gohmancc030b72009-06-26 22:36:20 +00003315 // Ok, both add recurrences are valid after the transformation.
Andrew Trick8b55b732011-03-14 16:50:06 +00003316 //
Andrew Trick8b55b732011-03-14 16:50:06 +00003317 // The inner recurrence keeps its NW flag but only keeps NUW/NSW if
3318 // the outer recurrence has the same property.
Andrew Trickf6b01ff2011-03-15 00:37:00 +00003319 SCEV::NoWrapFlags InnerFlags =
3320 maskFlags(NestedAR->getNoWrapFlags(), SCEV::FlagNW | Flags);
Andrew Trick8b55b732011-03-14 16:50:06 +00003321 return getAddRecExpr(NestedOperands, NestedLoop, InnerFlags);
3322 }
Dan Gohmancc030b72009-06-26 22:36:20 +00003323 }
3324 // Reset Operands to its original state.
3325 Operands[0] = NestedAR;
Dan Gohman223a5d22008-08-08 18:33:12 +00003326 }
3327 }
3328
Dan Gohman8d67d2f2010-01-19 22:27:22 +00003329 // Okay, it looks like we really DO need an addrec expr. Check to see if we
3330 // already have one, otherwise create a new one.
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003331 FoldingSetNodeID ID;
3332 ID.AddInteger(scAddRecExpr);
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003333 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
3334 ID.AddPointer(Operands[i]);
3335 ID.AddPointer(L);
Craig Topper9f008862014-04-15 04:59:12 +00003336 void *IP = nullptr;
Dan Gohman51ad99d2010-01-21 02:09:26 +00003337 SCEVAddRecExpr *S =
3338 static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
3339 if (!S) {
Dan Gohman00524492010-03-18 01:17:13 +00003340 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Operands.size());
3341 std::uninitialized_copy(Operands.begin(), Operands.end(), O);
Dan Gohman01c65a22010-03-18 18:49:47 +00003342 S = new (SCEVAllocator) SCEVAddRecExpr(ID.Intern(SCEVAllocator),
3343 O, Operands.size(), L);
Dan Gohman51ad99d2010-01-21 02:09:26 +00003344 UniqueSCEVs.InsertNode(S, IP);
Sanjoy Dase6b995f2017-10-13 05:50:52 +00003345 addToLoopUseLists(S);
Dan Gohman51ad99d2010-01-21 02:09:26 +00003346 }
Andrew Trick8b55b732011-03-14 16:50:06 +00003347 S->setNoWrapFlags(Flags);
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003348 return S;
Chris Lattnerd934c702004-04-02 20:23:17 +00003349}
3350
Jingyue Wu2982d4d2015-05-18 17:03:25 +00003351const SCEV *
Peter Collingbourne8dff0392016-11-13 06:59:50 +00003352ScalarEvolution::getGEPExpr(GEPOperator *GEP,
3353 const SmallVectorImpl<const SCEV *> &IndexExprs) {
3354 const SCEV *BaseExpr = getSCEV(GEP->getPointerOperand());
Jingyue Wu2982d4d2015-05-18 17:03:25 +00003355 // getSCEV(Base)->getType() has the same address space as Base->getType()
3356 // because SCEV::getType() preserves the address space.
3357 Type *IntPtrTy = getEffectiveSCEVType(BaseExpr->getType());
3358 // FIXME(PR23527): Don't blindly transfer the inbounds flag from the GEP
3359 // instruction to its SCEV, because the Instruction may be guarded by control
3360 // flow and the no-overflow bits may not be valid for the expression in any
Jingyue Wu42f1d672015-07-28 18:22:40 +00003361 // context. This can be fixed similarly to how these flags are handled for
3362 // adds.
Peter Collingbourne8dff0392016-11-13 06:59:50 +00003363 SCEV::NoWrapFlags Wrap = GEP->isInBounds() ? SCEV::FlagNSW
3364 : SCEV::FlagAnyWrap;
Jingyue Wu2982d4d2015-05-18 17:03:25 +00003365
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00003366 const SCEV *TotalOffset = getZero(IntPtrTy);
Peter Collingbourne45681582016-12-02 03:05:41 +00003367 // The array size is unimportant. The first thing we do on CurTy is getting
Jingyue Wu2982d4d2015-05-18 17:03:25 +00003368 // its element type.
Peter Collingbourne45681582016-12-02 03:05:41 +00003369 Type *CurTy = ArrayType::get(GEP->getSourceElementType(), 0);
Jingyue Wu2982d4d2015-05-18 17:03:25 +00003370 for (const SCEV *IndexExpr : IndexExprs) {
3371 // Compute the (potentially symbolic) offset in bytes for this index.
3372 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
3373 // For a struct, add the member offset.
3374 ConstantInt *Index = cast<SCEVConstant>(IndexExpr)->getValue();
3375 unsigned FieldNo = Index->getZExtValue();
3376 const SCEV *FieldOffset = getOffsetOfExpr(IntPtrTy, STy, FieldNo);
3377
3378 // Add the field offset to the running total offset.
3379 TotalOffset = getAddExpr(TotalOffset, FieldOffset);
3380
3381 // Update CurTy to the type of the field at Index.
3382 CurTy = STy->getTypeAtIndex(Index);
3383 } else {
3384 // Update CurTy to its element type.
3385 CurTy = cast<SequentialType>(CurTy)->getElementType();
3386 // For an array, add the element offset, explicitly scaled.
3387 const SCEV *ElementSize = getSizeOfExpr(IntPtrTy, CurTy);
3388 // Getelementptr indices are signed.
3389 IndexExpr = getTruncateOrSignExtend(IndexExpr, IntPtrTy);
3390
3391 // Multiply the index by the element size to compute the element offset.
3392 const SCEV *LocalOffset = getMulExpr(IndexExpr, ElementSize, Wrap);
3393
3394 // Add the element offset to the running total offset.
3395 TotalOffset = getAddExpr(TotalOffset, LocalOffset);
3396 }
3397 }
3398
3399 // Add the total offset from all the GEP indices to the base.
3400 return getAddExpr(BaseExpr, TotalOffset, Wrap);
3401}
3402
Dan Gohmanabd17092009-06-24 14:49:00 +00003403const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS,
3404 const SCEV *RHS) {
Benjamin Kramer3bc1edf2016-07-02 11:41:39 +00003405 SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
Nick Lewyckycdb7e542007-11-25 22:41:31 +00003406 return getSMaxExpr(Ops);
3407}
3408
Dan Gohmanaf752342009-07-07 17:06:11 +00003409const SCEV *
3410ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
Nick Lewyckycdb7e542007-11-25 22:41:31 +00003411 assert(!Ops.empty() && "Cannot get empty smax!");
3412 if (Ops.size() == 1) return Ops[0];
Dan Gohmand33f36e2009-05-18 15:44:58 +00003413#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +00003414 Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
Dan Gohmand33f36e2009-05-18 15:44:58 +00003415 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
Dan Gohmanb6c773e2010-08-16 16:13:54 +00003416 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
Dan Gohmand33f36e2009-05-18 15:44:58 +00003417 "SCEVSMaxExpr operand types don't match!");
3418#endif
Nick Lewyckycdb7e542007-11-25 22:41:31 +00003419
3420 // Sort by complexity, this groups all similar expression types together.
Max Kazantsevb09b5db2017-05-16 07:27:06 +00003421 GroupByComplexity(Ops, &LI, DT);
Nick Lewyckycdb7e542007-11-25 22:41:31 +00003422
3423 // If there are any constants, fold them together.
3424 unsigned Idx = 0;
Dan Gohmana30370b2009-05-04 22:02:23 +00003425 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Nick Lewyckycdb7e542007-11-25 22:41:31 +00003426 ++Idx;
3427 assert(Idx < Ops.size());
Dan Gohmana30370b2009-05-04 22:02:23 +00003428 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Nick Lewyckycdb7e542007-11-25 22:41:31 +00003429 // We found two constants, fold them together!
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003430 ConstantInt *Fold = ConstantInt::get(
3431 getContext(), APIntOps::smax(LHSC->getAPInt(), RHSC->getAPInt()));
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003432 Ops[0] = getConstant(Fold);
3433 Ops.erase(Ops.begin()+1); // Erase the folded element
3434 if (Ops.size() == 1) return Ops[0];
3435 LHSC = cast<SCEVConstant>(Ops[0]);
Nick Lewyckycdb7e542007-11-25 22:41:31 +00003436 }
3437
Dan Gohmanf57bdb72009-06-24 14:46:22 +00003438 // If we are left with a constant minimum-int, strip it off.
Nick Lewyckycdb7e542007-11-25 22:41:31 +00003439 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) {
3440 Ops.erase(Ops.begin());
3441 --Idx;
Dan Gohmanf57bdb72009-06-24 14:46:22 +00003442 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) {
3443 // If we have an smax with a constant maximum-int, it will always be
3444 // maximum-int.
3445 return Ops[0];
Nick Lewyckycdb7e542007-11-25 22:41:31 +00003446 }
Nick Lewyckycdb7e542007-11-25 22:41:31 +00003447
Dan Gohmanfe4b2912010-04-13 16:49:23 +00003448 if (Ops.size() == 1) return Ops[0];
3449 }
Nick Lewyckycdb7e542007-11-25 22:41:31 +00003450
3451 // Find the first SMax
3452 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr)
3453 ++Idx;
3454
3455 // Check to see if one of the operands is an SMax. If so, expand its operands
3456 // onto our operand list, and recurse to simplify.
3457 if (Idx < Ops.size()) {
3458 bool DeletedSMax = false;
Dan Gohmana30370b2009-05-04 22:02:23 +00003459 while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) {
Nick Lewyckycdb7e542007-11-25 22:41:31 +00003460 Ops.erase(Ops.begin()+Idx);
Dan Gohmandd41bba2010-06-21 19:47:52 +00003461 Ops.append(SMax->op_begin(), SMax->op_end());
Nick Lewyckycdb7e542007-11-25 22:41:31 +00003462 DeletedSMax = true;
3463 }
3464
3465 if (DeletedSMax)
3466 return getSMaxExpr(Ops);
3467 }
3468
3469 // Okay, check to see if the same value occurs in the operand list twice. If
3470 // so, delete one. Since we sorted the list, these values are required to
3471 // be adjacent.
3472 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
Dan Gohman7ef0dc22010-04-13 16:51:03 +00003473 // X smax Y smax Y --> X smax Y
3474 // X smax Y --> X, if X is always greater than Y
3475 if (Ops[i] == Ops[i+1] ||
3476 isKnownPredicate(ICmpInst::ICMP_SGE, Ops[i], Ops[i+1])) {
3477 Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2);
3478 --i; --e;
3479 } else if (isKnownPredicate(ICmpInst::ICMP_SLE, Ops[i], Ops[i+1])) {
Nick Lewyckycdb7e542007-11-25 22:41:31 +00003480 Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
3481 --i; --e;
3482 }
3483
3484 if (Ops.size() == 1) return Ops[0];
3485
3486 assert(!Ops.empty() && "Reduced smax down to nothing!");
3487
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003488 // Okay, it looks like we really DO need an smax expr. Check to see if we
Nick Lewyckycdb7e542007-11-25 22:41:31 +00003489 // already have one, otherwise create a new one.
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003490 FoldingSetNodeID ID;
3491 ID.AddInteger(scSMaxExpr);
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003492 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
3493 ID.AddPointer(Ops[i]);
Craig Topper9f008862014-04-15 04:59:12 +00003494 void *IP = nullptr;
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003495 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
Dan Gohman00524492010-03-18 01:17:13 +00003496 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
3497 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
Dan Gohman01c65a22010-03-18 18:49:47 +00003498 SCEV *S = new (SCEVAllocator) SCEVSMaxExpr(ID.Intern(SCEVAllocator),
3499 O, Ops.size());
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003500 UniqueSCEVs.InsertNode(S, IP);
Sanjoy Dase6b995f2017-10-13 05:50:52 +00003501 addToLoopUseLists(S);
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003502 return S;
Nick Lewyckycdb7e542007-11-25 22:41:31 +00003503}
3504
Dan Gohmanabd17092009-06-24 14:49:00 +00003505const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS,
3506 const SCEV *RHS) {
Benjamin Kramer3bc1edf2016-07-02 11:41:39 +00003507 SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003508 return getUMaxExpr(Ops);
3509}
3510
Dan Gohmanaf752342009-07-07 17:06:11 +00003511const SCEV *
3512ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003513 assert(!Ops.empty() && "Cannot get empty umax!");
3514 if (Ops.size() == 1) return Ops[0];
Dan Gohmand33f36e2009-05-18 15:44:58 +00003515#ifndef NDEBUG
Chris Lattner229907c2011-07-18 04:54:35 +00003516 Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
Dan Gohmand33f36e2009-05-18 15:44:58 +00003517 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
Dan Gohmanb6c773e2010-08-16 16:13:54 +00003518 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
Dan Gohmand33f36e2009-05-18 15:44:58 +00003519 "SCEVUMaxExpr operand types don't match!");
3520#endif
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003521
3522 // Sort by complexity, this groups all similar expression types together.
Max Kazantsevb09b5db2017-05-16 07:27:06 +00003523 GroupByComplexity(Ops, &LI, DT);
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003524
3525 // If there are any constants, fold them together.
3526 unsigned Idx = 0;
Dan Gohmana30370b2009-05-04 22:02:23 +00003527 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003528 ++Idx;
3529 assert(Idx < Ops.size());
Dan Gohmana30370b2009-05-04 22:02:23 +00003530 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003531 // We found two constants, fold them together!
Sanjoy Das0de2fec2015-12-17 20:28:46 +00003532 ConstantInt *Fold = ConstantInt::get(
3533 getContext(), APIntOps::umax(LHSC->getAPInt(), RHSC->getAPInt()));
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003534 Ops[0] = getConstant(Fold);
3535 Ops.erase(Ops.begin()+1); // Erase the folded element
3536 if (Ops.size() == 1) return Ops[0];
3537 LHSC = cast<SCEVConstant>(Ops[0]);
3538 }
3539
Dan Gohmanf57bdb72009-06-24 14:46:22 +00003540 // If we are left with a constant minimum-int, strip it off.
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003541 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) {
3542 Ops.erase(Ops.begin());
3543 --Idx;
Dan Gohmanf57bdb72009-06-24 14:46:22 +00003544 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) {
3545 // If we have an umax with a constant maximum-int, it will always be
3546 // maximum-int.
3547 return Ops[0];
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003548 }
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003549
Dan Gohmanfe4b2912010-04-13 16:49:23 +00003550 if (Ops.size() == 1) return Ops[0];
3551 }
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003552
3553 // Find the first UMax
3554 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr)
3555 ++Idx;
3556
3557 // Check to see if one of the operands is a UMax. If so, expand its operands
3558 // onto our operand list, and recurse to simplify.
3559 if (Idx < Ops.size()) {
3560 bool DeletedUMax = false;
Dan Gohmana30370b2009-05-04 22:02:23 +00003561 while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) {
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003562 Ops.erase(Ops.begin()+Idx);
Dan Gohmandd41bba2010-06-21 19:47:52 +00003563 Ops.append(UMax->op_begin(), UMax->op_end());
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003564 DeletedUMax = true;
3565 }
3566
3567 if (DeletedUMax)
3568 return getUMaxExpr(Ops);
3569 }
3570
3571 // Okay, check to see if the same value occurs in the operand list twice. If
3572 // so, delete one. Since we sorted the list, these values are required to
3573 // be adjacent.
3574 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
Dan Gohman7ef0dc22010-04-13 16:51:03 +00003575 // X umax Y umax Y --> X umax Y
3576 // X umax Y --> X, if X is always greater than Y
3577 if (Ops[i] == Ops[i+1] ||
3578 isKnownPredicate(ICmpInst::ICMP_UGE, Ops[i], Ops[i+1])) {
3579 Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2);
3580 --i; --e;
3581 } else if (isKnownPredicate(ICmpInst::ICMP_ULE, Ops[i], Ops[i+1])) {
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003582 Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
3583 --i; --e;
3584 }
3585
3586 if (Ops.size() == 1) return Ops[0];
3587
3588 assert(!Ops.empty() && "Reduced umax down to nothing!");
3589
3590 // Okay, it looks like we really DO need a umax expr. Check to see if we
3591 // already have one, otherwise create a new one.
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003592 FoldingSetNodeID ID;
3593 ID.AddInteger(scUMaxExpr);
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003594 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
3595 ID.AddPointer(Ops[i]);
Craig Topper9f008862014-04-15 04:59:12 +00003596 void *IP = nullptr;
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003597 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
Dan Gohman00524492010-03-18 01:17:13 +00003598 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
3599 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
Dan Gohman01c65a22010-03-18 18:49:47 +00003600 SCEV *S = new (SCEVAllocator) SCEVUMaxExpr(ID.Intern(SCEVAllocator),
3601 O, Ops.size());
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003602 UniqueSCEVs.InsertNode(S, IP);
Sanjoy Dase6b995f2017-10-13 05:50:52 +00003603 addToLoopUseLists(S);
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003604 return S;
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00003605}
3606
Dan Gohmanabd17092009-06-24 14:49:00 +00003607const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS,
3608 const SCEV *RHS) {
Dan Gohman692b4682009-06-22 03:18:45 +00003609 // ~smax(~x, ~y) == smin(x, y).
3610 return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
3611}
3612
Dan Gohmanabd17092009-06-24 14:49:00 +00003613const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS,
3614 const SCEV *RHS) {
Dan Gohman692b4682009-06-22 03:18:45 +00003615 // ~umax(~x, ~y) == umin(x, y)
3616 return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
3617}
3618
Matt Arsenaulta90a18e2013-09-10 19:55:24 +00003619const SCEV *ScalarEvolution::getSizeOfExpr(Type *IntTy, Type *AllocTy) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003620 // We can bypass creating a target-independent
Dan Gohman11862a62010-04-12 23:03:26 +00003621 // constant expression and then folding it back into a ConstantInt.
3622 // This is just a compile-time optimization.
Sanjoy Das49edd3b2015-10-27 00:52:09 +00003623 return getConstant(IntTy, getDataLayout().getTypeAllocSize(AllocTy));
Dan Gohmane5e1b7b2010-02-01 18:27:38 +00003624}
3625
Matt Arsenaulta90a18e2013-09-10 19:55:24 +00003626const SCEV *ScalarEvolution::getOffsetOfExpr(Type *IntTy,
3627 StructType *STy,
Dan Gohmane5e1b7b2010-02-01 18:27:38 +00003628 unsigned FieldNo) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003629 // We can bypass creating a target-independent
Dan Gohman11862a62010-04-12 23:03:26 +00003630 // constant expression and then folding it back into a ConstantInt.
3631 // This is just a compile-time optimization.
Mehdi Aminia28d91d2015-03-10 02:37:25 +00003632 return getConstant(
Sanjoy Das49edd3b2015-10-27 00:52:09 +00003633 IntTy, getDataLayout().getStructLayout(STy)->getElementOffset(FieldNo));
Dan Gohmanbf2a9ae2009-08-18 16:46:41 +00003634}
3635
Dan Gohmanaf752342009-07-07 17:06:11 +00003636const SCEV *ScalarEvolution::getUnknown(Value *V) {
Dan Gohmanf436bac2009-06-24 00:54:57 +00003637 // Don't attempt to do anything other than create a SCEVUnknown object
3638 // here. createSCEV only calls getUnknown after checking for all other
3639 // interesting possibilities, and any other code that calls getUnknown
3640 // is doing so in order to hide a value from SCEV canonicalization.
3641
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003642 FoldingSetNodeID ID;
3643 ID.AddInteger(scUnknown);
3644 ID.AddPointer(V);
Craig Topper9f008862014-04-15 04:59:12 +00003645 void *IP = nullptr;
Dan Gohman7cac9572010-08-02 23:49:30 +00003646 if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) {
3647 assert(cast<SCEVUnknown>(S)->getValue() == V &&
3648 "Stale SCEVUnknown in uniquing map!");
3649 return S;
3650 }
3651 SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator), V, this,
3652 FirstUnknown);
3653 FirstUnknown = cast<SCEVUnknown>(S);
Dan Gohmanc5c85c02009-06-27 21:21:31 +00003654 UniqueSCEVs.InsertNode(S, IP);
3655 return S;
Chris Lattnerb4f681b2004-04-15 15:07:24 +00003656}
3657
Chris Lattnerd934c702004-04-02 20:23:17 +00003658//===----------------------------------------------------------------------===//
Chris Lattnerd934c702004-04-02 20:23:17 +00003659// Basic SCEV Analysis and PHI Idiom Recognition Code
3660//
3661
Sanjoy Dasf8570812016-05-29 00:38:22 +00003662/// Test if values of the given type are analyzable within the SCEV
3663/// framework. This primarily includes integer types, and it can optionally
3664/// include pointer types if the ScalarEvolution class has access to
3665/// target-specific information.
Chris Lattner229907c2011-07-18 04:54:35 +00003666bool ScalarEvolution::isSCEVable(Type *Ty) const {
Dan Gohmanbf2a9ae2009-08-18 16:46:41 +00003667 // Integers and pointers are always SCEVable.
Duncan Sands19d0b472010-02-16 11:11:14 +00003668 return Ty->isIntegerTy() || Ty->isPointerTy();
Dan Gohmanb397e1a2009-04-21 01:07:12 +00003669}
3670
Sanjoy Dasf8570812016-05-29 00:38:22 +00003671/// Return the size in bits of the specified type, for which isSCEVable must
3672/// return true.
Chris Lattner229907c2011-07-18 04:54:35 +00003673uint64_t ScalarEvolution::getTypeSizeInBits(Type *Ty) const {
Dan Gohmanb397e1a2009-04-21 01:07:12 +00003674 assert(isSCEVable(Ty) && "Type is not SCEVable!");
Sanjoy Das49edd3b2015-10-27 00:52:09 +00003675 return getDataLayout().getTypeSizeInBits(Ty);
Dan Gohmanb397e1a2009-04-21 01:07:12 +00003676}
3677
Sanjoy Dasf8570812016-05-29 00:38:22 +00003678/// Return a type with the same bitwidth as the given type and which represents
3679/// how SCEV will treat the given type, for which isSCEVable must return
3680/// true. For pointer types, this is the pointer-sized integer type.
Chris Lattner229907c2011-07-18 04:54:35 +00003681Type *ScalarEvolution::getEffectiveSCEVType(Type *Ty) const {
Dan Gohmanb397e1a2009-04-21 01:07:12 +00003682 assert(isSCEVable(Ty) && "Type is not SCEVable!");
3683
Sanjoy Dasd295f2c2015-10-18 00:29:27 +00003684 if (Ty->isIntegerTy())
Dan Gohmanb397e1a2009-04-21 01:07:12 +00003685 return Ty;
3686
Dan Gohmanbf2a9ae2009-08-18 16:46:41 +00003687 // The only other support type is pointer.
Duncan Sands19d0b472010-02-16 11:11:14 +00003688 assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!");
Sanjoy Das49edd3b2015-10-27 00:52:09 +00003689 return getDataLayout().getIntPtrType(Ty);
Dan Gohman0a40ad92009-04-16 03:18:22 +00003690}
Chris Lattnerd934c702004-04-02 20:23:17 +00003691
Max Kazantsev2e44d292017-03-31 12:05:30 +00003692Type *ScalarEvolution::getWiderType(Type *T1, Type *T2) const {
3693 return getTypeSizeInBits(T1) >= getTypeSizeInBits(T2) ? T1 : T2;
3694}
3695
Dan Gohmanaf752342009-07-07 17:06:11 +00003696const SCEV *ScalarEvolution::getCouldNotCompute() {
Chandler Carruth2f1fd162015-08-17 02:08:17 +00003697 return CouldNotCompute.get();
Dan Gohman31efa302009-04-18 17:58:19 +00003698}
3699
Sanjoy Das7d752672015-12-08 04:32:54 +00003700bool ScalarEvolution::checkValidity(const SCEV *S) const {
Sanjoy Das6b46a0d2016-11-09 18:22:43 +00003701 bool ContainsNulls = SCEVExprContains(S, [](const SCEV *S) {
3702 auto *SU = dyn_cast<SCEVUnknown>(S);
3703 return SU && SU->getValue() == nullptr;
3704 });
Shuxin Yangefc4c012013-07-08 17:33:13 +00003705
Sanjoy Das6b46a0d2016-11-09 18:22:43 +00003706 return !ContainsNulls;
Shuxin Yangefc4c012013-07-08 17:33:13 +00003707}
3708
Wei Mia49559b2016-02-04 01:27:38 +00003709bool ScalarEvolution::containsAddRecurrence(const SCEV *S) {
Sanjoy Dasa2602142016-09-27 18:01:46 +00003710 HasRecMapType::iterator I = HasRecMap.find(S);
Wei Mia49559b2016-02-04 01:27:38 +00003711 if (I != HasRecMap.end())
3712 return I->second;
3713
Sanjoy Das0ae390a2016-11-10 06:33:54 +00003714 bool FoundAddRec = SCEVExprContains(S, isa<SCEVAddRecExpr, const SCEV *>);
Sanjoy Das6b46a0d2016-11-09 18:22:43 +00003715 HasRecMap.insert({S, FoundAddRec});
3716 return FoundAddRec;
Wei Mia49559b2016-02-04 01:27:38 +00003717}
3718
Wei Mi785858c2016-08-09 20:37:50 +00003719/// Try to split a SCEVAddExpr into a pair of {SCEV, ConstantInt}.
3720/// If \p S is a SCEVAddExpr and is composed of a sub SCEV S' and an
3721/// offset I, then return {S', I}, else return {\p S, nullptr}.
3722static std::pair<const SCEV *, ConstantInt *> splitAddExpr(const SCEV *S) {
3723 const auto *Add = dyn_cast<SCEVAddExpr>(S);
3724 if (!Add)
3725 return {S, nullptr};
3726
3727 if (Add->getNumOperands() != 2)
3728 return {S, nullptr};
3729
3730 auto *ConstOp = dyn_cast<SCEVConstant>(Add->getOperand(0));
3731 if (!ConstOp)
3732 return {S, nullptr};
3733
3734 return {Add->getOperand(1), ConstOp->getValue()};
3735}
3736
3737/// Return the ValueOffsetPair set for \p S. \p S can be represented
3738/// by the value and offset from any ValueOffsetPair in the set.
3739SetVector<ScalarEvolution::ValueOffsetPair> *
3740ScalarEvolution::getSCEVValues(const SCEV *S) {
Wei Mia49559b2016-02-04 01:27:38 +00003741 ExprValueMapType::iterator SI = ExprValueMap.find_as(S);
3742 if (SI == ExprValueMap.end())
3743 return nullptr;
3744#ifndef NDEBUG
3745 if (VerifySCEVMap) {
3746 // Check there is no dangling Value in the set returned.
3747 for (const auto &VE : SI->second)
Wei Mi785858c2016-08-09 20:37:50 +00003748 assert(ValueExprMap.count(VE.first));
Wei Mia49559b2016-02-04 01:27:38 +00003749 }
3750#endif
3751 return &SI->second;
3752}
3753
Wei Mi785858c2016-08-09 20:37:50 +00003754/// Erase Value from ValueExprMap and ExprValueMap. ValueExprMap.erase(V)
3755/// cannot be used separately. eraseValueFromMap should be used to remove
3756/// V from ValueExprMap and ExprValueMap at the same time.
Wei Mia49559b2016-02-04 01:27:38 +00003757void ScalarEvolution::eraseValueFromMap(Value *V) {
3758 ValueExprMapType::iterator I = ValueExprMap.find_as(V);
3759 if (I != ValueExprMap.end()) {
3760 const SCEV *S = I->second;
Wei Mi785858c2016-08-09 20:37:50 +00003761 // Remove {V, 0} from the set of ExprValueMap[S]
3762 if (SetVector<ValueOffsetPair> *SV = getSCEVValues(S))
3763 SV->remove({V, nullptr});
3764
3765 // Remove {V, Offset} from the set of ExprValueMap[Stripped]
3766 const SCEV *Stripped;
3767 ConstantInt *Offset;
3768 std::tie(Stripped, Offset) = splitAddExpr(S);
3769 if (Offset != nullptr) {
3770 if (SetVector<ValueOffsetPair> *SV = getSCEVValues(Stripped))
3771 SV->remove({V, Offset});
3772 }
Wei Mia49559b2016-02-04 01:27:38 +00003773 ValueExprMap.erase(V);
3774 }
3775}
3776
Sanjoy Dasf8570812016-05-29 00:38:22 +00003777/// Return an existing SCEV if it exists, otherwise analyze the expression and
3778/// create a new one.
Dan Gohmanaf752342009-07-07 17:06:11 +00003779const SCEV *ScalarEvolution::getSCEV(Value *V) {
Dan Gohmanb397e1a2009-04-21 01:07:12 +00003780 assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
Chris Lattnerd934c702004-04-02 20:23:17 +00003781
Jingyue Wu42f1d672015-07-28 18:22:40 +00003782 const SCEV *S = getExistingSCEV(V);
3783 if (S == nullptr) {
3784 S = createSCEV(V);
Wei Mia49559b2016-02-04 01:27:38 +00003785 // During PHI resolution, it is possible to create two SCEVs for the same
3786 // V, so it is needed to double check whether V->S is inserted into
Wei Mi785858c2016-08-09 20:37:50 +00003787 // ValueExprMap before insert S->{V, 0} into ExprValueMap.
Wei Mia49559b2016-02-04 01:27:38 +00003788 std::pair<ValueExprMapType::iterator, bool> Pair =
Sanjoy Dasc42f7cc2016-02-20 01:35:56 +00003789 ValueExprMap.insert({SCEVCallbackVH(V, this), S});
Wei Mi785858c2016-08-09 20:37:50 +00003790 if (Pair.second) {
3791 ExprValueMap[S].insert({V, nullptr});
3792
3793 // If S == Stripped + Offset, add Stripped -> {V, Offset} into
3794 // ExprValueMap.
3795 const SCEV *Stripped = S;
3796 ConstantInt *Offset = nullptr;
3797 std::tie(Stripped, Offset) = splitAddExpr(S);
3798 // If stripped is SCEVUnknown, don't bother to save
3799 // Stripped -> {V, offset}. It doesn't simplify and sometimes even
3800 // increase the complexity of the expansion code.
3801 // If V is GetElementPtrInst, don't save Stripped -> {V, offset}
3802 // because it may generate add/sub instead of GEP in SCEV expansion.
3803 if (Offset != nullptr && !isa<SCEVUnknown>(Stripped) &&
3804 !isa<GetElementPtrInst>(V))
3805 ExprValueMap[Stripped].insert({V, Offset});
3806 }
Jingyue Wu42f1d672015-07-28 18:22:40 +00003807 }
3808 return S;
3809}
3810
3811const SCEV *ScalarEvolution::getExistingSCEV(Value *V) {
3812 assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
3813
Shuxin Yangefc4c012013-07-08 17:33:13 +00003814 ValueExprMapType::iterator I = ValueExprMap.find_as(V);
3815 if (I != ValueExprMap.end()) {
3816 const SCEV *S = I->second;
Shuxin Yang23773b32013-07-12 07:25:38 +00003817 if (checkValidity(S))
Shuxin Yangefc4c012013-07-08 17:33:13 +00003818 return S;
Wei Mi785858c2016-08-09 20:37:50 +00003819 eraseValueFromMap(V);
Wei Mia49559b2016-02-04 01:27:38 +00003820 forgetMemoizedResults(S);
Shuxin Yangefc4c012013-07-08 17:33:13 +00003821 }
Jingyue Wu42f1d672015-07-28 18:22:40 +00003822 return nullptr;
Chris Lattnerd934c702004-04-02 20:23:17 +00003823}
3824
Sanjoy Dasf8570812016-05-29 00:38:22 +00003825/// Return a SCEV corresponding to -V = -1*V
Bjarke Hammersholt Roune9791ed42015-08-14 22:45:26 +00003826const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V,
3827 SCEV::NoWrapFlags Flags) {
Dan Gohmana30370b2009-05-04 22:02:23 +00003828 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
Owen Anderson53a52212009-07-13 04:09:18 +00003829 return getConstant(
Owen Anderson487375e2009-07-29 18:55:55 +00003830 cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue())));
Dan Gohman0a40ad92009-04-16 03:18:22 +00003831
Chris Lattner229907c2011-07-18 04:54:35 +00003832 Type *Ty = V->getType();
Dan Gohmanc8e23622009-04-21 23:15:49 +00003833 Ty = getEffectiveSCEVType(Ty);
Bjarke Hammersholt Roune9791ed42015-08-14 22:45:26 +00003834 return getMulExpr(
3835 V, getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))), Flags);
Dan Gohman0a40ad92009-04-16 03:18:22 +00003836}
3837
Sanjoy Dasf8570812016-05-29 00:38:22 +00003838/// Return a SCEV corresponding to ~V = -1-V
Dan Gohmanaf752342009-07-07 17:06:11 +00003839const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) {
Dan Gohmana30370b2009-05-04 22:02:23 +00003840 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
Owen Anderson542619e2009-07-13 20:58:05 +00003841 return getConstant(
Owen Anderson487375e2009-07-29 18:55:55 +00003842 cast<ConstantInt>(ConstantExpr::getNot(VC->getValue())));
Dan Gohman0a40ad92009-04-16 03:18:22 +00003843
Chris Lattner229907c2011-07-18 04:54:35 +00003844 Type *Ty = V->getType();
Dan Gohmanc8e23622009-04-21 23:15:49 +00003845 Ty = getEffectiveSCEVType(Ty);
Owen Anderson542619e2009-07-13 20:58:05 +00003846 const SCEV *AllOnes =
Owen Anderson5a1acd92009-07-31 20:28:14 +00003847 getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty)));
Dan Gohman0a40ad92009-04-16 03:18:22 +00003848 return getMinusSCEV(AllOnes, V);
3849}
3850
Chris Lattnerfc877522011-01-09 22:26:35 +00003851const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, const SCEV *RHS,
Max Kazantsevdc803662017-06-15 11:48:21 +00003852 SCEV::NoWrapFlags Flags,
3853 unsigned Depth) {
Dan Gohman46f00a22010-07-20 16:53:00 +00003854 // Fast path: X - X --> 0.
3855 if (LHS == RHS)
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00003856 return getZero(LHS->getType());
Dan Gohman46f00a22010-07-20 16:53:00 +00003857
Bjarke Hammersholt Roune9791ed42015-08-14 22:45:26 +00003858 // We represent LHS - RHS as LHS + (-1)*RHS. This transformation
3859 // makes it so that we cannot make much use of NUW.
3860 auto AddFlags = SCEV::FlagAnyWrap;
3861 const bool RHSIsNotMinSigned =
Craig Topper01020392017-06-24 23:34:50 +00003862 !getSignedRangeMin(RHS).isMinSignedValue();
Bjarke Hammersholt Roune9791ed42015-08-14 22:45:26 +00003863 if (maskFlags(Flags, SCEV::FlagNSW) == SCEV::FlagNSW) {
3864 // Let M be the minimum representable signed value. Then (-1)*RHS
3865 // signed-wraps if and only if RHS is M. That can happen even for
3866 // a NSW subtraction because e.g. (-1)*M signed-wraps even though
3867 // -1 - M does not. So to transfer NSW from LHS - RHS to LHS +
3868 // (-1)*RHS, we need to prove that RHS != M.
3869 //
3870 // If LHS is non-negative and we know that LHS - RHS does not
3871 // signed-wrap, then RHS cannot be M. So we can rule out signed-wrap
3872 // either by proving that RHS > M or that LHS >= 0.
3873 if (RHSIsNotMinSigned || isKnownNonNegative(LHS)) {
3874 AddFlags = SCEV::FlagNSW;
3875 }
3876 }
3877
3878 // FIXME: Find a correct way to transfer NSW to (-1)*M when LHS -
3879 // RHS is NSW and LHS >= 0.
3880 //
3881 // The difficulty here is that the NSW flag may have been proven
3882 // relative to a loop that is to be found in a recurrence in LHS and
3883 // not in RHS. Applying NSW to (-1)*M may then let the NSW have a
3884 // larger scope than intended.
3885 auto NegFlags = RHSIsNotMinSigned ? SCEV::FlagNSW : SCEV::FlagAnyWrap;
3886
Max Kazantsevdc803662017-06-15 11:48:21 +00003887 return getAddExpr(LHS, getNegativeSCEV(RHS, NegFlags), AddFlags, Depth);
Dan Gohman0a40ad92009-04-16 03:18:22 +00003888}
3889
Dan Gohmanaf752342009-07-07 17:06:11 +00003890const SCEV *
Chris Lattner229907c2011-07-18 04:54:35 +00003891ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, Type *Ty) {
3892 Type *SrcTy = V->getType();
Duncan Sands19d0b472010-02-16 11:11:14 +00003893 assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
3894 (Ty->isIntegerTy() || Ty->isPointerTy()) &&
Dan Gohman0a40ad92009-04-16 03:18:22 +00003895 "Cannot truncate or zero extend with non-integer arguments!");
Dan Gohmanb397e1a2009-04-21 01:07:12 +00003896 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
Dan Gohman0a40ad92009-04-16 03:18:22 +00003897 return V; // No conversion
Dan Gohmanb397e1a2009-04-21 01:07:12 +00003898 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
Dan Gohmanc8e23622009-04-21 23:15:49 +00003899 return getTruncateExpr(V, Ty);
3900 return getZeroExtendExpr(V, Ty);
Dan Gohman0a40ad92009-04-16 03:18:22 +00003901}
3902
Dan Gohmanaf752342009-07-07 17:06:11 +00003903const SCEV *
3904ScalarEvolution::getTruncateOrSignExtend(const SCEV *V,
Chris Lattner229907c2011-07-18 04:54:35 +00003905 Type *Ty) {
3906 Type *SrcTy = V->getType();
Duncan Sands19d0b472010-02-16 11:11:14 +00003907 assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
3908 (Ty->isIntegerTy() || Ty->isPointerTy()) &&
Dan Gohman0a40ad92009-04-16 03:18:22 +00003909 "Cannot truncate or zero extend with non-integer arguments!");
Dan Gohmanb397e1a2009-04-21 01:07:12 +00003910 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
Dan Gohman0a40ad92009-04-16 03:18:22 +00003911 return V; // No conversion
Dan Gohmanb397e1a2009-04-21 01:07:12 +00003912 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
Dan Gohmanc8e23622009-04-21 23:15:49 +00003913 return getTruncateExpr(V, Ty);
3914 return getSignExtendExpr(V, Ty);
Dan Gohman0a40ad92009-04-16 03:18:22 +00003915}
3916
Dan Gohmanaf752342009-07-07 17:06:11 +00003917const SCEV *
Chris Lattner229907c2011-07-18 04:54:35 +00003918ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, Type *Ty) {
3919 Type *SrcTy = V->getType();
Duncan Sands19d0b472010-02-16 11:11:14 +00003920 assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
3921 (Ty->isIntegerTy() || Ty->isPointerTy()) &&
Dan Gohmane712a2f2009-05-13 03:46:30 +00003922 "Cannot noop or zero extend with non-integer arguments!");
3923 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
3924 "getNoopOrZeroExtend cannot truncate!");
3925 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
3926 return V; // No conversion
3927 return getZeroExtendExpr(V, Ty);
3928}
3929
Dan Gohmanaf752342009-07-07 17:06:11 +00003930const SCEV *
Chris Lattner229907c2011-07-18 04:54:35 +00003931ScalarEvolution::getNoopOrSignExtend(const SCEV *V, Type *Ty) {
3932 Type *SrcTy = V->getType();
Duncan Sands19d0b472010-02-16 11:11:14 +00003933 assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
3934 (Ty->isIntegerTy() || Ty->isPointerTy()) &&
Dan Gohmane712a2f2009-05-13 03:46:30 +00003935 "Cannot noop or sign extend with non-integer arguments!");
3936 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
3937 "getNoopOrSignExtend cannot truncate!");
3938 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
3939 return V; // No conversion
3940 return getSignExtendExpr(V, Ty);
3941}
3942
Dan Gohmanaf752342009-07-07 17:06:11 +00003943const SCEV *
Chris Lattner229907c2011-07-18 04:54:35 +00003944ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, Type *Ty) {
3945 Type *SrcTy = V->getType();
Duncan Sands19d0b472010-02-16 11:11:14 +00003946 assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
3947 (Ty->isIntegerTy() || Ty->isPointerTy()) &&
Dan Gohman8db2edc2009-06-13 15:56:47 +00003948 "Cannot noop or any extend with non-integer arguments!");
3949 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
3950 "getNoopOrAnyExtend cannot truncate!");
3951 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
3952 return V; // No conversion
3953 return getAnyExtendExpr(V, Ty);
3954}
3955
Dan Gohmanaf752342009-07-07 17:06:11 +00003956const SCEV *
Chris Lattner229907c2011-07-18 04:54:35 +00003957ScalarEvolution::getTruncateOrNoop(const SCEV *V, Type *Ty) {
3958 Type *SrcTy = V->getType();
Duncan Sands19d0b472010-02-16 11:11:14 +00003959 assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
3960 (Ty->isIntegerTy() || Ty->isPointerTy()) &&
Dan Gohmane712a2f2009-05-13 03:46:30 +00003961 "Cannot truncate or noop with non-integer arguments!");
3962 assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) &&
3963 "getTruncateOrNoop cannot extend!");
3964 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
3965 return V; // No conversion
3966 return getTruncateExpr(V, Ty);
3967}
3968
Dan Gohmanabd17092009-06-24 14:49:00 +00003969const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS,
3970 const SCEV *RHS) {
Dan Gohmanaf752342009-07-07 17:06:11 +00003971 const SCEV *PromotedLHS = LHS;
3972 const SCEV *PromotedRHS = RHS;
Dan Gohman96212b62009-06-22 00:31:57 +00003973
3974 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
3975 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
3976 else
3977 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
3978
3979 return getUMaxExpr(PromotedLHS, PromotedRHS);
3980}
3981
Dan Gohmanabd17092009-06-24 14:49:00 +00003982const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS,
3983 const SCEV *RHS) {
Dan Gohmanaf752342009-07-07 17:06:11 +00003984 const SCEV *PromotedLHS = LHS;
3985 const SCEV *PromotedRHS = RHS;
Dan Gohman2bc22302009-06-22 15:03:27 +00003986
3987 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
3988 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
3989 else
3990 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
3991
3992 return getUMinExpr(PromotedLHS, PromotedRHS);
3993}
3994
Andrew Trick87716c92011-03-17 23:51:11 +00003995const SCEV *ScalarEvolution::getPointerBase(const SCEV *V) {
3996 // A pointer operand may evaluate to a nonpointer expression, such as null.
3997 if (!V->getType()->isPointerTy())
3998 return V;
3999
4000 if (const SCEVCastExpr *Cast = dyn_cast<SCEVCastExpr>(V)) {
4001 return getPointerBase(Cast->getOperand());
Sanjoy Dasd295f2c2015-10-18 00:29:27 +00004002 } else if (const SCEVNAryExpr *NAry = dyn_cast<SCEVNAryExpr>(V)) {
Craig Topper9f008862014-04-15 04:59:12 +00004003 const SCEV *PtrOp = nullptr;
Sanjoy Dasd87e4352015-12-08 22:53:36 +00004004 for (const SCEV *NAryOp : NAry->operands()) {
4005 if (NAryOp->getType()->isPointerTy()) {
Andrew Trick87716c92011-03-17 23:51:11 +00004006 // Cannot find the base of an expression with multiple pointer operands.
4007 if (PtrOp)
4008 return V;
Sanjoy Dasd87e4352015-12-08 22:53:36 +00004009 PtrOp = NAryOp;
Andrew Trick87716c92011-03-17 23:51:11 +00004010 }
4011 }
4012 if (!PtrOp)
4013 return V;
4014 return getPointerBase(PtrOp);
4015 }
4016 return V;
4017}
4018
Sanjoy Dasf8570812016-05-29 00:38:22 +00004019/// Push users of the given Instruction onto the given Worklist.
Dan Gohman0b89dff2009-07-25 01:13:03 +00004020static void
4021PushDefUseChildren(Instruction *I,
4022 SmallVectorImpl<Instruction *> &Worklist) {
4023 // Push the def-use children onto the Worklist stack.
Chandler Carruthcdf47882014-03-09 03:16:01 +00004024 for (User *U : I->users())
4025 Worklist.push_back(cast<Instruction>(U));
Dan Gohman0b89dff2009-07-25 01:13:03 +00004026}
4027
Sanjoy Dasf1e9cae02016-03-01 19:28:01 +00004028void ScalarEvolution::forgetSymbolicName(Instruction *PN, const SCEV *SymName) {
Dan Gohman0b89dff2009-07-25 01:13:03 +00004029 SmallVector<Instruction *, 16> Worklist;
Dan Gohmana9c205c2010-02-25 06:57:05 +00004030 PushDefUseChildren(PN, Worklist);
Chris Lattnerd934c702004-04-02 20:23:17 +00004031
Dan Gohman0b89dff2009-07-25 01:13:03 +00004032 SmallPtrSet<Instruction *, 8> Visited;
Dan Gohmana9c205c2010-02-25 06:57:05 +00004033 Visited.insert(PN);
Dan Gohman0b89dff2009-07-25 01:13:03 +00004034 while (!Worklist.empty()) {
Dan Gohmana9c205c2010-02-25 06:57:05 +00004035 Instruction *I = Worklist.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +00004036 if (!Visited.insert(I).second)
4037 continue;
Chris Lattner7b0fbe72005-02-13 04:37:18 +00004038
Sanjoy Das63914592015-10-18 00:29:20 +00004039 auto It = ValueExprMap.find_as(static_cast<Value *>(I));
Dan Gohman9bad2fb2010-08-27 18:55:03 +00004040 if (It != ValueExprMap.end()) {
Dan Gohman761065e2010-11-17 02:44:44 +00004041 const SCEV *Old = It->second;
4042
Dan Gohman0b89dff2009-07-25 01:13:03 +00004043 // Short-circuit the def-use traversal if the symbolic name
4044 // ceases to appear in expressions.
Dan Gohman534749b2010-11-17 22:27:42 +00004045 if (Old != SymName && !hasOperand(Old, SymName))
Dan Gohman0b89dff2009-07-25 01:13:03 +00004046 continue;
Chris Lattner7b0fbe72005-02-13 04:37:18 +00004047
Dan Gohman0b89dff2009-07-25 01:13:03 +00004048 // SCEVUnknown for a PHI either means that it has an unrecognized
Dan Gohmana9c205c2010-02-25 06:57:05 +00004049 // structure, it's a PHI that's in the progress of being computed
4050 // by createNodeForPHI, or it's a single-value PHI. In the first case,
4051 // additional loop trip count information isn't going to change anything.
4052 // In the second case, createNodeForPHI will perform the necessary
4053 // updates on its own when it gets to that point. In the third, we do
4054 // want to forget the SCEVUnknown.
4055 if (!isa<PHINode>(I) ||
Dan Gohman761065e2010-11-17 02:44:44 +00004056 !isa<SCEVUnknown>(Old) ||
4057 (I != PN && Old == SymName)) {
Wei Mi785858c2016-08-09 20:37:50 +00004058 eraseValueFromMap(It->first);
Dan Gohman7e6b3932010-11-17 23:28:48 +00004059 forgetMemoizedResults(Old);
Dan Gohmancc2f1eb2009-08-31 21:15:23 +00004060 }
Dan Gohman0b89dff2009-07-25 01:13:03 +00004061 }
4062
4063 PushDefUseChildren(I, Worklist);
4064 }
Chris Lattner7b0fbe72005-02-13 04:37:18 +00004065}
Chris Lattnerd934c702004-04-02 20:23:17 +00004066
Benjamin Kramer83709b12015-11-16 09:01:28 +00004067namespace {
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004068
Silviu Barangaf91c8072015-10-30 15:02:28 +00004069class SCEVInitRewriter : public SCEVRewriteVisitor<SCEVInitRewriter> {
4070public:
Sanjoy Das807d33d2016-02-20 01:44:10 +00004071 static const SCEV *rewrite(const SCEV *S, const Loop *L,
Silviu Barangaf91c8072015-10-30 15:02:28 +00004072 ScalarEvolution &SE) {
4073 SCEVInitRewriter Rewriter(L, SE);
Sanjoy Das807d33d2016-02-20 01:44:10 +00004074 const SCEV *Result = Rewriter.visit(S);
Silviu Barangaf91c8072015-10-30 15:02:28 +00004075 return Rewriter.isValid() ? Result : SE.getCouldNotCompute();
4076 }
4077
Silviu Barangaf91c8072015-10-30 15:02:28 +00004078 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
Max Kazantsev627ad0f2017-05-18 08:26:41 +00004079 if (!SE.isLoopInvariant(Expr, L))
Silviu Barangaf91c8072015-10-30 15:02:28 +00004080 Valid = false;
4081 return Expr;
4082 }
4083
4084 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
4085 // Only allow AddRecExprs for this loop.
4086 if (Expr->getLoop() == L)
4087 return Expr->getStart();
4088 Valid = false;
4089 return Expr;
4090 }
4091
4092 bool isValid() { return Valid; }
4093
4094private:
Jatin Bhateja7410eea2017-11-26 15:08:41 +00004095 explicit SCEVInitRewriter(const Loop *L, ScalarEvolution &SE)
4096 : SCEVRewriteVisitor(SE), L(L) {}
4097
Silviu Barangaf91c8072015-10-30 15:02:28 +00004098 const Loop *L;
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004099 bool Valid = true;
Silviu Barangaf91c8072015-10-30 15:02:28 +00004100};
4101
Jatin Bhatejac61ade12017-11-13 16:43:24 +00004102/// This class evaluates the compare condition by matching it against the
4103/// condition of loop latch. If there is a match we assume a true value
4104/// for the condition while building SCEV nodes.
4105class SCEVBackedgeConditionFolder
4106 : public SCEVRewriteVisitor<SCEVBackedgeConditionFolder> {
4107public:
4108 static const SCEV *rewrite(const SCEV *S, const Loop *L,
4109 ScalarEvolution &SE) {
Reid Klecknere021f702017-11-13 18:43:11 +00004110 bool IsPosBECond = false;
Jatin Bhatejac61ade12017-11-13 16:43:24 +00004111 Value *BECond = nullptr;
4112 if (BasicBlock *Latch = L->getLoopLatch()) {
4113 BranchInst *BI = dyn_cast<BranchInst>(Latch->getTerminator());
Jatin Bhatejaa1da5e42017-11-26 02:01:01 +00004114 if (BI && BI->isConditional()) {
Jatin Bhateja7410eea2017-11-26 15:08:41 +00004115 assert(BI->getSuccessor(0) != BI->getSuccessor(1) &&
4116 "Both outgoing branches should not target same header!");
Jatin Bhatejac61ade12017-11-13 16:43:24 +00004117 BECond = BI->getCondition();
4118 IsPosBECond = BI->getSuccessor(0) == L->getHeader();
4119 } else {
4120 return S;
4121 }
4122 }
4123 SCEVBackedgeConditionFolder Rewriter(L, BECond, IsPosBECond, SE);
4124 return Rewriter.visit(S);
4125 }
4126
4127 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4128 const SCEV *Result = Expr;
4129 bool InvariantF = SE.isLoopInvariant(Expr, L);
4130
4131 if (!InvariantF) {
4132 Instruction *I = cast<Instruction>(Expr->getValue());
4133 switch (I->getOpcode()) {
4134 case Instruction::Select: {
4135 SelectInst *SI = cast<SelectInst>(I);
4136 Optional<const SCEV *> Res =
4137 compareWithBackedgeCondition(SI->getCondition());
4138 if (Res.hasValue()) {
4139 bool IsOne = cast<SCEVConstant>(Res.getValue())->getValue()->isOne();
4140 Result = SE.getSCEV(IsOne ? SI->getTrueValue() : SI->getFalseValue());
4141 }
4142 break;
4143 }
4144 default: {
4145 Optional<const SCEV *> Res = compareWithBackedgeCondition(I);
4146 if (Res.hasValue())
4147 Result = Res.getValue();
4148 break;
4149 }
4150 }
4151 }
4152 return Result;
4153 }
4154
4155private:
4156 explicit SCEVBackedgeConditionFolder(const Loop *L, Value *BECond,
4157 bool IsPosBECond, ScalarEvolution &SE)
4158 : SCEVRewriteVisitor(SE), L(L), BackedgeCond(BECond),
4159 IsPositiveBECond(IsPosBECond) {}
4160
4161 Optional<const SCEV *> compareWithBackedgeCondition(Value *IC);
4162
4163 const Loop *L;
4164 /// Loop back condition.
4165 Value *BackedgeCond = nullptr;
4166 /// Set to true if loop back is on positive branch condition.
4167 bool IsPositiveBECond;
4168};
4169
4170Optional<const SCEV *>
4171SCEVBackedgeConditionFolder::compareWithBackedgeCondition(Value *IC) {
4172
4173 // If value matches the backedge condition for loop latch,
4174 // then return a constant evolution node based on loopback
4175 // branch taken.
4176 if (BackedgeCond == IC)
4177 return IsPositiveBECond ? SE.getOne(Type::getInt1Ty(SE.getContext()))
4178 : SE.getZero(Type::getInt1Ty(SE.getContext()));
4179 return None;
4180}
4181
Silviu Barangaf91c8072015-10-30 15:02:28 +00004182class SCEVShiftRewriter : public SCEVRewriteVisitor<SCEVShiftRewriter> {
4183public:
Sanjoy Das807d33d2016-02-20 01:44:10 +00004184 static const SCEV *rewrite(const SCEV *S, const Loop *L,
Silviu Barangaf91c8072015-10-30 15:02:28 +00004185 ScalarEvolution &SE) {
4186 SCEVShiftRewriter Rewriter(L, SE);
Sanjoy Das807d33d2016-02-20 01:44:10 +00004187 const SCEV *Result = Rewriter.visit(S);
Silviu Barangaf91c8072015-10-30 15:02:28 +00004188 return Rewriter.isValid() ? Result : SE.getCouldNotCompute();
4189 }
4190
Silviu Barangaf91c8072015-10-30 15:02:28 +00004191 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4192 // Only allow AddRecExprs for this loop.
Max Kazantsev627ad0f2017-05-18 08:26:41 +00004193 if (!SE.isLoopInvariant(Expr, L))
Silviu Barangaf91c8072015-10-30 15:02:28 +00004194 Valid = false;
4195 return Expr;
4196 }
4197
4198 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
4199 if (Expr->getLoop() == L && Expr->isAffine())
4200 return SE.getMinusSCEV(Expr, Expr->getStepRecurrence(SE));
4201 Valid = false;
4202 return Expr;
4203 }
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004204
Silviu Barangaf91c8072015-10-30 15:02:28 +00004205 bool isValid() { return Valid; }
4206
4207private:
Jatin Bhateja7410eea2017-11-26 15:08:41 +00004208 explicit SCEVShiftRewriter(const Loop *L, ScalarEvolution &SE)
4209 : SCEVRewriteVisitor(SE), L(L) {}
4210
Silviu Barangaf91c8072015-10-30 15:02:28 +00004211 const Loop *L;
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004212 bool Valid = true;
Silviu Barangaf91c8072015-10-30 15:02:28 +00004213};
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004214
Benjamin Kramer83709b12015-11-16 09:01:28 +00004215} // end anonymous namespace
Silviu Barangaf91c8072015-10-30 15:02:28 +00004216
Sanjoy Das724f5cf2016-03-03 18:31:29 +00004217SCEV::NoWrapFlags
4218ScalarEvolution::proveNoWrapViaConstantRanges(const SCEVAddRecExpr *AR) {
4219 if (!AR->isAffine())
4220 return SCEV::FlagAnyWrap;
4221
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004222 using OBO = OverflowingBinaryOperator;
4223
Sanjoy Das724f5cf2016-03-03 18:31:29 +00004224 SCEV::NoWrapFlags Result = SCEV::FlagAnyWrap;
4225
4226 if (!AR->hasNoSignedWrap()) {
4227 ConstantRange AddRecRange = getSignedRange(AR);
4228 ConstantRange IncRange = getSignedRange(AR->getStepRecurrence(*this));
4229
4230 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
4231 Instruction::Add, IncRange, OBO::NoSignedWrap);
4232 if (NSWRegion.contains(AddRecRange))
4233 Result = ScalarEvolution::setFlags(Result, SCEV::FlagNSW);
4234 }
4235
4236 if (!AR->hasNoUnsignedWrap()) {
4237 ConstantRange AddRecRange = getUnsignedRange(AR);
4238 ConstantRange IncRange = getUnsignedRange(AR->getStepRecurrence(*this));
4239
4240 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
4241 Instruction::Add, IncRange, OBO::NoUnsignedWrap);
4242 if (NUWRegion.contains(AddRecRange))
4243 Result = ScalarEvolution::setFlags(Result, SCEV::FlagNUW);
4244 }
4245
4246 return Result;
4247}
4248
Sanjoy Das118d9192016-03-31 05:14:22 +00004249namespace {
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004250
Sanjoy Das118d9192016-03-31 05:14:22 +00004251/// Represents an abstract binary operation. This may exist as a
4252/// normal instruction or constant expression, or may have been
4253/// derived from an expression tree.
4254struct BinaryOp {
4255 unsigned Opcode;
4256 Value *LHS;
4257 Value *RHS;
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004258 bool IsNSW = false;
4259 bool IsNUW = false;
Sanjoy Das118d9192016-03-31 05:14:22 +00004260
4261 /// Op is set if this BinaryOp corresponds to a concrete LLVM instruction or
4262 /// constant expression.
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004263 Operator *Op = nullptr;
Sanjoy Das118d9192016-03-31 05:14:22 +00004264
4265 explicit BinaryOp(Operator *Op)
4266 : Opcode(Op->getOpcode()), LHS(Op->getOperand(0)), RHS(Op->getOperand(1)),
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004267 Op(Op) {
Sanjoy Dase12c0e52016-03-31 05:14:26 +00004268 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Op)) {
4269 IsNSW = OBO->hasNoSignedWrap();
4270 IsNUW = OBO->hasNoUnsignedWrap();
4271 }
4272 }
Sanjoy Das118d9192016-03-31 05:14:22 +00004273
Sanjoy Dase12c0e52016-03-31 05:14:26 +00004274 explicit BinaryOp(unsigned Opcode, Value *LHS, Value *RHS, bool IsNSW = false,
4275 bool IsNUW = false)
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004276 : Opcode(Opcode), LHS(LHS), RHS(RHS), IsNSW(IsNSW), IsNUW(IsNUW) {}
Sanjoy Das118d9192016-03-31 05:14:22 +00004277};
Sanjoy Das118d9192016-03-31 05:14:22 +00004278
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004279} // end anonymous namespace
Sanjoy Das118d9192016-03-31 05:14:22 +00004280
4281/// Try to map \p V into a BinaryOp, and return \c None on failure.
Sanjoy Dasf49ca522016-05-29 00:34:42 +00004282static Optional<BinaryOp> MatchBinaryOp(Value *V, DominatorTree &DT) {
Sanjoy Das118d9192016-03-31 05:14:22 +00004283 auto *Op = dyn_cast<Operator>(V);
4284 if (!Op)
4285 return None;
4286
4287 // Implementation detail: all the cleverness here should happen without
4288 // creating new SCEV expressions -- our caller knowns tricks to avoid creating
4289 // SCEV expressions when possible, and we should not break that.
4290
4291 switch (Op->getOpcode()) {
4292 case Instruction::Add:
4293 case Instruction::Sub:
4294 case Instruction::Mul:
4295 case Instruction::UDiv:
Alexandre Isoard405728f2017-09-01 14:59:59 +00004296 case Instruction::URem:
Sanjoy Das118d9192016-03-31 05:14:22 +00004297 case Instruction::And:
4298 case Instruction::Or:
4299 case Instruction::AShr:
4300 case Instruction::Shl:
4301 return BinaryOp(Op);
4302
4303 case Instruction::Xor:
4304 if (auto *RHSC = dyn_cast<ConstantInt>(Op->getOperand(1)))
Craig Topperbcfd2d12017-04-20 16:56:25 +00004305 // If the RHS of the xor is a signmask, then this is just an add.
4306 // Instcombine turns add of signmask into xor as a strength reduction step.
4307 if (RHSC->getValue().isSignMask())
Sanjoy Das118d9192016-03-31 05:14:22 +00004308 return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1));
4309 return BinaryOp(Op);
4310
4311 case Instruction::LShr:
4312 // Turn logical shift right of a constant into a unsigned divide.
4313 if (ConstantInt *SA = dyn_cast<ConstantInt>(Op->getOperand(1))) {
4314 uint32_t BitWidth = cast<IntegerType>(Op->getType())->getBitWidth();
4315
4316 // If the shift count is not less than the bitwidth, the result of
4317 // the shift is undefined. Don't try to analyze it, because the
4318 // resolution chosen here may differ from the resolution chosen in
4319 // other parts of the compiler.
4320 if (SA->getValue().ult(BitWidth)) {
4321 Constant *X =
4322 ConstantInt::get(SA->getContext(),
4323 APInt::getOneBitSet(BitWidth, SA->getZExtValue()));
4324 return BinaryOp(Instruction::UDiv, Op->getOperand(0), X);
4325 }
4326 }
4327 return BinaryOp(Op);
4328
Sanjoy Dasf49ca522016-05-29 00:34:42 +00004329 case Instruction::ExtractValue: {
4330 auto *EVI = cast<ExtractValueInst>(Op);
4331 if (EVI->getNumIndices() != 1 || EVI->getIndices()[0] != 0)
4332 break;
4333
4334 auto *CI = dyn_cast<CallInst>(EVI->getAggregateOperand());
4335 if (!CI)
4336 break;
4337
4338 if (auto *F = CI->getCalledFunction())
4339 switch (F->getIntrinsicID()) {
4340 case Intrinsic::sadd_with_overflow:
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004341 case Intrinsic::uadd_with_overflow:
Sanjoy Dasf49ca522016-05-29 00:34:42 +00004342 if (!isOverflowIntrinsicNoWrap(cast<IntrinsicInst>(CI), DT))
4343 return BinaryOp(Instruction::Add, CI->getArgOperand(0),
4344 CI->getArgOperand(1));
4345
4346 // Now that we know that all uses of the arithmetic-result component of
4347 // CI are guarded by the overflow check, we can go ahead and pretend
4348 // that the arithmetic is non-overflowing.
4349 if (F->getIntrinsicID() == Intrinsic::sadd_with_overflow)
4350 return BinaryOp(Instruction::Add, CI->getArgOperand(0),
4351 CI->getArgOperand(1), /* IsNSW = */ true,
4352 /* IsNUW = */ false);
4353 else
4354 return BinaryOp(Instruction::Add, CI->getArgOperand(0),
4355 CI->getArgOperand(1), /* IsNSW = */ false,
4356 /* IsNUW*/ true);
Sanjoy Dasf49ca522016-05-29 00:34:42 +00004357 case Intrinsic::ssub_with_overflow:
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004358 case Intrinsic::usub_with_overflow:
Amara Emerson56dca4e32017-08-04 20:19:46 +00004359 if (!isOverflowIntrinsicNoWrap(cast<IntrinsicInst>(CI), DT))
4360 return BinaryOp(Instruction::Sub, CI->getArgOperand(0),
4361 CI->getArgOperand(1));
Sanjoy Dasf49ca522016-05-29 00:34:42 +00004362
Amara Emerson56dca4e32017-08-04 20:19:46 +00004363 // The same reasoning as sadd/uadd above.
4364 if (F->getIntrinsicID() == Intrinsic::ssub_with_overflow)
4365 return BinaryOp(Instruction::Sub, CI->getArgOperand(0),
4366 CI->getArgOperand(1), /* IsNSW = */ true,
4367 /* IsNUW = */ false);
4368 else
4369 return BinaryOp(Instruction::Sub, CI->getArgOperand(0),
4370 CI->getArgOperand(1), /* IsNSW = */ false,
4371 /* IsNUW = */ true);
Sanjoy Dasf49ca522016-05-29 00:34:42 +00004372 case Intrinsic::smul_with_overflow:
4373 case Intrinsic::umul_with_overflow:
4374 return BinaryOp(Instruction::Mul, CI->getArgOperand(0),
4375 CI->getArgOperand(1));
4376 default:
4377 break;
4378 }
Adrian Prantl0e6694d2017-12-19 22:05:25 +00004379 break;
Sanjoy Dasf49ca522016-05-29 00:34:42 +00004380 }
4381
Sanjoy Das118d9192016-03-31 05:14:22 +00004382 default:
4383 break;
4384 }
4385
4386 return None;
4387}
4388
Michael Liaob30286d2017-09-25 16:21:21 +00004389/// Helper function to createAddRecFromPHIWithCasts. We have a phi
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004390/// node whose symbolic (unknown) SCEV is \p SymbolicPHI, which is updated via
Michael Liaob30286d2017-09-25 16:21:21 +00004391/// the loop backedge by a SCEVAddExpr, possibly also with a few casts on the
4392/// way. This function checks if \p Op, an operand of this SCEVAddExpr,
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004393/// follows one of the following patterns:
4394/// Op == (SExt ix (Trunc iy (%SymbolicPHI) to ix) to iy)
4395/// Op == (ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy)
4396/// If the SCEV expression of \p Op conforms with one of the expected patterns
4397/// we return the type of the truncation operation, and indicate whether the
Michael Liaob30286d2017-09-25 16:21:21 +00004398/// truncated type should be treated as signed/unsigned by setting
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004399/// \p Signed to true/false, respectively.
4400static Type *isSimpleCastedPHI(const SCEV *Op, const SCEVUnknown *SymbolicPHI,
4401 bool &Signed, ScalarEvolution &SE) {
Michael Liaob30286d2017-09-25 16:21:21 +00004402 // The case where Op == SymbolicPHI (that is, with no type conversions on
4403 // the way) is handled by the regular add recurrence creating logic and
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004404 // would have already been triggered in createAddRecForPHI. Reaching it here
Michael Liaob30286d2017-09-25 16:21:21 +00004405 // means that createAddRecFromPHI had failed for this PHI before (e.g.,
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004406 // because one of the other operands of the SCEVAddExpr updating this PHI is
Michael Liaob30286d2017-09-25 16:21:21 +00004407 // not invariant).
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004408 //
Michael Liaob30286d2017-09-25 16:21:21 +00004409 // Here we look for the case where Op = (ext(trunc(SymbolicPHI))), and in
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004410 // this case predicates that allow us to prove that Op == SymbolicPHI will
4411 // be added.
4412 if (Op == SymbolicPHI)
4413 return nullptr;
4414
4415 unsigned SourceBits = SE.getTypeSizeInBits(SymbolicPHI->getType());
4416 unsigned NewBits = SE.getTypeSizeInBits(Op->getType());
4417 if (SourceBits != NewBits)
4418 return nullptr;
4419
4420 const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(Op);
4421 const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(Op);
4422 if (!SExt && !ZExt)
4423 return nullptr;
4424 const SCEVTruncateExpr *Trunc =
4425 SExt ? dyn_cast<SCEVTruncateExpr>(SExt->getOperand())
4426 : dyn_cast<SCEVTruncateExpr>(ZExt->getOperand());
4427 if (!Trunc)
4428 return nullptr;
4429 const SCEV *X = Trunc->getOperand();
4430 if (X != SymbolicPHI)
4431 return nullptr;
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004432 Signed = SExt != nullptr;
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004433 return Trunc->getType();
4434}
4435
4436static const Loop *isIntegerLoopHeaderPHI(const PHINode *PN, LoopInfo &LI) {
4437 if (!PN->getType()->isIntegerTy())
4438 return nullptr;
4439 const Loop *L = LI.getLoopFor(PN->getParent());
4440 if (!L || L->getHeader() != PN->getParent())
4441 return nullptr;
4442 return L;
4443}
4444
4445// Analyze \p SymbolicPHI, a SCEV expression of a phi node, and check if the
4446// computation that updates the phi follows the following pattern:
4447// (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
4448// which correspond to a phi->trunc->sext/zext->add->phi update chain.
4449// If so, try to see if it can be rewritten as an AddRecExpr under some
4450// Predicates. If successful, return them as a pair. Also cache the results
4451// of the analysis.
4452//
4453// Example usage scenario:
4454// Say the Rewriter is called for the following SCEV:
4455// 8 * ((sext i32 (trunc i64 %X to i32) to i64) + %Step)
4456// where:
4457// %X = phi i64 (%Start, %BEValue)
4458// It will visitMul->visitAdd->visitSExt->visitTrunc->visitUnknown(%X),
4459// and call this function with %SymbolicPHI = %X.
4460//
Michael Liaob30286d2017-09-25 16:21:21 +00004461// The analysis will find that the value coming around the backedge has
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004462// the following SCEV:
4463// BEValue = ((sext i32 (trunc i64 %X to i32) to i64) + %Step)
4464// Upon concluding that this matches the desired pattern, the function
4465// will return the pair {NewAddRec, SmallPredsVec} where:
4466// NewAddRec = {%Start,+,%Step}
4467// SmallPredsVec = {P1, P2, P3} as follows:
4468// P1(WrapPred): AR: {trunc(%Start),+,(trunc %Step)}<nsw> Flags: <nssw>
4469// P2(EqualPred): %Start == (sext i32 (trunc i64 %Start to i32) to i64)
4470// P3(EqualPred): %Step == (sext i32 (trunc i64 %Step to i32) to i64)
4471// The returned pair means that SymbolicPHI can be rewritten into NewAddRec
4472// under the predicates {P1,P2,P3}.
4473// This predicated rewrite will be cached in PredicatedSCEVRewrites:
Michael Liaob30286d2017-09-25 16:21:21 +00004474// PredicatedSCEVRewrites[{%X,L}] = {NewAddRec, {P1,P2,P3)}
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004475//
4476// TODO's:
4477//
4478// 1) Extend the Induction descriptor to also support inductions that involve
Michael Liaob30286d2017-09-25 16:21:21 +00004479// casts: When needed (namely, when we are called in the context of the
4480// vectorizer induction analysis), a Set of cast instructions will be
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004481// populated by this method, and provided back to isInductionPHI. This is
4482// needed to allow the vectorizer to properly record them to be ignored by
4483// the cost model and to avoid vectorizing them (otherwise these casts,
Michael Liaob30286d2017-09-25 16:21:21 +00004484// which are redundant under the runtime overflow checks, will be
4485// vectorized, which can be costly).
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004486//
4487// 2) Support additional induction/PHISCEV patterns: We also want to support
Michael Liaob30286d2017-09-25 16:21:21 +00004488// inductions where the sext-trunc / zext-trunc operations (partly) occur
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004489// after the induction update operation (the induction increment):
4490//
4491// (Trunc iy (SExt/ZExt ix (%SymbolicPHI + InvariantAccum) to iy) to ix)
4492// which correspond to a phi->add->trunc->sext/zext->phi update chain.
4493//
4494// (Trunc iy ((SExt/ZExt ix (%SymbolicPhi) to iy) + InvariantAccum) to ix)
4495// which correspond to a phi->trunc->add->sext/zext->phi update chain.
4496//
4497// 3) Outline common code with createAddRecFromPHI to avoid duplication.
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004498Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
4499ScalarEvolution::createAddRecFromPHIWithCastsImpl(const SCEVUnknown *SymbolicPHI) {
4500 SmallVector<const SCEVPredicate *, 3> Predicates;
4501
Michael Liaob30286d2017-09-25 16:21:21 +00004502 // *** Part1: Analyze if we have a phi-with-cast pattern for which we can
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004503 // return an AddRec expression under some predicate.
Michael Liaob30286d2017-09-25 16:21:21 +00004504
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004505 auto *PN = cast<PHINode>(SymbolicPHI->getValue());
4506 const Loop *L = isIntegerLoopHeaderPHI(PN, LI);
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004507 assert(L && "Expecting an integer loop header phi");
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004508
4509 // The loop may have multiple entrances or multiple exits; we can analyze
4510 // this phi as an addrec if it has a unique entry value and a unique
4511 // backedge value.
4512 Value *BEValueV = nullptr, *StartValueV = nullptr;
4513 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
4514 Value *V = PN->getIncomingValue(i);
4515 if (L->contains(PN->getIncomingBlock(i))) {
4516 if (!BEValueV) {
4517 BEValueV = V;
4518 } else if (BEValueV != V) {
4519 BEValueV = nullptr;
4520 break;
4521 }
4522 } else if (!StartValueV) {
4523 StartValueV = V;
4524 } else if (StartValueV != V) {
4525 StartValueV = nullptr;
4526 break;
4527 }
4528 }
4529 if (!BEValueV || !StartValueV)
4530 return None;
4531
4532 const SCEV *BEValue = getSCEV(BEValueV);
4533
4534 // If the value coming around the backedge is an add with the symbolic
4535 // value we just inserted, possibly with casts that we can ignore under
4536 // an appropriate runtime guard, then we found a simple induction variable!
4537 const auto *Add = dyn_cast<SCEVAddExpr>(BEValue);
4538 if (!Add)
4539 return None;
4540
4541 // If there is a single occurrence of the symbolic value, possibly
Michael Liaob30286d2017-09-25 16:21:21 +00004542 // casted, replace it with a recurrence.
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004543 unsigned FoundIndex = Add->getNumOperands();
4544 Type *TruncTy = nullptr;
4545 bool Signed;
4546 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
Michael Liaob30286d2017-09-25 16:21:21 +00004547 if ((TruncTy =
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004548 isSimpleCastedPHI(Add->getOperand(i), SymbolicPHI, Signed, *this)))
4549 if (FoundIndex == e) {
4550 FoundIndex = i;
4551 break;
4552 }
4553
4554 if (FoundIndex == Add->getNumOperands())
4555 return None;
4556
4557 // Create an add with everything but the specified operand.
4558 SmallVector<const SCEV *, 8> Ops;
4559 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
4560 if (i != FoundIndex)
4561 Ops.push_back(Add->getOperand(i));
4562 const SCEV *Accum = getAddExpr(Ops);
4563
4564 // The runtime checks will not be valid if the step amount is
4565 // varying inside the loop.
4566 if (!isLoopInvariant(Accum, L))
4567 return None;
Daniel Neilson3f0e4ad2017-09-05 19:54:03 +00004568
Michael Liaob30286d2017-09-25 16:21:21 +00004569 // *** Part2: Create the predicates
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004570
4571 // Analysis was successful: we have a phi-with-cast pattern for which we
4572 // can return an AddRec expression under the following predicates:
4573 //
4574 // P1: A Wrap predicate that guarantees that Trunc(Start) + i*Trunc(Accum)
4575 // fits within the truncated type (does not overflow) for i = 0 to n-1.
Michael Liaob30286d2017-09-25 16:21:21 +00004576 // P2: An Equal predicate that guarantees that
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004577 // Start = (Ext ix (Trunc iy (Start) to ix) to iy)
Michael Liaob30286d2017-09-25 16:21:21 +00004578 // P3: An Equal predicate that guarantees that
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004579 // Accum = (Ext ix (Trunc iy (Accum) to ix) to iy)
4580 //
Michael Liaob30286d2017-09-25 16:21:21 +00004581 // As we next prove, the above predicates guarantee that:
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004582 // Start + i*Accum = (Ext ix (Trunc iy ( Start + i*Accum ) to ix) to iy)
4583 //
4584 //
4585 // More formally, we want to prove that:
Michael Liaob30286d2017-09-25 16:21:21 +00004586 // Expr(i+1) = Start + (i+1) * Accum
4587 // = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004588 //
4589 // Given that:
Michael Liaob30286d2017-09-25 16:21:21 +00004590 // 1) Expr(0) = Start
4591 // 2) Expr(1) = Start + Accum
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004592 // = (Ext ix (Trunc iy (Start) to ix) to iy) + Accum :: from P2
4593 // 3) Induction hypothesis (step i):
Michael Liaob30286d2017-09-25 16:21:21 +00004594 // Expr(i) = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004595 //
4596 // Proof:
4597 // Expr(i+1) =
4598 // = Start + (i+1)*Accum
4599 // = (Start + i*Accum) + Accum
Michael Liaob30286d2017-09-25 16:21:21 +00004600 // = Expr(i) + Accum
4601 // = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum + Accum
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004602 // :: from step i
4603 //
Michael Liaob30286d2017-09-25 16:21:21 +00004604 // = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy) + Accum + Accum
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004605 //
4606 // = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy)
4607 // + (Ext ix (Trunc iy (Accum) to ix) to iy)
4608 // + Accum :: from P3
4609 //
Michael Liaob30286d2017-09-25 16:21:21 +00004610 // = (Ext ix (Trunc iy ((Start + (i-1)*Accum) + Accum) to ix) to iy)
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004611 // + Accum :: from P1: Ext(x)+Ext(y)=>Ext(x+y)
4612 //
4613 // = (Ext ix (Trunc iy (Start + i*Accum) to ix) to iy) + Accum
Michael Liaob30286d2017-09-25 16:21:21 +00004614 // = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004615 //
4616 // By induction, the same applies to all iterations 1<=i<n:
4617 //
Daniel Neilson3f0e4ad2017-09-05 19:54:03 +00004618
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004619 // Create a truncated addrec for which we will add a no overflow check (P1).
4620 const SCEV *StartVal = getSCEV(StartValueV);
Daniel Neilson3f0e4ad2017-09-05 19:54:03 +00004621 const SCEV *PHISCEV =
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004622 getAddRecExpr(getTruncateExpr(StartVal, TruncTy),
Daniel Neilson3f0e4ad2017-09-05 19:54:03 +00004623 getTruncateExpr(Accum, TruncTy), L, SCEV::FlagAnyWrap);
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004624
Daniel Neilson3f0e4ad2017-09-05 19:54:03 +00004625 // PHISCEV can be either a SCEVConstant or a SCEVAddRecExpr.
4626 // ex: If truncated Accum is 0 and StartVal is a constant, then PHISCEV
4627 // will be constant.
4628 //
4629 // If PHISCEV is a constant, then P1 degenerates into P2 or P3, so we don't
4630 // add P1.
4631 if (const auto *AR = dyn_cast<SCEVAddRecExpr>(PHISCEV)) {
4632 SCEVWrapPredicate::IncrementWrapFlags AddedFlags =
4633 Signed ? SCEVWrapPredicate::IncrementNSSW
4634 : SCEVWrapPredicate::IncrementNUSW;
4635 const SCEVPredicate *AddRecPred = getWrapPredicate(AR, AddedFlags);
4636 Predicates.push_back(AddRecPred);
Daniel Neilson5acfd1d2017-10-11 19:05:14 +00004637 }
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004638
4639 // Create the Equal Predicates P2,P3:
Daniel Neilson3f0e4ad2017-09-05 19:54:03 +00004640
4641 // It is possible that the predicates P2 and/or P3 are computable at
4642 // compile time due to StartVal and/or Accum being constants.
4643 // If either one is, then we can check that now and escape if either P2
4644 // or P3 is false.
4645
4646 // Construct the extended SCEV: (Ext ix (Trunc iy (Expr) to ix) to iy)
4647 // for each of StartVal and Accum
Dorit Nuzman5809e702017-12-10 11:13:35 +00004648 auto getExtendedExpr = [&](const SCEV *Expr,
4649 bool CreateSignExtend) -> const SCEV * {
Eugene Zelenkobe709f22017-08-18 23:51:26 +00004650 assert(isLoopInvariant(Expr, L) && "Expr is expected to be invariant");
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004651 const SCEV *TruncatedExpr = getTruncateExpr(Expr, TruncTy);
4652 const SCEV *ExtendedExpr =
Dorit Nuzman5809e702017-12-10 11:13:35 +00004653 CreateSignExtend ? getSignExtendExpr(TruncatedExpr, Expr->getType())
4654 : getZeroExtendExpr(TruncatedExpr, Expr->getType());
Daniel Neilson3f0e4ad2017-09-05 19:54:03 +00004655 return ExtendedExpr;
4656 };
4657
4658 // Given:
4659 // ExtendedExpr = (Ext ix (Trunc iy (Expr) to ix) to iy
Dorit Nuzman5809e702017-12-10 11:13:35 +00004660 // = getExtendedExpr(Expr)
Daniel Neilson3f0e4ad2017-09-05 19:54:03 +00004661 // Determine whether the predicate P: Expr == ExtendedExpr
4662 // is known to be false at compile time
4663 auto PredIsKnownFalse = [&](const SCEV *Expr,
4664 const SCEV *ExtendedExpr) -> bool {
4665 return Expr != ExtendedExpr &&
4666 isKnownPredicate(ICmpInst::ICMP_NE, Expr, ExtendedExpr);
4667 };
4668
Dorit Nuzman5809e702017-12-10 11:13:35 +00004669 const SCEV *StartExtended = getExtendedExpr(StartVal, Signed);
Daniel Neilson3f0e4ad2017-09-05 19:54:03 +00004670 if (PredIsKnownFalse(StartVal, StartExtended)) {
4671 DEBUG(dbgs() << "P2 is compile-time false\n";);
4672 return None;
4673 }
4674
Dorit Nuzman5809e702017-12-10 11:13:35 +00004675 // The Step is always Signed (because the overflow checks are either
4676 // NSSW or NUSW)
4677 const SCEV *AccumExtended = getExtendedExpr(Accum, /*CreateSignExtend=*/true);
Daniel Neilson3f0e4ad2017-09-05 19:54:03 +00004678 if (PredIsKnownFalse(Accum, AccumExtended)) {
4679 DEBUG(dbgs() << "P3 is compile-time false\n";);
4680 return None;
4681 }
4682
4683 auto AppendPredicate = [&](const SCEV *Expr,
4684 const SCEV *ExtendedExpr) -> void {
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004685 if (Expr != ExtendedExpr &&
4686 !isKnownPredicate(ICmpInst::ICMP_EQ, Expr, ExtendedExpr)) {
4687 const SCEVPredicate *Pred = getEqualPredicate(Expr, ExtendedExpr);
4688 DEBUG (dbgs() << "Added Predicate: " << *Pred);
4689 Predicates.push_back(Pred);
4690 }
4691 };
Daniel Neilson3f0e4ad2017-09-05 19:54:03 +00004692
4693 AppendPredicate(StartVal, StartExtended);
4694 AppendPredicate(Accum, AccumExtended);
4695
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004696 // *** Part3: Predicates are ready. Now go ahead and create the new addrec in
4697 // which the casts had been folded away. The caller can rewrite SymbolicPHI
4698 // into NewAR if it will also add the runtime overflow checks specified in
Michael Liaob30286d2017-09-25 16:21:21 +00004699 // Predicates.
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004700 auto *NewAR = getAddRecExpr(StartVal, Accum, L, SCEV::FlagAnyWrap);
4701
4702 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> PredRewrite =
4703 std::make_pair(NewAR, Predicates);
4704 // Remember the result of the analysis for this SCEV at this locayyytion.
4705 PredicatedSCEVRewrites[{SymbolicPHI, L}] = PredRewrite;
4706 return PredRewrite;
4707}
4708
4709Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
4710ScalarEvolution::createAddRecFromPHIWithCasts(const SCEVUnknown *SymbolicPHI) {
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004711 auto *PN = cast<PHINode>(SymbolicPHI->getValue());
4712 const Loop *L = isIntegerLoopHeaderPHI(PN, LI);
4713 if (!L)
4714 return None;
4715
4716 // Check to see if we already analyzed this PHI.
4717 auto I = PredicatedSCEVRewrites.find({SymbolicPHI, L});
4718 if (I != PredicatedSCEVRewrites.end()) {
4719 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> Rewrite =
4720 I->second;
4721 // Analysis was done before and failed to create an AddRec:
Michael Liaob30286d2017-09-25 16:21:21 +00004722 if (Rewrite.first == SymbolicPHI)
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00004723 return None;
4724 // Analysis was done before and succeeded to create an AddRec under
4725 // a predicate:
4726 assert(isa<SCEVAddRecExpr>(Rewrite.first) && "Expected an AddRec");
4727 assert(!(Rewrite.second).empty() && "Expected to find Predicates");
4728 return Rewrite;
4729 }
4730
4731 Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
4732 Rewrite = createAddRecFromPHIWithCastsImpl(SymbolicPHI);
4733
4734 // Record in the cache that the analysis failed
4735 if (!Rewrite) {
4736 SmallVector<const SCEVPredicate *, 3> Predicates;
4737 PredicatedSCEVRewrites[{SymbolicPHI, L}] = {SymbolicPHI, Predicates};
4738 return None;
4739 }
4740
4741 return Rewrite;
4742}
4743
Dorit Nuzman4750c782017-12-14 07:56:31 +00004744// FIXME: This utility is currently required because the Rewriter currently
4745// does not rewrite this expression:
4746// {0, +, (sext ix (trunc iy to ix) to iy)}
4747// into {0, +, %step},
4748// even when the following Equal predicate exists:
4749// "%step == (sext ix (trunc iy to ix) to iy)".
4750bool PredicatedScalarEvolution::areAddRecsEqualWithPreds(
4751 const SCEVAddRecExpr *AR1, const SCEVAddRecExpr *AR2) const {
4752 if (AR1 == AR2)
4753 return true;
4754
4755 auto areExprsEqual = [&](const SCEV *Expr1, const SCEV *Expr2) -> bool {
4756 if (Expr1 != Expr2 && !Preds.implies(SE.getEqualPredicate(Expr1, Expr2)) &&
4757 !Preds.implies(SE.getEqualPredicate(Expr2, Expr1)))
4758 return false;
4759 return true;
4760 };
4761
4762 if (!areExprsEqual(AR1->getStart(), AR2->getStart()) ||
4763 !areExprsEqual(AR1->getStepRecurrence(SE), AR2->getStepRecurrence(SE)))
4764 return false;
4765 return true;
4766}
4767
Michael Zolotukhin37162ad2017-05-03 23:53:38 +00004768/// A helper function for createAddRecFromPHI to handle simple cases.
4769///
4770/// This function tries to find an AddRec expression for the simplest (yet most
4771/// common) cases: PN = PHI(Start, OP(Self, LoopInvariant)).
4772/// If it fails, createAddRecFromPHI will use a more general, but slow,
4773/// technique for finding the AddRec expression.
4774const SCEV *ScalarEvolution::createSimpleAffineAddRec(PHINode *PN,
4775 Value *BEValueV,
4776 Value *StartValueV) {
4777 const Loop *L = LI.getLoopFor(PN->getParent());
4778 assert(L && L->getHeader() == PN->getParent());
4779 assert(BEValueV && StartValueV);
4780
4781 auto BO = MatchBinaryOp(BEValueV, DT);
4782 if (!BO)
4783 return nullptr;
4784
4785 if (BO->Opcode != Instruction::Add)
4786 return nullptr;
4787
4788 const SCEV *Accum = nullptr;
4789 if (BO->LHS == PN && L->isLoopInvariant(BO->RHS))
4790 Accum = getSCEV(BO->RHS);
4791 else if (BO->RHS == PN && L->isLoopInvariant(BO->LHS))
4792 Accum = getSCEV(BO->LHS);
4793
4794 if (!Accum)
4795 return nullptr;
4796
4797 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
4798 if (BO->IsNUW)
4799 Flags = setFlags(Flags, SCEV::FlagNUW);
4800 if (BO->IsNSW)
4801 Flags = setFlags(Flags, SCEV::FlagNSW);
4802
4803 const SCEV *StartVal = getSCEV(StartValueV);
4804 const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags);
4805
4806 ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV;
4807
4808 // We can add Flags to the post-inc expression only if we
Michael Zolotukhin3207d302017-05-04 17:42:34 +00004809 // know that it is *undefined behavior* for BEValueV to
Michael Zolotukhin37162ad2017-05-03 23:53:38 +00004810 // overflow.
4811 if (auto *BEInst = dyn_cast<Instruction>(BEValueV))
4812 if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L))
4813 (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
4814
4815 return PHISCEV;
4816}
4817
Sanjoy Das55015d22015-10-02 23:09:44 +00004818const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) {
4819 const Loop *L = LI.getLoopFor(PN->getParent());
4820 if (!L || L->getHeader() != PN->getParent())
4821 return nullptr;
4822
4823 // The loop may have multiple entrances or multiple exits; we can analyze
4824 // this phi as an addrec if it has a unique entry value and a unique
4825 // backedge value.
4826 Value *BEValueV = nullptr, *StartValueV = nullptr;
4827 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
4828 Value *V = PN->getIncomingValue(i);
4829 if (L->contains(PN->getIncomingBlock(i))) {
4830 if (!BEValueV) {
4831 BEValueV = V;
4832 } else if (BEValueV != V) {
4833 BEValueV = nullptr;
4834 break;
4835 }
4836 } else if (!StartValueV) {
4837 StartValueV = V;
4838 } else if (StartValueV != V) {
4839 StartValueV = nullptr;
4840 break;
4841 }
4842 }
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004843 if (!BEValueV || !StartValueV)
4844 return nullptr;
Sanjoy Das55015d22015-10-02 23:09:44 +00004845
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004846 assert(ValueExprMap.find_as(PN) == ValueExprMap.end() &&
4847 "PHI node already processed?");
Michael Zolotukhin37162ad2017-05-03 23:53:38 +00004848
4849 // First, try to find AddRec expression without creating a fictituos symbolic
4850 // value for PN.
4851 if (auto *S = createSimpleAffineAddRec(PN, BEValueV, StartValueV))
4852 return S;
4853
4854 // Handle PHI node value symbolically.
4855 const SCEV *SymbolicName = getUnknown(PN);
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004856 ValueExprMap.insert({SCEVCallbackVH(PN, this), SymbolicName});
Sanjoy Das55015d22015-10-02 23:09:44 +00004857
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004858 // Using this symbolic name for the PHI, analyze the value coming around
4859 // the back-edge.
4860 const SCEV *BEValue = getSCEV(BEValueV);
Sanjoy Das55015d22015-10-02 23:09:44 +00004861
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004862 // NOTE: If BEValue is loop invariant, we know that the PHI node just
4863 // has a special value for the first iteration of the loop.
4864
4865 // If the value coming around the backedge is an add with the symbolic
4866 // value we just inserted, then we found a simple induction variable!
4867 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
4868 // If there is a single occurrence of the symbolic value, replace it
4869 // with a recurrence.
4870 unsigned FoundIndex = Add->getNumOperands();
4871 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
4872 if (Add->getOperand(i) == SymbolicName)
4873 if (FoundIndex == e) {
4874 FoundIndex = i;
4875 break;
4876 }
4877
4878 if (FoundIndex != Add->getNumOperands()) {
4879 // Create an add with everything but the specified operand.
4880 SmallVector<const SCEV *, 8> Ops;
Sanjoy Das55015d22015-10-02 23:09:44 +00004881 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004882 if (i != FoundIndex)
Jatin Bhatejac61ade12017-11-13 16:43:24 +00004883 Ops.push_back(SCEVBackedgeConditionFolder::rewrite(Add->getOperand(i),
4884 L, *this));
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004885 const SCEV *Accum = getAddExpr(Ops);
4886
4887 // This is not a valid addrec if the step amount is varying each
4888 // loop iteration, but is not itself an addrec in this loop.
Sanjoy Das2f274562017-10-18 22:00:57 +00004889 if (isLoopInvariant(Accum, L) ||
4890 (isa<SCEVAddRecExpr>(Accum) &&
4891 cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004892 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
4893
4894 if (auto BO = MatchBinaryOp(BEValueV, DT)) {
4895 if (BO->Opcode == Instruction::Add && BO->LHS == PN) {
4896 if (BO->IsNUW)
4897 Flags = setFlags(Flags, SCEV::FlagNUW);
4898 if (BO->IsNSW)
4899 Flags = setFlags(Flags, SCEV::FlagNSW);
4900 }
4901 } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(BEValueV)) {
4902 // If the increment is an inbounds GEP, then we know the address
4903 // space cannot be wrapped around. We cannot make any guarantee
4904 // about signed or unsigned overflow because pointers are
4905 // unsigned but we may have a negative index from the base
4906 // pointer. We can guarantee that no unsigned wrap occurs if the
4907 // indices form a positive value.
4908 if (GEP->isInBounds() && GEP->getOperand(0) == PN) {
4909 Flags = setFlags(Flags, SCEV::FlagNW);
Dorit Nuzman4750c782017-12-14 07:56:31 +00004910
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004911 const SCEV *Ptr = getSCEV(GEP->getPointerOperand());
4912 if (isKnownPositive(getMinusSCEV(getSCEV(GEP), Ptr)))
4913 Flags = setFlags(Flags, SCEV::FlagNUW);
Dan Gohman6635bb22010-04-12 07:49:36 +00004914 }
Dorit Nuzman4750c782017-12-14 07:56:31 +00004915
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004916 // We cannot transfer nuw and nsw flags from subtraction
4917 // operations -- sub nuw X, Y is not the same as add nuw X, -Y
4918 // for instance.
Dan Gohman6635bb22010-04-12 07:49:36 +00004919 }
Dorit Nuzman4750c782017-12-14 07:56:31 +00004920
Sanjoy Das55015d22015-10-02 23:09:44 +00004921 const SCEV *StartVal = getSCEV(StartValueV);
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004922 const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags);
Dorit Nuzman4750c782017-12-14 07:56:31 +00004923
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004924 // Okay, for the entire analysis of this edge we assumed the PHI
4925 // to be symbolic. We now need to go back and purge all of the
4926 // entries for the scalars that use the symbolic expression.
4927 forgetSymbolicName(PN, SymbolicName);
4928 ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV;
Dorit Nuzman4750c782017-12-14 07:56:31 +00004929
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004930 // We can add Flags to the post-inc expression only if we
Michael Zolotukhin3207d302017-05-04 17:42:34 +00004931 // know that it is *undefined behavior* for BEValueV to
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004932 // overflow.
4933 if (auto *BEInst = dyn_cast<Instruction>(BEValueV))
4934 if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L))
4935 (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
Dorit Nuzman4750c782017-12-14 07:56:31 +00004936
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004937 return PHISCEV;
Chris Lattnerd934c702004-04-02 20:23:17 +00004938 }
Dan Gohman6635bb22010-04-12 07:49:36 +00004939 }
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004940 } else {
4941 // Otherwise, this could be a loop like this:
4942 // i = 0; for (j = 1; ..; ++j) { .... i = j; }
4943 // In this case, j = {1,+,1} and BEValue is j.
4944 // Because the other in-value of i (0) fits the evolution of BEValue
4945 // i really is an addrec evolution.
4946 //
4947 // We can generalize this saying that i is the shifted value of BEValue
4948 // by one iteration:
4949 // PHI(f(0), f({1,+,1})) --> f({0,+,1})
4950 const SCEV *Shifted = SCEVShiftRewriter::rewrite(BEValue, L, *this);
4951 const SCEV *Start = SCEVInitRewriter::rewrite(Shifted, L, *this);
4952 if (Shifted != getCouldNotCompute() &&
4953 Start != getCouldNotCompute()) {
4954 const SCEV *StartVal = getSCEV(StartValueV);
4955 if (Start == StartVal) {
4956 // Okay, for the entire analysis of this edge we assumed the PHI
4957 // to be symbolic. We now need to go back and purge all of the
4958 // entries for the scalars that use the symbolic expression.
4959 forgetSymbolicName(PN, SymbolicName);
4960 ValueExprMap[SCEVCallbackVH(PN, this)] = Shifted;
4961 return Shifted;
4962 }
4963 }
Sanjoy Das55015d22015-10-02 23:09:44 +00004964 }
4965
Michael Zolotukhin146a2212017-04-28 22:14:27 +00004966 // Remove the temporary PHI node SCEV that has been inserted while intending
4967 // to create an AddRecExpr for this PHI node. We can not keep this temporary
4968 // as it will prevent later (possibly simpler) SCEV expressions to be added
4969 // to the ValueExprMap.
4970 eraseValueFromMap(PN);
4971
Sanjoy Das55015d22015-10-02 23:09:44 +00004972 return nullptr;
4973}
4974
Sanjoy Das1cd930b2015-10-03 00:34:19 +00004975// Checks if the SCEV S is available at BB. S is considered available at BB
4976// if S can be materialized at BB without introducing a fault.
4977static bool IsAvailableOnEntry(const Loop *L, DominatorTree &DT, const SCEV *S,
4978 BasicBlock *BB) {
4979 struct CheckAvailable {
4980 bool TraversalDone = false;
4981 bool Available = true;
Sanjoy Das55015d22015-10-02 23:09:44 +00004982
Sanjoy Das1cd930b2015-10-03 00:34:19 +00004983 const Loop *L = nullptr; // The loop BB is in (can be nullptr)
4984 BasicBlock *BB = nullptr;
4985 DominatorTree &DT;
Sanjoy Das55015d22015-10-02 23:09:44 +00004986
Sanjoy Das1cd930b2015-10-03 00:34:19 +00004987 CheckAvailable(const Loop *L, BasicBlock *BB, DominatorTree &DT)
4988 : L(L), BB(BB), DT(DT) {}
Sanjoy Das55015d22015-10-02 23:09:44 +00004989
Sanjoy Das1cd930b2015-10-03 00:34:19 +00004990 bool setUnavailable() {
4991 TraversalDone = true;
4992 Available = false;
Sanjoy Das55015d22015-10-02 23:09:44 +00004993 return false;
Sanjoy Das55015d22015-10-02 23:09:44 +00004994 }
4995
Sanjoy Das1cd930b2015-10-03 00:34:19 +00004996 bool follow(const SCEV *S) {
4997 switch (S->getSCEVType()) {
4998 case scConstant: case scTruncate: case scZeroExtend: case scSignExtend:
4999 case scAddExpr: case scMulExpr: case scUMaxExpr: case scSMaxExpr:
Sanjoy Dasbb5ffc52015-10-24 05:37:28 +00005000 // These expressions are available if their operand(s) is/are.
5001 return true;
Sanjoy Das55015d22015-10-02 23:09:44 +00005002
Sanjoy Das1cd930b2015-10-03 00:34:19 +00005003 case scAddRecExpr: {
5004 // We allow add recurrences that are on the loop BB is in, or some
5005 // outer loop. This guarantees availability because the value of the
5006 // add recurrence at BB is simply the "current" value of the induction
5007 // variable. We can relax this in the future; for instance an add
5008 // recurrence on a sibling dominating loop is also available at BB.
5009 const auto *ARLoop = cast<SCEVAddRecExpr>(S)->getLoop();
5010 if (L && (ARLoop == L || ARLoop->contains(L)))
Sanjoy Das55015d22015-10-02 23:09:44 +00005011 return true;
5012
Sanjoy Das1cd930b2015-10-03 00:34:19 +00005013 return setUnavailable();
Sanjoy Das55015d22015-10-02 23:09:44 +00005014 }
5015
Sanjoy Das1cd930b2015-10-03 00:34:19 +00005016 case scUnknown: {
5017 // For SCEVUnknown, we check for simple dominance.
5018 const auto *SU = cast<SCEVUnknown>(S);
5019 Value *V = SU->getValue();
Sanjoy Das55015d22015-10-02 23:09:44 +00005020
Sanjoy Das1cd930b2015-10-03 00:34:19 +00005021 if (isa<Argument>(V))
5022 return false;
Sanjoy Das55015d22015-10-02 23:09:44 +00005023
Sanjoy Das1cd930b2015-10-03 00:34:19 +00005024 if (isa<Instruction>(V) && DT.dominates(cast<Instruction>(V), BB))
5025 return false;
5026
5027 return setUnavailable();
5028 }
5029
5030 case scUDivExpr:
5031 case scCouldNotCompute:
Sanjoy Dasd295f2c2015-10-18 00:29:27 +00005032 // We do not try to smart about these at all.
5033 return setUnavailable();
Sanjoy Das1cd930b2015-10-03 00:34:19 +00005034 }
5035 llvm_unreachable("switch should be fully covered!");
5036 }
5037
5038 bool isDone() { return TraversalDone; }
Sanjoy Das55015d22015-10-02 23:09:44 +00005039 };
5040
Sanjoy Das1cd930b2015-10-03 00:34:19 +00005041 CheckAvailable CA(L, BB, DT);
5042 SCEVTraversal<CheckAvailable> ST(CA);
5043
5044 ST.visitAll(S);
5045 return CA.Available;
5046}
5047
5048// Try to match a control flow sequence that branches out at BI and merges back
5049// at Merge into a "C ? LHS : RHS" select pattern. Return true on a successful
5050// match.
5051static bool BrPHIToSelect(DominatorTree &DT, BranchInst *BI, PHINode *Merge,
5052 Value *&C, Value *&LHS, Value *&RHS) {
5053 C = BI->getCondition();
5054
5055 BasicBlockEdge LeftEdge(BI->getParent(), BI->getSuccessor(0));
5056 BasicBlockEdge RightEdge(BI->getParent(), BI->getSuccessor(1));
5057
5058 if (!LeftEdge.isSingleEdge())
5059 return false;
5060
5061 assert(RightEdge.isSingleEdge() && "Follows from LeftEdge.isSingleEdge()");
5062
5063 Use &LeftUse = Merge->getOperandUse(0);
5064 Use &RightUse = Merge->getOperandUse(1);
5065
5066 if (DT.dominates(LeftEdge, LeftUse) && DT.dominates(RightEdge, RightUse)) {
5067 LHS = LeftUse;
5068 RHS = RightUse;
5069 return true;
5070 }
5071
5072 if (DT.dominates(LeftEdge, RightUse) && DT.dominates(RightEdge, LeftUse)) {
5073 LHS = RightUse;
5074 RHS = LeftUse;
5075 return true;
5076 }
5077
5078 return false;
5079}
5080
5081const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) {
Sanjoy Dasb0b4e862016-08-05 18:34:14 +00005082 auto IsReachable =
5083 [&](BasicBlock *BB) { return DT.isReachableFromEntry(BB); };
5084 if (PN->getNumIncomingValues() == 2 && all_of(PN->blocks(), IsReachable)) {
Sanjoy Das1cd930b2015-10-03 00:34:19 +00005085 const Loop *L = LI.getLoopFor(PN->getParent());
5086
Sanjoy Das337d4782015-10-31 23:21:40 +00005087 // We don't want to break LCSSA, even in a SCEV expression tree.
5088 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
5089 if (LI.getLoopFor(PN->getIncomingBlock(i)) != L)
5090 return nullptr;
5091
Sanjoy Das55015d22015-10-02 23:09:44 +00005092 // Try to match
5093 //
5094 // br %cond, label %left, label %right
5095 // left:
5096 // br label %merge
5097 // right:
5098 // br label %merge
5099 // merge:
5100 // V = phi [ %x, %left ], [ %y, %right ]
5101 //
5102 // as "select %cond, %x, %y"
5103
5104 BasicBlock *IDom = DT[PN->getParent()]->getIDom()->getBlock();
5105 assert(IDom && "At least the entry block should dominate PN");
5106
5107 auto *BI = dyn_cast<BranchInst>(IDom->getTerminator());
5108 Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr;
5109
Sanjoy Das1cd930b2015-10-03 00:34:19 +00005110 if (BI && BI->isConditional() &&
5111 BrPHIToSelect(DT, BI, PN, Cond, LHS, RHS) &&
5112 IsAvailableOnEntry(L, DT, getSCEV(LHS), PN->getParent()) &&
5113 IsAvailableOnEntry(L, DT, getSCEV(RHS), PN->getParent()))
Sanjoy Das55015d22015-10-02 23:09:44 +00005114 return createNodeForSelectOrPHI(PN, Cond, LHS, RHS);
5115 }
5116
5117 return nullptr;
5118}
5119
5120const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
5121 if (const SCEV *S = createAddRecFromPHI(PN))
5122 return S;
5123
5124 if (const SCEV *S = createNodeFromSelectLikePHI(PN))
5125 return S;
Misha Brukman01808ca2005-04-21 21:13:18 +00005126
Dan Gohmana9c205c2010-02-25 06:57:05 +00005127 // If the PHI has a single incoming value, follow that value, unless the
5128 // PHI's incoming blocks are in a different loop, in which case doing so
5129 // risks breaking LCSSA form. Instcombine would normally zap these, but
5130 // it doesn't have DominatorTree information, so it may miss cases.
Daniel Berlin4d0fe642017-04-28 19:55:38 +00005131 if (Value *V = SimplifyInstruction(PN, {getDataLayout(), &TLI, &DT, &AC}))
Chandler Carruth2f1fd162015-08-17 02:08:17 +00005132 if (LI.replacementPreservesLCSSAForm(PN, V))
Dan Gohmana9c205c2010-02-25 06:57:05 +00005133 return getSCEV(V);
Duncan Sands39d771312010-11-17 20:49:12 +00005134
Chris Lattnerd934c702004-04-02 20:23:17 +00005135 // If it's not a loop phi, we can't handle it yet.
Dan Gohmanc8e23622009-04-21 23:15:49 +00005136 return getUnknown(PN);
Chris Lattnerd934c702004-04-02 20:23:17 +00005137}
5138
Sanjoy Das55015d22015-10-02 23:09:44 +00005139const SCEV *ScalarEvolution::createNodeForSelectOrPHI(Instruction *I,
5140 Value *Cond,
5141 Value *TrueVal,
5142 Value *FalseVal) {
Mehdi Amini044cb342015-10-07 18:14:25 +00005143 // Handle "constant" branch or select. This can occur for instance when a
5144 // loop pass transforms an inner loop and moves on to process the outer loop.
5145 if (auto *CI = dyn_cast<ConstantInt>(Cond))
5146 return getSCEV(CI->isOne() ? TrueVal : FalseVal);
5147
Sanjoy Dasd0671342015-10-02 19:39:59 +00005148 // Try to match some simple smax or umax patterns.
5149 auto *ICI = dyn_cast<ICmpInst>(Cond);
5150 if (!ICI)
5151 return getUnknown(I);
5152
5153 Value *LHS = ICI->getOperand(0);
5154 Value *RHS = ICI->getOperand(1);
5155
5156 switch (ICI->getPredicate()) {
5157 case ICmpInst::ICMP_SLT:
5158 case ICmpInst::ICMP_SLE:
5159 std::swap(LHS, RHS);
Justin Bognercd1d5aa2016-08-17 20:30:52 +00005160 LLVM_FALLTHROUGH;
Sanjoy Dasd0671342015-10-02 19:39:59 +00005161 case ICmpInst::ICMP_SGT:
5162 case ICmpInst::ICMP_SGE:
5163 // a >s b ? a+x : b+x -> smax(a, b)+x
5164 // a >s b ? b+x : a+x -> smin(a, b)+x
5165 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType())) {
5166 const SCEV *LS = getNoopOrSignExtend(getSCEV(LHS), I->getType());
5167 const SCEV *RS = getNoopOrSignExtend(getSCEV(RHS), I->getType());
5168 const SCEV *LA = getSCEV(TrueVal);
5169 const SCEV *RA = getSCEV(FalseVal);
5170 const SCEV *LDiff = getMinusSCEV(LA, LS);
5171 const SCEV *RDiff = getMinusSCEV(RA, RS);
5172 if (LDiff == RDiff)
5173 return getAddExpr(getSMaxExpr(LS, RS), LDiff);
5174 LDiff = getMinusSCEV(LA, RS);
5175 RDiff = getMinusSCEV(RA, LS);
5176 if (LDiff == RDiff)
5177 return getAddExpr(getSMinExpr(LS, RS), LDiff);
5178 }
5179 break;
5180 case ICmpInst::ICMP_ULT:
5181 case ICmpInst::ICMP_ULE:
5182 std::swap(LHS, RHS);
Justin Bognercd1d5aa2016-08-17 20:30:52 +00005183 LLVM_FALLTHROUGH;
Sanjoy Dasd0671342015-10-02 19:39:59 +00005184 case ICmpInst::ICMP_UGT:
5185 case ICmpInst::ICMP_UGE:
5186 // a >u b ? a+x : b+x -> umax(a, b)+x
5187 // a >u b ? b+x : a+x -> umin(a, b)+x
5188 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType())) {
5189 const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType());
5190 const SCEV *RS = getNoopOrZeroExtend(getSCEV(RHS), I->getType());
5191 const SCEV *LA = getSCEV(TrueVal);
5192 const SCEV *RA = getSCEV(FalseVal);
5193 const SCEV *LDiff = getMinusSCEV(LA, LS);
5194 const SCEV *RDiff = getMinusSCEV(RA, RS);
5195 if (LDiff == RDiff)
5196 return getAddExpr(getUMaxExpr(LS, RS), LDiff);
5197 LDiff = getMinusSCEV(LA, RS);
5198 RDiff = getMinusSCEV(RA, LS);
5199 if (LDiff == RDiff)
5200 return getAddExpr(getUMinExpr(LS, RS), LDiff);
5201 }
5202 break;
5203 case ICmpInst::ICMP_NE:
5204 // n != 0 ? n+x : 1+x -> umax(n, 1)+x
5205 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType()) &&
5206 isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero()) {
5207 const SCEV *One = getOne(I->getType());
5208 const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType());
5209 const SCEV *LA = getSCEV(TrueVal);
5210 const SCEV *RA = getSCEV(FalseVal);
5211 const SCEV *LDiff = getMinusSCEV(LA, LS);
5212 const SCEV *RDiff = getMinusSCEV(RA, One);
5213 if (LDiff == RDiff)
5214 return getAddExpr(getUMaxExpr(One, LS), LDiff);
5215 }
5216 break;
5217 case ICmpInst::ICMP_EQ:
5218 // n == 0 ? 1+x : n+x -> umax(n, 1)+x
5219 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType()) &&
5220 isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero()) {
5221 const SCEV *One = getOne(I->getType());
5222 const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType());
5223 const SCEV *LA = getSCEV(TrueVal);
5224 const SCEV *RA = getSCEV(FalseVal);
5225 const SCEV *LDiff = getMinusSCEV(LA, One);
5226 const SCEV *RDiff = getMinusSCEV(RA, LS);
5227 if (LDiff == RDiff)
5228 return getAddExpr(getUMaxExpr(One, LS), LDiff);
5229 }
5230 break;
5231 default:
5232 break;
5233 }
5234
5235 return getUnknown(I);
5236}
5237
Sanjoy Dasf8570812016-05-29 00:38:22 +00005238/// Expand GEP instructions into add and multiply operations. This allows them
5239/// to be analyzed by regular SCEV code.
Dan Gohmanb256ccf2009-12-18 02:09:29 +00005240const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
Dan Gohman30f24fe2009-05-09 00:14:52 +00005241 // Don't attempt to analyze GEPs over unsized objects.
Eduard Burtescu19eb0312016-01-19 17:28:00 +00005242 if (!GEP->getSourceElementType()->isSized())
Dan Gohman30f24fe2009-05-09 00:14:52 +00005243 return getUnknown(GEP);
Matt Arsenault4c265902013-09-27 22:38:23 +00005244
Jingyue Wu2982d4d2015-05-18 17:03:25 +00005245 SmallVector<const SCEV *, 4> IndexExprs;
5246 for (auto Index = GEP->idx_begin(); Index != GEP->idx_end(); ++Index)
5247 IndexExprs.push_back(getSCEV(*Index));
Peter Collingbourne8dff0392016-11-13 06:59:50 +00005248 return getGEPExpr(GEP, IndexExprs);
Dan Gohmanee750d12009-05-08 20:26:55 +00005249}
5250
Igor Laevskyc11c1ed2017-02-14 15:53:12 +00005251uint32_t ScalarEvolution::GetMinTrailingZerosImpl(const SCEV *S) {
Dan Gohmana30370b2009-05-04 22:02:23 +00005252 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
Sanjoy Das0de2fec2015-12-17 20:28:46 +00005253 return C->getAPInt().countTrailingZeros();
Chris Lattner49b090e2006-12-12 02:26:09 +00005254
Dan Gohmana30370b2009-05-04 22:02:23 +00005255 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S))
Dan Gohmanc702fc02009-06-19 23:29:04 +00005256 return std::min(GetMinTrailingZeros(T->getOperand()),
5257 (uint32_t)getTypeSizeInBits(T->getType()));
Nick Lewycky3783b462007-11-22 07:59:40 +00005258
Dan Gohmana30370b2009-05-04 22:02:23 +00005259 if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) {
Dan Gohmanc702fc02009-06-19 23:29:04 +00005260 uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
Igor Laevskyc11c1ed2017-02-14 15:53:12 +00005261 return OpRes == getTypeSizeInBits(E->getOperand()->getType())
5262 ? getTypeSizeInBits(E->getType())
5263 : OpRes;
Nick Lewycky3783b462007-11-22 07:59:40 +00005264 }
5265
Dan Gohmana30370b2009-05-04 22:02:23 +00005266 if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) {
Dan Gohmanc702fc02009-06-19 23:29:04 +00005267 uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
Igor Laevskyc11c1ed2017-02-14 15:53:12 +00005268 return OpRes == getTypeSizeInBits(E->getOperand()->getType())
5269 ? getTypeSizeInBits(E->getType())
5270 : OpRes;
Nick Lewycky3783b462007-11-22 07:59:40 +00005271 }
5272
Dan Gohmana30370b2009-05-04 22:02:23 +00005273 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
Nick Lewycky3783b462007-11-22 07:59:40 +00005274 // The result is the min of all operands results.
Dan Gohmanc702fc02009-06-19 23:29:04 +00005275 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
Nick Lewycky3783b462007-11-22 07:59:40 +00005276 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohmanc702fc02009-06-19 23:29:04 +00005277 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
Nick Lewycky3783b462007-11-22 07:59:40 +00005278 return MinOpRes;
Chris Lattner49b090e2006-12-12 02:26:09 +00005279 }
5280
Dan Gohmana30370b2009-05-04 22:02:23 +00005281 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
Nick Lewycky3783b462007-11-22 07:59:40 +00005282 // The result is the sum of all operands results.
Dan Gohmanc702fc02009-06-19 23:29:04 +00005283 uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0));
5284 uint32_t BitWidth = getTypeSizeInBits(M->getType());
Nick Lewycky3783b462007-11-22 07:59:40 +00005285 for (unsigned i = 1, e = M->getNumOperands();
5286 SumOpRes != BitWidth && i != e; ++i)
Igor Laevskyc11c1ed2017-02-14 15:53:12 +00005287 SumOpRes =
5288 std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), BitWidth);
Nick Lewycky3783b462007-11-22 07:59:40 +00005289 return SumOpRes;
Chris Lattner49b090e2006-12-12 02:26:09 +00005290 }
Nick Lewycky3783b462007-11-22 07:59:40 +00005291
Dan Gohmana30370b2009-05-04 22:02:23 +00005292 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
Nick Lewycky3783b462007-11-22 07:59:40 +00005293 // The result is the min of all operands results.
Dan Gohmanc702fc02009-06-19 23:29:04 +00005294 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
Nick Lewycky3783b462007-11-22 07:59:40 +00005295 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohmanc702fc02009-06-19 23:29:04 +00005296 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
Nick Lewycky3783b462007-11-22 07:59:40 +00005297 return MinOpRes;
Chris Lattner49b090e2006-12-12 02:26:09 +00005298 }
Nick Lewycky3783b462007-11-22 07:59:40 +00005299
Dan Gohmana30370b2009-05-04 22:02:23 +00005300 if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) {
Nick Lewyckycdb7e542007-11-25 22:41:31 +00005301 // The result is the min of all operands results.
Dan Gohmanc702fc02009-06-19 23:29:04 +00005302 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
Nick Lewyckycdb7e542007-11-25 22:41:31 +00005303 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohmanc702fc02009-06-19 23:29:04 +00005304 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
Nick Lewyckycdb7e542007-11-25 22:41:31 +00005305 return MinOpRes;
5306 }
5307
Dan Gohmana30370b2009-05-04 22:02:23 +00005308 if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) {
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00005309 // The result is the min of all operands results.
Dan Gohmanc702fc02009-06-19 23:29:04 +00005310 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00005311 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
Dan Gohmanc702fc02009-06-19 23:29:04 +00005312 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00005313 return MinOpRes;
5314 }
5315
Dan Gohmanc702fc02009-06-19 23:29:04 +00005316 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
5317 // For a SCEVUnknown, ask ValueTracking.
Craig Topper8205a1a2017-05-24 16:53:07 +00005318 KnownBits Known = computeKnownBits(U->getValue(), getDataLayout(), 0, &AC, nullptr, &DT);
Craig Topper8df66c62017-05-12 17:20:30 +00005319 return Known.countMinTrailingZeros();
Dan Gohmanc702fc02009-06-19 23:29:04 +00005320 }
5321
5322 // SCEVUDivExpr
Nick Lewycky3783b462007-11-22 07:59:40 +00005323 return 0;
Chris Lattner49b090e2006-12-12 02:26:09 +00005324}
Chris Lattnerd934c702004-04-02 20:23:17 +00005325
Igor Laevskyc11c1ed2017-02-14 15:53:12 +00005326uint32_t ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
5327 auto I = MinTrailingZerosCache.find(S);
5328 if (I != MinTrailingZerosCache.end())
5329 return I->second;
5330
5331 uint32_t Result = GetMinTrailingZerosImpl(S);
5332 auto InsertPair = MinTrailingZerosCache.insert({S, Result});
5333 assert(InsertPair.second && "Should insert a new key");
5334 return InsertPair.first->second;
5335}
5336
Sanjoy Dasf8570812016-05-29 00:38:22 +00005337/// Helper method to assign a range to V from metadata present in the IR.
Sanjoy Das1f05c512014-10-10 21:22:34 +00005338static Optional<ConstantRange> GetRangeFromMetadata(Value *V) {
Sanjoy Dasa7e13782015-10-24 05:37:35 +00005339 if (Instruction *I = dyn_cast<Instruction>(V))
5340 if (MDNode *MD = I->getMetadata(LLVMContext::MD_range))
5341 return getConstantRangeFromMetadata(*MD);
Sanjoy Das1f05c512014-10-10 21:22:34 +00005342
5343 return None;
5344}
5345
Sanjoy Dasf8570812016-05-29 00:38:22 +00005346/// Determine the range for a particular SCEV. If SignHint is
Sanjoy Das91b54772015-03-09 21:43:43 +00005347/// HINT_RANGE_UNSIGNED (resp. HINT_RANGE_SIGNED) then getRange prefers ranges
5348/// with a "cleaner" unsigned (resp. signed) representation.
Craig Topper01020392017-06-24 23:34:50 +00005349const ConstantRange &
5350ScalarEvolution::getRangeRef(const SCEV *S,
5351 ScalarEvolution::RangeSignHint SignHint) {
Sanjoy Das91b54772015-03-09 21:43:43 +00005352 DenseMap<const SCEV *, ConstantRange> &Cache =
5353 SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges
5354 : SignedRanges;
5355
Dan Gohman761065e2010-11-17 02:44:44 +00005356 // See if we've computed this range already.
Sanjoy Das91b54772015-03-09 21:43:43 +00005357 DenseMap<const SCEV *, ConstantRange>::iterator I = Cache.find(S);
5358 if (I != Cache.end())
Dan Gohman761065e2010-11-17 02:44:44 +00005359 return I->second;
Dan Gohmanc702fc02009-06-19 23:29:04 +00005360
5361 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
Sanjoy Das0de2fec2015-12-17 20:28:46 +00005362 return setRange(C, SignHint, ConstantRange(C->getAPInt()));
Dan Gohmanc702fc02009-06-19 23:29:04 +00005363
Dan Gohman85be4332010-01-26 19:19:05 +00005364 unsigned BitWidth = getTypeSizeInBits(S->getType());
5365 ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true);
5366
Sanjoy Das91b54772015-03-09 21:43:43 +00005367 // If the value has known zeros, the maximum value will have those known zeros
5368 // as well.
Dan Gohman85be4332010-01-26 19:19:05 +00005369 uint32_t TZ = GetMinTrailingZeros(S);
Sanjoy Das91b54772015-03-09 21:43:43 +00005370 if (TZ != 0) {
5371 if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED)
5372 ConservativeResult =
5373 ConstantRange(APInt::getMinValue(BitWidth),
5374 APInt::getMaxValue(BitWidth).lshr(TZ).shl(TZ) + 1);
5375 else
5376 ConservativeResult = ConstantRange(
5377 APInt::getSignedMinValue(BitWidth),
5378 APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1);
5379 }
Dan Gohman85be4332010-01-26 19:19:05 +00005380
Dan Gohmane65c9172009-07-13 21:35:55 +00005381 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
Craig Topper01020392017-06-24 23:34:50 +00005382 ConstantRange X = getRangeRef(Add->getOperand(0), SignHint);
Dan Gohmane65c9172009-07-13 21:35:55 +00005383 for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
Craig Topper01020392017-06-24 23:34:50 +00005384 X = X.add(getRangeRef(Add->getOperand(i), SignHint));
Sanjoy Das91b54772015-03-09 21:43:43 +00005385 return setRange(Add, SignHint, ConservativeResult.intersectWith(X));
Dan Gohmane65c9172009-07-13 21:35:55 +00005386 }
5387
5388 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
Craig Topper01020392017-06-24 23:34:50 +00005389 ConstantRange X = getRangeRef(Mul->getOperand(0), SignHint);
Dan Gohmane65c9172009-07-13 21:35:55 +00005390 for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
Craig Topper01020392017-06-24 23:34:50 +00005391 X = X.multiply(getRangeRef(Mul->getOperand(i), SignHint));
Sanjoy Das91b54772015-03-09 21:43:43 +00005392 return setRange(Mul, SignHint, ConservativeResult.intersectWith(X));
Dan Gohmane65c9172009-07-13 21:35:55 +00005393 }
5394
5395 if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
Craig Topper01020392017-06-24 23:34:50 +00005396 ConstantRange X = getRangeRef(SMax->getOperand(0), SignHint);
Dan Gohmane65c9172009-07-13 21:35:55 +00005397 for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
Craig Topper01020392017-06-24 23:34:50 +00005398 X = X.smax(getRangeRef(SMax->getOperand(i), SignHint));
Sanjoy Das91b54772015-03-09 21:43:43 +00005399 return setRange(SMax, SignHint, ConservativeResult.intersectWith(X));
Dan Gohmane65c9172009-07-13 21:35:55 +00005400 }
5401
5402 if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
Craig Topper01020392017-06-24 23:34:50 +00005403 ConstantRange X = getRangeRef(UMax->getOperand(0), SignHint);
Dan Gohmane65c9172009-07-13 21:35:55 +00005404 for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
Craig Topper01020392017-06-24 23:34:50 +00005405 X = X.umax(getRangeRef(UMax->getOperand(i), SignHint));
Sanjoy Das91b54772015-03-09 21:43:43 +00005406 return setRange(UMax, SignHint, ConservativeResult.intersectWith(X));
Dan Gohmane65c9172009-07-13 21:35:55 +00005407 }
5408
5409 if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
Craig Topper01020392017-06-24 23:34:50 +00005410 ConstantRange X = getRangeRef(UDiv->getLHS(), SignHint);
5411 ConstantRange Y = getRangeRef(UDiv->getRHS(), SignHint);
Sanjoy Das91b54772015-03-09 21:43:43 +00005412 return setRange(UDiv, SignHint,
5413 ConservativeResult.intersectWith(X.udiv(Y)));
Dan Gohmane65c9172009-07-13 21:35:55 +00005414 }
5415
5416 if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
Craig Topper01020392017-06-24 23:34:50 +00005417 ConstantRange X = getRangeRef(ZExt->getOperand(), SignHint);
Sanjoy Das91b54772015-03-09 21:43:43 +00005418 return setRange(ZExt, SignHint,
5419 ConservativeResult.intersectWith(X.zeroExtend(BitWidth)));
Dan Gohmane65c9172009-07-13 21:35:55 +00005420 }
5421
5422 if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
Craig Topper01020392017-06-24 23:34:50 +00005423 ConstantRange X = getRangeRef(SExt->getOperand(), SignHint);
Sanjoy Das91b54772015-03-09 21:43:43 +00005424 return setRange(SExt, SignHint,
5425 ConservativeResult.intersectWith(X.signExtend(BitWidth)));
Dan Gohmane65c9172009-07-13 21:35:55 +00005426 }
5427
5428 if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
Craig Topper01020392017-06-24 23:34:50 +00005429 ConstantRange X = getRangeRef(Trunc->getOperand(), SignHint);
Sanjoy Das91b54772015-03-09 21:43:43 +00005430 return setRange(Trunc, SignHint,
5431 ConservativeResult.intersectWith(X.truncate(BitWidth)));
Dan Gohmane65c9172009-07-13 21:35:55 +00005432 }
5433
Dan Gohmane65c9172009-07-13 21:35:55 +00005434 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
Dan Gohman51ad99d2010-01-21 02:09:26 +00005435 // If there's no unsigned wrap, the value will never be less than its
5436 // initial value.
Sanjoy Das76c48e02016-02-04 18:21:54 +00005437 if (AddRec->hasNoUnsignedWrap())
Dan Gohman51ad99d2010-01-21 02:09:26 +00005438 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(AddRec->getStart()))
Dan Gohmanebbd05f2010-04-12 23:08:18 +00005439 if (!C->getValue()->isZero())
Sanjoy Das0de2fec2015-12-17 20:28:46 +00005440 ConservativeResult = ConservativeResult.intersectWith(
5441 ConstantRange(C->getAPInt(), APInt(BitWidth, 0)));
Dan Gohmane65c9172009-07-13 21:35:55 +00005442
Dan Gohman51ad99d2010-01-21 02:09:26 +00005443 // If there's no signed wrap, and all the operands have the same sign or
5444 // zero, the value won't ever change sign.
Sanjoy Das76c48e02016-02-04 18:21:54 +00005445 if (AddRec->hasNoSignedWrap()) {
Dan Gohman51ad99d2010-01-21 02:09:26 +00005446 bool AllNonNeg = true;
5447 bool AllNonPos = true;
5448 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
5449 if (!isKnownNonNegative(AddRec->getOperand(i))) AllNonNeg = false;
5450 if (!isKnownNonPositive(AddRec->getOperand(i))) AllNonPos = false;
5451 }
Dan Gohman51ad99d2010-01-21 02:09:26 +00005452 if (AllNonNeg)
Dan Gohman51aaf022010-01-26 04:40:18 +00005453 ConservativeResult = ConservativeResult.intersectWith(
5454 ConstantRange(APInt(BitWidth, 0),
5455 APInt::getSignedMinValue(BitWidth)));
Dan Gohman51ad99d2010-01-21 02:09:26 +00005456 else if (AllNonPos)
Dan Gohman51aaf022010-01-26 04:40:18 +00005457 ConservativeResult = ConservativeResult.intersectWith(
5458 ConstantRange(APInt::getSignedMinValue(BitWidth),
5459 APInt(BitWidth, 1)));
Dan Gohman51ad99d2010-01-21 02:09:26 +00005460 }
Dan Gohmane65c9172009-07-13 21:35:55 +00005461
5462 // TODO: non-affine addrec
Dan Gohman85be4332010-01-26 19:19:05 +00005463 if (AddRec->isAffine()) {
Dan Gohmane65c9172009-07-13 21:35:55 +00005464 const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop());
Dan Gohman85be4332010-01-26 19:19:05 +00005465 if (!isa<SCEVCouldNotCompute>(MaxBECount) &&
5466 getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) {
Sanjoy Dasb765b632016-03-02 00:57:39 +00005467 auto RangeFromAffine = getRangeForAffineAR(
5468 AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount,
5469 BitWidth);
5470 if (!RangeFromAffine.isFullSet())
5471 ConservativeResult =
5472 ConservativeResult.intersectWith(RangeFromAffine);
Sanjoy Dasbf730982016-03-02 00:57:54 +00005473
5474 auto RangeFromFactoring = getRangeViaFactoring(
5475 AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount,
5476 BitWidth);
5477 if (!RangeFromFactoring.isFullSet())
5478 ConservativeResult =
5479 ConservativeResult.intersectWith(RangeFromFactoring);
Dan Gohmand261d272009-06-24 01:05:09 +00005480 }
Dan Gohmand261d272009-06-24 01:05:09 +00005481 }
Dan Gohman51ad99d2010-01-21 02:09:26 +00005482
Craig Topper252682a2017-05-07 16:28:17 +00005483 return setRange(AddRec, SignHint, std::move(ConservativeResult));
Dan Gohmand261d272009-06-24 01:05:09 +00005484 }
5485
Dan Gohmanc702fc02009-06-19 23:29:04 +00005486 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
Sanjoy Das1f05c512014-10-10 21:22:34 +00005487 // Check if the IR explicitly contains !range metadata.
5488 Optional<ConstantRange> MDRange = GetRangeFromMetadata(U->getValue());
5489 if (MDRange.hasValue())
5490 ConservativeResult = ConservativeResult.intersectWith(MDRange.getValue());
5491
Sanjoy Das91b54772015-03-09 21:43:43 +00005492 // Split here to avoid paying the compile-time cost of calling both
5493 // computeKnownBits and ComputeNumSignBits. This restriction can be lifted
5494 // if needed.
Sanjoy Das49edd3b2015-10-27 00:52:09 +00005495 const DataLayout &DL = getDataLayout();
Sanjoy Das91b54772015-03-09 21:43:43 +00005496 if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED) {
5497 // For a SCEVUnknown, ask ValueTracking.
Craig Topper8205a1a2017-05-24 16:53:07 +00005498 KnownBits Known = computeKnownBits(U->getValue(), DL, 0, &AC, nullptr, &DT);
Craig Topperb45eabc2017-04-26 16:39:58 +00005499 if (Known.One != ~Known.Zero + 1)
Sanjoy Das91b54772015-03-09 21:43:43 +00005500 ConservativeResult =
Craig Topperb45eabc2017-04-26 16:39:58 +00005501 ConservativeResult.intersectWith(ConstantRange(Known.One,
5502 ~Known.Zero + 1));
Sanjoy Das91b54772015-03-09 21:43:43 +00005503 } else {
5504 assert(SignHint == ScalarEvolution::HINT_RANGE_SIGNED &&
5505 "generalize as needed!");
Daniel Jasperaec2fa32016-12-19 08:22:17 +00005506 unsigned NS = ComputeNumSignBits(U->getValue(), DL, 0, &AC, nullptr, &DT);
Mehdi Aminia28d91d2015-03-10 02:37:25 +00005507 if (NS > 1)
5508 ConservativeResult = ConservativeResult.intersectWith(
5509 ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1),
5510 APInt::getSignedMaxValue(BitWidth).ashr(NS - 1) + 1));
Sanjoy Das91b54772015-03-09 21:43:43 +00005511 }
5512
Craig Topper252682a2017-05-07 16:28:17 +00005513 return setRange(U, SignHint, std::move(ConservativeResult));
Dan Gohmanc702fc02009-06-19 23:29:04 +00005514 }
5515
Craig Topper252682a2017-05-07 16:28:17 +00005516 return setRange(S, SignHint, std::move(ConservativeResult));
Dan Gohmanc702fc02009-06-19 23:29:04 +00005517}
5518
Michael Zolotukhin99de88d2017-03-16 21:07:38 +00005519// Given a StartRange, Step and MaxBECount for an expression compute a range of
5520// values that the expression can take. Initially, the expression has a value
5521// from StartRange and then is changed by Step up to MaxBECount times. Signed
5522// argument defines if we treat Step as signed or unsigned.
5523static ConstantRange getRangeForAffineARHelper(APInt Step,
Craig Topperd6f26392017-05-08 02:29:15 +00005524 const ConstantRange &StartRange,
Craig Topper6c5e22a2017-05-06 06:03:07 +00005525 const APInt &MaxBECount,
Michael Zolotukhin99de88d2017-03-16 21:07:38 +00005526 unsigned BitWidth, bool Signed) {
5527 // If either Step or MaxBECount is 0, then the expression won't change, and we
5528 // just need to return the initial range.
5529 if (Step == 0 || MaxBECount == 0)
5530 return StartRange;
5531
Simon Pilgrim6bdc7552017-03-31 10:59:37 +00005532 // If we don't know anything about the initial value (i.e. StartRange is
Michael Zolotukhin99de88d2017-03-16 21:07:38 +00005533 // FullRange), then we don't know anything about the final range either.
5534 // Return FullRange.
5535 if (StartRange.isFullSet())
5536 return ConstantRange(BitWidth, /* isFullSet = */ true);
5537
5538 // If Step is signed and negative, then we use its absolute value, but we also
5539 // note that we're moving in the opposite direction.
5540 bool Descending = Signed && Step.isNegative();
5541
5542 if (Signed)
5543 // This is correct even for INT_SMIN. Let's look at i8 to illustrate this:
5544 // abs(INT_SMIN) = abs(-128) = abs(0x80) = -0x80 = 0x80 = 128.
5545 // This equations hold true due to the well-defined wrap-around behavior of
5546 // APInt.
5547 Step = Step.abs();
5548
5549 // Check if Offset is more than full span of BitWidth. If it is, the
5550 // expression is guaranteed to overflow.
5551 if (APInt::getMaxValue(StartRange.getBitWidth()).udiv(Step).ult(MaxBECount))
5552 return ConstantRange(BitWidth, /* isFullSet = */ true);
5553
5554 // Offset is by how much the expression can change. Checks above guarantee no
5555 // overflow here.
5556 APInt Offset = Step * MaxBECount;
5557
5558 // Minimum value of the final range will match the minimal value of StartRange
5559 // if the expression is increasing and will be decreased by Offset otherwise.
5560 // Maximum value of the final range will match the maximal value of StartRange
5561 // if the expression is decreasing and will be increased by Offset otherwise.
5562 APInt StartLower = StartRange.getLower();
5563 APInt StartUpper = StartRange.getUpper() - 1;
Craig Topper6c5e22a2017-05-06 06:03:07 +00005564 APInt MovedBoundary = Descending ? (StartLower - std::move(Offset))
5565 : (StartUpper + std::move(Offset));
Michael Zolotukhin99de88d2017-03-16 21:07:38 +00005566
5567 // It's possible that the new minimum/maximum value will fall into the initial
5568 // range (due to wrap around). This means that the expression can take any
5569 // value in this bitwidth, and we have to return full range.
5570 if (StartRange.contains(MovedBoundary))
5571 return ConstantRange(BitWidth, /* isFullSet = */ true);
5572
Craig Topper6c5e22a2017-05-06 06:03:07 +00005573 APInt NewLower =
5574 Descending ? std::move(MovedBoundary) : std::move(StartLower);
5575 APInt NewUpper =
5576 Descending ? std::move(StartUpper) : std::move(MovedBoundary);
5577 NewUpper += 1;
Michael Zolotukhin99de88d2017-03-16 21:07:38 +00005578
5579 // If we end up with full range, return a proper full range.
Craig Topper6c5e22a2017-05-06 06:03:07 +00005580 if (NewLower == NewUpper)
Michael Zolotukhin99de88d2017-03-16 21:07:38 +00005581 return ConstantRange(BitWidth, /* isFullSet = */ true);
5582
5583 // No overflow detected, return [StartLower, StartUpper + Offset + 1) range.
Craig Topper6c5e22a2017-05-06 06:03:07 +00005584 return ConstantRange(std::move(NewLower), std::move(NewUpper));
Michael Zolotukhin99de88d2017-03-16 21:07:38 +00005585}
5586
Sanjoy Dasb765b632016-03-02 00:57:39 +00005587ConstantRange ScalarEvolution::getRangeForAffineAR(const SCEV *Start,
5588 const SCEV *Step,
5589 const SCEV *MaxBECount,
5590 unsigned BitWidth) {
5591 assert(!isa<SCEVCouldNotCompute>(MaxBECount) &&
5592 getTypeSizeInBits(MaxBECount->getType()) <= BitWidth &&
5593 "Precondition!");
5594
Sanjoy Dasb765b632016-03-02 00:57:39 +00005595 MaxBECount = getNoopOrZeroExtend(MaxBECount, Start->getType());
Craig Topper01020392017-06-24 23:34:50 +00005596 APInt MaxBECountValue = getUnsignedRangeMax(MaxBECount);
Sanjoy Dasb765b632016-03-02 00:57:39 +00005597
Michael Zolotukhin99de88d2017-03-16 21:07:38 +00005598 // First, consider step signed.
Sanjoy Dasb765b632016-03-02 00:57:39 +00005599 ConstantRange StartSRange = getSignedRange(Start);
Michael Zolotukhin99de88d2017-03-16 21:07:38 +00005600 ConstantRange StepSRange = getSignedRange(Step);
Sanjoy Dasb765b632016-03-02 00:57:39 +00005601
Michael Zolotukhin99de88d2017-03-16 21:07:38 +00005602 // If Step can be both positive and negative, we need to find ranges for the
5603 // maximum absolute step values in both directions and union them.
5604 ConstantRange SR =
5605 getRangeForAffineARHelper(StepSRange.getSignedMin(), StartSRange,
5606 MaxBECountValue, BitWidth, /* Signed = */ true);
5607 SR = SR.unionWith(getRangeForAffineARHelper(StepSRange.getSignedMax(),
5608 StartSRange, MaxBECountValue,
5609 BitWidth, /* Signed = */ true));
Sanjoy Dasb765b632016-03-02 00:57:39 +00005610
Michael Zolotukhin99de88d2017-03-16 21:07:38 +00005611 // Next, consider step unsigned.
5612 ConstantRange UR = getRangeForAffineARHelper(
Craig Topper01020392017-06-24 23:34:50 +00005613 getUnsignedRangeMax(Step), getUnsignedRange(Start),
Michael Zolotukhin99de88d2017-03-16 21:07:38 +00005614 MaxBECountValue, BitWidth, /* Signed = */ false);
5615
5616 // Finally, intersect signed and unsigned ranges.
5617 return SR.intersectWith(UR);
Sanjoy Dasb765b632016-03-02 00:57:39 +00005618}
5619
Sanjoy Dasbf730982016-03-02 00:57:54 +00005620ConstantRange ScalarEvolution::getRangeViaFactoring(const SCEV *Start,
5621 const SCEV *Step,
5622 const SCEV *MaxBECount,
5623 unsigned BitWidth) {
Sanjoy Dasbf730982016-03-02 00:57:54 +00005624 // RangeOf({C?A:B,+,C?P:Q}) == RangeOf(C?{A,+,P}:{B,+,Q})
5625 // == RangeOf({A,+,P}) union RangeOf({B,+,Q})
5626
5627 struct SelectPattern {
5628 Value *Condition = nullptr;
Sanjoy Dasd3488c62016-03-09 01:50:57 +00005629 APInt TrueValue;
5630 APInt FalseValue;
Sanjoy Dasbf730982016-03-02 00:57:54 +00005631
Sanjoy Dasd3488c62016-03-09 01:50:57 +00005632 explicit SelectPattern(ScalarEvolution &SE, unsigned BitWidth,
5633 const SCEV *S) {
5634 Optional<unsigned> CastOp;
Sanjoy Das97d19bd2016-03-09 01:51:02 +00005635 APInt Offset(BitWidth, 0);
Sanjoy Dasd3488c62016-03-09 01:50:57 +00005636
5637 assert(SE.getTypeSizeInBits(S->getType()) == BitWidth &&
5638 "Should be!");
5639
Sanjoy Das97d19bd2016-03-09 01:51:02 +00005640 // Peel off a constant offset:
5641 if (auto *SA = dyn_cast<SCEVAddExpr>(S)) {
5642 // In the future we could consider being smarter here and handle
5643 // {Start+Step,+,Step} too.
5644 if (SA->getNumOperands() != 2 || !isa<SCEVConstant>(SA->getOperand(0)))
5645 return;
5646
5647 Offset = cast<SCEVConstant>(SA->getOperand(0))->getAPInt();
5648 S = SA->getOperand(1);
5649 }
5650
Sanjoy Dasd3488c62016-03-09 01:50:57 +00005651 // Peel off a cast operation
5652 if (auto *SCast = dyn_cast<SCEVCastExpr>(S)) {
5653 CastOp = SCast->getSCEVType();
5654 S = SCast->getOperand();
5655 }
5656
Sanjoy Dasbf730982016-03-02 00:57:54 +00005657 using namespace llvm::PatternMatch;
5658
Sanjoy Dasd3488c62016-03-09 01:50:57 +00005659 auto *SU = dyn_cast<SCEVUnknown>(S);
5660 const APInt *TrueVal, *FalseVal;
5661 if (!SU ||
5662 !match(SU->getValue(), m_Select(m_Value(Condition), m_APInt(TrueVal),
5663 m_APInt(FalseVal)))) {
Sanjoy Dasbf730982016-03-02 00:57:54 +00005664 Condition = nullptr;
Sanjoy Dasd3488c62016-03-09 01:50:57 +00005665 return;
Sanjoy Dasbf730982016-03-02 00:57:54 +00005666 }
Sanjoy Dasd3488c62016-03-09 01:50:57 +00005667
5668 TrueValue = *TrueVal;
5669 FalseValue = *FalseVal;
5670
5671 // Re-apply the cast we peeled off earlier
5672 if (CastOp.hasValue())
5673 switch (*CastOp) {
5674 default:
5675 llvm_unreachable("Unknown SCEV cast type!");
5676
5677 case scTruncate:
5678 TrueValue = TrueValue.trunc(BitWidth);
5679 FalseValue = FalseValue.trunc(BitWidth);
5680 break;
5681 case scZeroExtend:
5682 TrueValue = TrueValue.zext(BitWidth);
5683 FalseValue = FalseValue.zext(BitWidth);
5684 break;
5685 case scSignExtend:
5686 TrueValue = TrueValue.sext(BitWidth);
5687 FalseValue = FalseValue.sext(BitWidth);
5688 break;
5689 }
Sanjoy Das97d19bd2016-03-09 01:51:02 +00005690
5691 // Re-apply the constant offset we peeled off earlier
5692 TrueValue += Offset;
5693 FalseValue += Offset;
Sanjoy Dasbf730982016-03-02 00:57:54 +00005694 }
5695
Sanjoy Dasd3488c62016-03-09 01:50:57 +00005696 bool isRecognized() { return Condition != nullptr; }
Sanjoy Dasbf730982016-03-02 00:57:54 +00005697 };
5698
Sanjoy Dasd3488c62016-03-09 01:50:57 +00005699 SelectPattern StartPattern(*this, BitWidth, Start);
Sanjoy Dasbf730982016-03-02 00:57:54 +00005700 if (!StartPattern.isRecognized())
5701 return ConstantRange(BitWidth, /* isFullSet = */ true);
5702
Sanjoy Dasd3488c62016-03-09 01:50:57 +00005703 SelectPattern StepPattern(*this, BitWidth, Step);
Sanjoy Dasbf730982016-03-02 00:57:54 +00005704 if (!StepPattern.isRecognized())
5705 return ConstantRange(BitWidth, /* isFullSet = */ true);
5706
5707 if (StartPattern.Condition != StepPattern.Condition) {
5708 // We don't handle this case today; but we could, by considering four
5709 // possibilities below instead of two. I'm not sure if there are cases where
5710 // that will help over what getRange already does, though.
5711 return ConstantRange(BitWidth, /* isFullSet = */ true);
5712 }
5713
5714 // NB! Calling ScalarEvolution::getConstant is fine, but we should not try to
5715 // construct arbitrary general SCEV expressions here. This function is called
5716 // from deep in the call stack, and calling getSCEV (on a sext instruction,
5717 // say) can end up caching a suboptimal value.
5718
Sanjoy Das6b017a12016-03-02 02:56:29 +00005719 // FIXME: without the explicit `this` receiver below, MSVC errors out with
5720 // C2352 and C2512 (otherwise it isn't needed).
5721
Sanjoy Das97d19bd2016-03-09 01:51:02 +00005722 const SCEV *TrueStart = this->getConstant(StartPattern.TrueValue);
Sanjoy Dasd3488c62016-03-09 01:50:57 +00005723 const SCEV *TrueStep = this->getConstant(StepPattern.TrueValue);
Sanjoy Das97d19bd2016-03-09 01:51:02 +00005724 const SCEV *FalseStart = this->getConstant(StartPattern.FalseValue);
Sanjoy Dasd3488c62016-03-09 01:50:57 +00005725 const SCEV *FalseStep = this->getConstant(StepPattern.FalseValue);
Sanjoy Das62a1c332016-03-02 02:15:42 +00005726
Sanjoy Das1168f932016-03-02 02:34:20 +00005727 ConstantRange TrueRange =
Sanjoy Daseca1b532016-03-02 02:44:08 +00005728 this->getRangeForAffineAR(TrueStart, TrueStep, MaxBECount, BitWidth);
Sanjoy Das1168f932016-03-02 02:34:20 +00005729 ConstantRange FalseRange =
Sanjoy Daseca1b532016-03-02 02:44:08 +00005730 this->getRangeForAffineAR(FalseStart, FalseStep, MaxBECount, BitWidth);
Sanjoy Dasbf730982016-03-02 00:57:54 +00005731
5732 return TrueRange.unionWith(FalseRange);
5733}
5734
Jingyue Wu42f1d672015-07-28 18:22:40 +00005735SCEV::NoWrapFlags ScalarEvolution::getNoWrapFlagsFromUB(const Value *V) {
Bjarke Hammersholt Roune9791ed42015-08-14 22:45:26 +00005736 if (isa<ConstantExpr>(V)) return SCEV::FlagAnyWrap;
Jingyue Wu42f1d672015-07-28 18:22:40 +00005737 const BinaryOperator *BinOp = cast<BinaryOperator>(V);
5738
5739 // Return early if there are no flags to propagate to the SCEV.
5740 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
5741 if (BinOp->hasNoUnsignedWrap())
5742 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
5743 if (BinOp->hasNoSignedWrap())
5744 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW);
Sanjoy Dasdcd3a882016-03-02 04:52:22 +00005745 if (Flags == SCEV::FlagAnyWrap)
Jingyue Wu42f1d672015-07-28 18:22:40 +00005746 return SCEV::FlagAnyWrap;
Jingyue Wu42f1d672015-07-28 18:22:40 +00005747
Sanjoy Dasefdeb452016-04-22 05:38:54 +00005748 return isSCEVExprNeverPoison(BinOp) ? Flags : SCEV::FlagAnyWrap;
5749}
5750
5751bool ScalarEvolution::isSCEVExprNeverPoison(const Instruction *I) {
5752 // Here we check that I is in the header of the innermost loop containing I,
5753 // since we only deal with instructions in the loop header. The actual loop we
5754 // need to check later will come from an add recurrence, but getting that
5755 // requires computing the SCEV of the operands, which can be expensive. This
5756 // check we can do cheaply to rule out some cases early.
5757 Loop *InnermostContainingLoop = LI.getLoopFor(I->getParent());
Sanjoy Dasdcd3a882016-03-02 04:52:22 +00005758 if (InnermostContainingLoop == nullptr ||
Sanjoy Dasefdeb452016-04-22 05:38:54 +00005759 InnermostContainingLoop->getHeader() != I->getParent())
5760 return false;
Jingyue Wu42f1d672015-07-28 18:22:40 +00005761
Sanjoy Dasefdeb452016-04-22 05:38:54 +00005762 // Only proceed if we can prove that I does not yield poison.
Sanjoy Das08989c72017-04-30 19:41:19 +00005763 if (!programUndefinedIfFullPoison(I))
5764 return false;
Jingyue Wu42f1d672015-07-28 18:22:40 +00005765
Sanjoy Dasefdeb452016-04-22 05:38:54 +00005766 // At this point we know that if I is executed, then it does not wrap
5767 // according to at least one of NSW or NUW. If I is not executed, then we do
5768 // not know if the calculation that I represents would wrap. Multiple
5769 // instructions can map to the same SCEV. If we apply NSW or NUW from I to
Jingyue Wu42f1d672015-07-28 18:22:40 +00005770 // the SCEV, we must guarantee no wrapping for that SCEV also when it is
5771 // derived from other instructions that map to the same SCEV. We cannot make
Sanjoy Dasefdeb452016-04-22 05:38:54 +00005772 // that guarantee for cases where I is not executed. So we need to find the
5773 // loop that I is considered in relation to and prove that I is executed for
5774 // every iteration of that loop. That implies that the value that I
Jingyue Wu42f1d672015-07-28 18:22:40 +00005775 // calculates does not wrap anywhere in the loop, so then we can apply the
5776 // flags to the SCEV.
5777 //
Sanjoy Dasefdeb452016-04-22 05:38:54 +00005778 // We check isLoopInvariant to disambiguate in case we are adding recurrences
5779 // from different loops, so that we know which loop to prove that I is
5780 // executed in.
5781 for (unsigned OpIndex = 0; OpIndex < I->getNumOperands(); ++OpIndex) {
Hans Wennborg38790352016-08-17 22:50:18 +00005782 // I could be an extractvalue from a call to an overflow intrinsic.
5783 // TODO: We can do better here in some cases.
5784 if (!isSCEVable(I->getOperand(OpIndex)->getType()))
5785 return false;
Sanjoy Dasefdeb452016-04-22 05:38:54 +00005786 const SCEV *Op = getSCEV(I->getOperand(OpIndex));
Jingyue Wu42f1d672015-07-28 18:22:40 +00005787 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
Sanjoy Dasefdeb452016-04-22 05:38:54 +00005788 bool AllOtherOpsLoopInvariant = true;
5789 for (unsigned OtherOpIndex = 0; OtherOpIndex < I->getNumOperands();
5790 ++OtherOpIndex) {
5791 if (OtherOpIndex != OpIndex) {
5792 const SCEV *OtherOp = getSCEV(I->getOperand(OtherOpIndex));
5793 if (!isLoopInvariant(OtherOp, AddRec->getLoop())) {
5794 AllOtherOpsLoopInvariant = false;
5795 break;
5796 }
5797 }
5798 }
5799 if (AllOtherOpsLoopInvariant &&
5800 isGuaranteedToExecuteForEveryIteration(I, AddRec->getLoop()))
5801 return true;
Jingyue Wu42f1d672015-07-28 18:22:40 +00005802 }
5803 }
Sanjoy Dasefdeb452016-04-22 05:38:54 +00005804 return false;
Jingyue Wu42f1d672015-07-28 18:22:40 +00005805}
5806
Sanjoy Das7e4a6412016-05-29 00:32:17 +00005807bool ScalarEvolution::isAddRecNeverPoison(const Instruction *I, const Loop *L) {
5808 // If we know that \c I can never be poison period, then that's enough.
5809 if (isSCEVExprNeverPoison(I))
5810 return true;
5811
5812 // For an add recurrence specifically, we assume that infinite loops without
5813 // side effects are undefined behavior, and then reason as follows:
5814 //
5815 // If the add recurrence is poison in any iteration, it is poison on all
5816 // future iterations (since incrementing poison yields poison). If the result
5817 // of the add recurrence is fed into the loop latch condition and the loop
5818 // does not contain any throws or exiting blocks other than the latch, we now
5819 // have the ability to "choose" whether the backedge is taken or not (by
5820 // choosing a sufficiently evil value for the poison feeding into the branch)
5821 // for every iteration including and after the one in which \p I first became
5822 // poison. There are two possibilities (let's call the iteration in which \p
5823 // I first became poison as K):
5824 //
5825 // 1. In the set of iterations including and after K, the loop body executes
5826 // no side effects. In this case executing the backege an infinte number
5827 // of times will yield undefined behavior.
5828 //
5829 // 2. In the set of iterations including and after K, the loop body executes
5830 // at least one side effect. In this case, that specific instance of side
5831 // effect is control dependent on poison, which also yields undefined
5832 // behavior.
5833
5834 auto *ExitingBB = L->getExitingBlock();
5835 auto *LatchBB = L->getLoopLatch();
5836 if (!ExitingBB || !LatchBB || ExitingBB != LatchBB)
5837 return false;
5838
5839 SmallPtrSet<const Instruction *, 16> Pushed;
Sanjoy Dasa19edc42016-06-08 17:48:31 +00005840 SmallVector<const Instruction *, 8> PoisonStack;
Sanjoy Das7e4a6412016-05-29 00:32:17 +00005841
Sanjoy Dasa19edc42016-06-08 17:48:31 +00005842 // We start by assuming \c I, the post-inc add recurrence, is poison. Only
5843 // things that are known to be fully poison under that assumption go on the
5844 // PoisonStack.
Sanjoy Das7e4a6412016-05-29 00:32:17 +00005845 Pushed.insert(I);
Sanjoy Dasa19edc42016-06-08 17:48:31 +00005846 PoisonStack.push_back(I);
Sanjoy Das7e4a6412016-05-29 00:32:17 +00005847
5848 bool LatchControlDependentOnPoison = false;
Sanjoy Das2401c982016-06-08 17:48:46 +00005849 while (!PoisonStack.empty() && !LatchControlDependentOnPoison) {
Sanjoy Dasa19edc42016-06-08 17:48:31 +00005850 const Instruction *Poison = PoisonStack.pop_back_val();
Sanjoy Das7e4a6412016-05-29 00:32:17 +00005851
Sanjoy Dasa19edc42016-06-08 17:48:31 +00005852 for (auto *PoisonUser : Poison->users()) {
5853 if (propagatesFullPoison(cast<Instruction>(PoisonUser))) {
5854 if (Pushed.insert(cast<Instruction>(PoisonUser)).second)
5855 PoisonStack.push_back(cast<Instruction>(PoisonUser));
5856 } else if (auto *BI = dyn_cast<BranchInst>(PoisonUser)) {
Sanjoy Das7e4a6412016-05-29 00:32:17 +00005857 assert(BI->isConditional() && "Only possibility!");
5858 if (BI->getParent() == LatchBB) {
5859 LatchControlDependentOnPoison = true;
5860 break;
5861 }
5862 }
5863 }
5864 }
5865
Sanjoy Das97cd7d52016-06-09 01:13:54 +00005866 return LatchControlDependentOnPoison && loopHasNoAbnormalExits(L);
5867}
Sanjoy Das7e4a6412016-05-29 00:32:17 +00005868
Sanjoy Das5603fc02016-09-26 02:44:07 +00005869ScalarEvolution::LoopProperties
5870ScalarEvolution::getLoopProperties(const Loop *L) {
Eugene Zelenkobe709f22017-08-18 23:51:26 +00005871 using LoopProperties = ScalarEvolution::LoopProperties;
David L Kreitzer8bbabee2016-09-16 14:38:13 +00005872
Sanjoy Das5603fc02016-09-26 02:44:07 +00005873 auto Itr = LoopPropertiesCache.find(L);
5874 if (Itr == LoopPropertiesCache.end()) {
5875 auto HasSideEffects = [](Instruction *I) {
5876 if (auto *SI = dyn_cast<StoreInst>(I))
5877 return !SI->isSimple();
5878
5879 return I->mayHaveSideEffects();
David L Kreitzer8bbabee2016-09-16 14:38:13 +00005880 };
5881
Sanjoy Das5603fc02016-09-26 02:44:07 +00005882 LoopProperties LP = {/* HasNoAbnormalExits */ true,
5883 /*HasNoSideEffects*/ true};
David L Kreitzer8bbabee2016-09-16 14:38:13 +00005884
Sanjoy Das5603fc02016-09-26 02:44:07 +00005885 for (auto *BB : L->getBlocks())
5886 for (auto &I : *BB) {
5887 if (!isGuaranteedToTransferExecutionToSuccessor(&I))
5888 LP.HasNoAbnormalExits = false;
5889 if (HasSideEffects(&I))
5890 LP.HasNoSideEffects = false;
5891 if (!LP.HasNoAbnormalExits && !LP.HasNoSideEffects)
5892 break; // We're already as pessimistic as we can get.
5893 }
David L Kreitzer8bbabee2016-09-16 14:38:13 +00005894
Sanjoy Das5603fc02016-09-26 02:44:07 +00005895 auto InsertPair = LoopPropertiesCache.insert({L, LP});
Sanjoy Das7e4a6412016-05-29 00:32:17 +00005896 assert(InsertPair.second && "We just checked!");
5897 Itr = InsertPair.first;
5898 }
5899
Sanjoy Das97cd7d52016-06-09 01:13:54 +00005900 return Itr->second;
Sanjoy Das7e4a6412016-05-29 00:32:17 +00005901}
5902
Dan Gohmanaf752342009-07-07 17:06:11 +00005903const SCEV *ScalarEvolution::createSCEV(Value *V) {
Dan Gohmanb397e1a2009-04-21 01:07:12 +00005904 if (!isSCEVable(V->getType()))
Dan Gohmanc8e23622009-04-21 23:15:49 +00005905 return getUnknown(V);
Dan Gohman0a40ad92009-04-16 03:18:22 +00005906
Dan Gohman69451a02010-03-09 23:46:50 +00005907 if (Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohman69451a02010-03-09 23:46:50 +00005908 // Don't attempt to analyze instructions in blocks that aren't
5909 // reachable. Such instructions don't matter, and they aren't required
5910 // to obey basic rules for definitions dominating uses which this
5911 // analysis depends on.
Chandler Carruth2f1fd162015-08-17 02:08:17 +00005912 if (!DT.isReachableFromEntry(I->getParent()))
Dan Gohman69451a02010-03-09 23:46:50 +00005913 return getUnknown(V);
Sanjoy Das260ad4d2016-03-29 16:40:39 +00005914 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
Dan Gohmanf436bac2009-06-24 00:54:57 +00005915 return getConstant(CI);
5916 else if (isa<ConstantPointerNull>(V))
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00005917 return getZero(V->getType());
Dan Gohmanf161e06e2009-08-25 17:49:57 +00005918 else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
Sanjoy Das5ce32722016-04-08 00:48:30 +00005919 return GA->isInterposable() ? getUnknown(V) : getSCEV(GA->getAliasee());
Sanjoy Das260ad4d2016-03-29 16:40:39 +00005920 else if (!isa<ConstantExpr>(V))
Dan Gohmanc8e23622009-04-21 23:15:49 +00005921 return getUnknown(V);
Chris Lattnera3e0bb42007-04-02 05:41:38 +00005922
Dan Gohman80ca01c2009-07-17 20:47:02 +00005923 Operator *U = cast<Operator>(V);
Sanjoy Dasf49ca522016-05-29 00:34:42 +00005924 if (auto BO = MatchBinaryOp(U, DT)) {
Sanjoy Das2381fcd2016-03-29 16:40:44 +00005925 switch (BO->Opcode) {
5926 case Instruction::Add: {
5927 // The simple thing to do would be to just call getSCEV on both operands
5928 // and call getAddExpr with the result. However if we're looking at a
5929 // bunch of things all added together, this can be quite inefficient,
5930 // because it leads to N-1 getAddExpr calls for N ultimate operands.
5931 // Instead, gather up all the operands and make a single getAddExpr call.
5932 // LLVM IR canonical form means we need only traverse the left operands.
5933 SmallVector<const SCEV *, 4> AddOps;
5934 do {
5935 if (BO->Op) {
5936 if (auto *OpSCEV = getExistingSCEV(BO->Op)) {
5937 AddOps.push_back(OpSCEV);
5938 break;
5939 }
Jingyue Wu42f1d672015-07-28 18:22:40 +00005940
Sanjoy Das2381fcd2016-03-29 16:40:44 +00005941 // If a NUW or NSW flag can be applied to the SCEV for this
5942 // addition, then compute the SCEV for this addition by itself
5943 // with a separate call to getAddExpr. We need to do that
5944 // instead of pushing the operands of the addition onto AddOps,
5945 // since the flags are only known to apply to this particular
5946 // addition - they may not apply to other additions that can be
5947 // formed with operands from AddOps.
5948 const SCEV *RHS = getSCEV(BO->RHS);
5949 SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op);
5950 if (Flags != SCEV::FlagAnyWrap) {
5951 const SCEV *LHS = getSCEV(BO->LHS);
5952 if (BO->Opcode == Instruction::Sub)
5953 AddOps.push_back(getMinusSCEV(LHS, RHS, Flags));
5954 else
5955 AddOps.push_back(getAddExpr(LHS, RHS, Flags));
5956 break;
5957 }
Dan Gohman36bad002009-09-17 18:05:20 +00005958 }
Sanjoy Das2381fcd2016-03-29 16:40:44 +00005959
5960 if (BO->Opcode == Instruction::Sub)
5961 AddOps.push_back(getNegativeSCEV(getSCEV(BO->RHS)));
5962 else
5963 AddOps.push_back(getSCEV(BO->RHS));
5964
Sanjoy Dasf49ca522016-05-29 00:34:42 +00005965 auto NewBO = MatchBinaryOp(BO->LHS, DT);
Sanjoy Das2381fcd2016-03-29 16:40:44 +00005966 if (!NewBO || (NewBO->Opcode != Instruction::Add &&
5967 NewBO->Opcode != Instruction::Sub)) {
5968 AddOps.push_back(getSCEV(BO->LHS));
5969 break;
5970 }
5971 BO = NewBO;
5972 } while (true);
5973
5974 return getAddExpr(AddOps);
5975 }
5976
5977 case Instruction::Mul: {
5978 SmallVector<const SCEV *, 4> MulOps;
5979 do {
5980 if (BO->Op) {
5981 if (auto *OpSCEV = getExistingSCEV(BO->Op)) {
5982 MulOps.push_back(OpSCEV);
5983 break;
5984 }
5985
5986 SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op);
5987 if (Flags != SCEV::FlagAnyWrap) {
5988 MulOps.push_back(
5989 getMulExpr(getSCEV(BO->LHS), getSCEV(BO->RHS), Flags));
5990 break;
5991 }
5992 }
5993
5994 MulOps.push_back(getSCEV(BO->RHS));
Sanjoy Dasf49ca522016-05-29 00:34:42 +00005995 auto NewBO = MatchBinaryOp(BO->LHS, DT);
Sanjoy Das2381fcd2016-03-29 16:40:44 +00005996 if (!NewBO || NewBO->Opcode != Instruction::Mul) {
5997 MulOps.push_back(getSCEV(BO->LHS));
5998 break;
5999 }
NAKAMURA Takumi940cd932016-07-04 01:26:21 +00006000 BO = NewBO;
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006001 } while (true);
6002
6003 return getMulExpr(MulOps);
6004 }
6005 case Instruction::UDiv:
6006 return getUDivExpr(getSCEV(BO->LHS), getSCEV(BO->RHS));
Alexandre Isoard405728f2017-09-01 14:59:59 +00006007 case Instruction::URem:
6008 return getURemExpr(getSCEV(BO->LHS), getSCEV(BO->RHS));
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006009 case Instruction::Sub: {
6010 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
6011 if (BO->Op)
6012 Flags = getNoWrapFlagsFromUB(BO->Op);
6013 return getMinusSCEV(getSCEV(BO->LHS), getSCEV(BO->RHS), Flags);
6014 }
6015 case Instruction::And:
6016 // For an expression like x&255 that merely masks off the high bits,
6017 // use zext(trunc(x)) as the SCEV expression.
6018 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) {
Craig Topper79ab6432017-07-06 18:39:47 +00006019 if (CI->isZero())
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006020 return getSCEV(BO->RHS);
Craig Topper79ab6432017-07-06 18:39:47 +00006021 if (CI->isMinusOne())
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006022 return getSCEV(BO->LHS);
6023 const APInt &A = CI->getValue();
6024
6025 // Instcombine's ShrinkDemandedConstant may strip bits out of
6026 // constants, obscuring what would otherwise be a low-bits mask.
6027 // Use computeKnownBits to compute what ShrinkDemandedConstant
6028 // knew about to reconstruct a low-bits mask value.
6029 unsigned LZ = A.countLeadingZeros();
6030 unsigned TZ = A.countTrailingZeros();
6031 unsigned BitWidth = A.getBitWidth();
Craig Topperb45eabc2017-04-26 16:39:58 +00006032 KnownBits Known(BitWidth);
6033 computeKnownBits(BO->LHS, Known, getDataLayout(),
Daniel Jasperaec2fa32016-12-19 08:22:17 +00006034 0, &AC, nullptr, &DT);
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006035
6036 APInt EffectiveMask =
6037 APInt::getLowBitsSet(BitWidth, BitWidth - LZ - TZ).shl(TZ);
Craig Topperb45eabc2017-04-26 16:39:58 +00006038 if ((LZ != 0 || TZ != 0) && !((~A & ~Known.Zero) & EffectiveMask)) {
Eli Friedmanf1f49c82017-01-18 23:56:42 +00006039 const SCEV *MulCount = getConstant(APInt::getOneBitSet(BitWidth, TZ));
6040 const SCEV *LHS = getSCEV(BO->LHS);
6041 const SCEV *ShiftedLHS = nullptr;
6042 if (auto *LHSMul = dyn_cast<SCEVMulExpr>(LHS)) {
6043 if (auto *OpC = dyn_cast<SCEVConstant>(LHSMul->getOperand(0))) {
6044 // For an expression like (x * 8) & 8, simplify the multiply.
6045 unsigned MulZeros = OpC->getAPInt().countTrailingZeros();
6046 unsigned GCD = std::min(MulZeros, TZ);
6047 APInt DivAmt = APInt::getOneBitSet(BitWidth, TZ - GCD);
6048 SmallVector<const SCEV*, 4> MulOps;
6049 MulOps.push_back(getConstant(OpC->getAPInt().lshr(GCD)));
6050 MulOps.append(LHSMul->op_begin() + 1, LHSMul->op_end());
6051 auto *NewMul = getMulExpr(MulOps, LHSMul->getNoWrapFlags());
6052 ShiftedLHS = getUDivExpr(NewMul, getConstant(DivAmt));
6053 }
6054 }
6055 if (!ShiftedLHS)
6056 ShiftedLHS = getUDivExpr(LHS, MulCount);
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006057 return getMulExpr(
6058 getZeroExtendExpr(
Eli Friedmanf1f49c82017-01-18 23:56:42 +00006059 getTruncateExpr(ShiftedLHS,
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006060 IntegerType::get(getContext(), BitWidth - LZ - TZ)),
6061 BO->LHS->getType()),
6062 MulCount);
6063 }
Dan Gohman36bad002009-09-17 18:05:20 +00006064 }
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006065 break;
Nick Lewyckyf5c547d2008-07-07 06:15:49 +00006066
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006067 case Instruction::Or:
Eli Friedmand0e6ae562017-04-20 23:59:05 +00006068 // If the RHS of the Or is a constant, we may have something like:
6069 // X*4+1 which got turned into X*4|1. Handle this as an Add so loop
6070 // optimizations will transparently handle this case.
6071 //
6072 // In order for this transformation to be safe, the LHS must be of the
6073 // form X*(2^n) and the Or constant must be less than 2^n.
6074 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) {
6075 const SCEV *LHS = getSCEV(BO->LHS);
6076 const APInt &CIVal = CI->getValue();
6077 if (GetMinTrailingZeros(LHS) >=
6078 (CIVal.getBitWidth() - CIVal.countLeadingZeros())) {
6079 // Build a plain add SCEV.
6080 const SCEV *S = getAddExpr(LHS, getSCEV(CI));
6081 // If the LHS of the add was an addrec and it has no-wrap flags,
6082 // transfer the no-wrap flags, since an or won't introduce a wrap.
6083 if (const SCEVAddRecExpr *NewAR = dyn_cast<SCEVAddRecExpr>(S)) {
6084 const SCEVAddRecExpr *OldAR = cast<SCEVAddRecExpr>(LHS);
6085 const_cast<SCEVAddRecExpr *>(NewAR)->setNoWrapFlags(
6086 OldAR->getNoWrapFlags());
6087 }
6088 return S;
6089 }
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006090 }
6091 break;
Dan Gohman6350296e2009-05-18 16:29:04 +00006092
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006093 case Instruction::Xor:
6094 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) {
6095 // If the RHS of xor is -1, then this is a not operation.
Craig Topper79ab6432017-07-06 18:39:47 +00006096 if (CI->isMinusOne())
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006097 return getNotSCEV(getSCEV(BO->LHS));
Dan Gohmaneddf7712009-06-18 00:00:20 +00006098
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006099 // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask.
6100 // This is a variant of the check for xor with -1, and it handles
6101 // the case where instcombine has trimmed non-demanded bits out
6102 // of an xor with -1.
6103 if (auto *LBO = dyn_cast<BinaryOperator>(BO->LHS))
6104 if (ConstantInt *LCI = dyn_cast<ConstantInt>(LBO->getOperand(1)))
6105 if (LBO->getOpcode() == Instruction::And &&
6106 LCI->getValue() == CI->getValue())
6107 if (const SCEVZeroExtendExpr *Z =
6108 dyn_cast<SCEVZeroExtendExpr>(getSCEV(BO->LHS))) {
6109 Type *UTy = BO->LHS->getType();
6110 const SCEV *Z0 = Z->getOperand();
6111 Type *Z0Ty = Z0->getType();
6112 unsigned Z0TySize = getTypeSizeInBits(Z0Ty);
Dan Gohmaneddf7712009-06-18 00:00:20 +00006113
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006114 // If C is a low-bits mask, the zero extend is serving to
6115 // mask off the high bits. Complement the operand and
6116 // re-apply the zext.
Craig Topperd33ee1b2017-04-03 16:34:59 +00006117 if (CI->getValue().isMask(Z0TySize))
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006118 return getZeroExtendExpr(getNotSCEV(Z0), UTy);
6119
6120 // If C is a single bit, it may be in the sign-bit position
6121 // before the zero-extend. In this case, represent the xor
6122 // using an add, which is equivalent, and re-apply the zext.
6123 APInt Trunc = CI->getValue().trunc(Z0TySize);
6124 if (Trunc.zext(getTypeSizeInBits(UTy)) == CI->getValue() &&
Craig Topperbcfd2d12017-04-20 16:56:25 +00006125 Trunc.isSignMask())
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006126 return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)),
6127 UTy);
6128 }
6129 }
6130 break;
Dan Gohman05e89732008-06-22 19:56:46 +00006131
6132 case Instruction::Shl:
6133 // Turn shift left of a constant amount into a multiply.
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006134 if (ConstantInt *SA = dyn_cast<ConstantInt>(BO->RHS)) {
6135 uint32_t BitWidth = cast<IntegerType>(SA->getType())->getBitWidth();
Dan Gohmanacd700a2010-04-22 01:35:11 +00006136
6137 // If the shift count is not less than the bitwidth, the result of
6138 // the shift is undefined. Don't try to analyze it, because the
6139 // resolution chosen here may differ from the resolution chosen in
6140 // other parts of the compiler.
6141 if (SA->getValue().uge(BitWidth))
6142 break;
6143
Bjarke Hammersholt Roune9791ed42015-08-14 22:45:26 +00006144 // It is currently not resolved how to interpret NSW for left
6145 // shift by BitWidth - 1, so we avoid applying flags in that
6146 // case. Remove this check (or this comment) once the situation
6147 // is resolved. See
6148 // http://lists.llvm.org/pipermail/llvm-dev/2015-April/084195.html
6149 // and http://reviews.llvm.org/D8890 .
6150 auto Flags = SCEV::FlagAnyWrap;
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006151 if (BO->Op && SA->getValue().ult(BitWidth - 1))
6152 Flags = getNoWrapFlagsFromUB(BO->Op);
Bjarke Hammersholt Roune9791ed42015-08-14 22:45:26 +00006153
Owen Andersonedb4a702009-07-24 23:12:02 +00006154 Constant *X = ConstantInt::get(getContext(),
Benjamin Kramerfc3ea6f2013-07-11 16:05:50 +00006155 APInt::getOneBitSet(BitWidth, SA->getZExtValue()));
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006156 return getMulExpr(getSCEV(BO->LHS), getSCEV(X), Flags);
Dan Gohman05e89732008-06-22 19:56:46 +00006157 }
6158 break;
6159
Eugene Zelenkobe709f22017-08-18 23:51:26 +00006160 case Instruction::AShr: {
Zhaoshi Zhenge3c90702017-03-23 18:06:09 +00006161 // AShr X, C, where C is a constant.
6162 ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS);
6163 if (!CI)
6164 break;
Dan Gohmanacd700a2010-04-22 01:35:11 +00006165
Zhaoshi Zhenge3c90702017-03-23 18:06:09 +00006166 Type *OuterTy = BO->LHS->getType();
6167 uint64_t BitWidth = getTypeSizeInBits(OuterTy);
6168 // If the shift count is not less than the bitwidth, the result of
6169 // the shift is undefined. Don't try to analyze it, because the
6170 // resolution chosen here may differ from the resolution chosen in
6171 // other parts of the compiler.
6172 if (CI->getValue().uge(BitWidth))
6173 break;
Dan Gohmanacd700a2010-04-22 01:35:11 +00006174
Craig Topper79ab6432017-07-06 18:39:47 +00006175 if (CI->isZero())
Zhaoshi Zhenge3c90702017-03-23 18:06:09 +00006176 return getSCEV(BO->LHS); // shift by zero --> noop
6177
6178 uint64_t AShrAmt = CI->getZExtValue();
6179 Type *TruncTy = IntegerType::get(getContext(), BitWidth - AShrAmt);
6180
6181 Operator *L = dyn_cast<Operator>(BO->LHS);
6182 if (L && L->getOpcode() == Instruction::Shl) {
6183 // X = Shl A, n
6184 // Y = AShr X, m
6185 // Both n and m are constant.
6186
6187 const SCEV *ShlOp0SCEV = getSCEV(L->getOperand(0));
6188 if (L->getOperand(1) == BO->RHS)
6189 // For a two-shift sext-inreg, i.e. n = m,
6190 // use sext(trunc(x)) as the SCEV expression.
6191 return getSignExtendExpr(
6192 getTruncateExpr(ShlOp0SCEV, TruncTy), OuterTy);
6193
6194 ConstantInt *ShlAmtCI = dyn_cast<ConstantInt>(L->getOperand(1));
6195 if (ShlAmtCI && ShlAmtCI->getValue().ult(BitWidth)) {
6196 uint64_t ShlAmt = ShlAmtCI->getZExtValue();
6197 if (ShlAmt > AShrAmt) {
6198 // When n > m, use sext(mul(trunc(x), 2^(n-m)))) as the SCEV
6199 // expression. We already checked that ShlAmt < BitWidth, so
6200 // the multiplier, 1 << (ShlAmt - AShrAmt), fits into TruncTy as
6201 // ShlAmt - AShrAmt < Amt.
6202 APInt Mul = APInt::getOneBitSet(BitWidth - AShrAmt,
6203 ShlAmt - AShrAmt);
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006204 return getSignExtendExpr(
Zhaoshi Zhenge3c90702017-03-23 18:06:09 +00006205 getMulExpr(getTruncateExpr(ShlOp0SCEV, TruncTy),
6206 getConstant(Mul)), OuterTy);
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006207 }
Zhaoshi Zhenge3c90702017-03-23 18:06:09 +00006208 }
6209 }
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006210 break;
Nick Lewyckyf5c547d2008-07-07 06:15:49 +00006211 }
Eugene Zelenkobe709f22017-08-18 23:51:26 +00006212 }
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006213 }
Nick Lewyckyf5c547d2008-07-07 06:15:49 +00006214
Sanjoy Das2381fcd2016-03-29 16:40:44 +00006215 switch (U->getOpcode()) {
Dan Gohman05e89732008-06-22 19:56:46 +00006216 case Instruction::Trunc:
Dan Gohmanc8e23622009-04-21 23:15:49 +00006217 return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman05e89732008-06-22 19:56:46 +00006218
6219 case Instruction::ZExt:
Dan Gohmanc8e23622009-04-21 23:15:49 +00006220 return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman05e89732008-06-22 19:56:46 +00006221
6222 case Instruction::SExt:
Amara Emerson56dca4e32017-08-04 20:19:46 +00006223 if (auto BO = MatchBinaryOp(U->getOperand(0), DT)) {
6224 // The NSW flag of a subtract does not always survive the conversion to
6225 // A + (-1)*B. By pushing sign extension onto its operands we are much
6226 // more likely to preserve NSW and allow later AddRec optimisations.
6227 //
6228 // NOTE: This is effectively duplicating this logic from getSignExtend:
6229 // sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw>
6230 // but by that point the NSW information has potentially been lost.
6231 if (BO->Opcode == Instruction::Sub && BO->IsNSW) {
6232 Type *Ty = U->getType();
6233 auto *V1 = getSignExtendExpr(getSCEV(BO->LHS), Ty);
6234 auto *V2 = getSignExtendExpr(getSCEV(BO->RHS), Ty);
6235 return getMinusSCEV(V1, V2, SCEV::FlagNSW);
6236 }
6237 }
Dan Gohmanc8e23622009-04-21 23:15:49 +00006238 return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType());
Dan Gohman05e89732008-06-22 19:56:46 +00006239
6240 case Instruction::BitCast:
6241 // BitCasts are no-op casts so we just eliminate the cast.
Dan Gohmanb397e1a2009-04-21 01:07:12 +00006242 if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType()))
Dan Gohman05e89732008-06-22 19:56:46 +00006243 return getSCEV(U->getOperand(0));
6244 break;
6245
Dan Gohmane5e1b7b2010-02-01 18:27:38 +00006246 // It's tempting to handle inttoptr and ptrtoint as no-ops, however this can
6247 // lead to pointer expressions which cannot safely be expanded to GEPs,
6248 // because ScalarEvolution doesn't respect the GEP aliasing rules when
6249 // simplifying integer expressions.
Dan Gohman0a40ad92009-04-16 03:18:22 +00006250
Dan Gohmanee750d12009-05-08 20:26:55 +00006251 case Instruction::GetElementPtr:
Dan Gohmanb256ccf2009-12-18 02:09:29 +00006252 return createNodeForGEP(cast<GEPOperator>(U));
Dan Gohman0a40ad92009-04-16 03:18:22 +00006253
Dan Gohman05e89732008-06-22 19:56:46 +00006254 case Instruction::PHI:
6255 return createNodeForPHI(cast<PHINode>(U));
6256
6257 case Instruction::Select:
Sanjoy Dasd0671342015-10-02 19:39:59 +00006258 // U can also be a select constant expr, which let fall through. Since
6259 // createNodeForSelect only works for a condition that is an `ICmpInst`, and
6260 // constant expressions cannot have instructions as operands, we'd have
6261 // returned getUnknown for a select constant expressions anyway.
6262 if (isa<Instruction>(U))
Sanjoy Das55015d22015-10-02 23:09:44 +00006263 return createNodeForSelectOrPHI(cast<Instruction>(U), U->getOperand(0),
6264 U->getOperand(1), U->getOperand(2));
Hal Finkele186deb2016-07-11 02:48:23 +00006265 break;
6266
6267 case Instruction::Call:
6268 case Instruction::Invoke:
6269 if (Value *RV = CallSite(U).getReturnedArgOperand())
6270 return getSCEV(RV);
6271 break;
Chris Lattnerd934c702004-04-02 20:23:17 +00006272 }
6273
Dan Gohmanc8e23622009-04-21 23:15:49 +00006274 return getUnknown(V);
Chris Lattnerd934c702004-04-02 20:23:17 +00006275}
6276
Chris Lattnerd934c702004-04-02 20:23:17 +00006277//===----------------------------------------------------------------------===//
6278// Iteration Count Computation Code
6279//
6280
Haicheng Wu1ef17e92016-10-12 21:29:38 +00006281static unsigned getConstantTripCount(const SCEVConstant *ExitCount) {
6282 if (!ExitCount)
6283 return 0;
6284
6285 ConstantInt *ExitConst = ExitCount->getValue();
6286
6287 // Guard against huge trip counts.
6288 if (ExitConst->getValue().getActiveBits() > 32)
6289 return 0;
6290
6291 // In case of integer overflow, this returns 0, which is correct.
6292 return ((unsigned)ExitConst->getZExtValue()) + 1;
6293}
6294
Eli Friedmanf7b060b2017-03-17 22:19:52 +00006295unsigned ScalarEvolution::getSmallConstantTripCount(const Loop *L) {
Chandler Carruth6666c272014-10-11 00:12:11 +00006296 if (BasicBlock *ExitingBB = L->getExitingBlock())
6297 return getSmallConstantTripCount(L, ExitingBB);
6298
6299 // No trip count information for multiple exits.
6300 return 0;
6301}
6302
Eli Friedmanf7b060b2017-03-17 22:19:52 +00006303unsigned ScalarEvolution::getSmallConstantTripCount(const Loop *L,
Mark Heffernan2beab5f2014-10-10 17:39:11 +00006304 BasicBlock *ExitingBlock) {
Chandler Carruth6666c272014-10-11 00:12:11 +00006305 assert(ExitingBlock && "Must pass a non-null exiting block!");
6306 assert(L->isLoopExiting(ExitingBlock) &&
6307 "Exiting block must actually branch out of the loop!");
Andrew Trick2b6860f2011-08-11 23:36:16 +00006308 const SCEVConstant *ExitCount =
Mark Heffernan2beab5f2014-10-10 17:39:11 +00006309 dyn_cast<SCEVConstant>(getExitCount(L, ExitingBlock));
Haicheng Wu1ef17e92016-10-12 21:29:38 +00006310 return getConstantTripCount(ExitCount);
6311}
Andrew Trick2b6860f2011-08-11 23:36:16 +00006312
Eli Friedmanf7b060b2017-03-17 22:19:52 +00006313unsigned ScalarEvolution::getSmallConstantMaxTripCount(const Loop *L) {
Haicheng Wu1ef17e92016-10-12 21:29:38 +00006314 const auto *MaxExitCount =
6315 dyn_cast<SCEVConstant>(getMaxBackedgeTakenCount(L));
6316 return getConstantTripCount(MaxExitCount);
Andrew Trick2b6860f2011-08-11 23:36:16 +00006317}
6318
Eli Friedmanf7b060b2017-03-17 22:19:52 +00006319unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L) {
Chandler Carruth6666c272014-10-11 00:12:11 +00006320 if (BasicBlock *ExitingBB = L->getExitingBlock())
6321 return getSmallConstantTripMultiple(L, ExitingBB);
6322
6323 // No trip multiple information for multiple exits.
6324 return 0;
6325}
6326
Sanjoy Dasf8570812016-05-29 00:38:22 +00006327/// Returns the largest constant divisor of the trip count of this loop as a
6328/// normal unsigned value, if possible. This means that the actual trip count is
6329/// always a multiple of the returned value (don't forget the trip count could
6330/// very well be zero as well!).
Andrew Trick2b6860f2011-08-11 23:36:16 +00006331///
6332/// Returns 1 if the trip count is unknown or not guaranteed to be the
6333/// multiple of a constant (which is also the case if the trip count is simply
6334/// constant, use getSmallConstantTripCount for that case), Will also return 1
6335/// if the trip count is very large (>= 2^32).
Andrew Tricke81211f2012-01-11 06:52:55 +00006336///
6337/// As explained in the comments for getSmallConstantTripCount, this assumes
6338/// that control exits the loop via ExitingBlock.
Mark Heffernan2beab5f2014-10-10 17:39:11 +00006339unsigned
Eli Friedmanf7b060b2017-03-17 22:19:52 +00006340ScalarEvolution::getSmallConstantTripMultiple(const Loop *L,
Mark Heffernan2beab5f2014-10-10 17:39:11 +00006341 BasicBlock *ExitingBlock) {
Chandler Carruth6666c272014-10-11 00:12:11 +00006342 assert(ExitingBlock && "Must pass a non-null exiting block!");
6343 assert(L->isLoopExiting(ExitingBlock) &&
6344 "Exiting block must actually branch out of the loop!");
Mark Heffernan2beab5f2014-10-10 17:39:11 +00006345 const SCEV *ExitCount = getExitCount(L, ExitingBlock);
Andrew Trick2b6860f2011-08-11 23:36:16 +00006346 if (ExitCount == getCouldNotCompute())
6347 return 1;
6348
6349 // Get the trip count from the BE count by adding 1.
Eli Friedmanb1578d32017-03-20 20:25:46 +00006350 const SCEV *TCExpr = getAddExpr(ExitCount, getOne(ExitCount->getType()));
Andrew Trick2b6860f2011-08-11 23:36:16 +00006351
Eli Friedmanb1578d32017-03-20 20:25:46 +00006352 const SCEVConstant *TC = dyn_cast<SCEVConstant>(TCExpr);
6353 if (!TC)
6354 // Attempt to factor more general cases. Returns the greatest power of
6355 // two divisor. If overflow happens, the trip count expression is still
6356 // divisible by the greatest power of 2 divisor returned.
6357 return 1U << std::min((uint32_t)31, GetMinTrailingZeros(TCExpr));
Andrew Trick2b6860f2011-08-11 23:36:16 +00006358
Eli Friedmanb1578d32017-03-20 20:25:46 +00006359 ConstantInt *Result = TC->getValue();
Andrew Trick2b6860f2011-08-11 23:36:16 +00006360
Hal Finkel30bd9342012-10-24 19:46:44 +00006361 // Guard against huge trip counts (this requires checking
6362 // for zero to handle the case where the trip count == -1 and the
6363 // addition wraps).
6364 if (!Result || Result->getValue().getActiveBits() > 32 ||
6365 Result->getValue().getActiveBits() == 0)
Andrew Trick2b6860f2011-08-11 23:36:16 +00006366 return 1;
6367
6368 return (unsigned)Result->getZExtValue();
6369}
6370
Sanjoy Dasf8570812016-05-29 00:38:22 +00006371/// Get the expression for the number of loop iterations for which this loop is
6372/// guaranteed not to exit via ExitingBlock. Otherwise return
6373/// SCEVCouldNotCompute.
Eli Friedmanf7b060b2017-03-17 22:19:52 +00006374const SCEV *ScalarEvolution::getExitCount(const Loop *L,
6375 BasicBlock *ExitingBlock) {
Andrew Trick77c55422011-08-02 04:23:35 +00006376 return getBackedgeTakenInfo(L).getExact(ExitingBlock, this);
Andrew Trick3ca3f982011-07-26 17:19:55 +00006377}
6378
Silviu Baranga6f444df2016-04-08 14:29:09 +00006379const SCEV *
6380ScalarEvolution::getPredicatedBackedgeTakenCount(const Loop *L,
6381 SCEVUnionPredicate &Preds) {
6382 return getPredicatedBackedgeTakenInfo(L).getExact(this, &Preds);
6383}
6384
Dan Gohmanaf752342009-07-07 17:06:11 +00006385const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) {
Andrew Trick3ca3f982011-07-26 17:19:55 +00006386 return getBackedgeTakenInfo(L).getExact(this);
Dan Gohman2b8da352009-04-30 20:47:05 +00006387}
6388
Sanjoy Dasf8570812016-05-29 00:38:22 +00006389/// Similar to getBackedgeTakenCount, except return the least SCEV value that is
6390/// known never to be less than the actual backedge taken count.
Dan Gohmanaf752342009-07-07 17:06:11 +00006391const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) {
Andrew Trick3ca3f982011-07-26 17:19:55 +00006392 return getBackedgeTakenInfo(L).getMax(this);
Dan Gohman2b8da352009-04-30 20:47:05 +00006393}
6394
John Brawn84b21832016-10-21 11:08:48 +00006395bool ScalarEvolution::isBackedgeTakenCountMaxOrZero(const Loop *L) {
6396 return getBackedgeTakenInfo(L).isMaxOrZero(this);
6397}
6398
Sanjoy Dasf8570812016-05-29 00:38:22 +00006399/// Push PHI nodes in the header of the given loop onto the given Worklist.
Dan Gohmandc191042009-07-08 19:23:34 +00006400static void
6401PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) {
6402 BasicBlock *Header = L->getHeader();
6403
6404 // Push all Loop-header PHIs onto the Worklist stack.
6405 for (BasicBlock::iterator I = Header->begin();
6406 PHINode *PN = dyn_cast<PHINode>(I); ++I)
6407 Worklist.push_back(PN);
6408}
6409
Dan Gohman2b8da352009-04-30 20:47:05 +00006410const ScalarEvolution::BackedgeTakenInfo &
Silviu Baranga6f444df2016-04-08 14:29:09 +00006411ScalarEvolution::getPredicatedBackedgeTakenInfo(const Loop *L) {
6412 auto &BTI = getBackedgeTakenInfo(L);
6413 if (BTI.hasFullInfo())
6414 return BTI;
6415
6416 auto Pair = PredicatedBackedgeTakenCounts.insert({L, BackedgeTakenInfo()});
6417
6418 if (!Pair.second)
6419 return Pair.first->second;
6420
6421 BackedgeTakenInfo Result =
6422 computeBackedgeTakenCount(L, /*AllowPredicates=*/true);
6423
Sanjoy Dasc9bbf562016-09-25 23:12:04 +00006424 return PredicatedBackedgeTakenCounts.find(L)->second = std::move(Result);
Silviu Baranga6f444df2016-04-08 14:29:09 +00006425}
6426
6427const ScalarEvolution::BackedgeTakenInfo &
Dan Gohman2b8da352009-04-30 20:47:05 +00006428ScalarEvolution::getBackedgeTakenInfo(const Loop *L) {
Andrew Trick3ca3f982011-07-26 17:19:55 +00006429 // Initially insert an invalid entry for this loop. If the insertion
Dan Gohman8b0a4192010-03-01 17:49:51 +00006430 // succeeds, proceed to actually compute a backedge-taken count and
Dan Gohman76466372009-04-27 20:16:15 +00006431 // update the value. The temporary CouldNotCompute value tells SCEV
6432 // code elsewhere that it shouldn't attempt to request a new
6433 // backedge-taken count, which could result in infinite recursion.
Dan Gohman0daf6872011-05-09 18:44:09 +00006434 std::pair<DenseMap<const Loop *, BackedgeTakenInfo>::iterator, bool> Pair =
Sanjoy Dasc42f7cc2016-02-20 01:35:56 +00006435 BackedgeTakenCounts.insert({L, BackedgeTakenInfo()});
Chris Lattnera337f5e2011-01-09 02:16:18 +00006436 if (!Pair.second)
6437 return Pair.first->second;
Dan Gohman76466372009-04-27 20:16:15 +00006438
Sanjoy Das413dbbb2015-10-08 18:46:59 +00006439 // computeBackedgeTakenCount may allocate memory for its result. Inserting it
Andrew Trick3ca3f982011-07-26 17:19:55 +00006440 // into the BackedgeTakenCounts map transfers ownership. Otherwise, the result
6441 // must be cleared in this scope.
Sanjoy Das413dbbb2015-10-08 18:46:59 +00006442 BackedgeTakenInfo Result = computeBackedgeTakenCount(L);
Andrew Trick3ca3f982011-07-26 17:19:55 +00006443
6444 if (Result.getExact(this) != getCouldNotCompute()) {
6445 assert(isLoopInvariant(Result.getExact(this), L) &&
6446 isLoopInvariant(Result.getMax(this), L) &&
Chris Lattnera337f5e2011-01-09 02:16:18 +00006447 "Computed backedge-taken count isn't loop invariant for loop!");
6448 ++NumTripCountsComputed;
Andrew Trick3ca3f982011-07-26 17:19:55 +00006449 }
6450 else if (Result.getMax(this) == getCouldNotCompute() &&
6451 isa<PHINode>(L->getHeader()->begin())) {
6452 // Only count loops that have phi nodes as not being computable.
6453 ++NumTripCountsNotComputed;
Chris Lattnera337f5e2011-01-09 02:16:18 +00006454 }
Dan Gohman2b8da352009-04-30 20:47:05 +00006455
Chris Lattnera337f5e2011-01-09 02:16:18 +00006456 // Now that we know more about the trip count for this loop, forget any
6457 // existing SCEV values for PHI nodes in this loop since they are only
6458 // conservative estimates made without the benefit of trip count
6459 // information. This is similar to the code in forgetLoop, except that
6460 // it handles SCEVUnknown PHI nodes specially.
Andrew Trick3ca3f982011-07-26 17:19:55 +00006461 if (Result.hasAnyInfo()) {
Chris Lattnera337f5e2011-01-09 02:16:18 +00006462 SmallVector<Instruction *, 16> Worklist;
6463 PushLoopPHIs(L, Worklist);
Dan Gohmandc191042009-07-08 19:23:34 +00006464
Sanjoy Dasadf37512017-12-04 19:22:01 +00006465 SmallPtrSet<Instruction *, 8> Discovered;
Chris Lattnera337f5e2011-01-09 02:16:18 +00006466 while (!Worklist.empty()) {
6467 Instruction *I = Worklist.pop_back_val();
Dan Gohmandc191042009-07-08 19:23:34 +00006468
Chris Lattnera337f5e2011-01-09 02:16:18 +00006469 ValueExprMapType::iterator It =
Benjamin Kramere2ef47c2012-06-30 22:37:15 +00006470 ValueExprMap.find_as(static_cast<Value *>(I));
Chris Lattnera337f5e2011-01-09 02:16:18 +00006471 if (It != ValueExprMap.end()) {
6472 const SCEV *Old = It->second;
Dan Gohman761065e2010-11-17 02:44:44 +00006473
Chris Lattnera337f5e2011-01-09 02:16:18 +00006474 // SCEVUnknown for a PHI either means that it has an unrecognized
6475 // structure, or it's a PHI that's in the progress of being computed
6476 // by createNodeForPHI. In the former case, additional loop trip
6477 // count information isn't going to change anything. In the later
6478 // case, createNodeForPHI will perform the necessary updates on its
6479 // own when it gets to that point.
6480 if (!isa<PHINode>(I) || !isa<SCEVUnknown>(Old)) {
Wei Mi785858c2016-08-09 20:37:50 +00006481 eraseValueFromMap(It->first);
Sanjoy Das7e363372017-12-04 19:22:00 +00006482 forgetMemoizedResults(Old);
Dan Gohmandc191042009-07-08 19:23:34 +00006483 }
Chris Lattnera337f5e2011-01-09 02:16:18 +00006484 if (PHINode *PN = dyn_cast<PHINode>(I))
6485 ConstantEvolutionLoopExitValue.erase(PN);
Dan Gohmandc191042009-07-08 19:23:34 +00006486 }
Chris Lattnera337f5e2011-01-09 02:16:18 +00006487
Sanjoy Das7e363372017-12-04 19:22:00 +00006488 // Since we don't need to invalidate anything for correctness and we're
6489 // only invalidating to make SCEV's results more precise, we get to stop
6490 // early to avoid invalidating too much. This is especially important in
6491 // cases like:
6492 //
6493 // %v = f(pn0, pn1) // pn0 and pn1 used through some other phi node
6494 // loop0:
6495 // %pn0 = phi
6496 // ...
6497 // loop1:
6498 // %pn1 = phi
6499 // ...
6500 //
6501 // where both loop0 and loop1's backedge taken count uses the SCEV
6502 // expression for %v. If we don't have the early stop below then in cases
6503 // like the above, getBackedgeTakenInfo(loop1) will clear out the trip
6504 // count for loop0 and getBackedgeTakenInfo(loop0) will clear out the trip
6505 // count for loop1, effectively nullifying SCEV's trip count cache.
6506 for (auto *U : I->users())
6507 if (auto *I = dyn_cast<Instruction>(U)) {
6508 auto *LoopForUser = LI.getLoopFor(I->getParent());
Sanjoy Dasadf37512017-12-04 19:22:01 +00006509 if (LoopForUser && L->contains(LoopForUser) &&
6510 Discovered.insert(I).second)
Sanjoy Das7e363372017-12-04 19:22:00 +00006511 Worklist.push_back(I);
6512 }
Dan Gohmandc191042009-07-08 19:23:34 +00006513 }
Chris Lattnerd934c702004-04-02 20:23:17 +00006514 }
Dan Gohman6acd95b2011-04-25 22:48:29 +00006515
6516 // Re-lookup the insert position, since the call to
Sanjoy Das413dbbb2015-10-08 18:46:59 +00006517 // computeBackedgeTakenCount above could result in a
Dan Gohman6acd95b2011-04-25 22:48:29 +00006518 // recusive call to getBackedgeTakenInfo (on a different
6519 // loop), which would invalidate the iterator computed
6520 // earlier.
Sanjoy Dasc9bbf562016-09-25 23:12:04 +00006521 return BackedgeTakenCounts.find(L)->second = std::move(Result);
Chris Lattnerd934c702004-04-02 20:23:17 +00006522}
6523
Dan Gohman880c92a2009-10-31 15:04:55 +00006524void ScalarEvolution::forgetLoop(const Loop *L) {
6525 // Drop any stored trip count value.
Silviu Baranga6f444df2016-04-08 14:29:09 +00006526 auto RemoveLoopFromBackedgeMap =
Marcello Maggionice900602017-09-11 15:44:20 +00006527 [](DenseMap<const Loop *, BackedgeTakenInfo> &Map, const Loop *L) {
Silviu Baranga6f444df2016-04-08 14:29:09 +00006528 auto BTCPos = Map.find(L);
6529 if (BTCPos != Map.end()) {
6530 BTCPos->second.clear();
6531 Map.erase(BTCPos);
6532 }
6533 };
6534
Marcello Maggionice900602017-09-11 15:44:20 +00006535 SmallVector<const Loop *, 16> LoopWorklist(1, L);
6536 SmallVector<Instruction *, 32> Worklist;
6537 SmallPtrSet<Instruction *, 16> Visited;
Dan Gohmanf1505722009-05-02 17:43:35 +00006538
Marcello Maggionice900602017-09-11 15:44:20 +00006539 // Iterate over all the loops and sub-loops to drop SCEV information.
6540 while (!LoopWorklist.empty()) {
6541 auto *CurrL = LoopWorklist.pop_back_val();
Dorit Nuzmanca4fd182017-07-18 11:57:08 +00006542
Marcello Maggionice900602017-09-11 15:44:20 +00006543 RemoveLoopFromBackedgeMap(BackedgeTakenCounts, CurrL);
6544 RemoveLoopFromBackedgeMap(PredicatedBackedgeTakenCounts, CurrL);
Dan Gohman48f82222009-05-04 22:30:44 +00006545
Marcello Maggionice900602017-09-11 15:44:20 +00006546 // Drop information about predicated SCEV rewrites for this loop.
6547 for (auto I = PredicatedSCEVRewrites.begin();
6548 I != PredicatedSCEVRewrites.end();) {
6549 std::pair<const SCEV *, const Loop *> Entry = I->first;
6550 if (Entry.second == CurrL)
6551 PredicatedSCEVRewrites.erase(I++);
6552 else
6553 ++I;
Dan Gohmandc191042009-07-08 19:23:34 +00006554 }
6555
Sanjoy Dase6b995f2017-10-13 05:50:52 +00006556 auto LoopUsersItr = LoopUsers.find(CurrL);
6557 if (LoopUsersItr != LoopUsers.end()) {
Sanjoy Das3a5e2522017-10-17 01:03:56 +00006558 for (auto *S : LoopUsersItr->second)
6559 forgetMemoizedResults(S);
Sanjoy Dase6b995f2017-10-13 05:50:52 +00006560 LoopUsers.erase(LoopUsersItr);
6561 }
6562
Marcello Maggionice900602017-09-11 15:44:20 +00006563 // Drop information about expressions based on loop-header PHIs.
6564 PushLoopPHIs(CurrL, Worklist);
6565
6566 while (!Worklist.empty()) {
6567 Instruction *I = Worklist.pop_back_val();
6568 if (!Visited.insert(I).second)
6569 continue;
6570
6571 ValueExprMapType::iterator It =
6572 ValueExprMap.find_as(static_cast<Value *>(I));
6573 if (It != ValueExprMap.end()) {
6574 eraseValueFromMap(It->first);
6575 forgetMemoizedResults(It->second);
6576 if (PHINode *PN = dyn_cast<PHINode>(I))
6577 ConstantEvolutionLoopExitValue.erase(PN);
6578 }
6579
6580 PushDefUseChildren(I, Worklist);
6581 }
6582
Marcello Maggionice900602017-09-11 15:44:20 +00006583 LoopPropertiesCache.erase(CurrL);
6584 // Forget all contained loops too, to avoid dangling entries in the
6585 // ValuesAtScopes map.
6586 LoopWorklist.append(CurrL->begin(), CurrL->end());
Dan Gohman48f82222009-05-04 22:30:44 +00006587 }
Dan Gohman43300342009-02-17 20:49:49 +00006588}
6589
Eric Christopheref6d5932010-07-29 01:25:38 +00006590void ScalarEvolution::forgetValue(Value *V) {
Dale Johannesen1d6827a2010-02-19 07:14:22 +00006591 Instruction *I = dyn_cast<Instruction>(V);
6592 if (!I) return;
6593
6594 // Drop information about expressions based on loop-header PHIs.
6595 SmallVector<Instruction *, 16> Worklist;
6596 Worklist.push_back(I);
6597
6598 SmallPtrSet<Instruction *, 8> Visited;
6599 while (!Worklist.empty()) {
6600 I = Worklist.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +00006601 if (!Visited.insert(I).second)
6602 continue;
Dale Johannesen1d6827a2010-02-19 07:14:22 +00006603
Benjamin Kramere2ef47c2012-06-30 22:37:15 +00006604 ValueExprMapType::iterator It =
6605 ValueExprMap.find_as(static_cast<Value *>(I));
Dan Gohman9bad2fb2010-08-27 18:55:03 +00006606 if (It != ValueExprMap.end()) {
Wei Mi785858c2016-08-09 20:37:50 +00006607 eraseValueFromMap(It->first);
Dan Gohman7e6b3932010-11-17 23:28:48 +00006608 forgetMemoizedResults(It->second);
Dale Johannesen1d6827a2010-02-19 07:14:22 +00006609 if (PHINode *PN = dyn_cast<PHINode>(I))
6610 ConstantEvolutionLoopExitValue.erase(PN);
6611 }
6612
6613 PushDefUseChildren(I, Worklist);
6614 }
6615}
6616
Sanjoy Dasf8570812016-05-29 00:38:22 +00006617/// Get the exact loop backedge taken count considering all loop exits. A
6618/// computable result can only be returned for loops with a single exit.
6619/// Returning the minimum taken count among all exits is incorrect because one
6620/// of the loop's exit limit's may have been skipped. howFarToZero assumes that
6621/// the limit of each loop test is never skipped. This is a valid assumption as
6622/// long as the loop exits via that test. For precise results, it is the
6623/// caller's responsibility to specify the relevant loop exit using
Andrew Trick90c7a102011-11-16 00:52:40 +00006624/// getExact(ExitingBlock, SE).
Andrew Trick3ca3f982011-07-26 17:19:55 +00006625const SCEV *
Sanjoy Dasd1eb62a2016-09-25 23:12:00 +00006626ScalarEvolution::BackedgeTakenInfo::getExact(ScalarEvolution *SE,
6627 SCEVUnionPredicate *Preds) const {
Andrew Trick3ca3f982011-07-26 17:19:55 +00006628 // If any exits were not computable, the loop is not computable.
Sanjoy Dasd1eb62a2016-09-25 23:12:00 +00006629 if (!isComplete() || ExitNotTaken.empty())
6630 return SE->getCouldNotCompute();
Andrew Trick3ca3f982011-07-26 17:19:55 +00006631
Craig Topper9f008862014-04-15 04:59:12 +00006632 const SCEV *BECount = nullptr;
Silviu Baranga6f444df2016-04-08 14:29:09 +00006633 for (auto &ENT : ExitNotTaken) {
6634 assert(ENT.ExactNotTaken != SE->getCouldNotCompute() && "bad exit SCEV");
Andrew Trick3ca3f982011-07-26 17:19:55 +00006635
6636 if (!BECount)
Silviu Baranga6f444df2016-04-08 14:29:09 +00006637 BECount = ENT.ExactNotTaken;
6638 else if (BECount != ENT.ExactNotTaken)
Andrew Trick90c7a102011-11-16 00:52:40 +00006639 return SE->getCouldNotCompute();
Sanjoy Dasc9bbf562016-09-25 23:12:04 +00006640 if (Preds && !ENT.hasAlwaysTruePredicate())
6641 Preds->add(ENT.Predicate.get());
Silviu Baranga6f444df2016-04-08 14:29:09 +00006642
Sanjoy Dasd1eb62a2016-09-25 23:12:00 +00006643 assert((Preds || ENT.hasAlwaysTruePredicate()) &&
Silviu Baranga6f444df2016-04-08 14:29:09 +00006644 "Predicate should be always true!");
Andrew Trick3ca3f982011-07-26 17:19:55 +00006645 }
Silviu Baranga6f444df2016-04-08 14:29:09 +00006646
Andrew Trickbbb226a2011-09-02 21:20:46 +00006647 assert(BECount && "Invalid not taken count for loop exit");
Andrew Trick3ca3f982011-07-26 17:19:55 +00006648 return BECount;
6649}
6650
Sanjoy Dasf8570812016-05-29 00:38:22 +00006651/// Get the exact not taken count for this loop exit.
Andrew Trick3ca3f982011-07-26 17:19:55 +00006652const SCEV *
Andrew Trick77c55422011-08-02 04:23:35 +00006653ScalarEvolution::BackedgeTakenInfo::getExact(BasicBlock *ExitingBlock,
Andrew Trick3ca3f982011-07-26 17:19:55 +00006654 ScalarEvolution *SE) const {
Silviu Baranga6f444df2016-04-08 14:29:09 +00006655 for (auto &ENT : ExitNotTaken)
Sanjoy Dasd1eb62a2016-09-25 23:12:00 +00006656 if (ENT.ExitingBlock == ExitingBlock && ENT.hasAlwaysTruePredicate())
Silviu Baranga6f444df2016-04-08 14:29:09 +00006657 return ENT.ExactNotTaken;
Andrew Trick3ca3f982011-07-26 17:19:55 +00006658
Andrew Trick3ca3f982011-07-26 17:19:55 +00006659 return SE->getCouldNotCompute();
6660}
6661
6662/// getMax - Get the max backedge taken count for the loop.
6663const SCEV *
6664ScalarEvolution::BackedgeTakenInfo::getMax(ScalarEvolution *SE) const {
Sanjoy Das73268612016-09-26 01:10:22 +00006665 auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) {
6666 return !ENT.hasAlwaysTruePredicate();
6667 };
Silviu Baranga6f444df2016-04-08 14:29:09 +00006668
Sanjoy Das73268612016-09-26 01:10:22 +00006669 if (any_of(ExitNotTaken, PredicateNotAlwaysTrue) || !getMax())
6670 return SE->getCouldNotCompute();
6671
Sanjoy Das036dda22017-05-22 06:46:04 +00006672 assert((isa<SCEVCouldNotCompute>(getMax()) || isa<SCEVConstant>(getMax())) &&
6673 "No point in having a non-constant max backedge taken count!");
Sanjoy Das73268612016-09-26 01:10:22 +00006674 return getMax();
Andrew Trick3ca3f982011-07-26 17:19:55 +00006675}
6676
John Brawn84b21832016-10-21 11:08:48 +00006677bool ScalarEvolution::BackedgeTakenInfo::isMaxOrZero(ScalarEvolution *SE) const {
6678 auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) {
6679 return !ENT.hasAlwaysTruePredicate();
6680 };
6681 return MaxOrZero && !any_of(ExitNotTaken, PredicateNotAlwaysTrue);
6682}
6683
Andrew Trick9093e152013-03-26 03:14:53 +00006684bool ScalarEvolution::BackedgeTakenInfo::hasOperand(const SCEV *S,
6685 ScalarEvolution *SE) const {
Sanjoy Dasd1eb62a2016-09-25 23:12:00 +00006686 if (getMax() && getMax() != SE->getCouldNotCompute() &&
6687 SE->hasOperand(getMax(), S))
Andrew Trick9093e152013-03-26 03:14:53 +00006688 return true;
6689
Silviu Baranga6f444df2016-04-08 14:29:09 +00006690 for (auto &ENT : ExitNotTaken)
6691 if (ENT.ExactNotTaken != SE->getCouldNotCompute() &&
6692 SE->hasOperand(ENT.ExactNotTaken, S))
Silviu Barangaa393baf2016-04-06 14:06:32 +00006693 return true;
Silviu Baranga6f444df2016-04-08 14:29:09 +00006694
Andrew Trick9093e152013-03-26 03:14:53 +00006695 return false;
6696}
6697
Sanjoy Dasf6f6fb92017-05-15 04:22:09 +00006698ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E)
Eugene Zelenkobe709f22017-08-18 23:51:26 +00006699 : ExactNotTaken(E), MaxNotTaken(E) {
Sanjoy Das036dda22017-05-22 06:46:04 +00006700 assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
6701 isa<SCEVConstant>(MaxNotTaken)) &&
6702 "No point in having a non-constant max backedge taken count!");
6703}
Sanjoy Dasf6f6fb92017-05-15 04:22:09 +00006704
6705ScalarEvolution::ExitLimit::ExitLimit(
6706 const SCEV *E, const SCEV *M, bool MaxOrZero,
6707 ArrayRef<const SmallPtrSetImpl<const SCEVPredicate *> *> PredSetList)
6708 : ExactNotTaken(E), MaxNotTaken(M), MaxOrZero(MaxOrZero) {
6709 assert((isa<SCEVCouldNotCompute>(ExactNotTaken) ||
6710 !isa<SCEVCouldNotCompute>(MaxNotTaken)) &&
6711 "Exact is not allowed to be less precise than Max");
Sanjoy Das036dda22017-05-22 06:46:04 +00006712 assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
6713 isa<SCEVConstant>(MaxNotTaken)) &&
6714 "No point in having a non-constant max backedge taken count!");
Sanjoy Dasf6f6fb92017-05-15 04:22:09 +00006715 for (auto *PredSet : PredSetList)
6716 for (auto *P : *PredSet)
6717 addPredicate(P);
6718}
6719
6720ScalarEvolution::ExitLimit::ExitLimit(
6721 const SCEV *E, const SCEV *M, bool MaxOrZero,
6722 const SmallPtrSetImpl<const SCEVPredicate *> &PredSet)
Sanjoy Das036dda22017-05-22 06:46:04 +00006723 : ExitLimit(E, M, MaxOrZero, {&PredSet}) {
6724 assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
6725 isa<SCEVConstant>(MaxNotTaken)) &&
6726 "No point in having a non-constant max backedge taken count!");
6727}
Sanjoy Dasf6f6fb92017-05-15 04:22:09 +00006728
6729ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E, const SCEV *M,
6730 bool MaxOrZero)
Sanjoy Das036dda22017-05-22 06:46:04 +00006731 : ExitLimit(E, M, MaxOrZero, None) {
6732 assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
6733 isa<SCEVConstant>(MaxNotTaken)) &&
6734 "No point in having a non-constant max backedge taken count!");
6735}
Sanjoy Dasf6f6fb92017-05-15 04:22:09 +00006736
Andrew Trick3ca3f982011-07-26 17:19:55 +00006737/// Allocate memory for BackedgeTakenInfo and copy the not-taken count of each
6738/// computable exit into a persistent ExitNotTakenInfo array.
6739ScalarEvolution::BackedgeTakenInfo::BackedgeTakenInfo(
Sanjoy Das5c4869b2016-09-26 01:10:27 +00006740 SmallVectorImpl<ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo>
6741 &&ExitCounts,
John Brawn84b21832016-10-21 11:08:48 +00006742 bool Complete, const SCEV *MaxCount, bool MaxOrZero)
6743 : MaxAndComplete(MaxCount, Complete), MaxOrZero(MaxOrZero) {
Eugene Zelenkobe709f22017-08-18 23:51:26 +00006744 using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo;
6745
Sanjoy Dase935c772016-09-25 23:12:08 +00006746 ExitNotTaken.reserve(ExitCounts.size());
Sanjoy Dasc9bbf562016-09-25 23:12:04 +00006747 std::transform(
6748 ExitCounts.begin(), ExitCounts.end(), std::back_inserter(ExitNotTaken),
Sanjoy Das6b76cdf2016-09-26 01:10:25 +00006749 [&](const EdgeExitInfo &EEI) {
Sanjoy Dasc9bbf562016-09-25 23:12:04 +00006750 BasicBlock *ExitBB = EEI.first;
6751 const ExitLimit &EL = EEI.second;
Sanjoy Dasf0022122016-09-28 17:14:58 +00006752 if (EL.Predicates.empty())
Sanjoy Dasc9bbf562016-09-25 23:12:04 +00006753 return ExitNotTakenInfo(ExitBB, EL.ExactNotTaken, nullptr);
Sanjoy Dasf0022122016-09-28 17:14:58 +00006754
6755 std::unique_ptr<SCEVUnionPredicate> Predicate(new SCEVUnionPredicate);
6756 for (auto *Pred : EL.Predicates)
6757 Predicate->add(Pred);
6758
6759 return ExitNotTakenInfo(ExitBB, EL.ExactNotTaken, std::move(Predicate));
Sanjoy Dasc9bbf562016-09-25 23:12:04 +00006760 });
Sanjoy Das036dda22017-05-22 06:46:04 +00006761 assert((isa<SCEVCouldNotCompute>(MaxCount) || isa<SCEVConstant>(MaxCount)) &&
6762 "No point in having a non-constant max backedge taken count!");
Andrew Trick3ca3f982011-07-26 17:19:55 +00006763}
6764
Sanjoy Dasf8570812016-05-29 00:38:22 +00006765/// Invalidate this result and free the ExitNotTakenInfo array.
Andrew Trick3ca3f982011-07-26 17:19:55 +00006766void ScalarEvolution::BackedgeTakenInfo::clear() {
Sanjoy Dasd1eb62a2016-09-25 23:12:00 +00006767 ExitNotTaken.clear();
Andrew Trick3ca3f982011-07-26 17:19:55 +00006768}
6769
Sanjoy Dasf8570812016-05-29 00:38:22 +00006770/// Compute the number of times the backedge of the specified loop will execute.
Dan Gohman2b8da352009-04-30 20:47:05 +00006771ScalarEvolution::BackedgeTakenInfo
Silviu Baranga6f444df2016-04-08 14:29:09 +00006772ScalarEvolution::computeBackedgeTakenCount(const Loop *L,
6773 bool AllowPredicates) {
Dan Gohmancb0efec2009-12-18 01:14:11 +00006774 SmallVector<BasicBlock *, 8> ExitingBlocks;
Dan Gohman96212b62009-06-22 00:31:57 +00006775 L->getExitingBlocks(ExitingBlocks);
Chris Lattnerd934c702004-04-02 20:23:17 +00006776
Eugene Zelenkobe709f22017-08-18 23:51:26 +00006777 using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo;
Sanjoy Das6b76cdf2016-09-26 01:10:25 +00006778
6779 SmallVector<EdgeExitInfo, 4> ExitCounts;
Andrew Trick3ca3f982011-07-26 17:19:55 +00006780 bool CouldComputeBECount = true;
Andrew Trickee5aa7f2014-01-15 06:42:11 +00006781 BasicBlock *Latch = L->getLoopLatch(); // may be NULL.
Andrew Trick839e30b2014-05-23 19:47:13 +00006782 const SCEV *MustExitMaxBECount = nullptr;
6783 const SCEV *MayExitMaxBECount = nullptr;
John Brawn84b21832016-10-21 11:08:48 +00006784 bool MustExitMaxOrZero = false;
Andrew Trick839e30b2014-05-23 19:47:13 +00006785
6786 // Compute the ExitLimit for each loop exit. Use this to populate ExitCounts
6787 // and compute maxBECount.
Silviu Baranga6f444df2016-04-08 14:29:09 +00006788 // Do a union of all the predicates here.
Dan Gohman96212b62009-06-22 00:31:57 +00006789 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
Andrew Trick839e30b2014-05-23 19:47:13 +00006790 BasicBlock *ExitBB = ExitingBlocks[i];
Silviu Baranga6f444df2016-04-08 14:29:09 +00006791 ExitLimit EL = computeExitLimit(L, ExitBB, AllowPredicates);
6792
Sanjoy Dasf0022122016-09-28 17:14:58 +00006793 assert((AllowPredicates || EL.Predicates.empty()) &&
Silviu Baranga6f444df2016-04-08 14:29:09 +00006794 "Predicated exit limit when predicates are not allowed!");
Andrew Trick839e30b2014-05-23 19:47:13 +00006795
6796 // 1. For each exit that can be computed, add an entry to ExitCounts.
6797 // CouldComputeBECount is true only if all exits can be computed.
Sanjoy Das89eea6b2016-09-25 23:11:57 +00006798 if (EL.ExactNotTaken == getCouldNotCompute())
Dan Gohman96212b62009-06-22 00:31:57 +00006799 // We couldn't compute an exact value for this exit, so
Dan Gohman8885b372009-06-22 21:10:22 +00006800 // we won't be able to compute an exact value for the loop.
Andrew Trick3ca3f982011-07-26 17:19:55 +00006801 CouldComputeBECount = false;
6802 else
Sanjoy Dasbdd97102016-09-25 23:11:55 +00006803 ExitCounts.emplace_back(ExitBB, EL);
Andrew Trick3ca3f982011-07-26 17:19:55 +00006804
Andrew Trick839e30b2014-05-23 19:47:13 +00006805 // 2. Derive the loop's MaxBECount from each exit's max number of
6806 // non-exiting iterations. Partition the loop exits into two kinds:
6807 // LoopMustExits and LoopMayExits.
6808 //
Mark Heffernan2beab5f2014-10-10 17:39:11 +00006809 // If the exit dominates the loop latch, it is a LoopMustExit otherwise it
6810 // is a LoopMayExit. If any computable LoopMustExit is found, then
Sanjoy Das89eea6b2016-09-25 23:11:57 +00006811 // MaxBECount is the minimum EL.MaxNotTaken of computable
6812 // LoopMustExits. Otherwise, MaxBECount is conservatively the maximum
6813 // EL.MaxNotTaken, where CouldNotCompute is considered greater than any
6814 // computable EL.MaxNotTaken.
6815 if (EL.MaxNotTaken != getCouldNotCompute() && Latch &&
Chandler Carruth2f1fd162015-08-17 02:08:17 +00006816 DT.dominates(ExitBB, Latch)) {
John Brawn84b21832016-10-21 11:08:48 +00006817 if (!MustExitMaxBECount) {
Sanjoy Das89eea6b2016-09-25 23:11:57 +00006818 MustExitMaxBECount = EL.MaxNotTaken;
John Brawn84b21832016-10-21 11:08:48 +00006819 MustExitMaxOrZero = EL.MaxOrZero;
6820 } else {
Andrew Trick839e30b2014-05-23 19:47:13 +00006821 MustExitMaxBECount =
Sanjoy Das89eea6b2016-09-25 23:11:57 +00006822 getUMinFromMismatchedTypes(MustExitMaxBECount, EL.MaxNotTaken);
Andrew Tricke2553592014-05-22 00:37:03 +00006823 }
Andrew Trick839e30b2014-05-23 19:47:13 +00006824 } else if (MayExitMaxBECount != getCouldNotCompute()) {
Sanjoy Das89eea6b2016-09-25 23:11:57 +00006825 if (!MayExitMaxBECount || EL.MaxNotTaken == getCouldNotCompute())
6826 MayExitMaxBECount = EL.MaxNotTaken;
Andrew Trick839e30b2014-05-23 19:47:13 +00006827 else {
6828 MayExitMaxBECount =
Sanjoy Das89eea6b2016-09-25 23:11:57 +00006829 getUMaxFromMismatchedTypes(MayExitMaxBECount, EL.MaxNotTaken);
Andrew Trick839e30b2014-05-23 19:47:13 +00006830 }
Andrew Trick90c7a102011-11-16 00:52:40 +00006831 }
Dan Gohman96212b62009-06-22 00:31:57 +00006832 }
Andrew Trick839e30b2014-05-23 19:47:13 +00006833 const SCEV *MaxBECount = MustExitMaxBECount ? MustExitMaxBECount :
6834 (MayExitMaxBECount ? MayExitMaxBECount : getCouldNotCompute());
John Brawn84b21832016-10-21 11:08:48 +00006835 // The loop backedge will be taken the maximum or zero times if there's
6836 // a single exit that must be taken the maximum or zero times.
6837 bool MaxOrZero = (MustExitMaxOrZero && ExitingBlocks.size() == 1);
Sanjoy Das5c4869b2016-09-26 01:10:27 +00006838 return BackedgeTakenInfo(std::move(ExitCounts), CouldComputeBECount,
John Brawn84b21832016-10-21 11:08:48 +00006839 MaxBECount, MaxOrZero);
Dan Gohman96212b62009-06-22 00:31:57 +00006840}
6841
Andrew Trick3ca3f982011-07-26 17:19:55 +00006842ScalarEvolution::ExitLimit
Silviu Baranga6f444df2016-04-08 14:29:09 +00006843ScalarEvolution::computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
Max Kazantsev2cb36532017-08-03 08:41:30 +00006844 bool AllowPredicates) {
Sanjoy Das413dbbb2015-10-08 18:46:59 +00006845 // Okay, we've chosen an exiting block. See what condition causes us to exit
6846 // at this block and remember the exit block and whether all other targets
Benjamin Kramer5a188542014-02-11 15:44:32 +00006847 // lead to the loop header.
6848 bool MustExecuteLoopHeader = true;
Craig Topper9f008862014-04-15 04:59:12 +00006849 BasicBlock *Exit = nullptr;
Sanjoy Das0ff07872016-01-19 20:53:46 +00006850 for (auto *SBB : successors(ExitingBlock))
6851 if (!L->contains(SBB)) {
Benjamin Kramer5a188542014-02-11 15:44:32 +00006852 if (Exit) // Multiple exit successors.
6853 return getCouldNotCompute();
Sanjoy Das0ff07872016-01-19 20:53:46 +00006854 Exit = SBB;
6855 } else if (SBB != L->getHeader()) {
Benjamin Kramer5a188542014-02-11 15:44:32 +00006856 MustExecuteLoopHeader = false;
6857 }
Dan Gohmance973df2009-06-24 04:48:43 +00006858
Chris Lattner18954852007-01-07 02:24:26 +00006859 // At this point, we know we have a conditional branch that determines whether
6860 // the loop is exited. However, we don't know if the branch is executed each
6861 // time through the loop. If not, then the execution count of the branch will
6862 // not be equal to the trip count of the loop.
6863 //
6864 // Currently we check for this by checking to see if the Exit branch goes to
6865 // the loop header. If so, we know it will always execute the same number of
Chris Lattner5a554762007-01-14 01:24:47 +00006866 // times as the loop. We also handle the case where the exit block *is* the
Dan Gohman96212b62009-06-22 00:31:57 +00006867 // loop header. This is common for un-rotated loops.
6868 //
6869 // If both of those tests fail, walk up the unique predecessor chain to the
6870 // header, stopping if there is an edge that doesn't exit the loop. If the
6871 // header is reached, the execution count of the branch will be equal to the
6872 // trip count of the loop.
6873 //
6874 // More extensive analysis could be done to handle more cases here.
6875 //
Benjamin Kramer5a188542014-02-11 15:44:32 +00006876 if (!MustExecuteLoopHeader && ExitingBlock != L->getHeader()) {
Dan Gohman96212b62009-06-22 00:31:57 +00006877 // The simple checks failed, try climbing the unique predecessor chain
6878 // up to the header.
6879 bool Ok = false;
Benjamin Kramer5a188542014-02-11 15:44:32 +00006880 for (BasicBlock *BB = ExitingBlock; BB; ) {
Dan Gohman96212b62009-06-22 00:31:57 +00006881 BasicBlock *Pred = BB->getUniquePredecessor();
6882 if (!Pred)
Dan Gohmanc5c85c02009-06-27 21:21:31 +00006883 return getCouldNotCompute();
Dan Gohman96212b62009-06-22 00:31:57 +00006884 TerminatorInst *PredTerm = Pred->getTerminator();
Pete Cooperebcd7482015-08-06 20:22:46 +00006885 for (const BasicBlock *PredSucc : PredTerm->successors()) {
Dan Gohman96212b62009-06-22 00:31:57 +00006886 if (PredSucc == BB)
6887 continue;
6888 // If the predecessor has a successor that isn't BB and isn't
6889 // outside the loop, assume the worst.
6890 if (L->contains(PredSucc))
Dan Gohmanc5c85c02009-06-27 21:21:31 +00006891 return getCouldNotCompute();
Dan Gohman96212b62009-06-22 00:31:57 +00006892 }
6893 if (Pred == L->getHeader()) {
6894 Ok = true;
6895 break;
6896 }
6897 BB = Pred;
6898 }
6899 if (!Ok)
Dan Gohmanc5c85c02009-06-27 21:21:31 +00006900 return getCouldNotCompute();
Dan Gohman96212b62009-06-22 00:31:57 +00006901 }
6902
Mark Heffernan2beab5f2014-10-10 17:39:11 +00006903 bool IsOnlyExit = (L->getExitingBlock() != nullptr);
Benjamin Kramer5a188542014-02-11 15:44:32 +00006904 TerminatorInst *Term = ExitingBlock->getTerminator();
6905 if (BranchInst *BI = dyn_cast<BranchInst>(Term)) {
6906 assert(BI->isConditional() && "If unconditional, it can't be in loop!");
6907 // Proceed to the next level to examine the exit condition expression.
Silviu Baranga6f444df2016-04-08 14:29:09 +00006908 return computeExitLimitFromCond(
6909 L, BI->getCondition(), BI->getSuccessor(0), BI->getSuccessor(1),
6910 /*ControlsExit=*/IsOnlyExit, AllowPredicates);
Benjamin Kramer5a188542014-02-11 15:44:32 +00006911 }
6912
6913 if (SwitchInst *SI = dyn_cast<SwitchInst>(Term))
Sanjoy Das413dbbb2015-10-08 18:46:59 +00006914 return computeExitLimitFromSingleExitSwitch(L, SI, Exit,
Mark Heffernan2beab5f2014-10-10 17:39:11 +00006915 /*ControlsExit=*/IsOnlyExit);
Benjamin Kramer5a188542014-02-11 15:44:32 +00006916
6917 return getCouldNotCompute();
Dan Gohman96212b62009-06-22 00:31:57 +00006918}
6919
Sanjoy Dasbdbc4932017-04-24 00:09:46 +00006920ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCond(
6921 const Loop *L, Value *ExitCond, BasicBlock *TBB, BasicBlock *FBB,
6922 bool ControlsExit, bool AllowPredicates) {
6923 ScalarEvolution::ExitLimitCacheTy Cache(L, TBB, FBB, AllowPredicates);
6924 return computeExitLimitFromCondCached(Cache, L, ExitCond, TBB, FBB,
6925 ControlsExit, AllowPredicates);
6926}
6927
6928Optional<ScalarEvolution::ExitLimit>
6929ScalarEvolution::ExitLimitCache::find(const Loop *L, Value *ExitCond,
6930 BasicBlock *TBB, BasicBlock *FBB,
6931 bool ControlsExit, bool AllowPredicates) {
Sanjoy Das25972aa2017-04-24 00:46:40 +00006932 (void)this->L;
6933 (void)this->TBB;
6934 (void)this->FBB;
6935 (void)this->AllowPredicates;
6936
Sanjoy Dasbdbc4932017-04-24 00:09:46 +00006937 assert(this->L == L && this->TBB == TBB && this->FBB == FBB &&
6938 this->AllowPredicates == AllowPredicates &&
6939 "Variance in assumed invariant key components!");
6940 auto Itr = TripCountMap.find({ExitCond, ControlsExit});
6941 if (Itr == TripCountMap.end())
6942 return None;
6943 return Itr->second;
6944}
6945
6946void ScalarEvolution::ExitLimitCache::insert(const Loop *L, Value *ExitCond,
6947 BasicBlock *TBB, BasicBlock *FBB,
6948 bool ControlsExit,
6949 bool AllowPredicates,
6950 const ExitLimit &EL) {
6951 assert(this->L == L && this->TBB == TBB && this->FBB == FBB &&
6952 this->AllowPredicates == AllowPredicates &&
6953 "Variance in assumed invariant key components!");
6954
6955 auto InsertResult = TripCountMap.insert({{ExitCond, ControlsExit}, EL});
6956 assert(InsertResult.second && "Expected successful insertion!");
Sanjoy Das25972aa2017-04-24 00:46:40 +00006957 (void)InsertResult;
Sanjoy Dasbdbc4932017-04-24 00:09:46 +00006958}
6959
6960ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondCached(
6961 ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, BasicBlock *TBB,
6962 BasicBlock *FBB, bool ControlsExit, bool AllowPredicates) {
6963
6964 if (auto MaybeEL =
6965 Cache.find(L, ExitCond, TBB, FBB, ControlsExit, AllowPredicates))
6966 return *MaybeEL;
6967
6968 ExitLimit EL = computeExitLimitFromCondImpl(Cache, L, ExitCond, TBB, FBB,
6969 ControlsExit, AllowPredicates);
6970 Cache.insert(L, ExitCond, TBB, FBB, ControlsExit, AllowPredicates, EL);
6971 return EL;
6972}
6973
6974ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondImpl(
6975 ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, BasicBlock *TBB,
6976 BasicBlock *FBB, bool ControlsExit, bool AllowPredicates) {
Dan Gohmanf19aeec2009-06-24 01:18:18 +00006977 // Check if the controlling expression for this loop is an And or Or.
Dan Gohman96212b62009-06-22 00:31:57 +00006978 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) {
6979 if (BO->getOpcode() == Instruction::And) {
6980 // Recurse on the operands of the and.
Andrew Trick5b245a12013-05-31 06:43:25 +00006981 bool EitherMayExit = L->contains(TBB);
Sanjoy Dasbdbc4932017-04-24 00:09:46 +00006982 ExitLimit EL0 = computeExitLimitFromCondCached(
6983 Cache, L, BO->getOperand(0), TBB, FBB, ControlsExit && !EitherMayExit,
6984 AllowPredicates);
6985 ExitLimit EL1 = computeExitLimitFromCondCached(
6986 Cache, L, BO->getOperand(1), TBB, FBB, ControlsExit && !EitherMayExit,
6987 AllowPredicates);
Dan Gohmanaf752342009-07-07 17:06:11 +00006988 const SCEV *BECount = getCouldNotCompute();
6989 const SCEV *MaxBECount = getCouldNotCompute();
Andrew Trick5b245a12013-05-31 06:43:25 +00006990 if (EitherMayExit) {
Dan Gohman96212b62009-06-22 00:31:57 +00006991 // Both conditions must be true for the loop to continue executing.
6992 // Choose the less conservative count.
Sanjoy Das89eea6b2016-09-25 23:11:57 +00006993 if (EL0.ExactNotTaken == getCouldNotCompute() ||
6994 EL1.ExactNotTaken == getCouldNotCompute())
Dan Gohmanc5c85c02009-06-27 21:21:31 +00006995 BECount = getCouldNotCompute();
Dan Gohmaned627382009-06-22 15:09:28 +00006996 else
Sanjoy Das89eea6b2016-09-25 23:11:57 +00006997 BECount =
6998 getUMinFromMismatchedTypes(EL0.ExactNotTaken, EL1.ExactNotTaken);
6999 if (EL0.MaxNotTaken == getCouldNotCompute())
7000 MaxBECount = EL1.MaxNotTaken;
7001 else if (EL1.MaxNotTaken == getCouldNotCompute())
7002 MaxBECount = EL0.MaxNotTaken;
Dan Gohmaned627382009-06-22 15:09:28 +00007003 else
Sanjoy Das89eea6b2016-09-25 23:11:57 +00007004 MaxBECount =
7005 getUMinFromMismatchedTypes(EL0.MaxNotTaken, EL1.MaxNotTaken);
Dan Gohman96212b62009-06-22 00:31:57 +00007006 } else {
Dan Gohmanf7495f22010-08-11 00:12:36 +00007007 // Both conditions must be true at the same time for the loop to exit.
7008 // For now, be conservative.
Dan Gohman96212b62009-06-22 00:31:57 +00007009 assert(L->contains(FBB) && "Loop block has no successor in loop!");
Sanjoy Das89eea6b2016-09-25 23:11:57 +00007010 if (EL0.MaxNotTaken == EL1.MaxNotTaken)
7011 MaxBECount = EL0.MaxNotTaken;
7012 if (EL0.ExactNotTaken == EL1.ExactNotTaken)
7013 BECount = EL0.ExactNotTaken;
Dan Gohman96212b62009-06-22 00:31:57 +00007014 }
7015
Sanjoy Das29a4b5d2016-01-19 20:53:51 +00007016 // There are cases (e.g. PR26207) where computeExitLimitFromCond is able
7017 // to be more aggressive when computing BECount than when computing
Sanjoy Das89eea6b2016-09-25 23:11:57 +00007018 // MaxBECount. In these cases it is possible for EL0.ExactNotTaken and
7019 // EL1.ExactNotTaken to match, but for EL0.MaxNotTaken and EL1.MaxNotTaken
7020 // to not.
Sanjoy Das29a4b5d2016-01-19 20:53:51 +00007021 if (isa<SCEVCouldNotCompute>(MaxBECount) &&
7022 !isa<SCEVCouldNotCompute>(BECount))
Craig Topper01020392017-06-24 23:34:50 +00007023 MaxBECount = getConstant(getUnsignedRangeMax(BECount));
Sanjoy Das29a4b5d2016-01-19 20:53:51 +00007024
John Brawn84b21832016-10-21 11:08:48 +00007025 return ExitLimit(BECount, MaxBECount, false,
7026 {&EL0.Predicates, &EL1.Predicates});
Dan Gohman96212b62009-06-22 00:31:57 +00007027 }
7028 if (BO->getOpcode() == Instruction::Or) {
7029 // Recurse on the operands of the or.
Andrew Trick5b245a12013-05-31 06:43:25 +00007030 bool EitherMayExit = L->contains(FBB);
Sanjoy Dasbdbc4932017-04-24 00:09:46 +00007031 ExitLimit EL0 = computeExitLimitFromCondCached(
7032 Cache, L, BO->getOperand(0), TBB, FBB, ControlsExit && !EitherMayExit,
7033 AllowPredicates);
7034 ExitLimit EL1 = computeExitLimitFromCondCached(
7035 Cache, L, BO->getOperand(1), TBB, FBB, ControlsExit && !EitherMayExit,
7036 AllowPredicates);
Dan Gohmanaf752342009-07-07 17:06:11 +00007037 const SCEV *BECount = getCouldNotCompute();
7038 const SCEV *MaxBECount = getCouldNotCompute();
Andrew Trick5b245a12013-05-31 06:43:25 +00007039 if (EitherMayExit) {
Dan Gohman96212b62009-06-22 00:31:57 +00007040 // Both conditions must be false for the loop to continue executing.
7041 // Choose the less conservative count.
Sanjoy Das89eea6b2016-09-25 23:11:57 +00007042 if (EL0.ExactNotTaken == getCouldNotCompute() ||
7043 EL1.ExactNotTaken == getCouldNotCompute())
Dan Gohmanc5c85c02009-06-27 21:21:31 +00007044 BECount = getCouldNotCompute();
Dan Gohmaned627382009-06-22 15:09:28 +00007045 else
Sanjoy Das89eea6b2016-09-25 23:11:57 +00007046 BECount =
7047 getUMinFromMismatchedTypes(EL0.ExactNotTaken, EL1.ExactNotTaken);
7048 if (EL0.MaxNotTaken == getCouldNotCompute())
7049 MaxBECount = EL1.MaxNotTaken;
7050 else if (EL1.MaxNotTaken == getCouldNotCompute())
7051 MaxBECount = EL0.MaxNotTaken;
Dan Gohmaned627382009-06-22 15:09:28 +00007052 else
Sanjoy Das89eea6b2016-09-25 23:11:57 +00007053 MaxBECount =
7054 getUMinFromMismatchedTypes(EL0.MaxNotTaken, EL1.MaxNotTaken);
Dan Gohman96212b62009-06-22 00:31:57 +00007055 } else {
Dan Gohmanf7495f22010-08-11 00:12:36 +00007056 // Both conditions must be false at the same time for the loop to exit.
7057 // For now, be conservative.
Dan Gohman96212b62009-06-22 00:31:57 +00007058 assert(L->contains(TBB) && "Loop block has no successor in loop!");
Sanjoy Das89eea6b2016-09-25 23:11:57 +00007059 if (EL0.MaxNotTaken == EL1.MaxNotTaken)
7060 MaxBECount = EL0.MaxNotTaken;
7061 if (EL0.ExactNotTaken == EL1.ExactNotTaken)
7062 BECount = EL0.ExactNotTaken;
Dan Gohman96212b62009-06-22 00:31:57 +00007063 }
7064
John Brawn84b21832016-10-21 11:08:48 +00007065 return ExitLimit(BECount, MaxBECount, false,
7066 {&EL0.Predicates, &EL1.Predicates});
Dan Gohman96212b62009-06-22 00:31:57 +00007067 }
7068 }
7069
7070 // With an icmp, it may be feasible to compute an exact backedge-taken count.
Dan Gohman8b0a4192010-03-01 17:49:51 +00007071 // Proceed to the next level to examine the icmp.
Silviu Baranga6f444df2016-04-08 14:29:09 +00007072 if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) {
7073 ExitLimit EL =
7074 computeExitLimitFromICmp(L, ExitCondICmp, TBB, FBB, ControlsExit);
7075 if (EL.hasFullInfo() || !AllowPredicates)
7076 return EL;
7077
7078 // Try again, but use SCEV predicates this time.
7079 return computeExitLimitFromICmp(L, ExitCondICmp, TBB, FBB, ControlsExit,
7080 /*AllowPredicates=*/true);
7081 }
Reid Spencer266e42b2006-12-23 06:05:41 +00007082
Dan Gohman6b1e2a82010-02-19 18:12:07 +00007083 // Check for a constant condition. These are normally stripped out by
7084 // SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to
7085 // preserve the CFG and is temporarily leaving constant conditions
7086 // in place.
7087 if (ConstantInt *CI = dyn_cast<ConstantInt>(ExitCond)) {
7088 if (L->contains(FBB) == !CI->getZExtValue())
7089 // The backedge is always taken.
7090 return getCouldNotCompute();
7091 else
7092 // The backedge is never taken.
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00007093 return getZero(CI->getType());
Dan Gohman6b1e2a82010-02-19 18:12:07 +00007094 }
7095
Eli Friedmanebf98b02009-05-09 12:32:42 +00007096 // If it's not an integer or pointer comparison then compute it the hard way.
Sanjoy Das413dbbb2015-10-08 18:46:59 +00007097 return computeExitCountExhaustively(L, ExitCond, !L->contains(TBB));
Dan Gohman96212b62009-06-22 00:31:57 +00007098}
7099
Andrew Trick3ca3f982011-07-26 17:19:55 +00007100ScalarEvolution::ExitLimit
Sanjoy Das413dbbb2015-10-08 18:46:59 +00007101ScalarEvolution::computeExitLimitFromICmp(const Loop *L,
Andrew Trick3ca3f982011-07-26 17:19:55 +00007102 ICmpInst *ExitCond,
7103 BasicBlock *TBB,
Andrew Trick5b245a12013-05-31 06:43:25 +00007104 BasicBlock *FBB,
Silviu Baranga6f444df2016-04-08 14:29:09 +00007105 bool ControlsExit,
7106 bool AllowPredicates) {
Reid Spencer266e42b2006-12-23 06:05:41 +00007107 // If the condition was exit on true, convert the condition to exit on false
Max Kazantsev63a3de02017-12-08 12:54:32 +00007108 ICmpInst::Predicate Pred;
Dan Gohman96212b62009-06-22 00:31:57 +00007109 if (!L->contains(FBB))
Max Kazantsev63a3de02017-12-08 12:54:32 +00007110 Pred = ExitCond->getPredicate();
Chris Lattnerec901cc2004-10-12 01:49:27 +00007111 else
Max Kazantsev63a3de02017-12-08 12:54:32 +00007112 Pred = ExitCond->getInversePredicate();
7113 const ICmpInst::Predicate OriginalPred = Pred;
Chris Lattnerec901cc2004-10-12 01:49:27 +00007114
7115 // Handle common loops like: for (X = "string"; *X; ++X)
7116 if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0)))
7117 if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) {
Andrew Trick3ca3f982011-07-26 17:19:55 +00007118 ExitLimit ItCnt =
Max Kazantsev63a3de02017-12-08 12:54:32 +00007119 computeLoadConstantCompareExitLimit(LI, RHS, L, Pred);
Dan Gohmanba820342010-02-24 17:31:30 +00007120 if (ItCnt.hasAnyInfo())
7121 return ItCnt;
Chris Lattnerec901cc2004-10-12 01:49:27 +00007122 }
7123
Dan Gohmanaf752342009-07-07 17:06:11 +00007124 const SCEV *LHS = getSCEV(ExitCond->getOperand(0));
7125 const SCEV *RHS = getSCEV(ExitCond->getOperand(1));
Chris Lattnerd934c702004-04-02 20:23:17 +00007126
7127 // Try to evaluate any dependencies out of the loop.
Dan Gohman8ca08852009-05-24 23:25:42 +00007128 LHS = getSCEVAtScope(LHS, L);
7129 RHS = getSCEVAtScope(RHS, L);
Chris Lattnerd934c702004-04-02 20:23:17 +00007130
Dan Gohmance973df2009-06-24 04:48:43 +00007131 // At this point, we would like to compute how many iterations of the
Reid Spencer266e42b2006-12-23 06:05:41 +00007132 // loop the predicate will return true for these inputs.
Dan Gohmanafd6db92010-11-17 21:23:15 +00007133 if (isLoopInvariant(LHS, L) && !isLoopInvariant(RHS, L)) {
Dan Gohmandc5f5cb2008-09-16 18:52:57 +00007134 // If there is a loop-invariant, force it into the RHS.
Chris Lattnerd934c702004-04-02 20:23:17 +00007135 std::swap(LHS, RHS);
Max Kazantsev63a3de02017-12-08 12:54:32 +00007136 Pred = ICmpInst::getSwappedPredicate(Pred);
Chris Lattnerd934c702004-04-02 20:23:17 +00007137 }
7138
Dan Gohman81585c12010-05-03 16:35:17 +00007139 // Simplify the operands before analyzing them.
Max Kazantsev63a3de02017-12-08 12:54:32 +00007140 (void)SimplifyICmpOperands(Pred, LHS, RHS);
Dan Gohman81585c12010-05-03 16:35:17 +00007141
Chris Lattnerd934c702004-04-02 20:23:17 +00007142 // If we have a comparison of a chrec against a constant, try to use value
7143 // ranges to answer this query.
Dan Gohmana30370b2009-05-04 22:02:23 +00007144 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
7145 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
Chris Lattnerd934c702004-04-02 20:23:17 +00007146 if (AddRec->getLoop() == L) {
Eli Friedmanebf98b02009-05-09 12:32:42 +00007147 // Form the constant range.
Sanjoy Das1f7b8132016-10-02 00:09:57 +00007148 ConstantRange CompRange =
Max Kazantsev63a3de02017-12-08 12:54:32 +00007149 ConstantRange::makeExactICmpRegion(Pred, RHSC->getAPInt());
Misha Brukman01808ca2005-04-21 21:13:18 +00007150
Dan Gohmanaf752342009-07-07 17:06:11 +00007151 const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this);
Eli Friedmanebf98b02009-05-09 12:32:42 +00007152 if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
Chris Lattnerd934c702004-04-02 20:23:17 +00007153 }
Misha Brukman01808ca2005-04-21 21:13:18 +00007154
Max Kazantsev63a3de02017-12-08 12:54:32 +00007155 switch (Pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00007156 case ICmpInst::ICMP_NE: { // while (X != Y)
Chris Lattnerd934c702004-04-02 20:23:17 +00007157 // Convert to: while (X-Y != 0)
Sanjoy Das108fcf22016-05-29 00:38:00 +00007158 ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsExit,
Silviu Baranga6f444df2016-04-08 14:29:09 +00007159 AllowPredicates);
Andrew Trick3ca3f982011-07-26 17:19:55 +00007160 if (EL.hasAnyInfo()) return EL;
Chris Lattnerd934c702004-04-02 20:23:17 +00007161 break;
Reid Spencer266e42b2006-12-23 06:05:41 +00007162 }
Dan Gohman8a8ad7d2009-08-20 16:42:55 +00007163 case ICmpInst::ICMP_EQ: { // while (X == Y)
7164 // Convert to: while (X-Y == 0)
Sanjoy Das108fcf22016-05-29 00:38:00 +00007165 ExitLimit EL = howFarToNonZero(getMinusSCEV(LHS, RHS), L);
Andrew Trick3ca3f982011-07-26 17:19:55 +00007166 if (EL.hasAnyInfo()) return EL;
Chris Lattnerd934c702004-04-02 20:23:17 +00007167 break;
Reid Spencer266e42b2006-12-23 06:05:41 +00007168 }
Andrew Trick34e2f0c2013-11-06 02:08:26 +00007169 case ICmpInst::ICMP_SLT:
7170 case ICmpInst::ICMP_ULT: { // while (X < Y)
Max Kazantsev63a3de02017-12-08 12:54:32 +00007171 bool IsSigned = Pred == ICmpInst::ICMP_SLT;
Sanjoy Das108fcf22016-05-29 00:38:00 +00007172 ExitLimit EL = howManyLessThans(LHS, RHS, L, IsSigned, ControlsExit,
Silviu Baranga6f444df2016-04-08 14:29:09 +00007173 AllowPredicates);
Andrew Trick3ca3f982011-07-26 17:19:55 +00007174 if (EL.hasAnyInfo()) return EL;
Chris Lattner587a75b2005-08-15 23:33:51 +00007175 break;
Reid Spencer266e42b2006-12-23 06:05:41 +00007176 }
Andrew Trick34e2f0c2013-11-06 02:08:26 +00007177 case ICmpInst::ICMP_SGT:
7178 case ICmpInst::ICMP_UGT: { // while (X > Y)
Max Kazantsev63a3de02017-12-08 12:54:32 +00007179 bool IsSigned = Pred == ICmpInst::ICMP_SGT;
Silviu Baranga6f444df2016-04-08 14:29:09 +00007180 ExitLimit EL =
Sanjoy Das108fcf22016-05-29 00:38:00 +00007181 howManyGreaterThans(LHS, RHS, L, IsSigned, ControlsExit,
Silviu Baranga6f444df2016-04-08 14:29:09 +00007182 AllowPredicates);
Andrew Trick3ca3f982011-07-26 17:19:55 +00007183 if (EL.hasAnyInfo()) return EL;
Chris Lattner587a75b2005-08-15 23:33:51 +00007184 break;
Reid Spencer266e42b2006-12-23 06:05:41 +00007185 }
Chris Lattnerd934c702004-04-02 20:23:17 +00007186 default:
Chris Lattner0defaa12004-04-03 00:43:03 +00007187 break;
Chris Lattnerd934c702004-04-02 20:23:17 +00007188 }
Sanjoy Das0da2d142016-06-30 02:47:28 +00007189
7190 auto *ExhaustiveCount =
7191 computeExitCountExhaustively(L, ExitCond, !L->contains(TBB));
7192
7193 if (!isa<SCEVCouldNotCompute>(ExhaustiveCount))
7194 return ExhaustiveCount;
7195
7196 return computeShiftCompareExitLimit(ExitCond->getOperand(0),
Max Kazantsev63a3de02017-12-08 12:54:32 +00007197 ExitCond->getOperand(1), L, OriginalPred);
Chris Lattner4021d1a2004-04-17 18:36:24 +00007198}
7199
Benjamin Kramer5a188542014-02-11 15:44:32 +00007200ScalarEvolution::ExitLimit
Sanjoy Das413dbbb2015-10-08 18:46:59 +00007201ScalarEvolution::computeExitLimitFromSingleExitSwitch(const Loop *L,
Benjamin Kramer5a188542014-02-11 15:44:32 +00007202 SwitchInst *Switch,
7203 BasicBlock *ExitingBlock,
Mark Heffernan2beab5f2014-10-10 17:39:11 +00007204 bool ControlsExit) {
Benjamin Kramer5a188542014-02-11 15:44:32 +00007205 assert(!L->contains(ExitingBlock) && "Not an exiting block!");
7206
7207 // Give up if the exit is the default dest of a switch.
7208 if (Switch->getDefaultDest() == ExitingBlock)
7209 return getCouldNotCompute();
7210
7211 assert(L->contains(Switch->getDefaultDest()) &&
7212 "Default case must not exit the loop!");
7213 const SCEV *LHS = getSCEVAtScope(Switch->getCondition(), L);
7214 const SCEV *RHS = getConstant(Switch->findCaseDest(ExitingBlock));
7215
7216 // while (X != Y) --> while (X-Y != 0)
Sanjoy Das108fcf22016-05-29 00:38:00 +00007217 ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsExit);
Benjamin Kramer5a188542014-02-11 15:44:32 +00007218 if (EL.hasAnyInfo())
7219 return EL;
7220
7221 return getCouldNotCompute();
7222}
7223
Chris Lattnerec901cc2004-10-12 01:49:27 +00007224static ConstantInt *
Dan Gohmana37eaf22007-10-22 18:31:58 +00007225EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C,
7226 ScalarEvolution &SE) {
Dan Gohmanaf752342009-07-07 17:06:11 +00007227 const SCEV *InVal = SE.getConstant(C);
7228 const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE);
Chris Lattnerec901cc2004-10-12 01:49:27 +00007229 assert(isa<SCEVConstant>(Val) &&
7230 "Evaluation of SCEV at constant didn't fold correctly?");
7231 return cast<SCEVConstant>(Val)->getValue();
7232}
7233
Sanjoy Dasf8570812016-05-29 00:38:22 +00007234/// Given an exit condition of 'icmp op load X, cst', try to see if we can
7235/// compute the backedge execution count.
Andrew Trick3ca3f982011-07-26 17:19:55 +00007236ScalarEvolution::ExitLimit
Sanjoy Das413dbbb2015-10-08 18:46:59 +00007237ScalarEvolution::computeLoadConstantCompareExitLimit(
Andrew Trick3ca3f982011-07-26 17:19:55 +00007238 LoadInst *LI,
7239 Constant *RHS,
7240 const Loop *L,
7241 ICmpInst::Predicate predicate) {
Dan Gohmanc5c85c02009-06-27 21:21:31 +00007242 if (LI->isVolatile()) return getCouldNotCompute();
Chris Lattnerec901cc2004-10-12 01:49:27 +00007243
7244 // Check to see if the loaded pointer is a getelementptr of a global.
Dan Gohmanba820342010-02-24 17:31:30 +00007245 // TODO: Use SCEV instead of manually grubbing with GEPs.
Chris Lattnerec901cc2004-10-12 01:49:27 +00007246 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0));
Dan Gohmanc5c85c02009-06-27 21:21:31 +00007247 if (!GEP) return getCouldNotCompute();
Chris Lattnerec901cc2004-10-12 01:49:27 +00007248
7249 // Make sure that it is really a constant global we are gepping, with an
7250 // initializer, and make sure the first IDX is really 0.
7251 GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
Dan Gohman5d5bc6d2009-08-19 18:20:44 +00007252 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
Chris Lattnerec901cc2004-10-12 01:49:27 +00007253 GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) ||
7254 !cast<Constant>(GEP->getOperand(1))->isNullValue())
Dan Gohmanc5c85c02009-06-27 21:21:31 +00007255 return getCouldNotCompute();
Chris Lattnerec901cc2004-10-12 01:49:27 +00007256
7257 // Okay, we allow one non-constant index into the GEP instruction.
Craig Topper9f008862014-04-15 04:59:12 +00007258 Value *VarIdx = nullptr;
Chris Lattnere166a852012-01-24 05:49:24 +00007259 std::vector<Constant*> Indexes;
Chris Lattnerec901cc2004-10-12 01:49:27 +00007260 unsigned VarIdxNum = 0;
7261 for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i)
7262 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
7263 Indexes.push_back(CI);
7264 } else if (!isa<ConstantInt>(GEP->getOperand(i))) {
Dan Gohmanc5c85c02009-06-27 21:21:31 +00007265 if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's.
Chris Lattnerec901cc2004-10-12 01:49:27 +00007266 VarIdx = GEP->getOperand(i);
7267 VarIdxNum = i-2;
Craig Topper9f008862014-04-15 04:59:12 +00007268 Indexes.push_back(nullptr);
Chris Lattnerec901cc2004-10-12 01:49:27 +00007269 }
7270
Andrew Trick7004e4b2012-03-26 22:33:59 +00007271 // Loop-invariant loads may be a byproduct of loop optimization. Skip them.
7272 if (!VarIdx)
7273 return getCouldNotCompute();
7274
Chris Lattnerec901cc2004-10-12 01:49:27 +00007275 // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant.
7276 // Check to see if X is a loop variant variable value now.
Dan Gohmanaf752342009-07-07 17:06:11 +00007277 const SCEV *Idx = getSCEV(VarIdx);
Dan Gohman8ca08852009-05-24 23:25:42 +00007278 Idx = getSCEVAtScope(Idx, L);
Chris Lattnerec901cc2004-10-12 01:49:27 +00007279
7280 // We can only recognize very limited forms of loop index expressions, in
7281 // particular, only affine AddRec's like {C1,+,C2}.
Dan Gohman48f82222009-05-04 22:30:44 +00007282 const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx);
Dan Gohmanafd6db92010-11-17 21:23:15 +00007283 if (!IdxExpr || !IdxExpr->isAffine() || isLoopInvariant(IdxExpr, L) ||
Chris Lattnerec901cc2004-10-12 01:49:27 +00007284 !isa<SCEVConstant>(IdxExpr->getOperand(0)) ||
7285 !isa<SCEVConstant>(IdxExpr->getOperand(1)))
Dan Gohmanc5c85c02009-06-27 21:21:31 +00007286 return getCouldNotCompute();
Chris Lattnerec901cc2004-10-12 01:49:27 +00007287
7288 unsigned MaxSteps = MaxBruteForceIterations;
7289 for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
Owen Andersonedb4a702009-07-24 23:12:02 +00007290 ConstantInt *ItCst = ConstantInt::get(
Owen Andersonb6b25302009-07-14 23:09:55 +00007291 cast<IntegerType>(IdxExpr->getType()), IterationNum);
Dan Gohmanc8e23622009-04-21 23:15:49 +00007292 ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this);
Chris Lattnerec901cc2004-10-12 01:49:27 +00007293
7294 // Form the GEP offset.
7295 Indexes[VarIdxNum] = Val;
7296
Chris Lattnere166a852012-01-24 05:49:24 +00007297 Constant *Result = ConstantFoldLoadThroughGEPIndices(GV->getInitializer(),
7298 Indexes);
Craig Topper9f008862014-04-15 04:59:12 +00007299 if (!Result) break; // Cannot compute!
Chris Lattnerec901cc2004-10-12 01:49:27 +00007300
7301 // Evaluate the condition for this iteration.
Reid Spencer266e42b2006-12-23 06:05:41 +00007302 Result = ConstantExpr::getICmp(predicate, Result, RHS);
Zhou Sheng75b871f2007-01-11 12:24:14 +00007303 if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure
Reid Spencer983e3b32007-03-01 07:25:48 +00007304 if (cast<ConstantInt>(Result)->getValue().isMinValue()) {
Chris Lattnerec901cc2004-10-12 01:49:27 +00007305 ++NumArrayLenItCounts;
Dan Gohmanc8e23622009-04-21 23:15:49 +00007306 return getConstant(ItCst); // Found terminating iteration!
Chris Lattnerec901cc2004-10-12 01:49:27 +00007307 }
7308 }
Dan Gohmanc5c85c02009-06-27 21:21:31 +00007309 return getCouldNotCompute();
Chris Lattnerec901cc2004-10-12 01:49:27 +00007310}
7311
Sanjoy Dasc88f5d32015-10-28 21:27:14 +00007312ScalarEvolution::ExitLimit ScalarEvolution::computeShiftCompareExitLimit(
7313 Value *LHS, Value *RHSV, const Loop *L, ICmpInst::Predicate Pred) {
7314 ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV);
7315 if (!RHS)
7316 return getCouldNotCompute();
7317
7318 const BasicBlock *Latch = L->getLoopLatch();
7319 if (!Latch)
7320 return getCouldNotCompute();
7321
7322 const BasicBlock *Predecessor = L->getLoopPredecessor();
7323 if (!Predecessor)
7324 return getCouldNotCompute();
7325
7326 // Return true if V is of the form "LHS `shift_op` <positive constant>".
7327 // Return LHS in OutLHS and shift_opt in OutOpCode.
7328 auto MatchPositiveShift =
7329 [](Value *V, Value *&OutLHS, Instruction::BinaryOps &OutOpCode) {
7330
7331 using namespace PatternMatch;
7332
7333 ConstantInt *ShiftAmt;
7334 if (match(V, m_LShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt))))
7335 OutOpCode = Instruction::LShr;
7336 else if (match(V, m_AShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt))))
7337 OutOpCode = Instruction::AShr;
7338 else if (match(V, m_Shl(m_Value(OutLHS), m_ConstantInt(ShiftAmt))))
7339 OutOpCode = Instruction::Shl;
7340 else
7341 return false;
7342
7343 return ShiftAmt->getValue().isStrictlyPositive();
7344 };
7345
7346 // Recognize a "shift recurrence" either of the form %iv or of %iv.shifted in
7347 //
7348 // loop:
7349 // %iv = phi i32 [ %iv.shifted, %loop ], [ %val, %preheader ]
7350 // %iv.shifted = lshr i32 %iv, <positive constant>
7351 //
Simon Pilgrimf2fbf432016-11-20 13:47:59 +00007352 // Return true on a successful match. Return the corresponding PHI node (%iv
Sanjoy Dasc88f5d32015-10-28 21:27:14 +00007353 // above) in PNOut and the opcode of the shift operation in OpCodeOut.
7354 auto MatchShiftRecurrence =
7355 [&](Value *V, PHINode *&PNOut, Instruction::BinaryOps &OpCodeOut) {
7356 Optional<Instruction::BinaryOps> PostShiftOpCode;
7357
7358 {
7359 Instruction::BinaryOps OpC;
7360 Value *V;
7361
7362 // If we encounter a shift instruction, "peel off" the shift operation,
7363 // and remember that we did so. Later when we inspect %iv's backedge
7364 // value, we will make sure that the backedge value uses the same
7365 // operation.
7366 //
7367 // Note: the peeled shift operation does not have to be the same
7368 // instruction as the one feeding into the PHI's backedge value. We only
7369 // really care about it being the same *kind* of shift instruction --
7370 // that's all that is required for our later inferences to hold.
7371 if (MatchPositiveShift(LHS, V, OpC)) {
7372 PostShiftOpCode = OpC;
7373 LHS = V;
7374 }
7375 }
7376
7377 PNOut = dyn_cast<PHINode>(LHS);
7378 if (!PNOut || PNOut->getParent() != L->getHeader())
7379 return false;
7380
7381 Value *BEValue = PNOut->getIncomingValueForBlock(Latch);
7382 Value *OpLHS;
7383
7384 return
7385 // The backedge value for the PHI node must be a shift by a positive
7386 // amount
7387 MatchPositiveShift(BEValue, OpLHS, OpCodeOut) &&
7388
7389 // of the PHI node itself
7390 OpLHS == PNOut &&
7391
7392 // and the kind of shift should be match the kind of shift we peeled
7393 // off, if any.
7394 (!PostShiftOpCode.hasValue() || *PostShiftOpCode == OpCodeOut);
7395 };
7396
7397 PHINode *PN;
7398 Instruction::BinaryOps OpCode;
7399 if (!MatchShiftRecurrence(LHS, PN, OpCode))
7400 return getCouldNotCompute();
7401
7402 const DataLayout &DL = getDataLayout();
7403
7404 // The key rationale for this optimization is that for some kinds of shift
7405 // recurrences, the value of the recurrence "stabilizes" to either 0 or -1
7406 // within a finite number of iterations. If the condition guarding the
7407 // backedge (in the sense that the backedge is taken if the condition is true)
7408 // is false for the value the shift recurrence stabilizes to, then we know
7409 // that the backedge is taken only a finite number of times.
7410
7411 ConstantInt *StableValue = nullptr;
7412 switch (OpCode) {
7413 default:
7414 llvm_unreachable("Impossible case!");
7415
7416 case Instruction::AShr: {
7417 // {K,ashr,<positive-constant>} stabilizes to signum(K) in at most
7418 // bitwidth(K) iterations.
7419 Value *FirstValue = PN->getIncomingValueForBlock(Predecessor);
Craig Topper1a36b7d2017-05-15 06:39:41 +00007420 KnownBits Known = computeKnownBits(FirstValue, DL, 0, nullptr,
7421 Predecessor->getTerminator(), &DT);
Sanjoy Dasc88f5d32015-10-28 21:27:14 +00007422 auto *Ty = cast<IntegerType>(RHS->getType());
Craig Topper1a36b7d2017-05-15 06:39:41 +00007423 if (Known.isNonNegative())
Sanjoy Dasc88f5d32015-10-28 21:27:14 +00007424 StableValue = ConstantInt::get(Ty, 0);
Craig Topper1a36b7d2017-05-15 06:39:41 +00007425 else if (Known.isNegative())
Sanjoy Dasc88f5d32015-10-28 21:27:14 +00007426 StableValue = ConstantInt::get(Ty, -1, true);
7427 else
7428 return getCouldNotCompute();
7429
7430 break;
7431 }
7432 case Instruction::LShr:
7433 case Instruction::Shl:
7434 // Both {K,lshr,<positive-constant>} and {K,shl,<positive-constant>}
7435 // stabilize to 0 in at most bitwidth(K) iterations.
7436 StableValue = ConstantInt::get(cast<IntegerType>(RHS->getType()), 0);
7437 break;
7438 }
7439
7440 auto *Result =
7441 ConstantFoldCompareInstOperands(Pred, StableValue, RHS, DL, &TLI);
7442 assert(Result->getType()->isIntegerTy(1) &&
7443 "Otherwise cannot be an operand to a branch instruction");
7444
7445 if (Result->isZeroValue()) {
7446 unsigned BitWidth = getTypeSizeInBits(RHS->getType());
7447 const SCEV *UpperBound =
7448 getConstant(getEffectiveSCEVType(RHS->getType()), BitWidth);
John Brawn84b21832016-10-21 11:08:48 +00007449 return ExitLimit(getCouldNotCompute(), UpperBound, false);
Sanjoy Dasc88f5d32015-10-28 21:27:14 +00007450 }
7451
7452 return getCouldNotCompute();
7453}
Chris Lattnerec901cc2004-10-12 01:49:27 +00007454
Sanjoy Dasf8570812016-05-29 00:38:22 +00007455/// Return true if we can constant fold an instruction of the specified type,
7456/// assuming that all operands were constants.
Chris Lattnerdd730472004-04-17 22:58:41 +00007457static bool CanConstantFold(const Instruction *I) {
Reid Spencer2341c222007-02-02 02:16:23 +00007458 if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
Nick Lewyckya6674c72011-10-22 19:58:20 +00007459 isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I) ||
7460 isa<LoadInst>(I))
Chris Lattnerdd730472004-04-17 22:58:41 +00007461 return true;
Misha Brukman01808ca2005-04-21 21:13:18 +00007462
Chris Lattnerdd730472004-04-17 22:58:41 +00007463 if (const CallInst *CI = dyn_cast<CallInst>(I))
7464 if (const Function *F = CI->getCalledFunction())
Andrew Kaylor647025f2017-06-09 23:18:11 +00007465 return canConstantFoldCallTo(CI, F);
Chris Lattnerdd730472004-04-17 22:58:41 +00007466 return false;
Chris Lattner4021d1a2004-04-17 18:36:24 +00007467}
7468
Andrew Trick3a86ba72011-10-05 03:25:31 +00007469/// Determine whether this instruction can constant evolve within this loop
7470/// assuming its operands can all constant evolve.
7471static bool canConstantEvolve(Instruction *I, const Loop *L) {
7472 // An instruction outside of the loop can't be derived from a loop PHI.
7473 if (!L->contains(I)) return false;
7474
7475 if (isa<PHINode>(I)) {
David Blaikie19ef0d32015-03-24 16:33:19 +00007476 // We don't currently keep track of the control flow needed to evaluate
7477 // PHIs, so we cannot handle PHIs inside of loops.
7478 return L->getHeader() == I->getParent();
Andrew Trick3a86ba72011-10-05 03:25:31 +00007479 }
7480
7481 // If we won't be able to constant fold this expression even if the operands
7482 // are constants, bail early.
7483 return CanConstantFold(I);
7484}
7485
7486/// getConstantEvolvingPHIOperands - Implement getConstantEvolvingPHI by
7487/// recursing through each instruction operand until reaching a loop header phi.
7488static PHINode *
7489getConstantEvolvingPHIOperands(Instruction *UseInst, const Loop *L,
Michael Liao468fb742017-01-13 18:28:30 +00007490 DenseMap<Instruction *, PHINode *> &PHIMap,
7491 unsigned Depth) {
7492 if (Depth > MaxConstantEvolvingDepth)
7493 return nullptr;
Andrew Trick3a86ba72011-10-05 03:25:31 +00007494
7495 // Otherwise, we can evaluate this instruction if all of its operands are
7496 // constant or derived from a PHI node themselves.
Craig Topper9f008862014-04-15 04:59:12 +00007497 PHINode *PHI = nullptr;
Sanjoy Dasd87e4352015-12-08 22:53:36 +00007498 for (Value *Op : UseInst->operands()) {
7499 if (isa<Constant>(Op)) continue;
Andrew Trick3a86ba72011-10-05 03:25:31 +00007500
Sanjoy Dasd87e4352015-12-08 22:53:36 +00007501 Instruction *OpInst = dyn_cast<Instruction>(Op);
Craig Topper9f008862014-04-15 04:59:12 +00007502 if (!OpInst || !canConstantEvolve(OpInst, L)) return nullptr;
Andrew Trick3a86ba72011-10-05 03:25:31 +00007503
7504 PHINode *P = dyn_cast<PHINode>(OpInst);
Andrew Trick3e8a5762011-10-05 22:06:53 +00007505 if (!P)
7506 // If this operand is already visited, reuse the prior result.
7507 // We may have P != PHI if this is the deepest point at which the
7508 // inconsistent paths meet.
7509 P = PHIMap.lookup(OpInst);
7510 if (!P) {
7511 // Recurse and memoize the results, whether a phi is found or not.
7512 // This recursive call invalidates pointers into PHIMap.
Michael Liao468fb742017-01-13 18:28:30 +00007513 P = getConstantEvolvingPHIOperands(OpInst, L, PHIMap, Depth + 1);
Andrew Trick3e8a5762011-10-05 22:06:53 +00007514 PHIMap[OpInst] = P;
Andrew Tricke9162f12011-10-05 05:58:49 +00007515 }
Craig Topper9f008862014-04-15 04:59:12 +00007516 if (!P)
7517 return nullptr; // Not evolving from PHI
7518 if (PHI && PHI != P)
7519 return nullptr; // Evolving from multiple different PHIs.
Andrew Tricke9162f12011-10-05 05:58:49 +00007520 PHI = P;
Andrew Trick3a86ba72011-10-05 03:25:31 +00007521 }
7522 // This is a expression evolving from a constant PHI!
7523 return PHI;
7524}
7525
Chris Lattnerdd730472004-04-17 22:58:41 +00007526/// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node
7527/// in the loop that V is derived from. We allow arbitrary operations along the
7528/// way, but the operands of an operation must either be constants or a value
7529/// derived from a constant PHI. If this expression does not fit with these
7530/// constraints, return null.
7531static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) {
Chris Lattnerdd730472004-04-17 22:58:41 +00007532 Instruction *I = dyn_cast<Instruction>(V);
Craig Topper9f008862014-04-15 04:59:12 +00007533 if (!I || !canConstantEvolve(I, L)) return nullptr;
Chris Lattnerdd730472004-04-17 22:58:41 +00007534
Sanjoy Dasd295f2c2015-10-18 00:29:27 +00007535 if (PHINode *PN = dyn_cast<PHINode>(I))
Andrew Trick3a86ba72011-10-05 03:25:31 +00007536 return PN;
Chris Lattnerdd730472004-04-17 22:58:41 +00007537
Andrew Trick3a86ba72011-10-05 03:25:31 +00007538 // Record non-constant instructions contained by the loop.
Andrew Tricke9162f12011-10-05 05:58:49 +00007539 DenseMap<Instruction *, PHINode *> PHIMap;
Michael Liao468fb742017-01-13 18:28:30 +00007540 return getConstantEvolvingPHIOperands(I, L, PHIMap, 0);
Chris Lattnerdd730472004-04-17 22:58:41 +00007541}
7542
7543/// EvaluateExpression - Given an expression that passes the
7544/// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node
7545/// in the loop has the value PHIVal. If we can't fold this expression for some
7546/// reason, return null.
Andrew Trick3a86ba72011-10-05 03:25:31 +00007547static Constant *EvaluateExpression(Value *V, const Loop *L,
7548 DenseMap<Instruction *, Constant *> &Vals,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00007549 const DataLayout &DL,
Chad Rosiere6de63d2011-12-01 21:29:16 +00007550 const TargetLibraryInfo *TLI) {
Andrew Tricke9162f12011-10-05 05:58:49 +00007551 // Convenient constant check, but redundant for recursive calls.
Reid Spencer30d69a52004-07-18 00:18:30 +00007552 if (Constant *C = dyn_cast<Constant>(V)) return C;
Nick Lewyckya6674c72011-10-22 19:58:20 +00007553 Instruction *I = dyn_cast<Instruction>(V);
Craig Topper9f008862014-04-15 04:59:12 +00007554 if (!I) return nullptr;
Andrew Trick3a86ba72011-10-05 03:25:31 +00007555
Andrew Trick3a86ba72011-10-05 03:25:31 +00007556 if (Constant *C = Vals.lookup(I)) return C;
7557
Nick Lewyckya6674c72011-10-22 19:58:20 +00007558 // An instruction inside the loop depends on a value outside the loop that we
7559 // weren't given a mapping for, or a value such as a call inside the loop.
Craig Topper9f008862014-04-15 04:59:12 +00007560 if (!canConstantEvolve(I, L)) return nullptr;
Nick Lewyckya6674c72011-10-22 19:58:20 +00007561
7562 // An unmapped PHI can be due to a branch or another loop inside this loop,
7563 // or due to this not being the initial iteration through a loop where we
7564 // couldn't compute the evolution of this particular PHI last time.
Craig Topper9f008862014-04-15 04:59:12 +00007565 if (isa<PHINode>(I)) return nullptr;
Chris Lattnerdd730472004-04-17 22:58:41 +00007566
Dan Gohmanf820bd32010-06-22 13:15:46 +00007567 std::vector<Constant*> Operands(I->getNumOperands());
Chris Lattnerdd730472004-04-17 22:58:41 +00007568
7569 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Andrew Tricke9162f12011-10-05 05:58:49 +00007570 Instruction *Operand = dyn_cast<Instruction>(I->getOperand(i));
7571 if (!Operand) {
Nick Lewyckya447e0f32011-10-14 09:38:46 +00007572 Operands[i] = dyn_cast<Constant>(I->getOperand(i));
Craig Topper9f008862014-04-15 04:59:12 +00007573 if (!Operands[i]) return nullptr;
Andrew Tricke9162f12011-10-05 05:58:49 +00007574 continue;
7575 }
Rafael Espindola7c68beb2014-02-18 15:33:12 +00007576 Constant *C = EvaluateExpression(Operand, L, Vals, DL, TLI);
Andrew Tricke9162f12011-10-05 05:58:49 +00007577 Vals[Operand] = C;
Craig Topper9f008862014-04-15 04:59:12 +00007578 if (!C) return nullptr;
Andrew Tricke9162f12011-10-05 05:58:49 +00007579 Operands[i] = C;
Chris Lattnerdd730472004-04-17 22:58:41 +00007580 }
7581
Nick Lewyckya6674c72011-10-22 19:58:20 +00007582 if (CmpInst *CI = dyn_cast<CmpInst>(I))
Chris Lattnercdfb80d2009-11-09 23:06:58 +00007583 return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0],
Rafael Espindola7c68beb2014-02-18 15:33:12 +00007584 Operands[1], DL, TLI);
Nick Lewyckya6674c72011-10-22 19:58:20 +00007585 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
7586 if (!LI->isVolatile())
Eduard Burtescu14239212016-01-22 01:17:26 +00007587 return ConstantFoldLoadFromConstPtr(Operands[0], LI->getType(), DL);
Nick Lewyckya6674c72011-10-22 19:58:20 +00007588 }
Manuel Jacobe9024592016-01-21 06:33:22 +00007589 return ConstantFoldInstOperands(I, Operands, DL, TLI);
Chris Lattnerdd730472004-04-17 22:58:41 +00007590}
7591
Sanjoy Das52bfa0f2015-11-02 02:06:01 +00007592
7593// If every incoming value to PN except the one for BB is a specific Constant,
7594// return that, else return nullptr.
7595static Constant *getOtherIncomingValue(PHINode *PN, BasicBlock *BB) {
7596 Constant *IncomingVal = nullptr;
7597
7598 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
7599 if (PN->getIncomingBlock(i) == BB)
7600 continue;
7601
7602 auto *CurrentVal = dyn_cast<Constant>(PN->getIncomingValue(i));
7603 if (!CurrentVal)
7604 return nullptr;
7605
7606 if (IncomingVal != CurrentVal) {
7607 if (IncomingVal)
7608 return nullptr;
7609 IncomingVal = CurrentVal;
7610 }
7611 }
7612
7613 return IncomingVal;
7614}
7615
Chris Lattnerdd730472004-04-17 22:58:41 +00007616/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
7617/// in the header of its containing loop, we know the loop executes a
7618/// constant number of times, and the PHI node is just a recurrence
7619/// involving constants, fold it.
Dan Gohmance973df2009-06-24 04:48:43 +00007620Constant *
7621ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN,
Dan Gohmancb0efec2009-12-18 01:14:11 +00007622 const APInt &BEs,
Dan Gohmance973df2009-06-24 04:48:43 +00007623 const Loop *L) {
Sanjoy Das4493b402015-10-07 17:38:25 +00007624 auto I = ConstantEvolutionLoopExitValue.find(PN);
Chris Lattnerdd730472004-04-17 22:58:41 +00007625 if (I != ConstantEvolutionLoopExitValue.end())
7626 return I->second;
7627
Dan Gohman4ce1fb12010-04-08 23:03:40 +00007628 if (BEs.ugt(MaxBruteForceIterations))
Craig Topper9f008862014-04-15 04:59:12 +00007629 return ConstantEvolutionLoopExitValue[PN] = nullptr; // Not going to evaluate it.
Chris Lattnerdd730472004-04-17 22:58:41 +00007630
7631 Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
7632
Andrew Trick3a86ba72011-10-05 03:25:31 +00007633 DenseMap<Instruction *, Constant *> CurrentIterVals;
Nick Lewyckya6674c72011-10-22 19:58:20 +00007634 BasicBlock *Header = L->getHeader();
7635 assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
Andrew Trick3a86ba72011-10-05 03:25:31 +00007636
Sanjoy Dasdd709962015-10-08 18:28:36 +00007637 BasicBlock *Latch = L->getLoopLatch();
7638 if (!Latch)
7639 return nullptr;
7640
Sanjoy Das4493b402015-10-07 17:38:25 +00007641 for (auto &I : *Header) {
7642 PHINode *PHI = dyn_cast<PHINode>(&I);
7643 if (!PHI) break;
Sanjoy Das52bfa0f2015-11-02 02:06:01 +00007644 auto *StartCST = getOtherIncomingValue(PHI, Latch);
Craig Topper9f008862014-04-15 04:59:12 +00007645 if (!StartCST) continue;
Nick Lewyckya6674c72011-10-22 19:58:20 +00007646 CurrentIterVals[PHI] = StartCST;
7647 }
7648 if (!CurrentIterVals.count(PN))
Craig Topper9f008862014-04-15 04:59:12 +00007649 return RetVal = nullptr;
Chris Lattnerdd730472004-04-17 22:58:41 +00007650
Sanjoy Dasdd709962015-10-08 18:28:36 +00007651 Value *BEValue = PN->getIncomingValueForBlock(Latch);
Chris Lattnerdd730472004-04-17 22:58:41 +00007652
7653 // Execute the loop symbolically to determine the exit value.
Sanjoy Dasb5a968f2017-07-29 05:32:47 +00007654 assert(BEs.getActiveBits() < CHAR_BIT * sizeof(unsigned) &&
7655 "BEs is <= MaxBruteForceIterations which is an 'unsigned'!");
Chris Lattnerdd730472004-04-17 22:58:41 +00007656
Dan Gohman0bddac12009-02-24 18:55:53 +00007657 unsigned NumIterations = BEs.getZExtValue(); // must be in range
Reid Spencer983e3b32007-03-01 07:25:48 +00007658 unsigned IterationNum = 0;
Sanjoy Das49edd3b2015-10-27 00:52:09 +00007659 const DataLayout &DL = getDataLayout();
Andrew Trick3a86ba72011-10-05 03:25:31 +00007660 for (; ; ++IterationNum) {
Chris Lattnerdd730472004-04-17 22:58:41 +00007661 if (IterationNum == NumIterations)
Andrew Trick3a86ba72011-10-05 03:25:31 +00007662 return RetVal = CurrentIterVals[PN]; // Got exit value!
Chris Lattnerdd730472004-04-17 22:58:41 +00007663
Nick Lewyckya6674c72011-10-22 19:58:20 +00007664 // Compute the value of the PHIs for the next iteration.
Andrew Trick3a86ba72011-10-05 03:25:31 +00007665 // EvaluateExpression adds non-phi values to the CurrentIterVals map.
Nick Lewyckya6674c72011-10-22 19:58:20 +00007666 DenseMap<Instruction *, Constant *> NextIterVals;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00007667 Constant *NextPHI =
Chandler Carruth2f1fd162015-08-17 02:08:17 +00007668 EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
Craig Topper9f008862014-04-15 04:59:12 +00007669 if (!NextPHI)
7670 return nullptr; // Couldn't evaluate!
Andrew Trick3a86ba72011-10-05 03:25:31 +00007671 NextIterVals[PN] = NextPHI;
Nick Lewyckya6674c72011-10-22 19:58:20 +00007672
Duncan Sandsa370f3e2011-10-25 12:28:52 +00007673 bool StoppedEvolving = NextPHI == CurrentIterVals[PN];
7674
Nick Lewyckya6674c72011-10-22 19:58:20 +00007675 // Also evaluate the other PHI nodes. However, we don't get to stop if we
7676 // cease to be able to evaluate one of them or if they stop evolving,
7677 // because that doesn't necessarily prevent us from computing PN.
Nick Lewyckyd48ab842011-11-12 03:09:12 +00007678 SmallVector<std::pair<PHINode *, Constant *>, 8> PHIsToCompute;
Sanjoy Das4493b402015-10-07 17:38:25 +00007679 for (const auto &I : CurrentIterVals) {
7680 PHINode *PHI = dyn_cast<PHINode>(I.first);
Nick Lewycky8e904de2011-10-24 05:51:01 +00007681 if (!PHI || PHI == PN || PHI->getParent() != Header) continue;
Sanjoy Das4493b402015-10-07 17:38:25 +00007682 PHIsToCompute.emplace_back(PHI, I.second);
Nick Lewyckyd48ab842011-11-12 03:09:12 +00007683 }
7684 // We use two distinct loops because EvaluateExpression may invalidate any
7685 // iterators into CurrentIterVals.
Sanjoy Das4493b402015-10-07 17:38:25 +00007686 for (const auto &I : PHIsToCompute) {
7687 PHINode *PHI = I.first;
Nick Lewyckya6674c72011-10-22 19:58:20 +00007688 Constant *&NextPHI = NextIterVals[PHI];
Duncan Sandsa370f3e2011-10-25 12:28:52 +00007689 if (!NextPHI) { // Not already computed.
Sanjoy Dasdd709962015-10-08 18:28:36 +00007690 Value *BEValue = PHI->getIncomingValueForBlock(Latch);
Chandler Carruth2f1fd162015-08-17 02:08:17 +00007691 NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
Duncan Sandsa370f3e2011-10-25 12:28:52 +00007692 }
Sanjoy Das4493b402015-10-07 17:38:25 +00007693 if (NextPHI != I.second)
Duncan Sandsa370f3e2011-10-25 12:28:52 +00007694 StoppedEvolving = false;
Nick Lewyckya6674c72011-10-22 19:58:20 +00007695 }
Duncan Sandsa370f3e2011-10-25 12:28:52 +00007696
7697 // If all entries in CurrentIterVals == NextIterVals then we can stop
7698 // iterating, the loop can't continue to change.
7699 if (StoppedEvolving)
7700 return RetVal = CurrentIterVals[PN];
7701
Andrew Trick3a86ba72011-10-05 03:25:31 +00007702 CurrentIterVals.swap(NextIterVals);
Chris Lattnerdd730472004-04-17 22:58:41 +00007703 }
7704}
7705
Sanjoy Das413dbbb2015-10-08 18:46:59 +00007706const SCEV *ScalarEvolution::computeExitCountExhaustively(const Loop *L,
Nick Lewyckya6674c72011-10-22 19:58:20 +00007707 Value *Cond,
7708 bool ExitWhen) {
Chris Lattner4021d1a2004-04-17 18:36:24 +00007709 PHINode *PN = getConstantEvolvingPHI(Cond, L);
Craig Topper9f008862014-04-15 04:59:12 +00007710 if (!PN) return getCouldNotCompute();
Chris Lattner4021d1a2004-04-17 18:36:24 +00007711
Dan Gohman866971e2010-06-19 14:17:24 +00007712 // If the loop is canonicalized, the PHI will have exactly two entries.
7713 // That's the only form we support here.
7714 if (PN->getNumIncomingValues() != 2) return getCouldNotCompute();
7715
Duncan Sandsa370f3e2011-10-25 12:28:52 +00007716 DenseMap<Instruction *, Constant *> CurrentIterVals;
7717 BasicBlock *Header = L->getHeader();
7718 assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
7719
Sanjoy Dasdd709962015-10-08 18:28:36 +00007720 BasicBlock *Latch = L->getLoopLatch();
7721 assert(Latch && "Should follow from NumIncomingValues == 2!");
7722
Sanjoy Das4493b402015-10-07 17:38:25 +00007723 for (auto &I : *Header) {
7724 PHINode *PHI = dyn_cast<PHINode>(&I);
7725 if (!PHI)
7726 break;
Sanjoy Das52bfa0f2015-11-02 02:06:01 +00007727 auto *StartCST = getOtherIncomingValue(PHI, Latch);
Craig Topper9f008862014-04-15 04:59:12 +00007728 if (!StartCST) continue;
Duncan Sandsa370f3e2011-10-25 12:28:52 +00007729 CurrentIterVals[PHI] = StartCST;
7730 }
7731 if (!CurrentIterVals.count(PN))
7732 return getCouldNotCompute();
Chris Lattner4021d1a2004-04-17 18:36:24 +00007733
7734 // Okay, we find a PHI node that defines the trip count of this loop. Execute
7735 // the loop symbolically to determine when the condition gets a value of
7736 // "ExitWhen".
Andrew Trick90c7a102011-11-16 00:52:40 +00007737 unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis.
Sanjoy Das49edd3b2015-10-27 00:52:09 +00007738 const DataLayout &DL = getDataLayout();
Duncan Sandsa370f3e2011-10-25 12:28:52 +00007739 for (unsigned IterationNum = 0; IterationNum != MaxIterations;++IterationNum){
Sanjoy Das4493b402015-10-07 17:38:25 +00007740 auto *CondVal = dyn_cast_or_null<ConstantInt>(
Chandler Carruth2f1fd162015-08-17 02:08:17 +00007741 EvaluateExpression(Cond, L, CurrentIterVals, DL, &TLI));
Chris Lattnerdd730472004-04-17 22:58:41 +00007742
Zhou Sheng75b871f2007-01-11 12:24:14 +00007743 // Couldn't symbolically evaluate.
Dan Gohmanc5c85c02009-06-27 21:21:31 +00007744 if (!CondVal) return getCouldNotCompute();
Zhou Sheng75b871f2007-01-11 12:24:14 +00007745
Reid Spencer983e3b32007-03-01 07:25:48 +00007746 if (CondVal->getValue() == uint64_t(ExitWhen)) {
Chris Lattner4021d1a2004-04-17 18:36:24 +00007747 ++NumBruteForceTripCountsComputed;
Owen Anderson55f1c092009-08-13 21:58:54 +00007748 return getConstant(Type::getInt32Ty(getContext()), IterationNum);
Chris Lattner4021d1a2004-04-17 18:36:24 +00007749 }
Misha Brukman01808ca2005-04-21 21:13:18 +00007750
Duncan Sandsa370f3e2011-10-25 12:28:52 +00007751 // Update all the PHI nodes for the next iteration.
7752 DenseMap<Instruction *, Constant *> NextIterVals;
Nick Lewyckyd48ab842011-11-12 03:09:12 +00007753
7754 // Create a list of which PHIs we need to compute. We want to do this before
7755 // calling EvaluateExpression on them because that may invalidate iterators
7756 // into CurrentIterVals.
7757 SmallVector<PHINode *, 8> PHIsToCompute;
Sanjoy Das4493b402015-10-07 17:38:25 +00007758 for (const auto &I : CurrentIterVals) {
7759 PHINode *PHI = dyn_cast<PHINode>(I.first);
Duncan Sandsa370f3e2011-10-25 12:28:52 +00007760 if (!PHI || PHI->getParent() != Header) continue;
Nick Lewyckyd48ab842011-11-12 03:09:12 +00007761 PHIsToCompute.push_back(PHI);
7762 }
Sanjoy Das4493b402015-10-07 17:38:25 +00007763 for (PHINode *PHI : PHIsToCompute) {
Duncan Sandsa370f3e2011-10-25 12:28:52 +00007764 Constant *&NextPHI = NextIterVals[PHI];
7765 if (NextPHI) continue; // Already computed!
7766
Sanjoy Dasdd709962015-10-08 18:28:36 +00007767 Value *BEValue = PHI->getIncomingValueForBlock(Latch);
Chandler Carruth2f1fd162015-08-17 02:08:17 +00007768 NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
Duncan Sandsa370f3e2011-10-25 12:28:52 +00007769 }
7770 CurrentIterVals.swap(NextIterVals);
Chris Lattner4021d1a2004-04-17 18:36:24 +00007771 }
7772
7773 // Too many iterations were needed to evaluate.
Dan Gohmanc5c85c02009-06-27 21:21:31 +00007774 return getCouldNotCompute();
Chris Lattnerd934c702004-04-02 20:23:17 +00007775}
7776
Dan Gohmanaf752342009-07-07 17:06:11 +00007777const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) {
Sanjoy Das01947432015-11-22 21:20:13 +00007778 SmallVector<std::pair<const Loop *, const SCEV *>, 2> &Values =
7779 ValuesAtScopes[V];
Dan Gohmancc2f1eb2009-08-31 21:15:23 +00007780 // Check to see if we've folded this expression at this loop before.
Sanjoy Das01947432015-11-22 21:20:13 +00007781 for (auto &LS : Values)
7782 if (LS.first == L)
7783 return LS.second ? LS.second : V;
7784
7785 Values.emplace_back(L, nullptr);
7786
Dan Gohmancc2f1eb2009-08-31 21:15:23 +00007787 // Otherwise compute it.
7788 const SCEV *C = computeSCEVAtScope(V, L);
Sanjoy Das01947432015-11-22 21:20:13 +00007789 for (auto &LS : reverse(ValuesAtScopes[V]))
7790 if (LS.first == L) {
7791 LS.second = C;
Wan Xiaofeib2c8cdc2013-11-12 09:40:41 +00007792 break;
7793 }
Dan Gohmancc2f1eb2009-08-31 21:15:23 +00007794 return C;
7795}
7796
Nick Lewyckya6674c72011-10-22 19:58:20 +00007797/// This builds up a Constant using the ConstantExpr interface. That way, we
7798/// will return Constants for objects which aren't represented by a
7799/// SCEVConstant, because SCEVConstant is restricted to ConstantInt.
7800/// Returns NULL if the SCEV isn't representable as a Constant.
7801static Constant *BuildConstantFromSCEV(const SCEV *V) {
Benjamin Kramer987b8502014-02-11 19:02:55 +00007802 switch (static_cast<SCEVTypes>(V->getSCEVType())) {
Nick Lewyckya6674c72011-10-22 19:58:20 +00007803 case scCouldNotCompute:
7804 case scAddRecExpr:
7805 break;
7806 case scConstant:
7807 return cast<SCEVConstant>(V)->getValue();
7808 case scUnknown:
7809 return dyn_cast<Constant>(cast<SCEVUnknown>(V)->getValue());
7810 case scSignExtend: {
7811 const SCEVSignExtendExpr *SS = cast<SCEVSignExtendExpr>(V);
7812 if (Constant *CastOp = BuildConstantFromSCEV(SS->getOperand()))
7813 return ConstantExpr::getSExt(CastOp, SS->getType());
7814 break;
7815 }
7816 case scZeroExtend: {
7817 const SCEVZeroExtendExpr *SZ = cast<SCEVZeroExtendExpr>(V);
7818 if (Constant *CastOp = BuildConstantFromSCEV(SZ->getOperand()))
7819 return ConstantExpr::getZExt(CastOp, SZ->getType());
7820 break;
7821 }
7822 case scTruncate: {
7823 const SCEVTruncateExpr *ST = cast<SCEVTruncateExpr>(V);
7824 if (Constant *CastOp = BuildConstantFromSCEV(ST->getOperand()))
7825 return ConstantExpr::getTrunc(CastOp, ST->getType());
7826 break;
7827 }
7828 case scAddExpr: {
7829 const SCEVAddExpr *SA = cast<SCEVAddExpr>(V);
7830 if (Constant *C = BuildConstantFromSCEV(SA->getOperand(0))) {
Matt Arsenaultbe18b8a2013-10-21 18:41:10 +00007831 if (PointerType *PTy = dyn_cast<PointerType>(C->getType())) {
7832 unsigned AS = PTy->getAddressSpace();
7833 Type *DestPtrTy = Type::getInt8PtrTy(C->getContext(), AS);
7834 C = ConstantExpr::getBitCast(C, DestPtrTy);
7835 }
Nick Lewyckya6674c72011-10-22 19:58:20 +00007836 for (unsigned i = 1, e = SA->getNumOperands(); i != e; ++i) {
7837 Constant *C2 = BuildConstantFromSCEV(SA->getOperand(i));
Craig Topper9f008862014-04-15 04:59:12 +00007838 if (!C2) return nullptr;
Nick Lewyckya6674c72011-10-22 19:58:20 +00007839
7840 // First pointer!
7841 if (!C->getType()->isPointerTy() && C2->getType()->isPointerTy()) {
Matt Arsenaultbe18b8a2013-10-21 18:41:10 +00007842 unsigned AS = C2->getType()->getPointerAddressSpace();
Nick Lewyckya6674c72011-10-22 19:58:20 +00007843 std::swap(C, C2);
Matt Arsenaultbe18b8a2013-10-21 18:41:10 +00007844 Type *DestPtrTy = Type::getInt8PtrTy(C->getContext(), AS);
Nick Lewyckya6674c72011-10-22 19:58:20 +00007845 // The offsets have been converted to bytes. We can add bytes to an
7846 // i8* by GEP with the byte count in the first index.
Matt Arsenaultbe18b8a2013-10-21 18:41:10 +00007847 C = ConstantExpr::getBitCast(C, DestPtrTy);
Nick Lewyckya6674c72011-10-22 19:58:20 +00007848 }
7849
7850 // Don't bother trying to sum two pointers. We probably can't
7851 // statically compute a load that results from it anyway.
7852 if (C2->getType()->isPointerTy())
Craig Topper9f008862014-04-15 04:59:12 +00007853 return nullptr;
Nick Lewyckya6674c72011-10-22 19:58:20 +00007854
Matt Arsenaultbe18b8a2013-10-21 18:41:10 +00007855 if (PointerType *PTy = dyn_cast<PointerType>(C->getType())) {
7856 if (PTy->getElementType()->isStructTy())
Nick Lewyckya6674c72011-10-22 19:58:20 +00007857 C2 = ConstantExpr::getIntegerCast(
7858 C2, Type::getInt32Ty(C->getContext()), true);
David Blaikie4a2e73b2015-04-02 18:55:32 +00007859 C = ConstantExpr::getGetElementPtr(PTy->getElementType(), C, C2);
Nick Lewyckya6674c72011-10-22 19:58:20 +00007860 } else
7861 C = ConstantExpr::getAdd(C, C2);
7862 }
7863 return C;
7864 }
7865 break;
7866 }
7867 case scMulExpr: {
7868 const SCEVMulExpr *SM = cast<SCEVMulExpr>(V);
7869 if (Constant *C = BuildConstantFromSCEV(SM->getOperand(0))) {
7870 // Don't bother with pointers at all.
Craig Topper9f008862014-04-15 04:59:12 +00007871 if (C->getType()->isPointerTy()) return nullptr;
Nick Lewyckya6674c72011-10-22 19:58:20 +00007872 for (unsigned i = 1, e = SM->getNumOperands(); i != e; ++i) {
7873 Constant *C2 = BuildConstantFromSCEV(SM->getOperand(i));
Craig Topper9f008862014-04-15 04:59:12 +00007874 if (!C2 || C2->getType()->isPointerTy()) return nullptr;
Nick Lewyckya6674c72011-10-22 19:58:20 +00007875 C = ConstantExpr::getMul(C, C2);
7876 }
7877 return C;
7878 }
7879 break;
7880 }
7881 case scUDivExpr: {
7882 const SCEVUDivExpr *SU = cast<SCEVUDivExpr>(V);
7883 if (Constant *LHS = BuildConstantFromSCEV(SU->getLHS()))
7884 if (Constant *RHS = BuildConstantFromSCEV(SU->getRHS()))
7885 if (LHS->getType() == RHS->getType())
7886 return ConstantExpr::getUDiv(LHS, RHS);
7887 break;
7888 }
Benjamin Kramer987b8502014-02-11 19:02:55 +00007889 case scSMaxExpr:
7890 case scUMaxExpr:
7891 break; // TODO: smax, umax.
Nick Lewyckya6674c72011-10-22 19:58:20 +00007892 }
Craig Topper9f008862014-04-15 04:59:12 +00007893 return nullptr;
Nick Lewyckya6674c72011-10-22 19:58:20 +00007894}
7895
Dan Gohmancc2f1eb2009-08-31 21:15:23 +00007896const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
Chris Lattnerdd730472004-04-17 22:58:41 +00007897 if (isa<SCEVConstant>(V)) return V;
Misha Brukman01808ca2005-04-21 21:13:18 +00007898
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00007899 // If this instruction is evolved from a constant-evolving PHI, compute the
Chris Lattnerdd730472004-04-17 22:58:41 +00007900 // exit value from the loop without using SCEVs.
Dan Gohmana30370b2009-05-04 22:02:23 +00007901 if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) {
Chris Lattnerdd730472004-04-17 22:58:41 +00007902 if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) {
Chandler Carruth2f1fd162015-08-17 02:08:17 +00007903 const Loop *LI = this->LI[I->getParent()];
Chris Lattnerdd730472004-04-17 22:58:41 +00007904 if (LI && LI->getParentLoop() == L) // Looking for loop exit value.
7905 if (PHINode *PN = dyn_cast<PHINode>(I))
7906 if (PN->getParent() == LI->getHeader()) {
7907 // Okay, there is no closed form solution for the PHI node. Check
Dan Gohman0bddac12009-02-24 18:55:53 +00007908 // to see if the loop that contains it has a known backedge-taken
7909 // count. If so, we may be able to force computation of the exit
7910 // value.
Dan Gohmanaf752342009-07-07 17:06:11 +00007911 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI);
Dan Gohmana30370b2009-05-04 22:02:23 +00007912 if (const SCEVConstant *BTCC =
Dan Gohman0bddac12009-02-24 18:55:53 +00007913 dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
Sanjoy Das4cad61a2017-08-01 22:37:58 +00007914
7915 // This trivial case can show up in some degenerate cases where
7916 // the incoming IR has not yet been fully simplified.
7917 if (BTCC->getValue()->isZero()) {
7918 Value *InitValue = nullptr;
7919 bool MultipleInitValues = false;
7920 for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
7921 if (!LI->contains(PN->getIncomingBlock(i))) {
7922 if (!InitValue)
7923 InitValue = PN->getIncomingValue(i);
7924 else if (InitValue != PN->getIncomingValue(i)) {
7925 MultipleInitValues = true;
7926 break;
7927 }
7928 }
7929 if (!MultipleInitValues && InitValue)
7930 return getSCEV(InitValue);
7931 }
7932 }
Chris Lattnerdd730472004-04-17 22:58:41 +00007933 // Okay, we know how many times the containing loop executes. If
7934 // this is a constant evolving PHI node, get the final value at
7935 // the specified iteration number.
Sanjoy Das0de2fec2015-12-17 20:28:46 +00007936 Constant *RV =
7937 getConstantEvolutionLoopExitValue(PN, BTCC->getAPInt(), LI);
Dan Gohman9d203c62009-06-29 21:31:18 +00007938 if (RV) return getSCEV(RV);
Chris Lattnerdd730472004-04-17 22:58:41 +00007939 }
7940 }
7941
Reid Spencere6328ca2006-12-04 21:33:23 +00007942 // Okay, this is an expression that we cannot symbolically evaluate
Chris Lattnerdd730472004-04-17 22:58:41 +00007943 // into a SCEV. Check to see if it's possible to symbolically evaluate
Reid Spencere6328ca2006-12-04 21:33:23 +00007944 // the arguments into constants, and if so, try to constant propagate the
Chris Lattnerdd730472004-04-17 22:58:41 +00007945 // result. This is particularly useful for computing loop exit values.
7946 if (CanConstantFold(I)) {
Dan Gohmanae36b1e2010-06-29 23:43:06 +00007947 SmallVector<Constant *, 4> Operands;
7948 bool MadeImprovement = false;
Sanjoy Dasd9f6d332015-10-18 00:29:16 +00007949 for (Value *Op : I->operands()) {
Chris Lattnerdd730472004-04-17 22:58:41 +00007950 if (Constant *C = dyn_cast<Constant>(Op)) {
7951 Operands.push_back(C);
Dan Gohmanae36b1e2010-06-29 23:43:06 +00007952 continue;
Chris Lattnerdd730472004-04-17 22:58:41 +00007953 }
Dan Gohmanae36b1e2010-06-29 23:43:06 +00007954
7955 // If any of the operands is non-constant and if they are
7956 // non-integer and non-pointer, don't even try to analyze them
7957 // with scev techniques.
7958 if (!isSCEVable(Op->getType()))
7959 return V;
7960
7961 const SCEV *OrigV = getSCEV(Op);
7962 const SCEV *OpV = getSCEVAtScope(OrigV, L);
7963 MadeImprovement |= OrigV != OpV;
7964
Nick Lewyckya6674c72011-10-22 19:58:20 +00007965 Constant *C = BuildConstantFromSCEV(OpV);
Dan Gohmanae36b1e2010-06-29 23:43:06 +00007966 if (!C) return V;
7967 if (C->getType() != Op->getType())
7968 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
7969 Op->getType(),
7970 false),
7971 C, Op->getType());
7972 Operands.push_back(C);
Chris Lattnerdd730472004-04-17 22:58:41 +00007973 }
Dan Gohmance973df2009-06-24 04:48:43 +00007974
Dan Gohmanae36b1e2010-06-29 23:43:06 +00007975 // Check to see if getSCEVAtScope actually made an improvement.
7976 if (MadeImprovement) {
Craig Topper9f008862014-04-15 04:59:12 +00007977 Constant *C = nullptr;
Sanjoy Das49edd3b2015-10-27 00:52:09 +00007978 const DataLayout &DL = getDataLayout();
Dan Gohmanae36b1e2010-06-29 23:43:06 +00007979 if (const CmpInst *CI = dyn_cast<CmpInst>(I))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00007980 C = ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0],
Chandler Carruth2f1fd162015-08-17 02:08:17 +00007981 Operands[1], DL, &TLI);
Nick Lewyckya6674c72011-10-22 19:58:20 +00007982 else if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
7983 if (!LI->isVolatile())
Eduard Burtescu14239212016-01-22 01:17:26 +00007984 C = ConstantFoldLoadFromConstPtr(Operands[0], LI->getType(), DL);
Nick Lewyckya6674c72011-10-22 19:58:20 +00007985 } else
Manuel Jacobe9024592016-01-21 06:33:22 +00007986 C = ConstantFoldInstOperands(I, Operands, DL, &TLI);
Dan Gohmanae36b1e2010-06-29 23:43:06 +00007987 if (!C) return V;
Dan Gohman4aad7502010-02-24 19:31:47 +00007988 return getSCEV(C);
Dan Gohmanae36b1e2010-06-29 23:43:06 +00007989 }
Chris Lattnerdd730472004-04-17 22:58:41 +00007990 }
7991 }
7992
7993 // This is some other type of SCEVUnknown, just return it.
7994 return V;
7995 }
7996
Dan Gohmana30370b2009-05-04 22:02:23 +00007997 if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) {
Chris Lattnerd934c702004-04-02 20:23:17 +00007998 // Avoid performing the look-up in the common case where the specified
7999 // expression has no loop-variant portions.
8000 for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) {
Dan Gohmanaf752342009-07-07 17:06:11 +00008001 const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
Chris Lattnerd934c702004-04-02 20:23:17 +00008002 if (OpAtScope != Comm->getOperand(i)) {
Chris Lattnerd934c702004-04-02 20:23:17 +00008003 // Okay, at least one of these operands is loop variant but might be
8004 // foldable. Build a new instance of the folded commutative expression.
Dan Gohmance973df2009-06-24 04:48:43 +00008005 SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(),
8006 Comm->op_begin()+i);
Chris Lattnerd934c702004-04-02 20:23:17 +00008007 NewOps.push_back(OpAtScope);
8008
8009 for (++i; i != e; ++i) {
8010 OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
Chris Lattnerd934c702004-04-02 20:23:17 +00008011 NewOps.push_back(OpAtScope);
8012 }
8013 if (isa<SCEVAddExpr>(Comm))
Dan Gohmanc8e23622009-04-21 23:15:49 +00008014 return getAddExpr(NewOps);
Nick Lewyckycdb7e542007-11-25 22:41:31 +00008015 if (isa<SCEVMulExpr>(Comm))
Dan Gohmanc8e23622009-04-21 23:15:49 +00008016 return getMulExpr(NewOps);
Nick Lewyckycdb7e542007-11-25 22:41:31 +00008017 if (isa<SCEVSMaxExpr>(Comm))
Dan Gohmanc8e23622009-04-21 23:15:49 +00008018 return getSMaxExpr(NewOps);
Nick Lewycky1c44ebc2008-02-20 06:48:22 +00008019 if (isa<SCEVUMaxExpr>(Comm))
Dan Gohmanc8e23622009-04-21 23:15:49 +00008020 return getUMaxExpr(NewOps);
Torok Edwinfbcc6632009-07-14 16:55:14 +00008021 llvm_unreachable("Unknown commutative SCEV type!");
Chris Lattnerd934c702004-04-02 20:23:17 +00008022 }
8023 }
8024 // If we got here, all operands are loop invariant.
8025 return Comm;
8026 }
8027
Dan Gohmana30370b2009-05-04 22:02:23 +00008028 if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) {
Dan Gohmanaf752342009-07-07 17:06:11 +00008029 const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L);
8030 const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L);
Nick Lewycky52348302009-01-13 09:18:58 +00008031 if (LHS == Div->getLHS() && RHS == Div->getRHS())
8032 return Div; // must be loop invariant
Dan Gohmanc8e23622009-04-21 23:15:49 +00008033 return getUDivExpr(LHS, RHS);
Chris Lattnerd934c702004-04-02 20:23:17 +00008034 }
8035
8036 // If this is a loop recurrence for a loop that does not contain L, then we
8037 // are dealing with the final value computed by the loop.
Dan Gohmana30370b2009-05-04 22:02:23 +00008038 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
Dan Gohmanae36b1e2010-06-29 23:43:06 +00008039 // First, attempt to evaluate each operand.
8040 // Avoid performing the look-up in the common case where the specified
8041 // expression has no loop-variant portions.
8042 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
8043 const SCEV *OpAtScope = getSCEVAtScope(AddRec->getOperand(i), L);
8044 if (OpAtScope == AddRec->getOperand(i))
8045 continue;
8046
8047 // Okay, at least one of these operands is loop variant but might be
8048 // foldable. Build a new instance of the folded commutative expression.
8049 SmallVector<const SCEV *, 8> NewOps(AddRec->op_begin(),
8050 AddRec->op_begin()+i);
8051 NewOps.push_back(OpAtScope);
8052 for (++i; i != e; ++i)
8053 NewOps.push_back(getSCEVAtScope(AddRec->getOperand(i), L));
8054
Andrew Trick759ba082011-04-27 01:21:25 +00008055 const SCEV *FoldedRec =
Andrew Trick8b55b732011-03-14 16:50:06 +00008056 getAddRecExpr(NewOps, AddRec->getLoop(),
Andrew Trick759ba082011-04-27 01:21:25 +00008057 AddRec->getNoWrapFlags(SCEV::FlagNW));
8058 AddRec = dyn_cast<SCEVAddRecExpr>(FoldedRec);
Andrew Trick01eff822011-04-27 05:42:17 +00008059 // The addrec may be folded to a nonrecurrence, for example, if the
8060 // induction variable is multiplied by zero after constant folding. Go
8061 // ahead and return the folded value.
Andrew Trick759ba082011-04-27 01:21:25 +00008062 if (!AddRec)
8063 return FoldedRec;
Dan Gohmanae36b1e2010-06-29 23:43:06 +00008064 break;
8065 }
8066
8067 // If the scope is outside the addrec's loop, evaluate it by using the
8068 // loop exit value of the addrec.
8069 if (!AddRec->getLoop()->contains(L)) {
Chris Lattnerd934c702004-04-02 20:23:17 +00008070 // To evaluate this recurrence, we need to know how many times the AddRec
8071 // loop iterates. Compute this now.
Dan Gohmanaf752342009-07-07 17:06:11 +00008072 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop());
Dan Gohmanc5c85c02009-06-27 21:21:31 +00008073 if (BackedgeTakenCount == getCouldNotCompute()) return AddRec;
Misha Brukman01808ca2005-04-21 21:13:18 +00008074
Eli Friedman61f67622008-08-04 23:49:06 +00008075 // Then, evaluate the AddRec.
Dan Gohmanc8e23622009-04-21 23:15:49 +00008076 return AddRec->evaluateAtIteration(BackedgeTakenCount, *this);
Chris Lattnerd934c702004-04-02 20:23:17 +00008077 }
Dan Gohmanae36b1e2010-06-29 23:43:06 +00008078
Dan Gohman8ca08852009-05-24 23:25:42 +00008079 return AddRec;
Chris Lattnerd934c702004-04-02 20:23:17 +00008080 }
8081
Dan Gohmana30370b2009-05-04 22:02:23 +00008082 if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) {
Dan Gohmanaf752342009-07-07 17:06:11 +00008083 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohman0098d012009-04-29 22:29:01 +00008084 if (Op == Cast->getOperand())
8085 return Cast; // must be loop invariant
8086 return getZeroExtendExpr(Op, Cast->getType());
8087 }
8088
Dan Gohmana30370b2009-05-04 22:02:23 +00008089 if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) {
Dan Gohmanaf752342009-07-07 17:06:11 +00008090 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohman0098d012009-04-29 22:29:01 +00008091 if (Op == Cast->getOperand())
8092 return Cast; // must be loop invariant
8093 return getSignExtendExpr(Op, Cast->getType());
8094 }
8095
Dan Gohmana30370b2009-05-04 22:02:23 +00008096 if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) {
Dan Gohmanaf752342009-07-07 17:06:11 +00008097 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
Dan Gohman0098d012009-04-29 22:29:01 +00008098 if (Op == Cast->getOperand())
8099 return Cast; // must be loop invariant
8100 return getTruncateExpr(Op, Cast->getType());
8101 }
8102
Torok Edwinfbcc6632009-07-14 16:55:14 +00008103 llvm_unreachable("Unknown SCEV type!");
Chris Lattnerd934c702004-04-02 20:23:17 +00008104}
8105
Dan Gohmanaf752342009-07-07 17:06:11 +00008106const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) {
Dan Gohmanc8e23622009-04-21 23:15:49 +00008107 return getSCEVAtScope(getSCEV(V), L);
8108}
8109
Sanjoy Dasf8570812016-05-29 00:38:22 +00008110/// Finds the minimum unsigned root of the following equation:
Wojciech Matyjewiczf0d21cd2008-07-20 15:55:14 +00008111///
8112/// A * X = B (mod N)
8113///
8114/// where N = 2^BW and BW is the common bit width of A and B. The signedness of
8115/// A and B isn't important.
8116///
8117/// If the equation does not have a solution, SCEVCouldNotCompute is returned.
Eli Friedman10d1ff62017-01-31 00:42:42 +00008118static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const SCEV *B,
Wojciech Matyjewiczf0d21cd2008-07-20 15:55:14 +00008119 ScalarEvolution &SE) {
8120 uint32_t BW = A.getBitWidth();
Eli Friedman10d1ff62017-01-31 00:42:42 +00008121 assert(BW == SE.getTypeSizeInBits(B->getType()));
Wojciech Matyjewiczf0d21cd2008-07-20 15:55:14 +00008122 assert(A != 0 && "A must be non-zero.");
8123
8124 // 1. D = gcd(A, N)
8125 //
8126 // The gcd of A and N may have only one prime factor: 2. The number of
8127 // trailing zeros in A is its multiplicity
8128 uint32_t Mult2 = A.countTrailingZeros();
8129 // D = 2^Mult2
8130
8131 // 2. Check if B is divisible by D.
8132 //
8133 // B is divisible by D if and only if the multiplicity of prime factor 2 for B
8134 // is not less than multiplicity of this prime factor for D.
Eli Friedman10d1ff62017-01-31 00:42:42 +00008135 if (SE.GetMinTrailingZeros(B) < Mult2)
Dan Gohman31efa302009-04-18 17:58:19 +00008136 return SE.getCouldNotCompute();
Wojciech Matyjewiczf0d21cd2008-07-20 15:55:14 +00008137
8138 // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic
8139 // modulo (N / D).
8140 //
Eli Friedmanb5c3a0d2017-01-12 20:21:00 +00008141 // If D == 1, (N / D) == N == 2^BW, so we need one extra bit to represent
8142 // (N / D) in general. The inverse itself always fits into BW bits, though,
8143 // so we immediately truncate it.
Wojciech Matyjewiczf0d21cd2008-07-20 15:55:14 +00008144 APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D
8145 APInt Mod(BW + 1, 0);
Jay Foad25a5e4c2010-12-01 08:53:58 +00008146 Mod.setBit(BW - Mult2); // Mod = N / D
Eli Friedmanb5c3a0d2017-01-12 20:21:00 +00008147 APInt I = AD.multiplicativeInverse(Mod).trunc(BW);
Wojciech Matyjewiczf0d21cd2008-07-20 15:55:14 +00008148
8149 // 4. Compute the minimum unsigned root of the equation:
8150 // I * (B / D) mod (N / D)
Eli Friedmanb5c3a0d2017-01-12 20:21:00 +00008151 // To simplify the computation, we factor out the divide by D:
8152 // (I * B mod N) / D
Eli Friedman10d1ff62017-01-31 00:42:42 +00008153 const SCEV *D = SE.getConstant(APInt::getOneBitSet(BW, Mult2));
8154 return SE.getUDivExactExpr(SE.getMulExpr(B, SE.getConstant(I)), D);
Wojciech Matyjewiczf0d21cd2008-07-20 15:55:14 +00008155}
Chris Lattnerd934c702004-04-02 20:23:17 +00008156
Sanjoy Dasf8570812016-05-29 00:38:22 +00008157/// Find the roots of the quadratic equation for the given quadratic chrec
8158/// {L,+,M,+,N}. This returns either the two roots (which might be the same) or
8159/// two SCEVCouldNotCompute objects.
Sanjoy Das5a3d8932016-06-15 04:37:47 +00008160static Optional<std::pair<const SCEVConstant *,const SCEVConstant *>>
Dan Gohmana37eaf22007-10-22 18:31:58 +00008161SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) {
Chris Lattnerd934c702004-04-02 20:23:17 +00008162 assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
Dan Gohman48f82222009-05-04 22:30:44 +00008163 const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
8164 const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
8165 const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
Misha Brukman01808ca2005-04-21 21:13:18 +00008166
Chris Lattnerd934c702004-04-02 20:23:17 +00008167 // We currently can only solve this if the coefficients are constants.
Sanjoy Das5a3d8932016-06-15 04:37:47 +00008168 if (!LC || !MC || !NC)
8169 return None;
Chris Lattnerd934c702004-04-02 20:23:17 +00008170
Sanjoy Das0de2fec2015-12-17 20:28:46 +00008171 uint32_t BitWidth = LC->getAPInt().getBitWidth();
8172 const APInt &L = LC->getAPInt();
8173 const APInt &M = MC->getAPInt();
8174 const APInt &N = NC->getAPInt();
Reid Spencer983e3b32007-03-01 07:25:48 +00008175 APInt Two(BitWidth, 2);
Misha Brukman01808ca2005-04-21 21:13:18 +00008176
Craig Topper6694a4e2017-05-11 06:48:51 +00008177 // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C
Misha Brukman01808ca2005-04-21 21:13:18 +00008178
Craig Topper6694a4e2017-05-11 06:48:51 +00008179 // The A coefficient is N/2
Craig Topper716cad82017-05-15 18:14:16 +00008180 APInt A = N.sdiv(Two);
Chris Lattnerd934c702004-04-02 20:23:17 +00008181
Craig Toppere3e1a352017-05-11 06:48:54 +00008182 // The B coefficient is M-N/2
Craig Topper716cad82017-05-15 18:14:16 +00008183 APInt B = M;
Craig Toppere3e1a352017-05-11 06:48:54 +00008184 B -= A; // A is the same as N/2.
8185
8186 // The C coefficient is L.
8187 const APInt& C = L;
8188
Craig Topper6694a4e2017-05-11 06:48:51 +00008189 // Compute the B^2-4ac term.
Craig Topper716cad82017-05-15 18:14:16 +00008190 APInt SqrtTerm = B;
Craig Topper6694a4e2017-05-11 06:48:51 +00008191 SqrtTerm *= B;
8192 SqrtTerm -= 4 * (A * C);
Chris Lattnerd934c702004-04-02 20:23:17 +00008193
Craig Topper6694a4e2017-05-11 06:48:51 +00008194 if (SqrtTerm.isNegative()) {
8195 // The loop is provably infinite.
8196 return None;
8197 }
Nick Lewyckyfb780832012-08-01 09:14:36 +00008198
Craig Topper6694a4e2017-05-11 06:48:51 +00008199 // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest
8200 // integer value or else APInt::sqrt() will assert.
Craig Topper716cad82017-05-15 18:14:16 +00008201 APInt SqrtVal = SqrtTerm.sqrt();
Misha Brukman01808ca2005-04-21 21:13:18 +00008202
Craig Topper6694a4e2017-05-11 06:48:51 +00008203 // Compute the two solutions for the quadratic formula.
8204 // The divisions must be performed as signed divisions.
Craig Topper716cad82017-05-15 18:14:16 +00008205 APInt NegB = -std::move(B);
8206 APInt TwoA = std::move(A);
Craig Toppere3e1a352017-05-11 06:48:54 +00008207 TwoA <<= 1;
8208 if (TwoA.isNullValue())
Craig Topper6694a4e2017-05-11 06:48:51 +00008209 return None;
Nick Lewycky7b14e202008-11-03 02:43:49 +00008210
Craig Topper6694a4e2017-05-11 06:48:51 +00008211 LLVMContext &Context = SE.getContext();
Owen Andersonf1f17432009-07-06 22:37:39 +00008212
Craig Topper6694a4e2017-05-11 06:48:51 +00008213 ConstantInt *Solution1 =
8214 ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA));
8215 ConstantInt *Solution2 =
8216 ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA));
Misha Brukman01808ca2005-04-21 21:13:18 +00008217
Craig Topper6694a4e2017-05-11 06:48:51 +00008218 return std::make_pair(cast<SCEVConstant>(SE.getConstant(Solution1)),
8219 cast<SCEVConstant>(SE.getConstant(Solution2)));
Chris Lattnerd934c702004-04-02 20:23:17 +00008220}
8221
Andrew Trick3ca3f982011-07-26 17:19:55 +00008222ScalarEvolution::ExitLimit
Sanjoy Das108fcf22016-05-29 00:38:00 +00008223ScalarEvolution::howFarToZero(const SCEV *V, const Loop *L, bool ControlsExit,
Silviu Baranga6f444df2016-04-08 14:29:09 +00008224 bool AllowPredicates) {
Sanjoy Dasf8570812016-05-29 00:38:22 +00008225
8226 // This is only used for loops with a "x != y" exit test. The exit condition
8227 // is now expressed as a single expression, V = x-y. So the exit test is
8228 // effectively V != 0. We know and take advantage of the fact that this
8229 // expression only being used in a comparison by zero context.
8230
Sanjoy Dasf0022122016-09-28 17:14:58 +00008231 SmallPtrSet<const SCEVPredicate *, 4> Predicates;
Chris Lattnerd934c702004-04-02 20:23:17 +00008232 // If the value is a constant
Dan Gohmana30370b2009-05-04 22:02:23 +00008233 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
Chris Lattnerd934c702004-04-02 20:23:17 +00008234 // If the value is already zero, the branch will execute zero times.
Reid Spencer2e54a152007-03-02 00:28:52 +00008235 if (C->getValue()->isZero()) return C;
Dan Gohmanc5c85c02009-06-27 21:21:31 +00008236 return getCouldNotCompute(); // Otherwise it will loop infinitely.
Chris Lattnerd934c702004-04-02 20:23:17 +00008237 }
8238
Dan Gohman48f82222009-05-04 22:30:44 +00008239 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V);
Silviu Baranga6f444df2016-04-08 14:29:09 +00008240 if (!AddRec && AllowPredicates)
8241 // Try to make this an AddRec using runtime tests, in the first X
8242 // iterations of this loop, where X is the SCEV expression found by the
8243 // algorithm below.
Sanjoy Dasf0022122016-09-28 17:14:58 +00008244 AddRec = convertSCEVToAddRecWithPredicates(V, L, Predicates);
Silviu Baranga6f444df2016-04-08 14:29:09 +00008245
Chris Lattnerd934c702004-04-02 20:23:17 +00008246 if (!AddRec || AddRec->getLoop() != L)
Dan Gohmanc5c85c02009-06-27 21:21:31 +00008247 return getCouldNotCompute();
Chris Lattnerd934c702004-04-02 20:23:17 +00008248
Chris Lattnerdff679f2011-01-09 22:39:48 +00008249 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
8250 // the quadratic equation to solve it.
8251 if (AddRec->isQuadratic() && AddRec->getType()->isIntegerTy()) {
Sanjoy Das5a3d8932016-06-15 04:37:47 +00008252 if (auto Roots = SolveQuadraticEquation(AddRec, *this)) {
8253 const SCEVConstant *R1 = Roots->first;
8254 const SCEVConstant *R2 = Roots->second;
Chris Lattnerd934c702004-04-02 20:23:17 +00008255 // Pick the smallest positive root value.
Sanjoy Das0e392d52016-06-15 04:37:50 +00008256 if (ConstantInt *CB = dyn_cast<ConstantInt>(ConstantExpr::getICmp(
8257 CmpInst::ICMP_ULT, R1->getValue(), R2->getValue()))) {
David Blaikiedc3f01e2015-03-09 01:57:13 +00008258 if (!CB->getZExtValue())
Sanjoy Das0e392d52016-06-15 04:37:50 +00008259 std::swap(R1, R2); // R1 is the minimum root now.
Andrew Trick2a3b7162011-03-09 17:23:39 +00008260
Chris Lattnerd934c702004-04-02 20:23:17 +00008261 // We can only use this value if the chrec ends up with an exact zero
8262 // value at this index. When solving for "X*X != 5", for example, we
8263 // should not accept a root of 2.
Dan Gohmanaf752342009-07-07 17:06:11 +00008264 const SCEV *Val = AddRec->evaluateAtIteration(R1, *this);
Dan Gohmanbe928e32008-06-18 16:23:07 +00008265 if (Val->isZero())
John Brawn84b21832016-10-21 11:08:48 +00008266 // We found a quadratic root!
8267 return ExitLimit(R1, R1, false, Predicates);
Chris Lattnerd934c702004-04-02 20:23:17 +00008268 }
8269 }
Chris Lattnerdff679f2011-01-09 22:39:48 +00008270 return getCouldNotCompute();
Chris Lattnerd934c702004-04-02 20:23:17 +00008271 }
Misha Brukman01808ca2005-04-21 21:13:18 +00008272
Chris Lattnerdff679f2011-01-09 22:39:48 +00008273 // Otherwise we can only handle this if it is affine.
8274 if (!AddRec->isAffine())
8275 return getCouldNotCompute();
8276
8277 // If this is an affine expression, the execution count of this branch is
8278 // the minimum unsigned root of the following equation:
8279 //
8280 // Start + Step*N = 0 (mod 2^BW)
8281 //
8282 // equivalent to:
8283 //
8284 // Step*N = -Start (mod 2^BW)
8285 //
8286 // where BW is the common bit width of Start and Step.
8287
8288 // Get the initial value for the loop.
8289 const SCEV *Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop());
8290 const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), L->getParentLoop());
8291
8292 // For now we handle only constant steps.
Andrew Trick8b55b732011-03-14 16:50:06 +00008293 //
8294 // TODO: Handle a nonconstant Step given AddRec<NUW>. If the
8295 // AddRec is NUW, then (in an unsigned sense) it cannot be counting up to wrap
8296 // to 0, it must be counting down to equal 0. Consequently, N = Start / -Step.
8297 // We have not yet seen any such cases.
Chris Lattnerdff679f2011-01-09 22:39:48 +00008298 const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step);
Craig Topperca2c8762017-07-06 18:39:49 +00008299 if (!StepC || StepC->getValue()->isZero())
Chris Lattnerdff679f2011-01-09 22:39:48 +00008300 return getCouldNotCompute();
8301
Andrew Trick8b55b732011-03-14 16:50:06 +00008302 // For positive steps (counting up until unsigned overflow):
8303 // N = -Start/Step (as unsigned)
8304 // For negative steps (counting down to zero):
8305 // N = Start/-Step
8306 // First compute the unsigned distance from zero in the direction of Step.
Sanjoy Das0de2fec2015-12-17 20:28:46 +00008307 bool CountDown = StepC->getAPInt().isNegative();
Andrew Trickf1781db2011-03-14 17:28:02 +00008308 const SCEV *Distance = CountDown ? Start : getNegativeSCEV(Start);
Andrew Trick8b55b732011-03-14 16:50:06 +00008309
8310 // Handle unitary steps, which cannot wraparound.
Andrew Trickf1781db2011-03-14 17:28:02 +00008311 // 1*N = -Start; -1*N = Start (mod 2^BW), so:
8312 // N = Distance (as unsigned)
Craig Topper79ab6432017-07-06 18:39:47 +00008313 if (StepC->getValue()->isOne() || StepC->getValue()->isMinusOne()) {
Craig Topper01020392017-06-24 23:34:50 +00008314 APInt MaxBECount = getUnsignedRangeMax(Distance);
Eli Friedmanbd6deda2017-01-11 21:07:15 +00008315
8316 // When a loop like "for (int i = 0; i != n; ++i) { /* body */ }" is rotated,
8317 // we end up with a loop whose backedge-taken count is n - 1. Detect this
8318 // case, and see if we can improve the bound.
8319 //
8320 // Explicitly handling this here is necessary because getUnsignedRange
8321 // isn't context-sensitive; it doesn't know that we only care about the
8322 // range inside the loop.
8323 const SCEV *Zero = getZero(Distance->getType());
8324 const SCEV *One = getOne(Distance->getType());
8325 const SCEV *DistancePlusOne = getAddExpr(Distance, One);
8326 if (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, DistancePlusOne, Zero)) {
8327 // If Distance + 1 doesn't overflow, we can compute the maximum distance
8328 // as "unsigned_max(Distance + 1) - 1".
8329 ConstantRange CR = getUnsignedRange(DistancePlusOne);
8330 MaxBECount = APIntOps::umin(MaxBECount, CR.getUnsignedMax() - 1);
8331 }
Eli Friedman83962652017-01-11 20:55:48 +00008332 return ExitLimit(Distance, getConstant(MaxBECount), false, Predicates);
Nick Lewycky31555522011-10-03 07:10:45 +00008333 }
Andrew Trick2a3b7162011-03-09 17:23:39 +00008334
Mark Heffernan2beab5f2014-10-10 17:39:11 +00008335 // If the condition controls loop exit (the loop exits only if the expression
8336 // is true) and the addition is no-wrap we can use unsigned divide to
8337 // compute the backedge count. In this case, the step may not divide the
8338 // distance, but we don't care because if the condition is "missed" the loop
8339 // will have undefined behavior due to wrapping.
Sanjoy Dasc7f69b92016-06-09 01:13:59 +00008340 if (ControlsExit && AddRec->hasNoSelfWrap() &&
8341 loopHasNoAbnormalExits(AddRec->getLoop())) {
Mark Heffernan2beab5f2014-10-10 17:39:11 +00008342 const SCEV *Exact =
8343 getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step);
Sanjoy Das036dda22017-05-22 06:46:04 +00008344 const SCEV *Max =
8345 Exact == getCouldNotCompute()
8346 ? Exact
Craig Topper01020392017-06-24 23:34:50 +00008347 : getConstant(getUnsignedRangeMax(Exact));
Sanjoy Das036dda22017-05-22 06:46:04 +00008348 return ExitLimit(Exact, Max, false, Predicates);
Mark Heffernan2beab5f2014-10-10 17:39:11 +00008349 }
Benjamin Kramere75eaca2014-03-25 16:25:12 +00008350
Eli Friedman10d1ff62017-01-31 00:42:42 +00008351 // Solve the general equation.
Sanjoy Das036dda22017-05-22 06:46:04 +00008352 const SCEV *E = SolveLinEquationWithOverflow(StepC->getAPInt(),
8353 getNegativeSCEV(Start), *this);
8354 const SCEV *M = E == getCouldNotCompute()
8355 ? E
Craig Topper01020392017-06-24 23:34:50 +00008356 : getConstant(getUnsignedRangeMax(E));
Sanjoy Das036dda22017-05-22 06:46:04 +00008357 return ExitLimit(E, M, false, Predicates);
Chris Lattnerd934c702004-04-02 20:23:17 +00008358}
8359
Andrew Trick3ca3f982011-07-26 17:19:55 +00008360ScalarEvolution::ExitLimit
Sanjoy Das108fcf22016-05-29 00:38:00 +00008361ScalarEvolution::howFarToNonZero(const SCEV *V, const Loop *L) {
Chris Lattnerd934c702004-04-02 20:23:17 +00008362 // Loops that look like: while (X == 0) are very strange indeed. We don't
8363 // handle them yet except for the trivial case. This could be expanded in the
8364 // future as needed.
Misha Brukman01808ca2005-04-21 21:13:18 +00008365
Chris Lattnerd934c702004-04-02 20:23:17 +00008366 // If the value is a constant, check to see if it is known to be non-zero
8367 // already. If so, the backedge will execute zero times.
Dan Gohmana30370b2009-05-04 22:02:23 +00008368 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
Craig Topper79ab6432017-07-06 18:39:47 +00008369 if (!C->getValue()->isZero())
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00008370 return getZero(C->getType());
Dan Gohmanc5c85c02009-06-27 21:21:31 +00008371 return getCouldNotCompute(); // Otherwise it will loop infinitely.
Chris Lattnerd934c702004-04-02 20:23:17 +00008372 }
Misha Brukman01808ca2005-04-21 21:13:18 +00008373
Chris Lattnerd934c702004-04-02 20:23:17 +00008374 // We could implement others, but I really doubt anyone writes loops like
8375 // this, and if they did, they would already be constant folded.
Dan Gohmanc5c85c02009-06-27 21:21:31 +00008376 return getCouldNotCompute();
Chris Lattnerd934c702004-04-02 20:23:17 +00008377}
8378
Dan Gohman4e3c1132010-04-15 16:19:08 +00008379std::pair<BasicBlock *, BasicBlock *>
Dan Gohmanc8e23622009-04-21 23:15:49 +00008380ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
Dan Gohmanfa066ef2009-04-30 20:48:53 +00008381 // If the block has a unique predecessor, then there is no path from the
8382 // predecessor to the block that does not go through the direct edge
8383 // from the predecessor to the block.
Dan Gohmanf9081a22008-09-15 22:18:04 +00008384 if (BasicBlock *Pred = BB->getSinglePredecessor())
Sanjoy Dasc42f7cc2016-02-20 01:35:56 +00008385 return {Pred, BB};
Dan Gohmanf9081a22008-09-15 22:18:04 +00008386
8387 // A loop's header is defined to be a block that dominates the loop.
Dan Gohman8c77f1a2009-05-18 15:36:09 +00008388 // If the header has a unique predecessor outside the loop, it must be
8389 // a block that has exactly one successor that can reach the loop.
Chandler Carruth2f1fd162015-08-17 02:08:17 +00008390 if (Loop *L = LI.getLoopFor(BB))
Sanjoy Dasc42f7cc2016-02-20 01:35:56 +00008391 return {L->getLoopPredecessor(), L->getHeader()};
Dan Gohmanf9081a22008-09-15 22:18:04 +00008392
Sanjoy Dasc42f7cc2016-02-20 01:35:56 +00008393 return {nullptr, nullptr};
Dan Gohmanf9081a22008-09-15 22:18:04 +00008394}
8395
Sanjoy Dasf8570812016-05-29 00:38:22 +00008396/// SCEV structural equivalence is usually sufficient for testing whether two
8397/// expressions are equal, however for the purposes of looking for a condition
8398/// guarding a loop, it can be useful to be a little more general, since a
8399/// front-end may have replicated the controlling expression.
Dan Gohmanaf752342009-07-07 17:06:11 +00008400static bool HasSameValue(const SCEV *A, const SCEV *B) {
Dan Gohman450f4e02009-06-20 00:35:32 +00008401 // Quick check to see if they are the same SCEV.
8402 if (A == B) return true;
8403
Sanjoy Dasf1090b62015-09-27 21:09:48 +00008404 auto ComputesEqualValues = [](const Instruction *A, const Instruction *B) {
8405 // Not all instructions that are "identical" compute the same value. For
8406 // instance, two distinct alloca instructions allocating the same type are
8407 // identical and do not read memory; but compute distinct values.
8408 return A->isIdenticalTo(B) && (isa<BinaryOperator>(A) || isa<GetElementPtrInst>(A));
8409 };
8410
Dan Gohman450f4e02009-06-20 00:35:32 +00008411 // Otherwise, if they're both SCEVUnknown, it's possible that they hold
8412 // two different instructions with the same value. Check for this case.
8413 if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A))
8414 if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B))
8415 if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue()))
8416 if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue()))
Sanjoy Dasf1090b62015-09-27 21:09:48 +00008417 if (ComputesEqualValues(AI, BI))
Dan Gohman450f4e02009-06-20 00:35:32 +00008418 return true;
8419
8420 // Otherwise assume they may have a different value.
8421 return false;
8422}
8423
Dan Gohman48ff3cf2010-04-24 01:28:42 +00008424bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred,
Benjamin Kramer50b26eb2012-05-30 18:32:23 +00008425 const SCEV *&LHS, const SCEV *&RHS,
8426 unsigned Depth) {
Dan Gohman48ff3cf2010-04-24 01:28:42 +00008427 bool Changed = false;
8428
Benjamin Kramer50b26eb2012-05-30 18:32:23 +00008429 // If we hit the max recursion limit bail out.
8430 if (Depth >= 3)
8431 return false;
8432
Dan Gohman48ff3cf2010-04-24 01:28:42 +00008433 // Canonicalize a constant to the right side.
8434 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
8435 // Check for both operands constant.
8436 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
8437 if (ConstantExpr::getICmp(Pred,
8438 LHSC->getValue(),
8439 RHSC->getValue())->isNullValue())
8440 goto trivially_false;
8441 else
8442 goto trivially_true;
8443 }
8444 // Otherwise swap the operands to put the constant on the right.
8445 std::swap(LHS, RHS);
8446 Pred = ICmpInst::getSwappedPredicate(Pred);
8447 Changed = true;
8448 }
8449
8450 // If we're comparing an addrec with a value which is loop-invariant in the
Dan Gohmandf564ca2010-05-03 17:00:11 +00008451 // addrec's loop, put the addrec on the left. Also make a dominance check,
8452 // as both operands could be addrecs loop-invariant in each other's loop.
8453 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(RHS)) {
8454 const Loop *L = AR->getLoop();
Dan Gohman20d9ce22010-11-17 21:41:58 +00008455 if (isLoopInvariant(LHS, L) && properlyDominates(LHS, L->getHeader())) {
Dan Gohman48ff3cf2010-04-24 01:28:42 +00008456 std::swap(LHS, RHS);
8457 Pred = ICmpInst::getSwappedPredicate(Pred);
8458 Changed = true;
8459 }
Dan Gohmandf564ca2010-05-03 17:00:11 +00008460 }
Dan Gohman48ff3cf2010-04-24 01:28:42 +00008461
8462 // If there's a constant operand, canonicalize comparisons with boundary
8463 // cases, and canonicalize *-or-equal comparisons to regular comparisons.
8464 if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00008465 const APInt &RA = RC->getAPInt();
Sanjoy Das4aeb0f22016-10-02 20:59:10 +00008466
8467 bool SimplifiedByConstantRange = false;
8468
8469 if (!ICmpInst::isEquality(Pred)) {
8470 ConstantRange ExactCR = ConstantRange::makeExactICmpRegion(Pred, RA);
8471 if (ExactCR.isFullSet())
8472 goto trivially_true;
8473 else if (ExactCR.isEmptySet())
8474 goto trivially_false;
8475
8476 APInt NewRHS;
8477 CmpInst::Predicate NewPred;
8478 if (ExactCR.getEquivalentICmp(NewPred, NewRHS) &&
8479 ICmpInst::isEquality(NewPred)) {
8480 // We were able to convert an inequality to an equality.
8481 Pred = NewPred;
8482 RHS = getConstant(NewRHS);
8483 Changed = SimplifiedByConstantRange = true;
8484 }
8485 }
8486
8487 if (!SimplifiedByConstantRange) {
8488 switch (Pred) {
8489 default:
8490 break;
8491 case ICmpInst::ICMP_EQ:
8492 case ICmpInst::ICMP_NE:
8493 // Fold ((-1) * %a) + %b == 0 (equivalent to %b-%a == 0) into %a == %b.
8494 if (!RA)
8495 if (const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(LHS))
8496 if (const SCEVMulExpr *ME =
8497 dyn_cast<SCEVMulExpr>(AE->getOperand(0)))
8498 if (AE->getNumOperands() == 2 && ME->getNumOperands() == 2 &&
8499 ME->getOperand(0)->isAllOnesValue()) {
8500 RHS = AE->getOperand(1);
8501 LHS = ME->getOperand(1);
8502 Changed = true;
8503 }
8504 break;
8505
8506
8507 // The "Should have been caught earlier!" messages refer to the fact
8508 // that the ExactCR.isFullSet() or ExactCR.isEmptySet() check above
8509 // should have fired on the corresponding cases, and canonicalized the
8510 // check to trivially_true or trivially_false.
8511
8512 case ICmpInst::ICMP_UGE:
8513 assert(!RA.isMinValue() && "Should have been caught earlier!");
8514 Pred = ICmpInst::ICMP_UGT;
Sanjoy Dasf230b0a2016-10-02 02:40:27 +00008515 RHS = getConstant(RA - 1);
8516 Changed = true;
8517 break;
Sanjoy Das4aeb0f22016-10-02 20:59:10 +00008518 case ICmpInst::ICMP_ULE:
8519 assert(!RA.isMaxValue() && "Should have been caught earlier!");
8520 Pred = ICmpInst::ICMP_ULT;
Dan Gohman48ff3cf2010-04-24 01:28:42 +00008521 RHS = getConstant(RA + 1);
8522 Changed = true;
8523 break;
Sanjoy Das4aeb0f22016-10-02 20:59:10 +00008524 case ICmpInst::ICMP_SGE:
8525 assert(!RA.isMinSignedValue() && "Should have been caught earlier!");
8526 Pred = ICmpInst::ICMP_SGT;
Sanjoy Dasf230b0a2016-10-02 02:40:27 +00008527 RHS = getConstant(RA - 1);
8528 Changed = true;
8529 break;
Sanjoy Das4aeb0f22016-10-02 20:59:10 +00008530 case ICmpInst::ICMP_SLE:
8531 assert(!RA.isMaxSignedValue() && "Should have been caught earlier!");
8532 Pred = ICmpInst::ICMP_SLT;
Sanjoy Dasf230b0a2016-10-02 02:40:27 +00008533 RHS = getConstant(RA + 1);
8534 Changed = true;
8535 break;
8536 }
Dan Gohman48ff3cf2010-04-24 01:28:42 +00008537 }
8538 }
8539
8540 // Check for obvious equality.
8541 if (HasSameValue(LHS, RHS)) {
8542 if (ICmpInst::isTrueWhenEqual(Pred))
8543 goto trivially_true;
8544 if (ICmpInst::isFalseWhenEqual(Pred))
8545 goto trivially_false;
8546 }
8547
Dan Gohman81585c12010-05-03 16:35:17 +00008548 // If possible, canonicalize GE/LE comparisons to GT/LT comparisons, by
8549 // adding or subtracting 1 from one of the operands.
8550 switch (Pred) {
8551 case ICmpInst::ICMP_SLE:
Craig Topper01020392017-06-24 23:34:50 +00008552 if (!getSignedRangeMax(RHS).isMaxSignedValue()) {
Dan Gohman81585c12010-05-03 16:35:17 +00008553 RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS,
Andrew Trick8b55b732011-03-14 16:50:06 +00008554 SCEV::FlagNSW);
Dan Gohman81585c12010-05-03 16:35:17 +00008555 Pred = ICmpInst::ICMP_SLT;
8556 Changed = true;
Craig Topper01020392017-06-24 23:34:50 +00008557 } else if (!getSignedRangeMin(LHS).isMinSignedValue()) {
Dan Gohman267700c2010-05-03 20:23:47 +00008558 LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS,
Andrew Trick8b55b732011-03-14 16:50:06 +00008559 SCEV::FlagNSW);
Dan Gohman81585c12010-05-03 16:35:17 +00008560 Pred = ICmpInst::ICMP_SLT;
8561 Changed = true;
8562 }
8563 break;
8564 case ICmpInst::ICMP_SGE:
Craig Topper01020392017-06-24 23:34:50 +00008565 if (!getSignedRangeMin(RHS).isMinSignedValue()) {
Dan Gohman267700c2010-05-03 20:23:47 +00008566 RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS,
Andrew Trick8b55b732011-03-14 16:50:06 +00008567 SCEV::FlagNSW);
Dan Gohman81585c12010-05-03 16:35:17 +00008568 Pred = ICmpInst::ICMP_SGT;
8569 Changed = true;
Craig Topper01020392017-06-24 23:34:50 +00008570 } else if (!getSignedRangeMax(LHS).isMaxSignedValue()) {
Dan Gohman81585c12010-05-03 16:35:17 +00008571 LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS,
Andrew Trick8b55b732011-03-14 16:50:06 +00008572 SCEV::FlagNSW);
Dan Gohman81585c12010-05-03 16:35:17 +00008573 Pred = ICmpInst::ICMP_SGT;
8574 Changed = true;
8575 }
8576 break;
8577 case ICmpInst::ICMP_ULE:
Craig Topper01020392017-06-24 23:34:50 +00008578 if (!getUnsignedRangeMax(RHS).isMaxValue()) {
Dan Gohman267700c2010-05-03 20:23:47 +00008579 RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS,
Andrew Trick8b55b732011-03-14 16:50:06 +00008580 SCEV::FlagNUW);
Dan Gohman81585c12010-05-03 16:35:17 +00008581 Pred = ICmpInst::ICMP_ULT;
8582 Changed = true;
Craig Topper01020392017-06-24 23:34:50 +00008583 } else if (!getUnsignedRangeMin(LHS).isMinValue()) {
Peter Collingbournec85f4ce2015-11-20 01:26:13 +00008584 LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS);
Dan Gohman81585c12010-05-03 16:35:17 +00008585 Pred = ICmpInst::ICMP_ULT;
8586 Changed = true;
8587 }
8588 break;
8589 case ICmpInst::ICMP_UGE:
Craig Topper01020392017-06-24 23:34:50 +00008590 if (!getUnsignedRangeMin(RHS).isMinValue()) {
Peter Collingbournec85f4ce2015-11-20 01:26:13 +00008591 RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS);
Dan Gohman81585c12010-05-03 16:35:17 +00008592 Pred = ICmpInst::ICMP_UGT;
8593 Changed = true;
Craig Topper01020392017-06-24 23:34:50 +00008594 } else if (!getUnsignedRangeMax(LHS).isMaxValue()) {
Dan Gohman267700c2010-05-03 20:23:47 +00008595 LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS,
Andrew Trick8b55b732011-03-14 16:50:06 +00008596 SCEV::FlagNUW);
Dan Gohman81585c12010-05-03 16:35:17 +00008597 Pred = ICmpInst::ICMP_UGT;
8598 Changed = true;
8599 }
8600 break;
8601 default:
8602 break;
8603 }
8604
Dan Gohman48ff3cf2010-04-24 01:28:42 +00008605 // TODO: More simplifications are possible here.
8606
Benjamin Kramer50b26eb2012-05-30 18:32:23 +00008607 // Recursively simplify until we either hit a recursion limit or nothing
8608 // changes.
8609 if (Changed)
8610 return SimplifyICmpOperands(Pred, LHS, RHS, Depth+1);
8611
Dan Gohman48ff3cf2010-04-24 01:28:42 +00008612 return Changed;
8613
8614trivially_true:
8615 // Return 0 == 0.
Benjamin Kramerddd1b7b2010-11-20 18:43:35 +00008616 LHS = RHS = getConstant(ConstantInt::getFalse(getContext()));
Dan Gohman48ff3cf2010-04-24 01:28:42 +00008617 Pred = ICmpInst::ICMP_EQ;
8618 return true;
8619
8620trivially_false:
8621 // Return 0 != 0.
Benjamin Kramerddd1b7b2010-11-20 18:43:35 +00008622 LHS = RHS = getConstant(ConstantInt::getFalse(getContext()));
Dan Gohman48ff3cf2010-04-24 01:28:42 +00008623 Pred = ICmpInst::ICMP_NE;
8624 return true;
8625}
8626
Dan Gohmane65c9172009-07-13 21:35:55 +00008627bool ScalarEvolution::isKnownNegative(const SCEV *S) {
Craig Topper01020392017-06-24 23:34:50 +00008628 return getSignedRangeMax(S).isNegative();
Dan Gohmane65c9172009-07-13 21:35:55 +00008629}
8630
8631bool ScalarEvolution::isKnownPositive(const SCEV *S) {
Craig Topper01020392017-06-24 23:34:50 +00008632 return getSignedRangeMin(S).isStrictlyPositive();
Dan Gohmane65c9172009-07-13 21:35:55 +00008633}
8634
8635bool ScalarEvolution::isKnownNonNegative(const SCEV *S) {
Craig Topper01020392017-06-24 23:34:50 +00008636 return !getSignedRangeMin(S).isNegative();
Dan Gohmane65c9172009-07-13 21:35:55 +00008637}
8638
8639bool ScalarEvolution::isKnownNonPositive(const SCEV *S) {
Craig Topper01020392017-06-24 23:34:50 +00008640 return !getSignedRangeMax(S).isStrictlyPositive();
Dan Gohmane65c9172009-07-13 21:35:55 +00008641}
8642
8643bool ScalarEvolution::isKnownNonZero(const SCEV *S) {
8644 return isKnownNegative(S) || isKnownPositive(S);
8645}
8646
8647bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred,
8648 const SCEV *LHS, const SCEV *RHS) {
Dan Gohman36cce7e2010-04-24 01:38:36 +00008649 // Canonicalize the inputs first.
8650 (void)SimplifyICmpOperands(Pred, LHS, RHS);
8651
Dan Gohman07591692010-04-11 22:16:48 +00008652 // If LHS or RHS is an addrec, check to see if the condition is true in
8653 // every iteration of the loop.
Justin Bognercbb84382014-05-23 00:06:56 +00008654 // If LHS and RHS are both addrec, both conditions must be true in
8655 // every iteration of the loop.
8656 const SCEVAddRecExpr *LAR = dyn_cast<SCEVAddRecExpr>(LHS);
8657 const SCEVAddRecExpr *RAR = dyn_cast<SCEVAddRecExpr>(RHS);
8658 bool LeftGuarded = false;
8659 bool RightGuarded = false;
8660 if (LAR) {
8661 const Loop *L = LAR->getLoop();
8662 if (isLoopEntryGuardedByCond(L, Pred, LAR->getStart(), RHS) &&
8663 isLoopBackedgeGuardedByCond(L, Pred, LAR->getPostIncExpr(*this), RHS)) {
8664 if (!RAR) return true;
8665 LeftGuarded = true;
8666 }
8667 }
8668 if (RAR) {
8669 const Loop *L = RAR->getLoop();
8670 if (isLoopEntryGuardedByCond(L, Pred, LHS, RAR->getStart()) &&
8671 isLoopBackedgeGuardedByCond(L, Pred, LHS, RAR->getPostIncExpr(*this))) {
8672 if (!LAR) return true;
8673 RightGuarded = true;
8674 }
8675 }
8676 if (LeftGuarded && RightGuarded)
8677 return true;
Dan Gohmane65c9172009-07-13 21:35:55 +00008678
Sanjoy Das7d910f22015-10-02 18:50:30 +00008679 if (isKnownPredicateViaSplitting(Pred, LHS, RHS))
8680 return true;
8681
Dan Gohman07591692010-04-11 22:16:48 +00008682 // Otherwise see what can be done with known constant ranges.
Sanjoy Das401e6312016-02-01 20:48:10 +00008683 return isKnownPredicateViaConstantRanges(Pred, LHS, RHS);
Dan Gohman07591692010-04-11 22:16:48 +00008684}
8685
Sanjoy Das5dab2052015-07-27 21:42:49 +00008686bool ScalarEvolution::isMonotonicPredicate(const SCEVAddRecExpr *LHS,
8687 ICmpInst::Predicate Pred,
8688 bool &Increasing) {
8689 bool Result = isMonotonicPredicateImpl(LHS, Pred, Increasing);
8690
8691#ifndef NDEBUG
8692 // Verify an invariant: inverting the predicate should turn a monotonically
8693 // increasing change to a monotonically decreasing one, and vice versa.
8694 bool IncreasingSwapped;
8695 bool ResultSwapped = isMonotonicPredicateImpl(
8696 LHS, ICmpInst::getSwappedPredicate(Pred), IncreasingSwapped);
8697
8698 assert(Result == ResultSwapped && "should be able to analyze both!");
8699 if (ResultSwapped)
8700 assert(Increasing == !IncreasingSwapped &&
8701 "monotonicity should flip as we flip the predicate");
8702#endif
8703
8704 return Result;
8705}
8706
8707bool ScalarEvolution::isMonotonicPredicateImpl(const SCEVAddRecExpr *LHS,
8708 ICmpInst::Predicate Pred,
8709 bool &Increasing) {
Sanjoy Das5dab2052015-07-27 21:42:49 +00008710
8711 // A zero step value for LHS means the induction variable is essentially a
8712 // loop invariant value. We don't really depend on the predicate actually
8713 // flipping from false to true (for increasing predicates, and the other way
8714 // around for decreasing predicates), all we care about is that *if* the
8715 // predicate changes then it only changes from false to true.
8716 //
8717 // A zero step value in itself is not very useful, but there may be places
8718 // where SCEV can prove X >= 0 but not prove X > 0, so it is helpful to be
8719 // as general as possible.
8720
Sanjoy Das366acc12015-08-06 20:43:41 +00008721 switch (Pred) {
8722 default:
8723 return false; // Conservative answer
8724
8725 case ICmpInst::ICMP_UGT:
8726 case ICmpInst::ICMP_UGE:
8727 case ICmpInst::ICMP_ULT:
8728 case ICmpInst::ICMP_ULE:
Sanjoy Das76c48e02016-02-04 18:21:54 +00008729 if (!LHS->hasNoUnsignedWrap())
Sanjoy Das366acc12015-08-06 20:43:41 +00008730 return false;
8731
8732 Increasing = Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE;
Sanjoy Das5dab2052015-07-27 21:42:49 +00008733 return true;
Sanjoy Das366acc12015-08-06 20:43:41 +00008734
8735 case ICmpInst::ICMP_SGT:
8736 case ICmpInst::ICMP_SGE:
8737 case ICmpInst::ICMP_SLT:
8738 case ICmpInst::ICMP_SLE: {
Sanjoy Das76c48e02016-02-04 18:21:54 +00008739 if (!LHS->hasNoSignedWrap())
Sanjoy Das366acc12015-08-06 20:43:41 +00008740 return false;
8741
8742 const SCEV *Step = LHS->getStepRecurrence(*this);
8743
8744 if (isKnownNonNegative(Step)) {
8745 Increasing = Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE;
8746 return true;
8747 }
8748
8749 if (isKnownNonPositive(Step)) {
8750 Increasing = Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE;
8751 return true;
8752 }
8753
8754 return false;
Sanjoy Das5dab2052015-07-27 21:42:49 +00008755 }
8756
Sanjoy Das5dab2052015-07-27 21:42:49 +00008757 }
8758
Sanjoy Das366acc12015-08-06 20:43:41 +00008759 llvm_unreachable("switch has default clause!");
Sanjoy Das5dab2052015-07-27 21:42:49 +00008760}
8761
8762bool ScalarEvolution::isLoopInvariantPredicate(
8763 ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L,
8764 ICmpInst::Predicate &InvariantPred, const SCEV *&InvariantLHS,
8765 const SCEV *&InvariantRHS) {
8766
8767 // If there is a loop-invariant, force it into the RHS, otherwise bail out.
8768 if (!isLoopInvariant(RHS, L)) {
8769 if (!isLoopInvariant(LHS, L))
8770 return false;
8771
8772 std::swap(LHS, RHS);
8773 Pred = ICmpInst::getSwappedPredicate(Pred);
8774 }
8775
8776 const SCEVAddRecExpr *ArLHS = dyn_cast<SCEVAddRecExpr>(LHS);
8777 if (!ArLHS || ArLHS->getLoop() != L)
8778 return false;
8779
8780 bool Increasing;
8781 if (!isMonotonicPredicate(ArLHS, Pred, Increasing))
8782 return false;
8783
8784 // If the predicate "ArLHS `Pred` RHS" monotonically increases from false to
8785 // true as the loop iterates, and the backedge is control dependent on
8786 // "ArLHS `Pred` RHS" == true then we can reason as follows:
8787 //
8788 // * if the predicate was false in the first iteration then the predicate
8789 // is never evaluated again, since the loop exits without taking the
8790 // backedge.
8791 // * if the predicate was true in the first iteration then it will
8792 // continue to be true for all future iterations since it is
8793 // monotonically increasing.
8794 //
8795 // For both the above possibilities, we can replace the loop varying
8796 // predicate with its value on the first iteration of the loop (which is
8797 // loop invariant).
8798 //
8799 // A similar reasoning applies for a monotonically decreasing predicate, by
8800 // replacing true with false and false with true in the above two bullets.
8801
8802 auto P = Increasing ? Pred : ICmpInst::getInversePredicate(Pred);
8803
8804 if (!isLoopBackedgeGuardedByCond(L, P, LHS, RHS))
8805 return false;
8806
8807 InvariantPred = Pred;
8808 InvariantLHS = ArLHS->getStart();
8809 InvariantRHS = RHS;
8810 return true;
8811}
8812
Sanjoy Das401e6312016-02-01 20:48:10 +00008813bool ScalarEvolution::isKnownPredicateViaConstantRanges(
8814 ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS) {
Dan Gohmane65c9172009-07-13 21:35:55 +00008815 if (HasSameValue(LHS, RHS))
8816 return ICmpInst::isTrueWhenEqual(Pred);
8817
Dan Gohman07591692010-04-11 22:16:48 +00008818 // This code is split out from isKnownPredicate because it is called from
8819 // within isLoopEntryGuardedByCond.
Dan Gohmane65c9172009-07-13 21:35:55 +00008820
Sanjoy Das4c7b6d72016-02-01 20:48:14 +00008821 auto CheckRanges =
8822 [&](const ConstantRange &RangeLHS, const ConstantRange &RangeRHS) {
8823 return ConstantRange::makeSatisfyingICmpRegion(Pred, RangeRHS)
8824 .contains(RangeLHS);
8825 };
8826
8827 // The check at the top of the function catches the case where the values are
8828 // known to be equal.
8829 if (Pred == CmpInst::ICMP_EQ)
8830 return false;
8831
8832 if (Pred == CmpInst::ICMP_NE)
8833 return CheckRanges(getSignedRange(LHS), getSignedRange(RHS)) ||
8834 CheckRanges(getUnsignedRange(LHS), getUnsignedRange(RHS)) ||
8835 isKnownNonZero(getMinusSCEV(LHS, RHS));
8836
8837 if (CmpInst::isSigned(Pred))
8838 return CheckRanges(getSignedRange(LHS), getSignedRange(RHS));
8839
8840 return CheckRanges(getUnsignedRange(LHS), getUnsignedRange(RHS));
Dan Gohmane65c9172009-07-13 21:35:55 +00008841}
8842
Sanjoy Dasc1a29772015-11-05 23:45:38 +00008843bool ScalarEvolution::isKnownPredicateViaNoOverflow(ICmpInst::Predicate Pred,
8844 const SCEV *LHS,
8845 const SCEV *RHS) {
Sanjoy Dasc1a29772015-11-05 23:45:38 +00008846 // Match Result to (X + Y)<ExpectedFlags> where Y is a constant integer.
8847 // Return Y via OutY.
8848 auto MatchBinaryAddToConst =
8849 [this](const SCEV *Result, const SCEV *X, APInt &OutY,
8850 SCEV::NoWrapFlags ExpectedFlags) {
8851 const SCEV *NonConstOp, *ConstOp;
8852 SCEV::NoWrapFlags FlagsPresent;
8853
8854 if (!splitBinaryAdd(Result, ConstOp, NonConstOp, FlagsPresent) ||
8855 !isa<SCEVConstant>(ConstOp) || NonConstOp != X)
8856 return false;
8857
Sanjoy Das0de2fec2015-12-17 20:28:46 +00008858 OutY = cast<SCEVConstant>(ConstOp)->getAPInt();
Sanjoy Dasc1a29772015-11-05 23:45:38 +00008859 return (FlagsPresent & ExpectedFlags) == ExpectedFlags;
8860 };
8861
8862 APInt C;
8863
8864 switch (Pred) {
8865 default:
8866 break;
8867
8868 case ICmpInst::ICMP_SGE:
8869 std::swap(LHS, RHS);
Galina Kistanova8514dd52017-05-31 22:09:46 +00008870 LLVM_FALLTHROUGH;
Sanjoy Dasc1a29772015-11-05 23:45:38 +00008871 case ICmpInst::ICMP_SLE:
8872 // X s<= (X + C)<nsw> if C >= 0
8873 if (MatchBinaryAddToConst(RHS, LHS, C, SCEV::FlagNSW) && C.isNonNegative())
8874 return true;
8875
8876 // (X + C)<nsw> s<= X if C <= 0
8877 if (MatchBinaryAddToConst(LHS, RHS, C, SCEV::FlagNSW) &&
8878 !C.isStrictlyPositive())
8879 return true;
8880 break;
8881
8882 case ICmpInst::ICMP_SGT:
8883 std::swap(LHS, RHS);
Galina Kistanova8514dd52017-05-31 22:09:46 +00008884 LLVM_FALLTHROUGH;
Sanjoy Dasc1a29772015-11-05 23:45:38 +00008885 case ICmpInst::ICMP_SLT:
8886 // X s< (X + C)<nsw> if C > 0
8887 if (MatchBinaryAddToConst(RHS, LHS, C, SCEV::FlagNSW) &&
8888 C.isStrictlyPositive())
8889 return true;
8890
8891 // (X + C)<nsw> s< X if C < 0
8892 if (MatchBinaryAddToConst(LHS, RHS, C, SCEV::FlagNSW) && C.isNegative())
8893 return true;
8894 break;
8895 }
8896
8897 return false;
8898}
8899
Sanjoy Das7d910f22015-10-02 18:50:30 +00008900bool ScalarEvolution::isKnownPredicateViaSplitting(ICmpInst::Predicate Pred,
8901 const SCEV *LHS,
8902 const SCEV *RHS) {
Sanjoy Das10dffcb2015-10-08 03:46:00 +00008903 if (Pred != ICmpInst::ICMP_ULT || ProvingSplitPredicate)
Sanjoy Das7d910f22015-10-02 18:50:30 +00008904 return false;
8905
8906 // Allowing arbitrary number of activations of isKnownPredicateViaSplitting on
8907 // the stack can result in exponential time complexity.
8908 SaveAndRestore<bool> Restore(ProvingSplitPredicate, true);
8909
8910 // If L >= 0 then I `ult` L <=> I >= 0 && I `slt` L
8911 //
8912 // To prove L >= 0 we use isKnownNonNegative whereas to prove I >= 0 we use
8913 // isKnownPredicate. isKnownPredicate is more powerful, but also more
8914 // expensive; and using isKnownNonNegative(RHS) is sufficient for most of the
8915 // interesting cases seen in practice. We can consider "upgrading" L >= 0 to
8916 // use isKnownPredicate later if needed.
Alexander Kornienko484e48e32015-11-05 21:07:12 +00008917 return isKnownNonNegative(RHS) &&
8918 isKnownPredicate(CmpInst::ICMP_SGE, LHS, getZero(LHS->getType())) &&
8919 isKnownPredicate(CmpInst::ICMP_SLT, LHS, RHS);
Sanjoy Das7d910f22015-10-02 18:50:30 +00008920}
8921
Sanjoy Das2512d0c2016-05-10 00:31:49 +00008922bool ScalarEvolution::isImpliedViaGuard(BasicBlock *BB,
8923 ICmpInst::Predicate Pred,
8924 const SCEV *LHS, const SCEV *RHS) {
8925 // No need to even try if we know the module has no guards.
8926 if (!HasGuards)
8927 return false;
8928
8929 return any_of(*BB, [&](Instruction &I) {
8930 using namespace llvm::PatternMatch;
8931
8932 Value *Condition;
8933 return match(&I, m_Intrinsic<Intrinsic::experimental_guard>(
8934 m_Value(Condition))) &&
8935 isImpliedCond(Pred, LHS, RHS, Condition, false);
8936 });
8937}
8938
Dan Gohmane65c9172009-07-13 21:35:55 +00008939/// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
8940/// protected by a conditional between LHS and RHS. This is used to
8941/// to eliminate casts.
8942bool
8943ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L,
8944 ICmpInst::Predicate Pred,
8945 const SCEV *LHS, const SCEV *RHS) {
8946 // Interpret a null as meaning no loop, where there is obviously no guard
8947 // (interprocedural conditions notwithstanding).
8948 if (!L) return true;
8949
Sanjoy Das401e6312016-02-01 20:48:10 +00008950 if (isKnownPredicateViaConstantRanges(Pred, LHS, RHS))
8951 return true;
Sanjoy Das1f05c512014-10-10 21:22:34 +00008952
Dan Gohmane65c9172009-07-13 21:35:55 +00008953 BasicBlock *Latch = L->getLoopLatch();
8954 if (!Latch)
8955 return false;
8956
8957 BranchInst *LoopContinuePredicate =
8958 dyn_cast<BranchInst>(Latch->getTerminator());
Hal Finkelcebf0cc2014-09-07 21:37:59 +00008959 if (LoopContinuePredicate && LoopContinuePredicate->isConditional() &&
8960 isImpliedCond(Pred, LHS, RHS,
8961 LoopContinuePredicate->getCondition(),
8962 LoopContinuePredicate->getSuccessor(0) != L->getHeader()))
8963 return true;
Dan Gohmane65c9172009-07-13 21:35:55 +00008964
Piotr Padlewski0dde00d22015-09-09 20:47:30 +00008965 // We don't want more than one activation of the following loops on the stack
Sanjoy Dasb864c1f2015-04-01 18:24:06 +00008966 // -- that can lead to O(n!) time complexity.
8967 if (WalkingBEDominatingConds)
8968 return false;
8969
Sanjoy Das5d9a8cb2015-09-22 00:10:57 +00008970 SaveAndRestore<bool> ClearOnExit(WalkingBEDominatingConds, true);
Sanjoy Dasb864c1f2015-04-01 18:24:06 +00008971
Sanjoy Dasb174f9a2015-09-25 23:53:50 +00008972 // See if we can exploit a trip count to prove the predicate.
8973 const auto &BETakenInfo = getBackedgeTakenInfo(L);
8974 const SCEV *LatchBECount = BETakenInfo.getExact(Latch, this);
8975 if (LatchBECount != getCouldNotCompute()) {
8976 // We know that Latch branches back to the loop header exactly
8977 // LatchBECount times. This means the backdege condition at Latch is
8978 // equivalent to "{0,+,1} u< LatchBECount".
8979 Type *Ty = LatchBECount->getType();
8980 auto NoWrapFlags = SCEV::NoWrapFlags(SCEV::FlagNUW | SCEV::FlagNW);
8981 const SCEV *LoopCounter =
8982 getAddRecExpr(getZero(Ty), getOne(Ty), L, NoWrapFlags);
8983 if (isImpliedCond(Pred, LHS, RHS, ICmpInst::ICMP_ULT, LoopCounter,
8984 LatchBECount))
8985 return true;
8986 }
8987
Piotr Padlewski0dde00d22015-09-09 20:47:30 +00008988 // Check conditions due to any @llvm.assume intrinsics.
Daniel Jasperaec2fa32016-12-19 08:22:17 +00008989 for (auto &AssumeVH : AC.assumptions()) {
8990 if (!AssumeVH)
8991 continue;
8992 auto *CI = cast<CallInst>(AssumeVH);
8993 if (!DT.dominates(CI, Latch->getTerminator()))
8994 continue;
Piotr Padlewski0dde00d22015-09-09 20:47:30 +00008995
Daniel Jasperaec2fa32016-12-19 08:22:17 +00008996 if (isImpliedCond(Pred, LHS, RHS, CI->getArgOperand(0), false))
8997 return true;
8998 }
Piotr Padlewski0dde00d22015-09-09 20:47:30 +00008999
Sanjoy Dasb864c1f2015-04-01 18:24:06 +00009000 // If the loop is not reachable from the entry block, we risk running into an
9001 // infinite loop as we walk up into the dom tree. These loops do not matter
9002 // anyway, so we just return a conservative answer when we see them.
Chandler Carruth2f1fd162015-08-17 02:08:17 +00009003 if (!DT.isReachableFromEntry(L->getHeader()))
Sanjoy Dasb864c1f2015-04-01 18:24:06 +00009004 return false;
9005
Sanjoy Das2512d0c2016-05-10 00:31:49 +00009006 if (isImpliedViaGuard(Latch, Pred, LHS, RHS))
9007 return true;
9008
Chandler Carruth2f1fd162015-08-17 02:08:17 +00009009 for (DomTreeNode *DTN = DT[Latch], *HeaderDTN = DT[L->getHeader()];
9010 DTN != HeaderDTN; DTN = DTN->getIDom()) {
Sanjoy Dasb864c1f2015-04-01 18:24:06 +00009011 assert(DTN && "should reach the loop header before reaching the root!");
9012
9013 BasicBlock *BB = DTN->getBlock();
Sanjoy Das2512d0c2016-05-10 00:31:49 +00009014 if (isImpliedViaGuard(BB, Pred, LHS, RHS))
9015 return true;
9016
Sanjoy Dasb864c1f2015-04-01 18:24:06 +00009017 BasicBlock *PBB = BB->getSinglePredecessor();
9018 if (!PBB)
9019 continue;
9020
9021 BranchInst *ContinuePredicate = dyn_cast<BranchInst>(PBB->getTerminator());
9022 if (!ContinuePredicate || !ContinuePredicate->isConditional())
9023 continue;
9024
9025 Value *Condition = ContinuePredicate->getCondition();
9026
9027 // If we have an edge `E` within the loop body that dominates the only
9028 // latch, the condition guarding `E` also guards the backedge. This
9029 // reasoning works only for loops with a single latch.
9030
9031 BasicBlockEdge DominatingEdge(PBB, BB);
9032 if (DominatingEdge.isSingleEdge()) {
9033 // We're constructively (and conservatively) enumerating edges within the
9034 // loop body that dominate the latch. The dominator tree better agree
9035 // with us on this:
Chandler Carruth2f1fd162015-08-17 02:08:17 +00009036 assert(DT.dominates(DominatingEdge, Latch) && "should be!");
Sanjoy Dasb864c1f2015-04-01 18:24:06 +00009037
9038 if (isImpliedCond(Pred, LHS, RHS, Condition,
9039 BB != ContinuePredicate->getSuccessor(0)))
9040 return true;
9041 }
9042 }
9043
Hal Finkelcebf0cc2014-09-07 21:37:59 +00009044 return false;
Dan Gohmane65c9172009-07-13 21:35:55 +00009045}
9046
Dan Gohmane65c9172009-07-13 21:35:55 +00009047bool
Dan Gohmanb50349a2010-04-11 19:27:13 +00009048ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L,
9049 ICmpInst::Predicate Pred,
9050 const SCEV *LHS, const SCEV *RHS) {
Dan Gohman9cf09f82009-05-18 16:03:58 +00009051 // Interpret a null as meaning no loop, where there is obviously no guard
9052 // (interprocedural conditions notwithstanding).
9053 if (!L) return false;
9054
Sanjoy Das401e6312016-02-01 20:48:10 +00009055 if (isKnownPredicateViaConstantRanges(Pred, LHS, RHS))
9056 return true;
Sanjoy Das1f05c512014-10-10 21:22:34 +00009057
Dan Gohman8c77f1a2009-05-18 15:36:09 +00009058 // Starting at the loop predecessor, climb up the predecessor chain, as long
9059 // as there are predecessors that can be found that have unique successors
Dan Gohmanf9081a22008-09-15 22:18:04 +00009060 // leading to the original header.
Dan Gohman4e3c1132010-04-15 16:19:08 +00009061 for (std::pair<BasicBlock *, BasicBlock *>
Dan Gohman75c6b0b2010-06-22 23:43:28 +00009062 Pair(L->getLoopPredecessor(), L->getHeader());
Dan Gohman4e3c1132010-04-15 16:19:08 +00009063 Pair.first;
9064 Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
Dan Gohman2a62fd92008-08-12 20:17:31 +00009065
Sanjoy Das2512d0c2016-05-10 00:31:49 +00009066 if (isImpliedViaGuard(Pair.first, Pred, LHS, RHS))
9067 return true;
9068
Dan Gohman2a62fd92008-08-12 20:17:31 +00009069 BranchInst *LoopEntryPredicate =
Dan Gohman4e3c1132010-04-15 16:19:08 +00009070 dyn_cast<BranchInst>(Pair.first->getTerminator());
Dan Gohman2a62fd92008-08-12 20:17:31 +00009071 if (!LoopEntryPredicate ||
9072 LoopEntryPredicate->isUnconditional())
9073 continue;
9074
Dan Gohmane18c2d62010-08-10 23:46:30 +00009075 if (isImpliedCond(Pred, LHS, RHS,
9076 LoopEntryPredicate->getCondition(),
Dan Gohman4e3c1132010-04-15 16:19:08 +00009077 LoopEntryPredicate->getSuccessor(0) != Pair.second))
Dan Gohman2a62fd92008-08-12 20:17:31 +00009078 return true;
Nick Lewyckyb5688cc2008-07-12 07:41:32 +00009079 }
9080
Hal Finkelcebf0cc2014-09-07 21:37:59 +00009081 // Check conditions due to any @llvm.assume intrinsics.
Daniel Jasperaec2fa32016-12-19 08:22:17 +00009082 for (auto &AssumeVH : AC.assumptions()) {
9083 if (!AssumeVH)
9084 continue;
9085 auto *CI = cast<CallInst>(AssumeVH);
9086 if (!DT.dominates(CI, L->getHeader()))
9087 continue;
Hal Finkelcebf0cc2014-09-07 21:37:59 +00009088
Daniel Jasperaec2fa32016-12-19 08:22:17 +00009089 if (isImpliedCond(Pred, LHS, RHS, CI->getArgOperand(0), false))
9090 return true;
9091 }
Hal Finkelcebf0cc2014-09-07 21:37:59 +00009092
Dan Gohman2a62fd92008-08-12 20:17:31 +00009093 return false;
Nick Lewyckyb5688cc2008-07-12 07:41:32 +00009094}
9095
Dan Gohmane18c2d62010-08-10 23:46:30 +00009096bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred,
Dan Gohman430f0cc2009-07-21 23:03:19 +00009097 const SCEV *LHS, const SCEV *RHS,
Dan Gohmane18c2d62010-08-10 23:46:30 +00009098 Value *FoundCondValue,
Dan Gohman430f0cc2009-07-21 23:03:19 +00009099 bool Inverse) {
Sanjoy Dasc46bceb2016-09-27 18:01:42 +00009100 if (!PendingLoopPredicates.insert(FoundCondValue).second)
Andrew Trick7fa4e0f2012-05-19 00:48:25 +00009101 return false;
9102
Sanjoy Dasc46bceb2016-09-27 18:01:42 +00009103 auto ClearOnExit =
9104 make_scope_exit([&]() { PendingLoopPredicates.erase(FoundCondValue); });
9105
Dan Gohman8b0a4192010-03-01 17:49:51 +00009106 // Recursively handle And and Or conditions.
Dan Gohmane18c2d62010-08-10 23:46:30 +00009107 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FoundCondValue)) {
Dan Gohmanf19aeec2009-06-24 01:18:18 +00009108 if (BO->getOpcode() == Instruction::And) {
9109 if (!Inverse)
Dan Gohmane18c2d62010-08-10 23:46:30 +00009110 return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) ||
9111 isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse);
Dan Gohmanf19aeec2009-06-24 01:18:18 +00009112 } else if (BO->getOpcode() == Instruction::Or) {
9113 if (Inverse)
Dan Gohmane18c2d62010-08-10 23:46:30 +00009114 return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) ||
9115 isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse);
Dan Gohmanf19aeec2009-06-24 01:18:18 +00009116 }
9117 }
9118
Dan Gohmane18c2d62010-08-10 23:46:30 +00009119 ICmpInst *ICI = dyn_cast<ICmpInst>(FoundCondValue);
Dan Gohmanf19aeec2009-06-24 01:18:18 +00009120 if (!ICI) return false;
9121
Andrew Trickfa594032012-11-29 18:35:13 +00009122 // Now that we found a conditional branch that dominates the loop or controls
9123 // the loop latch. Check to see if it is the comparison we are looking for.
Dan Gohman430f0cc2009-07-21 23:03:19 +00009124 ICmpInst::Predicate FoundPred;
9125 if (Inverse)
9126 FoundPred = ICI->getInversePredicate();
9127 else
9128 FoundPred = ICI->getPredicate();
9129
9130 const SCEV *FoundLHS = getSCEV(ICI->getOperand(0));
9131 const SCEV *FoundRHS = getSCEV(ICI->getOperand(1));
Dan Gohmane65c9172009-07-13 21:35:55 +00009132
Sanjoy Dasdf1635d2015-09-25 19:59:52 +00009133 return isImpliedCond(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS);
9134}
9135
9136bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS,
9137 const SCEV *RHS,
9138 ICmpInst::Predicate FoundPred,
9139 const SCEV *FoundLHS,
9140 const SCEV *FoundRHS) {
Sanjoy Das14598832015-03-26 17:28:26 +00009141 // Balance the types.
9142 if (getTypeSizeInBits(LHS->getType()) <
9143 getTypeSizeInBits(FoundLHS->getType())) {
9144 if (CmpInst::isSigned(Pred)) {
9145 LHS = getSignExtendExpr(LHS, FoundLHS->getType());
9146 RHS = getSignExtendExpr(RHS, FoundLHS->getType());
9147 } else {
9148 LHS = getZeroExtendExpr(LHS, FoundLHS->getType());
9149 RHS = getZeroExtendExpr(RHS, FoundLHS->getType());
9150 }
9151 } else if (getTypeSizeInBits(LHS->getType()) >
Dan Gohmane65c9172009-07-13 21:35:55 +00009152 getTypeSizeInBits(FoundLHS->getType())) {
Stepan Dyatkovskiy431993b2014-01-09 12:26:12 +00009153 if (CmpInst::isSigned(FoundPred)) {
Dan Gohmane65c9172009-07-13 21:35:55 +00009154 FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType());
9155 FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType());
9156 } else {
9157 FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType());
9158 FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType());
9159 }
9160 }
9161
Dan Gohman430f0cc2009-07-21 23:03:19 +00009162 // Canonicalize the query to match the way instcombine will have
9163 // canonicalized the comparison.
Dan Gohman3673aa12010-04-24 01:34:53 +00009164 if (SimplifyICmpOperands(Pred, LHS, RHS))
9165 if (LHS == RHS)
Dan Gohmanb5025c72010-05-03 18:00:24 +00009166 return CmpInst::isTrueWhenEqual(Pred);
Benjamin Kramerba11a982012-11-29 19:07:57 +00009167 if (SimplifyICmpOperands(FoundPred, FoundLHS, FoundRHS))
9168 if (FoundLHS == FoundRHS)
9169 return CmpInst::isFalseWhenEqual(FoundPred);
Dan Gohman430f0cc2009-07-21 23:03:19 +00009170
9171 // Check to see if we can make the LHS or RHS match.
9172 if (LHS == FoundRHS || RHS == FoundLHS) {
9173 if (isa<SCEVConstant>(RHS)) {
9174 std::swap(FoundLHS, FoundRHS);
9175 FoundPred = ICmpInst::getSwappedPredicate(FoundPred);
9176 } else {
9177 std::swap(LHS, RHS);
9178 Pred = ICmpInst::getSwappedPredicate(Pred);
9179 }
9180 }
9181
9182 // Check whether the found predicate is the same as the desired predicate.
9183 if (FoundPred == Pred)
9184 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS);
9185
9186 // Check whether swapping the found predicate makes it the same as the
9187 // desired predicate.
9188 if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) {
9189 if (isa<SCEVConstant>(RHS))
9190 return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS);
9191 else
9192 return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred),
9193 RHS, LHS, FoundLHS, FoundRHS);
9194 }
9195
Sanjoy Das6e78b172015-10-22 19:57:34 +00009196 // Unsigned comparison is the same as signed comparison when both the operands
9197 // are non-negative.
9198 if (CmpInst::isUnsigned(FoundPred) &&
9199 CmpInst::getSignedPredicate(FoundPred) == Pred &&
9200 isKnownNonNegative(FoundLHS) && isKnownNonNegative(FoundRHS))
9201 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS);
9202
Sanjoy Dasc5676df2014-11-13 00:00:58 +00009203 // Check if we can make progress by sharpening ranges.
9204 if (FoundPred == ICmpInst::ICMP_NE &&
9205 (isa<SCEVConstant>(FoundLHS) || isa<SCEVConstant>(FoundRHS))) {
9206
9207 const SCEVConstant *C = nullptr;
9208 const SCEV *V = nullptr;
9209
9210 if (isa<SCEVConstant>(FoundLHS)) {
9211 C = cast<SCEVConstant>(FoundLHS);
9212 V = FoundRHS;
9213 } else {
9214 C = cast<SCEVConstant>(FoundRHS);
9215 V = FoundLHS;
9216 }
9217
9218 // The guarding predicate tells us that C != V. If the known range
9219 // of V is [C, t), we can sharpen the range to [C + 1, t). The
9220 // range we consider has to correspond to same signedness as the
9221 // predicate we're interested in folding.
9222
9223 APInt Min = ICmpInst::isSigned(Pred) ?
Craig Topper01020392017-06-24 23:34:50 +00009224 getSignedRangeMin(V) : getUnsignedRangeMin(V);
Sanjoy Dasc5676df2014-11-13 00:00:58 +00009225
Sanjoy Das0de2fec2015-12-17 20:28:46 +00009226 if (Min == C->getAPInt()) {
Sanjoy Dasc5676df2014-11-13 00:00:58 +00009227 // Given (V >= Min && V != Min) we conclude V >= (Min + 1).
9228 // This is true even if (Min + 1) wraps around -- in case of
9229 // wraparound, (Min + 1) < Min, so (V >= Min => V >= (Min + 1)).
9230
9231 APInt SharperMin = Min + 1;
9232
9233 switch (Pred) {
9234 case ICmpInst::ICMP_SGE:
9235 case ICmpInst::ICMP_UGE:
9236 // We know V `Pred` SharperMin. If this implies LHS `Pred`
9237 // RHS, we're done.
9238 if (isImpliedCondOperands(Pred, LHS, RHS, V,
9239 getConstant(SharperMin)))
9240 return true;
Galina Kistanova8514dd52017-05-31 22:09:46 +00009241 LLVM_FALLTHROUGH;
Sanjoy Dasc5676df2014-11-13 00:00:58 +00009242
9243 case ICmpInst::ICMP_SGT:
9244 case ICmpInst::ICMP_UGT:
9245 // We know from the range information that (V `Pred` Min ||
9246 // V == Min). We know from the guarding condition that !(V
9247 // == Min). This gives us
9248 //
9249 // V `Pred` Min || V == Min && !(V == Min)
9250 // => V `Pred` Min
9251 //
9252 // If V `Pred` Min implies LHS `Pred` RHS, we're done.
9253
9254 if (isImpliedCondOperands(Pred, LHS, RHS, V, getConstant(Min)))
9255 return true;
Galina Kistanova8514dd52017-05-31 22:09:46 +00009256 LLVM_FALLTHROUGH;
Sanjoy Dasc5676df2014-11-13 00:00:58 +00009257
9258 default:
9259 // No change
9260 break;
9261 }
9262 }
9263 }
9264
Dan Gohman430f0cc2009-07-21 23:03:19 +00009265 // Check whether the actual condition is beyond sufficient.
9266 if (FoundPred == ICmpInst::ICMP_EQ)
9267 if (ICmpInst::isTrueWhenEqual(Pred))
9268 if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS))
9269 return true;
9270 if (Pred == ICmpInst::ICMP_NE)
9271 if (!ICmpInst::isTrueWhenEqual(FoundPred))
9272 if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS))
9273 return true;
9274
9275 // Otherwise assume the worst.
9276 return false;
Dan Gohmane65c9172009-07-13 21:35:55 +00009277}
9278
Sanjoy Das1ed69102015-10-13 02:53:27 +00009279bool ScalarEvolution::splitBinaryAdd(const SCEV *Expr,
9280 const SCEV *&L, const SCEV *&R,
9281 SCEV::NoWrapFlags &Flags) {
9282 const auto *AE = dyn_cast<SCEVAddExpr>(Expr);
9283 if (!AE || AE->getNumOperands() != 2)
9284 return false;
9285
9286 L = AE->getOperand(0);
9287 R = AE->getOperand(1);
9288 Flags = AE->getNoWrapFlags();
9289 return true;
9290}
9291
Sanjoy Das0b1af852016-07-23 00:28:56 +00009292Optional<APInt> ScalarEvolution::computeConstantDifference(const SCEV *More,
9293 const SCEV *Less) {
Sanjoy Das96709c42015-09-25 23:53:45 +00009294 // We avoid subtracting expressions here because this function is usually
9295 // fairly deep in the call stack (i.e. is called many times).
9296
Sanjoy Das96709c42015-09-25 23:53:45 +00009297 if (isa<SCEVAddRecExpr>(Less) && isa<SCEVAddRecExpr>(More)) {
9298 const auto *LAR = cast<SCEVAddRecExpr>(Less);
9299 const auto *MAR = cast<SCEVAddRecExpr>(More);
9300
9301 if (LAR->getLoop() != MAR->getLoop())
Sanjoy Das0b1af852016-07-23 00:28:56 +00009302 return None;
Sanjoy Das96709c42015-09-25 23:53:45 +00009303
9304 // We look at affine expressions only; not for correctness but to keep
9305 // getStepRecurrence cheap.
9306 if (!LAR->isAffine() || !MAR->isAffine())
Sanjoy Das0b1af852016-07-23 00:28:56 +00009307 return None;
Sanjoy Das96709c42015-09-25 23:53:45 +00009308
Sanjoy Das1ed69102015-10-13 02:53:27 +00009309 if (LAR->getStepRecurrence(*this) != MAR->getStepRecurrence(*this))
Sanjoy Das0b1af852016-07-23 00:28:56 +00009310 return None;
Sanjoy Das96709c42015-09-25 23:53:45 +00009311
9312 Less = LAR->getStart();
9313 More = MAR->getStart();
9314
9315 // fall through
9316 }
9317
9318 if (isa<SCEVConstant>(Less) && isa<SCEVConstant>(More)) {
Sanjoy Das0de2fec2015-12-17 20:28:46 +00009319 const auto &M = cast<SCEVConstant>(More)->getAPInt();
9320 const auto &L = cast<SCEVConstant>(Less)->getAPInt();
Sanjoy Das0b1af852016-07-23 00:28:56 +00009321 return M - L;
Sanjoy Das96709c42015-09-25 23:53:45 +00009322 }
9323
9324 const SCEV *L, *R;
Sanjoy Das1ed69102015-10-13 02:53:27 +00009325 SCEV::NoWrapFlags Flags;
9326 if (splitBinaryAdd(Less, L, R, Flags))
Sanjoy Das96709c42015-09-25 23:53:45 +00009327 if (const auto *LC = dyn_cast<SCEVConstant>(L))
Sanjoy Das0b1af852016-07-23 00:28:56 +00009328 if (R == More)
9329 return -(LC->getAPInt());
Sanjoy Das96709c42015-09-25 23:53:45 +00009330
Sanjoy Das1ed69102015-10-13 02:53:27 +00009331 if (splitBinaryAdd(More, L, R, Flags))
Sanjoy Das96709c42015-09-25 23:53:45 +00009332 if (const auto *LC = dyn_cast<SCEVConstant>(L))
Sanjoy Das0b1af852016-07-23 00:28:56 +00009333 if (R == Less)
9334 return LC->getAPInt();
Sanjoy Das96709c42015-09-25 23:53:45 +00009335
Sanjoy Das0b1af852016-07-23 00:28:56 +00009336 return None;
Sanjoy Das96709c42015-09-25 23:53:45 +00009337}
9338
9339bool ScalarEvolution::isImpliedCondOperandsViaNoOverflow(
9340 ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS,
9341 const SCEV *FoundLHS, const SCEV *FoundRHS) {
9342 if (Pred != CmpInst::ICMP_SLT && Pred != CmpInst::ICMP_ULT)
9343 return false;
9344
9345 const auto *AddRecLHS = dyn_cast<SCEVAddRecExpr>(LHS);
9346 if (!AddRecLHS)
9347 return false;
9348
9349 const auto *AddRecFoundLHS = dyn_cast<SCEVAddRecExpr>(FoundLHS);
9350 if (!AddRecFoundLHS)
9351 return false;
9352
9353 // We'd like to let SCEV reason about control dependencies, so we constrain
9354 // both the inequalities to be about add recurrences on the same loop. This
9355 // way we can use isLoopEntryGuardedByCond later.
9356
9357 const Loop *L = AddRecFoundLHS->getLoop();
9358 if (L != AddRecLHS->getLoop())
9359 return false;
9360
9361 // FoundLHS u< FoundRHS u< -C => (FoundLHS + C) u< (FoundRHS + C) ... (1)
9362 //
9363 // FoundLHS s< FoundRHS s< INT_MIN - C => (FoundLHS + C) s< (FoundRHS + C)
9364 // ... (2)
9365 //
9366 // Informal proof for (2), assuming (1) [*]:
9367 //
9368 // We'll also assume (A s< B) <=> ((A + INT_MIN) u< (B + INT_MIN)) ... (3)[**]
9369 //
9370 // Then
9371 //
9372 // FoundLHS s< FoundRHS s< INT_MIN - C
9373 // <=> (FoundLHS + INT_MIN) u< (FoundRHS + INT_MIN) u< -C [ using (3) ]
9374 // <=> (FoundLHS + INT_MIN + C) u< (FoundRHS + INT_MIN + C) [ using (1) ]
9375 // <=> (FoundLHS + INT_MIN + C + INT_MIN) s<
9376 // (FoundRHS + INT_MIN + C + INT_MIN) [ using (3) ]
9377 // <=> FoundLHS + C s< FoundRHS + C
9378 //
9379 // [*]: (1) can be proved by ruling out overflow.
9380 //
9381 // [**]: This can be proved by analyzing all the four possibilities:
9382 // (A s< 0, B s< 0), (A s< 0, B s>= 0), (A s>= 0, B s< 0) and
9383 // (A s>= 0, B s>= 0).
9384 //
9385 // Note:
9386 // Despite (2), "FoundRHS s< INT_MIN - C" does not mean that "FoundRHS + C"
9387 // will not sign underflow. For instance, say FoundLHS = (i8 -128), FoundRHS
9388 // = (i8 -127) and C = (i8 -100). Then INT_MIN - C = (i8 -28), and FoundRHS
9389 // s< (INT_MIN - C). Lack of sign overflow / underflow in "FoundRHS + C" is
9390 // neither necessary nor sufficient to prove "(FoundLHS + C) s< (FoundRHS +
9391 // C)".
9392
Sanjoy Das0b1af852016-07-23 00:28:56 +00009393 Optional<APInt> LDiff = computeConstantDifference(LHS, FoundLHS);
9394 Optional<APInt> RDiff = computeConstantDifference(RHS, FoundRHS);
9395 if (!LDiff || !RDiff || *LDiff != *RDiff)
Sanjoy Das96709c42015-09-25 23:53:45 +00009396 return false;
9397
Sanjoy Das0b1af852016-07-23 00:28:56 +00009398 if (LDiff->isMinValue())
Sanjoy Das96709c42015-09-25 23:53:45 +00009399 return true;
9400
Sanjoy Das96709c42015-09-25 23:53:45 +00009401 APInt FoundRHSLimit;
9402
9403 if (Pred == CmpInst::ICMP_ULT) {
Sanjoy Das0b1af852016-07-23 00:28:56 +00009404 FoundRHSLimit = -(*RDiff);
Sanjoy Das96709c42015-09-25 23:53:45 +00009405 } else {
9406 assert(Pred == CmpInst::ICMP_SLT && "Checked above!");
Sanjoy Das0b1af852016-07-23 00:28:56 +00009407 FoundRHSLimit = APInt::getSignedMinValue(getTypeSizeInBits(RHS->getType())) - *RDiff;
Sanjoy Das96709c42015-09-25 23:53:45 +00009408 }
9409
9410 // Try to prove (1) or (2), as needed.
9411 return isLoopEntryGuardedByCond(L, Pred, FoundRHS,
9412 getConstant(FoundRHSLimit));
9413}
9414
Dan Gohman430f0cc2009-07-21 23:03:19 +00009415bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred,
9416 const SCEV *LHS, const SCEV *RHS,
9417 const SCEV *FoundLHS,
9418 const SCEV *FoundRHS) {
Sanjoy Dascb8bca12015-03-18 00:41:29 +00009419 if (isImpliedCondOperandsViaRanges(Pred, LHS, RHS, FoundLHS, FoundRHS))
9420 return true;
9421
Sanjoy Das96709c42015-09-25 23:53:45 +00009422 if (isImpliedCondOperandsViaNoOverflow(Pred, LHS, RHS, FoundLHS, FoundRHS))
9423 return true;
9424
Dan Gohman430f0cc2009-07-21 23:03:19 +00009425 return isImpliedCondOperandsHelper(Pred, LHS, RHS,
9426 FoundLHS, FoundRHS) ||
9427 // ~x < ~y --> x > y
9428 isImpliedCondOperandsHelper(Pred, LHS, RHS,
9429 getNotSCEV(FoundRHS),
9430 getNotSCEV(FoundLHS));
9431}
9432
Sanjoy Das4555b6d2014-12-15 22:50:15 +00009433/// If Expr computes ~A, return A else return nullptr
9434static const SCEV *MatchNotExpr(const SCEV *Expr) {
9435 const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Expr);
Sanjoy Das16e7ff12015-10-13 23:28:31 +00009436 if (!Add || Add->getNumOperands() != 2 ||
9437 !Add->getOperand(0)->isAllOnesValue())
Sanjoy Das4555b6d2014-12-15 22:50:15 +00009438 return nullptr;
9439
9440 const SCEVMulExpr *AddRHS = dyn_cast<SCEVMulExpr>(Add->getOperand(1));
Sanjoy Das16e7ff12015-10-13 23:28:31 +00009441 if (!AddRHS || AddRHS->getNumOperands() != 2 ||
9442 !AddRHS->getOperand(0)->isAllOnesValue())
Sanjoy Das4555b6d2014-12-15 22:50:15 +00009443 return nullptr;
9444
9445 return AddRHS->getOperand(1);
9446}
9447
Sanjoy Das4555b6d2014-12-15 22:50:15 +00009448/// Is MaybeMaxExpr an SMax or UMax of Candidate and some other values?
9449template<typename MaxExprType>
9450static bool IsMaxConsistingOf(const SCEV *MaybeMaxExpr,
9451 const SCEV *Candidate) {
9452 const MaxExprType *MaxExpr = dyn_cast<MaxExprType>(MaybeMaxExpr);
9453 if (!MaxExpr) return false;
9454
Sanjoy Das347d2722015-12-01 07:49:27 +00009455 return find(MaxExpr->operands(), Candidate) != MaxExpr->op_end();
Sanjoy Das4555b6d2014-12-15 22:50:15 +00009456}
9457
Sanjoy Das4555b6d2014-12-15 22:50:15 +00009458/// Is MaybeMinExpr an SMin or UMin of Candidate and some other values?
9459template<typename MaxExprType>
9460static bool IsMinConsistingOf(ScalarEvolution &SE,
9461 const SCEV *MaybeMinExpr,
9462 const SCEV *Candidate) {
9463 const SCEV *MaybeMaxExpr = MatchNotExpr(MaybeMinExpr);
9464 if (!MaybeMaxExpr)
9465 return false;
9466
9467 return IsMaxConsistingOf<MaxExprType>(MaybeMaxExpr, SE.getNotSCEV(Candidate));
9468}
9469
Hal Finkela8d205f2015-08-19 01:51:51 +00009470static bool IsKnownPredicateViaAddRecStart(ScalarEvolution &SE,
9471 ICmpInst::Predicate Pred,
9472 const SCEV *LHS, const SCEV *RHS) {
Hal Finkela8d205f2015-08-19 01:51:51 +00009473 // If both sides are affine addrecs for the same loop, with equal
9474 // steps, and we know the recurrences don't wrap, then we only
9475 // need to check the predicate on the starting values.
9476
9477 if (!ICmpInst::isRelational(Pred))
9478 return false;
9479
9480 const SCEVAddRecExpr *LAR = dyn_cast<SCEVAddRecExpr>(LHS);
9481 if (!LAR)
9482 return false;
9483 const SCEVAddRecExpr *RAR = dyn_cast<SCEVAddRecExpr>(RHS);
9484 if (!RAR)
9485 return false;
9486 if (LAR->getLoop() != RAR->getLoop())
9487 return false;
9488 if (!LAR->isAffine() || !RAR->isAffine())
9489 return false;
9490
9491 if (LAR->getStepRecurrence(SE) != RAR->getStepRecurrence(SE))
9492 return false;
9493
Hal Finkelff08a2e2015-08-19 17:26:07 +00009494 SCEV::NoWrapFlags NW = ICmpInst::isSigned(Pred) ?
9495 SCEV::FlagNSW : SCEV::FlagNUW;
9496 if (!LAR->getNoWrapFlags(NW) || !RAR->getNoWrapFlags(NW))
Hal Finkela8d205f2015-08-19 01:51:51 +00009497 return false;
9498
9499 return SE.isKnownPredicate(Pred, LAR->getStart(), RAR->getStart());
9500}
Sanjoy Das4555b6d2014-12-15 22:50:15 +00009501
9502/// Is LHS `Pred` RHS true on the virtue of LHS or RHS being a Min or Max
9503/// expression?
9504static bool IsKnownPredicateViaMinOrMax(ScalarEvolution &SE,
9505 ICmpInst::Predicate Pred,
9506 const SCEV *LHS, const SCEV *RHS) {
9507 switch (Pred) {
9508 default:
9509 return false;
9510
9511 case ICmpInst::ICMP_SGE:
9512 std::swap(LHS, RHS);
Justin Bognercd1d5aa2016-08-17 20:30:52 +00009513 LLVM_FALLTHROUGH;
Sanjoy Das4555b6d2014-12-15 22:50:15 +00009514 case ICmpInst::ICMP_SLE:
9515 return
9516 // min(A, ...) <= A
9517 IsMinConsistingOf<SCEVSMaxExpr>(SE, LHS, RHS) ||
9518 // A <= max(A, ...)
9519 IsMaxConsistingOf<SCEVSMaxExpr>(RHS, LHS);
9520
9521 case ICmpInst::ICMP_UGE:
9522 std::swap(LHS, RHS);
Justin Bognercd1d5aa2016-08-17 20:30:52 +00009523 LLVM_FALLTHROUGH;
Sanjoy Das4555b6d2014-12-15 22:50:15 +00009524 case ICmpInst::ICMP_ULE:
9525 return
9526 // min(A, ...) <= A
9527 IsMinConsistingOf<SCEVUMaxExpr>(SE, LHS, RHS) ||
9528 // A <= max(A, ...)
9529 IsMaxConsistingOf<SCEVUMaxExpr>(RHS, LHS);
9530 }
9531
9532 llvm_unreachable("covered switch fell through?!");
9533}
9534
Max Kazantsev2e44d292017-03-31 12:05:30 +00009535bool ScalarEvolution::isImpliedViaOperations(ICmpInst::Predicate Pred,
9536 const SCEV *LHS, const SCEV *RHS,
9537 const SCEV *FoundLHS,
9538 const SCEV *FoundRHS,
9539 unsigned Depth) {
9540 assert(getTypeSizeInBits(LHS->getType()) ==
9541 getTypeSizeInBits(RHS->getType()) &&
9542 "LHS and RHS have different sizes?");
9543 assert(getTypeSizeInBits(FoundLHS->getType()) ==
9544 getTypeSizeInBits(FoundRHS->getType()) &&
9545 "FoundLHS and FoundRHS have different sizes?");
9546 // We want to avoid hurting the compile time with analysis of too big trees.
9547 if (Depth > MaxSCEVOperationsImplicationDepth)
9548 return false;
9549 // We only want to work with ICMP_SGT comparison so far.
9550 // TODO: Extend to ICMP_UGT?
9551 if (Pred == ICmpInst::ICMP_SLT) {
9552 Pred = ICmpInst::ICMP_SGT;
9553 std::swap(LHS, RHS);
9554 std::swap(FoundLHS, FoundRHS);
9555 }
9556 if (Pred != ICmpInst::ICMP_SGT)
9557 return false;
9558
9559 auto GetOpFromSExt = [&](const SCEV *S) {
9560 if (auto *Ext = dyn_cast<SCEVSignExtendExpr>(S))
9561 return Ext->getOperand();
9562 // TODO: If S is a SCEVConstant then you can cheaply "strip" the sext off
9563 // the constant in some cases.
9564 return S;
9565 };
9566
9567 // Acquire values from extensions.
9568 auto *OrigFoundLHS = FoundLHS;
9569 LHS = GetOpFromSExt(LHS);
9570 FoundLHS = GetOpFromSExt(FoundLHS);
9571
9572 // Is the SGT predicate can be proved trivially or using the found context.
9573 auto IsSGTViaContext = [&](const SCEV *S1, const SCEV *S2) {
9574 return isKnownViaSimpleReasoning(ICmpInst::ICMP_SGT, S1, S2) ||
9575 isImpliedViaOperations(ICmpInst::ICMP_SGT, S1, S2, OrigFoundLHS,
9576 FoundRHS, Depth + 1);
9577 };
9578
9579 if (auto *LHSAddExpr = dyn_cast<SCEVAddExpr>(LHS)) {
9580 // We want to avoid creation of any new non-constant SCEV. Since we are
9581 // going to compare the operands to RHS, we should be certain that we don't
9582 // need any size extensions for this. So let's decline all cases when the
9583 // sizes of types of LHS and RHS do not match.
9584 // TODO: Maybe try to get RHS from sext to catch more cases?
9585 if (getTypeSizeInBits(LHS->getType()) != getTypeSizeInBits(RHS->getType()))
9586 return false;
9587
9588 // Should not overflow.
9589 if (!LHSAddExpr->hasNoSignedWrap())
9590 return false;
9591
9592 auto *LL = LHSAddExpr->getOperand(0);
9593 auto *LR = LHSAddExpr->getOperand(1);
9594 auto *MinusOne = getNegativeSCEV(getOne(RHS->getType()));
9595
9596 // Checks that S1 >= 0 && S2 > RHS, trivially or using the found context.
9597 auto IsSumGreaterThanRHS = [&](const SCEV *S1, const SCEV *S2) {
9598 return IsSGTViaContext(S1, MinusOne) && IsSGTViaContext(S2, RHS);
9599 };
9600 // Try to prove the following rule:
9601 // (LHS = LL + LR) && (LL >= 0) && (LR > RHS) => (LHS > RHS).
9602 // (LHS = LL + LR) && (LR >= 0) && (LL > RHS) => (LHS > RHS).
9603 if (IsSumGreaterThanRHS(LL, LR) || IsSumGreaterThanRHS(LR, LL))
9604 return true;
9605 } else if (auto *LHSUnknownExpr = dyn_cast<SCEVUnknown>(LHS)) {
9606 Value *LL, *LR;
9607 // FIXME: Once we have SDiv implemented, we can get rid of this matching.
Eugene Zelenkobe709f22017-08-18 23:51:26 +00009608
Max Kazantsev2e44d292017-03-31 12:05:30 +00009609 using namespace llvm::PatternMatch;
Eugene Zelenkobe709f22017-08-18 23:51:26 +00009610
Max Kazantsev2e44d292017-03-31 12:05:30 +00009611 if (match(LHSUnknownExpr->getValue(), m_SDiv(m_Value(LL), m_Value(LR)))) {
9612 // Rules for division.
9613 // We are going to perform some comparisons with Denominator and its
9614 // derivative expressions. In general case, creating a SCEV for it may
9615 // lead to a complex analysis of the entire graph, and in particular it
9616 // can request trip count recalculation for the same loop. This would
9617 // cache as SCEVCouldNotCompute to avoid the infinite recursion. To avoid
9618 // this, we only want to create SCEVs that are constants in this section.
9619 // So we bail if Denominator is not a constant.
9620 if (!isa<ConstantInt>(LR))
9621 return false;
9622
9623 auto *Denominator = cast<SCEVConstant>(getSCEV(LR));
9624
9625 // We want to make sure that LHS = FoundLHS / Denominator. If it is so,
9626 // then a SCEV for the numerator already exists and matches with FoundLHS.
9627 auto *Numerator = getExistingSCEV(LL);
9628 if (!Numerator || Numerator->getType() != FoundLHS->getType())
9629 return false;
9630
9631 // Make sure that the numerator matches with FoundLHS and the denominator
9632 // is positive.
9633 if (!HasSameValue(Numerator, FoundLHS) || !isKnownPositive(Denominator))
9634 return false;
9635
9636 auto *DTy = Denominator->getType();
9637 auto *FRHSTy = FoundRHS->getType();
9638 if (DTy->isPointerTy() != FRHSTy->isPointerTy())
9639 // One of types is a pointer and another one is not. We cannot extend
9640 // them properly to a wider type, so let us just reject this case.
9641 // TODO: Usage of getEffectiveSCEVType for DTy, FRHSTy etc should help
9642 // to avoid this check.
9643 return false;
9644
9645 // Given that:
9646 // FoundLHS > FoundRHS, LHS = FoundLHS / Denominator, Denominator > 0.
9647 auto *WTy = getWiderType(DTy, FRHSTy);
9648 auto *DenominatorExt = getNoopOrSignExtend(Denominator, WTy);
9649 auto *FoundRHSExt = getNoopOrSignExtend(FoundRHS, WTy);
9650
9651 // Try to prove the following rule:
9652 // (FoundRHS > Denominator - 2) && (RHS <= 0) => (LHS > RHS).
9653 // For example, given that FoundLHS > 2. It means that FoundLHS is at
9654 // least 3. If we divide it by Denominator < 4, we will have at least 1.
9655 auto *DenomMinusTwo = getMinusSCEV(DenominatorExt, getConstant(WTy, 2));
9656 if (isKnownNonPositive(RHS) &&
9657 IsSGTViaContext(FoundRHSExt, DenomMinusTwo))
9658 return true;
9659
9660 // Try to prove the following rule:
9661 // (FoundRHS > -1 - Denominator) && (RHS < 0) => (LHS > RHS).
9662 // For example, given that FoundLHS > -3. Then FoundLHS is at least -2.
9663 // If we divide it by Denominator > 2, then:
9664 // 1. If FoundLHS is negative, then the result is 0.
9665 // 2. If FoundLHS is non-negative, then the result is non-negative.
9666 // Anyways, the result is non-negative.
9667 auto *MinusOne = getNegativeSCEV(getOne(WTy));
9668 auto *NegDenomMinusOne = getMinusSCEV(MinusOne, DenominatorExt);
9669 if (isKnownNegative(RHS) &&
9670 IsSGTViaContext(FoundRHSExt, NegDenomMinusOne))
9671 return true;
9672 }
9673 }
9674
9675 return false;
9676}
9677
9678bool
9679ScalarEvolution::isKnownViaSimpleReasoning(ICmpInst::Predicate Pred,
9680 const SCEV *LHS, const SCEV *RHS) {
9681 return isKnownPredicateViaConstantRanges(Pred, LHS, RHS) ||
9682 IsKnownPredicateViaMinOrMax(*this, Pred, LHS, RHS) ||
9683 IsKnownPredicateViaAddRecStart(*this, Pred, LHS, RHS) ||
9684 isKnownPredicateViaNoOverflow(Pred, LHS, RHS);
9685}
9686
Dan Gohmane65c9172009-07-13 21:35:55 +00009687bool
Dan Gohman430f0cc2009-07-21 23:03:19 +00009688ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
9689 const SCEV *LHS, const SCEV *RHS,
9690 const SCEV *FoundLHS,
9691 const SCEV *FoundRHS) {
Dan Gohmane65c9172009-07-13 21:35:55 +00009692 switch (Pred) {
Dan Gohman8c129d72009-07-16 17:34:36 +00009693 default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
9694 case ICmpInst::ICMP_EQ:
9695 case ICmpInst::ICMP_NE:
9696 if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS))
9697 return true;
9698 break;
Dan Gohmane65c9172009-07-13 21:35:55 +00009699 case ICmpInst::ICMP_SLT:
Dan Gohman8c129d72009-07-16 17:34:36 +00009700 case ICmpInst::ICMP_SLE:
Max Kazantsev2e44d292017-03-31 12:05:30 +00009701 if (isKnownViaSimpleReasoning(ICmpInst::ICMP_SLE, LHS, FoundLHS) &&
9702 isKnownViaSimpleReasoning(ICmpInst::ICMP_SGE, RHS, FoundRHS))
Dan Gohmane65c9172009-07-13 21:35:55 +00009703 return true;
9704 break;
9705 case ICmpInst::ICMP_SGT:
Dan Gohman8c129d72009-07-16 17:34:36 +00009706 case ICmpInst::ICMP_SGE:
Max Kazantsev2e44d292017-03-31 12:05:30 +00009707 if (isKnownViaSimpleReasoning(ICmpInst::ICMP_SGE, LHS, FoundLHS) &&
9708 isKnownViaSimpleReasoning(ICmpInst::ICMP_SLE, RHS, FoundRHS))
Dan Gohmane65c9172009-07-13 21:35:55 +00009709 return true;
9710 break;
9711 case ICmpInst::ICMP_ULT:
Dan Gohman8c129d72009-07-16 17:34:36 +00009712 case ICmpInst::ICMP_ULE:
Max Kazantsev2e44d292017-03-31 12:05:30 +00009713 if (isKnownViaSimpleReasoning(ICmpInst::ICMP_ULE, LHS, FoundLHS) &&
9714 isKnownViaSimpleReasoning(ICmpInst::ICMP_UGE, RHS, FoundRHS))
Dan Gohmane65c9172009-07-13 21:35:55 +00009715 return true;
9716 break;
9717 case ICmpInst::ICMP_UGT:
Dan Gohman8c129d72009-07-16 17:34:36 +00009718 case ICmpInst::ICMP_UGE:
Max Kazantsev2e44d292017-03-31 12:05:30 +00009719 if (isKnownViaSimpleReasoning(ICmpInst::ICMP_UGE, LHS, FoundLHS) &&
9720 isKnownViaSimpleReasoning(ICmpInst::ICMP_ULE, RHS, FoundRHS))
Dan Gohmane65c9172009-07-13 21:35:55 +00009721 return true;
9722 break;
9723 }
9724
Max Kazantsev2e44d292017-03-31 12:05:30 +00009725 // Maybe it can be proved via operations?
9726 if (isImpliedViaOperations(Pred, LHS, RHS, FoundLHS, FoundRHS))
9727 return true;
9728
Dan Gohmane65c9172009-07-13 21:35:55 +00009729 return false;
Dan Gohmanf19aeec2009-06-24 01:18:18 +00009730}
9731
Sanjoy Dascb8bca12015-03-18 00:41:29 +00009732bool ScalarEvolution::isImpliedCondOperandsViaRanges(ICmpInst::Predicate Pred,
9733 const SCEV *LHS,
9734 const SCEV *RHS,
9735 const SCEV *FoundLHS,
9736 const SCEV *FoundRHS) {
9737 if (!isa<SCEVConstant>(RHS) || !isa<SCEVConstant>(FoundRHS))
9738 // The restriction on `FoundRHS` be lifted easily -- it exists only to
9739 // reduce the compile time impact of this optimization.
9740 return false;
9741
Sanjoy Dasa7d9ec82016-07-23 00:54:36 +00009742 Optional<APInt> Addend = computeConstantDifference(LHS, FoundLHS);
Sanjoy Das095f5b22016-07-22 20:47:55 +00009743 if (!Addend)
Sanjoy Dascb8bca12015-03-18 00:41:29 +00009744 return false;
9745
Craig Topper8f26b792017-05-06 05:15:09 +00009746 const APInt &ConstFoundRHS = cast<SCEVConstant>(FoundRHS)->getAPInt();
Sanjoy Dascb8bca12015-03-18 00:41:29 +00009747
9748 // `FoundLHSRange` is the range we know `FoundLHS` to be in by virtue of the
9749 // antecedent "`FoundLHS` `Pred` `FoundRHS`".
9750 ConstantRange FoundLHSRange =
9751 ConstantRange::makeAllowedICmpRegion(Pred, ConstFoundRHS);
9752
Sanjoy Das095f5b22016-07-22 20:47:55 +00009753 // Since `LHS` is `FoundLHS` + `Addend`, we can compute a range for `LHS`:
9754 ConstantRange LHSRange = FoundLHSRange.add(ConstantRange(*Addend));
Sanjoy Dascb8bca12015-03-18 00:41:29 +00009755
9756 // We can also compute the range of values for `LHS` that satisfy the
9757 // consequent, "`LHS` `Pred` `RHS`":
Craig Topper8f26b792017-05-06 05:15:09 +00009758 const APInt &ConstRHS = cast<SCEVConstant>(RHS)->getAPInt();
Sanjoy Dascb8bca12015-03-18 00:41:29 +00009759 ConstantRange SatisfyingLHSRange =
9760 ConstantRange::makeSatisfyingICmpRegion(Pred, ConstRHS);
9761
9762 // The antecedent implies the consequent if every value of `LHS` that
9763 // satisfies the antecedent also satisfies the consequent.
9764 return SatisfyingLHSRange.contains(LHSRange);
9765}
9766
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009767bool ScalarEvolution::doesIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride,
9768 bool IsSigned, bool NoWrap) {
David L Kreitzer8bbabee2016-09-16 14:38:13 +00009769 assert(isKnownPositive(Stride) && "Positive stride expected!");
9770
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009771 if (NoWrap) return false;
Dan Gohman51aaf022010-01-26 04:40:18 +00009772
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009773 unsigned BitWidth = getTypeSizeInBits(RHS->getType());
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00009774 const SCEV *One = getOne(Stride->getType());
Andrew Trick2afa3252011-03-09 17:29:58 +00009775
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009776 if (IsSigned) {
Craig Topper01020392017-06-24 23:34:50 +00009777 APInt MaxRHS = getSignedRangeMax(RHS);
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009778 APInt MaxValue = APInt::getSignedMaxValue(BitWidth);
Craig Topper01020392017-06-24 23:34:50 +00009779 APInt MaxStrideMinusOne = getSignedRangeMax(getMinusSCEV(Stride, One));
Andrew Trick2afa3252011-03-09 17:29:58 +00009780
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009781 // SMaxRHS + SMaxStrideMinusOne > SMaxValue => overflow!
Craig Topperef869ec2017-05-08 17:39:01 +00009782 return (std::move(MaxValue) - MaxStrideMinusOne).slt(MaxRHS);
Dan Gohman36bad002009-09-17 18:05:20 +00009783 }
Dan Gohman01048422009-06-21 23:46:38 +00009784
Craig Topper01020392017-06-24 23:34:50 +00009785 APInt MaxRHS = getUnsignedRangeMax(RHS);
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009786 APInt MaxValue = APInt::getMaxValue(BitWidth);
Craig Topper01020392017-06-24 23:34:50 +00009787 APInt MaxStrideMinusOne = getUnsignedRangeMax(getMinusSCEV(Stride, One));
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009788
9789 // UMaxRHS + UMaxStrideMinusOne > UMaxValue => overflow!
Craig Topperef869ec2017-05-08 17:39:01 +00009790 return (std::move(MaxValue) - MaxStrideMinusOne).ult(MaxRHS);
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009791}
9792
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009793bool ScalarEvolution::doesIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride,
9794 bool IsSigned, bool NoWrap) {
9795 if (NoWrap) return false;
9796
9797 unsigned BitWidth = getTypeSizeInBits(RHS->getType());
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00009798 const SCEV *One = getOne(Stride->getType());
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009799
9800 if (IsSigned) {
Craig Topper01020392017-06-24 23:34:50 +00009801 APInt MinRHS = getSignedRangeMin(RHS);
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009802 APInt MinValue = APInt::getSignedMinValue(BitWidth);
Craig Topper01020392017-06-24 23:34:50 +00009803 APInt MaxStrideMinusOne = getSignedRangeMax(getMinusSCEV(Stride, One));
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009804
9805 // SMinRHS - SMaxStrideMinusOne < SMinValue => overflow!
Craig Topperef869ec2017-05-08 17:39:01 +00009806 return (std::move(MinValue) + MaxStrideMinusOne).sgt(MinRHS);
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009807 }
9808
Craig Topper01020392017-06-24 23:34:50 +00009809 APInt MinRHS = getUnsignedRangeMin(RHS);
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009810 APInt MinValue = APInt::getMinValue(BitWidth);
Craig Topper01020392017-06-24 23:34:50 +00009811 APInt MaxStrideMinusOne = getUnsignedRangeMax(getMinusSCEV(Stride, One));
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009812
9813 // UMinRHS - UMaxStrideMinusOne < UMinValue => overflow!
Craig Topperef869ec2017-05-08 17:39:01 +00009814 return (std::move(MinValue) + MaxStrideMinusOne).ugt(MinRHS);
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009815}
9816
Johannes Doerfert2683e562015-02-09 12:34:23 +00009817const SCEV *ScalarEvolution::computeBECount(const SCEV *Delta, const SCEV *Step,
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009818 bool Equality) {
Sanjoy Das2aacc0e2015-09-23 01:59:04 +00009819 const SCEV *One = getOne(Step->getType());
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009820 Delta = Equality ? getAddExpr(Delta, Step)
9821 : getAddExpr(Delta, getMinusSCEV(Step, One));
9822 return getUDivExpr(Delta, Step);
Dan Gohman01048422009-06-21 23:46:38 +00009823}
9824
Anna Thomas79503c02017-10-16 17:47:17 +00009825const SCEV *ScalarEvolution::computeMaxBECountForLT(const SCEV *Start,
9826 const SCEV *Stride,
9827 const SCEV *End,
9828 unsigned BitWidth,
9829 bool IsSigned) {
Anna Thomasa2ca9022017-10-13 14:30:43 +00009830
9831 assert(!isKnownNonPositive(Stride) &&
9832 "Stride is expected strictly positive!");
9833 // Calculate the maximum backedge count based on the range of values
9834 // permitted by Start, End, and Stride.
9835 const SCEV *MaxBECount;
9836 APInt MinStart =
9837 IsSigned ? getSignedRangeMin(Start) : getUnsignedRangeMin(Start);
9838
Sanjoy Das8499ebf2017-10-25 21:41:00 +00009839 APInt StrideForMaxBECount =
9840 IsSigned ? getSignedRangeMin(Stride) : getUnsignedRangeMin(Stride);
Anna Thomasa2ca9022017-10-13 14:30:43 +00009841
Sanjoy Das8499ebf2017-10-25 21:41:00 +00009842 // We already know that the stride is positive, so we paper over conservatism
9843 // in our range computation by forcing StrideForMaxBECount to be at least one.
9844 // In theory this is unnecessary, but we expect MaxBECount to be a
9845 // SCEVConstant, and (udiv <constant> 0) is not constant folded by SCEV (there
9846 // is nothing to constant fold it to).
9847 APInt One(BitWidth, 1, IsSigned);
9848 StrideForMaxBECount = APIntOps::smax(One, StrideForMaxBECount);
Anna Thomasa2ca9022017-10-13 14:30:43 +00009849
9850 APInt MaxValue = IsSigned ? APInt::getSignedMaxValue(BitWidth)
9851 : APInt::getMaxValue(BitWidth);
9852 APInt Limit = MaxValue - (StrideForMaxBECount - 1);
9853
9854 // Although End can be a MAX expression we estimate MaxEnd considering only
9855 // the case End = RHS of the loop termination condition. This is safe because
9856 // in the other case (End - Start) is zero, leading to a zero maximum backedge
9857 // taken count.
9858 APInt MaxEnd = IsSigned ? APIntOps::smin(getSignedRangeMax(End), Limit)
9859 : APIntOps::umin(getUnsignedRangeMax(End), Limit);
9860
9861 MaxBECount = computeBECount(getConstant(MaxEnd - MinStart) /* Delta */,
9862 getConstant(StrideForMaxBECount) /* Step */,
9863 false /* Equality */);
9864
9865 return MaxBECount;
9866}
9867
Andrew Trick3ca3f982011-07-26 17:19:55 +00009868ScalarEvolution::ExitLimit
Sanjoy Das108fcf22016-05-29 00:38:00 +00009869ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009870 const Loop *L, bool IsSigned,
Silviu Baranga6f444df2016-04-08 14:29:09 +00009871 bool ControlsExit, bool AllowPredicates) {
Sanjoy Dasf0022122016-09-28 17:14:58 +00009872 SmallPtrSet<const SCEVPredicate *, 4> Predicates;
Chris Lattner587a75b2005-08-15 23:33:51 +00009873
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009874 const SCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(LHS);
David L Kreitzer8bbabee2016-09-16 14:38:13 +00009875 bool PredicatedIV = false;
9876
9877 if (!IV && AllowPredicates) {
Silviu Baranga6f444df2016-04-08 14:29:09 +00009878 // Try to make this an AddRec using runtime tests, in the first X
9879 // iterations of this loop, where X is the SCEV expression found by the
9880 // algorithm below.
Sanjoy Dasf0022122016-09-28 17:14:58 +00009881 IV = convertSCEVToAddRecWithPredicates(LHS, L, Predicates);
David L Kreitzer8bbabee2016-09-16 14:38:13 +00009882 PredicatedIV = true;
9883 }
Dan Gohman2b8da352009-04-30 20:47:05 +00009884
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009885 // Avoid weird loops
9886 if (!IV || IV->getLoop() != L || !IV->isAffine())
9887 return getCouldNotCompute();
Chris Lattner587a75b2005-08-15 23:33:51 +00009888
Mark Heffernan2beab5f2014-10-10 17:39:11 +00009889 bool NoWrap = ControlsExit &&
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009890 IV->getNoWrapFlags(IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW);
Wojciech Matyjewicz35545fd2008-02-13 11:51:34 +00009891
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009892 const SCEV *Stride = IV->getStepRecurrence(*this);
Wojciech Matyjewicz35545fd2008-02-13 11:51:34 +00009893
David L Kreitzer8bbabee2016-09-16 14:38:13 +00009894 bool PositiveStride = isKnownPositive(Stride);
Dan Gohman2b8da352009-04-30 20:47:05 +00009895
David L Kreitzer8bbabee2016-09-16 14:38:13 +00009896 // Avoid negative or zero stride values.
9897 if (!PositiveStride) {
9898 // We can compute the correct backedge taken count for loops with unknown
9899 // strides if we can prove that the loop is not an infinite loop with side
9900 // effects. Here's the loop structure we are trying to handle -
9901 //
9902 // i = start
9903 // do {
9904 // A[i] = i;
9905 // i += s;
9906 // } while (i < end);
9907 //
9908 // The backedge taken count for such loops is evaluated as -
9909 // (max(end, start + stride) - start - 1) /u stride
9910 //
9911 // The additional preconditions that we need to check to prove correctness
9912 // of the above formula is as follows -
9913 //
9914 // a) IV is either nuw or nsw depending upon signedness (indicated by the
9915 // NoWrap flag).
9916 // b) loop is single exit with no side effects.
9917 //
9918 //
9919 // Precondition a) implies that if the stride is negative, this is a single
9920 // trip loop. The backedge taken count formula reduces to zero in this case.
9921 //
9922 // Precondition b) implies that the unknown stride cannot be zero otherwise
9923 // we have UB.
9924 //
9925 // The positive stride case is the same as isKnownPositive(Stride) returning
9926 // true (original behavior of the function).
9927 //
9928 // We want to make sure that the stride is truly unknown as there are edge
9929 // cases where ScalarEvolution propagates no wrap flags to the
9930 // post-increment/decrement IV even though the increment/decrement operation
9931 // itself is wrapping. The computed backedge taken count may be wrong in
9932 // such cases. This is prevented by checking that the stride is not known to
9933 // be either positive or non-positive. For example, no wrap flags are
9934 // propagated to the post-increment IV of this loop with a trip count of 2 -
9935 //
9936 // unsigned char i;
9937 // for(i=127; i<128; i+=129)
9938 // A[i] = i;
9939 //
9940 if (PredicatedIV || !NoWrap || isKnownNonPositive(Stride) ||
9941 !loopHasNoSideEffects(L))
9942 return getCouldNotCompute();
David L Kreitzer8bbabee2016-09-16 14:38:13 +00009943 } else if (!Stride->isOne() &&
9944 doesIVOverflowOnLT(RHS, Stride, IsSigned, NoWrap))
9945 // Avoid proven overflow cases: this will ensure that the backedge taken
9946 // count will not generate any unsigned overflow. Relaxed no-overflow
9947 // conditions exploit NoWrapFlags, allowing to optimize in presence of
9948 // undefined behaviors like the case of C language.
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009949 return getCouldNotCompute();
Dan Gohman2b8da352009-04-30 20:47:05 +00009950
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009951 ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SLT
9952 : ICmpInst::ICMP_ULT;
9953 const SCEV *Start = IV->getStart();
9954 const SCEV *End = RHS;
Anna Thomasa2ca9022017-10-13 14:30:43 +00009955 // When the RHS is not invariant, we do not know the end bound of the loop and
9956 // cannot calculate the ExactBECount needed by ExitLimit. However, we can
9957 // calculate the MaxBECount, given the start, stride and max value for the end
9958 // bound of the loop (RHS), and the fact that IV does not overflow (which is
9959 // checked above).
9960 if (!isLoopInvariant(RHS, L)) {
Anna Thomas79503c02017-10-16 17:47:17 +00009961 const SCEV *MaxBECount = computeMaxBECountForLT(
Anna Thomasa2ca9022017-10-13 14:30:43 +00009962 Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
9963 return ExitLimit(getCouldNotCompute() /* ExactNotTaken */, MaxBECount,
9964 false /*MaxOrZero*/, Predicates);
9965 }
John Brawnecf79302016-10-18 10:10:53 +00009966 // If the backedge is taken at least once, then it will be taken
9967 // (End-Start)/Stride times (rounded up to a multiple of Stride), where Start
9968 // is the LHS value of the less-than comparison the first time it is evaluated
9969 // and End is the RHS.
9970 const SCEV *BECountIfBackedgeTaken =
9971 computeBECount(getMinusSCEV(End, Start), Stride, false);
9972 // If the loop entry is guarded by the result of the backedge test of the
9973 // first loop iteration, then we know the backedge will be taken at least
9974 // once and so the backedge taken count is as above. If not then we use the
9975 // expression (max(End,Start)-Start)/Stride to describe the backedge count,
9976 // as if the backedge is taken at least once max(End,Start) is End and so the
9977 // result is as above, and if not max(End,Start) is Start so we get a backedge
9978 // count of zero.
9979 const SCEV *BECount;
9980 if (isLoopEntryGuardedByCond(L, Cond, getMinusSCEV(Start, Stride), RHS))
9981 BECount = BECountIfBackedgeTaken;
9982 else {
Sanjoy Dase8fd9562016-06-18 04:38:31 +00009983 End = IsSigned ? getSMaxExpr(RHS, Start) : getUMaxExpr(RHS, Start);
John Brawnecf79302016-10-18 10:10:53 +00009984 BECount = computeBECount(getMinusSCEV(End, Start), Stride, false);
9985 }
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009986
Arnaud A. de Grandmaison75c9e6d2014-03-15 22:13:15 +00009987 const SCEV *MaxBECount;
John Brawn84b21832016-10-21 11:08:48 +00009988 bool MaxOrZero = false;
Andrew Trick34e2f0c2013-11-06 02:08:26 +00009989 if (isa<SCEVConstant>(BECount))
9990 MaxBECount = BECount;
John Brawn84b21832016-10-21 11:08:48 +00009991 else if (isa<SCEVConstant>(BECountIfBackedgeTaken)) {
John Brawnecf79302016-10-18 10:10:53 +00009992 // If we know exactly how many times the backedge will be taken if it's
9993 // taken at least once, then the backedge count will either be that or
9994 // zero.
9995 MaxBECount = BECountIfBackedgeTaken;
John Brawn84b21832016-10-21 11:08:48 +00009996 MaxOrZero = true;
9997 } else {
Anna Thomas79503c02017-10-16 17:47:17 +00009998 MaxBECount = computeMaxBECountForLT(
9999 Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
John Brawnecf79302016-10-18 10:10:53 +000010000 }
Andrew Trick34e2f0c2013-11-06 02:08:26 +000010001
Sanjoy Das036dda22017-05-22 06:46:04 +000010002 if (isa<SCEVCouldNotCompute>(MaxBECount) &&
10003 !isa<SCEVCouldNotCompute>(BECount))
Craig Topper01020392017-06-24 23:34:50 +000010004 MaxBECount = getConstant(getUnsignedRangeMax(BECount));
Andrew Trick34e2f0c2013-11-06 02:08:26 +000010005
John Brawn84b21832016-10-21 11:08:48 +000010006 return ExitLimit(BECount, MaxBECount, MaxOrZero, Predicates);
Andrew Trick34e2f0c2013-11-06 02:08:26 +000010007}
10008
10009ScalarEvolution::ExitLimit
Sanjoy Das108fcf22016-05-29 00:38:00 +000010010ScalarEvolution::howManyGreaterThans(const SCEV *LHS, const SCEV *RHS,
Andrew Trick34e2f0c2013-11-06 02:08:26 +000010011 const Loop *L, bool IsSigned,
Silviu Baranga6f444df2016-04-08 14:29:09 +000010012 bool ControlsExit, bool AllowPredicates) {
Sanjoy Dasf0022122016-09-28 17:14:58 +000010013 SmallPtrSet<const SCEVPredicate *, 4> Predicates;
Andrew Trick34e2f0c2013-11-06 02:08:26 +000010014 // We handle only IV > Invariant
10015 if (!isLoopInvariant(RHS, L))
10016 return getCouldNotCompute();
10017
10018 const SCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(LHS);
Silviu Baranga6f444df2016-04-08 14:29:09 +000010019 if (!IV && AllowPredicates)
10020 // Try to make this an AddRec using runtime tests, in the first X
10021 // iterations of this loop, where X is the SCEV expression found by the
10022 // algorithm below.
Sanjoy Dasf0022122016-09-28 17:14:58 +000010023 IV = convertSCEVToAddRecWithPredicates(LHS, L, Predicates);
Andrew Trick34e2f0c2013-11-06 02:08:26 +000010024
10025 // Avoid weird loops
10026 if (!IV || IV->getLoop() != L || !IV->isAffine())
10027 return getCouldNotCompute();
10028
Mark Heffernan2beab5f2014-10-10 17:39:11 +000010029 bool NoWrap = ControlsExit &&
Andrew Trick34e2f0c2013-11-06 02:08:26 +000010030 IV->getNoWrapFlags(IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW);
10031
10032 const SCEV *Stride = getNegativeSCEV(IV->getStepRecurrence(*this));
10033
10034 // Avoid negative or zero stride values
10035 if (!isKnownPositive(Stride))
10036 return getCouldNotCompute();
10037
10038 // Avoid proven overflow cases: this will ensure that the backedge taken count
10039 // will not generate any unsigned overflow. Relaxed no-overflow conditions
Johannes Doerfert2683e562015-02-09 12:34:23 +000010040 // exploit NoWrapFlags, allowing to optimize in presence of undefined
Andrew Trick34e2f0c2013-11-06 02:08:26 +000010041 // behaviors like the case of C language.
10042 if (!Stride->isOne() && doesIVOverflowOnGT(RHS, Stride, IsSigned, NoWrap))
10043 return getCouldNotCompute();
10044
10045 ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SGT
10046 : ICmpInst::ICMP_UGT;
10047
10048 const SCEV *Start = IV->getStart();
10049 const SCEV *End = RHS;
Sanjoy Dase8fd9562016-06-18 04:38:31 +000010050 if (!isLoopEntryGuardedByCond(L, Cond, getAddExpr(Start, Stride), RHS))
10051 End = IsSigned ? getSMinExpr(RHS, Start) : getUMinExpr(RHS, Start);
Andrew Trick34e2f0c2013-11-06 02:08:26 +000010052
10053 const SCEV *BECount = computeBECount(getMinusSCEV(Start, End), Stride, false);
10054
Craig Topper01020392017-06-24 23:34:50 +000010055 APInt MaxStart = IsSigned ? getSignedRangeMax(Start)
10056 : getUnsignedRangeMax(Start);
Andrew Trick34e2f0c2013-11-06 02:08:26 +000010057
Craig Topper01020392017-06-24 23:34:50 +000010058 APInt MinStride = IsSigned ? getSignedRangeMin(Stride)
10059 : getUnsignedRangeMin(Stride);
Andrew Trick34e2f0c2013-11-06 02:08:26 +000010060
10061 unsigned BitWidth = getTypeSizeInBits(LHS->getType());
10062 APInt Limit = IsSigned ? APInt::getSignedMinValue(BitWidth) + (MinStride - 1)
10063 : APInt::getMinValue(BitWidth) + (MinStride - 1);
10064
10065 // Although End can be a MIN expression we estimate MinEnd considering only
10066 // the case End = RHS. This is safe because in the other case (Start - End)
10067 // is zero, leading to a zero maximum backedge taken count.
10068 APInt MinEnd =
Craig Topper01020392017-06-24 23:34:50 +000010069 IsSigned ? APIntOps::smax(getSignedRangeMin(RHS), Limit)
10070 : APIntOps::umax(getUnsignedRangeMin(RHS), Limit);
Andrew Trick34e2f0c2013-11-06 02:08:26 +000010071
10072
10073 const SCEV *MaxBECount = getCouldNotCompute();
10074 if (isa<SCEVConstant>(BECount))
10075 MaxBECount = BECount;
10076 else
Johannes Doerfert2683e562015-02-09 12:34:23 +000010077 MaxBECount = computeBECount(getConstant(MaxStart - MinEnd),
Andrew Trick34e2f0c2013-11-06 02:08:26 +000010078 getConstant(MinStride), false);
10079
10080 if (isa<SCEVCouldNotCompute>(MaxBECount))
10081 MaxBECount = BECount;
10082
John Brawn84b21832016-10-21 11:08:48 +000010083 return ExitLimit(BECount, MaxBECount, false, Predicates);
Chris Lattner587a75b2005-08-15 23:33:51 +000010084}
10085
Benjamin Kramerc321e532016-06-08 19:09:22 +000010086const SCEV *SCEVAddRecExpr::getNumIterationsInRange(const ConstantRange &Range,
Dan Gohmance973df2009-06-24 04:48:43 +000010087 ScalarEvolution &SE) const {
Chris Lattnerd934c702004-04-02 20:23:17 +000010088 if (Range.isFullSet()) // Infinite loop.
Dan Gohman31efa302009-04-18 17:58:19 +000010089 return SE.getCouldNotCompute();
Chris Lattnerd934c702004-04-02 20:23:17 +000010090
10091 // If the start is a non-zero constant, shift the range to simplify things.
Dan Gohmana30370b2009-05-04 22:02:23 +000010092 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart()))
Reid Spencer2e54a152007-03-02 00:28:52 +000010093 if (!SC->getValue()->isZero()) {
Dan Gohmanaf752342009-07-07 17:06:11 +000010094 SmallVector<const SCEV *, 4> Operands(op_begin(), op_end());
Sanjoy Das2aacc0e2015-09-23 01:59:04 +000010095 Operands[0] = SE.getZero(SC->getType());
Andrew Trick8b55b732011-03-14 16:50:06 +000010096 const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop(),
Andrew Trickf6b01ff2011-03-15 00:37:00 +000010097 getNoWrapFlags(FlagNW));
Sanjoy Das63914592015-10-18 00:29:20 +000010098 if (const auto *ShiftedAddRec = dyn_cast<SCEVAddRecExpr>(Shifted))
Chris Lattnerd934c702004-04-02 20:23:17 +000010099 return ShiftedAddRec->getNumIterationsInRange(
Sanjoy Das0de2fec2015-12-17 20:28:46 +000010100 Range.subtract(SC->getAPInt()), SE);
Chris Lattnerd934c702004-04-02 20:23:17 +000010101 // This is strange and shouldn't happen.
Dan Gohman31efa302009-04-18 17:58:19 +000010102 return SE.getCouldNotCompute();
Chris Lattnerd934c702004-04-02 20:23:17 +000010103 }
10104
10105 // The only time we can solve this is when we have all constant indices.
10106 // Otherwise, we cannot determine the overflow conditions.
Sanjoy Dasff3b8b42015-12-01 07:49:23 +000010107 if (any_of(operands(), [](const SCEV *Op) { return !isa<SCEVConstant>(Op); }))
Sanjoy Dasf07d2a72015-10-18 00:29:23 +000010108 return SE.getCouldNotCompute();
Chris Lattnerd934c702004-04-02 20:23:17 +000010109
10110 // Okay at this point we know that all elements of the chrec are constants and
10111 // that the start element is zero.
10112
10113 // First check to see if the range contains zero. If not, the first
10114 // iteration exits.
Dan Gohmanb397e1a2009-04-21 01:07:12 +000010115 unsigned BitWidth = SE.getTypeSizeInBits(getType());
Dan Gohman0a40ad92009-04-16 03:18:22 +000010116 if (!Range.contains(APInt(BitWidth, 0)))
Sanjoy Das2aacc0e2015-09-23 01:59:04 +000010117 return SE.getZero(getType());
Misha Brukman01808ca2005-04-21 21:13:18 +000010118
Chris Lattnerd934c702004-04-02 20:23:17 +000010119 if (isAffine()) {
10120 // If this is an affine expression then we have this situation:
10121 // Solve {0,+,A} in Range === Ax in Range
10122
Nick Lewycky52460262007-07-16 02:08:00 +000010123 // We know that zero is in the range. If A is positive then we know that
10124 // the upper value of the range must be the first possible exit value.
10125 // If A is negative then the lower of the range is the last possible loop
10126 // value. Also note that we already checked for a full range.
Sanjoy Das0de2fec2015-12-17 20:28:46 +000010127 APInt A = cast<SCEVConstant>(getOperand(1))->getAPInt();
Craig Topperc97fdb82017-05-06 05:15:11 +000010128 APInt End = A.sge(1) ? (Range.getUpper() - 1) : Range.getLower();
Chris Lattnerd934c702004-04-02 20:23:17 +000010129
Nick Lewycky52460262007-07-16 02:08:00 +000010130 // The exit value should be (End+A)/A.
Nick Lewycky39349612007-09-27 14:12:54 +000010131 APInt ExitVal = (End + A).udiv(A);
Owen Andersonedb4a702009-07-24 23:12:02 +000010132 ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal);
Chris Lattnerd934c702004-04-02 20:23:17 +000010133
10134 // Evaluate at the exit value. If we really did fall out of the valid
10135 // range, then we computed our trip count, otherwise wrap around or other
10136 // things must have happened.
Dan Gohmana37eaf22007-10-22 18:31:58 +000010137 ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE);
Reid Spencer6a440332007-03-01 07:54:15 +000010138 if (Range.contains(Val->getValue()))
Dan Gohman31efa302009-04-18 17:58:19 +000010139 return SE.getCouldNotCompute(); // Something strange happened
Chris Lattnerd934c702004-04-02 20:23:17 +000010140
10141 // Ensure that the previous value is in the range. This is a sanity check.
Reid Spencer3a7e9d82007-02-28 19:57:34 +000010142 assert(Range.contains(
Dan Gohmance973df2009-06-24 04:48:43 +000010143 EvaluateConstantChrecAtConstant(this,
Craig Topperc97fdb82017-05-06 05:15:11 +000010144 ConstantInt::get(SE.getContext(), ExitVal - 1), SE)->getValue()) &&
Chris Lattnerd934c702004-04-02 20:23:17 +000010145 "Linear scev computation is off in a bad way!");
Dan Gohmana37eaf22007-10-22 18:31:58 +000010146 return SE.getConstant(ExitValue);
Chris Lattnerd934c702004-04-02 20:23:17 +000010147 } else if (isQuadratic()) {
10148 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the
10149 // quadratic equation to solve it. To do this, we must frame our problem in
10150 // terms of figuring out when zero is crossed, instead of when
10151 // Range.getUpper() is crossed.
Dan Gohmanaf752342009-07-07 17:06:11 +000010152 SmallVector<const SCEV *, 4> NewOps(op_begin(), op_end());
Dan Gohmana37eaf22007-10-22 18:31:58 +000010153 NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper()));
Sanjoy Das54e6a212016-10-02 00:09:45 +000010154 const SCEV *NewAddRec = SE.getAddRecExpr(NewOps, getLoop(), FlagAnyWrap);
Chris Lattnerd934c702004-04-02 20:23:17 +000010155
10156 // Next, solve the constructed addrec
Sanjoy Das0e392d52016-06-15 04:37:50 +000010157 if (auto Roots =
10158 SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE)) {
Sanjoy Das5a3d8932016-06-15 04:37:47 +000010159 const SCEVConstant *R1 = Roots->first;
10160 const SCEVConstant *R2 = Roots->second;
Chris Lattnerd934c702004-04-02 20:23:17 +000010161 // Pick the smallest positive root value.
Sanjoy Das01947432015-11-22 21:20:13 +000010162 if (ConstantInt *CB = dyn_cast<ConstantInt>(ConstantExpr::getICmp(
10163 ICmpInst::ICMP_ULT, R1->getValue(), R2->getValue()))) {
David Blaikiedc3f01e2015-03-09 01:57:13 +000010164 if (!CB->getZExtValue())
Sanjoy Das0e392d52016-06-15 04:37:50 +000010165 std::swap(R1, R2); // R1 is the minimum root now.
Misha Brukman01808ca2005-04-21 21:13:18 +000010166
Chris Lattnerd934c702004-04-02 20:23:17 +000010167 // Make sure the root is not off by one. The returned iteration should
10168 // not be in the range, but the previous one should be. When solving
10169 // for "X*X < 5", for example, we should not return a root of 2.
Sanjoy Das0e392d52016-06-15 04:37:50 +000010170 ConstantInt *R1Val =
10171 EvaluateConstantChrecAtConstant(this, R1->getValue(), SE);
Reid Spencer6a440332007-03-01 07:54:15 +000010172 if (Range.contains(R1Val->getValue())) {
Chris Lattnerd934c702004-04-02 20:23:17 +000010173 // The next iteration must be out of the range...
Owen Andersonf1f17432009-07-06 22:37:39 +000010174 ConstantInt *NextVal =
Sanjoy Das0de2fec2015-12-17 20:28:46 +000010175 ConstantInt::get(SE.getContext(), R1->getAPInt() + 1);
Misha Brukman01808ca2005-04-21 21:13:18 +000010176
Dan Gohmana37eaf22007-10-22 18:31:58 +000010177 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
Reid Spencer6a440332007-03-01 07:54:15 +000010178 if (!Range.contains(R1Val->getValue()))
Dan Gohmana37eaf22007-10-22 18:31:58 +000010179 return SE.getConstant(NextVal);
Sanjoy Das0e392d52016-06-15 04:37:50 +000010180 return SE.getCouldNotCompute(); // Something strange happened
Chris Lattnerd934c702004-04-02 20:23:17 +000010181 }
Misha Brukman01808ca2005-04-21 21:13:18 +000010182
Chris Lattnerd934c702004-04-02 20:23:17 +000010183 // If R1 was not in the range, then it is a good return value. Make
10184 // sure that R1-1 WAS in the range though, just in case.
Owen Andersonf1f17432009-07-06 22:37:39 +000010185 ConstantInt *NextVal =
Sanjoy Das0de2fec2015-12-17 20:28:46 +000010186 ConstantInt::get(SE.getContext(), R1->getAPInt() - 1);
Dan Gohmana37eaf22007-10-22 18:31:58 +000010187 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
Reid Spencer6a440332007-03-01 07:54:15 +000010188 if (Range.contains(R1Val->getValue()))
Chris Lattnerd934c702004-04-02 20:23:17 +000010189 return R1;
Sanjoy Das0e392d52016-06-15 04:37:50 +000010190 return SE.getCouldNotCompute(); // Something strange happened
Chris Lattnerd934c702004-04-02 20:23:17 +000010191 }
10192 }
10193 }
10194
Dan Gohman31efa302009-04-18 17:58:19 +000010195 return SE.getCouldNotCompute();
Chris Lattnerd934c702004-04-02 20:23:17 +000010196}
10197
Sebastian Popa7d3d6a2014-05-07 19:00:32 +000010198// Return true when S contains at least an undef value.
Sanjoy Das6b46a0d2016-11-09 18:22:43 +000010199static inline bool containsUndefs(const SCEV *S) {
10200 return SCEVExprContains(S, [](const SCEV *S) {
10201 if (const auto *SU = dyn_cast<SCEVUnknown>(S))
10202 return isa<UndefValue>(SU->getValue());
10203 else if (const auto *SC = dyn_cast<SCEVConstant>(S))
10204 return isa<UndefValue>(SC->getValue());
10205 return false;
10206 });
Sebastian Popa7d3d6a2014-05-07 19:00:32 +000010207}
10208
10209namespace {
Eugene Zelenkobe709f22017-08-18 23:51:26 +000010210
Sebastian Pop448712b2014-05-07 18:01:20 +000010211// Collect all steps of SCEV expressions.
10212struct SCEVCollectStrides {
10213 ScalarEvolution &SE;
10214 SmallVectorImpl<const SCEV *> &Strides;
10215
10216 SCEVCollectStrides(ScalarEvolution &SE, SmallVectorImpl<const SCEV *> &S)
10217 : SE(SE), Strides(S) {}
10218
10219 bool follow(const SCEV *S) {
10220 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S))
10221 Strides.push_back(AR->getStepRecurrence(SE));
10222 return true;
10223 }
Eugene Zelenkobe709f22017-08-18 23:51:26 +000010224
Sebastian Pop448712b2014-05-07 18:01:20 +000010225 bool isDone() const { return false; }
10226};
10227
10228// Collect all SCEVUnknown and SCEVMulExpr expressions.
10229struct SCEVCollectTerms {
10230 SmallVectorImpl<const SCEV *> &Terms;
10231
Eugene Zelenkobe709f22017-08-18 23:51:26 +000010232 SCEVCollectTerms(SmallVectorImpl<const SCEV *> &T) : Terms(T) {}
Sebastian Pop448712b2014-05-07 18:01:20 +000010233
10234 bool follow(const SCEV *S) {
Tobias Grosser2bbec0e2016-10-17 11:56:26 +000010235 if (isa<SCEVUnknown>(S) || isa<SCEVMulExpr>(S) ||
10236 isa<SCEVSignExtendExpr>(S)) {
Sebastian Popa7d3d6a2014-05-07 19:00:32 +000010237 if (!containsUndefs(S))
10238 Terms.push_back(S);
Sebastian Pop448712b2014-05-07 18:01:20 +000010239
10240 // Stop recursion: once we collected a term, do not walk its operands.
10241 return false;
10242 }
10243
10244 // Keep looking.
10245 return true;
10246 }
Eugene Zelenkobe709f22017-08-18 23:51:26 +000010247
Sebastian Pop448712b2014-05-07 18:01:20 +000010248 bool isDone() const { return false; }
10249};
Tobias Grosser374bce02015-10-12 08:02:00 +000010250
10251// Check if a SCEV contains an AddRecExpr.
10252struct SCEVHasAddRec {
10253 bool &ContainsAddRec;
10254
10255 SCEVHasAddRec(bool &ContainsAddRec) : ContainsAddRec(ContainsAddRec) {
Eugene Zelenkobe709f22017-08-18 23:51:26 +000010256 ContainsAddRec = false;
Tobias Grosser374bce02015-10-12 08:02:00 +000010257 }
10258
10259 bool follow(const SCEV *S) {
10260 if (isa<SCEVAddRecExpr>(S)) {
10261 ContainsAddRec = true;
10262
10263 // Stop recursion: once we collected a term, do not walk its operands.
10264 return false;
10265 }
10266
10267 // Keep looking.
10268 return true;
10269 }
Eugene Zelenkobe709f22017-08-18 23:51:26 +000010270
Tobias Grosser374bce02015-10-12 08:02:00 +000010271 bool isDone() const { return false; }
10272};
10273
10274// Find factors that are multiplied with an expression that (possibly as a
10275// subexpression) contains an AddRecExpr. In the expression:
10276//
10277// 8 * (100 + %p * %q * (%a + {0, +, 1}_loop))
10278//
10279// "%p * %q" are factors multiplied by the expression "(%a + {0, +, 1}_loop)"
10280// that contains the AddRec {0, +, 1}_loop. %p * %q are likely to be array size
10281// parameters as they form a product with an induction variable.
10282//
10283// This collector expects all array size parameters to be in the same MulExpr.
10284// It might be necessary to later add support for collecting parameters that are
10285// spread over different nested MulExpr.
10286struct SCEVCollectAddRecMultiplies {
10287 SmallVectorImpl<const SCEV *> &Terms;
10288 ScalarEvolution &SE;
10289
10290 SCEVCollectAddRecMultiplies(SmallVectorImpl<const SCEV *> &T, ScalarEvolution &SE)
10291 : Terms(T), SE(SE) {}
10292
10293 bool follow(const SCEV *S) {
10294 if (auto *Mul = dyn_cast<SCEVMulExpr>(S)) {
10295 bool HasAddRec = false;
10296 SmallVector<const SCEV *, 0> Operands;
10297 for (auto Op : Mul->operands()) {
Tobias Grossere3684d02017-05-27 15:17:49 +000010298 const SCEVUnknown *Unknown = dyn_cast<SCEVUnknown>(Op);
10299 if (Unknown && !isa<CallInst>(Unknown->getValue())) {
Tobias Grosser374bce02015-10-12 08:02:00 +000010300 Operands.push_back(Op);
Tobias Grossere3684d02017-05-27 15:17:49 +000010301 } else if (Unknown) {
10302 HasAddRec = true;
Tobias Grosser374bce02015-10-12 08:02:00 +000010303 } else {
10304 bool ContainsAddRec;
10305 SCEVHasAddRec ContiansAddRec(ContainsAddRec);
10306 visitAll(Op, ContiansAddRec);
10307 HasAddRec |= ContainsAddRec;
10308 }
10309 }
10310 if (Operands.size() == 0)
10311 return true;
10312
10313 if (!HasAddRec)
10314 return false;
10315
10316 Terms.push_back(SE.getMulExpr(Operands));
10317 // Stop recursion: once we collected a term, do not walk its operands.
10318 return false;
10319 }
10320
10321 // Keep looking.
10322 return true;
10323 }
Eugene Zelenkobe709f22017-08-18 23:51:26 +000010324
Tobias Grosser374bce02015-10-12 08:02:00 +000010325 bool isDone() const { return false; }
10326};
Eugene Zelenkobe709f22017-08-18 23:51:26 +000010327
10328} // end anonymous namespace
Sebastian Pop448712b2014-05-07 18:01:20 +000010329
Tobias Grosser374bce02015-10-12 08:02:00 +000010330/// Find parametric terms in this SCEVAddRecExpr. We first for parameters in
10331/// two places:
10332/// 1) The strides of AddRec expressions.
10333/// 2) Unknowns that are multiplied with AddRec expressions.
Tobias Grosser3cdc37c2015-06-29 14:42:48 +000010334void ScalarEvolution::collectParametricTerms(const SCEV *Expr,
10335 SmallVectorImpl<const SCEV *> &Terms) {
Sebastian Pop448712b2014-05-07 18:01:20 +000010336 SmallVector<const SCEV *, 4> Strides;
Tobias Grosser3cdc37c2015-06-29 14:42:48 +000010337 SCEVCollectStrides StrideCollector(*this, Strides);
10338 visitAll(Expr, StrideCollector);
Sebastian Pop448712b2014-05-07 18:01:20 +000010339
10340 DEBUG({
10341 dbgs() << "Strides:\n";
10342 for (const SCEV *S : Strides)
10343 dbgs() << *S << "\n";
10344 });
10345
10346 for (const SCEV *S : Strides) {
10347 SCEVCollectTerms TermCollector(Terms);
10348 visitAll(S, TermCollector);
10349 }
10350
10351 DEBUG({
10352 dbgs() << "Terms:\n";
10353 for (const SCEV *T : Terms)
10354 dbgs() << *T << "\n";
10355 });
Tobias Grosser374bce02015-10-12 08:02:00 +000010356
10357 SCEVCollectAddRecMultiplies MulCollector(Terms, *this);
10358 visitAll(Expr, MulCollector);
Sebastian Pop448712b2014-05-07 18:01:20 +000010359}
10360
Sebastian Popb1a548f2014-05-12 19:01:53 +000010361static bool findArrayDimensionsRec(ScalarEvolution &SE,
Sebastian Pop448712b2014-05-07 18:01:20 +000010362 SmallVectorImpl<const SCEV *> &Terms,
Sebastian Pop47fe7de2014-05-09 22:45:07 +000010363 SmallVectorImpl<const SCEV *> &Sizes) {
Sebastian Pope30bd352014-05-27 22:41:56 +000010364 int Last = Terms.size() - 1;
10365 const SCEV *Step = Terms[Last];
Sebastian Popc62c6792013-11-12 22:47:20 +000010366
Sebastian Pop448712b2014-05-07 18:01:20 +000010367 // End of recursion.
Sebastian Pope30bd352014-05-27 22:41:56 +000010368 if (Last == 0) {
10369 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Step)) {
Sebastian Pop448712b2014-05-07 18:01:20 +000010370 SmallVector<const SCEV *, 2> Qs;
10371 for (const SCEV *Op : M->operands())
10372 if (!isa<SCEVConstant>(Op))
10373 Qs.push_back(Op);
Sebastian Popc62c6792013-11-12 22:47:20 +000010374
Sebastian Pope30bd352014-05-27 22:41:56 +000010375 Step = SE.getMulExpr(Qs);
Sebastian Popc62c6792013-11-12 22:47:20 +000010376 }
10377
Sebastian Pope30bd352014-05-27 22:41:56 +000010378 Sizes.push_back(Step);
Sebastian Popb1a548f2014-05-12 19:01:53 +000010379 return true;
Sebastian Popc62c6792013-11-12 22:47:20 +000010380 }
10381
Benjamin Kramer8cff45a2014-05-10 17:47:18 +000010382 for (const SCEV *&Term : Terms) {
Sebastian Pop448712b2014-05-07 18:01:20 +000010383 // Normalize the terms before the next call to findArrayDimensionsRec.
10384 const SCEV *Q, *R;
David Majnemer4e879362014-12-14 09:12:33 +000010385 SCEVDivision::divide(SE, Term, Step, &Q, &R);
Sebastian Popb1a548f2014-05-12 19:01:53 +000010386
10387 // Bail out when GCD does not evenly divide one of the terms.
10388 if (!R->isZero())
10389 return false;
10390
Benjamin Kramer8cff45a2014-05-10 17:47:18 +000010391 Term = Q;
Sebastian Popc62c6792013-11-12 22:47:20 +000010392 }
10393
Tobias Grosser3080cf12014-05-08 07:55:34 +000010394 // Remove all SCEVConstants.
David Majnemerc7004902016-08-12 04:32:37 +000010395 Terms.erase(
10396 remove_if(Terms, [](const SCEV *E) { return isa<SCEVConstant>(E); }),
10397 Terms.end());
Sebastian Popc62c6792013-11-12 22:47:20 +000010398
Sebastian Pop448712b2014-05-07 18:01:20 +000010399 if (Terms.size() > 0)
Sebastian Popb1a548f2014-05-12 19:01:53 +000010400 if (!findArrayDimensionsRec(SE, Terms, Sizes))
10401 return false;
10402
Sebastian Pope30bd352014-05-27 22:41:56 +000010403 Sizes.push_back(Step);
Sebastian Popb1a548f2014-05-12 19:01:53 +000010404 return true;
Sebastian Pop448712b2014-05-07 18:01:20 +000010405}
Sebastian Popc62c6792013-11-12 22:47:20 +000010406
Sebastian Pop448712b2014-05-07 18:01:20 +000010407// Returns true when one of the SCEVs of Terms contains a SCEVUnknown parameter.
Sanjoy Das6b46a0d2016-11-09 18:22:43 +000010408static inline bool containsParameters(SmallVectorImpl<const SCEV *> &Terms) {
Sebastian Pop448712b2014-05-07 18:01:20 +000010409 for (const SCEV *T : Terms)
Sanjoy Das0ae390a2016-11-10 06:33:54 +000010410 if (SCEVExprContains(T, isa<SCEVUnknown, const SCEV *>))
Sebastian Pop448712b2014-05-07 18:01:20 +000010411 return true;
10412 return false;
10413}
10414
10415// Return the number of product terms in S.
10416static inline int numberOfTerms(const SCEV *S) {
10417 if (const SCEVMulExpr *Expr = dyn_cast<SCEVMulExpr>(S))
10418 return Expr->getNumOperands();
10419 return 1;
10420}
10421
Sebastian Popa6e58602014-05-27 22:41:45 +000010422static const SCEV *removeConstantFactors(ScalarEvolution &SE, const SCEV *T) {
10423 if (isa<SCEVConstant>(T))
10424 return nullptr;
10425
10426 if (isa<SCEVUnknown>(T))
10427 return T;
10428
10429 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(T)) {
10430 SmallVector<const SCEV *, 2> Factors;
10431 for (const SCEV *Op : M->operands())
10432 if (!isa<SCEVConstant>(Op))
10433 Factors.push_back(Op);
10434
10435 return SE.getMulExpr(Factors);
10436 }
10437
10438 return T;
10439}
10440
10441/// Return the size of an element read or written by Inst.
10442const SCEV *ScalarEvolution::getElementSize(Instruction *Inst) {
10443 Type *Ty;
10444 if (StoreInst *Store = dyn_cast<StoreInst>(Inst))
10445 Ty = Store->getValueOperand()->getType();
10446 else if (LoadInst *Load = dyn_cast<LoadInst>(Inst))
Tobias Grosser40ac1002014-06-08 19:21:20 +000010447 Ty = Load->getType();
Sebastian Popa6e58602014-05-27 22:41:45 +000010448 else
10449 return nullptr;
10450
10451 Type *ETy = getEffectiveSCEVType(PointerType::getUnqual(Ty));
10452 return getSizeOfExpr(ETy, Ty);
10453}
10454
Sebastian Popa6e58602014-05-27 22:41:45 +000010455void ScalarEvolution::findArrayDimensions(SmallVectorImpl<const SCEV *> &Terms,
10456 SmallVectorImpl<const SCEV *> &Sizes,
Sanjoy Dasdf8c2eb2017-05-07 05:29:36 +000010457 const SCEV *ElementSize) {
Sebastian Pop53524082014-05-29 19:44:05 +000010458 if (Terms.size() < 1 || !ElementSize)
Sebastian Pop448712b2014-05-07 18:01:20 +000010459 return;
10460
10461 // Early return when Terms do not contain parameters: we do not delinearize
10462 // non parametric SCEVs.
10463 if (!containsParameters(Terms))
10464 return;
10465
10466 DEBUG({
10467 dbgs() << "Terms:\n";
10468 for (const SCEV *T : Terms)
10469 dbgs() << *T << "\n";
10470 });
10471
10472 // Remove duplicates.
Sanjoy Das40415ee2017-05-07 05:29:34 +000010473 array_pod_sort(Terms.begin(), Terms.end());
Sebastian Pop448712b2014-05-07 18:01:20 +000010474 Terms.erase(std::unique(Terms.begin(), Terms.end()), Terms.end());
10475
10476 // Put larger terms first.
10477 std::sort(Terms.begin(), Terms.end(), [](const SCEV *LHS, const SCEV *RHS) {
10478 return numberOfTerms(LHS) > numberOfTerms(RHS);
10479 });
10480
Tobias Grosser374bce02015-10-12 08:02:00 +000010481 // Try to divide all terms by the element size. If term is not divisible by
10482 // element size, proceed with the original term.
Sebastian Popa6e58602014-05-27 22:41:45 +000010483 for (const SCEV *&Term : Terms) {
10484 const SCEV *Q, *R;
Sanjoy Dasdf8c2eb2017-05-07 05:29:36 +000010485 SCEVDivision::divide(*this, Term, ElementSize, &Q, &R);
Tobias Grosser374bce02015-10-12 08:02:00 +000010486 if (!Q->isZero())
10487 Term = Q;
Sebastian Popa6e58602014-05-27 22:41:45 +000010488 }
10489
10490 SmallVector<const SCEV *, 4> NewTerms;
10491
10492 // Remove constant factors.
10493 for (const SCEV *T : Terms)
Sanjoy Dasdf8c2eb2017-05-07 05:29:36 +000010494 if (const SCEV *NewT = removeConstantFactors(*this, T))
Sebastian Popa6e58602014-05-27 22:41:45 +000010495 NewTerms.push_back(NewT);
10496
Sebastian Pop448712b2014-05-07 18:01:20 +000010497 DEBUG({
10498 dbgs() << "Terms after sorting:\n";
Sebastian Popa6e58602014-05-27 22:41:45 +000010499 for (const SCEV *T : NewTerms)
Sebastian Pop448712b2014-05-07 18:01:20 +000010500 dbgs() << *T << "\n";
10501 });
10502
Sanjoy Dasdf8c2eb2017-05-07 05:29:36 +000010503 if (NewTerms.empty() || !findArrayDimensionsRec(*this, NewTerms, Sizes)) {
Sebastian Popb1a548f2014-05-12 19:01:53 +000010504 Sizes.clear();
10505 return;
10506 }
Sebastian Pop448712b2014-05-07 18:01:20 +000010507
Sebastian Popa6e58602014-05-27 22:41:45 +000010508 // The last element to be pushed into Sizes is the size of an element.
10509 Sizes.push_back(ElementSize);
10510
Sebastian Pop448712b2014-05-07 18:01:20 +000010511 DEBUG({
10512 dbgs() << "Sizes:\n";
10513 for (const SCEV *S : Sizes)
10514 dbgs() << *S << "\n";
10515 });
10516}
10517
Tobias Grosser3cdc37c2015-06-29 14:42:48 +000010518void ScalarEvolution::computeAccessFunctions(
10519 const SCEV *Expr, SmallVectorImpl<const SCEV *> &Subscripts,
10520 SmallVectorImpl<const SCEV *> &Sizes) {
Sebastian Popb1a548f2014-05-12 19:01:53 +000010521 // Early exit in case this SCEV is not an affine multivariate function.
Tobias Grosser3cdc37c2015-06-29 14:42:48 +000010522 if (Sizes.empty())
Sebastian Pop28e6b972014-05-27 22:41:51 +000010523 return;
Sebastian Popb1a548f2014-05-12 19:01:53 +000010524
Sanjoy Das1195dbe2015-10-08 03:45:58 +000010525 if (auto *AR = dyn_cast<SCEVAddRecExpr>(Expr))
Tobias Grosser3cdc37c2015-06-29 14:42:48 +000010526 if (!AR->isAffine())
10527 return;
10528
10529 const SCEV *Res = Expr;
Sebastian Pop448712b2014-05-07 18:01:20 +000010530 int Last = Sizes.size() - 1;
10531 for (int i = Last; i >= 0; i--) {
10532 const SCEV *Q, *R;
Tobias Grosser3cdc37c2015-06-29 14:42:48 +000010533 SCEVDivision::divide(*this, Res, Sizes[i], &Q, &R);
Sebastian Pop448712b2014-05-07 18:01:20 +000010534
10535 DEBUG({
10536 dbgs() << "Res: " << *Res << "\n";
10537 dbgs() << "Sizes[i]: " << *Sizes[i] << "\n";
10538 dbgs() << "Res divided by Sizes[i]:\n";
10539 dbgs() << "Quotient: " << *Q << "\n";
10540 dbgs() << "Remainder: " << *R << "\n";
10541 });
10542
10543 Res = Q;
10544
Sebastian Popa6e58602014-05-27 22:41:45 +000010545 // Do not record the last subscript corresponding to the size of elements in
10546 // the array.
Sebastian Pop448712b2014-05-07 18:01:20 +000010547 if (i == Last) {
Sebastian Popa6e58602014-05-27 22:41:45 +000010548
10549 // Bail out if the remainder is too complex.
Sebastian Pop28e6b972014-05-27 22:41:51 +000010550 if (isa<SCEVAddRecExpr>(R)) {
10551 Subscripts.clear();
10552 Sizes.clear();
10553 return;
10554 }
Sebastian Popa6e58602014-05-27 22:41:45 +000010555
Sebastian Pop448712b2014-05-07 18:01:20 +000010556 continue;
10557 }
10558
10559 // Record the access function for the current subscript.
10560 Subscripts.push_back(R);
10561 }
10562
10563 // Also push in last position the remainder of the last division: it will be
10564 // the access function of the innermost dimension.
10565 Subscripts.push_back(Res);
10566
10567 std::reverse(Subscripts.begin(), Subscripts.end());
10568
10569 DEBUG({
10570 dbgs() << "Subscripts:\n";
10571 for (const SCEV *S : Subscripts)
10572 dbgs() << *S << "\n";
10573 });
Sebastian Pop448712b2014-05-07 18:01:20 +000010574}
10575
Sebastian Popc62c6792013-11-12 22:47:20 +000010576/// Splits the SCEV into two vectors of SCEVs representing the subscripts and
10577/// sizes of an array access. Returns the remainder of the delinearization that
Sebastian Pop7ee14722013-11-13 22:37:58 +000010578/// is the offset start of the array. The SCEV->delinearize algorithm computes
10579/// the multiples of SCEV coefficients: that is a pattern matching of sub
10580/// expressions in the stride and base of a SCEV corresponding to the
10581/// computation of a GCD (greatest common divisor) of base and stride. When
10582/// SCEV->delinearize fails, it returns the SCEV unchanged.
10583///
10584/// For example: when analyzing the memory access A[i][j][k] in this loop nest
10585///
10586/// void foo(long n, long m, long o, double A[n][m][o]) {
10587///
10588/// for (long i = 0; i < n; i++)
10589/// for (long j = 0; j < m; j++)
10590/// for (long k = 0; k < o; k++)
10591/// A[i][j][k] = 1.0;
10592/// }
10593///
10594/// the delinearization input is the following AddRec SCEV:
10595///
10596/// AddRec: {{{%A,+,(8 * %m * %o)}<%for.i>,+,(8 * %o)}<%for.j>,+,8}<%for.k>
10597///
10598/// From this SCEV, we are able to say that the base offset of the access is %A
10599/// because it appears as an offset that does not divide any of the strides in
10600/// the loops:
10601///
10602/// CHECK: Base offset: %A
10603///
10604/// and then SCEV->delinearize determines the size of some of the dimensions of
10605/// the array as these are the multiples by which the strides are happening:
10606///
10607/// CHECK: ArrayDecl[UnknownSize][%m][%o] with elements of sizeof(double) bytes.
10608///
10609/// Note that the outermost dimension remains of UnknownSize because there are
10610/// no strides that would help identifying the size of the last dimension: when
10611/// the array has been statically allocated, one could compute the size of that
10612/// dimension by dividing the overall size of the array by the size of the known
10613/// dimensions: %m * %o * 8.
10614///
10615/// Finally delinearize provides the access functions for the array reference
10616/// that does correspond to A[i][j][k] of the above C testcase:
10617///
10618/// CHECK: ArrayRef[{0,+,1}<%for.i>][{0,+,1}<%for.j>][{0,+,1}<%for.k>]
10619///
10620/// The testcases are checking the output of a function pass:
10621/// DelinearizationPass that walks through all loads and stores of a function
10622/// asking for the SCEV of the memory access with respect to all enclosing
10623/// loops, calling SCEV->delinearize on that and printing the results.
Tobias Grosser3cdc37c2015-06-29 14:42:48 +000010624void ScalarEvolution::delinearize(const SCEV *Expr,
Sebastian Pop28e6b972014-05-27 22:41:51 +000010625 SmallVectorImpl<const SCEV *> &Subscripts,
10626 SmallVectorImpl<const SCEV *> &Sizes,
Tobias Grosser3cdc37c2015-06-29 14:42:48 +000010627 const SCEV *ElementSize) {
Sebastian Pop448712b2014-05-07 18:01:20 +000010628 // First step: collect parametric terms.
10629 SmallVector<const SCEV *, 4> Terms;
Tobias Grosser3cdc37c2015-06-29 14:42:48 +000010630 collectParametricTerms(Expr, Terms);
Sebastian Popc62c6792013-11-12 22:47:20 +000010631
Sebastian Popb1a548f2014-05-12 19:01:53 +000010632 if (Terms.empty())
Sebastian Pop28e6b972014-05-27 22:41:51 +000010633 return;
Sebastian Popb1a548f2014-05-12 19:01:53 +000010634
Sebastian Pop448712b2014-05-07 18:01:20 +000010635 // Second step: find subscript sizes.
Tobias Grosser3cdc37c2015-06-29 14:42:48 +000010636 findArrayDimensions(Terms, Sizes, ElementSize);
Sebastian Pop7ee14722013-11-13 22:37:58 +000010637
Sebastian Popb1a548f2014-05-12 19:01:53 +000010638 if (Sizes.empty())
Sebastian Pop28e6b972014-05-27 22:41:51 +000010639 return;
Sebastian Popb1a548f2014-05-12 19:01:53 +000010640
Sebastian Pop448712b2014-05-07 18:01:20 +000010641 // Third step: compute the access functions for each subscript.
Tobias Grosser3cdc37c2015-06-29 14:42:48 +000010642 computeAccessFunctions(Expr, Subscripts, Sizes);
Sebastian Popc62c6792013-11-12 22:47:20 +000010643
Sebastian Pop28e6b972014-05-27 22:41:51 +000010644 if (Subscripts.empty())
10645 return;
Sebastian Popb1a548f2014-05-12 19:01:53 +000010646
Sebastian Pop448712b2014-05-07 18:01:20 +000010647 DEBUG({
Tobias Grosser3cdc37c2015-06-29 14:42:48 +000010648 dbgs() << "succeeded to delinearize " << *Expr << "\n";
Sebastian Pop448712b2014-05-07 18:01:20 +000010649 dbgs() << "ArrayDecl[UnknownSize]";
10650 for (const SCEV *S : Sizes)
10651 dbgs() << "[" << *S << "]";
Sebastian Popc62c6792013-11-12 22:47:20 +000010652
Sebastian Pop444621a2014-05-09 22:45:02 +000010653 dbgs() << "\nArrayRef";
10654 for (const SCEV *S : Subscripts)
Sebastian Pop448712b2014-05-07 18:01:20 +000010655 dbgs() << "[" << *S << "]";
10656 dbgs() << "\n";
10657 });
Sebastian Popc62c6792013-11-12 22:47:20 +000010658}
Chris Lattnerd934c702004-04-02 20:23:17 +000010659
10660//===----------------------------------------------------------------------===//
Dan Gohman48f82222009-05-04 22:30:44 +000010661// SCEVCallbackVH Class Implementation
10662//===----------------------------------------------------------------------===//
10663
Dan Gohmand33a0902009-05-19 19:22:47 +000010664void ScalarEvolution::SCEVCallbackVH::deleted() {
Dan Gohmandd707af2009-07-13 22:20:53 +000010665 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
Dan Gohman48f82222009-05-04 22:30:44 +000010666 if (PHINode *PN = dyn_cast<PHINode>(getValPtr()))
10667 SE->ConstantEvolutionLoopExitValue.erase(PN);
Wei Mia49559b2016-02-04 01:27:38 +000010668 SE->eraseValueFromMap(getValPtr());
Dan Gohman48f82222009-05-04 22:30:44 +000010669 // this now dangles!
10670}
10671
Dan Gohman7a066722010-07-28 01:09:07 +000010672void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *V) {
Dan Gohmandd707af2009-07-13 22:20:53 +000010673 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
Eric Christopheref6d5932010-07-29 01:25:38 +000010674
Dan Gohman48f82222009-05-04 22:30:44 +000010675 // Forget all the expressions associated with users of the old value,
10676 // so that future queries will recompute the expressions using the new
10677 // value.
Dan Gohman7cac9572010-08-02 23:49:30 +000010678 Value *Old = getValPtr();
Chandler Carruthcdf47882014-03-09 03:16:01 +000010679 SmallVector<User *, 16> Worklist(Old->user_begin(), Old->user_end());
Dan Gohmanf34f8632009-07-14 14:34:04 +000010680 SmallPtrSet<User *, 8> Visited;
Dan Gohman48f82222009-05-04 22:30:44 +000010681 while (!Worklist.empty()) {
10682 User *U = Worklist.pop_back_val();
10683 // Deleting the Old value will cause this to dangle. Postpone
10684 // that until everything else is done.
Dan Gohman8aeb0fb2010-07-28 00:28:25 +000010685 if (U == Old)
Dan Gohman48f82222009-05-04 22:30:44 +000010686 continue;
David Blaikie70573dc2014-11-19 07:49:26 +000010687 if (!Visited.insert(U).second)
Dan Gohmanf34f8632009-07-14 14:34:04 +000010688 continue;
Dan Gohman48f82222009-05-04 22:30:44 +000010689 if (PHINode *PN = dyn_cast<PHINode>(U))
10690 SE->ConstantEvolutionLoopExitValue.erase(PN);
Wei Mia49559b2016-02-04 01:27:38 +000010691 SE->eraseValueFromMap(U);
Chandler Carruthcdf47882014-03-09 03:16:01 +000010692 Worklist.insert(Worklist.end(), U->user_begin(), U->user_end());
Dan Gohman48f82222009-05-04 22:30:44 +000010693 }
Dan Gohman8aeb0fb2010-07-28 00:28:25 +000010694 // Delete the Old value.
10695 if (PHINode *PN = dyn_cast<PHINode>(Old))
10696 SE->ConstantEvolutionLoopExitValue.erase(PN);
Wei Mia49559b2016-02-04 01:27:38 +000010697 SE->eraseValueFromMap(Old);
Dan Gohman8aeb0fb2010-07-28 00:28:25 +000010698 // this now dangles!
Dan Gohman48f82222009-05-04 22:30:44 +000010699}
10700
Dan Gohmand33a0902009-05-19 19:22:47 +000010701ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se)
Dan Gohman48f82222009-05-04 22:30:44 +000010702 : CallbackVH(V), SE(se) {}
10703
10704//===----------------------------------------------------------------------===//
Chris Lattnerd934c702004-04-02 20:23:17 +000010705// ScalarEvolution Class Implementation
10706//===----------------------------------------------------------------------===//
10707
Chandler Carruth2f1fd162015-08-17 02:08:17 +000010708ScalarEvolution::ScalarEvolution(Function &F, TargetLibraryInfo &TLI,
Daniel Jasperaec2fa32016-12-19 08:22:17 +000010709 AssumptionCache &AC, DominatorTree &DT,
10710 LoopInfo &LI)
10711 : F(F), TLI(TLI), AC(AC), DT(DT), LI(LI),
Eugene Zelenkobe709f22017-08-18 23:51:26 +000010712 CouldNotCompute(new SCEVCouldNotCompute()), ValuesAtScopes(64),
10713 LoopDispositions(64), BlockDispositions(64) {
Sanjoy Das2512d0c2016-05-10 00:31:49 +000010714 // To use guards for proving predicates, we need to scan every instruction in
10715 // relevant basic blocks, and not just terminators. Doing this is a waste of
10716 // time if the IR does not actually contain any calls to
10717 // @llvm.experimental.guard, so do a quick check and remember this beforehand.
10718 //
10719 // This pessimizes the case where a pass that preserves ScalarEvolution wants
10720 // to _add_ guards to the module when there weren't any before, and wants
10721 // ScalarEvolution to optimize based on those guards. For now we prefer to be
10722 // efficient in lieu of being smart in that rather obscure case.
10723
10724 auto *GuardDecl = F.getParent()->getFunction(
10725 Intrinsic::getName(Intrinsic::experimental_guard));
10726 HasGuards = GuardDecl && !GuardDecl->use_empty();
10727}
Chandler Carruth2f1fd162015-08-17 02:08:17 +000010728
10729ScalarEvolution::ScalarEvolution(ScalarEvolution &&Arg)
Daniel Jasperaec2fa32016-12-19 08:22:17 +000010730 : F(Arg.F), HasGuards(Arg.HasGuards), TLI(Arg.TLI), AC(Arg.AC), DT(Arg.DT),
Sanjoy Das2512d0c2016-05-10 00:31:49 +000010731 LI(Arg.LI), CouldNotCompute(std::move(Arg.CouldNotCompute)),
Chandler Carruth2f1fd162015-08-17 02:08:17 +000010732 ValueExprMap(std::move(Arg.ValueExprMap)),
Sanjoy Dasdb933752016-09-27 18:01:38 +000010733 PendingLoopPredicates(std::move(Arg.PendingLoopPredicates)),
Igor Laevskyc11c1ed2017-02-14 15:53:12 +000010734 MinTrailingZerosCache(std::move(Arg.MinTrailingZerosCache)),
Chandler Carruth2f1fd162015-08-17 02:08:17 +000010735 BackedgeTakenCounts(std::move(Arg.BackedgeTakenCounts)),
Silviu Baranga6f444df2016-04-08 14:29:09 +000010736 PredicatedBackedgeTakenCounts(
10737 std::move(Arg.PredicatedBackedgeTakenCounts)),
Chandler Carruth2f1fd162015-08-17 02:08:17 +000010738 ConstantEvolutionLoopExitValue(
10739 std::move(Arg.ConstantEvolutionLoopExitValue)),
10740 ValuesAtScopes(std::move(Arg.ValuesAtScopes)),
10741 LoopDispositions(std::move(Arg.LoopDispositions)),
Sanjoy Das5cb11b62016-09-26 02:44:10 +000010742 LoopPropertiesCache(std::move(Arg.LoopPropertiesCache)),
Chandler Carruth68abda52016-09-26 04:49:58 +000010743 BlockDispositions(std::move(Arg.BlockDispositions)),
Chandler Carruth2f1fd162015-08-17 02:08:17 +000010744 UnsignedRanges(std::move(Arg.UnsignedRanges)),
10745 SignedRanges(std::move(Arg.SignedRanges)),
10746 UniqueSCEVs(std::move(Arg.UniqueSCEVs)),
Silviu Barangae3c05342015-11-02 14:41:02 +000010747 UniquePreds(std::move(Arg.UniquePreds)),
Chandler Carruth2f1fd162015-08-17 02:08:17 +000010748 SCEVAllocator(std::move(Arg.SCEVAllocator)),
Sanjoy Dase6b995f2017-10-13 05:50:52 +000010749 LoopUsers(std::move(Arg.LoopUsers)),
Dorit Nuzmanca4fd182017-07-18 11:57:08 +000010750 PredicatedSCEVRewrites(std::move(Arg.PredicatedSCEVRewrites)),
Chandler Carruth2f1fd162015-08-17 02:08:17 +000010751 FirstUnknown(Arg.FirstUnknown) {
10752 Arg.FirstUnknown = nullptr;
Dan Gohmanc8e23622009-04-21 23:15:49 +000010753}
10754
Chandler Carruth2f1fd162015-08-17 02:08:17 +000010755ScalarEvolution::~ScalarEvolution() {
Dan Gohman7cac9572010-08-02 23:49:30 +000010756 // Iterate through all the SCEVUnknown instances and call their
10757 // destructors, so that they release their references to their values.
Naomi Musgravef90c1be2015-09-16 23:46:40 +000010758 for (SCEVUnknown *U = FirstUnknown; U;) {
10759 SCEVUnknown *Tmp = U;
10760 U = U->Next;
10761 Tmp->~SCEVUnknown();
10762 }
Craig Topper9f008862014-04-15 04:59:12 +000010763 FirstUnknown = nullptr;
Dan Gohman7cac9572010-08-02 23:49:30 +000010764
Wei Mia49559b2016-02-04 01:27:38 +000010765 ExprValueMap.clear();
Dan Gohman9bad2fb2010-08-27 18:55:03 +000010766 ValueExprMap.clear();
Wei Mia49559b2016-02-04 01:27:38 +000010767 HasRecMap.clear();
Andrew Trick3ca3f982011-07-26 17:19:55 +000010768
10769 // Free any extra memory created for ExitNotTakenInfo in the unlikely event
10770 // that a loop had multiple computable exits.
Sanjoy Dasd9f6d332015-10-18 00:29:16 +000010771 for (auto &BTCI : BackedgeTakenCounts)
10772 BTCI.second.clear();
Silviu Baranga6f444df2016-04-08 14:29:09 +000010773 for (auto &BTCI : PredicatedBackedgeTakenCounts)
10774 BTCI.second.clear();
Andrew Trick3ca3f982011-07-26 17:19:55 +000010775
Andrew Trick7fa4e0f2012-05-19 00:48:25 +000010776 assert(PendingLoopPredicates.empty() && "isImpliedCond garbage");
Sanjoy Dasb864c1f2015-04-01 18:24:06 +000010777 assert(!WalkingBEDominatingConds && "isLoopBackedgeGuardedByCond garbage!");
Sanjoy Das7d910f22015-10-02 18:50:30 +000010778 assert(!ProvingSplitPredicate && "ProvingSplitPredicate garbage!");
Dan Gohman0a40ad92009-04-16 03:18:22 +000010779}
10780
Dan Gohmanc8e23622009-04-21 23:15:49 +000010781bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) {
Dan Gohman0bddac12009-02-24 18:55:53 +000010782 return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L));
Chris Lattnerd934c702004-04-02 20:23:17 +000010783}
10784
Dan Gohmanc8e23622009-04-21 23:15:49 +000010785static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE,
Chris Lattnerd934c702004-04-02 20:23:17 +000010786 const Loop *L) {
10787 // Print all inner loops first
Benjamin Krameraa209152016-06-26 17:27:42 +000010788 for (Loop *I : *L)
10789 PrintLoopInfo(OS, SE, I);
Misha Brukman01808ca2005-04-21 21:13:18 +000010790
Dan Gohmanbc694912010-01-09 18:17:45 +000010791 OS << "Loop ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +000010792 L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
Dan Gohmanbc694912010-01-09 18:17:45 +000010793 OS << ": ";
Chris Lattnerd72c3eb2004-04-18 22:14:10 +000010794
Dan Gohmancb0efec2009-12-18 01:14:11 +000010795 SmallVector<BasicBlock *, 8> ExitBlocks;
Chris Lattnerd72c3eb2004-04-18 22:14:10 +000010796 L->getExitBlocks(ExitBlocks);
10797 if (ExitBlocks.size() != 1)
Nick Lewyckyd1200b02008-01-02 02:49:20 +000010798 OS << "<multiple exits> ";
Chris Lattnerd934c702004-04-02 20:23:17 +000010799
Dan Gohman0bddac12009-02-24 18:55:53 +000010800 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
10801 OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L);
Chris Lattnerd934c702004-04-02 20:23:17 +000010802 } else {
Dan Gohman0bddac12009-02-24 18:55:53 +000010803 OS << "Unpredictable backedge-taken count. ";
Chris Lattnerd934c702004-04-02 20:23:17 +000010804 }
10805
Dan Gohmanbc694912010-01-09 18:17:45 +000010806 OS << "\n"
10807 "Loop ";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +000010808 L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
Dan Gohmanbc694912010-01-09 18:17:45 +000010809 OS << ": ";
Dan Gohman69942932009-06-24 00:33:16 +000010810
10811 if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) {
10812 OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L);
John Brawn84b21832016-10-21 11:08:48 +000010813 if (SE->isBackedgeTakenCountMaxOrZero(L))
10814 OS << ", actual taken count either this or zero.";
Dan Gohman69942932009-06-24 00:33:16 +000010815 } else {
10816 OS << "Unpredictable max backedge-taken count. ";
10817 }
10818
Silviu Baranga6f444df2016-04-08 14:29:09 +000010819 OS << "\n"
10820 "Loop ";
10821 L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
10822 OS << ": ";
10823
10824 SCEVUnionPredicate Pred;
10825 auto PBT = SE->getPredicatedBackedgeTakenCount(L, Pred);
10826 if (!isa<SCEVCouldNotCompute>(PBT)) {
10827 OS << "Predicated backedge-taken count is " << *PBT << "\n";
10828 OS << " Predicates:\n";
10829 Pred.print(OS, 4);
10830 } else {
10831 OS << "Unpredictable predicated backedge-taken count. ";
10832 }
Dan Gohman69942932009-06-24 00:33:16 +000010833 OS << "\n";
Eli Friedmanb1578d32017-03-20 20:25:46 +000010834
10835 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
10836 OS << "Loop ";
10837 L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
10838 OS << ": ";
10839 OS << "Trip multiple is " << SE->getSmallConstantTripMultiple(L) << "\n";
10840 }
Chris Lattnerd934c702004-04-02 20:23:17 +000010841}
10842
Sanjoy Dasf2f00fb12016-05-01 04:51:05 +000010843static StringRef loopDispositionToStr(ScalarEvolution::LoopDisposition LD) {
10844 switch (LD) {
10845 case ScalarEvolution::LoopVariant:
10846 return "Variant";
10847 case ScalarEvolution::LoopInvariant:
10848 return "Invariant";
10849 case ScalarEvolution::LoopComputable:
10850 return "Computable";
10851 }
Simon Pilgrim33ae13d2016-05-01 15:52:31 +000010852 llvm_unreachable("Unknown ScalarEvolution::LoopDisposition kind!");
Sanjoy Dasf2f00fb12016-05-01 04:51:05 +000010853}
10854
Chandler Carruth2f1fd162015-08-17 02:08:17 +000010855void ScalarEvolution::print(raw_ostream &OS) const {
Dan Gohman8b0a4192010-03-01 17:49:51 +000010856 // ScalarEvolution's implementation of the print method is to print
Dan Gohmanc8e23622009-04-21 23:15:49 +000010857 // out SCEV values of all instructions that are interesting. Doing
10858 // this potentially causes it to create new SCEV objects though,
10859 // which technically conflicts with the const qualifier. This isn't
Dan Gohman028e6152009-07-10 20:25:29 +000010860 // observable from outside the class though, so casting away the
10861 // const isn't dangerous.
Dan Gohmancb0efec2009-12-18 01:14:11 +000010862 ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
Chris Lattnerd934c702004-04-02 20:23:17 +000010863
Dan Gohmanbc694912010-01-09 18:17:45 +000010864 OS << "Classifying expressions for: ";
Chandler Carruth2f1fd162015-08-17 02:08:17 +000010865 F.printAsOperand(OS, /*PrintType=*/false);
Dan Gohmanbc694912010-01-09 18:17:45 +000010866 OS << "\n";
Sanjoy Dasd9f6d332015-10-18 00:29:16 +000010867 for (Instruction &I : instructions(F))
10868 if (isSCEVable(I.getType()) && !isa<CmpInst>(I)) {
10869 OS << I << '\n';
Dan Gohman81313fd2008-09-14 17:21:12 +000010870 OS << " --> ";
Sanjoy Dasd9f6d332015-10-18 00:29:16 +000010871 const SCEV *SV = SE.getSCEV(&I);
Chris Lattnerd934c702004-04-02 20:23:17 +000010872 SV->print(OS);
Sanjoy Dasf2574522015-03-09 21:43:39 +000010873 if (!isa<SCEVCouldNotCompute>(SV)) {
10874 OS << " U: ";
10875 SE.getUnsignedRange(SV).print(OS);
10876 OS << " S: ";
10877 SE.getSignedRange(SV).print(OS);
10878 }
Misha Brukman01808ca2005-04-21 21:13:18 +000010879
Sanjoy Dasd9f6d332015-10-18 00:29:16 +000010880 const Loop *L = LI.getLoopFor(I.getParent());
Dan Gohmanb9063a82009-06-19 17:49:54 +000010881
Dan Gohmanaf752342009-07-07 17:06:11 +000010882 const SCEV *AtUse = SE.getSCEVAtScope(SV, L);
Dan Gohmanb9063a82009-06-19 17:49:54 +000010883 if (AtUse != SV) {
10884 OS << " --> ";
10885 AtUse->print(OS);
Sanjoy Dasf2574522015-03-09 21:43:39 +000010886 if (!isa<SCEVCouldNotCompute>(AtUse)) {
10887 OS << " U: ";
10888 SE.getUnsignedRange(AtUse).print(OS);
10889 OS << " S: ";
10890 SE.getSignedRange(AtUse).print(OS);
10891 }
Dan Gohmanb9063a82009-06-19 17:49:54 +000010892 }
10893
10894 if (L) {
Dan Gohman94c468f2009-06-18 00:37:45 +000010895 OS << "\t\t" "Exits: ";
Dan Gohmanaf752342009-07-07 17:06:11 +000010896 const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop());
Dan Gohmanafd6db92010-11-17 21:23:15 +000010897 if (!SE.isLoopInvariant(ExitValue, L)) {
Chris Lattnerd934c702004-04-02 20:23:17 +000010898 OS << "<<Unknown>>";
10899 } else {
10900 OS << *ExitValue;
10901 }
Sanjoy Dasf2f00fb12016-05-01 04:51:05 +000010902
10903 bool First = true;
10904 for (auto *Iter = L; Iter; Iter = Iter->getParentLoop()) {
10905 if (First) {
Sanjoy Das013a4ac2016-05-03 17:49:57 +000010906 OS << "\t\t" "LoopDispositions: { ";
Sanjoy Dasf2f00fb12016-05-01 04:51:05 +000010907 First = false;
10908 } else {
10909 OS << ", ";
10910 }
10911
Sanjoy Das013a4ac2016-05-03 17:49:57 +000010912 Iter->getHeader()->printAsOperand(OS, /*PrintType=*/false);
10913 OS << ": " << loopDispositionToStr(SE.getLoopDisposition(SV, Iter));
Sanjoy Dasf2f00fb12016-05-01 04:51:05 +000010914 }
10915
Sanjoy Das013a4ac2016-05-03 17:49:57 +000010916 for (auto *InnerL : depth_first(L)) {
10917 if (InnerL == L)
10918 continue;
10919 if (First) {
10920 OS << "\t\t" "LoopDispositions: { ";
10921 First = false;
10922 } else {
10923 OS << ", ";
10924 }
10925
10926 InnerL->getHeader()->printAsOperand(OS, /*PrintType=*/false);
10927 OS << ": " << loopDispositionToStr(SE.getLoopDisposition(SV, InnerL));
10928 }
10929
10930 OS << " }";
Chris Lattnerd934c702004-04-02 20:23:17 +000010931 }
10932
Chris Lattnerd934c702004-04-02 20:23:17 +000010933 OS << "\n";
10934 }
10935
Dan Gohmanbc694912010-01-09 18:17:45 +000010936 OS << "Determining loop execution counts for: ";
Chandler Carruth2f1fd162015-08-17 02:08:17 +000010937 F.printAsOperand(OS, /*PrintType=*/false);
Dan Gohmanbc694912010-01-09 18:17:45 +000010938 OS << "\n";
Benjamin Krameraa209152016-06-26 17:27:42 +000010939 for (Loop *I : LI)
10940 PrintLoopInfo(OS, &SE, I);
Chris Lattnerd934c702004-04-02 20:23:17 +000010941}
Dan Gohmane20f8242009-04-21 00:47:46 +000010942
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000010943ScalarEvolution::LoopDisposition
10944ScalarEvolution::getLoopDisposition(const SCEV *S, const Loop *L) {
Benjamin Kramerd7e331e2015-02-07 16:41:12 +000010945 auto &Values = LoopDispositions[S];
10946 for (auto &V : Values) {
10947 if (V.getPointer() == L)
10948 return V.getInt();
Wan Xiaofeib2c8cdc2013-11-12 09:40:41 +000010949 }
Benjamin Kramerd7e331e2015-02-07 16:41:12 +000010950 Values.emplace_back(L, LoopVariant);
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000010951 LoopDisposition D = computeLoopDisposition(S, L);
Benjamin Kramerd7e331e2015-02-07 16:41:12 +000010952 auto &Values2 = LoopDispositions[S];
10953 for (auto &V : make_range(Values2.rbegin(), Values2.rend())) {
10954 if (V.getPointer() == L) {
10955 V.setInt(D);
Wan Xiaofeib2c8cdc2013-11-12 09:40:41 +000010956 break;
10957 }
10958 }
10959 return D;
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000010960}
10961
10962ScalarEvolution::LoopDisposition
10963ScalarEvolution::computeLoopDisposition(const SCEV *S, const Loop *L) {
Benjamin Kramer987b8502014-02-11 19:02:55 +000010964 switch (static_cast<SCEVTypes>(S->getSCEVType())) {
Dan Gohmanafd6db92010-11-17 21:23:15 +000010965 case scConstant:
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000010966 return LoopInvariant;
Dan Gohmanafd6db92010-11-17 21:23:15 +000010967 case scTruncate:
10968 case scZeroExtend:
10969 case scSignExtend:
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000010970 return getLoopDisposition(cast<SCEVCastExpr>(S)->getOperand(), L);
Dan Gohmanafd6db92010-11-17 21:23:15 +000010971 case scAddRecExpr: {
10972 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S);
10973
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000010974 // If L is the addrec's loop, it's computable.
10975 if (AR->getLoop() == L)
10976 return LoopComputable;
10977
Dan Gohmanafd6db92010-11-17 21:23:15 +000010978 // Add recurrences are never invariant in the function-body (null loop).
10979 if (!L)
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000010980 return LoopVariant;
Dan Gohmanafd6db92010-11-17 21:23:15 +000010981
Max Kazantsev23044fa2017-11-22 06:21:39 +000010982 // Everything that is not defined at loop entry is variant.
10983 if (DT.dominates(L->getHeader(), AR->getLoop()->getHeader()))
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000010984 return LoopVariant;
Max Kazantsev23044fa2017-11-22 06:21:39 +000010985 assert(!L->contains(AR->getLoop()) && "Containing loop's header does not"
10986 " dominate the contained loop's header?");
Dan Gohmanafd6db92010-11-17 21:23:15 +000010987
10988 // This recurrence is invariant w.r.t. L if AR's loop contains L.
10989 if (AR->getLoop()->contains(L))
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000010990 return LoopInvariant;
Dan Gohmanafd6db92010-11-17 21:23:15 +000010991
10992 // This recurrence is variant w.r.t. L if any of its operands
10993 // are variant.
Sanjoy Das01947432015-11-22 21:20:13 +000010994 for (auto *Op : AR->operands())
10995 if (!isLoopInvariant(Op, L))
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000010996 return LoopVariant;
Dan Gohmanafd6db92010-11-17 21:23:15 +000010997
10998 // Otherwise it's loop-invariant.
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000010999 return LoopInvariant;
Dan Gohmanafd6db92010-11-17 21:23:15 +000011000 }
11001 case scAddExpr:
11002 case scMulExpr:
11003 case scUMaxExpr:
11004 case scSMaxExpr: {
Dan Gohmanafd6db92010-11-17 21:23:15 +000011005 bool HasVarying = false;
Sanjoy Das01947432015-11-22 21:20:13 +000011006 for (auto *Op : cast<SCEVNAryExpr>(S)->operands()) {
11007 LoopDisposition D = getLoopDisposition(Op, L);
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000011008 if (D == LoopVariant)
11009 return LoopVariant;
11010 if (D == LoopComputable)
11011 HasVarying = true;
Dan Gohmanafd6db92010-11-17 21:23:15 +000011012 }
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000011013 return HasVarying ? LoopComputable : LoopInvariant;
Dan Gohmanafd6db92010-11-17 21:23:15 +000011014 }
11015 case scUDivExpr: {
11016 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000011017 LoopDisposition LD = getLoopDisposition(UDiv->getLHS(), L);
11018 if (LD == LoopVariant)
11019 return LoopVariant;
11020 LoopDisposition RD = getLoopDisposition(UDiv->getRHS(), L);
11021 if (RD == LoopVariant)
11022 return LoopVariant;
11023 return (LD == LoopInvariant && RD == LoopInvariant) ?
11024 LoopInvariant : LoopComputable;
Dan Gohmanafd6db92010-11-17 21:23:15 +000011025 }
11026 case scUnknown:
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000011027 // All non-instruction values are loop invariant. All instructions are loop
11028 // invariant if they are not contained in the specified loop.
11029 // Instructions are never considered invariant in the function body
11030 // (null loop) because they are defined within the "loop".
Sanjoy Das01947432015-11-22 21:20:13 +000011031 if (auto *I = dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue()))
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000011032 return (L && !L->contains(I)) ? LoopInvariant : LoopVariant;
11033 return LoopInvariant;
Dan Gohmanafd6db92010-11-17 21:23:15 +000011034 case scCouldNotCompute:
11035 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
Dan Gohmanafd6db92010-11-17 21:23:15 +000011036 }
Benjamin Kramer987b8502014-02-11 19:02:55 +000011037 llvm_unreachable("Unknown SCEV kind!");
Dan Gohman7ee1bbb2010-11-17 23:21:44 +000011038}
11039
11040bool ScalarEvolution::isLoopInvariant(const SCEV *S, const Loop *L) {
11041 return getLoopDisposition(S, L) == LoopInvariant;
11042}
11043
11044bool ScalarEvolution::hasComputableLoopEvolution(const SCEV *S, const Loop *L) {
11045 return getLoopDisposition(S, L) == LoopComputable;
Dan Gohmanafd6db92010-11-17 21:23:15 +000011046}
Dan Gohman20d9ce22010-11-17 21:41:58 +000011047
Dan Gohman8ea83d82010-11-18 00:34:22 +000011048ScalarEvolution::BlockDisposition
11049ScalarEvolution::getBlockDisposition(const SCEV *S, const BasicBlock *BB) {
Benjamin Kramerd7e331e2015-02-07 16:41:12 +000011050 auto &Values = BlockDispositions[S];
11051 for (auto &V : Values) {
11052 if (V.getPointer() == BB)
11053 return V.getInt();
Wan Xiaofeib2c8cdc2013-11-12 09:40:41 +000011054 }
Benjamin Kramerd7e331e2015-02-07 16:41:12 +000011055 Values.emplace_back(BB, DoesNotDominateBlock);
Dan Gohman8ea83d82010-11-18 00:34:22 +000011056 BlockDisposition D = computeBlockDisposition(S, BB);
Benjamin Kramerd7e331e2015-02-07 16:41:12 +000011057 auto &Values2 = BlockDispositions[S];
11058 for (auto &V : make_range(Values2.rbegin(), Values2.rend())) {
11059 if (V.getPointer() == BB) {
11060 V.setInt(D);
Wan Xiaofeib2c8cdc2013-11-12 09:40:41 +000011061 break;
11062 }
11063 }
11064 return D;
Dan Gohman20d9ce22010-11-17 21:41:58 +000011065}
11066
Dan Gohman8ea83d82010-11-18 00:34:22 +000011067ScalarEvolution::BlockDisposition
11068ScalarEvolution::computeBlockDisposition(const SCEV *S, const BasicBlock *BB) {
Benjamin Kramer987b8502014-02-11 19:02:55 +000011069 switch (static_cast<SCEVTypes>(S->getSCEVType())) {
Dan Gohman20d9ce22010-11-17 21:41:58 +000011070 case scConstant:
Dan Gohman8ea83d82010-11-18 00:34:22 +000011071 return ProperlyDominatesBlock;
Dan Gohman20d9ce22010-11-17 21:41:58 +000011072 case scTruncate:
11073 case scZeroExtend:
11074 case scSignExtend:
Dan Gohman8ea83d82010-11-18 00:34:22 +000011075 return getBlockDisposition(cast<SCEVCastExpr>(S)->getOperand(), BB);
Dan Gohman20d9ce22010-11-17 21:41:58 +000011076 case scAddRecExpr: {
11077 // This uses a "dominates" query instead of "properly dominates" query
Dan Gohman8ea83d82010-11-18 00:34:22 +000011078 // to test for proper dominance too, because the instruction which
11079 // produces the addrec's value is a PHI, and a PHI effectively properly
11080 // dominates its entire containing block.
Dan Gohman20d9ce22010-11-17 21:41:58 +000011081 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S);
Chandler Carruth2f1fd162015-08-17 02:08:17 +000011082 if (!DT.dominates(AR->getLoop()->getHeader(), BB))
Dan Gohman8ea83d82010-11-18 00:34:22 +000011083 return DoesNotDominateBlock;
Justin Bognercd1d5aa2016-08-17 20:30:52 +000011084
11085 // Fall through into SCEVNAryExpr handling.
11086 LLVM_FALLTHROUGH;
Dan Gohman20d9ce22010-11-17 21:41:58 +000011087 }
Dan Gohman20d9ce22010-11-17 21:41:58 +000011088 case scAddExpr:
11089 case scMulExpr:
11090 case scUMaxExpr:
11091 case scSMaxExpr: {
11092 const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S);
Dan Gohman8ea83d82010-11-18 00:34:22 +000011093 bool Proper = true;
Sanjoy Dasd87e4352015-12-08 22:53:36 +000011094 for (const SCEV *NAryOp : NAry->operands()) {
11095 BlockDisposition D = getBlockDisposition(NAryOp, BB);
Dan Gohman8ea83d82010-11-18 00:34:22 +000011096 if (D == DoesNotDominateBlock)
11097 return DoesNotDominateBlock;
11098 if (D == DominatesBlock)
11099 Proper = false;
11100 }
11101 return Proper ? ProperlyDominatesBlock : DominatesBlock;
Dan Gohman20d9ce22010-11-17 21:41:58 +000011102 }
11103 case scUDivExpr: {
11104 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
Dan Gohman8ea83d82010-11-18 00:34:22 +000011105 const SCEV *LHS = UDiv->getLHS(), *RHS = UDiv->getRHS();
11106 BlockDisposition LD = getBlockDisposition(LHS, BB);
11107 if (LD == DoesNotDominateBlock)
11108 return DoesNotDominateBlock;
11109 BlockDisposition RD = getBlockDisposition(RHS, BB);
11110 if (RD == DoesNotDominateBlock)
11111 return DoesNotDominateBlock;
11112 return (LD == ProperlyDominatesBlock && RD == ProperlyDominatesBlock) ?
11113 ProperlyDominatesBlock : DominatesBlock;
Dan Gohman20d9ce22010-11-17 21:41:58 +000011114 }
11115 case scUnknown:
11116 if (Instruction *I =
Dan Gohman8ea83d82010-11-18 00:34:22 +000011117 dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) {
11118 if (I->getParent() == BB)
11119 return DominatesBlock;
Chandler Carruth2f1fd162015-08-17 02:08:17 +000011120 if (DT.properlyDominates(I->getParent(), BB))
Dan Gohman8ea83d82010-11-18 00:34:22 +000011121 return ProperlyDominatesBlock;
11122 return DoesNotDominateBlock;
11123 }
11124 return ProperlyDominatesBlock;
Dan Gohman20d9ce22010-11-17 21:41:58 +000011125 case scCouldNotCompute:
11126 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
Dan Gohman20d9ce22010-11-17 21:41:58 +000011127 }
Benjamin Kramer987b8502014-02-11 19:02:55 +000011128 llvm_unreachable("Unknown SCEV kind!");
Dan Gohman8ea83d82010-11-18 00:34:22 +000011129}
11130
11131bool ScalarEvolution::dominates(const SCEV *S, const BasicBlock *BB) {
11132 return getBlockDisposition(S, BB) >= DominatesBlock;
11133}
11134
11135bool ScalarEvolution::properlyDominates(const SCEV *S, const BasicBlock *BB) {
11136 return getBlockDisposition(S, BB) == ProperlyDominatesBlock;
Dan Gohman20d9ce22010-11-17 21:41:58 +000011137}
Dan Gohman534749b2010-11-17 22:27:42 +000011138
11139bool ScalarEvolution::hasOperand(const SCEV *S, const SCEV *Op) const {
Sanjoy Das6b46a0d2016-11-09 18:22:43 +000011140 return SCEVExprContains(S, [&](const SCEV *Expr) { return Expr == Op; });
Dan Gohman534749b2010-11-17 22:27:42 +000011141}
Dan Gohman7e6b3932010-11-17 23:28:48 +000011142
Max Kazantsev2cb36532017-08-03 08:41:30 +000011143bool ScalarEvolution::ExitLimit::hasOperand(const SCEV *S) const {
11144 auto IsS = [&](const SCEV *X) { return S == X; };
11145 auto ContainsS = [&](const SCEV *X) {
11146 return !isa<SCEVCouldNotCompute>(X) && SCEVExprContains(X, IsS);
11147 };
11148 return ContainsS(ExactNotTaken) || ContainsS(MaxNotTaken);
11149}
11150
11151void
Sanjoy Das7e363372017-12-04 19:22:00 +000011152ScalarEvolution::forgetMemoizedResults(const SCEV *S) {
Dan Gohman7e6b3932010-11-17 23:28:48 +000011153 ValuesAtScopes.erase(S);
11154 LoopDispositions.erase(S);
Dan Gohman8ea83d82010-11-18 00:34:22 +000011155 BlockDispositions.erase(S);
Dan Gohman7e6b3932010-11-17 23:28:48 +000011156 UnsignedRanges.erase(S);
11157 SignedRanges.erase(S);
Wei Mia49559b2016-02-04 01:27:38 +000011158 ExprValueMap.erase(S);
11159 HasRecMap.erase(S);
Igor Laevskyc11c1ed2017-02-14 15:53:12 +000011160 MinTrailingZerosCache.erase(S);
Andrew Trick9093e152013-03-26 03:14:53 +000011161
Michael Liaob30286d2017-09-25 16:21:21 +000011162 for (auto I = PredicatedSCEVRewrites.begin();
Dorit Nuzmanca4fd182017-07-18 11:57:08 +000011163 I != PredicatedSCEVRewrites.end();) {
11164 std::pair<const SCEV *, const Loop *> Entry = I->first;
11165 if (Entry.first == S)
11166 PredicatedSCEVRewrites.erase(I++);
11167 else
11168 ++I;
11169 }
11170
Sanjoy Das3a5e2522017-10-17 01:03:56 +000011171 auto RemoveSCEVFromBackedgeMap =
11172 [S, this](DenseMap<const Loop *, BackedgeTakenInfo> &Map) {
11173 for (auto I = Map.begin(), E = Map.end(); I != E;) {
11174 BackedgeTakenInfo &BEInfo = I->second;
11175 if (BEInfo.hasOperand(S, this)) {
11176 BEInfo.clear();
11177 Map.erase(I++);
11178 } else
11179 ++I;
11180 }
11181 };
11182
11183 RemoveSCEVFromBackedgeMap(BackedgeTakenCounts);
11184 RemoveSCEVFromBackedgeMap(PredicatedBackedgeTakenCounts);
Dan Gohman7e6b3932010-11-17 23:28:48 +000011185}
Benjamin Kramer214935e2012-10-26 17:31:32 +000011186
Sanjoy Dase6b995f2017-10-13 05:50:52 +000011187void ScalarEvolution::addToLoopUseLists(const SCEV *S) {
Sanjoy Das3a5e2522017-10-17 01:03:56 +000011188 struct FindUsedLoops {
11189 SmallPtrSet<const Loop *, 8> LoopsUsed;
11190 bool follow(const SCEV *S) {
11191 if (auto *AR = dyn_cast<SCEVAddRecExpr>(S))
11192 LoopsUsed.insert(AR->getLoop());
11193 return true;
11194 }
Sanjoy Dase6b995f2017-10-13 05:50:52 +000011195
Sanjoy Das3a5e2522017-10-17 01:03:56 +000011196 bool isDone() const { return false; }
11197 };
Sanjoy Dase6b995f2017-10-13 05:50:52 +000011198
Sanjoy Das3a5e2522017-10-17 01:03:56 +000011199 FindUsedLoops F;
11200 SCEVTraversal<FindUsedLoops>(F).visitAll(S);
11201
11202 for (auto *L : F.LoopsUsed)
11203 LoopUsers[L].push_back(S);
Sanjoy Dase6b995f2017-10-13 05:50:52 +000011204}
11205
Chandler Carruth2f1fd162015-08-17 02:08:17 +000011206void ScalarEvolution::verify() const {
Benjamin Kramer214935e2012-10-26 17:31:32 +000011207 ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
Daniel Jasperaec2fa32016-12-19 08:22:17 +000011208 ScalarEvolution SE2(F, TLI, AC, DT, LI);
Benjamin Kramer214935e2012-10-26 17:31:32 +000011209
Sanjoy Das148e49f2017-04-23 23:04:45 +000011210 SmallVector<Loop *, 8> LoopStack(LI.begin(), LI.end());
Benjamin Kramer214935e2012-10-26 17:31:32 +000011211
Sanjoy Das148e49f2017-04-23 23:04:45 +000011212 // Map's SCEV expressions from one ScalarEvolution "universe" to another.
11213 struct SCEVMapper : public SCEVRewriteVisitor<SCEVMapper> {
Eugene Zelenkobe709f22017-08-18 23:51:26 +000011214 SCEVMapper(ScalarEvolution &SE) : SCEVRewriteVisitor<SCEVMapper>(SE) {}
11215
Sanjoy Das148e49f2017-04-23 23:04:45 +000011216 const SCEV *visitConstant(const SCEVConstant *Constant) {
11217 return SE.getConstant(Constant->getAPInt());
11218 }
Eugene Zelenkobe709f22017-08-18 23:51:26 +000011219
Sanjoy Das148e49f2017-04-23 23:04:45 +000011220 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
11221 return SE.getUnknown(Expr->getValue());
11222 }
Benjamin Kramer214935e2012-10-26 17:31:32 +000011223
Sanjoy Das148e49f2017-04-23 23:04:45 +000011224 const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) {
11225 return SE.getCouldNotCompute();
11226 }
Sanjoy Das148e49f2017-04-23 23:04:45 +000011227 };
11228
11229 SCEVMapper SCM(SE2);
11230
11231 while (!LoopStack.empty()) {
11232 auto *L = LoopStack.pop_back_val();
11233 LoopStack.insert(LoopStack.end(), L->begin(), L->end());
11234
11235 auto *CurBECount = SCM.visit(
11236 const_cast<ScalarEvolution *>(this)->getBackedgeTakenCount(L));
11237 auto *NewBECount = SE2.getBackedgeTakenCount(L);
11238
11239 if (CurBECount == SE2.getCouldNotCompute() ||
11240 NewBECount == SE2.getCouldNotCompute()) {
11241 // NB! This situation is legal, but is very suspicious -- whatever pass
11242 // change the loop to make a trip count go from could not compute to
11243 // computable or vice-versa *should have* invalidated SCEV. However, we
11244 // choose not to assert here (for now) since we don't want false
11245 // positives.
11246 continue;
11247 }
11248
11249 if (containsUndefs(CurBECount) || containsUndefs(NewBECount)) {
11250 // SCEV treats "undef" as an unknown but consistent value (i.e. it does
11251 // not propagate undef aggressively). This means we can (and do) fail
11252 // verification in cases where a transform makes the trip count of a loop
11253 // go from "undef" to "undef+1" (say). The transform is fine, since in
11254 // both cases the loop iterates "undef" times, but SCEV thinks we
11255 // increased the trip count of the loop by 1 incorrectly.
11256 continue;
11257 }
11258
11259 if (SE.getTypeSizeInBits(CurBECount->getType()) >
11260 SE.getTypeSizeInBits(NewBECount->getType()))
11261 NewBECount = SE2.getZeroExtendExpr(NewBECount, CurBECount->getType());
11262 else if (SE.getTypeSizeInBits(CurBECount->getType()) <
11263 SE.getTypeSizeInBits(NewBECount->getType()))
11264 CurBECount = SE2.getZeroExtendExpr(CurBECount, NewBECount->getType());
11265
11266 auto *ConstantDelta =
11267 dyn_cast<SCEVConstant>(SE2.getMinusSCEV(CurBECount, NewBECount));
11268
11269 if (ConstantDelta && ConstantDelta->getAPInt() != 0) {
11270 dbgs() << "Trip Count Changed!\n";
11271 dbgs() << "Old: " << *CurBECount << "\n";
11272 dbgs() << "New: " << *NewBECount << "\n";
11273 dbgs() << "Delta: " << *ConstantDelta << "\n";
Benjamin Kramer214935e2012-10-26 17:31:32 +000011274 std::abort();
11275 }
11276 }
Benjamin Kramer214935e2012-10-26 17:31:32 +000011277}
Chandler Carruth2f1fd162015-08-17 02:08:17 +000011278
Chandler Carruth082c1832017-01-09 07:44:34 +000011279bool ScalarEvolution::invalidate(
11280 Function &F, const PreservedAnalyses &PA,
11281 FunctionAnalysisManager::Invalidator &Inv) {
11282 // Invalidate the ScalarEvolution object whenever it isn't preserved or one
11283 // of its dependencies is invalidated.
11284 auto PAC = PA.getChecker<ScalarEvolutionAnalysis>();
11285 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
11286 Inv.invalidate<AssumptionAnalysis>(F, PA) ||
11287 Inv.invalidate<DominatorTreeAnalysis>(F, PA) ||
11288 Inv.invalidate<LoopAnalysis>(F, PA);
11289}
11290
Chandler Carruthdab4eae2016-11-23 17:53:26 +000011291AnalysisKey ScalarEvolutionAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +000011292
Chandler Carruth2f1fd162015-08-17 02:08:17 +000011293ScalarEvolution ScalarEvolutionAnalysis::run(Function &F,
Sean Silva36e0d012016-08-09 00:28:15 +000011294 FunctionAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +000011295 return ScalarEvolution(F, AM.getResult<TargetLibraryAnalysis>(F),
Daniel Jasperaec2fa32016-12-19 08:22:17 +000011296 AM.getResult<AssumptionAnalysis>(F),
Chandler Carruthb47f8012016-03-11 11:05:24 +000011297 AM.getResult<DominatorTreeAnalysis>(F),
11298 AM.getResult<LoopAnalysis>(F));
Chandler Carruth2f1fd162015-08-17 02:08:17 +000011299}
11300
11301PreservedAnalyses
Sean Silva36e0d012016-08-09 00:28:15 +000011302ScalarEvolutionPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
Chandler Carruthb47f8012016-03-11 11:05:24 +000011303 AM.getResult<ScalarEvolutionAnalysis>(F).print(OS);
Chandler Carruth2f1fd162015-08-17 02:08:17 +000011304 return PreservedAnalyses::all();
11305}
11306
11307INITIALIZE_PASS_BEGIN(ScalarEvolutionWrapperPass, "scalar-evolution",
11308 "Scalar Evolution Analysis", false, true)
Daniel Jasperaec2fa32016-12-19 08:22:17 +000011309INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth2f1fd162015-08-17 02:08:17 +000011310INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
11311INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
11312INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
11313INITIALIZE_PASS_END(ScalarEvolutionWrapperPass, "scalar-evolution",
11314 "Scalar Evolution Analysis", false, true)
Eugene Zelenkobe709f22017-08-18 23:51:26 +000011315
Chandler Carruth2f1fd162015-08-17 02:08:17 +000011316char ScalarEvolutionWrapperPass::ID = 0;
11317
11318ScalarEvolutionWrapperPass::ScalarEvolutionWrapperPass() : FunctionPass(ID) {
11319 initializeScalarEvolutionWrapperPassPass(*PassRegistry::getPassRegistry());
11320}
11321
11322bool ScalarEvolutionWrapperPass::runOnFunction(Function &F) {
11323 SE.reset(new ScalarEvolution(
11324 F, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(),
Daniel Jasperaec2fa32016-12-19 08:22:17 +000011325 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
Chandler Carruth2f1fd162015-08-17 02:08:17 +000011326 getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
11327 getAnalysis<LoopInfoWrapperPass>().getLoopInfo()));
11328 return false;
11329}
11330
11331void ScalarEvolutionWrapperPass::releaseMemory() { SE.reset(); }
11332
11333void ScalarEvolutionWrapperPass::print(raw_ostream &OS, const Module *) const {
11334 SE->print(OS);
11335}
11336
11337void ScalarEvolutionWrapperPass::verifyAnalysis() const {
11338 if (!VerifySCEV)
11339 return;
11340
11341 SE->verify();
11342}
11343
11344void ScalarEvolutionWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
11345 AU.setPreservesAll();
Daniel Jasperaec2fa32016-12-19 08:22:17 +000011346 AU.addRequiredTransitive<AssumptionCacheTracker>();
Chandler Carruth2f1fd162015-08-17 02:08:17 +000011347 AU.addRequiredTransitive<LoopInfoWrapperPass>();
11348 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
11349 AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
11350}
Silviu Barangae3c05342015-11-02 14:41:02 +000011351
Dorit Nuzmanca4fd182017-07-18 11:57:08 +000011352const SCEVPredicate *ScalarEvolution::getEqualPredicate(const SCEV *LHS,
11353 const SCEV *RHS) {
Silviu Barangae3c05342015-11-02 14:41:02 +000011354 FoldingSetNodeID ID;
Dorit Nuzmanca4fd182017-07-18 11:57:08 +000011355 assert(LHS->getType() == RHS->getType() &&
11356 "Type mismatch between LHS and RHS");
Silviu Barangae3c05342015-11-02 14:41:02 +000011357 // Unique this node based on the arguments
11358 ID.AddInteger(SCEVPredicate::P_Equal);
11359 ID.AddPointer(LHS);
11360 ID.AddPointer(RHS);
11361 void *IP = nullptr;
11362 if (const auto *S = UniquePreds.FindNodeOrInsertPos(ID, IP))
11363 return S;
11364 SCEVEqualPredicate *Eq = new (SCEVAllocator)
11365 SCEVEqualPredicate(ID.Intern(SCEVAllocator), LHS, RHS);
11366 UniquePreds.InsertNode(Eq, IP);
11367 return Eq;
11368}
11369
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011370const SCEVPredicate *ScalarEvolution::getWrapPredicate(
11371 const SCEVAddRecExpr *AR,
11372 SCEVWrapPredicate::IncrementWrapFlags AddedFlags) {
11373 FoldingSetNodeID ID;
11374 // Unique this node based on the arguments
11375 ID.AddInteger(SCEVPredicate::P_Wrap);
11376 ID.AddPointer(AR);
11377 ID.AddInteger(AddedFlags);
11378 void *IP = nullptr;
11379 if (const auto *S = UniquePreds.FindNodeOrInsertPos(ID, IP))
11380 return S;
11381 auto *OF = new (SCEVAllocator)
11382 SCEVWrapPredicate(ID.Intern(SCEVAllocator), AR, AddedFlags);
11383 UniquePreds.InsertNode(OF, IP);
11384 return OF;
11385}
11386
Benjamin Kramer83709b12015-11-16 09:01:28 +000011387namespace {
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011388
Silviu Barangae3c05342015-11-02 14:41:02 +000011389class SCEVPredicateRewriter : public SCEVRewriteVisitor<SCEVPredicateRewriter> {
11390public:
Eugene Zelenkobe709f22017-08-18 23:51:26 +000011391
Sanjoy Dasf0022122016-09-28 17:14:58 +000011392 /// Rewrites \p S in the context of a loop L and the SCEV predication
11393 /// infrastructure.
11394 ///
11395 /// If \p Pred is non-null, the SCEV expression is rewritten to respect the
11396 /// equivalences present in \p Pred.
11397 ///
11398 /// If \p NewPreds is non-null, rewrite is free to add further predicates to
11399 /// \p NewPreds such that the result will be an AddRecExpr.
Sanjoy Das807d33d2016-02-20 01:44:10 +000011400 static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE,
Sanjoy Dasf0022122016-09-28 17:14:58 +000011401 SmallPtrSetImpl<const SCEVPredicate *> *NewPreds,
11402 SCEVUnionPredicate *Pred) {
11403 SCEVPredicateRewriter Rewriter(L, SE, NewPreds, Pred);
Sanjoy Das807d33d2016-02-20 01:44:10 +000011404 return Rewriter.visit(S);
Silviu Barangae3c05342015-11-02 14:41:02 +000011405 }
11406
Silviu Barangae3c05342015-11-02 14:41:02 +000011407 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
Sanjoy Dasf0022122016-09-28 17:14:58 +000011408 if (Pred) {
11409 auto ExprPreds = Pred->getPredicatesForExpr(Expr);
11410 for (auto *Pred : ExprPreds)
11411 if (const auto *IPred = dyn_cast<SCEVEqualPredicate>(Pred))
11412 if (IPred->getLHS() == Expr)
11413 return IPred->getRHS();
11414 }
Dorit Nuzmanca4fd182017-07-18 11:57:08 +000011415 return convertToAddRecWithPreds(Expr);
Silviu Barangae3c05342015-11-02 14:41:02 +000011416 }
11417
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011418 const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
11419 const SCEV *Operand = visit(Expr->getOperand());
Sanjoy Dasb277a422016-06-15 06:53:55 +000011420 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Operand);
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011421 if (AR && AR->getLoop() == L && AR->isAffine()) {
11422 // This couldn't be folded because the operand didn't have the nuw
11423 // flag. Add the nusw flag as an assumption that we could make.
11424 const SCEV *Step = AR->getStepRecurrence(SE);
11425 Type *Ty = Expr->getType();
11426 if (addOverflowAssumption(AR, SCEVWrapPredicate::IncrementNUSW))
11427 return SE.getAddRecExpr(SE.getZeroExtendExpr(AR->getStart(), Ty),
11428 SE.getSignExtendExpr(Step, Ty), L,
11429 AR->getNoWrapFlags());
11430 }
11431 return SE.getZeroExtendExpr(Operand, Expr->getType());
11432 }
11433
11434 const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
11435 const SCEV *Operand = visit(Expr->getOperand());
Sanjoy Dasb277a422016-06-15 06:53:55 +000011436 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Operand);
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011437 if (AR && AR->getLoop() == L && AR->isAffine()) {
11438 // This couldn't be folded because the operand didn't have the nsw
11439 // flag. Add the nssw flag as an assumption that we could make.
11440 const SCEV *Step = AR->getStepRecurrence(SE);
11441 Type *Ty = Expr->getType();
11442 if (addOverflowAssumption(AR, SCEVWrapPredicate::IncrementNSSW))
11443 return SE.getAddRecExpr(SE.getSignExtendExpr(AR->getStart(), Ty),
11444 SE.getSignExtendExpr(Step, Ty), L,
11445 AR->getNoWrapFlags());
11446 }
11447 return SE.getSignExtendExpr(Operand, Expr->getType());
11448 }
11449
Silviu Barangae3c05342015-11-02 14:41:02 +000011450private:
Jatin Bhateja7410eea2017-11-26 15:08:41 +000011451 explicit SCEVPredicateRewriter(const Loop *L, ScalarEvolution &SE,
11452 SmallPtrSetImpl<const SCEVPredicate *> *NewPreds,
11453 SCEVUnionPredicate *Pred)
11454 : SCEVRewriteVisitor(SE), NewPreds(NewPreds), Pred(Pred), L(L) {}
11455
Dorit Nuzmanca4fd182017-07-18 11:57:08 +000011456 bool addOverflowAssumption(const SCEVPredicate *P) {
Sanjoy Dasf0022122016-09-28 17:14:58 +000011457 if (!NewPreds) {
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011458 // Check if we've already made this assumption.
Dorit Nuzmanca4fd182017-07-18 11:57:08 +000011459 return Pred && Pred->implies(P);
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011460 }
Dorit Nuzmanca4fd182017-07-18 11:57:08 +000011461 NewPreds->insert(P);
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011462 return true;
11463 }
11464
Dorit Nuzmanca4fd182017-07-18 11:57:08 +000011465 bool addOverflowAssumption(const SCEVAddRecExpr *AR,
11466 SCEVWrapPredicate::IncrementWrapFlags AddedFlags) {
11467 auto *A = SE.getWrapPredicate(AR, AddedFlags);
11468 return addOverflowAssumption(A);
11469 }
11470
11471 // If \p Expr represents a PHINode, we try to see if it can be represented
Michael Liaob30286d2017-09-25 16:21:21 +000011472 // as an AddRec, possibly under a predicate (PHISCEVPred). If it is possible
Dorit Nuzmanca4fd182017-07-18 11:57:08 +000011473 // to add this predicate as a runtime overflow check, we return the AddRec.
Michael Liaob30286d2017-09-25 16:21:21 +000011474 // If \p Expr does not meet these conditions (is not a PHI node, or we
11475 // couldn't create an AddRec for it, or couldn't add the predicate), we just
Dorit Nuzmanca4fd182017-07-18 11:57:08 +000011476 // return \p Expr.
11477 const SCEV *convertToAddRecWithPreds(const SCEVUnknown *Expr) {
11478 if (!isa<PHINode>(Expr->getValue()))
11479 return Expr;
11480 Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
11481 PredicatedRewrite = SE.createAddRecFromPHIWithCasts(Expr);
11482 if (!PredicatedRewrite)
11483 return Expr;
11484 for (auto *P : PredicatedRewrite->second){
11485 if (!addOverflowAssumption(P))
11486 return Expr;
11487 }
11488 return PredicatedRewrite->first;
11489 }
Michael Liaob30286d2017-09-25 16:21:21 +000011490
Sanjoy Dasf0022122016-09-28 17:14:58 +000011491 SmallPtrSetImpl<const SCEVPredicate *> *NewPreds;
11492 SCEVUnionPredicate *Pred;
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011493 const Loop *L;
Silviu Barangae3c05342015-11-02 14:41:02 +000011494};
Eugene Zelenkobe709f22017-08-18 23:51:26 +000011495
Benjamin Kramer83709b12015-11-16 09:01:28 +000011496} // end anonymous namespace
Silviu Barangae3c05342015-11-02 14:41:02 +000011497
Sanjoy Das807d33d2016-02-20 01:44:10 +000011498const SCEV *ScalarEvolution::rewriteUsingPredicate(const SCEV *S, const Loop *L,
Silviu Barangae3c05342015-11-02 14:41:02 +000011499 SCEVUnionPredicate &Preds) {
Sanjoy Dasf0022122016-09-28 17:14:58 +000011500 return SCEVPredicateRewriter::rewrite(S, L, *this, nullptr, &Preds);
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011501}
11502
Sanjoy Dasf0022122016-09-28 17:14:58 +000011503const SCEVAddRecExpr *ScalarEvolution::convertSCEVToAddRecWithPredicates(
11504 const SCEV *S, const Loop *L,
11505 SmallPtrSetImpl<const SCEVPredicate *> &Preds) {
Sanjoy Dasf0022122016-09-28 17:14:58 +000011506 SmallPtrSet<const SCEVPredicate *, 4> TransformPreds;
11507 S = SCEVPredicateRewriter::rewrite(S, L, *this, &TransformPreds, nullptr);
Silviu Barangad68ed852016-03-23 15:29:30 +000011508 auto *AddRec = dyn_cast<SCEVAddRecExpr>(S);
11509
11510 if (!AddRec)
11511 return nullptr;
11512
11513 // Since the transformation was successful, we can now transfer the SCEV
11514 // predicates.
Sanjoy Dasf0022122016-09-28 17:14:58 +000011515 for (auto *P : TransformPreds)
11516 Preds.insert(P);
11517
Silviu Barangad68ed852016-03-23 15:29:30 +000011518 return AddRec;
Silviu Barangae3c05342015-11-02 14:41:02 +000011519}
11520
11521/// SCEV predicates
11522SCEVPredicate::SCEVPredicate(const FoldingSetNodeIDRef ID,
11523 SCEVPredicateKind Kind)
11524 : FastID(ID), Kind(Kind) {}
11525
11526SCEVEqualPredicate::SCEVEqualPredicate(const FoldingSetNodeIDRef ID,
Dorit Nuzmanca4fd182017-07-18 11:57:08 +000011527 const SCEV *LHS, const SCEV *RHS)
11528 : SCEVPredicate(ID, P_Equal), LHS(LHS), RHS(RHS) {
11529 assert(LHS->getType() == RHS->getType() && "LHS and RHS types don't match");
11530 assert(LHS != RHS && "LHS and RHS are the same SCEV");
11531}
Silviu Barangae3c05342015-11-02 14:41:02 +000011532
11533bool SCEVEqualPredicate::implies(const SCEVPredicate *N) const {
Sanjoy Dasb277a422016-06-15 06:53:55 +000011534 const auto *Op = dyn_cast<SCEVEqualPredicate>(N);
Silviu Barangae3c05342015-11-02 14:41:02 +000011535
11536 if (!Op)
11537 return false;
11538
11539 return Op->LHS == LHS && Op->RHS == RHS;
11540}
11541
11542bool SCEVEqualPredicate::isAlwaysTrue() const { return false; }
11543
11544const SCEV *SCEVEqualPredicate::getExpr() const { return LHS; }
11545
11546void SCEVEqualPredicate::print(raw_ostream &OS, unsigned Depth) const {
11547 OS.indent(Depth) << "Equal predicate: " << *LHS << " == " << *RHS << "\n";
11548}
11549
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011550SCEVWrapPredicate::SCEVWrapPredicate(const FoldingSetNodeIDRef ID,
11551 const SCEVAddRecExpr *AR,
11552 IncrementWrapFlags Flags)
11553 : SCEVPredicate(ID, P_Wrap), AR(AR), Flags(Flags) {}
11554
11555const SCEV *SCEVWrapPredicate::getExpr() const { return AR; }
11556
11557bool SCEVWrapPredicate::implies(const SCEVPredicate *N) const {
11558 const auto *Op = dyn_cast<SCEVWrapPredicate>(N);
11559
11560 return Op && Op->AR == AR && setFlags(Flags, Op->Flags) == Flags;
11561}
11562
11563bool SCEVWrapPredicate::isAlwaysTrue() const {
11564 SCEV::NoWrapFlags ScevFlags = AR->getNoWrapFlags();
11565 IncrementWrapFlags IFlags = Flags;
11566
11567 if (ScalarEvolution::setFlags(ScevFlags, SCEV::FlagNSW) == ScevFlags)
11568 IFlags = clearFlags(IFlags, IncrementNSSW);
11569
11570 return IFlags == IncrementAnyWrap;
11571}
11572
11573void SCEVWrapPredicate::print(raw_ostream &OS, unsigned Depth) const {
11574 OS.indent(Depth) << *getExpr() << " Added Flags: ";
11575 if (SCEVWrapPredicate::IncrementNUSW & getFlags())
11576 OS << "<nusw>";
11577 if (SCEVWrapPredicate::IncrementNSSW & getFlags())
11578 OS << "<nssw>";
11579 OS << "\n";
11580}
11581
11582SCEVWrapPredicate::IncrementWrapFlags
11583SCEVWrapPredicate::getImpliedFlags(const SCEVAddRecExpr *AR,
11584 ScalarEvolution &SE) {
11585 IncrementWrapFlags ImpliedFlags = IncrementAnyWrap;
11586 SCEV::NoWrapFlags StaticFlags = AR->getNoWrapFlags();
11587
11588 // We can safely transfer the NSW flag as NSSW.
11589 if (ScalarEvolution::setFlags(StaticFlags, SCEV::FlagNSW) == StaticFlags)
11590 ImpliedFlags = IncrementNSSW;
11591
11592 if (ScalarEvolution::setFlags(StaticFlags, SCEV::FlagNUW) == StaticFlags) {
11593 // If the increment is positive, the SCEV NUW flag will also imply the
11594 // WrapPredicate NUSW flag.
11595 if (const auto *Step = dyn_cast<SCEVConstant>(AR->getStepRecurrence(SE)))
11596 if (Step->getValue()->getValue().isNonNegative())
11597 ImpliedFlags = setFlags(ImpliedFlags, IncrementNUSW);
11598 }
11599
11600 return ImpliedFlags;
11601}
11602
Silviu Barangae3c05342015-11-02 14:41:02 +000011603/// Union predicates don't get cached so create a dummy set ID for it.
11604SCEVUnionPredicate::SCEVUnionPredicate()
11605 : SCEVPredicate(FoldingSetNodeIDRef(nullptr, 0), P_Union) {}
11606
11607bool SCEVUnionPredicate::isAlwaysTrue() const {
Sanjoy Das3b827c72015-11-29 23:40:53 +000011608 return all_of(Preds,
11609 [](const SCEVPredicate *I) { return I->isAlwaysTrue(); });
Silviu Barangae3c05342015-11-02 14:41:02 +000011610}
11611
11612ArrayRef<const SCEVPredicate *>
11613SCEVUnionPredicate::getPredicatesForExpr(const SCEV *Expr) {
11614 auto I = SCEVToPreds.find(Expr);
11615 if (I == SCEVToPreds.end())
11616 return ArrayRef<const SCEVPredicate *>();
11617 return I->second;
11618}
11619
11620bool SCEVUnionPredicate::implies(const SCEVPredicate *N) const {
Sanjoy Dasb277a422016-06-15 06:53:55 +000011621 if (const auto *Set = dyn_cast<SCEVUnionPredicate>(N))
Sanjoy Das3b827c72015-11-29 23:40:53 +000011622 return all_of(Set->Preds,
11623 [this](const SCEVPredicate *I) { return this->implies(I); });
Silviu Barangae3c05342015-11-02 14:41:02 +000011624
11625 auto ScevPredsIt = SCEVToPreds.find(N->getExpr());
11626 if (ScevPredsIt == SCEVToPreds.end())
11627 return false;
11628 auto &SCEVPreds = ScevPredsIt->second;
11629
Sanjoy Dasff3b8b42015-12-01 07:49:23 +000011630 return any_of(SCEVPreds,
11631 [N](const SCEVPredicate *I) { return I->implies(N); });
Silviu Barangae3c05342015-11-02 14:41:02 +000011632}
11633
11634const SCEV *SCEVUnionPredicate::getExpr() const { return nullptr; }
11635
11636void SCEVUnionPredicate::print(raw_ostream &OS, unsigned Depth) const {
11637 for (auto Pred : Preds)
11638 Pred->print(OS, Depth);
11639}
11640
11641void SCEVUnionPredicate::add(const SCEVPredicate *N) {
Sanjoy Dasb277a422016-06-15 06:53:55 +000011642 if (const auto *Set = dyn_cast<SCEVUnionPredicate>(N)) {
Silviu Barangae3c05342015-11-02 14:41:02 +000011643 for (auto Pred : Set->Preds)
11644 add(Pred);
11645 return;
11646 }
11647
11648 if (implies(N))
11649 return;
11650
11651 const SCEV *Key = N->getExpr();
11652 assert(Key && "Only SCEVUnionPredicate doesn't have an "
11653 " associated expression!");
11654
11655 SCEVToPreds[Key].push_back(N);
11656 Preds.push_back(N);
11657}
Silviu Baranga9cd9a7e2015-12-09 16:06:28 +000011658
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011659PredicatedScalarEvolution::PredicatedScalarEvolution(ScalarEvolution &SE,
11660 Loop &L)
Eugene Zelenkobe709f22017-08-18 23:51:26 +000011661 : SE(SE), L(L) {}
Silviu Baranga9cd9a7e2015-12-09 16:06:28 +000011662
11663const SCEV *PredicatedScalarEvolution::getSCEV(Value *V) {
11664 const SCEV *Expr = SE.getSCEV(V);
11665 RewriteEntry &Entry = RewriteMap[Expr];
11666
11667 // If we already have an entry and the version matches, return it.
11668 if (Entry.second && Generation == Entry.first)
11669 return Entry.second;
11670
11671 // We found an entry but it's stale. Rewrite the stale entry
Simon Pilgrimf2fbf432016-11-20 13:47:59 +000011672 // according to the current predicate.
Silviu Baranga9cd9a7e2015-12-09 16:06:28 +000011673 if (Entry.second)
11674 Expr = Entry.second;
11675
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011676 const SCEV *NewSCEV = SE.rewriteUsingPredicate(Expr, &L, Preds);
Silviu Baranga9cd9a7e2015-12-09 16:06:28 +000011677 Entry = {Generation, NewSCEV};
11678
11679 return NewSCEV;
11680}
11681
Silviu Baranga6f444df2016-04-08 14:29:09 +000011682const SCEV *PredicatedScalarEvolution::getBackedgeTakenCount() {
11683 if (!BackedgeCount) {
11684 SCEVUnionPredicate BackedgePred;
11685 BackedgeCount = SE.getPredicatedBackedgeTakenCount(&L, BackedgePred);
11686 addPredicate(BackedgePred);
11687 }
11688 return BackedgeCount;
11689}
11690
Silviu Baranga9cd9a7e2015-12-09 16:06:28 +000011691void PredicatedScalarEvolution::addPredicate(const SCEVPredicate &Pred) {
11692 if (Preds.implies(&Pred))
11693 return;
11694 Preds.add(&Pred);
11695 updateGeneration();
11696}
11697
11698const SCEVUnionPredicate &PredicatedScalarEvolution::getUnionPredicate() const {
11699 return Preds;
11700}
11701
11702void PredicatedScalarEvolution::updateGeneration() {
11703 // If the generation number wrapped recompute everything.
11704 if (++Generation == 0) {
11705 for (auto &II : RewriteMap) {
11706 const SCEV *Rewritten = II.second.second;
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011707 II.second = {Generation, SE.rewriteUsingPredicate(Rewritten, &L, Preds)};
Silviu Baranga9cd9a7e2015-12-09 16:06:28 +000011708 }
11709 }
11710}
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011711
11712void PredicatedScalarEvolution::setNoOverflow(
11713 Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags) {
11714 const SCEV *Expr = getSCEV(V);
11715 const auto *AR = cast<SCEVAddRecExpr>(Expr);
11716
11717 auto ImpliedFlags = SCEVWrapPredicate::getImpliedFlags(AR, SE);
11718
11719 // Clear the statically implied flags.
11720 Flags = SCEVWrapPredicate::clearFlags(Flags, ImpliedFlags);
11721 addPredicate(*SE.getWrapPredicate(AR, Flags));
11722
11723 auto II = FlagsMap.insert({V, Flags});
11724 if (!II.second)
11725 II.first->second = SCEVWrapPredicate::setFlags(Flags, II.first->second);
11726}
11727
11728bool PredicatedScalarEvolution::hasNoOverflow(
11729 Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags) {
11730 const SCEV *Expr = getSCEV(V);
11731 const auto *AR = cast<SCEVAddRecExpr>(Expr);
11732
11733 Flags = SCEVWrapPredicate::clearFlags(
11734 Flags, SCEVWrapPredicate::getImpliedFlags(AR, SE));
11735
11736 auto II = FlagsMap.find(V);
11737
11738 if (II != FlagsMap.end())
11739 Flags = SCEVWrapPredicate::clearFlags(Flags, II->second);
11740
11741 return Flags == SCEVWrapPredicate::IncrementAnyWrap;
11742}
11743
Silviu Barangad68ed852016-03-23 15:29:30 +000011744const SCEVAddRecExpr *PredicatedScalarEvolution::getAsAddRec(Value *V) {
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011745 const SCEV *Expr = this->getSCEV(V);
Sanjoy Dasf0022122016-09-28 17:14:58 +000011746 SmallPtrSet<const SCEVPredicate *, 4> NewPreds;
11747 auto *New = SE.convertSCEVToAddRecWithPredicates(Expr, &L, NewPreds);
Silviu Barangad68ed852016-03-23 15:29:30 +000011748
11749 if (!New)
11750 return nullptr;
11751
Sanjoy Dasf0022122016-09-28 17:14:58 +000011752 for (auto *P : NewPreds)
11753 Preds.add(P);
11754
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011755 updateGeneration();
11756 RewriteMap[SE.getSCEV(V)] = {Generation, New};
11757 return New;
11758}
11759
Silviu Baranga6f444df2016-04-08 14:29:09 +000011760PredicatedScalarEvolution::PredicatedScalarEvolution(
11761 const PredicatedScalarEvolution &Init)
11762 : RewriteMap(Init.RewriteMap), SE(Init.SE), L(Init.L), Preds(Init.Preds),
11763 Generation(Init.Generation), BackedgeCount(Init.BackedgeCount) {
Benjamin Krameraa209152016-06-26 17:27:42 +000011764 for (const auto &I : Init.FlagsMap)
11765 FlagsMap.insert(I);
Silviu Barangaea63a7f2016-02-08 17:02:45 +000011766}
Silviu Barangab77365b2016-04-14 16:08:45 +000011767
11768void PredicatedScalarEvolution::print(raw_ostream &OS, unsigned Depth) const {
11769 // For each block.
11770 for (auto *BB : L.getBlocks())
11771 for (auto &I : *BB) {
11772 if (!SE.isSCEVable(I.getType()))
11773 continue;
11774
11775 auto *Expr = SE.getSCEV(&I);
11776 auto II = RewriteMap.find(Expr);
11777
11778 if (II == RewriteMap.end())
11779 continue;
11780
11781 // Don't print things that are not interesting.
11782 if (II->second.second == Expr)
11783 continue;
11784
11785 OS.indent(Depth) << "[PSE]" << I << ":\n";
11786 OS.indent(Depth + 2) << *Expr << "\n";
11787 OS.indent(Depth + 2) << "--> " << *II->second.second << "\n";
11788 }
11789}