1 //===- Simplex.cpp - MLIR Simplex 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/Analysis/Presburger/Simplex.h"
10 #include "mlir/Analysis/Presburger/Matrix.h"
11 #include "mlir/Support/MathExtras.h"
12 #include "llvm/ADT/Optional.h"
13 
14 namespace mlir {
15 using Direction = Simplex::Direction;
16 
17 const int nullIndex = std::numeric_limits<int>::max();
18 
19 SimplexBase::SimplexBase(unsigned nVar, bool mustUseBigM)
20     : usingBigM(mustUseBigM), nRow(0), nCol(getNumFixedCols() + nVar),
21       nRedundant(0), tableau(0, nCol), empty(false) {
22   colUnknown.insert(colUnknown.begin(), getNumFixedCols(), nullIndex);
23   for (unsigned i = 0; i < nVar; ++i) {
24     var.emplace_back(Orientation::Column, /*restricted=*/false,
25                      /*pos=*/getNumFixedCols() + i);
26     colUnknown.push_back(i);
27   }
28 }
29 
30 const Simplex::Unknown &SimplexBase::unknownFromIndex(int index) const {
31   assert(index != nullIndex && "nullIndex passed to unknownFromIndex");
32   return index >= 0 ? var[index] : con[~index];
33 }
34 
35 const Simplex::Unknown &SimplexBase::unknownFromColumn(unsigned col) const {
36   assert(col < nCol && "Invalid column");
37   return unknownFromIndex(colUnknown[col]);
38 }
39 
40 const Simplex::Unknown &SimplexBase::unknownFromRow(unsigned row) const {
41   assert(row < nRow && "Invalid row");
42   return unknownFromIndex(rowUnknown[row]);
43 }
44 
45 Simplex::Unknown &SimplexBase::unknownFromIndex(int index) {
46   assert(index != nullIndex && "nullIndex passed to unknownFromIndex");
47   return index >= 0 ? var[index] : con[~index];
48 }
49 
50 Simplex::Unknown &SimplexBase::unknownFromColumn(unsigned col) {
51   assert(col < nCol && "Invalid column");
52   return unknownFromIndex(colUnknown[col]);
53 }
54 
55 Simplex::Unknown &SimplexBase::unknownFromRow(unsigned row) {
56   assert(row < nRow && "Invalid row");
57   return unknownFromIndex(rowUnknown[row]);
58 }
59 
60 /// Add a new row to the tableau corresponding to the given constant term and
61 /// list of coefficients. The coefficients are specified as a vector of
62 /// (variable index, coefficient) pairs.
63 unsigned SimplexBase::addRow(ArrayRef<int64_t> coeffs, bool makeRestricted) {
64   assert(coeffs.size() == var.size() + 1 &&
65          "Incorrect number of coefficients!");
66 
67   ++nRow;
68   // If the tableau is not big enough to accomodate the extra row, we extend it.
69   if (nRow >= tableau.getNumRows())
70     tableau.resizeVertically(nRow);
71   rowUnknown.push_back(~con.size());
72   con.emplace_back(Orientation::Row, makeRestricted, nRow - 1);
73 
74   // Zero out the new row.
75   tableau.fillRow(nRow - 1, 0);
76 
77   tableau(nRow - 1, 0) = 1;
78   tableau(nRow - 1, 1) = coeffs.back();
79   if (usingBigM) {
80     // When the lexicographic pivot rule is used, instead of the variables
81     //
82     // x, y, z ...
83     //
84     // we internally use the variables
85     //
86     // M, M + x, M + y, M + z, ...
87     //
88     // where M is the big M parameter. As such, when the user tries to add
89     // a row ax + by + cz + d, we express it in terms of our internal variables
90     // as -(a + b + c)M + a(M + x) + b(M + y) + c(M + z) + d.
91     int64_t bigMCoeff = 0;
92     for (unsigned i = 0; i < coeffs.size() - 1; ++i)
93       bigMCoeff -= coeffs[i];
94     // The coefficient to the big M parameter is stored in column 2.
95     tableau(nRow - 1, 2) = bigMCoeff;
96   }
97 
98   // Process each given variable coefficient.
99   for (unsigned i = 0; i < var.size(); ++i) {
100     unsigned pos = var[i].pos;
101     if (coeffs[i] == 0)
102       continue;
103 
104     if (var[i].orientation == Orientation::Column) {
105       // If a variable is in column position at column col, then we just add the
106       // coefficient for that variable (scaled by the common row denominator) to
107       // the corresponding entry in the new row.
108       tableau(nRow - 1, pos) += coeffs[i] * tableau(nRow - 1, 0);
109       continue;
110     }
111 
112     // If the variable is in row position, we need to add that row to the new
113     // row, scaled by the coefficient for the variable, accounting for the two
114     // rows potentially having different denominators. The new denominator is
115     // the lcm of the two.
116     int64_t lcm = mlir::lcm(tableau(nRow - 1, 0), tableau(pos, 0));
117     int64_t nRowCoeff = lcm / tableau(nRow - 1, 0);
118     int64_t idxRowCoeff = coeffs[i] * (lcm / tableau(pos, 0));
119     tableau(nRow - 1, 0) = lcm;
120     for (unsigned col = 1; col < nCol; ++col)
121       tableau(nRow - 1, col) =
122           nRowCoeff * tableau(nRow - 1, col) + idxRowCoeff * tableau(pos, col);
123   }
124 
125   normalizeRow(nRow - 1);
126   // Push to undo log along with the index of the new constraint.
127   undoLog.push_back(UndoLogEntry::RemoveLastConstraint);
128   return con.size() - 1;
129 }
130 
131 /// Normalize the row by removing factors that are common between the
132 /// denominator and all the numerator coefficients.
133 void SimplexBase::normalizeRow(unsigned row) {
134   int64_t gcd = 0;
135   for (unsigned col = 0; col < nCol; ++col) {
136     gcd = llvm::greatestCommonDivisor(gcd, std::abs(tableau(row, col)));
137     // If the gcd becomes 1 then the row is already normalized.
138     if (gcd == 1)
139       return;
140   }
141 
142   // Note that the gcd can never become zero since the first element of the row,
143   // the denominator, is non-zero.
144   assert(gcd != 0);
145   for (unsigned col = 0; col < nCol; ++col)
146     tableau(row, col) /= gcd;
147 }
148 
149 namespace {
150 bool signMatchesDirection(int64_t elem, Direction direction) {
151   assert(elem != 0 && "elem should not be 0");
152   return direction == Direction::Up ? elem > 0 : elem < 0;
153 }
154 
155 Direction flippedDirection(Direction direction) {
156   return direction == Direction::Up ? Direction::Down : Simplex::Direction::Up;
157 }
158 } // namespace
159 
160 Optional<SmallVector<Fraction, 8>> LexSimplex::getRationalLexMin() {
161   restoreRationalConsistency();
162   return getRationalSample();
163 }
164 
165 bool LexSimplex::rowIsViolated(unsigned row) const {
166   if (tableau(row, 2) < 0)
167     return true;
168   if (tableau(row, 2) == 0 && tableau(row, 1) < 0)
169     return true;
170   return false;
171 }
172 
173 Optional<unsigned> LexSimplex::maybeGetViolatedRow() const {
174   for (unsigned row = 0; row < nRow; ++row)
175     if (rowIsViolated(row))
176       return row;
177   return {};
178 }
179 
180 // We simply look for violated rows and keep trying to move them to column
181 // orientation, which always succeeds unless the constraints have no solution
182 // in which case we just give up and return.
183 void LexSimplex::restoreRationalConsistency() {
184   while (Optional<unsigned> maybeViolatedRow = maybeGetViolatedRow()) {
185     LogicalResult status = moveRowUnknownToColumn(*maybeViolatedRow);
186     if (failed(status))
187       return;
188   }
189 }
190 
191 // Move the row unknown to column orientation while preserving lexicopositivity
192 // of the basis transform.
193 //
194 // We only consider pivots where the pivot element is positive. Suppose no such
195 // pivot exists, i.e., some violated row has no positive coefficient for any
196 // basis unknown. The row can be represented as (s + c_1*u_1 + ... + c_n*u_n)/d,
197 // where d is the denominator, s is the sample value and the c_i are the basis
198 // coefficients. Since any feasible assignment of the basis satisfies u_i >= 0
199 // for all i, and we have s < 0 as well as c_i < 0 for all i, any feasible
200 // assignment would violate this row and therefore the constraints have no
201 // solution.
202 //
203 // We can preserve lexicopositivity by picking the pivot column with positive
204 // pivot element that makes the lexicographically smallest change to the sample
205 // point.
206 //
207 // Proof. Let
208 // x = (x_1, ... x_n) be the variables,
209 // z = (z_1, ... z_m) be the constraints,
210 // y = (y_1, ... y_n) be the current basis, and
211 // define w = (x_1, ... x_n, z_1, ... z_m) = B*y + s.
212 // B is basically the simplex tableau of our implementation except that instead
213 // of only describing the transform to get back the non-basis unknowns, it
214 // defines the values of all the unknowns in terms of the basis unknowns.
215 // Similarly, s is the column for the sample value.
216 //
217 // Our goal is to show that each column in B, restricted to the first n
218 // rows, is lexicopositive after the pivot if it is so before. This is
219 // equivalent to saying the columns in the whole matrix are lexicopositive;
220 // there must be some non-zero element in every column in the first n rows since
221 // the n variables cannot be spanned without using all the n basis unknowns.
222 //
223 // Consider a pivot where z_i replaces y_j in the basis. Recall the pivot
224 // transform for the tableau derived for SimplexBase::pivot:
225 //
226 //            pivot col    other col                   pivot col    other col
227 // pivot row     a             b       ->   pivot row     1/a         -b/a
228 // other row     c             d            other row     c/a        d - bc/a
229 //
230 // Similarly, a pivot results in B changing to B' and c to c'; the difference
231 // between the tableau and these matrices B and B' is that there is no special
232 // case for the pivot row, since it continues to represent the same unknown. The
233 // same formula applies for all rows:
234 //
235 // B'.col(j) = B.col(j) / B(i,j)
236 // B'.col(k) = B.col(k) - B(i,k) * B.col(j) / B(i,j) for k != j
237 // and similarly, s' = s - s_i * B.col(j) / B(i,j).
238 //
239 // Since the row is violated, we have s_i < 0, so the change in sample value
240 // when pivoting with column a is lexicographically smaller than that when
241 // pivoting with column b iff B.col(a) / B(i, a) is lexicographically smaller
242 // than B.col(b) / B(i, b).
243 //
244 // Since B(i, j) > 0, column j remains lexicopositive.
245 //
246 // For the other columns, suppose C.col(k) is not lexicopositive.
247 // This means that for some p, for all t < p,
248 // C(t,k) = 0 => B(t,k) = B(t,j) * B(i,k) / B(i,j) and
249 // C(t,k) < 0 => B(p,k) < B(t,j) * B(i,k) / B(i,j),
250 // which is in contradiction to the fact that B.col(j) / B(i,j) must be
251 // lexicographically smaller than B.col(k) / B(i,k), since it lexicographically
252 // minimizes the change in sample value.
253 LogicalResult LexSimplex::moveRowUnknownToColumn(unsigned row) {
254   Optional<unsigned> maybeColumn;
255   for (unsigned col = 3; col < nCol; ++col) {
256     if (tableau(row, col) <= 0)
257       continue;
258     maybeColumn =
259         !maybeColumn ? col : getLexMinPivotColumn(row, *maybeColumn, col);
260   }
261 
262   if (!maybeColumn) {
263     markEmpty();
264     return failure();
265   }
266 
267   pivot(row, *maybeColumn);
268   return success();
269 }
270 
271 unsigned LexSimplex::getLexMinPivotColumn(unsigned row, unsigned colA,
272                                           unsigned colB) const {
273   // A pivot causes the following change. (in the diagram the matrix elements
274   // are shown as rationals and there is no common denominator used)
275   //
276   //            pivot col    big M col      const col
277   // pivot row     a            p               b
278   // other row     c            q               d
279   //                        |
280   //                        v
281   //
282   //            pivot col    big M col      const col
283   // pivot row     1/a         -p/a           -b/a
284   // other row     c/a        q - pc/a       d - bc/a
285   //
286   // Let the sample value of the pivot row be s = pM + b before the pivot. Since
287   // the pivot row represents a violated constraint we know that s < 0.
288   //
289   // If the variable is a non-pivot column, its sample value is zero before and
290   // after the pivot.
291   //
292   // If the variable is the pivot column, then its sample value goes from 0 to
293   // (-p/a)M + (-b/a), i.e. 0 to -(pM + b)/a. Thus the change in the sample
294   // value is -s/a.
295   //
296   // If the variable is the pivot row, it sampel value goes from s to 0, for a
297   // change of -s.
298   //
299   // If the variable is a non-pivot row, its sample value changes from
300   // qM + d to qM + d + (-pc/a)M + (-bc/a). Thus the change in sample value
301   // is -(pM + b)(c/a) = -sc/a.
302   //
303   // Thus the change in sample value is either 0, -s/a, -s, or -sc/a. Here -s is
304   // fixed for all calls to this function since the row and tableau are fixed.
305   // The callee just wants to compare the return values with the return value of
306   // other invocations of the same function. So the -s is common for all
307   // comparisons involved and can be ignored, since -s is strictly positive.
308   //
309   // Thus we take away this common factor and just return 0, 1/a, 1, or c/a as
310   // appropriate. This allows us to run the entire algorithm without ever having
311   // to fix a value of M.
312   auto getSampleChangeCoeffForVar = [this, row](unsigned col,
313                                                 const Unknown &u) -> Fraction {
314     int64_t a = tableau(row, col);
315     if (u.orientation == Orientation::Column) {
316       // Pivot column case.
317       if (u.pos == col)
318         return {1, a};
319 
320       // Non-pivot column case.
321       return {0, 1};
322     }
323 
324     // Pivot row case.
325     if (u.pos == row)
326       return {1, 1};
327 
328     // Non-pivot row case.
329     int64_t c = tableau(u.pos, col);
330     return {c, a};
331   };
332 
333   for (const Unknown &u : var) {
334     Fraction changeA = getSampleChangeCoeffForVar(colA, u);
335     Fraction changeB = getSampleChangeCoeffForVar(colB, u);
336     if (changeA < changeB)
337       return colA;
338     if (changeA > changeB)
339       return colB;
340   }
341 
342   // If we reached here, both result in exactly the same changes, so it
343   // doesn't matter which we return.
344   return colA;
345 }
346 
347 /// Find a pivot to change the sample value of the row in the specified
348 /// direction. The returned pivot row will involve `row` if and only if the
349 /// unknown is unbounded in the specified direction.
350 ///
351 /// To increase (resp. decrease) the value of a row, we need to find a live
352 /// column with a non-zero coefficient. If the coefficient is positive, we need
353 /// to increase (decrease) the value of the column, and if the coefficient is
354 /// negative, we need to decrease (increase) the value of the column. Also,
355 /// we cannot decrease the sample value of restricted columns.
356 ///
357 /// If multiple columns are valid, we break ties by considering a lexicographic
358 /// ordering where we prefer unknowns with lower index.
359 Optional<SimplexBase::Pivot> Simplex::findPivot(int row,
360                                                 Direction direction) const {
361   Optional<unsigned> col;
362   for (unsigned j = 2; j < nCol; ++j) {
363     int64_t elem = tableau(row, j);
364     if (elem == 0)
365       continue;
366 
367     if (unknownFromColumn(j).restricted &&
368         !signMatchesDirection(elem, direction))
369       continue;
370     if (!col || colUnknown[j] < colUnknown[*col])
371       col = j;
372   }
373 
374   if (!col)
375     return {};
376 
377   Direction newDirection =
378       tableau(row, *col) < 0 ? flippedDirection(direction) : direction;
379   Optional<unsigned> maybePivotRow = findPivotRow(row, newDirection, *col);
380   return Pivot{maybePivotRow.getValueOr(row), *col};
381 }
382 
383 /// Swap the associated unknowns for the row and the column.
384 ///
385 /// First we swap the index associated with the row and column. Then we update
386 /// the unknowns to reflect their new position and orientation.
387 void SimplexBase::swapRowWithCol(unsigned row, unsigned col) {
388   std::swap(rowUnknown[row], colUnknown[col]);
389   Unknown &uCol = unknownFromColumn(col);
390   Unknown &uRow = unknownFromRow(row);
391   uCol.orientation = Orientation::Column;
392   uRow.orientation = Orientation::Row;
393   uCol.pos = col;
394   uRow.pos = row;
395 }
396 
397 void SimplexBase::pivot(Pivot pair) { pivot(pair.row, pair.column); }
398 
399 /// Pivot pivotRow and pivotCol.
400 ///
401 /// Let R be the pivot row unknown and let C be the pivot col unknown.
402 /// Since initially R = a*C + sum b_i * X_i
403 /// (where the sum is over the other column's unknowns, x_i)
404 /// C = (R - (sum b_i * X_i))/a
405 ///
406 /// Let u be some other row unknown.
407 /// u = c*C + sum d_i * X_i
408 /// So u = c*(R - sum b_i * X_i)/a + sum d_i * X_i
409 ///
410 /// This results in the following transform:
411 ///            pivot col    other col                   pivot col    other col
412 /// pivot row     a             b       ->   pivot row     1/a         -b/a
413 /// other row     c             d            other row     c/a        d - bc/a
414 ///
415 /// Taking into account the common denominators p and q:
416 ///
417 ///            pivot col    other col                    pivot col   other col
418 /// pivot row     a/p          b/p     ->   pivot row      p/a         -b/a
419 /// other row     c/q          d/q          other row     cp/aq    (da - bc)/aq
420 ///
421 /// The pivot row transform is accomplished be swapping a with the pivot row's
422 /// common denominator and negating the pivot row except for the pivot column
423 /// element.
424 void SimplexBase::pivot(unsigned pivotRow, unsigned pivotCol) {
425   assert(pivotCol >= getNumFixedCols() && "Refusing to pivot invalid column");
426 
427   swapRowWithCol(pivotRow, pivotCol);
428   std::swap(tableau(pivotRow, 0), tableau(pivotRow, pivotCol));
429   // We need to negate the whole pivot row except for the pivot column.
430   if (tableau(pivotRow, 0) < 0) {
431     // If the denominator is negative, we negate the row by simply negating the
432     // denominator.
433     tableau(pivotRow, 0) = -tableau(pivotRow, 0);
434     tableau(pivotRow, pivotCol) = -tableau(pivotRow, pivotCol);
435   } else {
436     for (unsigned col = 1; col < nCol; ++col) {
437       if (col == pivotCol)
438         continue;
439       tableau(pivotRow, col) = -tableau(pivotRow, col);
440     }
441   }
442   normalizeRow(pivotRow);
443 
444   for (unsigned row = 0; row < nRow; ++row) {
445     if (row == pivotRow)
446       continue;
447     if (tableau(row, pivotCol) == 0) // Nothing to do.
448       continue;
449     tableau(row, 0) *= tableau(pivotRow, 0);
450     for (unsigned j = 1; j < nCol; ++j) {
451       if (j == pivotCol)
452         continue;
453       // Add rather than subtract because the pivot row has been negated.
454       tableau(row, j) = tableau(row, j) * tableau(pivotRow, 0) +
455                         tableau(row, pivotCol) * tableau(pivotRow, j);
456     }
457     tableau(row, pivotCol) *= tableau(pivotRow, pivotCol);
458     normalizeRow(row);
459   }
460 }
461 
462 /// Perform pivots until the unknown has a non-negative sample value or until
463 /// no more upward pivots can be performed. Return success if we were able to
464 /// bring the row to a non-negative sample value, and failure otherwise.
465 LogicalResult Simplex::restoreRow(Unknown &u) {
466   assert(u.orientation == Orientation::Row &&
467          "unknown should be in row position");
468 
469   while (tableau(u.pos, 1) < 0) {
470     Optional<Pivot> maybePivot = findPivot(u.pos, Direction::Up);
471     if (!maybePivot)
472       break;
473 
474     pivot(*maybePivot);
475     if (u.orientation == Orientation::Column)
476       return success(); // the unknown is unbounded above.
477   }
478   return success(tableau(u.pos, 1) >= 0);
479 }
480 
481 /// Find a row that can be used to pivot the column in the specified direction.
482 /// This returns an empty optional if and only if the column is unbounded in the
483 /// specified direction (ignoring skipRow, if skipRow is set).
484 ///
485 /// If skipRow is set, this row is not considered, and (if it is restricted) its
486 /// restriction may be violated by the returned pivot. Usually, skipRow is set
487 /// because we don't want to move it to column position unless it is unbounded,
488 /// and we are either trying to increase the value of skipRow or explicitly
489 /// trying to make skipRow negative, so we are not concerned about this.
490 ///
491 /// If the direction is up (resp. down) and a restricted row has a negative
492 /// (positive) coefficient for the column, then this row imposes a bound on how
493 /// much the sample value of the column can change. Such a row with constant
494 /// term c and coefficient f for the column imposes a bound of c/|f| on the
495 /// change in sample value (in the specified direction). (note that c is
496 /// non-negative here since the row is restricted and the tableau is consistent)
497 ///
498 /// We iterate through the rows and pick the row which imposes the most
499 /// stringent bound, since pivoting with a row changes the row's sample value to
500 /// 0 and hence saturates the bound it imposes. We break ties between rows that
501 /// impose the same bound by considering a lexicographic ordering where we
502 /// prefer unknowns with lower index value.
503 Optional<unsigned> Simplex::findPivotRow(Optional<unsigned> skipRow,
504                                          Direction direction,
505                                          unsigned col) const {
506   Optional<unsigned> retRow;
507   // Initialize these to zero in order to silence a warning about retElem and
508   // retConst being used uninitialized in the initialization of `diff` below. In
509   // reality, these are always initialized when that line is reached since these
510   // are set whenever retRow is set.
511   int64_t retElem = 0, retConst = 0;
512   for (unsigned row = nRedundant; row < nRow; ++row) {
513     if (skipRow && row == *skipRow)
514       continue;
515     int64_t elem = tableau(row, col);
516     if (elem == 0)
517       continue;
518     if (!unknownFromRow(row).restricted)
519       continue;
520     if (signMatchesDirection(elem, direction))
521       continue;
522     int64_t constTerm = tableau(row, 1);
523 
524     if (!retRow) {
525       retRow = row;
526       retElem = elem;
527       retConst = constTerm;
528       continue;
529     }
530 
531     int64_t diff = retConst * elem - constTerm * retElem;
532     if ((diff == 0 && rowUnknown[row] < rowUnknown[*retRow]) ||
533         (diff != 0 && !signMatchesDirection(diff, direction))) {
534       retRow = row;
535       retElem = elem;
536       retConst = constTerm;
537     }
538   }
539   return retRow;
540 }
541 
542 bool SimplexBase::isEmpty() const { return empty; }
543 
544 void SimplexBase::swapRows(unsigned i, unsigned j) {
545   if (i == j)
546     return;
547   tableau.swapRows(i, j);
548   std::swap(rowUnknown[i], rowUnknown[j]);
549   unknownFromRow(i).pos = i;
550   unknownFromRow(j).pos = j;
551 }
552 
553 void SimplexBase::swapColumns(unsigned i, unsigned j) {
554   assert(i < nCol && j < nCol && "Invalid columns provided!");
555   if (i == j)
556     return;
557   tableau.swapColumns(i, j);
558   std::swap(colUnknown[i], colUnknown[j]);
559   unknownFromColumn(i).pos = i;
560   unknownFromColumn(j).pos = j;
561 }
562 
563 /// Mark this tableau empty and push an entry to the undo stack.
564 void SimplexBase::markEmpty() {
565   // If the set is already empty, then we shouldn't add another UnmarkEmpty log
566   // entry, since in that case the Simplex will be erroneously marked as
567   // non-empty when rolling back past this point.
568   if (empty)
569     return;
570   undoLog.push_back(UndoLogEntry::UnmarkEmpty);
571   empty = true;
572 }
573 
574 /// Add an inequality to the tableau. If coeffs is c_0, c_1, ... c_n, where n
575 /// is the current number of variables, then the corresponding inequality is
576 /// c_n + c_0*x_0 + c_1*x_1 + ... + c_{n-1}*x_{n-1} >= 0.
577 ///
578 /// We add the inequality and mark it as restricted. We then try to make its
579 /// sample value non-negative. If this is not possible, the tableau has become
580 /// empty and we mark it as such.
581 void Simplex::addInequality(ArrayRef<int64_t> coeffs) {
582   unsigned conIndex = addRow(coeffs, /*makeRestricted=*/true);
583   LogicalResult result = restoreRow(con[conIndex]);
584   if (failed(result))
585     markEmpty();
586 }
587 
588 /// Add an equality to the tableau. If coeffs is c_0, c_1, ... c_n, where n
589 /// is the current number of variables, then the corresponding equality is
590 /// c_n + c_0*x_0 + c_1*x_1 + ... + c_{n-1}*x_{n-1} == 0.
591 ///
592 /// We simply add two opposing inequalities, which force the expression to
593 /// be zero.
594 void SimplexBase::addEquality(ArrayRef<int64_t> coeffs) {
595   addInequality(coeffs);
596   SmallVector<int64_t, 8> negatedCoeffs;
597   for (int64_t coeff : coeffs)
598     negatedCoeffs.emplace_back(-coeff);
599   addInequality(negatedCoeffs);
600 }
601 
602 unsigned SimplexBase::getNumVariables() const { return var.size(); }
603 unsigned SimplexBase::getNumConstraints() const { return con.size(); }
604 
605 /// Return a snapshot of the current state. This is just the current size of the
606 /// undo log.
607 unsigned SimplexBase::getSnapshot() const { return undoLog.size(); }
608 
609 unsigned SimplexBase::getSnapshotBasis() {
610   SmallVector<int, 8> basis;
611   for (int index : colUnknown) {
612     if (index != nullIndex)
613       basis.push_back(index);
614   }
615   savedBases.push_back(std::move(basis));
616 
617   undoLog.emplace_back(UndoLogEntry::RestoreBasis);
618   return undoLog.size() - 1;
619 }
620 
621 void SimplexBase::removeLastConstraintRowOrientation() {
622   assert(con.back().orientation == Orientation::Row);
623 
624   // Move this unknown to the last row and remove the last row from the
625   // tableau.
626   swapRows(con.back().pos, nRow - 1);
627   // It is not strictly necessary to shrink the tableau, but for now we
628   // maintain the invariant that the tableau has exactly nRow rows.
629   tableau.resizeVertically(nRow - 1);
630   nRow--;
631   rowUnknown.pop_back();
632   con.pop_back();
633 }
634 
635 // This doesn't find a pivot row only if the column has zero
636 // coefficients for every row.
637 //
638 // If the unknown is a constraint, this can't happen, since it was added
639 // initially as a row. Such a row could never have been pivoted to a column. So
640 // a pivot row will always be found if we have a constraint.
641 //
642 // If we have a variable, then the column has zero coefficients for every row
643 // iff no constraints have been added with a non-zero coefficient for this row.
644 Optional<unsigned> SimplexBase::findAnyPivotRow(unsigned col) {
645   for (unsigned row = nRedundant; row < nRow; ++row)
646     if (tableau(row, col) != 0)
647       return row;
648   return {};
649 }
650 
651 // It's not valid to remove the constraint by deleting the column since this
652 // would result in an invalid basis.
653 void Simplex::undoLastConstraint() {
654   if (con.back().orientation == Orientation::Column) {
655     // We try to find any pivot row for this column that preserves tableau
656     // consistency (except possibly the column itself, which is going to be
657     // deallocated anyway).
658     //
659     // If no pivot row is found in either direction, then the unknown is
660     // unbounded in both directions and we are free to perform any pivot at
661     // all. To do this, we just need to find any row with a non-zero
662     // coefficient for the column. findAnyPivotRow will always be able to
663     // find such a row for a constraint.
664     unsigned column = con.back().pos;
665     if (Optional<unsigned> maybeRow = findPivotRow({}, Direction::Up, column)) {
666       pivot(*maybeRow, column);
667     } else if (Optional<unsigned> maybeRow =
668                    findPivotRow({}, Direction::Down, column)) {
669       pivot(*maybeRow, column);
670     } else {
671       Optional<unsigned> row = findAnyPivotRow(column);
672       assert(row.hasValue() && "Pivot should always exist for a constraint!");
673       pivot(*row, column);
674     }
675   }
676   removeLastConstraintRowOrientation();
677 }
678 
679 // It's not valid to remove the constraint by deleting the column since this
680 // would result in an invalid basis.
681 void LexSimplex::undoLastConstraint() {
682   if (con.back().orientation == Orientation::Column) {
683     // When removing the last constraint during a rollback, we just need to find
684     // any pivot at all, i.e., any row with non-zero coefficient for the
685     // column, because when rolling back a lexicographic simplex, we always
686     // end by restoring the exact basis that was present at the time of the
687     // snapshot, so what pivots we perform while undoing doesn't matter as
688     // long as we get the unknown to row orientation and remove it.
689     unsigned column = con.back().pos;
690     Optional<unsigned> row = findAnyPivotRow(column);
691     assert(row.hasValue() && "Pivot should always exist for a constraint!");
692     pivot(*row, column);
693   }
694   removeLastConstraintRowOrientation();
695 }
696 
697 void SimplexBase::undo(UndoLogEntry entry) {
698   if (entry == UndoLogEntry::RemoveLastConstraint) {
699     // Simplex and LexSimplex handle this differently, so we call out to a
700     // virtual function to handle this.
701     undoLastConstraint();
702   } else if (entry == UndoLogEntry::RemoveLastVariable) {
703     // Whenever we are rolling back the addition of a variable, it is guaranteed
704     // that the variable will be in column position.
705     //
706     // We can see this as follows: any constraint that depends on this variable
707     // was added after this variable was added, so the addition of such
708     // constraints should already have been rolled back by the time we get to
709     // rolling back the addition of the variable. Therefore, no constraint
710     // currently has a component along the variable, so the variable itself must
711     // be part of the basis.
712     assert(var.back().orientation == Orientation::Column &&
713            "Variable to be removed must be in column orientation!");
714 
715     // Move this variable to the last column and remove the column from the
716     // tableau.
717     swapColumns(var.back().pos, nCol - 1);
718     tableau.resizeHorizontally(nCol - 1);
719     var.pop_back();
720     colUnknown.pop_back();
721     nCol--;
722   } else if (entry == UndoLogEntry::UnmarkEmpty) {
723     empty = false;
724   } else if (entry == UndoLogEntry::UnmarkLastRedundant) {
725     nRedundant--;
726   } else if (entry == UndoLogEntry::RestoreBasis) {
727     assert(!savedBases.empty() && "No bases saved!");
728 
729     SmallVector<int, 8> basis = std::move(savedBases.back());
730     savedBases.pop_back();
731 
732     for (int index : basis) {
733       Unknown &u = unknownFromIndex(index);
734       if (u.orientation == Orientation::Column)
735         continue;
736       for (unsigned col = getNumFixedCols(); col < nCol; col++) {
737         assert(colUnknown[col] != nullIndex &&
738                "Column should not be a fixed column!");
739         if (std::find(basis.begin(), basis.end(), colUnknown[col]) !=
740             basis.end())
741           continue;
742         if (tableau(u.pos, col) == 0)
743           continue;
744         pivot(u.pos, col);
745         break;
746       }
747 
748       assert(u.orientation == Orientation::Column && "No pivot found!");
749     }
750   }
751 }
752 
753 /// Rollback to the specified snapshot.
754 ///
755 /// We undo all the log entries until the log size when the snapshot was taken
756 /// is reached.
757 void SimplexBase::rollback(unsigned snapshot) {
758   while (undoLog.size() > snapshot) {
759     undo(undoLog.back());
760     undoLog.pop_back();
761   }
762 }
763 
764 void SimplexBase::appendVariable(unsigned count) {
765   if (count == 0)
766     return;
767   var.reserve(var.size() + count);
768   colUnknown.reserve(colUnknown.size() + count);
769   for (unsigned i = 0; i < count; ++i) {
770     nCol++;
771     var.emplace_back(Orientation::Column, /*restricted=*/false,
772                      /*pos=*/nCol - 1);
773     colUnknown.push_back(var.size() - 1);
774   }
775   tableau.resizeHorizontally(nCol);
776   undoLog.insert(undoLog.end(), count, UndoLogEntry::RemoveLastVariable);
777 }
778 
779 /// Add all the constraints from the given IntegerPolyhedron.
780 void SimplexBase::intersectIntegerPolyhedron(const IntegerPolyhedron &poly) {
781   assert(poly.getNumIds() == getNumVariables() &&
782          "IntegerPolyhedron must have same dimensionality as simplex");
783   for (unsigned i = 0, e = poly.getNumInequalities(); i < e; ++i)
784     addInequality(poly.getInequality(i));
785   for (unsigned i = 0, e = poly.getNumEqualities(); i < e; ++i)
786     addEquality(poly.getEquality(i));
787 }
788 
789 Optional<Fraction> Simplex::computeRowOptimum(Direction direction,
790                                               unsigned row) {
791   // Keep trying to find a pivot for the row in the specified direction.
792   while (Optional<Pivot> maybePivot = findPivot(row, direction)) {
793     // If findPivot returns a pivot involving the row itself, then the optimum
794     // is unbounded, so we return None.
795     if (maybePivot->row == row)
796       return {};
797     pivot(*maybePivot);
798   }
799 
800   // The row has reached its optimal sample value, which we return.
801   // The sample value is the entry in the constant column divided by the common
802   // denominator for this row.
803   return Fraction(tableau(row, 1), tableau(row, 0));
804 }
805 
806 /// Compute the optimum of the specified expression in the specified direction,
807 /// or None if it is unbounded.
808 Optional<Fraction> Simplex::computeOptimum(Direction direction,
809                                            ArrayRef<int64_t> coeffs) {
810   assert(!empty && "Simplex should not be empty");
811 
812   unsigned snapshot = getSnapshot();
813   unsigned conIndex = addRow(coeffs);
814   unsigned row = con[conIndex].pos;
815   Optional<Fraction> optimum = computeRowOptimum(direction, row);
816   rollback(snapshot);
817   return optimum;
818 }
819 
820 Optional<Fraction> Simplex::computeOptimum(Direction direction, Unknown &u) {
821   assert(!empty && "Simplex should not be empty!");
822   if (u.orientation == Orientation::Column) {
823     unsigned column = u.pos;
824     Optional<unsigned> pivotRow = findPivotRow({}, direction, column);
825     // If no pivot is returned, the constraint is unbounded in the specified
826     // direction.
827     if (!pivotRow)
828       return {};
829     pivot(*pivotRow, column);
830   }
831 
832   unsigned row = u.pos;
833   Optional<Fraction> optimum = computeRowOptimum(direction, row);
834   if (u.restricted && direction == Direction::Down &&
835       (!optimum || *optimum < Fraction(0, 1))) {
836     if (failed(restoreRow(u)))
837       llvm_unreachable("Could not restore row!");
838   }
839   return optimum;
840 }
841 
842 bool Simplex::isBoundedAlongConstraint(unsigned constraintIndex) {
843   assert(!empty && "It is not meaningful to ask whether a direction is bounded "
844                    "in an empty set.");
845   // The constraint's perpendicular is already bounded below, since it is a
846   // constraint. If it is also bounded above, we can return true.
847   return computeOptimum(Direction::Up, con[constraintIndex]).hasValue();
848 }
849 
850 /// Redundant constraints are those that are in row orientation and lie in
851 /// rows 0 to nRedundant - 1.
852 bool Simplex::isMarkedRedundant(unsigned constraintIndex) const {
853   const Unknown &u = con[constraintIndex];
854   return u.orientation == Orientation::Row && u.pos < nRedundant;
855 }
856 
857 /// Mark the specified row redundant.
858 ///
859 /// This is done by moving the unknown to the end of the block of redundant
860 /// rows (namely, to row nRedundant) and incrementing nRedundant to
861 /// accomodate the new redundant row.
862 void Simplex::markRowRedundant(Unknown &u) {
863   assert(u.orientation == Orientation::Row &&
864          "Unknown should be in row position!");
865   assert(u.pos >= nRedundant && "Unknown is already marked redundant!");
866   swapRows(u.pos, nRedundant);
867   ++nRedundant;
868   undoLog.emplace_back(UndoLogEntry::UnmarkLastRedundant);
869 }
870 
871 /// Find a subset of constraints that is redundant and mark them redundant.
872 void Simplex::detectRedundant() {
873   // It is not meaningful to talk about redundancy for empty sets.
874   if (empty)
875     return;
876 
877   // Iterate through the constraints and check for each one if it can attain
878   // negative sample values. If it can, it's not redundant. Otherwise, it is.
879   // We mark redundant constraints redundant.
880   //
881   // Constraints that get marked redundant in one iteration are not respected
882   // when checking constraints in later iterations. This prevents, for example,
883   // two identical constraints both being marked redundant since each is
884   // redundant given the other one. In this example, only the first of the
885   // constraints that is processed will get marked redundant, as it should be.
886   for (Unknown &u : con) {
887     if (u.orientation == Orientation::Column) {
888       unsigned column = u.pos;
889       Optional<unsigned> pivotRow = findPivotRow({}, Direction::Down, column);
890       // If no downward pivot is returned, the constraint is unbounded below
891       // and hence not redundant.
892       if (!pivotRow)
893         continue;
894       pivot(*pivotRow, column);
895     }
896 
897     unsigned row = u.pos;
898     Optional<Fraction> minimum = computeRowOptimum(Direction::Down, row);
899     if (!minimum || *minimum < Fraction(0, 1)) {
900       // Constraint is unbounded below or can attain negative sample values and
901       // hence is not redundant.
902       if (failed(restoreRow(u)))
903         llvm_unreachable("Could not restore non-redundant row!");
904       continue;
905     }
906 
907     markRowRedundant(u);
908   }
909 }
910 
911 bool Simplex::isUnbounded() {
912   if (empty)
913     return false;
914 
915   SmallVector<int64_t, 8> dir(var.size() + 1);
916   for (unsigned i = 0; i < var.size(); ++i) {
917     dir[i] = 1;
918 
919     Optional<Fraction> maybeMax = computeOptimum(Direction::Up, dir);
920     if (!maybeMax)
921       return true;
922 
923     Optional<Fraction> maybeMin = computeOptimum(Direction::Down, dir);
924     if (!maybeMin)
925       return true;
926 
927     dir[i] = 0;
928   }
929   return false;
930 }
931 
932 /// Make a tableau to represent a pair of points in the original tableau.
933 ///
934 /// The product constraints and variables are stored as: first A's, then B's.
935 ///
936 /// The product tableau has row layout:
937 ///   A's redundant rows, B's redundant rows, A's other rows, B's other rows.
938 ///
939 /// It has column layout:
940 ///   denominator, constant, A's columns, B's columns.
941 Simplex Simplex::makeProduct(const Simplex &a, const Simplex &b) {
942   unsigned numVar = a.getNumVariables() + b.getNumVariables();
943   unsigned numCon = a.getNumConstraints() + b.getNumConstraints();
944   Simplex result(numVar);
945 
946   result.tableau.resizeVertically(numCon);
947   result.empty = a.empty || b.empty;
948 
949   auto concat = [](ArrayRef<Unknown> v, ArrayRef<Unknown> w) {
950     SmallVector<Unknown, 8> result;
951     result.reserve(v.size() + w.size());
952     result.insert(result.end(), v.begin(), v.end());
953     result.insert(result.end(), w.begin(), w.end());
954     return result;
955   };
956   result.con = concat(a.con, b.con);
957   result.var = concat(a.var, b.var);
958 
959   auto indexFromBIndex = [&](int index) {
960     return index >= 0 ? a.getNumVariables() + index
961                       : ~(a.getNumConstraints() + ~index);
962   };
963 
964   result.colUnknown.assign(2, nullIndex);
965   for (unsigned i = 2; i < a.nCol; ++i) {
966     result.colUnknown.push_back(a.colUnknown[i]);
967     result.unknownFromIndex(result.colUnknown.back()).pos =
968         result.colUnknown.size() - 1;
969   }
970   for (unsigned i = 2; i < b.nCol; ++i) {
971     result.colUnknown.push_back(indexFromBIndex(b.colUnknown[i]));
972     result.unknownFromIndex(result.colUnknown.back()).pos =
973         result.colUnknown.size() - 1;
974   }
975 
976   auto appendRowFromA = [&](unsigned row) {
977     for (unsigned col = 0; col < a.nCol; ++col)
978       result.tableau(result.nRow, col) = a.tableau(row, col);
979     result.rowUnknown.push_back(a.rowUnknown[row]);
980     result.unknownFromIndex(result.rowUnknown.back()).pos =
981         result.rowUnknown.size() - 1;
982     result.nRow++;
983   };
984 
985   // Also fixes the corresponding entry in rowUnknown and var/con (as the case
986   // may be).
987   auto appendRowFromB = [&](unsigned row) {
988     result.tableau(result.nRow, 0) = b.tableau(row, 0);
989     result.tableau(result.nRow, 1) = b.tableau(row, 1);
990 
991     unsigned offset = a.nCol - 2;
992     for (unsigned col = 2; col < b.nCol; ++col)
993       result.tableau(result.nRow, offset + col) = b.tableau(row, col);
994     result.rowUnknown.push_back(indexFromBIndex(b.rowUnknown[row]));
995     result.unknownFromIndex(result.rowUnknown.back()).pos =
996         result.rowUnknown.size() - 1;
997     result.nRow++;
998   };
999 
1000   result.nRedundant = a.nRedundant + b.nRedundant;
1001   for (unsigned row = 0; row < a.nRedundant; ++row)
1002     appendRowFromA(row);
1003   for (unsigned row = 0; row < b.nRedundant; ++row)
1004     appendRowFromB(row);
1005   for (unsigned row = a.nRedundant; row < a.nRow; ++row)
1006     appendRowFromA(row);
1007   for (unsigned row = b.nRedundant; row < b.nRow; ++row)
1008     appendRowFromB(row);
1009 
1010   return result;
1011 }
1012 
1013 Optional<SmallVector<Fraction, 8>> SimplexBase::getRationalSample() const {
1014   if (empty)
1015     return {};
1016 
1017   SmallVector<Fraction, 8> sample;
1018   sample.reserve(var.size());
1019   // Push the sample value for each variable into the vector.
1020   for (const Unknown &u : var) {
1021     if (u.orientation == Orientation::Column) {
1022       // If the variable is in column position, its sample value is zero.
1023       sample.emplace_back(0, 1);
1024     } else {
1025       int64_t denom = tableau(u.pos, 0);
1026 
1027       // When the big M parameter is being used, each variable x is represented
1028       // as M + x, so its sample value is finite only if it is of the form
1029       // 1*M + c. If the coefficient of M is not one then the sample value is
1030       // infinite, and we return an empty optional.
1031       if (usingBigM)
1032         if (tableau(u.pos, 2) != denom)
1033           return {};
1034 
1035       // Otherwise, If the variable is in row position, its sample value is the
1036       // entry in the constant column divided by the denominator.
1037       sample.emplace_back(tableau(u.pos, 1), denom);
1038     }
1039   }
1040   return sample;
1041 }
1042 
1043 Optional<SmallVector<int64_t, 8>> Simplex::getSamplePointIfIntegral() const {
1044   // If the tableau is empty, no sample point exists.
1045   if (empty)
1046     return {};
1047 
1048   // The value will always exist since the Simplex is non-empty.
1049   SmallVector<Fraction, 8> rationalSample = *getRationalSample();
1050   SmallVector<int64_t, 8> integerSample;
1051   integerSample.reserve(var.size());
1052   for (const Fraction &coord : rationalSample) {
1053     // If the sample is non-integral, return None.
1054     if (coord.num % coord.den != 0)
1055       return {};
1056     integerSample.push_back(coord.num / coord.den);
1057   }
1058   return integerSample;
1059 }
1060 
1061 /// Given a simplex for a polytope, construct a new simplex whose variables are
1062 /// identified with a pair of points (x, y) in the original polytope. Supports
1063 /// some operations needed for generalized basis reduction. In what follows,
1064 /// dotProduct(x, y) = x_1 * y_1 + x_2 * y_2 + ... x_n * y_n where n is the
1065 /// dimension of the original polytope.
1066 ///
1067 /// This supports adding equality constraints dotProduct(dir, x - y) == 0. It
1068 /// also supports rolling back this addition, by maintaining a snapshot stack
1069 /// that contains a snapshot of the Simplex's state for each equality, just
1070 /// before that equality was added.
1071 class GBRSimplex {
1072   using Orientation = Simplex::Orientation;
1073 
1074 public:
1075   GBRSimplex(const Simplex &originalSimplex)
1076       : simplex(Simplex::makeProduct(originalSimplex, originalSimplex)),
1077         simplexConstraintOffset(simplex.getNumConstraints()) {}
1078 
1079   /// Add an equality dotProduct(dir, x - y) == 0.
1080   /// First pushes a snapshot for the current simplex state to the stack so
1081   /// that this can be rolled back later.
1082   void addEqualityForDirection(ArrayRef<int64_t> dir) {
1083     assert(
1084         std::any_of(dir.begin(), dir.end(), [](int64_t x) { return x != 0; }) &&
1085         "Direction passed is the zero vector!");
1086     snapshotStack.push_back(simplex.getSnapshot());
1087     simplex.addEquality(getCoeffsForDirection(dir));
1088   }
1089   /// Compute max(dotProduct(dir, x - y)).
1090   Fraction computeWidth(ArrayRef<int64_t> dir) {
1091     Optional<Fraction> maybeWidth =
1092         simplex.computeOptimum(Direction::Up, getCoeffsForDirection(dir));
1093     assert(maybeWidth.hasValue() && "Width should not be unbounded!");
1094     return *maybeWidth;
1095   }
1096 
1097   /// Compute max(dotProduct(dir, x - y)) and save the dual variables for only
1098   /// the direction equalities to `dual`.
1099   Fraction computeWidthAndDuals(ArrayRef<int64_t> dir,
1100                                 SmallVectorImpl<int64_t> &dual,
1101                                 int64_t &dualDenom) {
1102     // We can't just call into computeWidth or computeOptimum since we need to
1103     // access the state of the tableau after computing the optimum, and these
1104     // functions rollback the insertion of the objective function into the
1105     // tableau before returning. We instead add a row for the objective function
1106     // ourselves, call into computeOptimum, compute the duals from the tableau
1107     // state, and finally rollback the addition of the row before returning.
1108     unsigned snap = simplex.getSnapshot();
1109     unsigned conIndex = simplex.addRow(getCoeffsForDirection(dir));
1110     unsigned row = simplex.con[conIndex].pos;
1111     Optional<Fraction> maybeWidth =
1112         simplex.computeRowOptimum(Simplex::Direction::Up, row);
1113     assert(maybeWidth.hasValue() && "Width should not be unbounded!");
1114     dualDenom = simplex.tableau(row, 0);
1115     dual.clear();
1116 
1117     // The increment is i += 2 because equalities are added as two inequalities,
1118     // one positive and one negative. Each iteration processes one equality.
1119     for (unsigned i = simplexConstraintOffset; i < conIndex; i += 2) {
1120       // The dual variable for an inequality in column orientation is the
1121       // negative of its coefficient at the objective row. If the inequality is
1122       // in row orientation, the corresponding dual variable is zero.
1123       //
1124       // We want the dual for the original equality, which corresponds to two
1125       // inequalities: a positive inequality, which has the same coefficients as
1126       // the equality, and a negative equality, which has negated coefficients.
1127       //
1128       // Note that at most one of these inequalities can be in column
1129       // orientation because the column unknowns should form a basis and hence
1130       // must be linearly independent. If the positive inequality is in column
1131       // position, its dual is the dual corresponding to the equality. If the
1132       // negative inequality is in column position, the negation of its dual is
1133       // the dual corresponding to the equality. If neither is in column
1134       // position, then that means that this equality is redundant, and its dual
1135       // is zero.
1136       //
1137       // Note that it is NOT valid to perform pivots during the computation of
1138       // the duals. This entire dual computation must be performed on the same
1139       // tableau configuration.
1140       assert(!(simplex.con[i].orientation == Orientation::Column &&
1141                simplex.con[i + 1].orientation == Orientation::Column) &&
1142              "Both inequalities for the equality cannot be in column "
1143              "orientation!");
1144       if (simplex.con[i].orientation == Orientation::Column)
1145         dual.push_back(-simplex.tableau(row, simplex.con[i].pos));
1146       else if (simplex.con[i + 1].orientation == Orientation::Column)
1147         dual.push_back(simplex.tableau(row, simplex.con[i + 1].pos));
1148       else
1149         dual.push_back(0);
1150     }
1151     simplex.rollback(snap);
1152     return *maybeWidth;
1153   }
1154 
1155   /// Remove the last equality that was added through addEqualityForDirection.
1156   ///
1157   /// We do this by rolling back to the snapshot at the top of the stack, which
1158   /// should be a snapshot taken just before the last equality was added.
1159   void removeLastEquality() {
1160     assert(!snapshotStack.empty() && "Snapshot stack is empty!");
1161     simplex.rollback(snapshotStack.back());
1162     snapshotStack.pop_back();
1163   }
1164 
1165 private:
1166   /// Returns coefficients of the expression 'dot_product(dir, x - y)',
1167   /// i.e.,   dir_1 * x_1 + dir_2 * x_2 + ... + dir_n * x_n
1168   ///       - dir_1 * y_1 - dir_2 * y_2 - ... - dir_n * y_n,
1169   /// where n is the dimension of the original polytope.
1170   SmallVector<int64_t, 8> getCoeffsForDirection(ArrayRef<int64_t> dir) {
1171     assert(2 * dir.size() == simplex.getNumVariables() &&
1172            "Direction vector has wrong dimensionality");
1173     SmallVector<int64_t, 8> coeffs(dir.begin(), dir.end());
1174     coeffs.reserve(2 * dir.size());
1175     for (int64_t coeff : dir)
1176       coeffs.push_back(-coeff);
1177     coeffs.push_back(0); // constant term
1178     return coeffs;
1179   }
1180 
1181   Simplex simplex;
1182   /// The first index of the equality constraints, the index immediately after
1183   /// the last constraint in the initial product simplex.
1184   unsigned simplexConstraintOffset;
1185   /// A stack of snapshots, used for rolling back.
1186   SmallVector<unsigned, 8> snapshotStack;
1187 };
1188 
1189 // Return a + scale*b;
1190 static SmallVector<int64_t, 8> scaleAndAdd(ArrayRef<int64_t> a, int64_t scale,
1191                                            ArrayRef<int64_t> b) {
1192   assert(a.size() == b.size());
1193   SmallVector<int64_t, 8> res;
1194   res.reserve(a.size());
1195   for (unsigned i = 0, e = a.size(); i < e; ++i)
1196     res.push_back(a[i] + scale * b[i]);
1197   return res;
1198 }
1199 
1200 /// Reduce the basis to try and find a direction in which the polytope is
1201 /// "thin". This only works for bounded polytopes.
1202 ///
1203 /// This is an implementation of the algorithm described in the paper
1204 /// "An Implementation of Generalized Basis Reduction for Integer Programming"
1205 /// by W. Cook, T. Rutherford, H. E. Scarf, D. Shallcross.
1206 ///
1207 /// Let b_{level}, b_{level + 1}, ... b_n be the current basis.
1208 /// Let width_i(v) = max <v, x - y> where x and y are points in the original
1209 /// polytope such that <b_j, x - y> = 0 is satisfied for all level <= j < i.
1210 ///
1211 /// In every iteration, we first replace b_{i+1} with b_{i+1} + u*b_i, where u
1212 /// is the integer such that width_i(b_{i+1} + u*b_i) is minimized. Let dual_i
1213 /// be the dual variable associated with the constraint <b_i, x - y> = 0 when
1214 /// computing width_{i+1}(b_{i+1}). It can be shown that dual_i is the
1215 /// minimizing value of u, if it were allowed to be fractional. Due to
1216 /// convexity, the minimizing integer value is either floor(dual_i) or
1217 /// ceil(dual_i), so we just need to check which of these gives a lower
1218 /// width_{i+1} value. If dual_i turned out to be an integer, then u = dual_i.
1219 ///
1220 /// Now if width_i(b_{i+1}) < 0.75 * width_i(b_i), we swap b_i and (the new)
1221 /// b_{i + 1} and decrement i (unless i = level, in which case we stay at the
1222 /// same i). Otherwise, we increment i.
1223 ///
1224 /// We keep f values and duals cached and invalidate them when necessary.
1225 /// Whenever possible, we use them instead of recomputing them. We implement the
1226 /// algorithm as follows.
1227 ///
1228 /// In an iteration at i we need to compute:
1229 ///   a) width_i(b_{i + 1})
1230 ///   b) width_i(b_i)
1231 ///   c) the integer u that minimizes width_i(b_{i + 1} + u*b_i)
1232 ///
1233 /// If width_i(b_i) is not already cached, we compute it.
1234 ///
1235 /// If the duals are not already cached, we compute width_{i+1}(b_{i+1}) and
1236 /// store the duals from this computation.
1237 ///
1238 /// We call updateBasisWithUAndGetFCandidate, which finds the minimizing value
1239 /// of u as explained before, caches the duals from this computation, sets
1240 /// b_{i+1} to b_{i+1} + u*b_i, and returns the new value of width_i(b_{i+1}).
1241 ///
1242 /// Now if width_i(b_{i+1}) < 0.75 * width_i(b_i), we swap b_i and b_{i+1} and
1243 /// decrement i, resulting in the basis
1244 /// ... b_{i - 1}, b_{i + 1} + u*b_i, b_i, b_{i+2}, ...
1245 /// with corresponding f values
1246 /// ... width_{i-1}(b_{i-1}), width_i(b_{i+1} + u*b_i), width_{i+1}(b_i), ...
1247 /// The values up to i - 1 remain unchanged. We have just gotten the middle
1248 /// value from updateBasisWithUAndGetFCandidate, so we can update that in the
1249 /// cache. The value at width_{i+1}(b_i) is unknown, so we evict this value from
1250 /// the cache. The iteration after decrementing needs exactly the duals from the
1251 /// computation of width_i(b_{i + 1} + u*b_i), so we keep these in the cache.
1252 ///
1253 /// When incrementing i, no cached f values get invalidated. However, the cached
1254 /// duals do get invalidated as the duals for the higher levels are different.
1255 void Simplex::reduceBasis(Matrix &basis, unsigned level) {
1256   const Fraction epsilon(3, 4);
1257 
1258   if (level == basis.getNumRows() - 1)
1259     return;
1260 
1261   GBRSimplex gbrSimplex(*this);
1262   SmallVector<Fraction, 8> width;
1263   SmallVector<int64_t, 8> dual;
1264   int64_t dualDenom;
1265 
1266   // Finds the value of u that minimizes width_i(b_{i+1} + u*b_i), caches the
1267   // duals from this computation, sets b_{i+1} to b_{i+1} + u*b_i, and returns
1268   // the new value of width_i(b_{i+1}).
1269   //
1270   // If dual_i is not an integer, the minimizing value must be either
1271   // floor(dual_i) or ceil(dual_i). We compute the expression for both and
1272   // choose the minimizing value.
1273   //
1274   // If dual_i is an integer, we don't need to perform these computations. We
1275   // know that in this case,
1276   //   a) u = dual_i.
1277   //   b) one can show that dual_j for j < i are the same duals we would have
1278   //      gotten from computing width_i(b_{i + 1} + u*b_i), so the correct duals
1279   //      are the ones already in the cache.
1280   //   c) width_i(b_{i+1} + u*b_i) = min_{alpha} width_i(b_{i+1} + alpha * b_i),
1281   //   which
1282   //      one can show is equal to width_{i+1}(b_{i+1}). The latter value must
1283   //      be in the cache, so we get it from there and return it.
1284   auto updateBasisWithUAndGetFCandidate = [&](unsigned i) -> Fraction {
1285     assert(i < level + dual.size() && "dual_i is not known!");
1286 
1287     int64_t u = floorDiv(dual[i - level], dualDenom);
1288     basis.addToRow(i, i + 1, u);
1289     if (dual[i - level] % dualDenom != 0) {
1290       SmallVector<int64_t, 8> candidateDual[2];
1291       int64_t candidateDualDenom[2];
1292       Fraction widthI[2];
1293 
1294       // Initially u is floor(dual) and basis reflects this.
1295       widthI[0] = gbrSimplex.computeWidthAndDuals(
1296           basis.getRow(i + 1), candidateDual[0], candidateDualDenom[0]);
1297 
1298       // Now try ceil(dual), i.e. floor(dual) + 1.
1299       ++u;
1300       basis.addToRow(i, i + 1, 1);
1301       widthI[1] = gbrSimplex.computeWidthAndDuals(
1302           basis.getRow(i + 1), candidateDual[1], candidateDualDenom[1]);
1303 
1304       unsigned j = widthI[0] < widthI[1] ? 0 : 1;
1305       if (j == 0)
1306         // Subtract 1 to go from u = ceil(dual) back to floor(dual).
1307         basis.addToRow(i, i + 1, -1);
1308 
1309       // width_i(b{i+1} + u*b_i) should be minimized at our value of u.
1310       // We assert that this holds by checking that the values of width_i at
1311       // u - 1 and u + 1 are greater than or equal to the value at u. If the
1312       // width is lesser at either of the adjacent values, then our computed
1313       // value of u is clearly not the minimizer. Otherwise by convexity the
1314       // computed value of u is really the minimizer.
1315 
1316       // Check the value at u - 1.
1317       assert(gbrSimplex.computeWidth(scaleAndAdd(
1318                  basis.getRow(i + 1), -1, basis.getRow(i))) >= widthI[j] &&
1319              "Computed u value does not minimize the width!");
1320       // Check the value at u + 1.
1321       assert(gbrSimplex.computeWidth(scaleAndAdd(
1322                  basis.getRow(i + 1), +1, basis.getRow(i))) >= widthI[j] &&
1323              "Computed u value does not minimize the width!");
1324 
1325       dual = std::move(candidateDual[j]);
1326       dualDenom = candidateDualDenom[j];
1327       return widthI[j];
1328     }
1329 
1330     assert(i + 1 - level < width.size() && "width_{i+1} wasn't saved");
1331     // f_i(b_{i+1} + dual*b_i) == width_{i+1}(b_{i+1}) when `dual` minimizes the
1332     // LHS. (note: the basis has already been updated, so b_{i+1} + dual*b_i in
1333     // the above expression is equal to basis.getRow(i+1) below.)
1334     assert(gbrSimplex.computeWidth(basis.getRow(i + 1)) ==
1335            width[i + 1 - level]);
1336     return width[i + 1 - level];
1337   };
1338 
1339   // In the ith iteration of the loop, gbrSimplex has constraints for directions
1340   // from `level` to i - 1.
1341   unsigned i = level;
1342   while (i < basis.getNumRows() - 1) {
1343     if (i >= level + width.size()) {
1344       // We don't even know the value of f_i(b_i), so let's find that first.
1345       // We have to do this first since later we assume that width already
1346       // contains values up to and including i.
1347 
1348       assert((i == 0 || i - 1 < level + width.size()) &&
1349              "We are at level i but we don't know the value of width_{i-1}");
1350 
1351       // We don't actually use these duals at all, but it doesn't matter
1352       // because this case should only occur when i is level, and there are no
1353       // duals in that case anyway.
1354       assert(i == level && "This case should only occur when i == level");
1355       width.push_back(
1356           gbrSimplex.computeWidthAndDuals(basis.getRow(i), dual, dualDenom));
1357     }
1358 
1359     if (i >= level + dual.size()) {
1360       assert(i + 1 >= level + width.size() &&
1361              "We don't know dual_i but we know width_{i+1}");
1362       // We don't know dual for our level, so let's find it.
1363       gbrSimplex.addEqualityForDirection(basis.getRow(i));
1364       width.push_back(gbrSimplex.computeWidthAndDuals(basis.getRow(i + 1), dual,
1365                                                       dualDenom));
1366       gbrSimplex.removeLastEquality();
1367     }
1368 
1369     // This variable stores width_i(b_{i+1} + u*b_i).
1370     Fraction widthICandidate = updateBasisWithUAndGetFCandidate(i);
1371     if (widthICandidate < epsilon * width[i - level]) {
1372       basis.swapRows(i, i + 1);
1373       width[i - level] = widthICandidate;
1374       // The values of width_{i+1}(b_{i+1}) and higher may change after the
1375       // swap, so we remove the cached values here.
1376       width.resize(i - level + 1);
1377       if (i == level) {
1378         dual.clear();
1379         continue;
1380       }
1381 
1382       gbrSimplex.removeLastEquality();
1383       i--;
1384       continue;
1385     }
1386 
1387     // Invalidate duals since the higher level needs to recompute its own duals.
1388     dual.clear();
1389     gbrSimplex.addEqualityForDirection(basis.getRow(i));
1390     i++;
1391   }
1392 }
1393 
1394 /// Search for an integer sample point using a branch and bound algorithm.
1395 ///
1396 /// Each row in the basis matrix is a vector, and the set of basis vectors
1397 /// should span the space. Initially this is the identity matrix,
1398 /// i.e., the basis vectors are just the variables.
1399 ///
1400 /// In every level, a value is assigned to the level-th basis vector, as
1401 /// follows. Compute the minimum and maximum rational values of this direction.
1402 /// If only one integer point lies in this range, constrain the variable to
1403 /// have this value and recurse to the next variable.
1404 ///
1405 /// If the range has multiple values, perform generalized basis reduction via
1406 /// reduceBasis and then compute the bounds again. Now we try constraining
1407 /// this direction in the first value in this range and "recurse" to the next
1408 /// level. If we fail to find a sample, we try assigning the direction the next
1409 /// value in this range, and so on.
1410 ///
1411 /// If no integer sample is found from any of the assignments, or if the range
1412 /// contains no integer value, then of course the polytope is empty for the
1413 /// current assignment of the values in previous levels, so we return to
1414 /// the previous level.
1415 ///
1416 /// If we reach the last level where all the variables have been assigned values
1417 /// already, then we simply return the current sample point if it is integral,
1418 /// and go back to the previous level otherwise.
1419 ///
1420 /// To avoid potentially arbitrarily large recursion depths leading to stack
1421 /// overflows, this algorithm is implemented iteratively.
1422 Optional<SmallVector<int64_t, 8>> Simplex::findIntegerSample() {
1423   if (empty)
1424     return {};
1425 
1426   unsigned nDims = var.size();
1427   Matrix basis = Matrix::identity(nDims);
1428 
1429   unsigned level = 0;
1430   // The snapshot just before constraining a direction to a value at each level.
1431   SmallVector<unsigned, 8> snapshotStack;
1432   // The maximum value in the range of the direction for each level.
1433   SmallVector<int64_t, 8> upperBoundStack;
1434   // The next value to try constraining the basis vector to at each level.
1435   SmallVector<int64_t, 8> nextValueStack;
1436 
1437   snapshotStack.reserve(basis.getNumRows());
1438   upperBoundStack.reserve(basis.getNumRows());
1439   nextValueStack.reserve(basis.getNumRows());
1440   while (level != -1u) {
1441     if (level == basis.getNumRows()) {
1442       // We've assigned values to all variables. Return if we have a sample,
1443       // or go back up to the previous level otherwise.
1444       if (auto maybeSample = getSamplePointIfIntegral())
1445         return maybeSample;
1446       level--;
1447       continue;
1448     }
1449 
1450     if (level >= upperBoundStack.size()) {
1451       // We haven't populated the stack values for this level yet, so we have
1452       // just come down a level ("recursed"). Find the lower and upper bounds.
1453       // If there is more than one integer point in the range, perform
1454       // generalized basis reduction.
1455       SmallVector<int64_t, 8> basisCoeffs =
1456           llvm::to_vector<8>(basis.getRow(level));
1457       basisCoeffs.push_back(0);
1458 
1459       Optional<int64_t> minRoundedUp, maxRoundedDown;
1460       std::tie(minRoundedUp, maxRoundedDown) =
1461           computeIntegerBounds(basisCoeffs);
1462 
1463       // Heuristic: if the sample point is integral at this point, just return
1464       // it.
1465       if (auto maybeSample = getSamplePointIfIntegral())
1466         return *maybeSample;
1467 
1468       if (minRoundedUp < maxRoundedDown) {
1469         reduceBasis(basis, level);
1470         basisCoeffs = llvm::to_vector<8>(basis.getRow(level));
1471         basisCoeffs.push_back(0);
1472         std::tie(minRoundedUp, maxRoundedDown) =
1473             computeIntegerBounds(basisCoeffs);
1474       }
1475 
1476       snapshotStack.push_back(getSnapshot());
1477       // The smallest value in the range is the next value to try.
1478       // The values in the optionals are guaranteed to exist since we know the
1479       // polytope is bounded.
1480       nextValueStack.push_back(*minRoundedUp);
1481       upperBoundStack.push_back(*maxRoundedDown);
1482     }
1483 
1484     assert((snapshotStack.size() - 1 == level &&
1485             nextValueStack.size() - 1 == level &&
1486             upperBoundStack.size() - 1 == level) &&
1487            "Mismatched variable stack sizes!");
1488 
1489     // Whether we "recursed" or "returned" from a lower level, we rollback
1490     // to the snapshot of the starting state at this level. (in the "recursed"
1491     // case this has no effect)
1492     rollback(snapshotStack.back());
1493     int64_t nextValue = nextValueStack.back();
1494     nextValueStack.back()++;
1495     if (nextValue > upperBoundStack.back()) {
1496       // We have exhausted the range and found no solution. Pop the stack and
1497       // return up a level.
1498       snapshotStack.pop_back();
1499       nextValueStack.pop_back();
1500       upperBoundStack.pop_back();
1501       level--;
1502       continue;
1503     }
1504 
1505     // Try the next value in the range and "recurse" into the next level.
1506     SmallVector<int64_t, 8> basisCoeffs(basis.getRow(level).begin(),
1507                                         basis.getRow(level).end());
1508     basisCoeffs.push_back(-nextValue);
1509     addEquality(basisCoeffs);
1510     level++;
1511   }
1512 
1513   return {};
1514 }
1515 
1516 /// Compute the minimum and maximum integer values the expression can take. We
1517 /// compute each separately.
1518 std::pair<Optional<int64_t>, Optional<int64_t>>
1519 Simplex::computeIntegerBounds(ArrayRef<int64_t> coeffs) {
1520   Optional<int64_t> minRoundedUp;
1521   if (Optional<Fraction> maybeMin =
1522           computeOptimum(Simplex::Direction::Down, coeffs))
1523     minRoundedUp = ceil(*maybeMin);
1524 
1525   Optional<int64_t> maxRoundedDown;
1526   if (Optional<Fraction> maybeMax =
1527           computeOptimum(Simplex::Direction::Up, coeffs))
1528     maxRoundedDown = floor(*maybeMax);
1529 
1530   return {minRoundedUp, maxRoundedDown};
1531 }
1532 
1533 void SimplexBase::print(raw_ostream &os) const {
1534   os << "rows = " << nRow << ", columns = " << nCol << "\n";
1535   if (empty)
1536     os << "Simplex marked empty!\n";
1537   os << "var: ";
1538   for (unsigned i = 0; i < var.size(); ++i) {
1539     if (i > 0)
1540       os << ", ";
1541     var[i].print(os);
1542   }
1543   os << "\ncon: ";
1544   for (unsigned i = 0; i < con.size(); ++i) {
1545     if (i > 0)
1546       os << ", ";
1547     con[i].print(os);
1548   }
1549   os << '\n';
1550   for (unsigned row = 0; row < nRow; ++row) {
1551     if (row > 0)
1552       os << ", ";
1553     os << "r" << row << ": " << rowUnknown[row];
1554   }
1555   os << '\n';
1556   os << "c0: denom, c1: const";
1557   for (unsigned col = 2; col < nCol; ++col)
1558     os << ", c" << col << ": " << colUnknown[col];
1559   os << '\n';
1560   for (unsigned row = 0; row < nRow; ++row) {
1561     for (unsigned col = 0; col < nCol; ++col)
1562       os << tableau(row, col) << '\t';
1563     os << '\n';
1564   }
1565   os << '\n';
1566 }
1567 
1568 void SimplexBase::dump() const { print(llvm::errs()); }
1569 
1570 bool Simplex::isRationalSubsetOf(const IntegerPolyhedron &poly) {
1571   if (isEmpty())
1572     return true;
1573 
1574   for (unsigned i = 0, e = poly.getNumInequalities(); i < e; ++i)
1575     if (!isRedundantInequality(poly.getInequality(i)))
1576       return false;
1577 
1578   for (unsigned i = 0, e = poly.getNumEqualities(); i < e; ++i)
1579     if (!isRedundantEquality(poly.getEquality(i)))
1580       return false;
1581 
1582   return true;
1583 }
1584 
1585 /// Computes the minimum value `coeffs` can take. If the value is greater than
1586 /// or equal to zero, the polytope entirely lies in the half-space defined by
1587 /// `coeffs >= 0`.
1588 bool Simplex::isRedundantInequality(ArrayRef<int64_t> coeffs) {
1589   Optional<Fraction> minimum = computeOptimum(Direction::Down, coeffs);
1590   return minimum && *minimum >= Fraction(0, 1);
1591 }
1592 
1593 /// Check whether the equality given by `coeffs == 0` is redundant given
1594 /// the existing constraints. This is redundant when `coeffs` is already
1595 /// always zero under the existing constraints. `coeffs` is always zero
1596 /// when the minimum and maximum value that `coeffs` can take are both zero.
1597 bool Simplex::isRedundantEquality(ArrayRef<int64_t> coeffs) {
1598   Optional<Fraction> minimum = computeOptimum(Direction::Down, coeffs);
1599   Optional<Fraction> maximum = computeOptimum(Direction::Up, coeffs);
1600   return minimum && maximum && *maximum == Fraction(0, 1) &&
1601          *minimum == Fraction(0, 1);
1602 }
1603 
1604 } // namespace mlir
1605