ConstraintSystem.cpp 5.0 KB

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