xref: /llvm-project/flang/lib/Optimizer/Transforms/AlgebraicSimplification.cpp (revision 09dfc5713d7e2342bea4c8447d1ed76c85eb8225)
1 //===- AlgebraicSimplification.cpp - Simplify algebraic expressions -------===//
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 // This file defines a pass that applies algebraic simplifications
9 // to operations of Math/Complex/etc. dialects that are used by Flang.
10 // It is done as a Flang specific pass, because we may want to tune
11 // the parameters of the patterns for Fortran programs.
12 //===----------------------------------------------------------------------===//
13 
14 #include "flang/Optimizer/Transforms/Passes.h"
15 #include "mlir/Dialect/Math/IR/Math.h"
16 #include "mlir/Dialect/Math/Transforms/Passes.h"
17 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
18 
19 namespace fir {
20 #define GEN_PASS_DEF_ALGEBRAICSIMPLIFICATION
21 #include "flang/Optimizer/Transforms/Passes.h.inc"
22 } // namespace fir
23 
24 using namespace mlir;
25 
26 namespace {
27 struct AlgebraicSimplification
28     : public fir::impl::AlgebraicSimplificationBase<AlgebraicSimplification> {
29   AlgebraicSimplification(const GreedyRewriteConfig &rewriteConfig) {
30     config = rewriteConfig;
31   }
32 
33   void runOnOperation() override;
34 
35   mlir::GreedyRewriteConfig config;
36 };
37 } // namespace
38 
39 void AlgebraicSimplification::runOnOperation() {
40   RewritePatternSet patterns(&getContext());
41   populateMathAlgebraicSimplificationPatterns(patterns);
42   (void)applyPatternsGreedily(getOperation(), std::move(patterns), config);
43 }
44 
45 std::unique_ptr<mlir::Pass> fir::createAlgebraicSimplificationPass() {
46   return std::make_unique<AlgebraicSimplification>(GreedyRewriteConfig());
47 }
48 
49 std::unique_ptr<mlir::Pass> fir::createAlgebraicSimplificationPass(
50     const mlir::GreedyRewriteConfig &config) {
51   return std::make_unique<AlgebraicSimplification>(config);
52 }
53