blob: fa283ae65fc514c705995cf55b56c0a792a85589 [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"
28
29using mlir::AffineExpr;
30
31/// Returns the trip count of the loop as an affine expression if the latter is
32/// expressible as an affine expression, and nullptr otherwise. The trip count
33/// expression is simplified before returning.
Uday Bondhugula5912e872018-09-18 10:22:03 -070034AffineExpr *mlir::getTripCountExpr(const ForStmt &forStmt) {
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070035 // upper_bound - lower_bound + 1
36 int64_t loopSpan;
37
38 int64_t step = forStmt.getStep();
39 auto *context = forStmt.getContext();
40
41 if (forStmt.hasConstantBounds()) {
42 int64_t lb = forStmt.getConstantLowerBound();
43 int64_t ub = forStmt.getConstantUpperBound();
44 loopSpan = ub - lb + 1;
45 } else {
Uday Bondhugula5912e872018-09-18 10:22:03 -070046 auto *lbMap = forStmt.getLowerBoundMap();
47 auto *ubMap = forStmt.getUpperBoundMap();
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070048 // TODO(bondhugula): handle max/min of multiple expressions.
Uday Bondhugula5912e872018-09-18 10:22:03 -070049 if (lbMap->getNumResults() != 1 || ubMap->getNumResults() != 1)
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070050 return nullptr;
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070051
52 // TODO(bondhugula): handle bounds with different operands.
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070053 // Bounds have different operands, unhandled for now.
Uday Bondhugula5912e872018-09-18 10:22:03 -070054 if (!forStmt.matchingBoundOperandList())
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070055 return nullptr;
56
57 // ub_expr - lb_expr + 1
Uday Bondhugula5912e872018-09-18 10:22:03 -070058 auto *lbExpr = lbMap->getResult(0);
59 auto *ubExpr = ubMap->getResult(0);
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070060 auto *loopSpanExpr = AffineBinaryOpExpr::getAdd(
Uday Bondhugula5912e872018-09-18 10:22:03 -070061 AffineBinaryOpExpr::getSub(ubExpr, lbExpr, context), 1, context);
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070062
63 if (auto *expr = simplifyAffineExpr(loopSpanExpr, lbMap->getNumDims(),
64 lbMap->getNumSymbols(), context))
65 loopSpanExpr = expr;
66
67 auto *cExpr = dyn_cast<AffineConstantExpr>(loopSpanExpr);
68 if (!cExpr)
69 return AffineBinaryOpExpr::getCeilDiv(loopSpanExpr, std::abs(step),
70 context);
71 loopSpan = cExpr->getValue();
72 }
73
74 // 0 iteration loops.
75 if ((loopSpan < 0 && step >= 1) || (loopSpan > 0 && step <= -1))
76 return 0;
77
78 return AffineConstantExpr::get(
79 static_cast<uint64_t>(loopSpan % step == 0 ? loopSpan / step
80 : loopSpan / step + 1),
81 context);
82}
83
84/// Returns the trip count of the loop if it's a constant, None otherwise. This
85/// method uses affine expression analysis (in turn using getTripCount) and is
86/// able to determine constant trip count in non-trivial cases.
87llvm::Optional<uint64_t> mlir::getConstantTripCount(const ForStmt &forStmt) {
Uday Bondhugula5912e872018-09-18 10:22:03 -070088 AffineExpr *tripCountExpr = getTripCountExpr(forStmt);
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070089
90 if (auto *constExpr = dyn_cast_or_null<AffineConstantExpr>(tripCountExpr))
91 return constExpr->getValue();
92
93 return None;
94}
95
96/// Returns the greatest known integral divisor of the trip count. Affine
97/// expression analysis is used (indirectly through getTripCount), and
98/// this method is thus able to determine non-trivial divisors.
99uint64_t mlir::getLargestDivisorOfTripCount(const ForStmt &forStmt) {
Uday Bondhugula5912e872018-09-18 10:22:03 -0700100 AffineExpr *tripCountExpr = getTripCountExpr(forStmt);
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -0700101
102 if (!tripCountExpr)
103 return 1;
104
105 if (auto *constExpr = dyn_cast<AffineConstantExpr>(tripCountExpr)) {
106 uint64_t tripCount = constExpr->getValue();
107
108 // 0 iteration loops (greatest divisor is 2^64 - 1).
109 if (tripCount == 0)
110 return ULONG_MAX;
111
112 // The greatest divisor is the trip count.
113 return tripCount;
114 }
115
116 // Trip count is not a known constant; return its largest known divisor.
117 return tripCountExpr->getLargestKnownDivisor();
118}