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