LLVM  3.7.0
ScalarEvolutionExpressions.h
Go to the documentation of this file.
1 //===- llvm/Analysis/ScalarEvolutionExpressions.h - SCEV Exprs --*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the classes used to represent and build scalar expressions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H
15 #define LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H
16 
17 #include "llvm/ADT/SmallPtrSet.h"
21 
22 namespace llvm {
23  class ConstantInt;
24  class ConstantRange;
25  class DominatorTree;
26 
27  enum SCEVTypes {
28  // These should be ordered in terms of increasing complexity to make the
29  // folders simpler.
33  };
34 
35  //===--------------------------------------------------------------------===//
36  /// SCEVConstant - This class represents a constant integer value.
37  ///
38  class SCEVConstant : public SCEV {
39  friend class ScalarEvolution;
40 
41  ConstantInt *V;
43  SCEV(ID, scConstant), V(v) {}
44  public:
45  ConstantInt *getValue() const { return V; }
46 
47  Type *getType() const { return V->getType(); }
48 
49  /// Methods for support type inquiry through isa, cast, and dyn_cast:
50  static inline bool classof(const SCEV *S) {
51  return S->getSCEVType() == scConstant;
52  }
53  };
54 
55  //===--------------------------------------------------------------------===//
56  /// SCEVCastExpr - This is the base class for unary cast operator classes.
57  ///
58  class SCEVCastExpr : public SCEV {
59  protected:
60  const SCEV *Op;
61  Type *Ty;
62 
64  unsigned SCEVTy, const SCEV *op, Type *ty);
65 
66  public:
67  const SCEV *getOperand() const { return Op; }
68  Type *getType() const { return Ty; }
69 
70  /// Methods for support type inquiry through isa, cast, and dyn_cast:
71  static inline bool classof(const SCEV *S) {
72  return S->getSCEVType() == scTruncate ||
73  S->getSCEVType() == scZeroExtend ||
74  S->getSCEVType() == scSignExtend;
75  }
76  };
77 
78  //===--------------------------------------------------------------------===//
79  /// SCEVTruncateExpr - This class represents a truncation of an integer value
80  /// to a smaller integer value.
81  ///
82  class SCEVTruncateExpr : public SCEVCastExpr {
83  friend class ScalarEvolution;
84 
86  const SCEV *op, Type *ty);
87 
88  public:
89  /// Methods for support type inquiry through isa, cast, and dyn_cast:
90  static inline bool classof(const SCEV *S) {
91  return S->getSCEVType() == scTruncate;
92  }
93  };
94 
95  //===--------------------------------------------------------------------===//
96  /// SCEVZeroExtendExpr - This class represents a zero extension of a small
97  /// integer value to a larger integer value.
98  ///
100  friend class ScalarEvolution;
101 
103  const SCEV *op, Type *ty);
104 
105  public:
106  /// Methods for support type inquiry through isa, cast, and dyn_cast:
107  static inline bool classof(const SCEV *S) {
108  return S->getSCEVType() == scZeroExtend;
109  }
110  };
111 
112  //===--------------------------------------------------------------------===//
113  /// SCEVSignExtendExpr - This class represents a sign extension of a small
114  /// integer value to a larger integer value.
115  ///
117  friend class ScalarEvolution;
118 
120  const SCEV *op, Type *ty);
121 
122  public:
123  /// Methods for support type inquiry through isa, cast, and dyn_cast:
124  static inline bool classof(const SCEV *S) {
125  return S->getSCEVType() == scSignExtend;
126  }
127  };
128 
129 
130  //===--------------------------------------------------------------------===//
131  /// SCEVNAryExpr - This node is a base class providing common
132  /// functionality for n'ary operators.
133  ///
134  class SCEVNAryExpr : public SCEV {
135  protected:
136  // Since SCEVs are immutable, ScalarEvolution allocates operand
137  // arrays with its SCEVAllocator, so this class just needs a simple
138  // pointer rather than a more elaborate vector-like data structure.
139  // This also avoids the need for a non-trivial destructor.
140  const SCEV *const *Operands;
141  size_t NumOperands;
142 
144  enum SCEVTypes T, const SCEV *const *O, size_t N)
145  : SCEV(ID, T), Operands(O), NumOperands(N) {}
146 
147  public:
148  size_t getNumOperands() const { return NumOperands; }
149  const SCEV *getOperand(unsigned i) const {
150  assert(i < NumOperands && "Operand index out of range!");
151  return Operands[i];
152  }
153 
154  typedef const SCEV *const *op_iterator;
156  op_iterator op_begin() const { return Operands; }
157  op_iterator op_end() const { return Operands + NumOperands; }
158  op_range operands() const {
159  return make_range(op_begin(), op_end());
160  }
161 
162  Type *getType() const { return getOperand(0)->getType(); }
163 
165  return (NoWrapFlags)(SubclassData & Mask);
166  }
167 
168  /// Methods for support type inquiry through isa, cast, and dyn_cast:
169  static inline bool classof(const SCEV *S) {
170  return S->getSCEVType() == scAddExpr ||
171  S->getSCEVType() == scMulExpr ||
172  S->getSCEVType() == scSMaxExpr ||
173  S->getSCEVType() == scUMaxExpr ||
174  S->getSCEVType() == scAddRecExpr;
175  }
176  };
177 
178  //===--------------------------------------------------------------------===//
179  /// SCEVCommutativeExpr - This node is the base class for n'ary commutative
180  /// operators.
181  ///
183  protected:
185  enum SCEVTypes T, const SCEV *const *O, size_t N)
186  : SCEVNAryExpr(ID, T, O, N) {}
187 
188  public:
189  /// Methods for support type inquiry through isa, cast, and dyn_cast:
190  static inline bool classof(const SCEV *S) {
191  return S->getSCEVType() == scAddExpr ||
192  S->getSCEVType() == scMulExpr ||
193  S->getSCEVType() == scSMaxExpr ||
194  S->getSCEVType() == scUMaxExpr;
195  }
196 
197  /// Set flags for a non-recurrence without clearing previously set flags.
199  SubclassData |= Flags;
200  }
201  };
202 
203 
204  //===--------------------------------------------------------------------===//
205  /// SCEVAddExpr - This node represents an addition of some number of SCEVs.
206  ///
208  friend class ScalarEvolution;
209 
211  const SCEV *const *O, size_t N)
212  : SCEVCommutativeExpr(ID, scAddExpr, O, N) {
213  }
214 
215  public:
216  Type *getType() const {
217  // Use the type of the last operand, which is likely to be a pointer
218  // type, if there is one. This doesn't usually matter, but it can help
219  // reduce casts when the expressions are expanded.
220  return getOperand(getNumOperands() - 1)->getType();
221  }
222 
223  /// Methods for support type inquiry through isa, cast, and dyn_cast:
224  static inline bool classof(const SCEV *S) {
225  return S->getSCEVType() == scAddExpr;
226  }
227  };
228 
229  //===--------------------------------------------------------------------===//
230  /// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
231  ///
233  friend class ScalarEvolution;
234 
236  const SCEV *const *O, size_t N)
237  : SCEVCommutativeExpr(ID, scMulExpr, O, N) {
238  }
239 
240  public:
241  /// Methods for support type inquiry through isa, cast, and dyn_cast:
242  static inline bool classof(const SCEV *S) {
243  return S->getSCEVType() == scMulExpr;
244  }
245  };
246 
247 
248  //===--------------------------------------------------------------------===//
249  /// SCEVUDivExpr - This class represents a binary unsigned division operation.
250  ///
251  class SCEVUDivExpr : public SCEV {
252  friend class ScalarEvolution;
253 
254  const SCEV *LHS;
255  const SCEV *RHS;
256  SCEVUDivExpr(const FoldingSetNodeIDRef ID, const SCEV *lhs, const SCEV *rhs)
257  : SCEV(ID, scUDivExpr), LHS(lhs), RHS(rhs) {}
258 
259  public:
260  const SCEV *getLHS() const { return LHS; }
261  const SCEV *getRHS() const { return RHS; }
262 
263  Type *getType() const {
264  // In most cases the types of LHS and RHS will be the same, but in some
265  // crazy cases one or the other may be a pointer. ScalarEvolution doesn't
266  // depend on the type for correctness, but handling types carefully can
267  // avoid extra casts in the SCEVExpander. The LHS is more likely to be
268  // a pointer type than the RHS, so use the RHS' type here.
269  return getRHS()->getType();
270  }
271 
272  /// Methods for support type inquiry through isa, cast, and dyn_cast:
273  static inline bool classof(const SCEV *S) {
274  return S->getSCEVType() == scUDivExpr;
275  }
276  };
277 
278 
279  //===--------------------------------------------------------------------===//
280  /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
281  /// count of the specified loop. This is the primary focus of the
282  /// ScalarEvolution framework; all the other SCEV subclasses are mostly just
283  /// supporting infrastructure to allow SCEVAddRecExpr expressions to be
284  /// created and analyzed.
285  ///
286  /// All operands of an AddRec are required to be loop invariant.
287  ///
288  class SCEVAddRecExpr : public SCEVNAryExpr {
289  friend class ScalarEvolution;
290 
291  const Loop *L;
292 
294  const SCEV *const *O, size_t N, const Loop *l)
295  : SCEVNAryExpr(ID, scAddRecExpr, O, N), L(l) {}
296 
297  public:
298  const SCEV *getStart() const { return Operands[0]; }
299  const Loop *getLoop() const { return L; }
300 
301  /// getStepRecurrence - This method constructs and returns the recurrence
302  /// indicating how much this expression steps by. If this is a polynomial
303  /// of degree N, it returns a chrec of degree N-1.
304  /// We cannot determine whether the step recurrence has self-wraparound.
306  if (isAffine()) return getOperand(1);
308  op_end()),
309  getLoop(), FlagAnyWrap);
310  }
311 
312  /// isAffine - Return true if this represents an expression
313  /// A + B*x where A and B are loop invariant values.
314  bool isAffine() const {
315  // We know that the start value is invariant. This expression is thus
316  // affine iff the step is also invariant.
317  return getNumOperands() == 2;
318  }
319 
320  /// isQuadratic - Return true if this represents an expression
321  /// A + B*x + C*x^2 where A, B and C are loop invariant values.
322  /// This corresponds to an addrec of the form {L,+,M,+,N}
323  bool isQuadratic() const {
324  return getNumOperands() == 3;
325  }
326 
327  /// Set flags for a recurrence without clearing any previously set flags.
328  /// For AddRec, either NUW or NSW implies NW. Keep track of this fact here
329  /// to make it easier to propagate flags.
331  if (Flags & (FlagNUW | FlagNSW))
332  Flags = ScalarEvolution::setFlags(Flags, FlagNW);
333  SubclassData |= Flags;
334  }
335 
336  /// evaluateAtIteration - Return the value of this chain of recurrences at
337  /// the specified iteration number.
338  const SCEV *evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const;
339 
340  /// getNumIterationsInRange - Return the number of iterations of this loop
341  /// that produce values in the specified constant range. Another way of
342  /// looking at this is that it returns the first iteration number where the
343  /// value is not in the condition, thus computing the exit count. If the
344  /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
345  /// returned.
347  ScalarEvolution &SE) const;
348 
349  /// getPostIncExpr - Return an expression representing the value of
350  /// this expression one iteration of the loop ahead.
352  return cast<SCEVAddRecExpr>(SE.getAddExpr(this, getStepRecurrence(SE)));
353  }
354 
355  /// Methods for support type inquiry through isa, cast, and dyn_cast:
356  static inline bool classof(const SCEV *S) {
357  return S->getSCEVType() == scAddRecExpr;
358  }
359  };
360 
361  //===--------------------------------------------------------------------===//
362  /// SCEVSMaxExpr - This class represents a signed maximum selection.
363  ///
365  friend class ScalarEvolution;
366 
368  const SCEV *const *O, size_t N)
369  : SCEVCommutativeExpr(ID, scSMaxExpr, O, N) {
370  // Max never overflows.
372  }
373 
374  public:
375  /// Methods for support type inquiry through isa, cast, and dyn_cast:
376  static inline bool classof(const SCEV *S) {
377  return S->getSCEVType() == scSMaxExpr;
378  }
379  };
380 
381 
382  //===--------------------------------------------------------------------===//
383  /// SCEVUMaxExpr - This class represents an unsigned maximum selection.
384  ///
386  friend class ScalarEvolution;
387 
389  const SCEV *const *O, size_t N)
390  : SCEVCommutativeExpr(ID, scUMaxExpr, O, N) {
391  // Max never overflows.
393  }
394 
395  public:
396  /// Methods for support type inquiry through isa, cast, and dyn_cast:
397  static inline bool classof(const SCEV *S) {
398  return S->getSCEVType() == scUMaxExpr;
399  }
400  };
401 
402  //===--------------------------------------------------------------------===//
403  /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
404  /// value, and only represent it as its LLVM Value. This is the "bottom"
405  /// value for the analysis.
406  ///
407  class SCEVUnknown : public SCEV, private CallbackVH {
408  friend class ScalarEvolution;
409 
410  // Implement CallbackVH.
411  void deleted() override;
412  void allUsesReplacedWith(Value *New) override;
413 
414  /// SE - The parent ScalarEvolution value. This is used to update
415  /// the parent's maps when the value associated with a SCEVUnknown
416  /// is deleted or RAUW'd.
417  ScalarEvolution *SE;
418 
419  /// Next - The next pointer in the linked list of all
420  /// SCEVUnknown instances owned by a ScalarEvolution.
421  SCEVUnknown *Next;
422 
424  ScalarEvolution *se, SCEVUnknown *next) :
425  SCEV(ID, scUnknown), CallbackVH(V), SE(se), Next(next) {}
426 
427  public:
428  Value *getValue() const { return getValPtr(); }
429 
430  /// isSizeOf, isAlignOf, isOffsetOf - Test whether this is a special
431  /// constant representing a type size, alignment, or field offset in
432  /// a target-independent manner, and hasn't happened to have been
433  /// folded with other operations into something unrecognizable. This
434  /// is mainly only useful for pretty-printing and other situations
435  /// where it isn't absolutely required for these to succeed.
436  bool isSizeOf(Type *&AllocTy) const;
437  bool isAlignOf(Type *&AllocTy) const;
438  bool isOffsetOf(Type *&STy, Constant *&FieldNo) const;
439 
440  Type *getType() const { return getValPtr()->getType(); }
441 
442  /// Methods for support type inquiry through isa, cast, and dyn_cast:
443  static inline bool classof(const SCEV *S) {
444  return S->getSCEVType() == scUnknown;
445  }
446  };
447 
448  /// SCEVVisitor - This class defines a simple visitor class that may be used
449  /// for various SCEV analysis purposes.
450  template<typename SC, typename RetVal=void>
451  struct SCEVVisitor {
452  RetVal visit(const SCEV *S) {
453  switch (S->getSCEVType()) {
454  case scConstant:
455  return ((SC*)this)->visitConstant((const SCEVConstant*)S);
456  case scTruncate:
457  return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
458  case scZeroExtend:
459  return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
460  case scSignExtend:
461  return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
462  case scAddExpr:
463  return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
464  case scMulExpr:
465  return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
466  case scUDivExpr:
467  return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
468  case scAddRecExpr:
469  return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
470  case scSMaxExpr:
471  return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
472  case scUMaxExpr:
473  return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
474  case scUnknown:
475  return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
476  case scCouldNotCompute:
477  return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
478  default:
479  llvm_unreachable("Unknown SCEV type!");
480  }
481  }
482 
484  llvm_unreachable("Invalid use of SCEVCouldNotCompute!");
485  }
486  };
487 
488  /// Visit all nodes in the expression tree using worklist traversal.
489  ///
490  /// Visitor implements:
491  /// // return true to follow this node.
492  /// bool follow(const SCEV *S);
493  /// // return true to terminate the search.
494  /// bool isDone();
495  template<typename SV>
497  SV &Visitor;
500 
501  void push(const SCEV *S) {
502  if (Visited.insert(S).second && Visitor.follow(S))
503  Worklist.push_back(S);
504  }
505  public:
506  SCEVTraversal(SV& V): Visitor(V) {}
507 
508  void visitAll(const SCEV *Root) {
509  push(Root);
510  while (!Worklist.empty() && !Visitor.isDone()) {
511  const SCEV *S = Worklist.pop_back_val();
512 
513  switch (S->getSCEVType()) {
514  case scConstant:
515  case scUnknown:
516  break;
517  case scTruncate:
518  case scZeroExtend:
519  case scSignExtend:
520  push(cast<SCEVCastExpr>(S)->getOperand());
521  break;
522  case scAddExpr:
523  case scMulExpr:
524  case scSMaxExpr:
525  case scUMaxExpr:
526  case scAddRecExpr: {
527  const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S);
528  for (SCEVNAryExpr::op_iterator I = NAry->op_begin(),
529  E = NAry->op_end(); I != E; ++I) {
530  push(*I);
531  }
532  break;
533  }
534  case scUDivExpr: {
535  const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
536  push(UDiv->getLHS());
537  push(UDiv->getRHS());
538  break;
539  }
540  case scCouldNotCompute:
541  llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
542  default:
543  llvm_unreachable("Unknown SCEV kind!");
544  }
545  }
546  }
547  };
548 
549  /// Use SCEVTraversal to visit all nodes in the given expression tree.
550  template<typename SV>
551  void visitAll(const SCEV *Root, SV& Visitor) {
552  SCEVTraversal<SV> T(Visitor);
553  T.visitAll(Root);
554  }
555 
557 
558  /// The SCEVParameterRewriter takes a scalar evolution expression and updates
559  /// the SCEVUnknown components following the Map (Value -> Value).
561  : public SCEVVisitor<SCEVParameterRewriter, const SCEV*> {
562  public:
563  static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE,
564  ValueToValueMap &Map,
565  bool InterpretConsts = false) {
566  SCEVParameterRewriter Rewriter(SE, Map, InterpretConsts);
567  return Rewriter.visit(Scev);
568  }
569 
571  : SE(S), Map(M), InterpretConsts(C) {}
572 
574  return Constant;
575  }
576 
578  const SCEV *Operand = visit(Expr->getOperand());
579  return SE.getTruncateExpr(Operand, Expr->getType());
580  }
581 
583  const SCEV *Operand = visit(Expr->getOperand());
584  return SE.getZeroExtendExpr(Operand, Expr->getType());
585  }
586 
588  const SCEV *Operand = visit(Expr->getOperand());
589  return SE.getSignExtendExpr(Operand, Expr->getType());
590  }
591 
592  const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
594  for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
595  Operands.push_back(visit(Expr->getOperand(i)));
596  return SE.getAddExpr(Operands);
597  }
598 
599  const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
601  for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
602  Operands.push_back(visit(Expr->getOperand(i)));
603  return SE.getMulExpr(Operands);
604  }
605 
606  const SCEV *visitUDivExpr(const SCEVUDivExpr *Expr) {
607  return SE.getUDivExpr(visit(Expr->getLHS()), visit(Expr->getRHS()));
608  }
609 
610  const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
612  for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
613  Operands.push_back(visit(Expr->getOperand(i)));
614  return SE.getAddRecExpr(Operands, Expr->getLoop(),
615  Expr->getNoWrapFlags());
616  }
617 
618  const SCEV *visitSMaxExpr(const SCEVSMaxExpr *Expr) {
620  for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
621  Operands.push_back(visit(Expr->getOperand(i)));
622  return SE.getSMaxExpr(Operands);
623  }
624 
625  const SCEV *visitUMaxExpr(const SCEVUMaxExpr *Expr) {
627  for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
628  Operands.push_back(visit(Expr->getOperand(i)));
629  return SE.getUMaxExpr(Operands);
630  }
631 
632  const SCEV *visitUnknown(const SCEVUnknown *Expr) {
633  Value *V = Expr->getValue();
634  if (Map.count(V)) {
635  Value *NV = Map[V];
636  if (InterpretConsts && isa<ConstantInt>(NV))
637  return SE.getConstant(cast<ConstantInt>(NV));
638  return SE.getUnknown(NV);
639  }
640  return Expr;
641  }
642 
644  return Expr;
645  }
646 
647  private:
648  ScalarEvolution &SE;
649  ValueToValueMap &Map;
650  bool InterpretConsts;
651  };
652 
654 
655  /// The SCEVApplyRewriter takes a scalar evolution expression and applies
656  /// the Map (Loop -> SCEV) to all AddRecExprs.
658  : public SCEVVisitor<SCEVApplyRewriter, const SCEV*> {
659  public:
660  static const SCEV *rewrite(const SCEV *Scev, LoopToScevMapT &Map,
661  ScalarEvolution &SE) {
662  SCEVApplyRewriter Rewriter(SE, Map);
663  return Rewriter.visit(Scev);
664  }
665 
667  : SE(S), Map(M) {}
668 
670  return Constant;
671  }
672 
674  const SCEV *Operand = visit(Expr->getOperand());
675  return SE.getTruncateExpr(Operand, Expr->getType());
676  }
677 
679  const SCEV *Operand = visit(Expr->getOperand());
680  return SE.getZeroExtendExpr(Operand, Expr->getType());
681  }
682 
684  const SCEV *Operand = visit(Expr->getOperand());
685  return SE.getSignExtendExpr(Operand, Expr->getType());
686  }
687 
688  const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
690  for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
691  Operands.push_back(visit(Expr->getOperand(i)));
692  return SE.getAddExpr(Operands);
693  }
694 
695  const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
697  for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
698  Operands.push_back(visit(Expr->getOperand(i)));
699  return SE.getMulExpr(Operands);
700  }
701 
702  const SCEV *visitUDivExpr(const SCEVUDivExpr *Expr) {
703  return SE.getUDivExpr(visit(Expr->getLHS()), visit(Expr->getRHS()));
704  }
705 
706  const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
708  for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
709  Operands.push_back(visit(Expr->getOperand(i)));
710 
711  const Loop *L = Expr->getLoop();
712  const SCEV *Res = SE.getAddRecExpr(Operands, L, Expr->getNoWrapFlags());
713 
714  if (0 == Map.count(L))
715  return Res;
716 
717  const SCEVAddRecExpr *Rec = (const SCEVAddRecExpr *) Res;
718  return Rec->evaluateAtIteration(Map[L], SE);
719  }
720 
721  const SCEV *visitSMaxExpr(const SCEVSMaxExpr *Expr) {
723  for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
724  Operands.push_back(visit(Expr->getOperand(i)));
725  return SE.getSMaxExpr(Operands);
726  }
727 
728  const SCEV *visitUMaxExpr(const SCEVUMaxExpr *Expr) {
730  for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
731  Operands.push_back(visit(Expr->getOperand(i)));
732  return SE.getUMaxExpr(Operands);
733  }
734 
735  const SCEV *visitUnknown(const SCEVUnknown *Expr) {
736  return Expr;
737  }
738 
740  return Expr;
741  }
742 
743  private:
744  ScalarEvolution &SE;
745  LoopToScevMapT &Map;
746  };
747 
748 /// Applies the Map (Loop -> SCEV) to the given Scev.
749 static inline const SCEV *apply(const SCEV *Scev, LoopToScevMapT &Map,
750  ScalarEvolution &SE) {
751  return SCEVApplyRewriter::rewrite(Scev, Map, SE);
752 }
753 
754 }
755 
756 #endif
NoWrapFlags getNoWrapFlags(NoWrapFlags Mask=NoWrapMask) const
SCEVParameterRewriter(ScalarEvolution &S, ValueToValueMap &M, bool C)
const SCEV * visitMulExpr(const SCEVMulExpr *Expr)
const SCEV * evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const
evaluateAtIteration - Return the value of this chain of recurrences at the specified iteration number...
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
Definition: Constants.h:140
const SCEV * visitAddRecExpr(const SCEVAddRecExpr *Expr)
SCEVCastExpr(const FoldingSetNodeIDRef ID, unsigned SCEVTy, const SCEV *op, Type *ty)
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
const SCEV * getConstant(ConstantInt *V)
const SCEV * visitSignExtendExpr(const SCEVSignExtendExpr *Expr)
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
ScalarEvolution - This class is the main scalar evolution driver.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
getStepRecurrence - This method constructs and returns the recurrence indicating how much this expres...
DenseMap< const Loop *, const SCEV * > LoopToScevMapT
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
SCEVTruncateExpr - This class represents a truncation of an integer value to a smaller integer value...
const SCEV * visitUDivExpr(const SCEVUDivExpr *Expr)
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
const SCEV *const * Operands
const SCEV * visitUnknown(const SCEVUnknown *Expr)
SCEVCouldNotCompute - An object of this class is returned by queries that could not be answered...
#define op(i)
RetVal visit(const SCEV *S)
const SCEV * visitConstant(const SCEVConstant *Constant)
SCEVCastExpr - This is the base class for unary cast operator classes.
DenseMap< const Value *, Value * > ValueToValueMap
bool isOffsetOf(Type *&STy, Constant *&FieldNo) const
iterator_range< op_iterator > op_range
const SCEV * getStart() const
The SCEVParameterRewriter takes a scalar evolution expression and updates the SCEVUnknown components ...
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
static const SCEV * apply(const SCEV *Scev, LoopToScevMapT &Map, ScalarEvolution &SE)
Applies the Map (Loop -> SCEV) to the given Scev.
const SCEV * visitSMaxExpr(const SCEVSMaxExpr *Expr)
static const SCEV * rewrite(const SCEV *Scev, LoopToScevMapT &Map, ScalarEvolution &SE)
bool isAlignOf(Type *&AllocTy) const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
const SCEV *const * op_iterator
unsigned short SubclassData
SubclassData - This field is initialized to zero and may be used in subclasses to store miscellaneous...
SCEVCommutativeExpr - This node is the base class for n'ary commutative operators.
op_iterator op_begin() const
SCEVMulExpr - This node represents multiplication of some number of SCEVs.
void setNoWrapFlags(NoWrapFlags Flags)
Set flags for a non-recurrence without clearing previously set flags.
const SCEV * visitCouldNotCompute(const SCEVCouldNotCompute *Expr)
SCEVAddRecExpr - This node represents a polynomial recurrence on the trip count of the specified loop...
#define T
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
const SCEV * getAddRecExpr(const SCEV *Start, const SCEV *Step, const Loop *L, SCEV::NoWrapFlags Flags)
getAddRecExpr - Get an add recurrence expression for the specified loop.
const SCEV * visitUDivExpr(const SCEVUDivExpr *Expr)
const SCEV * getNumIterationsInRange(ConstantRange Range, ScalarEvolution &SE) const
getNumIterationsInRange - Return the number of iterations of this loop that produce values in the spe...
Visit all nodes in the expression tree using worklist traversal.
SCEVUnknown - This means that we are dealing with an entirely unknown SCEV value, and only represent ...
bool isAffine() const
isAffine - Return true if this represents an expression A + B*x where A and B are loop invariant valu...
SCEVVisitor - This class defines a simple visitor class that may be used for various SCEV analysis pu...
bool isSizeOf(Type *&AllocTy) const
isSizeOf, isAlignOf, isOffsetOf - Test whether this is a special constant representing a type size...
Value * getValPtr() const
Definition: ValueHandle.h:100
static const SCEV * rewrite(const SCEV *Scev, ScalarEvolution &SE, ValueToValueMap &Map, bool InterpretConsts=false)
SCEVUDivExpr - This class represents a binary unsigned division operation.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
Type * getType() const
getType - Return the LLVM type of this SCEV expression.
This is an important base class in LLVM.
Definition: Constant.h:41
const SCEV * getOperand(unsigned i) const
void visitAll(const SCEV *Root)
const SCEV * getSMaxExpr(const SCEV *LHS, const SCEV *RHS)
const SCEVAddRecExpr * getPostIncExpr(ScalarEvolution &SE) const
getPostIncExpr - Return an expression representing the value of this expression one iteration of the ...
SI Fold Operands
void setNoWrapFlags(NoWrapFlags Flags)
Set flags for a recurrence without clearing any previously set flags.
const SCEV * visitAddExpr(const SCEVAddExpr *Expr)
static SCEV::NoWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT setFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OnFlags)
const SCEV * getLHS() const
const SCEV * visitMulExpr(const SCEVMulExpr *Expr)
const SCEV * getRHS() const
SCEVApplyRewriter(ScalarEvolution &S, LoopToScevMapT &M)
RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
const SCEV * visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
This is the shared class of boolean and integer constants.
Definition: Constants.h:47
const SCEV * getTruncateExpr(const SCEV *Op, Type *Ty)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
const SCEV * visitUMaxExpr(const SCEVUMaxExpr *Expr)
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
This class represents a range of values.
Definition: ConstantRange.h:43
const SCEV * visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr)
CHAIN = SC CHAIN, Imm128 - System call.
const SCEV * visitSMaxExpr(const SCEVSMaxExpr *Expr)
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
ConstantInt * getValue() const
const SCEV * getUMaxExpr(const SCEV *LHS, const SCEV *RHS)
size_type count(const KeyT &Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:119
A range adaptor for a pair of iterators.
SCEVAddExpr - This node represents an addition of some number of SCEVs.
const SCEV * getSignExtendExpr(const SCEV *Op, Type *Ty)
const SCEV * visitTruncateExpr(const SCEVTruncateExpr *Expr)
SCEVSMaxExpr - This class represents a signed maximum selection.
const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
getAddExpr - Get a canonical add expression, or something simpler if possible.
void visitAll(const SCEV *Root, SV &Visitor)
Use SCEVTraversal to visit all nodes in the given expression tree.
const SCEV * visitAddExpr(const SCEVAddExpr *Expr)
SCEVZeroExtendExpr - This class represents a zero extension of a small integer value to a larger inte...
Virtual Register Rewriter
Definition: VirtRegMap.cpp:190
const SCEV * visitUnknown(const SCEVUnknown *Expr)
const SCEV * visitAddRecExpr(const SCEVAddRecExpr *Expr)
FoldingSetNodeIDRef - This class describes a reference to an interned FoldingSetNodeID, which can be a useful to store node id data rather than using plain FoldingSetNodeIDs, since the 32-element SmallVector is often much larger than necessary, and the possibility of heap allocation means it requires a non-trivial destructor call.
Definition: FoldingSet.h:269
SCEV - This class represents an analyzed expression in the program.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
const SCEV * visitSignExtendExpr(const SCEVSignExtendExpr *Expr)
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
SCEVSignExtendExpr - This class represents a sign extension of a small integer value to a larger inte...
SCEVUMaxExpr - This class represents an unsigned maximum selection.
const Loop * getLoop() const
The SCEVApplyRewriter takes a scalar evolution expression and applies the Map (Loop -> SCEV) to all A...
const SCEV * visitConstant(const SCEVConstant *Constant)
unsigned getSCEVType() const
SCEVNAryExpr(const FoldingSetNodeIDRef ID, enum SCEVTypes T, const SCEV *const *O, size_t N)
LLVM Value Representation.
Definition: Value.h:69
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
const SCEV * getUDivExpr(const SCEV *LHS, const SCEV *RHS)
getUDivExpr - Get a canonical unsigned division expression, or something simpler if possible...
const SCEV * getUnknown(Value *V)
SCEVCommutativeExpr(const FoldingSetNodeIDRef ID, enum SCEVTypes T, const SCEV *const *O, size_t N)
op_iterator op_end() const
Value handle with callbacks on RAUW and destruction.
Definition: ValueHandle.h:344
const SCEV * visitTruncateExpr(const SCEVTruncateExpr *Expr)
const SCEV * getZeroExtendExpr(const SCEV *Op, Type *Ty)
SCEVNAryExpr - This node is a base class providing common functionality for n'ary operators...
NoWrapFlags
NoWrapFlags are bitfield indices into SubclassData.
const SCEV * getOperand() const
const SCEV * getMulExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
getMulExpr - Get a canonical multiply expression, or something simpler if possible.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
const SCEV * visitUMaxExpr(const SCEVUMaxExpr *Expr)
const SCEV * visitCouldNotCompute(const SCEVCouldNotCompute *Expr)
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
bool isQuadratic() const
isQuadratic - Return true if this represents an expression A + B*x + C*x^2 where A, B and C are loop invariant values.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
SCEVConstant - This class represents a constant integer value.