ConstraintSystem.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //===- ConstraintSytem.cpp - A system of linear constraints. ----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Analysis/ConstraintSystem.h"
  9. #include "llvm/ADT/SmallVector.h"
  10. #include "llvm/Support/MathExtras.h"
  11. #include "llvm/ADT/StringExtras.h"
  12. #include "llvm/Support/Debug.h"
  13. #include <string>
  14. using namespace llvm;
  15. #define DEBUG_TYPE "constraint-system"
  16. bool ConstraintSystem::eliminateUsingFM() {
  17. // Implementation of Fourier–Motzkin elimination, with some tricks from the
  18. // paper Pugh, William. "The Omega test: a fast and practical integer
  19. // programming algorithm for dependence
  20. // analysis."
  21. // Supercomputing'91: Proceedings of the 1991 ACM/
  22. // IEEE conference on Supercomputing. IEEE, 1991.
  23. assert(!Constraints.empty() &&
  24. "should only be called for non-empty constraint systems");
  25. unsigned NumVariables = Constraints[0].size();
  26. SmallVector<SmallVector<int64_t, 8>, 4> NewSystem;
  27. unsigned NumConstraints = Constraints.size();
  28. uint32_t NewGCD = 1;
  29. // FIXME do not use copy
  30. for (unsigned R1 = 0; R1 < NumConstraints; R1++) {
  31. if (Constraints[R1][1] == 0) {
  32. SmallVector<int64_t, 8> NR;
  33. NR.push_back(Constraints[R1][0]);
  34. for (unsigned i = 2; i < NumVariables; i++) {
  35. NR.push_back(Constraints[R1][i]);
  36. }
  37. NewSystem.push_back(std::move(NR));
  38. continue;
  39. }
  40. // FIXME do not use copy
  41. for (unsigned R2 = R1 + 1; R2 < NumConstraints; R2++) {
  42. if (R1 == R2)
  43. continue;
  44. // FIXME: can we do better than just dropping things here?
  45. if (Constraints[R2][1] == 0)
  46. continue;
  47. if ((Constraints[R1][1] < 0 && Constraints[R2][1] < 0) ||
  48. (Constraints[R1][1] > 0 && Constraints[R2][1] > 0))
  49. continue;
  50. unsigned LowerR = R1;
  51. unsigned UpperR = R2;
  52. if (Constraints[UpperR][1] < 0)
  53. std::swap(LowerR, UpperR);
  54. SmallVector<int64_t, 8> NR;
  55. for (unsigned I = 0; I < NumVariables; I++) {
  56. if (I == 1)
  57. continue;
  58. int64_t M1, M2, N;
  59. if (MulOverflow(Constraints[UpperR][I],
  60. ((-1) * Constraints[LowerR][1] / GCD), M1))
  61. return false;
  62. if (MulOverflow(Constraints[LowerR][I],
  63. (Constraints[UpperR][1] / GCD), M2))
  64. return false;
  65. if (AddOverflow(M1, M2, N))
  66. return false;
  67. NR.push_back(N);
  68. NewGCD = APIntOps::GreatestCommonDivisor({32, (uint32_t)NR.back()},
  69. {32, NewGCD})
  70. .getZExtValue();
  71. }
  72. NewSystem.push_back(std::move(NR));
  73. // Give up if the new system gets too big.
  74. if (NewSystem.size() > 500)
  75. return false;
  76. }
  77. }
  78. Constraints = std::move(NewSystem);
  79. GCD = NewGCD;
  80. return true;
  81. }
  82. bool ConstraintSystem::mayHaveSolutionImpl() {
  83. while (!Constraints.empty() && Constraints[0].size() > 1) {
  84. if (!eliminateUsingFM())
  85. return true;
  86. }
  87. if (Constraints.empty() || Constraints[0].size() > 1)
  88. return true;
  89. return all_of(Constraints, [](auto &R) { return R[0] >= 0; });
  90. }
  91. void ConstraintSystem::dump(ArrayRef<std::string> Names) const {
  92. if (Constraints.empty())
  93. return;
  94. for (const auto &Row : Constraints) {
  95. SmallVector<std::string, 16> Parts;
  96. for (unsigned I = 1, S = Row.size(); I < S; ++I) {
  97. if (Row[I] == 0)
  98. continue;
  99. std::string Coefficient;
  100. if (Row[I] != 1)
  101. Coefficient = std::to_string(Row[I]) + " * ";
  102. Parts.push_back(Coefficient + Names[I - 1]);
  103. }
  104. assert(!Parts.empty() && "need to have at least some parts");
  105. LLVM_DEBUG(dbgs() << join(Parts, std::string(" + "))
  106. << " <= " << std::to_string(Row[0]) << "\n");
  107. }
  108. }
  109. void ConstraintSystem::dump() const {
  110. SmallVector<std::string, 16> Names;
  111. for (unsigned i = 1; i < Constraints.back().size(); ++i)
  112. Names.push_back("x" + std::to_string(i));
  113. LLVM_DEBUG(dbgs() << "---\n");
  114. dump(Names);
  115. }
  116. bool ConstraintSystem::mayHaveSolution() {
  117. LLVM_DEBUG(dump());
  118. bool HasSolution = mayHaveSolutionImpl();
  119. LLVM_DEBUG(dbgs() << (HasSolution ? "sat" : "unsat") << "\n");
  120. return HasSolution;
  121. }
  122. bool ConstraintSystem::isConditionImplied(SmallVector<int64_t, 8> R) const {
  123. // If all variable coefficients are 0, we have 'C >= 0'. If the constant is >=
  124. // 0, R is always true, regardless of the system.
  125. if (all_of(ArrayRef(R).drop_front(1), [](int64_t C) { return C == 0; }))
  126. return R[0] >= 0;
  127. // If there is no solution with the negation of R added to the system, the
  128. // condition must hold based on the existing constraints.
  129. R = ConstraintSystem::negate(R);
  130. auto NewSystem = *this;
  131. NewSystem.addVariableRow(R);
  132. return !NewSystem.mayHaveSolution();
  133. }