21#define DEBUG_TYPE "constraint-system"
23bool ConstraintSystem::eliminateUsingFM() {
30 assert(!Constraints.empty() &&
31 "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++) {
62 int64_t UpperLast = getLastCoefficient(RemainingRows[
R2], LastIdx);
63 int64_t LowerLast = getLastCoefficient(RemainingRows[R1], LastIdx);
65 UpperLast != 0 && LowerLast != 0 &&
66 "RemainingRows should only contain rows where the variable is != 0");
68 if ((LowerLast < 0 && UpperLast < 0) || (LowerLast > 0 && UpperLast > 0))
79 unsigned IdxUpper = 0;
80 unsigned IdxLower = 0;
81 auto &LowerRow = RemainingRows[LowerR];
82 auto &UpperRow = RemainingRows[UpperR];
85 bool Overflow =
false;
89 if (IdxUpper >= UpperRow.size() || IdxLower >= LowerRow.size())
95 uint16_t CurrentId = std::numeric_limits<uint16_t>::max();
96 if (IdxUpper < UpperRow.size()) {
97 CurrentId = std::min(UpperRow[IdxUpper].Id, CurrentId);
99 if (IdxLower < LowerRow.size()) {
100 CurrentId = std::min(LowerRow[IdxLower].Id, CurrentId);
103 if (IdxUpper < UpperRow.size() && UpperRow[IdxUpper].Id == CurrentId) {
104 UpperV = UpperRow[IdxUpper].Coefficient;
112 if (IdxLower < LowerRow.size() && LowerRow[IdxLower].Id == CurrentId) {
113 LowerV = LowerRow[IdxLower].Coefficient;
144 if (Overflow || NR.
empty())
146 Constraints.push_back(std::move(NR));
148 if (Constraints.size() > 500)
157bool ConstraintSystem::mayHaveSolutionImpl() {
158 while (!Constraints.empty() && NumVariables > 1) {
159 if (!eliminateUsingFM())
163 if (Constraints.empty() || NumVariables > 1)
166 return all_of(Constraints, [](
auto &R) {
170 return R[0].Coefficient >= 0;
176 SmallVector<std::string> Names(Value2Index.size(),
"");
178 for (
auto &[V, Index] : Value2Index) {
179 std::string OperandName;
180 if (
V->getName().empty())
181 OperandName =
V->getNameOrAsOperand();
183 OperandName = std::string(
"%") +
V->getName().str();
184 Names[
Index - 1] = OperandName;
192 if (Constraints.empty())
195 for (
const auto &Row : Constraints) {
197 for (
const Entry &E : Row) {
198 if (E.Id >= NumVariables)
205 std::string Name = E.Id <= Names.
size() ? Names[E.Id - 1]
206 : (
"%v" + std::to_string(E.Id));
207 std::string Coefficient;
208 if (E.Coefficient != 1)
209 Coefficient = std::to_string(E.Coefficient) +
" * ";
213 int64_t ConstPart = 0;
215 ConstPart = Row[0].Coefficient;
217 <<
" <= " << std::to_string(ConstPart) <<
"\n");
225 bool HasSolution = mayHaveSolutionImpl();
230std::pair<ConstraintSystem, SmallVector<int64_t, 8>>
239 for (
unsigned Id = 1, E = R.size(); Id < E; ++Id)
245 for (
const auto &Row : Constraints) {
248 [&](
const Entry &E) {
return E.Id != 0 && InSystem[E.Id]; }))
250 for (
const Entry &E : Row)
251 if (E.Id != 0 && !InSystem[E.Id]) {
252 InSystem[E.Id] =
true;
260 OldToNew.
assign(NumVariables + 1, 0);
261 unsigned NextIdx = 1;
262 for (
unsigned Id : InSystem.
set_bits())
263 OldToNew[Id] = NextIdx++;
266 SubSystem.NumVariables = NextIdx;
267 for (
const auto &Row : Constraints) {
269 [&](
const Entry &E) {
return E.Id != 0 && InSystem[E.Id]; }))
272 for (
const Entry &E : Row) {
275 else if (
unsigned New = OldToNew[E.Id])
278 SubSystem.Constraints.push_back(std::move(NewRow));
284 for (
unsigned Id = 1, E = R.size(); Id < E; ++Id)
286 NewR[OldToNew[Id]] = R[Id];
287 return {std::move(SubSystem), std::move(NewR)};
303 Copy.addVariableRow(R);
304 return !Copy.mayHaveSolution();
319 return SubCS.isConditionImplied(NewR);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements the SmallBitVector class.
This file defines the SmallVector class.
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)
LLVM_ABI bool mayHaveSolution()
Returns true if there may be a solution for the constraints in the system.
LLVM_ABI std::pair< ConstraintSystem, SmallVector< int64_t, 8 > > getSubSystem(ArrayRef< int64_t > R) const
Build and return a sub-system of constraints connected (transitively) to query R, with variables comp...
LLVM_ABI bool isConditionImplied(SmallVector< int64_t, 8 > R) const
LLVM_ABI bool isConditionImpliedInSubSystem(SmallVector< int64_t, 8 > R) const
ConstraintSystem()=default
LLVM_ABI void dump() const
Print the constraints in the system.
This is a 'bitvector' (really, a variable-sized bit array), optimized for the case when the array is ...
iterator_range< const_set_bits_iterator > set_bits() const
void assign(size_type NumElts, ValueParamT Elt)
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.
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
constexpr std::enable_if_t< std::is_signed_v< T >, std::pair< T, bool > > AddOverflow(T X, T Y)
Add two signed integers, computing the two's complement truncated result, returning a pair {result,...
constexpr auto equal_to(T &&Arg)
Functor variant of std::equal_to that can be used as a UnaryPredicate in functional algorithms like a...
unsigned M1(unsigned Val)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
std::string join(IteratorT Begin, IteratorT End, StringRef Separator)
Joins the strings in the range [Begin, End), adding Separator between the elements.
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr std::enable_if_t< std::is_signed_v< T >, std::pair< T, bool > > MulOverflow(T X, T Y)
Multiply two signed integers, computing the two's complement truncated result, returning a pair {resu...
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.