xref: /llvm-project/mlir/lib/IR/IntegerSet.cpp (revision 85dcaf19c721c4c745cd4fa3972aa9093acd69d3)
1 //===- IntegerSet.cpp - MLIR Integer Set class ----------------------------===//
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/IntegerSet.h"
19 #include "IntegerSetDetail.h"
20 #include "mlir/IR/AffineExpr.h"
21 
22 using namespace mlir;
23 using namespace mlir::detail;
24 
25 unsigned IntegerSet::getNumDims() const { return set->dimCount; }
26 unsigned IntegerSet::getNumSymbols() const { return set->symbolCount; }
27 unsigned IntegerSet::getNumInputs() const {
28   return set->dimCount + set->symbolCount;
29 }
30 
31 unsigned IntegerSet::getNumConstraints() const {
32   return set->constraints.size();
33 }
34 
35 unsigned IntegerSet::getNumEqualities() const {
36   unsigned numEqualities = 0;
37   for (unsigned i = 0, e = getNumConstraints(); i < e; i++)
38     if (isEq(i))
39       ++numEqualities;
40   return numEqualities;
41 }
42 
43 unsigned IntegerSet::getNumInequalities() const {
44   return getNumConstraints() - getNumEqualities();
45 }
46 
47 bool IntegerSet::isEmptyIntegerSet() const {
48   // This will only work if uniquing is on.
49   static_assert(kUniquingThreshold >= 1,
50                 "uniquing threshold should be at least one");
51   return *this == getEmptySet(set->dimCount, set->symbolCount, getContext());
52 }
53 
54 ArrayRef<AffineExpr> IntegerSet::getConstraints() const {
55   return set->constraints;
56 }
57 
58 AffineExpr IntegerSet::getConstraint(unsigned idx) const {
59   return getConstraints()[idx];
60 }
61 
62 /// Returns the equality bits, which specify whether each of the constraints
63 /// is an equality or inequality.
64 ArrayRef<bool> IntegerSet::getEqFlags() const { return set->eqFlags; }
65 
66 /// Returns true if the idx^th constraint is an equality, false if it is an
67 /// inequality.
68 bool IntegerSet::isEq(unsigned idx) const { return getEqFlags()[idx]; }
69 
70 MLIRContext *IntegerSet::getContext() const {
71   return getConstraint(0).getContext();
72 }
73 
74 /// Walk all of the AffineExpr's in this set. Each node in an expression
75 /// tree is visited in postorder.
76 void IntegerSet::walkExprs(
77     llvm::function_ref<void(AffineExpr)> callback) const {
78   for (auto expr : getConstraints())
79     expr.walk(callback);
80 }
81 
82 IntegerSet IntegerSet::replaceDimsAndSymbols(
83     ArrayRef<AffineExpr> dimReplacements, ArrayRef<AffineExpr> symReplacements,
84     unsigned numResultDims, unsigned numResultSyms) {
85   SmallVector<AffineExpr, 8> constraints;
86   constraints.reserve(getNumConstraints());
87   for (auto cst : getConstraints())
88     constraints.push_back(
89         cst.replaceDimsAndSymbols(dimReplacements, symReplacements));
90 
91   return get(numResultDims, numResultSyms, constraints, getEqFlags());
92 }
93