Mercurial > hg > CbC > CbC_llvm
comparison include/llvm/Analysis/ScalarEvolutionExpressions.h @ 77:54457678186b LLVM3.6
LLVM 3.6
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 08 Sep 2014 22:06:00 +0900 |
parents | 95c75e76d11b |
children | 60c9769439b8 |
comparison
equal
deleted
inserted
replaced
34:e874dbf0ad9d | 77:54457678186b |
---|---|
12 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
13 | 13 |
14 #ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H | 14 #ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H |
15 #define LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H | 15 #define LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H |
16 | 16 |
17 #include "llvm/ADT/iterator_range.h" | |
17 #include "llvm/ADT/SmallPtrSet.h" | 18 #include "llvm/ADT/SmallPtrSet.h" |
18 #include "llvm/Analysis/ScalarEvolution.h" | 19 #include "llvm/Analysis/ScalarEvolution.h" |
19 #include "llvm/Support/ErrorHandling.h" | 20 #include "llvm/Support/ErrorHandling.h" |
20 | 21 |
21 namespace llvm { | 22 namespace llvm { |
149 assert(i < NumOperands && "Operand index out of range!"); | 150 assert(i < NumOperands && "Operand index out of range!"); |
150 return Operands[i]; | 151 return Operands[i]; |
151 } | 152 } |
152 | 153 |
153 typedef const SCEV *const *op_iterator; | 154 typedef const SCEV *const *op_iterator; |
155 typedef iterator_range<op_iterator> op_range; | |
154 op_iterator op_begin() const { return Operands; } | 156 op_iterator op_begin() const { return Operands; } |
155 op_iterator op_end() const { return Operands + NumOperands; } | 157 op_iterator op_end() const { return Operands + NumOperands; } |
158 op_range operands() const { | |
159 return make_range(op_begin(), op_end()); | |
160 } | |
156 | 161 |
157 Type *getType() const { return getOperand(0)->getType(); } | 162 Type *getType() const { return getOperand(0)->getType(); } |
158 | 163 |
159 NoWrapFlags getNoWrapFlags(NoWrapFlags Mask = NoWrapMask) const { | 164 NoWrapFlags getNoWrapFlags(NoWrapFlags Mask = NoWrapMask) const { |
160 return (NoWrapFlags)(SubclassData & Mask); | 165 return (NoWrapFlags)(SubclassData & Mask); |
302 return SE.getAddRecExpr(SmallVector<const SCEV *, 3>(op_begin()+1, | 307 return SE.getAddRecExpr(SmallVector<const SCEV *, 3>(op_begin()+1, |
303 op_end()), | 308 op_end()), |
304 getLoop(), FlagAnyWrap); | 309 getLoop(), FlagAnyWrap); |
305 } | 310 } |
306 | 311 |
307 /// isAffine - Return true if this is an affine AddRec (i.e., it represents | 312 /// isAffine - Return true if this represents an expression |
308 /// an expressions A+B*x where A and B are loop invariant values. | 313 /// A + B*x where A and B are loop invariant values. |
309 bool isAffine() const { | 314 bool isAffine() const { |
310 // We know that the start value is invariant. This expression is thus | 315 // We know that the start value is invariant. This expression is thus |
311 // affine iff the step is also invariant. | 316 // affine iff the step is also invariant. |
312 return getNumOperands() == 2; | 317 return getNumOperands() == 2; |
313 } | 318 } |
314 | 319 |
315 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it | 320 /// isQuadratic - Return true if this represents an expression |
316 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop | 321 /// A + B*x + C*x^2 where A, B and C are loop invariant values. |
317 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N} | 322 /// This corresponds to an addrec of the form {L,+,M,+,N} |
318 bool isQuadratic() const { | 323 bool isQuadratic() const { |
319 return getNumOperands() == 3; | 324 return getNumOperands() == 3; |
320 } | 325 } |
321 | 326 |
322 /// Set flags for a recurrence without clearing any previously set flags. | 327 /// Set flags for a recurrence without clearing any previously set flags. |
350 /// Methods for support type inquiry through isa, cast, and dyn_cast: | 355 /// Methods for support type inquiry through isa, cast, and dyn_cast: |
351 static inline bool classof(const SCEV *S) { | 356 static inline bool classof(const SCEV *S) { |
352 return S->getSCEVType() == scAddRecExpr; | 357 return S->getSCEVType() == scAddRecExpr; |
353 } | 358 } |
354 | 359 |
355 /// Splits the SCEV into two vectors of SCEVs representing the subscripts | 360 /// Collect parametric terms occurring in step expressions. |
356 /// and sizes of an array access. Returns the remainder of the | 361 void collectParametricTerms(ScalarEvolution &SE, |
357 /// delinearization that is the offset start of the array. | 362 SmallVectorImpl<const SCEV *> &Terms) const; |
358 const SCEV *delinearize(ScalarEvolution &SE, | 363 |
359 SmallVectorImpl<const SCEV *> &Subscripts, | 364 /// Return in Subscripts the access functions for each dimension in Sizes. |
360 SmallVectorImpl<const SCEV *> &Sizes) const; | 365 void computeAccessFunctions(ScalarEvolution &SE, |
366 SmallVectorImpl<const SCEV *> &Subscripts, | |
367 SmallVectorImpl<const SCEV *> &Sizes) const; | |
368 | |
369 /// Split this SCEVAddRecExpr into two vectors of SCEVs representing the | |
370 /// subscripts and sizes of an array access. | |
371 /// | |
372 /// The delinearization is a 3 step process: the first two steps compute the | |
373 /// sizes of each subscript and the third step computes the access functions | |
374 /// for the delinearized array: | |
375 /// | |
376 /// 1. Find the terms in the step functions | |
377 /// 2. Compute the array size | |
378 /// 3. Compute the access function: divide the SCEV by the array size | |
379 /// starting with the innermost dimensions found in step 2. The Quotient | |
380 /// is the SCEV to be divided in the next step of the recursion. The | |
381 /// Remainder is the subscript of the innermost dimension. Loop over all | |
382 /// array dimensions computed in step 2. | |
383 /// | |
384 /// To compute a uniform array size for several memory accesses to the same | |
385 /// object, one can collect in step 1 all the step terms for all the memory | |
386 /// accesses, and compute in step 2 a unique array shape. This guarantees | |
387 /// that the array shape will be the same across all memory accesses. | |
388 /// | |
389 /// FIXME: We could derive the result of steps 1 and 2 from a description of | |
390 /// the array shape given in metadata. | |
391 /// | |
392 /// Example: | |
393 /// | |
394 /// A[][n][m] | |
395 /// | |
396 /// for i | |
397 /// for j | |
398 /// for k | |
399 /// A[j+k][2i][5i] = | |
400 /// | |
401 /// The initial SCEV: | |
402 /// | |
403 /// A[{{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k] | |
404 /// | |
405 /// 1. Find the different terms in the step functions: | |
406 /// -> [2*m, 5, n*m, n*m] | |
407 /// | |
408 /// 2. Compute the array size: sort and unique them | |
409 /// -> [n*m, 2*m, 5] | |
410 /// find the GCD of all the terms = 1 | |
411 /// divide by the GCD and erase constant terms | |
412 /// -> [n*m, 2*m] | |
413 /// GCD = m | |
414 /// divide by GCD -> [n, 2] | |
415 /// remove constant terms | |
416 /// -> [n] | |
417 /// size of the array is A[unknown][n][m] | |
418 /// | |
419 /// 3. Compute the access function | |
420 /// a. Divide {{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k by the innermost size m | |
421 /// Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k | |
422 /// Remainder: {{{0,+,5}_i, +, 0}_j, +, 0}_k | |
423 /// The remainder is the subscript of the innermost array dimension: [5i]. | |
424 /// | |
425 /// b. Divide Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k by next outer size n | |
426 /// Quotient: {{{0,+,0}_i, +, 1}_j, +, 1}_k | |
427 /// Remainder: {{{0,+,2}_i, +, 0}_j, +, 0}_k | |
428 /// The Remainder is the subscript of the next array dimension: [2i]. | |
429 /// | |
430 /// The subscript of the outermost dimension is the Quotient: [j+k]. | |
431 /// | |
432 /// Overall, we have: A[][n][m], and the access function: A[j+k][2i][5i]. | |
433 void delinearize(ScalarEvolution &SE, | |
434 SmallVectorImpl<const SCEV *> &Subscripts, | |
435 SmallVectorImpl<const SCEV *> &Sizes, | |
436 const SCEV *ElementSize) const; | |
361 }; | 437 }; |
362 | 438 |
363 //===--------------------------------------------------------------------===// | 439 //===--------------------------------------------------------------------===// |
364 /// SCEVSMaxExpr - This class represents a signed maximum selection. | 440 /// SCEVSMaxExpr - This class represents a signed maximum selection. |
365 /// | 441 /// |
408 /// | 484 /// |
409 class SCEVUnknown : public SCEV, private CallbackVH { | 485 class SCEVUnknown : public SCEV, private CallbackVH { |
410 friend class ScalarEvolution; | 486 friend class ScalarEvolution; |
411 | 487 |
412 // Implement CallbackVH. | 488 // Implement CallbackVH. |
413 virtual void deleted(); | 489 void deleted() override; |
414 virtual void allUsesReplacedWith(Value *New); | 490 void allUsesReplacedWith(Value *New) override; |
415 | 491 |
416 /// SE - The parent ScalarEvolution value. This is used to update | 492 /// SE - The parent ScalarEvolution value. This is used to update |
417 /// the parent's maps when the value associated with a SCEVUnknown | 493 /// the parent's maps when the value associated with a SCEVUnknown |
418 /// is deleted or RAUW'd. | 494 /// is deleted or RAUW'd. |
419 ScalarEvolution *SE; | 495 ScalarEvolution *SE; |
561 /// the SCEVUnknown components following the Map (Value -> Value). | 637 /// the SCEVUnknown components following the Map (Value -> Value). |
562 struct SCEVParameterRewriter | 638 struct SCEVParameterRewriter |
563 : public SCEVVisitor<SCEVParameterRewriter, const SCEV*> { | 639 : public SCEVVisitor<SCEVParameterRewriter, const SCEV*> { |
564 public: | 640 public: |
565 static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE, | 641 static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE, |
566 ValueToValueMap &Map) { | 642 ValueToValueMap &Map, |
567 SCEVParameterRewriter Rewriter(SE, Map); | 643 bool InterpretConsts = false) { |
644 SCEVParameterRewriter Rewriter(SE, Map, InterpretConsts); | |
568 return Rewriter.visit(Scev); | 645 return Rewriter.visit(Scev); |
569 } | 646 } |
570 | 647 |
571 SCEVParameterRewriter(ScalarEvolution &S, ValueToValueMap &M) | 648 SCEVParameterRewriter(ScalarEvolution &S, ValueToValueMap &M, bool C) |
572 : SE(S), Map(M) {} | 649 : SE(S), Map(M), InterpretConsts(C) {} |
573 | 650 |
574 const SCEV *visitConstant(const SCEVConstant *Constant) { | 651 const SCEV *visitConstant(const SCEVConstant *Constant) { |
575 return Constant; | 652 return Constant; |
576 } | 653 } |
577 | 654 |
630 return SE.getUMaxExpr(Operands); | 707 return SE.getUMaxExpr(Operands); |
631 } | 708 } |
632 | 709 |
633 const SCEV *visitUnknown(const SCEVUnknown *Expr) { | 710 const SCEV *visitUnknown(const SCEVUnknown *Expr) { |
634 Value *V = Expr->getValue(); | 711 Value *V = Expr->getValue(); |
635 if (Map.count(V)) | 712 if (Map.count(V)) { |
636 return SE.getUnknown(Map[V]); | 713 Value *NV = Map[V]; |
714 if (InterpretConsts && isa<ConstantInt>(NV)) | |
715 return SE.getConstant(cast<ConstantInt>(NV)); | |
716 return SE.getUnknown(NV); | |
717 } | |
637 return Expr; | 718 return Expr; |
638 } | 719 } |
639 | 720 |
640 const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) { | 721 const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) { |
641 return Expr; | 722 return Expr; |
642 } | 723 } |
643 | 724 |
644 private: | 725 private: |
645 ScalarEvolution &SE; | 726 ScalarEvolution &SE; |
646 ValueToValueMap ⤅ | 727 ValueToValueMap ⤅ |
728 bool InterpretConsts; | |
647 }; | 729 }; |
648 | 730 |
649 typedef DenseMap<const Loop*, const SCEV*> LoopToScevMapT; | 731 typedef DenseMap<const Loop*, const SCEV*> LoopToScevMapT; |
650 | 732 |
651 /// The SCEVApplyRewriter takes a scalar evolution expression and applies | 733 /// The SCEVApplyRewriter takes a scalar evolution expression and applies |