1*af732203SDimitry Andric //===- ConstraintSytem.cpp - A system of linear constraints. ----*- C++ -*-===//
2*af732203SDimitry Andric //
3*af732203SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*af732203SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*af732203SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*af732203SDimitry Andric //
7*af732203SDimitry Andric //===----------------------------------------------------------------------===//
8*af732203SDimitry Andric 
9*af732203SDimitry Andric #include "llvm/Analysis/ConstraintSystem.h"
10*af732203SDimitry Andric #include "llvm/ADT/SmallVector.h"
11*af732203SDimitry Andric #include "llvm/Support/MathExtras.h"
12*af732203SDimitry Andric #include "llvm/ADT/StringExtras.h"
13*af732203SDimitry Andric #include "llvm/Support/Debug.h"
14*af732203SDimitry Andric 
15*af732203SDimitry Andric #include <algorithm>
16*af732203SDimitry Andric #include <string>
17*af732203SDimitry Andric 
18*af732203SDimitry Andric using namespace llvm;
19*af732203SDimitry Andric 
20*af732203SDimitry Andric #define DEBUG_TYPE "constraint-system"
21*af732203SDimitry Andric 
eliminateUsingFM()22*af732203SDimitry Andric bool ConstraintSystem::eliminateUsingFM() {
23*af732203SDimitry Andric   // Implementation of Fourier–Motzkin elimination, with some tricks from the
24*af732203SDimitry Andric   // paper Pugh, William. "The Omega test: a fast and practical integer
25*af732203SDimitry Andric   // programming algorithm for dependence
26*af732203SDimitry Andric   //  analysis."
27*af732203SDimitry Andric   // Supercomputing'91: Proceedings of the 1991 ACM/
28*af732203SDimitry Andric   // IEEE conference on Supercomputing. IEEE, 1991.
29*af732203SDimitry Andric   assert(!Constraints.empty() &&
30*af732203SDimitry Andric          "should only be called for non-empty constraint systems");
31*af732203SDimitry Andric   unsigned NumVariables = Constraints[0].size();
32*af732203SDimitry Andric   SmallVector<SmallVector<int64_t, 8>, 4> NewSystem;
33*af732203SDimitry Andric 
34*af732203SDimitry Andric   unsigned NumConstraints = Constraints.size();
35*af732203SDimitry Andric   uint32_t NewGCD = 1;
36*af732203SDimitry Andric   // FIXME do not use copy
37*af732203SDimitry Andric   for (unsigned R1 = 0; R1 < NumConstraints; R1++) {
38*af732203SDimitry Andric     if (Constraints[R1][1] == 0) {
39*af732203SDimitry Andric       SmallVector<int64_t, 8> NR;
40*af732203SDimitry Andric       NR.push_back(Constraints[R1][0]);
41*af732203SDimitry Andric       for (unsigned i = 2; i < NumVariables; i++) {
42*af732203SDimitry Andric         NR.push_back(Constraints[R1][i]);
43*af732203SDimitry Andric       }
44*af732203SDimitry Andric       NewSystem.push_back(std::move(NR));
45*af732203SDimitry Andric       continue;
46*af732203SDimitry Andric     }
47*af732203SDimitry Andric 
48*af732203SDimitry Andric     // FIXME do not use copy
49*af732203SDimitry Andric     for (unsigned R2 = R1 + 1; R2 < NumConstraints; R2++) {
50*af732203SDimitry Andric       if (R1 == R2)
51*af732203SDimitry Andric         continue;
52*af732203SDimitry Andric 
53*af732203SDimitry Andric       // FIXME: can we do better than just dropping things here?
54*af732203SDimitry Andric       if (Constraints[R2][1] == 0)
55*af732203SDimitry Andric         continue;
56*af732203SDimitry Andric 
57*af732203SDimitry Andric       if ((Constraints[R1][1] < 0 && Constraints[R2][1] < 0) ||
58*af732203SDimitry Andric           (Constraints[R1][1] > 0 && Constraints[R2][1] > 0))
59*af732203SDimitry Andric         continue;
60*af732203SDimitry Andric 
61*af732203SDimitry Andric       unsigned LowerR = R1;
62*af732203SDimitry Andric       unsigned UpperR = R2;
63*af732203SDimitry Andric       if (Constraints[UpperR][1] < 0)
64*af732203SDimitry Andric         std::swap(LowerR, UpperR);
65*af732203SDimitry Andric 
66*af732203SDimitry Andric       SmallVector<int64_t, 8> NR;
67*af732203SDimitry Andric       for (unsigned I = 0; I < NumVariables; I++) {
68*af732203SDimitry Andric         if (I == 1)
69*af732203SDimitry Andric           continue;
70*af732203SDimitry Andric 
71*af732203SDimitry Andric         int64_t M1, M2, N;
72*af732203SDimitry Andric         if (MulOverflow(Constraints[UpperR][I],
73*af732203SDimitry Andric                                    ((-1) * Constraints[LowerR][1] / GCD), M1))
74*af732203SDimitry Andric           return false;
75*af732203SDimitry Andric         if (MulOverflow(Constraints[LowerR][I],
76*af732203SDimitry Andric                                    (Constraints[UpperR][1] / GCD), M2))
77*af732203SDimitry Andric           return false;
78*af732203SDimitry Andric         if (AddOverflow(M1, M2, N))
79*af732203SDimitry Andric           return false;
80*af732203SDimitry Andric         NR.push_back(N);
81*af732203SDimitry Andric 
82*af732203SDimitry Andric         NewGCD = APIntOps::GreatestCommonDivisor({32, (uint32_t)NR.back()},
83*af732203SDimitry Andric                                                  {32, NewGCD})
84*af732203SDimitry Andric                      .getZExtValue();
85*af732203SDimitry Andric       }
86*af732203SDimitry Andric       NewSystem.push_back(std::move(NR));
87*af732203SDimitry Andric       // Give up if the new system gets too big.
88*af732203SDimitry Andric       if (NewSystem.size() > 500)
89*af732203SDimitry Andric         return false;
90*af732203SDimitry Andric     }
91*af732203SDimitry Andric   }
92*af732203SDimitry Andric   Constraints = std::move(NewSystem);
93*af732203SDimitry Andric   GCD = NewGCD;
94*af732203SDimitry Andric 
95*af732203SDimitry Andric   return true;
96*af732203SDimitry Andric }
97*af732203SDimitry Andric 
mayHaveSolutionImpl()98*af732203SDimitry Andric bool ConstraintSystem::mayHaveSolutionImpl() {
99*af732203SDimitry Andric   while (!Constraints.empty() && Constraints[0].size() > 1) {
100*af732203SDimitry Andric     if (!eliminateUsingFM())
101*af732203SDimitry Andric       return true;
102*af732203SDimitry Andric   }
103*af732203SDimitry Andric 
104*af732203SDimitry Andric   if (Constraints.empty() || Constraints[0].size() > 1)
105*af732203SDimitry Andric     return true;
106*af732203SDimitry Andric 
107*af732203SDimitry Andric   return all_of(Constraints, [](auto &R) { return R[0] >= 0; });
108*af732203SDimitry Andric }
109*af732203SDimitry Andric 
dump(ArrayRef<std::string> Names) const110*af732203SDimitry Andric void ConstraintSystem::dump(ArrayRef<std::string> Names) const {
111*af732203SDimitry Andric   if (Constraints.empty())
112*af732203SDimitry Andric     return;
113*af732203SDimitry Andric 
114*af732203SDimitry Andric   for (auto &Row : Constraints) {
115*af732203SDimitry Andric     SmallVector<std::string, 16> Parts;
116*af732203SDimitry Andric     for (unsigned I = 1, S = Row.size(); I < S; ++I) {
117*af732203SDimitry Andric       if (Row[I] == 0)
118*af732203SDimitry Andric         continue;
119*af732203SDimitry Andric       std::string Coefficient;
120*af732203SDimitry Andric       if (Row[I] != 1)
121*af732203SDimitry Andric         Coefficient = std::to_string(Row[I]) + " * ";
122*af732203SDimitry Andric       Parts.push_back(Coefficient + Names[I - 1]);
123*af732203SDimitry Andric     }
124*af732203SDimitry Andric     assert(!Parts.empty() && "need to have at least some parts");
125*af732203SDimitry Andric     LLVM_DEBUG(dbgs() << join(Parts, std::string(" + "))
126*af732203SDimitry Andric                       << " <= " << std::to_string(Row[0]) << "\n");
127*af732203SDimitry Andric   }
128*af732203SDimitry Andric }
129*af732203SDimitry Andric 
dump() const130*af732203SDimitry Andric void ConstraintSystem::dump() const {
131*af732203SDimitry Andric   SmallVector<std::string, 16> Names;
132*af732203SDimitry Andric   for (unsigned i = 1; i < Constraints.back().size(); ++i)
133*af732203SDimitry Andric     Names.push_back("x" + std::to_string(i));
134*af732203SDimitry Andric   LLVM_DEBUG(dbgs() << "---\n");
135*af732203SDimitry Andric   dump(Names);
136*af732203SDimitry Andric }
137*af732203SDimitry Andric 
mayHaveSolution()138*af732203SDimitry Andric bool ConstraintSystem::mayHaveSolution() {
139*af732203SDimitry Andric   LLVM_DEBUG(dump());
140*af732203SDimitry Andric   bool HasSolution = mayHaveSolutionImpl();
141*af732203SDimitry Andric   LLVM_DEBUG(dbgs() << (HasSolution ? "sat" : "unsat") << "\n");
142*af732203SDimitry Andric   return HasSolution;
143*af732203SDimitry Andric }
144*af732203SDimitry Andric 
isConditionImplied(SmallVector<int64_t,8> R)145*af732203SDimitry Andric bool ConstraintSystem::isConditionImplied(SmallVector<int64_t, 8> R) {
146*af732203SDimitry Andric   // If all variable coefficients are 0, we have 'C >= 0'. If the constant is >=
147*af732203SDimitry Andric   // 0, R is always true, regardless of the system.
148*af732203SDimitry Andric   if (all_of(makeArrayRef(R).drop_front(1), [](int64_t C) { return C == 0; }))
149*af732203SDimitry Andric     return R[0] >= 0;
150*af732203SDimitry Andric 
151*af732203SDimitry Andric   // If there is no solution with the negation of R added to the system, the
152*af732203SDimitry Andric   // condition must hold based on the existing constraints.
153*af732203SDimitry Andric   R = ConstraintSystem::negate(R);
154*af732203SDimitry Andric 
155*af732203SDimitry Andric   auto NewSystem = *this;
156*af732203SDimitry Andric   NewSystem.addVariableRow(R);
157*af732203SDimitry Andric   return !NewSystem.mayHaveSolution();
158*af732203SDimitry Andric }
159