Don't warn when alignment of global common data exceeds maximum alignment.
[official-gcc.git] / gcc / cp / logic.cc
blob9d892b1473bf5988e097f7c478f1892c5a18b292
1 /* Derivation and subsumption rules for constraints.
2 Copyright (C) 2013-2021 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)
10 any later version.
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/>. */
21 #include "config.h"
22 #define INCLUDE_LIST
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "timevar.h"
27 #include "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "stringpool.h"
38 #include "attribs.h"
39 #include "intl.h"
40 #include "flags.h"
41 #include "cp-tree.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"
46 #include "decl.h"
47 #include "toplev.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
54 into CNF or DNF. */
56 struct clause
58 typedef std::list<tree>::iterator iterator;
59 typedef std::list<tree>::const_iterator const_iterator;
61 /* Initialize a clause with an initial term. */
63 clause (tree t)
65 m_terms.push_back (t);
66 if (TREE_CODE (t) == ATOMIC_CONSTR)
67 m_set.add (t);
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. */
84 bool done () const
86 return m_current == end ();
89 /* Advance to the next term. */
91 void advance ()
93 gcc_assert (!done ());
94 ++m_current;
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)
109 if (m_set.add (t))
110 return std::make_pair (m_terms.erase (iter), true);
112 *iter = t;
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)
127 if (m_set.add (t))
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);
150 if (rep.second)
151 m_current = rep.first;
152 else
153 ++rep.first;
155 /* Insert the t2. Make this the current term if we erased
156 the prior term. */
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. */
173 iterator begin ()
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. */
187 iterator end ()
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
203 return m_current;
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. */
216 struct formula
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
222 single clause. */
224 formula (tree t)
226 m_clauses.emplace_back (t);
227 m_current = m_clauses.begin ();
230 /* Returns true when all clauses are atomic. */
231 bool done () const
233 return m_current == end ();
236 /* Advance to the next term. */
237 void advance ()
239 gcc_assert (!done ());
240 ++m_current;
243 /* Insert a copy of clause into the formula. This corresponds
244 to a distribution of one logical operation over the other. */
246 clause& branch ()
248 gcc_assert (!done ());
249 return *m_clauses.insert (std::next (m_current), *m_current);
252 /* Returns the position of the current clause. */
254 iterator current ()
256 return m_current;
259 /* Returns an iterator to the first clause in the formula. */
261 iterator begin ()
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. */
275 iterator end ()
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);
292 m_clauses.erase (i);
295 std::list<clause> m_clauses; /* The list of clauses. */
296 iterator m_current; /* The current clause. */
299 void
300 debug (clause& c)
302 for (clause::iterator i = c.begin(); i != c.end(); ++i)
303 verbatim (" # %E", *i);
306 void
307 debug (formula& f)
309 for (formula::iterator i = f.begin(); i != f.end(); ++i)
311 /* Format punctuators via %s to avoid -Wformat-diag. */
312 verbatim ("%s", "(((");
313 debug (*i);
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). */
322 enum rules
324 left, right
327 /* Distribution counting. */
329 static inline bool
330 disjunction_p (tree t)
332 return TREE_CODE (t) == DISJ_CONSTR;
335 static inline bool
336 conjunction_p (tree t)
338 return TREE_CODE (t) == CONJ_CONSTR;
341 static inline bool
342 atomic_p (tree t)
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>
354 dnf_size_r (tree t)
356 if (atomic_p (t))
357 /* Atomic constraints produce no clauses. */
358 return std::make_pair (0, false);
360 /* For compound constraints, recursively count clauses and unpack
361 the results. */
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);
381 else
382 /* Only LHS is a disjunction. */
383 return std::make_pair (1 + n1 + n2, d1 | d2);
384 gcc_unreachable ();
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);
396 else
397 /* Neither LHS nor RHS is a disjunction. */
398 return std::make_pair (2, false);
400 if (atomic_p (lhs))
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);
405 else
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);
423 else
424 /* Only LHS is a disjunction. */
425 return std::make_pair (n1 + n2, true);
426 gcc_unreachable ();
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);
438 else
439 /* Neither LHS nor RHS is a disjunction. */
440 return std::make_pair (0, false);
442 if (atomic_p (lhs))
444 if (disjunction_p (rhs) || (conjunction_p (rhs) && d2))
445 /* Only RHS is a disjunction. */
446 return std::make_pair (n1 + n2, true);
447 else
448 /* Neither LHS nor RHS is a disjunction. */
449 return std::make_pair (0, false);
452 gcc_unreachable ();
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>
462 cnf_size_r (tree t)
464 if (atomic_p (t))
465 /* Atomic constraints produce no clauses. */
466 return std::make_pair (0, false);
468 /* For compound constraints, recursively count clauses and unpack
469 the results. */
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);
495 else
496 /* Neither LHS nor RHS is a conjunction. */
497 return std::make_pair (0, false);
498 gcc_unreachable ();
500 if (conjunction_p (lhs))
502 if ((disjunction_p (rhs) && d2) || conjunction_p (rhs))
503 /* Both LHS and RHS are conjunctions. */
504 return std::make_pair (n1 * n2, true);
505 else
506 /* Only LHS is a conjunction. */
507 return std::make_pair (n1 + n2, true);
509 if (atomic_p (lhs))
511 if ((disjunction_p (rhs) && d2) || conjunction_p (rhs))
512 /* Only RHS is a disjunction. */
513 return std::make_pair (n1 + n2, true);
514 else
515 /* Neither LHS nor RHS is a disjunction. */
516 return std::make_pair (0, false);
519 else /* conjunction_p (t) */
521 /* Matches constraints of the form P /\ Q. Conjunctions contribute
522 linearly to the number of constraints. When both P and Q are
523 conjunctions, clauses are added. When only one of P and Q
524 is a conjunction, an additional clause is produced. When neither
525 P nor Q are conjunctions, two clauses are produced. */
526 if (disjunction_p (lhs))
528 if ((disjunction_p (rhs) && d1 && d2) || (conjunction_p (rhs) && d1))
529 /* Both P and Q are conjunctions. */
530 return std::make_pair (n1 + n2, d1 | d2);
531 if ((disjunction_p (rhs) && d1 != d2)
532 || conjunction_p (rhs)
533 || (atomic_p (rhs) && d1))
534 /* Either LHS or RHS is a conjunction. */
535 return std::make_pair (1 + n1 + n2, d1 | d2);
536 else
537 /* Neither LHS nor RHS is a conjunction. */
538 return std::make_pair (2, false);
539 gcc_unreachable ();
541 if (conjunction_p (lhs))
543 if ((disjunction_p (rhs) && d2) || conjunction_p (rhs))
544 /* Both LHS and RHS are conjunctions. */
545 return std::make_pair (n1 + n2, d1 | d2);
546 else
547 /* Only LHS is a conjunction. */
548 return std::make_pair (1 + n1 + n2, d1 | d2);
550 if (atomic_p (lhs))
552 if ((disjunction_p (rhs) && d2) || conjunction_p (rhs))
553 /* Only RHS is a disjunction. */
554 return std::make_pair (1 + n1 + n2, d1 | d2);
555 else
556 /* Neither LHS nor RHS is a disjunction. */
557 return std::make_pair (2, false);
560 gcc_unreachable ();
563 /* Count the number conjunctive clauses that would be created
564 when rewriting T to DNF. */
566 static int
567 dnf_size (tree t)
569 std::pair<int, bool> result = dnf_size_r (t);
570 return result.first == 0 ? 1 : result.first;
574 /* Count the number disjunctive clauses that would be created
575 when rewriting T to CNF. */
577 static int
578 cnf_size (tree t)
580 std::pair<int, bool> result = cnf_size_r (t);
581 return result.first == 0 ? 1 : result.first;
585 /* A left-conjunction is replaced by its operands. */
587 void
588 replace_term (clause& c, tree t)
590 tree t1 = TREE_OPERAND (t, 0);
591 tree t2 = TREE_OPERAND (t, 1);
592 return c.replace (t1, t2);
595 /* Create a new clause in the formula by copying the current
596 clause. In the current clause, the term at CI is replaced
597 by the first operand, and in the new clause, it is replaced
598 by the second. */
600 void
601 branch_clause (formula& f, clause& c1, tree t)
603 tree t1 = TREE_OPERAND (t, 0);
604 tree t2 = TREE_OPERAND (t, 1);
605 clause& c2 = f.branch ();
606 c1.replace (t1);
607 c2.replace (t2);
610 /* Decompose t1 /\ t2 according to the rules R. */
612 inline void
613 decompose_conjuntion (formula& f, clause& c, tree t, rules r)
615 if (r == left)
616 replace_term (c, t);
617 else
618 branch_clause (f, c, t);
621 /* Decompose t1 \/ t2 according to the rules R. */
623 inline void
624 decompose_disjunction (formula& f, clause& c, tree t, rules r)
626 if (r == right)
627 replace_term (c, t);
628 else
629 branch_clause (f, c, t);
632 /* An atomic constraint is already decomposed. */
633 inline void
634 decompose_atom (clause& c)
636 c.advance ();
639 /* Decompose a term of clause C (in formula F) according to the
640 logical rules R. */
642 void
643 decompose_term (formula& f, clause& c, tree t, rules r)
645 switch (TREE_CODE (t))
647 case CONJ_CONSTR:
648 return decompose_conjuntion (f, c, t, r);
649 case DISJ_CONSTR:
650 return decompose_disjunction (f, c, t, r);
651 default:
652 return decompose_atom (c);
656 /* Decompose C (in F) using the logical rules R until it
657 is comprised of only atomic constraints. */
659 void
660 decompose_clause (formula& f, clause& c, rules r)
662 while (!c.done ())
663 decompose_term (f, c, *c.current (), r);
664 f.advance ();
667 static bool derive_proof (clause&, tree, rules);
669 /* Derive a proof of both operands of T. */
671 static bool
672 derive_proof_for_both_operands (clause& c, tree t, rules r)
674 if (!derive_proof (c, TREE_OPERAND (t, 0), r))
675 return false;
676 return derive_proof (c, TREE_OPERAND (t, 1), r);
679 /* Derive a proof of either operand of T. */
681 static bool
682 derive_proof_for_either_operand (clause& c, tree t, rules r)
684 if (derive_proof (c, TREE_OPERAND (t, 0), r))
685 return true;
686 return derive_proof (c, TREE_OPERAND (t, 1), r);
689 /* Derive a proof of the atomic constraint T in clause C. */
691 static bool
692 derive_atomic_proof (clause& c, tree t)
694 return c.contains (t);
697 /* Derive a proof of T from the terms in C. */
699 static bool
700 derive_proof (clause& c, tree t, rules r)
702 switch (TREE_CODE (t))
704 case CONJ_CONSTR:
705 if (r == left)
706 return derive_proof_for_both_operands (c, t, r);
707 else
708 return derive_proof_for_either_operand (c, t, r);
709 case DISJ_CONSTR:
710 if (r == left)
711 return derive_proof_for_either_operand (c, t, r);
712 else
713 return derive_proof_for_both_operands (c, t, r);
714 default:
715 return derive_atomic_proof (c, t);
719 /* Key/value pair for caching subsumption results. This associates a pair of
720 constraints with a boolean value indicating the result. */
722 struct GTY((for_user)) subsumption_entry
724 tree lhs;
725 tree rhs;
726 bool result;
729 /* Hashing function and equality for constraint entries. */
731 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
733 static hashval_t hash (subsumption_entry *e)
735 hashval_t val = 0;
736 val = iterative_hash_constraint (e->lhs, val);
737 val = iterative_hash_constraint (e->rhs, val);
738 return val;
741 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
743 if (!constraints_equivalent_p (e1->lhs, e2->lhs))
744 return false;
745 if (!constraints_equivalent_p (e1->rhs, e2->rhs))
746 return false;
747 return true;
751 /* Caches the results of subsumes_non_null(t1, t1). */
753 static GTY ((deletable)) hash_table<subsumption_hasher> *subsumption_cache;
755 /* Search for a previously cached subsumption result. */
757 static bool*
758 lookup_subsumption (tree t1, tree t2)
760 if (!subsumption_cache)
761 return NULL;
762 subsumption_entry elt = { t1, t2, false };
763 subsumption_entry* found = subsumption_cache->find (&elt);
764 if (found)
765 return &found->result;
766 else
767 return 0;
770 /* Save a subsumption result. */
772 static bool
773 save_subsumption (tree t1, tree t2, bool result)
775 if (!subsumption_cache)
776 subsumption_cache = hash_table<subsumption_hasher>::create_ggc(31);
777 subsumption_entry elt = {t1, t2, result};
778 subsumption_entry** slot = subsumption_cache->find_slot (&elt, INSERT);
779 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
780 *entry = elt;
781 *slot = entry;
782 return result;
786 /* Returns true if the LEFT constraint subsume the RIGHT constraints.
787 This is done by deriving a proof of the conclusions on the RIGHT
788 from the assumptions on the LEFT assumptions. */
790 static bool
791 subsumes_constraints_nonnull (tree lhs, tree rhs)
793 auto_timevar time (TV_CONSTRAINT_SUB);
795 if (bool *b = lookup_subsumption(lhs, rhs))
796 return *b;
798 tree x, y;
799 rules r;
800 if (dnf_size (lhs) <= cnf_size (rhs))
801 /* When LHS looks simpler than RHS, we'll determine subsumption by
802 decomposing LHS into its disjunctive normal form and checking that
803 each (conjunctive) clause in the decomposed LHS implies RHS. */
804 x = lhs, y = rhs, r = left;
805 else
806 /* Otherwise, we'll determine subsumption by decomposing RHS into its
807 conjunctive normal form and checking that each (disjunctive) clause
808 in the decomposed RHS implies LHS. */
809 x = rhs, y = lhs, r = right;
811 /* Decompose X into a list of sequents according to R, and recursively
812 check for implication of Y. */
813 bool result = true;
814 formula f (x);
815 while (!f.done ())
817 auto i = f.current ();
818 decompose_clause (f, *i, r);
819 if (!derive_proof (*i, y, r))
821 result = false;
822 break;
824 f.erase (i);
827 return save_subsumption (lhs, rhs, result);
830 /* Returns true if the LEFT constraints subsume the RIGHT
831 constraints. */
833 bool
834 subsumes (tree lhs, tree rhs)
836 if (lhs == rhs)
837 return true;
838 if (!lhs || lhs == error_mark_node)
839 return false;
840 if (!rhs || rhs == error_mark_node)
841 return true;
842 return subsumes_constraints_nonnull (lhs, rhs);
845 #include "gt-cp-logic.h"