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 using namespace mlir;
15 using namespace presburger;
16 
17 using Direction = Simplex::Direction;
18 
19 const int nullIndex = std::numeric_limits<int>::max();
20 
21 SimplexBase::SimplexBase(unsigned nVar, bool mustUseBigM, unsigned symbolOffset,
22                          unsigned nSymbol)
23     : usingBigM(mustUseBigM), nRow(0), nCol(getNumFixedCols() + nVar),
24       nRedundant(0), nSymbol(nSymbol), tableau(0, nCol), empty(false) {
25   assert(symbolOffset + nSymbol <= nVar);
26 
27   colUnknown.insert(colUnknown.begin(), getNumFixedCols(), nullIndex);
28   for (unsigned i = 0; i < nVar; ++i) {
29     var.emplace_back(Orientation::Column, /*restricted=*/false,
30                      /*pos=*/getNumFixedCols() + i);
31     colUnknown.push_back(i);
32   }
33 
34   // Move the symbols to be in columns [3, 3 + nSymbol).
35   for (unsigned i = 0; i < nSymbol; ++i) {
36     var[symbolOffset + i].isSymbol = true;
37     swapColumns(var[symbolOffset + i].pos, getNumFixedCols() + i);
38   }
39 }
40 
41 const Simplex::Unknown &SimplexBase::unknownFromIndex(int index) const {
42   assert(index != nullIndex && "nullIndex passed to unknownFromIndex");
43   return index >= 0 ? var[index] : con[~index];
44 }
45 
46 const Simplex::Unknown &SimplexBase::unknownFromColumn(unsigned col) const {
47   assert(col < nCol && "Invalid column");
48   return unknownFromIndex(colUnknown[col]);
49 }
50 
51 const Simplex::Unknown &SimplexBase::unknownFromRow(unsigned row) const {
52   assert(row < nRow && "Invalid row");
53   return unknownFromIndex(rowUnknown[row]);
54 }
55 
56 Simplex::Unknown &SimplexBase::unknownFromIndex(int index) {
57   assert(index != nullIndex && "nullIndex passed to unknownFromIndex");
58   return index >= 0 ? var[index] : con[~index];
59 }
60 
61 Simplex::Unknown &SimplexBase::unknownFromColumn(unsigned col) {
62   assert(col < nCol && "Invalid column");
63   return unknownFromIndex(colUnknown[col]);
64 }
65 
66 Simplex::Unknown &SimplexBase::unknownFromRow(unsigned row) {
67   assert(row < nRow && "Invalid row");
68   return unknownFromIndex(rowUnknown[row]);
69 }
70 
71 unsigned SimplexBase::addZeroRow(bool makeRestricted) {
72   ++nRow;
73   // If the tableau is not big enough to accomodate the extra row, we extend it.
74   if (nRow >= tableau.getNumRows())
75     tableau.resizeVertically(nRow);
76   rowUnknown.push_back(~con.size());
77   con.emplace_back(Orientation::Row, makeRestricted, nRow - 1);
78   undoLog.push_back(UndoLogEntry::RemoveLastConstraint);
79 
80   // Zero out the new row.
81   tableau.fillRow(nRow - 1, 0);
82 
83   tableau(nRow - 1, 0) = 1;
84   return con.size() - 1;
85 }
86 
87 /// Add a new row to the tableau corresponding to the given constant term and
88 /// list of coefficients. The coefficients are specified as a vector of
89 /// (variable index, coefficient) pairs.
90 unsigned SimplexBase::addRow(ArrayRef<int64_t> coeffs, bool makeRestricted) {
91   assert(coeffs.size() == var.size() + 1 &&
92          "Incorrect number of coefficients!");
93 
94   addZeroRow(makeRestricted);
95   tableau(nRow - 1, 1) = coeffs.back();
96   if (usingBigM) {
97     // When the lexicographic pivot rule is used, instead of the variables
98     //
99     // x, y, z ...
100     //
101     // we internally use the variables
102     //
103     // M, M + x, M + y, M + z, ...
104     //
105     // where M is the big M parameter. As such, when the user tries to add
106     // a row ax + by + cz + d, we express it in terms of our internal variables
107     // as -(a + b + c)M + a(M + x) + b(M + y) + c(M + z) + d.
108     //
109     // Symbols don't use the big M parameter since they do not get lex
110     // optimized.
111     int64_t bigMCoeff = 0;
112     for (unsigned i = 0; i < coeffs.size() - 1; ++i)
113       if (!var[i].isSymbol)
114         bigMCoeff -= coeffs[i];
115     // The coefficient to the big M parameter is stored in column 2.
116     tableau(nRow - 1, 2) = bigMCoeff;
117   }
118 
119   // Process each given variable coefficient.
120   for (unsigned i = 0; i < var.size(); ++i) {
121     unsigned pos = var[i].pos;
122     if (coeffs[i] == 0)
123       continue;
124 
125     if (var[i].orientation == Orientation::Column) {
126       // If a variable is in column position at column col, then we just add the
127       // coefficient for that variable (scaled by the common row denominator) to
128       // the corresponding entry in the new row.
129       tableau(nRow - 1, pos) += coeffs[i] * tableau(nRow - 1, 0);
130       continue;
131     }
132 
133     // If the variable is in row position, we need to add that row to the new
134     // row, scaled by the coefficient for the variable, accounting for the two
135     // rows potentially having different denominators. The new denominator is
136     // the lcm of the two.
137     int64_t lcm = mlir::lcm(tableau(nRow - 1, 0), tableau(pos, 0));
138     int64_t nRowCoeff = lcm / tableau(nRow - 1, 0);
139     int64_t idxRowCoeff = coeffs[i] * (lcm / tableau(pos, 0));
140     tableau(nRow - 1, 0) = lcm;
141     for (unsigned col = 1; col < nCol; ++col)
142       tableau(nRow - 1, col) =
143           nRowCoeff * tableau(nRow - 1, col) + idxRowCoeff * tableau(pos, col);
144   }
145 
146   normalizeRow(nRow - 1);
147   // Push to undo log along with the index of the new constraint.
148   return con.size() - 1;
149 }
150 
151 /// Normalize the row by removing factors that are common between the
152 /// denominator and all the numerator coefficients.
153 void SimplexBase::normalizeRow(unsigned row) {
154   int64_t gcd = 0;
155   for (unsigned col = 0; col < nCol; ++col) {
156     gcd = llvm::greatestCommonDivisor(gcd, std::abs(tableau(row, col)));
157     // If the gcd becomes 1 then the row is already normalized.
158     if (gcd == 1)
159       return;
160   }
161 
162   // Note that the gcd can never become zero since the first element of the row,
163   // the denominator, is non-zero.
164   assert(gcd != 0);
165   for (unsigned col = 0; col < nCol; ++col)
166     tableau(row, col) /= gcd;
167 }
168 
169 namespace {
170 bool signMatchesDirection(int64_t elem, Direction direction) {
171   assert(elem != 0 && "elem should not be 0");
172   return direction == Direction::Up ? elem > 0 : elem < 0;
173 }
174 
175 Direction flippedDirection(Direction direction) {
176   return direction == Direction::Up ? Direction::Down : Simplex::Direction::Up;
177 }
178 } // namespace
179 
180 /// We simply make the tableau consistent while maintaining a lexicopositive
181 /// basis transform, and then return the sample value. If the tableau becomes
182 /// empty, we return empty.
183 ///
184 /// Let the variables be x = (x_1, ... x_n).
185 /// Let the basis unknowns be y = (y_1, ... y_n).
186 /// We have that x = A*y + b for some n x n matrix A and n x 1 column vector b.
187 ///
188 /// As we will show below, A*y is either zero or lexicopositive.
189 /// Adding a lexicopositive vector to b will make it lexicographically
190 /// greater, so A*y + b is always equal to or lexicographically greater than b.
191 /// Thus, since we can attain x = b, that is the lexicographic minimum.
192 ///
193 /// We have that that every column in A is lexicopositive, i.e., has at least
194 /// one non-zero element, with the first such element being positive. Since for
195 /// the tableau to be consistent we must have non-negative sample values not
196 /// only for the constraints but also for the variables, we also have x >= 0 and
197 /// y >= 0, by which we mean every element in these vectors is non-negative.
198 ///
199 /// Proof that if every column in A is lexicopositive, and y >= 0, then
200 /// A*y is zero or lexicopositive. Begin by considering A_1, the first row of A.
201 /// If this row is all zeros, then (A*y)_1 = (A_1)*y = 0; proceed to the next
202 /// row. If we run out of rows, A*y is zero and we are done; otherwise, we
203 /// encounter some row A_i that has a non-zero element. Every column is
204 /// lexicopositive and so has some positive element before any negative elements
205 /// occur, so the element in this row for any column, if non-zero, must be
206 /// positive. Consider (A*y)_i = (A_i)*y. All the elements in both vectors are
207 /// non-negative, so if this is non-zero then it must be positive. Then the
208 /// first non-zero element of A*y is positive so A*y is lexicopositive.
209 ///
210 /// Otherwise, if (A_i)*y is zero, then for every column j that had a non-zero
211 /// element in A_i, y_j is zero. Thus these columns have no contribution to A*y
212 /// and we can completely ignore these columns of A. We now continue downwards,
213 /// looking for rows of A that have a non-zero element other than in the ignored
214 /// columns. If we find one, say A_k, once again these elements must be positive
215 /// since they are the first non-zero element in each of these columns, so if
216 /// (A_k)*y is not zero then we have that A*y is lexicopositive and if not we
217 /// add these to the set of ignored columns and continue to the next row. If we
218 /// run out of rows, then A*y is zero and we are done.
219 MaybeOptimum<SmallVector<Fraction, 8>> LexSimplex::findRationalLexMin() {
220   if (restoreRationalConsistency().failed())
221     return OptimumKind::Empty;
222   return getRationalSample();
223 }
224 
225 /// Given a row that has a non-integer sample value, add an inequality such
226 /// that this fractional sample value is cut away from the polytope. The added
227 /// inequality will be such that no integer points are removed. i.e., the
228 /// integer lexmin, if it exists, is the same with and without this constraint.
229 ///
230 /// Let the row be
231 /// (c + coeffM*M + a_1*s_1 + ... + a_m*s_m + b_1*y_1 + ... + b_n*y_n)/d,
232 /// where s_1, ... s_m are the symbols and
233 ///       y_1, ... y_n are the other basis unknowns.
234 ///
235 /// For this to be an integer, we want
236 /// coeffM*M + a_1*s_1 + ... + a_m*s_m + b_1*y_1 + ... + b_n*y_n = -c (mod d)
237 /// Note that this constraint must always hold, independent of the basis,
238 /// becuse the row unknown's value always equals this expression, even if *we*
239 /// later compute the sample value from a different expression based on a
240 /// different basis.
241 ///
242 /// Let us assume that M has a factor of d in it. Imposing this constraint on M
243 /// does not in any way hinder us from finding a value of M that is big enough.
244 /// Moreover, this function is only called when the symbolic part of the sample,
245 /// a_1*s_1 + ... + a_m*s_m, is known to be an integer.
246 ///
247 /// Also, we can safely reduce the coefficients modulo d, so we have:
248 ///
249 /// (b_1%d)y_1 + ... + (b_n%d)y_n = (-c%d) + k*d for some integer `k`
250 ///
251 /// Note that all coefficient modulos here are non-negative. Also, all the
252 /// unknowns are non-negative here as both constraints and variables are
253 /// non-negative in LexSimplexBase. (We used the big M trick to make the
254 /// variables non-negative). Therefore, the LHS here is non-negative.
255 /// Since 0 <= (-c%d) < d, k is the quotient of dividing the LHS by d and
256 /// is therefore non-negative as well.
257 ///
258 /// So we have
259 /// ((b_1%d)y_1 + ... + (b_n%d)y_n - (-c%d))/d >= 0.
260 ///
261 /// The constraint is violated when added (it would be useless otherwise)
262 /// so we immediately try to move it to a column.
263 LogicalResult LexSimplexBase::addCut(unsigned row) {
264   int64_t d = tableau(row, 0);
265   addZeroRow(/*makeRestricted=*/true);
266   tableau(nRow - 1, 0) = d;
267   tableau(nRow - 1, 1) = -mod(-tableau(row, 1), d); // -c%d.
268   tableau(nRow - 1, 2) = 0;
269   for (unsigned col = 3 + nSymbol; col < nCol; ++col)
270     tableau(nRow - 1, col) = mod(tableau(row, col), d); // b_i%d.
271   return moveRowUnknownToColumn(nRow - 1);
272 }
273 
274 Optional<unsigned> LexSimplex::maybeGetNonIntegralVarRow() const {
275   for (const Unknown &u : var) {
276     if (u.orientation == Orientation::Column)
277       continue;
278     // If the sample value is of the form (a/d)M + b/d, we need b to be
279     // divisible by d. We assume M contains all possible
280     // factors and is divisible by everything.
281     unsigned row = u.pos;
282     if (tableau(row, 1) % tableau(row, 0) != 0)
283       return row;
284   }
285   return {};
286 }
287 
288 MaybeOptimum<SmallVector<int64_t, 8>> LexSimplex::findIntegerLexMin() {
289   // We first try to make the tableau consistent.
290   if (restoreRationalConsistency().failed())
291     return OptimumKind::Empty;
292 
293   // Then, if the sample value is integral, we are done.
294   while (Optional<unsigned> maybeRow = maybeGetNonIntegralVarRow()) {
295     // Otherwise, for the variable whose row has a non-integral sample value,
296     // we add a cut, a constraint that remove this rational point
297     // while preserving all integer points, thus keeping the lexmin the same.
298     // We then again try to make the tableau with the new constraint
299     // consistent. This continues until the tableau becomes empty, in which
300     // case there is no integer point, or until there are no variables with
301     // non-integral sample values.
302     //
303     // Failure indicates that the tableau became empty, which occurs when the
304     // polytope is integer empty.
305     if (addCut(*maybeRow).failed())
306       return OptimumKind::Empty;
307     if (restoreRationalConsistency().failed())
308       return OptimumKind::Empty;
309   }
310 
311   MaybeOptimum<SmallVector<Fraction, 8>> sample = getRationalSample();
312   assert(!sample.isEmpty() && "If we reached here the sample should exist!");
313   if (sample.isUnbounded())
314     return OptimumKind::Unbounded;
315   return llvm::to_vector<8>(
316       llvm::map_range(*sample, std::mem_fn(&Fraction::getAsInteger)));
317 }
318 
319 bool LexSimplex::isSeparateInequality(ArrayRef<int64_t> coeffs) {
320   SimplexRollbackScopeExit scopeExit(*this);
321   addInequality(coeffs);
322   return findIntegerLexMin().isEmpty();
323 }
324 
325 bool LexSimplex::isRedundantInequality(ArrayRef<int64_t> coeffs) {
326   return isSeparateInequality(getComplementIneq(coeffs));
327 }
328 
329 SmallVector<int64_t, 8>
330 SymbolicLexSimplex::getSymbolicSampleNumerator(unsigned row) const {
331   SmallVector<int64_t, 8> sample;
332   sample.reserve(nSymbol + 1);
333   for (unsigned col = 3; col < 3 + nSymbol; ++col)
334     sample.push_back(tableau(row, col));
335   sample.push_back(tableau(row, 1));
336   return sample;
337 }
338 
339 void LexSimplexBase::appendSymbol() {
340   appendVariable();
341   swapColumns(3 + nSymbol, nCol - 1);
342   var.back().isSymbol = true;
343   nSymbol++;
344 }
345 
346 static bool isRangeDivisibleBy(ArrayRef<int64_t> range, int64_t divisor) {
347   assert(divisor > 0 && "divisor must be positive!");
348   return llvm::all_of(range, [divisor](int64_t x) { return x % divisor == 0; });
349 }
350 
351 bool SymbolicLexSimplex::isSymbolicSampleIntegral(unsigned row) const {
352   int64_t denom = tableau(row, 0);
353   return tableau(row, 1) % denom == 0 &&
354          isRangeDivisibleBy(tableau.getRow(row).slice(3, nSymbol), denom);
355 }
356 
357 /// This proceeds similarly to LexSimplex::addCut(). We are given a row that has
358 /// a symbolic sample value with fractional coefficients.
359 ///
360 /// Let the row be
361 /// (c + coeffM*M + sum_i a_i*s_i + sum_j b_j*y_j)/d,
362 /// where s_1, ... s_m are the symbols and
363 ///       y_1, ... y_n are the other basis unknowns.
364 ///
365 /// As in LexSimplex::addCut, for this to be an integer, we want
366 ///
367 /// coeffM*M + sum_j b_j*y_j = -c + sum_i (-a_i*s_i) (mod d)
368 ///
369 /// This time, a_1*s_1 + ... + a_m*s_m may not be an integer. We find that
370 ///
371 /// sum_i (b_i%d)y_i = ((-c%d) + sum_i (-a_i%d)s_i)%d + k*d for some integer k
372 ///
373 /// where we take a modulo of the whole symbolic expression on the right to
374 /// bring it into the range [0, d - 1]. Therefore, as in LexSimplex::addCut,
375 /// k is the quotient on dividing the LHS by d, and since LHS >= 0, we have
376 /// k >= 0 as well. We realize the modulo of the symbolic expression by adding a
377 /// division variable
378 ///
379 /// q = ((-c%d) + sum_i (-a_i%d)s_i)/d
380 ///
381 /// to the symbol domain, so the equality becomes
382 ///
383 /// sum_i (b_i%d)y_i = (-c%d) + sum_i (-a_i%d)s_i - q*d + k*d for some integer k
384 ///
385 /// So the cut is
386 /// (sum_i (b_i%d)y_i - (-c%d) - sum_i (-a_i%d)s_i + q*d)/d >= 0
387 /// This constraint is violated when added so we immediately try to move it to a
388 /// column.
389 LogicalResult SymbolicLexSimplex::addSymbolicCut(unsigned row) {
390   int64_t d = tableau(row, 0);
391 
392   // Add the division variable `q` described above to the symbol domain.
393   // q = ((-c%d) + sum_i (-a_i%d)s_i)/d.
394   SmallVector<int64_t, 8> domainDivCoeffs;
395   domainDivCoeffs.reserve(nSymbol + 1);
396   for (unsigned col = 3; col < 3 + nSymbol; ++col)
397     domainDivCoeffs.push_back(mod(-tableau(row, col), d)); // (-a_i%d)s_i
398   domainDivCoeffs.push_back(mod(-tableau(row, 1), d));     // -c%d.
399 
400   domainSimplex.addDivisionVariable(domainDivCoeffs, d);
401   domainPoly.addLocalFloorDiv(domainDivCoeffs, d);
402 
403   // Update `this` to account for the additional symbol we just added.
404   appendSymbol();
405 
406   // Add the cut (sum_i (b_i%d)y_i - (-c%d) + sum_i -(-a_i%d)s_i + q*d)/d >= 0.
407   addZeroRow(/*makeRestricted=*/true);
408   tableau(nRow - 1, 0) = d;
409   tableau(nRow - 1, 2) = 0;
410 
411   tableau(nRow - 1, 1) = -mod(-tableau(row, 1), d); // -(-c%d).
412   for (unsigned col = 3; col < 3 + nSymbol - 1; ++col)
413     tableau(nRow - 1, col) = -mod(-tableau(row, col), d); // -(-a_i%d)s_i.
414   tableau(nRow - 1, 3 + nSymbol - 1) = d;                 // q*d.
415 
416   for (unsigned col = 3 + nSymbol; col < nCol; ++col)
417     tableau(nRow - 1, col) = mod(tableau(row, col), d); // (b_i%d)y_i.
418   return moveRowUnknownToColumn(nRow - 1);
419 }
420 
421 void SymbolicLexSimplex::recordOutput(SymbolicLexMin &result) const {
422   Matrix output(0, domainPoly.getNumIds() + 1);
423   output.reserveRows(result.lexmin.getNumOutputs());
424   for (const Unknown &u : var) {
425     if (u.isSymbol)
426       continue;
427 
428     if (u.orientation == Orientation::Column) {
429       // M + u has a sample value of zero so u has a sample value of -M, i.e,
430       // unbounded.
431       result.unboundedDomain.unionInPlace(domainPoly);
432       return;
433     }
434 
435     int64_t denom = tableau(u.pos, 0);
436     if (tableau(u.pos, 2) < denom) {
437       // M + u has a sample value of fM + something, where f < 1, so
438       // u = (f - 1)M + something, which has a negative coefficient for M,
439       // and so is unbounded.
440       result.unboundedDomain.unionInPlace(domainPoly);
441       return;
442     }
443     assert(tableau(u.pos, 2) == denom &&
444            "Coefficient of M should not be greater than 1!");
445 
446     SmallVector<int64_t, 8> sample = getSymbolicSampleNumerator(u.pos);
447     for (int64_t &elem : sample) {
448       assert(elem % denom == 0 && "coefficients must be integral!");
449       elem /= denom;
450     }
451     output.appendExtraRow(sample);
452   }
453   result.lexmin.addPiece(domainPoly, output);
454 }
455 
456 Optional<unsigned> SymbolicLexSimplex::maybeGetAlwaysViolatedRow() {
457   // First look for rows that are clearly violated just from the big M
458   // coefficient, without needing to perform any simplex queries on the domain.
459   for (unsigned row = 0; row < nRow; ++row)
460     if (tableau(row, 2) < 0)
461       return row;
462 
463   for (unsigned row = 0; row < nRow; ++row) {
464     if (tableau(row, 2) > 0)
465       continue;
466     if (domainSimplex.isSeparateInequality(getSymbolicSampleNumerator(row))) {
467       // Sample numerator always takes negative values in the symbol domain.
468       return row;
469     }
470   }
471   return {};
472 }
473 
474 Optional<unsigned> SymbolicLexSimplex::maybeGetNonIntegralVarRow() {
475   for (const Unknown &u : var) {
476     if (u.orientation == Orientation::Column)
477       continue;
478     assert(!u.isSymbol && "Symbol should not be in row orientation!");
479     if (!isSymbolicSampleIntegral(u.pos))
480       return u.pos;
481   }
482   return {};
483 }
484 
485 /// The non-branching pivots are just the ones moving the rows
486 /// that are always violated in the symbol domain.
487 LogicalResult SymbolicLexSimplex::doNonBranchingPivots() {
488   while (Optional<unsigned> row = maybeGetAlwaysViolatedRow())
489     if (moveRowUnknownToColumn(*row).failed())
490       return failure();
491   return success();
492 }
493 
494 SymbolicLexMin SymbolicLexSimplex::computeSymbolicIntegerLexMin() {
495   SymbolicLexMin result(nSymbol, var.size() - nSymbol);
496 
497   /// The algorithm is more naturally expressed recursively, but we implement
498   /// it iteratively here to avoid potential issues with stack overflows in the
499   /// compiler. We explicitly maintain the stack frames in a vector.
500   ///
501   /// To "recurse", we store the current "stack frame", i.e., state variables
502   /// that we will need when we "return", into `stack`, increment `level`, and
503   /// `continue`. To "tail recurse", we just `continue`.
504   /// To "return", we decrement `level` and `continue`.
505   ///
506   /// When there is no stack frame for the current `level`, this indicates that
507   /// we have just "recursed" or "tail recursed". When there does exist one,
508   /// this indicates that we have just "returned" from recursing. There is only
509   /// one point at which non-tail calls occur so we always "return" there.
510   unsigned level = 1;
511   struct StackFrame {
512     int splitIndex;
513     unsigned snapshot;
514     unsigned domainSnapshot;
515     IntegerRelation::CountsSnapshot domainPolyCounts;
516   };
517   SmallVector<StackFrame, 8> stack;
518 
519   while (level > 0) {
520     assert(level >= stack.size());
521     if (level > stack.size()) {
522       if (empty || domainSimplex.findIntegerLexMin().isEmpty()) {
523         // No integer points; return.
524         --level;
525         continue;
526       }
527 
528       if (doNonBranchingPivots().failed()) {
529         // Could not find pivots for violated constraints; return.
530         --level;
531         continue;
532       }
533 
534       unsigned splitRow;
535       SmallVector<int64_t, 8> symbolicSample;
536       for (splitRow = 0; splitRow < nRow; ++splitRow) {
537         if (tableau(splitRow, 2) > 0)
538           continue;
539         assert(tableau(splitRow, 2) == 0 &&
540                "Non-branching pivots should have been handled already!");
541 
542         symbolicSample = getSymbolicSampleNumerator(splitRow);
543         if (domainSimplex.isRedundantInequality(symbolicSample))
544           continue;
545 
546         // It's neither redundant nor separate, so it takes both positive and
547         // negative values, and hence constitutes a row for which we need to
548         // split the domain and separately run each case.
549         assert(!domainSimplex.isSeparateInequality(symbolicSample) &&
550                "Non-branching pivots should have been handled already!");
551         break;
552       }
553 
554       if (splitRow < nRow) {
555         unsigned domainSnapshot = domainSimplex.getSnapshot();
556         IntegerRelation::CountsSnapshot domainPolyCounts =
557             domainPoly.getCounts();
558 
559         // First, we consider the part of the domain where the row is not
560         // violated. We don't have to do any pivots for the row in this case,
561         // but we record the additional constraint that defines this part of
562         // the domain.
563         domainSimplex.addInequality(symbolicSample);
564         domainPoly.addInequality(symbolicSample);
565 
566         // Recurse.
567         //
568         // On return, the basis as a set is preserved but not the internal
569         // ordering within rows or columns. Thus, we take note of the index of
570         // the Unknown that caused the split, which may be in a different
571         // row when we come back from recursing. We will need this to recurse
572         // on the other part of the split domain, where the row is violated.
573         //
574         // Note that we have to capture the index above and not a reference to
575         // the Unknown itself, since the array it lives in might get
576         // reallocated.
577         int splitIndex = rowUnknown[splitRow];
578         unsigned snapshot = getSnapshot();
579         stack.push_back(
580             {splitIndex, snapshot, domainSnapshot, domainPolyCounts});
581         ++level;
582         continue;
583       }
584 
585       // The tableau is rationally consistent for the current domain.
586       // Now we look for non-integral sample values and add cuts for them.
587       if (Optional<unsigned> row = maybeGetNonIntegralVarRow()) {
588         if (addSymbolicCut(*row).failed()) {
589           // No integral points; return.
590           --level;
591           continue;
592         }
593 
594         // Rerun this level with the added cut constraint (tail recurse).
595         continue;
596       }
597 
598       // Record output and return.
599       recordOutput(result);
600       --level;
601       continue;
602     }
603 
604     if (level == stack.size()) {
605       // We have "returned" from "recursing".
606       const StackFrame &frame = stack.back();
607       domainPoly.truncate(frame.domainPolyCounts);
608       domainSimplex.rollback(frame.domainSnapshot);
609       rollback(frame.snapshot);
610       const Unknown &u = unknownFromIndex(frame.splitIndex);
611 
612       // Drop the frame. We don't need it anymore.
613       stack.pop_back();
614 
615       // Now we consider the part of the domain where the unknown `splitIndex`
616       // was negative.
617       assert(u.orientation == Orientation::Row &&
618              "The split row should have been returned to row orientation!");
619       SmallVector<int64_t, 8> splitIneq =
620           getComplementIneq(getSymbolicSampleNumerator(u.pos));
621       if (moveRowUnknownToColumn(u.pos).failed()) {
622         // The unknown can't be made non-negative; return.
623         --level;
624         continue;
625       }
626 
627       // The unknown can be made negative; recurse with the corresponding domain
628       // constraints.
629       domainSimplex.addInequality(splitIneq);
630       domainPoly.addInequality(splitIneq);
631 
632       // We are now taking care of the second half of the domain and we don't
633       // need to do anything else here after returning, so it's a tail recurse.
634       continue;
635     }
636   }
637 
638   return result;
639 }
640 
641 bool LexSimplex::rowIsViolated(unsigned row) const {
642   if (tableau(row, 2) < 0)
643     return true;
644   if (tableau(row, 2) == 0 && tableau(row, 1) < 0)
645     return true;
646   return false;
647 }
648 
649 Optional<unsigned> LexSimplex::maybeGetViolatedRow() const {
650   for (unsigned row = 0; row < nRow; ++row)
651     if (rowIsViolated(row))
652       return row;
653   return {};
654 }
655 
656 /// We simply look for violated rows and keep trying to move them to column
657 /// orientation, which always succeeds unless the constraints have no solution
658 /// in which case we just give up and return.
659 LogicalResult LexSimplex::restoreRationalConsistency() {
660   if (empty)
661     return failure();
662   while (Optional<unsigned> maybeViolatedRow = maybeGetViolatedRow())
663     if (moveRowUnknownToColumn(*maybeViolatedRow).failed())
664       return failure();
665   return success();
666 }
667 
668 // Move the row unknown to column orientation while preserving lexicopositivity
669 // of the basis transform. The sample value of the row must be negative.
670 //
671 // We only consider pivots where the pivot element is positive. Suppose no such
672 // pivot exists, i.e., some violated row has no positive coefficient for any
673 // basis unknown. The row can be represented as (s + c_1*u_1 + ... + c_n*u_n)/d,
674 // where d is the denominator, s is the sample value and the c_i are the basis
675 // coefficients. Since any feasible assignment of the basis satisfies u_i >= 0
676 // for all i, and we have s < 0 as well as c_i < 0 for all i, any feasible
677 // assignment would violate this row and therefore the constraints have no
678 // solution.
679 //
680 // We can preserve lexicopositivity by picking the pivot column with positive
681 // pivot element that makes the lexicographically smallest change to the sample
682 // point.
683 //
684 // Proof. Let
685 // x = (x_1, ... x_n) be the variables,
686 // z = (z_1, ... z_m) be the constraints,
687 // y = (y_1, ... y_n) be the current basis, and
688 // define w = (x_1, ... x_n, z_1, ... z_m) = B*y + s.
689 // B is basically the simplex tableau of our implementation except that instead
690 // of only describing the transform to get back the non-basis unknowns, it
691 // defines the values of all the unknowns in terms of the basis unknowns.
692 // Similarly, s is the column for the sample value.
693 //
694 // Our goal is to show that each column in B, restricted to the first n
695 // rows, is lexicopositive after the pivot if it is so before. This is
696 // equivalent to saying the columns in the whole matrix are lexicopositive;
697 // there must be some non-zero element in every column in the first n rows since
698 // the n variables cannot be spanned without using all the n basis unknowns.
699 //
700 // Consider a pivot where z_i replaces y_j in the basis. Recall the pivot
701 // transform for the tableau derived for SimplexBase::pivot:
702 //
703 //            pivot col    other col                   pivot col    other col
704 // pivot row     a             b       ->   pivot row     1/a         -b/a
705 // other row     c             d            other row     c/a        d - bc/a
706 //
707 // Similarly, a pivot results in B changing to B' and c to c'; the difference
708 // between the tableau and these matrices B and B' is that there is no special
709 // case for the pivot row, since it continues to represent the same unknown. The
710 // same formula applies for all rows:
711 //
712 // B'.col(j) = B.col(j) / B(i,j)
713 // B'.col(k) = B.col(k) - B(i,k) * B.col(j) / B(i,j) for k != j
714 // and similarly, s' = s - s_i * B.col(j) / B(i,j).
715 //
716 // Since the row is violated, we have s_i < 0, so the change in sample value
717 // when pivoting with column a is lexicographically smaller than that when
718 // pivoting with column b iff B.col(a) / B(i, a) is lexicographically smaller
719 // than B.col(b) / B(i, b).
720 //
721 // Since B(i, j) > 0, column j remains lexicopositive.
722 //
723 // For the other columns, suppose C.col(k) is not lexicopositive.
724 // This means that for some p, for all t < p,
725 // C(t,k) = 0 => B(t,k) = B(t,j) * B(i,k) / B(i,j) and
726 // C(t,k) < 0 => B(p,k) < B(t,j) * B(i,k) / B(i,j),
727 // which is in contradiction to the fact that B.col(j) / B(i,j) must be
728 // lexicographically smaller than B.col(k) / B(i,k), since it lexicographically
729 // minimizes the change in sample value.
730 LogicalResult LexSimplexBase::moveRowUnknownToColumn(unsigned row) {
731   Optional<unsigned> maybeColumn;
732   for (unsigned col = 3 + nSymbol; col < nCol; ++col) {
733     if (tableau(row, col) <= 0)
734       continue;
735     maybeColumn =
736         !maybeColumn ? col : getLexMinPivotColumn(row, *maybeColumn, col);
737   }
738 
739   if (!maybeColumn) {
740     markEmpty();
741     return failure();
742   }
743 
744   pivot(row, *maybeColumn);
745   return success();
746 }
747 
748 unsigned LexSimplexBase::getLexMinPivotColumn(unsigned row, unsigned colA,
749                                               unsigned colB) const {
750   // First, let's consider the non-symbolic case.
751   // A pivot causes the following change. (in the diagram the matrix elements
752   // are shown as rationals and there is no common denominator used)
753   //
754   //            pivot col    big M col      const col
755   // pivot row     a            p               b
756   // other row     c            q               d
757   //                        |
758   //                        v
759   //
760   //            pivot col    big M col      const col
761   // pivot row     1/a         -p/a           -b/a
762   // other row     c/a        q - pc/a       d - bc/a
763   //
764   // Let the sample value of the pivot row be s = pM + b before the pivot. Since
765   // the pivot row represents a violated constraint we know that s < 0.
766   //
767   // If the variable is a non-pivot column, its sample value is zero before and
768   // after the pivot.
769   //
770   // If the variable is the pivot column, then its sample value goes from 0 to
771   // (-p/a)M + (-b/a), i.e. 0 to -(pM + b)/a. Thus the change in the sample
772   // value is -s/a.
773   //
774   // If the variable is the pivot row, its sample value goes from s to 0, for a
775   // change of -s.
776   //
777   // If the variable is a non-pivot row, its sample value changes from
778   // qM + d to qM + d + (-pc/a)M + (-bc/a). Thus the change in sample value
779   // is -(pM + b)(c/a) = -sc/a.
780   //
781   // Thus the change in sample value is either 0, -s/a, -s, or -sc/a. Here -s is
782   // fixed for all calls to this function since the row and tableau are fixed.
783   // The callee just wants to compare the return values with the return value of
784   // other invocations of the same function. So the -s is common for all
785   // comparisons involved and can be ignored, since -s is strictly positive.
786   //
787   // Thus we take away this common factor and just return 0, 1/a, 1, or c/a as
788   // appropriate. This allows us to run the entire algorithm treating M
789   // symbolically, as the pivot to be performed does not depend on the value
790   // of M, so long as the sample value s is negative. Note that this is not
791   // because of any special feature of M; by the same argument, we ignore the
792   // symbols too. The caller ensure that the sample value s is negative for
793   // all possible values of the symbols.
794   auto getSampleChangeCoeffForVar = [this, row](unsigned col,
795                                                 const Unknown &u) -> Fraction {
796     int64_t a = tableau(row, col);
797     if (u.orientation == Orientation::Column) {
798       // Pivot column case.
799       if (u.pos == col)
800         return {1, a};
801 
802       // Non-pivot column case.
803       return {0, 1};
804     }
805 
806     // Pivot row case.
807     if (u.pos == row)
808       return {1, 1};
809 
810     // Non-pivot row case.
811     int64_t c = tableau(u.pos, col);
812     return {c, a};
813   };
814 
815   for (const Unknown &u : var) {
816     Fraction changeA = getSampleChangeCoeffForVar(colA, u);
817     Fraction changeB = getSampleChangeCoeffForVar(colB, u);
818     if (changeA < changeB)
819       return colA;
820     if (changeA > changeB)
821       return colB;
822   }
823 
824   // If we reached here, both result in exactly the same changes, so it
825   // doesn't matter which we return.
826   return colA;
827 }
828 
829 /// Find a pivot to change the sample value of the row in the specified
830 /// direction. The returned pivot row will involve `row` if and only if the
831 /// unknown is unbounded in the specified direction.
832 ///
833 /// To increase (resp. decrease) the value of a row, we need to find a live
834 /// column with a non-zero coefficient. If the coefficient is positive, we need
835 /// to increase (decrease) the value of the column, and if the coefficient is
836 /// negative, we need to decrease (increase) the value of the column. Also,
837 /// we cannot decrease the sample value of restricted columns.
838 ///
839 /// If multiple columns are valid, we break ties by considering a lexicographic
840 /// ordering where we prefer unknowns with lower index.
841 Optional<SimplexBase::Pivot> Simplex::findPivot(int row,
842                                                 Direction direction) const {
843   Optional<unsigned> col;
844   for (unsigned j = 2; j < nCol; ++j) {
845     int64_t elem = tableau(row, j);
846     if (elem == 0)
847       continue;
848 
849     if (unknownFromColumn(j).restricted &&
850         !signMatchesDirection(elem, direction))
851       continue;
852     if (!col || colUnknown[j] < colUnknown[*col])
853       col = j;
854   }
855 
856   if (!col)
857     return {};
858 
859   Direction newDirection =
860       tableau(row, *col) < 0 ? flippedDirection(direction) : direction;
861   Optional<unsigned> maybePivotRow = findPivotRow(row, newDirection, *col);
862   return Pivot{maybePivotRow.getValueOr(row), *col};
863 }
864 
865 /// Swap the associated unknowns for the row and the column.
866 ///
867 /// First we swap the index associated with the row and column. Then we update
868 /// the unknowns to reflect their new position and orientation.
869 void SimplexBase::swapRowWithCol(unsigned row, unsigned col) {
870   std::swap(rowUnknown[row], colUnknown[col]);
871   Unknown &uCol = unknownFromColumn(col);
872   Unknown &uRow = unknownFromRow(row);
873   uCol.orientation = Orientation::Column;
874   uRow.orientation = Orientation::Row;
875   uCol.pos = col;
876   uRow.pos = row;
877 }
878 
879 void SimplexBase::pivot(Pivot pair) { pivot(pair.row, pair.column); }
880 
881 /// Pivot pivotRow and pivotCol.
882 ///
883 /// Let R be the pivot row unknown and let C be the pivot col unknown.
884 /// Since initially R = a*C + sum b_i * X_i
885 /// (where the sum is over the other column's unknowns, x_i)
886 /// C = (R - (sum b_i * X_i))/a
887 ///
888 /// Let u be some other row unknown.
889 /// u = c*C + sum d_i * X_i
890 /// So u = c*(R - sum b_i * X_i)/a + sum d_i * X_i
891 ///
892 /// This results in the following transform:
893 ///            pivot col    other col                   pivot col    other col
894 /// pivot row     a             b       ->   pivot row     1/a         -b/a
895 /// other row     c             d            other row     c/a        d - bc/a
896 ///
897 /// Taking into account the common denominators p and q:
898 ///
899 ///            pivot col    other col                    pivot col   other col
900 /// pivot row     a/p          b/p     ->   pivot row      p/a         -b/a
901 /// other row     c/q          d/q          other row     cp/aq    (da - bc)/aq
902 ///
903 /// The pivot row transform is accomplished be swapping a with the pivot row's
904 /// common denominator and negating the pivot row except for the pivot column
905 /// element.
906 void SimplexBase::pivot(unsigned pivotRow, unsigned pivotCol) {
907   assert(pivotCol >= getNumFixedCols() && "Refusing to pivot invalid column");
908   assert(!unknownFromColumn(pivotCol).isSymbol);
909 
910   swapRowWithCol(pivotRow, pivotCol);
911   std::swap(tableau(pivotRow, 0), tableau(pivotRow, pivotCol));
912   // We need to negate the whole pivot row except for the pivot column.
913   if (tableau(pivotRow, 0) < 0) {
914     // If the denominator is negative, we negate the row by simply negating the
915     // denominator.
916     tableau(pivotRow, 0) = -tableau(pivotRow, 0);
917     tableau(pivotRow, pivotCol) = -tableau(pivotRow, pivotCol);
918   } else {
919     for (unsigned col = 1; col < nCol; ++col) {
920       if (col == pivotCol)
921         continue;
922       tableau(pivotRow, col) = -tableau(pivotRow, col);
923     }
924   }
925   normalizeRow(pivotRow);
926 
927   for (unsigned row = 0; row < nRow; ++row) {
928     if (row == pivotRow)
929       continue;
930     if (tableau(row, pivotCol) == 0) // Nothing to do.
931       continue;
932     tableau(row, 0) *= tableau(pivotRow, 0);
933     for (unsigned j = 1; j < nCol; ++j) {
934       if (j == pivotCol)
935         continue;
936       // Add rather than subtract because the pivot row has been negated.
937       tableau(row, j) = tableau(row, j) * tableau(pivotRow, 0) +
938                         tableau(row, pivotCol) * tableau(pivotRow, j);
939     }
940     tableau(row, pivotCol) *= tableau(pivotRow, pivotCol);
941     normalizeRow(row);
942   }
943 }
944 
945 /// Perform pivots until the unknown has a non-negative sample value or until
946 /// no more upward pivots can be performed. Return success if we were able to
947 /// bring the row to a non-negative sample value, and failure otherwise.
948 LogicalResult Simplex::restoreRow(Unknown &u) {
949   assert(u.orientation == Orientation::Row &&
950          "unknown should be in row position");
951 
952   while (tableau(u.pos, 1) < 0) {
953     Optional<Pivot> maybePivot = findPivot(u.pos, Direction::Up);
954     if (!maybePivot)
955       break;
956 
957     pivot(*maybePivot);
958     if (u.orientation == Orientation::Column)
959       return success(); // the unknown is unbounded above.
960   }
961   return success(tableau(u.pos, 1) >= 0);
962 }
963 
964 /// Find a row that can be used to pivot the column in the specified direction.
965 /// This returns an empty optional if and only if the column is unbounded in the
966 /// specified direction (ignoring skipRow, if skipRow is set).
967 ///
968 /// If skipRow is set, this row is not considered, and (if it is restricted) its
969 /// restriction may be violated by the returned pivot. Usually, skipRow is set
970 /// because we don't want to move it to column position unless it is unbounded,
971 /// and we are either trying to increase the value of skipRow or explicitly
972 /// trying to make skipRow negative, so we are not concerned about this.
973 ///
974 /// If the direction is up (resp. down) and a restricted row has a negative
975 /// (positive) coefficient for the column, then this row imposes a bound on how
976 /// much the sample value of the column can change. Such a row with constant
977 /// term c and coefficient f for the column imposes a bound of c/|f| on the
978 /// change in sample value (in the specified direction). (note that c is
979 /// non-negative here since the row is restricted and the tableau is consistent)
980 ///
981 /// We iterate through the rows and pick the row which imposes the most
982 /// stringent bound, since pivoting with a row changes the row's sample value to
983 /// 0 and hence saturates the bound it imposes. We break ties between rows that
984 /// impose the same bound by considering a lexicographic ordering where we
985 /// prefer unknowns with lower index value.
986 Optional<unsigned> Simplex::findPivotRow(Optional<unsigned> skipRow,
987                                          Direction direction,
988                                          unsigned col) const {
989   Optional<unsigned> retRow;
990   // Initialize these to zero in order to silence a warning about retElem and
991   // retConst being used uninitialized in the initialization of `diff` below. In
992   // reality, these are always initialized when that line is reached since these
993   // are set whenever retRow is set.
994   int64_t retElem = 0, retConst = 0;
995   for (unsigned row = nRedundant; row < nRow; ++row) {
996     if (skipRow && row == *skipRow)
997       continue;
998     int64_t elem = tableau(row, col);
999     if (elem == 0)
1000       continue;
1001     if (!unknownFromRow(row).restricted)
1002       continue;
1003     if (signMatchesDirection(elem, direction))
1004       continue;
1005     int64_t constTerm = tableau(row, 1);
1006 
1007     if (!retRow) {
1008       retRow = row;
1009       retElem = elem;
1010       retConst = constTerm;
1011       continue;
1012     }
1013 
1014     int64_t diff = retConst * elem - constTerm * retElem;
1015     if ((diff == 0 && rowUnknown[row] < rowUnknown[*retRow]) ||
1016         (diff != 0 && !signMatchesDirection(diff, direction))) {
1017       retRow = row;
1018       retElem = elem;
1019       retConst = constTerm;
1020     }
1021   }
1022   return retRow;
1023 }
1024 
1025 bool SimplexBase::isEmpty() const { return empty; }
1026 
1027 void SimplexBase::swapRows(unsigned i, unsigned j) {
1028   if (i == j)
1029     return;
1030   tableau.swapRows(i, j);
1031   std::swap(rowUnknown[i], rowUnknown[j]);
1032   unknownFromRow(i).pos = i;
1033   unknownFromRow(j).pos = j;
1034 }
1035 
1036 void SimplexBase::swapColumns(unsigned i, unsigned j) {
1037   assert(i < nCol && j < nCol && "Invalid columns provided!");
1038   if (i == j)
1039     return;
1040   tableau.swapColumns(i, j);
1041   std::swap(colUnknown[i], colUnknown[j]);
1042   unknownFromColumn(i).pos = i;
1043   unknownFromColumn(j).pos = j;
1044 }
1045 
1046 /// Mark this tableau empty and push an entry to the undo stack.
1047 void SimplexBase::markEmpty() {
1048   // If the set is already empty, then we shouldn't add another UnmarkEmpty log
1049   // entry, since in that case the Simplex will be erroneously marked as
1050   // non-empty when rolling back past this point.
1051   if (empty)
1052     return;
1053   undoLog.push_back(UndoLogEntry::UnmarkEmpty);
1054   empty = true;
1055 }
1056 
1057 /// Add an inequality to the tableau. If coeffs is c_0, c_1, ... c_n, where n
1058 /// is the current number of variables, then the corresponding inequality is
1059 /// c_n + c_0*x_0 + c_1*x_1 + ... + c_{n-1}*x_{n-1} >= 0.
1060 ///
1061 /// We add the inequality and mark it as restricted. We then try to make its
1062 /// sample value non-negative. If this is not possible, the tableau has become
1063 /// empty and we mark it as such.
1064 void Simplex::addInequality(ArrayRef<int64_t> coeffs) {
1065   unsigned conIndex = addRow(coeffs, /*makeRestricted=*/true);
1066   LogicalResult result = restoreRow(con[conIndex]);
1067   if (failed(result))
1068     markEmpty();
1069 }
1070 
1071 /// Add an equality to the tableau. If coeffs is c_0, c_1, ... c_n, where n
1072 /// is the current number of variables, then the corresponding equality is
1073 /// c_n + c_0*x_0 + c_1*x_1 + ... + c_{n-1}*x_{n-1} == 0.
1074 ///
1075 /// We simply add two opposing inequalities, which force the expression to
1076 /// be zero.
1077 void SimplexBase::addEquality(ArrayRef<int64_t> coeffs) {
1078   addInequality(coeffs);
1079   SmallVector<int64_t, 8> negatedCoeffs;
1080   for (int64_t coeff : coeffs)
1081     negatedCoeffs.emplace_back(-coeff);
1082   addInequality(negatedCoeffs);
1083 }
1084 
1085 unsigned SimplexBase::getNumVariables() const { return var.size(); }
1086 unsigned SimplexBase::getNumConstraints() const { return con.size(); }
1087 
1088 /// Return a snapshot of the current state. This is just the current size of the
1089 /// undo log.
1090 unsigned SimplexBase::getSnapshot() const { return undoLog.size(); }
1091 
1092 unsigned SimplexBase::getSnapshotBasis() {
1093   SmallVector<int, 8> basis;
1094   for (int index : colUnknown) {
1095     if (index != nullIndex)
1096       basis.push_back(index);
1097   }
1098   savedBases.push_back(std::move(basis));
1099 
1100   undoLog.emplace_back(UndoLogEntry::RestoreBasis);
1101   return undoLog.size() - 1;
1102 }
1103 
1104 void SimplexBase::removeLastConstraintRowOrientation() {
1105   assert(con.back().orientation == Orientation::Row);
1106 
1107   // Move this unknown to the last row and remove the last row from the
1108   // tableau.
1109   swapRows(con.back().pos, nRow - 1);
1110   // It is not strictly necessary to shrink the tableau, but for now we
1111   // maintain the invariant that the tableau has exactly nRow rows.
1112   tableau.resizeVertically(nRow - 1);
1113   nRow--;
1114   rowUnknown.pop_back();
1115   con.pop_back();
1116 }
1117 
1118 // This doesn't find a pivot row only if the column has zero
1119 // coefficients for every row.
1120 //
1121 // If the unknown is a constraint, this can't happen, since it was added
1122 // initially as a row. Such a row could never have been pivoted to a column. So
1123 // a pivot row will always be found if we have a constraint.
1124 //
1125 // If we have a variable, then the column has zero coefficients for every row
1126 // iff no constraints have been added with a non-zero coefficient for this row.
1127 Optional<unsigned> SimplexBase::findAnyPivotRow(unsigned col) {
1128   for (unsigned row = nRedundant; row < nRow; ++row)
1129     if (tableau(row, col) != 0)
1130       return row;
1131   return {};
1132 }
1133 
1134 // It's not valid to remove the constraint by deleting the column since this
1135 // would result in an invalid basis.
1136 void Simplex::undoLastConstraint() {
1137   if (con.back().orientation == Orientation::Column) {
1138     // We try to find any pivot row for this column that preserves tableau
1139     // consistency (except possibly the column itself, which is going to be
1140     // deallocated anyway).
1141     //
1142     // If no pivot row is found in either direction, then the unknown is
1143     // unbounded in both directions and we are free to perform any pivot at
1144     // all. To do this, we just need to find any row with a non-zero
1145     // coefficient for the column. findAnyPivotRow will always be able to
1146     // find such a row for a constraint.
1147     unsigned column = con.back().pos;
1148     if (Optional<unsigned> maybeRow = findPivotRow({}, Direction::Up, column)) {
1149       pivot(*maybeRow, column);
1150     } else if (Optional<unsigned> maybeRow =
1151                    findPivotRow({}, Direction::Down, column)) {
1152       pivot(*maybeRow, column);
1153     } else {
1154       Optional<unsigned> row = findAnyPivotRow(column);
1155       assert(row.hasValue() && "Pivot should always exist for a constraint!");
1156       pivot(*row, column);
1157     }
1158   }
1159   removeLastConstraintRowOrientation();
1160 }
1161 
1162 // It's not valid to remove the constraint by deleting the column since this
1163 // would result in an invalid basis.
1164 void LexSimplexBase::undoLastConstraint() {
1165   if (con.back().orientation == Orientation::Column) {
1166     // When removing the last constraint during a rollback, we just need to find
1167     // any pivot at all, i.e., any row with non-zero coefficient for the
1168     // column, because when rolling back a lexicographic simplex, we always
1169     // end by restoring the exact basis that was present at the time of the
1170     // snapshot, so what pivots we perform while undoing doesn't matter as
1171     // long as we get the unknown to row orientation and remove it.
1172     unsigned column = con.back().pos;
1173     Optional<unsigned> row = findAnyPivotRow(column);
1174     assert(row.hasValue() && "Pivot should always exist for a constraint!");
1175     pivot(*row, column);
1176   }
1177   removeLastConstraintRowOrientation();
1178 }
1179 
1180 void SimplexBase::undo(UndoLogEntry entry) {
1181   if (entry == UndoLogEntry::RemoveLastConstraint) {
1182     // Simplex and LexSimplex handle this differently, so we call out to a
1183     // virtual function to handle this.
1184     undoLastConstraint();
1185   } else if (entry == UndoLogEntry::RemoveLastVariable) {
1186     // Whenever we are rolling back the addition of a variable, it is guaranteed
1187     // that the variable will be in column position.
1188     //
1189     // We can see this as follows: any constraint that depends on this variable
1190     // was added after this variable was added, so the addition of such
1191     // constraints should already have been rolled back by the time we get to
1192     // rolling back the addition of the variable. Therefore, no constraint
1193     // currently has a component along the variable, so the variable itself must
1194     // be part of the basis.
1195     assert(var.back().orientation == Orientation::Column &&
1196            "Variable to be removed must be in column orientation!");
1197 
1198     if (var.back().isSymbol)
1199       nSymbol--;
1200 
1201     // Move this variable to the last column and remove the column from the
1202     // tableau.
1203     swapColumns(var.back().pos, nCol - 1);
1204     tableau.resizeHorizontally(nCol - 1);
1205     var.pop_back();
1206     colUnknown.pop_back();
1207     nCol--;
1208   } else if (entry == UndoLogEntry::UnmarkEmpty) {
1209     empty = false;
1210   } else if (entry == UndoLogEntry::UnmarkLastRedundant) {
1211     nRedundant--;
1212   } else if (entry == UndoLogEntry::RestoreBasis) {
1213     assert(!savedBases.empty() && "No bases saved!");
1214 
1215     SmallVector<int, 8> basis = std::move(savedBases.back());
1216     savedBases.pop_back();
1217 
1218     for (int index : basis) {
1219       Unknown &u = unknownFromIndex(index);
1220       if (u.orientation == Orientation::Column)
1221         continue;
1222       for (unsigned col = getNumFixedCols(); col < nCol; col++) {
1223         assert(colUnknown[col] != nullIndex &&
1224                "Column should not be a fixed column!");
1225         if (std::find(basis.begin(), basis.end(), colUnknown[col]) !=
1226             basis.end())
1227           continue;
1228         if (tableau(u.pos, col) == 0)
1229           continue;
1230         pivot(u.pos, col);
1231         break;
1232       }
1233 
1234       assert(u.orientation == Orientation::Column && "No pivot found!");
1235     }
1236   }
1237 }
1238 
1239 /// Rollback to the specified snapshot.
1240 ///
1241 /// We undo all the log entries until the log size when the snapshot was taken
1242 /// is reached.
1243 void SimplexBase::rollback(unsigned snapshot) {
1244   while (undoLog.size() > snapshot) {
1245     undo(undoLog.back());
1246     undoLog.pop_back();
1247   }
1248 }
1249 
1250 /// We add the usual floor division constraints:
1251 /// `0 <= coeffs - denom*q <= denom - 1`, where `q` is the new division
1252 /// variable.
1253 ///
1254 /// This constrains the remainder `coeffs - denom*q` to be in the
1255 /// range `[0, denom - 1]`, which fixes the integer value of the quotient `q`.
1256 void SimplexBase::addDivisionVariable(ArrayRef<int64_t> coeffs, int64_t denom) {
1257   assert(denom != 0 && "Cannot divide by zero!\n");
1258   appendVariable();
1259 
1260   SmallVector<int64_t, 8> ineq(coeffs.begin(), coeffs.end());
1261   int64_t constTerm = ineq.back();
1262   ineq.back() = -denom;
1263   ineq.push_back(constTerm);
1264   addInequality(ineq);
1265 
1266   for (int64_t &coeff : ineq)
1267     coeff = -coeff;
1268   ineq.back() += denom - 1;
1269   addInequality(ineq);
1270 }
1271 
1272 void SimplexBase::appendVariable(unsigned count) {
1273   if (count == 0)
1274     return;
1275   var.reserve(var.size() + count);
1276   colUnknown.reserve(colUnknown.size() + count);
1277   for (unsigned i = 0; i < count; ++i) {
1278     nCol++;
1279     var.emplace_back(Orientation::Column, /*restricted=*/false,
1280                      /*pos=*/nCol - 1);
1281     colUnknown.push_back(var.size() - 1);
1282   }
1283   tableau.resizeHorizontally(nCol);
1284   undoLog.insert(undoLog.end(), count, UndoLogEntry::RemoveLastVariable);
1285 }
1286 
1287 /// Add all the constraints from the given IntegerRelation.
1288 void SimplexBase::intersectIntegerRelation(const IntegerRelation &rel) {
1289   assert(rel.getNumIds() == getNumVariables() &&
1290          "IntegerRelation must have same dimensionality as simplex");
1291   for (unsigned i = 0, e = rel.getNumInequalities(); i < e; ++i)
1292     addInequality(rel.getInequality(i));
1293   for (unsigned i = 0, e = rel.getNumEqualities(); i < e; ++i)
1294     addEquality(rel.getEquality(i));
1295 }
1296 
1297 MaybeOptimum<Fraction> Simplex::computeRowOptimum(Direction direction,
1298                                                   unsigned row) {
1299   // Keep trying to find a pivot for the row in the specified direction.
1300   while (Optional<Pivot> maybePivot = findPivot(row, direction)) {
1301     // If findPivot returns a pivot involving the row itself, then the optimum
1302     // is unbounded, so we return None.
1303     if (maybePivot->row == row)
1304       return OptimumKind::Unbounded;
1305     pivot(*maybePivot);
1306   }
1307 
1308   // The row has reached its optimal sample value, which we return.
1309   // The sample value is the entry in the constant column divided by the common
1310   // denominator for this row.
1311   return Fraction(tableau(row, 1), tableau(row, 0));
1312 }
1313 
1314 /// Compute the optimum of the specified expression in the specified direction,
1315 /// or None if it is unbounded.
1316 MaybeOptimum<Fraction> Simplex::computeOptimum(Direction direction,
1317                                                ArrayRef<int64_t> coeffs) {
1318   if (empty)
1319     return OptimumKind::Empty;
1320 
1321   SimplexRollbackScopeExit scopeExit(*this);
1322   unsigned conIndex = addRow(coeffs);
1323   unsigned row = con[conIndex].pos;
1324   MaybeOptimum<Fraction> optimum = computeRowOptimum(direction, row);
1325   return optimum;
1326 }
1327 
1328 MaybeOptimum<Fraction> Simplex::computeOptimum(Direction direction,
1329                                                Unknown &u) {
1330   if (empty)
1331     return OptimumKind::Empty;
1332   if (u.orientation == Orientation::Column) {
1333     unsigned column = u.pos;
1334     Optional<unsigned> pivotRow = findPivotRow({}, direction, column);
1335     // If no pivot is returned, the constraint is unbounded in the specified
1336     // direction.
1337     if (!pivotRow)
1338       return OptimumKind::Unbounded;
1339     pivot(*pivotRow, column);
1340   }
1341 
1342   unsigned row = u.pos;
1343   MaybeOptimum<Fraction> optimum = computeRowOptimum(direction, row);
1344   if (u.restricted && direction == Direction::Down &&
1345       (optimum.isUnbounded() || *optimum < Fraction(0, 1))) {
1346     if (failed(restoreRow(u)))
1347       llvm_unreachable("Could not restore row!");
1348   }
1349   return optimum;
1350 }
1351 
1352 bool Simplex::isBoundedAlongConstraint(unsigned constraintIndex) {
1353   assert(!empty && "It is not meaningful to ask whether a direction is bounded "
1354                    "in an empty set.");
1355   // The constraint's perpendicular is already bounded below, since it is a
1356   // constraint. If it is also bounded above, we can return true.
1357   return computeOptimum(Direction::Up, con[constraintIndex]).isBounded();
1358 }
1359 
1360 /// Redundant constraints are those that are in row orientation and lie in
1361 /// rows 0 to nRedundant - 1.
1362 bool Simplex::isMarkedRedundant(unsigned constraintIndex) const {
1363   const Unknown &u = con[constraintIndex];
1364   return u.orientation == Orientation::Row && u.pos < nRedundant;
1365 }
1366 
1367 /// Mark the specified row redundant.
1368 ///
1369 /// This is done by moving the unknown to the end of the block of redundant
1370 /// rows (namely, to row nRedundant) and incrementing nRedundant to
1371 /// accomodate the new redundant row.
1372 void Simplex::markRowRedundant(Unknown &u) {
1373   assert(u.orientation == Orientation::Row &&
1374          "Unknown should be in row position!");
1375   assert(u.pos >= nRedundant && "Unknown is already marked redundant!");
1376   swapRows(u.pos, nRedundant);
1377   ++nRedundant;
1378   undoLog.emplace_back(UndoLogEntry::UnmarkLastRedundant);
1379 }
1380 
1381 /// Find a subset of constraints that is redundant and mark them redundant.
1382 void Simplex::detectRedundant() {
1383   // It is not meaningful to talk about redundancy for empty sets.
1384   if (empty)
1385     return;
1386 
1387   // Iterate through the constraints and check for each one if it can attain
1388   // negative sample values. If it can, it's not redundant. Otherwise, it is.
1389   // We mark redundant constraints redundant.
1390   //
1391   // Constraints that get marked redundant in one iteration are not respected
1392   // when checking constraints in later iterations. This prevents, for example,
1393   // two identical constraints both being marked redundant since each is
1394   // redundant given the other one. In this example, only the first of the
1395   // constraints that is processed will get marked redundant, as it should be.
1396   for (Unknown &u : con) {
1397     if (u.orientation == Orientation::Column) {
1398       unsigned column = u.pos;
1399       Optional<unsigned> pivotRow = findPivotRow({}, Direction::Down, column);
1400       // If no downward pivot is returned, the constraint is unbounded below
1401       // and hence not redundant.
1402       if (!pivotRow)
1403         continue;
1404       pivot(*pivotRow, column);
1405     }
1406 
1407     unsigned row = u.pos;
1408     MaybeOptimum<Fraction> minimum = computeRowOptimum(Direction::Down, row);
1409     if (minimum.isUnbounded() || *minimum < Fraction(0, 1)) {
1410       // Constraint is unbounded below or can attain negative sample values and
1411       // hence is not redundant.
1412       if (failed(restoreRow(u)))
1413         llvm_unreachable("Could not restore non-redundant row!");
1414       continue;
1415     }
1416 
1417     markRowRedundant(u);
1418   }
1419 }
1420 
1421 bool Simplex::isUnbounded() {
1422   if (empty)
1423     return false;
1424 
1425   SmallVector<int64_t, 8> dir(var.size() + 1);
1426   for (unsigned i = 0; i < var.size(); ++i) {
1427     dir[i] = 1;
1428 
1429     if (computeOptimum(Direction::Up, dir).isUnbounded())
1430       return true;
1431 
1432     if (computeOptimum(Direction::Down, dir).isUnbounded())
1433       return true;
1434 
1435     dir[i] = 0;
1436   }
1437   return false;
1438 }
1439 
1440 /// Make a tableau to represent a pair of points in the original tableau.
1441 ///
1442 /// The product constraints and variables are stored as: first A's, then B's.
1443 ///
1444 /// The product tableau has row layout:
1445 ///   A's redundant rows, B's redundant rows, A's other rows, B's other rows.
1446 ///
1447 /// It has column layout:
1448 ///   denominator, constant, A's columns, B's columns.
1449 Simplex Simplex::makeProduct(const Simplex &a, const Simplex &b) {
1450   unsigned numVar = a.getNumVariables() + b.getNumVariables();
1451   unsigned numCon = a.getNumConstraints() + b.getNumConstraints();
1452   Simplex result(numVar);
1453 
1454   result.tableau.resizeVertically(numCon);
1455   result.empty = a.empty || b.empty;
1456 
1457   auto concat = [](ArrayRef<Unknown> v, ArrayRef<Unknown> w) {
1458     SmallVector<Unknown, 8> result;
1459     result.reserve(v.size() + w.size());
1460     result.insert(result.end(), v.begin(), v.end());
1461     result.insert(result.end(), w.begin(), w.end());
1462     return result;
1463   };
1464   result.con = concat(a.con, b.con);
1465   result.var = concat(a.var, b.var);
1466 
1467   auto indexFromBIndex = [&](int index) {
1468     return index >= 0 ? a.getNumVariables() + index
1469                       : ~(a.getNumConstraints() + ~index);
1470   };
1471 
1472   result.colUnknown.assign(2, nullIndex);
1473   for (unsigned i = 2; i < a.nCol; ++i) {
1474     result.colUnknown.push_back(a.colUnknown[i]);
1475     result.unknownFromIndex(result.colUnknown.back()).pos =
1476         result.colUnknown.size() - 1;
1477   }
1478   for (unsigned i = 2; i < b.nCol; ++i) {
1479     result.colUnknown.push_back(indexFromBIndex(b.colUnknown[i]));
1480     result.unknownFromIndex(result.colUnknown.back()).pos =
1481         result.colUnknown.size() - 1;
1482   }
1483 
1484   auto appendRowFromA = [&](unsigned row) {
1485     for (unsigned col = 0; col < a.nCol; ++col)
1486       result.tableau(result.nRow, col) = a.tableau(row, col);
1487     result.rowUnknown.push_back(a.rowUnknown[row]);
1488     result.unknownFromIndex(result.rowUnknown.back()).pos =
1489         result.rowUnknown.size() - 1;
1490     result.nRow++;
1491   };
1492 
1493   // Also fixes the corresponding entry in rowUnknown and var/con (as the case
1494   // may be).
1495   auto appendRowFromB = [&](unsigned row) {
1496     result.tableau(result.nRow, 0) = b.tableau(row, 0);
1497     result.tableau(result.nRow, 1) = b.tableau(row, 1);
1498 
1499     unsigned offset = a.nCol - 2;
1500     for (unsigned col = 2; col < b.nCol; ++col)
1501       result.tableau(result.nRow, offset + col) = b.tableau(row, col);
1502     result.rowUnknown.push_back(indexFromBIndex(b.rowUnknown[row]));
1503     result.unknownFromIndex(result.rowUnknown.back()).pos =
1504         result.rowUnknown.size() - 1;
1505     result.nRow++;
1506   };
1507 
1508   result.nRedundant = a.nRedundant + b.nRedundant;
1509   for (unsigned row = 0; row < a.nRedundant; ++row)
1510     appendRowFromA(row);
1511   for (unsigned row = 0; row < b.nRedundant; ++row)
1512     appendRowFromB(row);
1513   for (unsigned row = a.nRedundant; row < a.nRow; ++row)
1514     appendRowFromA(row);
1515   for (unsigned row = b.nRedundant; row < b.nRow; ++row)
1516     appendRowFromB(row);
1517 
1518   return result;
1519 }
1520 
1521 Optional<SmallVector<Fraction, 8>> Simplex::getRationalSample() const {
1522   if (empty)
1523     return {};
1524 
1525   SmallVector<Fraction, 8> sample;
1526   sample.reserve(var.size());
1527   // Push the sample value for each variable into the vector.
1528   for (const Unknown &u : var) {
1529     if (u.orientation == Orientation::Column) {
1530       // If the variable is in column position, its sample value is zero.
1531       sample.emplace_back(0, 1);
1532     } else {
1533       // If the variable is in row position, its sample value is the
1534       // entry in the constant column divided by the denominator.
1535       int64_t denom = tableau(u.pos, 0);
1536       sample.emplace_back(tableau(u.pos, 1), denom);
1537     }
1538   }
1539   return sample;
1540 }
1541 
1542 void LexSimplexBase::addInequality(ArrayRef<int64_t> coeffs) {
1543   addRow(coeffs, /*makeRestricted=*/true);
1544 }
1545 
1546 MaybeOptimum<SmallVector<Fraction, 8>> LexSimplex::getRationalSample() const {
1547   if (empty)
1548     return OptimumKind::Empty;
1549 
1550   SmallVector<Fraction, 8> sample;
1551   sample.reserve(var.size());
1552   // Push the sample value for each variable into the vector.
1553   for (const Unknown &u : var) {
1554     // When the big M parameter is being used, each variable x is represented
1555     // as M + x, so its sample value is finite if and only if it is of the
1556     // form 1*M + c. If the coefficient of M is not one then the sample value
1557     // is infinite, and we return an empty optional.
1558 
1559     if (u.orientation == Orientation::Column) {
1560       // If the variable is in column position, the sample value of M + x is
1561       // zero, so x = -M which is unbounded.
1562       return OptimumKind::Unbounded;
1563     }
1564 
1565     // If the variable is in row position, its sample value is the
1566     // entry in the constant column divided by the denominator.
1567     int64_t denom = tableau(u.pos, 0);
1568     if (usingBigM)
1569       if (tableau(u.pos, 2) != denom)
1570         return OptimumKind::Unbounded;
1571     sample.emplace_back(tableau(u.pos, 1), denom);
1572   }
1573   return sample;
1574 }
1575 
1576 Optional<SmallVector<int64_t, 8>> Simplex::getSamplePointIfIntegral() const {
1577   // If the tableau is empty, no sample point exists.
1578   if (empty)
1579     return {};
1580 
1581   // The value will always exist since the Simplex is non-empty.
1582   SmallVector<Fraction, 8> rationalSample = *getRationalSample();
1583   SmallVector<int64_t, 8> integerSample;
1584   integerSample.reserve(var.size());
1585   for (const Fraction &coord : rationalSample) {
1586     // If the sample is non-integral, return None.
1587     if (coord.num % coord.den != 0)
1588       return {};
1589     integerSample.push_back(coord.num / coord.den);
1590   }
1591   return integerSample;
1592 }
1593 
1594 /// Given a simplex for a polytope, construct a new simplex whose variables are
1595 /// identified with a pair of points (x, y) in the original polytope. Supports
1596 /// some operations needed for generalized basis reduction. In what follows,
1597 /// dotProduct(x, y) = x_1 * y_1 + x_2 * y_2 + ... x_n * y_n where n is the
1598 /// dimension of the original polytope.
1599 ///
1600 /// This supports adding equality constraints dotProduct(dir, x - y) == 0. It
1601 /// also supports rolling back this addition, by maintaining a snapshot stack
1602 /// that contains a snapshot of the Simplex's state for each equality, just
1603 /// before that equality was added.
1604 class presburger::GBRSimplex {
1605   using Orientation = Simplex::Orientation;
1606 
1607 public:
1608   GBRSimplex(const Simplex &originalSimplex)
1609       : simplex(Simplex::makeProduct(originalSimplex, originalSimplex)),
1610         simplexConstraintOffset(simplex.getNumConstraints()) {}
1611 
1612   /// Add an equality dotProduct(dir, x - y) == 0.
1613   /// First pushes a snapshot for the current simplex state to the stack so
1614   /// that this can be rolled back later.
1615   void addEqualityForDirection(ArrayRef<int64_t> dir) {
1616     assert(llvm::any_of(dir, [](int64_t x) { return x != 0; }) &&
1617            "Direction passed is the zero vector!");
1618     snapshotStack.push_back(simplex.getSnapshot());
1619     simplex.addEquality(getCoeffsForDirection(dir));
1620   }
1621   /// Compute max(dotProduct(dir, x - y)).
1622   Fraction computeWidth(ArrayRef<int64_t> dir) {
1623     MaybeOptimum<Fraction> maybeWidth =
1624         simplex.computeOptimum(Direction::Up, getCoeffsForDirection(dir));
1625     assert(maybeWidth.isBounded() && "Width should be bounded!");
1626     return *maybeWidth;
1627   }
1628 
1629   /// Compute max(dotProduct(dir, x - y)) and save the dual variables for only
1630   /// the direction equalities to `dual`.
1631   Fraction computeWidthAndDuals(ArrayRef<int64_t> dir,
1632                                 SmallVectorImpl<int64_t> &dual,
1633                                 int64_t &dualDenom) {
1634     // We can't just call into computeWidth or computeOptimum since we need to
1635     // access the state of the tableau after computing the optimum, and these
1636     // functions rollback the insertion of the objective function into the
1637     // tableau before returning. We instead add a row for the objective function
1638     // ourselves, call into computeOptimum, compute the duals from the tableau
1639     // state, and finally rollback the addition of the row before returning.
1640     SimplexRollbackScopeExit scopeExit(simplex);
1641     unsigned conIndex = simplex.addRow(getCoeffsForDirection(dir));
1642     unsigned row = simplex.con[conIndex].pos;
1643     MaybeOptimum<Fraction> maybeWidth =
1644         simplex.computeRowOptimum(Simplex::Direction::Up, row);
1645     assert(maybeWidth.isBounded() && "Width should be bounded!");
1646     dualDenom = simplex.tableau(row, 0);
1647     dual.clear();
1648 
1649     // The increment is i += 2 because equalities are added as two inequalities,
1650     // one positive and one negative. Each iteration processes one equality.
1651     for (unsigned i = simplexConstraintOffset; i < conIndex; i += 2) {
1652       // The dual variable for an inequality in column orientation is the
1653       // negative of its coefficient at the objective row. If the inequality is
1654       // in row orientation, the corresponding dual variable is zero.
1655       //
1656       // We want the dual for the original equality, which corresponds to two
1657       // inequalities: a positive inequality, which has the same coefficients as
1658       // the equality, and a negative equality, which has negated coefficients.
1659       //
1660       // Note that at most one of these inequalities can be in column
1661       // orientation because the column unknowns should form a basis and hence
1662       // must be linearly independent. If the positive inequality is in column
1663       // position, its dual is the dual corresponding to the equality. If the
1664       // negative inequality is in column position, the negation of its dual is
1665       // the dual corresponding to the equality. If neither is in column
1666       // position, then that means that this equality is redundant, and its dual
1667       // is zero.
1668       //
1669       // Note that it is NOT valid to perform pivots during the computation of
1670       // the duals. This entire dual computation must be performed on the same
1671       // tableau configuration.
1672       assert(!(simplex.con[i].orientation == Orientation::Column &&
1673                simplex.con[i + 1].orientation == Orientation::Column) &&
1674              "Both inequalities for the equality cannot be in column "
1675              "orientation!");
1676       if (simplex.con[i].orientation == Orientation::Column)
1677         dual.push_back(-simplex.tableau(row, simplex.con[i].pos));
1678       else if (simplex.con[i + 1].orientation == Orientation::Column)
1679         dual.push_back(simplex.tableau(row, simplex.con[i + 1].pos));
1680       else
1681         dual.push_back(0);
1682     }
1683     return *maybeWidth;
1684   }
1685 
1686   /// Remove the last equality that was added through addEqualityForDirection.
1687   ///
1688   /// We do this by rolling back to the snapshot at the top of the stack, which
1689   /// should be a snapshot taken just before the last equality was added.
1690   void removeLastEquality() {
1691     assert(!snapshotStack.empty() && "Snapshot stack is empty!");
1692     simplex.rollback(snapshotStack.back());
1693     snapshotStack.pop_back();
1694   }
1695 
1696 private:
1697   /// Returns coefficients of the expression 'dot_product(dir, x - y)',
1698   /// i.e.,   dir_1 * x_1 + dir_2 * x_2 + ... + dir_n * x_n
1699   ///       - dir_1 * y_1 - dir_2 * y_2 - ... - dir_n * y_n,
1700   /// where n is the dimension of the original polytope.
1701   SmallVector<int64_t, 8> getCoeffsForDirection(ArrayRef<int64_t> dir) {
1702     assert(2 * dir.size() == simplex.getNumVariables() &&
1703            "Direction vector has wrong dimensionality");
1704     SmallVector<int64_t, 8> coeffs(dir.begin(), dir.end());
1705     coeffs.reserve(2 * dir.size());
1706     for (int64_t coeff : dir)
1707       coeffs.push_back(-coeff);
1708     coeffs.push_back(0); // constant term
1709     return coeffs;
1710   }
1711 
1712   Simplex simplex;
1713   /// The first index of the equality constraints, the index immediately after
1714   /// the last constraint in the initial product simplex.
1715   unsigned simplexConstraintOffset;
1716   /// A stack of snapshots, used for rolling back.
1717   SmallVector<unsigned, 8> snapshotStack;
1718 };
1719 
1720 // Return a + scale*b;
1721 static SmallVector<int64_t, 8> scaleAndAdd(ArrayRef<int64_t> a, int64_t scale,
1722                                            ArrayRef<int64_t> b) {
1723   assert(a.size() == b.size());
1724   SmallVector<int64_t, 8> res;
1725   res.reserve(a.size());
1726   for (unsigned i = 0, e = a.size(); i < e; ++i)
1727     res.push_back(a[i] + scale * b[i]);
1728   return res;
1729 }
1730 
1731 /// Reduce the basis to try and find a direction in which the polytope is
1732 /// "thin". This only works for bounded polytopes.
1733 ///
1734 /// This is an implementation of the algorithm described in the paper
1735 /// "An Implementation of Generalized Basis Reduction for Integer Programming"
1736 /// by W. Cook, T. Rutherford, H. E. Scarf, D. Shallcross.
1737 ///
1738 /// Let b_{level}, b_{level + 1}, ... b_n be the current basis.
1739 /// Let width_i(v) = max <v, x - y> where x and y are points in the original
1740 /// polytope such that <b_j, x - y> = 0 is satisfied for all level <= j < i.
1741 ///
1742 /// In every iteration, we first replace b_{i+1} with b_{i+1} + u*b_i, where u
1743 /// is the integer such that width_i(b_{i+1} + u*b_i) is minimized. Let dual_i
1744 /// be the dual variable associated with the constraint <b_i, x - y> = 0 when
1745 /// computing width_{i+1}(b_{i+1}). It can be shown that dual_i is the
1746 /// minimizing value of u, if it were allowed to be fractional. Due to
1747 /// convexity, the minimizing integer value is either floor(dual_i) or
1748 /// ceil(dual_i), so we just need to check which of these gives a lower
1749 /// width_{i+1} value. If dual_i turned out to be an integer, then u = dual_i.
1750 ///
1751 /// Now if width_i(b_{i+1}) < 0.75 * width_i(b_i), we swap b_i and (the new)
1752 /// b_{i + 1} and decrement i (unless i = level, in which case we stay at the
1753 /// same i). Otherwise, we increment i.
1754 ///
1755 /// We keep f values and duals cached and invalidate them when necessary.
1756 /// Whenever possible, we use them instead of recomputing them. We implement the
1757 /// algorithm as follows.
1758 ///
1759 /// In an iteration at i we need to compute:
1760 ///   a) width_i(b_{i + 1})
1761 ///   b) width_i(b_i)
1762 ///   c) the integer u that minimizes width_i(b_{i + 1} + u*b_i)
1763 ///
1764 /// If width_i(b_i) is not already cached, we compute it.
1765 ///
1766 /// If the duals are not already cached, we compute width_{i+1}(b_{i+1}) and
1767 /// store the duals from this computation.
1768 ///
1769 /// We call updateBasisWithUAndGetFCandidate, which finds the minimizing value
1770 /// of u as explained before, caches the duals from this computation, sets
1771 /// b_{i+1} to b_{i+1} + u*b_i, and returns the new value of width_i(b_{i+1}).
1772 ///
1773 /// Now if width_i(b_{i+1}) < 0.75 * width_i(b_i), we swap b_i and b_{i+1} and
1774 /// decrement i, resulting in the basis
1775 /// ... b_{i - 1}, b_{i + 1} + u*b_i, b_i, b_{i+2}, ...
1776 /// with corresponding f values
1777 /// ... width_{i-1}(b_{i-1}), width_i(b_{i+1} + u*b_i), width_{i+1}(b_i), ...
1778 /// The values up to i - 1 remain unchanged. We have just gotten the middle
1779 /// value from updateBasisWithUAndGetFCandidate, so we can update that in the
1780 /// cache. The value at width_{i+1}(b_i) is unknown, so we evict this value from
1781 /// the cache. The iteration after decrementing needs exactly the duals from the
1782 /// computation of width_i(b_{i + 1} + u*b_i), so we keep these in the cache.
1783 ///
1784 /// When incrementing i, no cached f values get invalidated. However, the cached
1785 /// duals do get invalidated as the duals for the higher levels are different.
1786 void Simplex::reduceBasis(Matrix &basis, unsigned level) {
1787   const Fraction epsilon(3, 4);
1788 
1789   if (level == basis.getNumRows() - 1)
1790     return;
1791 
1792   GBRSimplex gbrSimplex(*this);
1793   SmallVector<Fraction, 8> width;
1794   SmallVector<int64_t, 8> dual;
1795   int64_t dualDenom;
1796 
1797   // Finds the value of u that minimizes width_i(b_{i+1} + u*b_i), caches the
1798   // duals from this computation, sets b_{i+1} to b_{i+1} + u*b_i, and returns
1799   // the new value of width_i(b_{i+1}).
1800   //
1801   // If dual_i is not an integer, the minimizing value must be either
1802   // floor(dual_i) or ceil(dual_i). We compute the expression for both and
1803   // choose the minimizing value.
1804   //
1805   // If dual_i is an integer, we don't need to perform these computations. We
1806   // know that in this case,
1807   //   a) u = dual_i.
1808   //   b) one can show that dual_j for j < i are the same duals we would have
1809   //      gotten from computing width_i(b_{i + 1} + u*b_i), so the correct duals
1810   //      are the ones already in the cache.
1811   //   c) width_i(b_{i+1} + u*b_i) = min_{alpha} width_i(b_{i+1} + alpha * b_i),
1812   //   which
1813   //      one can show is equal to width_{i+1}(b_{i+1}). The latter value must
1814   //      be in the cache, so we get it from there and return it.
1815   auto updateBasisWithUAndGetFCandidate = [&](unsigned i) -> Fraction {
1816     assert(i < level + dual.size() && "dual_i is not known!");
1817 
1818     int64_t u = floorDiv(dual[i - level], dualDenom);
1819     basis.addToRow(i, i + 1, u);
1820     if (dual[i - level] % dualDenom != 0) {
1821       SmallVector<int64_t, 8> candidateDual[2];
1822       int64_t candidateDualDenom[2];
1823       Fraction widthI[2];
1824 
1825       // Initially u is floor(dual) and basis reflects this.
1826       widthI[0] = gbrSimplex.computeWidthAndDuals(
1827           basis.getRow(i + 1), candidateDual[0], candidateDualDenom[0]);
1828 
1829       // Now try ceil(dual), i.e. floor(dual) + 1.
1830       ++u;
1831       basis.addToRow(i, i + 1, 1);
1832       widthI[1] = gbrSimplex.computeWidthAndDuals(
1833           basis.getRow(i + 1), candidateDual[1], candidateDualDenom[1]);
1834 
1835       unsigned j = widthI[0] < widthI[1] ? 0 : 1;
1836       if (j == 0)
1837         // Subtract 1 to go from u = ceil(dual) back to floor(dual).
1838         basis.addToRow(i, i + 1, -1);
1839 
1840       // width_i(b{i+1} + u*b_i) should be minimized at our value of u.
1841       // We assert that this holds by checking that the values of width_i at
1842       // u - 1 and u + 1 are greater than or equal to the value at u. If the
1843       // width is lesser at either of the adjacent values, then our computed
1844       // value of u is clearly not the minimizer. Otherwise by convexity the
1845       // computed value of u is really the minimizer.
1846 
1847       // Check the value at u - 1.
1848       assert(gbrSimplex.computeWidth(scaleAndAdd(
1849                  basis.getRow(i + 1), -1, basis.getRow(i))) >= widthI[j] &&
1850              "Computed u value does not minimize the width!");
1851       // Check the value at u + 1.
1852       assert(gbrSimplex.computeWidth(scaleAndAdd(
1853                  basis.getRow(i + 1), +1, basis.getRow(i))) >= widthI[j] &&
1854              "Computed u value does not minimize the width!");
1855 
1856       dual = std::move(candidateDual[j]);
1857       dualDenom = candidateDualDenom[j];
1858       return widthI[j];
1859     }
1860 
1861     assert(i + 1 - level < width.size() && "width_{i+1} wasn't saved");
1862     // f_i(b_{i+1} + dual*b_i) == width_{i+1}(b_{i+1}) when `dual` minimizes the
1863     // LHS. (note: the basis has already been updated, so b_{i+1} + dual*b_i in
1864     // the above expression is equal to basis.getRow(i+1) below.)
1865     assert(gbrSimplex.computeWidth(basis.getRow(i + 1)) ==
1866            width[i + 1 - level]);
1867     return width[i + 1 - level];
1868   };
1869 
1870   // In the ith iteration of the loop, gbrSimplex has constraints for directions
1871   // from `level` to i - 1.
1872   unsigned i = level;
1873   while (i < basis.getNumRows() - 1) {
1874     if (i >= level + width.size()) {
1875       // We don't even know the value of f_i(b_i), so let's find that first.
1876       // We have to do this first since later we assume that width already
1877       // contains values up to and including i.
1878 
1879       assert((i == 0 || i - 1 < level + width.size()) &&
1880              "We are at level i but we don't know the value of width_{i-1}");
1881 
1882       // We don't actually use these duals at all, but it doesn't matter
1883       // because this case should only occur when i is level, and there are no
1884       // duals in that case anyway.
1885       assert(i == level && "This case should only occur when i == level");
1886       width.push_back(
1887           gbrSimplex.computeWidthAndDuals(basis.getRow(i), dual, dualDenom));
1888     }
1889 
1890     if (i >= level + dual.size()) {
1891       assert(i + 1 >= level + width.size() &&
1892              "We don't know dual_i but we know width_{i+1}");
1893       // We don't know dual for our level, so let's find it.
1894       gbrSimplex.addEqualityForDirection(basis.getRow(i));
1895       width.push_back(gbrSimplex.computeWidthAndDuals(basis.getRow(i + 1), dual,
1896                                                       dualDenom));
1897       gbrSimplex.removeLastEquality();
1898     }
1899 
1900     // This variable stores width_i(b_{i+1} + u*b_i).
1901     Fraction widthICandidate = updateBasisWithUAndGetFCandidate(i);
1902     if (widthICandidate < epsilon * width[i - level]) {
1903       basis.swapRows(i, i + 1);
1904       width[i - level] = widthICandidate;
1905       // The values of width_{i+1}(b_{i+1}) and higher may change after the
1906       // swap, so we remove the cached values here.
1907       width.resize(i - level + 1);
1908       if (i == level) {
1909         dual.clear();
1910         continue;
1911       }
1912 
1913       gbrSimplex.removeLastEquality();
1914       i--;
1915       continue;
1916     }
1917 
1918     // Invalidate duals since the higher level needs to recompute its own duals.
1919     dual.clear();
1920     gbrSimplex.addEqualityForDirection(basis.getRow(i));
1921     i++;
1922   }
1923 }
1924 
1925 /// Search for an integer sample point using a branch and bound algorithm.
1926 ///
1927 /// Each row in the basis matrix is a vector, and the set of basis vectors
1928 /// should span the space. Initially this is the identity matrix,
1929 /// i.e., the basis vectors are just the variables.
1930 ///
1931 /// In every level, a value is assigned to the level-th basis vector, as
1932 /// follows. Compute the minimum and maximum rational values of this direction.
1933 /// If only one integer point lies in this range, constrain the variable to
1934 /// have this value and recurse to the next variable.
1935 ///
1936 /// If the range has multiple values, perform generalized basis reduction via
1937 /// reduceBasis and then compute the bounds again. Now we try constraining
1938 /// this direction in the first value in this range and "recurse" to the next
1939 /// level. If we fail to find a sample, we try assigning the direction the next
1940 /// value in this range, and so on.
1941 ///
1942 /// If no integer sample is found from any of the assignments, or if the range
1943 /// contains no integer value, then of course the polytope is empty for the
1944 /// current assignment of the values in previous levels, so we return to
1945 /// the previous level.
1946 ///
1947 /// If we reach the last level where all the variables have been assigned values
1948 /// already, then we simply return the current sample point if it is integral,
1949 /// and go back to the previous level otherwise.
1950 ///
1951 /// To avoid potentially arbitrarily large recursion depths leading to stack
1952 /// overflows, this algorithm is implemented iteratively.
1953 Optional<SmallVector<int64_t, 8>> Simplex::findIntegerSample() {
1954   if (empty)
1955     return {};
1956 
1957   unsigned nDims = var.size();
1958   Matrix basis = Matrix::identity(nDims);
1959 
1960   unsigned level = 0;
1961   // The snapshot just before constraining a direction to a value at each level.
1962   SmallVector<unsigned, 8> snapshotStack;
1963   // The maximum value in the range of the direction for each level.
1964   SmallVector<int64_t, 8> upperBoundStack;
1965   // The next value to try constraining the basis vector to at each level.
1966   SmallVector<int64_t, 8> nextValueStack;
1967 
1968   snapshotStack.reserve(basis.getNumRows());
1969   upperBoundStack.reserve(basis.getNumRows());
1970   nextValueStack.reserve(basis.getNumRows());
1971   while (level != -1u) {
1972     if (level == basis.getNumRows()) {
1973       // We've assigned values to all variables. Return if we have a sample,
1974       // or go back up to the previous level otherwise.
1975       if (auto maybeSample = getSamplePointIfIntegral())
1976         return maybeSample;
1977       level--;
1978       continue;
1979     }
1980 
1981     if (level >= upperBoundStack.size()) {
1982       // We haven't populated the stack values for this level yet, so we have
1983       // just come down a level ("recursed"). Find the lower and upper bounds.
1984       // If there is more than one integer point in the range, perform
1985       // generalized basis reduction.
1986       SmallVector<int64_t, 8> basisCoeffs =
1987           llvm::to_vector<8>(basis.getRow(level));
1988       basisCoeffs.push_back(0);
1989 
1990       MaybeOptimum<int64_t> minRoundedUp, maxRoundedDown;
1991       std::tie(minRoundedUp, maxRoundedDown) =
1992           computeIntegerBounds(basisCoeffs);
1993 
1994       // We don't have any integer values in the range.
1995       // Pop the stack and return up a level.
1996       if (minRoundedUp.isEmpty() || maxRoundedDown.isEmpty()) {
1997         assert((minRoundedUp.isEmpty() && maxRoundedDown.isEmpty()) &&
1998                "If one bound is empty, both should be.");
1999         snapshotStack.pop_back();
2000         nextValueStack.pop_back();
2001         upperBoundStack.pop_back();
2002         level--;
2003         continue;
2004       }
2005 
2006       // We already checked the empty case above.
2007       assert((minRoundedUp.isBounded() && maxRoundedDown.isBounded()) &&
2008              "Polyhedron should be bounded!");
2009 
2010       // Heuristic: if the sample point is integral at this point, just return
2011       // it.
2012       if (auto maybeSample = getSamplePointIfIntegral())
2013         return *maybeSample;
2014 
2015       if (*minRoundedUp < *maxRoundedDown) {
2016         reduceBasis(basis, level);
2017         basisCoeffs = llvm::to_vector<8>(basis.getRow(level));
2018         basisCoeffs.push_back(0);
2019         std::tie(minRoundedUp, maxRoundedDown) =
2020             computeIntegerBounds(basisCoeffs);
2021       }
2022 
2023       snapshotStack.push_back(getSnapshot());
2024       // The smallest value in the range is the next value to try.
2025       // The values in the optionals are guaranteed to exist since we know the
2026       // polytope is bounded.
2027       nextValueStack.push_back(*minRoundedUp);
2028       upperBoundStack.push_back(*maxRoundedDown);
2029     }
2030 
2031     assert((snapshotStack.size() - 1 == level &&
2032             nextValueStack.size() - 1 == level &&
2033             upperBoundStack.size() - 1 == level) &&
2034            "Mismatched variable stack sizes!");
2035 
2036     // Whether we "recursed" or "returned" from a lower level, we rollback
2037     // to the snapshot of the starting state at this level. (in the "recursed"
2038     // case this has no effect)
2039     rollback(snapshotStack.back());
2040     int64_t nextValue = nextValueStack.back();
2041     nextValueStack.back()++;
2042     if (nextValue > upperBoundStack.back()) {
2043       // We have exhausted the range and found no solution. Pop the stack and
2044       // return up a level.
2045       snapshotStack.pop_back();
2046       nextValueStack.pop_back();
2047       upperBoundStack.pop_back();
2048       level--;
2049       continue;
2050     }
2051 
2052     // Try the next value in the range and "recurse" into the next level.
2053     SmallVector<int64_t, 8> basisCoeffs(basis.getRow(level).begin(),
2054                                         basis.getRow(level).end());
2055     basisCoeffs.push_back(-nextValue);
2056     addEquality(basisCoeffs);
2057     level++;
2058   }
2059 
2060   return {};
2061 }
2062 
2063 /// Compute the minimum and maximum integer values the expression can take. We
2064 /// compute each separately.
2065 std::pair<MaybeOptimum<int64_t>, MaybeOptimum<int64_t>>
2066 Simplex::computeIntegerBounds(ArrayRef<int64_t> coeffs) {
2067   MaybeOptimum<int64_t> minRoundedUp(
2068       computeOptimum(Simplex::Direction::Down, coeffs).map(ceil));
2069   MaybeOptimum<int64_t> maxRoundedDown(
2070       computeOptimum(Simplex::Direction::Up, coeffs).map(floor));
2071   return {minRoundedUp, maxRoundedDown};
2072 }
2073 
2074 void SimplexBase::print(raw_ostream &os) const {
2075   os << "rows = " << nRow << ", columns = " << nCol << "\n";
2076   if (empty)
2077     os << "Simplex marked empty!\n";
2078   os << "var: ";
2079   for (unsigned i = 0; i < var.size(); ++i) {
2080     if (i > 0)
2081       os << ", ";
2082     var[i].print(os);
2083   }
2084   os << "\ncon: ";
2085   for (unsigned i = 0; i < con.size(); ++i) {
2086     if (i > 0)
2087       os << ", ";
2088     con[i].print(os);
2089   }
2090   os << '\n';
2091   for (unsigned row = 0; row < nRow; ++row) {
2092     if (row > 0)
2093       os << ", ";
2094     os << "r" << row << ": " << rowUnknown[row];
2095   }
2096   os << '\n';
2097   os << "c0: denom, c1: const";
2098   for (unsigned col = 2; col < nCol; ++col)
2099     os << ", c" << col << ": " << colUnknown[col];
2100   os << '\n';
2101   for (unsigned row = 0; row < nRow; ++row) {
2102     for (unsigned col = 0; col < nCol; ++col)
2103       os << tableau(row, col) << '\t';
2104     os << '\n';
2105   }
2106   os << '\n';
2107 }
2108 
2109 void SimplexBase::dump() const { print(llvm::errs()); }
2110 
2111 bool Simplex::isRationalSubsetOf(const IntegerRelation &rel) {
2112   if (isEmpty())
2113     return true;
2114 
2115   for (unsigned i = 0, e = rel.getNumInequalities(); i < e; ++i)
2116     if (findIneqType(rel.getInequality(i)) != IneqType::Redundant)
2117       return false;
2118 
2119   for (unsigned i = 0, e = rel.getNumEqualities(); i < e; ++i)
2120     if (!isRedundantEquality(rel.getEquality(i)))
2121       return false;
2122 
2123   return true;
2124 }
2125 
2126 /// Returns the type of the inequality with coefficients `coeffs`.
2127 /// Possible types are:
2128 /// Redundant   The inequality is satisfied by all points in the polytope
2129 /// Cut         The inequality is satisfied by some points, but not by others
2130 /// Separate    The inequality is not satisfied by any point
2131 ///
2132 /// Internally, this computes the minimum and the maximum the inequality with
2133 /// coefficients `coeffs` can take. If the minimum is >= 0, the inequality holds
2134 /// for all points in the polytope, so it is redundant.  If the minimum is <= 0
2135 /// and the maximum is >= 0, the points in between the minimum and the
2136 /// inequality do not satisfy it, the points in between the inequality and the
2137 /// maximum satisfy it. Hence, it is a cut inequality. If both are < 0, no
2138 /// points of the polytope satisfy the inequality, which means it is a separate
2139 /// inequality.
2140 Simplex::IneqType Simplex::findIneqType(ArrayRef<int64_t> coeffs) {
2141   MaybeOptimum<Fraction> minimum = computeOptimum(Direction::Down, coeffs);
2142   if (minimum.isBounded() && *minimum >= Fraction(0, 1)) {
2143     return IneqType::Redundant;
2144   }
2145   MaybeOptimum<Fraction> maximum = computeOptimum(Direction::Up, coeffs);
2146   if ((!minimum.isBounded() || *minimum <= Fraction(0, 1)) &&
2147       (!maximum.isBounded() || *maximum >= Fraction(0, 1))) {
2148     return IneqType::Cut;
2149   }
2150   return IneqType::Separate;
2151 }
2152 
2153 /// Checks whether the type of the inequality with coefficients `coeffs`
2154 /// is Redundant.
2155 bool Simplex::isRedundantInequality(ArrayRef<int64_t> coeffs) {
2156   assert(!empty &&
2157          "It is not meaningful to ask about redundancy in an empty set!");
2158   return findIneqType(coeffs) == IneqType::Redundant;
2159 }
2160 
2161 /// Check whether the equality given by `coeffs == 0` is redundant given
2162 /// the existing constraints. This is redundant when `coeffs` is already
2163 /// always zero under the existing constraints. `coeffs` is always zero
2164 /// when the minimum and maximum value that `coeffs` can take are both zero.
2165 bool Simplex::isRedundantEquality(ArrayRef<int64_t> coeffs) {
2166   assert(!empty &&
2167          "It is not meaningful to ask about redundancy in an empty set!");
2168   MaybeOptimum<Fraction> minimum = computeOptimum(Direction::Down, coeffs);
2169   MaybeOptimum<Fraction> maximum = computeOptimum(Direction::Up, coeffs);
2170   assert((!minimum.isEmpty() && !maximum.isEmpty()) &&
2171          "Optima should be non-empty for a non-empty set");
2172   return minimum.isBounded() && maximum.isBounded() &&
2173          *maximum == Fraction(0, 1) && *minimum == Fraction(0, 1);
2174 }
2175