|
| 1 | +//===-- SimplifyConstraints.cpp ---------------------------------*- 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 | +#include "clang/Analysis/FlowSensitive/SimplifyConstraints.h" |
| 10 | +#include "llvm/ADT/EquivalenceClasses.h" |
| 11 | + |
| 12 | +namespace clang { |
| 13 | +namespace dataflow { |
| 14 | + |
| 15 | +// Substitutes all occurrences of a given atom in `F` by a given formula and |
| 16 | +// returns the resulting formula. |
| 17 | +static const Formula & |
| 18 | +substitute(const Formula &F, |
| 19 | + const llvm::DenseMap<Atom, const Formula *> &Substitutions, |
| 20 | + Arena &arena) { |
| 21 | + switch (F.kind()) { |
| 22 | + case Formula::AtomRef: |
| 23 | + if (auto iter = Substitutions.find(F.getAtom()); |
| 24 | + iter != Substitutions.end()) |
| 25 | + return *iter->second; |
| 26 | + return F; |
| 27 | + case Formula::Literal: |
| 28 | + return F; |
| 29 | + case Formula::Not: |
| 30 | + return arena.makeNot(substitute(*F.operands()[0], Substitutions, arena)); |
| 31 | + case Formula::And: |
| 32 | + return arena.makeAnd(substitute(*F.operands()[0], Substitutions, arena), |
| 33 | + substitute(*F.operands()[1], Substitutions, arena)); |
| 34 | + case Formula::Or: |
| 35 | + return arena.makeOr(substitute(*F.operands()[0], Substitutions, arena), |
| 36 | + substitute(*F.operands()[1], Substitutions, arena)); |
| 37 | + case Formula::Implies: |
| 38 | + return arena.makeImplies( |
| 39 | + substitute(*F.operands()[0], Substitutions, arena), |
| 40 | + substitute(*F.operands()[1], Substitutions, arena)); |
| 41 | + case Formula::Equal: |
| 42 | + return arena.makeEquals(substitute(*F.operands()[0], Substitutions, arena), |
| 43 | + substitute(*F.operands()[1], Substitutions, arena)); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// Returns the result of replacing atoms in `Atoms` with the leader of their |
| 48 | +// equivalence class in `EquivalentAtoms`. |
| 49 | +// Atoms that don't have an equivalence class in `EquivalentAtoms` are inserted |
| 50 | +// into it as single-member equivalence classes. |
| 51 | +static llvm::DenseSet<Atom> |
| 52 | +projectToLeaders(const llvm::DenseSet<Atom> &Atoms, |
| 53 | + llvm::EquivalenceClasses<Atom> &EquivalentAtoms) { |
| 54 | + llvm::DenseSet<Atom> Result; |
| 55 | + |
| 56 | + for (Atom Atom : Atoms) |
| 57 | + Result.insert(EquivalentAtoms.getOrInsertLeaderValue(Atom)); |
| 58 | + |
| 59 | + return Result; |
| 60 | +} |
| 61 | + |
| 62 | +// Returns the atoms in the equivalence class for the leader identified by |
| 63 | +// `LeaderIt`. |
| 64 | +static llvm::SmallVector<Atom> |
| 65 | +atomsInEquivalenceClass(const llvm::EquivalenceClasses<Atom> &EquivalentAtoms, |
| 66 | + llvm::EquivalenceClasses<Atom>::iterator LeaderIt) { |
| 67 | + llvm::SmallVector<Atom> Result; |
| 68 | + for (auto MemberIt = EquivalentAtoms.member_begin(LeaderIt); |
| 69 | + MemberIt != EquivalentAtoms.member_end(); ++MemberIt) |
| 70 | + Result.push_back(*MemberIt); |
| 71 | + return Result; |
| 72 | +} |
| 73 | + |
| 74 | +void simplifyConstraints(llvm::SetVector<const Formula *> &Constraints, |
| 75 | + Arena &arena, SimplifyConstraintsInfo *Info) { |
| 76 | + auto contradiction = [&]() { |
| 77 | + Constraints.clear(); |
| 78 | + Constraints.insert(&arena.makeLiteral(false)); |
| 79 | + }; |
| 80 | + |
| 81 | + llvm::EquivalenceClasses<Atom> EquivalentAtoms; |
| 82 | + llvm::DenseSet<Atom> TrueAtoms; |
| 83 | + llvm::DenseSet<Atom> FalseAtoms; |
| 84 | + |
| 85 | + while (true) { |
| 86 | + for (const auto *Constraint : Constraints) { |
| 87 | + switch (Constraint->kind()) { |
| 88 | + case Formula::AtomRef: |
| 89 | + TrueAtoms.insert(Constraint->getAtom()); |
| 90 | + break; |
| 91 | + case Formula::Not: |
| 92 | + if (Constraint->operands()[0]->kind() == Formula::AtomRef) |
| 93 | + FalseAtoms.insert(Constraint->operands()[0]->getAtom()); |
| 94 | + break; |
| 95 | + case Formula::Equal: { |
| 96 | + ArrayRef<const Formula *> operands = Constraint->operands(); |
| 97 | + if (operands[0]->kind() == Formula::AtomRef && |
| 98 | + operands[1]->kind() == Formula::AtomRef) { |
| 99 | + EquivalentAtoms.unionSets(operands[0]->getAtom(), |
| 100 | + operands[1]->getAtom()); |
| 101 | + } |
| 102 | + break; |
| 103 | + } |
| 104 | + default: |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + TrueAtoms = projectToLeaders(TrueAtoms, EquivalentAtoms); |
| 110 | + FalseAtoms = projectToLeaders(FalseAtoms, EquivalentAtoms); |
| 111 | + |
| 112 | + llvm::DenseMap<Atom, const Formula *> Substitutions; |
| 113 | + for (auto It = EquivalentAtoms.begin(); It != EquivalentAtoms.end(); ++It) { |
| 114 | + Atom TheAtom = It->getData(); |
| 115 | + Atom Leader = EquivalentAtoms.getLeaderValue(TheAtom); |
| 116 | + if (TrueAtoms.contains(Leader)) { |
| 117 | + if (FalseAtoms.contains(Leader)) { |
| 118 | + contradiction(); |
| 119 | + return; |
| 120 | + } |
| 121 | + Substitutions.insert({TheAtom, &arena.makeLiteral(true)}); |
| 122 | + } else if (FalseAtoms.contains(Leader)) { |
| 123 | + Substitutions.insert({TheAtom, &arena.makeLiteral(false)}); |
| 124 | + } else if (TheAtom != Leader) { |
| 125 | + Substitutions.insert({TheAtom, &arena.makeAtomRef(Leader)}); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + llvm::SetVector<const Formula *> NewConstraints; |
| 130 | + for (const auto *Constraint : Constraints) { |
| 131 | + const Formula &NewConstraint = |
| 132 | + substitute(*Constraint, Substitutions, arena); |
| 133 | + if (&NewConstraint == &arena.makeLiteral(true)) |
| 134 | + continue; |
| 135 | + if (&NewConstraint == &arena.makeLiteral(false)) { |
| 136 | + contradiction(); |
| 137 | + return; |
| 138 | + } |
| 139 | + if (NewConstraint.kind() == Formula::And) { |
| 140 | + NewConstraints.insert(NewConstraint.operands()[0]); |
| 141 | + NewConstraints.insert(NewConstraint.operands()[1]); |
| 142 | + continue; |
| 143 | + } |
| 144 | + NewConstraints.insert(&NewConstraint); |
| 145 | + } |
| 146 | + |
| 147 | + if (NewConstraints == Constraints) |
| 148 | + break; |
| 149 | + Constraints = std::move(NewConstraints); |
| 150 | + } |
| 151 | + |
| 152 | + if (Info) { |
| 153 | + for (auto It = EquivalentAtoms.begin(), End = EquivalentAtoms.end(); |
| 154 | + It != End; ++It) { |
| 155 | + if (!It->isLeader()) |
| 156 | + continue; |
| 157 | + Atom At = *EquivalentAtoms.findLeader(It); |
| 158 | + if (TrueAtoms.contains(At) || FalseAtoms.contains(At)) |
| 159 | + continue; |
| 160 | + llvm::SmallVector<Atom> Atoms = |
| 161 | + atomsInEquivalenceClass(EquivalentAtoms, It); |
| 162 | + if (Atoms.size() == 1) |
| 163 | + continue; |
| 164 | + std::sort(Atoms.begin(), Atoms.end()); |
| 165 | + Info->EquivalentAtoms.push_back(std::move(Atoms)); |
| 166 | + } |
| 167 | + for (Atom At : TrueAtoms) |
| 168 | + Info->TrueAtoms.append(atomsInEquivalenceClass( |
| 169 | + EquivalentAtoms, EquivalentAtoms.findValue(At))); |
| 170 | + std::sort(Info->TrueAtoms.begin(), Info->TrueAtoms.end()); |
| 171 | + for (Atom At : FalseAtoms) |
| 172 | + Info->FalseAtoms.append(atomsInEquivalenceClass( |
| 173 | + EquivalentAtoms, EquivalentAtoms.findValue(At))); |
| 174 | + std::sort(Info->FalseAtoms.begin(), Info->FalseAtoms.end()); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +} // namespace dataflow |
| 179 | +} // namespace clang |
0 commit comments