Polly 23.0.0git
SCEVAffinator.h
Go to the documentation of this file.
1//===------ polly/SCEVAffinator.h - Create isl expressions from SCEVs -----===//
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// Create a polyhedral description for a SCEV value.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef POLLY_SCEV_AFFINATOR_H
14#define POLLY_SCEV_AFFINATOR_H
15
17#include "llvm/Analysis/ScalarEvolutionExpressions.h"
19
20namespace polly {
21class Scop;
22
23/// The result type of the SCEVAffinator.
24///
25/// The first element of the pair is the isl representation of the SCEV, the
26/// second is the domain under which it is __invalid__.
27typedef std::pair<isl::pw_aff, isl::set> PWACtx;
28
29/// Translate a SCEV to an isl::pw_aff and the domain on which it is invalid.
30class SCEVAffinator final : public llvm::SCEVVisitor<SCEVAffinator, PWACtx> {
31public:
32 SCEVAffinator(Scop *S, llvm::LoopInfo &LI);
33
34 /// Translate a SCEV to an isl::pw_aff.
35 ///
36 /// @param E The expression that is translated.
37 /// @param BB The block in which @p E is executed.
38 /// @param RecordedAssumptions If set, assumptions that make the translation
39 /// valid are added here.
40 /// @param IsInsideDomain If true, assumptions only need to apply during the
41 /// execution of @p BB. That is, when we know that we
42 /// are in its domain. Must be false if the SCEV is
43 /// evaluated outside a ScopStmt, or for code that
44 /// computes the domain (since while doing that, we
45 /// don't know whether we are in the domain yet).
46 ///
47 /// @returns The isl representation of the SCEV @p E in @p Domain.
48 PWACtx getPwAff(const llvm::SCEV *E, llvm::BasicBlock *BB = nullptr,
50 bool IsInsideDomain = true);
51
52 /// Take the assumption that @p PWAC is non-negative.
55
56 /// Interpret the PWA in @p PWAC as an unsigned value.
57 void interpretAsUnsigned(PWACtx &PWAC, unsigned Width);
58
59 /// Check an <nsw> AddRec for the loop @p L is cached.
60 bool hasNSWAddRecForLoop(llvm::Loop *L) const;
61
62 /// Return the LoopInfo used by the object.
63 llvm::LoopInfo *getLI() const { return &LI; }
64
65private:
66 /// Key to identify cached expressions.
67 using CacheKey = std::pair<const llvm::SCEV *, llvm::BasicBlock *>;
68
69 /// Map to remembered cached expressions.
70 llvm::DenseMap<CacheKey, PWACtx> CachedExpressions;
71
74 unsigned NumIterators;
75 llvm::ScalarEvolution &SE;
76 llvm::LoopInfo &LI;
77 llvm::BasicBlock *BB;
78
79 /// Whether the evaluation takes place only when @p BB's domain has already
80 /// been checked.
81 /// @see getPwAff
82 bool IsInsideDomain = true;
83
85
86 /// Target data for element size computing.
87 const llvm::DataLayout &TD;
88
89 /// Return the loop for the current block if any.
90 llvm::Loop *getScope();
91
92 /// Return a PWACtx for @p PWA that is always valid.
94
95 /// Compute the non-wrapping version of @p PWA for type @p ExprType.
96 ///
97 /// @param PWA The piece-wise affine function that might wrap.
98 /// @param Type The type of the SCEV that was translated to @p PWA.
99 ///
100 /// @returns The expr @p PWA modulo the size constraints of @p ExprType.
101 isl::pw_aff addModuloSemantic(isl::pw_aff PWA, llvm::Type *ExprType) const;
102
103 /// If @p Expr might cause an integer wrap record an assumption.
104 ///
105 /// @param Expr The SCEV expression that might wrap.
106 /// @param PWAC The isl representation of @p Expr with the invalid domain.
107 ///
108 /// @returns The isl representation @p PWAC with a possibly adjusted domain.
109 PWACtx checkForWrapping(const llvm::SCEV *Expr, PWACtx PWAC) const;
110
111 /// Whether to track the value of this expression precisely, rather than
112 /// assuming it won't wrap.
113 bool computeModuloForExpr(const llvm::SCEV *Expr);
114
115 PWACtx visit(const llvm::SCEV *E);
116 PWACtx visitConstant(const llvm::SCEVConstant *E);
117 PWACtx visitVScale(const llvm::SCEVVScale *E);
118 PWACtx visitPtrToAddrExpr(const llvm::SCEVPtrToAddrExpr *E);
119 PWACtx visitPtrToIntExpr(const llvm::SCEVPtrToIntExpr *E);
120 PWACtx visitTruncateExpr(const llvm::SCEVTruncateExpr *E);
121 PWACtx visitZeroExtendExpr(const llvm::SCEVZeroExtendExpr *E);
122 PWACtx visitSignExtendExpr(const llvm::SCEVSignExtendExpr *E);
123 PWACtx visitAddExpr(const llvm::SCEVAddExpr *E);
124 PWACtx visitMulExpr(const llvm::SCEVMulExpr *E);
125 PWACtx visitUDivExpr(const llvm::SCEVUDivExpr *E);
126 PWACtx visitAddRecExpr(const llvm::SCEVAddRecExpr *E);
127 PWACtx visitSMaxExpr(const llvm::SCEVSMaxExpr *E);
128 PWACtx visitSMinExpr(const llvm::SCEVSMinExpr *E);
129 PWACtx visitUMaxExpr(const llvm::SCEVUMaxExpr *E);
130 PWACtx visitUMinExpr(const llvm::SCEVUMinExpr *E);
131 PWACtx visitSequentialUMinExpr(const llvm::SCEVSequentialUMinExpr *E);
132 PWACtx visitUnknown(const llvm::SCEVUnknown *E);
133 PWACtx visitSDivInstruction(llvm::Instruction *SDiv);
134 PWACtx visitSRemInstruction(llvm::Instruction *SRem);
136
137 friend struct llvm::SCEVVisitor<SCEVAffinator, PWACtx>;
138};
139} // namespace polly
140
141#endif
PWACtx visitMulExpr(const llvm::SCEVMulExpr *E)
PWACtx getPwAff(const llvm::SCEV *E, llvm::BasicBlock *BB=nullptr, RecordedAssumptionsTy *RecordedAssumptions=nullptr, bool IsInsideDomain=true)
Translate a SCEV to an isl::pw_aff.
PWACtx visitUMinExpr(const llvm::SCEVUMinExpr *E)
void interpretAsUnsigned(PWACtx &PWAC, unsigned Width)
Interpret the PWA in PWAC as an unsigned value.
bool computeModuloForExpr(const llvm::SCEV *Expr)
Whether to track the value of this expression precisely, rather than assuming it won't wrap.
PWACtx visitUMaxExpr(const llvm::SCEVUMaxExpr *E)
SCEVAffinator(Scop *S, llvm::LoopInfo &LI)
PWACtx visitUDivExpr(const llvm::SCEVUDivExpr *E)
PWACtx visit(const llvm::SCEV *E)
bool hasNSWAddRecForLoop(llvm::Loop *L) const
Check an <nsw> AddRec for the loop L is cached.
void takeNonNegativeAssumption(PWACtx &PWAC, RecordedAssumptionsTy *RecordedAssumptions=nullptr)
Take the assumption that PWAC is non-negative.
llvm::DenseMap< CacheKey, PWACtx > CachedExpressions
Map to remembered cached expressions.
RecordedAssumptionsTy * RecordedAssumptions
llvm::LoopInfo * getLI() const
Return the LoopInfo used by the object.
PWACtx visitTruncateExpr(const llvm::SCEVTruncateExpr *E)
PWACtx visitSMinExpr(const llvm::SCEVSMinExpr *E)
PWACtx visitSDivInstruction(llvm::Instruction *SDiv)
PWACtx visitConstant(const llvm::SCEVConstant *E)
PWACtx visitPtrToIntExpr(const llvm::SCEVPtrToIntExpr *E)
llvm::LoopInfo & LI
const llvm::DataLayout & TD
Target data for element size computing.
std::pair< const llvm::SCEV *, llvm::BasicBlock * > CacheKey
Key to identify cached expressions.
PWACtx visitAddExpr(const llvm::SCEVAddExpr *E)
PWACtx visitVScale(const llvm::SCEVVScale *E)
llvm::BasicBlock * BB
PWACtx visitSequentialUMinExpr(const llvm::SCEVSequentialUMinExpr *E)
isl::pw_aff addModuloSemantic(isl::pw_aff PWA, llvm::Type *ExprType) const
Compute the non-wrapping version of PWA for type ExprType.
llvm::ScalarEvolution & SE
PWACtx visitSRemInstruction(llvm::Instruction *SRem)
PWACtx visitAddRecExpr(const llvm::SCEVAddRecExpr *E)
PWACtx checkForWrapping(const llvm::SCEV *Expr, PWACtx PWAC) const
If Expr might cause an integer wrap record an assumption.
PWACtx visitUnknown(const llvm::SCEVUnknown *E)
PWACtx visitPtrToAddrExpr(const llvm::SCEVPtrToAddrExpr *E)
PWACtx getPWACtxFromPWA(isl::pw_aff PWA)
Return a PWACtx for PWA that is always valid.
llvm::Loop * getScope()
Return the loop for the current block if any.
bool IsInsideDomain
Whether the evaluation takes place only when BB's domain has already been checked.
PWACtx visitSMaxExpr(const llvm::SCEVSMaxExpr *E)
PWACtx visitSignExtendExpr(const llvm::SCEVSignExtendExpr *E)
PWACtx visitZeroExtendExpr(const llvm::SCEVZeroExtendExpr *E)
Static Control Part.
Definition ScopInfo.h:1625
std::pair< isl::pw_aff, isl::set > PWACtx
The result type of the SCEVAffinator.
llvm::SmallVector< Assumption, 8 > RecordedAssumptionsTy
Definition ScopHelper.h:81