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"
1443959a25SRiver Riddle #include "mlir/Interfaces/LoopLikeInterface.h"
15*fa26c7ffSMogball #include "mlir/Transforms/LoopInvariantCodeMotionUtils.h"
16a70aa7bbSRiver Riddle #include "mlir/Transforms/Passes.h"
17*fa26c7ffSMogball #include "mlir/Transforms/SideEffectUtils.h"
187905da65SAmit Sabne 
197905da65SAmit Sabne using namespace mlir;
207905da65SAmit Sabne 
217905da65SAmit Sabne namespace {
227905da65SAmit Sabne /// Loop invariant code motion (LICM) pass.
2380aca1eaSRiver Riddle struct LoopInvariantCodeMotion
241834ad4aSRiver Riddle     : public LoopInvariantCodeMotionBase<LoopInvariantCodeMotion> {
25b843cc5dSStephan Herhut   void runOnOperation() override;
267905da65SAmit Sabne };
27be0a7e9fSMehdi Amini } // namespace
287905da65SAmit Sabne 
runOnOperation()29b843cc5dSStephan Herhut void LoopInvariantCodeMotion::runOnOperation() {
300134b5dfSChris Lattner   // Walk through all loops in a function in innermost-loop-first order. This
310134b5dfSChris Lattner   // way, we first LICM from the inner loop, and place the ops in
320134b5dfSChris Lattner   // the outer loop, which in turn can be further LICM'ed.
33*fa26c7ffSMogball   getOperation()->walk(
34*fa26c7ffSMogball       [&](LoopLikeOpInterface loopLike) { moveLoopInvariantCode(loopLike); });
357905da65SAmit Sabne }
367905da65SAmit Sabne 
createLoopInvariantCodeMotionPass()37b843cc5dSStephan Herhut std::unique_ptr<Pass> mlir::createLoopInvariantCodeMotionPass() {
38b843cc5dSStephan Herhut   return std::make_unique<LoopInvariantCodeMotion>();
39b843cc5dSStephan Herhut }
40