comparison include/llvm/Analysis/ScalarEvolution.h @ 100:7d135dc70f03 LLVM 3.9

LLVM 3.9
author Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
date Tue, 26 Jan 2016 22:53:40 +0900
parents afa8332a0e37
children 1172e4bd9c6f
comparison
equal deleted inserted replaced
96:6418606d0ead 100:7d135dc70f03
21 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H 21 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H
22 #define LLVM_ANALYSIS_SCALAREVOLUTION_H 22 #define LLVM_ANALYSIS_SCALAREVOLUTION_H
23 23
24 #include "llvm/ADT/DenseSet.h" 24 #include "llvm/ADT/DenseSet.h"
25 #include "llvm/ADT/FoldingSet.h" 25 #include "llvm/ADT/FoldingSet.h"
26 #include "llvm/Analysis/LoopInfo.h"
26 #include "llvm/IR/ConstantRange.h" 27 #include "llvm/IR/ConstantRange.h"
27 #include "llvm/IR/Function.h" 28 #include "llvm/IR/Function.h"
28 #include "llvm/IR/Instructions.h" 29 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/Operator.h" 30 #include "llvm/IR/Operator.h"
30 #include "llvm/IR/PassManager.h" 31 #include "llvm/IR/PassManager.h"
43 class Type; 44 class Type;
44 class ScalarEvolution; 45 class ScalarEvolution;
45 class DataLayout; 46 class DataLayout;
46 class TargetLibraryInfo; 47 class TargetLibraryInfo;
47 class LLVMContext; 48 class LLVMContext;
48 class Loop;
49 class LoopInfo;
50 class Operator; 49 class Operator;
50 class SCEV;
51 class SCEVAddRecExpr;
52 class SCEVConstant;
53 class SCEVExpander;
54 class SCEVPredicate;
51 class SCEVUnknown; 55 class SCEVUnknown;
52 class SCEVAddRecExpr; 56
53 class SCEV; 57 template <> struct FoldingSetTrait<SCEV>;
54 template<> struct FoldingSetTrait<SCEV>; 58 template <> struct FoldingSetTrait<SCEVPredicate>;
55 59
56 /// This class represents an analyzed expression in the program. These are 60 /// This class represents an analyzed expression in the program. These are
57 /// opaque objects that the client is not allowed to do much with directly. 61 /// opaque objects that the client is not allowed to do much with directly.
58 /// 62 ///
59 class SCEV : public FoldingSetNode { 63 class SCEV : public FoldingSetNode {
126 130
127 /// Print out the internal representation of this scalar to the specified 131 /// Print out the internal representation of this scalar to the specified
128 /// stream. This should really only be used for debugging purposes. 132 /// stream. This should really only be used for debugging purposes.
129 void print(raw_ostream &OS) const; 133 void print(raw_ostream &OS) const;
130 134
131 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
132 /// This method is used for debugging. 135 /// This method is used for debugging.
133 /// 136 ///
134 void dump() const; 137 void dump() const;
135 #endif
136 }; 138 };
137 139
138 // Specialize FoldingSetTrait for SCEV to avoid needing to compute 140 // Specialize FoldingSetTrait for SCEV to avoid needing to compute
139 // temporary FoldingSetNodeID values. 141 // temporary FoldingSetNodeID values.
140 template<> struct FoldingSetTrait<SCEV> : DefaultFoldingSetTrait<SCEV> { 142 template<> struct FoldingSetTrait<SCEV> : DefaultFoldingSetTrait<SCEV> {
162 struct SCEVCouldNotCompute : public SCEV { 164 struct SCEVCouldNotCompute : public SCEV {
163 SCEVCouldNotCompute(); 165 SCEVCouldNotCompute();
164 166
165 /// Methods for support type inquiry through isa, cast, and dyn_cast: 167 /// Methods for support type inquiry through isa, cast, and dyn_cast:
166 static bool classof(const SCEV *S); 168 static bool classof(const SCEV *S);
169 };
170
171 /// SCEVPredicate - This class represents an assumption made using SCEV
172 /// expressions which can be checked at run-time.
173 class SCEVPredicate : public FoldingSetNode {
174 friend struct FoldingSetTrait<SCEVPredicate>;
175
176 /// A reference to an Interned FoldingSetNodeID for this node. The
177 /// ScalarEvolution's BumpPtrAllocator holds the data.
178 FoldingSetNodeIDRef FastID;
179
180 public:
181 enum SCEVPredicateKind { P_Union, P_Equal };
182
183 protected:
184 SCEVPredicateKind Kind;
185 ~SCEVPredicate() = default;
186 SCEVPredicate(const SCEVPredicate&) = default;
187 SCEVPredicate &operator=(const SCEVPredicate&) = default;
188
189 public:
190 SCEVPredicate(const FoldingSetNodeIDRef ID, SCEVPredicateKind Kind);
191
192 SCEVPredicateKind getKind() const { return Kind; }
193
194 /// \brief Returns the estimated complexity of this predicate.
195 /// This is roughly measured in the number of run-time checks required.
196 virtual unsigned getComplexity() const { return 1; }
197
198 /// \brief Returns true if the predicate is always true. This means that no
199 /// assumptions were made and nothing needs to be checked at run-time.
200 virtual bool isAlwaysTrue() const = 0;
201
202 /// \brief Returns true if this predicate implies \p N.
203 virtual bool implies(const SCEVPredicate *N) const = 0;
204
205 /// \brief Prints a textual representation of this predicate with an
206 /// indentation of \p Depth.
207 virtual void print(raw_ostream &OS, unsigned Depth = 0) const = 0;
208
209 /// \brief Returns the SCEV to which this predicate applies, or nullptr
210 /// if this is a SCEVUnionPredicate.
211 virtual const SCEV *getExpr() const = 0;
212 };
213
214 inline raw_ostream &operator<<(raw_ostream &OS, const SCEVPredicate &P) {
215 P.print(OS);
216 return OS;
217 }
218
219 // Specialize FoldingSetTrait for SCEVPredicate to avoid needing to compute
220 // temporary FoldingSetNodeID values.
221 template <>
222 struct FoldingSetTrait<SCEVPredicate>
223 : DefaultFoldingSetTrait<SCEVPredicate> {
224
225 static void Profile(const SCEVPredicate &X, FoldingSetNodeID &ID) {
226 ID = X.FastID;
227 }
228
229 static bool Equals(const SCEVPredicate &X, const FoldingSetNodeID &ID,
230 unsigned IDHash, FoldingSetNodeID &TempID) {
231 return ID == X.FastID;
232 }
233 static unsigned ComputeHash(const SCEVPredicate &X,
234 FoldingSetNodeID &TempID) {
235 return X.FastID.ComputeHash();
236 }
237 };
238
239 /// SCEVEqualPredicate - This class represents an assumption that two SCEV
240 /// expressions are equal, and this can be checked at run-time. We assume
241 /// that the left hand side is a SCEVUnknown and the right hand side a
242 /// constant.
243 class SCEVEqualPredicate final : public SCEVPredicate {
244 /// We assume that LHS == RHS, where LHS is a SCEVUnknown and RHS a
245 /// constant.
246 const SCEVUnknown *LHS;
247 const SCEVConstant *RHS;
248
249 public:
250 SCEVEqualPredicate(const FoldingSetNodeIDRef ID, const SCEVUnknown *LHS,
251 const SCEVConstant *RHS);
252
253 /// Implementation of the SCEVPredicate interface
254 bool implies(const SCEVPredicate *N) const override;
255 void print(raw_ostream &OS, unsigned Depth = 0) const override;
256 bool isAlwaysTrue() const override;
257 const SCEV *getExpr() const override;
258
259 /// \brief Returns the left hand side of the equality.
260 const SCEVUnknown *getLHS() const { return LHS; }
261
262 /// \brief Returns the right hand side of the equality.
263 const SCEVConstant *getRHS() const { return RHS; }
264
265 /// Methods for support type inquiry through isa, cast, and dyn_cast:
266 static inline bool classof(const SCEVPredicate *P) {
267 return P->getKind() == P_Equal;
268 }
269 };
270
271 /// SCEVUnionPredicate - This class represents a composition of other
272 /// SCEV predicates, and is the class that most clients will interact with.
273 /// This is equivalent to a logical "AND" of all the predicates in the union.
274 class SCEVUnionPredicate final : public SCEVPredicate {
275 private:
276 typedef DenseMap<const SCEV *, SmallVector<const SCEVPredicate *, 4>>
277 PredicateMap;
278
279 /// Vector with references to all predicates in this union.
280 SmallVector<const SCEVPredicate *, 16> Preds;
281 /// Maps SCEVs to predicates for quick look-ups.
282 PredicateMap SCEVToPreds;
283
284 public:
285 SCEVUnionPredicate();
286
287 const SmallVectorImpl<const SCEVPredicate *> &getPredicates() const {
288 return Preds;
289 }
290
291 /// \brief Adds a predicate to this union.
292 void add(const SCEVPredicate *N);
293
294 /// \brief Returns a reference to a vector containing all predicates
295 /// which apply to \p Expr.
296 ArrayRef<const SCEVPredicate *> getPredicatesForExpr(const SCEV *Expr);
297
298 /// Implementation of the SCEVPredicate interface
299 bool isAlwaysTrue() const override;
300 bool implies(const SCEVPredicate *N) const override;
301 void print(raw_ostream &OS, unsigned Depth) const override;
302 const SCEV *getExpr() const override;
303
304 /// \brief We estimate the complexity of a union predicate as the size
305 /// number of predicates in the union.
306 unsigned getComplexity() const override { return Preds.size(); }
307
308 /// Methods for support type inquiry through isa, cast, and dyn_cast:
309 static inline bool classof(const SCEVPredicate *P) {
310 return P->getKind() == P_Union;
311 }
167 }; 312 };
168 313
169 /// The main scalar evolution driver. Because client code (intentionally) 314 /// The main scalar evolution driver. Because client code (intentionally)
170 /// can't do much with the SCEV objects directly, they must ask this class 315 /// can't do much with the SCEV objects directly, they must ask this class
171 /// for services. 316 /// for services.
265 const SCEV *Exact; 410 const SCEV *Exact;
266 const SCEV *Max; 411 const SCEV *Max;
267 412
268 /*implicit*/ ExitLimit(const SCEV *E) : Exact(E), Max(E) {} 413 /*implicit*/ ExitLimit(const SCEV *E) : Exact(E), Max(E) {}
269 414
270 ExitLimit(const SCEV *E, const SCEV *M) : Exact(E), Max(M) {} 415 ExitLimit(const SCEV *E, const SCEV *M) : Exact(E), Max(M) {
416 assert((isa<SCEVCouldNotCompute>(Exact) ||
417 !isa<SCEVCouldNotCompute>(Max)) &&
418 "Exact is not allowed to be less precise than Max");
419 }
271 420
272 /// Test whether this ExitLimit contains any computed information, or 421 /// Test whether this ExitLimit contains any computed information, or
273 /// whether it's all SCEVCouldNotCompute values. 422 /// whether it's all SCEVCouldNotCompute values.
274 bool hasAnyInfo() const { 423 bool hasAnyInfo() const {
275 return !isa<SCEVCouldNotCompute>(Exact) || 424 return !isa<SCEVCouldNotCompute>(Exact) ||
482 ExitLimit computeLoadConstantCompareExitLimit(LoadInst *LI, 631 ExitLimit computeLoadConstantCompareExitLimit(LoadInst *LI,
483 Constant *RHS, 632 Constant *RHS,
484 const Loop *L, 633 const Loop *L,
485 ICmpInst::Predicate p); 634 ICmpInst::Predicate p);
486 635
636 /// Compute the exit limit of a loop that is controlled by a
637 /// "(IV >> 1) != 0" type comparison. We cannot compute the exact trip
638 /// count in these cases (since SCEV has no way of expressing them), but we
639 /// can still sometimes compute an upper bound.
640 ///
641 /// Return an ExitLimit for a loop whose backedge is guarded by `LHS Pred
642 /// RHS`.
643 ExitLimit computeShiftCompareExitLimit(Value *LHS, Value *RHS,
644 const Loop *L,
645 ICmpInst::Predicate Pred);
646
487 /// If the loop is known to execute a constant number of times (the 647 /// If the loop is known to execute a constant number of times (the
488 /// condition evolves only from constants), try to evaluate a few iterations 648 /// condition evolves only from constants), try to evaluate a few iterations
489 /// of the loop until we get the exit condition gets a value of ExitWhen 649 /// of the loop until we get the exit condition gets a value of ExitWhen
490 /// (true or false). If we cannot evaluate the exit count of the loop, 650 /// (true or false). If we cannot evaluate the exit count of the loop,
491 /// return CouldNotCompute. 651 /// return CouldNotCompute.
573 /// Test if the given expression is known to satisfy the condition described 733 /// Test if the given expression is known to satisfy the condition described
574 /// by Pred and the known constant ranges of LHS and RHS. 734 /// by Pred and the known constant ranges of LHS and RHS.
575 /// 735 ///
576 bool isKnownPredicateWithRanges(ICmpInst::Predicate Pred, 736 bool isKnownPredicateWithRanges(ICmpInst::Predicate Pred,
577 const SCEV *LHS, const SCEV *RHS); 737 const SCEV *LHS, const SCEV *RHS);
738
739 /// Try to prove the condition described by "LHS Pred RHS" by ruling out
740 /// integer overflow.
741 ///
742 /// For instance, this will return true for "A s< (A + C)<nsw>" if C is
743 /// positive.
744 bool isKnownPredicateViaNoOverflow(ICmpInst::Predicate Pred,
745 const SCEV *LHS, const SCEV *RHS);
578 746
579 /// Try to split Pred LHS RHS into logical conjunctions (and's) and try to 747 /// Try to split Pred LHS RHS into logical conjunctions (and's) and try to
580 /// prove them individually. 748 /// prove them individually.
581 bool isKnownPredicateViaSplitting(ICmpInst::Predicate Pred, const SCEV *LHS, 749 bool isKnownPredicateViaSplitting(ICmpInst::Predicate Pred, const SCEV *LHS,
582 const SCEV *RHS); 750 const SCEV *RHS);
666 const SCEV *getAnyExtendExpr(const SCEV *Op, Type *Ty); 834 const SCEV *getAnyExtendExpr(const SCEV *Op, Type *Ty);
667 const SCEV *getAddExpr(SmallVectorImpl<const SCEV *> &Ops, 835 const SCEV *getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
668 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap); 836 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap);
669 const SCEV *getAddExpr(const SCEV *LHS, const SCEV *RHS, 837 const SCEV *getAddExpr(const SCEV *LHS, const SCEV *RHS,
670 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) { 838 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) {
671 SmallVector<const SCEV *, 2> Ops; 839 SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
672 Ops.push_back(LHS);
673 Ops.push_back(RHS);
674 return getAddExpr(Ops, Flags); 840 return getAddExpr(Ops, Flags);
675 } 841 }
676 const SCEV *getAddExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2, 842 const SCEV *getAddExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2,
677 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) { 843 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) {
678 SmallVector<const SCEV *, 3> Ops; 844 SmallVector<const SCEV *, 3> Ops = {Op0, Op1, Op2};
679 Ops.push_back(Op0);
680 Ops.push_back(Op1);
681 Ops.push_back(Op2);
682 return getAddExpr(Ops, Flags); 845 return getAddExpr(Ops, Flags);
683 } 846 }
684 const SCEV *getMulExpr(SmallVectorImpl<const SCEV *> &Ops, 847 const SCEV *getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
685 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap); 848 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap);
686 const SCEV *getMulExpr(const SCEV *LHS, const SCEV *RHS, 849 const SCEV *getMulExpr(const SCEV *LHS, const SCEV *RHS,
687 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) 850 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) {
688 { 851 SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
689 SmallVector<const SCEV *, 2> Ops;
690 Ops.push_back(LHS);
691 Ops.push_back(RHS);
692 return getMulExpr(Ops, Flags); 852 return getMulExpr(Ops, Flags);
693 } 853 }
694 const SCEV *getMulExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2, 854 const SCEV *getMulExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2,
695 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) { 855 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) {
696 SmallVector<const SCEV *, 3> Ops; 856 SmallVector<const SCEV *, 3> Ops = {Op0, Op1, Op2};
697 Ops.push_back(Op0);
698 Ops.push_back(Op1);
699 Ops.push_back(Op2);
700 return getMulExpr(Ops, Flags); 857 return getMulExpr(Ops, Flags);
701 } 858 }
702 const SCEV *getUDivExpr(const SCEV *LHS, const SCEV *RHS); 859 const SCEV *getUDivExpr(const SCEV *LHS, const SCEV *RHS);
703 const SCEV *getUDivExactExpr(const SCEV *LHS, const SCEV *RHS); 860 const SCEV *getUDivExactExpr(const SCEV *LHS, const SCEV *RHS);
704 const SCEV *getAddRecExpr(const SCEV *Start, const SCEV *Step, 861 const SCEV *getAddRecExpr(const SCEV *Start, const SCEV *Step,
1083 void delinearize(const SCEV *Expr, 1240 void delinearize(const SCEV *Expr,
1084 SmallVectorImpl<const SCEV *> &Subscripts, 1241 SmallVectorImpl<const SCEV *> &Subscripts,
1085 SmallVectorImpl<const SCEV *> &Sizes, 1242 SmallVectorImpl<const SCEV *> &Sizes,
1086 const SCEV *ElementSize); 1243 const SCEV *ElementSize);
1087 1244
1245 /// Return the DataLayout associated with the module this SCEV instance is
1246 /// operating on.
1247 const DataLayout &getDataLayout() const {
1248 return F.getParent()->getDataLayout();
1249 }
1250
1251 const SCEVPredicate *getEqualPredicate(const SCEVUnknown *LHS,
1252 const SCEVConstant *RHS);
1253
1254 /// Re-writes the SCEV according to the Predicates in \p Preds.
1255 const SCEV *rewriteUsingPredicate(const SCEV *Scev, SCEVUnionPredicate &A);
1256
1088 private: 1257 private:
1089 /// Compute the backedge taken count knowing the interval difference, the 1258 /// Compute the backedge taken count knowing the interval difference, the
1090 /// stride and presence of the equality in the comparison. 1259 /// stride and presence of the equality in the comparison.
1091 const SCEV *computeBECount(const SCEV *Delta, const SCEV *Stride, 1260 const SCEV *computeBECount(const SCEV *Delta, const SCEV *Stride,
1092 bool Equality); 1261 bool Equality);
1103 bool doesIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride, 1272 bool doesIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride,
1104 bool IsSigned, bool NoWrap); 1273 bool IsSigned, bool NoWrap);
1105 1274
1106 private: 1275 private:
1107 FoldingSet<SCEV> UniqueSCEVs; 1276 FoldingSet<SCEV> UniqueSCEVs;
1277 FoldingSet<SCEVPredicate> UniquePreds;
1108 BumpPtrAllocator SCEVAllocator; 1278 BumpPtrAllocator SCEVAllocator;
1109 1279
1110 /// The head of a linked list of all SCEVUnknown values that have been 1280 /// The head of a linked list of all SCEVUnknown values that have been
1111 /// allocated. This is used by releaseMemory to locate them all and call 1281 /// allocated. This is used by releaseMemory to locate them all and call
1112 /// their destructors. 1282 /// their destructors.
1155 void releaseMemory() override; 1325 void releaseMemory() override;
1156 void getAnalysisUsage(AnalysisUsage &AU) const override; 1326 void getAnalysisUsage(AnalysisUsage &AU) const override;
1157 void print(raw_ostream &OS, const Module * = nullptr) const override; 1327 void print(raw_ostream &OS, const Module * = nullptr) const override;
1158 void verifyAnalysis() const override; 1328 void verifyAnalysis() const override;
1159 }; 1329 };
1330
1331 /// An interface layer with SCEV used to manage how we see SCEV expressions
1332 /// for values in the context of existing predicates. We can add new
1333 /// predicates, but we cannot remove them.
1334 ///
1335 /// This layer has multiple purposes:
1336 /// - provides a simple interface for SCEV versioning.
1337 /// - guarantees that the order of transformations applied on a SCEV
1338 /// expression for a single Value is consistent across two different
1339 /// getSCEV calls. This means that, for example, once we've obtained
1340 /// an AddRec expression for a certain value through expression
1341 /// rewriting, we will continue to get an AddRec expression for that
1342 /// Value.
1343 /// - lowers the number of expression rewrites.
1344 class PredicatedScalarEvolution {
1345 public:
1346 PredicatedScalarEvolution(ScalarEvolution &SE);
1347 const SCEVUnionPredicate &getUnionPredicate() const;
1348 /// \brief Returns the SCEV expression of V, in the context of the current
1349 /// SCEV predicate.
1350 /// The order of transformations applied on the expression of V returned
1351 /// by ScalarEvolution is guaranteed to be preserved, even when adding new
1352 /// predicates.
1353 const SCEV *getSCEV(Value *V);
1354 /// \brief Adds a new predicate.
1355 void addPredicate(const SCEVPredicate &Pred);
1356 /// \brief Returns the ScalarEvolution analysis used.
1357 ScalarEvolution *getSE() const { return &SE; }
1358
1359 private:
1360 /// \brief Increments the version number of the predicate.
1361 /// This needs to be called every time the SCEV predicate changes.
1362 void updateGeneration();
1363 /// Holds a SCEV and the version number of the SCEV predicate used to
1364 /// perform the rewrite of the expression.
1365 typedef std::pair<unsigned, const SCEV *> RewriteEntry;
1366 /// Maps a SCEV to the rewrite result of that SCEV at a certain version
1367 /// number. If this number doesn't match the current Generation, we will
1368 /// need to do a rewrite. To preserve the transformation order of previous
1369 /// rewrites, we will rewrite the previous result instead of the original
1370 /// SCEV.
1371 DenseMap<const SCEV *, RewriteEntry> RewriteMap;
1372 /// The ScalarEvolution analysis.
1373 ScalarEvolution &SE;
1374 /// The SCEVPredicate that forms our context. We will rewrite all
1375 /// expressions assuming that this predicate true.
1376 SCEVUnionPredicate Preds;
1377 /// Marks the version of the SCEV predicate used. When rewriting a SCEV
1378 /// expression we mark it with the version of the predicate. We use this to
1379 /// figure out if the predicate has changed from the last rewrite of the
1380 /// SCEV. If so, we need to perform a new rewrite.
1381 unsigned Generation;
1382 };
1160 } 1383 }
1161 1384
1162 #endif 1385 #endif