blob: 2aaa4c1ae117f7c63126fac9d4dc552c23c2bd36 [file] [log] [blame]
Nick Lewyckyaf508372016-04-24 17:55:41 +00001//===- ScalarEvolutionNormalization.cpp - See below -----------------------===//
Dan Gohmand006ab92010-04-07 22:27:08 +00002//
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
Dan Gohmand006ab92010-04-07 22:27:08 +000015#include "llvm/Analysis/LoopInfo.h"
16#include "llvm/Analysis/ScalarEvolutionExpressions.h"
17#include "llvm/Analysis/ScalarEvolutionNormalization.h"
18using namespace llvm;
19
Sanjoy Dase3a15e82017-04-14 15:49:59 +000020/// TransformKind - Different types of transformations that
21/// TransformForPostIncUse can do.
22enum 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 Das3470e142017-04-14 17:42:10 +000030namespace {
31struct NormalizeDenormalizeRewriter
32 : public SCEVRewriteVisitor<NormalizeDenormalizeRewriter> {
33 const TransformKind Kind;
Andrew Trick1393ec22011-10-13 17:21:09 +000034
Sanjoy Das3470e142017-04-14 17:42:10 +000035 // 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 Trick1393ec22011-10-13 17:21:09 +000038
Sanjoy Das3470e142017-04-14 17:42:10 +000039 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 Gohmand1488fd2010-07-20 16:32:11 +000046
Sanjoy Das3470e142017-04-14 17:42:10 +000047const SCEV *
48NormalizeDenormalizeRewriter::visitAddRecExpr(const SCEVAddRecExpr *AR) {
49 SmallVector<const SCEV *, 8> Operands;
Sanjoy Das62f4b6b2017-04-14 01:33:13 +000050
Sanjoy Das3470e142017-04-14 17:42:10 +000051 transform(AR->operands(), std::back_inserter(Operands),
52 [&](const SCEV *Op) { return visit(Op); });
Sanjoy Das62f4b6b2017-04-14 01:33:13 +000053
Sanjoy Das3470e142017-04-14 17:42:10 +000054 // Conservatively use AnyWrap until/unless we need FlagNW.
55 const SCEV *Result =
56 SE.getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagAnyWrap);
57 switch (Kind) {
58 case Normalize:
59 // We want to normalize step expression, because otherwise we might not be
60 // able to denormalize to the original expression.
61 //
62 // Here is an example what will happen if we don't normalize step:
63 // ORIGINAL ISE:
64 // {(100 /u {1,+,1}<%bb16>),+,(100 /u {1,+,1}<%bb16>)}<%bb25>
65 // NORMALIZED ISE:
66 // {((-1 * (100 /u {1,+,1}<%bb16>)) + (100 /u {0,+,1}<%bb16>)),+,
67 // (100 /u {0,+,1}<%bb16>)}<%bb25>
68 // DENORMALIZED BACK ISE:
69 // {((2 * (100 /u {1,+,1}<%bb16>)) + (-1 * (100 /u {2,+,1}<%bb16>))),+,
70 // (100 /u {1,+,1}<%bb16>)}<%bb25>
71 // Note that the initial value changes after normalization +
72 // denormalization, which isn't correct.
73 if (Pred(AR)) {
74 const SCEV *TransformedStep = visit(AR->getStepRecurrence(SE));
75 Result = SE.getMinusSCEV(Result, TransformedStep);
Dan Gohman625fd222010-07-20 17:06:20 +000076 }
Sanjoy Das3470e142017-04-14 17:42:10 +000077 break;
78 case Denormalize:
79 // Here we want to normalize step expressions for the same reasons, as
80 // stated above.
81 if (Pred(AR)) {
82 const SCEV *TransformedStep = visit(AR->getStepRecurrence(SE));
83 Result = SE.getAddExpr(Result, TransformedStep);
Dan Gohmand006ab92010-04-07 22:27:08 +000084 }
Sanjoy Das3470e142017-04-14 17:42:10 +000085 break;
Dan Gohmand006ab92010-04-07 22:27:08 +000086 }
Andrew Trick7e442562011-10-13 18:49:23 +000087 return Result;
Andrew Trick1393ec22011-10-13 17:21:09 +000088}
89
Sanjoy Dase3a15e82017-04-14 15:49:59 +000090const SCEV *llvm::normalizeForPostIncUse(const SCEV *S,
91 const PostIncLoopSet &Loops,
92 ScalarEvolution &SE) {
Sanjoy Dasc5a87a12017-04-14 15:50:07 +000093 auto Pred = [&](const SCEVAddRecExpr *AR) {
94 return Loops.count(AR->getLoop());
95 };
Sanjoy Das3470e142017-04-14 17:42:10 +000096 return NormalizeDenormalizeRewriter(Normalize, Pred, SE).visit(S);
Sanjoy Dase3a15e82017-04-14 15:49:59 +000097}
98
99const SCEV *llvm::normalizeForPostIncUseIf(const SCEV *S, NormalizePredTy Pred,
100 ScalarEvolution &SE) {
Sanjoy Das3470e142017-04-14 17:42:10 +0000101 return NormalizeDenormalizeRewriter(Normalize, Pred, SE).visit(S);
Sanjoy Dase3a15e82017-04-14 15:49:59 +0000102}
103
104const SCEV *llvm::denormalizeForPostIncUse(const SCEV *S,
105 const PostIncLoopSet &Loops,
106 ScalarEvolution &SE) {
Sanjoy Dasc5a87a12017-04-14 15:50:07 +0000107 auto Pred = [&](const SCEVAddRecExpr *AR) {
108 return Loops.count(AR->getLoop());
109 };
Sanjoy Das3470e142017-04-14 17:42:10 +0000110 return NormalizeDenormalizeRewriter(Denormalize, Pred, SE).visit(S);
Sanjoy Dase3a15e82017-04-14 15:49:59 +0000111}