1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
//===- CNFFormula.cpp -------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// A representation of a boolean formula in 3-CNF.
//
//===----------------------------------------------------------------------===//
#include "clang/Analysis/FlowSensitive/CNFFormula.h"
#include "llvm/ADT/DenseSet.h"
#include <queue>
namespace clang {
namespace dataflow {
namespace {
/// Applies simplifications while building up a BooleanFormula.
/// We keep track of unit clauses, which tell us variables that must be
/// true/false in any model that satisfies the overall formula.
/// Such variables can be dropped from subsequently-added clauses, which
/// may in turn yield more unit clauses or even a contradiction.
/// The total added complexity of this preprocessing is O(N) where we
/// for every clause, we do a lookup for each unit clauses.
/// The lookup is O(1) on average. This method won't catch all
/// contradictory formulas, more passes can in principle catch
/// more cases but we leave all these and the general case to the
/// proper SAT solver.
struct CNFFormulaBuilder {
// Formula should outlive CNFFormulaBuilder.
explicit CNFFormulaBuilder(CNFFormula &CNF) : Formula(CNF) {}
/// Adds the `L1 v ... v Ln` clause to the formula. Applies
/// simplifications, based on single-literal clauses.
///
/// Requirements:
///
/// `Li` must not be `NullLit`.
///
/// All literals must be distinct.
void addClause(ArrayRef<Literal> Literals) {
// We generate clauses with up to 3 literals in this file.
assert(!Literals.empty() && Literals.size() <= 3);
// Contains literals of the simplified clause.
llvm::SmallVector<Literal> Simplified;
for (auto L : Literals) {
assert(L != NullLit &&
llvm::all_of(Simplified, [L](Literal S) { return S != L; }));
auto X = var(L);
if (trueVars.contains(X)) { // X must be true
if (isPosLit(L))
return; // Omit clause `(... v X v ...)`, it is `true`.
else
continue; // Omit `!X` from `(... v !X v ...)`.
}
if (falseVars.contains(X)) { // X must be false
if (isNegLit(L))
return; // Omit clause `(... v !X v ...)`, it is `true`.
else
continue; // Omit `X` from `(... v X v ...)`.
}
Simplified.push_back(L);
}
if (Simplified.empty()) {
// Simplification made the clause empty, which is equivalent to `false`.
// We already know that this formula is unsatisfiable.
Formula.addClause(Simplified);
return;
}
if (Simplified.size() == 1) {
// We have new unit clause.
const Literal lit = Simplified.front();
const Variable v = var(lit);
if (isPosLit(lit))
trueVars.insert(v);
else
falseVars.insert(v);
}
Formula.addClause(Simplified);
}
/// Returns true if we observed a contradiction while adding clauses.
/// In this case then the formula is already known to be unsatisfiable.
bool isKnownContradictory() { return Formula.knownContradictory(); }
private:
CNFFormula &Formula;
llvm::DenseSet<Variable> trueVars;
llvm::DenseSet<Variable> falseVars;
};
} // namespace
CNFFormula::CNFFormula(Variable LargestVar)
: LargestVar(LargestVar), KnownContradictory(false) {
Clauses.push_back(0);
ClauseStarts.push_back(0);
}
void CNFFormula::addClause(ArrayRef<Literal> lits) {
assert(llvm::all_of(lits, [](Literal L) { return L != NullLit; }));
if (lits.empty())
KnownContradictory = true;
const size_t S = Clauses.size();
ClauseStarts.push_back(S);
Clauses.insert(Clauses.end(), lits.begin(), lits.end());
}
CNFFormula buildCNF(const llvm::ArrayRef<const Formula *> &Formulas,
llvm::DenseMap<Variable, Atom> &Atomics) {
// The general strategy of the algorithm implemented below is to map each
// of the sub-values in `Vals` to a unique variable and use these variables in
// the resulting CNF expression to avoid exponential blow up. The number of
// literals in the resulting formula is guaranteed to be linear in the number
// of sub-formulas in `Vals`.
// Map each sub-formula in `Vals` to a unique variable.
llvm::DenseMap<const Formula *, Variable> FormulaToVar;
// Store variable identifiers and Atom of atomic booleans.
Variable NextVar = 1;
{
std::queue<const Formula *> UnprocessedFormulas;
for (const Formula *F : Formulas)
UnprocessedFormulas.push(F);
while (!UnprocessedFormulas.empty()) {
Variable Var = NextVar;
const Formula *F = UnprocessedFormulas.front();
UnprocessedFormulas.pop();
if (!FormulaToVar.try_emplace(F, Var).second)
continue;
++NextVar;
for (const Formula *Op : F->operands())
UnprocessedFormulas.push(Op);
if (F->kind() == Formula::AtomRef)
Atomics[Var] = F->getAtom();
}
}
auto GetVar = [&FormulaToVar](const Formula *F) {
auto ValIt = FormulaToVar.find(F);
assert(ValIt != FormulaToVar.end());
return ValIt->second;
};
CNFFormula CNF(NextVar - 1);
std::vector<bool> ProcessedSubVals(NextVar, false);
CNFFormulaBuilder builder(CNF);
// Add a conjunct for each variable that represents a top-level conjunction
// value in `Vals`.
for (const Formula *F : Formulas)
builder.addClause(posLit(GetVar(F)));
// Add conjuncts that represent the mapping between newly-created variables
// and their corresponding sub-formulas.
std::queue<const Formula *> UnprocessedFormulas;
for (const Formula *F : Formulas)
UnprocessedFormulas.push(F);
while (!UnprocessedFormulas.empty()) {
const Formula *F = UnprocessedFormulas.front();
UnprocessedFormulas.pop();
const Variable Var = GetVar(F);
if (ProcessedSubVals[Var])
continue;
ProcessedSubVals[Var] = true;
switch (F->kind()) {
case Formula::AtomRef:
break;
case Formula::Literal:
CNF.addClause(F->literal() ? posLit(Var) : negLit(Var));
break;
case Formula::And: {
const Variable LHS = GetVar(F->operands()[0]);
const Variable RHS = GetVar(F->operands()[1]);
if (LHS == RHS) {
// `X <=> (A ^ A)` is equivalent to `(!X v A) ^ (X v !A)` which is
// already in conjunctive normal form. Below we add each of the
// conjuncts of the latter expression to the result.
builder.addClause({negLit(Var), posLit(LHS)});
builder.addClause({posLit(Var), negLit(LHS)});
} else {
// `X <=> (A ^ B)` is equivalent to `(!X v A) ^ (!X v B) ^ (X v !A v
// !B)` which is already in conjunctive normal form. Below we add each
// of the conjuncts of the latter expression to the result.
builder.addClause({negLit(Var), posLit(LHS)});
builder.addClause({negLit(Var), posLit(RHS)});
builder.addClause({posLit(Var), negLit(LHS), negLit(RHS)});
}
break;
}
case Formula::Or: {
const Variable LHS = GetVar(F->operands()[0]);
const Variable RHS = GetVar(F->operands()[1]);
if (LHS == RHS) {
// `X <=> (A v A)` is equivalent to `(!X v A) ^ (X v !A)` which is
// already in conjunctive normal form. Below we add each of the
// conjuncts of the latter expression to the result.
builder.addClause({negLit(Var), posLit(LHS)});
builder.addClause({posLit(Var), negLit(LHS)});
} else {
// `X <=> (A v B)` is equivalent to `(!X v A v B) ^ (X v !A) ^ (X v
// !B)` which is already in conjunctive normal form. Below we add each
// of the conjuncts of the latter expression to the result.
builder.addClause({negLit(Var), posLit(LHS), posLit(RHS)});
builder.addClause({posLit(Var), negLit(LHS)});
builder.addClause({posLit(Var), negLit(RHS)});
}
break;
}
case Formula::Not: {
const Variable Operand = GetVar(F->operands()[0]);
// `X <=> !Y` is equivalent to `(!X v !Y) ^ (X v Y)` which is
// already in conjunctive normal form. Below we add each of the
// conjuncts of the latter expression to the result.
builder.addClause({negLit(Var), negLit(Operand)});
builder.addClause({posLit(Var), posLit(Operand)});
break;
}
case Formula::Implies: {
const Variable LHS = GetVar(F->operands()[0]);
const Variable RHS = GetVar(F->operands()[1]);
// `X <=> (A => B)` is equivalent to
// `(X v A) ^ (X v !B) ^ (!X v !A v B)` which is already in
// conjunctive normal form. Below we add each of the conjuncts of
// the latter expression to the result.
builder.addClause({posLit(Var), posLit(LHS)});
builder.addClause({posLit(Var), negLit(RHS)});
builder.addClause({negLit(Var), negLit(LHS), posLit(RHS)});
break;
}
case Formula::Equal: {
const Variable LHS = GetVar(F->operands()[0]);
const Variable RHS = GetVar(F->operands()[1]);
if (LHS == RHS) {
// `X <=> (A <=> A)` is equivalent to `X` which is already in
// conjunctive normal form. Below we add each of the conjuncts of the
// latter expression to the result.
builder.addClause(posLit(Var));
// No need to visit the sub-values of `Val`.
continue;
}
// `X <=> (A <=> B)` is equivalent to
// `(X v A v B) ^ (X v !A v !B) ^ (!X v A v !B) ^ (!X v !A v B)` which
// is already in conjunctive normal form. Below we add each of the
// conjuncts of the latter expression to the result.
builder.addClause({posLit(Var), posLit(LHS), posLit(RHS)});
builder.addClause({posLit(Var), negLit(LHS), negLit(RHS)});
builder.addClause({negLit(Var), posLit(LHS), negLit(RHS)});
builder.addClause({negLit(Var), negLit(LHS), posLit(RHS)});
break;
}
}
if (builder.isKnownContradictory()) {
return CNF;
}
for (const Formula *Child : F->operands())
UnprocessedFormulas.push(Child);
}
// Unit clauses that were added later were not
// considered for the simplification of earlier clauses. Do a final
// pass to find more opportunities for simplification.
CNFFormula FinalCNF(NextVar - 1);
CNFFormulaBuilder FinalBuilder(FinalCNF);
// Collect unit clauses.
for (ClauseID C = 1; C <= CNF.numClauses(); ++C) {
if (CNF.clauseSize(C) == 1) {
FinalBuilder.addClause(CNF.clauseLiterals(C)[0]);
}
}
// Add all clauses that were added previously, preserving the order.
for (ClauseID C = 1; C <= CNF.numClauses(); ++C) {
FinalBuilder.addClause(CNF.clauseLiterals(C));
if (FinalBuilder.isKnownContradictory()) {
break;
}
}
// It is possible there were new unit clauses again, but
// we stop here and leave the rest to the solver algorithm.
return FinalCNF;
}
} // namespace dataflow
} // namespace clang
|