14#include "llvm/ADT/DenseSet.h"
34struct CNFFormulaBuilder {
36 explicit CNFFormulaBuilder(CNFFormula &CNF) : Formula(CNF) {}
46 void addClause(ArrayRef<Literal> Literals) {
48 assert(!Literals.empty() && Literals.size() <= 3);
51 for (
auto L : Literals) {
53 llvm::all_of(Simplified, [L](
Literal S) {
return S != L; }));
55 if (trueVars.contains(
X)) {
61 if (falseVars.contains(
X)) {
67 Simplified.push_back(L);
69 if (Simplified.empty()) {
72 Formula.addClause(Simplified);
75 if (Simplified.size() == 1) {
77 const Literal lit = Simplified.front();
84 Formula.addClause(Simplified);
89 bool isKnownContradictory() {
return Formula.knownContradictory(); }
93 llvm::DenseSet<Variable> trueVars;
94 llvm::DenseSet<Variable> falseVars;
100 : LargestVar(LargestVar), KnownContradictory(
false) {
101 Clauses.push_back(0);
102 ClauseStarts.push_back(0);
106 assert(llvm::all_of(lits, [](
Literal L) {
return L !=
NullLit; }));
109 KnownContradictory =
true;
111 const size_t S = Clauses.size();
112 ClauseStarts.push_back(S);
113 Clauses.insert(Clauses.end(), lits.begin(), lits.end());
117 llvm::DenseMap<Variable, Atom> &Atomics) {
125 llvm::DenseMap<const Formula *, Variable> FormulaToVar;
129 std::queue<const Formula *> UnprocessedFormulas;
130 for (
const Formula *F : Formulas)
131 UnprocessedFormulas.push(F);
132 while (!UnprocessedFormulas.empty()) {
134 const Formula *F = UnprocessedFormulas.front();
135 UnprocessedFormulas.pop();
137 if (!FormulaToVar.try_emplace(F, Var).second)
142 UnprocessedFormulas.push(Op);
148 auto GetVar = [&FormulaToVar](
const Formula *F) {
149 auto ValIt = FormulaToVar.find(F);
150 assert(ValIt != FormulaToVar.end());
151 return ValIt->second;
155 std::vector<bool> ProcessedSubVals(NextVar,
false);
156 CNFFormulaBuilder builder(CNF);
160 for (
const Formula *F : Formulas)
161 builder.addClause(
posLit(GetVar(F)));
165 std::queue<const Formula *> UnprocessedFormulas;
166 for (
const Formula *F : Formulas)
167 UnprocessedFormulas.push(F);
168 while (!UnprocessedFormulas.empty()) {
169 const Formula *F = UnprocessedFormulas.front();
170 UnprocessedFormulas.pop();
173 if (ProcessedSubVals[Var])
175 ProcessedSubVals[Var] =
true;
254 builder.addClause(
posLit(Var));
270 if (builder.isKnownContradictory()) {
274 UnprocessedFormulas.push(Child);
281 CNFFormulaBuilder FinalBuilder(FinalCNF);
293 if (FinalBuilder.isKnownContradictory()) {
uint32_t ClauseID
Clause identifiers are represented as positive integers.
constexpr Literal posLit(Variable V)
Returns the positive literal V.
constexpr bool isNegLit(Literal L)
Returns whether L is a negative literal.
CNFFormula buildCNF(const llvm::ArrayRef< const Formula * > &Formulas, llvm::DenseMap< Variable, Atom > &Atomics)
Converts the conjunction of Vals into a formula in conjunctive normal form where each clause has at l...
constexpr Literal NullLit
A null literal is used as a placeholder in various data structures and algorithms.
constexpr Variable var(Literal L)
Returns the variable of L.
constexpr Literal negLit(Variable V)
Returns the negative literal !V.
uint32_t Literal
Literals are represented as positive integers.
uint32_t Variable
Boolean variables are represented as positive integers.
constexpr bool isPosLit(Literal L)
Returns whether L is a positive literal.
The JSON file list parser is used to communicate input to InstallAPI.