20#define DEBUG_TYPE "constraint-system"
22bool ConstraintSystem::eliminateUsingFM() {
29 assert(!Constraints.empty() &&
30 "should only be called for non-empty constraint systems");
32 unsigned LastIdx = NumVariables - 1;
37 for (
unsigned R1 = 0; R1 < Constraints.size();) {
39 if (getLastCoefficient(Row1, LastIdx) == 0) {
40 if (Row1.
size() > 0 && Row1.
back().Id == LastIdx)
44 std::swap(Constraints[R1], Constraints.back());
45 RemainingRows.
push_back(std::move(Constraints.back()));
46 Constraints.pop_back();
51 unsigned NumRemainingConstraints = RemainingRows.
size();
52 for (
unsigned R1 = 0; R1 < NumRemainingConstraints; R1++) {
54 for (
unsigned R2 = R1 + 1;
R2 < NumRemainingConstraints;
R2++) {
58 int64_t UpperLast = getLastCoefficient(RemainingRows[
R2], LastIdx);
59 int64_t LowerLast = getLastCoefficient(RemainingRows[R1], LastIdx);
61 UpperLast != 0 && LowerLast != 0 &&
62 "RemainingRows should only contain rows where the variable is != 0");
64 if ((LowerLast < 0 && UpperLast < 0) || (LowerLast > 0 && UpperLast > 0))
75 unsigned IdxUpper = 0;
76 unsigned IdxLower = 0;
77 auto &LowerRow = RemainingRows[LowerR];
78 auto &UpperRow = RemainingRows[UpperR];
80 if (IdxUpper >= UpperRow.size() || IdxLower >= LowerRow.size())
85 uint16_t CurrentId = std::numeric_limits<uint16_t>::max();
86 if (IdxUpper < UpperRow.size()) {
87 CurrentId = std::min(UpperRow[IdxUpper].Id, CurrentId);
89 if (IdxLower < LowerRow.size()) {
90 CurrentId = std::min(LowerRow[IdxLower].Id, CurrentId);
93 if (IdxUpper < UpperRow.size() && UpperRow[IdxUpper].Id == CurrentId) {
94 UpperV = UpperRow[IdxUpper].Coefficient;
100 if (IdxLower < LowerRow.size() && LowerRow[IdxLower].Id == CurrentId) {
101 LowerV = LowerRow[IdxLower].Coefficient;
115 Constraints.push_back(std::move(NR));
117 if (Constraints.size() > 500)
126bool ConstraintSystem::mayHaveSolutionImpl() {
127 while (!Constraints.empty() && NumVariables > 1) {
128 if (!eliminateUsingFM())
132 if (Constraints.empty() || NumVariables > 1)
135 return all_of(Constraints, [](
auto &R) {
139 return R[0].Coefficient >= 0;
147 for (
auto &[V, Index] : Value2Index) {
148 std::string OperandName;
149 if (
V->getName().empty())
150 OperandName =
V->getNameOrAsOperand();
152 OperandName = std::string(
"%") +
V->getName().str();
153 Names[
Index - 1] = OperandName;
161 if (Constraints.empty())
164 for (
const auto &Row : Constraints) {
166 for (
const Entry &E : Row) {
167 if (E.Id >= NumVariables)
171 std::string Coefficient;
172 if (E.Coefficient != 1)
173 Coefficient = std::to_string(E.Coefficient) +
" * ";
174 Parts.
push_back(Coefficient + Names[E.Id - 1]);
177 int64_t ConstPart = 0;
179 ConstPart = Row[0].Coefficient;
181 <<
" <= " << std::to_string(ConstPart) <<
"\n");
189 bool HasSolution = mayHaveSolutionImpl();
206 auto NewSystem = *
this;
207 NewSystem.addVariableRow(R);
208 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.
@ 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.