blob: 15491826e2885126b555757c07920bf24ffea286 [file] [log] [blame]
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -07001//===- LoopAnalysis.cpp - Misc loop analysis routines //-------------------===//
2//
3// Copyright 2019 The MLIR Authors.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// =============================================================================
17//
18// This file implements miscellaneous loop analysis routines.
19//
20//===----------------------------------------------------------------------===//
21
22#include "mlir/Analysis/LoopAnalysis.h"
23
24#include "mlir/Analysis/AffineAnalysis.h"
25#include "mlir/IR/AffineExpr.h"
26#include "mlir/IR/AffineMap.h"
27#include "mlir/IR/Statements.h"
Uday Bondhugula48e4c4b2018-10-03 10:07:54 -070028#include "mlir/Support/MathExtras.h"
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070029
30using mlir::AffineExpr;
31
32/// Returns the trip count of the loop as an affine expression if the latter is
33/// expressible as an affine expression, and nullptr otherwise. The trip count
34/// expression is simplified before returning.
Uday Bondhugula5912e872018-09-18 10:22:03 -070035AffineExpr *mlir::getTripCountExpr(const ForStmt &forStmt) {
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070036 // upper_bound - lower_bound + 1
37 int64_t loopSpan;
38
39 int64_t step = forStmt.getStep();
40 auto *context = forStmt.getContext();
41
42 if (forStmt.hasConstantBounds()) {
43 int64_t lb = forStmt.getConstantLowerBound();
44 int64_t ub = forStmt.getConstantUpperBound();
45 loopSpan = ub - lb + 1;
46 } else {
Uday Bondhugula5912e872018-09-18 10:22:03 -070047 auto *lbMap = forStmt.getLowerBoundMap();
48 auto *ubMap = forStmt.getUpperBoundMap();
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070049 // TODO(bondhugula): handle max/min of multiple expressions.
Uday Bondhugula5912e872018-09-18 10:22:03 -070050 if (lbMap->getNumResults() != 1 || ubMap->getNumResults() != 1)
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070051 return nullptr;
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070052
53 // TODO(bondhugula): handle bounds with different operands.
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070054 // Bounds have different operands, unhandled for now.
Uday Bondhugula5912e872018-09-18 10:22:03 -070055 if (!forStmt.matchingBoundOperandList())
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070056 return nullptr;
57
58 // ub_expr - lb_expr + 1
Uday Bondhugula5912e872018-09-18 10:22:03 -070059 auto *lbExpr = lbMap->getResult(0);
60 auto *ubExpr = ubMap->getResult(0);
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070061 auto *loopSpanExpr = AffineBinaryOpExpr::getAdd(
Uday Bondhugula5912e872018-09-18 10:22:03 -070062 AffineBinaryOpExpr::getSub(ubExpr, lbExpr, context), 1, context);
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070063
64 if (auto *expr = simplifyAffineExpr(loopSpanExpr, lbMap->getNumDims(),
65 lbMap->getNumSymbols(), context))
66 loopSpanExpr = expr;
67
68 auto *cExpr = dyn_cast<AffineConstantExpr>(loopSpanExpr);
69 if (!cExpr)
Uday Bondhugulaff5d6bd2018-09-27 18:03:27 -070070 return AffineBinaryOpExpr::getCeilDiv(loopSpanExpr, step, context);
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070071 loopSpan = cExpr->getValue();
72 }
73
74 // 0 iteration loops.
Uday Bondhugulaff5d6bd2018-09-27 18:03:27 -070075 if (loopSpan < 0)
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070076 return 0;
77
Uday Bondhugula48e4c4b2018-10-03 10:07:54 -070078 return AffineConstantExpr::get(static_cast<uint64_t>(ceilDiv(loopSpan, step)),
79 context);
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070080}
81
82/// Returns the trip count of the loop if it's a constant, None otherwise. This
83/// method uses affine expression analysis (in turn using getTripCount) and is
84/// able to determine constant trip count in non-trivial cases.
85llvm::Optional<uint64_t> mlir::getConstantTripCount(const ForStmt &forStmt) {
Uday Bondhugula5912e872018-09-18 10:22:03 -070086 AffineExpr *tripCountExpr = getTripCountExpr(forStmt);
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070087
88 if (auto *constExpr = dyn_cast_or_null<AffineConstantExpr>(tripCountExpr))
89 return constExpr->getValue();
90
91 return None;
92}
93
94/// Returns the greatest known integral divisor of the trip count. Affine
95/// expression analysis is used (indirectly through getTripCount), and
96/// this method is thus able to determine non-trivial divisors.
97uint64_t mlir::getLargestDivisorOfTripCount(const ForStmt &forStmt) {
Uday Bondhugula5912e872018-09-18 10:22:03 -070098 AffineExpr *tripCountExpr = getTripCountExpr(forStmt);
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070099
100 if (!tripCountExpr)
101 return 1;
102
103 if (auto *constExpr = dyn_cast<AffineConstantExpr>(tripCountExpr)) {
104 uint64_t tripCount = constExpr->getValue();
105
106 // 0 iteration loops (greatest divisor is 2^64 - 1).
107 if (tripCount == 0)
108 return ULONG_MAX;
109
110 // The greatest divisor is the trip count.
111 return tripCount;
112 }
113
114 // Trip count is not a known constant; return its largest known divisor.
115 return tripCountExpr->getLargestKnownDivisor();
116}