| 1 | //===- llvm/Analysis/ScalarEvolutionDivision.h - See below ------*- C++ -*-===// |
| 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 defines the class that knows how to divide SCEV's. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_ANALYSIS_SCALAREVOLUTIONDIVISION_H |
| 14 | #define LLVM_ANALYSIS_SCALAREVOLUTIONDIVISION_H |
| 15 | |
| 16 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 17 | |
| 18 | namespace llvm { |
| 19 | |
| 20 | class SCEV; |
| 21 | |
| 22 | class ScalarEvolution; |
| 23 | |
| 24 | struct SCEVCouldNotCompute; |
| 25 | |
| 26 | struct SCEVDivision : public SCEVVisitor<SCEVDivision, void> { |
| 27 | public: |
| 28 | /// Computes the Quotient and Remainder of the division of Numerator by |
| 29 | /// Denominator. We are not actually performing the division here. Instead, we |
| 30 | /// are trying to find SCEV expressions Quotient and Remainder that satisfy: |
| 31 | /// |
| 32 | /// Numerator = Denominator * Quotient + Remainder |
| 33 | /// |
| 34 | /// There may be multiple valid answers for Quotient and Remainder. This |
| 35 | /// function finds one of them. Especially, there is always a trivial |
| 36 | /// solution: (Quotient, Remainder) = (0, Numerator). |
| 37 | /// |
| 38 | /// Note the following: |
| 39 | /// * The condition Remainder < Denominator is NOT necessarily required. |
| 40 | /// * Division of constants is performed as signed. |
| 41 | /// * The multiplication of Quotient and Denominator may wrap. |
| 42 | /// * The addition of Quotient*Denominator and Remainder may wrap. |
| 43 | static void divide(ScalarEvolution &SE, const SCEV *Numerator, |
| 44 | const SCEV *Denominator, const SCEV **Quotient, |
| 45 | const SCEV **Remainder); |
| 46 | |
| 47 | // Except in the trivial case described above, we do not know how to divide |
| 48 | // Expr by Denominator for the following functions with empty implementation. |
| 49 | void visitPtrToAddrExpr(const SCEVPtrToAddrExpr *Numerator) {} |
| 50 | void visitPtrToIntExpr(const SCEVPtrToIntExpr *Numerator) {} |
| 51 | void visitTruncateExpr(const SCEVTruncateExpr *Numerator) {} |
| 52 | void visitZeroExtendExpr(const SCEVZeroExtendExpr *Numerator) {} |
| 53 | void visitSignExtendExpr(const SCEVSignExtendExpr *Numerator) {} |
| 54 | void visitUDivExpr(const SCEVUDivExpr *Numerator) {} |
| 55 | void visitSMaxExpr(const SCEVSMaxExpr *Numerator) {} |
| 56 | void visitUMaxExpr(const SCEVUMaxExpr *Numerator) {} |
| 57 | void visitSMinExpr(const SCEVSMinExpr *Numerator) {} |
| 58 | void visitUMinExpr(const SCEVUMinExpr *Numerator) {} |
| 59 | void visitSequentialUMinExpr(const SCEVSequentialUMinExpr *Numerator) {} |
| 60 | void visitUnknown(const SCEVUnknown *Numerator) {} |
| 61 | void visitCouldNotCompute(const SCEVCouldNotCompute *Numerator) {} |
| 62 | |
| 63 | void visitConstant(const SCEVConstant *Numerator); |
| 64 | |
| 65 | void visitVScale(const SCEVVScale *Numerator); |
| 66 | |
| 67 | void visitAddRecExpr(const SCEVAddRecExpr *Numerator); |
| 68 | |
| 69 | void visitAddExpr(const SCEVAddExpr *Numerator); |
| 70 | |
| 71 | void visitMulExpr(const SCEVMulExpr *Numerator); |
| 72 | |
| 73 | private: |
| 74 | SCEVDivision(ScalarEvolution &S, const SCEV *Numerator, |
| 75 | const SCEV *Denominator); |
| 76 | |
| 77 | // Convenience function for giving up on the division. We set the quotient to |
| 78 | // be equal to zero and the remainder to be equal to the numerator. |
| 79 | void cannotDivide(const SCEV *Numerator); |
| 80 | |
| 81 | ScalarEvolution &SE; |
| 82 | const SCEV *Denominator, *Quotient, *Remainder, *Zero, *One; |
| 83 | }; |
| 84 | |
| 85 | class SCEVDivisionPrinterPass : public PassInfoMixin<SCEVDivisionPrinterPass> { |
| 86 | raw_ostream &OS; |
| 87 | void runImpl(Function &F, ScalarEvolution &SE); |
| 88 | |
| 89 | public: |
| 90 | explicit SCEVDivisionPrinterPass(raw_ostream &OS) : OS(OS) {} |
| 91 | PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); |
| 92 | }; |
| 93 | |
| 94 | } // end namespace llvm |
| 95 | |
| 96 | #endif // LLVM_ANALYSIS_SCALAREVOLUTIONDIVISION_H |
| 97 | |