/src/llvm-project/llvm/lib/Analysis/ConstraintSystem.cpp
Line | Count | Source (jump to first uncovered line) |
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/IR/Value.h" |
14 | | #include "llvm/Support/Debug.h" |
15 | | |
16 | | #include <string> |
17 | | |
18 | | using namespace llvm; |
19 | | |
20 | | #define DEBUG_TYPE "constraint-system" |
21 | | |
22 | 0 | bool ConstraintSystem::eliminateUsingFM() { |
23 | | // Implementation of Fourier–Motzkin elimination, with some tricks from the |
24 | | // paper Pugh, William. "The Omega test: a fast and practical integer |
25 | | // programming algorithm for dependence |
26 | | // analysis." |
27 | | // Supercomputing'91: Proceedings of the 1991 ACM/ |
28 | | // IEEE conference on Supercomputing. IEEE, 1991. |
29 | 0 | assert(!Constraints.empty() && |
30 | 0 | "should only be called for non-empty constraint systems"); |
31 | | |
32 | 0 | unsigned LastIdx = NumVariables - 1; |
33 | | |
34 | | // First, either remove the variable in place if it is 0 or add the row to |
35 | | // RemainingRows and remove it from the system. |
36 | 0 | SmallVector<SmallVector<Entry, 8>, 4> RemainingRows; |
37 | 0 | for (unsigned R1 = 0; R1 < Constraints.size();) { |
38 | 0 | SmallVector<Entry, 8> &Row1 = Constraints[R1]; |
39 | 0 | if (getLastCoefficient(Row1, LastIdx) == 0) { |
40 | 0 | if (Row1.size() > 0 && Row1.back().Id == LastIdx) |
41 | 0 | Row1.pop_back(); |
42 | 0 | R1++; |
43 | 0 | } else { |
44 | 0 | std::swap(Constraints[R1], Constraints.back()); |
45 | 0 | RemainingRows.push_back(std::move(Constraints.back())); |
46 | 0 | Constraints.pop_back(); |
47 | 0 | } |
48 | 0 | } |
49 | | |
50 | | // Process rows where the variable is != 0. |
51 | 0 | unsigned NumRemainingConstraints = RemainingRows.size(); |
52 | 0 | for (unsigned R1 = 0; R1 < NumRemainingConstraints; R1++) { |
53 | | // FIXME do not use copy |
54 | 0 | for (unsigned R2 = R1 + 1; R2 < NumRemainingConstraints; R2++) { |
55 | 0 | if (R1 == R2) |
56 | 0 | continue; |
57 | | |
58 | 0 | int64_t UpperLast = getLastCoefficient(RemainingRows[R2], LastIdx); |
59 | 0 | int64_t LowerLast = getLastCoefficient(RemainingRows[R1], LastIdx); |
60 | 0 | assert( |
61 | 0 | UpperLast != 0 && LowerLast != 0 && |
62 | 0 | "RemainingRows should only contain rows where the variable is != 0"); |
63 | | |
64 | 0 | if ((LowerLast < 0 && UpperLast < 0) || (LowerLast > 0 && UpperLast > 0)) |
65 | 0 | continue; |
66 | | |
67 | 0 | unsigned LowerR = R1; |
68 | 0 | unsigned UpperR = R2; |
69 | 0 | if (UpperLast < 0) { |
70 | 0 | std::swap(LowerR, UpperR); |
71 | 0 | std::swap(LowerLast, UpperLast); |
72 | 0 | } |
73 | |
|
74 | 0 | SmallVector<Entry, 8> NR; |
75 | 0 | unsigned IdxUpper = 0; |
76 | 0 | unsigned IdxLower = 0; |
77 | 0 | auto &LowerRow = RemainingRows[LowerR]; |
78 | 0 | auto &UpperRow = RemainingRows[UpperR]; |
79 | 0 | while (true) { |
80 | 0 | if (IdxUpper >= UpperRow.size() || IdxLower >= LowerRow.size()) |
81 | 0 | break; |
82 | 0 | int64_t M1, M2, N; |
83 | 0 | int64_t UpperV = 0; |
84 | 0 | int64_t LowerV = 0; |
85 | 0 | uint16_t CurrentId = std::numeric_limits<uint16_t>::max(); |
86 | 0 | if (IdxUpper < UpperRow.size()) { |
87 | 0 | CurrentId = std::min(UpperRow[IdxUpper].Id, CurrentId); |
88 | 0 | } |
89 | 0 | if (IdxLower < LowerRow.size()) { |
90 | 0 | CurrentId = std::min(LowerRow[IdxLower].Id, CurrentId); |
91 | 0 | } |
92 | |
|
93 | 0 | if (IdxUpper < UpperRow.size() && UpperRow[IdxUpper].Id == CurrentId) { |
94 | 0 | UpperV = UpperRow[IdxUpper].Coefficient; |
95 | 0 | IdxUpper++; |
96 | 0 | } |
97 | |
|
98 | 0 | if (MulOverflow(UpperV, -1 * LowerLast, M1)) |
99 | 0 | return false; |
100 | 0 | if (IdxLower < LowerRow.size() && LowerRow[IdxLower].Id == CurrentId) { |
101 | 0 | LowerV = LowerRow[IdxLower].Coefficient; |
102 | 0 | IdxLower++; |
103 | 0 | } |
104 | |
|
105 | 0 | if (MulOverflow(LowerV, UpperLast, M2)) |
106 | 0 | return false; |
107 | 0 | if (AddOverflow(M1, M2, N)) |
108 | 0 | return false; |
109 | 0 | if (N == 0) |
110 | 0 | continue; |
111 | 0 | NR.emplace_back(N, CurrentId); |
112 | 0 | } |
113 | 0 | if (NR.empty()) |
114 | 0 | continue; |
115 | 0 | Constraints.push_back(std::move(NR)); |
116 | | // Give up if the new system gets too big. |
117 | 0 | if (Constraints.size() > 500) |
118 | 0 | return false; |
119 | 0 | } |
120 | 0 | } |
121 | 0 | NumVariables -= 1; |
122 | |
|
123 | 0 | return true; |
124 | 0 | } |
125 | | |
126 | 0 | bool ConstraintSystem::mayHaveSolutionImpl() { |
127 | 0 | while (!Constraints.empty() && NumVariables > 1) { |
128 | 0 | if (!eliminateUsingFM()) |
129 | 0 | return true; |
130 | 0 | } |
131 | | |
132 | 0 | if (Constraints.empty() || NumVariables > 1) |
133 | 0 | return true; |
134 | | |
135 | 0 | return all_of(Constraints, [](auto &R) { |
136 | 0 | if (R.empty()) |
137 | 0 | return true; |
138 | 0 | if (R[0].Id == 0) |
139 | 0 | return R[0].Coefficient >= 0; |
140 | 0 | return true; |
141 | 0 | }); |
142 | 0 | } |
143 | | |
144 | 0 | SmallVector<std::string> ConstraintSystem::getVarNamesList() const { |
145 | 0 | SmallVector<std::string> Names(Value2Index.size(), ""); |
146 | 0 | #ifndef NDEBUG |
147 | 0 | for (auto &[V, Index] : Value2Index) { |
148 | 0 | std::string OperandName; |
149 | 0 | if (V->getName().empty()) |
150 | 0 | OperandName = V->getNameOrAsOperand(); |
151 | 0 | else |
152 | 0 | OperandName = std::string("%") + V->getName().str(); |
153 | 0 | Names[Index - 1] = OperandName; |
154 | 0 | } |
155 | 0 | #endif |
156 | 0 | return Names; |
157 | 0 | } |
158 | | |
159 | 0 | void ConstraintSystem::dump() const { |
160 | 0 | #ifndef NDEBUG |
161 | 0 | if (Constraints.empty()) |
162 | 0 | return; |
163 | 0 | SmallVector<std::string> Names = getVarNamesList(); |
164 | 0 | for (const auto &Row : Constraints) { |
165 | 0 | SmallVector<std::string, 16> Parts; |
166 | 0 | for (unsigned I = 0, S = Row.size(); I < S; ++I) { |
167 | 0 | if (Row[I].Id >= NumVariables) |
168 | 0 | break; |
169 | 0 | if (Row[I].Id == 0) |
170 | 0 | continue; |
171 | 0 | std::string Coefficient; |
172 | 0 | if (Row[I].Coefficient != 1) |
173 | 0 | Coefficient = std::to_string(Row[I].Coefficient) + " * "; |
174 | 0 | Parts.push_back(Coefficient + Names[Row[I].Id - 1]); |
175 | 0 | } |
176 | | // assert(!Parts.empty() && "need to have at least some parts"); |
177 | 0 | int64_t ConstPart = 0; |
178 | 0 | if (Row[0].Id == 0) |
179 | 0 | ConstPart = Row[0].Coefficient; |
180 | 0 | LLVM_DEBUG(dbgs() << join(Parts, std::string(" + ")) |
181 | 0 | << " <= " << std::to_string(ConstPart) << "\n"); |
182 | 0 | } |
183 | 0 | #endif |
184 | 0 | } |
185 | | |
186 | 0 | bool ConstraintSystem::mayHaveSolution() { |
187 | 0 | LLVM_DEBUG(dbgs() << "---\n"); |
188 | 0 | LLVM_DEBUG(dump()); |
189 | 0 | bool HasSolution = mayHaveSolutionImpl(); |
190 | 0 | LLVM_DEBUG(dbgs() << (HasSolution ? "sat" : "unsat") << "\n"); |
191 | 0 | return HasSolution; |
192 | 0 | } |
193 | | |
194 | 0 | bool ConstraintSystem::isConditionImplied(SmallVector<int64_t, 8> R) const { |
195 | | // If all variable coefficients are 0, we have 'C >= 0'. If the constant is >= |
196 | | // 0, R is always true, regardless of the system. |
197 | 0 | if (all_of(ArrayRef(R).drop_front(1), [](int64_t C) { return C == 0; })) |
198 | 0 | return R[0] >= 0; |
199 | | |
200 | | // If there is no solution with the negation of R added to the system, the |
201 | | // condition must hold based on the existing constraints. |
202 | 0 | R = ConstraintSystem::negate(R); |
203 | 0 | if (R.empty()) |
204 | 0 | return false; |
205 | | |
206 | 0 | auto NewSystem = *this; |
207 | 0 | NewSystem.addVariableRow(R); |
208 | 0 | return !NewSystem.mayHaveSolution(); |
209 | 0 | } |