20#define DEBUG_TYPE "constraint-system"
22bool ConstraintSystem::eliminateUsingFM() {
29 assert(!Constraints.empty() &&
30 "should only be called for non-empty constraint systems");
33 unsigned LastIdx = NumVariables - 1;
38 for (
unsigned R1 = 0; R1 < Constraints.size();) {
40 if (getLastCoefficient(Row1, LastIdx) == 0) {
41 if (Row1.
size() > 0 && Row1.
back().Id == LastIdx)
45 std::swap(Constraints[R1], Constraints.back());
46 RemainingRows.
push_back(std::move(Constraints.back()));
47 Constraints.pop_back();
52 unsigned NumRemainingConstraints = RemainingRows.
size();
53 for (
unsigned R1 = 0; R1 < NumRemainingConstraints; R1++) {
55 for (
unsigned R2 = R1 + 1;
R2 < NumRemainingConstraints;
R2++) {
59 int64_t UpperLast = getLastCoefficient(RemainingRows[
R2], LastIdx);
60 int64_t LowerLast = getLastCoefficient(RemainingRows[R1], LastIdx);
62 UpperLast != 0 && LowerLast != 0 &&
63 "RemainingRows should only contain rows where the variable is != 0");
65 if ((LowerLast < 0 && UpperLast < 0) || (LowerLast > 0 && UpperLast > 0))
76 unsigned IdxUpper = 0;
77 unsigned IdxLower = 0;
78 auto &LowerRow = RemainingRows[LowerR];
79 auto &UpperRow = RemainingRows[UpperR];
81 if (IdxUpper >= UpperRow.size() || IdxLower >= LowerRow.size())
86 uint16_t CurrentId = std::numeric_limits<uint16_t>::max();
87 if (IdxUpper < UpperRow.size()) {
88 CurrentId = std::min(UpperRow[IdxUpper].Id, CurrentId);
90 if (IdxLower < LowerRow.size()) {
91 CurrentId = std::min(LowerRow[IdxLower].Id, CurrentId);
94 if (IdxUpper < UpperRow.size() && UpperRow[IdxUpper].Id == CurrentId) {
95 UpperV = UpperRow[IdxUpper].Coefficient;
101 if (IdxLower < LowerRow.size() && LowerRow[IdxLower].Id == CurrentId) {
102 LowerV = LowerRow[IdxLower].Coefficient;
120 Constraints.push_back(std::move(NR));
122 if (Constraints.size() > 500)
132bool ConstraintSystem::mayHaveSolutionImpl() {
133 while (!Constraints.empty() && NumVariables > 1) {
134 if (!eliminateUsingFM())
138 if (Constraints.empty() || NumVariables > 1)
141 return all_of(Constraints, [](
auto &R) {
145 return R[0].Coefficient >= 0;
153 for (
auto &[V,
Index] : Value2Index) {
154 std::string OperandName;
155 if (
V->getName().empty())
156 OperandName =
V->getNameOrAsOperand();
158 OperandName = std::string(
"%") +
V->getName().str();
159 Names[
Index - 1] = OperandName;
167 if (Constraints.empty())
170 for (
const auto &Row : Constraints) {
172 for (
unsigned I = 0, S = Row.size();
I < S; ++
I) {
173 if (Row[
I].Id >= NumVariables)
177 std::string Coefficient;
178 if (Row[
I].Coefficient != 1)
179 Coefficient = std::to_string(Row[
I].Coefficient) +
" * ";
180 Parts.
push_back(Coefficient + Names[Row[
I].Id - 1]);
183 int64_t ConstPart = 0;
185 ConstPart = Row[0].Coefficient;
187 <<
" <= " << std::to_string(ConstPart) <<
"\n");
195 bool HasSolution = mayHaveSolutionImpl();
212 auto NewSystem = *
this;
213 NewSystem.addVariableRow(R);
214 return !NewSystem.mayHaveSolution();
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
static SmallVector< int64_t, 8 > negate(SmallVector< int64_t, 8 > R)
bool mayHaveSolution()
Returns true if there may be a solution for the constraints in the system.
bool isConditionImplied(SmallVector< int64_t, 8 > R) const
void dump() const
Print the constraints in the system.
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
APInt GreatestCommonDivisor(APInt A, APInt B)
Compute GCD of two unsigned APInt values.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
std::enable_if_t< std::is_signed_v< T >, T > MulOverflow(T X, T Y, T &Result)
Multiply two signed integers, computing the two's complement truncated result, returning true if an o...
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
unsigned M1(unsigned Val)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
std::enable_if_t< std::is_signed_v< T >, T > AddOverflow(T X, T Y, T &Result)
Add two signed integers, computing the two's complement truncated result, returning true if overflow ...
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.