22#define DEBUG_TYPE "constraint-system"
24bool ConstraintSystem::eliminateUsingFM() {
31 assert(!Constraints.empty() &&
32 "should only be called for non-empty constraint systems");
34 unsigned LastIdx = NumVariables;
39 for (
unsigned R1 = 0; R1 < Constraints.size();) {
40 RowTy &Row1 = Constraints[R1];
41 if (getLastCoefficient(Row1, LastIdx) == 0) {
42 if (Row1.size() > 0 && Row1.back().Id == LastIdx)
46 std::swap(Constraints[R1], Constraints.back());
47 RemainingRows.
push_back(std::move(Constraints.back()));
48 Constraints.pop_back();
53 unsigned NumRemainingConstraints = RemainingRows.
size();
54 for (
unsigned R1 = 0; R1 < NumRemainingConstraints; R1++) {
56 for (
unsigned R2 = R1 + 1;
R2 < NumRemainingConstraints;
R2++) {
63 int64_t UpperLast = getLastCoefficient(RemainingRows[
R2], LastIdx);
64 int64_t LowerLast = getLastCoefficient(RemainingRows[R1], LastIdx);
66 UpperLast != 0 && LowerLast != 0 &&
67 "RemainingRows should only contain rows where the variable is != 0");
69 if ((LowerLast < 0 && UpperLast < 0) || (LowerLast > 0 && UpperLast > 0))
80 unsigned IdxUpper = 0;
81 unsigned IdxLower = 0;
82 auto &LowerRow = RemainingRows[LowerR];
83 auto &UpperRow = RemainingRows[UpperR];
86 bool Overflow =
false;
90 if (IdxUpper >= UpperRow.size() || IdxLower >= LowerRow.size())
96 uint16_t CurrentId = std::numeric_limits<uint16_t>::max();
97 if (IdxUpper < UpperRow.size()) {
98 CurrentId = std::min(UpperRow[IdxUpper].Id, CurrentId);
100 if (IdxLower < LowerRow.size()) {
101 CurrentId = std::min(LowerRow[IdxLower].Id, CurrentId);
104 if (IdxUpper < UpperRow.size() && UpperRow[IdxUpper].Id == CurrentId) {
105 UpperV = UpperRow[IdxUpper].Coefficient;
113 if (IdxLower < LowerRow.size() && LowerRow[IdxLower].Id == CurrentId) {
114 LowerV = LowerRow[IdxLower].Coefficient;
143 NR.emplace_back(
N, CurrentId);
145 if (Overflow || NR.empty())
148 Constraints.push_back(std::move(NR));
150 if (Constraints.size() > 500)
163 for (
const Entry &E : R) {
166 if (E.Coefficient == std::numeric_limits<int64_t>::min())
168 G = std::gcd(
G, E.Coefficient);
179bool ConstraintSystem::mayHaveSolutionImpl() {
180 while (!Constraints.empty() && NumVariables > 0) {
181 if (!eliminateUsingFM())
185 assert((Constraints.empty() || NumVariables == 0) &&
186 "non-empty system must have all variables eliminated");
187 return all_of(Constraints,
192 SmallVector<std::string> Names(Value2Index.size(),
"");
194 for (
auto &[V, Index] : Value2Index) {
195 std::string OperandName;
196 if (
V->getName().empty())
197 OperandName =
V->getNameOrAsOperand();
199 OperandName = std::string(
"%") +
V->getName().str();
200 Names[
Index - 1] = OperandName;
208 if (Constraints.empty())
211 for (
const auto &Row : Constraints) {
213 for (
const Entry &E : Row) {
214 if (E.Id > NumVariables)
221 std::string Name = E.Id <= Names.
size() ? Names[E.Id - 1]
222 : (
"%v" + std::to_string(E.Id));
223 std::string Coefficient;
224 if (E.Coefficient != 1)
225 Coefficient = std::to_string(E.Coefficient) +
" * ";
237 bool HasSolution = mayHaveSolutionImpl();
242std::pair<ConstraintSystem, ConstraintSystem::RowTy>
244 assert((R.empty() || R.back().Id <= NumVariables) &&
245 "query must only use variables of the system");
253 for (
const Entry &E : R)
255 InSystem[E.Id] =
true;
258 return E.Id != 0 && InSystem[E.Id];
264 for (
const RowTy &Row : Constraints) {
266 if (!SharesVariable(Row))
268 for (
const Entry &E : Row)
269 if (E.Id != 0 && !InSystem[E.Id]) {
270 InSystem[E.Id] =
true;
278 unsigned NextIdx = 1;
279 for (
unsigned Id : InSystem.
set_bits())
280 OldToNew[Id] = NextIdx++;
283 SubSystem.NumVariables = NextIdx - 1;
284 for (
const RowTy &Row : Constraints) {
285 if (!SharesVariable(Row))
288 for (
const Entry &E : Row) {
289 unsigned New = OldToNew[E.Id];
290 assert((E.Id == 0) == (New == 0) &&
"constant entry must be preserved");
293 SubSystem.Constraints.push_back(std::move(NewRow));
298 for (
const Entry &E : R)
301 return {std::move(SubSystem), std::move(NewR)};
305 int64_t
C = getConstant(R);
306 if (hasConstantEntry(R))
309 if (getConstant(Row) >
C)
311 if (hasConstantEntry(Row))
312 Row = Row.drop_front();
314 return A.Id ==
B.Id &&
A.Coefficient ==
B.Coefficient;
322 if (isConstantOnly(R))
323 return getConstant(R) >= 0;
338 Copy.addRow(R, NumVariables);
339 return !Copy.mayHaveSolution();
348 if (isConstantOnly(R))
349 return getConstant(R) >= 0;
353 return SubCS.isConditionImplied(NewR);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static uint64_t getConstant(const Value *IndexValue)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
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 isImpliedBySingleRow(ArrayRef< Entry > R) const
Returns true if a single row of the system implies R.
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...
static LLVM_ABI void normalizeByGCD(MutableArrayRef< Entry > R)
Divide the variable coefficients of R by their greatest common divisor G and round the constant down ...
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.
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
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 T divideFloorSigned(U Numerator, V Denominator)
Returns the integer floor(Numerator / Denominator).
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.
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...
bool equal(L &&LRange, R &&RRange)
Wrapper function around std::equal to detect if pair-wise elements between two ranges are the same.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.