1 //===- Utils.cpp - General utilities for Presburger library ---------------===//
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 // Utility functions required by the Presburger Library.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Analysis/Presburger/Utils.h"
14 #include "mlir/Analysis/Presburger/IntegerPolyhedron.h"
15 #include "mlir/Support/LogicalResult.h"
16 #include "mlir/Support/MathExtras.h"
17 
18 using namespace mlir;
19 
20 /// Normalize a division's `dividend` and the `divisor` by their GCD. For
21 /// example: if the dividend and divisor are [2,0,4] and 4 respectively,
22 /// they get normalized to [1,0,2] and 2.
23 static void normalizeDivisionByGCD(SmallVectorImpl<int64_t> &dividend,
24                                    unsigned &divisor) {
25   if (divisor == 0 || dividend.empty())
26     return;
27   int64_t gcd = llvm::greatestCommonDivisor(dividend.front(), int64_t(divisor));
28 
29   // The reason for ignoring the constant term is as follows.
30   // For a division:
31   //      floor((a + m.f(x))/(m.d))
32   // It can be replaced by:
33   //      floor((floor(a/m) + f(x))/d)
34   // Since `{a/m}/d` in the dividend satisfies 0 <= {a/m}/d < 1/d, it will not
35   // influence the result of the floor division and thus, can be ignored.
36   for (size_t i = 1, m = dividend.size() - 1; i < m; i++) {
37     gcd = llvm::greatestCommonDivisor(dividend[i], gcd);
38     if (gcd == 1)
39       return;
40   }
41 
42   // Normalize the dividend and the denominator.
43   std::transform(dividend.begin(), dividend.end(), dividend.begin(),
44                  [gcd](int64_t &n) { return floor(n / gcd); });
45   divisor /= gcd;
46 }
47 
48 /// Check if the pos^th identifier can be represented as a division using upper
49 /// bound inequality at position `ubIneq` and lower bound inequality at position
50 /// `lbIneq`.
51 ///
52 /// Let `id` be the pos^th identifier, then `id` is equivalent to
53 /// `expr floordiv divisor` if there are constraints of the form:
54 ///      0 <= expr - divisor * id <= divisor - 1
55 /// Rearranging, we have:
56 ///       divisor * id - expr + (divisor - 1) >= 0  <-- Lower bound for 'id'
57 ///      -divisor * id + expr                 >= 0  <-- Upper bound for 'id'
58 ///
59 /// For example:
60 ///     32*k >= 16*i + j - 31                 <-- Lower bound for 'k'
61 ///     32*k  <= 16*i + j                     <-- Upper bound for 'k'
62 ///     expr = 16*i + j, divisor = 32
63 ///     k = ( 16*i + j ) floordiv 32
64 ///
65 ///     4q >= i + j - 2                       <-- Lower bound for 'q'
66 ///     4q <= i + j + 1                       <-- Upper bound for 'q'
67 ///     expr = i + j + 1, divisor = 4
68 ///     q = (i + j + 1) floordiv 4
69 //
70 /// This function also supports detecting divisions from bounds that are
71 /// strictly tighter than the division bounds described above, since tighter
72 /// bounds imply the division bounds. For example:
73 ///     4q - i - j + 2 >= 0                       <-- Lower bound for 'q'
74 ///    -4q + i + j     >= 0                       <-- Tight upper bound for 'q'
75 ///
76 /// To extract floor divisions with tighter bounds, we assume that that the
77 /// constraints are of the form:
78 ///     c <= expr - divisior * id <= divisor - 1, where 0 <= c <= divisor - 1
79 /// Rearranging, we have:
80 ///     divisor * id - expr + (divisor - 1) >= 0  <-- Lower bound for 'id'
81 ///    -divisor * id + expr - c             >= 0  <-- Upper bound for 'id'
82 ///
83 /// If successful, `expr` is set to dividend of the division and `divisor` is
84 /// set to the denominator of the division. The final division expression is
85 /// normalized by GCD.
86 static LogicalResult getDivRepr(const IntegerPolyhedron &cst, unsigned pos,
87                                 unsigned ubIneq, unsigned lbIneq,
88                                 SmallVector<int64_t, 8> &expr,
89                                 unsigned &divisor) {
90 
91   assert(pos <= cst.getNumIds() && "Invalid identifier position");
92   assert(ubIneq <= cst.getNumInequalities() &&
93          "Invalid upper bound inequality position");
94   assert(lbIneq <= cst.getNumInequalities() &&
95          "Invalid upper bound inequality position");
96 
97   // Extract divisor from the lower bound.
98   divisor = cst.atIneq(lbIneq, pos);
99 
100   // First, check if the constraints are opposite of each other except the
101   // constant term.
102   unsigned i = 0, e = 0;
103   for (i = 0, e = cst.getNumIds(); i < e; ++i)
104     if (cst.atIneq(ubIneq, i) != -cst.atIneq(lbIneq, i))
105       break;
106 
107   if (i < e)
108     return failure();
109 
110   // Then, check if the constant term is of the proper form.
111   // Due to the form of the upper/lower bound inequalities, the sum of their
112   // constants is `divisor - 1 - c`. From this, we can extract c:
113   int64_t constantSum = cst.atIneq(lbIneq, cst.getNumCols() - 1) +
114                         cst.atIneq(ubIneq, cst.getNumCols() - 1);
115   int64_t c = divisor - 1 - constantSum;
116 
117   // Check if `c` satisfies the condition `0 <= c <= divisor - 1`. This also
118   // implictly checks that `divisor` is positive.
119   if (!(c >= 0 && c <= divisor - 1))
120     return failure();
121 
122   // The inequality pair can be used to extract the division.
123   // Set `expr` to the dividend of the division except the constant term, which
124   // is set below.
125   expr.resize(cst.getNumCols(), 0);
126   for (i = 0, e = cst.getNumIds(); i < e; ++i)
127     if (i != pos)
128       expr[i] = cst.atIneq(ubIneq, i);
129 
130   // From the upper bound inequality's form, its constant term is equal to the
131   // constant term of `expr`, minus `c`. From this,
132   // constant term of `expr` = constant term of upper bound + `c`.
133   expr.back() = cst.atIneq(ubIneq, cst.getNumCols() - 1) + c;
134   normalizeDivisionByGCD(expr, divisor);
135 
136   return success();
137 }
138 
139 /// Check if the pos^th identifier can be expressed as a floordiv of an affine
140 /// function of other identifiers (where the divisor is a positive constant).
141 /// `foundRepr` contains a boolean for each identifier indicating if the
142 /// explicit representation for that identifier has already been computed.
143 /// Returns the upper and lower bound inequalities using which the floordiv can
144 /// be computed. If the representation could be computed, `dividend` and
145 /// `denominator` are set. If the representation could not be computed,
146 /// `llvm::None` is returned.
147 Optional<std::pair<unsigned, unsigned>> presburger_utils::computeSingleVarRepr(
148     const IntegerPolyhedron &cst, ArrayRef<bool> foundRepr, unsigned pos,
149     SmallVector<int64_t, 8> &dividend, unsigned &divisor) {
150   assert(pos < cst.getNumIds() && "invalid position");
151   assert(foundRepr.size() == cst.getNumIds() &&
152          "Size of foundRepr does not match total number of variables");
153 
154   SmallVector<unsigned, 4> lbIndices, ubIndices;
155   cst.getLowerAndUpperBoundIndices(pos, &lbIndices, &ubIndices);
156 
157   for (unsigned ubPos : ubIndices) {
158     for (unsigned lbPos : lbIndices) {
159       // Attempt to get divison representation from ubPos, lbPos.
160       if (failed(getDivRepr(cst, pos, ubPos, lbPos, dividend, divisor)))
161         continue;
162 
163       // Check if the inequalities depend on a variable for which
164       // an explicit representation has not been found yet.
165       // Exit to avoid circular dependencies between divisions.
166       unsigned c, f;
167       for (c = 0, f = cst.getNumIds(); c < f; ++c) {
168         if (c == pos)
169           continue;
170         if (!foundRepr[c] && dividend[c] != 0)
171           break;
172       }
173 
174       // Expression can't be constructed as it depends on a yet unknown
175       // identifier.
176       // TODO: Visit/compute the identifiers in an order so that this doesn't
177       // happen. More complex but much more efficient.
178       if (c < f)
179         continue;
180 
181       return std::make_pair(ubPos, lbPos);
182     }
183   }
184 
185   return llvm::None;
186 }
187