xref: /src/contrib/llvm-project/llvm/lib/Analysis/ConstraintSystem.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1b60736ecSDimitry Andric //===- ConstraintSytem.cpp - A system of linear constraints. ----*- C++ -*-===//
2b60736ecSDimitry Andric //
3b60736ecSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4b60736ecSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5b60736ecSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b60736ecSDimitry Andric //
7b60736ecSDimitry Andric //===----------------------------------------------------------------------===//
8b60736ecSDimitry Andric 
9b60736ecSDimitry Andric #include "llvm/Analysis/ConstraintSystem.h"
10b60736ecSDimitry Andric #include "llvm/ADT/SmallVector.h"
11b60736ecSDimitry Andric #include "llvm/Support/MathExtras.h"
12b60736ecSDimitry Andric #include "llvm/ADT/StringExtras.h"
137fa27ce4SDimitry Andric #include "llvm/IR/Value.h"
14b60736ecSDimitry Andric #include "llvm/Support/Debug.h"
15b60736ecSDimitry Andric 
16b60736ecSDimitry Andric #include <string>
17b60736ecSDimitry Andric 
18b60736ecSDimitry Andric using namespace llvm;
19b60736ecSDimitry Andric 
20b60736ecSDimitry Andric #define DEBUG_TYPE "constraint-system"
21b60736ecSDimitry Andric 
eliminateUsingFM()22b60736ecSDimitry Andric bool ConstraintSystem::eliminateUsingFM() {
23b60736ecSDimitry Andric   // Implementation of Fourier–Motzkin elimination, with some tricks from the
24b60736ecSDimitry Andric   // paper Pugh, William. "The Omega test: a fast and practical integer
25b60736ecSDimitry Andric   // programming algorithm for dependence
26b60736ecSDimitry Andric   //  analysis."
27b60736ecSDimitry Andric   // Supercomputing'91: Proceedings of the 1991 ACM/
28b60736ecSDimitry Andric   // IEEE conference on Supercomputing. IEEE, 1991.
29b60736ecSDimitry Andric   assert(!Constraints.empty() &&
30b60736ecSDimitry Andric          "should only be called for non-empty constraint systems");
31b60736ecSDimitry Andric 
327fa27ce4SDimitry Andric   unsigned LastIdx = NumVariables - 1;
337fa27ce4SDimitry Andric 
347fa27ce4SDimitry Andric   // First, either remove the variable in place if it is 0 or add the row to
357fa27ce4SDimitry Andric   // RemainingRows and remove it from the system.
367fa27ce4SDimitry Andric   SmallVector<SmallVector<Entry, 8>, 4> RemainingRows;
377fa27ce4SDimitry Andric   for (unsigned R1 = 0; R1 < Constraints.size();) {
387fa27ce4SDimitry Andric     SmallVector<Entry, 8> &Row1 = Constraints[R1];
397fa27ce4SDimitry Andric     if (getLastCoefficient(Row1, LastIdx) == 0) {
407fa27ce4SDimitry Andric       if (Row1.size() > 0 && Row1.back().Id == LastIdx)
417fa27ce4SDimitry Andric         Row1.pop_back();
427fa27ce4SDimitry Andric       R1++;
437fa27ce4SDimitry Andric     } else {
447fa27ce4SDimitry Andric       std::swap(Constraints[R1], Constraints.back());
457fa27ce4SDimitry Andric       RemainingRows.push_back(std::move(Constraints.back()));
467fa27ce4SDimitry Andric       Constraints.pop_back();
47b60736ecSDimitry Andric     }
48b60736ecSDimitry Andric   }
49b60736ecSDimitry Andric 
507fa27ce4SDimitry Andric   // Process rows where the variable is != 0.
517fa27ce4SDimitry Andric   unsigned NumRemainingConstraints = RemainingRows.size();
527fa27ce4SDimitry Andric   for (unsigned R1 = 0; R1 < NumRemainingConstraints; R1++) {
53b60736ecSDimitry Andric     // FIXME do not use copy
547fa27ce4SDimitry Andric     for (unsigned R2 = R1 + 1; R2 < NumRemainingConstraints; R2++) {
55b60736ecSDimitry Andric       if (R1 == R2)
56b60736ecSDimitry Andric         continue;
57b60736ecSDimitry Andric 
587fa27ce4SDimitry Andric       int64_t UpperLast = getLastCoefficient(RemainingRows[R2], LastIdx);
597fa27ce4SDimitry Andric       int64_t LowerLast = getLastCoefficient(RemainingRows[R1], LastIdx);
607fa27ce4SDimitry Andric       assert(
617fa27ce4SDimitry Andric           UpperLast != 0 && LowerLast != 0 &&
627fa27ce4SDimitry Andric           "RemainingRows should only contain rows where the variable is != 0");
63b60736ecSDimitry Andric 
647fa27ce4SDimitry Andric       if ((LowerLast < 0 && UpperLast < 0) || (LowerLast > 0 && UpperLast > 0))
65b60736ecSDimitry Andric         continue;
66b60736ecSDimitry Andric 
67b60736ecSDimitry Andric       unsigned LowerR = R1;
68b60736ecSDimitry Andric       unsigned UpperR = R2;
697fa27ce4SDimitry Andric       if (UpperLast < 0) {
70b60736ecSDimitry Andric         std::swap(LowerR, UpperR);
717fa27ce4SDimitry Andric         std::swap(LowerLast, UpperLast);
727fa27ce4SDimitry Andric       }
73b60736ecSDimitry Andric 
747fa27ce4SDimitry Andric       SmallVector<Entry, 8> NR;
757fa27ce4SDimitry Andric       unsigned IdxUpper = 0;
767fa27ce4SDimitry Andric       unsigned IdxLower = 0;
777fa27ce4SDimitry Andric       auto &LowerRow = RemainingRows[LowerR];
787fa27ce4SDimitry Andric       auto &UpperRow = RemainingRows[UpperR];
797fa27ce4SDimitry Andric       while (true) {
807fa27ce4SDimitry Andric         if (IdxUpper >= UpperRow.size() || IdxLower >= LowerRow.size())
817fa27ce4SDimitry Andric           break;
82b60736ecSDimitry Andric         int64_t M1, M2, N;
837fa27ce4SDimitry Andric         int64_t UpperV = 0;
847fa27ce4SDimitry Andric         int64_t LowerV = 0;
857fa27ce4SDimitry Andric         uint16_t CurrentId = std::numeric_limits<uint16_t>::max();
867fa27ce4SDimitry Andric         if (IdxUpper < UpperRow.size()) {
877fa27ce4SDimitry Andric           CurrentId = std::min(UpperRow[IdxUpper].Id, CurrentId);
887fa27ce4SDimitry Andric         }
897fa27ce4SDimitry Andric         if (IdxLower < LowerRow.size()) {
907fa27ce4SDimitry Andric           CurrentId = std::min(LowerRow[IdxLower].Id, CurrentId);
917fa27ce4SDimitry Andric         }
927fa27ce4SDimitry Andric 
937fa27ce4SDimitry Andric         if (IdxUpper < UpperRow.size() && UpperRow[IdxUpper].Id == CurrentId) {
947fa27ce4SDimitry Andric           UpperV = UpperRow[IdxUpper].Coefficient;
957fa27ce4SDimitry Andric           IdxUpper++;
967fa27ce4SDimitry Andric         }
977fa27ce4SDimitry Andric 
98aca2e42cSDimitry Andric         if (MulOverflow(UpperV, -1 * LowerLast, M1))
99b60736ecSDimitry Andric           return false;
1007fa27ce4SDimitry Andric         if (IdxLower < LowerRow.size() && LowerRow[IdxLower].Id == CurrentId) {
1017fa27ce4SDimitry Andric           LowerV = LowerRow[IdxLower].Coefficient;
1027fa27ce4SDimitry Andric           IdxLower++;
1037fa27ce4SDimitry Andric         }
1047fa27ce4SDimitry Andric 
105aca2e42cSDimitry Andric         if (MulOverflow(LowerV, UpperLast, M2))
106b60736ecSDimitry Andric           return false;
107b60736ecSDimitry Andric         if (AddOverflow(M1, M2, N))
108b60736ecSDimitry Andric           return false;
1097fa27ce4SDimitry Andric         if (N == 0)
1107fa27ce4SDimitry Andric           continue;
1117fa27ce4SDimitry Andric         NR.emplace_back(N, CurrentId);
112b60736ecSDimitry Andric       }
1137fa27ce4SDimitry Andric       if (NR.empty())
1147fa27ce4SDimitry Andric         continue;
1157fa27ce4SDimitry Andric       Constraints.push_back(std::move(NR));
116b60736ecSDimitry Andric       // Give up if the new system gets too big.
1177fa27ce4SDimitry Andric       if (Constraints.size() > 500)
118b60736ecSDimitry Andric         return false;
119b60736ecSDimitry Andric     }
120b60736ecSDimitry Andric   }
1217fa27ce4SDimitry Andric   NumVariables -= 1;
122b60736ecSDimitry Andric 
123b60736ecSDimitry Andric   return true;
124b60736ecSDimitry Andric }
125b60736ecSDimitry Andric 
mayHaveSolutionImpl()126b60736ecSDimitry Andric bool ConstraintSystem::mayHaveSolutionImpl() {
1277fa27ce4SDimitry Andric   while (!Constraints.empty() && NumVariables > 1) {
128b60736ecSDimitry Andric     if (!eliminateUsingFM())
129b60736ecSDimitry Andric       return true;
130b60736ecSDimitry Andric   }
131b60736ecSDimitry Andric 
1327fa27ce4SDimitry Andric   if (Constraints.empty() || NumVariables > 1)
133b60736ecSDimitry Andric     return true;
134b60736ecSDimitry Andric 
1357fa27ce4SDimitry Andric   return all_of(Constraints, [](auto &R) {
1367fa27ce4SDimitry Andric     if (R.empty())
1377fa27ce4SDimitry Andric       return true;
1387fa27ce4SDimitry Andric     if (R[0].Id == 0)
1397fa27ce4SDimitry Andric       return R[0].Coefficient >= 0;
1407fa27ce4SDimitry Andric     return true;
1417fa27ce4SDimitry Andric   });
142b60736ecSDimitry Andric }
143b60736ecSDimitry Andric 
getVarNamesList() const1447fa27ce4SDimitry Andric SmallVector<std::string> ConstraintSystem::getVarNamesList() const {
1457fa27ce4SDimitry Andric   SmallVector<std::string> Names(Value2Index.size(), "");
1467fa27ce4SDimitry Andric #ifndef NDEBUG
1477fa27ce4SDimitry Andric   for (auto &[V, Index] : Value2Index) {
1487fa27ce4SDimitry Andric     std::string OperandName;
1497fa27ce4SDimitry Andric     if (V->getName().empty())
1507fa27ce4SDimitry Andric       OperandName = V->getNameOrAsOperand();
1517fa27ce4SDimitry Andric     else
1527fa27ce4SDimitry Andric       OperandName = std::string("%") + V->getName().str();
1537fa27ce4SDimitry Andric     Names[Index - 1] = OperandName;
154b60736ecSDimitry Andric   }
1557fa27ce4SDimitry Andric #endif
1567fa27ce4SDimitry Andric   return Names;
157b60736ecSDimitry Andric }
158b60736ecSDimitry Andric 
dump() const159b60736ecSDimitry Andric void ConstraintSystem::dump() const {
1607fa27ce4SDimitry Andric #ifndef NDEBUG
1617fa27ce4SDimitry Andric   if (Constraints.empty())
1627fa27ce4SDimitry Andric     return;
1637fa27ce4SDimitry Andric   SmallVector<std::string> Names = getVarNamesList();
1647fa27ce4SDimitry Andric   for (const auto &Row : Constraints) {
1657fa27ce4SDimitry Andric     SmallVector<std::string, 16> Parts;
166ac9a064cSDimitry Andric     for (const Entry &E : Row) {
167ac9a064cSDimitry Andric       if (E.Id >= NumVariables)
1687fa27ce4SDimitry Andric         break;
169ac9a064cSDimitry Andric       if (E.Id == 0)
1707fa27ce4SDimitry Andric         continue;
1717fa27ce4SDimitry Andric       std::string Coefficient;
172ac9a064cSDimitry Andric       if (E.Coefficient != 1)
173ac9a064cSDimitry Andric         Coefficient = std::to_string(E.Coefficient) + " * ";
174ac9a064cSDimitry Andric       Parts.push_back(Coefficient + Names[E.Id - 1]);
1757fa27ce4SDimitry Andric     }
1767fa27ce4SDimitry Andric     // assert(!Parts.empty() && "need to have at least some parts");
1777fa27ce4SDimitry Andric     int64_t ConstPart = 0;
1787fa27ce4SDimitry Andric     if (Row[0].Id == 0)
1797fa27ce4SDimitry Andric       ConstPart = Row[0].Coefficient;
1807fa27ce4SDimitry Andric     LLVM_DEBUG(dbgs() << join(Parts, std::string(" + "))
1817fa27ce4SDimitry Andric                       << " <= " << std::to_string(ConstPart) << "\n");
1827fa27ce4SDimitry Andric   }
1837fa27ce4SDimitry Andric #endif
184b60736ecSDimitry Andric }
185b60736ecSDimitry Andric 
mayHaveSolution()186b60736ecSDimitry Andric bool ConstraintSystem::mayHaveSolution() {
1877fa27ce4SDimitry Andric   LLVM_DEBUG(dbgs() << "---\n");
188b60736ecSDimitry Andric   LLVM_DEBUG(dump());
189b60736ecSDimitry Andric   bool HasSolution = mayHaveSolutionImpl();
190b60736ecSDimitry Andric   LLVM_DEBUG(dbgs() << (HasSolution ? "sat" : "unsat") << "\n");
191b60736ecSDimitry Andric   return HasSolution;
192b60736ecSDimitry Andric }
193b60736ecSDimitry Andric 
isConditionImplied(SmallVector<int64_t,8> R) const1946f8fc217SDimitry Andric bool ConstraintSystem::isConditionImplied(SmallVector<int64_t, 8> R) const {
195b60736ecSDimitry Andric   // If all variable coefficients are 0, we have 'C >= 0'. If the constant is >=
196b60736ecSDimitry Andric   // 0, R is always true, regardless of the system.
197e3b55780SDimitry Andric   if (all_of(ArrayRef(R).drop_front(1), [](int64_t C) { return C == 0; }))
198b60736ecSDimitry Andric     return R[0] >= 0;
199b60736ecSDimitry Andric 
200b60736ecSDimitry Andric   // If there is no solution with the negation of R added to the system, the
201b60736ecSDimitry Andric   // condition must hold based on the existing constraints.
202b60736ecSDimitry Andric   R = ConstraintSystem::negate(R);
2037fa27ce4SDimitry Andric   if (R.empty())
2047fa27ce4SDimitry Andric     return false;
205b60736ecSDimitry Andric 
206b60736ecSDimitry Andric   auto NewSystem = *this;
207b60736ecSDimitry Andric   NewSystem.addVariableRow(R);
208b60736ecSDimitry Andric   return !NewSystem.mayHaveSolution();
209b60736ecSDimitry Andric }
210