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