ConstraintSystem.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ConstraintSystem.h - A system of linear constraints. --------------===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ANALYSIS_CONSTRAINTSYSTEM_H
  14. #define LLVM_ANALYSIS_CONSTRAINTSYSTEM_H
  15. #include "llvm/ADT/APInt.h"
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include <string>
  19. namespace llvm {
  20. class ConstraintSystem {
  21. /// Current linear constraints in the system.
  22. /// An entry of the form c0, c1, ... cn represents the following constraint:
  23. /// c0 >= v0 * c1 + .... + v{n-1} * cn
  24. SmallVector<SmallVector<int64_t, 8>, 4> Constraints;
  25. /// Current greatest common divisor for all coefficients in the system.
  26. uint32_t GCD = 1;
  27. // Eliminate constraints from the system using Fourier–Motzkin elimination.
  28. bool eliminateUsingFM();
  29. /// Print the constraints in the system, using x0...xn as variable names.
  30. void dump() const;
  31. /// Returns true if there may be a solution for the constraints in the system.
  32. bool mayHaveSolutionImpl();
  33. public:
  34. bool addVariableRow(ArrayRef<int64_t> R) {
  35. assert(Constraints.empty() || R.size() == Constraints.back().size());
  36. // If all variable coefficients are 0, the constraint does not provide any
  37. // usable information.
  38. if (all_of(ArrayRef(R).drop_front(1), [](int64_t C) { return C == 0; }))
  39. return false;
  40. for (const auto &C : R) {
  41. auto A = std::abs(C);
  42. GCD = APIntOps::GreatestCommonDivisor({32, (uint32_t)A}, {32, GCD})
  43. .getZExtValue();
  44. }
  45. Constraints.emplace_back(R.begin(), R.end());
  46. return true;
  47. }
  48. bool addVariableRowFill(ArrayRef<int64_t> R) {
  49. // If all variable coefficients are 0, the constraint does not provide any
  50. // usable information.
  51. if (all_of(ArrayRef(R).drop_front(1), [](int64_t C) { return C == 0; }))
  52. return false;
  53. for (auto &CR : Constraints) {
  54. while (CR.size() != R.size())
  55. CR.push_back(0);
  56. }
  57. return addVariableRow(R);
  58. }
  59. /// Returns true if there may be a solution for the constraints in the system.
  60. bool mayHaveSolution();
  61. static SmallVector<int64_t, 8> negate(SmallVector<int64_t, 8> R) {
  62. // The negated constraint R is obtained by multiplying by -1 and adding 1 to
  63. // the constant.
  64. R[0] += 1;
  65. for (auto &C : R)
  66. C *= -1;
  67. return R;
  68. }
  69. bool isConditionImplied(SmallVector<int64_t, 8> R) const;
  70. ArrayRef<int64_t> getLastConstraint() { return Constraints[0]; }
  71. void popLastConstraint() { Constraints.pop_back(); }
  72. void popLastNVariables(unsigned N) {
  73. for (auto &C : Constraints) {
  74. for (unsigned i = 0; i < N; i++)
  75. C.pop_back();
  76. }
  77. }
  78. /// Returns the number of rows in the constraint system.
  79. unsigned size() const { return Constraints.size(); }
  80. /// Print the constraints in the system, using \p Names as variable names.
  81. void dump(ArrayRef<std::string> Names) const;
  82. };
  83. } // namespace llvm
  84. #endif // LLVM_ANALYSIS_CONSTRAINTSYSTEM_H
  85. #ifdef __GNUC__
  86. #pragma GCC diagnostic pop
  87. #endif