xref: /freebsd-src/contrib/llvm-project/clang/lib/Analysis/ThreadSafetyLogical.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===- ThreadSafetyLogical.cpp ---------------------------------*- C++ --*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric // This file defines a representation for logical expressions with SExpr leaves
9*0b57cec5SDimitry Andric // that are used as part of fact-checking capability expressions.
10*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
11*0b57cec5SDimitry Andric 
12*0b57cec5SDimitry Andric #include "clang/Analysis/Analyses/ThreadSafetyLogical.h"
13*0b57cec5SDimitry Andric 
14*0b57cec5SDimitry Andric using namespace llvm;
15*0b57cec5SDimitry Andric using namespace clang::threadSafety::lexpr;
16*0b57cec5SDimitry Andric 
17*0b57cec5SDimitry Andric // Implication.  We implement De Morgan's Laws by maintaining LNeg and RNeg
18*0b57cec5SDimitry Andric // to keep track of whether LHS and RHS are negated.
implies(const LExpr * LHS,bool LNeg,const LExpr * RHS,bool RNeg)19*0b57cec5SDimitry Andric static bool implies(const LExpr *LHS, bool LNeg, const LExpr *RHS, bool RNeg) {
20*0b57cec5SDimitry Andric   // In comments below, we write => for implication.
21*0b57cec5SDimitry Andric 
22*0b57cec5SDimitry Andric   // Calculates the logical AND implication operator.
23*0b57cec5SDimitry Andric   const auto LeftAndOperator = [=](const BinOp *A) {
24*0b57cec5SDimitry Andric     return implies(A->left(), LNeg, RHS, RNeg) &&
25*0b57cec5SDimitry Andric            implies(A->right(), LNeg, RHS, RNeg);
26*0b57cec5SDimitry Andric   };
27*0b57cec5SDimitry Andric   const auto RightAndOperator = [=](const BinOp *A) {
28*0b57cec5SDimitry Andric     return implies(LHS, LNeg, A->left(), RNeg) &&
29*0b57cec5SDimitry Andric            implies(LHS, LNeg, A->right(), RNeg);
30*0b57cec5SDimitry Andric   };
31*0b57cec5SDimitry Andric 
32*0b57cec5SDimitry Andric   // Calculates the logical OR implication operator.
33*0b57cec5SDimitry Andric   const auto LeftOrOperator = [=](const BinOp *A) {
34*0b57cec5SDimitry Andric     return implies(A->left(), LNeg, RHS, RNeg) ||
35*0b57cec5SDimitry Andric            implies(A->right(), LNeg, RHS, RNeg);
36*0b57cec5SDimitry Andric   };
37*0b57cec5SDimitry Andric   const auto RightOrOperator = [=](const BinOp *A) {
38*0b57cec5SDimitry Andric     return implies(LHS, LNeg, A->left(), RNeg) ||
39*0b57cec5SDimitry Andric            implies(LHS, LNeg, A->right(), RNeg);
40*0b57cec5SDimitry Andric   };
41*0b57cec5SDimitry Andric 
42*0b57cec5SDimitry Andric   // Recurse on right.
43*0b57cec5SDimitry Andric   switch (RHS->kind()) {
44*0b57cec5SDimitry Andric   case LExpr::And:
45*0b57cec5SDimitry Andric     // When performing right recursion:
46*0b57cec5SDimitry Andric     //   C => A & B  [if]  C => A and C => B
47*0b57cec5SDimitry Andric     // When performing right recursion (negated):
48*0b57cec5SDimitry Andric     //   C => !(A & B)  [if]  C => !A | !B  [===]  C => !A or C => !B
49*0b57cec5SDimitry Andric     return RNeg ? RightOrOperator(cast<And>(RHS))
50*0b57cec5SDimitry Andric                 : RightAndOperator(cast<And>(RHS));
51*0b57cec5SDimitry Andric   case LExpr::Or:
52*0b57cec5SDimitry Andric     // When performing right recursion:
53*0b57cec5SDimitry Andric     //   C => (A | B)  [if]  C => A or C => B
54*0b57cec5SDimitry Andric     // When performing right recursion (negated):
55*0b57cec5SDimitry Andric     //   C => !(A | B)  [if]  C => !A & !B  [===]  C => !A and C => !B
56*0b57cec5SDimitry Andric     return RNeg ? RightAndOperator(cast<Or>(RHS))
57*0b57cec5SDimitry Andric                 : RightOrOperator(cast<Or>(RHS));
58*0b57cec5SDimitry Andric   case LExpr::Not:
59*0b57cec5SDimitry Andric     // Note that C => !A is very different from !(C => A). It would be incorrect
60*0b57cec5SDimitry Andric     // to return !implies(LHS, RHS).
61*0b57cec5SDimitry Andric     return implies(LHS, LNeg, cast<Not>(RHS)->exp(), !RNeg);
62*0b57cec5SDimitry Andric   case LExpr::Terminal:
63*0b57cec5SDimitry Andric     // After reaching the terminal, it's time to recurse on the left.
64*0b57cec5SDimitry Andric     break;
65*0b57cec5SDimitry Andric   }
66*0b57cec5SDimitry Andric 
67*0b57cec5SDimitry Andric   // RHS is now a terminal.  Recurse on Left.
68*0b57cec5SDimitry Andric   switch (LHS->kind()) {
69*0b57cec5SDimitry Andric   case LExpr::And:
70*0b57cec5SDimitry Andric     // When performing left recursion:
71*0b57cec5SDimitry Andric     //   A & B => C  [if]  A => C or B => C
72*0b57cec5SDimitry Andric     // When performing left recursion (negated):
73*0b57cec5SDimitry Andric     //   !(A & B) => C  [if]  !A | !B => C  [===]  !A => C and !B => C
74*0b57cec5SDimitry Andric     return LNeg ? LeftAndOperator(cast<And>(LHS))
75*0b57cec5SDimitry Andric                 : LeftOrOperator(cast<And>(LHS));
76*0b57cec5SDimitry Andric   case LExpr::Or:
77*0b57cec5SDimitry Andric     // When performing left recursion:
78*0b57cec5SDimitry Andric     //   A | B => C  [if]  A => C and B => C
79*0b57cec5SDimitry Andric     // When performing left recursion (negated):
80*0b57cec5SDimitry Andric     //   !(A | B) => C  [if]  !A & !B => C  [===]  !A => C or !B => C
81*0b57cec5SDimitry Andric     return LNeg ? LeftOrOperator(cast<Or>(LHS))
82*0b57cec5SDimitry Andric                 : LeftAndOperator(cast<Or>(LHS));
83*0b57cec5SDimitry Andric   case LExpr::Not:
84*0b57cec5SDimitry Andric     // Note that A => !C is very different from !(A => C). It would be incorrect
85*0b57cec5SDimitry Andric     // to return !implies(LHS, RHS).
86*0b57cec5SDimitry Andric     return implies(cast<Not>(LHS)->exp(), !LNeg, RHS, RNeg);
87*0b57cec5SDimitry Andric   case LExpr::Terminal:
88*0b57cec5SDimitry Andric     // After reaching the terminal, it's time to perform identity comparisons.
89*0b57cec5SDimitry Andric     break;
90*0b57cec5SDimitry Andric   }
91*0b57cec5SDimitry Andric 
92*0b57cec5SDimitry Andric   // A => A
93*0b57cec5SDimitry Andric   // !A => !A
94*0b57cec5SDimitry Andric   if (LNeg != RNeg)
95*0b57cec5SDimitry Andric     return false;
96*0b57cec5SDimitry Andric 
97*0b57cec5SDimitry Andric   // FIXME -- this should compare SExprs for equality, not pointer equality.
98*0b57cec5SDimitry Andric   return cast<Terminal>(LHS)->expr() == cast<Terminal>(RHS)->expr();
99*0b57cec5SDimitry Andric }
100*0b57cec5SDimitry Andric 
101*0b57cec5SDimitry Andric namespace clang {
102*0b57cec5SDimitry Andric namespace threadSafety {
103*0b57cec5SDimitry Andric namespace lexpr {
104*0b57cec5SDimitry Andric 
implies(const LExpr * LHS,const LExpr * RHS)105*0b57cec5SDimitry Andric bool implies(const LExpr *LHS, const LExpr *RHS) {
106*0b57cec5SDimitry Andric   // Start out by assuming that LHS and RHS are not negated.
107*0b57cec5SDimitry Andric   return ::implies(LHS, false, RHS, false);
108*0b57cec5SDimitry Andric }
109*0b57cec5SDimitry Andric }
110*0b57cec5SDimitry Andric }
111*0b57cec5SDimitry Andric }
112