xref: /llvm-project/mlir/lib/Transforms/LoopInvariantCodeMotion.cpp (revision e51652f9bf2e63497ed1e007c4a8c31c7ec08930)
1 //===- LoopInvariantCodeMotion.cpp - Code to perform loop fusion-----------===//
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 // This file implements loop invariant code motion.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "PassDetail.h"
14 #include "mlir/IR/Builders.h"
15 #include "mlir/Interfaces/LoopLikeInterface.h"
16 #include "mlir/Interfaces/SideEffectInterfaces.h"
17 #include "mlir/Transforms/Passes.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Debug.h"
21 
22 #define DEBUG_TYPE "licm"
23 
24 using namespace mlir;
25 
26 namespace {
27 /// Loop invariant code motion (LICM) pass.
28 struct LoopInvariantCodeMotion
29     : public LoopInvariantCodeMotionBase<LoopInvariantCodeMotion> {
30   void runOnOperation() override;
31 };
32 } // namespace
33 
34 void LoopInvariantCodeMotion::runOnOperation() {
35   // Walk through all loops in a function in innermost-loop-first order. This
36   // way, we first LICM from the inner loop, and place the ops in
37   // the outer loop, which in turn can be further LICM'ed.
38   getOperation()->walk([&](LoopLikeOpInterface loopLike) {
39     LLVM_DEBUG(loopLike.print(llvm::dbgs() << "\nOriginal loop:\n"));
40     moveLoopInvariantCode(loopLike);
41   });
42 }
43 
44 std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() {
45   return std::make_unique<LoopInvariantCodeMotion>();
46 }
47