Line data Source code
1 : /* Tracking equivalence classes and constraints at a point on an execution path.
2 : Copyright (C) 2019-2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it
8 : 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.
11 :
12 : GCC is distributed in the hope that it will be useful, but
13 : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : General Public License for more details.
16 :
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/>. */
20 :
21 : #include "analyzer/common.h"
22 :
23 : #include "fold-const.h"
24 : #include "ordered-hash-map.h"
25 : #include "cgraph.h"
26 : #include "cfg.h"
27 : #include "digraph.h"
28 : #include "sbitmap.h"
29 : #include "tree-pretty-print.h"
30 :
31 : #include "analyzer/supergraph.h"
32 : #include "analyzer/analyzer-logging.h"
33 : #include "analyzer/call-string.h"
34 : #include "analyzer/program-point.h"
35 : #include "analyzer/store.h"
36 : #include "analyzer/region-model.h"
37 : #include "analyzer/constraint-manager.h"
38 : #include "analyzer/call-summary.h"
39 : #include "analyzer/analyzer-selftests.h"
40 : #include "analyzer/liveness-context.h"
41 :
42 : #if ENABLE_ANALYZER
43 :
44 : namespace ana {
45 :
46 : tristate
47 96878 : compare_constants (tree lhs_const, enum tree_code op, tree rhs_const)
48 : {
49 96878 : tree comparison
50 96878 : = fold_binary (op, boolean_type_node, lhs_const, rhs_const);
51 96878 : if (comparison == boolean_true_node)
52 74800 : return tristate (tristate::TS_TRUE);
53 22078 : if (comparison == boolean_false_node)
54 22078 : return tristate (tristate::TS_FALSE);
55 0 : return tristate (tristate::TS_UNKNOWN);
56 : }
57 :
58 : /* Return true iff CST is below the maximum value for its type. */
59 :
60 : static bool
61 25845 : can_plus_one_p (tree cst)
62 : {
63 25845 : gcc_assert (CONSTANT_CLASS_P (cst));
64 25845 : return tree_int_cst_lt (cst, TYPE_MAX_VALUE (TREE_TYPE (cst)));
65 : }
66 :
67 : /* Return (CST + 1). */
68 :
69 : static tree
70 12957 : plus_one (tree cst)
71 : {
72 12957 : gcc_assert (CONSTANT_CLASS_P (cst));
73 12957 : gcc_assert (can_plus_one_p (cst));
74 12957 : tree result = fold_build2 (PLUS_EXPR, TREE_TYPE (cst),
75 : cst, integer_one_node);
76 12957 : gcc_assert (CONSTANT_CLASS_P (result));
77 12957 : return result;
78 : }
79 :
80 : /* Return true iff CST is above the minimum value for its type. */
81 :
82 : static bool
83 2848 : can_minus_one_p (tree cst)
84 : {
85 2848 : gcc_assert (CONSTANT_CLASS_P (cst));
86 2848 : return tree_int_cst_lt (TYPE_MIN_VALUE (TREE_TYPE (cst)), cst);
87 : }
88 :
89 : /* Return (CST - 1). */
90 :
91 : static tree
92 1439 : minus_one (tree cst)
93 : {
94 1439 : gcc_assert (CONSTANT_CLASS_P (cst));
95 1439 : gcc_assert (can_minus_one_p (cst));
96 1439 : tree result = fold_build2 (MINUS_EXPR, TREE_TYPE (cst),
97 : cst, integer_one_node);
98 1439 : gcc_assert (CONSTANT_CLASS_P (result));
99 1439 : return result;
100 : }
101 :
102 : /* struct bound. */
103 :
104 : /* Ensure that this bound is closed by converting an open bound to a
105 : closed one. */
106 :
107 : void
108 22856 : bound::ensure_closed (enum bound_kind bnd_kind)
109 : {
110 22856 : if (!m_closed)
111 : {
112 : /* Offset by 1 in the appropriate direction.
113 : For example, convert 3 < x into 4 <= x,
114 : and convert x < 5 into x <= 4. */
115 9097 : gcc_assert (CONSTANT_CLASS_P (m_constant));
116 9097 : gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (m_constant)));
117 17657 : m_constant = fold_build2 (bnd_kind == bound_kind::upper ? MINUS_EXPR : PLUS_EXPR,
118 : TREE_TYPE (m_constant),
119 : m_constant, integer_one_node);
120 9097 : gcc_assert (CONSTANT_CLASS_P (m_constant));
121 9097 : gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (m_constant)));
122 9097 : m_closed = true;
123 : }
124 22856 : }
125 :
126 : /* Get "<=" vs "<" for this bound. */
127 :
128 : const char *
129 0 : bound::get_relation_as_str () const
130 : {
131 0 : if (m_closed)
132 : return "<=";
133 : else
134 0 : return "<";
135 : }
136 :
137 : /* struct range. */
138 :
139 : /* Dump this range to PP, which must support %E for tree. */
140 :
141 : void
142 0 : range::dump_to_pp (pretty_printer *pp) const
143 : {
144 0 : if (m_lower_bound.m_constant)
145 : {
146 0 : if (m_upper_bound.m_constant)
147 0 : pp_printf (pp, "%qE %s x %s %qE",
148 0 : m_lower_bound.m_constant,
149 : m_lower_bound.get_relation_as_str (),
150 : m_upper_bound.get_relation_as_str (),
151 : m_upper_bound.m_constant);
152 : else
153 0 : pp_printf (pp, "%qE %s x",
154 0 : m_lower_bound.m_constant,
155 : m_lower_bound.get_relation_as_str ());
156 : }
157 : else
158 : {
159 0 : if (m_upper_bound.m_constant)
160 0 : pp_printf (pp, "x %s %qE",
161 : m_upper_bound.get_relation_as_str (),
162 : m_upper_bound.m_constant);
163 : else
164 0 : pp_string (pp, "x");
165 : }
166 0 : }
167 :
168 : /* Dump this range to stderr. */
169 :
170 : DEBUG_FUNCTION void
171 0 : range::dump () const
172 : {
173 0 : tree_dump_pretty_printer pp (stderr);
174 0 : dump_to_pp (&pp);
175 0 : pp_newline (&pp);
176 0 : }
177 :
178 : /* Determine if there is only one possible value for this range.
179 : If so, return the constant; otherwise, return NULL_TREE. */
180 :
181 : tree
182 14516 : range::constrained_to_single_element ()
183 : {
184 14516 : if (m_lower_bound.m_constant == NULL_TREE
185 4349 : || m_upper_bound.m_constant == NULL_TREE)
186 : return NULL_TREE;
187 :
188 610 : if (!INTEGRAL_TYPE_P (TREE_TYPE (m_lower_bound.m_constant)))
189 : return NULL_TREE;
190 610 : if (!INTEGRAL_TYPE_P (TREE_TYPE (m_upper_bound.m_constant)))
191 : return NULL_TREE;
192 :
193 : /* Convert any open bounds to closed bounds. */
194 610 : m_lower_bound.ensure_closed (bound_kind::lower);
195 610 : m_upper_bound.ensure_closed (bound_kind::upper);
196 :
197 : // Are they equal?
198 610 : tree comparison = fold_binary (EQ_EXPR, boolean_type_node,
199 : m_lower_bound.m_constant,
200 : m_upper_bound.m_constant);
201 610 : if (comparison == boolean_true_node)
202 256 : return m_lower_bound.m_constant;
203 : else
204 : return NULL_TREE;
205 : }
206 :
207 : /* Eval the condition "X OP RHS_CONST" for X within the range. */
208 :
209 : tristate
210 14356 : range::eval_condition (enum tree_code op, tree rhs_const) const
211 : {
212 14356 : range copy (*this);
213 14356 : if (tree single_element = copy.constrained_to_single_element ())
214 160 : return compare_constants (single_element, op, rhs_const);
215 :
216 14196 : switch (op)
217 : {
218 1324 : case EQ_EXPR:
219 1324 : if (below_lower_bound (rhs_const))
220 127 : return tristate (tristate::TS_FALSE);
221 1197 : if (above_upper_bound (rhs_const))
222 16 : return tristate (tristate::TS_FALSE);
223 : break;
224 :
225 3214 : case LT_EXPR:
226 3214 : case LE_EXPR:
227 : /* Qn: "X </<= RHS_CONST". */
228 : /* If RHS_CONST > upper bound, then it's true.
229 : If RHS_CONST < lower bound, then it's false.
230 : Otherwise unknown. */
231 3214 : if (above_upper_bound (rhs_const))
232 195 : return tristate (tristate::TS_TRUE);
233 3019 : if (below_lower_bound (rhs_const))
234 252 : return tristate (tristate::TS_FALSE);
235 : break;
236 :
237 6210 : case NE_EXPR:
238 : /* Qn: "X != RHS_CONST". */
239 : /* If RHS_CONST < lower bound, then it's true.
240 : If RHS_CONST > upper bound, then it's false.
241 : Otherwise unknown. */
242 6210 : if (below_lower_bound (rhs_const))
243 140 : return tristate (tristate::TS_TRUE);
244 6070 : if (above_upper_bound (rhs_const))
245 4 : return tristate (tristate::TS_TRUE);
246 : break;
247 :
248 3448 : case GE_EXPR:
249 3448 : case GT_EXPR:
250 : /* Qn: "X >=/> RHS_CONST". */
251 3448 : if (above_upper_bound (rhs_const))
252 362 : return tristate (tristate::TS_FALSE);
253 3086 : if (below_lower_bound (rhs_const))
254 476 : return tristate (tristate::TS_TRUE);
255 : break;
256 :
257 0 : default:
258 0 : gcc_unreachable ();
259 12624 : break;
260 : }
261 12624 : return tristate (tristate::TS_UNKNOWN);
262 : }
263 :
264 : /* Return true if RHS_CONST is below the lower bound of this range. */
265 :
266 : bool
267 13639 : range::below_lower_bound (tree rhs_const) const
268 : {
269 13639 : if (!m_lower_bound.m_constant)
270 : return false;
271 :
272 4020 : return compare_constants (rhs_const,
273 4020 : m_lower_bound.m_closed ? LT_EXPR : LE_EXPR,
274 4020 : m_lower_bound.m_constant).is_true ();
275 : }
276 :
277 : /* Return true if RHS_CONST is above the upper bound of this range. */
278 :
279 : bool
280 13929 : range::above_upper_bound (tree rhs_const) const
281 : {
282 13929 : if (!m_upper_bound.m_constant)
283 : return false;
284 :
285 1899 : return compare_constants (rhs_const,
286 1899 : m_upper_bound.m_closed ? GT_EXPR : GE_EXPR,
287 1899 : m_upper_bound.m_constant).is_true ();
288 : }
289 :
290 : /* Attempt to add B to the bound of the given kind of this range.
291 : Return true if feasible; false if infeasible. */
292 :
293 : bool
294 14561 : range::add_bound (bound b, enum bound_kind bnd_kind)
295 : {
296 : /* Bail out on floating point constants. */
297 14561 : if (!INTEGRAL_TYPE_P (TREE_TYPE (b.m_constant)))
298 : return true;
299 :
300 14553 : b.ensure_closed (bnd_kind);
301 :
302 14553 : switch (bnd_kind)
303 : {
304 0 : default:
305 0 : gcc_unreachable ();
306 9635 : case bound_kind::lower:
307 : /* Discard redundant bounds. */
308 9635 : if (m_lower_bound.m_constant)
309 : {
310 4493 : m_lower_bound.ensure_closed (bound_kind::lower);
311 4493 : if (tree_int_cst_le (b.m_constant,
312 4493 : m_lower_bound.m_constant))
313 : return true;
314 : }
315 9547 : if (m_upper_bound.m_constant)
316 : {
317 689 : m_upper_bound.ensure_closed (bound_kind::upper);
318 : /* Reject B <= V <= UPPER when B > UPPER. */
319 689 : if (!tree_int_cst_le (b.m_constant,
320 689 : m_upper_bound.m_constant))
321 : return false;
322 : }
323 9501 : m_lower_bound = b;
324 9501 : break;
325 :
326 4918 : case bound_kind::upper:
327 : /* Discard redundant bounds. */
328 4918 : if (m_upper_bound.m_constant)
329 : {
330 386 : m_upper_bound.ensure_closed (bound_kind::upper);
331 386 : if (!tree_int_cst_lt (b.m_constant,
332 386 : m_upper_bound.m_constant))
333 : return true;
334 : }
335 4888 : if (m_lower_bound.m_constant)
336 : {
337 1515 : m_lower_bound.ensure_closed (bound_kind::lower);
338 : /* Reject LOWER <= V <= B when LOWER > B. */
339 1515 : if (!tree_int_cst_le (m_lower_bound.m_constant,
340 1515 : b.m_constant))
341 : return false;
342 : }
343 4840 : m_upper_bound = b;
344 4840 : break;
345 : }
346 :
347 : return true;
348 : }
349 :
350 : /* Attempt to add (RANGE OP RHS_CONST) as a bound to this range.
351 : Return true if feasible; false if infeasible. */
352 :
353 : bool
354 12688 : range::add_bound (enum tree_code op, tree rhs_const)
355 : {
356 12688 : switch (op)
357 : {
358 : default:
359 : return true;
360 291 : case LT_EXPR:
361 : /* "V < RHS_CONST" */
362 291 : return add_bound (bound (rhs_const, false), bound_kind::upper);
363 2508 : case LE_EXPR:
364 : /* "V <= RHS_CONST" */
365 2508 : return add_bound (bound (rhs_const, true), bound_kind::upper);
366 463 : case GE_EXPR:
367 : /* "V >= RHS_CONST" */
368 463 : return add_bound (bound (rhs_const, true), bound_kind::lower);
369 2179 : case GT_EXPR:
370 : /* "V > RHS_CONST" */
371 2179 : return add_bound (bound (rhs_const, false), bound_kind::lower);
372 : }
373 : }
374 :
375 : /* struct bounded_range. */
376 :
377 11080 : bounded_range::bounded_range (const_tree lower, const_tree upper)
378 11080 : : m_lower (const_cast<tree> (lower)),
379 11080 : m_upper (const_cast<tree> (upper))
380 : {
381 11080 : if (lower && upper)
382 : {
383 9626 : gcc_assert (TREE_CODE (m_lower) == INTEGER_CST);
384 9626 : gcc_assert (TREE_CODE (m_upper) == INTEGER_CST);
385 : /* We should have lower <= upper. */
386 9626 : gcc_assert (!tree_int_cst_lt (m_upper, m_lower));
387 : }
388 : else
389 : {
390 : /* Purely for pending on-stack values, for
391 : writing back to. */
392 1454 : gcc_assert (m_lower == NULL_TREE);
393 : gcc_assert (m_lower == NULL_TREE);
394 : }
395 11080 : }
396 :
397 : static void
398 701 : dump_cst (pretty_printer *pp, tree cst, bool show_types)
399 : {
400 701 : gcc_assert (cst);
401 701 : if (show_types)
402 : {
403 21 : pp_character (pp, '(');
404 21 : dump_generic_node (pp, TREE_TYPE (cst), 0, (dump_flags_t)0, false);
405 21 : pp_character (pp, ')');
406 : }
407 701 : dump_generic_node (pp, cst, 0, (dump_flags_t)0, false);
408 701 : }
409 :
410 : /* Dump this object to PP. */
411 :
412 : void
413 414 : bounded_range::dump_to_pp (pretty_printer *pp, bool show_types) const
414 : {
415 414 : if (singleton_p ())
416 127 : dump_cst (pp, m_lower, show_types);
417 : else
418 : {
419 287 : pp_character (pp, '[');
420 287 : dump_cst (pp, m_lower, show_types);
421 287 : pp_string (pp, ", ");
422 287 : dump_cst (pp, m_upper, show_types);
423 287 : pp_character (pp, ']');
424 : }
425 414 : }
426 :
427 : /* Dump this object to stderr. */
428 :
429 : void
430 0 : bounded_range::dump (bool show_types) const
431 : {
432 0 : tree_dump_pretty_printer pp (stderr);
433 0 : dump_to_pp (&pp, show_types);
434 0 : pp_newline (&pp);
435 0 : }
436 :
437 : std::unique_ptr<json::object>
438 0 : bounded_range::to_json () const
439 : {
440 0 : auto range_obj = std::make_unique<json::object> ();
441 0 : set_json_attr (*range_obj, "lower", m_lower);
442 0 : set_json_attr (*range_obj, "upper", m_upper);
443 0 : return range_obj;
444 : }
445 :
446 : std::unique_ptr<text_art::widget>
447 146 : bounded_range::make_dump_widget (const text_art::dump_widget_info &dwi) const
448 : {
449 146 : using text_art::tree_widget;
450 146 : return tree_widget::from_fmt (dwi, default_tree_printer,
451 146 : "%qE ... %qE", m_lower, m_upper);
452 : }
453 :
454 : /* Subroutine of bounded_range::to_json. */
455 :
456 : void
457 0 : bounded_range::set_json_attr (json::object &obj, const char *name, tree value)
458 : {
459 0 : pretty_printer pp;
460 0 : pp_format_decoder (&pp) = default_tree_printer;
461 0 : pp_printf (&pp, "%E", value);
462 0 : obj.set_string (name, pp_formatted_text (&pp));
463 0 : }
464 :
465 :
466 : /* Return true iff CST is within this range. */
467 :
468 : bool
469 367 : bounded_range::contains_p (tree cst) const
470 : {
471 : /* Reject if below lower bound. */
472 367 : if (tree_int_cst_lt (cst, m_lower))
473 : return false;
474 : /* Reject if above lower bound. */
475 310 : if (tree_int_cst_lt (m_upper, cst))
476 161 : return false;
477 : return true;
478 : }
479 :
480 : /* If this range intersects OTHER, return true, writing
481 : the intersection to *OUT if OUT is non-NULL.
482 : Return false if they do not intersect. */
483 :
484 : bool
485 8494 : bounded_range::intersects_p (const bounded_range &other,
486 : bounded_range *out) const
487 : {
488 8494 : const tree max_lower
489 8494 : = (tree_int_cst_le (m_lower, other.m_lower)
490 8494 : ? other.m_lower : m_lower);
491 8494 : gcc_assert (TREE_CODE (max_lower) == INTEGER_CST);
492 8494 : const tree min_upper
493 8494 : = (tree_int_cst_le (m_upper, other.m_upper)
494 8494 : ? m_upper : other.m_upper);
495 8494 : gcc_assert (TREE_CODE (min_upper) == INTEGER_CST);
496 :
497 8494 : if (tree_int_cst_le (max_lower, min_upper))
498 : {
499 713 : if (out)
500 395 : *out = bounded_range (max_lower, min_upper);
501 : return true;
502 : }
503 : else
504 : return false;
505 : }
506 :
507 : bool
508 53237 : bounded_range::operator== (const bounded_range &other) const
509 : {
510 53237 : return (TREE_TYPE (m_lower) == TREE_TYPE (other.m_lower)
511 51497 : && TREE_TYPE (m_upper) == TREE_TYPE (other.m_upper)
512 50661 : && tree_int_cst_equal (m_lower, other.m_lower)
513 63511 : && tree_int_cst_equal (m_upper, other.m_upper));
514 : }
515 :
516 : int
517 159510 : bounded_range::cmp (const bounded_range &br1, const bounded_range &br2)
518 : {
519 159510 : if (int cmp_lower = tree_int_cst_compare (br1.m_lower, br2.m_lower))
520 : return cmp_lower;
521 897 : if (int cmp_upper = tree_int_cst_compare (br1.m_upper, br2.m_upper))
522 : return cmp_upper;
523 683 : if (int cmp_lower_type = cmp_types (TREE_TYPE (br1.m_lower),
524 683 : TREE_TYPE (br2.m_lower)))
525 : return cmp_lower_type;
526 678 : if (int cmp_upper_type = cmp_types (TREE_TYPE (br1.m_upper),
527 678 : TREE_TYPE (br2.m_upper)))
528 : return cmp_upper_type;
529 : return 0;
530 : }
531 :
532 : /* struct bounded_ranges. */
533 :
534 : /* Construct a bounded_ranges instance from a single range. */
535 :
536 6568 : bounded_ranges::bounded_ranges (const bounded_range &range)
537 6568 : : m_ranges (1)
538 : {
539 6568 : m_ranges.quick_push (range);
540 6568 : canonicalize ();
541 6568 : validate ();
542 6568 : }
543 :
544 : /* Construct a bounded_ranges instance from multiple ranges. */
545 :
546 4741 : bounded_ranges::bounded_ranges (const vec<bounded_range> &ranges)
547 9025 : : m_ranges (ranges.length ())
548 : {
549 4741 : m_ranges.safe_splice (ranges);
550 4741 : canonicalize ();
551 4741 : validate ();
552 4741 : }
553 :
554 : /* Construct a bounded_ranges instance for values of LHS for which
555 : (LHS OP RHS_CONST) is true (e.g. "(LHS > 3)". */
556 :
557 818 : bounded_ranges::bounded_ranges (enum tree_code op, tree rhs_const)
558 818 : : m_ranges ()
559 : {
560 818 : gcc_assert (TREE_CODE (rhs_const) == INTEGER_CST);
561 818 : tree type = TREE_TYPE (rhs_const);
562 818 : switch (op)
563 : {
564 0 : default:
565 0 : gcc_unreachable ();
566 538 : case EQ_EXPR:
567 538 : m_ranges.safe_push (bounded_range (rhs_const, rhs_const));
568 538 : break;
569 :
570 124 : case GE_EXPR:
571 124 : m_ranges.safe_push (bounded_range (rhs_const, TYPE_MAX_VALUE (type)));
572 124 : break;
573 :
574 50 : case LE_EXPR:
575 50 : m_ranges.safe_push (bounded_range (TYPE_MIN_VALUE (type), rhs_const));
576 50 : break;
577 :
578 52 : case NE_EXPR:
579 52 : if (tree_int_cst_lt (TYPE_MIN_VALUE (type), rhs_const))
580 88 : m_ranges.safe_push (bounded_range (TYPE_MIN_VALUE (type),
581 88 : minus_one (rhs_const)));
582 52 : if (tree_int_cst_lt (rhs_const, TYPE_MAX_VALUE (type)))
583 44 : m_ranges.safe_push (bounded_range (plus_one (rhs_const),
584 44 : TYPE_MAX_VALUE (type)));
585 : break;
586 38 : case GT_EXPR:
587 38 : if (tree_int_cst_lt (rhs_const, TYPE_MAX_VALUE (type)))
588 30 : m_ranges.safe_push (bounded_range (plus_one (rhs_const),
589 30 : TYPE_MAX_VALUE (type)));
590 : break;
591 16 : case LT_EXPR:
592 16 : if (tree_int_cst_lt (TYPE_MIN_VALUE (type), rhs_const))
593 16 : m_ranges.safe_push (bounded_range (TYPE_MIN_VALUE (type),
594 16 : minus_one (rhs_const)));
595 : break;
596 : }
597 818 : canonicalize ();
598 818 : validate ();
599 818 : }
600 :
601 : /* Subroutine of ctors for fixing up m_ranges.
602 : Also, initialize m_hash. */
603 :
604 : void
605 12127 : bounded_ranges::canonicalize ()
606 : {
607 : /* Sort the ranges. */
608 12127 : m_ranges.qsort ([](const void *p1, const void *p2) -> int
609 : {
610 : const bounded_range &br1 = *(const bounded_range *)p1;
611 : const bounded_range &br2 = *(const bounded_range *)p2;
612 : return bounded_range::cmp (br1, br2);
613 : });
614 :
615 : /* Merge ranges that are touching or overlapping. */
616 19143 : for (unsigned i = 1; i < m_ranges.length (); )
617 : {
618 7016 : bounded_range *prev = &m_ranges[i - 1];
619 7016 : const bounded_range *next = &m_ranges[i];
620 7016 : if (prev->intersects_p (*next, nullptr)
621 7016 : || (can_plus_one_p (prev->m_upper)
622 6698 : && tree_int_cst_equal (plus_one (prev->m_upper),
623 6698 : next->m_lower)))
624 : {
625 2286 : prev->m_upper = next->m_upper;
626 2286 : m_ranges.ordered_remove (i);
627 : }
628 : else
629 4730 : i++;
630 : }
631 :
632 : /* Initialize m_hash. */
633 12127 : inchash::hash hstate (0);
634 51819 : for (const auto &iter : m_ranges)
635 : {
636 16384 : inchash::add_expr (iter.m_lower, hstate);
637 16384 : inchash::add_expr (iter.m_upper, hstate);
638 : }
639 12127 : m_hash = hstate.end ();
640 12127 : }
641 :
642 : /* Assert that this object is valid. */
643 :
644 : void
645 12127 : bounded_ranges::validate () const
646 : {
647 : /* Skip this in a release build. */
648 : #if !CHECKING_P
649 : return;
650 : #endif
651 :
652 16857 : for (unsigned i = 1; i < m_ranges.length (); i++)
653 : {
654 4730 : const bounded_range &prev = m_ranges[i - 1];
655 4730 : const bounded_range &next = m_ranges[i];
656 :
657 : /* Give up if we somehow have incompatible different types. */
658 4730 : if (!types_compatible_p (TREE_TYPE (prev.m_upper),
659 4730 : TREE_TYPE (next.m_lower)))
660 0 : continue;
661 :
662 : /* Verify sorted. */
663 4730 : gcc_assert (tree_int_cst_lt (prev.m_upper, next.m_lower));
664 :
665 4730 : gcc_assert (can_plus_one_p (prev.m_upper));
666 : /* otherwise there's no room for "next". */
667 :
668 : /* Verify no ranges touch each other. */
669 4730 : gcc_assert (tree_int_cst_lt (plus_one (prev.m_upper), next.m_lower));
670 : }
671 12127 : }
672 :
673 : /* bounded_ranges equality operator. */
674 :
675 : bool
676 69342 : bounded_ranges::operator== (const bounded_ranges &other) const
677 : {
678 203871 : if (m_ranges.length () != other.m_ranges.length ())
679 : return false;
680 60977 : for (unsigned i = 0; i < m_ranges.length (); i++)
681 : {
682 53237 : if (m_ranges[i] != other.m_ranges[i])
683 : return false;
684 : }
685 : return true;
686 : }
687 :
688 : /* Dump this object to PP. */
689 :
690 : void
691 314 : bounded_ranges::dump_to_pp (pretty_printer *pp, bool show_types) const
692 : {
693 314 : pp_character (pp, '{');
694 978 : for (unsigned i = 0; i < m_ranges.length (); ++i)
695 : {
696 350 : if (i > 0)
697 68 : pp_string (pp, ", ");
698 350 : m_ranges[i].dump_to_pp (pp, show_types);
699 : }
700 314 : pp_character (pp, '}');
701 314 : }
702 :
703 : /* Dump this object to stderr. */
704 :
705 : DEBUG_FUNCTION void
706 0 : bounded_ranges::dump (bool show_types) const
707 : {
708 0 : tree_dump_pretty_printer pp (stderr);
709 0 : dump_to_pp (&pp, show_types);
710 0 : pp_newline (&pp);
711 0 : }
712 :
713 : std::unique_ptr<json::value>
714 0 : bounded_ranges::to_json () const
715 : {
716 0 : auto arr_obj = std::make_unique<json::array> ();
717 :
718 0 : for (unsigned i = 0; i < m_ranges.length (); ++i)
719 0 : arr_obj->append (m_ranges[i].to_json ());
720 :
721 0 : return arr_obj;
722 0 : }
723 :
724 : void
725 65 : bounded_ranges::add_to_dump_widget (text_art::tree_widget &parent,
726 : const text_art::dump_widget_info &dwi) const
727 : {
728 341 : for (auto &range : m_ranges)
729 292 : parent.add_child (range.make_dump_widget (dwi));
730 65 : }
731 :
732 : /* Determine whether (X OP RHS_CONST) is known to be true or false
733 : for all X in the ranges expressed by this object. */
734 :
735 : tristate
736 722 : bounded_ranges::eval_condition (enum tree_code op,
737 : tree rhs_const,
738 : bounded_ranges_manager *mgr) const
739 : {
740 : /* Convert (X OP RHS_CONST) to a bounded_ranges instance and find
741 : the intersection of that with this object. */
742 722 : bounded_ranges other (op, rhs_const);
743 722 : const bounded_ranges *intersection
744 722 : = mgr->get_or_create_intersection (this, &other);
745 :
746 722 : if (intersection->m_ranges.length () > 0)
747 : {
748 : /* We can use pointer equality to check for equality,
749 : due to instance consolidation. */
750 296 : if (intersection == this)
751 50 : return tristate (tristate::TS_TRUE);
752 : else
753 246 : return tristate (tristate::TS_UNKNOWN);
754 : }
755 : else
756 : /* No intersection. */
757 426 : return tristate (tristate::TS_FALSE);
758 722 : }
759 :
760 : /* Return true if CST is within any of the ranges. */
761 :
762 : bool
763 250 : bounded_ranges::contain_p (tree cst) const
764 : {
765 250 : gcc_assert (TREE_CODE (cst) == INTEGER_CST);
766 904 : for (const auto &iter : m_ranges)
767 : {
768 : /* TODO: should we optimize this based on sorting? */
769 335 : if (iter.contains_p (cst))
770 : return true;
771 : }
772 : return false;
773 : }
774 :
775 : int
776 124 : bounded_ranges::cmp (const bounded_ranges *a, const bounded_ranges *b)
777 : {
778 124 : if (int cmp_length = ((int)a->m_ranges.length ()
779 248 : - (int)b->m_ranges.length ()))
780 : return cmp_length;
781 70 : for (unsigned i = 0; i < a->m_ranges.length (); i++)
782 : {
783 70 : if (int cmp_range = bounded_range::cmp (a->m_ranges[i], b->m_ranges[i]))
784 : return cmp_range;
785 : }
786 : /* They are equal. They ought to have been consolidated, so we should
787 : have two pointers to the same object. */
788 0 : gcc_assert (a == b);
789 : return 0;
790 : }
791 :
792 : /* class bounded_ranges_manager. */
793 :
794 : /* bounded_ranges_manager's dtor. */
795 :
796 4503 : bounded_ranges_manager::~bounded_ranges_manager ()
797 : {
798 : /* Delete the managed objects. */
799 8064 : for (const auto &iter : m_map)
800 3561 : delete iter.second;
801 4503 : }
802 :
803 : /* Get the bounded_ranges instance for the empty set, creating it if
804 : necessary. */
805 :
806 : const bounded_ranges *
807 8 : bounded_ranges_manager::get_or_create_empty ()
808 : {
809 8 : auto_vec<bounded_range> empty_vec;
810 :
811 8 : return consolidate (new bounded_ranges (empty_vec));
812 8 : }
813 :
814 : /* Get the bounded_ranges instance for {CST}, creating it if necessary. */
815 :
816 : const bounded_ranges *
817 5912 : bounded_ranges_manager::get_or_create_point (const_tree cst)
818 : {
819 5912 : gcc_assert (TREE_CODE (cst) == INTEGER_CST);
820 :
821 5912 : return get_or_create_range (cst, cst);
822 : }
823 :
824 : /* Get the bounded_ranges instance for {[LOWER_BOUND..UPPER_BOUND]},
825 : creating it if necessary. */
826 :
827 : const bounded_ranges *
828 6568 : bounded_ranges_manager::get_or_create_range (const_tree lower_bound,
829 : const_tree upper_bound)
830 : {
831 6568 : gcc_assert (TREE_CODE (lower_bound) == INTEGER_CST);
832 6568 : gcc_assert (TREE_CODE (upper_bound) == INTEGER_CST);
833 :
834 6568 : return consolidate
835 6568 : (new bounded_ranges (bounded_range (lower_bound, upper_bound)));
836 : }
837 :
838 : /* Get the bounded_ranges instance for the union of OTHERS,
839 : creating it if necessary. */
840 :
841 : const bounded_ranges *
842 3582 : bounded_ranges_manager::
843 : get_or_create_union (const vec <const bounded_ranges *> &others)
844 : {
845 3582 : auto_vec<bounded_range> ranges;
846 18049 : for (const auto &r : others)
847 7303 : ranges.safe_splice (r->m_ranges);
848 3582 : return consolidate (new bounded_ranges (ranges));
849 3582 : }
850 :
851 : /* Get the bounded_ranges instance for the intersection of A and B,
852 : creating it if necessary. */
853 :
854 : const bounded_ranges *
855 758 : bounded_ranges_manager::get_or_create_intersection (const bounded_ranges *a,
856 : const bounded_ranges *b)
857 : {
858 758 : auto_vec<bounded_range> ranges;
859 758 : unsigned a_idx = 0;
860 758 : unsigned b_idx = 0;
861 758 : while (a_idx < a->m_ranges.length ()
862 2204 : && b_idx < b->m_ranges.length ())
863 : {
864 1446 : const bounded_range &r_a = a->m_ranges[a_idx];
865 1446 : const bounded_range &r_b = b->m_ranges[b_idx];
866 :
867 1446 : bounded_range intersection (NULL_TREE, NULL_TREE);
868 1446 : if (r_a.intersects_p (r_b, &intersection))
869 : {
870 379 : ranges.safe_push (intersection);
871 : }
872 1446 : if (tree_int_cst_lt (r_a.m_lower, r_b.m_lower))
873 : {
874 923 : a_idx++;
875 : }
876 : else
877 : {
878 523 : if (tree_int_cst_lt (r_a.m_upper, r_b.m_upper))
879 36 : a_idx++;
880 : else
881 487 : b_idx++;
882 : }
883 : }
884 :
885 758 : return consolidate (new bounded_ranges (ranges));
886 758 : }
887 :
888 : /* Get the bounded_ranges instance for the inverse of OTHER relative
889 : to TYPE, creating it if necessary.
890 : This is for use when handling "default" in switch statements, where
891 : OTHER represents all the other cases. */
892 :
893 : const bounded_ranges *
894 385 : bounded_ranges_manager::get_or_create_inverse (const bounded_ranges *other,
895 : tree type)
896 : {
897 385 : tree min_val = TYPE_MIN_VALUE (type);
898 385 : tree max_val = TYPE_MAX_VALUE (type);
899 385 : if (other->m_ranges.length () == 0)
900 0 : return get_or_create_range (min_val, max_val);
901 385 : auto_vec<bounded_range> ranges;
902 385 : tree first_lb = other->m_ranges[0].m_lower;
903 385 : if (tree_int_cst_lt (min_val, first_lb)
904 385 : && can_minus_one_p (first_lb))
905 298 : ranges.safe_push (bounded_range (min_val,
906 298 : minus_one (first_lb)));
907 1474 : for (unsigned i = 1; i < other->m_ranges.length (); i++)
908 : {
909 1089 : tree prev_ub = other->m_ranges[i - 1].m_upper;
910 1089 : tree iter_lb = other->m_ranges[i].m_lower;
911 1089 : gcc_assert (tree_int_cst_lt (prev_ub, iter_lb));
912 1089 : if (can_plus_one_p (prev_ub) && can_minus_one_p (iter_lb))
913 2178 : ranges.safe_push (bounded_range (plus_one (prev_ub),
914 2178 : minus_one (iter_lb)));
915 : }
916 385 : tree last_ub
917 770 : = other->m_ranges[other->m_ranges.length () - 1].m_upper;
918 385 : if (tree_int_cst_lt (last_ub, max_val)
919 385 : && can_plus_one_p (last_ub))
920 366 : ranges.safe_push (bounded_range (plus_one (last_ub), max_val));
921 :
922 385 : return consolidate (new bounded_ranges (ranges));
923 385 : }
924 :
925 : /* If an object equal to INST is already present, delete INST and
926 : return the existing object.
927 : Otherwise add INST and return it. */
928 :
929 : const bounded_ranges *
930 11301 : bounded_ranges_manager::consolidate (bounded_ranges *inst)
931 : {
932 11301 : if (bounded_ranges **slot = m_map.get (inst))
933 : {
934 15480 : delete inst;
935 7740 : return *slot;
936 : }
937 3561 : m_map.put (inst, inst);
938 3561 : return inst;
939 : }
940 :
941 : /* Get the bounded_ranges instance for CASE_LABEL within
942 : SWITCH_STMT. */
943 :
944 : const bounded_ranges *
945 6777 : bounded_ranges_manager::
946 : make_case_label_ranges (const gswitch *switch_stmt,
947 : tree case_label)
948 : {
949 6777 : gcc_assert (TREE_CODE (case_label) == CASE_LABEL_EXPR);
950 6777 : tree lower_bound = CASE_LOW (case_label);
951 6777 : tree upper_bound = CASE_HIGH (case_label);
952 6777 : if (lower_bound)
953 : {
954 6440 : if (upper_bound)
955 : /* Range. */
956 584 : return get_or_create_range (lower_bound, upper_bound);
957 : else
958 : /* Single-value. */
959 5856 : return get_or_create_point (lower_bound);
960 : }
961 : else
962 : {
963 : /* The default case.
964 : Add exclusions based on the other cases. */
965 337 : auto_vec <const bounded_ranges *> other_case_ranges
966 337 : (gimple_switch_num_labels (switch_stmt));
967 337 : for (unsigned other_idx = 1;
968 3557 : other_idx < gimple_switch_num_labels (switch_stmt);
969 : other_idx++)
970 : {
971 3220 : tree other_label = gimple_switch_label (switch_stmt,
972 : other_idx);
973 3220 : const bounded_ranges *other_ranges
974 3220 : = make_case_label_ranges (switch_stmt, other_label);
975 3220 : other_case_ranges.quick_push (other_ranges);
976 : }
977 337 : const bounded_ranges *other_cases_ranges
978 337 : = get_or_create_union (other_case_ranges);
979 337 : tree type = TREE_TYPE (gimple_switch_index (switch_stmt));
980 337 : return get_or_create_inverse (other_cases_ranges, type);
981 337 : }
982 : }
983 :
984 : /* Dump the number of objects of each class that were managed by this
985 : manager to LOGGER.
986 : If SHOW_OBJS is true, also dump the objects themselves. */
987 :
988 : void
989 5 : bounded_ranges_manager::log_stats (logger *logger, bool show_objs) const
990 : {
991 5 : LOG_SCOPE (logger);
992 5 : logger->log (" # %s: %li", "ranges", (long)m_map.elements ());
993 5 : if (!show_objs)
994 0 : return;
995 :
996 5 : auto_vec<const bounded_ranges *> vec_objs (m_map.elements ());
997 15 : for (const auto &iter : m_map)
998 10 : vec_objs.quick_push (iter.second);
999 5 : vec_objs.qsort
1000 : ([](const void *p1, const void *p2) -> int
1001 : {
1002 : const bounded_ranges *br1 = *(const bounded_ranges * const *)p1;
1003 : const bounded_ranges *br2 = *(const bounded_ranges * const *)p2;
1004 : return bounded_ranges::cmp (br1, br2);
1005 : });
1006 :
1007 17 : for (const auto &iter : vec_objs)
1008 : {
1009 10 : logger->start_log_line ();
1010 10 : pretty_printer *pp = logger->get_printer ();
1011 10 : pp_string (pp, " ");
1012 10 : iter->dump_to_pp (pp, true);
1013 10 : logger->end_log_line ();
1014 : }
1015 5 : }
1016 :
1017 : /* class equiv_class. */
1018 :
1019 : /* equiv_class's default ctor. */
1020 :
1021 259156 : equiv_class::equiv_class ()
1022 259156 : : m_constant (NULL_TREE), m_cst_sval (nullptr), m_vars ()
1023 : {
1024 259156 : }
1025 :
1026 : /* equiv_class's copy ctor. */
1027 :
1028 8777711 : equiv_class::equiv_class (const equiv_class &other)
1029 8777711 : : m_constant (other.m_constant), m_cst_sval (other.m_cst_sval),
1030 17555422 : m_vars (other.m_vars.length ())
1031 : {
1032 35835326 : for (const svalue *sval : other.m_vars)
1033 9502193 : m_vars.quick_push (sval);
1034 8777711 : }
1035 :
1036 : /* Print an all-on-one-line representation of this equiv_class to PP,
1037 : which must support %E for trees. */
1038 :
1039 : void
1040 1564 : equiv_class::print (pretty_printer *pp) const
1041 : {
1042 1564 : pp_character (pp, '{');
1043 1564 : int i;
1044 1564 : const svalue *sval;
1045 5040 : FOR_EACH_VEC_ELT (m_vars, i, sval)
1046 : {
1047 1912 : if (i > 0)
1048 348 : pp_string (pp, " == ");
1049 1912 : sval->dump_to_pp (pp, true);
1050 : }
1051 1564 : if (m_constant)
1052 : {
1053 782 : if (i > 0)
1054 782 : pp_string (pp, " == ");
1055 782 : pp_printf (pp, "[m_constant]%qE", m_constant);
1056 : }
1057 1564 : pp_character (pp, '}');
1058 1564 : }
1059 :
1060 : /* Return a new json::object of the form
1061 : {"svals" : [str],
1062 : "constant" : optional str}. */
1063 :
1064 : std::unique_ptr<json::object>
1065 19 : equiv_class::to_json () const
1066 : {
1067 19 : auto ec_obj = std::make_unique<json::object> ();
1068 :
1069 19 : auto sval_arr = std::make_unique<json::array> ();
1070 81 : for (const svalue *sval : m_vars)
1071 24 : sval_arr->append (sval->to_json ());
1072 19 : ec_obj->set ("svals", std::move (sval_arr));
1073 :
1074 19 : if (m_constant)
1075 : {
1076 12 : pretty_printer pp;
1077 12 : pp_format_decoder (&pp) = default_tree_printer;
1078 12 : pp_printf (&pp, "%qE", m_constant);
1079 12 : ec_obj->set_string ("constant", pp_formatted_text (&pp));
1080 12 : }
1081 :
1082 38 : return ec_obj;
1083 19 : }
1084 :
1085 : std::unique_ptr<text_art::tree_widget>
1086 1259 : equiv_class::make_dump_widget (symbolic::node_dump_state &nds,
1087 : unsigned id) const
1088 : {
1089 1259 : using text_art::tree_widget;
1090 1259 : std::unique_ptr<tree_widget> ec_widget;
1091 :
1092 1259 : {
1093 1259 : pretty_printer pp;
1094 1259 : pp_string (&pp, "Equivalence class ");
1095 1259 : equiv_class_id (id).print (&pp);
1096 1259 : ec_widget = tree_widget::make (nds, &pp);
1097 1259 : }
1098 :
1099 5182 : for (const svalue *sval : m_vars)
1100 2810 : ec_widget->add_child (sval->make_dump_widget (nds));
1101 :
1102 1259 : if (m_constant)
1103 : {
1104 412 : pretty_printer pp;
1105 412 : pp_format_decoder (&pp) = default_tree_printer;
1106 412 : pp_printf (&pp, "%qE", m_constant);
1107 824 : ec_widget->add_child (tree_widget::make (nds, &pp));
1108 412 : }
1109 :
1110 1259 : return ec_widget;
1111 : }
1112 :
1113 : /* Generate a hash value for this equiv_class.
1114 : This relies on the ordering of m_vars, and so this object needs to
1115 : have been canonicalized for this to be meaningful. */
1116 :
1117 : hashval_t
1118 3529495 : equiv_class::hash () const
1119 : {
1120 3529495 : inchash::hash hstate;
1121 :
1122 3529495 : inchash::add_expr (m_constant, hstate);
1123 14385323 : for (const svalue * sval : m_vars)
1124 3796838 : hstate.add_ptr (sval);
1125 3529495 : return hstate.end ();
1126 : }
1127 :
1128 : /* Equality operator for equiv_class.
1129 : This relies on the ordering of m_vars, and so this object
1130 : and OTHER need to have been canonicalized for this to be
1131 : meaningful. */
1132 :
1133 : bool
1134 1137064 : equiv_class::operator== (const equiv_class &other) const
1135 : {
1136 1137064 : if (m_constant != other.m_constant)
1137 : return false; // TODO: use tree equality here?
1138 :
1139 : /* FIXME: should we compare m_cst_sval? */
1140 :
1141 3410712 : if (m_vars.length () != other.m_vars.length ())
1142 : return false;
1143 :
1144 : int i;
1145 : const svalue *sval;
1146 2359788 : FOR_EACH_VEC_ELT (m_vars, i, sval)
1147 1223013 : if (sval != other.m_vars[i])
1148 : return false;
1149 :
1150 : return true;
1151 : }
1152 :
1153 : /* Add SID to this equiv_class, using CM to check if it's a constant. */
1154 :
1155 : void
1156 285276 : equiv_class::add (const svalue *sval)
1157 : {
1158 285276 : gcc_assert (sval);
1159 285276 : if (tree cst = sval->maybe_get_constant ())
1160 : {
1161 98138 : gcc_assert (CONSTANT_CLASS_P (cst));
1162 : /* FIXME: should we canonicalize which svalue is the constant
1163 : when there are multiple equal constants? */
1164 98138 : m_constant = cst;
1165 98138 : m_cst_sval = sval;
1166 : }
1167 285276 : m_vars.safe_push (sval);
1168 285276 : }
1169 :
1170 : /* Remove SID from this equivalence class.
1171 : Return true if SID was the last var in the equivalence class (suggesting
1172 : a possible leak). */
1173 :
1174 : bool
1175 28825 : equiv_class::del (const svalue *sval)
1176 : {
1177 28825 : gcc_assert (sval);
1178 28825 : gcc_assert (sval != m_cst_sval);
1179 :
1180 : int i;
1181 : const svalue *iv;
1182 29399 : FOR_EACH_VEC_ELT (m_vars, i, iv)
1183 : {
1184 29399 : if (iv == sval)
1185 : {
1186 28825 : m_vars[i] = m_vars[m_vars.length () - 1];
1187 28825 : m_vars.pop ();
1188 28825 : return m_vars.length () == 0;
1189 : }
1190 : }
1191 :
1192 : /* SVAL must be in the class. */
1193 0 : gcc_unreachable ();
1194 : return false;
1195 : }
1196 :
1197 : /* Get a representative member of this class, for handling cases
1198 : where the IDs can change mid-traversal. */
1199 :
1200 : const svalue *
1201 46793726 : equiv_class::get_representative () const
1202 : {
1203 46793726 : gcc_assert (m_vars.length () > 0);
1204 46793726 : return m_vars[0];
1205 : }
1206 :
1207 : /* Sort the svalues within this equiv_class. */
1208 :
1209 : void
1210 2390115 : equiv_class::canonicalize ()
1211 : {
1212 2390115 : m_vars.qsort (svalue::cmp_ptr_ptr);
1213 2390115 : }
1214 :
1215 : /* Return true if this EC contains a variable, false if it merely
1216 : contains constants.
1217 : Subroutine of constraint_manager::canonicalize, for removing
1218 : redundant ECs. */
1219 :
1220 : bool
1221 140103 : equiv_class::contains_non_constant_p () const
1222 : {
1223 140103 : if (m_constant)
1224 : {
1225 414723 : for (auto iter : m_vars)
1226 158373 : if (iter->maybe_get_constant ())
1227 47331 : continue;
1228 : else
1229 : /* We have {non-constant == constant}. */
1230 : return true;
1231 : /* We only have constants. */
1232 : return false;
1233 : }
1234 : else
1235 : /* Return true if we have {non-constant == non-constant}. */
1236 35278 : return m_vars.length () > 1;
1237 : }
1238 :
1239 : /* Get a debug string for C_OP. */
1240 :
1241 : const char *
1242 789 : constraint_op_code (enum constraint_op c_op)
1243 : {
1244 789 : switch (c_op)
1245 : {
1246 0 : default:
1247 0 : gcc_unreachable ();
1248 : case CONSTRAINT_NE: return "!=";
1249 0 : case CONSTRAINT_LT: return "<";
1250 0 : case CONSTRAINT_LE: return "<=";
1251 : }
1252 : }
1253 :
1254 : /* Convert C_OP to an enum tree_code. */
1255 :
1256 : enum tree_code
1257 90856 : constraint_tree_code (enum constraint_op c_op)
1258 : {
1259 90856 : switch (c_op)
1260 : {
1261 0 : default:
1262 0 : gcc_unreachable ();
1263 : case CONSTRAINT_NE: return NE_EXPR;
1264 : case CONSTRAINT_LT: return LT_EXPR;
1265 : case CONSTRAINT_LE: return LE_EXPR;
1266 : }
1267 : }
1268 :
1269 : /* Given "lhs C_OP rhs", determine "lhs T_OP rhs".
1270 :
1271 : For example, given "x < y", then "x > y" is false. */
1272 :
1273 : static tristate
1274 229143 : eval_constraint_op_for_op (enum constraint_op c_op, enum tree_code t_op)
1275 : {
1276 229143 : switch (c_op)
1277 : {
1278 0 : default:
1279 0 : gcc_unreachable ();
1280 196476 : case CONSTRAINT_NE:
1281 196476 : if (t_op == EQ_EXPR)
1282 2208 : return tristate (tristate::TS_FALSE);
1283 194268 : if (t_op == NE_EXPR)
1284 193850 : return tristate (tristate::TS_TRUE);
1285 : break;
1286 15167 : case CONSTRAINT_LT:
1287 15167 : if (t_op == LT_EXPR || t_op == LE_EXPR || t_op == NE_EXPR)
1288 14222 : return tristate (tristate::TS_TRUE);
1289 945 : if (t_op == EQ_EXPR || t_op == GT_EXPR || t_op == GE_EXPR)
1290 945 : return tristate (tristate::TS_FALSE);
1291 : break;
1292 17500 : case CONSTRAINT_LE:
1293 17500 : if (t_op == LE_EXPR)
1294 15281 : return tristate (tristate::TS_TRUE);
1295 2219 : if (t_op == GT_EXPR)
1296 1111 : return tristate (tristate::TS_FALSE);
1297 : break;
1298 : }
1299 1526 : return tristate (tristate::TS_UNKNOWN);
1300 : }
1301 :
1302 : /* class constraint. */
1303 :
1304 : /* Print this constraint to PP (which must support %E for trees),
1305 : using CM to look up equiv_class instances from ids. */
1306 :
1307 : void
1308 782 : constraint::print (pretty_printer *pp, const constraint_manager &cm) const
1309 : {
1310 782 : m_lhs.print (pp);
1311 782 : pp_string (pp, ": ");
1312 782 : m_lhs.get_obj (cm).print (pp);
1313 782 : pp_string (pp, " ");
1314 782 : pp_string (pp, constraint_op_code (m_op));
1315 782 : pp_string (pp, " ");
1316 782 : m_rhs.print (pp);
1317 782 : pp_string (pp, ": ");
1318 782 : m_rhs.get_obj (cm).print (pp);
1319 782 : }
1320 :
1321 : /* Return a new json::object of the form
1322 : {"lhs" : int, the EC index
1323 : "op" : str,
1324 : "rhs" : int, the EC index}. */
1325 :
1326 : std::unique_ptr<json::object>
1327 7 : constraint::to_json () const
1328 : {
1329 7 : auto con_obj = std::make_unique<json::object> ();
1330 :
1331 7 : con_obj->set_integer ("lhs", m_lhs.as_int ());
1332 7 : con_obj->set_string ("op", constraint_op_code (m_op));
1333 7 : con_obj->set_integer ("rhs", m_rhs.as_int ());
1334 :
1335 7 : return con_obj;
1336 : }
1337 :
1338 : std::unique_ptr<text_art::widget>
1339 782 : constraint::make_dump_widget (symbolic::node_dump_state &nds,
1340 : const constraint_manager &cm) const
1341 : {
1342 782 : pretty_printer pp;
1343 782 : pp_format_decoder (&pp) = default_tree_printer;
1344 782 : pp_show_color (&pp) = true;
1345 782 : print (&pp, cm);
1346 782 : return text_art::tree_widget::make (nds, &pp);
1347 782 : }
1348 :
1349 : /* Generate a hash value for this constraint. */
1350 :
1351 : hashval_t
1352 2373989 : constraint::hash () const
1353 : {
1354 2373989 : inchash::hash hstate;
1355 2373989 : hstate.add_int (m_lhs.m_idx);
1356 2373989 : hstate.add_int (m_op);
1357 2373989 : hstate.add_int (m_rhs.m_idx);
1358 2373989 : return hstate.end ();
1359 : }
1360 :
1361 : /* Equality operator for constraints. */
1362 :
1363 : bool
1364 765969 : constraint::operator== (const constraint &other) const
1365 : {
1366 765969 : if (m_lhs != other.m_lhs)
1367 : return false;
1368 765947 : if (m_op != other.m_op)
1369 : return false;
1370 765947 : if (m_rhs != other.m_rhs)
1371 0 : return false;
1372 : return true;
1373 : }
1374 :
1375 : /* Return true if this constraint is implied by OTHER. */
1376 :
1377 : bool
1378 318260 : constraint::implied_by (const constraint &other,
1379 : const constraint_manager &cm) const
1380 : {
1381 318260 : if (m_lhs == other.m_lhs)
1382 17190 : if (tree rhs_const = m_rhs.get_obj (cm).get_any_constant ())
1383 14706 : if (tree other_rhs_const = other.m_rhs.get_obj (cm).get_any_constant ())
1384 13809 : if (m_lhs.get_obj (cm).get_any_constant () == NULL_TREE)
1385 13809 : if (m_op == other.m_op)
1386 12685 : switch (m_op)
1387 : {
1388 : default:
1389 : break;
1390 79 : case CONSTRAINT_LE:
1391 79 : case CONSTRAINT_LT:
1392 79 : if (compare_constants (rhs_const,
1393 : GE_EXPR,
1394 79 : other_rhs_const).is_true ())
1395 79 : return true;
1396 : break;
1397 : }
1398 : return false;
1399 : }
1400 :
1401 : /* class bounded_ranges_constraint. */
1402 :
1403 : void
1404 0 : bounded_ranges_constraint::print (pretty_printer *pp,
1405 : const constraint_manager &cm) const
1406 : {
1407 0 : m_ec_id.print (pp);
1408 0 : pp_string (pp, ": ");
1409 0 : m_ec_id.get_obj (cm).print (pp);
1410 0 : pp_string (pp, ": ");
1411 0 : m_ranges->dump_to_pp (pp, true);
1412 0 : }
1413 :
1414 : std::unique_ptr<json::object>
1415 0 : bounded_ranges_constraint::to_json () const
1416 : {
1417 0 : auto con_obj = std::make_unique<json::object> ();
1418 :
1419 0 : con_obj->set_integer ("ec", m_ec_id.as_int ());
1420 0 : con_obj->set ("ranges", m_ranges->to_json ());
1421 :
1422 0 : return con_obj;
1423 : }
1424 :
1425 : std::unique_ptr<text_art::tree_widget>
1426 65 : bounded_ranges_constraint::
1427 : make_dump_widget (symbolic::node_dump_state &nds) const
1428 : {
1429 65 : using text_art::tree_widget;
1430 65 : std::unique_ptr<tree_widget> brc_widget
1431 : (tree_widget::from_fmt (nds, nullptr,
1432 65 : "ec%i bounded ranges", m_ec_id.as_int ()));
1433 65 : m_ranges->add_to_dump_widget (*brc_widget.get (), nds);
1434 65 : return brc_widget;
1435 : }
1436 :
1437 : bool
1438 4840 : bounded_ranges_constraint::
1439 : operator== (const bounded_ranges_constraint &other) const
1440 : {
1441 4840 : if (m_ec_id != other.m_ec_id)
1442 : return false;
1443 :
1444 : /* We can compare by pointer, since the bounded_ranges_manager
1445 : consolidates instances. */
1446 4840 : return m_ranges == other.m_ranges;
1447 : }
1448 :
1449 : void
1450 14545 : bounded_ranges_constraint::add_to_hash (inchash::hash *hstate) const
1451 : {
1452 14545 : hstate->add_int (m_ec_id.m_idx);
1453 14545 : hstate->merge_hash (m_ranges->get_hash ());
1454 14545 : }
1455 :
1456 : /* class equiv_class_id. */
1457 :
1458 : /* Get the underlying equiv_class for this ID from CM. */
1459 :
1460 : const equiv_class &
1461 1209613 : equiv_class_id::get_obj (const constraint_manager &cm) const
1462 : {
1463 1209613 : return cm.get_equiv_class_by_index (m_idx);
1464 : }
1465 :
1466 : /* Access the underlying equiv_class for this ID from CM. */
1467 :
1468 : equiv_class &
1469 115914 : equiv_class_id::get_obj (constraint_manager &cm) const
1470 : {
1471 115914 : return cm.get_equiv_class_by_index (m_idx);
1472 : }
1473 :
1474 : /* Print this equiv_class_id to PP. */
1475 :
1476 : void
1477 2823 : equiv_class_id::print (pretty_printer *pp) const
1478 : {
1479 2823 : if (null_p ())
1480 0 : pp_printf (pp, "null");
1481 : else
1482 2823 : pp_printf (pp, "ec%i", m_idx);
1483 2823 : }
1484 :
1485 : /* class constraint_manager. */
1486 :
1487 : /* constraint_manager's copy ctor. */
1488 :
1489 2794333 : constraint_manager::constraint_manager (const constraint_manager &other)
1490 2794333 : : m_equiv_classes (other.m_equiv_classes.length ()),
1491 4436930 : m_constraints (other.m_constraints.length ()),
1492 2829493 : m_bounded_ranges_constraints (other.m_bounded_ranges_constraints.length ()),
1493 2794333 : m_mgr (other.m_mgr)
1494 : {
1495 2794333 : int i;
1496 2794333 : equiv_class *ec;
1497 11572044 : FOR_EACH_VEC_ELT (other.m_equiv_classes, i, ec)
1498 8777711 : m_equiv_classes.quick_push (new equiv_class (*ec));
1499 : constraint *c;
1500 8775258 : FOR_EACH_VEC_ELT (other.m_constraints, i, c)
1501 5980925 : m_constraints.quick_push (*c);
1502 2910653 : for (const auto &iter : other.m_bounded_ranges_constraints)
1503 46000 : m_bounded_ranges_constraints.quick_push (iter);
1504 2794333 : }
1505 :
1506 : /* constraint_manager's assignment operator. */
1507 :
1508 : constraint_manager&
1509 0 : constraint_manager::operator= (const constraint_manager &other)
1510 : {
1511 0 : gcc_assert (m_equiv_classes.length () == 0);
1512 0 : gcc_assert (m_constraints.length () == 0);
1513 0 : gcc_assert (m_bounded_ranges_constraints.length () == 0);
1514 :
1515 0 : int i;
1516 0 : equiv_class *ec;
1517 0 : m_equiv_classes.reserve (other.m_equiv_classes.length ());
1518 0 : FOR_EACH_VEC_ELT (other.m_equiv_classes, i, ec)
1519 0 : m_equiv_classes.quick_push (new equiv_class (*ec));
1520 0 : constraint *c;
1521 0 : m_constraints.reserve (other.m_constraints.length ());
1522 0 : FOR_EACH_VEC_ELT (other.m_constraints, i, c)
1523 0 : m_constraints.quick_push (*c);
1524 0 : for (const auto &iter : other.m_bounded_ranges_constraints)
1525 0 : m_bounded_ranges_constraints.quick_push (iter);
1526 :
1527 0 : return *this;
1528 : }
1529 :
1530 : /* Generate a hash value for this constraint_manager. */
1531 :
1532 : hashval_t
1533 1180171 : constraint_manager::hash () const
1534 : {
1535 1180171 : inchash::hash hstate;
1536 1180171 : int i;
1537 1180171 : equiv_class *ec;
1538 1180171 : constraint *c;
1539 :
1540 4709666 : FOR_EACH_VEC_ELT (m_equiv_classes, i, ec)
1541 3529495 : hstate.merge_hash (ec->hash ());
1542 3554160 : FOR_EACH_VEC_ELT (m_constraints, i, c)
1543 2373989 : hstate.merge_hash (c->hash ());
1544 1221116 : for (const auto &iter : m_bounded_ranges_constraints)
1545 14545 : iter.add_to_hash (&hstate);
1546 1180171 : return hstate.end ();
1547 : }
1548 :
1549 : /* Equality operator for constraint_manager. */
1550 :
1551 : bool
1552 386396 : constraint_manager::operator== (const constraint_manager &other) const
1553 : {
1554 875006 : if (m_equiv_classes.length () != other.m_equiv_classes.length ())
1555 : return false;
1556 829657 : if (m_constraints.length () != other.m_constraints.length ())
1557 : return false;
1558 382030 : if (m_bounded_ranges_constraints.length ()
1559 386424 : != other.m_bounded_ranges_constraints.length ())
1560 : return false;
1561 :
1562 : int i;
1563 : equiv_class *ec;
1564 :
1565 1518770 : FOR_EACH_VEC_ELT (m_equiv_classes, i, ec)
1566 1137064 : if (!(*ec == *other.m_equiv_classes[i]))
1567 : return false;
1568 :
1569 : constraint *c;
1570 :
1571 1147653 : FOR_EACH_VEC_ELT (m_constraints, i, c)
1572 765969 : if (!(*c == other.m_constraints[i]))
1573 : return false;
1574 :
1575 386475 : for (unsigned i = 0; i < m_bounded_ranges_constraints.length (); i++)
1576 : {
1577 4840 : if (m_bounded_ranges_constraints[i]
1578 9680 : != other.m_bounded_ranges_constraints[i])
1579 : return false;
1580 : }
1581 :
1582 : return true;
1583 : }
1584 :
1585 : /* Print this constraint_manager to PP (which must support %E for trees). */
1586 :
1587 : void
1588 0 : constraint_manager::print (pretty_printer *pp) const
1589 : {
1590 0 : pp_string (pp, "{");
1591 0 : int i;
1592 0 : equiv_class *ec;
1593 0 : FOR_EACH_VEC_ELT (m_equiv_classes, i, ec)
1594 : {
1595 0 : if (i > 0)
1596 0 : pp_string (pp, ", ");
1597 0 : equiv_class_id (i).print (pp);
1598 0 : pp_string (pp, ": ");
1599 0 : ec->print (pp);
1600 : }
1601 0 : pp_string (pp, " | ");
1602 0 : constraint *c;
1603 0 : FOR_EACH_VEC_ELT (m_constraints, i, c)
1604 : {
1605 0 : if (i > 0)
1606 0 : pp_string (pp, " && ");
1607 0 : c->print (pp, *this);
1608 : }
1609 0 : if (m_bounded_ranges_constraints.length ())
1610 : {
1611 0 : pp_string (pp, " | ");
1612 0 : i = 0;
1613 0 : for (const auto &iter : m_bounded_ranges_constraints)
1614 : {
1615 0 : if (i > 0)
1616 0 : pp_string (pp, " && ");
1617 0 : iter.print (pp, *this);
1618 0 : i++;
1619 : }
1620 : }
1621 0 : pp_printf (pp, "}");
1622 0 : }
1623 :
1624 : /* Dump a representation of this constraint_manager to PP
1625 : (which must support %E for trees). */
1626 :
1627 : void
1628 0 : constraint_manager::dump_to_pp (pretty_printer *pp, bool multiline) const
1629 : {
1630 0 : if (multiline)
1631 0 : pp_string (pp, " ");
1632 0 : pp_string (pp, "equiv classes:");
1633 0 : if (multiline)
1634 0 : pp_newline (pp);
1635 : else
1636 0 : pp_string (pp, " {");
1637 0 : int i;
1638 0 : equiv_class *ec;
1639 0 : FOR_EACH_VEC_ELT (m_equiv_classes, i, ec)
1640 : {
1641 0 : if (multiline)
1642 0 : pp_string (pp, " ");
1643 0 : else if (i > 0)
1644 0 : pp_string (pp, ", ");
1645 0 : equiv_class_id (i).print (pp);
1646 0 : pp_string (pp, ": ");
1647 0 : ec->print (pp);
1648 0 : if (multiline)
1649 0 : pp_newline (pp);
1650 : }
1651 0 : if (multiline)
1652 0 : pp_string (pp, " ");
1653 : else
1654 0 : pp_string (pp, "}");
1655 0 : pp_string (pp, "constraints:");
1656 0 : if (multiline)
1657 0 : pp_newline (pp);
1658 : else
1659 0 : pp_string (pp, "{");
1660 0 : constraint *c;
1661 0 : FOR_EACH_VEC_ELT (m_constraints, i, c)
1662 : {
1663 0 : if (multiline)
1664 0 : pp_string (pp, " ");
1665 0 : pp_printf (pp, "%i: ", i);
1666 0 : c->print (pp, *this);
1667 0 : if (multiline)
1668 0 : pp_newline (pp);
1669 : }
1670 0 : if (!multiline)
1671 0 : pp_string (pp, "}");
1672 0 : if (m_bounded_ranges_constraints.length ())
1673 : {
1674 0 : if (multiline)
1675 0 : pp_string (pp, " ");
1676 0 : pp_string (pp, "ranges:");
1677 0 : if (multiline)
1678 0 : pp_newline (pp);
1679 : else
1680 0 : pp_string (pp, "{");
1681 0 : i = 0;
1682 0 : for (const auto &iter : m_bounded_ranges_constraints)
1683 : {
1684 0 : if (multiline)
1685 0 : pp_string (pp, " ");
1686 0 : else if (i > 0)
1687 0 : pp_string (pp, " && ");
1688 0 : iter.print (pp, *this);
1689 0 : if (multiline)
1690 0 : pp_newline (pp);
1691 0 : i++;
1692 : }
1693 0 : if (!multiline)
1694 0 : pp_string (pp, "}");
1695 : }
1696 0 : }
1697 :
1698 : /* Dump a multiline representation of this constraint_manager to FP. */
1699 :
1700 : void
1701 0 : constraint_manager::dump (FILE *fp) const
1702 : {
1703 0 : tree_dump_pretty_printer pp (fp);
1704 0 : dump_to_pp (&pp, true);
1705 0 : }
1706 :
1707 : /* Dump a multiline representation of this constraint_manager to stderr. */
1708 :
1709 : DEBUG_FUNCTION void
1710 0 : constraint_manager::dump () const
1711 : {
1712 0 : symbolic::dump_node_tree_to_stderr<constraint_manager> (*this);
1713 0 : }
1714 :
1715 : /* Dump a multiline representation of CM to stderr. */
1716 :
1717 : DEBUG_FUNCTION void
1718 0 : debug (const constraint_manager &cm)
1719 : {
1720 0 : cm.dump ();
1721 0 : }
1722 :
1723 : /* Return a new json::object of the form
1724 : {"ecs" : array of objects, one per equiv_class
1725 : "constraints" : array of objects, one per constraint}. */
1726 :
1727 : std::unique_ptr<json::object>
1728 19 : constraint_manager::to_json () const
1729 : {
1730 19 : auto cm_obj = std::make_unique<json::object> ();
1731 :
1732 : /* Equivalence classes. */
1733 19 : {
1734 19 : auto ec_arr = std::make_unique<json::array> ();
1735 62 : for (const equiv_class *ec : m_equiv_classes)
1736 19 : ec_arr->append (ec->to_json ());
1737 19 : cm_obj->set ("ecs", std::move (ec_arr));
1738 19 : }
1739 :
1740 : /* Constraints. */
1741 19 : {
1742 19 : auto con_arr = std::make_unique<json::array> ();
1743 40 : for (const constraint &c : m_constraints)
1744 7 : con_arr->append (c.to_json ());
1745 19 : cm_obj->set ("constraints", std::move (con_arr));
1746 19 : }
1747 :
1748 : /* m_bounded_ranges_constraints. */
1749 19 : {
1750 19 : auto con_arr = std::make_unique<json::array> ();
1751 19 : for (const auto &c : m_bounded_ranges_constraints)
1752 0 : con_arr->append (c.to_json ());
1753 19 : cm_obj->set ("bounded_ranges_constraints", std::move (con_arr));
1754 19 : }
1755 :
1756 19 : return cm_obj;
1757 : }
1758 :
1759 : std::unique_ptr<text_art::tree_widget>
1760 720 : constraint_manager::make_dump_widget (symbolic::node_dump_state &nds) const
1761 : {
1762 720 : using text_art::tree_widget;
1763 720 : std::unique_ptr<tree_widget> cm_widget
1764 720 : (tree_widget::make (nds, "Constraints"));
1765 :
1766 : /* Equivalence classes. */
1767 720 : unsigned i;
1768 720 : equiv_class *ec;
1769 2699 : FOR_EACH_VEC_ELT (m_equiv_classes, i, ec)
1770 2518 : cm_widget->add_child (ec->make_dump_widget (nds, i));
1771 :
1772 : /* Constraints. */
1773 2282 : for (const constraint &c : m_constraints)
1774 1564 : cm_widget->add_child (c.make_dump_widget (nds, *this));
1775 :
1776 : /* m_bounded_ranges_constraints. */
1777 915 : for (const auto &brc : m_bounded_ranges_constraints)
1778 130 : cm_widget->add_child (brc.make_dump_widget (nds));
1779 :
1780 720 : if (cm_widget->get_num_children () == 0)
1781 243 : return nullptr;
1782 :
1783 477 : return cm_widget;
1784 720 : }
1785 :
1786 : /* Attempt to add the constraint LHS OP RHS to this constraint_manager.
1787 : Return true if the constraint could be added (or is already true).
1788 : Return false if the constraint contradicts existing knowledge. */
1789 :
1790 : bool
1791 377318 : constraint_manager::add_constraint (const svalue *lhs,
1792 : enum tree_code op,
1793 : const svalue *rhs)
1794 : {
1795 377318 : lhs = lhs->unwrap_any_unmergeable ();
1796 377318 : rhs = rhs->unwrap_any_unmergeable ();
1797 :
1798 : /* Nothing can be known about unknown/poisoned values. */
1799 377318 : if (!lhs->can_have_associated_state_p ()
1800 377318 : || !rhs->can_have_associated_state_p ())
1801 : /* Not a contradiction. */
1802 : return true;
1803 :
1804 : /* Check the conditions on svalues. */
1805 362276 : {
1806 362276 : tristate t_cond = eval_condition (lhs, op, rhs);
1807 :
1808 : /* If we already have the condition, do nothing. */
1809 362276 : if (t_cond.is_true ())
1810 377318 : return true;
1811 :
1812 : /* Reject a constraint that would contradict existing knowledge, as
1813 : unsatisfiable. */
1814 158690 : if (t_cond.is_false ())
1815 : return false;
1816 : }
1817 :
1818 156888 : equiv_class_id lhs_ec_id = get_or_add_equiv_class (lhs);
1819 156888 : equiv_class_id rhs_ec_id = get_or_add_equiv_class (rhs);
1820 :
1821 : /* Check the stronger conditions on ECs. */
1822 156888 : {
1823 156888 : tristate t = eval_condition (lhs_ec_id, op, rhs_ec_id);
1824 :
1825 : /* Discard constraints that are already known. */
1826 156888 : if (t.is_true ())
1827 377318 : return true;
1828 :
1829 : /* Reject unsatisfiable constraints. */
1830 156888 : if (t.is_false ())
1831 : return false;
1832 : }
1833 :
1834 : /* If adding
1835 : (SVAL + OFFSET) > CST,
1836 : then that can imply:
1837 : SVAL > (CST - OFFSET). */
1838 156888 : if (const binop_svalue *lhs_binop = lhs->dyn_cast_binop_svalue ())
1839 11503 : if (tree rhs_cst = rhs->maybe_get_constant ())
1840 10673 : if (tree offset = lhs_binop->get_arg1 ()->maybe_get_constant ())
1841 5575 : if ((op == GT_EXPR || op == LT_EXPR
1842 5144 : || op == GE_EXPR || op == LE_EXPR)
1843 6843 : && lhs_binop->get_op () == PLUS_EXPR)
1844 : {
1845 1334 : tree offset_of_cst = fold_build2 (MINUS_EXPR, TREE_TYPE (rhs_cst),
1846 : rhs_cst, offset);
1847 1334 : const svalue *implied_lhs = lhs_binop->get_arg0 ();
1848 1334 : enum tree_code implied_op = op;
1849 1334 : const svalue *implied_rhs
1850 1334 : = m_mgr->get_or_create_constant_svalue (offset_of_cst);
1851 1334 : if (!add_constraint (implied_lhs, implied_op, implied_rhs))
1852 : return false;
1853 : /* The above add_constraint could lead to EC merger, so we need
1854 : to refresh the EC IDs. */
1855 1246 : lhs_ec_id = get_or_add_equiv_class (lhs);
1856 1246 : rhs_ec_id = get_or_add_equiv_class (rhs);
1857 : }
1858 :
1859 156800 : add_unknown_constraint (lhs_ec_id, op, rhs_ec_id);
1860 156800 : return true;
1861 : }
1862 :
1863 : /* Attempt to add the constraint LHS_EC_ID OP RHS_EC_ID to this
1864 : constraint_manager.
1865 : Return true if the constraint could be added (or is already true).
1866 : Return false if the constraint contradicts existing knowledge. */
1867 :
1868 : bool
1869 678 : constraint_manager::add_constraint (equiv_class_id lhs_ec_id,
1870 : enum tree_code op,
1871 : equiv_class_id rhs_ec_id)
1872 : {
1873 678 : tristate t = eval_condition (lhs_ec_id, op, rhs_ec_id);
1874 :
1875 : /* Discard constraints that are already known. */
1876 678 : if (t.is_true ())
1877 : return true;
1878 :
1879 : /* Reject unsatisfiable constraints. */
1880 549 : if (t.is_false ())
1881 : return false;
1882 :
1883 549 : add_unknown_constraint (lhs_ec_id, op, rhs_ec_id);
1884 549 : return true;
1885 : }
1886 :
1887 : /* Add the constraint LHS_EC_ID OP RHS_EC_ID to this constraint_manager,
1888 : where the constraint has already been checked for being "unknown". */
1889 :
1890 : void
1891 157349 : constraint_manager::add_unknown_constraint (equiv_class_id lhs_ec_id,
1892 : enum tree_code op,
1893 : equiv_class_id rhs_ec_id)
1894 : {
1895 157349 : gcc_assert (lhs_ec_id != rhs_ec_id);
1896 :
1897 : /* For now, simply accumulate constraints, without attempting any further
1898 : optimization. */
1899 157349 : switch (op)
1900 : {
1901 23908 : case EQ_EXPR:
1902 23908 : {
1903 : /* Merge rhs_ec into lhs_ec. */
1904 23908 : equiv_class &lhs_ec_obj = lhs_ec_id.get_obj (*this);
1905 23908 : const equiv_class &rhs_ec_obj = rhs_ec_id.get_obj (*this);
1906 :
1907 23908 : int i;
1908 23908 : const svalue *sval;
1909 73919 : FOR_EACH_VEC_ELT (rhs_ec_obj.m_vars, i, sval)
1910 26103 : lhs_ec_obj.add (sval);
1911 :
1912 23908 : if (rhs_ec_obj.m_constant)
1913 : {
1914 16337 : lhs_ec_obj.m_constant = rhs_ec_obj.m_constant;
1915 16337 : lhs_ec_obj.m_cst_sval = rhs_ec_obj.m_cst_sval;
1916 : }
1917 :
1918 : /* Drop rhs equivalence class, overwriting it with the
1919 : final ec (which might be the same one). */
1920 47816 : equiv_class_id final_ec_id = m_equiv_classes.length () - 1;
1921 23908 : equiv_class *old_ec = m_equiv_classes[rhs_ec_id.m_idx];
1922 23908 : equiv_class *final_ec = m_equiv_classes.pop ();
1923 23908 : if (final_ec != old_ec)
1924 5013 : m_equiv_classes[rhs_ec_id.m_idx] = final_ec;
1925 47816 : delete old_ec;
1926 23908 : if (lhs_ec_id == final_ec_id)
1927 4947 : lhs_ec_id = rhs_ec_id;
1928 :
1929 : /* Update the constraints. */
1930 23908 : constraint *c;
1931 71829 : FOR_EACH_VEC_ELT (m_constraints, i, c)
1932 : {
1933 : /* Update references to the rhs_ec so that
1934 : they refer to the lhs_ec. */
1935 38847 : if (c->m_lhs == rhs_ec_id)
1936 301 : c->m_lhs = lhs_ec_id;
1937 38847 : if (c->m_rhs == rhs_ec_id)
1938 11099 : c->m_rhs = lhs_ec_id;
1939 :
1940 : /* Renumber all constraints that refer to the final rhs_ec
1941 : to the old rhs_ec, where the old final_ec now lives. */
1942 38847 : if (c->m_lhs == final_ec_id)
1943 90 : c->m_lhs = rhs_ec_id;
1944 38847 : if (c->m_rhs == final_ec_id)
1945 20 : c->m_rhs = rhs_ec_id;
1946 : }
1947 : bounded_ranges_constraint *brc;
1948 24775 : FOR_EACH_VEC_ELT (m_bounded_ranges_constraints, i, brc)
1949 : {
1950 867 : if (brc->m_ec_id == rhs_ec_id)
1951 0 : brc->m_ec_id = lhs_ec_id;
1952 867 : if (brc->m_ec_id == final_ec_id)
1953 3 : brc->m_ec_id = rhs_ec_id;
1954 : }
1955 :
1956 : /* We may now have self-comparisons due to the merger; these
1957 : constraints should be removed. */
1958 : unsigned read_index, write_index;
1959 62755 : VEC_ORDERED_REMOVE_IF (m_constraints, read_index, write_index, c,
1960 : (c->m_lhs == c->m_rhs));
1961 : }
1962 : break;
1963 2676 : case GE_EXPR:
1964 2676 : add_constraint_internal (rhs_ec_id, CONSTRAINT_LE, lhs_ec_id);
1965 2676 : break;
1966 8960 : case LE_EXPR:
1967 8960 : add_constraint_internal (lhs_ec_id, CONSTRAINT_LE, rhs_ec_id);
1968 8960 : break;
1969 108487 : case NE_EXPR:
1970 108487 : add_constraint_internal (lhs_ec_id, CONSTRAINT_NE, rhs_ec_id);
1971 108487 : break;
1972 2851 : case GT_EXPR:
1973 2851 : add_constraint_internal (rhs_ec_id, CONSTRAINT_LT, lhs_ec_id);
1974 2851 : break;
1975 10467 : case LT_EXPR:
1976 10467 : add_constraint_internal (lhs_ec_id, CONSTRAINT_LT, rhs_ec_id);
1977 10467 : break;
1978 : default:
1979 : /* do nothing. */
1980 : break;
1981 : }
1982 157349 : validate ();
1983 157349 : }
1984 :
1985 : /* Subroutine of constraint_manager::add_constraint, for handling all
1986 : operations other than equality (for which equiv classes are merged). */
1987 :
1988 : void
1989 133441 : constraint_manager::add_constraint_internal (equiv_class_id lhs_id,
1990 : enum constraint_op c_op,
1991 : equiv_class_id rhs_id)
1992 : {
1993 219805 : if (m_constraints.length () >= (unsigned)param_analyzer_max_constraints)
1994 129310 : return;
1995 :
1996 133242 : constraint new_c (lhs_id, c_op, rhs_id);
1997 :
1998 : /* Remove existing constraints that would be implied by the
1999 : new constraint. */
2000 133242 : unsigned read_index, write_index;
2001 133242 : constraint *c;
2002 451502 : VEC_ORDERED_REMOVE_IF (m_constraints, read_index, write_index, c,
2003 133242 : (c->implied_by (new_c, *this)));
2004 :
2005 : /* Add the constraint. */
2006 133242 : m_constraints.safe_push (new_c);
2007 :
2008 : /* We don't yet update m_bounded_ranges_constraints here yet. */
2009 :
2010 133242 : if (!flag_analyzer_transitivity)
2011 : return;
2012 :
2013 4229 : if (c_op != CONSTRAINT_NE)
2014 : {
2015 : /* The following can potentially add EQ_EXPR facts, which could lead
2016 : to ECs being merged, which would change the meaning of the EC IDs.
2017 : Hence we need to do this via representatives. */
2018 2529 : const svalue *lhs = lhs_id.get_obj (*this).get_representative ();
2019 2529 : const svalue *rhs = rhs_id.get_obj (*this).get_representative ();
2020 :
2021 : /* We have LHS </<= RHS */
2022 :
2023 : /* Handle transitivity of ordering by adding additional constraints
2024 : based on what we already knew.
2025 :
2026 : So if we have already have:
2027 : (a < b)
2028 : (c < d)
2029 : Then adding:
2030 : (b < c)
2031 : will also add:
2032 : (a < c)
2033 : (b < d)
2034 : We need to recurse to ensure we also add:
2035 : (a < d).
2036 : We call the checked add_constraint to avoid adding constraints
2037 : that are already present. Doing so also ensures termination
2038 : in the case of cycles.
2039 :
2040 : We also check for single-element ranges, adding EQ_EXPR facts
2041 : where we discover them. For example 3 < x < 5 implies
2042 : that x == 4 (if x is an integer). */
2043 15816 : for (unsigned i = 0; i < m_constraints.length (); i++)
2044 : {
2045 9156 : const constraint *other = &m_constraints[i];
2046 9156 : if (other->is_ordering_p ())
2047 : {
2048 : /* Refresh the EC IDs, in case any mergers have happened. */
2049 7025 : lhs_id = get_or_add_equiv_class (lhs);
2050 7025 : rhs_id = get_or_add_equiv_class (rhs);
2051 :
2052 7025 : tree lhs_const = lhs_id.get_obj (*this).m_constant;
2053 7025 : tree rhs_const = rhs_id.get_obj (*this).m_constant;
2054 7025 : tree other_lhs_const
2055 7025 : = other->m_lhs.get_obj (*this).m_constant;
2056 7025 : tree other_rhs_const
2057 7025 : = other->m_rhs.get_obj (*this).m_constant;
2058 :
2059 : /* We have "LHS </<= RHS" and "other.lhs </<= other.rhs". */
2060 :
2061 : /* If we have LHS </<= RHS and RHS </<= LHS, then we have a
2062 : cycle. */
2063 7025 : if (rhs_id == other->m_lhs
2064 7025 : && other->m_rhs == lhs_id)
2065 : {
2066 : /* We must have equality for this to be possible. */
2067 18 : gcc_assert (c_op == CONSTRAINT_LE
2068 : && other->m_op == CONSTRAINT_LE);
2069 18 : add_constraint (lhs_id, EQ_EXPR, rhs_id);
2070 : /* Adding an equality will merge the two ECs and potentially
2071 : reorganize the constraints. Stop iterating. */
2072 18 : return;
2073 : }
2074 : /* Otherwise, check for transitivity. */
2075 7007 : if (rhs_id == other->m_lhs)
2076 : {
2077 : /* With RHS == other.lhs, we have:
2078 : "LHS </<= (RHS, other.lhs) </<= other.rhs"
2079 : and thus this implies "LHS </<= other.rhs". */
2080 :
2081 : /* Do we have a tightly-constrained range? */
2082 174 : if (lhs_const
2083 174 : && !rhs_const
2084 48 : && other_rhs_const)
2085 : {
2086 48 : range r (bound (lhs_const, c_op == CONSTRAINT_LE),
2087 : bound (other_rhs_const,
2088 48 : other->m_op == CONSTRAINT_LE));
2089 48 : if (tree constant = r.constrained_to_single_element ())
2090 : {
2091 20 : const svalue *cst_sval
2092 20 : = m_mgr->get_or_create_constant_svalue (constant);
2093 20 : add_constraint
2094 20 : (rhs_id, EQ_EXPR,
2095 : get_or_add_equiv_class (cst_sval));
2096 20 : return;
2097 : }
2098 : }
2099 :
2100 : /* Otherwise, add the constraint implied by transitivity. */
2101 144 : enum tree_code new_op
2102 10 : = ((c_op == CONSTRAINT_LE && other->m_op == CONSTRAINT_LE)
2103 154 : ? LE_EXPR : LT_EXPR);
2104 154 : add_constraint (lhs_id, new_op, other->m_rhs);
2105 : }
2106 6833 : else if (other->m_rhs == lhs_id)
2107 : {
2108 : /* With other.rhs == LHS, we have:
2109 : "other.lhs </<= (other.rhs, LHS) </<= RHS"
2110 : and thus this implies "other.lhs </<= RHS". */
2111 :
2112 : /* Do we have a tightly-constrained range? */
2113 486 : if (other_lhs_const
2114 486 : && !lhs_const
2115 257 : && rhs_const)
2116 : {
2117 72 : range r (bound (other_lhs_const,
2118 72 : other->m_op == CONSTRAINT_LE),
2119 : bound (rhs_const,
2120 72 : c_op == CONSTRAINT_LE));
2121 72 : if (tree constant = r.constrained_to_single_element ())
2122 : {
2123 60 : const svalue *cst_sval
2124 60 : = m_mgr->get_or_create_constant_svalue (constant);
2125 60 : add_constraint
2126 60 : (lhs_id, EQ_EXPR,
2127 : get_or_add_equiv_class (cst_sval));
2128 60 : return;
2129 : }
2130 : }
2131 :
2132 : /* Otherwise, add the constraint implied by transitivity. */
2133 417 : enum tree_code new_op
2134 394 : = ((c_op == CONSTRAINT_LE && other->m_op == CONSTRAINT_LE)
2135 426 : ? LE_EXPR : LT_EXPR);
2136 426 : add_constraint (other->m_lhs, new_op, rhs_id);
2137 : }
2138 : }
2139 : }
2140 : }
2141 : }
2142 :
2143 : /* Attempt to add the constraint that SVAL is within RANGES to this
2144 : constraint_manager.
2145 :
2146 : Return true if the constraint was successfully added (or is already
2147 : known to be true).
2148 : Return false if the constraint contradicts existing knowledge. */
2149 :
2150 : bool
2151 7889 : constraint_manager::add_bounded_ranges (const svalue *sval,
2152 : const bounded_ranges *ranges)
2153 : {
2154 : /* If RANGES is just a singleton, convert this to adding the constraint:
2155 : "SVAL == {the singleton}". */
2156 7889 : if (ranges->get_count () == 1
2157 14268 : && ranges->get_range (0).singleton_p ())
2158 : {
2159 6117 : tree range_cst = ranges->get_range (0).m_lower;
2160 6117 : const svalue *range_sval
2161 6117 : = m_mgr->get_or_create_constant_svalue (range_cst);
2162 6117 : return add_constraint (sval, EQ_EXPR, range_sval);
2163 : }
2164 :
2165 1772 : sval = sval->unwrap_any_unmergeable ();
2166 :
2167 : /* Nothing can be known about unknown/poisoned values. */
2168 1772 : if (!sval->can_have_associated_state_p ())
2169 : /* Not a contradiction. */
2170 : return true;
2171 :
2172 : /* If SVAL is a constant, then we can look at RANGES directly. */
2173 1696 : if (tree cst = sval->maybe_get_constant ())
2174 : {
2175 : /* If the ranges contain CST, then it's a successful no-op;
2176 : otherwise it's a contradiction. */
2177 114 : return ranges->contain_p (cst);
2178 : }
2179 :
2180 1582 : equiv_class_id ec_id = get_or_add_equiv_class (sval);
2181 :
2182 : /* If the EC has a constant, it's either true or false. */
2183 1582 : const equiv_class &ec = ec_id.get_obj (*this);
2184 1582 : if (tree ec_cst = ec.get_any_constant ())
2185 : {
2186 16 : if (ranges->contain_p (ec_cst))
2187 : /* We already have SVAL == EC_CST, within RANGES, so
2188 : we can discard RANGES and succeed. */
2189 : return true;
2190 : else
2191 : /* We already have SVAL == EC_CST, not within RANGES, so
2192 : we can reject RANGES as a contradiction. */
2193 : return false;
2194 : }
2195 :
2196 : /* We have at most one per ec_id. */
2197 : /* Iterate through each range in RANGES. */
2198 2637 : for (auto iter : m_bounded_ranges_constraints)
2199 : {
2200 497 : if (iter.m_ec_id == ec_id)
2201 : {
2202 : /* Update with intersection, or fail if empty. */
2203 28 : bounded_ranges_manager *mgr = get_range_manager ();
2204 28 : const bounded_ranges *intersection
2205 28 : = mgr->get_or_create_intersection (iter.m_ranges, ranges);
2206 28 : if (intersection->empty_p ())
2207 : {
2208 : /* No intersection; fail. */
2209 7889 : return false;
2210 : }
2211 : else
2212 : {
2213 : /* Update with intersection; succeed. */
2214 25 : iter.m_ranges = intersection;
2215 25 : validate ();
2216 25 : return true;
2217 : }
2218 : }
2219 : }
2220 1538 : m_bounded_ranges_constraints.safe_push
2221 1538 : (bounded_ranges_constraint (ec_id, ranges));
2222 :
2223 1538 : validate ();
2224 :
2225 1538 : return true;
2226 : }
2227 :
2228 : /* Look for SVAL within the equivalence classes of this constraint_manager;
2229 : if found, return true, writing the id to *OUT if OUT is non-NULL,
2230 : otherwise return false. */
2231 :
2232 : bool
2233 1557637 : constraint_manager::get_equiv_class_by_svalue (const svalue *sval,
2234 : equiv_class_id *out) const
2235 : {
2236 : /* TODO: should we have a map, rather than these searches? */
2237 1557637 : int i;
2238 1557637 : equiv_class *ec;
2239 11515980 : FOR_EACH_VEC_ELT (m_equiv_classes, i, ec)
2240 : {
2241 : int j;
2242 : const svalue *iv;
2243 15162394 : FOR_EACH_VEC_ELT (ec->m_vars, j, iv)
2244 5985596 : if (iv == sval)
2245 : {
2246 781545 : if (out)
2247 779583 : *out = equiv_class_id (i);
2248 1557637 : return true;
2249 : }
2250 : }
2251 : return false;
2252 : }
2253 :
2254 : /* Look for an equivalence class with an INTEGER_CST constant that
2255 : contains an integral-typed view of BV, for example the "long" view
2256 : of the bits when we're asking about the "unsigned int" view.
2257 : Return that constant, or NULL_TREE if there isn't one. */
2258 :
2259 : tree
2260 13 : constraint_manager::
2261 : get_any_constant_for_bit_vector (const symbolic::bit_vector &bv) const
2262 : {
2263 13 : int i;
2264 13 : equiv_class *ec;
2265 20 : FOR_EACH_VEC_ELT (m_equiv_classes, i, ec)
2266 : {
2267 11 : tree cst = ec->get_any_constant ();
2268 11 : if (!cst || TREE_CODE (cst) != INTEGER_CST)
2269 5 : continue;
2270 :
2271 : int j;
2272 : const svalue *iv;
2273 15 : FOR_EACH_VEC_ELT (ec->m_vars, j, iv)
2274 6 : if (auto *bv_sval = iv->dyn_cast_svalue_from_bit_vector ())
2275 4 : if (&bv_sval->get_bit_vector () == &bv
2276 4 : && bv_sval->get_type ()
2277 8 : && INTEGRAL_TYPE_P (bv_sval->get_type ()))
2278 13 : return cst;
2279 : }
2280 : return NULL_TREE;
2281 : }
2282 :
2283 : /* Tries to find a svalue inside another svalue. */
2284 :
2285 : class sval_finder : public visitor
2286 : {
2287 : public:
2288 228 : sval_finder (const svalue *query) : m_query (query), m_found (false)
2289 : {
2290 : }
2291 :
2292 475 : bool found_query_p ()
2293 : {
2294 475 : return m_found;
2295 : }
2296 :
2297 135 : void visit_pointer_svalue (const pointer_svalue *sval)
2298 : {
2299 135 : m_found |= m_query == sval;
2300 135 : }
2301 :
2302 178 : void visit_constant_svalue (const constant_svalue *sval)
2303 : {
2304 178 : m_found |= m_query == sval;
2305 178 : }
2306 :
2307 0 : void visit_unknown_svalue (const unknown_svalue *sval)
2308 : {
2309 0 : m_found |= m_query == sval;
2310 0 : }
2311 :
2312 0 : void visit_poisoned_svalue (const poisoned_svalue *sval)
2313 : {
2314 0 : m_found |= m_query == sval;
2315 0 : }
2316 :
2317 0 : void visit_setjmp_svalue (const setjmp_svalue *sval)
2318 : {
2319 0 : m_found |= m_query == sval;
2320 0 : }
2321 :
2322 210 : void visit_initial_svalue (const initial_svalue *sval)
2323 : {
2324 210 : m_found |= m_query == sval;
2325 210 : }
2326 :
2327 6 : void visit_unaryop_svalue (const unaryop_svalue *sval)
2328 : {
2329 6 : m_found |= m_query == sval;
2330 6 : }
2331 :
2332 68 : void visit_binop_svalue (const binop_svalue *sval)
2333 : {
2334 68 : m_found |= m_query == sval;
2335 68 : }
2336 :
2337 0 : void visit_unmergeable_svalue (const unmergeable_svalue *sval)
2338 : {
2339 0 : m_found |= m_query == sval;
2340 0 : }
2341 :
2342 0 : void visit_placeholder_svalue (const placeholder_svalue *sval)
2343 : {
2344 0 : m_found |= m_query == sval;
2345 0 : }
2346 :
2347 0 : void visit_widening_svalue (const widening_svalue *sval)
2348 : {
2349 0 : m_found |= m_query == sval;
2350 0 : }
2351 :
2352 0 : void visit_asm_output_svalue (const asm_output_svalue *sval)
2353 : {
2354 0 : m_found |= m_query == sval;
2355 0 : }
2356 :
2357 0 : void visit_const_fn_result_svalue (const const_fn_result_svalue *sval)
2358 : {
2359 0 : m_found |= m_query == sval;
2360 0 : }
2361 :
2362 : private:
2363 : const svalue *m_query;
2364 : bool m_found;
2365 : };
2366 :
2367 : /* Returns true if SVAL is constrained. */
2368 :
2369 : bool
2370 228 : constraint_manager::sval_constrained_p (const svalue *sval) const
2371 : {
2372 228 : int i;
2373 228 : equiv_class *ec;
2374 228 : sval_finder finder (sval);
2375 1092 : FOR_EACH_VEC_ELT (m_equiv_classes, i, ec)
2376 : {
2377 : int j;
2378 : const svalue *iv;
2379 1285 : FOR_EACH_VEC_ELT (ec->m_vars, j, iv)
2380 : {
2381 475 : iv->accept (&finder);
2382 475 : if (finder.found_query_p ())
2383 228 : return true;
2384 : }
2385 : }
2386 : return false;
2387 : }
2388 :
2389 : /* Ensure that SVAL has an equivalence class within this constraint_manager;
2390 : return the ID of the class. */
2391 :
2392 : equiv_class_id
2393 365258 : constraint_manager::get_or_add_equiv_class (const svalue *sval)
2394 : {
2395 365258 : equiv_class_id result (-1);
2396 :
2397 365258 : gcc_assert (sval->can_have_associated_state_p ());
2398 :
2399 : /* Convert all NULL pointers to (void *) to avoid state explosions
2400 : involving all of the various (foo *)NULL vs (bar *)NULL. */
2401 365258 : if (sval->get_type ())
2402 365214 : if (POINTER_TYPE_P (sval->get_type ()))
2403 224346 : if (tree cst = sval->maybe_get_constant ())
2404 96303 : if (zerop (cst))
2405 96303 : sval = m_mgr->get_or_create_constant_svalue (null_pointer_node);
2406 :
2407 : /* Try svalue match. */
2408 365258 : if (get_equiv_class_by_svalue (sval, &result))
2409 106085 : return result;
2410 :
2411 : /* Try equality of constants. */
2412 259173 : if (tree cst = sval->maybe_get_constant ())
2413 : {
2414 : int i;
2415 : equiv_class *ec;
2416 282216 : FOR_EACH_VEC_ELT (m_equiv_classes, i, ec)
2417 200440 : if (ec->m_constant
2418 258791 : && types_compatible_p (TREE_TYPE (cst),
2419 58351 : TREE_TYPE (ec->m_constant)))
2420 : {
2421 28223 : tree eq = fold_binary (EQ_EXPR, boolean_type_node,
2422 : cst, ec->m_constant);
2423 28223 : if (eq == boolean_true_node)
2424 : {
2425 17 : ec->add (sval);
2426 17 : return equiv_class_id (i);
2427 : }
2428 : }
2429 : }
2430 :
2431 :
2432 : /* Not found. */
2433 259156 : equiv_class *new_ec = new equiv_class ();
2434 259156 : new_ec->add (sval);
2435 259156 : m_equiv_classes.safe_push (new_ec);
2436 :
2437 518312 : equiv_class_id new_id (m_equiv_classes.length () - 1);
2438 :
2439 259156 : return new_id;
2440 : }
2441 :
2442 : /* Evaluate the condition LHS_EC OP RHS_EC. */
2443 :
2444 : tristate
2445 419572 : constraint_manager::eval_condition (equiv_class_id lhs_ec,
2446 : enum tree_code op,
2447 : equiv_class_id rhs_ec) const
2448 : {
2449 419572 : if (lhs_ec == rhs_ec)
2450 : {
2451 30300 : switch (op)
2452 : {
2453 27101 : case EQ_EXPR:
2454 27101 : case GE_EXPR:
2455 27101 : case LE_EXPR:
2456 27101 : return tristate (tristate::TS_TRUE);
2457 :
2458 3199 : case NE_EXPR:
2459 3199 : case GT_EXPR:
2460 3199 : case LT_EXPR:
2461 3199 : return tristate (tristate::TS_FALSE);
2462 : default:
2463 : break;
2464 : }
2465 : }
2466 :
2467 389272 : tree lhs_const = lhs_ec.get_obj (*this).get_any_constant ();
2468 389272 : tree rhs_const = rhs_ec.get_obj (*this).get_any_constant ();
2469 389272 : if (lhs_const && rhs_const)
2470 : {
2471 1335 : tristate result_for_constants
2472 1335 : = compare_constants (lhs_const, op, rhs_const);
2473 1335 : if (result_for_constants.is_known ())
2474 1335 : return result_for_constants;
2475 : }
2476 :
2477 387937 : enum tree_code swapped_op = swap_tree_comparison (op);
2478 :
2479 387937 : int i;
2480 387937 : constraint *c;
2481 1694510 : FOR_EACH_VEC_ELT (m_constraints, i, c)
2482 : {
2483 1146253 : if (c->m_lhs == lhs_ec
2484 1146253 : && c->m_rhs == rhs_ec)
2485 : {
2486 224995 : tristate result_for_constraint
2487 224995 : = eval_constraint_op_for_op (c->m_op, op);
2488 224995 : if (result_for_constraint.is_known ())
2489 223940 : return result_for_constraint;
2490 : }
2491 : /* Swapped operands. */
2492 922313 : if (c->m_lhs == rhs_ec
2493 922313 : && c->m_rhs == lhs_ec)
2494 : {
2495 4148 : tristate result_for_constraint
2496 4148 : = eval_constraint_op_for_op (c->m_op, swapped_op);
2497 4148 : if (result_for_constraint.is_known ())
2498 3677 : return result_for_constraint;
2499 : }
2500 : }
2501 :
2502 : /* We don't use m_bounded_ranges_constraints here yet. */
2503 :
2504 160320 : return tristate (tristate::TS_UNKNOWN);
2505 : }
2506 :
2507 : range
2508 14356 : constraint_manager::get_ec_bounds (equiv_class_id ec_id) const
2509 : {
2510 14356 : range result;
2511 :
2512 14356 : int i;
2513 14356 : constraint *c;
2514 80631 : FOR_EACH_VEC_ELT (m_constraints, i, c)
2515 : {
2516 66275 : if (c->m_lhs == ec_id)
2517 : {
2518 20933 : if (tree other_cst = c->m_rhs.get_obj (*this).get_any_constant ())
2519 18760 : switch (c->m_op)
2520 : {
2521 0 : default:
2522 0 : gcc_unreachable ();
2523 16637 : case CONSTRAINT_NE:
2524 16637 : continue;
2525 :
2526 226 : case CONSTRAINT_LT:
2527 : /* We have "EC_ID < OTHER_CST". */
2528 226 : result.add_bound (bound (other_cst, false), bound_kind::upper);
2529 226 : break;
2530 :
2531 1897 : case CONSTRAINT_LE:
2532 : /* We have "EC_ID <= OTHER_CST". */
2533 1897 : result.add_bound (bound (other_cst, true), bound_kind::upper);
2534 1897 : break;
2535 : }
2536 : }
2537 49638 : if (c->m_rhs == ec_id)
2538 : {
2539 7879 : if (tree other_cst = c->m_lhs.get_obj (*this).get_any_constant ())
2540 7021 : switch (c->m_op)
2541 : {
2542 0 : default:
2543 0 : gcc_unreachable ();
2544 24 : case CONSTRAINT_NE:
2545 24 : continue;
2546 :
2547 6269 : case CONSTRAINT_LT:
2548 : /* We have "OTHER_CST < EC_ID"
2549 : i.e. "EC_ID > OTHER_CST". */
2550 6269 : result.add_bound (bound (other_cst, false), bound_kind::lower);
2551 6269 : break;
2552 :
2553 728 : case CONSTRAINT_LE:
2554 : /* We have "OTHER_CST <= EC_ID"
2555 : i.e. "EC_ID >= OTHER_CST". */
2556 728 : result.add_bound (bound (other_cst, true), bound_kind::lower);
2557 728 : break;
2558 : }
2559 : }
2560 : }
2561 :
2562 14356 : return result;
2563 : }
2564 :
2565 : /* Evaluate the condition LHS_EC OP RHS_CONST, avoiding the creation
2566 : of equiv_class instances. */
2567 :
2568 : tristate
2569 85633 : constraint_manager::eval_condition (equiv_class_id lhs_ec,
2570 : enum tree_code op,
2571 : tree rhs_const) const
2572 : {
2573 85633 : gcc_assert (!lhs_ec.null_p ());
2574 85633 : gcc_assert (CONSTANT_CLASS_P (rhs_const));
2575 :
2576 85633 : if (tree lhs_const = lhs_ec.get_obj (*this).get_any_constant ())
2577 1249 : return compare_constants (lhs_const, op, rhs_const);
2578 :
2579 : /* Check for known inequalities of the form
2580 : (LHS_EC != OTHER_CST) or (OTHER_CST != LHS_EC).
2581 : If RHS_CONST == OTHER_CST, then we also know that LHS_EC != OTHER_CST.
2582 : For example, we might have the constraint
2583 : ptr != (void *)0
2584 : so we want the condition
2585 : ptr == (foo *)0
2586 : to be false. */
2587 : int i;
2588 : constraint *c;
2589 251926 : FOR_EACH_VEC_ELT (m_constraints, i, c)
2590 : {
2591 236848 : if (c->m_op == CONSTRAINT_NE)
2592 : {
2593 208604 : if (c->m_lhs == lhs_ec)
2594 : {
2595 86335 : if (tree other_cst = c->m_rhs.get_obj (*this).get_any_constant ())
2596 85866 : if (compare_constants
2597 85866 : (rhs_const, EQ_EXPR, other_cst).is_true ())
2598 : {
2599 69372 : switch (op)
2600 : {
2601 1434 : case EQ_EXPR:
2602 1434 : return tristate (tristate::TS_FALSE);
2603 67795 : case NE_EXPR:
2604 67795 : return tristate (tristate::TS_TRUE);
2605 : default:
2606 : break;
2607 : }
2608 : }
2609 : }
2610 139375 : if (c->m_rhs == lhs_ec)
2611 : {
2612 545 : if (tree other_cst = c->m_lhs.get_obj (*this).get_any_constant ())
2613 101 : if (compare_constants
2614 101 : (rhs_const, EQ_EXPR, other_cst).is_true ())
2615 : {
2616 81 : switch (op)
2617 : {
2618 0 : case EQ_EXPR:
2619 0 : return tristate (tristate::TS_FALSE);
2620 77 : case NE_EXPR:
2621 77 : return tristate (tristate::TS_TRUE);
2622 : default:
2623 : break;
2624 : }
2625 : }
2626 : }
2627 : }
2628 : }
2629 :
2630 15078 : bounded_ranges_manager *mgr = get_range_manager ();
2631 16546 : for (const auto &iter : m_bounded_ranges_constraints)
2632 730 : if (iter.m_ec_id == lhs_ec)
2633 722 : return iter.m_ranges->eval_condition (op, rhs_const, mgr);
2634 :
2635 : /* Look at existing bounds on LHS_EC. */
2636 14356 : range lhs_bounds = get_ec_bounds (lhs_ec);
2637 14356 : tristate result = lhs_bounds.eval_condition (op, rhs_const);
2638 14356 : if (result.is_known ())
2639 1732 : return result;
2640 :
2641 : /* Also reject if range::add_bound fails. */
2642 12624 : if (!lhs_bounds.add_bound (op, rhs_const))
2643 78 : return tristate (false);
2644 :
2645 12546 : return tristate::unknown ();
2646 : }
2647 :
2648 : /* Return true iff "LHS == RHS" is known to be impossible due to
2649 : derived conditions.
2650 :
2651 : Look for an EC containing an EC_VAL of the form (LHS OP CST).
2652 : If found, see if (LHS OP CST) == EC_VAL is false.
2653 : If so, we know this condition is false.
2654 :
2655 : For example, if we already know that
2656 : (X & CST_MASK) == Y
2657 : and we're evaluating X == Z, we can test to see if
2658 : (Z & CST_MASK) == EC_VAL
2659 : and thus if:
2660 : (Z & CST_MASK) == Y
2661 : and reject this if we know that's false. */
2662 :
2663 : bool
2664 52597 : constraint_manager::impossible_derived_conditions_p (const svalue *lhs,
2665 : const svalue *rhs) const
2666 : {
2667 52597 : int i;
2668 52597 : equiv_class *ec;
2669 207831 : FOR_EACH_VEC_ELT (m_equiv_classes, i, ec)
2670 : {
2671 644787 : for (const svalue *ec_sval : ec->m_vars)
2672 178839 : switch (ec_sval->get_kind ())
2673 : {
2674 : default:
2675 : break;
2676 16498 : case SK_BINOP:
2677 16498 : {
2678 16498 : const binop_svalue *iter_binop
2679 16498 : = as_a <const binop_svalue *> (ec_sval);
2680 16498 : if (lhs == iter_binop->get_arg0 ()
2681 16498 : && iter_binop->get_type ())
2682 545 : if (iter_binop->get_arg1 ()->get_kind () == SK_CONSTANT)
2683 : {
2684 : /* Try evaluating EC_SVAL with LHS
2685 : as the value of EC_SVAL's lhs, and see if it's
2686 : consistent with existing knowledge. */
2687 305 : const svalue *subst_bin_op
2688 305 : = m_mgr->get_or_create_binop
2689 305 : (iter_binop->get_type (),
2690 : iter_binop->get_op (),
2691 : rhs,
2692 : iter_binop->get_arg1 ());
2693 305 : tristate t = eval_condition (subst_bin_op,
2694 : EQ_EXPR,
2695 : ec_sval);
2696 305 : if (t.is_false ())
2697 123 : return true; /* Impossible. */
2698 : }
2699 : }
2700 : break;
2701 : }
2702 : }
2703 : /* Not known to be impossible. */
2704 : return false;
2705 : }
2706 :
2707 :
2708 : /* Evaluate the condition LHS OP RHS, without modifying this
2709 : constraint_manager (avoiding the creation of equiv_class instances). */
2710 :
2711 : tristate
2712 757986 : constraint_manager::eval_condition (const svalue *lhs,
2713 : enum tree_code op,
2714 : const svalue *rhs) const
2715 : {
2716 757986 : lhs = lhs->unwrap_any_unmergeable ();
2717 757986 : rhs = rhs->unwrap_any_unmergeable ();
2718 :
2719 : /* Nothing can be known about unknown or poisoned values. */
2720 757986 : if (lhs->get_kind () == SK_UNKNOWN
2721 716038 : || lhs->get_kind () == SK_POISONED
2722 716038 : || rhs->get_kind () == SK_UNKNOWN
2723 1459310 : || rhs->get_kind () == SK_POISONED)
2724 56662 : return tristate (tristate::TS_UNKNOWN);
2725 :
2726 701324 : if (lhs == rhs
2727 701324 : && !(FLOAT_TYPE_P (lhs->get_type ())
2728 111091 : || FLOAT_TYPE_P (rhs->get_type ())))
2729 : {
2730 111091 : switch (op)
2731 : {
2732 110563 : case EQ_EXPR:
2733 110563 : case GE_EXPR:
2734 110563 : case LE_EXPR:
2735 110563 : return tristate (tristate::TS_TRUE);
2736 :
2737 528 : case NE_EXPR:
2738 528 : case GT_EXPR:
2739 528 : case LT_EXPR:
2740 528 : return tristate (tristate::TS_FALSE);
2741 : default:
2742 : break;
2743 : }
2744 : }
2745 :
2746 590233 : equiv_class_id lhs_ec (-1);
2747 590233 : equiv_class_id rhs_ec (-1);
2748 590233 : get_equiv_class_by_svalue (lhs, &lhs_ec);
2749 590233 : get_equiv_class_by_svalue (rhs, &rhs_ec);
2750 590233 : if (!lhs_ec.null_p () && !rhs_ec.null_p ())
2751 : {
2752 262006 : tristate result_for_ecs
2753 262006 : = eval_condition (lhs_ec, op, rhs_ec);
2754 262006 : if (result_for_ecs.is_known ())
2755 259123 : return result_for_ecs;
2756 : }
2757 :
2758 331110 : if (op == EQ_EXPR
2759 331110 : && impossible_derived_conditions_p (lhs, rhs))
2760 123 : return false;
2761 :
2762 : /* If at least one is not in an EC, we have no constraints
2763 : comparing LHS and RHS yet.
2764 : They might still be comparable if one (or both) is a constant.
2765 :
2766 : Alternatively, we can also get here if we had ECs but they weren't
2767 : comparable. Again, constant comparisons might give an answer. */
2768 330987 : tree lhs_const = lhs->maybe_get_constant ();
2769 330987 : tree rhs_const = rhs->maybe_get_constant ();
2770 330987 : if (lhs_const && rhs_const)
2771 : {
2772 1980 : tristate result_for_constants
2773 1980 : = compare_constants (lhs_const, op, rhs_const);
2774 1980 : if (result_for_constants.is_known ())
2775 1980 : return result_for_constants;
2776 : }
2777 :
2778 329007 : if (!lhs_ec.null_p ())
2779 : {
2780 91358 : if (rhs_const)
2781 83689 : return eval_condition (lhs_ec, op, rhs_const);
2782 : }
2783 245318 : if (!rhs_ec.null_p ())
2784 : {
2785 60733 : if (lhs_const)
2786 : {
2787 1944 : enum tree_code swapped_op = swap_tree_comparison (op);
2788 1944 : return eval_condition (rhs_ec, swapped_op, lhs_const);
2789 : }
2790 : }
2791 :
2792 243374 : return tristate (tristate::TS_UNKNOWN);
2793 : }
2794 :
2795 : /* Delete any information about svalues identified by P.
2796 : Such instances are removed from equivalence classes, and any
2797 : redundant ECs and constraints are also removed.
2798 : Accumulate stats into STATS. */
2799 :
2800 : template <typename PurgeCriteria>
2801 : void
2802 461460 : constraint_manager::purge (const PurgeCriteria &p, purge_stats *stats)
2803 : {
2804 : /* Delete any svalues identified by P within the various equivalence
2805 : classes. */
2806 4282246 : for (unsigned ec_idx = 0; ec_idx < m_equiv_classes.length (); )
2807 : {
2808 1732608 : equiv_class *ec = m_equiv_classes[ec_idx];
2809 :
2810 : int i;
2811 : const svalue *sval;
2812 1732608 : bool delete_ec = false;
2813 3607548 : FOR_EACH_VEC_ELT (ec->m_vars, i, sval)
2814 : {
2815 1874940 : if (sval == ec->m_cst_sval)
2816 558749 : continue;
2817 1316191 : if (p.should_purge_p (sval))
2818 : {
2819 28825 : if (ec->del (sval))
2820 24405 : if (!ec->m_constant)
2821 1874940 : delete_ec = true;
2822 : }
2823 : }
2824 :
2825 1732608 : if (delete_ec)
2826 : {
2827 24405 : delete ec;
2828 24405 : m_equiv_classes.ordered_remove (ec_idx);
2829 24405 : if (stats)
2830 0 : stats->m_num_equiv_classes++;
2831 :
2832 : /* Update the constraints, potentially removing some. */
2833 187686 : for (unsigned con_idx = 0; con_idx < m_constraints.length (); )
2834 : {
2835 71832 : constraint *c = &m_constraints[con_idx];
2836 :
2837 : /* Remove constraints that refer to the deleted EC. */
2838 71832 : if (c->m_lhs == ec_idx
2839 71832 : || c->m_rhs == ec_idx)
2840 : {
2841 19300 : m_constraints.ordered_remove (con_idx);
2842 19300 : if (stats)
2843 0 : stats->m_num_constraints++;
2844 : }
2845 : else
2846 : {
2847 : /* Renumber constraints that refer to ECs that have
2848 : had their idx changed. */
2849 52532 : c->m_lhs.update_for_removal (ec_idx);
2850 52532 : c->m_rhs.update_for_removal (ec_idx);
2851 :
2852 52532 : con_idx++;
2853 : }
2854 : }
2855 :
2856 : /* Update bounded_ranges_constraint instances. */
2857 : for (unsigned r_idx = 0;
2858 1733522 : r_idx < m_bounded_ranges_constraints.length (); )
2859 : {
2860 : bounded_ranges_constraint *brc
2861 914 : = &m_bounded_ranges_constraints[r_idx];
2862 :
2863 : /* Remove if it refers to the deleted EC. */
2864 914 : if (brc->m_ec_id == ec_idx)
2865 : {
2866 314 : m_bounded_ranges_constraints.ordered_remove (r_idx);
2867 314 : if (stats)
2868 0 : stats->m_num_bounded_ranges_constraints++;
2869 : }
2870 : else
2871 : {
2872 : /* Renumber any EC ids that refer to ECs that have
2873 : had their idx changed. */
2874 600 : brc->m_ec_id.update_for_removal (ec_idx);
2875 600 : r_idx++;
2876 : }
2877 : }
2878 : }
2879 : else
2880 1708203 : ec_idx++;
2881 : }
2882 :
2883 : /* Now delete any constraints that are purely between constants. */
2884 3039836 : for (unsigned con_idx = 0; con_idx < m_constraints.length (); )
2885 : {
2886 1133531 : constraint *c = &m_constraints[con_idx];
2887 1133531 : if (m_equiv_classes[c->m_lhs.m_idx]->m_vars.length () == 0
2888 1133531 : && m_equiv_classes[c->m_rhs.m_idx]->m_vars.length () == 0)
2889 : {
2890 0 : m_constraints.ordered_remove (con_idx);
2891 0 : if (stats)
2892 0 : stats->m_num_constraints++;
2893 : }
2894 : else
2895 : {
2896 1133531 : con_idx++;
2897 : }
2898 : }
2899 :
2900 : /* Finally, delete any ECs that purely contain constants and aren't
2901 : referenced by any constraints. */
2902 2169663 : for (unsigned ec_idx = 0; ec_idx < m_equiv_classes.length (); )
2903 : {
2904 1708203 : equiv_class *ec = m_equiv_classes[ec_idx];
2905 1708203 : if (ec->m_vars.length () == 0)
2906 : {
2907 0 : equiv_class_id ec_id (ec_idx);
2908 0 : bool has_constraint = false;
2909 0 : for (unsigned con_idx = 0; con_idx < m_constraints.length ();
2910 : con_idx++)
2911 : {
2912 0 : constraint *c = &m_constraints[con_idx];
2913 0 : if (c->m_lhs == ec_id
2914 0 : || c->m_rhs == ec_id)
2915 : {
2916 : has_constraint = true;
2917 : break;
2918 : }
2919 : }
2920 0 : if (!has_constraint)
2921 : {
2922 0 : delete ec;
2923 0 : m_equiv_classes.ordered_remove (ec_idx);
2924 0 : if (stats)
2925 0 : stats->m_num_equiv_classes++;
2926 :
2927 : /* Renumber constraints that refer to ECs that have
2928 : had their idx changed. */
2929 0 : for (unsigned con_idx = 0; con_idx < m_constraints.length ();
2930 : con_idx++)
2931 : {
2932 0 : constraint *c = &m_constraints[con_idx];
2933 0 : c->m_lhs.update_for_removal (ec_idx);
2934 0 : c->m_rhs.update_for_removal (ec_idx);
2935 : }
2936 :
2937 : /* Likewise for m_bounded_ranges_constraints. */
2938 0 : for (unsigned r_idx = 0;
2939 0 : r_idx < m_bounded_ranges_constraints.length ();
2940 : r_idx++)
2941 : {
2942 : bounded_ranges_constraint *brc
2943 0 : = &m_bounded_ranges_constraints[r_idx];
2944 0 : brc->m_ec_id.update_for_removal (ec_idx);
2945 : }
2946 :
2947 0 : continue;
2948 0 : }
2949 : }
2950 1708203 : ec_idx++;
2951 : }
2952 :
2953 461460 : validate ();
2954 461460 : }
2955 :
2956 : /* Implementation of PurgeCriteria: purge svalues that are not live
2957 : with respect to LIVENESS_CTXT. */
2958 :
2959 : class dead_svalue_purger
2960 : {
2961 : public:
2962 435616 : dead_svalue_purger (symbolic::liveness_context &liveness_ctxt)
2963 435616 : : m_liveness_ctxt (liveness_ctxt)
2964 : {
2965 : }
2966 :
2967 1199466 : bool should_purge_p (const svalue *sval) const
2968 : {
2969 1199466 : return !m_liveness_ctxt.node_live_p (*sval);
2970 : }
2971 :
2972 : private:
2973 : symbolic::liveness_context &m_liveness_ctxt;
2974 : };
2975 :
2976 : /* Purge dead values from equivalence classes and update constraints
2977 : accordingly. */
2978 :
2979 : void
2980 435616 : constraint_manager::
2981 : on_liveness_change (symbolic::liveness_context &liveness_ctxt)
2982 : {
2983 435616 : dead_svalue_purger p (liveness_ctxt);
2984 435616 : purge (p, nullptr);
2985 435616 : }
2986 :
2987 : class node_purger
2988 : {
2989 : public:
2990 25844 : node_purger (const symbolic::node &node) : m_node (node) {}
2991 :
2992 116725 : bool should_purge_p (const svalue *sval) const
2993 : {
2994 116725 : return sval->involves_p (m_node);
2995 : }
2996 :
2997 : private:
2998 : const symbolic::node &m_node;
2999 : };
3000 :
3001 : /* Purge any state involving SVAL. */
3002 :
3003 : void
3004 25844 : constraint_manager::purge_state_involving (const symbolic::node &node)
3005 : {
3006 25844 : node_purger p (node);
3007 25844 : purge (p, nullptr);
3008 25844 : }
3009 :
3010 : /* Comparator for use by constraint_manager::canonicalize.
3011 : Sort a pair of equiv_class instances, using the representative
3012 : svalue as a sort key. */
3013 :
3014 : static int
3015 21017933 : equiv_class_cmp (const void *p1, const void *p2)
3016 : {
3017 21017933 : const equiv_class *ec1 = *(const equiv_class * const *)p1;
3018 21017933 : const equiv_class *ec2 = *(const equiv_class * const *)p2;
3019 :
3020 21017933 : const svalue *rep1 = ec1->get_representative ();
3021 21017933 : const svalue *rep2 = ec2->get_representative ();
3022 :
3023 21017933 : gcc_assert (rep1);
3024 21017933 : gcc_assert (rep2);
3025 :
3026 21017933 : return svalue::cmp_ptr (rep1, rep2);
3027 : }
3028 :
3029 : /* Comparator for use by constraint_manager::canonicalize.
3030 : Sort a pair of constraint instances. */
3031 :
3032 : static int
3033 11566874 : constraint_cmp (const void *p1, const void *p2)
3034 : {
3035 11566874 : const constraint *c1 = (const constraint *)p1;
3036 11566874 : const constraint *c2 = (const constraint *)p2;
3037 11566874 : int lhs_cmp = c1->m_lhs.as_int () - c2->m_lhs.as_int ();
3038 11566874 : if (lhs_cmp)
3039 : return lhs_cmp;
3040 237253 : int rhs_cmp = c1->m_rhs.as_int () - c2->m_rhs.as_int ();
3041 237253 : if (rhs_cmp)
3042 : return rhs_cmp;
3043 15479 : return c1->m_op - c2->m_op;
3044 : }
3045 :
3046 : /* Purge redundant equivalence classes and constraints, and reorder them
3047 : within this constraint_manager into a canonical order, to increase the
3048 : chances of finding equality with another instance. */
3049 :
3050 : void
3051 785401 : constraint_manager::canonicalize ()
3052 : {
3053 : /* First, sort svalues within the ECs. */
3054 785401 : unsigned i;
3055 785401 : equiv_class *ec;
3056 3175516 : FOR_EACH_VEC_ELT (m_equiv_classes, i, ec)
3057 2390115 : ec->canonicalize ();
3058 :
3059 : /* TODO: remove constraints where both sides have a constant, and are
3060 : thus implicit. But does this break transitivity? */
3061 :
3062 : /* We will be purging and reordering ECs.
3063 : We will need to remap the equiv_class_ids in the constraints,
3064 : so we need to store the original index of each EC.
3065 : Build a lookup table, mapping from the representative svalue
3066 : to the original equiv_class_id of that svalue. */
3067 785401 : hash_map<const svalue *, equiv_class_id> original_ec_id;
3068 785401 : const unsigned orig_num_equiv_classes = m_equiv_classes.length ();
3069 3175516 : FOR_EACH_VEC_ELT (m_equiv_classes, i, ec)
3070 : {
3071 2390115 : const svalue *rep = ec->get_representative ();
3072 2390115 : gcc_assert (rep);
3073 2390115 : original_ec_id.put (rep, i);
3074 : }
3075 :
3076 : /* Find ECs used by constraints. */
3077 785401 : hash_set<const equiv_class *> used_ecs;
3078 785401 : constraint *c;
3079 3159847 : FOR_EACH_VEC_ELT (m_constraints, i, c)
3080 : {
3081 1589045 : used_ecs.add (m_equiv_classes[c->m_lhs.as_int ()]);
3082 1589045 : used_ecs.add (m_equiv_classes[c->m_rhs.as_int ()]);
3083 : }
3084 :
3085 813633 : for (const auto &iter : m_bounded_ranges_constraints)
3086 9904 : used_ecs.add (m_equiv_classes[iter.m_ec_id.as_int ()]);
3087 :
3088 : /* Purge unused ECs: those that aren't used by constraints and
3089 : that effectively have only one svalue (either in m_constant
3090 : or in m_vars). */
3091 : {
3092 : /* "unordered remove if" from a vec. */
3093 : unsigned i = 0;
3094 3175516 : while (i < m_equiv_classes.length ())
3095 : {
3096 2390115 : equiv_class *ec = m_equiv_classes[i];
3097 2390115 : if (!used_ecs.contains (ec)
3098 2390115 : && !ec->contains_non_constant_p ())
3099 : {
3100 27428 : m_equiv_classes.unordered_remove (i);
3101 27428 : delete ec;
3102 : }
3103 : else
3104 2362687 : i++;
3105 : }
3106 : }
3107 :
3108 : /* Next, sort the surviving ECs into a canonical order. */
3109 785401 : m_equiv_classes.qsort (equiv_class_cmp);
3110 :
3111 : /* Populate ec_id_map based on the old vs new EC ids. */
3112 785401 : one_way_id_map<equiv_class_id> ec_id_map (orig_num_equiv_classes);
3113 3933489 : FOR_EACH_VEC_ELT (m_equiv_classes, i, ec)
3114 : {
3115 2362687 : const svalue *rep = ec->get_representative ();
3116 2362687 : gcc_assert (rep);
3117 2362687 : ec_id_map.put (*original_ec_id.get (rep), i);
3118 : }
3119 :
3120 : /* Use ec_id_map to update the EC ids within the constraints. */
3121 2374446 : FOR_EACH_VEC_ELT (m_constraints, i, c)
3122 : {
3123 1589045 : ec_id_map.update (&c->m_lhs);
3124 1589045 : ec_id_map.update (&c->m_rhs);
3125 : }
3126 :
3127 813633 : for (auto &iter : m_bounded_ranges_constraints)
3128 9904 : ec_id_map.update (&iter.m_ec_id);
3129 :
3130 : /* Finally, sort the constraints. */
3131 1253222 : m_constraints.qsort (constraint_cmp);
3132 785401 : }
3133 :
3134 : /* Concrete subclass of fact_visitor for use by constraint_manager::merge.
3135 : For every fact in CM_A, see if it is also true in *CM_B. Add such
3136 : facts to *OUT. */
3137 :
3138 42252 : class merger_fact_visitor : public fact_visitor
3139 : {
3140 : public:
3141 42252 : merger_fact_visitor (const constraint_manager *cm_b,
3142 : constraint_manager *out)
3143 42252 : : m_cm_b (cm_b), m_out (out)
3144 : {}
3145 :
3146 261205 : void on_fact (const svalue *lhs, enum tree_code code, const svalue *rhs)
3147 : final override
3148 : {
3149 : /* Special-case for widening. */
3150 261205 : if (lhs->get_kind () == SK_WIDENING)
3151 11584 : if (!m_cm_b->get_equiv_class_by_svalue (lhs, nullptr))
3152 : {
3153 : /* LHS isn't constrained within m_cm_b. */
3154 9622 : bool sat = m_out->add_constraint (lhs, code, rhs);
3155 9622 : gcc_assert (sat);
3156 : return;
3157 : }
3158 :
3159 251583 : if (m_cm_b->eval_condition (lhs, code, rhs).is_true ())
3160 : {
3161 213583 : bool sat = m_out->add_constraint (lhs, code, rhs);
3162 213583 : if (!sat)
3163 : {
3164 : /* If -fanalyzer-transitivity is off, we can encounter cases
3165 : where at least one of the two constraint_managers being merged
3166 : is infeasible, but we only discover that infeasibility
3167 : during merging (PR analyzer/96650).
3168 : Silently drop such constraints. */
3169 8 : gcc_assert (!flag_analyzer_transitivity);
3170 : }
3171 : }
3172 : }
3173 :
3174 524 : void on_ranges (const svalue *lhs_sval,
3175 : const bounded_ranges *ranges) final override
3176 : {
3177 1454 : for (const auto &iter : m_cm_b->m_bounded_ranges_constraints)
3178 : {
3179 310 : const equiv_class &ec_rhs = iter.m_ec_id.get_obj (*m_cm_b);
3180 932 : for (unsigned i = 0; i < ec_rhs.m_vars.length (); i++)
3181 : {
3182 312 : const svalue *rhs_sval = ec_rhs.m_vars[i];
3183 312 : if (lhs_sval == rhs_sval)
3184 : {
3185 : /* Union of the two ranges. */
3186 219 : auto_vec <const bounded_ranges *> pair (2);
3187 219 : pair.quick_push (ranges);
3188 219 : pair.quick_push (iter.m_ranges);
3189 219 : bounded_ranges_manager *ranges_mgr
3190 219 : = m_cm_b->get_range_manager ();
3191 219 : const bounded_ranges *union_
3192 219 : = ranges_mgr->get_or_create_union (pair);
3193 219 : bool sat = m_out->add_bounded_ranges (lhs_sval, union_);
3194 219 : gcc_assert (sat);
3195 219 : }
3196 : }
3197 : }
3198 524 : }
3199 :
3200 : private:
3201 : const constraint_manager *m_cm_b;
3202 : constraint_manager *m_out;
3203 : };
3204 :
3205 : /* Use MERGER to merge CM_A and CM_B into *OUT.
3206 : If one thinks of a constraint_manager as a subset of N-dimensional
3207 : space, this takes the union of the points of CM_A and CM_B, and
3208 : expresses that into *OUT. Alternatively, it can be thought of
3209 : as the intersection of the constraints. */
3210 :
3211 : void
3212 42252 : constraint_manager::merge (const constraint_manager &cm_a,
3213 : const constraint_manager &cm_b,
3214 : constraint_manager *out)
3215 : {
3216 : /* Merge the equivalence classes and constraints.
3217 : The easiest way to do this seems to be to enumerate all of the facts
3218 : in cm_a, see which are also true in cm_b,
3219 : and add those to *OUT. */
3220 42252 : merger_fact_visitor v (&cm_b, out);
3221 42252 : cm_a.for_each_fact (&v);
3222 42252 : }
3223 :
3224 : /* Call VISITOR's on_fact vfunc repeatedly to express the various
3225 : equivalence classes and constraints.
3226 : This is used by constraint_manager::merge to find the common
3227 : facts between two input constraint_managers. */
3228 :
3229 : void
3230 42759 : constraint_manager::for_each_fact (fact_visitor *visitor) const
3231 : {
3232 : /* First, call EQ_EXPR within the various equivalence classes. */
3233 42759 : unsigned ec_idx;
3234 42759 : equiv_class *ec;
3235 188783 : FOR_EACH_VEC_ELT (m_equiv_classes, ec_idx, ec)
3236 : {
3237 146024 : if (ec->m_cst_sval)
3238 : {
3239 : unsigned i;
3240 : const svalue *sval;
3241 123706 : FOR_EACH_VEC_ELT (ec->m_vars, i, sval)
3242 68277 : visitor->on_fact (ec->m_cst_sval, EQ_EXPR, sval);
3243 : }
3244 305003 : for (unsigned i = 0; i < ec->m_vars.length (); i++)
3245 175032 : for (unsigned j = i + 1; j < ec->m_vars.length (); j++)
3246 16053 : visitor->on_fact (ec->m_vars[i], EQ_EXPR, ec->m_vars[j]);
3247 : }
3248 :
3249 : /* Now, express the various constraints. */
3250 : unsigned con_idx;
3251 : constraint *c;
3252 133615 : FOR_EACH_VEC_ELT (m_constraints, con_idx, c)
3253 : {
3254 90856 : const equiv_class &ec_lhs = c->m_lhs.get_obj (*this);
3255 90856 : const equiv_class &ec_rhs = c->m_rhs.get_obj (*this);
3256 90856 : enum tree_code code = constraint_tree_code (c->m_op);
3257 :
3258 90856 : if (ec_lhs.m_cst_sval)
3259 : {
3260 14595 : for (unsigned j = 0; j < ec_rhs.m_vars.length (); j++)
3261 : {
3262 7318 : visitor->on_fact (ec_lhs.m_cst_sval, code, ec_rhs.m_vars[j]);
3263 : }
3264 : }
3265 182257 : for (unsigned i = 0; i < ec_lhs.m_vars.length (); i++)
3266 : {
3267 91401 : if (ec_rhs.m_cst_sval)
3268 77408 : visitor->on_fact (ec_lhs.m_vars[i], code, ec_rhs.m_cst_sval);
3269 185034 : for (unsigned j = 0; j < ec_rhs.m_vars.length (); j++)
3270 93633 : visitor->on_fact (ec_lhs.m_vars[i], code, ec_rhs.m_vars[j]);
3271 : }
3272 : }
3273 :
3274 44130 : for (const auto &iter : m_bounded_ranges_constraints)
3275 : {
3276 453 : const equiv_class &ec_lhs = iter.m_ec_id.get_obj (*this);
3277 1430 : for (unsigned i = 0; i < ec_lhs.m_vars.length (); i++)
3278 : {
3279 524 : const svalue *lhs_sval = ec_lhs.m_vars[i];
3280 524 : visitor->on_ranges (lhs_sval, iter.m_ranges);
3281 : }
3282 : }
3283 42759 : }
3284 :
3285 : /* Subclass of fact_visitor for use by
3286 : constraint_manager::replay_call_summary. */
3287 :
3288 507 : class replay_fact_visitor : public fact_visitor
3289 : {
3290 : public:
3291 507 : replay_fact_visitor (call_summary_replay &r,
3292 : constraint_manager *out)
3293 507 : : m_r (r), m_out (out), m_feasible (true)
3294 : {}
3295 :
3296 507 : bool feasible_p () const { return m_feasible; }
3297 :
3298 1484 : void on_fact (const svalue *lhs, enum tree_code code, const svalue *rhs)
3299 : final override
3300 : {
3301 1484 : const svalue *caller_lhs = m_r.convert_svalue_from_summary (lhs);
3302 1484 : if (!caller_lhs)
3303 : return;
3304 1316 : const svalue *caller_rhs = m_r.convert_svalue_from_summary (rhs);
3305 1316 : if (!caller_rhs)
3306 : return;
3307 1308 : if (!m_out->add_constraint (caller_lhs, code, caller_rhs))
3308 102 : m_feasible = false;
3309 : }
3310 :
3311 0 : void on_ranges (const svalue *lhs_sval,
3312 : const bounded_ranges *ranges) final override
3313 : {
3314 0 : const svalue *caller_lhs = m_r.convert_svalue_from_summary (lhs_sval);
3315 0 : if (!caller_lhs)
3316 : return;
3317 0 : if (!m_out->add_bounded_ranges (caller_lhs, ranges))
3318 0 : m_feasible = false;
3319 : }
3320 :
3321 : private:
3322 : call_summary_replay &m_r;
3323 : constraint_manager *m_out;
3324 : bool m_feasible;
3325 : };
3326 :
3327 : /* Attempt to use R to replay the constraints from SUMMARY into this object.
3328 : Return true if it is feasible. */
3329 :
3330 : bool
3331 507 : constraint_manager::replay_call_summary (call_summary_replay &r,
3332 : const constraint_manager &summary)
3333 : {
3334 507 : replay_fact_visitor v (r, this);
3335 507 : summary.for_each_fact (&v);
3336 507 : return v.feasible_p ();
3337 507 : }
3338 :
3339 : /* Assert that this object is valid. */
3340 :
3341 : void
3342 620372 : constraint_manager::validate () const
3343 : {
3344 : /* Skip this in a release build. */
3345 : #if !CHECKING_P
3346 : return;
3347 : #endif
3348 :
3349 620372 : int i;
3350 620372 : equiv_class *ec;
3351 3615348 : FOR_EACH_VEC_ELT (m_equiv_classes, i, ec)
3352 : {
3353 2480494 : gcc_assert (ec);
3354 :
3355 : int j;
3356 : const svalue *sval;
3357 5197140 : FOR_EACH_VEC_ELT (ec->m_vars, j, sval)
3358 2716646 : gcc_assert (sval);
3359 2480494 : if (ec->m_constant)
3360 : {
3361 843161 : gcc_assert (CONSTANT_CLASS_P (ec->m_constant));
3362 843161 : gcc_assert (ec->m_cst_sval);
3363 : }
3364 : #if 0
3365 : else
3366 : gcc_assert (ec->m_vars.length () > 0);
3367 : #endif
3368 : }
3369 :
3370 : constraint *c;
3371 2251576 : FOR_EACH_VEC_ELT (m_constraints, i, c)
3372 : {
3373 1631204 : gcc_assert (!c->m_lhs.null_p ());
3374 3262408 : gcc_assert (c->m_lhs.as_int () < (int)m_equiv_classes.length ());
3375 1631204 : gcc_assert (!c->m_rhs.null_p ());
3376 1631204 : gcc_assert (c->m_rhs.as_int () < (int)m_equiv_classes.length ());
3377 : }
3378 :
3379 649142 : for (const auto &iter : m_bounded_ranges_constraints)
3380 : {
3381 11022 : gcc_assert (!iter.m_ec_id.null_p ());
3382 22044 : gcc_assert (iter.m_ec_id.as_int () < (int)m_equiv_classes.length ());
3383 : }
3384 620372 : }
3385 :
3386 : bounded_ranges_manager *
3387 15325 : constraint_manager::get_range_manager () const
3388 : {
3389 15325 : return m_mgr->get_range_manager ();
3390 : }
3391 :
3392 : #if CHECKING_P
3393 :
3394 : namespace selftest {
3395 :
3396 : /* Various constraint_manager selftests.
3397 : These have to be written in terms of a region_model, since
3398 : the latter is responsible for managing svalue instances. */
3399 :
3400 : /* Verify that range::add_bound works as expected. */
3401 :
3402 : static void
3403 8 : test_range ()
3404 : {
3405 8 : tree int_0 = integer_zero_node;
3406 8 : tree int_1 = integer_one_node;
3407 8 : tree int_2 = build_int_cst (integer_type_node, 2);
3408 8 : tree int_5 = build_int_cst (integer_type_node, 5);
3409 :
3410 8 : {
3411 8 : range r;
3412 8 : ASSERT_FALSE (r.constrained_to_single_element ());
3413 :
3414 : /* (r >= 1). */
3415 8 : ASSERT_TRUE (r.add_bound (GE_EXPR, int_1));
3416 :
3417 : /* Redundant. */
3418 8 : ASSERT_TRUE (r.add_bound (GE_EXPR, int_0));
3419 8 : ASSERT_TRUE (r.add_bound (GT_EXPR, int_0));
3420 :
3421 8 : ASSERT_FALSE (r.constrained_to_single_element ());
3422 :
3423 : /* Contradiction. */
3424 8 : ASSERT_FALSE (r.add_bound (LT_EXPR, int_1));
3425 :
3426 : /* (r < 5). */
3427 8 : ASSERT_TRUE (r.add_bound (LT_EXPR, int_5));
3428 8 : ASSERT_FALSE (r.constrained_to_single_element ());
3429 :
3430 : /* Contradiction. */
3431 8 : ASSERT_FALSE (r.add_bound (GE_EXPR, int_5));
3432 :
3433 : /* (r < 2). */
3434 8 : ASSERT_TRUE (r.add_bound (LT_EXPR, int_2));
3435 8 : ASSERT_TRUE (r.constrained_to_single_element ());
3436 :
3437 : /* Redundant. */
3438 8 : ASSERT_TRUE (r.add_bound (LE_EXPR, int_1));
3439 8 : ASSERT_TRUE (r.constrained_to_single_element ());
3440 : }
3441 8 : }
3442 :
3443 : /* Verify that setting and getting simple conditions within a region_model
3444 : work (thus exercising the underlying constraint_manager). */
3445 :
3446 : static void
3447 8 : test_constraint_conditions ()
3448 : {
3449 8 : tree int_42 = build_int_cst (integer_type_node, 42);
3450 8 : tree int_0 = integer_zero_node;
3451 :
3452 8 : tree x = build_global_decl ("x", integer_type_node);
3453 8 : tree y = build_global_decl ("y", integer_type_node);
3454 8 : tree z = build_global_decl ("z", integer_type_node);
3455 :
3456 : /* Self-comparisons. */
3457 8 : {
3458 8 : symbolic::node_manager node_mgr;
3459 8 : region_model model (node_mgr);
3460 8 : ASSERT_CONDITION_TRUE (model, x, EQ_EXPR, x);
3461 8 : ASSERT_CONDITION_TRUE (model, x, LE_EXPR, x);
3462 8 : ASSERT_CONDITION_TRUE (model, x, GE_EXPR, x);
3463 8 : ASSERT_CONDITION_FALSE (model, x, NE_EXPR, x);
3464 8 : ASSERT_CONDITION_FALSE (model, x, LT_EXPR, x);
3465 8 : ASSERT_CONDITION_FALSE (model, x, GT_EXPR, x);
3466 8 : }
3467 :
3468 : /* Adding self-equality shouldn't add equiv classes. */
3469 8 : {
3470 8 : symbolic::node_manager node_mgr;
3471 8 : region_model model (node_mgr);
3472 8 : ADD_SAT_CONSTRAINT (model, x, EQ_EXPR, x);
3473 8 : ADD_SAT_CONSTRAINT (model, int_42, EQ_EXPR, int_42);
3474 : /* ...even when done directly via svalues: */
3475 8 : const svalue *sval_int_42 = model.get_rvalue (int_42, nullptr);
3476 8 : bool sat = model.get_constraints ()->add_constraint (sval_int_42,
3477 : EQ_EXPR,
3478 : sval_int_42);
3479 8 : ASSERT_TRUE (sat);
3480 8 : ASSERT_EQ (model.get_constraints ()->m_equiv_classes.length (), 0);
3481 8 : }
3482 :
3483 : /* x == y. */
3484 8 : {
3485 8 : symbolic::node_manager node_mgr;
3486 8 : region_model model (node_mgr);
3487 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, y);
3488 :
3489 8 : ADD_SAT_CONSTRAINT (model, x, EQ_EXPR, y);
3490 :
3491 8 : ASSERT_CONDITION_TRUE (model, x, EQ_EXPR, y);
3492 8 : ASSERT_CONDITION_TRUE (model, x, LE_EXPR, y);
3493 8 : ASSERT_CONDITION_TRUE (model, x, GE_EXPR, y);
3494 8 : ASSERT_CONDITION_FALSE (model, x, NE_EXPR, y);
3495 8 : ASSERT_CONDITION_FALSE (model, x, LT_EXPR, y);
3496 8 : ASSERT_CONDITION_FALSE (model, x, GT_EXPR, y);
3497 :
3498 : /* Swapped operands. */
3499 8 : ASSERT_CONDITION_TRUE (model, y, EQ_EXPR, x);
3500 8 : ASSERT_CONDITION_TRUE (model, y, LE_EXPR, x);
3501 8 : ASSERT_CONDITION_TRUE (model, y, GE_EXPR, x);
3502 8 : ASSERT_CONDITION_FALSE (model, y, NE_EXPR, x);
3503 8 : ASSERT_CONDITION_FALSE (model, y, LT_EXPR, x);
3504 8 : ASSERT_CONDITION_FALSE (model, y, GT_EXPR, x);
3505 :
3506 : /* Comparison with other var. */
3507 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, z);
3508 8 : ASSERT_CONDITION_UNKNOWN (model, x, LE_EXPR, z);
3509 8 : ASSERT_CONDITION_UNKNOWN (model, x, GE_EXPR, z);
3510 8 : ASSERT_CONDITION_UNKNOWN (model, x, NE_EXPR, z);
3511 8 : ASSERT_CONDITION_UNKNOWN (model, x, LT_EXPR, z);
3512 8 : ASSERT_CONDITION_UNKNOWN (model, x, GT_EXPR, z);
3513 8 : }
3514 :
3515 : /* x == y, then y == z */
3516 8 : {
3517 8 : symbolic::node_manager node_mgr;
3518 8 : region_model model (node_mgr);
3519 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, y);
3520 :
3521 8 : ADD_SAT_CONSTRAINT (model, x, EQ_EXPR, y);
3522 8 : ADD_SAT_CONSTRAINT (model, y, EQ_EXPR, z);
3523 :
3524 8 : ASSERT_CONDITION_TRUE (model, x, EQ_EXPR, z);
3525 8 : ASSERT_CONDITION_TRUE (model, x, LE_EXPR, z);
3526 8 : ASSERT_CONDITION_TRUE (model, x, GE_EXPR, z);
3527 8 : ASSERT_CONDITION_FALSE (model, x, NE_EXPR, z);
3528 8 : ASSERT_CONDITION_FALSE (model, x, LT_EXPR, z);
3529 8 : ASSERT_CONDITION_FALSE (model, x, GT_EXPR, z);
3530 8 : }
3531 :
3532 : /* x != y. */
3533 8 : {
3534 8 : symbolic::node_manager node_mgr;
3535 8 : region_model model (node_mgr);
3536 :
3537 8 : ADD_SAT_CONSTRAINT (model, x, NE_EXPR, y);
3538 :
3539 8 : ASSERT_CONDITION_TRUE (model, x, NE_EXPR, y);
3540 8 : ASSERT_CONDITION_FALSE (model, x, EQ_EXPR, y);
3541 8 : ASSERT_CONDITION_UNKNOWN (model, x, LE_EXPR, y);
3542 8 : ASSERT_CONDITION_UNKNOWN (model, x, GE_EXPR, y);
3543 8 : ASSERT_CONDITION_UNKNOWN (model, x, LT_EXPR, y);
3544 8 : ASSERT_CONDITION_UNKNOWN (model, x, GT_EXPR, y);
3545 :
3546 : /* Swapped operands. */
3547 8 : ASSERT_CONDITION_TRUE (model, y, NE_EXPR, x);
3548 8 : ASSERT_CONDITION_FALSE (model, y, EQ_EXPR, x);
3549 8 : ASSERT_CONDITION_UNKNOWN (model, y, LE_EXPR, x);
3550 8 : ASSERT_CONDITION_UNKNOWN (model, y, GE_EXPR, x);
3551 8 : ASSERT_CONDITION_UNKNOWN (model, y, LT_EXPR, x);
3552 8 : ASSERT_CONDITION_UNKNOWN (model, y, GT_EXPR, x);
3553 :
3554 : /* Comparison with other var. */
3555 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, z);
3556 8 : ASSERT_CONDITION_UNKNOWN (model, x, LE_EXPR, z);
3557 8 : ASSERT_CONDITION_UNKNOWN (model, x, GE_EXPR, z);
3558 8 : ASSERT_CONDITION_UNKNOWN (model, x, NE_EXPR, z);
3559 8 : ASSERT_CONDITION_UNKNOWN (model, x, LT_EXPR, z);
3560 8 : ASSERT_CONDITION_UNKNOWN (model, x, GT_EXPR, z);
3561 8 : }
3562 :
3563 : /* x < y. */
3564 8 : {
3565 8 : symbolic::node_manager node_mgr;
3566 8 : region_model model (node_mgr);
3567 :
3568 8 : ADD_SAT_CONSTRAINT (model, x, LT_EXPR, y);
3569 :
3570 8 : ASSERT_CONDITION_TRUE (model, x, LT_EXPR, y);
3571 8 : ASSERT_CONDITION_TRUE (model, x, LE_EXPR, y);
3572 8 : ASSERT_CONDITION_TRUE (model, x, NE_EXPR, y);
3573 8 : ASSERT_CONDITION_FALSE (model, x, EQ_EXPR, y);
3574 8 : ASSERT_CONDITION_FALSE (model, x, GT_EXPR, y);
3575 8 : ASSERT_CONDITION_FALSE (model, x, GE_EXPR, y);
3576 :
3577 : /* Swapped operands. */
3578 8 : ASSERT_CONDITION_FALSE (model, y, LT_EXPR, x);
3579 8 : ASSERT_CONDITION_FALSE (model, y, LE_EXPR, x);
3580 8 : ASSERT_CONDITION_TRUE (model, y, NE_EXPR, x);
3581 8 : ASSERT_CONDITION_FALSE (model, y, EQ_EXPR, x);
3582 8 : ASSERT_CONDITION_TRUE (model, y, GT_EXPR, x);
3583 8 : ASSERT_CONDITION_TRUE (model, y, GE_EXPR, x);
3584 8 : }
3585 :
3586 : /* x <= y. */
3587 8 : {
3588 8 : symbolic::node_manager node_mgr;
3589 8 : region_model model (node_mgr);
3590 :
3591 8 : ADD_SAT_CONSTRAINT (model, x, LE_EXPR, y);
3592 :
3593 8 : ASSERT_CONDITION_UNKNOWN (model, x, LT_EXPR, y);
3594 8 : ASSERT_CONDITION_TRUE (model, x, LE_EXPR, y);
3595 8 : ASSERT_CONDITION_UNKNOWN (model, x, NE_EXPR, y);
3596 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, y);
3597 8 : ASSERT_CONDITION_FALSE (model, x, GT_EXPR, y);
3598 8 : ASSERT_CONDITION_UNKNOWN (model, x, GE_EXPR, y);
3599 :
3600 : /* Swapped operands. */
3601 8 : ASSERT_CONDITION_FALSE (model, y, LT_EXPR, x);
3602 8 : ASSERT_CONDITION_UNKNOWN (model, y, LE_EXPR, x);
3603 8 : ASSERT_CONDITION_UNKNOWN (model, y, NE_EXPR, x);
3604 8 : ASSERT_CONDITION_UNKNOWN (model, y, EQ_EXPR, x);
3605 8 : ASSERT_CONDITION_UNKNOWN (model, y, GT_EXPR, x);
3606 8 : ASSERT_CONDITION_TRUE (model, y, GE_EXPR, x);
3607 8 : }
3608 :
3609 : /* x > y. */
3610 8 : {
3611 8 : symbolic::node_manager node_mgr;
3612 8 : region_model model (node_mgr);
3613 :
3614 8 : ADD_SAT_CONSTRAINT (model, x, GT_EXPR, y);
3615 :
3616 8 : ASSERT_CONDITION_TRUE (model, x, GT_EXPR, y);
3617 8 : ASSERT_CONDITION_TRUE (model, x, GE_EXPR, y);
3618 8 : ASSERT_CONDITION_TRUE (model, x, NE_EXPR, y);
3619 8 : ASSERT_CONDITION_FALSE (model, x, EQ_EXPR, y);
3620 8 : ASSERT_CONDITION_FALSE (model, x, LT_EXPR, y);
3621 8 : ASSERT_CONDITION_FALSE (model, x, LE_EXPR, y);
3622 :
3623 : /* Swapped operands. */
3624 8 : ASSERT_CONDITION_FALSE (model, y, GT_EXPR, x);
3625 8 : ASSERT_CONDITION_FALSE (model, y, GE_EXPR, x);
3626 8 : ASSERT_CONDITION_TRUE (model, y, NE_EXPR, x);
3627 8 : ASSERT_CONDITION_FALSE (model, y, EQ_EXPR, x);
3628 8 : ASSERT_CONDITION_TRUE (model, y, LT_EXPR, x);
3629 8 : ASSERT_CONDITION_TRUE (model, y, LE_EXPR, x);
3630 8 : }
3631 :
3632 : /* x >= y. */
3633 8 : {
3634 8 : symbolic::node_manager node_mgr;
3635 8 : region_model model (node_mgr);
3636 :
3637 8 : ADD_SAT_CONSTRAINT (model, x, GE_EXPR, y);
3638 :
3639 8 : ASSERT_CONDITION_UNKNOWN (model, x, GT_EXPR, y);
3640 8 : ASSERT_CONDITION_TRUE (model, x, GE_EXPR, y);
3641 8 : ASSERT_CONDITION_UNKNOWN (model, x, NE_EXPR, y);
3642 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, y);
3643 8 : ASSERT_CONDITION_FALSE (model, x, LT_EXPR, y);
3644 8 : ASSERT_CONDITION_UNKNOWN (model, x, LE_EXPR, y);
3645 :
3646 : /* Swapped operands. */
3647 8 : ASSERT_CONDITION_FALSE (model, y, GT_EXPR, x);
3648 8 : ASSERT_CONDITION_UNKNOWN (model, y, GE_EXPR, x);
3649 8 : ASSERT_CONDITION_UNKNOWN (model, y, NE_EXPR, x);
3650 8 : ASSERT_CONDITION_UNKNOWN (model, y, EQ_EXPR, x);
3651 8 : ASSERT_CONDITION_UNKNOWN (model, y, LT_EXPR, x);
3652 8 : ASSERT_CONDITION_TRUE (model, y, LE_EXPR, x);
3653 8 : }
3654 :
3655 : // TODO: implied orderings
3656 :
3657 : /* Constants. */
3658 8 : {
3659 8 : symbolic::node_manager node_mgr;
3660 8 : region_model model (node_mgr);
3661 :
3662 8 : ASSERT_CONDITION_FALSE (model, int_0, EQ_EXPR, int_42);
3663 8 : ASSERT_CONDITION_TRUE (model, int_0, NE_EXPR, int_42);
3664 8 : ASSERT_CONDITION_TRUE (model, int_0, LT_EXPR, int_42);
3665 8 : ASSERT_CONDITION_TRUE (model, int_0, LE_EXPR, int_42);
3666 8 : ASSERT_CONDITION_FALSE (model, int_0, GT_EXPR, int_42);
3667 8 : ASSERT_CONDITION_FALSE (model, int_0, GE_EXPR, int_42);
3668 8 : }
3669 :
3670 : /* x == 0, y == 42. */
3671 8 : {
3672 8 : symbolic::node_manager node_mgr;
3673 8 : region_model model (node_mgr);
3674 :
3675 8 : ADD_SAT_CONSTRAINT (model, x, EQ_EXPR, int_0);
3676 8 : ADD_SAT_CONSTRAINT (model, y, EQ_EXPR, int_42);
3677 :
3678 8 : ASSERT_CONDITION_TRUE (model, x, NE_EXPR, y);
3679 8 : ASSERT_CONDITION_FALSE (model, x, EQ_EXPR, y);
3680 8 : ASSERT_CONDITION_TRUE (model, x, LE_EXPR, y);
3681 8 : ASSERT_CONDITION_FALSE (model, x, GE_EXPR, y);
3682 8 : ASSERT_CONDITION_TRUE (model, x, LT_EXPR, y);
3683 8 : ASSERT_CONDITION_FALSE (model, x, GT_EXPR, y);
3684 8 : }
3685 :
3686 : /* Unsatisfiable combinations. */
3687 :
3688 : /* x == y && x != y. */
3689 8 : {
3690 8 : symbolic::node_manager node_mgr;
3691 8 : region_model model (node_mgr);
3692 :
3693 8 : ADD_SAT_CONSTRAINT (model, x, EQ_EXPR, y);
3694 8 : ADD_UNSAT_CONSTRAINT (model, x, NE_EXPR, y);
3695 8 : }
3696 :
3697 : /* x == 0 then x == 42. */
3698 8 : {
3699 8 : symbolic::node_manager node_mgr;
3700 8 : region_model model (node_mgr);
3701 :
3702 8 : ADD_SAT_CONSTRAINT (model, x, EQ_EXPR, int_0);
3703 8 : ADD_UNSAT_CONSTRAINT (model, x, EQ_EXPR, int_42);
3704 8 : }
3705 :
3706 : /* x == 0 then x != 0. */
3707 8 : {
3708 8 : symbolic::node_manager node_mgr;
3709 8 : region_model model (node_mgr);
3710 :
3711 8 : ADD_SAT_CONSTRAINT (model, x, EQ_EXPR, int_0);
3712 8 : ADD_UNSAT_CONSTRAINT (model, x, NE_EXPR, int_0);
3713 8 : }
3714 :
3715 : /* x == 0 then x > 0. */
3716 8 : {
3717 8 : symbolic::node_manager node_mgr;
3718 8 : region_model model (node_mgr);
3719 :
3720 8 : ADD_SAT_CONSTRAINT (model, x, EQ_EXPR, int_0);
3721 8 : ADD_UNSAT_CONSTRAINT (model, x, GT_EXPR, int_0);
3722 8 : }
3723 :
3724 : /* x != y && x == y. */
3725 8 : {
3726 8 : symbolic::node_manager node_mgr;
3727 8 : region_model model (node_mgr);
3728 :
3729 8 : ADD_SAT_CONSTRAINT (model, x, NE_EXPR, y);
3730 8 : ADD_UNSAT_CONSTRAINT (model, x, EQ_EXPR, y);
3731 8 : }
3732 :
3733 : /* x <= y && x > y. */
3734 8 : {
3735 8 : symbolic::node_manager node_mgr;
3736 8 : region_model model (node_mgr);
3737 :
3738 8 : ADD_SAT_CONSTRAINT (model, x, LE_EXPR, y);
3739 8 : ADD_UNSAT_CONSTRAINT (model, x, GT_EXPR, y);
3740 8 : }
3741 :
3742 : // etc
3743 8 : }
3744 :
3745 : /* Test transitivity of conditions. */
3746 :
3747 : static void
3748 4 : test_transitivity ()
3749 : {
3750 4 : tree a = build_global_decl ("a", integer_type_node);
3751 4 : tree b = build_global_decl ("b", integer_type_node);
3752 4 : tree c = build_global_decl ("c", integer_type_node);
3753 4 : tree d = build_global_decl ("d", integer_type_node);
3754 :
3755 : /* a == b, then c == d, then c == b. */
3756 4 : {
3757 4 : symbolic::node_manager node_mgr;
3758 4 : region_model model (node_mgr);
3759 :
3760 4 : ASSERT_CONDITION_UNKNOWN (model, a, EQ_EXPR, b);
3761 4 : ASSERT_CONDITION_UNKNOWN (model, b, EQ_EXPR, c);
3762 4 : ASSERT_CONDITION_UNKNOWN (model, c, EQ_EXPR, d);
3763 4 : ASSERT_CONDITION_UNKNOWN (model, a, EQ_EXPR, d);
3764 :
3765 4 : ADD_SAT_CONSTRAINT (model, a, EQ_EXPR, b);
3766 4 : ASSERT_CONDITION_TRUE (model, a, EQ_EXPR, b);
3767 :
3768 4 : ADD_SAT_CONSTRAINT (model, c, EQ_EXPR, d);
3769 4 : ASSERT_CONDITION_TRUE (model, c, EQ_EXPR, d);
3770 4 : ASSERT_CONDITION_UNKNOWN (model, a, EQ_EXPR, d);
3771 :
3772 4 : ADD_SAT_CONSTRAINT (model, c, EQ_EXPR, b);
3773 4 : ASSERT_CONDITION_TRUE (model, c, EQ_EXPR, b);
3774 4 : ASSERT_CONDITION_TRUE (model, a, EQ_EXPR, d);
3775 4 : }
3776 :
3777 : /* Transitivity: "a < b", "b < c" should imply "a < c". */
3778 4 : {
3779 4 : symbolic::node_manager node_mgr;
3780 4 : region_model model (node_mgr);
3781 :
3782 4 : ADD_SAT_CONSTRAINT (model, a, LT_EXPR, b);
3783 4 : ADD_SAT_CONSTRAINT (model, b, LT_EXPR, c);
3784 :
3785 4 : ASSERT_CONDITION_TRUE (model, a, LT_EXPR, c);
3786 4 : ASSERT_CONDITION_FALSE (model, a, EQ_EXPR, c);
3787 4 : }
3788 :
3789 : /* Transitivity: "a <= b", "b < c" should imply "a < c". */
3790 4 : {
3791 4 : symbolic::node_manager node_mgr;
3792 4 : region_model model (node_mgr);
3793 :
3794 4 : ADD_SAT_CONSTRAINT (model, a, LE_EXPR, b);
3795 4 : ADD_SAT_CONSTRAINT (model, b, LT_EXPR, c);
3796 :
3797 4 : ASSERT_CONDITION_TRUE (model, a, LT_EXPR, c);
3798 4 : ASSERT_CONDITION_FALSE (model, a, EQ_EXPR, c);
3799 4 : }
3800 :
3801 : /* Transitivity: "a <= b", "b <= c" should imply "a <= c". */
3802 4 : {
3803 4 : symbolic::node_manager node_mgr;
3804 4 : region_model model (node_mgr);
3805 :
3806 4 : ADD_SAT_CONSTRAINT (model, a, LE_EXPR, b);
3807 4 : ADD_SAT_CONSTRAINT (model, b, LE_EXPR, c);
3808 :
3809 4 : ASSERT_CONDITION_TRUE (model, a, LE_EXPR, c);
3810 4 : ASSERT_CONDITION_UNKNOWN (model, a, EQ_EXPR, c);
3811 4 : }
3812 :
3813 : /* Transitivity: "a > b", "b > c" should imply "a > c". */
3814 4 : {
3815 4 : symbolic::node_manager node_mgr;
3816 4 : region_model model (node_mgr);
3817 :
3818 4 : ADD_SAT_CONSTRAINT (model, a, GT_EXPR, b);
3819 4 : ADD_SAT_CONSTRAINT (model, b, GT_EXPR, c);
3820 :
3821 4 : ASSERT_CONDITION_TRUE (model, a, GT_EXPR, c);
3822 4 : ASSERT_CONDITION_FALSE (model, a, EQ_EXPR, c);
3823 4 : }
3824 :
3825 : /* Transitivity: "a >= b", "b > c" should imply " a > c". */
3826 4 : {
3827 4 : symbolic::node_manager node_mgr;
3828 4 : region_model model (node_mgr);
3829 :
3830 4 : ADD_SAT_CONSTRAINT (model, a, GE_EXPR, b);
3831 4 : ADD_SAT_CONSTRAINT (model, b, GT_EXPR, c);
3832 :
3833 4 : ASSERT_CONDITION_TRUE (model, a, GT_EXPR, c);
3834 4 : ASSERT_CONDITION_FALSE (model, a, EQ_EXPR, c);
3835 4 : }
3836 :
3837 : /* Transitivity: "a >= b", "b >= c" should imply "a >= c". */
3838 4 : {
3839 4 : symbolic::node_manager node_mgr;
3840 4 : region_model model (node_mgr);
3841 :
3842 4 : ADD_SAT_CONSTRAINT (model, a, GE_EXPR, b);
3843 4 : ADD_SAT_CONSTRAINT (model, b, GE_EXPR, c);
3844 :
3845 4 : ASSERT_CONDITION_TRUE (model, a, GE_EXPR, c);
3846 4 : ASSERT_CONDITION_UNKNOWN (model, a, EQ_EXPR, c);
3847 4 : }
3848 :
3849 : /* Transitivity: "(a < b)", "(c < d)", "(b < c)" should
3850 : imply the easy cases:
3851 : (a < c)
3852 : (b < d)
3853 : but also that:
3854 : (a < d). */
3855 4 : {
3856 4 : symbolic::node_manager node_mgr;
3857 4 : region_model model (node_mgr);
3858 :
3859 4 : ADD_SAT_CONSTRAINT (model, a, LT_EXPR, b);
3860 4 : ADD_SAT_CONSTRAINT (model, c, LT_EXPR, d);
3861 4 : ADD_SAT_CONSTRAINT (model, b, LT_EXPR, c);
3862 :
3863 4 : ASSERT_CONDITION_TRUE (model, a, LT_EXPR, c);
3864 4 : ASSERT_CONDITION_TRUE (model, b, LT_EXPR, d);
3865 4 : ASSERT_CONDITION_TRUE (model, a, LT_EXPR, d);
3866 4 : }
3867 :
3868 : /* Transitivity: "a >= b", "b >= a" should imply that a == b. */
3869 4 : {
3870 4 : symbolic::node_manager node_mgr;
3871 4 : region_model model (node_mgr);
3872 :
3873 4 : ADD_SAT_CONSTRAINT (model, a, GE_EXPR, b);
3874 4 : ADD_SAT_CONSTRAINT (model, b, GE_EXPR, a);
3875 :
3876 : // TODO:
3877 4 : ASSERT_CONDITION_TRUE (model, a, EQ_EXPR, b);
3878 :
3879 : /* The ECs for a and b should have merged, and any constraints removed. */
3880 4 : ASSERT_EQ (model.get_constraints ()->m_equiv_classes.length (), 1);
3881 4 : ASSERT_EQ (model.get_constraints ()->m_constraints.length (), 0);
3882 4 : }
3883 :
3884 : /* Transitivity: "a >= b", "b > a" should be impossible. */
3885 4 : {
3886 4 : symbolic::node_manager node_mgr;
3887 4 : region_model model (node_mgr);
3888 :
3889 4 : ADD_SAT_CONSTRAINT (model, a, GE_EXPR, b);
3890 4 : ADD_UNSAT_CONSTRAINT (model, b, GT_EXPR, a);
3891 4 : }
3892 :
3893 : /* Transitivity: "a >= b", "b >= c", "c >= a" should imply
3894 : that a == b == c. */
3895 4 : {
3896 4 : symbolic::node_manager node_mgr;
3897 4 : region_model model (node_mgr);
3898 :
3899 4 : ADD_SAT_CONSTRAINT (model, a, GE_EXPR, b);
3900 4 : ADD_SAT_CONSTRAINT (model, b, GE_EXPR, c);
3901 4 : ADD_SAT_CONSTRAINT (model, c, GE_EXPR, a);
3902 :
3903 4 : ASSERT_CONDITION_TRUE (model, a, EQ_EXPR, c);
3904 4 : }
3905 :
3906 : /* Transitivity: "a > b", "b > c", "c > a"
3907 : should be impossible. */
3908 4 : {
3909 4 : symbolic::node_manager node_mgr;
3910 4 : region_model model (node_mgr);
3911 :
3912 4 : ADD_SAT_CONSTRAINT (model, a, GT_EXPR, b);
3913 4 : ADD_SAT_CONSTRAINT (model, b, GT_EXPR, c);
3914 4 : ADD_UNSAT_CONSTRAINT (model, c, GT_EXPR, a);
3915 4 : }
3916 :
3917 4 : }
3918 :
3919 : /* Test various conditionals involving constants where the results
3920 : ought to be implied based on the values of the constants. */
3921 :
3922 : static void
3923 8 : test_constant_comparisons ()
3924 : {
3925 8 : tree int_1 = integer_one_node;
3926 8 : tree int_3 = build_int_cst (integer_type_node, 3);
3927 8 : tree int_4 = build_int_cst (integer_type_node, 4);
3928 8 : tree int_5 = build_int_cst (integer_type_node, 5);
3929 :
3930 8 : tree int_1023 = build_int_cst (integer_type_node, 1023);
3931 8 : tree int_1024 = build_int_cst (integer_type_node, 1024);
3932 :
3933 8 : tree a = build_global_decl ("a", integer_type_node);
3934 8 : tree b = build_global_decl ("b", integer_type_node);
3935 :
3936 8 : tree a_plus_one = build2 (PLUS_EXPR, integer_type_node, a, int_1);
3937 :
3938 : /* Given a >= 1024, then a <= 1023 should be impossible. */
3939 8 : {
3940 8 : symbolic::node_manager node_mgr;
3941 8 : region_model model (node_mgr);
3942 :
3943 8 : ADD_SAT_CONSTRAINT (model, a, GE_EXPR, int_1024);
3944 8 : ADD_UNSAT_CONSTRAINT (model, a, LE_EXPR, int_1023);
3945 8 : }
3946 :
3947 : /* a > 4. */
3948 8 : {
3949 8 : symbolic::node_manager node_mgr;
3950 8 : region_model model (node_mgr);
3951 :
3952 8 : ADD_SAT_CONSTRAINT (model, a, GT_EXPR, int_4);
3953 8 : ASSERT_CONDITION_TRUE (model, a, GT_EXPR, int_4);
3954 8 : ASSERT_CONDITION_TRUE (model, a, NE_EXPR, int_3);
3955 8 : ASSERT_CONDITION_UNKNOWN (model, a, NE_EXPR, int_5);
3956 8 : }
3957 :
3958 : /* a <= 4. */
3959 8 : {
3960 8 : symbolic::node_manager node_mgr;
3961 8 : region_model model (node_mgr);
3962 :
3963 8 : ADD_SAT_CONSTRAINT (model, a, LE_EXPR, int_4);
3964 8 : ASSERT_CONDITION_FALSE (model, a, GT_EXPR, int_4);
3965 8 : ASSERT_CONDITION_FALSE (model, a, GT_EXPR, int_5);
3966 8 : ASSERT_CONDITION_UNKNOWN (model, a, NE_EXPR, int_3);
3967 8 : }
3968 :
3969 : /* If "a > b" and "a == 3", then "b == 4" ought to be unsatisfiable. */
3970 8 : {
3971 8 : symbolic::node_manager node_mgr;
3972 8 : region_model model (node_mgr);
3973 :
3974 8 : ADD_SAT_CONSTRAINT (model, a, GT_EXPR, b);
3975 8 : ADD_SAT_CONSTRAINT (model, a, EQ_EXPR, int_3);
3976 8 : ADD_UNSAT_CONSTRAINT (model, b, EQ_EXPR, int_4);
3977 8 : }
3978 :
3979 : /* Various tests of int ranges where there is only one possible candidate. */
3980 8 : {
3981 : /* If "a <= 4" && "a > 3", then "a == 4",
3982 : assuming a is of integral type. */
3983 8 : {
3984 8 : symbolic::node_manager node_mgr;
3985 8 : region_model model (node_mgr);
3986 8 : ADD_SAT_CONSTRAINT (model, a, LE_EXPR, int_4);
3987 8 : ADD_SAT_CONSTRAINT (model, a, GT_EXPR, int_3);
3988 8 : ASSERT_CONDITION_TRUE (model, a, EQ_EXPR, int_4);
3989 8 : }
3990 :
3991 : /* If "a > 3" && "a <= 4", then "a == 4",
3992 : assuming a is of integral type. */
3993 8 : {
3994 8 : symbolic::node_manager node_mgr;
3995 8 : region_model model (node_mgr);
3996 8 : ADD_SAT_CONSTRAINT (model, a, GT_EXPR, int_3);
3997 8 : ADD_SAT_CONSTRAINT (model, a, LE_EXPR, int_4);
3998 8 : ASSERT_CONDITION_TRUE (model, a, EQ_EXPR, int_4);
3999 8 : }
4000 : /* If "a > 3" && "a < 5", then "a == 4",
4001 : assuming a is of integral type. */
4002 8 : {
4003 8 : symbolic::node_manager node_mgr;
4004 8 : region_model model (node_mgr);
4005 :
4006 8 : ADD_SAT_CONSTRAINT (model, a, GT_EXPR, int_3);
4007 8 : ADD_SAT_CONSTRAINT (model, a, LT_EXPR, int_5);
4008 8 : ASSERT_CONDITION_TRUE (model, a, EQ_EXPR, int_4);
4009 8 : }
4010 : /* If "a >= 4" && "a < 5", then "a == 4",
4011 : assuming a is of integral type. */
4012 8 : {
4013 8 : symbolic::node_manager node_mgr;
4014 8 : region_model model (node_mgr);
4015 :
4016 8 : ADD_SAT_CONSTRAINT (model, a, GE_EXPR, int_4);
4017 8 : ADD_SAT_CONSTRAINT (model, a, LT_EXPR, int_5);
4018 8 : ASSERT_CONDITION_TRUE (model, a, EQ_EXPR, int_4);
4019 8 : }
4020 : /* If "a >= 4" && "a <= 4", then "a == 4". */
4021 8 : {
4022 8 : symbolic::node_manager node_mgr;
4023 8 : region_model model (node_mgr);
4024 :
4025 8 : ADD_SAT_CONSTRAINT (model, a, GE_EXPR, int_4);
4026 8 : ADD_SAT_CONSTRAINT (model, a, LE_EXPR, int_4);
4027 8 : ASSERT_CONDITION_TRUE (model, a, EQ_EXPR, int_4);
4028 8 : }
4029 : }
4030 :
4031 : /* As above, but for floating-point:
4032 : if "f > 3" && "f <= 4" we don't know that f == 4. */
4033 8 : {
4034 8 : tree f = build_global_decl ("f", double_type_node);
4035 8 : tree float_3 = build_real_from_int_cst (double_type_node, int_3);
4036 8 : tree float_4 = build_real_from_int_cst (double_type_node, int_4);
4037 :
4038 8 : symbolic::node_manager node_mgr;
4039 8 : region_model model (node_mgr);
4040 :
4041 8 : ADD_SAT_CONSTRAINT (model, f, GT_EXPR, float_3);
4042 8 : ADD_SAT_CONSTRAINT (model, f, LE_EXPR, float_4);
4043 8 : ASSERT_CONDITION_UNKNOWN (model, f, EQ_EXPR, float_4);
4044 8 : ASSERT_CONDITION_UNKNOWN (model, f, EQ_EXPR, int_4);
4045 8 : }
4046 :
4047 : /* "a > 3 && a <= 3" should be impossible. */
4048 8 : {
4049 8 : symbolic::node_manager node_mgr;
4050 8 : region_model model (node_mgr);
4051 :
4052 8 : ADD_SAT_CONSTRAINT (model, a, GT_EXPR, int_3);
4053 8 : ADD_UNSAT_CONSTRAINT (model, a, LE_EXPR, int_3);
4054 8 : }
4055 :
4056 : /* "(a + 1) > 3 && a < 3" should be impossible. */
4057 8 : {
4058 8 : symbolic::node_manager node_mgr;
4059 8 : {
4060 8 : region_model model (node_mgr);
4061 8 : ADD_SAT_CONSTRAINT (model, a_plus_one, GT_EXPR, int_3);
4062 8 : ADD_UNSAT_CONSTRAINT (model, a, LT_EXPR, int_3);
4063 8 : }
4064 8 : {
4065 8 : region_model model (node_mgr);
4066 8 : ADD_SAT_CONSTRAINT (model, a, LT_EXPR, int_3);
4067 8 : ADD_UNSAT_CONSTRAINT (model, a_plus_one, GT_EXPR, int_3);
4068 8 : }
4069 8 : }
4070 :
4071 : /* "3 < a < 4" should be impossible for integer a. */
4072 8 : {
4073 8 : symbolic::node_manager node_mgr;
4074 8 : {
4075 8 : region_model model (node_mgr);
4076 8 : ADD_SAT_CONSTRAINT (model, int_3, LT_EXPR, a);
4077 8 : ADD_UNSAT_CONSTRAINT (model, a, LT_EXPR, int_4);
4078 8 : }
4079 8 : {
4080 8 : region_model model (node_mgr);
4081 8 : ADD_SAT_CONSTRAINT (model, int_1, LT_EXPR, a);
4082 8 : ADD_SAT_CONSTRAINT (model, int_3, LT_EXPR, a);
4083 8 : ADD_SAT_CONSTRAINT (model, a, LT_EXPR, int_5);
4084 8 : ADD_UNSAT_CONSTRAINT (model, a, LT_EXPR, int_4);
4085 8 : }
4086 8 : {
4087 8 : region_model model (node_mgr);
4088 8 : ADD_SAT_CONSTRAINT (model, int_1, LT_EXPR, a);
4089 8 : ADD_SAT_CONSTRAINT (model, a, LT_EXPR, int_5);
4090 8 : ADD_SAT_CONSTRAINT (model, int_3, LT_EXPR, a);
4091 8 : ADD_UNSAT_CONSTRAINT (model, a, LT_EXPR, int_4);
4092 8 : }
4093 8 : {
4094 8 : region_model model (node_mgr);
4095 8 : ADD_SAT_CONSTRAINT (model, a, LT_EXPR, int_4);
4096 8 : ADD_UNSAT_CONSTRAINT (model, int_3, LT_EXPR, a);
4097 8 : }
4098 8 : {
4099 8 : region_model model (node_mgr);
4100 8 : ADD_SAT_CONSTRAINT (model, a, GT_EXPR, int_3);
4101 8 : ADD_UNSAT_CONSTRAINT (model, int_4, GT_EXPR, a);
4102 8 : }
4103 8 : {
4104 8 : region_model model (node_mgr);
4105 8 : ADD_SAT_CONSTRAINT (model, int_4, GT_EXPR, a);
4106 8 : ADD_UNSAT_CONSTRAINT (model, a, GT_EXPR, int_3);
4107 8 : }
4108 8 : }
4109 8 : }
4110 :
4111 : /* Verify various lower-level implementation details about
4112 : constraint_manager. */
4113 :
4114 : static void
4115 8 : test_constraint_impl ()
4116 : {
4117 8 : tree int_42 = build_int_cst (integer_type_node, 42);
4118 8 : tree int_0 = integer_zero_node;
4119 :
4120 8 : tree x = build_global_decl ("x", integer_type_node);
4121 8 : tree y = build_global_decl ("y", integer_type_node);
4122 8 : tree z = build_global_decl ("z", integer_type_node);
4123 :
4124 : /* x == y. */
4125 8 : {
4126 8 : symbolic::node_manager node_mgr;
4127 8 : region_model model (node_mgr);
4128 :
4129 8 : ADD_SAT_CONSTRAINT (model, x, EQ_EXPR, y);
4130 :
4131 : /* Assert various things about the insides of model. */
4132 8 : constraint_manager *cm = model.get_constraints ();
4133 8 : ASSERT_EQ (cm->m_constraints.length (), 0);
4134 8 : ASSERT_EQ (cm->m_equiv_classes.length (), 1);
4135 8 : }
4136 :
4137 : /* y <= z; x == y. */
4138 8 : {
4139 8 : symbolic::node_manager node_mgr;
4140 8 : region_model model (node_mgr);
4141 :
4142 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, y);
4143 8 : ASSERT_CONDITION_UNKNOWN (model, x, GE_EXPR, z);
4144 :
4145 8 : ADD_SAT_CONSTRAINT (model, y, GE_EXPR, z);
4146 8 : ASSERT_CONDITION_TRUE (model, y, GE_EXPR, z);
4147 8 : ASSERT_CONDITION_UNKNOWN (model, x, GE_EXPR, z);
4148 :
4149 8 : ADD_SAT_CONSTRAINT (model, x, EQ_EXPR, y);
4150 :
4151 : /* Assert various things about the insides of model. */
4152 8 : constraint_manager *cm = model.get_constraints ();
4153 8 : ASSERT_EQ (cm->m_constraints.length (), 1);
4154 8 : ASSERT_EQ (cm->m_equiv_classes.length (), 2);
4155 :
4156 : /* Ensure that we merged the constraints. */
4157 8 : ASSERT_CONDITION_TRUE (model, x, GE_EXPR, z);
4158 8 : }
4159 :
4160 : /* y <= z; y == x. */
4161 8 : {
4162 8 : symbolic::node_manager node_mgr;
4163 8 : region_model model (node_mgr);
4164 :
4165 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, y);
4166 8 : ASSERT_CONDITION_UNKNOWN (model, x, GE_EXPR, z);
4167 :
4168 8 : ADD_SAT_CONSTRAINT (model, y, GE_EXPR, z);
4169 8 : ASSERT_CONDITION_TRUE (model, y, GE_EXPR, z);
4170 8 : ASSERT_CONDITION_UNKNOWN (model, x, GE_EXPR, z);
4171 :
4172 8 : ADD_SAT_CONSTRAINT (model, y, EQ_EXPR, x);
4173 :
4174 : /* Assert various things about the insides of model. */
4175 8 : constraint_manager *cm = model.get_constraints ();
4176 8 : ASSERT_EQ (cm->m_constraints.length (), 1);
4177 8 : ASSERT_EQ (cm->m_equiv_classes.length (), 2);
4178 :
4179 : /* Ensure that we merged the constraints. */
4180 8 : ASSERT_CONDITION_TRUE (model, x, GE_EXPR, z);
4181 8 : }
4182 :
4183 : /* x == 0, then x != 42. */
4184 8 : {
4185 8 : symbolic::node_manager node_mgr;
4186 8 : region_model model (node_mgr);
4187 :
4188 8 : ADD_SAT_CONSTRAINT (model, x, EQ_EXPR, int_0);
4189 8 : ADD_SAT_CONSTRAINT (model, x, NE_EXPR, int_42);
4190 :
4191 : /* Assert various things about the insides of model. */
4192 8 : constraint_manager *cm = model.get_constraints ();
4193 8 : ASSERT_EQ (cm->m_constraints.length (), 0);
4194 8 : ASSERT_EQ (cm->m_equiv_classes.length (), 1);
4195 8 : }
4196 :
4197 : // TODO: selftest for merging ecs "in the middle"
4198 : // where a non-final one gets overwritten
4199 :
4200 : // TODO: selftest where there are pre-existing constraints
4201 8 : }
4202 :
4203 : /* Check that operator== and hashing works as expected for the
4204 : various types. */
4205 :
4206 : static void
4207 8 : test_equality ()
4208 : {
4209 8 : tree x = build_global_decl ("x", integer_type_node);
4210 8 : tree y = build_global_decl ("y", integer_type_node);
4211 :
4212 8 : {
4213 8 : symbolic::node_manager node_mgr;
4214 :
4215 8 : region_model model0 (node_mgr);
4216 8 : region_model model1 (node_mgr);
4217 :
4218 8 : constraint_manager *cm0 = model0.get_constraints ();
4219 8 : constraint_manager *cm1 = model1.get_constraints ();
4220 :
4221 8 : ASSERT_EQ (cm0->hash (), cm1->hash ());
4222 8 : ASSERT_EQ (*cm0, *cm1);
4223 :
4224 8 : ASSERT_EQ (model0.hash (), model1.hash ());
4225 8 : ASSERT_EQ (model0, model1);
4226 :
4227 8 : ADD_SAT_CONSTRAINT (model1, x, EQ_EXPR, y);
4228 8 : ASSERT_NE (cm0->hash (), cm1->hash ());
4229 8 : ASSERT_NE (*cm0, *cm1);
4230 :
4231 8 : ASSERT_NE (model0.hash (), model1.hash ());
4232 8 : ASSERT_NE (model0, model1);
4233 :
4234 8 : region_model model2 (node_mgr);
4235 8 : constraint_manager *cm2 = model2.get_constraints ();
4236 : /* Make the same change to cm2. */
4237 8 : ADD_SAT_CONSTRAINT (model2, x, EQ_EXPR, y);
4238 8 : ASSERT_EQ (cm1->hash (), cm2->hash ());
4239 8 : ASSERT_EQ (*cm1, *cm2);
4240 :
4241 8 : ASSERT_EQ (model1.hash (), model2.hash ());
4242 8 : ASSERT_EQ (model1, model2);
4243 8 : }
4244 8 : }
4245 :
4246 : /* Verify tracking inequality of a variable against many constants. */
4247 :
4248 : static void
4249 8 : test_many_constants ()
4250 : {
4251 8 : symbolic::node_manager node_mgr;
4252 8 : auto &mgr = node_mgr.get_region_model_manager ();
4253 8 : program_point point (program_point::origin (mgr));
4254 8 : tree a = build_global_decl ("a", integer_type_node);
4255 :
4256 8 : region_model model (node_mgr);
4257 8 : auto_vec<tree> constants;
4258 168 : for (int i = 0; i < 20; i++)
4259 : {
4260 160 : tree constant = build_int_cst (integer_type_node, i);
4261 160 : constants.safe_push (constant);
4262 160 : ADD_SAT_CONSTRAINT (model, a, NE_EXPR, constant);
4263 :
4264 : /* Merge, and check the result. */
4265 160 : region_model other (model);
4266 :
4267 160 : region_model merged (node_mgr);
4268 160 : ASSERT_TRUE (model.can_merge_with_p (other, point, &merged));
4269 160 : model.canonicalize ();
4270 160 : merged.canonicalize ();
4271 160 : ASSERT_EQ (model, merged);
4272 :
4273 1840 : for (int j = 0; j <= i; j++)
4274 1680 : ASSERT_CONDITION_TRUE (model, a, NE_EXPR, constants[j]);
4275 160 : }
4276 8 : }
4277 :
4278 : /* Verify that purging state relating to a variable doesn't leave stray
4279 : equivalence classes (after canonicalization). */
4280 :
4281 : static void
4282 8 : test_purging (void)
4283 : {
4284 8 : tree int_0 = integer_zero_node;
4285 8 : tree a = build_global_decl ("a", integer_type_node);
4286 8 : tree b = build_global_decl ("b", integer_type_node);
4287 :
4288 : /* "a != 0". */
4289 8 : {
4290 8 : symbolic::node_manager node_mgr;
4291 8 : region_model model (node_mgr);
4292 :
4293 8 : ADD_SAT_CONSTRAINT (model, a, NE_EXPR, int_0);
4294 8 : ASSERT_EQ (model.get_constraints ()->m_equiv_classes.length (), 2);
4295 8 : ASSERT_EQ (model.get_constraints ()->m_constraints.length (), 1);
4296 :
4297 : /* Purge state for "a". */
4298 8 : const svalue *sval_a = model.get_rvalue (a, nullptr);
4299 8 : model.purge_state_involving (*sval_a, nullptr);
4300 8 : model.canonicalize ();
4301 : /* We should have an empty constraint_manager. */
4302 8 : ASSERT_EQ (model.get_constraints ()->m_equiv_classes.length (), 0);
4303 8 : ASSERT_EQ (model.get_constraints ()->m_constraints.length (), 0);
4304 8 : }
4305 :
4306 : /* "a != 0" && "b != 0". */
4307 8 : {
4308 8 : symbolic::node_manager node_mgr;
4309 8 : region_model model (node_mgr);
4310 :
4311 8 : ADD_SAT_CONSTRAINT (model, a, NE_EXPR, int_0);
4312 8 : ADD_SAT_CONSTRAINT (model, b, NE_EXPR, int_0);
4313 8 : ASSERT_EQ (model.get_constraints ()->m_equiv_classes.length (), 3);
4314 8 : ASSERT_EQ (model.get_constraints ()->m_constraints.length (), 2);
4315 :
4316 : /* Purge state for "a". */
4317 8 : const svalue *sval_a = model.get_rvalue (a, nullptr);
4318 8 : model.purge_state_involving (*sval_a, nullptr);
4319 8 : model.canonicalize ();
4320 : /* We should just have the constraint/ECs involving b != 0. */
4321 8 : ASSERT_EQ (model.get_constraints ()->m_equiv_classes.length (), 2);
4322 8 : ASSERT_EQ (model.get_constraints ()->m_constraints.length (), 1);
4323 8 : ASSERT_CONDITION_TRUE (model, b, NE_EXPR, int_0);
4324 8 : }
4325 :
4326 : /* "a != 0" && "b == 0". */
4327 8 : {
4328 8 : symbolic::node_manager node_mgr;
4329 8 : region_model model (node_mgr);
4330 :
4331 8 : ADD_SAT_CONSTRAINT (model, a, NE_EXPR, int_0);
4332 8 : ADD_SAT_CONSTRAINT (model, b, EQ_EXPR, int_0);
4333 8 : ASSERT_EQ (model.get_constraints ()->m_equiv_classes.length (), 2);
4334 8 : ASSERT_EQ (model.get_constraints ()->m_constraints.length (), 1);
4335 :
4336 : /* Purge state for "a". */
4337 8 : const svalue *sval_a = model.get_rvalue (a, nullptr);
4338 8 : model.purge_state_involving (*sval_a, nullptr);
4339 8 : model.canonicalize ();
4340 : /* We should just have the EC involving b == 0. */
4341 8 : ASSERT_EQ (model.get_constraints ()->m_equiv_classes.length (), 1);
4342 8 : ASSERT_EQ (model.get_constraints ()->m_constraints.length (), 0);
4343 8 : ASSERT_CONDITION_TRUE (model, b, EQ_EXPR, int_0);
4344 8 : }
4345 :
4346 : /* "a == 0". */
4347 8 : {
4348 8 : symbolic::node_manager node_mgr;
4349 8 : region_model model (node_mgr);
4350 :
4351 8 : ADD_SAT_CONSTRAINT (model, a, EQ_EXPR, int_0);
4352 8 : ASSERT_EQ (model.get_constraints ()->m_equiv_classes.length (), 1);
4353 8 : ASSERT_EQ (model.get_constraints ()->m_constraints.length (), 0);
4354 :
4355 : /* Purge state for "a". */
4356 8 : const svalue *sval_a = model.get_rvalue (a, nullptr);
4357 8 : model.purge_state_involving (*sval_a, nullptr);
4358 8 : model.canonicalize ();
4359 : /* We should have an empty constraint_manager. */
4360 8 : ASSERT_EQ (model.get_constraints ()->m_equiv_classes.length (), 0);
4361 8 : ASSERT_EQ (model.get_constraints ()->m_constraints.length (), 0);
4362 8 : }
4363 :
4364 : /* "a == 0" && "b != 0". */
4365 8 : {
4366 8 : symbolic::node_manager node_mgr;
4367 8 : region_model model (node_mgr);
4368 :
4369 8 : ADD_SAT_CONSTRAINT (model, a, EQ_EXPR, int_0);
4370 8 : ADD_SAT_CONSTRAINT (model, b, NE_EXPR, int_0);
4371 8 : ASSERT_EQ (model.get_constraints ()->m_equiv_classes.length (), 2);
4372 8 : ASSERT_EQ (model.get_constraints ()->m_constraints.length (), 1);
4373 :
4374 : /* Purge state for "a". */
4375 8 : const svalue *sval_a = model.get_rvalue (a, nullptr);
4376 8 : model.purge_state_involving (*sval_a, nullptr);
4377 8 : model.canonicalize ();
4378 : /* We should just have the constraint/ECs involving b != 0. */
4379 8 : ASSERT_EQ (model.get_constraints ()->m_equiv_classes.length (), 2);
4380 8 : ASSERT_EQ (model.get_constraints ()->m_constraints.length (), 1);
4381 8 : ASSERT_CONDITION_TRUE (model, b, NE_EXPR, int_0);
4382 8 : }
4383 :
4384 : /* "a == 0" && "b == 0". */
4385 8 : {
4386 8 : symbolic::node_manager node_mgr;
4387 8 : region_model model (node_mgr);
4388 :
4389 8 : ADD_SAT_CONSTRAINT (model, a, EQ_EXPR, int_0);
4390 8 : ADD_SAT_CONSTRAINT (model, b, EQ_EXPR, int_0);
4391 8 : ASSERT_EQ (model.get_constraints ()->m_equiv_classes.length (), 1);
4392 8 : ASSERT_EQ (model.get_constraints ()->m_constraints.length (), 0);
4393 :
4394 : /* Purge state for "a". */
4395 8 : const svalue *sval_a = model.get_rvalue (a, nullptr);
4396 8 : model.purge_state_involving (*sval_a, nullptr);
4397 8 : model.canonicalize ();
4398 : /* We should just have the EC involving b == 0. */
4399 8 : ASSERT_EQ (model.get_constraints ()->m_equiv_classes.length (), 1);
4400 8 : ASSERT_EQ (model.get_constraints ()->m_constraints.length (), 0);
4401 8 : ASSERT_CONDITION_TRUE (model, b, EQ_EXPR, int_0);
4402 8 : }
4403 8 : }
4404 :
4405 : /* Implementation detail of ASSERT_DUMP_BOUNDED_RANGES_EQ. */
4406 :
4407 : static void
4408 64 : assert_dump_bounded_range_eq (const location &loc,
4409 : const bounded_range &range,
4410 : const char *expected)
4411 : {
4412 64 : auto_fix_quotes sentinel;
4413 64 : pretty_printer pp;
4414 64 : pp_format_decoder (&pp) = default_tree_printer;
4415 64 : range.dump_to_pp (&pp, false);
4416 64 : ASSERT_STREQ_AT (loc, pp_formatted_text (&pp), expected);
4417 64 : }
4418 :
4419 : /* Assert that BR.dump (false) is EXPECTED. */
4420 :
4421 : #define ASSERT_DUMP_BOUNDED_RANGE_EQ(BR, EXPECTED) \
4422 : SELFTEST_BEGIN_STMT \
4423 : assert_dump_bounded_range_eq ((SELFTEST_LOCATION), (BR), (EXPECTED)); \
4424 : SELFTEST_END_STMT
4425 :
4426 : /* Verify that bounded_range works as expected. */
4427 :
4428 : static void
4429 8 : test_bounded_range ()
4430 : {
4431 8 : tree u8_0 = build_int_cst (unsigned_char_type_node, 0);
4432 8 : tree u8_1 = build_int_cst (unsigned_char_type_node, 1);
4433 8 : tree u8_64 = build_int_cst (unsigned_char_type_node, 64);
4434 8 : tree u8_128 = build_int_cst (unsigned_char_type_node, 128);
4435 8 : tree u8_255 = build_int_cst (unsigned_char_type_node, 255);
4436 :
4437 8 : tree s8_0 = build_int_cst (signed_char_type_node, 0);
4438 8 : tree s8_1 = build_int_cst (signed_char_type_node, 1);
4439 8 : tree s8_2 = build_int_cst (signed_char_type_node, 2);
4440 :
4441 8 : bounded_range br_u8_0 (u8_0, u8_0);
4442 8 : ASSERT_DUMP_BOUNDED_RANGE_EQ (br_u8_0, "0");
4443 8 : ASSERT_TRUE (br_u8_0.contains_p (u8_0));
4444 8 : ASSERT_FALSE (br_u8_0.contains_p (u8_1));
4445 8 : ASSERT_TRUE (br_u8_0.contains_p (s8_0));
4446 8 : ASSERT_FALSE (br_u8_0.contains_p (s8_1));
4447 :
4448 8 : bounded_range br_u8_0_1 (u8_0, u8_1);
4449 8 : ASSERT_DUMP_BOUNDED_RANGE_EQ (br_u8_0_1, "[0, 1]");
4450 :
4451 8 : bounded_range tmp (NULL_TREE, NULL_TREE);
4452 8 : ASSERT_TRUE (br_u8_0.intersects_p (br_u8_0_1, &tmp));
4453 8 : ASSERT_DUMP_BOUNDED_RANGE_EQ (tmp, "0");
4454 :
4455 8 : bounded_range br_u8_64_128 (u8_64, u8_128);
4456 8 : ASSERT_DUMP_BOUNDED_RANGE_EQ (br_u8_64_128, "[64, 128]");
4457 :
4458 8 : ASSERT_FALSE (br_u8_0.intersects_p (br_u8_64_128, nullptr));
4459 8 : ASSERT_FALSE (br_u8_64_128.intersects_p (br_u8_0, nullptr));
4460 :
4461 8 : bounded_range br_u8_128_255 (u8_128, u8_255);
4462 8 : ASSERT_DUMP_BOUNDED_RANGE_EQ (br_u8_128_255, "[128, 255]");
4463 8 : ASSERT_TRUE (br_u8_128_255.intersects_p (br_u8_64_128, &tmp));
4464 8 : ASSERT_DUMP_BOUNDED_RANGE_EQ (tmp, "128");
4465 :
4466 8 : bounded_range br_s8_2 (s8_2, s8_2);
4467 8 : ASSERT_DUMP_BOUNDED_RANGE_EQ (br_s8_2, "2");
4468 8 : bounded_range br_s8_2_u8_255 (s8_2, u8_255);
4469 8 : ASSERT_DUMP_BOUNDED_RANGE_EQ (br_s8_2_u8_255, "[2, 255]");
4470 8 : }
4471 :
4472 : /* Implementation detail of ASSERT_DUMP_BOUNDED_RANGES_EQ. */
4473 :
4474 : static void
4475 208 : assert_dump_bounded_ranges_eq (const location &loc,
4476 : const bounded_ranges *ranges,
4477 : const char *expected)
4478 : {
4479 208 : auto_fix_quotes sentinel;
4480 208 : pretty_printer pp;
4481 208 : pp_format_decoder (&pp) = default_tree_printer;
4482 208 : ranges->dump_to_pp (&pp, false);
4483 208 : ASSERT_STREQ_AT (loc, pp_formatted_text (&pp), expected);
4484 208 : }
4485 :
4486 : /* Implementation detail of ASSERT_DUMP_BOUNDED_RANGES_EQ. */
4487 :
4488 : static void
4489 96 : assert_dump_bounded_ranges_eq (const location &loc,
4490 : const bounded_ranges &ranges,
4491 : const char *expected)
4492 : {
4493 96 : auto_fix_quotes sentinel;
4494 96 : pretty_printer pp;
4495 96 : pp_format_decoder (&pp) = default_tree_printer;
4496 96 : ranges.dump_to_pp (&pp, false);
4497 96 : ASSERT_STREQ_AT (loc, pp_formatted_text (&pp), expected);
4498 96 : }
4499 :
4500 : /* Assert that BRS.dump (false) is EXPECTED. */
4501 :
4502 : #define ASSERT_DUMP_BOUNDED_RANGES_EQ(BRS, EXPECTED) \
4503 : SELFTEST_BEGIN_STMT \
4504 : assert_dump_bounded_ranges_eq ((SELFTEST_LOCATION), (BRS), (EXPECTED)); \
4505 : SELFTEST_END_STMT
4506 :
4507 : /* Verify that the bounded_ranges class works as expected. */
4508 :
4509 : static void
4510 8 : test_bounded_ranges ()
4511 : {
4512 8 : bounded_ranges_manager mgr;
4513 :
4514 8 : tree ch0 = build_int_cst (unsigned_char_type_node, 0);
4515 8 : tree ch1 = build_int_cst (unsigned_char_type_node, 1);
4516 8 : tree ch2 = build_int_cst (unsigned_char_type_node, 2);
4517 8 : tree ch3 = build_int_cst (unsigned_char_type_node, 3);
4518 8 : tree ch128 = build_int_cst (unsigned_char_type_node, 128);
4519 8 : tree ch129 = build_int_cst (unsigned_char_type_node, 129);
4520 8 : tree ch254 = build_int_cst (unsigned_char_type_node, 254);
4521 8 : tree ch255 = build_int_cst (unsigned_char_type_node, 255);
4522 :
4523 8 : const bounded_ranges *empty = mgr.get_or_create_empty ();
4524 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (empty, "{}");
4525 :
4526 8 : const bounded_ranges *point0 = mgr.get_or_create_point (ch0);
4527 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (point0, "{0}");
4528 :
4529 8 : const bounded_ranges *point1 = mgr.get_or_create_point (ch1);
4530 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (point1, "{1}");
4531 :
4532 8 : const bounded_ranges *point2 = mgr.get_or_create_point (ch2);
4533 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (point2, "{2}");
4534 :
4535 8 : const bounded_ranges *range0_128 = mgr.get_or_create_range (ch0, ch128);
4536 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (range0_128, "{[0, 128]}");
4537 :
4538 8 : const bounded_ranges *range0_255 = mgr.get_or_create_range (ch0, ch255);
4539 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (range0_255, "{[0, 255]}");
4540 :
4541 8 : ASSERT_FALSE (empty->contain_p (ch0));
4542 8 : ASSERT_FALSE (empty->contain_p (ch1));
4543 8 : ASSERT_FALSE (empty->contain_p (ch255));
4544 :
4545 8 : ASSERT_TRUE (point0->contain_p (ch0));
4546 8 : ASSERT_FALSE (point0->contain_p (ch1));
4547 8 : ASSERT_FALSE (point0->contain_p (ch255));
4548 :
4549 8 : ASSERT_FALSE (point1->contain_p (ch0));
4550 8 : ASSERT_TRUE (point1->contain_p (ch1));
4551 8 : ASSERT_FALSE (point0->contain_p (ch255));
4552 :
4553 8 : ASSERT_TRUE (range0_128->contain_p (ch0));
4554 8 : ASSERT_TRUE (range0_128->contain_p (ch1));
4555 8 : ASSERT_TRUE (range0_128->contain_p (ch128));
4556 8 : ASSERT_FALSE (range0_128->contain_p (ch129));
4557 8 : ASSERT_FALSE (range0_128->contain_p (ch254));
4558 8 : ASSERT_FALSE (range0_128->contain_p (ch255));
4559 :
4560 8 : const bounded_ranges *inv0_128
4561 8 : = mgr.get_or_create_inverse (range0_128, unsigned_char_type_node);
4562 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (inv0_128, "{[129, 255]}");
4563 :
4564 8 : const bounded_ranges *range128_129 = mgr.get_or_create_range (ch128, ch129);
4565 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (range128_129, "{[128, 129]}");
4566 :
4567 8 : const bounded_ranges *inv128_129
4568 8 : = mgr.get_or_create_inverse (range128_129, unsigned_char_type_node);
4569 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (inv128_129, "{[0, 127], [130, 255]}");
4570 :
4571 : /* Intersection. */
4572 8 : {
4573 : /* Intersection of disjoint ranges should be empty set. */
4574 8 : const bounded_ranges *intersect0_1
4575 8 : = mgr.get_or_create_intersection (point0, point1);
4576 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (intersect0_1, "{}");
4577 : }
4578 :
4579 : /* Various tests of "union of ranges". */
4580 8 : {
4581 8 : {
4582 : /* Touching points should be merged into a range. */
4583 8 : auto_vec <const bounded_ranges *> v;
4584 8 : v.safe_push (point0);
4585 8 : v.safe_push (point1);
4586 8 : const bounded_ranges *union_0_and_1 = mgr.get_or_create_union (v);
4587 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (union_0_and_1, "{[0, 1]}");
4588 8 : }
4589 :
4590 8 : {
4591 : /* Overlapping and out-of-order. */
4592 8 : auto_vec <const bounded_ranges *> v;
4593 8 : v.safe_push (inv0_128); // {[129, 255]}
4594 8 : v.safe_push (range128_129);
4595 8 : const bounded_ranges *union_129_255_and_128_129
4596 8 : = mgr.get_or_create_union (v);
4597 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (union_129_255_and_128_129, "{[128, 255]}");
4598 8 : }
4599 :
4600 8 : {
4601 : /* Union of R and inverse(R) should be full range of type. */
4602 8 : auto_vec <const bounded_ranges *> v;
4603 8 : v.safe_push (range128_129);
4604 8 : v.safe_push (inv128_129);
4605 8 : const bounded_ranges *union_ = mgr.get_or_create_union (v);
4606 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (union_, "{[0, 255]}");
4607 8 : }
4608 :
4609 : /* Union with an endpoint. */
4610 8 : {
4611 8 : const bounded_ranges *range2_to_255
4612 8 : = mgr.get_or_create_range (ch2, ch255);
4613 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (range2_to_255, "{[2, 255]}");
4614 8 : auto_vec <const bounded_ranges *> v;
4615 8 : v.safe_push (point0);
4616 8 : v.safe_push (point2);
4617 8 : v.safe_push (range2_to_255);
4618 8 : const bounded_ranges *union_ = mgr.get_or_create_union (v);
4619 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (union_, "{0, [2, 255]}");
4620 8 : }
4621 :
4622 : /* Construct from vector of bounded_range. */
4623 8 : {
4624 8 : auto_vec<bounded_range> v;
4625 8 : v.safe_push (bounded_range (ch2, ch2));
4626 8 : v.safe_push (bounded_range (ch0, ch0));
4627 8 : v.safe_push (bounded_range (ch2, ch255));
4628 8 : bounded_ranges br (v);
4629 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (&br, "{0, [2, 255]}");
4630 8 : }
4631 : }
4632 :
4633 : /* Various tests of "inverse". */
4634 8 : {
4635 8 : {
4636 8 : const bounded_ranges *range_1_to_3 = mgr.get_or_create_range (ch1, ch3);
4637 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (range_1_to_3, "{[1, 3]}");
4638 8 : const bounded_ranges *inv
4639 8 : = mgr.get_or_create_inverse (range_1_to_3, unsigned_char_type_node);
4640 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (inv, "{0, [4, 255]}");
4641 : }
4642 8 : {
4643 8 : const bounded_ranges *range_1_to_255
4644 8 : = mgr.get_or_create_range (ch1, ch255);
4645 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (range_1_to_255, "{[1, 255]}");
4646 8 : const bounded_ranges *inv
4647 8 : = mgr.get_or_create_inverse (range_1_to_255, unsigned_char_type_node);
4648 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (inv, "{0}");
4649 : }
4650 8 : {
4651 8 : const bounded_ranges *range_0_to_254
4652 8 : = mgr.get_or_create_range (ch0, ch254);
4653 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (range_0_to_254, "{[0, 254]}");
4654 8 : const bounded_ranges *inv
4655 8 : = mgr.get_or_create_inverse (range_0_to_254, unsigned_char_type_node);
4656 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (inv, "{255}");
4657 : }
4658 : }
4659 :
4660 : /* "case 'a'-'z': case 'A-Z':" vs "default:", for ASCII. */
4661 8 : {
4662 8 : tree ch65 = build_int_cst (unsigned_char_type_node, 65);
4663 8 : tree ch90 = build_int_cst (unsigned_char_type_node, 90);
4664 :
4665 8 : tree ch97 = build_int_cst (unsigned_char_type_node, 97);
4666 8 : tree ch122 = build_int_cst (unsigned_char_type_node, 122);
4667 :
4668 8 : const bounded_ranges *A_to_Z = mgr.get_or_create_range (ch65, ch90);
4669 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (A_to_Z, "{[65, 90]}");
4670 8 : const bounded_ranges *a_to_z = mgr.get_or_create_range (ch97, ch122);
4671 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (a_to_z, "{[97, 122]}");
4672 8 : auto_vec <const bounded_ranges *> v;
4673 8 : v.safe_push (A_to_Z);
4674 8 : v.safe_push (a_to_z);
4675 8 : const bounded_ranges *label_ranges = mgr.get_or_create_union (v);
4676 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (label_ranges, "{[65, 90], [97, 122]}");
4677 8 : const bounded_ranges *default_ranges
4678 8 : = mgr.get_or_create_inverse (label_ranges, unsigned_char_type_node);
4679 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (default_ranges,
4680 : "{[0, 64], [91, 96], [123, 255]}");
4681 8 : }
4682 :
4683 : /* Verify ranges from ops. */
4684 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (bounded_ranges (EQ_EXPR, ch128),
4685 : "{128}");
4686 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (bounded_ranges (NE_EXPR, ch128),
4687 : "{[0, 127], [129, 255]}");
4688 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (bounded_ranges (LT_EXPR, ch128),
4689 : "{[0, 127]}");
4690 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (bounded_ranges (LE_EXPR, ch128),
4691 : "{[0, 128]}");
4692 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (bounded_ranges (GE_EXPR, ch128),
4693 : "{[128, 255]}");
4694 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (bounded_ranges (GT_EXPR, ch128),
4695 : "{[129, 255]}");
4696 : /* Ops at endpoints of type ranges. */
4697 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (bounded_ranges (LE_EXPR, ch0),
4698 : "{0}");
4699 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (bounded_ranges (LT_EXPR, ch0),
4700 : "{}");
4701 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (bounded_ranges (NE_EXPR, ch0),
4702 : "{[1, 255]}");
4703 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (bounded_ranges (GE_EXPR, ch255),
4704 : "{255}");
4705 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (bounded_ranges (GT_EXPR, ch255),
4706 : "{}");
4707 8 : ASSERT_DUMP_BOUNDED_RANGES_EQ (bounded_ranges (NE_EXPR, ch255),
4708 : "{[0, 254]}");
4709 :
4710 : /* Verify that instances are consolidated by mgr. */
4711 8 : ASSERT_EQ (mgr.get_or_create_point (ch0),
4712 : mgr.get_or_create_point (ch0));
4713 8 : ASSERT_NE (mgr.get_or_create_point (ch0),
4714 : mgr.get_or_create_point (ch1));
4715 8 : }
4716 :
4717 : /* Verify that we can handle sufficiently simple bitmasking operations. */
4718 :
4719 : static void
4720 8 : test_bits (void)
4721 : {
4722 8 : symbolic::node_manager node_mgr;
4723 :
4724 8 : tree int_0 = integer_zero_node;
4725 8 : tree int_0x80 = build_int_cst (integer_type_node, 0x80);
4726 8 : tree int_0xff = build_int_cst (integer_type_node, 0xff);
4727 8 : tree x = build_global_decl ("x", integer_type_node);
4728 :
4729 8 : tree x_bit_and_0x80 = build2 (BIT_AND_EXPR, integer_type_node, x, int_0x80);
4730 8 : tree x_bit_and_0xff = build2 (BIT_AND_EXPR, integer_type_node, x, int_0xff);
4731 :
4732 : /* "x & 0x80 == 0x80". */
4733 8 : {
4734 8 : region_model model (node_mgr);
4735 8 : ADD_SAT_CONSTRAINT (model, x_bit_and_0x80, EQ_EXPR, int_0x80);
4736 8 : ASSERT_CONDITION_FALSE (model, x, EQ_EXPR, int_0);
4737 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, int_0x80);
4738 8 : }
4739 :
4740 : /* "x & 0x80 != 0x80". */
4741 8 : {
4742 8 : region_model model (node_mgr);
4743 8 : ADD_SAT_CONSTRAINT (model, x_bit_and_0x80, NE_EXPR, int_0x80);
4744 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, int_0);
4745 8 : ASSERT_CONDITION_FALSE (model, x, EQ_EXPR, int_0x80);
4746 8 : }
4747 :
4748 : /* "x & 0x80 == 0". */
4749 8 : {
4750 8 : region_model model (node_mgr);
4751 :
4752 8 : ADD_SAT_CONSTRAINT (model, x_bit_and_0x80, EQ_EXPR, int_0);
4753 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, int_0);
4754 8 : ASSERT_CONDITION_FALSE (model, x, EQ_EXPR, int_0x80);
4755 8 : }
4756 :
4757 : /* "x & 0x80 != 0". */
4758 8 : {
4759 8 : region_model model (node_mgr);
4760 8 : ADD_SAT_CONSTRAINT (model, x_bit_and_0x80, NE_EXPR, int_0);
4761 8 : ASSERT_CONDITION_FALSE (model, x, EQ_EXPR, int_0);
4762 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, int_0x80);
4763 8 : }
4764 :
4765 : /* More that one bit in the mask. */
4766 :
4767 : /* "x & 0xff == 0x80". */
4768 8 : {
4769 8 : region_model model (node_mgr);
4770 8 : ADD_SAT_CONSTRAINT (model, x_bit_and_0xff, EQ_EXPR, int_0x80);
4771 8 : ASSERT_CONDITION_FALSE (model, x, EQ_EXPR, int_0);
4772 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, int_0x80);
4773 8 : ASSERT_CONDITION_FALSE (model, x, EQ_EXPR, int_0xff);
4774 8 : }
4775 :
4776 : /* "x & 0xff != 0x80". */
4777 8 : {
4778 8 : region_model model (node_mgr);
4779 8 : ADD_SAT_CONSTRAINT (model, x_bit_and_0xff, NE_EXPR, int_0x80);
4780 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, int_0);
4781 8 : ASSERT_CONDITION_FALSE (model, x, EQ_EXPR, int_0x80);
4782 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, int_0xff);
4783 8 : }
4784 :
4785 : /* "x & 0xff == 0". */
4786 8 : {
4787 8 : region_model model (node_mgr);
4788 :
4789 8 : ADD_SAT_CONSTRAINT (model, x_bit_and_0xff, EQ_EXPR, int_0);
4790 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, int_0);
4791 8 : ASSERT_CONDITION_FALSE (model, x, EQ_EXPR, int_0x80);
4792 8 : ASSERT_CONDITION_FALSE (model, x, EQ_EXPR, int_0xff);
4793 8 : }
4794 :
4795 : /* "x & 0xff != 0". */
4796 8 : {
4797 8 : region_model model (node_mgr);
4798 8 : ADD_SAT_CONSTRAINT (model, x_bit_and_0xff, NE_EXPR, int_0);
4799 8 : ASSERT_CONDITION_FALSE (model, x, EQ_EXPR, int_0);
4800 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, int_0x80);
4801 8 : ASSERT_CONDITION_UNKNOWN (model, x, EQ_EXPR, int_0xff);
4802 8 : }
4803 8 : }
4804 :
4805 : /* Run the selftests in this file, temporarily overriding
4806 : flag_analyzer_transitivity with TRANSITIVITY. */
4807 :
4808 : static void
4809 8 : run_constraint_manager_tests (bool transitivity)
4810 : {
4811 8 : int saved_flag_analyzer_transitivity = flag_analyzer_transitivity;
4812 8 : flag_analyzer_transitivity = transitivity;
4813 :
4814 8 : test_range ();
4815 8 : test_constraint_conditions ();
4816 8 : if (flag_analyzer_transitivity)
4817 : {
4818 : /* These selftests assume transitivity. */
4819 4 : test_transitivity ();
4820 : }
4821 8 : test_constant_comparisons ();
4822 8 : test_constraint_impl ();
4823 8 : test_equality ();
4824 8 : test_many_constants ();
4825 8 : test_purging ();
4826 8 : test_bounded_range ();
4827 8 : test_bounded_ranges ();
4828 8 : test_bits ();
4829 :
4830 8 : flag_analyzer_transitivity = saved_flag_analyzer_transitivity;
4831 8 : }
4832 :
4833 : /* Run all of the selftests within this file. */
4834 :
4835 : void
4836 4 : analyzer_constraint_manager_cc_tests ()
4837 : {
4838 : /* Run the tests twice: with and without transitivity. */
4839 4 : run_constraint_manager_tests (true);
4840 4 : run_constraint_manager_tests (false);
4841 4 : }
4842 :
4843 : } // namespace selftest
4844 :
4845 : #endif /* CHECKING_P */
4846 :
4847 : } // namespace ana
4848 :
4849 : #endif /* #if ENABLE_ANALYZER */
|