Debian Codegraph @

gcc/gimple-predicate-analysis.cc

raw
1 /* Support for simple predicate analysis.
2
3 Copyright (C) 2001-2025 Free Software Foundation, Inc.
4 Contributed by Xinliang David Li <[email protected]>
5 Generalized by Martin Sebor <[email protected]>
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #define INCLUDE_STRING
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "backend.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "tree-pass.h"
31 #include "ssa.h"
32 #include "gimple-pretty-print.h"
33 #include "diagnostic-core.h"
34 #include "fold-const.h"
35 #include "gimple-iterator.h"
36 #include "tree-ssa.h"
37 #include "tree-cfg.h"
38 #include "cfghooks.h"
39 #include "attribs.h"
40 #include "builtins.h"
41 #include "calls.h"
42 #include "value-query.h"
43 #include "cfganal.h"
44 #include "tree-eh.h"
45 #include "gimple-fold.h"
46
47 #include "gimple-predicate-analysis.h"
48
49 #define DEBUG_PREDICATE_ANALYZER 1
50
51 /* In our predicate normal form we have MAX_NUM_CHAINS or predicates
52 and in those MAX_CHAIN_LEN (inverted) and predicates. */
53 #define MAX_NUM_CHAINS (unsigned)param_uninit_max_num_chains
54 #define MAX_CHAIN_LEN (unsigned)param_uninit_max_chain_len
55
56 /* Return true if X1 is the negation of X2. */
57
58 static inline bool
59 pred_neg_p (const pred_info &x1, const pred_info &x2)
60 {
61 if (!operand_equal_p (x1.pred_lhs, x2.pred_lhs, 0)
62 || !operand_equal_p (x1.pred_rhs, x2.pred_rhs, 0))
63 return false;
64
65 tree_code c1 = x1.cond_code, c2;
66 if (x1.invert == x2.invert)
67 c2 = invert_tree_comparison (x2.cond_code, false);
68 else
69 c2 = x2.cond_code;
70
71 return c1 == c2;
72 }
73
74 /* Return whether the condition (VAL CMPC BOUNDARY) is true. */
75
76 static bool
77 is_value_included_in (tree val, tree boundary, tree_code cmpc)
78 {
79 /* Only handle integer constant here. */
80 if (TREE_CODE (val) != INTEGER_CST || TREE_CODE (boundary) != INTEGER_CST)
81 return true;
82
83 bool inverted = false;
84 if (cmpc == GE_EXPR || cmpc == GT_EXPR || cmpc == NE_EXPR)
85 {
86 cmpc = invert_tree_comparison (cmpc, false);
87 inverted = true;
88 }
89
90 bool result;
91 if (cmpc == EQ_EXPR)
92 result = tree_int_cst_equal (val, boundary);
93 else if (cmpc == LT_EXPR)
94 result = tree_int_cst_lt (val, boundary);
95 else
96 {
97 gcc_assert (cmpc == LE_EXPR);
98 result = tree_int_cst_le (val, boundary);
99 }
100
101 if (inverted)
102 result ^= 1;
103
104 return result;
105 }
106
107 /* Format the vector of edges EV as a string. */
108
109 static std::string
110 format_edge_vec (const vec<edge> &ev)
111 {
112 std::string str;
113
114 unsigned n = ev.length ();
115 for (unsigned i = 0; i < n; ++i)
116 {
117 char es[32];
118 const_edge e = ev[i];
119 sprintf (es, "%u -> %u", e->src->index, e->dest->index);
120 str += es;
121 if (i + 1 < n)
122 str += ", ";
123 }
124 return str;
125 }
126
127 /* Format the first N elements of the array of vector of edges EVA as
128 a string. */
129
130 static std::string
131 format_edge_vecs (const vec<edge> eva[], unsigned n)
132 {
133 std::string str;
134
135 for (unsigned i = 0; i != n; ++i)
136 {
137 str += '{';
138 str += format_edge_vec (eva[i]);
139 str += '}';
140 if (i + 1 < n)
141 str += ", ";
142 }
143 return str;
144 }
145
146 /* Dump a single pred_info to F. */
147
148 static void
149 dump_pred_info (FILE *f, const pred_info &pred)
150 {
151 if (pred.invert)
152 fprintf (f, "NOT (");
153 print_generic_expr (f, pred.pred_lhs);
154 fprintf (f, " %s ", op_symbol_code (pred.cond_code));
155 print_generic_expr (f, pred.pred_rhs);
156 if (pred.invert)
157 fputc (')', f);
158 }
159
160 /* Dump a pred_chain to F. */
161
162 static void
163 dump_pred_chain (FILE *f, const pred_chain &chain)
164 {
165 unsigned np = chain.length ();
166 for (unsigned j = 0; j < np; j++)
167 {
168 if (j > 0)
169 fprintf (f, " AND (");
170 else
171 fputc ('(', f);
172 dump_pred_info (f, chain[j]);
173 fputc (')', f);
174 }
175 }
176
177 /* Return the 'normalized' conditional code with operand swapping
178 and condition inversion controlled by SWAP_COND and INVERT. */
179
180 static tree_code
181 get_cmp_code (tree_code orig_cmp_code, bool swap_cond, bool invert)
182 {
183 tree_code tc = orig_cmp_code;
184
185 if (swap_cond)
186 tc = swap_tree_comparison (orig_cmp_code);
187 if (invert)
188 tc = invert_tree_comparison (tc, false);
189
190 switch (tc)
191 {
192 case LT_EXPR:
193 case LE_EXPR:
194 case GT_EXPR:
195 case GE_EXPR:
196 case EQ_EXPR:
197 case NE_EXPR:
198 break;
199 default:
200 return ERROR_MARK;
201 }
202 return tc;
203 }
204
205 /* Return true if PRED is common among all predicate chains in PREDS
206 (and therefore can be factored out). */
207
208 static bool
209 find_matching_predicate_in_rest_chains (const pred_info &pred,
210 const pred_chain_union &preds)
211 {
212 /* Trival case. */
213 if (preds.length () == 1)
214 return true;
215
216 for (unsigned i = 1; i < preds.length (); i++)
217 {
218 bool found = false;
219 const pred_chain &chain = preds[i];
220 unsigned n = chain.length ();
221 for (unsigned j = 0; j < n; j++)
222 {
223 const pred_info &pred2 = chain[j];
224 /* Can relax the condition comparison to not use address
225 comparison. However, the most common case is that
226 multiple control dependent paths share a common path
227 prefix, so address comparison should be ok. */
228 if (operand_equal_p (pred2.pred_lhs, pred.pred_lhs, 0)
229 && operand_equal_p (pred2.pred_rhs, pred.pred_rhs, 0)
230 && pred2.invert == pred.invert)
231 {
232 found = true;
233 break;
234 }
235 }
236 if (!found)
237 return false;
238 }
239 return true;
240 }
241
242 /* Find a predicate to examine against paths of interest. If there
243 is no predicate of the "FLAG_VAR CMP CONST" form, try to find one
244 of that's the form "FLAG_VAR CMP FLAG_VAR" with value range info.
245 PHI is the phi node whose incoming (interesting) paths need to be
246 examined. On success, return the comparison code, set defintion
247 gimple of FLAG_DEF and BOUNDARY_CST. Otherwise return ERROR_MARK.
248 I is the running iterator so the function can be called repeatedly
249 to gather all candidates. */
250
251 static tree_code
252 find_var_cmp_const (pred_chain_union preds, gphi *phi, gimple **flag_def,
253 tree *boundary_cst, unsigned &i)
254 {
255 gcc_assert (preds.length () > 0);
256 pred_chain chain = preds[0];
257 for (; i < chain.length (); i++)
258 {
259 const pred_info &pred = chain[i];
260 tree cond_lhs = pred.pred_lhs;
261 tree cond_rhs = pred.pred_rhs;
262 if (cond_lhs == NULL_TREE || cond_rhs == NULL_TREE)
263 continue;
264
265 tree_code code = get_cmp_code (pred.cond_code, false, pred.invert);
266 if (code == ERROR_MARK)
267 continue;
268
269 /* Convert to the canonical form SSA_NAME CMP CONSTANT. */
270 if (TREE_CODE (cond_lhs) == SSA_NAME
271 && is_gimple_constant (cond_rhs))
272 ;
273 else if (TREE_CODE (cond_rhs) == SSA_NAME
274 && is_gimple_constant (cond_lhs))
275 {
276 std::swap (cond_lhs, cond_rhs);
277 if ((code = get_cmp_code (code, true, false)) == ERROR_MARK)
278 continue;
279 }
280 /* Check if we can take advantage of FLAG_VAR COMP FLAG_VAR predicate
281 with value range info. Note only first of such case is handled. */
282 else if (TREE_CODE (cond_lhs) == SSA_NAME
283 && TREE_CODE (cond_rhs) == SSA_NAME)
284 {
285 gimple* lhs_def = SSA_NAME_DEF_STMT (cond_lhs);
286 if (!lhs_def || gimple_code (lhs_def) != GIMPLE_PHI
287 || gimple_bb (lhs_def) != gimple_bb (phi))
288 {
289 std::swap (cond_lhs, cond_rhs);
290 if ((code = get_cmp_code (code, true, false)) == ERROR_MARK)
291 continue;
292 }
293
294 /* Check value range info of rhs, do following transforms:
295 flag_var < [min, max] -> flag_var < max
296 flag_var > [min, max] -> flag_var > min
297
298 We can also transform LE_EXPR/GE_EXPR to LT_EXPR/GT_EXPR:
299 flag_var <= [min, max] -> flag_var < [min, max+1]
300 flag_var >= [min, max] -> flag_var > [min-1, max]
301 if no overflow/wrap. */
302 tree type = TREE_TYPE (cond_lhs);
303 int_range_max r;
304 if (!INTEGRAL_TYPE_P (type)
305 || !get_range_query (cfun)->range_of_expr (r, cond_rhs)
306 || r.undefined_p ()
307 || r.varying_p ())
308 continue;
309
310 wide_int min = r.lower_bound ();
311 wide_int max = r.upper_bound ();
312 if (code == LE_EXPR
313 && max != wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type)))
314 {
315 code = LT_EXPR;
316 max = max + 1;
317 }
318 if (code == GE_EXPR
319 && min != wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type)))
320 {
321 code = GT_EXPR;
322 min = min - 1;
323 }
324 if (code == LT_EXPR)
325 cond_rhs = wide_int_to_tree (type, max);
326 else if (code == GT_EXPR)
327 cond_rhs = wide_int_to_tree (type, min);
328 else
329 continue;
330 }
331 else
332 continue;
333
334 if ((*flag_def = SSA_NAME_DEF_STMT (cond_lhs)) == NULL)
335 continue;
336
337 if (gimple_code (*flag_def) != GIMPLE_PHI
338 || gimple_bb (*flag_def) != gimple_bb (phi)
339 || !find_matching_predicate_in_rest_chains (pred, preds))
340 continue;
341
342 /* Return predicate found. */
343 *boundary_cst = cond_rhs;
344 ++i;
345 return code;
346 }
347
348 return ERROR_MARK;
349 }
350
351 /* Return true if all interesting opnds are pruned, false otherwise.
352 PHI is the phi node with interesting operands, OPNDS is the bitmap
353 of the interesting operand positions, FLAG_DEF is the statement
354 defining the flag guarding the use of the PHI output, BOUNDARY_CST
355 is the const value used in the predicate associated with the flag,
356 CMP_CODE is the comparison code used in the predicate, VISITED_PHIS
357 is the pointer set of phis visited, and VISITED_FLAG_PHIS is
358 the pointer to the pointer set of flag definitions that are also
359 phis.
360
361 Example scenario:
362
363 BB1:
364 flag_1 = phi <0, 1> // (1)
365 var_1 = phi <undef, some_val>
366
367
368 BB2:
369 flag_2 = phi <0, flag_1, flag_1> // (2)
370 var_2 = phi <undef, var_1, var_1>
371 if (flag_2 == 1)
372 goto BB3;
373
374 BB3:
375 use of var_2 // (3)
376
377 Because some flag arg in (1) is not constant, if we do not look into
378 the flag phis recursively, it is conservatively treated as unknown and
379 var_1 is thought to flow into use at (3). Since var_1 is potentially
380 uninitialized a false warning will be emitted.
381 Checking recursively into (1), the compiler can find out that only
382 some_val (which is defined) can flow into (3) which is OK. */
383
384 bool
385 uninit_analysis::prune_phi_opnds (gphi *phi, unsigned opnds, gphi *flag_def,
386 tree boundary_cst, tree_code cmp_code,
387 hash_set<gphi *> *visited_phis,
388 bitmap *visited_flag_phis,
389 unsigned &max_attempts)
390 {
391 /* The Boolean predicate guarding the PHI definition. Initialized
392 lazily from PHI in the first call to is_use_guarded() and cached
393 for subsequent iterations. */
394 uninit_analysis def_preds (m_eval);
395
396 unsigned n = MIN (m_eval.max_phi_args, gimple_phi_num_args (flag_def));
397 for (unsigned i = 0; i < n; i++)
398 {
399 if (!MASK_TEST_BIT (opnds, i))
400 continue;
401
402 if (max_attempts == 0)
403 return false;
404 --max_attempts;
405
406 tree flag_arg = gimple_phi_arg_def (flag_def, i);
407 if (!is_gimple_constant (flag_arg))
408 {
409 if (TREE_CODE (flag_arg) != SSA_NAME)
410 return false;
411
412 gphi *flag_arg_def = dyn_cast<gphi *> (SSA_NAME_DEF_STMT (flag_arg));
413 if (!flag_arg_def)
414 return false;
415
416 tree phi_arg = gimple_phi_arg_def (phi, i);
417 if (TREE_CODE (phi_arg) != SSA_NAME)
418 return false;
419
420 gphi *phi_arg_def = dyn_cast<gphi *> (SSA_NAME_DEF_STMT (phi_arg));
421 if (!phi_arg_def)
422 return false;
423
424 if (gimple_bb (phi_arg_def) != gimple_bb (flag_arg_def))
425 return false;
426
427 if (!*visited_flag_phis)
428 *visited_flag_phis = BITMAP_ALLOC (NULL);
429
430 tree phi_result = gimple_phi_result (flag_arg_def);
431 if (bitmap_bit_p (*visited_flag_phis, SSA_NAME_VERSION (phi_result)))
432 return false;
433
434 bitmap_set_bit (*visited_flag_phis, SSA_NAME_VERSION (phi_result));
435
436 /* Now recursively try to prune the interesting phi args. */
437 unsigned opnds_arg_phi = m_eval.phi_arg_set (phi_arg_def);
438 if (!prune_phi_opnds (phi_arg_def, opnds_arg_phi, flag_arg_def,
439 boundary_cst, cmp_code, visited_phis,
440 visited_flag_phis, max_attempts))
441 return false;
442
443 bitmap_clear_bit (*visited_flag_phis, SSA_NAME_VERSION (phi_result));
444 continue;
445 }
446
447 /* Now check if the constant is in the guarded range. */
448 if (is_value_included_in (flag_arg, boundary_cst, cmp_code))
449 {
450 /* Now that we know that this undefined edge is not pruned.
451 If the operand is defined by another phi, we can further
452 prune the incoming edges of that phi by checking
453 the predicates of this operands. */
454
455 tree opnd = gimple_phi_arg_def (phi, i);
456 gimple *opnd_def = SSA_NAME_DEF_STMT (opnd);
457 if (gphi *opnd_def_phi = dyn_cast <gphi *> (opnd_def))
458 {
459 unsigned opnds2 = m_eval.phi_arg_set (opnd_def_phi);
460 if (!MASK_EMPTY (opnds2))
461 {
462 edge opnd_edge = gimple_phi_arg_edge (phi, i);
463 if (def_preds.is_use_guarded (phi, opnd_edge->src,
464 opnd_def_phi, opnds2,
465 visited_phis))
466 return false;
467 }
468 }
469 else
470 return false;
471 }
472 }
473
474 return true;
475 }
476
477 /* Recursively compute the set PHI's incoming edges with "uninteresting"
478 operands of a phi chain, i.e., those for which EVAL returns false.
479 CD_ROOT is the control dependence root from which edges are collected
480 up the CFG nodes that it's dominated by. *EDGES holds the result, and
481 VISITED is used for detecting cycles. */
482
483 void
484 uninit_analysis::collect_phi_def_edges (gphi *phi, basic_block cd_root,
485 vec<edge> *edges,
486 hash_set<gimple *> *visited)
487 {
488 if (visited->elements () == 0
489 && DEBUG_PREDICATE_ANALYZER
490 && dump_file)
491 {
492 fprintf (dump_file, "%s for cd_root %u and ",
493 __func__, cd_root->index);
494 print_gimple_stmt (dump_file, phi, 0);
495
496 }
497
498 if (visited->add (phi))
499 return;
500
501 unsigned n = gimple_phi_num_args (phi);
502 unsigned opnds_arg_phi = m_eval.phi_arg_set (phi);
503 for (unsigned i = 0; i < n; i++)
504 {
505 if (!MASK_TEST_BIT (opnds_arg_phi, i))
506 {
507 /* Add the edge for a not maybe-undefined edge value. */
508 edge opnd_edge = gimple_phi_arg_edge (phi, i);
509 if (dump_file && (dump_flags & TDF_DETAILS))
510 {
511 fprintf (dump_file,
512 "\tFound def edge %i -> %i for cd_root %i "
513 "and operand %u of: ",
514 opnd_edge->src->index, opnd_edge->dest->index,
515 cd_root->index, i);
516 print_gimple_stmt (dump_file, phi, 0);
517 }
518 edges->safe_push (opnd_edge);
519 continue;
520 }
521 else
522 {
523 tree opnd = gimple_phi_arg_def (phi, i);
524 if (TREE_CODE (opnd) == SSA_NAME)
525 {
526 gimple *def = SSA_NAME_DEF_STMT (opnd);
527 if (gimple_code (def) == GIMPLE_PHI
528 && dominated_by_p (CDI_DOMINATORS, gimple_bb (def), cd_root))
529 /* Process PHI defs of maybe-undefined edge values
530 recursively. */
531 collect_phi_def_edges (as_a<gphi *> (def), cd_root, edges,
532 visited);
533 }
534 }
535 }
536 }
537
538 /* Return a bitset of all PHI arguments or zero if there are too many. */
539
540 unsigned
541 uninit_analysis::func_t::phi_arg_set (gphi *phi)
542 {
543 unsigned n = gimple_phi_num_args (phi);
544
545 if (max_phi_args < n)
546 return 0;
547
548 /* Set the least significant N bits. */
549 return (1U << n) - 1;
550 }
551
552 /* Determine if the predicate set of the use does not overlap with that
553 of the interesting paths. The most common senario of guarded use is
554 in Example 1:
555 Example 1:
556 if (some_cond)
557 {
558 x = ...; // set x to valid
559 flag = true;
560 }
561
562 ... some code ...
563
564 if (flag)
565 use (x); // use when x is valid
566
567 The real world examples are usually more complicated, but similar
568 and usually result from inlining:
569
570 bool init_func (int * x)
571 {
572 if (some_cond)
573 return false;
574 *x = ...; // set *x to valid
575 return true;
576 }
577
578 void foo (..)
579 {
580 int x;
581
582 if (!init_func (&x))
583 return;
584
585 .. some_code ...
586 use (x); // use when x is valid
587 }
588
589 Another possible use scenario is in the following trivial example:
590
591 Example 2:
592 if (n > 0)
593 x = 1;
594 ...
595 if (n > 0)
596 {
597 if (m < 2)
598 ... = x;
599 }
600
601 Predicate analysis needs to compute the composite predicate:
602
603 1) 'x' use predicate: (n > 0) .AND. (m < 2)
604 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
605 (the predicate chain for phi operand defs can be computed
606 starting from a bb that is control equivalent to the phi's
607 bb and is dominating the operand def.)
608
609 and check overlapping:
610 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
611 <==> false
612
613 This implementation provides a framework that can handle different
614 scenarios. (Note that many simple cases are handled properly without
615 the predicate analysis if jump threading eliminates the merge point
616 thus makes path-sensitive analysis unnecessary.)
617
618 PHI is the phi node whose incoming (undefined) paths need to be
619 pruned, and OPNDS is the bitmap holding interesting operand
620 positions. VISITED is the pointer set of phi stmts being
621 checked. */
622
623 bool
624 uninit_analysis::overlap (gphi *phi, unsigned opnds, hash_set<gphi *> *visited,
625 const predicate &use_preds)
626 {
627 gimple *flag_def = NULL;
628 tree boundary_cst = NULL_TREE;
629
630 /* Find within the common prefix of multiple predicate chains
631 a predicate that is a comparison of a flag variable against
632 a constant. */
633 unsigned i = 0;
634 tree_code cmp_code;
635 while ((cmp_code = find_var_cmp_const (use_preds.chain (), phi, &flag_def,
636 &boundary_cst, i)) != ERROR_MARK)
637 {
638 /* Now check all the uninit incoming edges have a constant flag
639 value that is in conflict with the use guard/predicate. */
640 bitmap visited_flag_phis = NULL;
641 gphi *phi_def = as_a<gphi *> (flag_def);
642 unsigned max_attempts = param_uninit_max_prune_work;
643 bool all_pruned = prune_phi_opnds (phi, opnds, phi_def, boundary_cst,
644 cmp_code, visited,
645 &visited_flag_phis, max_attempts);
646 if (visited_flag_phis)
647 BITMAP_FREE (visited_flag_phis);
648 if (all_pruned)
649 return false;
650 }
651
652 return true;
653 }
654
655 /* Return true if two predicates PRED1 and X2 are equivalent. Assume
656 the expressions have already properly re-associated. */
657
658 static inline bool
659 pred_equal_p (const pred_info &pred1, const pred_info &pred2)
660 {
661 if (!operand_equal_p (pred1.pred_lhs, pred2.pred_lhs, 0)
662 || !operand_equal_p (pred1.pred_rhs, pred2.pred_rhs, 0))
663 return false;
664
665 tree_code c1 = pred1.cond_code, c2;
666 if (pred1.invert != pred2.invert
667 && TREE_CODE_CLASS (pred2.cond_code) == tcc_comparison)
668 c2 = invert_tree_comparison (pred2.cond_code, false);
669 else
670 c2 = pred2.cond_code;
671
672 return c1 == c2;
673 }
674
675 /* Return true if PRED tests inequality (i.e., X != Y). */
676
677 static inline bool
678 is_neq_relop_p (const pred_info &pred)
679 {
680
681 return ((pred.cond_code == NE_EXPR && !pred.invert)
682 || (pred.cond_code == EQ_EXPR && pred.invert));
683 }
684
685 /* Returns true if PRED is of the form X != 0. */
686
687 static inline bool
688 is_neq_zero_form_p (const pred_info &pred)
689 {
690 if (!is_neq_relop_p (pred) || !integer_zerop (pred.pred_rhs)
691 || TREE_CODE (pred.pred_lhs) != SSA_NAME)
692 return false;
693 return true;
694 }
695
696 /* Return true if PRED is equivalent to X != 0. */
697
698 static inline bool
699 pred_expr_equal_p (const pred_info &pred, tree expr)
700 {
701 if (!is_neq_zero_form_p (pred))
702 return false;
703
704 return operand_equal_p (pred.pred_lhs, expr, 0);
705 }
706
707 /* Return true if VAL satisfies (x CMPC BOUNDARY) predicate. CMPC can
708 be either one of the range comparison codes ({GE,LT,EQ,NE}_EXPR and
709 the like), or BIT_AND_EXPR. EXACT_P is only meaningful for the latter.
710 Modify the question from VAL & BOUNDARY != 0 to VAL & BOUNDARY == VAL.
711 For other values of CMPC, EXACT_P is ignored. */
712
713 static bool
714 value_sat_pred_p (tree val, tree boundary, tree_code cmpc,
715 bool exact_p = false)
716 {
717 if (cmpc != BIT_AND_EXPR)
718 return is_value_included_in (val, boundary, cmpc);
719
720 widest_int andw = wi::to_widest (val) & wi::to_widest (boundary);
721 if (exact_p)
722 return andw == wi::to_widest (val);
723
724 return wi::ne_p (andw, 0);
725 }
726
727 /* Return true if the domain of single predicate expression PRED1
728 is a subset of that of PRED2, and false if it cannot be proved. */
729
730 static bool
731 subset_of (const pred_info &pred1, const pred_info &pred2)
732 {
733 if (pred_equal_p (pred1, pred2))
734 return true;
735
736 if ((TREE_CODE (pred1.pred_rhs) != INTEGER_CST)
737 || (TREE_CODE (pred2.pred_rhs) != INTEGER_CST))
738 return false;
739
740 if (!operand_equal_p (pred1.pred_lhs, pred2.pred_lhs, 0))
741 return false;
742
743 tree_code code1 = pred1.cond_code;
744 if (pred1.invert)
745 code1 = invert_tree_comparison (code1, false);
746 tree_code code2 = pred2.cond_code;
747 if (pred2.invert)
748 code2 = invert_tree_comparison (code2, false);
749
750 if (code2 == NE_EXPR && code1 == NE_EXPR)
751 return false;
752
753 if (code2 == NE_EXPR)
754 return !value_sat_pred_p (pred2.pred_rhs, pred1.pred_rhs, code1);
755
756 if (code1 == EQ_EXPR)
757 return value_sat_pred_p (pred1.pred_rhs, pred2.pred_rhs, code2);
758
759 if (code1 == code2)
760 return value_sat_pred_p (pred1.pred_rhs, pred2.pred_rhs, code2,
761 code1 == BIT_AND_EXPR);
762
763 return false;
764 }
765
766 /* Return true if the domain of CHAIN1 is a subset of that of CHAIN2.
767 Return false if it cannot be proven so. */
768
769 static bool
770 subset_of (const pred_chain &chain1, const pred_chain &chain2)
771 {
772 unsigned np1 = chain1.length ();
773 unsigned np2 = chain2.length ();
774 for (unsigned i2 = 0; i2 < np2; i2++)
775 {
776 bool found = false;
777 const pred_info &info2 = chain2[i2];
778 for (unsigned i1 = 0; i1 < np1; i1++)
779 {
780 const pred_info &info1 = chain1[i1];
781 if (subset_of (info1, info2))
782 {
783 found = true;
784 break;
785 }
786 }
787 if (!found)
788 return false;
789 }
790 return true;
791 }
792
793 /* Return true if the domain defined by the predicate chain PREDS is
794 a subset of the domain of *THIS. Return false if PREDS's domain
795 is not a subset of any of the sub-domains of *THIS (corresponding
796 to each individual chains in it), even though it may be still be
797 a subset of whole domain of *THIS which is the union (ORed) of all
798 its subdomains. In other words, the result is conservative. */
799
800 bool
801 predicate::includes (const pred_chain &chain) const
802 {
803 for (unsigned i = 0; i < m_preds.length (); i++)
804 if (subset_of (chain, m_preds[i]))
805 return true;
806
807 return false;
808 }
809
810 /* Return true if the domain defined by *THIS is a superset of PREDS's
811 domain.
812 Avoid building generic trees (and rely on the folding capability
813 of the compiler), and instead perform brute force comparison of
814 individual predicate chains (this won't be a computationally costly
815 since the chains are pretty short). Returning false does not
816 necessarily mean *THIS is not a superset of *PREDS, only that
817 it need not be since the analysis cannot prove it. */
818
819 bool
820 predicate::superset_of (const predicate &preds) const
821 {
822 for (unsigned i = 0; i < preds.m_preds.length (); i++)
823 if (!includes (preds.m_preds[i]))
824 return false;
825
826 return true;
827 }
828
829 /* Create a predicate of the form OP != 0 and push it the work list CHAIN. */
830
831 static void
832 push_to_worklist (tree op, pred_chain *chain, hash_set<tree> *mark_set)
833 {
834 if (mark_set->contains (op))
835 return;
836 mark_set->add (op);
837
838 pred_info arg_pred;
839 arg_pred.pred_lhs = op;
840 arg_pred.pred_rhs = integer_zero_node;
841 arg_pred.cond_code = NE_EXPR;
842 arg_pred.invert = false;
843 chain->safe_push (arg_pred);
844 }
845
846 /* Return a pred_info for a gimple assignment CMP_ASSIGN with comparison
847 rhs. */
848
849 static pred_info
850 get_pred_info_from_cmp (const gimple *cmp_assign)
851 {
852 pred_info pred;
853 pred.pred_lhs = gimple_assign_rhs1 (cmp_assign);
854 pred.pred_rhs = gimple_assign_rhs2 (cmp_assign);
855 pred.cond_code = gimple_assign_rhs_code (cmp_assign);
856 pred.invert = false;
857 return pred;
858 }
859
860 /* If PHI is a degenerate phi with all operands having the same value (relop)
861 update *PRED to that value and return true. Otherwise return false. */
862
863 static bool
864 is_degenerate_phi (gimple *phi, pred_info *pred)
865 {
866 tree op0 = gimple_phi_arg_def (phi, 0);
867
868 if (TREE_CODE (op0) != SSA_NAME)
869 return false;
870
871 gimple *def0 = SSA_NAME_DEF_STMT (op0);
872 if (gimple_code (def0) != GIMPLE_ASSIGN)
873 return false;
874
875 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def0)) != tcc_comparison)
876 return false;
877
878 pred_info pred0 = get_pred_info_from_cmp (def0);
879
880 unsigned n = gimple_phi_num_args (phi);
881 for (unsigned i = 1; i < n; ++i)
882 {
883 tree op = gimple_phi_arg_def (phi, i);
884 if (TREE_CODE (op) != SSA_NAME)
885 return false;
886
887 gimple *def = SSA_NAME_DEF_STMT (op);
888 if (gimple_code (def) != GIMPLE_ASSIGN)
889 return false;
890
891 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def)) != tcc_comparison)
892 return false;
893
894 pred_info pred = get_pred_info_from_cmp (def);
895 if (!pred_equal_p (pred, pred0))
896 return false;
897 }
898
899 *pred = pred0;
900 return true;
901 }
902
903 /* If compute_control_dep_chain bailed out due to limits this routine
904 tries to build a partial sparse path using dominators. Returns
905 path edges whose predicates are always true when reaching E. */
906
907 static void
908 simple_control_dep_chain (vec<edge>& chain, basic_block from, basic_block to)
909 {
910 if (!dominated_by_p (CDI_DOMINATORS, to, from))
911 return;
912
913 basic_block src = to;
914 while (src != from
915 && chain.length () <= MAX_CHAIN_LEN)
916 {
917 basic_block dest = src;
918 src = get_immediate_dominator (CDI_DOMINATORS, src);
919 if (single_pred_p (dest))
920 {
921 edge pred_e = single_pred_edge (dest);
922 gcc_assert (pred_e->src == src);
923 if (!(pred_e->flags & ((EDGE_FAKE | EDGE_ABNORMAL | EDGE_DFS_BACK)))
924 && !single_succ_p (src))
925 chain.safe_push (pred_e);
926 }
927 }
928 }
929
930 /* Perform a DFS walk on predecessor edges to mark the region denoted
931 by the EXIT_SRC block and DOM which dominates EXIT_SRC, including DOM.
932 Blocks in the region are marked with FLAG and added to BBS. BBS is
933 filled up to its capacity only after which the walk is terminated
934 and false is returned. If the whole region was marked, true is returned. */
935
936 static bool
937 dfs_mark_dominating_region (basic_block exit_src, basic_block dom, int flag,
938 vec<basic_block> &bbs)
939 {
940 if (exit_src == dom || exit_src->flags & flag)
941 return true;
942 if (!bbs.space (1))
943 return false;
944 bbs.quick_push (exit_src);
945 exit_src->flags |= flag;
946 auto_vec<edge_iterator, 20> stack (bbs.allocated () - bbs.length () + 1);
947 stack.quick_push (ei_start (exit_src->preds));
948 while (!stack.is_empty ())
949 {
950 /* Look at the edge on the top of the stack. */
951 edge_iterator ei = stack.last ();
952 basic_block src = ei_edge (ei)->src;
953
954 /* Check if the edge source has been visited yet. */
955 if (!(src->flags & flag))
956 {
957 /* Mark the source if there's still space. If not, return early. */
958 if (!bbs.space (1))
959 return false;
960 src->flags |= flag;
961 bbs.quick_push (src);
962
963 /* Queue its predecessors if we didn't reach DOM. */
964 if (src != dom && EDGE_COUNT (src->preds) > 0)
965 stack.quick_push (ei_start (src->preds));
966 }
967 else
968 {
969 if (!ei_one_before_end_p (ei))
970 ei_next (&stack.last ());
971 else
972 stack.pop ();
973 }
974 }
975 return true;
976 }
977
978 static bool
979 compute_control_dep_chain (basic_block dom_bb, const_basic_block dep_bb,
980 vec<edge> cd_chains[], unsigned *num_chains,
981 vec<edge> &cur_cd_chain, unsigned *num_calls,
982 unsigned in_region, unsigned depth,
983 bool *complete_p);
984
985 /* Helper for compute_control_dep_chain that walks the post-dominator
986 chain from CD_BB up unto TARGET_BB looking for paths to DEP_BB. */
987
988 static bool
989 compute_control_dep_chain_pdom (basic_block cd_bb, const_basic_block dep_bb,
990 basic_block target_bb,
991 vec<edge> cd_chains[], unsigned *num_chains,
992 vec<edge> &cur_cd_chain, unsigned *num_calls,
993 unsigned in_region, unsigned depth,
994 bool *complete_p)
995 {
996 bool found_cd_chain = false;
997 while (cd_bb != target_bb)
998 {
999 if (cd_bb == dep_bb)
1000 {
1001 /* Found a direct control dependence. */
1002 if (*num_chains < MAX_NUM_CHAINS)
1003 {
1004 if (DEBUG_PREDICATE_ANALYZER && dump_file)
1005 fprintf (dump_file, "%*s pushing { %s }\n",
1006 depth, "", format_edge_vec (cur_cd_chain).c_str ());
1007 cd_chains[*num_chains] = cur_cd_chain.copy ();
1008 (*num_chains)++;
1009 }
1010 found_cd_chain = true;
1011 /* Check path from next edge. */
1012 break;
1013 }
1014
1015 /* If the dominating region has been marked avoid walking outside. */
1016 if (in_region != 0 && !(cd_bb->flags & in_region))
1017 break;
1018
1019 /* Count the number of steps we perform to limit compile-time.
1020 This should cover both recursion and the post-dominator walk. */
1021 if (*num_calls > (unsigned)param_uninit_control_dep_attempts)
1022 {
1023 if (dump_file)
1024 fprintf (dump_file, "param_uninit_control_dep_attempts "
1025 "exceeded: %u\n", *num_calls);
1026 *complete_p = false;
1027 break;
1028 }
1029 ++*num_calls;
1030
1031 /* Check if DEP_BB is indirectly control-dependent on DOM_BB. */
1032 if (!single_succ_p (cd_bb)
1033 && compute_control_dep_chain (cd_bb, dep_bb, cd_chains,
1034 num_chains, cur_cd_chain,
1035 num_calls, in_region, depth + 1,
1036 complete_p))
1037 {
1038 found_cd_chain = true;
1039 break;
1040 }
1041
1042 /* The post-dominator walk will reach a backedge only
1043 from a forwarder, otherwise it should choose to exit
1044 the SCC. */
1045 if (single_succ_p (cd_bb)
1046 && single_succ_edge (cd_bb)->flags & EDGE_DFS_BACK)
1047 break;
1048 basic_block prev_cd_bb = cd_bb;
1049 cd_bb = get_immediate_dominator (CDI_POST_DOMINATORS, cd_bb);
1050 if (cd_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1051 break;
1052 /* Pick up conditions toward the post dominator such like
1053 loop exit conditions. See gcc.dg/uninit-pred-11.c and
1054 gcc.dg/unninit-pred-12.c and PR106754. */
1055 if (single_pred_p (cd_bb))
1056 {
1057 edge e2 = single_pred_edge (cd_bb);
1058 gcc_assert (e2->src == prev_cd_bb);
1059 /* But avoid adding fallthru or abnormal edges. */
1060 if (!(e2->flags & (EDGE_FAKE | EDGE_ABNORMAL | EDGE_DFS_BACK))
1061 && !single_succ_p (prev_cd_bb))
1062 cur_cd_chain.safe_push (e2);
1063 }
1064 }
1065 return found_cd_chain;
1066 }
1067
1068
1069 /* Recursively compute the control dependence chains (paths of edges)
1070 from the dependent basic block, DEP_BB, up to the dominating basic
1071 block, DOM_BB (the head node of a chain should be dominated by it),
1072 storing them in the CD_CHAINS array.
1073 CUR_CD_CHAIN is the current chain being computed.
1074 *NUM_CHAINS is total number of chains in the CD_CHAINS array.
1075 *NUM_CALLS is the number of recursive calls to control unbounded
1076 recursion.
1077 Return true if the information is successfully computed, false if
1078 there is no control dependence or not computed.
1079 *COMPLETE_P is set to false if we stopped walking due to limits.
1080 In this case there might be missing chains. */
1081
1082 static bool
1083 compute_control_dep_chain (basic_block dom_bb, const_basic_block dep_bb,
1084 vec<edge> cd_chains[], unsigned *num_chains,
1085 vec<edge> &cur_cd_chain, unsigned *num_calls,
1086 unsigned in_region, unsigned depth,
1087 bool *complete_p)
1088 {
1089 /* In our recursive calls this doesn't happen. */
1090 if (single_succ_p (dom_bb))
1091 return false;
1092
1093 /* FIXME: Use a set instead. */
1094 unsigned cur_chain_len = cur_cd_chain.length ();
1095 if (cur_chain_len > MAX_CHAIN_LEN)
1096 {
1097 if (dump_file)
1098 fprintf (dump_file, "MAX_CHAIN_LEN exceeded: %u\n", cur_chain_len);
1099
1100 *complete_p = false;
1101 return false;
1102 }
1103
1104 if (cur_chain_len > 5)
1105 {
1106 if (dump_file)
1107 fprintf (dump_file, "chain length exceeds 5: %u\n", cur_chain_len);
1108 }
1109
1110 if (DEBUG_PREDICATE_ANALYZER && dump_file)
1111 fprintf (dump_file,
1112 "%*s%s (dom_bb = %u, dep_bb = %u, ..., "
1113 "cur_cd_chain = { %s }, ...)\n",
1114 depth, "", __func__, dom_bb->index, dep_bb->index,
1115 format_edge_vec (cur_cd_chain).c_str ());
1116
1117 bool found_cd_chain = false;
1118
1119 /* Iterate over DOM_BB's successors. */
1120 edge e;
1121 edge_iterator ei;
1122 FOR_EACH_EDGE (e, ei, dom_bb->succs)
1123 {
1124 if (e->flags & (EDGE_FAKE | EDGE_ABNORMAL | EDGE_DFS_BACK))
1125 continue;
1126
1127 basic_block cd_bb = e->dest;
1128 unsigned pop_mark = cur_cd_chain.length ();
1129 cur_cd_chain.safe_push (e);
1130 basic_block target_bb
1131 = get_immediate_dominator (CDI_POST_DOMINATORS, dom_bb);
1132 /* Walk the post-dominator chain up to the CFG merge. */
1133 found_cd_chain
1134 |= compute_control_dep_chain_pdom (cd_bb, dep_bb, target_bb,
1135 cd_chains, num_chains,
1136 cur_cd_chain, num_calls,
1137 in_region, depth, complete_p);
1138 cur_cd_chain.truncate (pop_mark);
1139 gcc_assert (cur_cd_chain.length () == cur_chain_len);
1140 }
1141
1142 gcc_assert (cur_cd_chain.length () == cur_chain_len);
1143 return found_cd_chain;
1144 }
1145
1146 /* Wrapper around the compute_control_dep_chain worker above. Returns
1147 true when the collected set of chains in CD_CHAINS is complete. */
1148
1149 static bool
1150 compute_control_dep_chain (basic_block dom_bb, const_basic_block dep_bb,
1151 vec<edge> cd_chains[], unsigned *num_chains,
1152 unsigned in_region = 0)
1153 {
1154 auto_vec<edge, 10> cur_cd_chain;
1155 unsigned num_calls = 0;
1156 unsigned depth = 0;
1157 bool complete_p = true;
1158 /* Walk the post-dominator chain. */
1159 cur_cd_chain.reserve (MAX_CHAIN_LEN + 1);
1160 compute_control_dep_chain_pdom (dom_bb, dep_bb, NULL, cd_chains,
1161 num_chains, cur_cd_chain, &num_calls,
1162 in_region, depth, &complete_p);
1163 return complete_p;
1164 }
1165
1166 /* Implemented simplifications:
1167
1168 1a) ((x IOR y) != 0) AND (x != 0) is equivalent to (x != 0);
1169 1b) [!](X rel y) AND [!](X rel y') where y == y' or both constant
1170 can possibly be simplified
1171 2) (X AND Y) OR (!X AND Y) is equivalent to Y;
1172 3) X OR (!X AND Y) is equivalent to (X OR Y);
1173 4) ((x IAND y) != 0) || (x != 0 AND y != 0)) is equivalent to
1174 (x != 0 AND y != 0)
1175 5) (X AND Y) OR (!X AND Z) OR (!Y AND Z) is equivalent to
1176 (X AND Y) OR Z
1177
1178 PREDS is the predicate chains, and N is the number of chains. */
1179
1180 /* Implement rule 1a above. PREDS is the AND predicate to simplify
1181 in place. */
1182
1183 static void
1184 simplify_1a (pred_chain &chain)
1185 {
1186 bool simplified = false;
1187 pred_chain s_chain = vNULL;
1188
1189 unsigned n = chain.length ();
1190 for (unsigned i = 0; i < n; i++)
1191 {
1192 pred_info &a_pred = chain[i];
1193
1194 if (!a_pred.pred_lhs
1195 || !is_neq_zero_form_p (a_pred))
1196 continue;
1197
1198 gimple *def_stmt = SSA_NAME_DEF_STMT (a_pred.pred_lhs);
1199 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1200 continue;
1201
1202 if (gimple_assign_rhs_code (def_stmt) != BIT_IOR_EXPR)
1203 continue;
1204
1205 for (unsigned j = 0; j < n; j++)
1206 {
1207 const pred_info &b_pred = chain[j];
1208
1209 if (!b_pred.pred_lhs
1210 || !is_neq_zero_form_p (b_pred))
1211 continue;
1212
1213 if (pred_expr_equal_p (b_pred, gimple_assign_rhs1 (def_stmt))
1214 || pred_expr_equal_p (b_pred, gimple_assign_rhs2 (def_stmt)))
1215 {
1216 /* Mark A_PRED for removal from PREDS. */
1217 a_pred.pred_lhs = NULL;
1218 a_pred.pred_rhs = NULL;
1219 simplified = true;
1220 break;
1221 }
1222 }
1223 }
1224
1225 if (!simplified)
1226 return;
1227
1228 /* Remove predicates marked above. */
1229 for (unsigned i = 0; i < n; i++)
1230 {
1231 pred_info &a_pred = chain[i];
1232 if (!a_pred.pred_lhs)
1233 continue;
1234 s_chain.safe_push (a_pred);
1235 }
1236
1237 chain.release ();
1238 chain = s_chain;
1239 }
1240
1241 /* Implement rule 1b above. PREDS is the AND predicate to simplify
1242 in place. Returns true if CHAIN simplifies to true or false. */
1243
1244 static bool
1245 simplify_1b (pred_chain &chain)
1246 {
1247 for (unsigned i = 0; i < chain.length (); i++)
1248 {
1249 pred_info &a_pred = chain[i];
1250
1251 for (unsigned j = i + 1; j < chain.length (); ++j)
1252 {
1253 pred_info &b_pred = chain[j];
1254
1255 if (!operand_equal_p (a_pred.pred_lhs, b_pred.pred_lhs)
1256 || (!operand_equal_p (a_pred.pred_rhs, b_pred.pred_rhs)
1257 && !(CONSTANT_CLASS_P (a_pred.pred_rhs)
1258 && CONSTANT_CLASS_P (b_pred.pred_rhs))))
1259 continue;
1260
1261 tree_code a_code = a_pred.cond_code;
1262 if (a_pred.invert)
1263 a_code = invert_tree_comparison (a_code, false);
1264 tree_code b_code = b_pred.cond_code;
1265 if (b_pred.invert)
1266 b_code = invert_tree_comparison (b_code, false);
1267 /* Try to combine X a_code Y && X b_code Y'. */
1268 tree comb = maybe_fold_and_comparisons (boolean_type_node,
1269 a_code,
1270 a_pred.pred_lhs,
1271 a_pred.pred_rhs,
1272 b_code,
1273 b_pred.pred_lhs,
1274 b_pred.pred_rhs, NULL);
1275 if (!comb)
1276 ;
1277 else if (integer_zerop (comb))
1278 return true;
1279 else if (integer_truep (comb))
1280 {
1281 chain.ordered_remove (j);
1282 chain.ordered_remove (i);
1283 if (chain.is_empty ())
1284 return true;
1285 i--;
1286 break;
1287 }
1288 else if (COMPARISON_CLASS_P (comb)
1289 && operand_equal_p (a_pred.pred_lhs, TREE_OPERAND (comb, 0)))
1290 {
1291 chain.ordered_remove (j);
1292 a_pred.cond_code = TREE_CODE (comb);
1293 a_pred.pred_rhs = TREE_OPERAND (comb, 1);
1294 a_pred.invert = false;
1295 j--;
1296 }
1297 }
1298 }
1299
1300 return false;
1301 }
1302
1303 /* Implements rule 2 for the OR predicate PREDS:
1304
1305 2) (X AND Y) OR (!X AND Y) is equivalent to Y. */
1306
1307 bool
1308 predicate::simplify_2 ()
1309 {
1310 bool simplified = false;
1311
1312 /* (X AND Y) OR (!X AND Y) is equivalent to Y.
1313 (X AND Y) OR (X AND !Y) is equivalent to X. */
1314
1315 for (unsigned i = 0; i < m_preds.length (); i++)
1316 {
1317 pred_chain &a_chain = m_preds[i];
1318
1319 for (unsigned j = i + 1; j < m_preds.length (); j++)
1320 {
1321 pred_chain &b_chain = m_preds[j];
1322 if (b_chain.length () != a_chain.length ())
1323 continue;
1324
1325 unsigned neg_idx = -1U;
1326 for (unsigned k = 0; k < a_chain.length (); ++k)
1327 {
1328 if (pred_equal_p (a_chain[k], b_chain[k]))
1329 continue;
1330 if (neg_idx != -1U)
1331 {
1332 neg_idx = -1U;
1333 break;
1334 }
1335 if (pred_neg_p (a_chain[k], b_chain[k]))
1336 neg_idx = k;
1337 else
1338 break;
1339 }
1340 /* If we found equal chains with one negated predicate
1341 simplify. */
1342 if (neg_idx != -1U)
1343 {
1344 a_chain.ordered_remove (neg_idx);
1345 m_preds.ordered_remove (j);
1346 simplified = true;
1347 if (a_chain.is_empty ())
1348 {
1349 /* A && !A simplifies to true, wipe the whole predicate. */
1350 for (unsigned k = 0; k < m_preds.length (); ++k)
1351 m_preds[k].release ();
1352 m_preds.truncate (0);
1353 }
1354 break;
1355 }
1356 }
1357 }
1358
1359 return simplified;
1360 }
1361
1362 /* Implement rule 3 for the OR predicate PREDS:
1363
1364 3) x OR (!x AND y) is equivalent to x OR y. */
1365
1366 bool
1367 predicate::simplify_3 ()
1368 {
1369 /* Now iteratively simplify X OR (!X AND Z ..)
1370 into X OR (Z ...). */
1371
1372 unsigned n = m_preds.length ();
1373 if (n < 2)
1374 return false;
1375
1376 bool simplified = false;
1377 for (unsigned i = 0; i < n; i++)
1378 {
1379 const pred_chain &a_chain = m_preds[i];
1380
1381 if (a_chain.length () != 1)
1382 continue;
1383
1384 const pred_info &x = a_chain[0];
1385 for (unsigned j = 0; j < n; j++)
1386 {
1387 if (j == i)
1388 continue;
1389
1390 pred_chain b_chain = m_preds[j];
1391 if (b_chain.length () < 2)
1392 continue;
1393
1394 for (unsigned k = 0; k < b_chain.length (); k++)
1395 {
1396 const pred_info &x2 = b_chain[k];
1397 if (pred_neg_p (x, x2))
1398 {
1399 b_chain.unordered_remove (k);
1400 simplified = true;
1401 break;
1402 }
1403 }
1404 }
1405 }
1406 return simplified;
1407 }
1408
1409 /* Implement rule 4 for the OR predicate PREDS:
1410
1411 2) ((x AND y) != 0) OR (x != 0 AND y != 0) is equivalent to
1412 (x != 0 AND y != 0). */
1413
1414 bool
1415 predicate::simplify_4 ()
1416 {
1417 bool simplified = false;
1418 pred_chain_union s_preds = vNULL;
1419
1420 unsigned n = m_preds.length ();
1421 for (unsigned i = 0; i < n; i++)
1422 {
1423 pred_chain a_chain = m_preds[i];
1424 if (a_chain.length () != 1)
1425 continue;
1426
1427 const pred_info &z = a_chain[0];
1428 if (!is_neq_zero_form_p (z))
1429 continue;
1430
1431 gimple *def_stmt = SSA_NAME_DEF_STMT (z.pred_lhs);
1432 if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1433 continue;
1434
1435 if (gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
1436 continue;
1437
1438 for (unsigned j = 0; j < n; j++)
1439 {
1440 if (j == i)
1441 continue;
1442
1443 pred_chain b_chain = m_preds[j];
1444 if (b_chain.length () != 2)
1445 continue;
1446
1447 const pred_info &x2 = b_chain[0];
1448 const pred_info &y2 = b_chain[1];
1449 if (!is_neq_zero_form_p (x2) || !is_neq_zero_form_p (y2))
1450 continue;
1451
1452 if ((pred_expr_equal_p (x2, gimple_assign_rhs1 (def_stmt))
1453 && pred_expr_equal_p (y2, gimple_assign_rhs2 (def_stmt)))
1454 || (pred_expr_equal_p (x2, gimple_assign_rhs2 (def_stmt))
1455 && pred_expr_equal_p (y2, gimple_assign_rhs1 (def_stmt))))
1456 {
1457 /* Kill a_chain. */
1458 a_chain.release ();
1459 simplified = true;
1460 break;
1461 }
1462 }
1463 }
1464 /* Now clean up the chain. */
1465 if (simplified)
1466 {
1467 for (unsigned i = 0; i < n; i++)
1468 {
1469 if (m_preds[i].is_empty ())
1470 continue;
1471 s_preds.safe_push (m_preds[i]);
1472 }
1473
1474 m_preds.release ();
1475 m_preds = s_preds;
1476 s_preds = vNULL;
1477 }
1478
1479 return simplified;
1480 }
1481
1482 /* Simplify predicates in *THIS. */
1483
1484 void
1485 predicate::simplify (gimple *use_or_def, bool is_use)
1486 {
1487 if (dump_file && dump_flags & TDF_DETAILS)
1488 {
1489 fprintf (dump_file, "Before simplication ");
1490 dump (dump_file, use_or_def, is_use ? "[USE]:\n" : "[DEF]:\n");
1491 }
1492
1493 for (unsigned i = 0; i < m_preds.length (); i++)
1494 {
1495 ::simplify_1a (m_preds[i]);
1496 if (::simplify_1b (m_preds[i]))
1497 {
1498 m_preds[i].release ();
1499 m_preds.ordered_remove (i);
1500 i--;
1501 }
1502 }
1503
1504 if (m_preds.length () < 2)
1505 return;
1506
1507 bool changed;
1508 do
1509 {
1510 changed = false;
1511 if (simplify_2 ())
1512 changed = true;
1513
1514 if (simplify_3 ())
1515 changed = true;
1516
1517 if (simplify_4 ())
1518 changed = true;
1519 }
1520 while (changed);
1521 }
1522
1523 /* Attempt to normalize predicate chains by following UD chains by
1524 building up a big tree of either IOR operations or AND operations,
1525 and converting the IOR tree into a pred_chain_union or the BIT_AND
1526 tree into a pred_chain.
1527 Example:
1528
1529 _3 = _2 RELOP1 _1;
1530 _6 = _5 RELOP2 _4;
1531 _9 = _8 RELOP3 _7;
1532 _10 = _3 | _6;
1533 _12 = _9 | _0;
1534 _t = _10 | _12;
1535
1536 then _t != 0 will be normalized into a pred_chain_union
1537
1538 (_2 RELOP1 _1) OR (_5 RELOP2 _4) OR (_8 RELOP3 _7) OR (_0 != 0)
1539
1540 Similarly given:
1541
1542 _3 = _2 RELOP1 _1;
1543 _6 = _5 RELOP2 _4;
1544 _9 = _8 RELOP3 _7;
1545 _10 = _3 & _6;
1546 _12 = _9 & _0;
1547
1548 then _t != 0 will be normalized into a pred_chain:
1549 (_2 RELOP1 _1) AND (_5 RELOP2 _4) AND (_8 RELOP3 _7) AND (_0 != 0)
1550 */
1551
1552 /* Normalize predicate PRED:
1553 1) if PRED can no longer be normalized, append it to *THIS.
1554 2) otherwise if PRED is of the form x != 0, follow x's definition
1555 and put normalized predicates into WORK_LIST. */
1556
1557 void
1558 predicate::normalize (pred_chain *norm_chain,
1559 pred_info pred,
1560 tree_code and_or_code,
1561 pred_chain *work_list,
1562 hash_set<tree> *mark_set)
1563 {
1564 if (!is_neq_zero_form_p (pred))
1565 {
1566 if (and_or_code == BIT_IOR_EXPR)
1567 push_pred (pred);
1568 else
1569 norm_chain->safe_push (pred);
1570 return;
1571 }
1572
1573 gimple *def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
1574
1575 if (gimple_code (def_stmt) == GIMPLE_PHI
1576 && is_degenerate_phi (def_stmt, &pred))
1577 /* PRED has been modified above. */
1578 work_list->safe_push (pred);
1579 else if (gimple_code (def_stmt) == GIMPLE_PHI && and_or_code == BIT_IOR_EXPR)
1580 {
1581 unsigned n = gimple_phi_num_args (def_stmt);
1582
1583 /* Punt for a nonzero constant. The predicate should be one guarding
1584 the phi edge. */
1585 for (unsigned i = 0; i < n; ++i)
1586 {
1587 tree op = gimple_phi_arg_def (def_stmt, i);
1588 if (TREE_CODE (op) == INTEGER_CST && !integer_zerop (op))
1589 {
1590 push_pred (pred);
1591 return;
1592 }
1593 }
1594
1595 for (unsigned i = 0; i < n; ++i)
1596 {
1597 tree op = gimple_phi_arg_def (def_stmt, i);
1598 if (integer_zerop (op))
1599 continue;
1600
1601 push_to_worklist (op, work_list, mark_set);
1602 }
1603 }
1604 else if (gimple_code (def_stmt) != GIMPLE_ASSIGN)
1605 {
1606 if (and_or_code == BIT_IOR_EXPR)
1607 push_pred (pred);
1608 else
1609 norm_chain->safe_push (pred);
1610 }
1611 else if (gimple_assign_rhs_code (def_stmt) == and_or_code)
1612 {
1613 /* Avoid splitting up bit manipulations like x & 3 or y | 1. */
1614 if (is_gimple_min_invariant (gimple_assign_rhs2 (def_stmt)))
1615 {
1616 /* But treat x & 3 as a condition. */
1617 if (and_or_code == BIT_AND_EXPR)
1618 {
1619 pred_info n_pred;
1620 n_pred.pred_lhs = gimple_assign_rhs1 (def_stmt);
1621 n_pred.pred_rhs = gimple_assign_rhs2 (def_stmt);
1622 n_pred.cond_code = and_or_code;
1623 n_pred.invert = false;
1624 norm_chain->safe_push (n_pred);
1625 }
1626 }
1627 else
1628 {
1629 push_to_worklist (gimple_assign_rhs1 (def_stmt), work_list, mark_set);
1630 push_to_worklist (gimple_assign_rhs2 (def_stmt), work_list, mark_set);
1631 }
1632 }
1633 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
1634 == tcc_comparison)
1635 {
1636 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
1637 if (and_or_code == BIT_IOR_EXPR)
1638 push_pred (n_pred);
1639 else
1640 norm_chain->safe_push (n_pred);
1641 }
1642 else
1643 {
1644 if (and_or_code == BIT_IOR_EXPR)
1645 push_pred (pred);
1646 else
1647 norm_chain->safe_push (pred);
1648 }
1649 }
1650
1651 /* Normalize PRED and store the normalized predicates in THIS->M_PREDS. */
1652
1653 void
1654 predicate::normalize (const pred_info &pred)
1655 {
1656 if (!is_neq_zero_form_p (pred))
1657 {
1658 push_pred (pred);
1659 return;
1660 }
1661
1662 tree_code and_or_code = ERROR_MARK;
1663
1664 gimple *def_stmt = SSA_NAME_DEF_STMT (pred.pred_lhs);
1665 if (gimple_code (def_stmt) == GIMPLE_ASSIGN)
1666 and_or_code = gimple_assign_rhs_code (def_stmt);
1667 if (and_or_code != BIT_IOR_EXPR && and_or_code != BIT_AND_EXPR)
1668 {
1669 if (TREE_CODE_CLASS (and_or_code) == tcc_comparison)
1670 {
1671 pred_info n_pred = get_pred_info_from_cmp (def_stmt);
1672 push_pred (n_pred);
1673 }
1674 else
1675 push_pred (pred);
1676 return;
1677 }
1678
1679
1680 pred_chain norm_chain = vNULL;
1681 pred_chain work_list = vNULL;
1682 work_list.safe_push (pred);
1683 hash_set<tree> mark_set;
1684
1685 while (!work_list.is_empty ())
1686 {
1687 pred_info a_pred = work_list.pop ();
1688 normalize (&norm_chain, a_pred, and_or_code, &work_list, &mark_set);
1689 }
1690
1691 if (and_or_code == BIT_AND_EXPR)
1692 m_preds.safe_push (norm_chain);
1693
1694 work_list.release ();
1695 }
1696
1697 /* Normalize a single predicate PRED_CHAIN and append it to *THIS. */
1698
1699 void
1700 predicate::normalize (const pred_chain &chain)
1701 {
1702 pred_chain work_list = vNULL;
1703 hash_set<tree> mark_set;
1704 for (unsigned i = 0; i < chain.length (); i++)
1705 {
1706 work_list.safe_push (chain[i]);
1707 mark_set.add (chain[i].pred_lhs);
1708 }
1709
1710 /* Normalized chain of predicates built up below. */
1711 pred_chain norm_chain = vNULL;
1712 while (!work_list.is_empty ())
1713 {
1714 pred_info pi = work_list.pop ();
1715 /* The predicate object is not modified here, only NORM_CHAIN and
1716 WORK_LIST are appended to. */
1717 unsigned oldlen = m_preds.length ();
1718 normalize (&norm_chain, pi, BIT_AND_EXPR, &work_list, &mark_set);
1719 gcc_assert (m_preds.length () == oldlen);
1720 }
1721
1722 m_preds.safe_push (norm_chain);
1723 work_list.release ();
1724 }
1725
1726 /* Normalize predicate chains in THIS. */
1727
1728 void
1729 predicate::normalize (gimple *use_or_def, bool is_use)
1730 {
1731 if (dump_file && dump_flags & TDF_DETAILS)
1732 {
1733 fprintf (dump_file, "Before normalization ");
1734 dump (dump_file, use_or_def, is_use ? "[USE]:\n" : "[DEF]:\n");
1735 }
1736
1737 predicate norm_preds (empty_val ());
1738 for (unsigned i = 0; i < m_preds.length (); i++)
1739 {
1740 if (m_preds[i].length () != 1)
1741 norm_preds.normalize (m_preds[i]);
1742 else
1743 norm_preds.normalize (m_preds[i][0]);
1744 }
1745
1746 *this = norm_preds;
1747
1748 if (dump_file)
1749 {
1750 fprintf (dump_file, "After normalization ");
1751 dump (dump_file, use_or_def, is_use ? "[USE]:\n" : "[DEF]:\n");
1752 }
1753 }
1754
1755 /* Convert the chains of control dependence edges into a set of predicates.
1756 A control dependence chain is represented by a vector edges. DEP_CHAINS
1757 points to an array of NUM_CHAINS dependence chains. One edge in
1758 a dependence chain is mapped to predicate expression represented by
1759 pred_info type. One dependence chain is converted to a composite
1760 predicate that is the result of AND operation of pred_info mapped to
1761 each edge. A composite predicate is represented by a vector of
1762 pred_info. Sets M_PREDS to the resulting composite predicates. */
1763
1764 void
1765 predicate::init_from_control_deps (const vec<edge> *dep_chains,
1766 unsigned num_chains, bool is_use)
1767 {
1768 gcc_assert (is_empty ());
1769
1770 if (num_chains == 0)
1771 return;
1772
1773 if (DEBUG_PREDICATE_ANALYZER && dump_file)
1774 fprintf (dump_file, "init_from_control_deps [%s] {%s}:\n",
1775 is_use ? "USE" : "DEF",
1776 format_edge_vecs (dep_chains, num_chains).c_str ());
1777
1778 /* Convert the control dependency chain into a set of predicates. */
1779 m_preds.reserve (num_chains);
1780
1781 for (unsigned i = 0; i < num_chains; i++)
1782 {
1783 /* One path through the CFG represents a logical conjunction
1784 of the predicates. */
1785 const vec<edge> &path = dep_chains[i];
1786
1787 bool has_valid_pred = false;
1788 /* The chain of predicates guarding the definition along this path. */
1789 pred_chain t_chain{ };
1790 for (unsigned j = 0; j < path.length (); j++)
1791 {
1792 edge e = path[j];
1793 basic_block guard_bb = e->src;
1794
1795 gcc_assert (!empty_block_p (guard_bb) && !single_succ_p (guard_bb));
1796
1797 /* Skip this edge if it is bypassing an abort - when the
1798 condition is not satisfied we are neither reaching the
1799 definition nor the use so it isn't meaningful. Note if
1800 we are processing the use predicate the condition is
1801 meaningful. See PR65244. */
1802 if (!is_use && EDGE_COUNT (e->src->succs) == 2)
1803 {
1804 edge e1;
1805 edge_iterator ei1;
1806 bool skip = false;
1807
1808 FOR_EACH_EDGE (e1, ei1, e->src->succs)
1809 {
1810 if (EDGE_COUNT (e1->dest->succs) == 0)
1811 {
1812 skip = true;
1813 break;
1814 }
1815 }
1816 if (skip)
1817 {
1818 has_valid_pred = true;
1819 continue;
1820 }
1821 }
1822 /* Get the conditional controlling the bb exit edge. */
1823 gimple *cond_stmt = *gsi_last_bb (guard_bb);
1824 if (gimple_code (cond_stmt) == GIMPLE_COND)
1825 {
1826 /* The true edge corresponds to the uninteresting condition.
1827 Add the negated predicate(s) for the edge to record
1828 the interesting condition. */
1829 pred_info one_pred;
1830 one_pred.pred_lhs = gimple_cond_lhs (cond_stmt);
1831 one_pred.pred_rhs = gimple_cond_rhs (cond_stmt);
1832 one_pred.cond_code = gimple_cond_code (cond_stmt);
1833 one_pred.invert = !!(e->flags & EDGE_FALSE_VALUE);
1834
1835 t_chain.safe_push (one_pred);
1836
1837 if (DEBUG_PREDICATE_ANALYZER && dump_file)
1838 {
1839 fprintf (dump_file, "%d -> %d: one_pred = ",
1840 e->src->index, e->dest->index);
1841 dump_pred_info (dump_file, one_pred);
1842 fputc ('\n', dump_file);
1843 }
1844
1845 has_valid_pred = true;
1846 }
1847 else if (gswitch *gs = dyn_cast<gswitch *> (cond_stmt))
1848 {
1849 /* Find the case label, but avoid quadratic behavior. */
1850 tree l = get_cases_for_edge (e, gs);
1851 /* If more than one label reaches this block or the case
1852 label doesn't have a contiguous range of values (like the
1853 default one) fail. */
1854 if (!l || CASE_CHAIN (l) || !CASE_LOW (l))
1855 has_valid_pred = false;
1856 else if (!CASE_HIGH (l)
1857 || operand_equal_p (CASE_LOW (l), CASE_HIGH (l)))
1858 {
1859 pred_info one_pred;
1860 one_pred.pred_lhs = gimple_switch_index (gs);
1861 one_pred.pred_rhs = CASE_LOW (l);
1862 one_pred.cond_code = EQ_EXPR;
1863 one_pred.invert = false;
1864 t_chain.safe_push (one_pred);
1865 has_valid_pred = true;
1866 }
1867 else
1868 {
1869 /* Support a case label with a range with
1870 two predicates. We're overcommitting on
1871 the MAX_CHAIN_LEN budget by at most a factor
1872 of two here. */
1873 pred_info one_pred;
1874 one_pred.pred_lhs = gimple_switch_index (gs);
1875 one_pred.pred_rhs = CASE_LOW (l);
1876 one_pred.cond_code = GE_EXPR;
1877 one_pred.invert = false;
1878 t_chain.safe_push (one_pred);
1879 one_pred.pred_rhs = CASE_HIGH (l);
1880 one_pred.cond_code = LE_EXPR;
1881 t_chain.safe_push (one_pred);
1882 has_valid_pred = true;
1883 }
1884 }
1885 else if (stmt_can_throw_internal (cfun, cond_stmt)
1886 && !(e->flags & EDGE_EH))
1887 /* Ignore the exceptional control flow and proceed as if
1888 E were a fallthru without a controlling predicate for
1889 both the USE (valid) and DEF (questionable) case. */
1890 has_valid_pred = true;
1891 else
1892 has_valid_pred = false;
1893
1894 /* For USE predicates we can drop components of the
1895 AND chain. */
1896 if (!has_valid_pred && !is_use)
1897 break;
1898 }
1899
1900 /* For DEF predicates we have to drop components of the OR chain
1901 on failure. */
1902 if (!has_valid_pred && !is_use)
1903 {
1904 t_chain.release ();
1905 continue;
1906 }
1907
1908 /* When we add || 1 simply prune the chain and return. */
1909 if (t_chain.is_empty ())
1910 {
1911 t_chain.release ();
1912 for (auto chain : m_preds)
1913 chain.release ();
1914 m_preds.truncate (0);
1915 break;
1916 }
1917
1918 m_preds.quick_push (t_chain);
1919 }
1920
1921 if (DEBUG_PREDICATE_ANALYZER && dump_file)
1922 dump (dump_file);
1923 }
1924
1925 /* Store a PRED in *THIS. */
1926
1927 void
1928 predicate::push_pred (const pred_info &pred)
1929 {
1930 pred_chain chain = vNULL;
1931 chain.safe_push (pred);
1932 m_preds.safe_push (chain);
1933 }
1934
1935 /* Dump predicates in *THIS to F. */
1936
1937 void
1938 predicate::dump (FILE *f) const
1939 {
1940 unsigned np = m_preds.length ();
1941 if (np == 0)
1942 {
1943 fprintf (f, "\tTRUE (empty)\n");
1944 return;
1945 }
1946
1947 for (unsigned i = 0; i < np; i++)
1948 {
1949 if (i > 0)
1950 fprintf (f, "\tOR (");
1951 else
1952 fprintf (f, "\t(");
1953 dump_pred_chain (f, m_preds[i]);
1954 fprintf (f, ")\n");
1955 }
1956 }
1957
1958 /* Dump predicates in *THIS to stderr. */
1959
1960 void
1961 predicate::debug () const
1962 {
1963 dump (stderr);
1964 }
1965
1966 /* Dump predicates in *THIS for STMT prepended by MSG to F. */
1967
1968 void
1969 predicate::dump (FILE *f, gimple *stmt, const char *msg) const
1970 {
1971 fprintf (f, "%s", msg);
1972 if (stmt)
1973 {
1974 fputc ('\t', f);
1975 print_gimple_stmt (f, stmt, 0);
1976 fprintf (f, " is conditional on:\n");
1977 }
1978
1979 dump (f);
1980 }
1981
1982 /* Initialize USE_PREDS with the predicates of the control dependence chains
1983 between the basic block DEF_BB that defines a variable of interst and
1984 USE_BB that uses the variable, respectively. */
1985
1986 bool
1987 uninit_analysis::init_use_preds (predicate &use_preds, basic_block def_bb,
1988 basic_block use_bb)
1989 {
1990 if (DEBUG_PREDICATE_ANALYZER && dump_file)
1991 fprintf (dump_file, "init_use_preds (def_bb = %u, use_bb = %u)\n",
1992 def_bb->index, use_bb->index);
1993
1994 gcc_assert (use_preds.is_empty ()
1995 && dominated_by_p (CDI_DOMINATORS, use_bb, def_bb));
1996
1997 /* Set CD_ROOT to the basic block closest to USE_BB that is the control
1998 equivalent of (is guarded by the same predicate as) DEF_BB that also
1999 dominates USE_BB. This mimics the inner loop in
2000 compute_control_dep_chain. */
2001 basic_block cd_root = def_bb;
2002 do
2003 {
2004 basic_block pdom = get_immediate_dominator (CDI_POST_DOMINATORS, cd_root);
2005
2006 /* Stop at a loop exit which is also postdominating cd_root. */
2007 if (single_pred_p (pdom) && !single_succ_p (cd_root))
2008 break;
2009
2010 if (!dominated_by_p (CDI_DOMINATORS, pdom, cd_root)
2011 || !dominated_by_p (CDI_DOMINATORS, use_bb, pdom))
2012 break;
2013
2014 cd_root = pdom;
2015 }
2016 while (1);
2017
2018 auto_bb_flag in_region (cfun);
2019 auto_vec<basic_block, 20> region (MIN (n_basic_blocks_for_fn (cfun),
2020 param_uninit_control_dep_attempts));
2021
2022 /* Set DEP_CHAINS to the set of edges between CD_ROOT and USE_BB.
2023 Each DEP_CHAINS element is a series of edges whose conditions
2024 are logical conjunctions. Together, the DEP_CHAINS vector is
2025 used below to initialize an OR expression of the conjunctions. */
2026 unsigned num_chains = 0;
2027 auto_vec<edge> *dep_chains = new auto_vec<edge>[MAX_NUM_CHAINS];
2028
2029 if (!dfs_mark_dominating_region (use_bb, cd_root, in_region, region)
2030 || !compute_control_dep_chain (cd_root, use_bb, dep_chains, &num_chains,
2031 in_region))
2032 {
2033 /* If the info in dep_chains is not complete we need to use a
2034 conservative approximation for the use predicate. */
2035 if (DEBUG_PREDICATE_ANALYZER && dump_file)
2036 fprintf (dump_file, "init_use_preds: dep_chain incomplete, using "
2037 "conservative approximation\n");
2038 num_chains = 1;
2039 dep_chains[0].truncate (0);
2040 simple_control_dep_chain (dep_chains[0], cd_root, use_bb);
2041 }
2042
2043 /* Unmark the region. */
2044 for (auto bb : region)
2045 bb->flags &= ~in_region;
2046
2047 /* From the set of edges computed above initialize *THIS as the OR
2048 condition under which the definition in DEF_BB is used in USE_BB.
2049 Each OR subexpression is represented by one element of DEP_CHAINS,
2050 where each element consists of a series of AND subexpressions. */
2051 use_preds.init_from_control_deps (dep_chains, num_chains, true);
2052 delete[] dep_chains;
2053 return !use_preds.is_empty ();
2054 }
2055
2056 /* Release resources in *THIS. */
2057
2058 predicate::~predicate ()
2059 {
2060 unsigned n = m_preds.length ();
2061 for (unsigned i = 0; i != n; ++i)
2062 m_preds[i].release ();
2063 m_preds.release ();
2064 }
2065
2066 /* Copy-assign RHS to *THIS. */
2067
2068 predicate&
2069 predicate::operator= (const predicate &rhs)
2070 {
2071 if (this == &rhs)
2072 return *this;
2073
2074 m_cval = rhs.m_cval;
2075
2076 unsigned n = m_preds.length ();
2077 for (unsigned i = 0; i != n; ++i)
2078 m_preds[i].release ();
2079 m_preds.release ();
2080
2081 n = rhs.m_preds.length ();
2082 for (unsigned i = 0; i != n; ++i)
2083 {
2084 const pred_chain &chain = rhs.m_preds[i];
2085 m_preds.safe_push (chain.copy ());
2086 }
2087
2088 return *this;
2089 }
2090
2091 /* For each use edge of PHI, compute all control dependence chains
2092 and convert those to the composite predicates in M_PREDS.
2093 Return true if a nonempty predicate has been obtained. */
2094
2095 bool
2096 uninit_analysis::init_from_phi_def (gphi *phi)
2097 {
2098 gcc_assert (m_phi_def_preds.is_empty ());
2099
2100 basic_block phi_bb = gimple_bb (phi);
2101 /* Find the closest dominating bb to be the control dependence root. */
2102 basic_block cd_root = get_immediate_dominator (CDI_DOMINATORS, phi_bb);
2103 if (!cd_root)
2104 return false;
2105
2106 /* Set DEF_EDGES to the edges to the PHI from the bb's that provide
2107 definitions of each of the PHI operands for which M_EVAL is false. */
2108 auto_vec<edge> def_edges;
2109 hash_set<gimple *> visited_phis;
2110 collect_phi_def_edges (phi, cd_root, &def_edges, &visited_phis);
2111
2112 unsigned nedges = def_edges.length ();
2113 if (nedges == 0)
2114 return false;
2115
2116 auto_bb_flag in_region (cfun);
2117 auto_vec<basic_block, 20> region (MIN (n_basic_blocks_for_fn (cfun),
2118 param_uninit_control_dep_attempts));
2119 /* Pre-mark the PHI incoming edges PHI block to make sure we only walk
2120 interesting edges from there. */
2121 for (unsigned i = 0; i < nedges; i++)
2122 {
2123 if (!(def_edges[i]->dest->flags & in_region))
2124 {
2125 if (!region.space (1))
2126 break;
2127 def_edges[i]->dest->flags |= in_region;
2128 region.quick_push (def_edges[i]->dest);
2129 }
2130 }
2131 for (unsigned i = 0; i < nedges; i++)
2132 if (!dfs_mark_dominating_region (def_edges[i]->src, cd_root,
2133 in_region, region))
2134 break;
2135
2136 unsigned num_chains = 0;
2137 auto_vec<edge> *dep_chains = new auto_vec<edge>[MAX_NUM_CHAINS];
2138 for (unsigned i = 0; i < nedges; i++)
2139 {
2140 edge e = def_edges[i];
2141 unsigned prev_nc = num_chains;
2142 bool complete_p = compute_control_dep_chain (cd_root, e->src, dep_chains,
2143 &num_chains, in_region);
2144
2145 /* Update the newly added chains with the phi operand edge. */
2146 if (EDGE_COUNT (e->src->succs) > 1)
2147 {
2148 if (complete_p
2149 && prev_nc == num_chains
2150 && num_chains < MAX_NUM_CHAINS)
2151 /* We can only add a chain for the PHI operand edge when the
2152 collected info was complete, otherwise the predicate may
2153 not be conservative. */
2154 dep_chains[num_chains++] = vNULL;
2155 for (unsigned j = prev_nc; j < num_chains; j++)
2156 dep_chains[j].safe_push (e);
2157 }
2158 }
2159
2160 /* Unmark the region. */
2161 for (auto bb : region)
2162 bb->flags &= ~in_region;
2163
2164 /* Convert control dependence chains to the predicate in *THIS under
2165 which the PHI operands are defined to values for which M_EVAL is
2166 false. */
2167 m_phi_def_preds.init_from_control_deps (dep_chains, num_chains, false);
2168 delete[] dep_chains;
2169 return !m_phi_def_preds.is_empty ();
2170 }
2171
2172 /* Compute the predicates that guard the use USE_STMT and check if
2173 the incoming paths that have an empty (or possibly empty) definition
2174 can be pruned. Return true if it can be determined that the use of
2175 PHI's def in USE_STMT is guarded by a predicate set that does not
2176 overlap with the predicate sets of all runtime paths that do not
2177 have a definition.
2178
2179 Return false if the use is not guarded or if it cannot be determined.
2180 USE_BB is the bb of the use (for phi operand use, the bb is not the bb
2181 of the phi stmt, but the source bb of the operand edge).
2182
2183 OPNDS is a bitmap with a bit set for each PHI operand of interest.
2184
2185 THIS->M_PREDS contains the (memoized) defining predicate chains of
2186 a PHI. If THIS->M_PREDS is empty, the PHI's defining predicate
2187 chains are computed and stored into THIS->M_PREDS as needed.
2188
2189 VISITED_PHIS is a pointer set of phis being visited. */
2190
2191 bool
2192 uninit_analysis::is_use_guarded (gimple *use_stmt, basic_block use_bb,
2193 gphi *phi, unsigned opnds,
2194 hash_set<gphi *> *visited)
2195 {
2196 if (visited->add (phi))
2197 return false;
2198
2199 /* The basic block where the PHI is defined. */
2200 basic_block def_bb = gimple_bb (phi);
2201
2202 /* Try to build the predicate expression under which the PHI flows
2203 into its use. This will be empty if the PHI is defined and used
2204 in the same bb. */
2205 predicate use_preds (true);
2206 if (!init_use_preds (use_preds, def_bb, use_bb))
2207 return false;
2208
2209 use_preds.simplify (use_stmt, /*is_use=*/true);
2210 use_preds.normalize (use_stmt, /*is_use=*/true);
2211 if (use_preds.is_false ())
2212 return true;
2213 if (use_preds.is_true ())
2214 return false;
2215
2216 /* Try to prune the dead incoming phi edges. */
2217 if (!overlap (phi, opnds, visited, use_preds))
2218 {
2219 if (DEBUG_PREDICATE_ANALYZER && dump_file)
2220 fputs ("found predicate overlap\n", dump_file);
2221
2222 return true;
2223 }
2224
2225 if (m_phi_def_preds.is_empty ())
2226 {
2227 /* Lazily initialize *THIS from PHI. */
2228 if (!init_from_phi_def (phi))
2229 return false;
2230
2231 m_phi_def_preds.simplify (phi);
2232 m_phi_def_preds.normalize (phi);
2233 if (m_phi_def_preds.is_false ())
2234 return false;
2235 if (m_phi_def_preds.is_true ())
2236 return true;
2237 }
2238
2239 /* Return true if the predicate guarding the valid definition (i.e.,
2240 *THIS) is a superset of the predicate guarding the use (i.e.,
2241 USE_PREDS). */
2242 if (m_phi_def_preds.superset_of (use_preds))
2243 return true;
2244
2245 return false;
2246 }
2247
2248 /* Public interface to the above. */
2249
2250 bool
2251 uninit_analysis::is_use_guarded (gimple *stmt, basic_block use_bb, gphi *phi,
2252 unsigned opnds)
2253 {
2254 hash_set<gphi *> visited;
2255 return is_use_guarded (stmt, use_bb, phi, opnds, &visited);
2256 }
2257