Nick Lewycky | af50837 | 2016-04-24 17:55:41 +0000 | [diff] [blame] | 1 | //===- ScalarEvolutionNormalization.cpp - See below -----------------------===// |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 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 implements utilities for working with "normalized" expressions. |
| 11 | // See the comments at the top of ScalarEvolutionNormalization.h for details. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/ScalarEvolutionNormalization.h" |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/LoopInfo.h" |
| 17 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | |
Sanjoy Das | e3a15e8 | 2017-04-14 15:49:59 +0000 | [diff] [blame] | 20 | /// TransformKind - Different types of transformations that |
| 21 | /// TransformForPostIncUse can do. |
| 22 | enum TransformKind { |
| 23 | /// Normalize - Normalize according to the given loops. |
| 24 | Normalize, |
| 25 | /// Denormalize - Perform the inverse transform on the expression with the |
| 26 | /// given loop set. |
| 27 | Denormalize |
| 28 | }; |
| 29 | |
Sanjoy Das | 3470e14 | 2017-04-14 17:42:10 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | struct NormalizeDenormalizeRewriter |
| 32 | : public SCEVRewriteVisitor<NormalizeDenormalizeRewriter> { |
| 33 | const TransformKind Kind; |
Andrew Trick | 1393ec2 | 2011-10-13 17:21:09 +0000 | [diff] [blame] | 34 | |
Sanjoy Das | 3470e14 | 2017-04-14 17:42:10 +0000 | [diff] [blame] | 35 | // NB! Pred is a function_ref. Storing it here is okay only because |
| 36 | // we're careful about the lifetime of NormalizeDenormalizeRewriter. |
| 37 | const NormalizePredTy Pred; |
Andrew Trick | 1393ec2 | 2011-10-13 17:21:09 +0000 | [diff] [blame] | 38 | |
Sanjoy Das | 3470e14 | 2017-04-14 17:42:10 +0000 | [diff] [blame] | 39 | NormalizeDenormalizeRewriter(TransformKind Kind, NormalizePredTy Pred, |
| 40 | ScalarEvolution &SE) |
| 41 | : SCEVRewriteVisitor<NormalizeDenormalizeRewriter>(SE), Kind(Kind), |
| 42 | Pred(Pred) {} |
| 43 | const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr); |
| 44 | }; |
| 45 | } // namespace |
Dan Gohman | d1488fd | 2010-07-20 16:32:11 +0000 | [diff] [blame] | 46 | |
Sanjoy Das | 3470e14 | 2017-04-14 17:42:10 +0000 | [diff] [blame] | 47 | const SCEV * |
| 48 | NormalizeDenormalizeRewriter::visitAddRecExpr(const SCEVAddRecExpr *AR) { |
| 49 | SmallVector<const SCEV *, 8> Operands; |
Sanjoy Das | 62f4b6b | 2017-04-14 01:33:13 +0000 | [diff] [blame] | 50 | |
Sanjoy Das | 3470e14 | 2017-04-14 17:42:10 +0000 | [diff] [blame] | 51 | transform(AR->operands(), std::back_inserter(Operands), |
| 52 | [&](const SCEV *Op) { return visit(Op); }); |
Sanjoy Das | 62f4b6b | 2017-04-14 01:33:13 +0000 | [diff] [blame] | 53 | |
Sanjoy Das | bbebcb6 | 2017-04-25 00:09:19 +0000 | [diff] [blame] | 54 | if (!Pred(AR)) |
| 55 | return SE.getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagAnyWrap); |
| 56 | |
| 57 | // Normalization and denormalization are fancy names for decrementing and |
| 58 | // incrementing a SCEV expression with respect to a set of loops. Since |
| 59 | // Pred(AR) has returned true, we know we need to normalize or denormalize AR |
| 60 | // with respect to its loop. |
| 61 | |
| 62 | if (Kind == Denormalize) { |
| 63 | // Denormalization / "partial increment" is essentially the same as \c |
| 64 | // SCEVAddRecExpr::getPostIncExpr. Here we use an explicit loop to make the |
| 65 | // symmetry with Normalization clear. |
| 66 | for (int i = 0, e = Operands.size() - 1; i < e; i++) |
| 67 | Operands[i] = SE.getAddExpr(Operands[i], Operands[i + 1]); |
| 68 | } else { |
| 69 | assert(Kind == Normalize && "Only two possibilities!"); |
| 70 | |
| 71 | // Normalization / "partial decrement" is a bit more subtle. Since |
| 72 | // incrementing a SCEV expression (in general) changes the step of the SCEV |
| 73 | // expression as well, we cannot use the step of the current expression. |
| 74 | // Instead, we have to use the step of the very expression we're trying to |
| 75 | // compute! |
Sanjoy Das | 3470e14 | 2017-04-14 17:42:10 +0000 | [diff] [blame] | 76 | // |
Sanjoy Das | bbebcb6 | 2017-04-25 00:09:19 +0000 | [diff] [blame] | 77 | // We solve the issue by recursively building up the result, starting from |
| 78 | // the "least significant" operand in the add recurrence: |
| 79 | // |
| 80 | // Base case: |
| 81 | // Single operand add recurrence. It's its own normalization. |
| 82 | // |
| 83 | // N-operand case: |
| 84 | // {S_{N-1},+,S_{N-2},+,...,+,S_0} = S |
| 85 | // |
| 86 | // Since the step recurrence of S is {S_{N-2},+,...,+,S_0}, we know its |
| 87 | // normalization by induction. We subtract the normalized step |
| 88 | // recurrence from S_{N-1} to get the normalization of S. |
| 89 | |
| 90 | for (int i = Operands.size() - 2; i >= 0; i--) |
| 91 | Operands[i] = SE.getMinusSCEV(Operands[i], Operands[i + 1]); |
Dan Gohman | d006ab9 | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 92 | } |
Sanjoy Das | bbebcb6 | 2017-04-25 00:09:19 +0000 | [diff] [blame] | 93 | |
| 94 | return SE.getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagAnyWrap); |
Andrew Trick | 1393ec2 | 2011-10-13 17:21:09 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Sanjoy Das | e3a15e8 | 2017-04-14 15:49:59 +0000 | [diff] [blame] | 97 | const SCEV *llvm::normalizeForPostIncUse(const SCEV *S, |
| 98 | const PostIncLoopSet &Loops, |
| 99 | ScalarEvolution &SE) { |
Sanjoy Das | c5a87a1 | 2017-04-14 15:50:07 +0000 | [diff] [blame] | 100 | auto Pred = [&](const SCEVAddRecExpr *AR) { |
| 101 | return Loops.count(AR->getLoop()); |
| 102 | }; |
Sanjoy Das | 3470e14 | 2017-04-14 17:42:10 +0000 | [diff] [blame] | 103 | return NormalizeDenormalizeRewriter(Normalize, Pred, SE).visit(S); |
Sanjoy Das | e3a15e8 | 2017-04-14 15:49:59 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | const SCEV *llvm::normalizeForPostIncUseIf(const SCEV *S, NormalizePredTy Pred, |
| 107 | ScalarEvolution &SE) { |
Sanjoy Das | 3470e14 | 2017-04-14 17:42:10 +0000 | [diff] [blame] | 108 | return NormalizeDenormalizeRewriter(Normalize, Pred, SE).visit(S); |
Sanjoy Das | e3a15e8 | 2017-04-14 15:49:59 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | const SCEV *llvm::denormalizeForPostIncUse(const SCEV *S, |
| 112 | const PostIncLoopSet &Loops, |
| 113 | ScalarEvolution &SE) { |
Sanjoy Das | c5a87a1 | 2017-04-14 15:50:07 +0000 | [diff] [blame] | 114 | auto Pred = [&](const SCEVAddRecExpr *AR) { |
| 115 | return Loops.count(AR->getLoop()); |
| 116 | }; |
Sanjoy Das | 3470e14 | 2017-04-14 17:42:10 +0000 | [diff] [blame] | 117 | return NormalizeDenormalizeRewriter(Denormalize, Pred, SE).visit(S); |
Sanjoy Das | e3a15e8 | 2017-04-14 15:49:59 +0000 | [diff] [blame] | 118 | } |