1 /* Derivation and subsumption rules for constraints.
2 Copyright (C) 2013-2024 Free Software Foundation, Inc.
3 Contributed by Andrew Sutton (andrew.n.sutton@gmail.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
30 #include "double-int.h"
37 #include "stringpool.h"
42 #include "c-family/c-common.h"
43 #include "c-family/c-objc.h"
44 #include "cp-objcp-common.h"
45 #include "tree-inline.h"
48 #include "type-utils.h"
50 /* A conjunctive or disjunctive clause.
52 Each clause maintains an iterator that refers to the current
53 term, which is used in the linear decomposition of a formula
58 typedef std::list
<tree
>::iterator iterator
;
59 typedef std::list
<tree
>::const_iterator const_iterator
;
61 /* Initialize a clause with an initial term. */
65 m_terms
.push_back (t
);
66 if (TREE_CODE (t
) == ATOMIC_CONSTR
)
69 m_current
= m_terms
.begin ();
72 /* Create a copy of the current term. The current
73 iterator is set to point to the same position in the
74 copied list of terms. */
76 clause (clause
const& c
)
77 : m_terms (c
.m_terms
), m_set (c
.m_set
), m_current (m_terms
.begin ())
79 std::advance (m_current
, std::distance (c
.begin (), c
.current ()));
82 /* Returns true when all terms are atoms. */
86 return m_current
== end ();
89 /* Advance to the next term. */
93 gcc_assert (!done ());
97 /* Replaces the current term at position ITER with T. If
98 T is an atomic constraint that already appears in the
99 clause, remove but do not replace ITER. Returns a pair
100 containing an iterator to the replace object or past
101 the erased object and a boolean value which is true if
102 an object was erased. */
104 std::pair
<iterator
, bool> replace (iterator iter
, tree t
)
106 gcc_assert (TREE_CODE (*iter
) != ATOMIC_CONSTR
);
107 if (TREE_CODE (t
) == ATOMIC_CONSTR
)
110 return std::make_pair (m_terms
.erase (iter
), true);
113 return std::make_pair (iter
, false);
116 /* Inserts T before ITER in the list of terms. If T has
117 already is an atomic constraint that already appears in
118 the clause, no action is taken, and the current iterator
119 is returned. Returns a pair of an iterator to the inserted
120 object or ITER if no insertion occurred and a boolean
121 value which is true if an object was inserted. */
123 std::pair
<iterator
, bool> insert (iterator iter
, tree t
)
125 if (TREE_CODE (t
) == ATOMIC_CONSTR
)
128 return std::make_pair (iter
, false);
130 return std::make_pair (m_terms
.insert (iter
, t
), true);
133 /* Replaces the current term with T. In the case where the
134 current term is erased (because T is redundant), update
135 the position of the current term to the next term. */
137 void replace (tree t
)
139 m_current
= replace (m_current
, t
).first
;
142 /* Replace the current term with T1 and T2, in that order. */
144 void replace (tree t1
, tree t2
)
146 /* Replace the current term with t1. Ensure that iter points
147 to the term before which t2 will be inserted. Update the
148 current term as needed. */
149 std::pair
<iterator
, bool> rep
= replace (m_current
, t1
);
151 m_current
= rep
.first
;
155 /* Insert the t2. Make this the current term if we erased
157 std::pair
<iterator
, bool> ins
= insert (rep
.first
, t2
);
158 if (rep
.second
&& ins
.second
)
159 m_current
= ins
.first
;
162 /* Returns true if the clause contains the term T. */
164 bool contains (tree t
)
166 gcc_assert (TREE_CODE (t
) == ATOMIC_CONSTR
);
167 return m_set
.contains (t
);
171 /* Returns an iterator to the first clause in the formula. */
175 return m_terms
.begin ();
178 /* Returns an iterator to the first clause in the formula. */
180 const_iterator
begin () const
182 return m_terms
.begin ();
185 /* Returns an iterator past the last clause in the formula. */
189 return m_terms
.end ();
192 /* Returns an iterator past the last clause in the formula. */
194 const_iterator
end () const
196 return m_terms
.end ();
199 /* Returns the current iterator. */
201 const_iterator
current () const
206 std::list
<tree
> m_terms
; /* The list of terms. */
207 hash_set
<tree
, false, atom_hasher
> m_set
; /* The set of atomic constraints. */
208 iterator m_current
; /* The current term. */
212 /* A proof state owns a list of goals and tracks the
213 current sub-goal. The class also provides facilities
214 for managing subgoals and constructing term lists. */
218 typedef std::list
<clause
>::iterator iterator
;
219 typedef std::list
<clause
>::const_iterator const_iterator
;
221 /* Construct a formula with an initial formula in a
226 m_clauses
.emplace_back (t
);
227 m_current
= m_clauses
.begin ();
230 /* Returns true when all clauses are atomic. */
233 return m_current
== end ();
236 /* Advance to the next term. */
239 gcc_assert (!done ());
243 /* Insert a copy of clause into the formula. This corresponds
244 to a distribution of one logical operation over the other. */
248 gcc_assert (!done ());
249 return *m_clauses
.insert (std::next (m_current
), *m_current
);
252 /* Returns the position of the current clause. */
259 /* Returns an iterator to the first clause in the formula. */
263 return m_clauses
.begin ();
266 /* Returns an iterator to the first clause in the formula. */
268 const_iterator
begin () const
270 return m_clauses
.begin ();
273 /* Returns an iterator past the last clause in the formula. */
277 return m_clauses
.end ();
280 /* Returns an iterator past the last clause in the formula. */
282 const_iterator
end () const
284 return m_clauses
.end ();
287 /* Remove the specified clause from the formula. */
289 void erase (iterator i
)
291 gcc_assert (i
!= m_current
);
295 std::list
<clause
> m_clauses
; /* The list of clauses. */
296 iterator m_current
; /* The current clause. */
302 for (clause::iterator i
= c
.begin(); i
!= c
.end(); ++i
)
303 verbatim (" # %E", *i
);
309 for (formula::iterator i
= f
.begin(); i
!= f
.end(); ++i
)
311 /* Format punctuators via %s to avoid -Wformat-diag. */
312 verbatim ("%s", "(((");
314 verbatim ("%s", ")))");
318 /* The logical rules used to analyze a logical formula. The
319 "left" and "right" refer to the position of formula in a
320 sequent (as in sequent calculus). */
327 /* Distribution counting. */
330 disjunction_p (tree t
)
332 return TREE_CODE (t
) == DISJ_CONSTR
;
336 conjunction_p (tree t
)
338 return TREE_CODE (t
) == CONJ_CONSTR
;
344 return TREE_CODE (t
) == ATOMIC_CONSTR
;
347 /* Recursively count the number of clauses produced when converting T
348 to DNF. Returns a pair containing the number of clauses and a bool
349 value signifying that the tree would be rewritten as a result of
350 distributing. In general, a conjunction for which this flag is set
351 is considered a disjunction for the purpose of counting. */
353 static std::pair
<int, bool>
357 /* Atomic constraints produce no clauses. */
358 return std::make_pair (0, false);
360 /* For compound constraints, recursively count clauses and unpack
362 tree lhs
= TREE_OPERAND (t
, 0);
363 tree rhs
= TREE_OPERAND (t
, 1);
364 std::pair
<int, bool> p1
= dnf_size_r (lhs
);
365 std::pair
<int, bool> p2
= dnf_size_r (rhs
);
366 int n1
= p1
.first
, n2
= p2
.first
;
367 bool d1
= p1
.second
, d2
= p2
.second
;
369 if (disjunction_p (t
))
371 /* Matches constraints of the form P \/ Q. Disjunctions contribute
372 linearly to the number of constraints. When both P and Q are
373 disjunctions, clauses are added. When only one of P and Q
374 is a disjunction, an additional clause is produced. When neither
375 P nor Q are disjunctions, two clauses are produced. */
376 if (disjunction_p (lhs
))
378 if (disjunction_p (rhs
) || (conjunction_p (rhs
) && d2
))
379 /* Both P and Q are disjunctions. */
380 return std::make_pair (n1
+ n2
, d1
| d2
);
382 /* Only LHS is a disjunction. */
383 return std::make_pair (1 + n1
+ n2
, d1
| d2
);
386 if (conjunction_p (lhs
))
388 if ((disjunction_p (rhs
) && d1
) || (conjunction_p (rhs
) && d1
&& d2
))
389 /* Both P and Q are disjunctions. */
390 return std::make_pair (n1
+ n2
, d1
| d2
);
391 if (disjunction_p (rhs
)
392 || (conjunction_p (rhs
) && d1
!= d2
)
393 || (atomic_p (rhs
) && d1
))
394 /* Either LHS or RHS is a disjunction. */
395 return std::make_pair (1 + n1
+ n2
, d1
| d2
);
397 /* Neither LHS nor RHS is a disjunction. */
398 return std::make_pair (2, false);
402 if (disjunction_p (rhs
) || (conjunction_p (rhs
) && d2
))
403 /* Only RHS is a disjunction. */
404 return std::make_pair (1 + n1
+ n2
, d1
| d2
);
406 /* Neither LHS nor RHS is a disjunction. */
407 return std::make_pair (2, false);
410 else /* conjunction_p (t) */
412 /* Matches constraints of the form P /\ Q, possibly resulting
413 in the distribution of one side over the other. When both
414 P and Q are disjunctions, the number of clauses are multiplied.
415 When only one of P and Q is a disjunction, the number of
416 clauses are added. Otherwise, neither side is a disjunction and
417 no clauses are created. */
418 if (disjunction_p (lhs
))
420 if (disjunction_p (rhs
) || (conjunction_p (rhs
) && d2
))
421 /* Both P and Q are disjunctions. */
422 return std::make_pair (n1
* n2
, true);
424 /* Only LHS is a disjunction. */
425 return std::make_pair (n1
+ n2
, true);
428 if (conjunction_p (lhs
))
430 if ((disjunction_p (rhs
) && d1
) || (conjunction_p (rhs
) && d1
&& d2
))
431 /* Both P and Q are disjunctions. */
432 return std::make_pair (n1
* n2
, true);
433 if (disjunction_p (rhs
)
434 || (conjunction_p (rhs
) && d1
!= d2
)
435 || (atomic_p (rhs
) && d1
))
436 /* Either LHS or RHS is a disjunction. */
437 return std::make_pair (n1
+ n2
, true);
439 /* Neither LHS nor RHS is a disjunction. */
440 return std::make_pair (0, false);
444 if (disjunction_p (rhs
) || (conjunction_p (rhs
) && d2
))
445 /* Only RHS is a disjunction. */
446 return std::make_pair (n1
+ n2
, true);
448 /* Neither LHS nor RHS is a disjunction. */
449 return std::make_pair (0, false);
455 /* Recursively count the number of clauses produced when converting T
456 to CNF. Returns a pair containing the number of clauses and a bool
457 value signifying that the tree would be rewritten as a result of
458 distributing. In general, a disjunction for which this flag is set
459 is considered a conjunction for the purpose of counting. */
461 static std::pair
<int, bool>
465 /* Atomic constraints produce no clauses. */
466 return std::make_pair (0, false);
468 /* For compound constraints, recursively count clauses and unpack
470 tree lhs
= TREE_OPERAND (t
, 0);
471 tree rhs
= TREE_OPERAND (t
, 1);
472 std::pair
<int, bool> p1
= cnf_size_r (lhs
);
473 std::pair
<int, bool> p2
= cnf_size_r (rhs
);
474 int n1
= p1
.first
, n2
= p2
.first
;
475 bool d1
= p1
.second
, d2
= p2
.second
;
477 if (disjunction_p (t
))
479 /* Matches constraints of the form P \/ Q, possibly resulting
480 in the distribution of one side over the other. When both
481 P and Q are conjunctions, the number of clauses are multiplied.
482 When only one of P and Q is a conjunction, the number of
483 clauses are added. Otherwise, neither side is a conjunction and
484 no clauses are created. */
485 if (disjunction_p (lhs
))
487 if ((disjunction_p (rhs
) && d1
&& d2
) || (conjunction_p (rhs
) && d1
))
488 /* Both P and Q are conjunctions. */
489 return std::make_pair (n1
* n2
, true);
490 if ((disjunction_p (rhs
) && d1
!= d2
)
491 || conjunction_p (rhs
)
492 || (atomic_p (rhs
) && d1
))
493 /* Either LHS or RHS is a conjunction. */
494 return std::make_pair (n1
+ n2
, true);
496 /* Neither LHS nor RHS is a conjunction. */
497 return std::make_pair (0, false);
499 if (conjunction_p (lhs
))
501 if ((disjunction_p (rhs
) && d2
) || conjunction_p (rhs
))
502 /* Both LHS and RHS are conjunctions. */
503 return std::make_pair (n1
* n2
, true);
505 /* Only LHS is a conjunction. */
506 return std::make_pair (n1
+ n2
, true);
510 if ((disjunction_p (rhs
) && d2
) || conjunction_p (rhs
))
511 /* Only RHS is a disjunction. */
512 return std::make_pair (n1
+ n2
, true);
514 /* Neither LHS nor RHS is a disjunction. */
515 return std::make_pair (0, false);
518 else /* conjunction_p (t) */
520 /* Matches constraints of the form P /\ Q. Conjunctions contribute
521 linearly to the number of constraints. When both P and Q are
522 conjunctions, clauses are added. When only one of P and Q
523 is a conjunction, an additional clause is produced. When neither
524 P nor Q are conjunctions, two clauses are produced. */
525 if (disjunction_p (lhs
))
527 if ((disjunction_p (rhs
) && d1
&& d2
) || (conjunction_p (rhs
) && d1
))
528 /* Both P and Q are conjunctions. */
529 return std::make_pair (n1
+ n2
, d1
| d2
);
530 if ((disjunction_p (rhs
) && d1
!= d2
)
531 || conjunction_p (rhs
)
532 || (atomic_p (rhs
) && d1
))
533 /* Either LHS or RHS is a conjunction. */
534 return std::make_pair (1 + n1
+ n2
, d1
| d2
);
536 /* Neither LHS nor RHS is a conjunction. */
537 return std::make_pair (2, false);
539 if (conjunction_p (lhs
))
541 if ((disjunction_p (rhs
) && d2
) || conjunction_p (rhs
))
542 /* Both LHS and RHS are conjunctions. */
543 return std::make_pair (n1
+ n2
, d1
| d2
);
545 /* Only LHS is a conjunction. */
546 return std::make_pair (1 + n1
+ n2
, d1
| d2
);
550 if ((disjunction_p (rhs
) && d2
) || conjunction_p (rhs
))
551 /* Only RHS is a disjunction. */
552 return std::make_pair (1 + n1
+ n2
, d1
| d2
);
554 /* Neither LHS nor RHS is a disjunction. */
555 return std::make_pair (2, false);
561 /* Count the number conjunctive clauses that would be created
562 when rewriting T to DNF. */
567 std::pair
<int, bool> result
= dnf_size_r (t
);
568 return result
.first
== 0 ? 1 : result
.first
;
572 /* Count the number disjunctive clauses that would be created
573 when rewriting T to CNF. */
578 std::pair
<int, bool> result
= cnf_size_r (t
);
579 return result
.first
== 0 ? 1 : result
.first
;
583 /* A left-conjunction is replaced by its operands. */
586 replace_term (clause
& c
, tree t
)
588 tree t1
= TREE_OPERAND (t
, 0);
589 tree t2
= TREE_OPERAND (t
, 1);
590 return c
.replace (t1
, t2
);
593 /* Create a new clause in the formula by copying the current
594 clause. In the current clause, the term at CI is replaced
595 by the first operand, and in the new clause, it is replaced
599 branch_clause (formula
& f
, clause
& c1
, tree t
)
601 tree t1
= TREE_OPERAND (t
, 0);
602 tree t2
= TREE_OPERAND (t
, 1);
603 clause
& c2
= f
.branch ();
608 /* Decompose t1 /\ t2 according to the rules R. */
611 decompose_conjuntion (formula
& f
, clause
& c
, tree t
, rules r
)
616 branch_clause (f
, c
, t
);
619 /* Decompose t1 \/ t2 according to the rules R. */
622 decompose_disjunction (formula
& f
, clause
& c
, tree t
, rules r
)
627 branch_clause (f
, c
, t
);
630 /* An atomic constraint is already decomposed. */
632 decompose_atom (clause
& c
)
637 /* Decompose a term of clause C (in formula F) according to the
641 decompose_term (formula
& f
, clause
& c
, tree t
, rules r
)
643 switch (TREE_CODE (t
))
646 return decompose_conjuntion (f
, c
, t
, r
);
648 return decompose_disjunction (f
, c
, t
, r
);
650 return decompose_atom (c
);
654 /* Decompose C (in F) using the logical rules R until it
655 is comprised of only atomic constraints. */
658 decompose_clause (formula
& f
, clause
& c
, rules r
)
661 decompose_term (f
, c
, *c
.current (), r
);
665 static bool derive_proof (clause
&, tree
, rules
);
667 /* Derive a proof of both operands of T. */
670 derive_proof_for_both_operands (clause
& c
, tree t
, rules r
)
672 if (!derive_proof (c
, TREE_OPERAND (t
, 0), r
))
674 return derive_proof (c
, TREE_OPERAND (t
, 1), r
);
677 /* Derive a proof of either operand of T. */
680 derive_proof_for_either_operand (clause
& c
, tree t
, rules r
)
682 if (derive_proof (c
, TREE_OPERAND (t
, 0), r
))
684 return derive_proof (c
, TREE_OPERAND (t
, 1), r
);
687 /* Derive a proof of the atomic constraint T in clause C. */
690 derive_atomic_proof (clause
& c
, tree t
)
692 return c
.contains (t
);
695 /* Derive a proof of T from the terms in C. */
698 derive_proof (clause
& c
, tree t
, rules r
)
700 switch (TREE_CODE (t
))
704 return derive_proof_for_both_operands (c
, t
, r
);
706 return derive_proof_for_either_operand (c
, t
, r
);
709 return derive_proof_for_either_operand (c
, t
, r
);
711 return derive_proof_for_both_operands (c
, t
, r
);
713 return derive_atomic_proof (c
, t
);
717 /* Key/value pair for caching subsumption results. This associates a pair of
718 constraints with a boolean value indicating the result. */
720 struct GTY((for_user
)) subsumption_entry
727 /* Hashing function and equality for constraint entries. */
729 struct subsumption_hasher
: ggc_ptr_hash
<subsumption_entry
>
731 static hashval_t
hash (subsumption_entry
*e
)
734 val
= iterative_hash_constraint (e
->lhs
, val
);
735 val
= iterative_hash_constraint (e
->rhs
, val
);
739 static bool equal (subsumption_entry
*e1
, subsumption_entry
*e2
)
741 if (!constraints_equivalent_p (e1
->lhs
, e2
->lhs
))
743 if (!constraints_equivalent_p (e1
->rhs
, e2
->rhs
))
749 /* Caches the results of subsumes_non_null(t1, t1). */
751 static GTY ((deletable
)) hash_table
<subsumption_hasher
> *subsumption_cache
;
753 /* Search for a previously cached subsumption result. */
756 lookup_subsumption (tree t1
, tree t2
)
758 if (!subsumption_cache
)
760 subsumption_entry elt
= { t1
, t2
, false };
761 subsumption_entry
* found
= subsumption_cache
->find (&elt
);
763 return &found
->result
;
768 /* Save a subsumption result. */
771 save_subsumption (tree t1
, tree t2
, bool result
)
773 if (!subsumption_cache
)
774 subsumption_cache
= hash_table
<subsumption_hasher
>::create_ggc(31);
775 subsumption_entry elt
= {t1
, t2
, result
};
776 subsumption_entry
** slot
= subsumption_cache
->find_slot (&elt
, INSERT
);
777 subsumption_entry
* entry
= ggc_alloc
<subsumption_entry
> ();
784 /* Returns true if the LEFT constraint subsume the RIGHT constraints.
785 This is done by deriving a proof of the conclusions on the RIGHT
786 from the assumptions on the LEFT assumptions. */
789 subsumes_constraints_nonnull (tree lhs
, tree rhs
)
791 auto_timevar
time (TV_CONSTRAINT_SUB
);
793 if (bool *b
= lookup_subsumption(lhs
, rhs
))
798 if (dnf_size (lhs
) <= cnf_size (rhs
))
799 /* When LHS looks simpler than RHS, we'll determine subsumption by
800 decomposing LHS into its disjunctive normal form and checking that
801 each (conjunctive) clause in the decomposed LHS implies RHS. */
802 x
= lhs
, y
= rhs
, r
= left
;
804 /* Otherwise, we'll determine subsumption by decomposing RHS into its
805 conjunctive normal form and checking that each (disjunctive) clause
806 in the decomposed RHS implies LHS. */
807 x
= rhs
, y
= lhs
, r
= right
;
809 /* Decompose X into a list of sequents according to R, and recursively
810 check for implication of Y. */
815 auto i
= f
.current ();
816 decompose_clause (f
, *i
, r
);
817 if (!derive_proof (*i
, y
, r
))
825 return save_subsumption (lhs
, rhs
, result
);
828 /* Returns true if the LEFT constraints subsume the RIGHT
832 subsumes (tree lhs
, tree rhs
)
836 if (!lhs
|| lhs
== error_mark_node
)
838 if (!rhs
|| rhs
== error_mark_node
)
840 return subsumes_constraints_nonnull (lhs
, rhs
);
843 #include "gt-cp-logic.h"