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 
12 using namespace mlir;
13 using namespace mlir::detail;
14 
getNumDims() const15 unsigned IntegerSet::getNumDims() const { return set->dimCount; }
getNumSymbols() const16 unsigned IntegerSet::getNumSymbols() const { return set->symbolCount; }
getNumInputs() const17 unsigned IntegerSet::getNumInputs() const {
18   return set->dimCount + set->symbolCount;
19 }
20 
getNumConstraints() const21 unsigned IntegerSet::getNumConstraints() const {
22   return set->constraints.size();
23 }
24 
getNumEqualities() const25 unsigned IntegerSet::getNumEqualities() const {
26   unsigned numEqualities = 0;
27   for (unsigned i = 0, e = getNumConstraints(); i < e; i++)
28     if (isEq(i))
29       ++numEqualities;
30   return numEqualities;
31 }
32 
getNumInequalities() const33 unsigned IntegerSet::getNumInequalities() const {
34   return getNumConstraints() - getNumEqualities();
35 }
36 
isEmptyIntegerSet() const37 bool IntegerSet::isEmptyIntegerSet() const {
38   // This will only work if uniquing is on.
39   static_assert(kUniquingThreshold >= 1,
40                 "uniquing threshold should be at least one");
41   return *this == getEmptySet(set->dimCount, set->symbolCount, getContext());
42 }
43 
getConstraints() const44 ArrayRef<AffineExpr> IntegerSet::getConstraints() const {
45   return set->constraints;
46 }
47 
getConstraint(unsigned idx) const48 AffineExpr IntegerSet::getConstraint(unsigned idx) const {
49   return getConstraints()[idx];
50 }
51 
52 /// Returns the equality bits, which specify whether each of the constraints
53 /// is an equality or inequality.
getEqFlags() const54 ArrayRef<bool> IntegerSet::getEqFlags() const { return set->eqFlags; }
55 
56 /// Returns true if the idx^th constraint is an equality, false if it is an
57 /// inequality.
isEq(unsigned idx) const58 bool IntegerSet::isEq(unsigned idx) const { return getEqFlags()[idx]; }
59 
getContext() const60 MLIRContext *IntegerSet::getContext() const {
61   return getConstraint(0).getContext();
62 }
63 
64 /// Walk all of the AffineExpr's in this set. Each node in an expression
65 /// tree is visited in postorder.
walkExprs(function_ref<void (AffineExpr)> callback) const66 void IntegerSet::walkExprs(function_ref<void(AffineExpr)> callback) const {
67   for (auto expr : getConstraints())
68     expr.walk(callback);
69 }
70 
replaceDimsAndSymbols(ArrayRef<AffineExpr> dimReplacements,ArrayRef<AffineExpr> symReplacements,unsigned numResultDims,unsigned numResultSyms)71 IntegerSet IntegerSet::replaceDimsAndSymbols(
72     ArrayRef<AffineExpr> dimReplacements, ArrayRef<AffineExpr> symReplacements,
73     unsigned numResultDims, unsigned numResultSyms) {
74   SmallVector<AffineExpr, 8> constraints;
75   constraints.reserve(getNumConstraints());
76   for (auto cst : getConstraints())
77     constraints.push_back(
78         cst.replaceDimsAndSymbols(dimReplacements, symReplacements));
79 
80   return get(numResultDims, numResultSyms, constraints, getEqFlags());
81 }
82