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