-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drop clause if it contains an impossible BinaryConstraint (#2376)
* Drop clause if impossible BinaryConstraint Some BinaryConstraint atoms can be determined to be impossible during synthesis. Introduce a transform to eliminate clauses containing such impossible atoms. These kinds of atoms are not likely to be written directly, but can easily arise with inlined code. * Rewrite as SimplifyBinaryConstraintsTransform
- Loading branch information
1 parent
84878b9
commit 0d75dc3
Showing
5 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Souffle - A Datalog Compiler | ||
* Copyright (c) 2018, The Souffle Developers. All rights reserved | ||
* Licensed under the Universal Permissive License v 1.0 as shown at: | ||
* - https://opensource.org/licenses/UPL | ||
* - <souffle root>/licenses/SOUFFLE-UPL.txt | ||
*/ | ||
|
||
/************************************************************************ | ||
* | ||
* @file SimplifyConstantBinaryConstraints.cpp | ||
* | ||
***********************************************************************/ | ||
|
||
#include "ast/transform/SimplifyConstantBinaryConstraints.h" | ||
#include "ast/BinaryConstraint.h" | ||
#include "ast/BooleanConstraint.h" | ||
#include "ast/Clause.h" | ||
#include "ast/Constant.h" | ||
#include "ast/Literal.h" | ||
#include "ast/Program.h" | ||
#include "ast/Relation.h" | ||
#include "ast/TranslationUnit.h" | ||
#include "souffle/BinaryConstraintOps.h" | ||
|
||
namespace souffle::ast::transform { | ||
|
||
bool SimplifyConstantBinaryConstraintsTransformer::transform(TranslationUnit& translationUnit) { | ||
Program& program = translationUnit.getProgram(); | ||
bool changed = false; | ||
|
||
for (Relation* rel : program.getRelations()) { | ||
for (auto&& clause : program.getClauses(*rel)) { | ||
bool canSimplify = false; | ||
|
||
for (Literal* lit : clause->getBodyLiterals()) { | ||
if (auto* bc = as<BinaryConstraint>(lit)) { | ||
if (isA<Constant>(*bc->getLHS()) && isA<Constant>(*bc->getRHS()) && | ||
(bc->getBaseOperator() == BinaryConstraintOp::EQ || | ||
bc->getBaseOperator() == BinaryConstraintOp::NE)) { | ||
canSimplify = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (!canSimplify) { | ||
continue; | ||
} | ||
|
||
auto replacementClause = Own<Clause>(clause->cloneHead()); | ||
for (Literal* lit : clause->getBodyLiterals()) { | ||
bool replaced = false; | ||
|
||
if (auto* bc = as<BinaryConstraint>(lit)) { | ||
auto* lhs = bc->getLHS(); | ||
auto* rhs = bc->getRHS(); | ||
Constant *lhsConst, *rhsConst; | ||
if ((lhsConst = as<Constant>(lhs)) && (rhsConst = as<Constant>(rhs))) { | ||
bool constsEq = *lhsConst == *rhsConst; | ||
if (bc->getBaseOperator() == BinaryConstraintOp::EQ || | ||
bc->getBaseOperator() == BinaryConstraintOp::NE) { | ||
bool literalBool = constsEq; | ||
if (bc->getBaseOperator() == BinaryConstraintOp::NE) { | ||
literalBool = !constsEq; | ||
} | ||
BooleanConstraint replacementLiteral(literalBool); | ||
replacementClause->addToBody(clone(replacementLiteral)); | ||
replaced = true; | ||
} | ||
} | ||
} | ||
|
||
if (!replaced) { | ||
replacementClause->addToBody(clone(lit)); | ||
} | ||
} | ||
|
||
program.removeClause(*clause); | ||
program.addClause(std::move(replacementClause)); | ||
changed = true; | ||
} | ||
} | ||
|
||
return changed; | ||
} | ||
|
||
} // namespace souffle::ast::transform |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Souffle - A Datalog Compiler | ||
* Copyright (c) 2018, The Souffle Developers. All rights reserved | ||
* Licensed under the Universal Permissive License v 1.0 as shown at: | ||
* - https://opensource.org/licenses/UPL | ||
* - <souffle root>/licenses/SOUFFLE-UPL.txt | ||
*/ | ||
|
||
/************************************************************************ | ||
* | ||
* @file SimplifyConstantBinaryConstraints.h | ||
* | ||
***********************************************************************/ | ||
|
||
#pragma once | ||
|
||
#include "ast/TranslationUnit.h" | ||
#include "ast/transform/Transformer.h" | ||
#include <string> | ||
|
||
namespace souffle::ast::transform { | ||
|
||
/** | ||
* Rewrites constant binary constraints into boolean constraints | ||
*/ | ||
class SimplifyConstantBinaryConstraintsTransformer : public Transformer { | ||
public: | ||
std::string getName() const override { | ||
return "SimplifyConstantBinaryConstraintsTransformer"; | ||
} | ||
|
||
private: | ||
SimplifyConstantBinaryConstraintsTransformer* cloning() const override { | ||
return new SimplifyConstantBinaryConstraintsTransformer(); | ||
} | ||
|
||
bool transform(TranslationUnit& translationUnit) override; | ||
}; | ||
|
||
} // namespace souffle::ast::transform |