ScalarEvolutionNormalization.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //===- ScalarEvolutionNormalization.cpp - See below -----------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements utilities for working with "normalized" expressions.
  10. // See the comments at the top of ScalarEvolutionNormalization.h for details.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/ScalarEvolutionNormalization.h"
  14. #include "llvm/Analysis/LoopInfo.h"
  15. #include "llvm/Analysis/ScalarEvolutionExpressions.h"
  16. using namespace llvm;
  17. /// TransformKind - Different types of transformations that
  18. /// TransformForPostIncUse can do.
  19. enum TransformKind {
  20. /// Normalize - Normalize according to the given loops.
  21. Normalize,
  22. /// Denormalize - Perform the inverse transform on the expression with the
  23. /// given loop set.
  24. Denormalize
  25. };
  26. namespace {
  27. struct NormalizeDenormalizeRewriter
  28. : public SCEVRewriteVisitor<NormalizeDenormalizeRewriter> {
  29. const TransformKind Kind;
  30. // NB! Pred is a function_ref. Storing it here is okay only because
  31. // we're careful about the lifetime of NormalizeDenormalizeRewriter.
  32. const NormalizePredTy Pred;
  33. NormalizeDenormalizeRewriter(TransformKind Kind, NormalizePredTy Pred,
  34. ScalarEvolution &SE)
  35. : SCEVRewriteVisitor<NormalizeDenormalizeRewriter>(SE), Kind(Kind),
  36. Pred(Pred) {}
  37. const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr);
  38. };
  39. } // namespace
  40. const SCEV *
  41. NormalizeDenormalizeRewriter::visitAddRecExpr(const SCEVAddRecExpr *AR) {
  42. SmallVector<const SCEV *, 8> Operands;
  43. transform(AR->operands(), std::back_inserter(Operands),
  44. [&](const SCEV *Op) { return visit(Op); });
  45. if (!Pred(AR))
  46. return SE.getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagAnyWrap);
  47. // Normalization and denormalization are fancy names for decrementing and
  48. // incrementing a SCEV expression with respect to a set of loops. Since
  49. // Pred(AR) has returned true, we know we need to normalize or denormalize AR
  50. // with respect to its loop.
  51. if (Kind == Denormalize) {
  52. // Denormalization / "partial increment" is essentially the same as \c
  53. // SCEVAddRecExpr::getPostIncExpr. Here we use an explicit loop to make the
  54. // symmetry with Normalization clear.
  55. for (int i = 0, e = Operands.size() - 1; i < e; i++)
  56. Operands[i] = SE.getAddExpr(Operands[i], Operands[i + 1]);
  57. } else {
  58. assert(Kind == Normalize && "Only two possibilities!");
  59. // Normalization / "partial decrement" is a bit more subtle. Since
  60. // incrementing a SCEV expression (in general) changes the step of the SCEV
  61. // expression as well, we cannot use the step of the current expression.
  62. // Instead, we have to use the step of the very expression we're trying to
  63. // compute!
  64. //
  65. // We solve the issue by recursively building up the result, starting from
  66. // the "least significant" operand in the add recurrence:
  67. //
  68. // Base case:
  69. // Single operand add recurrence. It's its own normalization.
  70. //
  71. // N-operand case:
  72. // {S_{N-1},+,S_{N-2},+,...,+,S_0} = S
  73. //
  74. // Since the step recurrence of S is {S_{N-2},+,...,+,S_0}, we know its
  75. // normalization by induction. We subtract the normalized step
  76. // recurrence from S_{N-1} to get the normalization of S.
  77. for (int i = Operands.size() - 2; i >= 0; i--)
  78. Operands[i] = SE.getMinusSCEV(Operands[i], Operands[i + 1]);
  79. }
  80. return SE.getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagAnyWrap);
  81. }
  82. const SCEV *llvm::normalizeForPostIncUse(const SCEV *S,
  83. const PostIncLoopSet &Loops,
  84. ScalarEvolution &SE) {
  85. auto Pred = [&](const SCEVAddRecExpr *AR) {
  86. return Loops.count(AR->getLoop());
  87. };
  88. return NormalizeDenormalizeRewriter(Normalize, Pred, SE).visit(S);
  89. }
  90. const SCEV *llvm::normalizeForPostIncUseIf(const SCEV *S, NormalizePredTy Pred,
  91. ScalarEvolution &SE) {
  92. return NormalizeDenormalizeRewriter(Normalize, Pred, SE).visit(S);
  93. }
  94. const SCEV *llvm::denormalizeForPostIncUse(const SCEV *S,
  95. const PostIncLoopSet &Loops,
  96. ScalarEvolution &SE) {
  97. auto Pred = [&](const SCEVAddRecExpr *AR) {
  98. return Loops.count(AR->getLoop());
  99. };
  100. return NormalizeDenormalizeRewriter(Denormalize, Pred, SE).visit(S);
  101. }