xref: /llvm-project/flang/lib/Optimizer/Transforms/SimplifyRegionLite.cpp (revision 92f4f0b061651fc57e8434f0d651851878e0eab5)
1 //===- SimplifyRegionLite.cpp -- region simplification lite ---------------===//
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 #include "flang/Optimizer/Dialect/FIROps.h"
10 #include "flang/Optimizer/Transforms/Passes.h"
11 #include "mlir/IR/PatternMatch.h"
12 #include "mlir/Pass/Pass.h"
13 #include "mlir/Transforms/DialectConversion.h"
14 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
15 #include "mlir/Transforms/RegionUtils.h"
16 
17 namespace fir {
18 #define GEN_PASS_DEF_SIMPLIFYREGIONLITE
19 #include "flang/Optimizer/Transforms/Passes.h.inc"
20 } // namespace fir
21 
22 namespace {
23 
24 class SimplifyRegionLitePass
25     : public fir::impl::SimplifyRegionLiteBase<SimplifyRegionLitePass> {
26 public:
27   void runOnOperation() override;
28 };
29 
30 class DummyRewriter : public mlir::PatternRewriter {
31 public:
DummyRewriter(mlir::MLIRContext * ctx)32   DummyRewriter(mlir::MLIRContext *ctx) : mlir::PatternRewriter(ctx) {}
33 };
34 
35 } // namespace
36 
runOnOperation()37 void SimplifyRegionLitePass::runOnOperation() {
38   auto op = getOperation();
39   auto regions = op->getRegions();
40   mlir::RewritePatternSet patterns(op.getContext());
41   DummyRewriter rewriter(op.getContext());
42   if (regions.empty())
43     return;
44 
45   (void)mlir::eraseUnreachableBlocks(rewriter, regions);
46   (void)mlir::runRegionDCE(rewriter, regions);
47 }
48