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 \p Names as variable names.
  31. void dump(ArrayRef<std::string> Names) const;
  32. /// Print the constraints in the system, using x0...xn as variable names.
  33. void dump() const;
  34. /// Returns true if there may be a solution for the constraints in the system.
  35. bool mayHaveSolutionImpl();
  36. public:
  37. bool addVariableRow(const SmallVector<int64_t, 8> &R) {
  38. assert(Constraints.empty() || R.size() == Constraints.back().size());
  39. // If all variable coefficients are 0, the constraint does not provide any
  40. // usable information.
  41. if (all_of(makeArrayRef(R).drop_front(1), [](int64_t C) { return C == 0; }))
  42. return false;
  43. for (const auto &C : R) {
  44. auto A = std::abs(C);
  45. GCD = APIntOps::GreatestCommonDivisor({32, (uint32_t)A}, {32, GCD})
  46. .getZExtValue();
  47. }
  48. Constraints.push_back(R);
  49. return true;
  50. }
  51. bool addVariableRowFill(const SmallVector<int64_t, 8> &R) {
  52. for (auto &CR : Constraints) {
  53. while (CR.size() != R.size())
  54. CR.push_back(0);
  55. }
  56. return addVariableRow(R);
  57. }
  58. /// Returns true if there may be a solution for the constraints in the system.
  59. bool mayHaveSolution();
  60. static SmallVector<int64_t, 8> negate(SmallVector<int64_t, 8> R) {
  61. // The negated constraint R is obtained by multiplying by -1 and adding 1 to
  62. // the constant.
  63. R[0] += 1;
  64. for (auto &C : R)
  65. C *= -1;
  66. return R;
  67. }
  68. bool isConditionImplied(SmallVector<int64_t, 8> R);
  69. void popLastConstraint() { Constraints.pop_back(); }
  70. /// Returns the number of rows in the constraint system.
  71. unsigned size() const { return Constraints.size(); }
  72. };
  73. } // namespace llvm
  74. #endif // LLVM_ANALYSIS_CONSTRAINTSYSTEM_H
  75. #ifdef __GNUC__
  76. #pragma GCC diagnostic pop
  77. #endif