blob: e894f4917de57a1a515796e95e95e4a447d9914d [file] [log] [blame]
MLIR Teamf85a6262018-06-27 11:03:08 -07001//===- AffineExpr.cpp - MLIR Affine Expr Classes --------------------------===//
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#include "mlir/IR/AffineExpr.h"
Uday Bondhugula3934d4d2018-07-09 09:00:25 -070019#include "mlir/Support/STLExtras.h"
MLIR Teamf85a6262018-06-27 11:03:08 -070020
21using namespace mlir;
Uday Bondhugula3934d4d2018-07-09 09:00:25 -070022
Chris Lattner1ac20cb2018-07-10 10:59:53 -070023/// Returns true if this expression is made out of only symbols and
24/// constants (no dimensional identifiers).
Uday Bondhugula3934d4d2018-07-09 09:00:25 -070025bool AffineExpr::isSymbolic() const {
26 switch (getKind()) {
27 case Kind::Constant:
28 return true;
29 case Kind::DimId:
30 return false;
31 case Kind::SymbolId:
32 return true;
33
34 case Kind::Add:
35 case Kind::Sub:
36 case Kind::Mul:
37 case Kind::FloorDiv:
38 case Kind::CeilDiv:
Chris Lattner1ac20cb2018-07-10 10:59:53 -070039 case Kind::Mod: {
40 auto expr = cast<AffineBinaryOpExpr>(this);
41 return expr->getLHS()->isSymbolic() && expr->getRHS()->isSymbolic();
42 }
Uday Bondhugula3934d4d2018-07-09 09:00:25 -070043 }
44}
45
Chris Lattner1ac20cb2018-07-10 10:59:53 -070046/// Returns true if this is a pure affine expression, i.e., multiplication,
47/// floordiv, ceildiv, and mod is only allowed w.r.t constants.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -070048bool AffineExpr::isPureAffine() const {
49 switch (getKind()) {
50 case Kind::SymbolId:
Uday Bondhugula3934d4d2018-07-09 09:00:25 -070051 case Kind::DimId:
Uday Bondhugula3934d4d2018-07-09 09:00:25 -070052 case Kind::Constant:
Chris Lattner1ac20cb2018-07-10 10:59:53 -070053 return true;
Uday Bondhugula3934d4d2018-07-09 09:00:25 -070054 case Kind::Add:
Chris Lattner1ac20cb2018-07-10 10:59:53 -070055 case Kind::Sub: {
56 auto op = cast<AffineBinaryOpExpr>(this);
57 return op->getLHS()->isPureAffine() && op->getRHS()->isPureAffine();
Uday Bondhugula3934d4d2018-07-09 09:00:25 -070058 }
Uday Bondhugula3934d4d2018-07-09 09:00:25 -070059
Chris Lattner1ac20cb2018-07-10 10:59:53 -070060 case Kind::Mul: {
61 // TODO: Canonicalize the constants in binary operators to the RHS when
62 // possible, allowing this to merge into the next case.
63 auto op = cast<AffineBinaryOpExpr>(this);
64 return op->getLHS()->isPureAffine() && op->getRHS()->isPureAffine() &&
65 (isa<AffineConstantExpr>(op->getLHS()) ||
66 isa<AffineConstantExpr>(op->getRHS()));
67 }
68 case Kind::FloorDiv:
69 case Kind::CeilDiv:
70 case Kind::Mod: {
71 auto op = cast<AffineBinaryOpExpr>(this);
72 return op->getLHS()->isPureAffine() &&
73 isa<AffineConstantExpr>(op->getRHS());
74 }
75 }
Uday Bondhugula3934d4d2018-07-09 09:00:25 -070076}