17905da65SAmit Sabne //===- LoopInvariantCodeMotion.cpp - Code to perform loop fusion-----------===//
27905da65SAmit Sabne //
330857107SMehdi Amini // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
456222a06SMehdi Amini // See https://llvm.org/LICENSE.txt for license information.
556222a06SMehdi Amini // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67905da65SAmit Sabne //
756222a06SMehdi Amini //===----------------------------------------------------------------------===//
87905da65SAmit Sabne //
97905da65SAmit Sabne // This file implements loop invariant code motion.
107905da65SAmit Sabne //
117905da65SAmit Sabne //===----------------------------------------------------------------------===//
127905da65SAmit Sabne 
131834ad4aSRiver Riddle #include "PassDetail.h"
14b843cc5dSStephan Herhut #include "mlir/IR/Builders.h"
1543959a25SRiver Riddle #include "mlir/Interfaces/LoopLikeInterface.h"
16eb623ae8SStephen Neuendorffer #include "mlir/Interfaces/SideEffectInterfaces.h"
17*a70aa7bbSRiver Riddle #include "mlir/Transforms/Passes.h"
187a43da60SAmit Sabne #include "llvm/ADT/SmallPtrSet.h"
197905da65SAmit Sabne #include "llvm/Support/CommandLine.h"
207905da65SAmit Sabne #include "llvm/Support/Debug.h"
217905da65SAmit Sabne 
227905da65SAmit Sabne #define DEBUG_TYPE "licm"
237905da65SAmit Sabne 
247905da65SAmit Sabne using namespace mlir;
257905da65SAmit Sabne 
267905da65SAmit Sabne namespace {
277905da65SAmit Sabne /// Loop invariant code motion (LICM) pass.
2880aca1eaSRiver Riddle struct LoopInvariantCodeMotion
291834ad4aSRiver Riddle     : public LoopInvariantCodeMotionBase<LoopInvariantCodeMotion> {
30b843cc5dSStephan Herhut   void runOnOperation() override;
317905da65SAmit Sabne };
32be0a7e9fSMehdi Amini } // namespace
337905da65SAmit Sabne 
34b843cc5dSStephan Herhut void LoopInvariantCodeMotion::runOnOperation() {
350134b5dfSChris Lattner   // Walk through all loops in a function in innermost-loop-first order. This
360134b5dfSChris Lattner   // way, we first LICM from the inner loop, and place the ops in
370134b5dfSChris Lattner   // the outer loop, which in turn can be further LICM'ed.
38b10c6625SRiver Riddle   getOperation()->walk([&](LoopLikeOpInterface loopLike) {
39e7f7137cSRahul Joshi     LLVM_DEBUG(loopLike.print(llvm::dbgs() << "\nOriginal loop:\n"));
40b10c6625SRiver Riddle     if (failed(moveLoopInvariantCode(loopLike)))
41b843cc5dSStephan Herhut       signalPassFailure();
420134b5dfSChris Lattner   });
437905da65SAmit Sabne }
447905da65SAmit Sabne 
45b843cc5dSStephan Herhut std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() {
46b843cc5dSStephan Herhut   return std::make_unique<LoopInvariantCodeMotion>();
47b843cc5dSStephan Herhut }
48