ConstraintSystem.h 2.9 KB

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