1 //===- AlgebraicSimplification.cpp - Simplify algebraic expressions -------===//
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
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 //===----------------------------------------------------------------------===//
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"
20 #define GEN_PASS_DEF_ALGEBRAICSIMPLIFICATION
21 #include "flang/Optimizer/Transforms/Passes.h.inc"
27 struct AlgebraicSimplification
28 : public fir::impl::AlgebraicSimplificationBase
<AlgebraicSimplification
> {
29 AlgebraicSimplification(const GreedyRewriteConfig
&rewriteConfig
) {
30 config
= rewriteConfig
;
33 void runOnOperation() override
;
35 mlir::GreedyRewriteConfig config
;
39 void AlgebraicSimplification::runOnOperation() {
40 RewritePatternSet
patterns(&getContext());
41 populateMathAlgebraicSimplificationPatterns(patterns
);
42 (void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns
),
46 std::unique_ptr
<mlir::Pass
> fir::createAlgebraicSimplificationPass() {
47 return std::make_unique
<AlgebraicSimplification
>(GreedyRewriteConfig());
50 std::unique_ptr
<mlir::Pass
> fir::createAlgebraicSimplificationPass(
51 const mlir::GreedyRewriteConfig
&config
) {
52 return std::make_unique
<AlgebraicSimplification
>(config
);