| 1 | //===- TrivialDeadCodeElimination.cpp - Trivial DCE -----------------------===// |
|---|---|
| 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 "mlir/IR/Operation.h" |
| 10 | #include "mlir/Pass/Pass.h" |
| 11 | #include "mlir/Transforms/Passes.h" |
| 12 | #include "mlir/Transforms/RegionUtils.h" |
| 13 | |
| 14 | namespace mlir { |
| 15 | #define GEN_PASS_DEF_TRIVIALDEADCODEELIMINATIONPASS |
| 16 | #include "mlir/Transforms/Passes.h.inc" |
| 17 | } // namespace mlir |
| 18 | |
| 19 | using namespace mlir; |
| 20 | |
| 21 | namespace { |
| 22 | struct TrivialDeadCodeElimination |
| 23 | : public impl::TrivialDeadCodeEliminationPassBase< |
| 24 | TrivialDeadCodeElimination> { |
| 25 | using impl::TrivialDeadCodeEliminationPassBase< |
| 26 | TrivialDeadCodeElimination>::TrivialDeadCodeEliminationPassBase; |
| 27 | |
| 28 | void runOnOperation() override { |
| 29 | Operation *target = getOperation(); |
| 30 | IRRewriter rewriter(target->getContext()); |
| 31 | if (removeBlocks) |
| 32 | (void)eraseUnreachableBlocks(rewriter, regions: target->getRegions(), recurse: recursive); |
| 33 | for (Region ®ion : target->getRegions()) |
| 34 | eliminateTriviallyDeadOps(rewriter, region, includeNestedRegions: recursive); |
| 35 | } |
| 36 | }; |
| 37 | } // namespace |
| 38 |