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