Skip to content

[clang][dataflow] Add an early-out to flowConditionImplies() / flowConditionAllows(). #78172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/include/clang/Analysis/FlowSensitive/Formula.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class alignas(const Formula *) Formula {
return static_cast<bool>(Value);
}

bool isLiteral(bool b) const {
return kind() == Literal && static_cast<bool>(Value) == b;
}

ArrayRef<const Formula *> operands() const {
return ArrayRef(reinterpret_cast<Formula *const *>(this + 1),
numOperands(kind()));
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Analysis/FlowSensitive/DataflowAnalysisContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ Solver::Result DataflowAnalysisContext::querySolver(

bool DataflowAnalysisContext::flowConditionImplies(Atom Token,
const Formula &F) {
if (F.isLiteral(true))
return true;

// Returns true if and only if truth assignment of the flow condition implies
// that `F` is also true. We prove whether or not this property holds by
// reducing the problem to satisfiability checking. In other words, we attempt
Expand All @@ -188,6 +191,9 @@ bool DataflowAnalysisContext::flowConditionImplies(Atom Token,

bool DataflowAnalysisContext::flowConditionAllows(Atom Token,
const Formula &F) {
if (F.isLiteral(false))
return false;

llvm::SetVector<const Formula *> Constraints;
Constraints.insert(&arena().makeAtomRef(Token));
Constraints.insert(&F);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,34 @@ TEST_F(DataflowAnalysisContextTest, DistinctTopsNotEquivalent) {
EXPECT_FALSE(Context.equivalentFormulas(X.formula(), Y.formula()));
}

TEST_F(DataflowAnalysisContextTest, EmptyFlowCondition) {
TEST_F(DataflowAnalysisContextTest, TautologicalFlowConditionImplies) {
Atom FC = A.makeFlowConditionToken();
auto &C = A.makeAtomRef(A.makeAtom());
EXPECT_FALSE(Context.flowConditionImplies(FC, C));
EXPECT_TRUE(Context.flowConditionImplies(FC, A.makeLiteral(true)));
EXPECT_FALSE(Context.flowConditionImplies(FC, A.makeLiteral(false)));
EXPECT_FALSE(Context.flowConditionImplies(FC, A.makeAtomRef(A.makeAtom())));
}

TEST_F(DataflowAnalysisContextTest, TautologicalFlowConditionAllows) {
Atom FC = A.makeFlowConditionToken();
EXPECT_TRUE(Context.flowConditionAllows(FC, A.makeLiteral(true)));
EXPECT_FALSE(Context.flowConditionAllows(FC, A.makeLiteral(false)));
EXPECT_TRUE(Context.flowConditionAllows(FC, A.makeAtomRef(A.makeAtom())));
}

TEST_F(DataflowAnalysisContextTest, ContradictoryFlowConditionImpliesAnything) {
Atom FC = A.makeFlowConditionToken();
Context.addFlowConditionConstraint(FC, A.makeLiteral(false));
EXPECT_TRUE(Context.flowConditionImplies(FC, A.makeLiteral(true)));
EXPECT_TRUE(Context.flowConditionImplies(FC, A.makeLiteral(false)));
EXPECT_TRUE(Context.flowConditionImplies(FC, A.makeAtomRef(A.makeAtom())));
}

TEST_F(DataflowAnalysisContextTest, ContradictoryFlowConditionAllowsNothing) {
Atom FC = A.makeFlowConditionToken();
Context.addFlowConditionConstraint(FC, A.makeLiteral(false));
EXPECT_FALSE(Context.flowConditionAllows(FC, A.makeLiteral(true)));
EXPECT_FALSE(Context.flowConditionAllows(FC, A.makeLiteral(false)));
EXPECT_FALSE(Context.flowConditionAllows(FC, A.makeAtomRef(A.makeAtom())));
}

TEST_F(DataflowAnalysisContextTest, AddFlowConditionConstraint) {
Expand Down