xref: /freebsd/contrib/llvm-project/clang/include/clang/Analysis/FlowSensitive/WatchedLiteralsSolver.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===- WatchedLiteralsSolver.h ----------------------------------*- C++ -*-===//
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 //
9 //  This file defines a SAT solver implementation that can be used by dataflow
10 //  analyses.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_WATCHEDLITERALSSOLVER_H
15 #define LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_WATCHEDLITERALSSOLVER_H
16 
17 #include "clang/Analysis/FlowSensitive/Formula.h"
18 #include "clang/Analysis/FlowSensitive/Solver.h"
19 #include "llvm/ADT/ArrayRef.h"
20 
21 namespace clang {
22 namespace dataflow {
23 
24 /// A SAT solver that is an implementation of Algorithm D from Knuth's The Art
25 /// of Computer Programming Volume 4: Satisfiability, Fascicle 6. It is based on
26 /// the Davis-Putnam-Logemann-Loveland (DPLL) algorithm [1], keeps references to
27 /// a single "watched" literal per clause, and uses a set of "active" variables
28 /// for unit propagation.
29 //
30 // [1] https://en.wikipedia.org/wiki/DPLL_algorithm
31 class WatchedLiteralsSolver : public Solver {
32   // Count of the iterations of the main loop of the solver. This spans *all*
33   // calls to the underlying solver across the life of this object. It is
34   // reduced with every (non-trivial) call to the solver.
35   //
36   // We give control over the abstract count of iterations instead of concrete
37   // measurements like CPU cycles or time to ensure deterministic results.
38   std::int64_t MaxIterations = std::numeric_limits<std::int64_t>::max();
39 
40 public:
41   WatchedLiteralsSolver() = default;
42 
43   // `Work` specifies a computational limit on the solver. Units of "work"
44   // roughly correspond to attempts to assign a value to a single
45   // variable. Since the algorithm is exponential in the number of variables,
46   // this is the most direct (abstract) unit to target.
WatchedLiteralsSolver(std::int64_t WorkLimit)47   explicit WatchedLiteralsSolver(std::int64_t WorkLimit)
48       : MaxIterations(WorkLimit) {}
49 
50   Result solve(llvm::ArrayRef<const Formula *> Vals) override;
51 
reachedLimit()52   bool reachedLimit() const override { return MaxIterations == 0; }
53 };
54 
55 } // namespace dataflow
56 } // namespace clang
57 
58 #endif // LLVM_CLANG_ANALYSIS_FLOWSENSITIVE_WATCHEDLITERALSSOLVER_H
59