| 1 | //===- SimplifyCFG.cpp - Simplify LLVM control-flow graphs ----------------===// |
| 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/Dialect/LLVMIR/Transforms/SimplifyCFG.h" |
| 10 | |
| 11 | #include "mlir/Dialect/LLVMIR/LLVMModuleOps.h" |
| 12 | #include "mlir/Dialect/LLVMIR/LLVMTerminatorOps.h" |
| 13 | #include "mlir/IR/PatternMatch.h" |
| 14 | #include "mlir/Pass/Pass.h" |
| 15 | #include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 16 | |
| 17 | namespace mlir { |
| 18 | namespace LLVM { |
| 19 | #define GEN_PASS_DEF_LLVMSIMPLIFYCFGPASS |
| 20 | #include "mlir/Dialect/LLVMIR/Transforms/Passes.h.inc" |
| 21 | } // namespace LLVM |
| 22 | } // namespace mlir |
| 23 | |
| 24 | using namespace mlir; |
| 25 | |
| 26 | namespace { |
| 27 | // Running the branch patterns standalone is profitable where full |
| 28 | // canonicalization is not wanted: the generic canonicalizer runs |
| 29 | // every pattern and folds the whole module, which costs more than |
| 30 | // merging blocks and changes operations beyond the branches. Today |
| 31 | // the pass applies only the branch patterns, with folding off, so its |
| 32 | // only effect is block merging, and pipelines that never run |
| 33 | // canonicalization at all can still use the transform through the |
| 34 | // same pattern set. |
| 35 | // Merge forwarding blocks in `func` by applying the llvm.br |
| 36 | // canonicalization patterns. |
| 37 | static LogicalResult mergeBranchBlocks(LLVM::LLVMFuncOp func) { |
| 38 | RewritePatternSet patterns(func.getContext()); |
| 39 | LLVM::BrOp::getCanonicalizationPatterns(results&: patterns, context: func.getContext()); |
| 40 | SmallVector<Operation *> ops; |
| 41 | func.walk(callback: [&](LLVM::BrOp op) { ops.push_back(Elt: op); }); |
| 42 | // Only the collected branches are rewritten, and folding is off: |
| 43 | // otherwise the driver would fold operations exposed by a merge and |
| 44 | // delete dead operand definitions, changing IR beyond block merging. |
| 45 | GreedyRewriteConfig config; |
| 46 | config.setStrictness(GreedyRewriteStrictness::ExistingOps); |
| 47 | config.enableFolding(enable: false); |
| 48 | return applyOpPatternsGreedily(ops, patterns: std::move(patterns), config); |
| 49 | } |
| 50 | |
| 51 | // This pass is the home for control-flow simplifications in the LLVM |
| 52 | // dialect. Simplifications should be added here. |
| 53 | struct SimplifyCFGPass |
| 54 | : public LLVM::impl::LLVMSimplifyCFGPassBase<SimplifyCFGPass> { |
| 55 | void runOnOperation() override { |
| 56 | LLVM::LLVMFuncOp func = getOperation(); |
| 57 | if (func.isOptimizeNone()) |
| 58 | return; |
| 59 | if (failed(Result: mergeBranchBlocks(func))) |
| 60 | signalPassFailure(); |
| 61 | // Further transforms can be called here. |
| 62 | } |
| 63 | }; |
| 64 | } // namespace |
| 65 | |