1bde89ac7SEric Schweitz //===-- DoLoopHelper.cpp --------------------------------------------------===//
2bde89ac7SEric Schweitz //
3bde89ac7SEric Schweitz // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bde89ac7SEric Schweitz // See https://llvm.org/LICENSE.txt for license information.
5bde89ac7SEric Schweitz // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bde89ac7SEric Schweitz //
7bde89ac7SEric Schweitz //===----------------------------------------------------------------------===//
8bde89ac7SEric Schweitz
9bde89ac7SEric Schweitz #include "flang/Optimizer/Builder/DoLoopHelper.h"
10bde89ac7SEric Schweitz
11bde89ac7SEric Schweitz //===----------------------------------------------------------------------===//
12bde89ac7SEric Schweitz // DoLoopHelper implementation
13bde89ac7SEric Schweitz //===----------------------------------------------------------------------===//
14bde89ac7SEric Schweitz
15bde89ac7SEric Schweitz fir::DoLoopOp
createLoop(mlir::Value lb,mlir::Value ub,mlir::Value step,const BodyGenerator & bodyGenerator)16bde89ac7SEric Schweitz fir::factory::DoLoopHelper::createLoop(mlir::Value lb, mlir::Value ub,
17bde89ac7SEric Schweitz mlir::Value step,
18bde89ac7SEric Schweitz const BodyGenerator &bodyGenerator) {
19bde89ac7SEric Schweitz auto lbi = builder.convertToIndexType(loc, lb);
20bde89ac7SEric Schweitz auto ubi = builder.convertToIndexType(loc, ub);
21bde89ac7SEric Schweitz assert(step && "step must be an actual Value");
22bde89ac7SEric Schweitz auto inc = builder.convertToIndexType(loc, step);
23bde89ac7SEric Schweitz auto loop = builder.create<fir::DoLoopOp>(loc, lbi, ubi, inc);
24bde89ac7SEric Schweitz auto insertPt = builder.saveInsertionPoint();
25bde89ac7SEric Schweitz builder.setInsertionPointToStart(loop.getBody());
26bde89ac7SEric Schweitz auto index = loop.getInductionVar();
27bde89ac7SEric Schweitz bodyGenerator(builder, index);
28bde89ac7SEric Schweitz builder.restoreInsertionPoint(insertPt);
29bde89ac7SEric Schweitz return loop;
30bde89ac7SEric Schweitz }
31bde89ac7SEric Schweitz
32bde89ac7SEric Schweitz fir::DoLoopOp
createLoop(mlir::Value lb,mlir::Value ub,const BodyGenerator & bodyGenerator)33bde89ac7SEric Schweitz fir::factory::DoLoopHelper::createLoop(mlir::Value lb, mlir::Value ub,
34bde89ac7SEric Schweitz const BodyGenerator &bodyGenerator) {
35bde89ac7SEric Schweitz return createLoop(
36bde89ac7SEric Schweitz lb, ub, builder.createIntegerConstant(loc, builder.getIndexType(), 1),
37bde89ac7SEric Schweitz bodyGenerator);
38bde89ac7SEric Schweitz }
39bde89ac7SEric Schweitz
40bde89ac7SEric Schweitz fir::DoLoopOp
createLoop(mlir::Value count,const BodyGenerator & bodyGenerator)41bde89ac7SEric Schweitz fir::factory::DoLoopHelper::createLoop(mlir::Value count,
42bde89ac7SEric Schweitz const BodyGenerator &bodyGenerator) {
43bde89ac7SEric Schweitz auto indexType = builder.getIndexType();
44bde89ac7SEric Schweitz auto zero = builder.createIntegerConstant(loc, indexType, 0);
45bde89ac7SEric Schweitz auto one = builder.createIntegerConstant(loc, count.getType(), 1);
46*092601d4SAndrzej Warzynski auto up = builder.create<mlir::arith::SubIOp>(loc, count, one);
47bde89ac7SEric Schweitz return createLoop(zero, up, one, bodyGenerator);
48bde89ac7SEric Schweitz }
49