ConstraintSystem.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //===- ConstraintSystem.h - A system of linear constraints. --------------===//
  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. #ifndef LLVM_ANALYSIS_CONSTRAINTSYSTEM_H
  9. #define LLVM_ANALYSIS_CONSTRAINTSYSTEM_H
  10. #include "llvm/ADT/APInt.h"
  11. #include "llvm/ADT/ArrayRef.h"
  12. #include "llvm/ADT/STLExtras.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include <string>
  15. namespace llvm {
  16. class ConstraintSystem {
  17. /// Current linear constraints in the system.
  18. /// An entry of the form c0, c1, ... cn represents the following constraint:
  19. /// c0 >= v0 * c1 + .... + v{n-1} * cn
  20. SmallVector<SmallVector<int64_t, 8>, 4> Constraints;
  21. /// Current greatest common divisor for all coefficients in the system.
  22. uint32_t GCD = 1;
  23. // Eliminate constraints from the system using Fourier–Motzkin elimination.
  24. bool eliminateUsingFM();
  25. /// Print the constraints in the system, using x0...xn as variable names.
  26. void dump() const;
  27. /// Returns true if there may be a solution for the constraints in the system.
  28. bool mayHaveSolutionImpl();
  29. public:
  30. bool addVariableRow(const SmallVector<int64_t, 8> &R) {
  31. assert(Constraints.empty() || R.size() == Constraints.back().size());
  32. // If all variable coefficients are 0, the constraint does not provide any
  33. // usable information.
  34. if (all_of(makeArrayRef(R).drop_front(1), [](int64_t C) { return C == 0; }))
  35. return false;
  36. for (const auto &C : R) {
  37. auto A = std::abs(C);
  38. GCD = APIntOps::GreatestCommonDivisor({32, (uint32_t)A}, {32, GCD})
  39. .getZExtValue();
  40. }
  41. Constraints.push_back(R);
  42. return true;
  43. }
  44. bool addVariableRowFill(const SmallVector<int64_t, 8> &R) {
  45. for (auto &CR : Constraints) {
  46. while (CR.size() != R.size())
  47. CR.push_back(0);
  48. }
  49. return addVariableRow(R);
  50. }
  51. /// Returns true if there may be a solution for the constraints in the system.
  52. bool mayHaveSolution();
  53. static SmallVector<int64_t, 8> negate(SmallVector<int64_t, 8> R) {
  54. // The negated constraint R is obtained by multiplying by -1 and adding 1 to
  55. // the constant.
  56. R[0] += 1;
  57. for (auto &C : R)
  58. C *= -1;
  59. return R;
  60. }
  61. bool isConditionImplied(SmallVector<int64_t, 8> R);
  62. void popLastConstraint() { Constraints.pop_back(); }
  63. /// Returns the number of rows in the constraint system.
  64. unsigned size() const { return Constraints.size(); }
  65. /// Print the constraints in the system, using \p Names as variable names.
  66. void dump(ArrayRef<std::string> Names) const;
  67. };
  68. } // namespace llvm
  69. #endif // LLVM_ANALYSIS_CONSTRAINTSYSTEM_H