xref: /llvm-project/mlir/lib/IR/IntegerSet.cpp (revision 308571074c13ea2a0758aa085aa02f72150f891e)
1 //===- IntegerSet.cpp - MLIR Integer Set class ----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/IR/IntegerSet.h"
10 #include "IntegerSetDetail.h"
11 #include "mlir/IR/AffineExpr.h"
12 
13 using namespace mlir;
14 using namespace mlir::detail;
15 
16 unsigned IntegerSet::getNumDims() const { return set->dimCount; }
17 unsigned IntegerSet::getNumSymbols() const { return set->symbolCount; }
18 unsigned IntegerSet::getNumInputs() const {
19   return set->dimCount + set->symbolCount;
20 }
21 
22 unsigned IntegerSet::getNumConstraints() const {
23   return set->constraints.size();
24 }
25 
26 unsigned IntegerSet::getNumEqualities() const {
27   unsigned numEqualities = 0;
28   for (unsigned i = 0, e = getNumConstraints(); i < e; i++)
29     if (isEq(i))
30       ++numEqualities;
31   return numEqualities;
32 }
33 
34 unsigned IntegerSet::getNumInequalities() const {
35   return getNumConstraints() - getNumEqualities();
36 }
37 
38 bool IntegerSet::isEmptyIntegerSet() const {
39   // This will only work if uniquing is on.
40   static_assert(kUniquingThreshold >= 1,
41                 "uniquing threshold should be at least one");
42   return *this == getEmptySet(set->dimCount, set->symbolCount, getContext());
43 }
44 
45 ArrayRef<AffineExpr> IntegerSet::getConstraints() const {
46   return set->constraints;
47 }
48 
49 AffineExpr IntegerSet::getConstraint(unsigned idx) const {
50   return getConstraints()[idx];
51 }
52 
53 /// Returns the equality bits, which specify whether each of the constraints
54 /// is an equality or inequality.
55 ArrayRef<bool> IntegerSet::getEqFlags() const { return set->eqFlags; }
56 
57 /// Returns true if the idx^th constraint is an equality, false if it is an
58 /// inequality.
59 bool IntegerSet::isEq(unsigned idx) const { return getEqFlags()[idx]; }
60 
61 MLIRContext *IntegerSet::getContext() const {
62   return getConstraint(0).getContext();
63 }
64 
65 /// Walk all of the AffineExpr's in this set. Each node in an expression
66 /// tree is visited in postorder.
67 void IntegerSet::walkExprs(function_ref<void(AffineExpr)> callback) const {
68   for (auto expr : getConstraints())
69     expr.walk(callback);
70 }
71 
72 IntegerSet IntegerSet::replaceDimsAndSymbols(
73     ArrayRef<AffineExpr> dimReplacements, ArrayRef<AffineExpr> symReplacements,
74     unsigned numResultDims, unsigned numResultSyms) {
75   SmallVector<AffineExpr, 8> constraints;
76   constraints.reserve(getNumConstraints());
77   for (auto cst : getConstraints())
78     constraints.push_back(
79         cst.replaceDimsAndSymbols(dimReplacements, symReplacements));
80 
81   return get(numResultDims, numResultSyms, constraints, getEqFlags());
82 }
83