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;
38 for (
unsigned R1 = 0; R1 < Constraints.size();) {
39 RowTy &Row1 = Constraints[R1];
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;
142 NR.emplace_back(
N, CurrentId);
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 > 0) {
159 if (!eliminateUsingFM())
163 assert((Constraints.empty() || NumVariables == 0) &&
164 "non-empty system must have all variables eliminated");
165 return all_of(Constraints,
170 SmallVector<std::string> Names(Value2Index.size(),
"");
172 for (
auto &[V, Index] : Value2Index) {
173 std::string OperandName;
174 if (
V->getName().empty())
175 OperandName =
V->getNameOrAsOperand();
177 OperandName = std::string(
"%") +
V->getName().str();
178 Names[
Index - 1] = OperandName;
186 if (Constraints.empty())
189 for (
const auto &Row : Constraints) {
191 for (
const Entry &E : Row) {
192 if (E.Id > NumVariables)
199 std::string Name = E.Id <= Names.
size() ? Names[E.Id - 1]
200 : (
"%v" + std::to_string(E.Id));
201 std::string Coefficient;
202 if (E.Coefficient != 1)
203 Coefficient = std::to_string(E.Coefficient) +
" * ";
215 bool HasSolution = mayHaveSolutionImpl();
220std::pair<ConstraintSystem, ConstraintSystem::RowTy>
229 for (
const Entry &E : R)
231 InSystem[E.Id] =
true;
234 return E.Id != 0 && InSystem[E.Id];
240 for (
const RowTy &Row : Constraints) {
242 if (!SharesVariable(Row))
244 for (
const Entry &E : Row)
245 if (E.Id != 0 && !InSystem[E.Id]) {
246 InSystem[E.Id] =
true;
254 unsigned NextIdx = 1;
255 for (
unsigned Id : InSystem.
set_bits())
256 OldToNew[Id] = NextIdx++;
259 SubSystem.NumVariables = NextIdx - 1;
260 for (
const RowTy &Row : Constraints) {
261 if (!SharesVariable(Row))
264 for (
const Entry &E : Row) {
265 unsigned New = OldToNew[E.Id];
266 assert((E.Id == 0) == (New == 0) &&
"constant entry must be preserved");
269 SubSystem.Constraints.push_back(std::move(NewRow));
274 for (
const Entry &E : R)
277 return {std::move(SubSystem), std::move(NewR)};
283 if (isConstantOnly(R))
284 return getConstant(R) >= 0;
293 Copy.addRow(R, NumVariables);
294 return !Copy.mayHaveSolution();
303 if (isConstantOnly(R))
304 return getConstant(R) >= 0;
308 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),...
LLVM_ABI bool isConditionImplied(RowTy R) const
LLVM_ABI bool mayHaveSolution()
Returns true if there may be a solution for the constraints in the system.
static RowTy negate(RowTy R)
LLVM_ABI std::pair< ConstraintSystem, RowTy > getSubSystem(ArrayRef< Entry > R) const
Build and return a sub-system of constraints connected (transitively) to query R, with variables comp...
LLVM_ABI bool isConditionImpliedInSubSystem(ArrayRef< Entry > R) const
ConstraintSystem()=default
SmallVector< Entry, 8 > RowTy
A single constraint of the form 'c >= v1 * c1 + ... + vn * cn'.
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
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,...
unsigned M1(unsigned Val)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
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.