Bug Summary

File:llvm/lib/Analysis/ScalarEvolution.cpp
Warning:line 4178, column 32
Dereference of null pointer (loaded from variable 'PtrOp')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ScalarEvolution.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/lib/Analysis -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/lib/Analysis -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/lib/Analysis -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/include -D NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/lib/Analysis -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-09-04-040900-46481-1 -x c++ /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/lib/Analysis/ScalarEvolution.cpp
1//===- ScalarEvolution.cpp - Scalar Evolution Analysis --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the implementation of the scalar evolution analysis
10// engine, which is used primarily to analyze expressions involving induction
11// variables in loops.
12//
13// There are several aspects to this library. First is the representation of
14// scalar expressions, which are represented as subclasses of the SCEV class.
15// These classes are used to represent certain types of subexpressions that we
16// can handle. We only create one SCEV of a particular shape, so
17// pointer-comparisons for equality are legal.
18//
19// One important aspect of the SCEV objects is that they are never cyclic, even
20// if there is a cycle in the dataflow for an expression (ie, a PHI node). If
21// the PHI node is one of the idioms that we can represent (e.g., a polynomial
22// recurrence) then we represent it directly as a recurrence node, otherwise we
23// represent it as a SCEVUnknown node.
24//
25// In addition to being able to represent expressions of various types, we also
26// have folders that are used to build the *canonical* representation for a
27// particular expression. These folders are capable of using a variety of
28// rewrite rules to simplify the expressions.
29//
30// Once the folders are defined, we can implement the more interesting
31// higher-level code, such as the code that recognizes PHI nodes of various
32// types, computes the execution count of a loop, etc.
33//
34// TODO: We should use these routines and value representations to implement
35// dependence analysis!
36//
37//===----------------------------------------------------------------------===//
38//
39// There are several good references for the techniques used in this analysis.
40//
41// Chains of recurrences -- a method to expedite the evaluation
42// of closed-form functions
43// Olaf Bachmann, Paul S. Wang, Eugene V. Zima
44//
45// On computational properties of chains of recurrences
46// Eugene V. Zima
47//
48// Symbolic Evaluation of Chains of Recurrences for Loop Optimization
49// Robert A. van Engelen
50//
51// Efficient Symbolic Analysis for Optimizing Compilers
52// Robert A. van Engelen
53//
54// Using the chains of recurrences algebra for data dependence testing and
55// induction variable substitution
56// MS Thesis, Johnie Birch
57//
58//===----------------------------------------------------------------------===//
59
60#include "llvm/Analysis/ScalarEvolution.h"
61#include "llvm/ADT/APInt.h"
62#include "llvm/ADT/ArrayRef.h"
63#include "llvm/ADT/DenseMap.h"
64#include "llvm/ADT/DepthFirstIterator.h"
65#include "llvm/ADT/EquivalenceClasses.h"
66#include "llvm/ADT/FoldingSet.h"
67#include "llvm/ADT/None.h"
68#include "llvm/ADT/Optional.h"
69#include "llvm/ADT/STLExtras.h"
70#include "llvm/ADT/ScopeExit.h"
71#include "llvm/ADT/Sequence.h"
72#include "llvm/ADT/SetVector.h"
73#include "llvm/ADT/SmallPtrSet.h"
74#include "llvm/ADT/SmallSet.h"
75#include "llvm/ADT/SmallVector.h"
76#include "llvm/ADT/Statistic.h"
77#include "llvm/ADT/StringRef.h"
78#include "llvm/Analysis/AssumptionCache.h"
79#include "llvm/Analysis/ConstantFolding.h"
80#include "llvm/Analysis/InstructionSimplify.h"
81#include "llvm/Analysis/LoopInfo.h"
82#include "llvm/Analysis/ScalarEvolutionDivision.h"
83#include "llvm/Analysis/ScalarEvolutionExpressions.h"
84#include "llvm/Analysis/TargetLibraryInfo.h"
85#include "llvm/Analysis/ValueTracking.h"
86#include "llvm/Config/llvm-config.h"
87#include "llvm/IR/Argument.h"
88#include "llvm/IR/BasicBlock.h"
89#include "llvm/IR/CFG.h"
90#include "llvm/IR/Constant.h"
91#include "llvm/IR/ConstantRange.h"
92#include "llvm/IR/Constants.h"
93#include "llvm/IR/DataLayout.h"
94#include "llvm/IR/DerivedTypes.h"
95#include "llvm/IR/Dominators.h"
96#include "llvm/IR/Function.h"
97#include "llvm/IR/GlobalAlias.h"
98#include "llvm/IR/GlobalValue.h"
99#include "llvm/IR/GlobalVariable.h"
100#include "llvm/IR/InstIterator.h"
101#include "llvm/IR/InstrTypes.h"
102#include "llvm/IR/Instruction.h"
103#include "llvm/IR/Instructions.h"
104#include "llvm/IR/IntrinsicInst.h"
105#include "llvm/IR/Intrinsics.h"
106#include "llvm/IR/LLVMContext.h"
107#include "llvm/IR/Metadata.h"
108#include "llvm/IR/Operator.h"
109#include "llvm/IR/PatternMatch.h"
110#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/IR/Verifier.h"
115#include "llvm/InitializePasses.h"
116#include "llvm/Pass.h"
117#include "llvm/Support/Casting.h"
118#include "llvm/Support/CommandLine.h"
119#include "llvm/Support/Compiler.h"
120#include "llvm/Support/Debug.h"
121#include "llvm/Support/ErrorHandling.h"
122#include "llvm/Support/KnownBits.h"
123#include "llvm/Support/SaveAndRestore.h"
124#include "llvm/Support/raw_ostream.h"
125#include <algorithm>
126#include <cassert>
127#include <climits>
128#include <cstddef>
129#include <cstdint>
130#include <cstdlib>
131#include <map>
132#include <memory>
133#include <tuple>
134#include <utility>
135#include <vector>
136
137using namespace llvm;
138using namespace PatternMatch;
139
140#define DEBUG_TYPE"scalar-evolution" "scalar-evolution"
141
142STATISTIC(NumArrayLenItCounts,static llvm::Statistic NumArrayLenItCounts = {"scalar-evolution"
, "NumArrayLenItCounts", "Number of trip counts computed with array length"
}
143 "Number of trip counts computed with array length")static llvm::Statistic NumArrayLenItCounts = {"scalar-evolution"
, "NumArrayLenItCounts", "Number of trip counts computed with array length"
}
;
144STATISTIC(NumTripCountsComputed,static llvm::Statistic NumTripCountsComputed = {"scalar-evolution"
, "NumTripCountsComputed", "Number of loops with predictable loop counts"
}
145 "Number of loops with predictable loop counts")static llvm::Statistic NumTripCountsComputed = {"scalar-evolution"
, "NumTripCountsComputed", "Number of loops with predictable loop counts"
}
;
146STATISTIC(NumTripCountsNotComputed,static llvm::Statistic NumTripCountsNotComputed = {"scalar-evolution"
, "NumTripCountsNotComputed", "Number of loops without predictable loop counts"
}
147 "Number of loops without predictable loop counts")static llvm::Statistic NumTripCountsNotComputed = {"scalar-evolution"
, "NumTripCountsNotComputed", "Number of loops without predictable loop counts"
}
;
148STATISTIC(NumBruteForceTripCountsComputed,static llvm::Statistic NumBruteForceTripCountsComputed = {"scalar-evolution"
, "NumBruteForceTripCountsComputed", "Number of loops with trip counts computed by force"
}
149 "Number of loops with trip counts computed by force")static llvm::Statistic NumBruteForceTripCountsComputed = {"scalar-evolution"
, "NumBruteForceTripCountsComputed", "Number of loops with trip counts computed by force"
}
;
150
151static cl::opt<unsigned>
152MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden,
153 cl::ZeroOrMore,
154 cl::desc("Maximum number of iterations SCEV will "
155 "symbolically execute a constant "
156 "derived loop"),
157 cl::init(100));
158
159// FIXME: Enable this with EXPENSIVE_CHECKS when the test suite is clean.
160static cl::opt<bool> VerifySCEV(
161 "verify-scev", cl::Hidden,
162 cl::desc("Verify ScalarEvolution's backedge taken counts (slow)"));
163static cl::opt<bool> VerifySCEVStrict(
164 "verify-scev-strict", cl::Hidden,
165 cl::desc("Enable stricter verification with -verify-scev is passed"));
166static cl::opt<bool>
167 VerifySCEVMap("verify-scev-maps", cl::Hidden,
168 cl::desc("Verify no dangling value in ScalarEvolution's "
169 "ExprValueMap (slow)"));
170
171static cl::opt<bool> VerifyIR(
172 "scev-verify-ir", cl::Hidden,
173 cl::desc("Verify IR correctness when making sensitive SCEV queries (slow)"),
174 cl::init(false));
175
176static cl::opt<unsigned> MulOpsInlineThreshold(
177 "scev-mulops-inline-threshold", cl::Hidden,
178 cl::desc("Threshold for inlining multiplication operands into a SCEV"),
179 cl::init(32));
180
181static cl::opt<unsigned> AddOpsInlineThreshold(
182 "scev-addops-inline-threshold", cl::Hidden,
183 cl::desc("Threshold for inlining addition operands into a SCEV"),
184 cl::init(500));
185
186static cl::opt<unsigned> MaxSCEVCompareDepth(
187 "scalar-evolution-max-scev-compare-depth", cl::Hidden,
188 cl::desc("Maximum depth of recursive SCEV complexity comparisons"),
189 cl::init(32));
190
191static cl::opt<unsigned> MaxSCEVOperationsImplicationDepth(
192 "scalar-evolution-max-scev-operations-implication-depth", cl::Hidden,
193 cl::desc("Maximum depth of recursive SCEV operations implication analysis"),
194 cl::init(2));
195
196static cl::opt<unsigned> MaxValueCompareDepth(
197 "scalar-evolution-max-value-compare-depth", cl::Hidden,
198 cl::desc("Maximum depth of recursive value complexity comparisons"),
199 cl::init(2));
200
201static cl::opt<unsigned>
202 MaxArithDepth("scalar-evolution-max-arith-depth", cl::Hidden,
203 cl::desc("Maximum depth of recursive arithmetics"),
204 cl::init(32));
205
206static cl::opt<unsigned> MaxConstantEvolvingDepth(
207 "scalar-evolution-max-constant-evolving-depth", cl::Hidden,
208 cl::desc("Maximum depth of recursive constant evolving"), cl::init(32));
209
210static cl::opt<unsigned>
211 MaxCastDepth("scalar-evolution-max-cast-depth", cl::Hidden,
212 cl::desc("Maximum depth of recursive SExt/ZExt/Trunc"),
213 cl::init(8));
214
215static cl::opt<unsigned>
216 MaxAddRecSize("scalar-evolution-max-add-rec-size", cl::Hidden,
217 cl::desc("Max coefficients in AddRec during evolving"),
218 cl::init(8));
219
220static cl::opt<unsigned>
221 HugeExprThreshold("scalar-evolution-huge-expr-threshold", cl::Hidden,
222 cl::desc("Size of the expression which is considered huge"),
223 cl::init(4096));
224
225static cl::opt<bool>
226ClassifyExpressions("scalar-evolution-classify-expressions",
227 cl::Hidden, cl::init(true),
228 cl::desc("When printing analysis, include information on every instruction"));
229
230static cl::opt<bool> UseExpensiveRangeSharpening(
231 "scalar-evolution-use-expensive-range-sharpening", cl::Hidden,
232 cl::init(false),
233 cl::desc("Use more powerful methods of sharpening expression ranges. May "
234 "be costly in terms of compile time"));
235
236//===----------------------------------------------------------------------===//
237// SCEV class definitions
238//===----------------------------------------------------------------------===//
239
240//===----------------------------------------------------------------------===//
241// Implementation of the SCEV class.
242//
243
244#if !defined(NDEBUG1) || defined(LLVM_ENABLE_DUMP)
245LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void SCEV::dump() const {
246 print(dbgs());
247 dbgs() << '\n';
248}
249#endif
250
251void SCEV::print(raw_ostream &OS) const {
252 switch (getSCEVType()) {
253 case scConstant:
254 cast<SCEVConstant>(this)->getValue()->printAsOperand(OS, false);
255 return;
256 case scPtrToInt: {
257 const SCEVPtrToIntExpr *PtrToInt = cast<SCEVPtrToIntExpr>(this);
258 const SCEV *Op = PtrToInt->getOperand();
259 OS << "(ptrtoint " << *Op->getType() << " " << *Op << " to "
260 << *PtrToInt->getType() << ")";
261 return;
262 }
263 case scTruncate: {
264 const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(this);
265 const SCEV *Op = Trunc->getOperand();
266 OS << "(trunc " << *Op->getType() << " " << *Op << " to "
267 << *Trunc->getType() << ")";
268 return;
269 }
270 case scZeroExtend: {
271 const SCEVZeroExtendExpr *ZExt = cast<SCEVZeroExtendExpr>(this);
272 const SCEV *Op = ZExt->getOperand();
273 OS << "(zext " << *Op->getType() << " " << *Op << " to "
274 << *ZExt->getType() << ")";
275 return;
276 }
277 case scSignExtend: {
278 const SCEVSignExtendExpr *SExt = cast<SCEVSignExtendExpr>(this);
279 const SCEV *Op = SExt->getOperand();
280 OS << "(sext " << *Op->getType() << " " << *Op << " to "
281 << *SExt->getType() << ")";
282 return;
283 }
284 case scAddRecExpr: {
285 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(this);
286 OS << "{" << *AR->getOperand(0);
287 for (unsigned i = 1, e = AR->getNumOperands(); i != e; ++i)
288 OS << ",+," << *AR->getOperand(i);
289 OS << "}<";
290 if (AR->hasNoUnsignedWrap())
291 OS << "nuw><";
292 if (AR->hasNoSignedWrap())
293 OS << "nsw><";
294 if (AR->hasNoSelfWrap() &&
295 !AR->getNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW)))
296 OS << "nw><";
297 AR->getLoop()->getHeader()->printAsOperand(OS, /*PrintType=*/false);
298 OS << ">";
299 return;
300 }
301 case scAddExpr:
302 case scMulExpr:
303 case scUMaxExpr:
304 case scSMaxExpr:
305 case scUMinExpr:
306 case scSMinExpr: {
307 const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(this);
308 const char *OpStr = nullptr;
309 switch (NAry->getSCEVType()) {
310 case scAddExpr: OpStr = " + "; break;
311 case scMulExpr: OpStr = " * "; break;
312 case scUMaxExpr: OpStr = " umax "; break;
313 case scSMaxExpr: OpStr = " smax "; break;
314 case scUMinExpr:
315 OpStr = " umin ";
316 break;
317 case scSMinExpr:
318 OpStr = " smin ";
319 break;
320 default:
321 llvm_unreachable("There are no other nary expression types.")__builtin_unreachable();
322 }
323 OS << "(";
324 ListSeparator LS(OpStr);
325 for (const SCEV *Op : NAry->operands())
326 OS << LS << *Op;
327 OS << ")";
328 switch (NAry->getSCEVType()) {
329 case scAddExpr:
330 case scMulExpr:
331 if (NAry->hasNoUnsignedWrap())
332 OS << "<nuw>";
333 if (NAry->hasNoSignedWrap())
334 OS << "<nsw>";
335 break;
336 default:
337 // Nothing to print for other nary expressions.
338 break;
339 }
340 return;
341 }
342 case scUDivExpr: {
343 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(this);
344 OS << "(" << *UDiv->getLHS() << " /u " << *UDiv->getRHS() << ")";
345 return;
346 }
347 case scUnknown: {
348 const SCEVUnknown *U = cast<SCEVUnknown>(this);
349 Type *AllocTy;
350 if (U->isSizeOf(AllocTy)) {
351 OS << "sizeof(" << *AllocTy << ")";
352 return;
353 }
354 if (U->isAlignOf(AllocTy)) {
355 OS << "alignof(" << *AllocTy << ")";
356 return;
357 }
358
359 Type *CTy;
360 Constant *FieldNo;
361 if (U->isOffsetOf(CTy, FieldNo)) {
362 OS << "offsetof(" << *CTy << ", ";
363 FieldNo->printAsOperand(OS, false);
364 OS << ")";
365 return;
366 }
367
368 // Otherwise just print it normally.
369 U->getValue()->printAsOperand(OS, false);
370 return;
371 }
372 case scCouldNotCompute:
373 OS << "***COULDNOTCOMPUTE***";
374 return;
375 }
376 llvm_unreachable("Unknown SCEV kind!")__builtin_unreachable();
377}
378
379Type *SCEV::getType() const {
380 switch (getSCEVType()) {
381 case scConstant:
382 return cast<SCEVConstant>(this)->getType();
383 case scPtrToInt:
384 case scTruncate:
385 case scZeroExtend:
386 case scSignExtend:
387 return cast<SCEVCastExpr>(this)->getType();
388 case scAddRecExpr:
389 return cast<SCEVAddRecExpr>(this)->getType();
390 case scMulExpr:
391 return cast<SCEVMulExpr>(this)->getType();
392 case scUMaxExpr:
393 case scSMaxExpr:
394 case scUMinExpr:
395 case scSMinExpr:
396 return cast<SCEVMinMaxExpr>(this)->getType();
397 case scAddExpr:
398 return cast<SCEVAddExpr>(this)->getType();
399 case scUDivExpr:
400 return cast<SCEVUDivExpr>(this)->getType();
401 case scUnknown:
402 return cast<SCEVUnknown>(this)->getType();
403 case scCouldNotCompute:
404 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!")__builtin_unreachable();
405 }
406 llvm_unreachable("Unknown SCEV kind!")__builtin_unreachable();
407}
408
409bool SCEV::isZero() const {
410 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
411 return SC->getValue()->isZero();
412 return false;
413}
414
415bool SCEV::isOne() const {
416 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
417 return SC->getValue()->isOne();
418 return false;
419}
420
421bool SCEV::isAllOnesValue() const {
422 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
423 return SC->getValue()->isMinusOne();
424 return false;
425}
426
427bool SCEV::isNonConstantNegative() const {
428 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(this);
429 if (!Mul) return false;
430
431 // If there is a constant factor, it will be first.
432 const SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0));
433 if (!SC) return false;
434
435 // Return true if the value is negative, this matches things like (-42 * V).
436 return SC->getAPInt().isNegative();
437}
438
439SCEVCouldNotCompute::SCEVCouldNotCompute() :
440 SCEV(FoldingSetNodeIDRef(), scCouldNotCompute, 0) {}
441
442bool SCEVCouldNotCompute::classof(const SCEV *S) {
443 return S->getSCEVType() == scCouldNotCompute;
444}
445
446const SCEV *ScalarEvolution::getConstant(ConstantInt *V) {
447 FoldingSetNodeID ID;
448 ID.AddInteger(scConstant);
449 ID.AddPointer(V);
450 void *IP = nullptr;
451 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
452 SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V);
453 UniqueSCEVs.InsertNode(S, IP);
454 return S;
455}
456
457const SCEV *ScalarEvolution::getConstant(const APInt &Val) {
458 return getConstant(ConstantInt::get(getContext(), Val));
459}
460
461const SCEV *
462ScalarEvolution::getConstant(Type *Ty, uint64_t V, bool isSigned) {
463 IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty));
464 return getConstant(ConstantInt::get(ITy, V, isSigned));
465}
466
467SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy,
468 const SCEV *op, Type *ty)
469 : SCEV(ID, SCEVTy, computeExpressionSize(op)), Ty(ty) {
470 Operands[0] = op;
471}
472
473SCEVPtrToIntExpr::SCEVPtrToIntExpr(const FoldingSetNodeIDRef ID, const SCEV *Op,
474 Type *ITy)
475 : SCEVCastExpr(ID, scPtrToInt, Op, ITy) {
476 assert(getOperand()->getType()->isPointerTy() && Ty->isIntegerTy() &&(static_cast<void> (0))
477 "Must be a non-bit-width-changing pointer-to-integer cast!")(static_cast<void> (0));
478}
479
480SCEVIntegralCastExpr::SCEVIntegralCastExpr(const FoldingSetNodeIDRef ID,
481 SCEVTypes SCEVTy, const SCEV *op,
482 Type *ty)
483 : SCEVCastExpr(ID, SCEVTy, op, ty) {}
484
485SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID, const SCEV *op,
486 Type *ty)
487 : SCEVIntegralCastExpr(ID, scTruncate, op, ty) {
488 assert(getOperand()->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&(static_cast<void> (0))
489 "Cannot truncate non-integer value!")(static_cast<void> (0));
490}
491
492SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
493 const SCEV *op, Type *ty)
494 : SCEVIntegralCastExpr(ID, scZeroExtend, op, ty) {
495 assert(getOperand()->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&(static_cast<void> (0))
496 "Cannot zero extend non-integer value!")(static_cast<void> (0));
497}
498
499SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
500 const SCEV *op, Type *ty)
501 : SCEVIntegralCastExpr(ID, scSignExtend, op, ty) {
502 assert(getOperand()->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&(static_cast<void> (0))
503 "Cannot sign extend non-integer value!")(static_cast<void> (0));
504}
505
506void SCEVUnknown::deleted() {
507 // Clear this SCEVUnknown from various maps.
508 SE->forgetMemoizedResults(this);
509
510 // Remove this SCEVUnknown from the uniquing map.
511 SE->UniqueSCEVs.RemoveNode(this);
512
513 // Release the value.
514 setValPtr(nullptr);
515}
516
517void SCEVUnknown::allUsesReplacedWith(Value *New) {
518 // Remove this SCEVUnknown from the uniquing map.
519 SE->UniqueSCEVs.RemoveNode(this);
520
521 // Update this SCEVUnknown to point to the new value. This is needed
522 // because there may still be outstanding SCEVs which still point to
523 // this SCEVUnknown.
524 setValPtr(New);
525}
526
527bool SCEVUnknown::isSizeOf(Type *&AllocTy) const {
528 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
529 if (VCE->getOpcode() == Instruction::PtrToInt)
530 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
531 if (CE->getOpcode() == Instruction::GetElementPtr &&
532 CE->getOperand(0)->isNullValue() &&
533 CE->getNumOperands() == 2)
534 if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(1)))
535 if (CI->isOne()) {
536 AllocTy = cast<GEPOperator>(CE)->getSourceElementType();
537 return true;
538 }
539
540 return false;
541}
542
543bool SCEVUnknown::isAlignOf(Type *&AllocTy) const {
544 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
545 if (VCE->getOpcode() == Instruction::PtrToInt)
546 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
547 if (CE->getOpcode() == Instruction::GetElementPtr &&
548 CE->getOperand(0)->isNullValue()) {
549 Type *Ty = cast<GEPOperator>(CE)->getSourceElementType();
550 if (StructType *STy = dyn_cast<StructType>(Ty))
551 if (!STy->isPacked() &&
552 CE->getNumOperands() == 3 &&
553 CE->getOperand(1)->isNullValue()) {
554 if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(2)))
555 if (CI->isOne() &&
556 STy->getNumElements() == 2 &&
557 STy->getElementType(0)->isIntegerTy(1)) {
558 AllocTy = STy->getElementType(1);
559 return true;
560 }
561 }
562 }
563
564 return false;
565}
566
567bool SCEVUnknown::isOffsetOf(Type *&CTy, Constant *&FieldNo) const {
568 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
569 if (VCE->getOpcode() == Instruction::PtrToInt)
570 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
571 if (CE->getOpcode() == Instruction::GetElementPtr &&
572 CE->getNumOperands() == 3 &&
573 CE->getOperand(0)->isNullValue() &&
574 CE->getOperand(1)->isNullValue()) {
575 Type *Ty = cast<GEPOperator>(CE)->getSourceElementType();
576 // Ignore vector types here so that ScalarEvolutionExpander doesn't
577 // emit getelementptrs that index into vectors.
578 if (Ty->isStructTy() || Ty->isArrayTy()) {
579 CTy = Ty;
580 FieldNo = CE->getOperand(2);
581 return true;
582 }
583 }
584
585 return false;
586}
587
588//===----------------------------------------------------------------------===//
589// SCEV Utilities
590//===----------------------------------------------------------------------===//
591
592/// Compare the two values \p LV and \p RV in terms of their "complexity" where
593/// "complexity" is a partial (and somewhat ad-hoc) relation used to order
594/// operands in SCEV expressions. \p EqCache is a set of pairs of values that
595/// have been previously deemed to be "equally complex" by this routine. It is
596/// intended to avoid exponential time complexity in cases like:
597///
598/// %a = f(%x, %y)
599/// %b = f(%a, %a)
600/// %c = f(%b, %b)
601///
602/// %d = f(%x, %y)
603/// %e = f(%d, %d)
604/// %f = f(%e, %e)
605///
606/// CompareValueComplexity(%f, %c)
607///
608/// Since we do not continue running this routine on expression trees once we
609/// have seen unequal values, there is no need to track them in the cache.
610static int
611CompareValueComplexity(EquivalenceClasses<const Value *> &EqCacheValue,
612 const LoopInfo *const LI, Value *LV, Value *RV,
613 unsigned Depth) {
614 if (Depth > MaxValueCompareDepth || EqCacheValue.isEquivalent(LV, RV))
615 return 0;
616
617 // Order pointer values after integer values. This helps SCEVExpander form
618 // GEPs.
619 bool LIsPointer = LV->getType()->isPointerTy(),
620 RIsPointer = RV->getType()->isPointerTy();
621 if (LIsPointer != RIsPointer)
622 return (int)LIsPointer - (int)RIsPointer;
623
624 // Compare getValueID values.
625 unsigned LID = LV->getValueID(), RID = RV->getValueID();
626 if (LID != RID)
627 return (int)LID - (int)RID;
628
629 // Sort arguments by their position.
630 if (const auto *LA = dyn_cast<Argument>(LV)) {
631 const auto *RA = cast<Argument>(RV);
632 unsigned LArgNo = LA->getArgNo(), RArgNo = RA->getArgNo();
633 return (int)LArgNo - (int)RArgNo;
634 }
635
636 if (const auto *LGV = dyn_cast<GlobalValue>(LV)) {
637 const auto *RGV = cast<GlobalValue>(RV);
638
639 const auto IsGVNameSemantic = [&](const GlobalValue *GV) {
640 auto LT = GV->getLinkage();
641 return !(GlobalValue::isPrivateLinkage(LT) ||
642 GlobalValue::isInternalLinkage(LT));
643 };
644
645 // Use the names to distinguish the two values, but only if the
646 // names are semantically important.
647 if (IsGVNameSemantic(LGV) && IsGVNameSemantic(RGV))
648 return LGV->getName().compare(RGV->getName());
649 }
650
651 // For instructions, compare their loop depth, and their operand count. This
652 // is pretty loose.
653 if (const auto *LInst = dyn_cast<Instruction>(LV)) {
654 const auto *RInst = cast<Instruction>(RV);
655
656 // Compare loop depths.
657 const BasicBlock *LParent = LInst->getParent(),
658 *RParent = RInst->getParent();
659 if (LParent != RParent) {
660 unsigned LDepth = LI->getLoopDepth(LParent),
661 RDepth = LI->getLoopDepth(RParent);
662 if (LDepth != RDepth)
663 return (int)LDepth - (int)RDepth;
664 }
665
666 // Compare the number of operands.
667 unsigned LNumOps = LInst->getNumOperands(),
668 RNumOps = RInst->getNumOperands();
669 if (LNumOps != RNumOps)
670 return (int)LNumOps - (int)RNumOps;
671
672 for (unsigned Idx : seq(0u, LNumOps)) {
673 int Result =
674 CompareValueComplexity(EqCacheValue, LI, LInst->getOperand(Idx),
675 RInst->getOperand(Idx), Depth + 1);
676 if (Result != 0)
677 return Result;
678 }
679 }
680
681 EqCacheValue.unionSets(LV, RV);
682 return 0;
683}
684
685// Return negative, zero, or positive, if LHS is less than, equal to, or greater
686// than RHS, respectively. A three-way result allows recursive comparisons to be
687// more efficient.
688// If the max analysis depth was reached, return None, assuming we do not know
689// if they are equivalent for sure.
690static Optional<int>
691CompareSCEVComplexity(EquivalenceClasses<const SCEV *> &EqCacheSCEV,
692 EquivalenceClasses<const Value *> &EqCacheValue,
693 const LoopInfo *const LI, const SCEV *LHS,
694 const SCEV *RHS, DominatorTree &DT, unsigned Depth = 0) {
695 // Fast-path: SCEVs are uniqued so we can do a quick equality check.
696 if (LHS == RHS)
697 return 0;
698
699 // Primarily, sort the SCEVs by their getSCEVType().
700 SCEVTypes LType = LHS->getSCEVType(), RType = RHS->getSCEVType();
701 if (LType != RType)
702 return (int)LType - (int)RType;
703
704 if (EqCacheSCEV.isEquivalent(LHS, RHS))
705 return 0;
706
707 if (Depth > MaxSCEVCompareDepth)
708 return None;
709
710 // Aside from the getSCEVType() ordering, the particular ordering
711 // isn't very important except that it's beneficial to be consistent,
712 // so that (a + b) and (b + a) don't end up as different expressions.
713 switch (LType) {
714 case scUnknown: {
715 const SCEVUnknown *LU = cast<SCEVUnknown>(LHS);
716 const SCEVUnknown *RU = cast<SCEVUnknown>(RHS);
717
718 int X = CompareValueComplexity(EqCacheValue, LI, LU->getValue(),
719 RU->getValue(), Depth + 1);
720 if (X == 0)
721 EqCacheSCEV.unionSets(LHS, RHS);
722 return X;
723 }
724
725 case scConstant: {
726 const SCEVConstant *LC = cast<SCEVConstant>(LHS);
727 const SCEVConstant *RC = cast<SCEVConstant>(RHS);
728
729 // Compare constant values.
730 const APInt &LA = LC->getAPInt();
731 const APInt &RA = RC->getAPInt();
732 unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.getBitWidth();
733 if (LBitWidth != RBitWidth)
734 return (int)LBitWidth - (int)RBitWidth;
735 return LA.ult(RA) ? -1 : 1;
736 }
737
738 case scAddRecExpr: {
739 const SCEVAddRecExpr *LA = cast<SCEVAddRecExpr>(LHS);
740 const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS);
741
742 // There is always a dominance between two recs that are used by one SCEV,
743 // so we can safely sort recs by loop header dominance. We require such
744 // order in getAddExpr.
745 const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop();
746 if (LLoop != RLoop) {
747 const BasicBlock *LHead = LLoop->getHeader(), *RHead = RLoop->getHeader();
748 assert(LHead != RHead && "Two loops share the same header?")(static_cast<void> (0));
749 if (DT.dominates(LHead, RHead))
750 return 1;
751 else
752 assert(DT.dominates(RHead, LHead) &&(static_cast<void> (0))
753 "No dominance between recurrences used by one SCEV?")(static_cast<void> (0));
754 return -1;
755 }
756
757 // Addrec complexity grows with operand count.
758 unsigned LNumOps = LA->getNumOperands(), RNumOps = RA->getNumOperands();
759 if (LNumOps != RNumOps)
760 return (int)LNumOps - (int)RNumOps;
761
762 // Lexicographically compare.
763 for (unsigned i = 0; i != LNumOps; ++i) {
764 auto X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI,
765 LA->getOperand(i), RA->getOperand(i), DT,
766 Depth + 1);
767 if (X != 0)
768 return X;
769 }
770 EqCacheSCEV.unionSets(LHS, RHS);
771 return 0;
772 }
773
774 case scAddExpr:
775 case scMulExpr:
776 case scSMaxExpr:
777 case scUMaxExpr:
778 case scSMinExpr:
779 case scUMinExpr: {
780 const SCEVNAryExpr *LC = cast<SCEVNAryExpr>(LHS);
781 const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS);
782
783 // Lexicographically compare n-ary expressions.
784 unsigned LNumOps = LC->getNumOperands(), RNumOps = RC->getNumOperands();
785 if (LNumOps != RNumOps)
786 return (int)LNumOps - (int)RNumOps;
787
788 for (unsigned i = 0; i != LNumOps; ++i) {
789 auto X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI,
790 LC->getOperand(i), RC->getOperand(i), DT,
791 Depth + 1);
792 if (X != 0)
793 return X;
794 }
795 EqCacheSCEV.unionSets(LHS, RHS);
796 return 0;
797 }
798
799 case scUDivExpr: {
800 const SCEVUDivExpr *LC = cast<SCEVUDivExpr>(LHS);
801 const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS);
802
803 // Lexicographically compare udiv expressions.
804 auto X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getLHS(),
805 RC->getLHS(), DT, Depth + 1);
806 if (X != 0)
807 return X;
808 X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getRHS(),
809 RC->getRHS(), DT, Depth + 1);
810 if (X == 0)
811 EqCacheSCEV.unionSets(LHS, RHS);
812 return X;
813 }
814
815 case scPtrToInt:
816 case scTruncate:
817 case scZeroExtend:
818 case scSignExtend: {
819 const SCEVCastExpr *LC = cast<SCEVCastExpr>(LHS);
820 const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS);
821
822 // Compare cast expressions by operand.
823 auto X =
824 CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getOperand(),
825 RC->getOperand(), DT, Depth + 1);
826 if (X == 0)
827 EqCacheSCEV.unionSets(LHS, RHS);
828 return X;
829 }
830
831 case scCouldNotCompute:
832 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!")__builtin_unreachable();
833 }
834 llvm_unreachable("Unknown SCEV kind!")__builtin_unreachable();
835}
836
837/// Given a list of SCEV objects, order them by their complexity, and group
838/// objects of the same complexity together by value. When this routine is
839/// finished, we know that any duplicates in the vector are consecutive and that
840/// complexity is monotonically increasing.
841///
842/// Note that we go take special precautions to ensure that we get deterministic
843/// results from this routine. In other words, we don't want the results of
844/// this to depend on where the addresses of various SCEV objects happened to
845/// land in memory.
846static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops,
847 LoopInfo *LI, DominatorTree &DT) {
848 if (Ops.size() < 2) return; // Noop
849
850 EquivalenceClasses<const SCEV *> EqCacheSCEV;
851 EquivalenceClasses<const Value *> EqCacheValue;
852
853 // Whether LHS has provably less complexity than RHS.
854 auto IsLessComplex = [&](const SCEV *LHS, const SCEV *RHS) {
855 auto Complexity =
856 CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LHS, RHS, DT);
857 return Complexity && *Complexity < 0;
858 };
859 if (Ops.size() == 2) {
860 // This is the common case, which also happens to be trivially simple.
861 // Special case it.
862 const SCEV *&LHS = Ops[0], *&RHS = Ops[1];
863 if (IsLessComplex(RHS, LHS))
864 std::swap(LHS, RHS);
865 return;
866 }
867
868 // Do the rough sort by complexity.
869 llvm::stable_sort(Ops, [&](const SCEV *LHS, const SCEV *RHS) {
870 return IsLessComplex(LHS, RHS);
871 });
872
873 // Now that we are sorted by complexity, group elements of the same
874 // complexity. Note that this is, at worst, N^2, but the vector is likely to
875 // be extremely short in practice. Note that we take this approach because we
876 // do not want to depend on the addresses of the objects we are grouping.
877 for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
878 const SCEV *S = Ops[i];
879 unsigned Complexity = S->getSCEVType();
880
881 // If there are any objects of the same complexity and same value as this
882 // one, group them.
883 for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
884 if (Ops[j] == S) { // Found a duplicate.
885 // Move it to immediately after i'th element.
886 std::swap(Ops[i+1], Ops[j]);
887 ++i; // no need to rescan it.
888 if (i == e-2) return; // Done!
889 }
890 }
891 }
892}
893
894/// Returns true if \p Ops contains a huge SCEV (the subtree of S contains at
895/// least HugeExprThreshold nodes).
896static bool hasHugeExpression(ArrayRef<const SCEV *> Ops) {
897 return any_of(Ops, [](const SCEV *S) {
898 return S->getExpressionSize() >= HugeExprThreshold;
899 });
900}
901
902//===----------------------------------------------------------------------===//
903// Simple SCEV method implementations
904//===----------------------------------------------------------------------===//
905
906/// Compute BC(It, K). The result has width W. Assume, K > 0.
907static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K,
908 ScalarEvolution &SE,
909 Type *ResultTy) {
910 // Handle the simplest case efficiently.
911 if (K == 1)
912 return SE.getTruncateOrZeroExtend(It, ResultTy);
913
914 // We are using the following formula for BC(It, K):
915 //
916 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K!
917 //
918 // Suppose, W is the bitwidth of the return value. We must be prepared for
919 // overflow. Hence, we must assure that the result of our computation is
920 // equal to the accurate one modulo 2^W. Unfortunately, division isn't
921 // safe in modular arithmetic.
922 //
923 // However, this code doesn't use exactly that formula; the formula it uses
924 // is something like the following, where T is the number of factors of 2 in
925 // K! (i.e. trailing zeros in the binary representation of K!), and ^ is
926 // exponentiation:
927 //
928 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T)
929 //
930 // This formula is trivially equivalent to the previous formula. However,
931 // this formula can be implemented much more efficiently. The trick is that
932 // K! / 2^T is odd, and exact division by an odd number *is* safe in modular
933 // arithmetic. To do exact division in modular arithmetic, all we have
934 // to do is multiply by the inverse. Therefore, this step can be done at
935 // width W.
936 //
937 // The next issue is how to safely do the division by 2^T. The way this
938 // is done is by doing the multiplication step at a width of at least W + T
939 // bits. This way, the bottom W+T bits of the product are accurate. Then,
940 // when we perform the division by 2^T (which is equivalent to a right shift
941 // by T), the bottom W bits are accurate. Extra bits are okay; they'll get
942 // truncated out after the division by 2^T.
943 //
944 // In comparison to just directly using the first formula, this technique
945 // is much more efficient; using the first formula requires W * K bits,
946 // but this formula less than W + K bits. Also, the first formula requires
947 // a division step, whereas this formula only requires multiplies and shifts.
948 //
949 // It doesn't matter whether the subtraction step is done in the calculation
950 // width or the input iteration count's width; if the subtraction overflows,
951 // the result must be zero anyway. We prefer here to do it in the width of
952 // the induction variable because it helps a lot for certain cases; CodeGen
953 // isn't smart enough to ignore the overflow, which leads to much less
954 // efficient code if the width of the subtraction is wider than the native
955 // register width.
956 //
957 // (It's possible to not widen at all by pulling out factors of 2 before
958 // the multiplication; for example, K=2 can be calculated as
959 // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires
960 // extra arithmetic, so it's not an obvious win, and it gets
961 // much more complicated for K > 3.)
962
963 // Protection from insane SCEVs; this bound is conservative,
964 // but it probably doesn't matter.
965 if (K > 1000)
966 return SE.getCouldNotCompute();
967
968 unsigned W = SE.getTypeSizeInBits(ResultTy);
969
970 // Calculate K! / 2^T and T; we divide out the factors of two before
971 // multiplying for calculating K! / 2^T to avoid overflow.
972 // Other overflow doesn't matter because we only care about the bottom
973 // W bits of the result.
974 APInt OddFactorial(W, 1);
975 unsigned T = 1;
976 for (unsigned i = 3; i <= K; ++i) {
977 APInt Mult(W, i);
978 unsigned TwoFactors = Mult.countTrailingZeros();
979 T += TwoFactors;
980 Mult.lshrInPlace(TwoFactors);
981 OddFactorial *= Mult;
982 }
983
984 // We need at least W + T bits for the multiplication step
985 unsigned CalculationBits = W + T;
986
987 // Calculate 2^T, at width T+W.
988 APInt DivFactor = APInt::getOneBitSet(CalculationBits, T);
989
990 // Calculate the multiplicative inverse of K! / 2^T;
991 // this multiplication factor will perform the exact division by
992 // K! / 2^T.
993 APInt Mod = APInt::getSignedMinValue(W+1);
994 APInt MultiplyFactor = OddFactorial.zext(W+1);
995 MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod);
996 MultiplyFactor = MultiplyFactor.trunc(W);
997
998 // Calculate the product, at width T+W
999 IntegerType *CalculationTy = IntegerType::get(SE.getContext(),
1000 CalculationBits);
1001 const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy);
1002 for (unsigned i = 1; i != K; ++i) {
1003 const SCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i));
1004 Dividend = SE.getMulExpr(Dividend,
1005 SE.getTruncateOrZeroExtend(S, CalculationTy));
1006 }
1007
1008 // Divide by 2^T
1009 const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor));
1010
1011 // Truncate the result, and divide by K! / 2^T.
1012
1013 return SE.getMulExpr(SE.getConstant(MultiplyFactor),
1014 SE.getTruncateOrZeroExtend(DivResult, ResultTy));
1015}
1016
1017/// Return the value of this chain of recurrences at the specified iteration
1018/// number. We can evaluate this recurrence by multiplying each element in the
1019/// chain by the binomial coefficient corresponding to it. In other words, we
1020/// can evaluate {A,+,B,+,C,+,D} as:
1021///
1022/// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3)
1023///
1024/// where BC(It, k) stands for binomial coefficient.
1025const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It,
1026 ScalarEvolution &SE) const {
1027 return evaluateAtIteration(makeArrayRef(op_begin(), op_end()), It, SE);
1028}
1029
1030const SCEV *
1031SCEVAddRecExpr::evaluateAtIteration(ArrayRef<const SCEV *> Operands,
1032 const SCEV *It, ScalarEvolution &SE) {
1033 assert(Operands.size() > 0)(static_cast<void> (0));
1034 const SCEV *Result = Operands[0];
1035 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1036 // The computation is correct in the face of overflow provided that the
1037 // multiplication is performed _after_ the evaluation of the binomial
1038 // coefficient.
1039 const SCEV *Coeff = BinomialCoefficient(It, i, SE, Result->getType());
1040 if (isa<SCEVCouldNotCompute>(Coeff))
1041 return Coeff;
1042
1043 Result = SE.getAddExpr(Result, SE.getMulExpr(Operands[i], Coeff));
1044 }
1045 return Result;
1046}
1047
1048//===----------------------------------------------------------------------===//
1049// SCEV Expression folder implementations
1050//===----------------------------------------------------------------------===//
1051
1052const SCEV *ScalarEvolution::getLosslessPtrToIntExpr(const SCEV *Op,
1053 unsigned Depth) {
1054 assert(Depth <= 1 &&(static_cast<void> (0))
1055 "getLosslessPtrToIntExpr() should self-recurse at most once.")(static_cast<void> (0));
1056
1057 // We could be called with an integer-typed operands during SCEV rewrites.
1058 // Since the operand is an integer already, just perform zext/trunc/self cast.
1059 if (!Op->getType()->isPointerTy())
1060 return Op;
1061
1062 // What would be an ID for such a SCEV cast expression?
1063 FoldingSetNodeID ID;
1064 ID.AddInteger(scPtrToInt);
1065 ID.AddPointer(Op);
1066
1067 void *IP = nullptr;
1068
1069 // Is there already an expression for such a cast?
1070 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
1071 return S;
1072
1073 // It isn't legal for optimizations to construct new ptrtoint expressions
1074 // for non-integral pointers.
1075 if (getDataLayout().isNonIntegralPointerType(Op->getType()))
1076 return getCouldNotCompute();
1077
1078 Type *IntPtrTy = getDataLayout().getIntPtrType(Op->getType());
1079
1080 // We can only trivially model ptrtoint if SCEV's effective (integer) type
1081 // is sufficiently wide to represent all possible pointer values.
1082 // We could theoretically teach SCEV to truncate wider pointers, but
1083 // that isn't implemented for now.
1084 if (getDataLayout().getTypeSizeInBits(getEffectiveSCEVType(Op->getType())) !=
1085 getDataLayout().getTypeSizeInBits(IntPtrTy))
1086 return getCouldNotCompute();
1087
1088 // If not, is this expression something we can't reduce any further?
1089 if (auto *U = dyn_cast<SCEVUnknown>(Op)) {
1090 // Perform some basic constant folding. If the operand of the ptr2int cast
1091 // is a null pointer, don't create a ptr2int SCEV expression (that will be
1092 // left as-is), but produce a zero constant.
1093 // NOTE: We could handle a more general case, but lack motivational cases.
1094 if (isa<ConstantPointerNull>(U->getValue()))
1095 return getZero(IntPtrTy);
1096
1097 // Create an explicit cast node.
1098 // We can reuse the existing insert position since if we get here,
1099 // we won't have made any changes which would invalidate it.
1100 SCEV *S = new (SCEVAllocator)
1101 SCEVPtrToIntExpr(ID.Intern(SCEVAllocator), Op, IntPtrTy);
1102 UniqueSCEVs.InsertNode(S, IP);
1103 addToLoopUseLists(S);
1104 return S;
1105 }
1106
1107 assert(Depth == 0 && "getLosslessPtrToIntExpr() should not self-recurse for "(static_cast<void> (0))
1108 "non-SCEVUnknown's.")(static_cast<void> (0));
1109
1110 // Otherwise, we've got some expression that is more complex than just a
1111 // single SCEVUnknown. But we don't want to have a SCEVPtrToIntExpr of an
1112 // arbitrary expression, we want to have SCEVPtrToIntExpr of an SCEVUnknown
1113 // only, and the expressions must otherwise be integer-typed.
1114 // So sink the cast down to the SCEVUnknown's.
1115
1116 /// The SCEVPtrToIntSinkingRewriter takes a scalar evolution expression,
1117 /// which computes a pointer-typed value, and rewrites the whole expression
1118 /// tree so that *all* the computations are done on integers, and the only
1119 /// pointer-typed operands in the expression are SCEVUnknown.
1120 class SCEVPtrToIntSinkingRewriter
1121 : public SCEVRewriteVisitor<SCEVPtrToIntSinkingRewriter> {
1122 using Base = SCEVRewriteVisitor<SCEVPtrToIntSinkingRewriter>;
1123
1124 public:
1125 SCEVPtrToIntSinkingRewriter(ScalarEvolution &SE) : SCEVRewriteVisitor(SE) {}
1126
1127 static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE) {
1128 SCEVPtrToIntSinkingRewriter Rewriter(SE);
1129 return Rewriter.visit(Scev);
1130 }
1131
1132 const SCEV *visit(const SCEV *S) {
1133 Type *STy = S->getType();
1134 // If the expression is not pointer-typed, just keep it as-is.
1135 if (!STy->isPointerTy())
1136 return S;
1137 // Else, recursively sink the cast down into it.
1138 return Base::visit(S);
1139 }
1140
1141 const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
1142 SmallVector<const SCEV *, 2> Operands;
1143 bool Changed = false;
1144 for (auto *Op : Expr->operands()) {
1145 Operands.push_back(visit(Op));
1146 Changed |= Op != Operands.back();
1147 }
1148 return !Changed ? Expr : SE.getAddExpr(Operands, Expr->getNoWrapFlags());
1149 }
1150
1151 const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
1152 SmallVector<const SCEV *, 2> Operands;
1153 bool Changed = false;
1154 for (auto *Op : Expr->operands()) {
1155 Operands.push_back(visit(Op));
1156 Changed |= Op != Operands.back();
1157 }
1158 return !Changed ? Expr : SE.getMulExpr(Operands, Expr->getNoWrapFlags());
1159 }
1160
1161 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
1162 assert(Expr->getType()->isPointerTy() &&(static_cast<void> (0))
1163 "Should only reach pointer-typed SCEVUnknown's.")(static_cast<void> (0));
1164 return SE.getLosslessPtrToIntExpr(Expr, /*Depth=*/1);
1165 }
1166 };
1167
1168 // And actually perform the cast sinking.
1169 const SCEV *IntOp = SCEVPtrToIntSinkingRewriter::rewrite(Op, *this);
1170 assert(IntOp->getType()->isIntegerTy() &&(static_cast<void> (0))
1171 "We must have succeeded in sinking the cast, "(static_cast<void> (0))
1172 "and ending up with an integer-typed expression!")(static_cast<void> (0));
1173 return IntOp;
1174}
1175
1176const SCEV *ScalarEvolution::getPtrToIntExpr(const SCEV *Op, Type *Ty) {
1177 assert(Ty->isIntegerTy() && "Target type must be an integer type!")(static_cast<void> (0));
1178
1179 const SCEV *IntOp = getLosslessPtrToIntExpr(Op);
1180 if (isa<SCEVCouldNotCompute>(IntOp))
1181 return IntOp;
1182
1183 return getTruncateOrZeroExtend(IntOp, Ty);
1184}
1185
1186const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, Type *Ty,
1187 unsigned Depth) {
1188 assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) &&(static_cast<void> (0))
1189 "This is not a truncating conversion!")(static_cast<void> (0));
1190 assert(isSCEVable(Ty) &&(static_cast<void> (0))
1191 "This is not a conversion to a SCEVable type!")(static_cast<void> (0));
1192 assert(!Op->getType()->isPointerTy() && "Can't truncate pointer!")(static_cast<void> (0));
1193 Ty = getEffectiveSCEVType(Ty);
1194
1195 FoldingSetNodeID ID;
1196 ID.AddInteger(scTruncate);
1197 ID.AddPointer(Op);
1198 ID.AddPointer(Ty);
1199 void *IP = nullptr;
1200 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1201
1202 // Fold if the operand is constant.
1203 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1204 return getConstant(
1205 cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty)));
1206
1207 // trunc(trunc(x)) --> trunc(x)
1208 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op))
1209 return getTruncateExpr(ST->getOperand(), Ty, Depth + 1);
1210
1211 // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing
1212 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
1213 return getTruncateOrSignExtend(SS->getOperand(), Ty, Depth + 1);
1214
1215 // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing
1216 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
1217 return getTruncateOrZeroExtend(SZ->getOperand(), Ty, Depth + 1);
1218
1219 if (Depth > MaxCastDepth) {
1220 SCEV *S =
1221 new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator), Op, Ty);
1222 UniqueSCEVs.InsertNode(S, IP);
1223 addToLoopUseLists(S);
1224 return S;
1225 }
1226
1227 // trunc(x1 + ... + xN) --> trunc(x1) + ... + trunc(xN) and
1228 // trunc(x1 * ... * xN) --> trunc(x1) * ... * trunc(xN),
1229 // if after transforming we have at most one truncate, not counting truncates
1230 // that replace other casts.
1231 if (isa<SCEVAddExpr>(Op) || isa<SCEVMulExpr>(Op)) {
1232 auto *CommOp = cast<SCEVCommutativeExpr>(Op);
1233 SmallVector<const SCEV *, 4> Operands;
1234 unsigned numTruncs = 0;
1235 for (unsigned i = 0, e = CommOp->getNumOperands(); i != e && numTruncs < 2;
1236 ++i) {
1237 const SCEV *S = getTruncateExpr(CommOp->getOperand(i), Ty, Depth + 1);
1238 if (!isa<SCEVIntegralCastExpr>(CommOp->getOperand(i)) &&
1239 isa<SCEVTruncateExpr>(S))
1240 numTruncs++;
1241 Operands.push_back(S);
1242 }
1243 if (numTruncs < 2) {
1244 if (isa<SCEVAddExpr>(Op))
1245 return getAddExpr(Operands);
1246 else if (isa<SCEVMulExpr>(Op))
1247 return getMulExpr(Operands);
1248 else
1249 llvm_unreachable("Unexpected SCEV type for Op.")__builtin_unreachable();
1250 }
1251 // Although we checked in the beginning that ID is not in the cache, it is
1252 // possible that during recursion and different modification ID was inserted
1253 // into the cache. So if we find it, just return it.
1254 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
1255 return S;
1256 }
1257
1258 // If the input value is a chrec scev, truncate the chrec's operands.
1259 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
1260 SmallVector<const SCEV *, 4> Operands;
1261 for (const SCEV *Op : AddRec->operands())
1262 Operands.push_back(getTruncateExpr(Op, Ty, Depth + 1));
1263 return getAddRecExpr(Operands, AddRec->getLoop(), SCEV::FlagAnyWrap);
1264 }
1265
1266 // Return zero if truncating to known zeros.
1267 uint32_t MinTrailingZeros = GetMinTrailingZeros(Op);
1268 if (MinTrailingZeros >= getTypeSizeInBits(Ty))
1269 return getZero(Ty);
1270
1271 // The cast wasn't folded; create an explicit cast node. We can reuse
1272 // the existing insert position since if we get here, we won't have
1273 // made any changes which would invalidate it.
1274 SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator),
1275 Op, Ty);
1276 UniqueSCEVs.InsertNode(S, IP);
1277 addToLoopUseLists(S);
1278 return S;
1279}
1280
1281// Get the limit of a recurrence such that incrementing by Step cannot cause
1282// signed overflow as long as the value of the recurrence within the
1283// loop does not exceed this limit before incrementing.
1284static const SCEV *getSignedOverflowLimitForStep(const SCEV *Step,
1285 ICmpInst::Predicate *Pred,
1286 ScalarEvolution *SE) {
1287 unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
1288 if (SE->isKnownPositive(Step)) {
1289 *Pred = ICmpInst::ICMP_SLT;
1290 return SE->getConstant(APInt::getSignedMinValue(BitWidth) -
1291 SE->getSignedRangeMax(Step));
1292 }
1293 if (SE->isKnownNegative(Step)) {
1294 *Pred = ICmpInst::ICMP_SGT;
1295 return SE->getConstant(APInt::getSignedMaxValue(BitWidth) -
1296 SE->getSignedRangeMin(Step));
1297 }
1298 return nullptr;
1299}
1300
1301// Get the limit of a recurrence such that incrementing by Step cannot cause
1302// unsigned overflow as long as the value of the recurrence within the loop does
1303// not exceed this limit before incrementing.
1304static const SCEV *getUnsignedOverflowLimitForStep(const SCEV *Step,
1305 ICmpInst::Predicate *Pred,
1306 ScalarEvolution *SE) {
1307 unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
1308 *Pred = ICmpInst::ICMP_ULT;
1309
1310 return SE->getConstant(APInt::getMinValue(BitWidth) -
1311 SE->getUnsignedRangeMax(Step));
1312}
1313
1314namespace {
1315
1316struct ExtendOpTraitsBase {
1317 typedef const SCEV *(ScalarEvolution::*GetExtendExprTy)(const SCEV *, Type *,
1318 unsigned);
1319};
1320
1321// Used to make code generic over signed and unsigned overflow.
1322template <typename ExtendOp> struct ExtendOpTraits {
1323 // Members present:
1324 //
1325 // static const SCEV::NoWrapFlags WrapType;
1326 //
1327 // static const ExtendOpTraitsBase::GetExtendExprTy GetExtendExpr;
1328 //
1329 // static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1330 // ICmpInst::Predicate *Pred,
1331 // ScalarEvolution *SE);
1332};
1333
1334template <>
1335struct ExtendOpTraits<SCEVSignExtendExpr> : public ExtendOpTraitsBase {
1336 static const SCEV::NoWrapFlags WrapType = SCEV::FlagNSW;
1337
1338 static const GetExtendExprTy GetExtendExpr;
1339
1340 static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1341 ICmpInst::Predicate *Pred,
1342 ScalarEvolution *SE) {
1343 return getSignedOverflowLimitForStep(Step, Pred, SE);
1344 }
1345};
1346
1347const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1348 SCEVSignExtendExpr>::GetExtendExpr = &ScalarEvolution::getSignExtendExpr;
1349
1350template <>
1351struct ExtendOpTraits<SCEVZeroExtendExpr> : public ExtendOpTraitsBase {
1352 static const SCEV::NoWrapFlags WrapType = SCEV::FlagNUW;
1353
1354 static const GetExtendExprTy GetExtendExpr;
1355
1356 static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1357 ICmpInst::Predicate *Pred,
1358 ScalarEvolution *SE) {
1359 return getUnsignedOverflowLimitForStep(Step, Pred, SE);
1360 }
1361};
1362
1363const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1364 SCEVZeroExtendExpr>::GetExtendExpr = &ScalarEvolution::getZeroExtendExpr;
1365
1366} // end anonymous namespace
1367
1368// The recurrence AR has been shown to have no signed/unsigned wrap or something
1369// close to it. Typically, if we can prove NSW/NUW for AR, then we can just as
1370// easily prove NSW/NUW for its preincrement or postincrement sibling. This
1371// allows normalizing a sign/zero extended AddRec as such: {sext/zext(Step +
1372// Start),+,Step} => {(Step + sext/zext(Start),+,Step} As a result, the
1373// expression "Step + sext/zext(PreIncAR)" is congruent with
1374// "sext/zext(PostIncAR)"
1375template <typename ExtendOpTy>
1376static const SCEV *getPreStartForExtend(const SCEVAddRecExpr *AR, Type *Ty,
1377 ScalarEvolution *SE, unsigned Depth) {
1378 auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1379 auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1380
1381 const Loop *L = AR->getLoop();
1382 const SCEV *Start = AR->getStart();
1383 const SCEV *Step = AR->getStepRecurrence(*SE);
1384
1385 // Check for a simple looking step prior to loop entry.
1386 const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Start);
1387 if (!SA)
1388 return nullptr;
1389
1390 // Create an AddExpr for "PreStart" after subtracting Step. Full SCEV
1391 // subtraction is expensive. For this purpose, perform a quick and dirty
1392 // difference, by checking for Step in the operand list.
1393 SmallVector<const SCEV *, 4> DiffOps;
1394 for (const SCEV *Op : SA->operands())
1395 if (Op != Step)
1396 DiffOps.push_back(Op);
1397
1398 if (DiffOps.size() == SA->getNumOperands())
1399 return nullptr;
1400
1401 // Try to prove `WrapType` (SCEV::FlagNSW or SCEV::FlagNUW) on `PreStart` +
1402 // `Step`:
1403
1404 // 1. NSW/NUW flags on the step increment.
1405 auto PreStartFlags =
1406 ScalarEvolution::maskFlags(SA->getNoWrapFlags(), SCEV::FlagNUW);
1407 const SCEV *PreStart = SE->getAddExpr(DiffOps, PreStartFlags);
1408 const SCEVAddRecExpr *PreAR = dyn_cast<SCEVAddRecExpr>(
1409 SE->getAddRecExpr(PreStart, Step, L, SCEV::FlagAnyWrap));
1410
1411 // "{S,+,X} is <nsw>/<nuw>" and "the backedge is taken at least once" implies
1412 // "S+X does not sign/unsign-overflow".
1413 //
1414
1415 const SCEV *BECount = SE->getBackedgeTakenCount(L);
1416 if (PreAR && PreAR->getNoWrapFlags(WrapType) &&
1417 !isa<SCEVCouldNotCompute>(BECount) && SE->isKnownPositive(BECount))
1418 return PreStart;
1419
1420 // 2. Direct overflow check on the step operation's expression.
1421 unsigned BitWidth = SE->getTypeSizeInBits(AR->getType());
1422 Type *WideTy = IntegerType::get(SE->getContext(), BitWidth * 2);
1423 const SCEV *OperandExtendedStart =
1424 SE->getAddExpr((SE->*GetExtendExpr)(PreStart, WideTy, Depth),
1425 (SE->*GetExtendExpr)(Step, WideTy, Depth));
1426 if ((SE->*GetExtendExpr)(Start, WideTy, Depth) == OperandExtendedStart) {
1427 if (PreAR && AR->getNoWrapFlags(WrapType)) {
1428 // If we know `AR` == {`PreStart`+`Step`,+,`Step`} is `WrapType` (FlagNSW
1429 // or FlagNUW) and that `PreStart` + `Step` is `WrapType` too, then
1430 // `PreAR` == {`PreStart`,+,`Step`} is also `WrapType`. Cache this fact.
1431 SE->setNoWrapFlags(const_cast<SCEVAddRecExpr *>(PreAR), WrapType);
1432 }
1433 return PreStart;
1434 }
1435
1436 // 3. Loop precondition.
1437 ICmpInst::Predicate Pred;
1438 const SCEV *OverflowLimit =
1439 ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(Step, &Pred, SE);
1440
1441 if (OverflowLimit &&
1442 SE->isLoopEntryGuardedByCond(L, Pred, PreStart, OverflowLimit))
1443 return PreStart;
1444
1445 return nullptr;
1446}
1447
1448// Get the normalized zero or sign extended expression for this AddRec's Start.
1449template <typename ExtendOpTy>
1450static const SCEV *getExtendAddRecStart(const SCEVAddRecExpr *AR, Type *Ty,
1451 ScalarEvolution *SE,
1452 unsigned Depth) {
1453 auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1454
1455 const SCEV *PreStart = getPreStartForExtend<ExtendOpTy>(AR, Ty, SE, Depth);
1456 if (!PreStart)
1457 return (SE->*GetExtendExpr)(AR->getStart(), Ty, Depth);
1458
1459 return SE->getAddExpr((SE->*GetExtendExpr)(AR->getStepRecurrence(*SE), Ty,
1460 Depth),
1461 (SE->*GetExtendExpr)(PreStart, Ty, Depth));
1462}
1463
1464// Try to prove away overflow by looking at "nearby" add recurrences. A
1465// motivating example for this rule: if we know `{0,+,4}` is `ult` `-1` and it
1466// does not itself wrap then we can conclude that `{1,+,4}` is `nuw`.
1467//
1468// Formally:
1469//
1470// {S,+,X} == {S-T,+,X} + T
1471// => Ext({S,+,X}) == Ext({S-T,+,X} + T)
1472//
1473// If ({S-T,+,X} + T) does not overflow ... (1)
1474//
1475// RHS == Ext({S-T,+,X} + T) == Ext({S-T,+,X}) + Ext(T)
1476//
1477// If {S-T,+,X} does not overflow ... (2)
1478//
1479// RHS == Ext({S-T,+,X}) + Ext(T) == {Ext(S-T),+,Ext(X)} + Ext(T)
1480// == {Ext(S-T)+Ext(T),+,Ext(X)}
1481//
1482// If (S-T)+T does not overflow ... (3)
1483//
1484// RHS == {Ext(S-T)+Ext(T),+,Ext(X)} == {Ext(S-T+T),+,Ext(X)}
1485// == {Ext(S),+,Ext(X)} == LHS
1486//
1487// Thus, if (1), (2) and (3) are true for some T, then
1488// Ext({S,+,X}) == {Ext(S),+,Ext(X)}
1489//
1490// (3) is implied by (1) -- "(S-T)+T does not overflow" is simply "({S-T,+,X}+T)
1491// does not overflow" restricted to the 0th iteration. Therefore we only need
1492// to check for (1) and (2).
1493//
1494// In the current context, S is `Start`, X is `Step`, Ext is `ExtendOpTy` and T
1495// is `Delta` (defined below).
1496template <typename ExtendOpTy>
1497bool ScalarEvolution::proveNoWrapByVaryingStart(const SCEV *Start,
1498 const SCEV *Step,
1499 const Loop *L) {
1500 auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1501
1502 // We restrict `Start` to a constant to prevent SCEV from spending too much
1503 // time here. It is correct (but more expensive) to continue with a
1504 // non-constant `Start` and do a general SCEV subtraction to compute
1505 // `PreStart` below.
1506 const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start);
1507 if (!StartC)
1508 return false;
1509
1510 APInt StartAI = StartC->getAPInt();
1511
1512 for (unsigned Delta : {-2, -1, 1, 2}) {
1513 const SCEV *PreStart = getConstant(StartAI - Delta);
1514
1515 FoldingSetNodeID ID;
1516 ID.AddInteger(scAddRecExpr);
1517 ID.AddPointer(PreStart);
1518 ID.AddPointer(Step);
1519 ID.AddPointer(L);
1520 void *IP = nullptr;
1521 const auto *PreAR =
1522 static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
1523
1524 // Give up if we don't already have the add recurrence we need because
1525 // actually constructing an add recurrence is relatively expensive.
1526 if (PreAR && PreAR->getNoWrapFlags(WrapType)) { // proves (2)
1527 const SCEV *DeltaS = getConstant(StartC->getType(), Delta);
1528 ICmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
1529 const SCEV *Limit = ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(
1530 DeltaS, &Pred, this);
1531 if (Limit && isKnownPredicate(Pred, PreAR, Limit)) // proves (1)
1532 return true;
1533 }
1534 }
1535
1536 return false;
1537}
1538
1539// Finds an integer D for an expression (C + x + y + ...) such that the top
1540// level addition in (D + (C - D + x + y + ...)) would not wrap (signed or
1541// unsigned) and the number of trailing zeros of (C - D + x + y + ...) is
1542// maximized, where C is the \p ConstantTerm, x, y, ... are arbitrary SCEVs, and
1543// the (C + x + y + ...) expression is \p WholeAddExpr.
1544static APInt extractConstantWithoutWrapping(ScalarEvolution &SE,
1545 const SCEVConstant *ConstantTerm,
1546 const SCEVAddExpr *WholeAddExpr) {
1547 const APInt &C = ConstantTerm->getAPInt();
1548 const unsigned BitWidth = C.getBitWidth();
1549 // Find number of trailing zeros of (x + y + ...) w/o the C first:
1550 uint32_t TZ = BitWidth;
1551 for (unsigned I = 1, E = WholeAddExpr->getNumOperands(); I < E && TZ; ++I)
1552 TZ = std::min(TZ, SE.GetMinTrailingZeros(WholeAddExpr->getOperand(I)));
1553 if (TZ) {
1554 // Set D to be as many least significant bits of C as possible while still
1555 // guaranteeing that adding D to (C - D + x + y + ...) won't cause a wrap:
1556 return TZ < BitWidth ? C.trunc(TZ).zext(BitWidth) : C;
1557 }
1558 return APInt(BitWidth, 0);
1559}
1560
1561// Finds an integer D for an affine AddRec expression {C,+,x} such that the top
1562// level addition in (D + {C-D,+,x}) would not wrap (signed or unsigned) and the
1563// number of trailing zeros of (C - D + x * n) is maximized, where C is the \p
1564// ConstantStart, x is an arbitrary \p Step, and n is the loop trip count.
1565static APInt extractConstantWithoutWrapping(ScalarEvolution &SE,
1566 const APInt &ConstantStart,
1567 const SCEV *Step) {
1568 const unsigned BitWidth = ConstantStart.getBitWidth();
1569 const uint32_t TZ = SE.GetMinTrailingZeros(Step);
1570 if (TZ)
1571 return TZ < BitWidth ? ConstantStart.trunc(TZ).zext(BitWidth)
1572 : ConstantStart;
1573 return APInt(BitWidth, 0);
1574}
1575
1576const SCEV *
1577ScalarEvolution::getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
1578 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&(static_cast<void> (0))
1579 "This is not an extending conversion!")(static_cast<void> (0));
1580 assert(isSCEVable(Ty) &&(static_cast<void> (0))
1581 "This is not a conversion to a SCEVable type!")(static_cast<void> (0));
1582 assert(!Op->getType()->isPointerTy() && "Can't extend pointer!")(static_cast<void> (0));
1583 Ty = getEffectiveSCEVType(Ty);
1584
1585 // Fold if the operand is constant.
1586 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1587 return getConstant(
1588 cast<ConstantInt>(ConstantExpr::getZExt(SC->getValue(), Ty)));
1589
1590 // zext(zext(x)) --> zext(x)
1591 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
1592 return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1);
1593
1594 // Before doing any expensive analysis, check to see if we've already
1595 // computed a SCEV for this Op and Ty.
1596 FoldingSetNodeID ID;
1597 ID.AddInteger(scZeroExtend);
1598 ID.AddPointer(Op);
1599 ID.AddPointer(Ty);
1600 void *IP = nullptr;
1601 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1602 if (Depth > MaxCastDepth) {
1603 SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
1604 Op, Ty);
1605 UniqueSCEVs.InsertNode(S, IP);
1606 addToLoopUseLists(S);
1607 return S;
1608 }
1609
1610 // zext(trunc(x)) --> zext(x) or x or trunc(x)
1611 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
1612 // It's possible the bits taken off by the truncate were all zero bits. If
1613 // so, we should be able to simplify this further.
1614 const SCEV *X = ST->getOperand();
1615 ConstantRange CR = getUnsignedRange(X);
1616 unsigned TruncBits = getTypeSizeInBits(ST->getType());
1617 unsigned NewBits = getTypeSizeInBits(Ty);
1618 if (CR.truncate(TruncBits).zeroExtend(NewBits).contains(
1619 CR.zextOrTrunc(NewBits)))
1620 return getTruncateOrZeroExtend(X, Ty, Depth);
1621 }
1622
1623 // If the input value is a chrec scev, and we can prove that the value
1624 // did not overflow the old, smaller, value, we can zero extend all of the
1625 // operands (often constants). This allows analysis of something like
1626 // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
1627 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
1628 if (AR->isAffine()) {
1629 const SCEV *Start = AR->getStart();
1630 const SCEV *Step = AR->getStepRecurrence(*this);
1631 unsigned BitWidth = getTypeSizeInBits(AR->getType());
1632 const Loop *L = AR->getLoop();
1633
1634 if (!AR->hasNoUnsignedWrap()) {
1635 auto NewFlags = proveNoWrapViaConstantRanges(AR);
1636 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), NewFlags);
1637 }
1638
1639 // If we have special knowledge that this addrec won't overflow,
1640 // we don't need to do any further analysis.
1641 if (AR->hasNoUnsignedWrap())
1642 return getAddRecExpr(
1643 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, Depth + 1),
1644 getZeroExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
1645
1646 // Check whether the backedge-taken count is SCEVCouldNotCompute.
1647 // Note that this serves two purposes: It filters out loops that are
1648 // simply not analyzable, and it covers the case where this code is
1649 // being called from within backedge-taken count analysis, such that
1650 // attempting to ask for the backedge-taken count would likely result
1651 // in infinite recursion. In the later case, the analysis code will
1652 // cope with a conservative value, and it will take care to purge
1653 // that value once it has finished.
1654 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L);
1655 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
1656 // Manually compute the final value for AR, checking for overflow.
1657
1658 // Check whether the backedge-taken count can be losslessly casted to
1659 // the addrec's type. The count is always unsigned.
1660 const SCEV *CastedMaxBECount =
1661 getTruncateOrZeroExtend(MaxBECount, Start->getType(), Depth);
1662 const SCEV *RecastedMaxBECount = getTruncateOrZeroExtend(
1663 CastedMaxBECount, MaxBECount->getType(), Depth);
1664 if (MaxBECount == RecastedMaxBECount) {
1665 Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
1666 // Check whether Start+Step*MaxBECount has no unsigned overflow.
1667 const SCEV *ZMul = getMulExpr(CastedMaxBECount, Step,
1668 SCEV::FlagAnyWrap, Depth + 1);
1669 const SCEV *ZAdd = getZeroExtendExpr(getAddExpr(Start, ZMul,
1670 SCEV::FlagAnyWrap,
1671 Depth + 1),
1672 WideTy, Depth + 1);
1673 const SCEV *WideStart = getZeroExtendExpr(Start, WideTy, Depth + 1);
1674 const SCEV *WideMaxBECount =
1675 getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1);
1676 const SCEV *OperandExtendedAdd =
1677 getAddExpr(WideStart,
1678 getMulExpr(WideMaxBECount,
1679 getZeroExtendExpr(Step, WideTy, Depth + 1),
1680 SCEV::FlagAnyWrap, Depth + 1),
1681 SCEV::FlagAnyWrap, Depth + 1);
1682 if (ZAdd == OperandExtendedAdd) {
1683 // Cache knowledge of AR NUW, which is propagated to this AddRec.
1684 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNUW);
1685 // Return the expression with the addrec on the outside.
1686 return getAddRecExpr(
1687 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
1688 Depth + 1),
1689 getZeroExtendExpr(Step, Ty, Depth + 1), L,
1690 AR->getNoWrapFlags());
1691 }
1692 // Similar to above, only this time treat the step value as signed.
1693 // This covers loops that count down.
1694 OperandExtendedAdd =
1695 getAddExpr(WideStart,
1696 getMulExpr(WideMaxBECount,
1697 getSignExtendExpr(Step, WideTy, Depth + 1),
1698 SCEV::FlagAnyWrap, Depth + 1),
1699 SCEV::FlagAnyWrap, Depth + 1);
1700 if (ZAdd == OperandExtendedAdd) {
1701 // Cache knowledge of AR NW, which is propagated to this AddRec.
1702 // Negative step causes unsigned wrap, but it still can't self-wrap.
1703 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNW);
1704 // Return the expression with the addrec on the outside.
1705 return getAddRecExpr(
1706 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
1707 Depth + 1),
1708 getSignExtendExpr(Step, Ty, Depth + 1), L,
1709 AR->getNoWrapFlags());
1710 }
1711 }
1712 }
1713
1714 // Normally, in the cases we can prove no-overflow via a
1715 // backedge guarding condition, we can also compute a backedge
1716 // taken count for the loop. The exceptions are assumptions and
1717 // guards present in the loop -- SCEV is not great at exploiting
1718 // these to compute max backedge taken counts, but can still use
1719 // these to prove lack of overflow. Use this fact to avoid
1720 // doing extra work that may not pay off.
1721 if (!isa<SCEVCouldNotCompute>(MaxBECount) || HasGuards ||
1722 !AC.assumptions().empty()) {
1723
1724 auto NewFlags = proveNoUnsignedWrapViaInduction(AR);
1725 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), NewFlags);
1726 if (AR->hasNoUnsignedWrap()) {
1727 // Same as nuw case above - duplicated here to avoid a compile time
1728 // issue. It's not clear that the order of checks does matter, but
1729 // it's one of two issue possible causes for a change which was
1730 // reverted. Be conservative for the moment.
1731 return getAddRecExpr(
1732 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
1733 Depth + 1),
1734 getZeroExtendExpr(Step, Ty, Depth + 1), L,
1735 AR->getNoWrapFlags());
1736 }
1737
1738 // For a negative step, we can extend the operands iff doing so only
1739 // traverses values in the range zext([0,UINT_MAX]).
1740 if (isKnownNegative(Step)) {
1741 const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) -
1742 getSignedRangeMin(Step));
1743 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) ||
1744 isKnownOnEveryIteration(ICmpInst::ICMP_UGT, AR, N)) {
1745 // Cache knowledge of AR NW, which is propagated to this
1746 // AddRec. Negative step causes unsigned wrap, but it
1747 // still can't self-wrap.
1748 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNW);
1749 // Return the expression with the addrec on the outside.
1750 return getAddRecExpr(
1751 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
1752 Depth + 1),
1753 getSignExtendExpr(Step, Ty, Depth + 1), L,
1754 AR->getNoWrapFlags());
1755 }
1756 }
1757 }
1758
1759 // zext({C,+,Step}) --> (zext(D) + zext({C-D,+,Step}))<nuw><nsw>
1760 // if D + (C - D + Step * n) could be proven to not unsigned wrap
1761 // where D maximizes the number of trailing zeros of (C - D + Step * n)
1762 if (const auto *SC = dyn_cast<SCEVConstant>(Start)) {
1763 const APInt &C = SC->getAPInt();
1764 const APInt &D = extractConstantWithoutWrapping(*this, C, Step);
1765 if (D != 0) {
1766 const SCEV *SZExtD = getZeroExtendExpr(getConstant(D), Ty, Depth);
1767 const SCEV *SResidual =
1768 getAddRecExpr(getConstant(C - D), Step, L, AR->getNoWrapFlags());
1769 const SCEV *SZExtR = getZeroExtendExpr(SResidual, Ty, Depth + 1);
1770 return getAddExpr(SZExtD, SZExtR,
1771 (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW),
1772 Depth + 1);
1773 }
1774 }
1775
1776 if (proveNoWrapByVaryingStart<SCEVZeroExtendExpr>(Start, Step, L)) {
1777 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNUW);
1778 return getAddRecExpr(
1779 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, Depth + 1),
1780 getZeroExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
1781 }
1782 }
1783
1784 // zext(A % B) --> zext(A) % zext(B)
1785 {
1786 const SCEV *LHS;
1787 const SCEV *RHS;
1788 if (matchURem(Op, LHS, RHS))
1789 return getURemExpr(getZeroExtendExpr(LHS, Ty, Depth + 1),
1790 getZeroExtendExpr(RHS, Ty, Depth + 1));
1791 }
1792
1793 // zext(A / B) --> zext(A) / zext(B).
1794 if (auto *Div = dyn_cast<SCEVUDivExpr>(Op))
1795 return getUDivExpr(getZeroExtendExpr(Div->getLHS(), Ty, Depth + 1),
1796 getZeroExtendExpr(Div->getRHS(), Ty, Depth + 1));
1797
1798 if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) {
1799 // zext((A + B + ...)<nuw>) --> (zext(A) + zext(B) + ...)<nuw>
1800 if (SA->hasNoUnsignedWrap()) {
1801 // If the addition does not unsign overflow then we can, by definition,
1802 // commute the zero extension with the addition operation.
1803 SmallVector<const SCEV *, 4> Ops;
1804 for (const auto *Op : SA->operands())
1805 Ops.push_back(getZeroExtendExpr(Op, Ty, Depth + 1));
1806 return getAddExpr(Ops, SCEV::FlagNUW, Depth + 1);
1807 }
1808
1809 // zext(C + x + y + ...) --> (zext(D) + zext((C - D) + x + y + ...))
1810 // if D + (C - D + x + y + ...) could be proven to not unsigned wrap
1811 // where D maximizes the number of trailing zeros of (C - D + x + y + ...)
1812 //
1813 // Often address arithmetics contain expressions like
1814 // (zext (add (shl X, C1), C2)), for instance, (zext (5 + (4 * X))).
1815 // This transformation is useful while proving that such expressions are
1816 // equal or differ by a small constant amount, see LoadStoreVectorizer pass.
1817 if (const auto *SC = dyn_cast<SCEVConstant>(SA->getOperand(0))) {
1818 const APInt &D = extractConstantWithoutWrapping(*this, SC, SA);
1819 if (D != 0) {
1820 const SCEV *SZExtD = getZeroExtendExpr(getConstant(D), Ty, Depth);
1821 const SCEV *SResidual =
1822 getAddExpr(getConstant(-D), SA, SCEV::FlagAnyWrap, Depth);
1823 const SCEV *SZExtR = getZeroExtendExpr(SResidual, Ty, Depth + 1);
1824 return getAddExpr(SZExtD, SZExtR,
1825 (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW),
1826 Depth + 1);
1827 }
1828 }
1829 }
1830
1831 if (auto *SM = dyn_cast<SCEVMulExpr>(Op)) {
1832 // zext((A * B * ...)<nuw>) --> (zext(A) * zext(B) * ...)<nuw>
1833 if (SM->hasNoUnsignedWrap()) {
1834 // If the multiply does not unsign overflow then we can, by definition,
1835 // commute the zero extension with the multiply operation.
1836 SmallVector<const SCEV *, 4> Ops;
1837 for (const auto *Op : SM->operands())
1838 Ops.push_back(getZeroExtendExpr(Op, Ty, Depth + 1));
1839 return getMulExpr(Ops, SCEV::FlagNUW, Depth + 1);
1840 }
1841
1842 // zext(2^K * (trunc X to iN)) to iM ->
1843 // 2^K * (zext(trunc X to i{N-K}) to iM)<nuw>
1844 //
1845 // Proof:
1846 //
1847 // zext(2^K * (trunc X to iN)) to iM
1848 // = zext((trunc X to iN) << K) to iM
1849 // = zext((trunc X to i{N-K}) << K)<nuw> to iM
1850 // (because shl removes the top K bits)
1851 // = zext((2^K * (trunc X to i{N-K}))<nuw>) to iM
1852 // = (2^K * (zext(trunc X to i{N-K}) to iM))<nuw>.
1853 //
1854 if (SM->getNumOperands() == 2)
1855 if (auto *MulLHS = dyn_cast<SCEVConstant>(SM->getOperand(0)))
1856 if (MulLHS->getAPInt().isPowerOf2())
1857 if (auto *TruncRHS = dyn_cast<SCEVTruncateExpr>(SM->getOperand(1))) {
1858 int NewTruncBits = getTypeSizeInBits(TruncRHS->getType()) -
1859 MulLHS->getAPInt().logBase2();
1860 Type *NewTruncTy = IntegerType::get(getContext(), NewTruncBits);
1861 return getMulExpr(
1862 getZeroExtendExpr(MulLHS, Ty),
1863 getZeroExtendExpr(
1864 getTruncateExpr(TruncRHS->getOperand(), NewTruncTy), Ty),
1865 SCEV::FlagNUW, Depth + 1);
1866 }
1867 }
1868
1869 // The cast wasn't folded; create an explicit cast node.
1870 // Recompute the insert position, as it may have been invalidated.
1871 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1872 SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
1873 Op, Ty);
1874 UniqueSCEVs.InsertNode(S, IP);
1875 addToLoopUseLists(S);
1876 return S;
1877}
1878
1879const SCEV *
1880ScalarEvolution::getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
1881 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&(static_cast<void> (0))
1882 "This is not an extending conversion!")(static_cast<void> (0));
1883 assert(isSCEVable(Ty) &&(static_cast<void> (0))
1884 "This is not a conversion to a SCEVable type!")(static_cast<void> (0));
1885 assert(!Op->getType()->isPointerTy() && "Can't extend pointer!")(static_cast<void> (0));
1886 Ty = getEffectiveSCEVType(Ty);
1887
1888 // Fold if the operand is constant.
1889 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1890 return getConstant(
1891 cast<ConstantInt>(ConstantExpr::getSExt(SC->getValue(), Ty)));
1892
1893 // sext(sext(x)) --> sext(x)
1894 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
1895 return getSignExtendExpr(SS->getOperand(), Ty, Depth + 1);
1896
1897 // sext(zext(x)) --> zext(x)
1898 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
1899 return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1);
1900
1901 // Before doing any expensive analysis, check to see if we've already
1902 // computed a SCEV for this Op and Ty.
1903 FoldingSetNodeID ID;
1904 ID.AddInteger(scSignExtend);
1905 ID.AddPointer(Op);
1906 ID.AddPointer(Ty);
1907 void *IP = nullptr;
1908 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1909 // Limit recursion depth.
1910 if (Depth > MaxCastDepth) {
1911 SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
1912 Op, Ty);
1913 UniqueSCEVs.InsertNode(S, IP);
1914 addToLoopUseLists(S);
1915 return S;
1916 }
1917
1918 // sext(trunc(x)) --> sext(x) or x or trunc(x)
1919 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
1920 // It's possible the bits taken off by the truncate were all sign bits. If
1921 // so, we should be able to simplify this further.
1922 const SCEV *X = ST->getOperand();
1923 ConstantRange CR = getSignedRange(X);
1924 unsigned TruncBits = getTypeSizeInBits(ST->getType());
1925 unsigned NewBits = getTypeSizeInBits(Ty);
1926 if (CR.truncate(TruncBits).signExtend(NewBits).contains(
1927 CR.sextOrTrunc(NewBits)))
1928 return getTruncateOrSignExtend(X, Ty, Depth);
1929 }
1930
1931 if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) {
1932 // sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw>
1933 if (SA->hasNoSignedWrap()) {
1934 // If the addition does not sign overflow then we can, by definition,
1935 // commute the sign extension with the addition operation.
1936 SmallVector<const SCEV *, 4> Ops;
1937 for (const auto *Op : SA->operands())
1938 Ops.push_back(getSignExtendExpr(Op, Ty, Depth + 1));
1939 return getAddExpr(Ops, SCEV::FlagNSW, Depth + 1);
1940 }
1941
1942 // sext(C + x + y + ...) --> (sext(D) + sext((C - D) + x + y + ...))
1943 // if D + (C - D + x + y + ...) could be proven to not signed wrap
1944 // where D maximizes the number of trailing zeros of (C - D + x + y + ...)
1945 //
1946 // For instance, this will bring two seemingly different expressions:
1947 // 1 + sext(5 + 20 * %x + 24 * %y) and
1948 // sext(6 + 20 * %x + 24 * %y)
1949 // to the same form:
1950 // 2 + sext(4 + 20 * %x + 24 * %y)
1951 if (const auto *SC = dyn_cast<SCEVConstant>(SA->getOperand(0))) {
1952 const APInt &D = extractConstantWithoutWrapping(*this, SC, SA);
1953 if (D != 0) {
1954 const SCEV *SSExtD = getSignExtendExpr(getConstant(D), Ty, Depth);
1955 const SCEV *SResidual =
1956 getAddExpr(getConstant(-D), SA, SCEV::FlagAnyWrap, Depth);
1957 const SCEV *SSExtR = getSignExtendExpr(SResidual, Ty, Depth + 1);
1958 return getAddExpr(SSExtD, SSExtR,
1959 (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW),
1960 Depth + 1);
1961 }
1962 }
1963 }
1964 // If the input value is a chrec scev, and we can prove that the value
1965 // did not overflow the old, smaller, value, we can sign extend all of the
1966 // operands (often constants). This allows analysis of something like
1967 // this: for (signed char X = 0; X < 100; ++X) { int Y = X; }
1968 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
1969 if (AR->isAffine()) {
1970 const SCEV *Start = AR->getStart();
1971 const SCEV *Step = AR->getStepRecurrence(*this);
1972 unsigned BitWidth = getTypeSizeInBits(AR->getType());
1973 const Loop *L = AR->getLoop();
1974
1975 if (!AR->hasNoSignedWrap()) {
1976 auto NewFlags = proveNoWrapViaConstantRanges(AR);
1977 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), NewFlags);
1978 }
1979
1980 // If we have special knowledge that this addrec won't overflow,
1981 // we don't need to do any further analysis.
1982 if (AR->hasNoSignedWrap())
1983 return getAddRecExpr(
1984 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1),
1985 getSignExtendExpr(Step, Ty, Depth + 1), L, SCEV::FlagNSW);
1986
1987 // Check whether the backedge-taken count is SCEVCouldNotCompute.
1988 // Note that this serves two purposes: It filters out loops that are
1989 // simply not analyzable, and it covers the case where this code is
1990 // being called from within backedge-taken count analysis, such that
1991 // attempting to ask for the backedge-taken count would likely result
1992 // in infinite recursion. In the later case, the analysis code will
1993 // cope with a conservative value, and it will take care to purge
1994 // that value once it has finished.
1995 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L);
1996 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
1997 // Manually compute the final value for AR, checking for
1998 // overflow.
1999
2000 // Check whether the backedge-taken count can be losslessly casted to
2001 // the addrec's type. The count is always unsigned.
2002 const SCEV *CastedMaxBECount =
2003 getTruncateOrZeroExtend(MaxBECount, Start->getType(), Depth);
2004 const SCEV *RecastedMaxBECount = getTruncateOrZeroExtend(
2005 CastedMaxBECount, MaxBECount->getType(), Depth);
2006 if (MaxBECount == RecastedMaxBECount) {
2007 Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
2008 // Check whether Start+Step*MaxBECount has no signed overflow.
2009 const SCEV *SMul = getMulExpr(CastedMaxBECount, Step,
2010 SCEV::FlagAnyWrap, Depth + 1);
2011 const SCEV *SAdd = getSignExtendExpr(getAddExpr(Start, SMul,
2012 SCEV::FlagAnyWrap,
2013 Depth + 1),
2014 WideTy, Depth + 1);
2015 const SCEV *WideStart = getSignExtendExpr(Start, WideTy, Depth + 1);
2016 const SCEV *WideMaxBECount =
2017 getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1);
2018 const SCEV *OperandExtendedAdd =
2019 getAddExpr(WideStart,
2020 getMulExpr(WideMaxBECount,
2021 getSignExtendExpr(Step, WideTy, Depth + 1),
2022 SCEV::FlagAnyWrap, Depth + 1),
2023 SCEV::FlagAnyWrap, Depth + 1);
2024 if (SAdd == OperandExtendedAdd) {
2025 // Cache knowledge of AR NSW, which is propagated to this AddRec.
2026 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNSW);
2027 // Return the expression with the addrec on the outside.
2028 return getAddRecExpr(
2029 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this,
2030 Depth + 1),
2031 getSignExtendExpr(Step, Ty, Depth + 1), L,
2032 AR->getNoWrapFlags());
2033 }
2034 // Similar to above, only this time treat the step value as unsigned.
2035 // This covers loops that count up with an unsigned step.
2036 OperandExtendedAdd =
2037 getAddExpr(WideStart,
2038 getMulExpr(WideMaxBECount,
2039 getZeroExtendExpr(Step, WideTy, Depth + 1),
2040 SCEV::FlagAnyWrap, Depth + 1),
2041 SCEV::FlagAnyWrap, Depth + 1);
2042 if (SAdd == OperandExtendedAdd) {
2043 // If AR wraps around then
2044 //
2045 // abs(Step) * MaxBECount > unsigned-max(AR->getType())
2046 // => SAdd != OperandExtendedAdd
2047 //
2048 // Thus (AR is not NW => SAdd != OperandExtendedAdd) <=>
2049 // (SAdd == OperandExtendedAdd => AR is NW)
2050
2051 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNW);
2052
2053 // Return the expression with the addrec on the outside.
2054 return getAddRecExpr(
2055 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this,
2056 Depth + 1),
2057 getZeroExtendExpr(Step, Ty, Depth + 1), L,
2058 AR->getNoWrapFlags());
2059 }
2060 }
2061 }
2062
2063 auto NewFlags = proveNoSignedWrapViaInduction(AR);
2064 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), NewFlags);
2065 if (AR->hasNoSignedWrap()) {
2066 // Same as nsw case above - duplicated here to avoid a compile time
2067 // issue. It's not clear that the order of checks does matter, but
2068 // it's one of two issue possible causes for a change which was
2069 // reverted. Be conservative for the moment.
2070 return getAddRecExpr(
2071 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1),
2072 getSignExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
2073 }
2074
2075 // sext({C,+,Step}) --> (sext(D) + sext({C-D,+,Step}))<nuw><nsw>
2076 // if D + (C - D + Step * n) could be proven to not signed wrap
2077 // where D maximizes the number of trailing zeros of (C - D + Step * n)
2078 if (const auto *SC = dyn_cast<SCEVConstant>(Start)) {
2079 const APInt &C = SC->getAPInt();
2080 const APInt &D = extractConstantWithoutWrapping(*this, C, Step);
2081 if (D != 0) {
2082 const SCEV *SSExtD = getSignExtendExpr(getConstant(D), Ty, Depth);
2083 const SCEV *SResidual =
2084 getAddRecExpr(getConstant(C - D), Step, L, AR->getNoWrapFlags());
2085 const SCEV *SSExtR = getSignExtendExpr(SResidual, Ty, Depth + 1);
2086 return getAddExpr(SSExtD, SSExtR,
2087 (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW),
2088 Depth + 1);
2089 }
2090 }
2091
2092 if (proveNoWrapByVaryingStart<SCEVSignExtendExpr>(Start, Step, L)) {
2093 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNSW);
2094 return getAddRecExpr(
2095 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1),
2096 getSignExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
2097 }
2098 }
2099
2100 // If the input value is provably positive and we could not simplify
2101 // away the sext build a zext instead.
2102 if (isKnownNonNegative(Op))
2103 return getZeroExtendExpr(Op, Ty, Depth + 1);
2104
2105 // The cast wasn't folded; create an explicit cast node.
2106 // Recompute the insert position, as it may have been invalidated.
2107 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2108 SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
2109 Op, Ty);
2110 UniqueSCEVs.InsertNode(S, IP);
2111 addToLoopUseLists(S);
2112 return S;
2113}
2114
2115/// getAnyExtendExpr - Return a SCEV for the given operand extended with
2116/// unspecified bits out to the given type.
2117const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op,
2118 Type *Ty) {
2119 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&(static_cast<void> (0))
2120 "This is not an extending conversion!")(static_cast<void> (0));
2121 assert(isSCEVable(Ty) &&(static_cast<void> (0))
2122 "This is not a conversion to a SCEVable type!")(static_cast<void> (0));
2123 Ty = getEffectiveSCEVType(Ty);
2124
2125 // Sign-extend negative constants.
2126 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
2127 if (SC->getAPInt().isNegative())
2128 return getSignExtendExpr(Op, Ty);
2129
2130 // Peel off a truncate cast.
2131 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) {
2132 const SCEV *NewOp = T->getOperand();
2133 if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty))
2134 return getAnyExtendExpr(NewOp, Ty);
2135 return getTruncateOrNoop(NewOp, Ty);
2136 }
2137
2138 // Next try a zext cast. If the cast is folded, use it.
2139 const SCEV *ZExt = getZeroExtendExpr(Op, Ty);
2140 if (!isa<SCEVZeroExtendExpr>(ZExt))
2141 return ZExt;
2142
2143 // Next try a sext cast. If the cast is folded, use it.
2144 const SCEV *SExt = getSignExtendExpr(Op, Ty);
2145 if (!isa<SCEVSignExtendExpr>(SExt))
2146 return SExt;
2147
2148 // Force the cast to be folded into the operands of an addrec.
2149 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) {
2150 SmallVector<const SCEV *, 4> Ops;
2151 for (const SCEV *Op : AR->operands())
2152 Ops.push_back(getAnyExtendExpr(Op, Ty));
2153 return getAddRecExpr(Ops, AR->getLoop(), SCEV::FlagNW);
2154 }
2155
2156 // If the expression is obviously signed, use the sext cast value.
2157 if (isa<SCEVSMaxExpr>(Op))
2158 return SExt;
2159
2160 // Absent any other information, use the zext cast value.
2161 return ZExt;
2162}
2163
2164/// Process the given Ops list, which is a list of operands to be added under
2165/// the given scale, update the given map. This is a helper function for
2166/// getAddRecExpr. As an example of what it does, given a sequence of operands
2167/// that would form an add expression like this:
2168///
2169/// m + n + 13 + (A * (o + p + (B * (q + m + 29)))) + r + (-1 * r)
2170///
2171/// where A and B are constants, update the map with these values:
2172///
2173/// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0)
2174///
2175/// and add 13 + A*B*29 to AccumulatedConstant.
2176/// This will allow getAddRecExpr to produce this:
2177///
2178/// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B)
2179///
2180/// This form often exposes folding opportunities that are hidden in
2181/// the original operand list.
2182///
2183/// Return true iff it appears that any interesting folding opportunities
2184/// may be exposed. This helps getAddRecExpr short-circuit extra work in
2185/// the common case where no interesting opportunities are present, and
2186/// is also used as a check to avoid infinite recursion.
2187static bool
2188CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M,
2189 SmallVectorImpl<const SCEV *> &NewOps,
2190 APInt &AccumulatedConstant,
2191 const SCEV *const *Ops, size_t NumOperands,
2192 const APInt &Scale,
2193 ScalarEvolution &SE) {
2194 bool Interesting = false;
2195
2196 // Iterate over the add operands. They are sorted, with constants first.
2197 unsigned i = 0;
2198 while (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
2199 ++i;
2200 // Pull a buried constant out to the outside.
2201 if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero())
2202 Interesting = true;
2203 AccumulatedConstant += Scale * C->getAPInt();
2204 }
2205
2206 // Next comes everything else. We're especially interested in multiplies
2207 // here, but they're in the middle, so just visit the rest with one loop.
2208 for (; i != NumOperands; ++i) {
2209 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]);
2210 if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) {
2211 APInt NewScale =
2212 Scale * cast<SCEVConstant>(Mul->getOperand(0))->getAPInt();
2213 if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) {
2214 // A multiplication of a constant with another add; recurse.
2215 const SCEVAddExpr *Add = cast<SCEVAddExpr>(Mul->getOperand(1));
2216 Interesting |=
2217 CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
2218 Add->op_begin(), Add->getNumOperands(),
2219 NewScale, SE);
2220 } else {
2221 // A multiplication of a constant with some other value. Update
2222 // the map.
2223 SmallVector<const SCEV *, 4> MulOps(drop_begin(Mul->operands()));
2224 const SCEV *Key = SE.getMulExpr(MulOps);
2225 auto Pair = M.insert({Key, NewScale});
2226 if (Pair.second) {
2227 NewOps.push_back(Pair.first->first);
2228 } else {
2229 Pair.first->second += NewScale;
2230 // The map already had an entry for this value, which may indicate
2231 // a folding opportunity.
2232 Interesting = true;
2233 }
2234 }
2235 } else {
2236 // An ordinary operand. Update the map.
2237 std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
2238 M.insert({Ops[i], Scale});
2239 if (Pair.second) {
2240 NewOps.push_back(Pair.first->first);
2241 } else {
2242 Pair.first->second += Scale;
2243 // The map already had an entry for this value, which may indicate
2244 // a folding opportunity.
2245 Interesting = true;
2246 }
2247 }
2248 }
2249
2250 return Interesting;
2251}
2252
2253bool ScalarEvolution::willNotOverflow(Instruction::BinaryOps BinOp, bool Signed,
2254 const SCEV *LHS, const SCEV *RHS) {
2255 const SCEV *(ScalarEvolution::*Operation)(const SCEV *, const SCEV *,
2256 SCEV::NoWrapFlags, unsigned);
2257 switch (BinOp) {
2258 default:
2259 llvm_unreachable("Unsupported binary op")__builtin_unreachable();
2260 case Instruction::Add:
2261 Operation = &ScalarEvolution::getAddExpr;
2262 break;
2263 case Instruction::Sub:
2264 Operation = &ScalarEvolution::getMinusSCEV;
2265 break;
2266 case Instruction::Mul:
2267 Operation = &ScalarEvolution::getMulExpr;
2268 break;
2269 }
2270
2271 const SCEV *(ScalarEvolution::*Extension)(const SCEV *, Type *, unsigned) =
2272 Signed ? &ScalarEvolution::getSignExtendExpr
2273 : &ScalarEvolution::getZeroExtendExpr;
2274
2275 // Check ext(LHS op RHS) == ext(LHS) op ext(RHS)
2276 auto *NarrowTy = cast<IntegerType>(LHS->getType());
2277 auto *WideTy =
2278 IntegerType::get(NarrowTy->getContext(), NarrowTy->getBitWidth() * 2);
2279
2280 const SCEV *A = (this->*Extension)(
2281 (this->*Operation)(LHS, RHS, SCEV::FlagAnyWrap, 0), WideTy, 0);
2282 const SCEV *B = (this->*Operation)((this->*Extension)(LHS, WideTy, 0),
2283 (this->*Extension)(RHS, WideTy, 0),
2284 SCEV::FlagAnyWrap, 0);
2285 return A == B;
2286}
2287
2288std::pair<SCEV::NoWrapFlags, bool /*Deduced*/>
2289ScalarEvolution::getStrengthenedNoWrapFlagsFromBinOp(
2290 const OverflowingBinaryOperator *OBO) {
2291 SCEV::NoWrapFlags Flags = SCEV::NoWrapFlags::FlagAnyWrap;
2292
2293 if (OBO->hasNoUnsignedWrap())
2294 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
2295 if (OBO->hasNoSignedWrap())
2296 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW);
2297
2298 bool Deduced = false;
2299
2300 if (OBO->hasNoUnsignedWrap() && OBO->hasNoSignedWrap())
2301 return {Flags, Deduced};
2302
2303 if (OBO->getOpcode() != Instruction::Add &&
2304 OBO->getOpcode() != Instruction::Sub &&
2305 OBO->getOpcode() != Instruction::Mul)
2306 return {Flags, Deduced};
2307
2308 const SCEV *LHS = getSCEV(OBO->getOperand(0));
2309 const SCEV *RHS = getSCEV(OBO->getOperand(1));
2310
2311 if (!OBO->hasNoUnsignedWrap() &&
2312 willNotOverflow((Instruction::BinaryOps)OBO->getOpcode(),
2313 /* Signed */ false, LHS, RHS)) {
2314 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
2315 Deduced = true;
2316 }
2317
2318 if (!OBO->hasNoSignedWrap() &&
2319 willNotOverflow((Instruction::BinaryOps)OBO->getOpcode(),
2320 /* Signed */ true, LHS, RHS)) {
2321 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW);
2322 Deduced = true;
2323 }
2324
2325 return {Flags, Deduced};
2326}
2327
2328// We're trying to construct a SCEV of type `Type' with `Ops' as operands and
2329// `OldFlags' as can't-wrap behavior. Infer a more aggressive set of
2330// can't-overflow flags for the operation if possible.
2331static SCEV::NoWrapFlags
2332StrengthenNoWrapFlags(ScalarEvolution *SE, SCEVTypes Type,
2333 const ArrayRef<const SCEV *> Ops,
2334 SCEV::NoWrapFlags Flags) {
2335 using namespace std::placeholders;
2336
2337 using OBO = OverflowingBinaryOperator;
2338
2339 bool CanAnalyze =
2340 Type == scAddExpr || Type == scAddRecExpr || Type == scMulExpr;
2341 (void)CanAnalyze;
2342 assert(CanAnalyze && "don't call from other places!")(static_cast<void> (0));
2343
2344 int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW;
2345 SCEV::NoWrapFlags SignOrUnsignWrap =
2346 ScalarEvolution::maskFlags(Flags, SignOrUnsignMask);
2347
2348 // If FlagNSW is true and all the operands are non-negative, infer FlagNUW.
2349 auto IsKnownNonNegative = [&](const SCEV *S) {
2350 return SE->isKnownNonNegative(S);
2351 };
2352
2353 if (SignOrUnsignWrap == SCEV::FlagNSW && all_of(Ops, IsKnownNonNegative))
2354 Flags =
2355 ScalarEvolution::setFlags(Flags, (SCEV::NoWrapFlags)SignOrUnsignMask);
2356
2357 SignOrUnsignWrap = ScalarEvolution::maskFlags(Flags, SignOrUnsignMask);
2358
2359 if (SignOrUnsignWrap != SignOrUnsignMask &&
2360 (Type == scAddExpr || Type == scMulExpr) && Ops.size() == 2 &&
2361 isa<SCEVConstant>(Ops[0])) {
2362
2363 auto Opcode = [&] {
2364 switch (Type) {
2365 case scAddExpr:
2366 return Instruction::Add;
2367 case scMulExpr:
2368 return Instruction::Mul;
2369 default:
2370 llvm_unreachable("Unexpected SCEV op.")__builtin_unreachable();
2371 }
2372 }();
2373
2374 const APInt &C = cast<SCEVConstant>(Ops[0])->getAPInt();
2375
2376 // (A <opcode> C) --> (A <opcode> C)<nsw> if the op doesn't sign overflow.
2377 if (!(SignOrUnsignWrap & SCEV::FlagNSW)) {
2378 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
2379 Opcode, C, OBO::NoSignedWrap);
2380 if (NSWRegion.contains(SE->getSignedRange(Ops[1])))
2381 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW);
2382 }
2383
2384 // (A <opcode> C) --> (A <opcode> C)<nuw> if the op doesn't unsign overflow.
2385 if (!(SignOrUnsignWrap & SCEV::FlagNUW)) {
2386 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
2387 Opcode, C, OBO::NoUnsignedWrap);
2388 if (NUWRegion.contains(SE->getUnsignedRange(Ops[1])))
2389 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
2390 }
2391 }
2392
2393 // <0,+,nonnegative><nw> is also nuw
2394 // TODO: Add corresponding nsw case
2395 if (Type == scAddRecExpr && ScalarEvolution::hasFlags(Flags, SCEV::FlagNW) &&
2396 !ScalarEvolution::hasFlags(Flags, SCEV::FlagNUW) && Ops.size() == 2 &&
2397 Ops[0]->isZero() && IsKnownNonNegative(Ops[1]))
2398 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
2399
2400 return Flags;
2401}
2402
2403bool ScalarEvolution::isAvailableAtLoopEntry(const SCEV *S, const Loop *L) {
2404 return isLoopInvariant(S, L) && properlyDominates(S, L->getHeader());
2405}
2406
2407/// Get a canonical add expression, or something simpler if possible.
2408const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
2409 SCEV::NoWrapFlags OrigFlags,
2410 unsigned Depth) {
2411 assert(!(OrigFlags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&(static_cast<void> (0))
2412 "only nuw or nsw allowed")(static_cast<void> (0));
2413 assert(!Ops.empty() && "Cannot get empty add!")(static_cast<void> (0));
2414 if (Ops.size() == 1) return Ops[0];
2415#ifndef NDEBUG1
2416 Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
2417 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
2418 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&(static_cast<void> (0))
2419 "SCEVAddExpr operand types don't match!")(static_cast<void> (0));
2420 unsigned NumPtrs = count_if(
2421 Ops, [](const SCEV *Op) { return Op->getType()->isPointerTy(); });
2422 assert(NumPtrs <= 1 && "add has at most one pointer operand")(static_cast<void> (0));
2423#endif
2424
2425 // Sort by complexity, this groups all similar expression types together.
2426 GroupByComplexity(Ops, &LI, DT);
2427
2428 // If there are any constants, fold them together.
2429 unsigned Idx = 0;
2430 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
2431 ++Idx;
2432 assert(Idx < Ops.size())(static_cast<void> (0));
2433 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
2434 // We found two constants, fold them together!
2435 Ops[0] = getConstant(LHSC->getAPInt() + RHSC->getAPInt());
2436 if (Ops.size() == 2) return Ops[0];
2437 Ops.erase(Ops.begin()+1); // Erase the folded element
2438 LHSC = cast<SCEVConstant>(Ops[0]);
2439 }
2440
2441 // If we are left with a constant zero being added, strip it off.
2442 if (LHSC->getValue()->isZero()) {
2443 Ops.erase(Ops.begin());
2444 --Idx;
2445 }
2446
2447 if (Ops.size() == 1) return Ops[0];
2448 }
2449
2450 // Delay expensive flag strengthening until necessary.
2451 auto ComputeFlags = [this, OrigFlags](const ArrayRef<const SCEV *> Ops) {
2452 return StrengthenNoWrapFlags(this, scAddExpr, Ops, OrigFlags);
2453 };
2454
2455 // Limit recursion calls depth.
2456 if (Depth > MaxArithDepth || hasHugeExpression(Ops))
2457 return getOrCreateAddExpr(Ops, ComputeFlags(Ops));
2458
2459 if (SCEV *S = std::get<0>(findExistingSCEVInCache(scAddExpr, Ops))) {
2460 // Don't strengthen flags if we have no new information.
2461 SCEVAddExpr *Add = static_cast<SCEVAddExpr *>(S);
2462 if (Add->getNoWrapFlags(OrigFlags) != OrigFlags)
2463 Add->setNoWrapFlags(ComputeFlags(Ops));
2464 return S;
2465 }
2466
2467 // Okay, check to see if the same value occurs in the operand list more than
2468 // once. If so, merge them together into an multiply expression. Since we
2469 // sorted the list, these values are required to be adjacent.
2470 Type *Ty = Ops[0]->getType();
2471 bool FoundMatch = false;
2472 for (unsigned i = 0, e = Ops.size(); i != e-1; ++i)
2473 if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2
2474 // Scan ahead to count how many equal operands there are.
2475 unsigned Count = 2;
2476 while (i+Count != e && Ops[i+Count] == Ops[i])
2477 ++Count;
2478 // Merge the values into a multiply.
2479 const SCEV *Scale = getConstant(Ty, Count);
2480 const SCEV *Mul = getMulExpr(Scale, Ops[i], SCEV::FlagAnyWrap, Depth + 1);
2481 if (Ops.size() == Count)
2482 return Mul;
2483 Ops[i] = Mul;
2484 Ops.erase(Ops.begin()+i+1, Ops.begin()+i+Count);
2485 --i; e -= Count - 1;
2486 FoundMatch = true;
2487 }
2488 if (FoundMatch)
2489 return getAddExpr(Ops, OrigFlags, Depth + 1);
2490
2491 // Check for truncates. If all the operands are truncated from the same
2492 // type, see if factoring out the truncate would permit the result to be
2493 // folded. eg., n*trunc(x) + m*trunc(y) --> trunc(trunc(m)*x + trunc(n)*y)
2494 // if the contents of the resulting outer trunc fold to something simple.
2495 auto FindTruncSrcType = [&]() -> Type * {
2496 // We're ultimately looking to fold an addrec of truncs and muls of only
2497 // constants and truncs, so if we find any other types of SCEV
2498 // as operands of the addrec then we bail and return nullptr here.
2499 // Otherwise, we return the type of the operand of a trunc that we find.
2500 if (auto *T = dyn_cast<SCEVTruncateExpr>(Ops[Idx]))
2501 return T->getOperand()->getType();
2502 if (const auto *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
2503 const auto *LastOp = Mul->getOperand(Mul->getNumOperands() - 1);
2504 if (const auto *T = dyn_cast<SCEVTruncateExpr>(LastOp))
2505 return T->getOperand()->getType();
2506 }
2507 return nullptr;
2508 };
2509 if (auto *SrcType = FindTruncSrcType()) {
2510 SmallVector<const SCEV *, 8> LargeOps;
2511 bool Ok = true;
2512 // Check all the operands to see if they can be represented in the
2513 // source type of the truncate.
2514 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
2515 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) {
2516 if (T->getOperand()->getType() != SrcType) {
2517 Ok = false;
2518 break;
2519 }
2520 LargeOps.push_back(T->getOperand());
2521 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
2522 LargeOps.push_back(getAnyExtendExpr(C, SrcType));
2523 } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) {
2524 SmallVector<const SCEV *, 8> LargeMulOps;
2525 for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
2526 if (const SCEVTruncateExpr *T =
2527 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) {
2528 if (T->getOperand()->getType() != SrcType) {
2529 Ok = false;
2530 break;
2531 }
2532 LargeMulOps.push_back(T->getOperand());
2533 } else if (const auto *C = dyn_cast<SCEVConstant>(M->getOperand(j))) {
2534 LargeMulOps.push_back(getAnyExtendExpr(C, SrcType));
2535 } else {
2536 Ok = false;
2537 break;
2538 }
2539 }
2540 if (Ok)
2541 LargeOps.push_back(getMulExpr(LargeMulOps, SCEV::FlagAnyWrap, Depth + 1));
2542 } else {
2543 Ok = false;
2544 break;
2545 }
2546 }
2547 if (Ok) {
2548 // Evaluate the expression in the larger type.
2549 const SCEV *Fold = getAddExpr(LargeOps, SCEV::FlagAnyWrap, Depth + 1);
2550 // If it folds to something simple, use it. Otherwise, don't.
2551 if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold))
2552 return getTruncateExpr(Fold, Ty);
2553 }
2554 }
2555
2556 if (Ops.size() == 2) {
2557 // Check if we have an expression of the form ((X + C1) - C2), where C1 and
2558 // C2 can be folded in a way that allows retaining wrapping flags of (X +
2559 // C1).
2560 const SCEV *A = Ops[0];
2561 const SCEV *B = Ops[1];
2562 auto *AddExpr = dyn_cast<SCEVAddExpr>(B);
2563 auto *C = dyn_cast<SCEVConstant>(A);
2564 if (AddExpr && C && isa<SCEVConstant>(AddExpr->getOperand(0))) {
2565 auto C1 = cast<SCEVConstant>(AddExpr->getOperand(0))->getAPInt();
2566 auto C2 = C->getAPInt();
2567 SCEV::NoWrapFlags PreservedFlags = SCEV::FlagAnyWrap;
2568
2569 APInt ConstAdd = C1 + C2;
2570 auto AddFlags = AddExpr->getNoWrapFlags();
2571 // Adding a smaller constant is NUW if the original AddExpr was NUW.
2572 if (ScalarEvolution::hasFlags(AddFlags, SCEV::FlagNUW) &&
2573 ConstAdd.ule(C1)) {
2574 PreservedFlags =
2575 ScalarEvolution::setFlags(PreservedFlags, SCEV::FlagNUW);
2576 }
2577
2578 // Adding a constant with the same sign and small magnitude is NSW, if the
2579 // original AddExpr was NSW.
2580 if (ScalarEvolution::hasFlags(AddFlags, SCEV::FlagNSW) &&
2581 C1.isSignBitSet() == ConstAdd.isSignBitSet() &&
2582 ConstAdd.abs().ule(C1.abs())) {
2583 PreservedFlags =
2584 ScalarEvolution::setFlags(PreservedFlags, SCEV::FlagNSW);
2585 }
2586
2587 if (PreservedFlags != SCEV::FlagAnyWrap) {
2588 SmallVector<const SCEV *, 4> NewOps(AddExpr->op_begin(),
2589 AddExpr->op_end());
2590 NewOps[0] = getConstant(ConstAdd);
2591 return getAddExpr(NewOps, PreservedFlags);
2592 }
2593 }
2594 }
2595
2596 // Skip past any other cast SCEVs.
2597 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr)
2598 ++Idx;
2599
2600 // If there are add operands they would be next.
2601 if (Idx < Ops.size()) {
2602 bool DeletedAdd = false;
2603 // If the original flags and all inlined SCEVAddExprs are NUW, use the
2604 // common NUW flag for expression after inlining. Other flags cannot be
2605 // preserved, because they may depend on the original order of operations.
2606 SCEV::NoWrapFlags CommonFlags = maskFlags(OrigFlags, SCEV::FlagNUW);
2607 while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
2608 if (Ops.size() > AddOpsInlineThreshold ||
2609 Add->getNumOperands() > AddOpsInlineThreshold)
2610 break;
2611 // If we have an add, expand the add operands onto the end of the operands
2612 // list.
2613 Ops.erase(Ops.begin()+Idx);
2614 Ops.append(Add->op_begin(), Add->op_end());
2615 DeletedAdd = true;
2616 CommonFlags = maskFlags(CommonFlags, Add->getNoWrapFlags());
2617 }
2618
2619 // If we deleted at least one add, we added operands to the end of the list,
2620 // and they are not necessarily sorted. Recurse to resort and resimplify
2621 // any operands we just acquired.
2622 if (DeletedAdd)
2623 return getAddExpr(Ops, CommonFlags, Depth + 1);
2624 }
2625
2626 // Skip over the add expression until we get to a multiply.
2627 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
2628 ++Idx;
2629
2630 // Check to see if there are any folding opportunities present with
2631 // operands multiplied by constant values.
2632 if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) {
2633 uint64_t BitWidth = getTypeSizeInBits(Ty);
2634 DenseMap<const SCEV *, APInt> M;
2635 SmallVector<const SCEV *, 8> NewOps;
2636 APInt AccumulatedConstant(BitWidth, 0);
2637 if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
2638 Ops.data(), Ops.size(),
2639 APInt(BitWidth, 1), *this)) {
2640 struct APIntCompare {
2641 bool operator()(const APInt &LHS, const APInt &RHS) const {
2642 return LHS.ult(RHS);
2643 }
2644 };
2645
2646 // Some interesting folding opportunity is present, so its worthwhile to
2647 // re-generate the operands list. Group the operands by constant scale,
2648 // to avoid multiplying by the same constant scale multiple times.
2649 std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists;
2650 for (const SCEV *NewOp : NewOps)
2651 MulOpLists[M.find(NewOp)->second].push_back(NewOp);
2652 // Re-generate the operands list.
2653 Ops.clear();
2654 if (AccumulatedConstant != 0)
2655 Ops.push_back(getConstant(AccumulatedConstant));
2656 for (auto &MulOp : MulOpLists) {
2657 if (MulOp.first == 1) {
2658 Ops.push_back(getAddExpr(MulOp.second, SCEV::FlagAnyWrap, Depth + 1));
2659 } else if (MulOp.first != 0) {
2660 Ops.push_back(getMulExpr(
2661 getConstant(MulOp.first),
2662 getAddExpr(MulOp.second, SCEV::FlagAnyWrap, Depth + 1),
2663 SCEV::FlagAnyWrap, Depth + 1));
2664 }
2665 }
2666 if (Ops.empty())
2667 return getZero(Ty);
2668 if (Ops.size() == 1)
2669 return Ops[0];
2670 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2671 }
2672 }
2673
2674 // If we are adding something to a multiply expression, make sure the
2675 // something is not already an operand of the multiply. If so, merge it into
2676 // the multiply.
2677 for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
2678 const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
2679 for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
2680 const SCEV *MulOpSCEV = Mul->getOperand(MulOp);
2681 if (isa<SCEVConstant>(MulOpSCEV))
2682 continue;
2683 for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
2684 if (MulOpSCEV == Ops[AddOp]) {
2685 // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1))
2686 const SCEV *InnerMul = Mul->getOperand(MulOp == 0);
2687 if (Mul->getNumOperands() != 2) {
2688 // If the multiply has more than two operands, we must get the
2689 // Y*Z term.
2690 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
2691 Mul->op_begin()+MulOp);
2692 MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end());
2693 InnerMul = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1);
2694 }
2695 SmallVector<const SCEV *, 2> TwoOps = {getOne(Ty), InnerMul};
2696 const SCEV *AddOne = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1);
2697 const SCEV *OuterMul = getMulExpr(AddOne, MulOpSCEV,
2698 SCEV::FlagAnyWrap, Depth + 1);
2699 if (Ops.size() == 2) return OuterMul;
2700 if (AddOp < Idx) {
2701 Ops.erase(Ops.begin()+AddOp);
2702 Ops.erase(Ops.begin()+Idx-1);
2703 } else {
2704 Ops.erase(Ops.begin()+Idx);
2705 Ops.erase(Ops.begin()+AddOp-1);
2706 }
2707 Ops.push_back(OuterMul);
2708 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2709 }
2710
2711 // Check this multiply against other multiplies being added together.
2712 for (unsigned OtherMulIdx = Idx+1;
2713 OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
2714 ++OtherMulIdx) {
2715 const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
2716 // If MulOp occurs in OtherMul, we can fold the two multiplies
2717 // together.
2718 for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
2719 OMulOp != e; ++OMulOp)
2720 if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
2721 // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
2722 const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0);
2723 if (Mul->getNumOperands() != 2) {
2724 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
2725 Mul->op_begin()+MulOp);
2726 MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end());
2727 InnerMul1 = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1);
2728 }
2729 const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0);
2730 if (OtherMul->getNumOperands() != 2) {
2731 SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(),
2732 OtherMul->op_begin()+OMulOp);
2733 MulOps.append(OtherMul->op_begin()+OMulOp+1, OtherMul->op_end());
2734 InnerMul2 = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1);
2735 }
2736 SmallVector<const SCEV *, 2> TwoOps = {InnerMul1, InnerMul2};
2737 const SCEV *InnerMulSum =
2738 getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1);
2739 const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum,
2740 SCEV::FlagAnyWrap, Depth + 1);
2741 if (Ops.size() == 2) return OuterMul;
2742 Ops.erase(Ops.begin()+Idx);
2743 Ops.erase(Ops.begin()+OtherMulIdx-1);
2744 Ops.push_back(OuterMul);
2745 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2746 }
2747 }
2748 }
2749 }
2750
2751 // If there are any add recurrences in the operands list, see if any other
2752 // added values are loop invariant. If so, we can fold them into the
2753 // recurrence.
2754 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
2755 ++Idx;
2756
2757 // Scan over all recurrences, trying to fold loop invariants into them.
2758 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
2759 // Scan all of the other operands to this add and add them to the vector if
2760 // they are loop invariant w.r.t. the recurrence.
2761 SmallVector<const SCEV *, 8> LIOps;
2762 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
2763 const Loop *AddRecLoop = AddRec->getLoop();
2764 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2765 if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) {
2766 LIOps.push_back(Ops[i]);
2767 Ops.erase(Ops.begin()+i);
2768 --i; --e;
2769 }
2770
2771 // If we found some loop invariants, fold them into the recurrence.
2772 if (!LIOps.empty()) {
2773 // Compute nowrap flags for the addition of the loop-invariant ops and
2774 // the addrec. Temporarily push it as an operand for that purpose.
2775 LIOps.push_back(AddRec);
2776 SCEV::NoWrapFlags Flags = ComputeFlags(LIOps);
2777 LIOps.pop_back();
2778
2779 // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step}
2780 LIOps.push_back(AddRec->getStart());
2781
2782 SmallVector<const SCEV *, 4> AddRecOps(AddRec->operands());
2783 // This follows from the fact that the no-wrap flags on the outer add
2784 // expression are applicable on the 0th iteration, when the add recurrence
2785 // will be equal to its start value.
2786 AddRecOps[0] = getAddExpr(LIOps, Flags, Depth + 1);
2787
2788 // Build the new addrec. Propagate the NUW and NSW flags if both the
2789 // outer add and the inner addrec are guaranteed to have no overflow.
2790 // Always propagate NW.
2791 Flags = AddRec->getNoWrapFlags(setFlags(Flags, SCEV::FlagNW));
2792 const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRecLoop, Flags);
2793
2794 // If all of the other operands were loop invariant, we are done.
2795 if (Ops.size() == 1) return NewRec;
2796
2797 // Otherwise, add the folded AddRec by the non-invariant parts.
2798 for (unsigned i = 0;; ++i)
2799 if (Ops[i] == AddRec) {
2800 Ops[i] = NewRec;
2801 break;
2802 }
2803 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2804 }
2805
2806 // Okay, if there weren't any loop invariants to be folded, check to see if
2807 // there are multiple AddRec's with the same loop induction variable being
2808 // added together. If so, we can fold them.
2809 for (unsigned OtherIdx = Idx+1;
2810 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2811 ++OtherIdx) {
2812 // We expect the AddRecExpr's to be sorted in reverse dominance order,
2813 // so that the 1st found AddRecExpr is dominated by all others.
2814 assert(DT.dominates((static_cast<void> (0))
2815 cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()->getHeader(),(static_cast<void> (0))
2816 AddRec->getLoop()->getHeader()) &&(static_cast<void> (0))
2817 "AddRecExprs are not sorted in reverse dominance order?")(static_cast<void> (0));
2818 if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) {
2819 // Other + {A,+,B}<L> + {C,+,D}<L> --> Other + {A+C,+,B+D}<L>
2820 SmallVector<const SCEV *, 4> AddRecOps(AddRec->operands());
2821 for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2822 ++OtherIdx) {
2823 const auto *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
2824 if (OtherAddRec->getLoop() == AddRecLoop) {
2825 for (unsigned i = 0, e = OtherAddRec->getNumOperands();
2826 i != e; ++i) {
2827 if (i >= AddRecOps.size()) {
2828 AddRecOps.append(OtherAddRec->op_begin()+i,
2829 OtherAddRec->op_end());
2830 break;
2831 }
2832 SmallVector<const SCEV *, 2> TwoOps = {
2833 AddRecOps[i], OtherAddRec->getOperand(i)};
2834 AddRecOps[i] = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1);
2835 }
2836 Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
2837 }
2838 }
2839 // Step size has changed, so we cannot guarantee no self-wraparound.
2840 Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop, SCEV::FlagAnyWrap);
2841 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2842 }
2843 }
2844
2845 // Otherwise couldn't fold anything into this recurrence. Move onto the
2846 // next one.
2847 }
2848
2849 // Okay, it looks like we really DO need an add expr. Check to see if we
2850 // already have one, otherwise create a new one.
2851 return getOrCreateAddExpr(Ops, ComputeFlags(Ops));
2852}
2853
2854const SCEV *
2855ScalarEvolution::getOrCreateAddExpr(ArrayRef<const SCEV *> Ops,
2856 SCEV::NoWrapFlags Flags) {
2857 FoldingSetNodeID ID;
2858 ID.AddInteger(scAddExpr);
2859 for (const SCEV *Op : Ops)
2860 ID.AddPointer(Op);
2861 void *IP = nullptr;
2862 SCEVAddExpr *S =
2863 static_cast<SCEVAddExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2864 if (!S) {
2865 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
2866 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
2867 S = new (SCEVAllocator)
2868 SCEVAddExpr(ID.Intern(SCEVAllocator), O, Ops.size());
2869 UniqueSCEVs.InsertNode(S, IP);
2870 addToLoopUseLists(S);
2871 }
2872 S->setNoWrapFlags(Flags);
2873 return S;
2874}
2875
2876const SCEV *
2877ScalarEvolution::getOrCreateAddRecExpr(ArrayRef<const SCEV *> Ops,
2878 const Loop *L, SCEV::NoWrapFlags Flags) {
2879 FoldingSetNodeID ID;
2880 ID.AddInteger(scAddRecExpr);
2881 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2882 ID.AddPointer(Ops[i]);
2883 ID.AddPointer(L);
2884 void *IP = nullptr;
2885 SCEVAddRecExpr *S =
2886 static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2887 if (!S) {
2888 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
2889 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
2890 S = new (SCEVAllocator)
2891 SCEVAddRecExpr(ID.Intern(SCEVAllocator), O, Ops.size(), L);
2892 UniqueSCEVs.InsertNode(S, IP);
2893 addToLoopUseLists(S);
2894 }
2895 setNoWrapFlags(S, Flags);
2896 return S;
2897}
2898
2899const SCEV *
2900ScalarEvolution::getOrCreateMulExpr(ArrayRef<const SCEV *> Ops,
2901 SCEV::NoWrapFlags Flags) {
2902 FoldingSetNodeID ID;
2903 ID.AddInteger(scMulExpr);
2904 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2905 ID.AddPointer(Ops[i]);
2906 void *IP = nullptr;
2907 SCEVMulExpr *S =
2908 static_cast<SCEVMulExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2909 if (!S) {
2910 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
2911 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
2912 S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator),
2913 O, Ops.size());
2914 UniqueSCEVs.InsertNode(S, IP);
2915 addToLoopUseLists(S);
2916 }
2917 S->setNoWrapFlags(Flags);
2918 return S;
2919}
2920
2921static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow) {
2922 uint64_t k = i*j;
2923 if (j > 1 && k / j != i) Overflow = true;
2924 return k;
2925}
2926
2927/// Compute the result of "n choose k", the binomial coefficient. If an
2928/// intermediate computation overflows, Overflow will be set and the return will
2929/// be garbage. Overflow is not cleared on absence of overflow.
2930static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow) {
2931 // We use the multiplicative formula:
2932 // n(n-1)(n-2)...(n-(k-1)) / k(k-1)(k-2)...1 .
2933 // At each iteration, we take the n-th term of the numeral and divide by the
2934 // (k-n)th term of the denominator. This division will always produce an
2935 // integral result, and helps reduce the chance of overflow in the
2936 // intermediate computations. However, we can still overflow even when the
2937 // final result would fit.
2938
2939 if (n == 0 || n == k) return 1;
2940 if (k > n) return 0;
2941
2942 if (k > n/2)
2943 k = n-k;
2944
2945 uint64_t r = 1;
2946 for (uint64_t i = 1; i <= k; ++i) {
2947 r = umul_ov(r, n-(i-1), Overflow);
2948 r /= i;
2949 }
2950 return r;
2951}
2952
2953/// Determine if any of the operands in this SCEV are a constant or if
2954/// any of the add or multiply expressions in this SCEV contain a constant.
2955static bool containsConstantInAddMulChain(const SCEV *StartExpr) {
2956 struct FindConstantInAddMulChain {
2957 bool FoundConstant = false;
2958
2959 bool follow(const SCEV *S) {
2960 FoundConstant |= isa<SCEVConstant>(S);
2961 return isa<SCEVAddExpr>(S) || isa<SCEVMulExpr>(S);
2962 }
2963
2964 bool isDone() const {
2965 return FoundConstant;
2966 }
2967 };
2968
2969 FindConstantInAddMulChain F;
2970 SCEVTraversal<FindConstantInAddMulChain> ST(F);
2971 ST.visitAll(StartExpr);
2972 return F.FoundConstant;
2973}
2974
2975/// Get a canonical multiply expression, or something simpler if possible.
2976const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
2977 SCEV::NoWrapFlags OrigFlags,
2978 unsigned Depth) {
2979 assert(OrigFlags == maskFlags(OrigFlags, SCEV::FlagNUW | SCEV::FlagNSW) &&(static_cast<void> (0))
2980 "only nuw or nsw allowed")(static_cast<void> (0));
2981 assert(!Ops.empty() && "Cannot get empty mul!")(static_cast<void> (0));
2982 if (Ops.size() == 1) return Ops[0];
2983#ifndef NDEBUG1
2984 Type *ETy = Ops[0]->getType();
2985 assert(!ETy->isPointerTy())(static_cast<void> (0));
2986 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
2987 assert(Ops[i]->getType() == ETy &&(static_cast<void> (0))
2988 "SCEVMulExpr operand types don't match!")(static_cast<void> (0));
2989#endif
2990
2991 // Sort by complexity, this groups all similar expression types together.
2992 GroupByComplexity(Ops, &LI, DT);
2993
2994 // If there are any constants, fold them together.
2995 unsigned Idx = 0;
2996 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
2997 ++Idx;
2998 assert(Idx < Ops.size())(static_cast<void> (0));
2999 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
3000 // We found two constants, fold them together!
3001 Ops[0] = getConstant(LHSC->getAPInt() * RHSC->getAPInt());
3002 if (Ops.size() == 2) return Ops[0];
3003 Ops.erase(Ops.begin()+1); // Erase the folded element
3004 LHSC = cast<SCEVConstant>(Ops[0]);
3005 }
3006
3007 // If we have a multiply of zero, it will always be zero.
3008 if (LHSC->getValue()->isZero())
3009 return LHSC;
3010
3011 // If we are left with a constant one being multiplied, strip it off.
3012 if (LHSC->getValue()->isOne()) {
3013 Ops.erase(Ops.begin());
3014 --Idx;
3015 }
3016
3017 if (Ops.size() == 1)
3018 return Ops[0];
3019 }
3020
3021 // Delay expensive flag strengthening until necessary.
3022 auto ComputeFlags = [this, OrigFlags](const ArrayRef<const SCEV *> Ops) {
3023 return StrengthenNoWrapFlags(this, scMulExpr, Ops, OrigFlags);
3024 };
3025
3026 // Limit recursion calls depth.
3027 if (Depth > MaxArithDepth || hasHugeExpression(Ops))
3028 return getOrCreateMulExpr(Ops, ComputeFlags(Ops));
3029
3030 if (SCEV *S = std::get<0>(findExistingSCEVInCache(scMulExpr, Ops))) {
3031 // Don't strengthen flags if we have no new information.
3032 SCEVMulExpr *Mul = static_cast<SCEVMulExpr *>(S);
3033 if (Mul->getNoWrapFlags(OrigFlags) != OrigFlags)
3034 Mul->setNoWrapFlags(ComputeFlags(Ops));
3035 return S;
3036 }
3037
3038 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
3039 if (Ops.size() == 2) {
3040 // C1*(C2+V) -> C1*C2 + C1*V
3041 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
3042 // If any of Add's ops are Adds or Muls with a constant, apply this
3043 // transformation as well.
3044 //
3045 // TODO: There are some cases where this transformation is not
3046 // profitable; for example, Add = (C0 + X) * Y + Z. Maybe the scope of
3047 // this transformation should be narrowed down.
3048 if (Add->getNumOperands() == 2 && containsConstantInAddMulChain(Add))
3049 return getAddExpr(getMulExpr(LHSC, Add->getOperand(0),
3050 SCEV::FlagAnyWrap, Depth + 1),
3051 getMulExpr(LHSC, Add->getOperand(1),
3052 SCEV::FlagAnyWrap, Depth + 1),
3053 SCEV::FlagAnyWrap, Depth + 1);
3054
3055 if (Ops[0]->isAllOnesValue()) {
3056 // If we have a mul by -1 of an add, try distributing the -1 among the
3057 // add operands.
3058 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) {
3059 SmallVector<const SCEV *, 4> NewOps;
3060 bool AnyFolded = false;
3061 for (const SCEV *AddOp : Add->operands()) {
3062 const SCEV *Mul = getMulExpr(Ops[0], AddOp, SCEV::FlagAnyWrap,
3063 Depth + 1);
3064 if (!isa<SCEVMulExpr>(Mul)) AnyFolded = true;
3065 NewOps.push_back(Mul);
3066 }
3067 if (AnyFolded)
3068 return getAddExpr(NewOps, SCEV::FlagAnyWrap, Depth + 1);
3069 } else if (const auto *AddRec = dyn_cast<SCEVAddRecExpr>(Ops[1])) {
3070 // Negation preserves a recurrence's no self-wrap property.
3071 SmallVector<const SCEV *, 4> Operands;
3072 for (const SCEV *AddRecOp : AddRec->operands())
3073 Operands.push_back(getMulExpr(Ops[0], AddRecOp, SCEV::FlagAnyWrap,
3074 Depth + 1));
3075
3076 return getAddRecExpr(Operands, AddRec->getLoop(),
3077 AddRec->getNoWrapFlags(SCEV::FlagNW));
3078 }
3079 }
3080 }
3081 }
3082
3083 // Skip over the add expression until we get to a multiply.
3084 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
3085 ++Idx;
3086
3087 // If there are mul operands inline them all into this expression.
3088 if (Idx < Ops.size()) {
3089 bool DeletedMul = false;
3090 while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
3091 if (Ops.size() > MulOpsInlineThreshold)
3092 break;
3093 // If we have an mul, expand the mul operands onto the end of the
3094 // operands list.
3095 Ops.erase(Ops.begin()+Idx);
3096 Ops.append(Mul->op_begin(), Mul->op_end());
3097 DeletedMul = true;
3098 }
3099
3100 // If we deleted at least one mul, we added operands to the end of the
3101 // list, and they are not necessarily sorted. Recurse to resort and
3102 // resimplify any operands we just acquired.
3103 if (DeletedMul)
3104 return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
3105 }
3106
3107 // If there are any add recurrences in the operands list, see if any other
3108 // added values are loop invariant. If so, we can fold them into the
3109 // recurrence.
3110 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
3111 ++Idx;
3112
3113 // Scan over all recurrences, trying to fold loop invariants into them.
3114 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
3115 // Scan all of the other operands to this mul and add them to the vector
3116 // if they are loop invariant w.r.t. the recurrence.
3117 SmallVector<const SCEV *, 8> LIOps;
3118 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
3119 const Loop *AddRecLoop = AddRec->getLoop();
3120 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
3121 if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) {
3122 LIOps.push_back(Ops[i]);
3123 Ops.erase(Ops.begin()+i);
3124 --i; --e;
3125 }
3126
3127 // If we found some loop invariants, fold them into the recurrence.
3128 if (!LIOps.empty()) {
3129 // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step}
3130 SmallVector<const SCEV *, 4> NewOps;
3131 NewOps.reserve(AddRec->getNumOperands());
3132 const SCEV *Scale = getMulExpr(LIOps, SCEV::FlagAnyWrap, Depth + 1);
3133 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
3134 NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i),
3135 SCEV::FlagAnyWrap, Depth + 1));
3136
3137 // Build the new addrec. Propagate the NUW and NSW flags if both the
3138 // outer mul and the inner addrec are guaranteed to have no overflow.
3139 //
3140 // No self-wrap cannot be guaranteed after changing the step size, but
3141 // will be inferred if either NUW or NSW is true.
3142 SCEV::NoWrapFlags Flags = ComputeFlags({Scale, AddRec});
3143 const SCEV *NewRec = getAddRecExpr(
3144 NewOps, AddRecLoop, AddRec->getNoWrapFlags(Flags));
3145
3146 // If all of the other operands were loop invariant, we are done.
3147 if (Ops.size() == 1) return NewRec;
3148
3149 // Otherwise, multiply the folded AddRec by the non-invariant parts.
3150 for (unsigned i = 0;; ++i)
3151 if (Ops[i] == AddRec) {
3152 Ops[i] = NewRec;
3153 break;
3154 }
3155 return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
3156 }
3157
3158 // Okay, if there weren't any loop invariants to be folded, check to see
3159 // if there are multiple AddRec's with the same loop induction variable
3160 // being multiplied together. If so, we can fold them.
3161
3162 // {A1,+,A2,+,...,+,An}<L> * {B1,+,B2,+,...,+,Bn}<L>
3163 // = {x=1 in [ sum y=x..2x [ sum z=max(y-x, y-n)..min(x,n) [
3164 // choose(x, 2x)*choose(2x-y, x-z)*A_{y-z}*B_z
3165 // ]]],+,...up to x=2n}.
3166 // Note that the arguments to choose() are always integers with values
3167 // known at compile time, never SCEV objects.
3168 //
3169 // The implementation avoids pointless extra computations when the two
3170 // addrec's are of different length (mathematically, it's equivalent to
3171 // an infinite stream of zeros on the right).
3172 bool OpsModified = false;
3173 for (unsigned OtherIdx = Idx+1;
3174 OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
3175 ++OtherIdx) {
3176 const SCEVAddRecExpr *OtherAddRec =
3177 dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx]);
3178 if (!OtherAddRec || OtherAddRec->getLoop() != AddRecLoop)
3179 continue;
3180
3181 // Limit max number of arguments to avoid creation of unreasonably big
3182 // SCEVAddRecs with very complex operands.
3183 if (AddRec->getNumOperands() + OtherAddRec->getNumOperands() - 1 >
3184 MaxAddRecSize || hasHugeExpression({AddRec, OtherAddRec}))
3185 continue;
3186
3187 bool Overflow = false;
3188 Type *Ty = AddRec->getType();
3189 bool LargerThan64Bits = getTypeSizeInBits(Ty) > 64;
3190 SmallVector<const SCEV*, 7> AddRecOps;
3191 for (int x = 0, xe = AddRec->getNumOperands() +
3192 OtherAddRec->getNumOperands() - 1; x != xe && !Overflow; ++x) {
3193 SmallVector <const SCEV *, 7> SumOps;
3194 for (int y = x, ye = 2*x+1; y != ye && !Overflow; ++y) {
3195 uint64_t Coeff1 = Choose(x, 2*x - y, Overflow);
3196 for (int z = std::max(y-x, y-(int)AddRec->getNumOperands()+1),
3197 ze = std::min(x+1, (int)OtherAddRec->getNumOperands());
3198 z < ze && !Overflow; ++z) {
3199 uint64_t Coeff2 = Choose(2*x - y, x-z, Overflow);
3200 uint64_t Coeff;
3201 if (LargerThan64Bits)
3202 Coeff = umul_ov(Coeff1, Coeff2, Overflow);
3203 else
3204 Coeff = Coeff1*Coeff2;
3205 const SCEV *CoeffTerm = getConstant(Ty, Coeff);
3206 const SCEV *Term1 = AddRec->getOperand(y-z);
3207 const SCEV *Term2 = OtherAddRec->getOperand(z);
3208 SumOps.push_back(getMulExpr(CoeffTerm, Term1, Term2,
3209 SCEV::FlagAnyWrap, Depth + 1));
3210 }
3211 }
3212 if (SumOps.empty())
3213 SumOps.push_back(getZero(Ty));
3214 AddRecOps.push_back(getAddExpr(SumOps, SCEV::FlagAnyWrap, Depth + 1));
3215 }
3216 if (!Overflow) {
3217 const SCEV *NewAddRec = getAddRecExpr(AddRecOps, AddRecLoop,
3218 SCEV::FlagAnyWrap);
3219 if (Ops.size() == 2) return NewAddRec;
3220 Ops[Idx] = NewAddRec;
3221 Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
3222 OpsModified = true;
3223 AddRec = dyn_cast<SCEVAddRecExpr>(NewAddRec);
3224 if (!AddRec)
3225 break;
3226 }
3227 }
3228 if (OpsModified)
3229 return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
3230
3231 // Otherwise couldn't fold anything into this recurrence. Move onto the
3232 // next one.
3233 }
3234
3235 // Okay, it looks like we really DO need an mul expr. Check to see if we
3236 // already have one, otherwise create a new one.
3237 return getOrCreateMulExpr(Ops, ComputeFlags(Ops));
3238}
3239
3240/// Represents an unsigned remainder expression based on unsigned division.
3241const SCEV *ScalarEvolution::getURemExpr(const SCEV *LHS,
3242 const SCEV *RHS) {
3243 assert(getEffectiveSCEVType(LHS->getType()) ==(static_cast<void> (0))
3244 getEffectiveSCEVType(RHS->getType()) &&(static_cast<void> (0))
3245 "SCEVURemExpr operand types don't match!")(static_cast<void> (0));
3246
3247 // Short-circuit easy cases
3248 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
3249 // If constant is one, the result is trivial
3250 if (RHSC->getValue()->isOne())
3251 return getZero(LHS->getType()); // X urem 1 --> 0
3252
3253 // If constant is a power of two, fold into a zext(trunc(LHS)).
3254 if (RHSC->getAPInt().isPowerOf2()) {
3255 Type *FullTy = LHS->getType();
3256 Type *TruncTy =
3257 IntegerType::get(getContext(), RHSC->getAPInt().logBase2());
3258 return getZeroExtendExpr(getTruncateExpr(LHS, TruncTy), FullTy);
3259 }
3260 }
3261
3262 // Fallback to %a == %x urem %y == %x -<nuw> ((%x udiv %y) *<nuw> %y)
3263 const SCEV *UDiv = getUDivExpr(LHS, RHS);
3264 const SCEV *Mult = getMulExpr(UDiv, RHS, SCEV::FlagNUW);
3265 return getMinusSCEV(LHS, Mult, SCEV::FlagNUW);
3266}
3267
3268/// Get a canonical unsigned division expression, or something simpler if
3269/// possible.
3270const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
3271 const SCEV *RHS) {
3272 assert(!LHS->getType()->isPointerTy() &&(static_cast<void> (0))
3273 "SCEVUDivExpr operand can't be pointer!")(static_cast<void> (0));
3274 assert(LHS->getType() == RHS->getType() &&(static_cast<void> (0))
3275 "SCEVUDivExpr operand types don't match!")(static_cast<void> (0));
3276
3277 FoldingSetNodeID ID;
3278 ID.AddInteger(scUDivExpr);
3279 ID.AddPointer(LHS);
3280 ID.AddPointer(RHS);
3281 void *IP = nullptr;
3282 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
3283 return S;
3284
3285 // 0 udiv Y == 0
3286 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS))
3287 if (LHSC->getValue()->isZero())
3288 return LHS;
3289
3290 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
3291 if (RHSC->getValue()->isOne())
3292 return LHS; // X udiv 1 --> x
3293 // If the denominator is zero, the result of the udiv is undefined. Don't
3294 // try to analyze it, because the resolution chosen here may differ from
3295 // the resolution chosen in other parts of the compiler.
3296 if (!RHSC->getValue()->isZero()) {
3297 // Determine if the division can be folded into the operands of
3298 // its operands.
3299 // TODO: Generalize this to non-constants by using known-bits information.
3300 Type *Ty = LHS->getType();
3301 unsigned LZ = RHSC->getAPInt().countLeadingZeros();
3302 unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1;
3303 // For non-power-of-two values, effectively round the value up to the
3304 // nearest power of two.
3305 if (!RHSC->getAPInt().isPowerOf2())
3306 ++MaxShiftAmt;
3307 IntegerType *ExtTy =
3308 IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt);
3309 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
3310 if (const SCEVConstant *Step =
3311 dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) {
3312 // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
3313 const APInt &StepInt = Step->getAPInt();
3314 const APInt &DivInt = RHSC->getAPInt();
3315 if (!StepInt.urem(DivInt) &&
3316 getZeroExtendExpr(AR, ExtTy) ==
3317 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
3318 getZeroExtendExpr(Step, ExtTy),
3319 AR->getLoop(), SCEV::FlagAnyWrap)) {
3320 SmallVector<const SCEV *, 4> Operands;
3321 for (const SCEV *Op : AR->operands())
3322 Operands.push_back(getUDivExpr(Op, RHS));
3323 return getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagNW);
3324 }
3325 /// Get a canonical UDivExpr for a recurrence.
3326 /// {X,+,N}/C => {Y,+,N}/C where Y=X-(X%N). Safe when C%N=0.
3327 // We can currently only fold X%N if X is constant.
3328 const SCEVConstant *StartC = dyn_cast<SCEVConstant>(AR->getStart());
3329 if (StartC && !DivInt.urem(StepInt) &&
3330 getZeroExtendExpr(AR, ExtTy) ==
3331 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
3332 getZeroExtendExpr(Step, ExtTy),
3333 AR->getLoop(), SCEV::FlagAnyWrap)) {
3334 const APInt &StartInt = StartC->getAPInt();
3335 const APInt &StartRem = StartInt.urem(StepInt);
3336 if (StartRem != 0) {
3337 const SCEV *NewLHS =
3338 getAddRecExpr(getConstant(StartInt - StartRem), Step,
3339 AR->getLoop(), SCEV::FlagNW);
3340 if (LHS != NewLHS) {
3341 LHS = NewLHS;
3342
3343 // Reset the ID to include the new LHS, and check if it is
3344 // already cached.
3345 ID.clear();
3346 ID.AddInteger(scUDivExpr);
3347 ID.AddPointer(LHS);
3348 ID.AddPointer(RHS);
3349 IP = nullptr;
3350 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
3351 return S;
3352 }
3353 }
3354 }
3355 }
3356 // (A*B)/C --> A*(B/C) if safe and B/C can be folded.
3357 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
3358 SmallVector<const SCEV *, 4> Operands;
3359 for (const SCEV *Op : M->operands())
3360 Operands.push_back(getZeroExtendExpr(Op, ExtTy));
3361 if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands))
3362 // Find an operand that's safely divisible.
3363 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
3364 const SCEV *Op = M->getOperand(i);
3365 const SCEV *Div = getUDivExpr(Op, RHSC);
3366 if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) {
3367 Operands = SmallVector<const SCEV *, 4>(M->operands());
3368 Operands[i] = Div;
3369 return getMulExpr(Operands);
3370 }
3371 }
3372 }
3373
3374 // (A/B)/C --> A/(B*C) if safe and B*C can be folded.
3375 if (const SCEVUDivExpr *OtherDiv = dyn_cast<SCEVUDivExpr>(LHS)) {
3376 if (auto *DivisorConstant =
3377 dyn_cast<SCEVConstant>(OtherDiv->getRHS())) {
3378 bool Overflow = false;
3379 APInt NewRHS =
3380 DivisorConstant->getAPInt().umul_ov(RHSC->getAPInt(), Overflow);
3381 if (Overflow) {
3382 return getConstant(RHSC->getType(), 0, false);
3383 }
3384 return getUDivExpr(OtherDiv->getLHS(), getConstant(NewRHS));
3385 }
3386 }
3387
3388 // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded.
3389 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(LHS)) {
3390 SmallVector<const SCEV *, 4> Operands;
3391 for (const SCEV *Op : A->operands())
3392 Operands.push_back(getZeroExtendExpr(Op, ExtTy));
3393 if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) {
3394 Operands.clear();
3395 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
3396 const SCEV *Op = getUDivExpr(A->getOperand(i), RHS);
3397 if (isa<SCEVUDivExpr>(Op) ||
3398 getMulExpr(Op, RHS) != A->getOperand(i))
3399 break;
3400 Operands.push_back(Op);
3401 }
3402 if (Operands.size() == A->getNumOperands())
3403 return getAddExpr(Operands);
3404 }
3405 }
3406
3407 // Fold if both operands are constant.
3408 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
3409 Constant *LHSCV = LHSC->getValue();
3410 Constant *RHSCV = RHSC->getValue();
3411 return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV,
3412 RHSCV)));
3413 }
3414 }
3415 }
3416
3417 // The Insertion Point (IP) might be invalid by now (due to UniqueSCEVs
3418 // changes). Make sure we get a new one.
3419 IP = nullptr;
3420 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
3421 SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator),
3422 LHS, RHS);
3423 UniqueSCEVs.InsertNode(S, IP);
3424 addToLoopUseLists(S);
3425 return S;
3426}
3427
3428static const APInt gcd(const SCEVConstant *C1, const SCEVConstant *C2) {
3429 APInt A = C1->getAPInt().abs();
3430 APInt B = C2->getAPInt().abs();
3431 uint32_t ABW = A.getBitWidth();
3432 uint32_t BBW = B.getBitWidth();
3433
3434 if (ABW > BBW)
3435 B = B.zext(ABW);
3436 else if (ABW < BBW)
3437 A = A.zext(BBW);
3438
3439 return APIntOps::GreatestCommonDivisor(std::move(A), std::move(B));
3440}
3441
3442/// Get a canonical unsigned division expression, or something simpler if
3443/// possible. There is no representation for an exact udiv in SCEV IR, but we
3444/// can attempt to remove factors from the LHS and RHS. We can't do this when
3445/// it's not exact because the udiv may be clearing bits.
3446const SCEV *ScalarEvolution::getUDivExactExpr(const SCEV *LHS,
3447 const SCEV *RHS) {
3448 // TODO: we could try to find factors in all sorts of things, but for now we
3449 // just deal with u/exact (multiply, constant). See SCEVDivision towards the
3450 // end of this file for inspiration.
3451
3452 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(LHS);
3453 if (!Mul || !Mul->hasNoUnsignedWrap())
3454 return getUDivExpr(LHS, RHS);
3455
3456 if (const SCEVConstant *RHSCst = dyn_cast<SCEVConstant>(RHS)) {
3457 // If the mulexpr multiplies by a constant, then that constant must be the
3458 // first element of the mulexpr.
3459 if (const auto *LHSCst = dyn_cast<SCEVConstant>(Mul->getOperand(0))) {
3460 if (LHSCst == RHSCst) {
3461 SmallVector<const SCEV *, 2> Operands(drop_begin(Mul->operands()));
3462 return getMulExpr(Operands);
3463 }
3464
3465 // We can't just assume that LHSCst divides RHSCst cleanly, it could be
3466 // that there's a factor provided by one of the other terms. We need to
3467 // check.
3468 APInt Factor = gcd(LHSCst, RHSCst);
3469 if (!Factor.isIntN(1)) {
3470 LHSCst =
3471 cast<SCEVConstant>(getConstant(LHSCst->getAPInt().udiv(Factor)));
3472 RHSCst =
3473 cast<SCEVConstant>(getConstant(RHSCst->getAPInt().udiv(Factor)));
3474 SmallVector<const SCEV *, 2> Operands;
3475 Operands.push_back(LHSCst);
3476 Operands.append(Mul->op_begin() + 1, Mul->op_end());
3477 LHS = getMulExpr(Operands);
3478 RHS = RHSCst;
3479 Mul = dyn_cast<SCEVMulExpr>(LHS);
3480 if (!Mul)
3481 return getUDivExactExpr(LHS, RHS);
3482 }
3483 }
3484 }
3485
3486 for (int i = 0, e = Mul->getNumOperands(); i != e; ++i) {
3487 if (Mul->getOperand(i) == RHS) {
3488 SmallVector<const SCEV *, 2> Operands;
3489 Operands.append(Mul->op_begin(), Mul->op_begin() + i);
3490 Operands.append(Mul->op_begin() + i + 1, Mul->op_end());
3491 return getMulExpr(Operands);
3492 }
3493 }
3494
3495 return getUDivExpr(LHS, RHS);
3496}
3497
3498/// Get an add recurrence expression for the specified loop. Simplify the
3499/// expression as much as possible.
3500const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, const SCEV *Step,
3501 const Loop *L,
3502 SCEV::NoWrapFlags Flags) {
3503 SmallVector<const SCEV *, 4> Operands;
3504 Operands.push_back(Start);
3505 if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
3506 if (StepChrec->getLoop() == L) {
3507 Operands.append(StepChrec->op_begin(), StepChrec->op_end());
3508 return getAddRecExpr(Operands, L, maskFlags(Flags, SCEV::FlagNW));
3509 }
3510
3511 Operands.push_back(Step);
3512 return getAddRecExpr(Operands, L, Flags);
3513}
3514
3515/// Get an add recurrence expression for the specified loop. Simplify the
3516/// expression as much as possible.
3517const SCEV *
3518ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
3519 const Loop *L, SCEV::NoWrapFlags Flags) {
3520 if (Operands.size() == 1) return Operands[0];
3521#ifndef NDEBUG1
3522 Type *ETy = getEffectiveSCEVType(Operands[0]->getType());
3523 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
3524 assert(getEffectiveSCEVType(Operands[i]->getType()) == ETy &&(static_cast<void> (0))
3525 "SCEVAddRecExpr operand types don't match!")(static_cast<void> (0));
3526 assert(!Operands[i]->getType()->isPointerTy() && "Step must be integer")(static_cast<void> (0));
3527 }
3528 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
3529 assert(isLoopInvariant(Operands[i], L) &&(static_cast<void> (0))
3530 "SCEVAddRecExpr operand is not loop-invariant!")(static_cast<void> (0));
3531#endif
3532
3533 if (Operands.back()->isZero()) {
3534 Operands.pop_back();
3535 return getAddRecExpr(Operands, L, SCEV::FlagAnyWrap); // {X,+,0} --> X
3536 }
3537
3538 // It's tempting to want to call getConstantMaxBackedgeTakenCount count here and
3539 // use that information to infer NUW and NSW flags. However, computing a
3540 // BE count requires calling getAddRecExpr, so we may not yet have a
3541 // meaningful BE count at this point (and if we don't, we'd be stuck
3542 // with a SCEVCouldNotCompute as the cached BE count).
3543
3544 Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags);
3545
3546 // Canonicalize nested AddRecs in by nesting them in order of loop depth.
3547 if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
3548 const Loop *NestedLoop = NestedAR->getLoop();
3549 if (L->contains(NestedLoop)
3550 ? (L->getLoopDepth() < NestedLoop->getLoopDepth())
3551 : (!NestedLoop->contains(L) &&
3552 DT.dominates(L->getHeader(), NestedLoop->getHeader()))) {
3553 SmallVector<const SCEV *, 4> NestedOperands(NestedAR->operands());
3554 Operands[0] = NestedAR->getStart();
3555 // AddRecs require their operands be loop-invariant with respect to their
3556 // loops. Don't perform this transformation if it would break this
3557 // requirement.
3558 bool AllInvariant = all_of(
3559 Operands, [&](const SCEV *Op) { return isLoopInvariant(Op, L); });
3560
3561 if (AllInvariant) {
3562 // Create a recurrence for the outer loop with the same step size.
3563 //
3564 // The outer recurrence keeps its NW flag but only keeps NUW/NSW if the
3565 // inner recurrence has the same property.
3566 SCEV::NoWrapFlags OuterFlags =
3567 maskFlags(Flags, SCEV::FlagNW | NestedAR->getNoWrapFlags());
3568
3569 NestedOperands[0] = getAddRecExpr(Operands, L, OuterFlags);
3570 AllInvariant = all_of(NestedOperands, [&](const SCEV *Op) {
3571 return isLoopInvariant(Op, NestedLoop);
3572 });
3573
3574 if (AllInvariant) {
3575 // Ok, both add recurrences are valid after the transformation.
3576 //
3577 // The inner recurrence keeps its NW flag but only keeps NUW/NSW if
3578 // the outer recurrence has the same property.
3579 SCEV::NoWrapFlags InnerFlags =
3580 maskFlags(NestedAR->getNoWrapFlags(), SCEV::FlagNW | Flags);
3581 return getAddRecExpr(NestedOperands, NestedLoop, InnerFlags);
3582 }
3583 }
3584 // Reset Operands to its original state.
3585 Operands[0] = NestedAR;
3586 }
3587 }
3588
3589 // Okay, it looks like we really DO need an addrec expr. Check to see if we
3590 // already have one, otherwise create a new one.
3591 return getOrCreateAddRecExpr(Operands, L, Flags);
3592}
3593
3594const SCEV *
3595ScalarEvolution::getGEPExpr(GEPOperator *GEP,
3596 const SmallVectorImpl<const SCEV *> &IndexExprs) {
3597 const SCEV *BaseExpr = getSCEV(GEP->getPointerOperand());
3598 // getSCEV(Base)->getType() has the same address space as Base->getType()
3599 // because SCEV::getType() preserves the address space.
3600 Type *IntIdxTy = getEffectiveSCEVType(BaseExpr->getType());
3601 // FIXME(PR23527): Don't blindly transfer the inbounds flag from the GEP
3602 // instruction to its SCEV, because the Instruction may be guarded by control
3603 // flow and the no-overflow bits may not be valid for the expression in any
3604 // context. This can be fixed similarly to how these flags are handled for
3605 // adds.
3606 SCEV::NoWrapFlags OffsetWrap =
3607 GEP->isInBounds() ? SCEV::FlagNSW : SCEV::FlagAnyWrap;
3608
3609 Type *CurTy = GEP->getType();
3610 bool FirstIter = true;
3611 SmallVector<const SCEV *, 4> Offsets;
3612 for (const SCEV *IndexExpr : IndexExprs) {
3613 // Compute the (potentially symbolic) offset in bytes for this index.
3614 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
3615 // For a struct, add the member offset.
3616 ConstantInt *Index = cast<SCEVConstant>(IndexExpr)->getValue();
3617 unsigned FieldNo = Index->getZExtValue();
3618 const SCEV *FieldOffset = getOffsetOfExpr(IntIdxTy, STy, FieldNo);
3619 Offsets.push_back(FieldOffset);
3620
3621 // Update CurTy to the type of the field at Index.
3622 CurTy = STy->getTypeAtIndex(Index);
3623 } else {
3624 // Update CurTy to its element type.
3625 if (FirstIter) {
3626 assert(isa<PointerType>(CurTy) &&(static_cast<void> (0))
3627 "The first index of a GEP indexes a pointer")(static_cast<void> (0));
3628 CurTy = GEP->getSourceElementType();
3629 FirstIter = false;
3630 } else {
3631 CurTy = GetElementPtrInst::getTypeAtIndex(CurTy, (uint64_t)0);
3632 }
3633 // For an array, add the element offset, explicitly scaled.
3634 const SCEV *ElementSize = getSizeOfExpr(IntIdxTy, CurTy);
3635 // Getelementptr indices are signed.
3636 IndexExpr = getTruncateOrSignExtend(IndexExpr, IntIdxTy);
3637
3638 // Multiply the index by the element size to compute the element offset.
3639 const SCEV *LocalOffset = getMulExpr(IndexExpr, ElementSize, OffsetWrap);
3640 Offsets.push_back(LocalOffset);
3641 }
3642 }
3643
3644 // Handle degenerate case of GEP without offsets.
3645 if (Offsets.empty())
3646 return BaseExpr;
3647
3648 // Add the offsets together, assuming nsw if inbounds.
3649 const SCEV *Offset = getAddExpr(Offsets, OffsetWrap);
3650 // Add the base address and the offset. We cannot use the nsw flag, as the
3651 // base address is unsigned. However, if we know that the offset is
3652 // non-negative, we can use nuw.
3653 SCEV::NoWrapFlags BaseWrap = GEP->isInBounds() && isKnownNonNegative(Offset)
3654 ? SCEV::FlagNUW : SCEV::FlagAnyWrap;
3655 auto *GEPExpr = getAddExpr(BaseExpr, Offset, BaseWrap);
3656 assert(BaseExpr->getType() == GEPExpr->getType() &&(static_cast<void> (0))
3657 "GEP should not change type mid-flight.")(static_cast<void> (0));
3658 return GEPExpr;
3659}
3660
3661std::tuple<SCEV *, FoldingSetNodeID, void *>
3662ScalarEvolution::findExistingSCEVInCache(SCEVTypes SCEVType,
3663 ArrayRef<const SCEV *> Ops) {
3664 FoldingSetNodeID ID;
3665 void *IP = nullptr;
3666 ID.AddInteger(SCEVType);
3667 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
3668 ID.AddPointer(Ops[i]);
3669 return std::tuple<SCEV *, FoldingSetNodeID, void *>(
3670 UniqueSCEVs.FindNodeOrInsertPos(ID, IP), std::move(ID), IP);
3671}
3672
3673const SCEV *ScalarEvolution::getAbsExpr(const SCEV *Op, bool IsNSW) {
3674 SCEV::NoWrapFlags Flags = IsNSW ? SCEV::FlagNSW : SCEV::FlagAnyWrap;
3675 return getSMaxExpr(Op, getNegativeSCEV(Op, Flags));
3676}
3677
3678const SCEV *ScalarEvolution::getMinMaxExpr(SCEVTypes Kind,
3679 SmallVectorImpl<const SCEV *> &Ops) {
3680 assert(!Ops.empty() && "Cannot get empty (u|s)(min|max)!")(static_cast<void> (0));
3681 if (Ops.size() == 1) return Ops[0];
3682#ifndef NDEBUG1
3683 Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
3684 for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
3685 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&(static_cast<void> (0))
3686 "Operand types don't match!")(static_cast<void> (0));
3687 assert(Ops[0]->getType()->isPointerTy() ==(static_cast<void> (0))
3688 Ops[i]->getType()->isPointerTy() &&(static_cast<void> (0))
3689 "min/max should be consistently pointerish")(static_cast<void> (0));
3690 }
3691#endif
3692
3693 bool IsSigned = Kind == scSMaxExpr || Kind == scSMinExpr;
3694 bool IsMax = Kind == scSMaxExpr || Kind == scUMaxExpr;
3695
3696 // Sort by complexity, this groups all similar expression types together.
3697 GroupByComplexity(Ops, &LI, DT);
3698
3699 // Check if we have created the same expression before.
3700 if (const SCEV *S = std::get<0>(findExistingSCEVInCache(Kind, Ops))) {
3701 return S;
3702 }
3703
3704 // If there are any constants, fold them together.
3705 unsigned Idx = 0;
3706 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
3707 ++Idx;
3708 assert(Idx < Ops.size())(static_cast<void> (0));
3709 auto FoldOp = [&](const APInt &LHS, const APInt &RHS) {
3710 if (Kind == scSMaxExpr)
3711 return APIntOps::smax(LHS, RHS);
3712 else if (Kind == scSMinExpr)
3713 return APIntOps::smin(LHS, RHS);
3714 else if (Kind == scUMaxExpr)
3715 return APIntOps::umax(LHS, RHS);
3716 else if (Kind == scUMinExpr)
3717 return APIntOps::umin(LHS, RHS);
3718 llvm_unreachable("Unknown SCEV min/max opcode")__builtin_unreachable();
3719 };
3720
3721 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
3722 // We found two constants, fold them together!
3723 ConstantInt *Fold = ConstantInt::get(
3724 getContext(), FoldOp(LHSC->getAPInt(), RHSC->getAPInt()));
3725 Ops[0] = getConstant(Fold);
3726 Ops.erase(Ops.begin()+1); // Erase the folded element
3727 if (Ops.size() == 1) return Ops[0];
3728 LHSC = cast<SCEVConstant>(Ops[0]);
3729 }
3730
3731 bool IsMinV = LHSC->getValue()->isMinValue(IsSigned);
3732 bool IsMaxV = LHSC->getValue()->isMaxValue(IsSigned);
3733
3734 if (IsMax ? IsMinV : IsMaxV) {
3735 // If we are left with a constant minimum(/maximum)-int, strip it off.
3736 Ops.erase(Ops.begin());
3737 --Idx;
3738 } else if (IsMax ? IsMaxV : IsMinV) {
3739 // If we have a max(/min) with a constant maximum(/minimum)-int,
3740 // it will always be the extremum.
3741 return LHSC;
3742 }
3743
3744 if (Ops.size() == 1) return Ops[0];
3745 }
3746
3747 // Find the first operation of the same kind
3748 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < Kind)
3749 ++Idx;
3750
3751 // Check to see if one of the operands is of the same kind. If so, expand its
3752 // operands onto our operand list, and recurse to simplify.
3753 if (Idx < Ops.size()) {
3754 bool DeletedAny = false;
3755 while (Ops[Idx]->getSCEVType() == Kind) {
3756 const SCEVMinMaxExpr *SMME = cast<SCEVMinMaxExpr>(Ops[Idx]);
3757 Ops.erase(Ops.begin()+Idx);
3758 Ops.append(SMME->op_begin(), SMME->op_end());
3759 DeletedAny = true;
3760 }
3761
3762 if (DeletedAny)
3763 return getMinMaxExpr(Kind, Ops);
3764 }
3765
3766 // Okay, check to see if the same value occurs in the operand list twice. If
3767 // so, delete one. Since we sorted the list, these values are required to
3768 // be adjacent.
3769 llvm::CmpInst::Predicate GEPred =
3770 IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
3771 llvm::CmpInst::Predicate LEPred =
3772 IsSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
3773 llvm::CmpInst::Predicate FirstPred = IsMax ? GEPred : LEPred;
3774 llvm::CmpInst::Predicate SecondPred = IsMax ? LEPred : GEPred;
3775 for (unsigned i = 0, e = Ops.size() - 1; i != e; ++i) {
3776 if (Ops[i] == Ops[i + 1] ||
3777 isKnownViaNonRecursiveReasoning(FirstPred, Ops[i], Ops[i + 1])) {
3778 // X op Y op Y --> X op Y
3779 // X op Y --> X, if we know X, Y are ordered appropriately
3780 Ops.erase(Ops.begin() + i + 1, Ops.begin() + i + 2);
3781 --i;
3782 --e;
3783 } else if (isKnownViaNonRecursiveReasoning(SecondPred, Ops[i],
3784 Ops[i + 1])) {
3785 // X op Y --> Y, if we know X, Y are ordered appropriately
3786 Ops.erase(Ops.begin() + i, Ops.begin() + i + 1);
3787 --i;
3788 --e;
3789 }
3790 }
3791
3792 if (Ops.size() == 1) return Ops[0];
3793
3794 assert(!Ops.empty() && "Reduced smax down to nothing!")(static_cast<void> (0));
3795
3796 // Okay, it looks like we really DO need an expr. Check to see if we
3797 // already have one, otherwise create a new one.
3798 const SCEV *ExistingSCEV;
3799 FoldingSetNodeID ID;
3800 void *IP;
3801 std::tie(ExistingSCEV, ID, IP) = findExistingSCEVInCache(Kind, Ops);
3802 if (ExistingSCEV)
3803 return ExistingSCEV;
3804 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
3805 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
3806 SCEV *S = new (SCEVAllocator)
3807 SCEVMinMaxExpr(ID.Intern(SCEVAllocator), Kind, O, Ops.size());
3808
3809 UniqueSCEVs.InsertNode(S, IP);
3810 addToLoopUseLists(S);
3811 return S;
3812}
3813
3814const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, const SCEV *RHS) {
3815 SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
3816 return getSMaxExpr(Ops);
3817}
3818
3819const SCEV *ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
3820 return getMinMaxExpr(scSMaxExpr, Ops);
3821}
3822
3823const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, const SCEV *RHS) {
3824 SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
3825 return getUMaxExpr(Ops);
3826}
3827
3828const SCEV *ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
3829 return getMinMaxExpr(scUMaxExpr, Ops);
3830}
3831
3832const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS,
3833 const SCEV *RHS) {
3834 SmallVector<const SCEV *, 2> Ops = { LHS, RHS };
3835 return getSMinExpr(Ops);
3836}
3837
3838const SCEV *ScalarEvolution::getSMinExpr(SmallVectorImpl<const SCEV *> &Ops) {
3839 return getMinMaxExpr(scSMinExpr, Ops);
3840}
3841
3842const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS,
3843 const SCEV *RHS) {
3844 SmallVector<const SCEV *, 2> Ops = { LHS, RHS };
3845 return getUMinExpr(Ops);
3846}
3847
3848const SCEV *ScalarEvolution::getUMinExpr(SmallVectorImpl<const SCEV *> &Ops) {
3849 return getMinMaxExpr(scUMinExpr, Ops);
3850}
3851
3852const SCEV *
3853ScalarEvolution::getSizeOfScalableVectorExpr(Type *IntTy,
3854 ScalableVectorType *ScalableTy) {
3855 Constant *NullPtr = Constant::getNullValue(ScalableTy->getPointerTo());
3856 Constant *One = ConstantInt::get(IntTy, 1);
3857 Constant *GEP = ConstantExpr::getGetElementPtr(ScalableTy, NullPtr, One);
3858 // Note that the expression we created is the final expression, we don't
3859 // want to simplify it any further Also, if we call a normal getSCEV(),
3860 // we'll end up in an endless recursion. So just create an SCEVUnknown.
3861 return getUnknown(ConstantExpr::getPtrToInt(GEP, IntTy));
3862}
3863
3864const SCEV *ScalarEvolution::getSizeOfExpr(Type *IntTy, Type *AllocTy) {
3865 if (auto *ScalableAllocTy = dyn_cast<ScalableVectorType>(AllocTy))
3866 return getSizeOfScalableVectorExpr(IntTy, ScalableAllocTy);
3867 // We can bypass creating a target-independent constant expression and then
3868 // folding it back into a ConstantInt. This is just a compile-time
3869 // optimization.
3870 return getConstant(IntTy, getDataLayout().getTypeAllocSize(AllocTy));
3871}
3872
3873const SCEV *ScalarEvolution::getStoreSizeOfExpr(Type *IntTy, Type *StoreTy) {
3874 if (auto *ScalableStoreTy = dyn_cast<ScalableVectorType>(StoreTy))
3875 return getSizeOfScalableVectorExpr(IntTy, ScalableStoreTy);
3876 // We can bypass creating a target-independent constant expression and then
3877 // folding it back into a ConstantInt. This is just a compile-time
3878 // optimization.
3879 return getConstant(IntTy, getDataLayout().getTypeStoreSize(StoreTy));
3880}
3881
3882const SCEV *ScalarEvolution::getOffsetOfExpr(Type *IntTy,
3883 StructType *STy,
3884 unsigned FieldNo) {
3885 // We can bypass creating a target-independent constant expression and then
3886 // folding it back into a ConstantInt. This is just a compile-time
3887 // optimization.
3888 return getConstant(
3889 IntTy, getDataLayout().getStructLayout(STy)->getElementOffset(FieldNo));
3890}
3891
3892const SCEV *ScalarEvolution::getUnknown(Value *V) {
3893 // Don't attempt to do anything other than create a SCEVUnknown object
3894 // here. createSCEV only calls getUnknown after checking for all other
3895 // interesting possibilities, and any other code that calls getUnknown
3896 // is doing so in order to hide a value from SCEV canonicalization.
3897
3898 FoldingSetNodeID ID;
3899 ID.AddInteger(scUnknown);
3900 ID.AddPointer(V);
3901 void *IP = nullptr;
3902 if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) {
3903 assert(cast<SCEVUnknown>(S)->getValue() == V &&(static_cast<void> (0))
3904 "Stale SCEVUnknown in uniquing map!")(static_cast<void> (0));
3905 return S;
3906 }
3907 SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator), V, this,
3908 FirstUnknown);
3909 FirstUnknown = cast<SCEVUnknown>(S);
3910 UniqueSCEVs.InsertNode(S, IP);
3911 return S;
3912}
3913
3914//===----------------------------------------------------------------------===//
3915// Basic SCEV Analysis and PHI Idiom Recognition Code
3916//
3917
3918/// Test if values of the given type are analyzable within the SCEV
3919/// framework. This primarily includes integer types, and it can optionally
3920/// include pointer types if the ScalarEvolution class has access to
3921/// target-specific information.
3922bool ScalarEvolution::isSCEVable(Type *Ty) const {
3923 // Integers and pointers are always SCEVable.
3924 return Ty->isIntOrPtrTy();
3925}
3926
3927/// Return the size in bits of the specified type, for which isSCEVable must
3928/// return true.
3929uint64_t ScalarEvolution::getTypeSizeInBits(Type *Ty) const {
3930 assert(isSCEVable(Ty) && "Type is not SCEVable!")(static_cast<void> (0));
3931 if (Ty->isPointerTy())
3932 return getDataLayout().getIndexTypeSizeInBits(Ty);
3933 return getDataLayout().getTypeSizeInBits(Ty);
3934}
3935
3936/// Return a type with the same bitwidth as the given type and which represents
3937/// how SCEV will treat the given type, for which isSCEVable must return
3938/// true. For pointer types, this is the pointer index sized integer type.
3939Type *ScalarEvolution::getEffectiveSCEVType(Type *Ty) const {
3940 assert(isSCEVable(Ty) && "Type is not SCEVable!")(static_cast<void> (0));
3941
3942 if (Ty->isIntegerTy())
3943 return Ty;
3944
3945 // The only other support type is pointer.
3946 assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!")(static_cast<void> (0));
3947 return getDataLayout().getIndexType(Ty);
3948}
3949
3950Type *ScalarEvolution::getWiderType(Type *T1, Type *T2) const {
3951 return getTypeSizeInBits(T1) >= getTypeSizeInBits(T2) ? T1 : T2;
3952}
3953
3954const SCEV *ScalarEvolution::getCouldNotCompute() {
3955 return CouldNotCompute.get();
3956}
3957
3958bool ScalarEvolution::checkValidity(const SCEV *S) const {
3959 bool ContainsNulls = SCEVExprContains(S, [](const SCEV *S) {
3960 auto *SU = dyn_cast<SCEVUnknown>(S);
3961 return SU && SU->getValue() == nullptr;
3962 });
3963
3964 return !ContainsNulls;
3965}
3966
3967bool ScalarEvolution::containsAddRecurrence(const SCEV *S) {
3968 HasRecMapType::iterator I = HasRecMap.find(S);
3969 if (I != HasRecMap.end())
3970 return I->second;
3971
3972 bool FoundAddRec =
3973 SCEVExprContains(S, [](const SCEV *S) { return isa<SCEVAddRecExpr>(S); });
3974 HasRecMap.insert({S, FoundAddRec});
3975 return FoundAddRec;
3976}
3977
3978/// Try to split a SCEVAddExpr into a pair of {SCEV, ConstantInt}.
3979/// If \p S is a SCEVAddExpr and is composed of a sub SCEV S' and an
3980/// offset I, then return {S', I}, else return {\p S, nullptr}.
3981static std::pair<const SCEV *, ConstantInt *> splitAddExpr(const SCEV *S) {
3982 const auto *Add = dyn_cast<SCEVAddExpr>(S);
3983 if (!Add)
3984 return {S, nullptr};
3985
3986 if (Add->getNumOperands() != 2)
3987 return {S, nullptr};
3988
3989 auto *ConstOp = dyn_cast<SCEVConstant>(Add->getOperand(0));
3990 if (!ConstOp)
3991 return {S, nullptr};
3992
3993 return {Add->getOperand(1), ConstOp->getValue()};
3994}
3995
3996/// Return the ValueOffsetPair set for \p S. \p S can be represented
3997/// by the value and offset from any ValueOffsetPair in the set.
3998ScalarEvolution::ValueOffsetPairSetVector *
3999ScalarEvolution::getSCEVValues(const SCEV *S) {
4000 ExprValueMapType::iterator SI = ExprValueMap.find_as(S);
4001 if (SI == ExprValueMap.end())
4002 return nullptr;
4003#ifndef NDEBUG1
4004 if (VerifySCEVMap) {
4005 // Check there is no dangling Value in the set returned.
4006 for (const auto &VE : SI->second)
4007 assert(ValueExprMap.count(VE.first))(static_cast<void> (0));
4008 }
4009#endif
4010 return &SI->second;
4011}
4012
4013/// Erase Value from ValueExprMap and ExprValueMap. ValueExprMap.erase(V)
4014/// cannot be used separately. eraseValueFromMap should be used to remove
4015/// V from ValueExprMap and ExprValueMap at the same time.
4016void ScalarEvolution::eraseValueFromMap(Value *V) {
4017 ValueExprMapType::iterator I = ValueExprMap.find_as(V);
4018 if (I != ValueExprMap.end()) {
4019 const SCEV *S = I->second;
4020 // Remove {V, 0} from the set of ExprValueMap[S]
4021 if (auto *SV = getSCEVValues(S))
4022 SV->remove({V, nullptr});
4023
4024 // Remove {V, Offset} from the set of ExprValueMap[Stripped]
4025 const SCEV *Stripped;
4026 ConstantInt *Offset;
4027 std::tie(Stripped, Offset) = splitAddExpr(S);
4028 if (Offset != nullptr) {
4029 if (auto *SV = getSCEVValues(Stripped))
4030 SV->remove({V, Offset});
4031 }
4032 ValueExprMap.erase(V);
4033 }
4034}
4035
4036/// Check whether value has nuw/nsw/exact set but SCEV does not.
4037/// TODO: In reality it is better to check the poison recursively
4038/// but this is better than nothing.
4039static bool SCEVLostPoisonFlags(const SCEV *S, const Value *V) {
4040 if (auto *I = dyn_cast<Instruction>(V)) {
4041 if (isa<OverflowingBinaryOperator>(I)) {
4042 if (auto *NS = dyn_cast<SCEVNAryExpr>(S)) {
4043 if (I->hasNoSignedWrap() && !NS->hasNoSignedWrap())
4044 return true;
4045 if (I->hasNoUnsignedWrap() && !NS->hasNoUnsignedWrap())
4046 return true;
4047 }
4048 } else if (isa<PossiblyExactOperator>(I) && I->isExact())
4049 return true;
4050 }
4051 return false;
4052}
4053
4054/// Return an existing SCEV if it exists, otherwise analyze the expression and
4055/// create a new one.
4056const SCEV *ScalarEvolution::getSCEV(Value *V) {
4057 assert(isSCEVable(V->getType()) && "Value is not SCEVable!")(static_cast<void> (0));
4058
4059 const SCEV *S = getExistingSCEV(V);
4060 if (S == nullptr) {
4061 S = createSCEV(V);
4062 // During PHI resolution, it is possible to create two SCEVs for the same
4063 // V, so it is needed to double check whether V->S is inserted into
4064 // ValueExprMap before insert S->{V, 0} into ExprValueMap.
4065 std::pair<ValueExprMapType::iterator, bool> Pair =
4066 ValueExprMap.insert({SCEVCallbackVH(V, this), S});
4067 if (Pair.second && !SCEVLostPoisonFlags(S, V)) {
4068 ExprValueMap[S].insert({V, nullptr});
4069
4070 // If S == Stripped + Offset, add Stripped -> {V, Offset} into
4071 // ExprValueMap.
4072 const SCEV *Stripped = S;
4073 ConstantInt *Offset = nullptr;
4074 std::tie(Stripped, Offset) = splitAddExpr(S);
4075 // If stripped is SCEVUnknown, don't bother to save
4076 // Stripped -> {V, offset}. It doesn't simplify and sometimes even
4077 // increase the complexity of the expansion code.
4078 // If V is GetElementPtrInst, don't save Stripped -> {V, offset}
4079 // because it may generate add/sub instead of GEP in SCEV expansion.
4080 if (Offset != nullptr && !isa<SCEVUnknown>(Stripped) &&
4081 !isa<GetElementPtrInst>(V))
4082 ExprValueMap[Stripped].insert({V, Offset});
4083 }
4084 }
4085 return S;
4086}
4087
4088const SCEV *ScalarEvolution::getExistingSCEV(Value *V) {
4089 assert(isSCEVable(V->getType()) && "Value is not SCEVable!")(static_cast<void> (0));
4090
4091 ValueExprMapType::iterator I = ValueExprMap.find_as(V);
4092 if (I != ValueExprMap.end()) {
4093 const SCEV *S = I->second;
4094 if (checkValidity(S))
4095 return S;
4096 eraseValueFromMap(V);
4097 forgetMemoizedResults(S);
4098 }
4099 return nullptr;
4100}
4101
4102/// Return a SCEV corresponding to -V = -1*V
4103const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V,
4104 SCEV::NoWrapFlags Flags) {
4105 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
4106 return getConstant(
4107 cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue())));
4108
4109 Type *Ty = V->getType();
4110 Ty = getEffectiveSCEVType(Ty);
4111 return getMulExpr(V, getMinusOne(Ty), Flags);
4112}
4113
4114/// If Expr computes ~A, return A else return nullptr
4115static const SCEV *MatchNotExpr(const SCEV *Expr) {
4116 const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Expr);
4117 if (!Add || Add->getNumOperands() != 2 ||
4118 !Add->getOperand(0)->isAllOnesValue())
4119 return nullptr;
4120
4121 const SCEVMulExpr *AddRHS = dyn_cast<SCEVMulExpr>(Add->getOperand(1));
4122 if (!AddRHS || AddRHS->getNumOperands() != 2 ||
4123 !AddRHS->getOperand(0)->isAllOnesValue())
4124 return nullptr;
4125
4126 return AddRHS->getOperand(1);
4127}
4128
4129/// Return a SCEV corresponding to ~V = -1-V
4130const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) {
4131 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
4132 return getConstant(
4133 cast<ConstantInt>(ConstantExpr::getNot(VC->getValue())));
4134
4135 // Fold ~(u|s)(min|max)(~x, ~y) to (u|s)(max|min)(x, y)
4136 if (const SCEVMinMaxExpr *MME = dyn_cast<SCEVMinMaxExpr>(V)) {
4137 auto MatchMinMaxNegation = [&](const SCEVMinMaxExpr *MME) {
4138 SmallVector<const SCEV *, 2> MatchedOperands;
4139 for (const SCEV *Operand : MME->operands()) {
4140 const SCEV *Matched = MatchNotExpr(Operand);
4141 if (!Matched)
4142 return (const SCEV *)nullptr;
4143 MatchedOperands.push_back(Matched);
4144 }
4145 return getMinMaxExpr(SCEVMinMaxExpr::negate(MME->getSCEVType()),
4146 MatchedOperands);
4147 };
4148 if (const SCEV *Replaced = MatchMinMaxNegation(MME))
4149 return Replaced;
4150 }
4151
4152 Type *Ty = V->getType();
4153 Ty = getEffectiveSCEVType(Ty);
4154 return getMinusSCEV(getMinusOne(Ty), V);
4155}
4156
4157const SCEV *ScalarEvolution::removePointerBase(const SCEV *P) {
4158 assert(P->getType()->isPointerTy())(static_cast<void> (0));
4159
4160 if (auto *AddRec
18.1
'AddRec' is null
= dyn_cast<SCEVAddRecExpr>(P)) {
18
Assuming 'P' is not a 'SCEVAddRecExpr'
19
Taking false branch
4161 // The base of an AddRec is the first operand.
4162 SmallVector<const SCEV *> Ops{AddRec->operands()};
4163 Ops[0] = removePointerBase(Ops[0]);
4164 // Don't try to transfer nowrap flags for now. We could in some cases
4165 // (for example, if pointer operand of the AddRec is a SCEVUnknown).
4166 return getAddRecExpr(Ops, AddRec->getLoop(), SCEV::FlagAnyWrap);
4167 }
4168 if (auto *Add
20.1
'Add' is non-null
= dyn_cast<SCEVAddExpr>(P)) {
20
Assuming 'P' is a 'SCEVAddExpr'
21
Taking true branch
4169 // The base of an Add is the pointer operand.
4170 SmallVector<const SCEV *> Ops{Add->operands()};
4171 const SCEV **PtrOp = nullptr;
22
'PtrOp' initialized to a null pointer value
4172 for (const SCEV *&AddOp : Ops) {
23
Assuming '__begin2' is equal to '__end2'
4173 if (AddOp->getType()->isPointerTy()) {
4174 assert(!PtrOp && "Cannot have multiple pointer ops")(static_cast<void> (0));
4175 PtrOp = &AddOp;
4176 }
4177 }
4178 *PtrOp = removePointerBase(*PtrOp);
24
Dereference of null pointer (loaded from variable 'PtrOp')
4179 // Don't try to transfer nowrap flags for now. We could in some cases
4180 // (for example, if the pointer operand of the Add is a SCEVUnknown).
4181 return getAddExpr(Ops);
4182 }
4183 // Any other expression must be a pointer base.
4184 return getZero(P->getType());
4185}
4186
4187const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, const SCEV *RHS,
4188 SCEV::NoWrapFlags Flags,
4189 unsigned Depth) {
4190 // Fast path: X - X --> 0.
4191 if (LHS == RHS)
13
Assuming 'LHS' is not equal to 'RHS'
14
Taking false branch
4192 return getZero(LHS->getType());
4193
4194 // If we subtract two pointers with different pointer bases, bail.
4195 // Eventually, we're going to add an assertion to getMulExpr that we
4196 // can't multiply by a pointer.
4197 if (RHS->getType()->isPointerTy()) {
4198 if (!LHS->getType()->isPointerTy() ||
16
Taking false branch
4199 getPointerBase(LHS) != getPointerBase(RHS))
15
Assuming the condition is false
4200 return getCouldNotCompute();
4201 LHS = removePointerBase(LHS);
17
Calling 'ScalarEvolution::removePointerBase'
4202 RHS = removePointerBase(RHS);
4203 }
4204
4205 // We represent LHS - RHS as LHS + (-1)*RHS. This transformation
4206 // makes it so that we cannot make much use of NUW.
4207 auto AddFlags = SCEV::FlagAnyWrap;
4208 const bool RHSIsNotMinSigned =
4209 !getSignedRangeMin(RHS).isMinSignedValue();
4210 if (hasFlags(Flags, SCEV::FlagNSW)) {
4211 // Let M be the minimum representable signed value. Then (-1)*RHS
4212 // signed-wraps if and only if RHS is M. That can happen even for
4213 // a NSW subtraction because e.g. (-1)*M signed-wraps even though
4214 // -1 - M does not. So to transfer NSW from LHS - RHS to LHS +
4215 // (-1)*RHS, we need to prove that RHS != M.
4216 //
4217 // If LHS is non-negative and we know that LHS - RHS does not
4218 // signed-wrap, then RHS cannot be M. So we can rule out signed-wrap
4219 // either by proving that RHS > M or that LHS >= 0.
4220 if (RHSIsNotMinSigned || isKnownNonNegative(LHS)) {
4221 AddFlags = SCEV::FlagNSW;
4222 }
4223 }
4224
4225 // FIXME: Find a correct way to transfer NSW to (-1)*M when LHS -
4226 // RHS is NSW and LHS >= 0.
4227 //
4228 // The difficulty here is that the NSW flag may have been proven
4229 // relative to a loop that is to be found in a recurrence in LHS and
4230 // not in RHS. Applying NSW to (-1)*M may then let the NSW have a
4231 // larger scope than intended.
4232 auto NegFlags = RHSIsNotMinSigned ? SCEV::FlagNSW : SCEV::FlagAnyWrap;
4233
4234 return getAddExpr(LHS, getNegativeSCEV(RHS, NegFlags), AddFlags, Depth);
4235}
4236
4237const SCEV *ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, Type *Ty,
4238 unsigned Depth) {
4239 Type *SrcTy = V->getType();
4240 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&(static_cast<void> (0))
4241 "Cannot truncate or zero extend with non-integer arguments!")(static_cast<void> (0));
4242 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4243 return V; // No conversion
4244 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
4245 return getTruncateExpr(V, Ty, Depth);
4246 return getZeroExtendExpr(V, Ty, Depth);
4247}
4248
4249const SCEV *ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, Type *Ty,
4250 unsigned Depth) {
4251 Type *SrcTy = V->getType();
4252 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&(static_cast<void> (0))
4253 "Cannot truncate or zero extend with non-integer arguments!")(static_cast<void> (0));
4254 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4255 return V; // No conversion
4256 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
4257 return getTruncateExpr(V, Ty, Depth);
4258 return getSignExtendExpr(V, Ty, Depth);
4259}
4260
4261const SCEV *
4262ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, Type *Ty) {
4263 Type *SrcTy = V->getType();
4264 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&(static_cast<void> (0))
4265 "Cannot noop or zero extend with non-integer arguments!")(static_cast<void> (0));
4266 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&(static_cast<void> (0))
4267 "getNoopOrZeroExtend cannot truncate!")(static_cast<void> (0));
4268 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4269 return V; // No conversion
4270 return getZeroExtendExpr(V, Ty);
4271}
4272
4273const SCEV *
4274ScalarEvolution::getNoopOrSignExtend(const SCEV *V, Type *Ty) {
4275 Type *SrcTy = V->getType();
4276 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&(static_cast<void> (0))
4277 "Cannot noop or sign extend with non-integer arguments!")(static_cast<void> (0));
4278 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&(static_cast<void> (0))
4279 "getNoopOrSignExtend cannot truncate!")(static_cast<void> (0));
4280 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4281 return V; // No conversion
4282 return getSignExtendExpr(V, Ty);
4283}
4284
4285const SCEV *
4286ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, Type *Ty) {
4287 Type *SrcTy = V->getType();
4288 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&(static_cast<void> (0))
4289 "Cannot noop or any extend with non-integer arguments!")(static_cast<void> (0));
4290 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&(static_cast<void> (0))
4291 "getNoopOrAnyExtend cannot truncate!")(static_cast<void> (0));
4292 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4293 return V; // No conversion
4294 return getAnyExtendExpr(V, Ty);
4295}
4296
4297const SCEV *
4298ScalarEvolution::getTruncateOrNoop(const SCEV *V, Type *Ty) {
4299 Type *SrcTy = V->getType();
4300 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&(static_cast<void> (0))
4301 "Cannot truncate or noop with non-integer arguments!")(static_cast<void> (0));
4302 assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) &&(static_cast<void> (0))
4303 "getTruncateOrNoop cannot extend!")(static_cast<void> (0));
4304 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4305 return V; // No conversion
4306 return getTruncateExpr(V, Ty);
4307}
4308
4309const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS,
4310 const SCEV *RHS) {
4311 const SCEV *PromotedLHS = LHS;
4312 const SCEV *PromotedRHS = RHS;
4313
4314 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
4315 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
4316 else
4317 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
4318
4319 return getUMaxExpr(PromotedLHS, PromotedRHS);
4320}
4321
4322const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS,
4323 const SCEV *RHS) {
4324 SmallVector<const SCEV *, 2> Ops = { LHS, RHS };
4325 return getUMinFromMismatchedTypes(Ops);
4326}
4327
4328const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(
4329 SmallVectorImpl<const SCEV *> &Ops) {
4330 assert(!Ops.empty() && "At least one operand must be!")(static_cast<void> (0));
4331 // Trivial case.
4332 if (Ops.size() == 1)
4333 return Ops[0];
4334
4335 // Find the max type first.
4336 Type *MaxType = nullptr;
4337 for (auto *S : Ops)
4338 if (MaxType)
4339 MaxType = getWiderType(MaxType, S->getType());
4340 else
4341 MaxType = S->getType();
4342 assert(MaxType && "Failed to find maximum type!")(static_cast<void> (0));
4343
4344 // Extend all ops to max type.
4345 SmallVector<const SCEV *, 2> PromotedOps;
4346 for (auto *S : Ops)
4347 PromotedOps.push_back(getNoopOrZeroExtend(S, MaxType));
4348
4349 // Generate umin.
4350 return getUMinExpr(PromotedOps);
4351}
4352
4353const SCEV *ScalarEvolution::getPointerBase(const SCEV *V) {
4354 // A pointer operand may evaluate to a nonpointer expression, such as null.
4355 if (!V->getType()->isPointerTy())
4356 return V;
4357
4358 while (true) {
4359 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
4360 V = AddRec->getStart();
4361 } else if (auto *Add = dyn_cast<SCEVAddExpr>(V)) {
4362 const SCEV *PtrOp = nullptr;
4363 for (const SCEV *AddOp : Add->operands()) {
4364 if (AddOp->getType()->isPointerTy()) {
4365 assert(!PtrOp && "Cannot have multiple pointer ops")(static_cast<void> (0));
4366 PtrOp = AddOp;
4367 }
4368 }
4369 assert(PtrOp && "Must have pointer op")(static_cast<void> (0));
4370 V = PtrOp;
4371 } else // Not something we can look further into.
4372 return V;
4373 }
4374}
4375
4376/// Push users of the given Instruction onto the given Worklist.
4377static void
4378PushDefUseChildren(Instruction *I,
4379 SmallVectorImpl<Instruction *> &Worklist) {
4380 // Push the def-use children onto the Worklist stack.
4381 for (User *U : I->users())
4382 Worklist.push_back(cast<Instruction>(U));
4383}
4384
4385void ScalarEvolution::forgetSymbolicName(Instruction *PN, const SCEV *SymName) {
4386 SmallVector<Instruction *, 16> Worklist;
4387 PushDefUseChildren(PN, Worklist);
4388
4389 SmallPtrSet<Instruction *, 8> Visited;
4390 Visited.insert(PN);
4391 while (!Worklist.empty()) {
4392 Instruction *I = Worklist.pop_back_val();
4393 if (!Visited.insert(I).second)
4394 continue;
4395
4396 auto It = ValueExprMap.find_as(static_cast<Value *>(I));
4397 if (It != ValueExprMap.end()) {
4398 const SCEV *Old = It->second;
4399
4400 // Short-circuit the def-use traversal if the symbolic name
4401 // ceases to appear in expressions.
4402 if (Old != SymName && !hasOperand(Old, SymName))
4403 continue;
4404
4405 // SCEVUnknown for a PHI either means that it has an unrecognized
4406 // structure, it's a PHI that's in the progress of being computed
4407 // by createNodeForPHI, or it's a single-value PHI. In the first case,
4408 // additional loop trip count information isn't going to change anything.
4409 // In the second case, createNodeForPHI will perform the necessary
4410 // updates on its own when it gets to that point. In the third, we do
4411 // want to forget the SCEVUnknown.
4412 if (!isa<PHINode>(I) ||
4413 !isa<SCEVUnknown>(Old) ||
4414 (I != PN && Old == SymName)) {
4415 eraseValueFromMap(It->first);
4416 forgetMemoizedResults(Old);
4417 }
4418 }
4419
4420 PushDefUseChildren(I, Worklist);
4421 }
4422}
4423
4424namespace {
4425
4426/// Takes SCEV S and Loop L. For each AddRec sub-expression, use its start
4427/// expression in case its Loop is L. If it is not L then
4428/// if IgnoreOtherLoops is true then use AddRec itself
4429/// otherwise rewrite cannot be done.
4430/// If SCEV contains non-invariant unknown SCEV rewrite cannot be done.
4431class SCEVInitRewriter : public SCEVRewriteVisitor<SCEVInitRewriter> {
4432public:
4433 static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE,
4434 bool IgnoreOtherLoops = true) {
4435 SCEVInitRewriter Rewriter(L, SE);
4436 const SCEV *Result = Rewriter.visit(S);
4437 if (Rewriter.hasSeenLoopVariantSCEVUnknown())
4438 return SE.getCouldNotCompute();
4439 return Rewriter.hasSeenOtherLoops() && !IgnoreOtherLoops
4440 ? SE.getCouldNotCompute()
4441 : Result;
4442 }
4443
4444 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4445 if (!SE.isLoopInvariant(Expr, L))
4446 SeenLoopVariantSCEVUnknown = true;
4447 return Expr;
4448 }
4449
4450 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
4451 // Only re-write AddRecExprs for this loop.
4452 if (Expr->getLoop() == L)
4453 return Expr->getStart();
4454 SeenOtherLoops = true;
4455 return Expr;
4456 }
4457
4458 bool hasSeenLoopVariantSCEVUnknown() { return SeenLoopVariantSCEVUnknown; }
4459
4460 bool hasSeenOtherLoops() { return SeenOtherLoops; }
4461
4462private:
4463 explicit SCEVInitRewriter(const Loop *L, ScalarEvolution &SE)
4464 : SCEVRewriteVisitor(SE), L(L) {}
4465
4466 const Loop *L;
4467 bool SeenLoopVariantSCEVUnknown = false;
4468 bool SeenOtherLoops = false;
4469};
4470
4471/// Takes SCEV S and Loop L. For each AddRec sub-expression, use its post
4472/// increment expression in case its Loop is L. If it is not L then
4473/// use AddRec itself.
4474/// If SCEV contains non-invariant unknown SCEV rewrite cannot be done.
4475class SCEVPostIncRewriter : public SCEVRewriteVisitor<SCEVPostIncRewriter> {
4476public:
4477 static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE) {
4478 SCEVPostIncRewriter Rewriter(L, SE);
4479 const SCEV *Result = Rewriter.visit(S);
4480 return Rewriter.hasSeenLoopVariantSCEVUnknown()
4481 ? SE.getCouldNotCompute()
4482 : Result;
4483 }
4484
4485 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4486 if (!SE.isLoopInvariant(Expr, L))
4487 SeenLoopVariantSCEVUnknown = true;
4488 return Expr;
4489 }
4490
4491 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
4492 // Only re-write AddRecExprs for this loop.
4493 if (Expr->getLoop() == L)
4494 return Expr->getPostIncExpr(SE);
4495 SeenOtherLoops = true;
4496 return Expr;
4497 }
4498
4499 bool hasSeenLoopVariantSCEVUnknown() { return SeenLoopVariantSCEVUnknown; }
4500
4501 bool hasSeenOtherLoops() { return SeenOtherLoops; }
4502
4503private:
4504 explicit SCEVPostIncRewriter(const Loop *L, ScalarEvolution &SE)
4505 : SCEVRewriteVisitor(SE), L(L) {}
4506
4507 const Loop *L;
4508 bool SeenLoopVariantSCEVUnknown = false;
4509 bool SeenOtherLoops = false;
4510};
4511
4512/// This class evaluates the compare condition by matching it against the
4513/// condition of loop latch. If there is a match we assume a true value
4514/// for the condition while building SCEV nodes.
4515class SCEVBackedgeConditionFolder
4516 : public SCEVRewriteVisitor<SCEVBackedgeConditionFolder> {
4517public:
4518 static const SCEV *rewrite(const SCEV *S, const Loop *L,
4519 ScalarEvolution &SE) {
4520 bool IsPosBECond = false;
4521 Value *BECond = nullptr;
4522 if (BasicBlock *Latch = L->getLoopLatch()) {
4523 BranchInst *BI = dyn_cast<BranchInst>(Latch->getTerminator());
4524 if (BI && BI->isConditional()) {
4525 assert(BI->getSuccessor(0) != BI->getSuccessor(1) &&(static_cast<void> (0))
4526 "Both outgoing branches should not target same header!")(static_cast<void> (0));
4527 BECond = BI->getCondition();
4528 IsPosBECond = BI->getSuccessor(0) == L->getHeader();
4529 } else {
4530 return S;
4531 }
4532 }
4533 SCEVBackedgeConditionFolder Rewriter(L, BECond, IsPosBECond, SE);
4534 return Rewriter.visit(S);
4535 }
4536
4537 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4538 const SCEV *Result = Expr;
4539 bool InvariantF = SE.isLoopInvariant(Expr, L);
4540
4541 if (!InvariantF) {
4542 Instruction *I = cast<Instruction>(Expr->getValue());
4543 switch (I->getOpcode()) {
4544 case Instruction::Select: {
4545 SelectInst *SI = cast<SelectInst>(I);
4546 Optional<const SCEV *> Res =
4547 compareWithBackedgeCondition(SI->getCondition());
4548 if (Res.hasValue()) {
4549 bool IsOne = cast<SCEVConstant>(Res.getValue())->getValue()->isOne();
4550 Result = SE.getSCEV(IsOne ? SI->getTrueValue() : SI->getFalseValue());
4551 }
4552 break;
4553 }
4554 default: {
4555 Optional<const SCEV *> Res = compareWithBackedgeCondition(I);
4556 if (Res.hasValue())
4557 Result = Res.getValue();
4558 break;
4559 }
4560 }
4561 }
4562 return Result;
4563 }
4564
4565private:
4566 explicit SCEVBackedgeConditionFolder(const Loop *L, Value *BECond,
4567 bool IsPosBECond, ScalarEvolution &SE)
4568 : SCEVRewriteVisitor(SE), L(L), BackedgeCond(BECond),
4569 IsPositiveBECond(IsPosBECond) {}
4570
4571 Optional<const SCEV *> compareWithBackedgeCondition(Value *IC);
4572
4573 const Loop *L;
4574 /// Loop back condition.
4575 Value *BackedgeCond = nullptr;
4576 /// Set to true if loop back is on positive branch condition.
4577 bool IsPositiveBECond;
4578};
4579
4580Optional<const SCEV *>
4581SCEVBackedgeConditionFolder::compareWithBackedgeCondition(Value *IC) {
4582
4583 // If value matches the backedge condition for loop latch,
4584 // then return a constant evolution node based on loopback
4585 // branch taken.
4586 if (BackedgeCond == IC)
4587 return IsPositiveBECond ? SE.getOne(Type::getInt1Ty(SE.getContext()))
4588 : SE.getZero(Type::getInt1Ty(SE.getContext()));
4589 return None;
4590}
4591
4592class SCEVShiftRewriter : public SCEVRewriteVisitor<SCEVShiftRewriter> {
4593public:
4594 static const SCEV *rewrite(const SCEV *S, const Loop *L,
4595 ScalarEvolution &SE) {
4596 SCEVShiftRewriter Rewriter(L, SE);
4597 const SCEV *Result = Rewriter.visit(S);
4598 return Rewriter.isValid() ? Result : SE.getCouldNotCompute();
4599 }
4600
4601 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4602 // Only allow AddRecExprs for this loop.
4603 if (!SE.isLoopInvariant(Expr, L))
4604 Valid = false;
4605 return Expr;
4606 }
4607
4608 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
4609 if (Expr->getLoop() == L && Expr->isAffine())
4610 return SE.getMinusSCEV(Expr, Expr->getStepRecurrence(SE));
4611 Valid = false;
4612 return Expr;
4613 }
4614
4615 bool isValid() { return Valid; }
4616
4617private:
4618 explicit SCEVShiftRewriter(const Loop *L, ScalarEvolution &SE)
4619 : SCEVRewriteVisitor(SE), L(L) {}
4620
4621 const Loop *L;
4622 bool Valid = true;
4623};
4624
4625} // end anonymous namespace
4626
4627SCEV::NoWrapFlags
4628ScalarEvolution::proveNoWrapViaConstantRanges(const SCEVAddRecExpr *AR) {
4629 if (!AR->isAffine())
4630 return SCEV::FlagAnyWrap;
4631
4632 using OBO = OverflowingBinaryOperator;
4633
4634 SCEV::NoWrapFlags Result = SCEV::FlagAnyWrap;
4635
4636 if (!AR->hasNoSignedWrap()) {
4637 ConstantRange AddRecRange = getSignedRange(AR);
4638 ConstantRange IncRange = getSignedRange(AR->getStepRecurrence(*this));
4639
4640 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
4641 Instruction::Add, IncRange, OBO::NoSignedWrap);
4642 if (NSWRegion.contains(AddRecRange))
4643 Result = ScalarEvolution::setFlags(Result, SCEV::FlagNSW);
4644 }
4645
4646 if (!AR->hasNoUnsignedWrap()) {
4647 ConstantRange AddRecRange = getUnsignedRange(AR);
4648 ConstantRange IncRange = getUnsignedRange(AR->getStepRecurrence(*this));
4649
4650 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
4651 Instruction::Add, IncRange, OBO::NoUnsignedWrap);
4652 if (NUWRegion.contains(AddRecRange))
4653 Result = ScalarEvolution::setFlags(Result, SCEV::FlagNUW);
4654 }
4655
4656 return Result;
4657}
4658
4659SCEV::NoWrapFlags
4660ScalarEvolution::proveNoSignedWrapViaInduction(const SCEVAddRecExpr *AR) {
4661 SCEV::NoWrapFlags Result = AR->getNoWrapFlags();
4662
4663 if (AR->hasNoSignedWrap())
4664 return Result;
4665
4666 if (!AR->isAffine())
4667 return Result;
4668
4669 const SCEV *Step = AR->getStepRecurrence(*this);
4670 const Loop *L = AR->getLoop();
4671
4672 // Check whether the backedge-taken count is SCEVCouldNotCompute.
4673 // Note that this serves two purposes: It filters out loops that are
4674 // simply not analyzable, and it covers the case where this code is
4675 // being called from within backedge-taken count analysis, such that
4676 // attempting to ask for the backedge-taken count would likely result
4677 // in infinite recursion. In the later case, the analysis code will
4678 // cope with a conservative value, and it will take care to purge
4679 // that value once it has finished.
4680 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L);
4681
4682 // Normally, in the cases we can prove no-overflow via a
4683 // backedge guarding condition, we can also compute a backedge
4684 // taken count for the loop. The exceptions are assumptions and
4685 // guards present in the loop -- SCEV is not great at exploiting
4686 // these to compute max backedge taken counts, but can still use
4687 // these to prove lack of overflow. Use this fact to avoid
4688 // doing extra work that may not pay off.
4689
4690 if (isa<SCEVCouldNotCompute>(MaxBECount) && !HasGuards &&
4691 AC.assumptions().empty())
4692 return Result;
4693
4694 // If the backedge is guarded by a comparison with the pre-inc value the
4695 // addrec is safe. Also, if the entry is guarded by a comparison with the
4696 // start value and the backedge is guarded by a comparison with the post-inc
4697 // value, the addrec is safe.
4698 ICmpInst::Predicate Pred;
4699 const SCEV *OverflowLimit =
4700 getSignedOverflowLimitForStep(Step, &Pred, this);
4701 if (OverflowLimit &&
4702 (isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) ||
4703 isKnownOnEveryIteration(Pred, AR, OverflowLimit))) {
4704 Result = setFlags(Result, SCEV::FlagNSW);
4705 }
4706 return Result;
4707}
4708SCEV::NoWrapFlags
4709ScalarEvolution::proveNoUnsignedWrapViaInduction(const SCEVAddRecExpr *AR) {
4710 SCEV::NoWrapFlags Result = AR->getNoWrapFlags();
4711
4712 if (AR->hasNoUnsignedWrap())
4713 return Result;
4714
4715 if (!AR->isAffine())
4716 return Result;
4717
4718 const SCEV *Step = AR->getStepRecurrence(*this);
4719 unsigned BitWidth = getTypeSizeInBits(AR->getType());
4720 const Loop *L = AR->getLoop();
4721
4722 // Check whether the backedge-taken count is SCEVCouldNotCompute.
4723 // Note that this serves two purposes: It filters out loops that are
4724 // simply not analyzable, and it covers the case where this code is
4725 // being called from within backedge-taken count analysis, such that
4726 // attempting to ask for the backedge-taken count would likely result
4727 // in infinite recursion. In the later case, the analysis code will
4728 // cope with a conservative value, and it will take care to purge
4729 // that value once it has finished.
4730 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L);
4731
4732 // Normally, in the cases we can prove no-overflow via a
4733 // backedge guarding condition, we can also compute a backedge
4734 // taken count for the loop. The exceptions are assumptions and
4735 // guards present in the loop -- SCEV is not great at exploiting
4736 // these to compute max backedge taken counts, but can still use
4737 // these to prove lack of overflow. Use this fact to avoid
4738 // doing extra work that may not pay off.
4739
4740 if (isa<SCEVCouldNotCompute>(MaxBECount) && !HasGuards &&
4741 AC.assumptions().empty())
4742 return Result;
4743
4744 // If the backedge is guarded by a comparison with the pre-inc value the
4745 // addrec is safe. Also, if the entry is guarded by a comparison with the
4746 // start value and the backedge is guarded by a comparison with the post-inc
4747 // value, the addrec is safe.
4748 if (isKnownPositive(Step)) {
4749 const SCEV *N = getConstant(APInt::getMinValue(BitWidth) -
4750 getUnsignedRangeMax(Step));
4751 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) ||
4752 isKnownOnEveryIteration(ICmpInst::ICMP_ULT, AR, N)) {
4753 Result = setFlags(Result, SCEV::FlagNUW);
4754 }
4755 }
4756
4757 return Result;
4758}
4759
4760namespace {
4761
4762/// Represents an abstract binary operation. This may exist as a
4763/// normal instruction or constant expression, or may have been
4764/// derived from an expression tree.
4765struct BinaryOp {
4766 unsigned Opcode;
4767 Value *LHS;
4768 Value *RHS;
4769 bool IsNSW = false;
4770 bool IsNUW = false;
4771
4772 /// Op is set if this BinaryOp corresponds to a concrete LLVM instruction or
4773 /// constant expression.
4774 Operator *Op = nullptr;
4775
4776 explicit BinaryOp(Operator *Op)
4777 : Opcode(Op->getOpcode()), LHS(Op->getOperand(0)), RHS(Op->getOperand(1)),
4778 Op(Op) {
4779 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Op)) {
4780 IsNSW = OBO->hasNoSignedWrap();
4781 IsNUW = OBO->hasNoUnsignedWrap();
4782 }
4783 }
4784
4785 explicit BinaryOp(unsigned Opcode, Value *LHS, Value *RHS, bool IsNSW = false,
4786 bool IsNUW = false)
4787 : Opcode(Opcode), LHS(LHS), RHS(RHS), IsNSW(IsNSW), IsNUW(IsNUW) {}
4788};
4789
4790} // end anonymous namespace
4791
4792/// Try to map \p V into a BinaryOp, and return \c None on failure.
4793static Optional<BinaryOp> MatchBinaryOp(Value *V, DominatorTree &DT) {
4794 auto *Op = dyn_cast<Operator>(V);
4795 if (!Op)
4796 return None;
4797
4798 // Implementation detail: all the cleverness here should happen without
4799 // creating new SCEV expressions -- our caller knowns tricks to avoid creating
4800 // SCEV expressions when possible, and we should not break that.
4801
4802 switch (Op->getOpcode()) {
4803 case Instruction::Add:
4804 case Instruction::Sub:
4805 case Instruction::Mul:
4806 case Instruction::UDiv:
4807 case Instruction::URem:
4808 case Instruction::And:
4809 case Instruction::Or:
4810 case Instruction::AShr:
4811 case Instruction::Shl:
4812 return BinaryOp(Op);
4813
4814 case Instruction::Xor:
4815 if (auto *RHSC = dyn_cast<ConstantInt>(Op->getOperand(1)))
4816 // If the RHS of the xor is a signmask, then this is just an add.
4817 // Instcombine turns add of signmask into xor as a strength reduction step.
4818 if (RHSC->getValue().isSignMask())
4819 return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1));
4820 return BinaryOp(Op);
4821
4822 case Instruction::LShr:
4823 // Turn logical shift right of a constant into a unsigned divide.
4824 if (ConstantInt *SA = dyn_cast<ConstantInt>(Op->getOperand(1))) {
4825 uint32_t BitWidth = cast<IntegerType>(Op->getType())->getBitWidth();
4826
4827 // If the shift count is not less than the bitwidth, the result of
4828 // the shift is undefined. Don't try to analyze it, because the
4829 // resolution chosen here may differ from the resolution chosen in
4830 // other parts of the compiler.
4831 if (SA->getValue().ult(BitWidth)) {
4832 Constant *X =
4833 ConstantInt::get(SA->getContext(),
4834 APInt::getOneBitSet(BitWidth, SA->getZExtValue()));
4835 return BinaryOp(Instruction::UDiv, Op->getOperand(0), X);
4836 }
4837 }
4838 return BinaryOp(Op);
4839
4840 case Instruction::ExtractValue: {
4841 auto *EVI = cast<ExtractValueInst>(Op);
4842 if (EVI->getNumIndices() != 1 || EVI->getIndices()[0] != 0)
4843 break;
4844
4845 auto *WO = dyn_cast<WithOverflowInst>(EVI->getAggregateOperand());
4846 if (!WO)
4847 break;
4848
4849 Instruction::BinaryOps BinOp = WO->getBinaryOp();
4850 bool Signed = WO->isSigned();
4851 // TODO: Should add nuw/nsw flags for mul as well.
4852 if (BinOp == Instruction::Mul || !isOverflowIntrinsicNoWrap(WO, DT))
4853 return BinaryOp(BinOp, WO->getLHS(), WO->getRHS());
4854
4855 // Now that we know that all uses of the arithmetic-result component of
4856 // CI are guarded by the overflow check, we can go ahead and pretend
4857 // that the arithmetic is non-overflowing.
4858 return BinaryOp(BinOp, WO->getLHS(), WO->getRHS(),
4859 /* IsNSW = */ Signed, /* IsNUW = */ !Signed);
4860 }
4861
4862 default:
4863 break;
4864 }
4865
4866 // Recognise intrinsic loop.decrement.reg, and as this has exactly the same
4867 // semantics as a Sub, return a binary sub expression.
4868 if (auto *II = dyn_cast<IntrinsicInst>(V))
4869 if (II->getIntrinsicID() == Intrinsic::loop_decrement_reg)
4870 return BinaryOp(Instruction::Sub, II->getOperand(0), II->getOperand(1));
4871
4872 return None;
4873}
4874
4875/// Helper function to createAddRecFromPHIWithCasts. We have a phi
4876/// node whose symbolic (unknown) SCEV is \p SymbolicPHI, which is updated via
4877/// the loop backedge by a SCEVAddExpr, possibly also with a few casts on the
4878/// way. This function checks if \p Op, an operand of this SCEVAddExpr,
4879/// follows one of the following patterns:
4880/// Op == (SExt ix (Trunc iy (%SymbolicPHI) to ix) to iy)
4881/// Op == (ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy)
4882/// If the SCEV expression of \p Op conforms with one of the expected patterns
4883/// we return the type of the truncation operation, and indicate whether the
4884/// truncated type should be treated as signed/unsigned by setting
4885/// \p Signed to true/false, respectively.
4886static Type *isSimpleCastedPHI(const SCEV *Op, const SCEVUnknown *SymbolicPHI,
4887 bool &Signed, ScalarEvolution &SE) {
4888 // The case where Op == SymbolicPHI (that is, with no type conversions on
4889 // the way) is handled by the regular add recurrence creating logic and
4890 // would have already been triggered in createAddRecForPHI. Reaching it here
4891 // means that createAddRecFromPHI had failed for this PHI before (e.g.,
4892 // because one of the other operands of the SCEVAddExpr updating this PHI is
4893 // not invariant).
4894 //
4895 // Here we look for the case where Op = (ext(trunc(SymbolicPHI))), and in
4896 // this case predicates that allow us to prove that Op == SymbolicPHI will
4897 // be added.
4898 if (Op == SymbolicPHI)
4899 return nullptr;
4900
4901 unsigned SourceBits = SE.getTypeSizeInBits(SymbolicPHI->getType());
4902 unsigned NewBits = SE.getTypeSizeInBits(Op->getType());
4903 if (SourceBits != NewBits)
4904 return nullptr;
4905
4906 const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(Op);
4907 const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(Op);
4908 if (!SExt && !ZExt)
4909 return nullptr;
4910 const SCEVTruncateExpr *Trunc =
4911 SExt ? dyn_cast<SCEVTruncateExpr>(SExt->getOperand())
4912 : dyn_cast<SCEVTruncateExpr>(ZExt->getOperand());
4913 if (!Trunc)
4914 return nullptr;
4915 const SCEV *X = Trunc->getOperand();
4916 if (X != SymbolicPHI)
4917 return nullptr;
4918 Signed = SExt != nullptr;
4919 return Trunc->getType();
4920}
4921
4922static const Loop *isIntegerLoopHeaderPHI(const PHINode *PN, LoopInfo &LI) {
4923 if (!PN->getType()->isIntegerTy())
4924 return nullptr;
4925 const Loop *L = LI.getLoopFor(PN->getParent());
4926 if (!L || L->getHeader() != PN->getParent())
4927 return nullptr;
4928 return L;
4929}
4930
4931// Analyze \p SymbolicPHI, a SCEV expression of a phi node, and check if the
4932// computation that updates the phi follows the following pattern:
4933// (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
4934// which correspond to a phi->trunc->sext/zext->add->phi update chain.
4935// If so, try to see if it can be rewritten as an AddRecExpr under some
4936// Predicates. If successful, return them as a pair. Also cache the results
4937// of the analysis.
4938//
4939// Example usage scenario:
4940// Say the Rewriter is called for the following SCEV:
4941// 8 * ((sext i32 (trunc i64 %X to i32) to i64) + %Step)
4942// where:
4943// %X = phi i64 (%Start, %BEValue)
4944// It will visitMul->visitAdd->visitSExt->visitTrunc->visitUnknown(%X),
4945// and call this function with %SymbolicPHI = %X.
4946//
4947// The analysis will find that the value coming around the backedge has
4948// the following SCEV:
4949// BEValue = ((sext i32 (trunc i64 %X to i32) to i64) + %Step)
4950// Upon concluding that this matches the desired pattern, the function
4951// will return the pair {NewAddRec, SmallPredsVec} where:
4952// NewAddRec = {%Start,+,%Step}
4953// SmallPredsVec = {P1, P2, P3} as follows:
4954// P1(WrapPred): AR: {trunc(%Start),+,(trunc %Step)}<nsw> Flags: <nssw>
4955// P2(EqualPred): %Start == (sext i32 (trunc i64 %Start to i32) to i64)
4956// P3(EqualPred): %Step == (sext i32 (trunc i64 %Step to i32) to i64)
4957// The returned pair means that SymbolicPHI can be rewritten into NewAddRec
4958// under the predicates {P1,P2,P3}.
4959// This predicated rewrite will be cached in PredicatedSCEVRewrites:
4960// PredicatedSCEVRewrites[{%X,L}] = {NewAddRec, {P1,P2,P3)}
4961//
4962// TODO's:
4963//
4964// 1) Extend the Induction descriptor to also support inductions that involve
4965// casts: When needed (namely, when we are called in the context of the
4966// vectorizer induction analysis), a Set of cast instructions will be
4967// populated by this method, and provided back to isInductionPHI. This is
4968// needed to allow the vectorizer to properly record them to be ignored by
4969// the cost model and to avoid vectorizing them (otherwise these casts,
4970// which are redundant under the runtime overflow checks, will be
4971// vectorized, which can be costly).
4972//
4973// 2) Support additional induction/PHISCEV patterns: We also want to support
4974// inductions where the sext-trunc / zext-trunc operations (partly) occur
4975// after the induction update operation (the induction increment):
4976//
4977// (Trunc iy (SExt/ZExt ix (%SymbolicPHI + InvariantAccum) to iy) to ix)
4978// which correspond to a phi->add->trunc->sext/zext->phi update chain.
4979//
4980// (Trunc iy ((SExt/ZExt ix (%SymbolicPhi) to iy) + InvariantAccum) to ix)
4981// which correspond to a phi->trunc->add->sext/zext->phi update chain.
4982//
4983// 3) Outline common code with createAddRecFromPHI to avoid duplication.
4984Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
4985ScalarEvolution::createAddRecFromPHIWithCastsImpl(const SCEVUnknown *SymbolicPHI) {
4986 SmallVector<const SCEVPredicate *, 3> Predicates;
4987
4988 // *** Part1: Analyze if we have a phi-with-cast pattern for which we can
4989 // return an AddRec expression under some predicate.
4990
4991 auto *PN = cast<PHINode>(SymbolicPHI->getValue());
4992 const Loop *L = isIntegerLoopHeaderPHI(PN, LI);
4993 assert(L && "Expecting an integer loop header phi")(static_cast<void> (0));
4994
4995 // The loop may have multiple entrances or multiple exits; we can analyze
4996 // this phi as an addrec if it has a unique entry value and a unique
4997 // backedge value.
4998 Value *BEValueV = nullptr, *StartValueV = nullptr;
4999 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
5000 Value *V = PN->getIncomingValue(i);
5001 if (L->contains(PN->getIncomingBlock(i))) {
5002 if (!BEValueV) {
5003 BEValueV = V;
5004 } else if (BEValueV != V) {
5005 BEValueV = nullptr;
5006 break;
5007 }
5008 } else if (!StartValueV) {
5009 StartValueV = V;
5010 } else if (StartValueV != V) {
5011 StartValueV = nullptr;
5012 break;
5013 }
5014 }
5015 if (!BEValueV || !StartValueV)
5016 return None;
5017
5018 const SCEV *BEValue = getSCEV(BEValueV);
5019
5020 // If the value coming around the backedge is an add with the symbolic
5021 // value we just inserted, possibly with casts that we can ignore under
5022 // an appropriate runtime guard, then we found a simple induction variable!
5023 const auto *Add = dyn_cast<SCEVAddExpr>(BEValue);
5024 if (!Add)
5025 return None;
5026
5027 // If there is a single occurrence of the symbolic value, possibly
5028 // casted, replace it with a recurrence.
5029 unsigned FoundIndex = Add->getNumOperands();
5030 Type *TruncTy = nullptr;
5031 bool Signed;
5032 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
5033 if ((TruncTy =
5034 isSimpleCastedPHI(Add->getOperand(i), SymbolicPHI, Signed, *this)))
5035 if (FoundIndex == e) {
5036 FoundIndex = i;
5037 break;
5038 }
5039
5040 if (FoundIndex == Add->getNumOperands())
5041 return None;
5042
5043 // Create an add with everything but the specified operand.
5044 SmallVector<const SCEV *, 8> Ops;
5045 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
5046 if (i != FoundIndex)
5047 Ops.push_back(Add->getOperand(i));
5048 const SCEV *Accum = getAddExpr(Ops);
5049
5050 // The runtime checks will not be valid if the step amount is
5051 // varying inside the loop.
5052 if (!isLoopInvariant(Accum, L))
5053 return None;
5054
5055 // *** Part2: Create the predicates
5056
5057 // Analysis was successful: we have a phi-with-cast pattern for which we
5058 // can return an AddRec expression under the following predicates:
5059 //
5060 // P1: A Wrap predicate that guarantees that Trunc(Start) + i*Trunc(Accum)
5061 // fits within the truncated type (does not overflow) for i = 0 to n-1.
5062 // P2: An Equal predicate that guarantees that
5063 // Start = (Ext ix (Trunc iy (Start) to ix) to iy)
5064 // P3: An Equal predicate that guarantees that
5065 // Accum = (Ext ix (Trunc iy (Accum) to ix) to iy)
5066 //
5067 // As we next prove, the above predicates guarantee that:
5068 // Start + i*Accum = (Ext ix (Trunc iy ( Start + i*Accum ) to ix) to iy)
5069 //
5070 //
5071 // More formally, we want to prove that:
5072 // Expr(i+1) = Start + (i+1) * Accum
5073 // = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum
5074 //
5075 // Given that:
5076 // 1) Expr(0) = Start
5077 // 2) Expr(1) = Start + Accum
5078 // = (Ext ix (Trunc iy (Start) to ix) to iy) + Accum :: from P2
5079 // 3) Induction hypothesis (step i):
5080 // Expr(i) = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum
5081 //
5082 // Proof:
5083 // Expr(i+1) =
5084 // = Start + (i+1)*Accum
5085 // = (Start + i*Accum) + Accum
5086 // = Expr(i) + Accum
5087 // = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum + Accum
5088 // :: from step i
5089 //
5090 // = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy) + Accum + Accum
5091 //
5092 // = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy)
5093 // + (Ext ix (Trunc iy (Accum) to ix) to iy)
5094 // + Accum :: from P3
5095 //
5096 // = (Ext ix (Trunc iy ((Start + (i-1)*Accum) + Accum) to ix) to iy)
5097 // + Accum :: from P1: Ext(x)+Ext(y)=>Ext(x+y)
5098 //
5099 // = (Ext ix (Trunc iy (Start + i*Accum) to ix) to iy) + Accum
5100 // = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum
5101 //
5102 // By induction, the same applies to all iterations 1<=i<n:
5103 //
5104
5105 // Create a truncated addrec for which we will add a no overflow check (P1).
5106 const SCEV *StartVal = getSCEV(StartValueV);
5107 const SCEV *PHISCEV =
5108 getAddRecExpr(getTruncateExpr(StartVal, TruncTy),
5109 getTruncateExpr(Accum, TruncTy), L, SCEV::FlagAnyWrap);
5110
5111 // PHISCEV can be either a SCEVConstant or a SCEVAddRecExpr.
5112 // ex: If truncated Accum is 0 and StartVal is a constant, then PHISCEV
5113 // will be constant.
5114 //
5115 // If PHISCEV is a constant, then P1 degenerates into P2 or P3, so we don't
5116 // add P1.
5117 if (const auto *AR = dyn_cast<SCEVAddRecExpr>(PHISCEV)) {
5118 SCEVWrapPredicate::IncrementWrapFlags AddedFlags =
5119 Signed ? SCEVWrapPredicate::IncrementNSSW
5120 : SCEVWrapPredicate::IncrementNUSW;
5121 const SCEVPredicate *AddRecPred = getWrapPredicate(AR, AddedFlags);
5122 Predicates.push_back(AddRecPred);
5123 }
5124
5125 // Create the Equal Predicates P2,P3:
5126
5127 // It is possible that the predicates P2 and/or P3 are computable at
5128 // compile time due to StartVal and/or Accum being constants.
5129 // If either one is, then we can check that now and escape if either P2
5130 // or P3 is false.
5131
5132 // Construct the extended SCEV: (Ext ix (Trunc iy (Expr) to ix) to iy)
5133 // for each of StartVal and Accum
5134 auto getExtendedExpr = [&](const SCEV *Expr,
5135 bool CreateSignExtend) -> const SCEV * {
5136 assert(isLoopInvariant(Expr, L) && "Expr is expected to be invariant")(static_cast<void> (0));
5137 const SCEV *TruncatedExpr = getTruncateExpr(Expr, TruncTy);
5138 const SCEV *ExtendedExpr =
5139 CreateSignExtend ? getSignExtendExpr(TruncatedExpr, Expr->getType())
5140 : getZeroExtendExpr(TruncatedExpr, Expr->getType());
5141 return ExtendedExpr;
5142 };
5143
5144 // Given:
5145 // ExtendedExpr = (Ext ix (Trunc iy (Expr) to ix) to iy
5146 // = getExtendedExpr(Expr)
5147 // Determine whether the predicate P: Expr == ExtendedExpr
5148 // is known to be false at compile time
5149 auto PredIsKnownFalse = [&](const SCEV *Expr,
5150 const SCEV *ExtendedExpr) -> bool {
5151 return Expr != ExtendedExpr &&
5152 isKnownPredicate(ICmpInst::ICMP_NE, Expr, ExtendedExpr);
5153 };
5154
5155 const SCEV *StartExtended = getExtendedExpr(StartVal, Signed);
5156 if (PredIsKnownFalse(StartVal, StartExtended)) {
5157 LLVM_DEBUG(dbgs() << "P2 is compile-time false\n";)do { } while (false);
5158 return None;
5159 }
5160
5161 // The Step is always Signed (because the overflow checks are either
5162 // NSSW or NUSW)
5163 const SCEV *AccumExtended = getExtendedExpr(Accum, /*CreateSignExtend=*/true);
5164 if (PredIsKnownFalse(Accum, AccumExtended)) {
5165 LLVM_DEBUG(dbgs() << "P3 is compile-time false\n";)do { } while (false);
5166 return None;
5167 }
5168
5169 auto AppendPredicate = [&](const SCEV *Expr,
5170 const SCEV *ExtendedExpr) -> void {
5171 if (Expr != ExtendedExpr &&
5172 !isKnownPredicate(ICmpInst::ICMP_EQ, Expr, ExtendedExpr)) {
5173 const SCEVPredicate *Pred = getEqualPredicate(Expr, ExtendedExpr);
5174 LLVM_DEBUG(dbgs() << "Added Predicate: " << *Pred)do { } while (false);
5175 Predicates.push_back(Pred);
5176 }
5177 };
5178
5179 AppendPredicate(StartVal, StartExtended);
5180 AppendPredicate(Accum, AccumExtended);
5181
5182 // *** Part3: Predicates are ready. Now go ahead and create the new addrec in
5183 // which the casts had been folded away. The caller can rewrite SymbolicPHI
5184 // into NewAR if it will also add the runtime overflow checks specified in
5185 // Predicates.
5186 auto *NewAR = getAddRecExpr(StartVal, Accum, L, SCEV::FlagAnyWrap);
5187
5188 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> PredRewrite =
5189 std::make_pair(NewAR, Predicates);
5190 // Remember the result of the analysis for this SCEV at this locayyytion.
5191 PredicatedSCEVRewrites[{SymbolicPHI, L}] = PredRewrite;
5192 return PredRewrite;
5193}
5194
5195Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
5196ScalarEvolution::createAddRecFromPHIWithCasts(const SCEVUnknown *SymbolicPHI) {
5197 auto *PN = cast<PHINode>(SymbolicPHI->getValue());
5198 const Loop *L = isIntegerLoopHeaderPHI(PN, LI);
5199 if (!L)
5200 return None;
5201
5202 // Check to see if we already analyzed this PHI.
5203 auto I = PredicatedSCEVRewrites.find({SymbolicPHI, L});
5204 if (I != PredicatedSCEVRewrites.end()) {
5205 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> Rewrite =
5206 I->second;
5207 // Analysis was done before and failed to create an AddRec:
5208 if (Rewrite.first == SymbolicPHI)
5209 return None;
5210 // Analysis was done before and succeeded to create an AddRec under
5211 // a predicate:
5212 assert(isa<SCEVAddRecExpr>(Rewrite.first) && "Expected an AddRec")(static_cast<void> (0));
5213 assert(!(Rewrite.second).empty() && "Expected to find Predicates")(static_cast<void> (0));
5214 return Rewrite;
5215 }
5216
5217 Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
5218 Rewrite = createAddRecFromPHIWithCastsImpl(SymbolicPHI);
5219
5220 // Record in the cache that the analysis failed
5221 if (!Rewrite) {
5222 SmallVector<const SCEVPredicate *, 3> Predicates;
5223 PredicatedSCEVRewrites[{SymbolicPHI, L}] = {SymbolicPHI, Predicates};
5224 return None;
5225 }
5226
5227 return Rewrite;
5228}
5229
5230// FIXME: This utility is currently required because the Rewriter currently
5231// does not rewrite this expression:
5232// {0, +, (sext ix (trunc iy to ix) to iy)}
5233// into {0, +, %step},
5234// even when the following Equal predicate exists:
5235// "%step == (sext ix (trunc iy to ix) to iy)".
5236bool PredicatedScalarEvolution::areAddRecsEqualWithPreds(
5237 const SCEVAddRecExpr *AR1, const SCEVAddRecExpr *AR2) const {
5238 if (AR1 == AR2)
5239 return true;
5240
5241 auto areExprsEqual = [&](const SCEV *Expr1, const SCEV *Expr2) -> bool {
5242 if (Expr1 != Expr2 && !Preds.implies(SE.getEqualPredicate(Expr1, Expr2)) &&
5243 !Preds.implies(SE.getEqualPredicate(Expr2, Expr1)))
5244 return false;
5245 return true;
5246 };
5247
5248 if (!areExprsEqual(AR1->getStart(), AR2->getStart()) ||
5249 !areExprsEqual(AR1->getStepRecurrence(SE), AR2->getStepRecurrence(SE)))
5250 return false;
5251 return true;
5252}
5253
5254/// A helper function for createAddRecFromPHI to handle simple cases.
5255///
5256/// This function tries to find an AddRec expression for the simplest (yet most
5257/// common) cases: PN = PHI(Start, OP(Self, LoopInvariant)).
5258/// If it fails, createAddRecFromPHI will use a more general, but slow,
5259/// technique for finding the AddRec expression.
5260const SCEV *ScalarEvolution::createSimpleAffineAddRec(PHINode *PN,
5261 Value *BEValueV,
5262 Value *StartValueV) {
5263 const Loop *L = LI.getLoopFor(PN->getParent());
5264 assert(L && L->getHeader() == PN->getParent())(static_cast<void> (0));
5265 assert(BEValueV && StartValueV)(static_cast<void> (0));
5266
5267 auto BO = MatchBinaryOp(BEValueV, DT);
5268 if (!BO)
5269 return nullptr;
5270
5271 if (BO->Opcode != Instruction::Add)
5272 return nullptr;
5273
5274 const SCEV *Accum = nullptr;
5275 if (BO->LHS == PN && L->isLoopInvariant(BO->RHS))
5276 Accum = getSCEV(BO->RHS);
5277 else if (BO->RHS == PN && L->isLoopInvariant(BO->LHS))
5278 Accum = getSCEV(BO->LHS);
5279
5280 if (!Accum)
5281 return nullptr;
5282
5283 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
5284 if (BO->IsNUW)
5285 Flags = setFlags(Flags, SCEV::FlagNUW);
5286 if (BO->IsNSW)
5287 Flags = setFlags(Flags, SCEV::FlagNSW);
5288
5289 const SCEV *StartVal = getSCEV(StartValueV);
5290 const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags);
5291
5292 ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV;
5293
5294 // We can add Flags to the post-inc expression only if we
5295 // know that it is *undefined behavior* for BEValueV to
5296 // overflow.
5297 if (auto *BEInst = dyn_cast<Instruction>(BEValueV))
5298 if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L))
5299 (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
5300
5301 return PHISCEV;
5302}
5303
5304const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) {
5305 const Loop *L = LI.getLoopFor(PN->getParent());
5306 if (!L || L->getHeader() != PN->getParent())
5307 return nullptr;
5308
5309 // The loop may have multiple entrances or multiple exits; we can analyze
5310 // this phi as an addrec if it has a unique entry value and a unique
5311 // backedge value.
5312 Value *BEValueV = nullptr, *StartValueV = nullptr;
5313 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
5314 Value *V = PN->getIncomingValue(i);
5315 if (L->contains(PN->getIncomingBlock(i))) {
5316 if (!BEValueV) {
5317 BEValueV = V;
5318 } else if (BEValueV != V) {
5319 BEValueV = nullptr;
5320 break;
5321 }
5322 } else if (!StartValueV) {
5323 StartValueV = V;
5324 } else if (StartValueV != V) {
5325 StartValueV = nullptr;
5326 break;
5327 }
5328 }
5329 if (!BEValueV || !StartValueV)
5330 return nullptr;
5331
5332 assert(ValueExprMap.find_as(PN) == ValueExprMap.end() &&(static_cast<void> (0))
5333 "PHI node already processed?")(static_cast<void> (0));
5334
5335 // First, try to find AddRec expression without creating a fictituos symbolic
5336 // value for PN.
5337 if (auto *S = createSimpleAffineAddRec(PN, BEValueV, StartValueV))
5338 return S;
5339
5340 // Handle PHI node value symbolically.
5341 const SCEV *SymbolicName = getUnknown(PN);
5342 ValueExprMap.insert({SCEVCallbackVH(PN, this), SymbolicName});
5343
5344 // Using this symbolic name for the PHI, analyze the value coming around
5345 // the back-edge.
5346 const SCEV *BEValue = getSCEV(BEValueV);
5347
5348 // NOTE: If BEValue is loop invariant, we know that the PHI node just
5349 // has a special value for the first iteration of the loop.
5350
5351 // If the value coming around the backedge is an add with the symbolic
5352 // value we just inserted, then we found a simple induction variable!
5353 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
5354 // If there is a single occurrence of the symbolic value, replace it
5355 // with a recurrence.
5356 unsigned FoundIndex = Add->getNumOperands();
5357 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
5358 if (Add->getOperand(i) == SymbolicName)
5359 if (FoundIndex == e) {
5360 FoundIndex = i;
5361 break;
5362 }
5363
5364 if (FoundIndex != Add->getNumOperands()) {
5365 // Create an add with everything but the specified operand.
5366 SmallVector<const SCEV *, 8> Ops;
5367 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
5368 if (i != FoundIndex)
5369 Ops.push_back(SCEVBackedgeConditionFolder::rewrite(Add->getOperand(i),
5370 L, *this));
5371 const SCEV *Accum = getAddExpr(Ops);
5372
5373 // This is not a valid addrec if the step amount is varying each
5374 // loop iteration, but is not itself an addrec in this loop.
5375 if (isLoopInvariant(Accum, L) ||
5376 (isa<SCEVAddRecExpr>(Accum) &&
5377 cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
5378 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
5379
5380 if (auto BO = MatchBinaryOp(BEValueV, DT)) {
5381 if (BO->Opcode == Instruction::Add && BO->LHS == PN) {
5382 if (BO->IsNUW)
5383 Flags = setFlags(Flags, SCEV::FlagNUW);
5384 if (BO->IsNSW)
5385 Flags = setFlags(Flags, SCEV::FlagNSW);
5386 }
5387 } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(BEValueV)) {
5388 // If the increment is an inbounds GEP, then we know the address
5389 // space cannot be wrapped around. We cannot make any guarantee
5390 // about signed or unsigned overflow because pointers are
5391 // unsigned but we may have a negative index from the base
5392 // pointer. We can guarantee that no unsigned wrap occurs if the
5393 // indices form a positive value.
5394 if (GEP->isInBounds() && GEP->getOperand(0) == PN) {
5395 Flags = setFlags(Flags, SCEV::FlagNW);
5396
5397 const SCEV *Ptr = getSCEV(GEP->getPointerOperand());
5398 if (isKnownPositive(getMinusSCEV(getSCEV(GEP), Ptr)))
5399 Flags = setFlags(Flags, SCEV::FlagNUW);
5400 }
5401
5402 // We cannot transfer nuw and nsw flags from subtraction
5403 // operations -- sub nuw X, Y is not the same as add nuw X, -Y
5404 // for instance.
5405 }
5406
5407 const SCEV *StartVal = getSCEV(StartValueV);
5408 const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags);
5409
5410 // Okay, for the entire analysis of this edge we assumed the PHI
5411 // to be symbolic. We now need to go back and purge all of the
5412 // entries for the scalars that use the symbolic expression.
5413 forgetSymbolicName(PN, SymbolicName);
5414 ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV;
5415
5416 // We can add Flags to the post-inc expression only if we
5417 // know that it is *undefined behavior* for BEValueV to
5418 // overflow.
5419 if (auto *BEInst = dyn_cast<Instruction>(BEValueV))
5420 if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L))
5421 (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
5422
5423 return PHISCEV;
5424 }
5425 }
5426 } else {
5427 // Otherwise, this could be a loop like this:
5428 // i = 0; for (j = 1; ..; ++j) { .... i = j; }
5429 // In this case, j = {1,+,1} and BEValue is j.
5430 // Because the other in-value of i (0) fits the evolution of BEValue
5431 // i really is an addrec evolution.
5432 //
5433 // We can generalize this saying that i is the shifted value of BEValue
5434 // by one iteration:
5435 // PHI(f(0), f({1,+,1})) --> f({0,+,1})
5436 const SCEV *Shifted = SCEVShiftRewriter::rewrite(BEValue, L, *this);
5437 const SCEV *Start = SCEVInitRewriter::rewrite(Shifted, L, *this, false);
5438 if (Shifted != getCouldNotCompute() &&
5439 Start != getCouldNotCompute()) {
5440 const SCEV *StartVal = getSCEV(StartValueV);
5441 if (Start == StartVal) {
5442 // Okay, for the entire analysis of this edge we assumed the PHI
5443 // to be symbolic. We now need to go back and purge all of the
5444 // entries for the scalars that use the symbolic expression.
5445 forgetSymbolicName(PN, SymbolicName);
5446 ValueExprMap[SCEVCallbackVH(PN, this)] = Shifted;
5447 return Shifted;
5448 }
5449 }
5450 }
5451
5452 // Remove the temporary PHI node SCEV that has been inserted while intending
5453 // to create an AddRecExpr for this PHI node. We can not keep this temporary
5454 // as it will prevent later (possibly simpler) SCEV expressions to be added
5455 // to the ValueExprMap.
5456 eraseValueFromMap(PN);
5457
5458 return nullptr;
5459}
5460
5461// Checks if the SCEV S is available at BB. S is considered available at BB
5462// if S can be materialized at BB without introducing a fault.
5463static bool IsAvailableOnEntry(const Loop *L, DominatorTree &DT, const SCEV *S,
5464 BasicBlock *BB) {
5465 struct CheckAvailable {
5466 bool TraversalDone = false;
5467 bool Available = true;
5468
5469 const Loop *L = nullptr; // The loop BB is in (can be nullptr)
5470 BasicBlock *BB = nullptr;
5471 DominatorTree &DT;
5472
5473 CheckAvailable(const Loop *L, BasicBlock *BB, DominatorTree &DT)
5474 : L(L), BB(BB), DT(DT) {}
5475
5476 bool setUnavailable() {
5477 TraversalDone = true;
5478 Available = false;
5479 return false;
5480 }
5481
5482 bool follow(const SCEV *S) {
5483 switch (S->getSCEVType()) {
5484 case scConstant:
5485 case scPtrToInt:
5486 case scTruncate:
5487 case scZeroExtend:
5488 case scSignExtend:
5489 case scAddExpr:
5490 case scMulExpr:
5491 case scUMaxExpr:
5492 case scSMaxExpr:
5493 case scUMinExpr:
5494 case scSMinExpr:
5495 // These expressions are available if their operand(s) is/are.
5496 return true;
5497
5498 case scAddRecExpr: {
5499 // We allow add recurrences that are on the loop BB is in, or some
5500 // outer loop. This guarantees availability because the value of the
5501 // add recurrence at BB is simply the "current" value of the induction
5502 // variable. We can relax this in the future; for instance an add
5503 // recurrence on a sibling dominating loop is also available at BB.
5504 const auto *ARLoop = cast<SCEVAddRecExpr>(S)->getLoop();
5505 if (L && (ARLoop == L || ARLoop->contains(L)))
5506 return true;
5507
5508 return setUnavailable();
5509 }
5510
5511 case scUnknown: {
5512 // For SCEVUnknown, we check for simple dominance.
5513 const auto *SU = cast<SCEVUnknown>(S);
5514 Value *V = SU->getValue();
5515
5516 if (isa<Argument>(V))
5517 return false;
5518
5519 if (isa<Instruction>(V) && DT.dominates(cast<Instruction>(V), BB))
5520 return false;
5521
5522 return setUnavailable();
5523 }
5524
5525 case scUDivExpr:
5526 case scCouldNotCompute:
5527 // We do not try to smart about these at all.
5528 return setUnavailable();
5529 }
5530 llvm_unreachable("Unknown SCEV kind!")__builtin_unreachable();
5531 }
5532
5533 bool isDone() { return TraversalDone; }
5534 };
5535
5536 CheckAvailable CA(L, BB, DT);
5537 SCEVTraversal<CheckAvailable> ST(CA);
5538
5539 ST.visitAll(S);
5540 return CA.Available;
5541}
5542
5543// Try to match a control flow sequence that branches out at BI and merges back
5544// at Merge into a "C ? LHS : RHS" select pattern. Return true on a successful
5545// match.
5546static bool BrPHIToSelect(DominatorTree &DT, BranchInst *BI, PHINode *Merge,
5547 Value *&C, Value *&LHS, Value *&RHS) {
5548 C = BI->getCondition();
5549
5550 BasicBlockEdge LeftEdge(BI->getParent(), BI->getSuccessor(0));
5551 BasicBlockEdge RightEdge(BI->getParent(), BI->getSuccessor(1));
5552
5553 if (!LeftEdge.isSingleEdge())
5554 return false;
5555
5556 assert(RightEdge.isSingleEdge() && "Follows from LeftEdge.isSingleEdge()")(static_cast<void> (0));
5557
5558 Use &LeftUse = Merge->getOperandUse(0);
5559 Use &RightUse = Merge->getOperandUse(1);
5560
5561 if (DT.dominates(LeftEdge, LeftUse) && DT.dominates(RightEdge, RightUse)) {
5562 LHS = LeftUse;
5563 RHS = RightUse;
5564 return true;
5565 }
5566
5567 if (DT.dominates(LeftEdge, RightUse) && DT.dominates(RightEdge, LeftUse)) {
5568 LHS = RightUse;
5569 RHS = LeftUse;
5570 return true;
5571 }
5572
5573 return false;
5574}
5575
5576const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) {
5577 auto IsReachable =
5578 [&](BasicBlock *BB) { return DT.isReachableFromEntry(BB); };
5579 if (PN->getNumIncomingValues() == 2 && all_of(PN->blocks(), IsReachable)) {
5580 const Loop *L = LI.getLoopFor(PN->getParent());
5581
5582 // We don't want to break LCSSA, even in a SCEV expression tree.
5583 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
5584 if (LI.getLoopFor(PN->getIncomingBlock(i)) != L)
5585 return nullptr;
5586
5587 // Try to match
5588 //
5589 // br %cond, label %left, label %right
5590 // left:
5591 // br label %merge
5592 // right:
5593 // br label %merge
5594 // merge:
5595 // V = phi [ %x, %left ], [ %y, %right ]
5596 //
5597 // as "select %cond, %x, %y"
5598
5599 BasicBlock *IDom = DT[PN->getParent()]->getIDom()->getBlock();
5600 assert(IDom && "At least the entry block should dominate PN")(static_cast<void> (0));
5601
5602 auto *BI = dyn_cast<BranchInst>(IDom->getTerminator());
5603 Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr;
5604
5605 if (BI && BI->isConditional() &&
5606 BrPHIToSelect(DT, BI, PN, Cond, LHS, RHS) &&
5607 IsAvailableOnEntry(L, DT, getSCEV(LHS), PN->getParent()) &&
5608 IsAvailableOnEntry(L, DT, getSCEV(RHS), PN->getParent()))
5609 return createNodeForSelectOrPHI(PN, Cond, LHS, RHS);
5610 }
5611
5612 return nullptr;
5613}
5614
5615const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
5616 if (const SCEV *S = createAddRecFromPHI(PN))
5617 return S;
5618
5619 if (const SCEV *S = createNodeFromSelectLikePHI(PN))
5620 return S;
5621
5622 // If the PHI has a single incoming value, follow that value, unless the
5623 // PHI's incoming blocks are in a different loop, in which case doing so
5624 // risks breaking LCSSA form. Instcombine would normally zap these, but
5625 // it doesn't have DominatorTree information, so it may miss cases.
5626 if (Value *V = SimplifyInstruction(PN, {getDataLayout(), &TLI, &DT, &AC}))
5627 if (LI.replacementPreservesLCSSAForm(PN, V))
5628 return getSCEV(V);
5629
5630 // If it's not a loop phi, we can't handle it yet.
5631 return getUnknown(PN);
5632}
5633
5634const SCEV *ScalarEvolution::createNodeForSelectOrPHI(Instruction *I,
5635 Value *Cond,
5636 Value *TrueVal,
5637 Value *FalseVal) {
5638 // Handle "constant" branch or select. This can occur for instance when a
5639 // loop pass transforms an inner loop and moves on to process the outer loop.
5640 if (auto *CI = dyn_cast<ConstantInt>(Cond))
5641 return getSCEV(CI->isOne() ? TrueVal : FalseVal);
5642
5643 // Try to match some simple smax or umax patterns.
5644 auto *ICI = dyn_cast<ICmpInst>(Cond);
5645 if (!ICI)
5646 return getUnknown(I);
5647
5648 Value *LHS = ICI->getOperand(0);
5649 Value *RHS = ICI->getOperand(1);
5650
5651 switch (ICI->getPredicate()) {
5652 case ICmpInst::ICMP_SLT:
5653 case ICmpInst::ICMP_SLE:
5654 case ICmpInst::ICMP_ULT:
5655 case ICmpInst::ICMP_ULE:
5656 std::swap(LHS, RHS);
5657 LLVM_FALLTHROUGH[[gnu::fallthrough]];
5658 case ICmpInst::ICMP_SGT:
5659 case ICmpInst::ICMP_SGE:
5660 case ICmpInst::ICMP_UGT:
5661 case ICmpInst::ICMP_UGE:
5662 // a > b ? a+x : b+x -> max(a, b)+x
5663 // a > b ? b+x : a+x -> min(a, b)+x
5664 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType())) {
5665 bool Signed = ICI->isSigned();
5666 const SCEV *LA = getSCEV(TrueVal);
5667 const SCEV *RA = getSCEV(FalseVal);
5668 const SCEV *LS = getSCEV(LHS);
5669 const SCEV *RS = getSCEV(RHS);
5670 if (LA->getType()->isPointerTy()) {
5671 // FIXME: Handle cases where LS/RS are pointers not equal to LA/RA.
5672 // Need to make sure we can't produce weird expressions involving
5673 // negated pointers.
5674 if (LA == LS && RA == RS)
5675 return Signed ? getSMaxExpr(LS, RS) : getUMaxExpr(LS, RS);
5676 if (LA == RS && RA == LS)
5677 return Signed ? getSMinExpr(LS, RS) : getUMinExpr(LS, RS);
5678 }
5679 auto CoerceOperand = [&](const SCEV *Op) -> const SCEV * {
5680 if (Op->getType()->isPointerTy()) {
5681 Op = getLosslessPtrToIntExpr(Op);
5682 if (isa<SCEVCouldNotCompute>(Op))
5683 return Op;
5684 }
5685 if (Signed)
5686 Op = getNoopOrSignExtend(Op, I->getType());
5687 else
5688 Op = getNoopOrZeroExtend(Op, I->getType());
5689 return Op;
5690 };
5691 LS = CoerceOperand(LS);
5692 RS = CoerceOperand(RS);
5693 if (isa<SCEVCouldNotCompute>(LS) || isa<SCEVCouldNotCompute>(RS))
5694 break;
5695 const SCEV *LDiff = getMinusSCEV(LA, LS);
5696 const SCEV *RDiff = getMinusSCEV(RA, RS);
5697 if (LDiff == RDiff)
5698 return getAddExpr(Signed ? getSMaxExpr(LS, RS) : getUMaxExpr(LS, RS),
5699 LDiff);
5700 LDiff = getMinusSCEV(LA, RS);
5701 RDiff = getMinusSCEV(RA, LS);
5702 if (LDiff == RDiff)
5703 return getAddExpr(Signed ? getSMinExpr(LS, RS) : getUMinExpr(LS, RS),
5704 LDiff);
5705 }
5706 break;
5707 case ICmpInst::ICMP_NE:
5708 // n != 0 ? n+x : 1+x -> umax(n, 1)+x
5709 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType()) &&
5710 isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero()) {
5711 const SCEV *One = getOne(I->getType());
5712 const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType());
5713 const SCEV *LA = getSCEV(TrueVal);
5714 const SCEV *RA = getSCEV(FalseVal);
5715 const SCEV *LDiff = getMinusSCEV(LA, LS);
5716 const SCEV *RDiff = getMinusSCEV(RA, One);
5717 if (LDiff == RDiff)
5718 return getAddExpr(getUMaxExpr(One, LS), LDiff);
5719 }
5720 break;
5721 case ICmpInst::ICMP_EQ:
5722 // n == 0 ? 1+x : n+x -> umax(n, 1)+x
5723 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType()) &&
5724 isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero()) {
5725 const SCEV *One = getOne(I->getType());
5726 const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType());
5727 const SCEV *LA = getSCEV(TrueVal);
5728 const SCEV *RA = getSCEV(FalseVal);
5729 const SCEV *LDiff = getMinusSCEV(LA, One);
5730 const SCEV *RDiff = getMinusSCEV(RA, LS);
5731 if (LDiff == RDiff)
5732 return getAddExpr(getUMaxExpr(One, LS), LDiff);
5733 }
5734 break;
5735 default:
5736 break;
5737 }
5738
5739 return getUnknown(I);
5740}
5741
5742/// Expand GEP instructions into add and multiply operations. This allows them
5743/// to be analyzed by regular SCEV code.
5744const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
5745 // Don't attempt to analyze GEPs over unsized objects.
5746 if (!GEP->getSourceElementType()->isSized())
5747 return getUnknown(GEP);
5748
5749 SmallVector<const SCEV *, 4> IndexExprs;
5750 for (Value *Index : GEP->indices())
5751 IndexExprs.push_back(getSCEV(Index));
5752 return getGEPExpr(GEP, IndexExprs);
5753}
5754
5755uint32_t ScalarEvolution::GetMinTrailingZerosImpl(const SCEV *S) {
5756 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
5757 return C->getAPInt().countTrailingZeros();
5758
5759 if (const SCEVPtrToIntExpr *I = dyn_cast<SCEVPtrToIntExpr>(S))
5760 return GetMinTrailingZeros(I->getOperand());
5761
5762 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S))
5763 return std::min(GetMinTrailingZeros(T->getOperand()),
5764 (uint32_t)getTypeSizeInBits(T->getType()));
5765
5766 if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) {
5767 uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
5768 return OpRes == getTypeSizeInBits(E->getOperand()->getType())
5769 ? getTypeSizeInBits(E->getType())
5770 : OpRes;
5771 }
5772
5773 if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) {
5774 uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
5775 return OpRes == getTypeSizeInBits(E->getOperand()->getType())
5776 ? getTypeSizeInBits(E->getType())
5777 : OpRes;
5778 }
5779
5780 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
5781 // The result is the min of all operands results.
5782 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
5783 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
5784 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
5785 return MinOpRes;
5786 }
5787
5788 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
5789 // The result is the sum of all operands results.
5790 uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0));
5791 uint32_t BitWidth = getTypeSizeInBits(M->getType());
5792 for (unsigned i = 1, e = M->getNumOperands();
5793 SumOpRes != BitWidth && i != e; ++i)
5794 SumOpRes =
5795 std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), BitWidth);
5796 return SumOpRes;
5797 }
5798
5799 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
5800 // The result is the min of all operands results.
5801 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
5802 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
5803 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
5804 return MinOpRes;
5805 }
5806
5807 if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) {
5808 // The result is the min of all operands results.
5809 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
5810 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
5811 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
5812 return MinOpRes;
5813 }
5814
5815 if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) {
5816 // The result is the min of all operands results.
5817 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
5818 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
5819 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
5820 return MinOpRes;
5821 }
5822
5823 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
5824 // For a SCEVUnknown, ask ValueTracking.
5825 KnownBits Known = computeKnownBits(U->getValue(), getDataLayout(), 0, &AC, nullptr, &DT);
5826 return Known.countMinTrailingZeros();
5827 }
5828
5829 // SCEVUDivExpr
5830 return 0;
5831}
5832
5833uint32_t ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
5834 auto I = MinTrailingZerosCache.find(S);
5835 if (I != MinTrailingZerosCache.end())
5836 return I->second;
5837
5838 uint32_t Result = GetMinTrailingZerosImpl(S);
5839 auto InsertPair = MinTrailingZerosCache.insert({S, Result});
5840 assert(InsertPair.second && "Should insert a new key")(static_cast<void> (0));
5841 return InsertPair.first->second;
5842}
5843