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) {
52 assert(L !=
NullLit && !llvm::is_contained(Simplified, L));
54 if (trueVars.contains(
X)) {
60 if (falseVars.contains(
X)) {
66 Simplified.push_back(L);
68 if (Simplified.empty()) {
71 Formula.addClause(Simplified);
74 if (Simplified.size() == 1) {
83 Formula.addClause(Simplified);
88 bool isKnownContradictory() {
return Formula.knownContradictory(); }
92 llvm::DenseSet<Variable> trueVars;
93 llvm::DenseSet<Variable> falseVars;
99 : LargestVar(LargestVar), KnownContradictory(
false) {
100 Clauses.push_back(0);
101 ClauseStarts.push_back(0);
105 assert(!llvm::is_contained(lits,
NullLit));
108 KnownContradictory =
true;
110 const size_t S = Clauses.size();
111 ClauseStarts.push_back(S);
112 llvm::append_range(Clauses, lits);
116 llvm::DenseMap<Variable, Atom> &Atomics) {
124 llvm::DenseMap<const Formula *, Variable> FormulaToVar;
128 std::queue<const Formula *> UnprocessedFormulas;
129 for (
const Formula *F : Formulas)
130 UnprocessedFormulas.push(F);
131 while (!UnprocessedFormulas.empty()) {
133 const Formula *F = UnprocessedFormulas.front();
134 UnprocessedFormulas.pop();
136 if (!FormulaToVar.try_emplace(F, Var).second)
141 UnprocessedFormulas.push(Op);
147 auto GetVar = [&FormulaToVar](
const Formula *F) {
148 auto ValIt = FormulaToVar.find(F);
149 assert(ValIt != FormulaToVar.end());
150 return ValIt->second;
154 std::vector<bool> ProcessedSubVals(NextVar,
false);
155 CNFFormulaBuilder builder(CNF);
159 for (
const Formula *F : Formulas)
160 builder.addClause(
posLit(GetVar(F)));
164 std::queue<const Formula *> UnprocessedFormulas;
165 for (
const Formula *F : Formulas)
166 UnprocessedFormulas.push(F);
167 while (!UnprocessedFormulas.empty()) {
168 const Formula *F = UnprocessedFormulas.front();
169 UnprocessedFormulas.pop();
172 if (ProcessedSubVals[Var])
174 ProcessedSubVals[Var] =
true;
253 builder.addClause(
posLit(Var));
269 if (builder.isKnownContradictory()) {
273 UnprocessedFormulas.push(Child);
280 CNFFormulaBuilder FinalBuilder(FinalCNF);
292 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.
const half4 lit(half NDotL, half NDotH, half M)