LLVM 24.0.0git
ScalarEvolutionExpressions.h
Go to the documentation of this file.
1//===- llvm/Analysis/ScalarEvolutionExpressions.h - SCEV Exprs --*- C++ -*-===//
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 defines the classes used to represent and build scalar expressions.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H
14#define LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H
15
16#include "llvm/ADT/DenseMap.h"
20#include "llvm/IR/Constants.h"
21#include "llvm/IR/ValueHandle.h"
25#include <cassert>
26#include <cstddef>
27
28namespace llvm {
29
30class APInt;
31class Constant;
32class ConstantInt;
33class ConstantRange;
34class Loop;
35class Type;
36class Value;
37
59
60/// This class represents a constant integer value.
61class SCEVConstant : public SCEV {
62 friend class ScalarEvolution;
63
64 ConstantInt *V;
65
66 SCEVConstant(const FoldingSetNodeIDRef ID, ConstantInt *v)
67 : SCEV(ID, scConstant, 1, v->getType()), V(v) {}
68
69public:
70 ConstantInt *getValue() const { return V; }
71 const APInt &getAPInt() const { return getValue()->getValue(); }
72
73 /// Methods for support type inquiry through isa, cast, and dyn_cast:
74 static bool classof(const SCEV *S) { return S->getSCEVType() == scConstant; }
75};
76
77/// This class represents the value of vscale, as used when defining the length
78/// of a scalable vector or returned by the llvm.vscale() intrinsic.
79class SCEVVScale : public SCEV {
80 friend class ScalarEvolution;
81
82 SCEVVScale(const FoldingSetNodeIDRef ID, Type *ty)
83 : SCEV(ID, scVScale, 0, ty) {}
84
85public:
86 /// Methods for support type inquiry through isa, cast, and dyn_cast:
87 static bool classof(const SCEV *S) { return S->getSCEVType() == scVScale; }
88};
89
90inline unsigned short computeExpressionSize(ArrayRef<SCEVUse> Args) {
91 APInt Size(16, 1);
92 for (const SCEV *Arg : Args)
93 Size = Size.uadd_sat(APInt(16, Arg->getExpressionSize()));
94 return (unsigned short)Size.getZExtValue();
95}
96
97/// This is the base class for unary cast operator classes.
98class SCEVCastExpr : public SCEV {
99protected:
101
103 SCEVUse op, Type *ty);
104
105public:
106 SCEVUse getOperand() const { return Op; }
107 SCEVUse getOperand(unsigned i) const {
108 assert(i == 0 && "Operand index out of range!");
109 return Op;
110 }
111 ArrayRef<SCEVUse> operands() const { return Op; }
112 size_t getNumOperands() const { return 1; }
113
114 /// Methods for support type inquiry through isa, cast, and dyn_cast:
115 static bool classof(const SCEV *S) {
116 return S->getSCEVType() == scPtrToAddr || S->getSCEVType() == scTruncate ||
118 }
119};
120
121/// This class represents a cast from a pointer to a pointer-sized integer
122/// value, without capturing the provenance of the pointer.
123class SCEVPtrToAddrExpr : public SCEVCastExpr {
124 friend class ScalarEvolution;
125
126 SCEVPtrToAddrExpr(const FoldingSetNodeIDRef ID, const SCEV *Op, Type *ITy);
127
128public:
129 /// Methods for support type inquiry through isa, cast, and dyn_cast:
130 static bool classof(const SCEV *S) { return S->getSCEVType() == scPtrToAddr; }
131};
132
133/// This is the base class for unary integral cast operator classes.
135protected:
137 SCEVUse op, Type *ty);
138
139public:
140 /// Methods for support type inquiry through isa, cast, and dyn_cast:
141 static bool classof(const SCEV *S) {
142 return S->getSCEVType() == scTruncate || S->getSCEVType() == scZeroExtend ||
144 }
145};
146
147/// This class represents a truncation of an integer value to a
148/// smaller integer value.
149class SCEVTruncateExpr : public SCEVIntegralCastExpr {
150 friend class ScalarEvolution;
151
152 SCEVTruncateExpr(const FoldingSetNodeIDRef ID, SCEVUse op, Type *ty);
153
154public:
155 /// Methods for support type inquiry through isa, cast, and dyn_cast:
156 static bool classof(const SCEV *S) { return S->getSCEVType() == scTruncate; }
157};
158
159/// This class represents a zero extension of a small integer value
160/// to a larger integer value.
161class SCEVZeroExtendExpr : public SCEVIntegralCastExpr {
162 friend class ScalarEvolution;
163
164 SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID, SCEVUse op, Type *ty);
165
166public:
167 /// Methods for support type inquiry through isa, cast, and dyn_cast:
168 static bool classof(const SCEV *S) {
169 return S->getSCEVType() == scZeroExtend;
170 }
171};
172
173/// This class represents a sign extension of a small integer value
174/// to a larger integer value.
175class SCEVSignExtendExpr : public SCEVIntegralCastExpr {
176 friend class ScalarEvolution;
177
178 SCEVSignExtendExpr(const FoldingSetNodeIDRef ID, SCEVUse op, Type *ty);
179
180public:
181 /// Methods for support type inquiry through isa, cast, and dyn_cast:
182 static bool classof(const SCEV *S) {
183 return S->getSCEVType() == scSignExtend;
184 }
185};
186
187/// This node is a base class providing common functionality for
188/// n'ary operators.
189class SCEVNAryExpr : public SCEV {
190protected:
191 // Since SCEVs are immutable, ScalarEvolution allocates operand
192 // arrays with its SCEVAllocator, so this class just needs a simple
193 // pointer rather than a more elaborate vector-like data structure.
194 // This also avoids the need for a non-trivial destructor.
197
199 size_t N, Type *Ty)
200 : SCEV(ID, T, computeExpressionSize(ArrayRef(O, N)), Ty), Operands(O),
201 NumOperands(N) {}
202
203public:
204 size_t getNumOperands() const { return NumOperands; }
205
206 SCEVUse getOperand(unsigned i) const {
207 assert(i < NumOperands && "Operand index out of range!");
208 return Operands[i];
209 }
210
212
214 return static_cast<SCEVFlags>(SubclassData) & Mask & SCEV::FlagsNoWrapMask;
215 }
216
218
219 bool hasNoSignedWrap() const { return getNoWrapFlags(FlagNSW) != FlagNone; }
220
221 bool hasNoSelfWrap() const { return getNoWrapFlags(FlagNW) != FlagNone; }
222
223 /// Methods for support type inquiry through isa, cast, and dyn_cast:
224 static bool classof(const SCEV *S) {
225 return S->getSCEVType() == scAddExpr || S->getSCEVType() == scMulExpr ||
226 S->getSCEVType() == scSMaxExpr || S->getSCEVType() == scUMaxExpr ||
227 S->getSCEVType() == scSMinExpr || S->getSCEVType() == scUMinExpr ||
230 }
231};
232
233/// This node is the base class for n'ary commutative operators.
235protected:
237 const SCEVUse *O, size_t N, Type *Ty)
238 : SCEVNAryExpr(ID, T, O, N, Ty) {}
239
240public:
241 /// Methods for support type inquiry through isa, cast, and dyn_cast:
242 static bool classof(const SCEV *S) {
243 return S->getSCEVType() == scAddExpr || S->getSCEVType() == scMulExpr ||
244 S->getSCEVType() == scSMaxExpr || S->getSCEVType() == scUMaxExpr ||
245 S->getSCEVType() == scSMinExpr || S->getSCEVType() == scUMinExpr;
246 }
247
248 /// Set flags for a non-recurrence without clearing previously set flags.
249 void setFlags(SCEVFlags Flags) {
250 SubclassData |= static_cast<unsigned short>(Flags);
251 }
254 }
255};
256
257/// This node represents an addition of some number of SCEVs.
258class SCEVAddExpr : public SCEVCommutativeExpr {
259 friend class ScalarEvolution;
260
261 /// The type of an add is the type of its first pointer-typed operand, if
262 /// any, otherwise the type of operand 0.
263 static Type *computeType(const SCEVUse *O, size_t N) {
265 auto *FirstPointerTypedOp =
266 find_if(Ops, [](SCEVUse Op) { return Op->getType()->isPointerTy(); });
267 if (FirstPointerTypedOp != Ops.end())
268 return (*FirstPointerTypedOp)->getType();
269 return Ops[0]->getType();
270 }
271
272 SCEVAddExpr(const FoldingSetNodeIDRef ID, const SCEVUse *O, size_t N)
273 : SCEVCommutativeExpr(ID, scAddExpr, O, N, computeType(O, N)) {}
274
275public:
276 /// Methods for support type inquiry through isa, cast, and dyn_cast:
277 static bool classof(const SCEV *S) { return S->getSCEVType() == scAddExpr; }
278};
279
280/// This node represents multiplication of some number of SCEVs.
281class SCEVMulExpr : public SCEVCommutativeExpr {
282 friend class ScalarEvolution;
283
284 SCEVMulExpr(const FoldingSetNodeIDRef ID, const SCEVUse *O, size_t N)
285 : SCEVCommutativeExpr(ID, scMulExpr, O, N, O[0]->getType()) {}
286
287public:
288 /// Methods for support type inquiry through isa, cast, and dyn_cast:
289 static bool classof(const SCEV *S) { return S->getSCEVType() == scMulExpr; }
290};
291
292/// This class represents a binary unsigned division operation.
293class SCEVUDivExpr : public SCEV {
294 friend class ScalarEvolution;
295
296 std::array<SCEVUse, 2> Operands;
297
298 SCEVUDivExpr(const FoldingSetNodeIDRef ID, SCEVUse lhs, SCEVUse rhs)
299 : SCEV(ID, scUDivExpr, computeExpressionSize({lhs, rhs}),
300 lhs->getType()) {
301 Operands[0] = lhs;
302 Operands[1] = rhs;
303 }
304
305public:
306 SCEVUse getLHS() const { return Operands[0]; }
307 SCEVUse getRHS() const { return Operands[1]; }
308 size_t getNumOperands() const { return 2; }
309 SCEVUse getOperand(unsigned i) const {
310 assert((i == 0 || i == 1) && "Operand index out of range!");
311 return i == 0 ? getLHS() : getRHS();
312 }
313
314 ArrayRef<SCEVUse> operands() const { return Operands; }
315
316 /// Methods for support type inquiry through isa, cast, and dyn_cast:
317 static bool classof(const SCEV *S) { return S->getSCEVType() == scUDivExpr; }
318};
319
320/// This node represents a polynomial recurrence on the trip count
321/// of the specified loop. This is the primary focus of the
322/// ScalarEvolution framework; all the other SCEV subclasses are
323/// mostly just supporting infrastructure to allow SCEVAddRecExpr
324/// expressions to be created and analyzed.
325///
326/// All operands of an AddRec are required to be loop invariant.
327///
328class SCEVAddRecExpr : public SCEVNAryExpr {
329 friend class ScalarEvolution;
330
331 const Loop *L;
332
333 SCEVAddRecExpr(const FoldingSetNodeIDRef ID, const SCEVUse *O, size_t N,
334 const Loop *l)
335 : SCEVNAryExpr(ID, scAddRecExpr, O, N, O[0]->getType()), L(l) {}
336
337public:
338 SCEVUse getStart() const { return Operands[0]; }
339 const Loop *getLoop() const { return L; }
340
341 /// Constructs and returns the recurrence indicating how much this
342 /// expression steps by. If this is a polynomial of degree N, it
343 /// returns a chrec of degree N-1. We cannot determine whether
344 /// the step recurrence has self-wraparound.
346 if (isAffine())
347 return getOperand(1);
348 return SE.getAddRecExpr(SmallVector<SCEVUse, 3>(operands().drop_front()),
349 getLoop(), FlagNone);
350 }
351
352 /// Return true if this represents an expression A + B*x where A
353 /// and B are loop invariant values.
354 bool isAffine() const {
355 // We know that the start value is invariant. This expression is thus
356 // affine iff the step is also invariant.
357 return getNumOperands() == 2;
358 }
359
360 /// Return true if this represents an expression A + B*x + C*x^2
361 /// where A, B and C are loop invariant values. This corresponds
362 /// to an addrec of the form {L,+,M,+,N}
363 bool isQuadratic() const { return getNumOperands() == 3; }
364
365 /// Set flags for a recurrence without clearing any previously set flags.
366 /// For AddRec, either NUW or NSW implies NW. Keep track of this fact here
367 /// to make it easier to propagate flags.
369 if (!any(Flags & FlagsNoWrapMask))
370 return;
371 if (any(Flags & (FlagNUW | FlagNSW)))
372 Flags = ScalarEvolution::setFlags(Flags, FlagNW);
373 SubclassData |= static_cast<unsigned short>(Flags);
374 }
375
376 /// Return the value of this chain of recurrences at the specified
377 /// iteration number.
378 LLVM_ABI const SCEV *evaluateAtIteration(const SCEV *It,
379 ScalarEvolution &SE) const;
380
381 /// Return the value of this chain of recurrences at the specified iteration
382 /// number. Takes an explicit list of operands to represent an AddRec.
383 LLVM_ABI static SCEVUse
386
387 /// Return the value of this recurrences when its loop exits, i.e. its value
388 /// at the loop's exact backedge-taken count, or SCEVCouldNotCompute if that
389 /// count cannot be computed.
391
392 /// Return the number of iterations of this loop that produce
393 /// values in the specified constant range. Another way of
394 /// looking at this is that it returns the first iteration number
395 /// where the value is not in the condition, thus computing the
396 /// exit count. If the iteration count can't be computed, an
397 /// instance of SCEVCouldNotCompute is returned.
399 ScalarEvolution &SE) const;
400
401 /// Return an expression representing the value of this expression
402 /// one iteration of the loop ahead.
404
405 /// Methods for support type inquiry through isa, cast, and dyn_cast:
406 static bool classof(const SCEV *S) {
407 return S->getSCEVType() == scAddRecExpr;
408 }
409};
410
411/// This node is the base class min/max selections.
413 friend class ScalarEvolution;
414
415 static bool isMinMaxType(enum SCEVTypes T) {
416 return T == scSMaxExpr || T == scUMaxExpr || T == scSMinExpr ||
417 T == scUMinExpr;
418 }
419
420protected:
421 /// Note: Constructing subclasses via this constructor is allowed
423 const SCEVUse *O, size_t N)
424 : SCEVCommutativeExpr(ID, T, O, N, O[0]->getType()) {
425 assert(isMinMaxType(T));
426 // Min and max never overflow
428 }
429
430public:
431 static bool classof(const SCEV *S) { return isMinMaxType(S->getSCEVType()); }
432
433 static enum SCEVTypes negate(enum SCEVTypes T) {
434 switch (T) {
435 case scSMaxExpr:
436 return scSMinExpr;
437 case scSMinExpr:
438 return scSMaxExpr;
439 case scUMaxExpr:
440 return scUMinExpr;
441 case scUMinExpr:
442 return scUMaxExpr;
443 default:
444 llvm_unreachable("Not a min or max SCEV type!");
445 }
446 }
447};
448
449/// This class represents a signed maximum selection.
450class SCEVSMaxExpr : public SCEVMinMaxExpr {
451 friend class ScalarEvolution;
452
453 SCEVSMaxExpr(const FoldingSetNodeIDRef ID, const SCEVUse *O, size_t N)
454 : SCEVMinMaxExpr(ID, scSMaxExpr, O, N) {}
455
456public:
457 /// Methods for support type inquiry through isa, cast, and dyn_cast:
458 static bool classof(const SCEV *S) { return S->getSCEVType() == scSMaxExpr; }
459};
460
461/// This class represents an unsigned maximum selection.
462class SCEVUMaxExpr : public SCEVMinMaxExpr {
463 friend class ScalarEvolution;
464
465 SCEVUMaxExpr(const FoldingSetNodeIDRef ID, const SCEVUse *O, size_t N)
466 : SCEVMinMaxExpr(ID, scUMaxExpr, O, N) {}
467
468public:
469 /// Methods for support type inquiry through isa, cast, and dyn_cast:
470 static bool classof(const SCEV *S) { return S->getSCEVType() == scUMaxExpr; }
471};
472
473/// This class represents a signed minimum selection.
474class SCEVSMinExpr : public SCEVMinMaxExpr {
475 friend class ScalarEvolution;
476
477 SCEVSMinExpr(const FoldingSetNodeIDRef ID, const SCEVUse *O, size_t N)
478 : SCEVMinMaxExpr(ID, scSMinExpr, O, N) {}
479
480public:
481 /// Methods for support type inquiry through isa, cast, and dyn_cast:
482 static bool classof(const SCEV *S) { return S->getSCEVType() == scSMinExpr; }
483};
484
485/// This class represents an unsigned minimum selection.
486class SCEVUMinExpr : public SCEVMinMaxExpr {
487 friend class ScalarEvolution;
488
489 SCEVUMinExpr(const FoldingSetNodeIDRef ID, const SCEVUse *O, size_t N)
490 : SCEVMinMaxExpr(ID, scUMinExpr, O, N) {}
491
492public:
493 /// Methods for support type inquiry through isa, cast, and dyn_cast:
494 static bool classof(const SCEV *S) { return S->getSCEVType() == scUMinExpr; }
495};
496
497/// This node is the base class for sequential/in-order min/max selections.
498/// Note that their fundamental difference from SCEVMinMaxExpr's is that they
499/// are early-returning upon reaching saturation point.
500/// I.e. given `0 umin_seq poison`, the result will be `0`, while the result of
501/// `0 umin poison` is `poison`. When returning early, later expressions are not
502/// executed, so `0 umin_seq (%x u/ 0)` does not result in undefined behavior.
504 friend class ScalarEvolution;
505
506 static bool isSequentialMinMaxType(enum SCEVTypes T) {
507 return T == scSequentialUMinExpr;
508 }
509
510 /// Set flags for a non-recurrence without clearing previously set flags.
511 void setFlags(SCEVFlags Flags) {
512 SubclassData |= static_cast<unsigned short>(Flags);
513 }
514 void setNoWrapFlags(SCEVFlags Flags) {
515 setFlags(Flags & SCEV::FlagsNoWrapMask);
516 }
517
518protected:
519 /// Note: Constructing subclasses via this constructor is allowed
521 const SCEVUse *O, size_t N)
522 : SCEVNAryExpr(ID, T, O, N, O[0]->getType()) {
523 assert(isSequentialMinMaxType(T));
524 // Min and max never overflow
525 setNoWrapFlags(FlagNUW | FlagNSW);
526 }
527
528public:
530 assert(isSequentialMinMaxType(Ty));
531 switch (Ty) {
533 return scUMinExpr;
534 default:
535 llvm_unreachable("Not a sequential min/max type.");
536 }
537 }
538
539 static bool classof(const SCEV *S) {
540 return isSequentialMinMaxType(S->getSCEVType());
541 }
542};
543
544/// This class represents a sequential/in-order unsigned minimum selection.
545class SCEVSequentialUMinExpr : public SCEVSequentialMinMaxExpr {
546 friend class ScalarEvolution;
547
548 SCEVSequentialUMinExpr(const FoldingSetNodeIDRef ID, const SCEVUse *O,
549 size_t N)
551
552public:
553 /// Methods for support type inquiry through isa, cast, and dyn_cast:
554 static bool classof(const SCEV *S) {
555 return S->getSCEVType() == scSequentialUMinExpr;
556 }
557};
558
559/// This means that we are dealing with an entirely unknown SCEV
560/// value, and only represent it as its LLVM Value. This is the
561/// "bottom" value for the analysis.
562class LLVM_ABI SCEVUnknown final : public SCEV, private CallbackVH {
563 friend class ScalarEvolution;
564
565 /// The parent ScalarEvolution value. This is used to update the
566 /// parent's maps when the value associated with a SCEVUnknown is
567 /// deleted or RAUW'd.
568 ScalarEvolution *SE;
569
570 /// The next pointer in the linked list of all SCEVUnknown
571 /// instances owned by a ScalarEvolution.
572 SCEVUnknown *Next;
573
574 /// Deterministic ID used for ordering SCEVUnknowns by their creation time.
575 unsigned OrderID;
576
577 SCEVUnknown(const FoldingSetNodeIDRef ID, Value *V, ScalarEvolution *se,
578 SCEVUnknown *next, unsigned OrderID)
579 : SCEV(ID, scUnknown, 1, V->getType()), CallbackVH(V), SE(se), Next(next),
580 OrderID(OrderID) {}
581
582 // Implement CallbackVH.
583 void deleted() override;
584 void allUsesReplacedWith(Value *New) override;
585
586public:
587 Value *getValue() const { return getValPtr(); }
588
589 unsigned getOrderID() const { return OrderID; }
590
591 /// Methods for support type inquiry through isa, cast, and dyn_cast:
592 static bool classof(const SCEV *S) { return S->getSCEVType() == scUnknown; }
593};
594
595/// This class defines a simple visitor class that may be used for
596/// various SCEV analysis purposes.
597template <typename SC, typename RetVal = void> struct SCEVVisitor {
598 RetVal visit(const SCEV *S) {
599 switch (S->getSCEVType()) {
600 case scConstant:
601 return ((SC *)this)->visitConstant((const SCEVConstant *)S);
602 case scVScale:
603 return ((SC *)this)->visitVScale((const SCEVVScale *)S);
604 case scPtrToAddr:
605 return ((SC *)this)->visitPtrToAddrExpr((const SCEVPtrToAddrExpr *)S);
606 case scTruncate:
607 return ((SC *)this)->visitTruncateExpr((const SCEVTruncateExpr *)S);
608 case scZeroExtend:
609 return ((SC *)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr *)S);
610 case scSignExtend:
611 return ((SC *)this)->visitSignExtendExpr((const SCEVSignExtendExpr *)S);
612 case scAddExpr:
613 return ((SC *)this)->visitAddExpr((const SCEVAddExpr *)S);
614 case scMulExpr:
615 return ((SC *)this)->visitMulExpr((const SCEVMulExpr *)S);
616 case scUDivExpr:
617 return ((SC *)this)->visitUDivExpr((const SCEVUDivExpr *)S);
618 case scAddRecExpr:
619 return ((SC *)this)->visitAddRecExpr((const SCEVAddRecExpr *)S);
620 case scSMaxExpr:
621 return ((SC *)this)->visitSMaxExpr((const SCEVSMaxExpr *)S);
622 case scUMaxExpr:
623 return ((SC *)this)->visitUMaxExpr((const SCEVUMaxExpr *)S);
624 case scSMinExpr:
625 return ((SC *)this)->visitSMinExpr((const SCEVSMinExpr *)S);
626 case scUMinExpr:
627 return ((SC *)this)->visitUMinExpr((const SCEVUMinExpr *)S);
629 return ((SC *)this)
630 ->visitSequentialUMinExpr((const SCEVSequentialUMinExpr *)S);
631 case scUnknown:
632 return ((SC *)this)->visitUnknown((const SCEVUnknown *)S);
634 return ((SC *)this)->visitCouldNotCompute((const SCEVCouldNotCompute *)S);
635 }
636 llvm_unreachable("Unknown SCEV kind!");
637 }
638
640 llvm_unreachable("Invalid use of SCEVCouldNotCompute!");
641 }
642};
643
644/// A visitor class for SCEVUse.
645template <typename SC, typename RetVal = void> struct SCEVUseVisitor {
646 RetVal visit(SCEVUse S) {
647 switch (S->getSCEVType()) {
648 case scConstant:
649 return ((SC *)this)
650 ->visitConstant(cast<SCEVUseT<const SCEVConstant *>>(S));
651 case scVScale:
652 return ((SC *)this)->visitVScale(cast<SCEVUseT<const SCEVVScale *>>(S));
653 case scPtrToAddr:
654 return ((SC *)this)
655 ->visitPtrToAddrExpr(cast<SCEVUseT<const SCEVPtrToAddrExpr *>>(S));
656 case scTruncate:
657 return ((SC *)this)
658 ->visitTruncateExpr(cast<SCEVUseT<const SCEVTruncateExpr *>>(S));
659 case scZeroExtend:
660 return ((SC *)this)
661 ->visitZeroExtendExpr(cast<SCEVUseT<const SCEVZeroExtendExpr *>>(S));
662 case scSignExtend:
663 return ((SC *)this)
664 ->visitSignExtendExpr(cast<SCEVUseT<const SCEVSignExtendExpr *>>(S));
665 case scAddExpr:
666 return ((SC *)this)->visitAddExpr(cast<SCEVUseT<const SCEVAddExpr *>>(S));
667 case scMulExpr:
668 return ((SC *)this)->visitMulExpr(cast<SCEVUseT<const SCEVMulExpr *>>(S));
669 case scUDivExpr:
670 return ((SC *)this)
671 ->visitUDivExpr(cast<SCEVUseT<const SCEVUDivExpr *>>(S));
672 case scAddRecExpr:
673 return ((SC *)this)
674 ->visitAddRecExpr(cast<SCEVUseT<const SCEVAddRecExpr *>>(S));
675 case scSMaxExpr:
676 return ((SC *)this)
677 ->visitSMaxExpr(cast<SCEVUseT<const SCEVSMaxExpr *>>(S));
678 case scUMaxExpr:
679 return ((SC *)this)
680 ->visitUMaxExpr(cast<SCEVUseT<const SCEVUMaxExpr *>>(S));
681 case scSMinExpr:
682 return ((SC *)this)
683 ->visitSMinExpr(cast<SCEVUseT<const SCEVSMinExpr *>>(S));
684 case scUMinExpr:
685 return ((SC *)this)
686 ->visitUMinExpr(cast<SCEVUseT<const SCEVUMinExpr *>>(S));
688 return ((SC *)this)
689 ->visitSequentialUMinExpr(
691 case scUnknown:
692 return ((SC *)this)->visitUnknown(cast<SCEVUseT<const SCEVUnknown *>>(S));
694 return ((SC *)this)
695 ->visitCouldNotCompute(
697 }
698 llvm_unreachable("Unknown SCEV kind!");
699 }
700
702 llvm_unreachable("Invalid use of SCEVCouldNotCompute!");
703 }
704};
705
706/// Visit all nodes in the expression tree using worklist traversal.
707///
708/// Visitor implements:
709/// // return true to follow this node.
710/// bool follow(const SCEV *S);
711/// // return true to terminate the search.
712/// bool isDone();
713template <typename SV> class SCEVTraversal {
714 SV &Visitor;
717
718 void push(const SCEV *S) {
719 if (Visited.insert(S).second && Visitor.follow(S))
720 Worklist.push_back(S);
721 }
722
723public:
724 SCEVTraversal(SV &V) : Visitor(V) {}
725
726 void visitAll(const SCEV *Root) {
727 push(Root);
728 while (!Worklist.empty() && !Visitor.isDone()) {
729 const SCEV *S = Worklist.pop_back_val();
730
731 switch (S->getSCEVType()) {
732 case scConstant:
733 case scVScale:
734 case scUnknown:
735 continue;
736 case scPtrToAddr:
737 case scTruncate:
738 case scZeroExtend:
739 case scSignExtend:
740 case scAddExpr:
741 case scMulExpr:
742 case scUDivExpr:
743 case scSMaxExpr:
744 case scUMaxExpr:
745 case scSMinExpr:
746 case scUMinExpr:
748 case scAddRecExpr:
749 for (const SCEV *Op : S->operands()) {
750 push(Op);
751 if (Visitor.isDone())
752 break;
753 }
754 continue;
756 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
757 }
758 llvm_unreachable("Unknown SCEV kind!");
759 }
760 }
761};
762
763/// Use SCEVTraversal to visit all nodes in the given expression tree.
764template <typename SV> void visitAll(const SCEV *Root, SV &Visitor) {
765 SCEVTraversal<SV> T(Visitor);
766 T.visitAll(Root);
767}
768
769/// Return true if any node in \p Root satisfies the predicate \p Pred.
770template <typename PredTy>
771bool SCEVExprContains(const SCEV *Root, PredTy Pred) {
772 struct FindClosure {
773 bool Found = false;
774 PredTy Pred;
775
776 FindClosure(PredTy Pred) : Pred(Pred) {}
777
778 bool follow(const SCEV *S) {
779 if (!Pred(S))
780 return true;
781
782 Found = true;
783 return false;
784 }
785
786 bool isDone() const { return Found; }
787 };
788
789 FindClosure FC(Pred);
790 visitAll(Root, FC);
791 return FC.Found;
792}
793
794/// This visitor recursively visits a SCEV expression and re-writes it.
795/// The result from each visit is cached, so it will return the same
796/// SCEV for the same input.
797template <typename SC>
798class SCEVRewriteVisitor : public SCEVVisitor<SC, const SCEV *> {
799protected:
801 // Memoize the result of each visit so that we only compute once for
802 // the same input SCEV. This is to avoid redundant computations when
803 // a SCEV is referenced by multiple SCEVs. Without memoization, this
804 // visit algorithm would have exponential time complexity in the worst
805 // case, causing the compiler to hang on certain tests.
807
808public:
810
811 const SCEV *visit(const SCEV *S) {
812 auto It = RewriteResults.find(S);
813 if (It != RewriteResults.end())
814 return It->second;
815 auto *Visited = SCEVVisitor<SC, const SCEV *>::visit(S);
816 auto Result = RewriteResults.try_emplace(S, Visited);
817 assert(Result.second && "Should insert a new entry");
818 return Result.first->second;
819 }
820
822
823 const SCEV *visitVScale(const SCEVVScale *VScale) { return VScale; }
824
826 const SCEV *Operand = ((SC *)this)->visit(Expr->getOperand());
827 return Operand == Expr->getOperand() ? Expr : SE.getPtrToAddrExpr(Operand);
828 }
829
831 const SCEV *Operand = ((SC *)this)->visit(Expr->getOperand());
832 return Operand == Expr->getOperand()
833 ? Expr
834 : SE.getTruncateExpr(Operand, Expr->getType());
835 }
836
838 const SCEV *Operand = ((SC *)this)->visit(Expr->getOperand());
839 return Operand == Expr->getOperand()
840 ? Expr
841 : SE.getZeroExtendExpr(Operand, Expr->getType());
842 }
843
845 const SCEV *Operand = ((SC *)this)->visit(Expr->getOperand());
846 return Operand == Expr->getOperand()
847 ? Expr
848 : SE.getSignExtendExpr(Operand, Expr->getType());
849 }
850
851 const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
853 bool Changed = false;
854 for (const SCEV *Op : Expr->operands()) {
855 Operands.push_back(((SC *)this)->visit(Op));
856 Changed |= Op != Operands.back();
857 }
858 return !Changed ? Expr : SE.getAddExpr(Operands);
859 }
860
861 const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
863 bool Changed = false;
864 for (const SCEV *Op : Expr->operands()) {
865 Operands.push_back(((SC *)this)->visit(Op));
866 Changed |= Op != Operands.back();
867 }
868 return !Changed ? Expr : SE.getMulExpr(Operands);
869 }
870
871 const SCEV *visitUDivExpr(const SCEVUDivExpr *Expr) {
872 auto *LHS = ((SC *)this)->visit(Expr->getLHS());
873 auto *RHS = ((SC *)this)->visit(Expr->getRHS());
874 bool Changed = LHS != Expr->getLHS() || RHS != Expr->getRHS();
875 return !Changed ? Expr : SE.getUDivExpr(LHS, RHS);
876 }
877
878 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
880 bool Changed = false;
881 for (const SCEV *Op : Expr->operands()) {
882 Operands.push_back(((SC *)this)->visit(Op));
883 Changed |= Op != Operands.back();
884 }
885 return !Changed ? Expr
886 : SE.getAddRecExpr(Operands, Expr->getLoop(),
887 Expr->getNoWrapFlags());
888 }
889
890 const SCEV *visitSMaxExpr(const SCEVSMaxExpr *Expr) {
892 bool Changed = false;
893 for (const SCEV *Op : Expr->operands()) {
894 Operands.push_back(((SC *)this)->visit(Op));
895 Changed |= Op != Operands.back();
896 }
897 return !Changed ? Expr : SE.getSMaxExpr(Operands);
898 }
899
900 const SCEV *visitUMaxExpr(const SCEVUMaxExpr *Expr) {
902 bool Changed = false;
903 for (const SCEV *Op : Expr->operands()) {
904 Operands.push_back(((SC *)this)->visit(Op));
905 Changed |= Op != Operands.back();
906 }
907 return !Changed ? Expr : SE.getUMaxExpr(Operands);
908 }
909
910 const SCEV *visitSMinExpr(const SCEVSMinExpr *Expr) {
912 bool Changed = false;
913 for (const SCEV *Op : Expr->operands()) {
914 Operands.push_back(((SC *)this)->visit(Op));
915 Changed |= Op != Operands.back();
916 }
917 return !Changed ? Expr : SE.getSMinExpr(Operands);
918 }
919
920 const SCEV *visitUMinExpr(const SCEVUMinExpr *Expr) {
922 bool Changed = false;
923 for (const SCEV *Op : Expr->operands()) {
924 Operands.push_back(((SC *)this)->visit(Op));
925 Changed |= Op != Operands.back();
926 }
927 return !Changed ? Expr : SE.getUMinExpr(Operands);
928 }
929
932 bool Changed = false;
933 for (const SCEV *Op : Expr->operands()) {
934 Operands.push_back(((SC *)this)->visit(Op));
935 Changed |= Op != Operands.back();
936 }
937 return !Changed ? Expr : SE.getUMinExpr(Operands, /*Sequential=*/true);
938 }
939
940 const SCEV *visitUnknown(const SCEVUnknown *Expr) { return Expr; }
941
943 return Expr;
944 }
945};
946
949
950/// The SCEVParameterRewriter takes a scalar evolution expression and updates
951/// the SCEVUnknown components following the Map (Value -> SCEV).
952class SCEVParameterRewriter : public SCEVRewriteVisitor<SCEVParameterRewriter> {
953public:
954 static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE,
955 ValueToSCEVMapTy &Map) {
957 return Rewriter.visit(Scev);
958 }
959
962
963 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
964 auto I = Map.find(Expr->getValue());
965 if (I == Map.end())
966 return Expr;
967 return I->second;
968 }
969
970private:
971 ValueToSCEVMapTy &Map;
972};
973
974template <typename SCEVPtrT>
975inline SCEVUseT<SCEVPtrT>::SCEVUseT(SCEVPtrT S, SCEVFlags Flags) : Base(S, 0) {
976 assert((Flags & SCEVFlags::FlagsNoWrapMask) == Flags &&
977 "Expected only no-wrap flags");
978 if (any(Flags)) {
980 "use flags require an expression that can carry no-wrap flags");
981 // Drop flags already present on S.
982 Flags &= ~cast<SCEVNAryExpr>(S)->getNoWrapFlags();
983 }
984 Base::setInt(static_cast<unsigned>(Flags) >> 1);
985}
986
987template <typename SCEVPtrT>
990 if (auto *NAry = dyn_cast<SCEVNAryExpr>(Base::getPointer()))
991 Flags = NAry->getNoWrapFlags();
992 return (Flags | getUseNoWrapFlags()) & Mask;
993}
994
995} // end namespace llvm
996
997#endif // LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:215
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
#define op(i)
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define I(x, y, z)
Definition MD5.cpp:57
#define T
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
SI Fold Operands
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
virt reg Virtual Register Rewriter
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
CallbackVH(const CallbackVH &)=default
This is the shared class of boolean and integer constants.
Definition Constants.h:87
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
This class represents a range of values.
This is an important base class in LLVM.
Definition Constant.h:43
This class describes a reference to an interned FoldingSetNodeID, which can be a useful to store node...
Definition FoldingSet.h:123
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
This node represents an addition of some number of SCEVs.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This node represents a polynomial recurrence on the trip count of the specified loop.
LLVM_ABI SCEVUse getExitValue(ScalarEvolution &SE) const
Return the value of this recurrences when its loop exits, i.e.
LLVM_ABI const SCEV * evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const
Return the value of this chain of recurrences at the specified iteration number.
void setNoWrapFlags(SCEVFlags Flags)
Set flags for a recurrence without clearing any previously set flags.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
bool isQuadratic() const
Return true if this represents an expression A + B*x + C*x^2 where A, B and C are loop invariant valu...
LLVM_ABI const SCEV * getNumIterationsInRange(const ConstantRange &Range, ScalarEvolution &SE) const
Return the number of iterations of this loop that produce values in the specified constant range.
LLVM_ABI const SCEVAddRecExpr * getPostIncExpr(ScalarEvolution &SE) const
Return an expression representing the value of this expression one iteration of the loop ahead.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
SCEVUse getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
ArrayRef< SCEVUse > operands() const
SCEVUse getOperand(unsigned i) const
LLVM_ABI SCEVCastExpr(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy, SCEVUse op, Type *ty)
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
SCEVCommutativeExpr(const FoldingSetNodeIDRef ID, enum SCEVTypes T, const SCEVUse *O, size_t N, Type *Ty)
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
void setFlags(SCEVFlags Flags)
Set flags for a non-recurrence without clearing previously set flags.
This class represents a constant integer value.
ConstantInt * getValue() const
const APInt & getAPInt() const
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
LLVM_ABI SCEVIntegralCastExpr(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy, SCEVUse op, Type *ty)
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
static enum SCEVTypes negate(enum SCEVTypes T)
SCEVMinMaxExpr(const FoldingSetNodeIDRef ID, enum SCEVTypes T, const SCEVUse *O, size_t N)
Note: Constructing subclasses via this constructor is allowed.
static bool classof(const SCEV *S)
This node represents multiplication of some number of SCEVs.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
ArrayRef< SCEVUse > operands() const
SCEVNAryExpr(const FoldingSetNodeIDRef ID, enum SCEVTypes T, const SCEVUse *O, size_t N, Type *Ty)
SCEVFlags getNoWrapFlags(SCEVFlags Mask=FlagsNoWrapMask) const
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
SCEVUse getOperand(unsigned i) const
const SCEV * visitUnknown(const SCEVUnknown *Expr)
static const SCEV * rewrite(const SCEV *Scev, ScalarEvolution &SE, ValueToSCEVMapTy &Map)
SCEVParameterRewriter(ScalarEvolution &SE, ValueToSCEVMapTy &M)
This class represents a cast from a pointer to a pointer-sized integer value, without capturing the p...
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
SmallDenseMap< const SCEV *, const SCEV *, 16 > RewriteResults
const SCEV * visitPtrToAddrExpr(const SCEVPtrToAddrExpr *Expr)
const SCEV * visitSignExtendExpr(const SCEVSignExtendExpr *Expr)
const SCEV * visit(const SCEV *S)
const SCEV * visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr)
const SCEV * visitUnknown(const SCEVUnknown *Expr)
const SCEV * visitSMinExpr(const SCEVSMinExpr *Expr)
const SCEV * visitSequentialUMinExpr(const SCEVSequentialUMinExpr *Expr)
const SCEV * visitAddExpr(const SCEVAddExpr *Expr)
const SCEV * visitUMinExpr(const SCEVUMinExpr *Expr)
const SCEV * visitMulExpr(const SCEVMulExpr *Expr)
const SCEV * visitTruncateExpr(const SCEVTruncateExpr *Expr)
const SCEV * visitUMaxExpr(const SCEVUMaxExpr *Expr)
const SCEV * visitSMaxExpr(const SCEVSMaxExpr *Expr)
const SCEV * visitUDivExpr(const SCEVUDivExpr *Expr)
const SCEV * visitCouldNotCompute(const SCEVCouldNotCompute *Expr)
const SCEV * visitVScale(const SCEVVScale *VScale)
const SCEV * visitAddRecExpr(const SCEVAddRecExpr *Expr)
const SCEV * visitConstant(const SCEVConstant *Constant)
This class represents a signed maximum selection.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This class represents a signed minimum selection.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
static SCEVTypes getEquivalentNonSequentialSCEVType(SCEVTypes Ty)
SCEVSequentialMinMaxExpr(const FoldingSetNodeIDRef ID, enum SCEVTypes T, const SCEVUse *O, size_t N)
Note: Constructing subclasses via this constructor is allowed.
This class represents a sequential/in-order unsigned minimum selection.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This class represents a sign extension of a small integer value to a larger integer value.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
Visit all nodes in the expression tree using worklist traversal.
void visitAll(const SCEV *Root)
This class represents a truncation of an integer value to a smaller integer value.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This class represents a binary unsigned division operation.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
ArrayRef< SCEVUse > operands() const
SCEVUse getOperand(unsigned i) const
This class represents an unsigned maximum selection.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This class represents an unsigned minimum selection.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This class represents the value of vscale, as used when defining the length of a scalable vector or r...
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This class represents a zero extension of a small integer value to a larger integer value.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This class represents an analyzed expression in the program.
static constexpr auto FlagsNoWrapMask
SCEV(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy, unsigned short ExpressionSize, Type *Ty)
static constexpr auto FlagNUW
Type *const Ty
Immutable type of the SCEV.
static constexpr auto FlagNSW
LLVM_ABI ArrayRef< SCEVUse > operands() const
Return operands of this SCEV expression.
Type * getType() const
Return the LLVM type of this SCEV expression.
static constexpr auto FlagNone
SCEVTypes getSCEVType() const
unsigned short SubclassData
This field is initialized to zero and may be used in subclasses to store miscellaneous information.
static constexpr auto FlagNW
The main scalar evolution driver.
static SCEVFlags setFlags(SCEVFlags Flags, SCEVFlags OnFlags)
LLVM_ABI SCEVUse getAddRecExpr(SCEVUse Start, SCEVUse Step, const Loop *L, SCEVFlagsPair Flags)
Get an add recurrence expression for the specified loop.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
Value * getValPtr() const
LLVM Value Representation.
Definition Value.h:75
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
void visitAll(const SCEV *Root, SV &Visitor)
Use SCEVTraversal to visit all nodes in the given expression tree.
DenseMap< const Value *, const SCEV * > ValueToSCEVMapTy
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
unsigned short computeExpressionSize(ArrayRef< SCEVUse > Args)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
DWARFExpression::Operation Op
SCEVFlags
SCEVFlags are bitfield indices into SCEV's SubclassData.
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1788
DenseMap< const Value *, Value * > ValueToValueMap
SCEVUseT< const SCEV * > SCEVUse
bool SCEVExprContains(const SCEV *Root, PredTy Pred)
Return true if any node in Root satisfies the predicate Pred.
#define N
An object of this class is returned by queries that could not be answered.
SCEVFlags getUseNoWrapFlags() const
Return only the use-specific flags without the underlying SCEV's flags.
SCEVFlags getNoWrapFlags(SCEVFlags Mask=SCEVFlags::FlagsNoWrapMask) const
Return the flags for this SCEVUse, which is the union of the use-specific flags and the underlying SC...
PointerIntPair< SCEVPtrT, 2 > Base
A visitor class for SCEVUse.
RetVal visitCouldNotCompute(SCEVUseT< const SCEVCouldNotCompute * > S)
This class defines a simple visitor class that may be used for various SCEV analysis purposes.
RetVal visit(const SCEV *S)
RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S)