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