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