Solver.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Solver.h -------------------------------------------------*- C++ -*-===//
  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. //
  14. // This file defines an interface for a SAT solver that can be used by
  15. // dataflow analyses.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_SOLVER_H
  19. #define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_SOLVER_H
  20. #include "clang/Analysis/FlowSensitive/Value.h"
  21. #include "llvm/ADT/DenseMap.h"
  22. #include "llvm/ADT/DenseSet.h"
  23. #include <optional>
  24. namespace clang {
  25. namespace dataflow {
  26. /// An interface for a SAT solver that can be used by dataflow analyses.
  27. class Solver {
  28. public:
  29. struct Result {
  30. enum class Status {
  31. /// Indicates that there exists a satisfying assignment for a boolean
  32. /// formula.
  33. Satisfiable,
  34. /// Indicates that there is no satisfying assignment for a boolean
  35. /// formula.
  36. Unsatisfiable,
  37. /// Indicates that the solver gave up trying to find a satisfying
  38. /// assignment for a boolean formula.
  39. TimedOut,
  40. };
  41. /// A boolean value is set to true or false in a truth assignment.
  42. enum class Assignment : uint8_t { AssignedFalse = 0, AssignedTrue = 1 };
  43. /// Constructs a result indicating that the queried boolean formula is
  44. /// satisfiable. The result will hold a solution found by the solver.
  45. static Result
  46. Satisfiable(llvm::DenseMap<AtomicBoolValue *, Assignment> Solution) {
  47. return Result(Status::Satisfiable, std::move(Solution));
  48. }
  49. /// Constructs a result indicating that the queried boolean formula is
  50. /// unsatisfiable.
  51. static Result Unsatisfiable() { return Result(Status::Unsatisfiable, {}); }
  52. /// Constructs a result indicating that satisfiability checking on the
  53. /// queried boolean formula was not completed.
  54. static Result TimedOut() { return Result(Status::TimedOut, {}); }
  55. /// Returns the status of satisfiability checking on the queried boolean
  56. /// formula.
  57. Status getStatus() const { return SATCheckStatus; }
  58. /// Returns a truth assignment to boolean values that satisfies the queried
  59. /// boolean formula if available. Otherwise, an empty optional is returned.
  60. std::optional<llvm::DenseMap<AtomicBoolValue *, Assignment>>
  61. getSolution() const {
  62. return Solution;
  63. }
  64. private:
  65. Result(
  66. enum Status SATCheckStatus,
  67. std::optional<llvm::DenseMap<AtomicBoolValue *, Assignment>> Solution)
  68. : SATCheckStatus(SATCheckStatus), Solution(std::move(Solution)) {}
  69. Status SATCheckStatus;
  70. std::optional<llvm::DenseMap<AtomicBoolValue *, Assignment>> Solution;
  71. };
  72. virtual ~Solver() = default;
  73. /// Checks if the conjunction of `Vals` is satisfiable and returns the
  74. /// corresponding result.
  75. ///
  76. /// Requirements:
  77. ///
  78. /// All elements in `Vals` must not be null.
  79. virtual Result solve(llvm::DenseSet<BoolValue *> Vals) = 0;
  80. };
  81. } // namespace dataflow
  82. } // namespace clang
  83. #endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_SOLVER_H
  84. #ifdef __GNUC__
  85. #pragma GCC diagnostic pop
  86. #endif