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