1 /* Support for simple predicate analysis.
3 Copyright (C) 2001-2022 Free Software Foundation, Inc.
4 Contributed by Xinliang David Li <davidxl@google.com>
5 Generalized by Martin Sebor <msebor@redhat.com>
7 This file is part of GCC.
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)
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.
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/>. */
23 #define INCLUDE_STRING
26 #include "coretypes.h"
30 #include "tree-pass.h"
32 #include "gimple-pretty-print.h"
33 #include "diagnostic-core.h"
34 #include "fold-const.h"
35 #include "gimple-iterator.h"
42 #include "value-query.h"
44 #include "gimple-predicate-analysis.h"
46 #define DEBUG_PREDICATE_ANALYZER 1
48 /* Return true if BB1 is postdominating BB2 and BB1 is not a loop exit
49 bb. The loop exit bb check is simple and does not cover all cases. */
52 is_non_loop_exit_postdominating (basic_block bb1
, basic_block bb2
)
54 if (!dominated_by_p (CDI_POST_DOMINATORS
, bb2
, bb1
))
57 if (single_pred_p (bb1
) && !single_succ_p (bb2
))
63 /* Find BB's closest postdominator that is its control equivalent (i.e.,
64 that's controlled by the same predicate). */
66 static inline basic_block
67 find_control_equiv_block (basic_block bb
)
69 basic_block pdom
= get_immediate_dominator (CDI_POST_DOMINATORS
, bb
);
71 /* Skip the postdominating bb that is also a loop exit. */
72 if (!is_non_loop_exit_postdominating (pdom
, bb
))
75 /* If the postdominator is dominated by BB, return it. */
76 if (dominated_by_p (CDI_DOMINATORS
, pdom
, bb
))
82 /* Return true if X1 is the negation of X2. */
85 pred_neg_p (const pred_info
&x1
, const pred_info
&x2
)
87 if (!operand_equal_p (x1
.pred_lhs
, x2
.pred_lhs
, 0)
88 || !operand_equal_p (x1
.pred_rhs
, x2
.pred_rhs
, 0))
91 tree_code c1
= x1
.cond_code
, c2
;
92 if (x1
.invert
== x2
.invert
)
93 c2
= invert_tree_comparison (x2
.cond_code
, false);
100 /* Return whether the condition (VAL CMPC BOUNDARY) is true. */
103 is_value_included_in (tree val
, tree boundary
, tree_code cmpc
)
105 /* Only handle integer constant here. */
106 if (TREE_CODE (val
) != INTEGER_CST
|| TREE_CODE (boundary
) != INTEGER_CST
)
109 bool inverted
= false;
110 if (cmpc
== GE_EXPR
|| cmpc
== GT_EXPR
|| cmpc
== NE_EXPR
)
112 cmpc
= invert_tree_comparison (cmpc
, false);
118 result
= tree_int_cst_equal (val
, boundary
);
119 else if (cmpc
== LT_EXPR
)
120 result
= tree_int_cst_lt (val
, boundary
);
123 gcc_assert (cmpc
== LE_EXPR
);
124 result
= tree_int_cst_le (val
, boundary
);
133 /* Format the vector of edges EV as a string. */
136 format_edge_vec (const vec
<edge
> &ev
)
140 unsigned n
= ev
.length ();
141 for (unsigned i
= 0; i
< n
; ++i
)
144 const_edge e
= ev
[i
];
145 sprintf (es
, "%u", e
->src
->index
);
153 /* Format the first N elements of the array of vector of edges EVA as
157 format_edge_vecs (const vec
<edge
> eva
[], unsigned n
)
161 for (unsigned i
= 0; i
!= n
; ++i
)
164 str
+= format_edge_vec (eva
[i
]);
172 /* Dump a single pred_info to DUMP_FILE. */
175 dump_pred_info (const pred_info
&pred
)
178 fprintf (dump_file
, "NOT (");
179 print_generic_expr (dump_file
, pred
.pred_lhs
);
180 fprintf (dump_file
, " %s ", op_symbol_code (pred
.cond_code
));
181 print_generic_expr (dump_file
, pred
.pred_rhs
);
183 fputc (')', dump_file
);
186 /* Dump a pred_chain to DUMP_FILE. */
189 dump_pred_chain (const pred_chain
&chain
)
191 unsigned np
= chain
.length ();
193 fprintf (dump_file
, "AND (");
195 for (unsigned j
= 0; j
< np
; j
++)
197 dump_pred_info (chain
[j
]);
199 fprintf (dump_file
, ", ");
201 fputc (')', dump_file
);
205 /* Dump the predicate chain PREDS for STMT, prefixed by MSG. */
208 dump_predicates (gimple
*stmt
, const pred_chain_union
&preds
, const char *msg
)
210 fprintf (dump_file
, "%s", msg
);
213 print_gimple_stmt (dump_file
, stmt
, 0);
214 fprintf (dump_file
, "is guarded by:\n");
217 unsigned np
= preds
.length ();
219 fprintf (dump_file
, "OR (");
220 for (unsigned i
= 0; i
< np
; i
++)
222 dump_pred_chain (preds
[i
]);
224 fprintf (dump_file
, ", ");
226 fputc (')', dump_file
);
228 fputc ('\n', dump_file
);
231 /* Dump the first NCHAINS elements of the DEP_CHAINS array into DUMP_FILE. */
234 dump_dep_chains (const auto_vec
<edge
> dep_chains
[], unsigned nchains
)
239 for (unsigned i
= 0; i
!= nchains
; ++i
)
241 const auto_vec
<edge
> &v
= dep_chains
[i
];
242 unsigned n
= v
.length ();
243 for (unsigned j
= 0; j
!= n
; ++j
)
245 fprintf (dump_file
, "%u", v
[j
]->src
->index
);
247 fprintf (dump_file
, " -> ");
249 fputc ('\n', dump_file
);
253 /* Return the 'normalized' conditional code with operand swapping
254 and condition inversion controlled by SWAP_COND and INVERT. */
257 get_cmp_code (tree_code orig_cmp_code
, bool swap_cond
, bool invert
)
259 tree_code tc
= orig_cmp_code
;
262 tc
= swap_tree_comparison (orig_cmp_code
);
264 tc
= invert_tree_comparison (tc
, false);
281 /* Return true if PRED is common among all predicate chains in PREDS
282 (and therefore can be factored out). */
285 find_matching_predicate_in_rest_chains (const pred_info
&pred
,
286 const pred_chain_union
&preds
)
289 if (preds
.length () == 1)
292 for (unsigned i
= 1; i
< preds
.length (); i
++)
295 const pred_chain
&chain
= preds
[i
];
296 unsigned n
= chain
.length ();
297 for (unsigned j
= 0; j
< n
; j
++)
299 const pred_info
&pred2
= chain
[j
];
300 /* Can relax the condition comparison to not use address
301 comparison. However, the most common case is that
302 multiple control dependent paths share a common path
303 prefix, so address comparison should be ok. */
304 if (operand_equal_p (pred2
.pred_lhs
, pred
.pred_lhs
, 0)
305 && operand_equal_p (pred2
.pred_rhs
, pred
.pred_rhs
, 0)
306 && pred2
.invert
== pred
.invert
)
318 /* Find a predicate to examine against paths of interest. If there
319 is no predicate of the "FLAG_VAR CMP CONST" form, try to find one
320 of that's the form "FLAG_VAR CMP FLAG_VAR" with value range info.
321 PHI is the phi node whose incoming (interesting) paths need to be
322 examined. On success, return the comparison code, set defintion
323 gimple of FLAG_DEF and BOUNDARY_CST. Otherwise return ERROR_MARK. */
326 find_var_cmp_const (pred_chain_union preds
, gphi
*phi
, gimple
**flag_def
,
329 tree_code vrinfo_code
= ERROR_MARK
;
330 gimple
*vrinfo_def
= NULL
;
331 tree vrinfo_cst
= NULL
;
333 gcc_assert (preds
.length () > 0);
334 pred_chain chain
= preds
[0];
335 for (unsigned i
= 0; i
< chain
.length (); i
++)
337 bool use_vrinfo_p
= false;
338 const pred_info
&pred
= chain
[i
];
339 tree cond_lhs
= pred
.pred_lhs
;
340 tree cond_rhs
= pred
.pred_rhs
;
341 if (cond_lhs
== NULL_TREE
|| cond_rhs
== NULL_TREE
)
344 tree_code code
= get_cmp_code (pred
.cond_code
, false, pred
.invert
);
345 if (code
== ERROR_MARK
)
348 /* Convert to the canonical form SSA_NAME CMP CONSTANT. */
349 if (TREE_CODE (cond_lhs
) == SSA_NAME
350 && is_gimple_constant (cond_rhs
))
352 else if (TREE_CODE (cond_rhs
) == SSA_NAME
353 && is_gimple_constant (cond_lhs
))
355 std::swap (cond_lhs
, cond_rhs
);
356 if ((code
= get_cmp_code (code
, true, false)) == ERROR_MARK
)
359 /* Check if we can take advantage of FLAG_VAR COMP FLAG_VAR predicate
360 with value range info. Note only first of such case is handled. */
361 else if (vrinfo_code
== ERROR_MARK
362 && TREE_CODE (cond_lhs
) == SSA_NAME
363 && TREE_CODE (cond_rhs
) == SSA_NAME
)
365 gimple
* lhs_def
= SSA_NAME_DEF_STMT (cond_lhs
);
366 if (!lhs_def
|| gimple_code (lhs_def
) != GIMPLE_PHI
367 || gimple_bb (lhs_def
) != gimple_bb (phi
))
369 std::swap (cond_lhs
, cond_rhs
);
370 if ((code
= get_cmp_code (code
, true, false)) == ERROR_MARK
)
374 /* Check value range info of rhs, do following transforms:
375 flag_var < [min, max] -> flag_var < max
376 flag_var > [min, max] -> flag_var > min
378 We can also transform LE_EXPR/GE_EXPR to LT_EXPR/GT_EXPR:
379 flag_var <= [min, max] -> flag_var < [min, max+1]
380 flag_var >= [min, max] -> flag_var > [min-1, max]
381 if no overflow/wrap. */
382 tree type
= TREE_TYPE (cond_lhs
);
384 if (!INTEGRAL_TYPE_P (type
)
385 || !get_range_query (cfun
)->range_of_expr (r
, cond_rhs
)
386 || r
.kind () != VR_RANGE
)
389 wide_int min
= r
.lower_bound ();
390 wide_int max
= r
.upper_bound ();
392 && max
!= wi::max_value (TYPE_PRECISION (type
), TYPE_SIGN (type
)))
398 && min
!= wi::min_value (TYPE_PRECISION (type
), TYPE_SIGN (type
)))
404 cond_rhs
= wide_int_to_tree (type
, max
);
405 else if (code
== GT_EXPR
)
406 cond_rhs
= wide_int_to_tree (type
, min
);
415 if ((*flag_def
= SSA_NAME_DEF_STMT (cond_lhs
)) == NULL
)
418 if (gimple_code (*flag_def
) != GIMPLE_PHI
419 || gimple_bb (*flag_def
) != gimple_bb (phi
)
420 || !find_matching_predicate_in_rest_chains (pred
, preds
))
423 /* Return if any "flag_var comp const" predicate is found. */
426 *boundary_cst
= cond_rhs
;
429 /* Record if any "flag_var comp flag_var[vinfo]" predicate is found. */
430 else if (vrinfo_code
== ERROR_MARK
)
433 vrinfo_def
= *flag_def
;
434 vrinfo_cst
= cond_rhs
;
437 /* Return the "flag_var cmp flag_var[vinfo]" predicate we found. */
438 if (vrinfo_code
!= ERROR_MARK
)
440 *flag_def
= vrinfo_def
;
441 *boundary_cst
= vrinfo_cst
;
446 /* Return true if all interesting opnds are pruned, false otherwise.
447 PHI is the phi node with interesting operands, OPNDS is the bitmap
448 of the interesting operand positions, FLAG_DEF is the statement
449 defining the flag guarding the use of the PHI output, BOUNDARY_CST
450 is the const value used in the predicate associated with the flag,
451 CMP_CODE is the comparison code used in the predicate, VISITED_PHIS
452 is the pointer set of phis visited, and VISITED_FLAG_PHIS is
453 the pointer to the pointer set of flag definitions that are also
459 flag_1 = phi <0, 1> // (1)
460 var_1 = phi <undef, some_val>
464 flag_2 = phi <0, flag_1, flag_1> // (2)
465 var_2 = phi <undef, var_1, var_1>
472 Because some flag arg in (1) is not constant, if we do not look into
473 the flag phis recursively, it is conservatively treated as unknown and
474 var_1 is thought to flow into use at (3). Since var_1 is potentially
475 uninitialized a false warning will be emitted.
476 Checking recursively into (1), the compiler can find out that only
477 some_val (which is defined) can flow into (3) which is OK. */
480 prune_phi_opnds (gphi
*phi
, unsigned opnds
, gphi
*flag_def
,
481 tree boundary_cst
, tree_code cmp_code
,
482 predicate::func_t
&eval
,
483 hash_set
<gphi
*> *visited_phis
,
484 bitmap
*visited_flag_phis
)
486 /* The Boolean predicate guarding the PHI definition. Initialized
487 lazily from PHI in the first call to is_use_guarded() and cached
488 for subsequent iterations. */
489 predicate
def_preds (eval
);
491 unsigned n
= MIN (eval
.max_phi_args
, gimple_phi_num_args (flag_def
));
492 for (unsigned i
= 0; i
< n
; i
++)
494 if (!MASK_TEST_BIT (opnds
, i
))
497 tree flag_arg
= gimple_phi_arg_def (flag_def
, i
);
498 if (!is_gimple_constant (flag_arg
))
500 if (TREE_CODE (flag_arg
) != SSA_NAME
)
503 gphi
*flag_arg_def
= dyn_cast
<gphi
*> (SSA_NAME_DEF_STMT (flag_arg
));
507 tree phi_arg
= gimple_phi_arg_def (phi
, i
);
508 if (TREE_CODE (phi_arg
) != SSA_NAME
)
511 gphi
*phi_arg_def
= dyn_cast
<gphi
*> (SSA_NAME_DEF_STMT (phi_arg
));
515 if (gimple_bb (phi_arg_def
) != gimple_bb (flag_arg_def
))
518 if (!*visited_flag_phis
)
519 *visited_flag_phis
= BITMAP_ALLOC (NULL
);
521 tree phi_result
= gimple_phi_result (flag_arg_def
);
522 if (bitmap_bit_p (*visited_flag_phis
, SSA_NAME_VERSION (phi_result
)))
525 bitmap_set_bit (*visited_flag_phis
, SSA_NAME_VERSION (phi_result
));
527 /* Now recursively try to prune the interesting phi args. */
528 unsigned opnds_arg_phi
= eval
.phi_arg_set (phi_arg_def
);
529 if (!prune_phi_opnds (phi_arg_def
, opnds_arg_phi
, flag_arg_def
,
530 boundary_cst
, cmp_code
, eval
, visited_phis
,
534 bitmap_clear_bit (*visited_flag_phis
, SSA_NAME_VERSION (phi_result
));
538 /* Now check if the constant is in the guarded range. */
539 if (is_value_included_in (flag_arg
, boundary_cst
, cmp_code
))
541 /* Now that we know that this undefined edge is not pruned.
542 If the operand is defined by another phi, we can further
543 prune the incoming edges of that phi by checking
544 the predicates of this operands. */
546 tree opnd
= gimple_phi_arg_def (phi
, i
);
547 gimple
*opnd_def
= SSA_NAME_DEF_STMT (opnd
);
548 if (gphi
*opnd_def_phi
= dyn_cast
<gphi
*> (opnd_def
))
550 unsigned opnds2
= eval
.phi_arg_set (opnd_def_phi
);
551 if (!MASK_EMPTY (opnds2
))
553 edge opnd_edge
= gimple_phi_arg_edge (phi
, i
);
554 if (def_preds
.is_use_guarded (phi
, opnd_edge
->src
,
555 opnd_def_phi
, opnds2
,
568 /* Recursively compute the set PHI's incoming edges with "uninteresting"
569 operands of a phi chain, i.e., those for which EVAL returns false.
570 CD_ROOT is the control dependence root from which edges are collected
571 up the CFG nodes that it's dominated by. *EDGES holds the result, and
572 VISITED is used for detecting cycles. */
575 collect_phi_def_edges (gphi
*phi
, basic_block cd_root
, auto_vec
<edge
> *edges
,
576 predicate::func_t
&eval
, hash_set
<gimple
*> *visited
)
578 if (visited
->elements () == 0
579 && DEBUG_PREDICATE_ANALYZER
582 fprintf (dump_file
, "%s for cd_root %u and ",
583 __func__
, cd_root
->index
);
584 print_gimple_stmt (dump_file
, phi
, 0);
588 if (visited
->add (phi
))
591 unsigned n
= gimple_phi_num_args (phi
);
592 for (unsigned i
= 0; i
< n
; i
++)
594 edge opnd_edge
= gimple_phi_arg_edge (phi
, i
);
595 tree opnd
= gimple_phi_arg_def (phi
, i
);
597 if (TREE_CODE (opnd
) == SSA_NAME
)
599 gimple
*def
= SSA_NAME_DEF_STMT (opnd
);
601 if (gimple_code (def
) == GIMPLE_PHI
602 && dominated_by_p (CDI_DOMINATORS
, gimple_bb (def
), cd_root
))
603 collect_phi_def_edges (as_a
<gphi
*> (def
), cd_root
, edges
, eval
,
605 else if (!eval (opnd
))
607 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
610 "\tFound def edge %i -> %i for cd_root %i "
611 "and operand %u of: ",
612 opnd_edge
->src
->index
, opnd_edge
->dest
->index
,
614 print_gimple_stmt (dump_file
, phi
, 0);
616 edges
->safe_push (opnd_edge
);
621 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
624 "\tFound def edge %i -> %i for cd_root %i "
625 "and operand %u of: ",
626 opnd_edge
->src
->index
, opnd_edge
->dest
->index
,
628 print_gimple_stmt (dump_file
, phi
, 0);
632 edges
->safe_push (opnd_edge
);
637 /* Return an expression corresponding to the predicate PRED. */
640 build_pred_expr (const pred_info
&pred
)
642 tree_code cond_code
= pred
.cond_code
;
643 tree lhs
= pred
.pred_lhs
;
644 tree rhs
= pred
.pred_rhs
;
647 cond_code
= invert_tree_comparison (cond_code
, false);
649 return build2 (cond_code
, TREE_TYPE (lhs
), lhs
, rhs
);
652 /* Return an expression corresponding to PREDS. */
655 build_pred_expr (const pred_chain_union
&preds
, bool invert
= false)
657 tree_code code
= invert
? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
;
658 tree_code subcode
= invert
? TRUTH_OR_EXPR
: TRUTH_AND_EXPR
;
660 tree expr
= NULL_TREE
;
661 for (unsigned i
= 0; i
!= preds
.length (); ++i
)
663 tree subexpr
= NULL_TREE
;
664 for (unsigned j
= 0; j
!= preds
[i
].length (); ++j
)
666 const pred_info
&pi
= preds
[i
][j
];
667 tree cond
= build_pred_expr (pi
);
669 cond
= invert_truthvalue (cond
);
670 subexpr
= subexpr
? build2 (subcode
, boolean_type_node
,
671 subexpr
, cond
) : cond
;
674 expr
= build2 (code
, boolean_type_node
, expr
, subexpr
);
682 /* Return a bitset of all PHI arguments or zero if there are too many. */
685 predicate::func_t::phi_arg_set (gphi
*phi
)
687 unsigned n
= gimple_phi_num_args (phi
);
689 if (max_phi_args
< n
)
692 /* Set the least significant N bits. */
693 return (1U << n
) - 1;
696 /* Determine if the predicate set of the use does not overlap with that
697 of the interesting paths. The most common senario of guarded use is
702 x = ...; // set x to valid
709 use (x); // use when x is valid
711 The real world examples are usually more complicated, but similar
712 and usually result from inlining:
714 bool init_func (int * x)
718 *x = ...; // set *x to valid
730 use (x); // use when x is valid
733 Another possible use scenario is in the following trivial example:
745 Predicate analysis needs to compute the composite predicate:
747 1) 'x' use predicate: (n > 0) .AND. (m < 2)
748 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
749 (the predicate chain for phi operand defs can be computed
750 starting from a bb that is control equivalent to the phi's
751 bb and is dominating the operand def.)
753 and check overlapping:
754 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
757 This implementation provides a framework that can handle different
758 scenarios. (Note that many simple cases are handled properly without
759 the predicate analysis if jump threading eliminates the merge point
760 thus makes path-sensitive analysis unnecessary.)
762 PHI is the phi node whose incoming (undefined) paths need to be
763 pruned, and OPNDS is the bitmap holding interesting operand
764 positions. VISITED is the pointer set of phi stmts being
768 predicate::overlap (gphi
*phi
, unsigned opnds
, hash_set
<gphi
*> *visited
)
770 gimple
*flag_def
= NULL
;
771 tree boundary_cst
= NULL_TREE
;
772 bitmap visited_flag_phis
= NULL
;
774 /* Find within the common prefix of multiple predicate chains
775 a predicate that is a comparison of a flag variable against
777 tree_code cmp_code
= find_var_cmp_const (m_preds
, phi
, &flag_def
,
779 if (cmp_code
== ERROR_MARK
)
782 /* Now check all the uninit incoming edges have a constant flag
783 value that is in conflict with the use guard/predicate. */
784 gphi
*phi_def
= as_a
<gphi
*> (flag_def
);
785 bool all_pruned
= prune_phi_opnds (phi
, opnds
, phi_def
, boundary_cst
,
786 cmp_code
, m_eval
, visited
,
789 if (visited_flag_phis
)
790 BITMAP_FREE (visited_flag_phis
);
795 /* Return true if two predicates PRED1 and X2 are equivalent. Assume
796 the expressions have already properly re-associated. */
799 pred_equal_p (const pred_info
&pred1
, const pred_info
&pred2
)
801 if (!operand_equal_p (pred1
.pred_lhs
, pred2
.pred_lhs
, 0)
802 || !operand_equal_p (pred1
.pred_rhs
, pred2
.pred_rhs
, 0))
805 tree_code c1
= pred1
.cond_code
, c2
;
806 if (pred1
.invert
!= pred2
.invert
807 && TREE_CODE_CLASS (pred2
.cond_code
) == tcc_comparison
)
808 c2
= invert_tree_comparison (pred2
.cond_code
, false);
810 c2
= pred2
.cond_code
;
815 /* Return true if PRED tests inequality (i.e., X != Y). */
818 is_neq_relop_p (const pred_info
&pred
)
821 return ((pred
.cond_code
== NE_EXPR
&& !pred
.invert
)
822 || (pred
.cond_code
== EQ_EXPR
&& pred
.invert
));
825 /* Returns true if PRED is of the form X != 0. */
828 is_neq_zero_form_p (const pred_info
&pred
)
830 if (!is_neq_relop_p (pred
) || !integer_zerop (pred
.pred_rhs
)
831 || TREE_CODE (pred
.pred_lhs
) != SSA_NAME
)
836 /* Return true if PRED is equivalent to X != 0. */
839 pred_expr_equal_p (const pred_info
&pred
, tree expr
)
841 if (!is_neq_zero_form_p (pred
))
844 return operand_equal_p (pred
.pred_lhs
, expr
, 0);
847 /* Return true if VAL satisfies (x CMPC BOUNDARY) predicate. CMPC can
848 be either one of the range comparison codes ({GE,LT,EQ,NE}_EXPR and
849 the like), or BIT_AND_EXPR. EXACT_P is only meaningful for the latter.
850 Modify the question from VAL & BOUNDARY != 0 to VAL & BOUNDARY == VAL.
851 For other values of CMPC, EXACT_P is ignored. */
854 value_sat_pred_p (tree val
, tree boundary
, tree_code cmpc
,
855 bool exact_p
= false)
857 if (cmpc
!= BIT_AND_EXPR
)
858 return is_value_included_in (val
, boundary
, cmpc
);
860 wide_int andw
= wi::to_wide (val
) & wi::to_wide (boundary
);
862 return andw
== wi::to_wide (val
);
864 return andw
.to_uhwi ();
867 /* Return true if the domain of single predicate expression PRED1
868 is a subset of that of PRED2, and false if it cannot be proved. */
871 subset_of (const pred_info
&pred1
, const pred_info
&pred2
)
873 if (pred_equal_p (pred1
, pred2
))
876 if ((TREE_CODE (pred1
.pred_rhs
) != INTEGER_CST
)
877 || (TREE_CODE (pred2
.pred_rhs
) != INTEGER_CST
))
880 if (!operand_equal_p (pred1
.pred_lhs
, pred2
.pred_lhs
, 0))
883 tree_code code1
= pred1
.cond_code
;
885 code1
= invert_tree_comparison (code1
, false);
886 tree_code code2
= pred2
.cond_code
;
888 code2
= invert_tree_comparison (code2
, false);
890 if (code2
== NE_EXPR
&& code1
== NE_EXPR
)
893 if (code2
== NE_EXPR
)
894 return !value_sat_pred_p (pred2
.pred_rhs
, pred1
.pred_rhs
, code1
);
896 if (code1
== EQ_EXPR
)
897 return value_sat_pred_p (pred1
.pred_rhs
, pred2
.pred_rhs
, code2
);
900 return value_sat_pred_p (pred1
.pred_rhs
, pred2
.pred_rhs
, code2
,
901 code1
== BIT_AND_EXPR
);
906 /* Return true if the domain of CHAIN1 is a subset of that of CHAIN2.
907 Return false if it cannot be proven so. */
910 subset_of (const pred_chain
&chain1
, const pred_chain
&chain2
)
912 unsigned np1
= chain1
.length ();
913 unsigned np2
= chain2
.length ();
914 for (unsigned i2
= 0; i2
< np2
; i2
++)
917 const pred_info
&info2
= chain2
[i2
];
918 for (unsigned i1
= 0; i1
< np1
; i1
++)
920 const pred_info
&info1
= chain1
[i1
];
921 if (subset_of (info1
, info2
))
933 /* Return true if the domain defined by the predicate chain PREDS is
934 a subset of the domain of *THIS. Return false if PREDS's domain
935 is not a subset of any of the sub-domains of *THIS (corresponding
936 to each individual chains in it), even though it may be still be
937 a subset of whole domain of *THIS which is the union (ORed) of all
938 its subdomains. In other words, the result is conservative. */
941 predicate::includes (const pred_chain
&chain
) const
943 for (unsigned i
= 0; i
< m_preds
.length (); i
++)
944 if (subset_of (chain
, m_preds
[i
]))
950 /* Return true if the domain defined by *THIS is a superset of PREDS's
952 Avoid building generic trees (and rely on the folding capability
953 of the compiler), and instead perform brute force comparison of
954 individual predicate chains (this won't be a computationally costly
955 since the chains are pretty short). Returning false does not
956 necessarily mean *THIS is not a superset of *PREDS, only that
957 it need not be since the analysis cannot prove it. */
960 predicate::superset_of (const predicate
&preds
) const
962 for (unsigned i
= 0; i
< preds
.m_preds
.length (); i
++)
963 if (!includes (preds
.m_preds
[i
]))
969 /* Create a predicate of the form OP != 0 and push it the work list CHAIN. */
972 push_to_worklist (tree op
, pred_chain
*chain
, hash_set
<tree
> *mark_set
)
974 if (mark_set
->contains (op
))
979 arg_pred
.pred_lhs
= op
;
980 arg_pred
.pred_rhs
= integer_zero_node
;
981 arg_pred
.cond_code
= NE_EXPR
;
982 arg_pred
.invert
= false;
983 chain
->safe_push (arg_pred
);
986 /* Return a pred_info for a gimple assignment CMP_ASSIGN with comparison
990 get_pred_info_from_cmp (const gimple
*cmp_assign
)
993 pred
.pred_lhs
= gimple_assign_rhs1 (cmp_assign
);
994 pred
.pred_rhs
= gimple_assign_rhs2 (cmp_assign
);
995 pred
.cond_code
= gimple_assign_rhs_code (cmp_assign
);
1000 /* If PHI is a degenerate phi with all operands having the same value (relop)
1001 update *PRED to that value and return true. Otherwise return false. */
1004 is_degenerate_phi (gimple
*phi
, pred_info
*pred
)
1006 tree op0
= gimple_phi_arg_def (phi
, 0);
1008 if (TREE_CODE (op0
) != SSA_NAME
)
1011 gimple
*def0
= SSA_NAME_DEF_STMT (op0
);
1012 if (gimple_code (def0
) != GIMPLE_ASSIGN
)
1015 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def0
)) != tcc_comparison
)
1018 pred_info pred0
= get_pred_info_from_cmp (def0
);
1020 unsigned n
= gimple_phi_num_args (phi
);
1021 for (unsigned i
= 1; i
< n
; ++i
)
1023 tree op
= gimple_phi_arg_def (phi
, i
);
1024 if (TREE_CODE (op
) != SSA_NAME
)
1027 gimple
*def
= SSA_NAME_DEF_STMT (op
);
1028 if (gimple_code (def
) != GIMPLE_ASSIGN
)
1031 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def
)) != tcc_comparison
)
1034 pred_info pred
= get_pred_info_from_cmp (def
);
1035 if (!pred_equal_p (pred
, pred0
))
1043 /* Recursively compute the control dependence chains (paths of edges)
1044 from the dependent basic block, DEP_BB, up to the dominating basic
1045 block, DOM_BB (the head node of a chain should be dominated by it),
1046 storing them in the CD_CHAINS array.
1047 CUR_CD_CHAIN is the current chain being computed.
1048 *NUM_CHAINS is total number of chains in the CD_CHAINS array.
1049 *NUM_CALLS is the number of recursive calls to control unbounded
1051 Return true if the information is successfully computed, false if
1052 there is no control dependence or not computed. */
1055 compute_control_dep_chain (basic_block dom_bb
, const_basic_block dep_bb
,
1056 vec
<edge
> cd_chains
[], unsigned *num_chains
,
1057 vec
<edge
> &cur_cd_chain
, unsigned *num_calls
,
1060 if (*num_calls
> (unsigned)param_uninit_control_dep_attempts
)
1063 fprintf (dump_file
, "param_uninit_control_dep_attempts exceeded: %u\n",
1069 /* FIXME: Use a set instead. */
1070 unsigned cur_chain_len
= cur_cd_chain
.length ();
1071 if (cur_chain_len
> MAX_CHAIN_LEN
)
1074 fprintf (dump_file
, "MAX_CHAIN_LEN exceeded: %u\n", cur_chain_len
);
1079 if (cur_chain_len
> 5)
1082 fprintf (dump_file
, "chain length exceeds 5: %u\n", cur_chain_len
);
1085 for (unsigned i
= 0; i
< cur_chain_len
; i
++)
1087 edge e
= cur_cd_chain
[i
];
1088 /* Cycle detected. */
1089 if (e
->src
== dom_bb
)
1092 fprintf (dump_file
, "cycle detected\n");
1097 if (DEBUG_PREDICATE_ANALYZER
&& dump_file
)
1099 "%*s%s (dom_bb = %u, dep_bb = %u, cd_chains = { %s }, ...)\n",
1100 depth
, "", __func__
, dom_bb
->index
, dep_bb
->index
,
1101 format_edge_vecs (cd_chains
, *num_chains
).c_str ());
1103 bool found_cd_chain
= false;
1105 /* Iterate over DOM_BB's successors. */
1108 FOR_EACH_EDGE (e
, ei
, dom_bb
->succs
)
1110 int post_dom_check
= 0;
1111 if (e
->flags
& (EDGE_FAKE
| EDGE_ABNORMAL
))
1114 basic_block cd_bb
= e
->dest
;
1115 cur_cd_chain
.safe_push (e
);
1116 while (!is_non_loop_exit_postdominating (cd_bb
, dom_bb
))
1118 if (cd_bb
== dep_bb
)
1120 /* Found a direct control dependence. */
1121 if (*num_chains
< MAX_NUM_CHAINS
)
1123 cd_chains
[*num_chains
] = cur_cd_chain
.copy ();
1126 found_cd_chain
= true;
1127 /* Check path from next edge. */
1131 /* Check if DEP_BB is indirectly control-dependent on DOM_BB. */
1132 if (compute_control_dep_chain (cd_bb
, dep_bb
, cd_chains
,
1133 num_chains
, cur_cd_chain
,
1134 num_calls
, depth
+ 1))
1136 found_cd_chain
= true;
1140 cd_bb
= get_immediate_dominator (CDI_POST_DOMINATORS
, cd_bb
);
1142 if (cd_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
)
1143 || post_dom_check
> MAX_POSTDOM_CHECK
)
1146 cur_cd_chain
.pop ();
1147 gcc_assert (cur_cd_chain
.length () == cur_chain_len
);
1150 gcc_assert (cur_cd_chain
.length () == cur_chain_len
);
1151 return found_cd_chain
;
1154 /* Return true if PRED can be invalidated by any predicate in GUARD. */
1157 can_be_invalidated_p (const pred_info
&pred
, const pred_chain
&guard
)
1159 if (dump_file
&& dump_flags
& TDF_DETAILS
)
1161 fprintf (dump_file
, "Testing if predicate: ");
1162 dump_pred_info (pred
);
1163 fprintf (dump_file
, "\n...can be invalidated by a USE guard of: ");
1164 dump_pred_chain (guard
);
1165 fputc ('\n', dump_file
);
1168 unsigned n
= guard
.length ();
1169 for (unsigned i
= 0; i
< n
; ++i
)
1171 if (pred_neg_p (pred
, guard
[i
]))
1173 if (dump_file
&& dump_flags
& TDF_DETAILS
)
1175 fprintf (dump_file
, " Predicate invalidated by: ");
1176 dump_pred_info (guard
[i
]);
1177 fputc ('\n', dump_file
);
1186 /* Return true if all predicates in PREDS are invalidated by GUARD being
1190 can_be_invalidated_p (const pred_chain_union
&preds
, const pred_chain
&guard
)
1192 if (preds
.is_empty ())
1195 if (dump_file
&& dump_flags
& TDF_DETAILS
)
1196 dump_predicates (NULL
, preds
,
1197 "Testing if anything here can be invalidated: ");
1199 for (unsigned i
= 0; i
< preds
.length (); ++i
)
1201 const pred_chain
&chain
= preds
[i
];
1203 for (j
= 0; j
< chain
.length (); ++j
)
1204 if (can_be_invalidated_p (chain
[j
], guard
))
1207 /* If we were unable to invalidate any predicate in C, then there
1208 is a viable path from entry to the PHI where the PHI takes
1209 an interesting value and continues to a use of the PHI. */
1210 if (j
== chain
.length ())
1216 /* Return true if none of the PHI arguments in OPNDS is used given
1217 the use guards in *THIS that guard the PHI's use. */
1220 predicate::use_cannot_happen (gphi
*phi
, unsigned opnds
)
1222 if (!m_eval
.phi_arg_set (phi
))
1225 /* PHI_USE_GUARDS are OR'ed together. If we have more than one
1226 possible guard, there's no way of knowing which guard was true.
1227 Since we need to be absolutely sure that the uninitialized
1228 operands will be invalidated, bail. */
1229 const pred_chain_union
&phi_use_guards
= m_preds
;
1230 if (phi_use_guards
.length () != 1)
1233 const pred_chain
&use_guard
= phi_use_guards
[0];
1235 /* Look for the control dependencies of all the interesting operands
1236 and build guard predicates describing them. */
1237 unsigned n
= gimple_phi_num_args (phi
);
1238 for (unsigned i
= 0; i
< n
; ++i
)
1240 if (!MASK_TEST_BIT (opnds
, i
))
1243 edge e
= gimple_phi_arg_edge (phi
, i
);
1244 auto_vec
<edge
> dep_chains
[MAX_NUM_CHAINS
];
1245 auto_vec
<edge
, MAX_CHAIN_LEN
+ 1> cur_chain
;
1246 unsigned num_chains
= 0;
1247 unsigned num_calls
= 0;
1249 /* Build the control dependency chain for the PHI argument... */
1250 if (!compute_control_dep_chain (ENTRY_BLOCK_PTR_FOR_FN (cfun
),
1251 e
->src
, dep_chains
, &num_chains
,
1252 cur_chain
, &num_calls
))
1255 if (DEBUG_PREDICATE_ANALYZER
&& dump_file
)
1257 fprintf (dump_file
, "predicate::use_cannot_happen (...) "
1258 "dep_chains for arg %u:\n\t", i
);
1259 dump_dep_chains (dep_chains
, num_chains
);
1262 /* ...and convert it into a set of predicates guarding its
1264 predicate
def_preds (m_eval
);
1265 def_preds
.init_from_control_deps (dep_chains
, num_chains
);
1266 if (def_preds
.is_empty ())
1267 /* If there's no predicate there's no basis to rule the use out. */
1270 def_preds
.simplify ();
1271 def_preds
.normalize ();
1273 /* Can the guard for this PHI argument be negated by the one
1274 guarding the PHI use? */
1275 if (!can_be_invalidated_p (def_preds
.chain (), use_guard
))
1282 /* Implemented simplifications:
1284 1) ((x IOR y) != 0) AND (x != 0) is equivalent to (x != 0);
1285 2) (X AND Y) OR (!X AND Y) is equivalent to Y;
1286 3) X OR (!X AND Y) is equivalent to (X OR Y);
1287 4) ((x IAND y) != 0) || (x != 0 AND y != 0)) is equivalent to
1289 5) (X AND Y) OR (!X AND Z) OR (!Y AND Z) is equivalent to
1292 PREDS is the predicate chains, and N is the number of chains. */
1294 /* Implement rule 1 above. PREDS is the AND predicate to simplify
1298 simplify_1 (pred_chain
&chain
)
1300 bool simplified
= false;
1301 pred_chain s_chain
= vNULL
;
1303 unsigned n
= chain
.length ();
1304 for (unsigned i
= 0; i
< n
; i
++)
1306 pred_info
&a_pred
= chain
[i
];
1308 if (!a_pred
.pred_lhs
1309 || !is_neq_zero_form_p (a_pred
))
1312 gimple
*def_stmt
= SSA_NAME_DEF_STMT (a_pred
.pred_lhs
);
1313 if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
1316 if (gimple_assign_rhs_code (def_stmt
) != BIT_IOR_EXPR
)
1319 for (unsigned j
= 0; j
< n
; j
++)
1321 const pred_info
&b_pred
= chain
[j
];
1323 if (!b_pred
.pred_lhs
1324 || !is_neq_zero_form_p (b_pred
))
1327 if (pred_expr_equal_p (b_pred
, gimple_assign_rhs1 (def_stmt
))
1328 || pred_expr_equal_p (b_pred
, gimple_assign_rhs2 (def_stmt
)))
1330 /* Mark A_PRED for removal from PREDS. */
1331 a_pred
.pred_lhs
= NULL
;
1332 a_pred
.pred_rhs
= NULL
;
1342 /* Remove predicates marked above. */
1343 for (unsigned i
= 0; i
< n
; i
++)
1345 pred_info
&a_pred
= chain
[i
];
1346 if (!a_pred
.pred_lhs
)
1348 s_chain
.safe_push (a_pred
);
1355 /* Implements rule 2 for the OR predicate PREDS:
1357 2) (X AND Y) OR (!X AND Y) is equivalent to Y. */
1360 predicate::simplify_2 ()
1362 bool simplified
= false;
1364 /* (X AND Y) OR (!X AND Y) is equivalent to Y.
1365 (X AND Y) OR (X AND !Y) is equivalent to X. */
1367 unsigned n
= m_preds
.length ();
1368 for (unsigned i
= 0; i
< n
; i
++)
1370 pred_chain
&a_chain
= m_preds
[i
];
1371 if (a_chain
.length () != 2)
1374 /* Create copies since the chain may be released below before
1375 the copy is added to the other chain. */
1376 const pred_info x
= a_chain
[0];
1377 const pred_info y
= a_chain
[1];
1379 for (unsigned j
= 0; j
< n
; j
++)
1384 pred_chain
&b_chain
= m_preds
[j
];
1385 if (b_chain
.length () != 2)
1388 const pred_info
&x2
= b_chain
[0];
1389 const pred_info
&y2
= b_chain
[1];
1391 if (pred_equal_p (x
, x2
) && pred_neg_p (y
, y2
))
1396 b_chain
.safe_push (x
);
1400 if (pred_neg_p (x
, x2
) && pred_equal_p (y
, y2
))
1405 b_chain
.safe_push (y
);
1411 /* Now clean up the chain. */
1414 pred_chain_union s_preds
= vNULL
;
1415 for (unsigned i
= 0; i
< n
; i
++)
1417 if (m_preds
[i
].is_empty ())
1419 s_preds
.safe_push (m_preds
[i
]);
1429 /* Implement rule 3 for the OR predicate PREDS:
1431 3) x OR (!x AND y) is equivalent to x OR y. */
1434 predicate::simplify_3 ()
1436 /* Now iteratively simplify X OR (!X AND Z ..)
1437 into X OR (Z ...). */
1439 unsigned n
= m_preds
.length ();
1443 bool simplified
= false;
1444 for (unsigned i
= 0; i
< n
; i
++)
1446 const pred_chain
&a_chain
= m_preds
[i
];
1448 if (a_chain
.length () != 1)
1451 const pred_info
&x
= a_chain
[0];
1452 for (unsigned j
= 0; j
< n
; j
++)
1457 pred_chain b_chain
= m_preds
[j
];
1458 if (b_chain
.length () < 2)
1461 for (unsigned k
= 0; k
< b_chain
.length (); k
++)
1463 const pred_info
&x2
= b_chain
[k
];
1464 if (pred_neg_p (x
, x2
))
1466 b_chain
.unordered_remove (k
);
1476 /* Implement rule 4 for the OR predicate PREDS:
1478 2) ((x AND y) != 0) OR (x != 0 AND y != 0) is equivalent to
1479 (x != 0 ANd y != 0). */
1482 predicate::simplify_4 ()
1484 bool simplified
= false;
1485 pred_chain_union s_preds
= vNULL
;
1487 unsigned n
= m_preds
.length ();
1488 for (unsigned i
= 0; i
< n
; i
++)
1490 pred_chain a_chain
= m_preds
[i
];
1491 if (a_chain
.length () != 1)
1494 const pred_info
&z
= a_chain
[0];
1495 if (!is_neq_zero_form_p (z
))
1498 gimple
*def_stmt
= SSA_NAME_DEF_STMT (z
.pred_lhs
);
1499 if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
1502 if (gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
1505 for (unsigned j
= 0; j
< n
; j
++)
1510 pred_chain b_chain
= m_preds
[j
];
1511 if (b_chain
.length () != 2)
1514 const pred_info
&x2
= b_chain
[0];
1515 const pred_info
&y2
= b_chain
[1];
1516 if (!is_neq_zero_form_p (x2
) || !is_neq_zero_form_p (y2
))
1519 if ((pred_expr_equal_p (x2
, gimple_assign_rhs1 (def_stmt
))
1520 && pred_expr_equal_p (y2
, gimple_assign_rhs2 (def_stmt
)))
1521 || (pred_expr_equal_p (x2
, gimple_assign_rhs2 (def_stmt
))
1522 && pred_expr_equal_p (y2
, gimple_assign_rhs1 (def_stmt
))))
1531 /* Now clean up the chain. */
1534 for (unsigned i
= 0; i
< n
; i
++)
1536 if (m_preds
[i
].is_empty ())
1538 s_preds
.safe_push (m_preds
[i
]);
1549 /* Simplify predicates in *THIS. */
1552 predicate::simplify (gimple
*use_or_def
, bool is_use
)
1554 if (dump_file
&& dump_flags
& TDF_DETAILS
)
1556 fprintf (dump_file
, "Before simplication ");
1557 dump (use_or_def
, is_use
? "[USE]:\n" : "[DEF]:\n");
1560 unsigned n
= m_preds
.length ();
1561 for (unsigned i
= 0; i
< n
; i
++)
1562 ::simplify_1 (m_preds
[i
]);
1583 /* Attempt to normalize predicate chains by following UD chains by
1584 building up a big tree of either IOR operations or AND operations,
1585 and converting the IOR tree into a pred_chain_union or the BIT_AND
1586 tree into a pred_chain.
1596 then _t != 0 will be normalized into a pred_chain_union
1598 (_2 RELOP1 _1) OR (_5 RELOP2 _4) OR (_8 RELOP3 _7) OR (_0 != 0)
1608 then _t != 0 will be normalized into a pred_chain:
1609 (_2 RELOP1 _1) AND (_5 RELOP2 _4) AND (_8 RELOP3 _7) AND (_0 != 0)
1612 /* Store a PRED in *THIS. */
1615 predicate::push_pred (const pred_info
&pred
)
1617 pred_chain chain
= vNULL
;
1618 chain
.safe_push (pred
);
1619 m_preds
.safe_push (chain
);
1622 /* Dump predicates in *THIS for STMT prepended by MSG. */
1625 predicate::dump (gimple
*stmt
, const char *msg
) const
1627 fprintf (dump_file
, "%s", msg
);
1630 fputc ('\t', dump_file
);
1631 print_gimple_stmt (dump_file
, stmt
, 0);
1632 fprintf (dump_file
, " is conditional on:\n");
1635 unsigned np
= m_preds
.length ();
1638 fprintf (dump_file
, "\t(empty)\n");
1643 tree expr
= build_pred_expr (m_preds
);
1644 char *str
= print_generic_expr_to_str (expr
);
1645 fprintf (dump_file
, "\t%s (expanded)\n", str
);
1650 fprintf (dump_file
, "\tOR (");
1652 fputc ('\t', dump_file
);
1653 for (unsigned i
= 0; i
< np
; i
++)
1655 dump_pred_chain (m_preds
[i
]);
1657 fprintf (dump_file
, ", ");
1659 fputc (')', dump_file
);
1661 fputc ('\n', dump_file
);
1664 /* Initialize *THIS with the predicates of the control dependence chains
1665 between the basic block DEF_BB that defines a variable of interst and
1666 USE_BB that uses the variable, respectively. */
1668 predicate::predicate (basic_block def_bb
, basic_block use_bb
, func_t
&eval
)
1669 : m_preds (vNULL
), m_eval (eval
)
1671 /* Set CD_ROOT to the basic block closest to USE_BB that is the control
1672 equivalent of (is guarded by the same predicate as) DEF_BB that also
1673 dominates USE_BB. */
1674 basic_block cd_root
= def_bb
;
1675 while (dominated_by_p (CDI_DOMINATORS
, use_bb
, cd_root
))
1677 /* Find CD_ROOT's closest postdominator that's its control
1679 if (basic_block bb
= find_control_equiv_block (cd_root
))
1680 if (dominated_by_p (CDI_DOMINATORS
, use_bb
, bb
))
1689 /* Set DEP_CHAINS to the set of edges between CD_ROOT and USE_BB.
1690 Each DEP_CHAINS element is a series of edges whose conditions
1691 are logical conjunctions. Together, the DEP_CHAINS vector is
1692 used below to initialize an OR expression of the conjunctions. */
1693 unsigned num_calls
= 0;
1694 unsigned num_chains
= 0;
1695 auto_vec
<edge
> dep_chains
[MAX_NUM_CHAINS
];
1696 auto_vec
<edge
, MAX_CHAIN_LEN
+ 1> cur_chain
;
1698 compute_control_dep_chain (cd_root
, use_bb
, dep_chains
, &num_chains
,
1699 cur_chain
, &num_calls
);
1701 if (DEBUG_PREDICATE_ANALYZER
&& dump_file
)
1703 fprintf (dump_file
, "predicate::predicate (def_bb = %u, use_bb = %u, func_t) "
1704 "initialized from %u dep_chains:\n\t",
1705 def_bb
->index
, use_bb
->index
, num_chains
);
1706 dump_dep_chains (dep_chains
, num_chains
);
1709 /* From the set of edges computed above initialize *THIS as the OR
1710 condition under which the definition in DEF_BB is used in USE_BB.
1711 Each OR subexpression is represented by one element of DEP_CHAINS,
1712 where each element consists of a series of AND subexpressions. */
1713 init_from_control_deps (dep_chains
, num_chains
);
1716 /* Release resources in *THIS. */
1718 predicate::~predicate ()
1720 unsigned n
= m_preds
.length ();
1721 for (unsigned i
= 0; i
!= n
; ++i
)
1722 m_preds
[i
].release ();
1726 /* Copy-assign RHS to *THIS. */
1729 predicate::operator= (const predicate
&rhs
)
1734 /* FIXME: Make this a compile-time constraint? */
1735 gcc_assert (&m_eval
== &rhs
.m_eval
);
1737 unsigned n
= m_preds
.length ();
1738 for (unsigned i
= 0; i
!= n
; ++i
)
1739 m_preds
[i
].release ();
1742 n
= rhs
.m_preds
.length ();
1743 for (unsigned i
= 0; i
!= n
; ++i
)
1745 const pred_chain
&chain
= rhs
.m_preds
[i
];
1746 m_preds
.safe_push (chain
.copy ());
1752 /* For each use edge of PHI, compute all control dependence chains
1753 and convert those to the composite predicates in M_PREDS.
1754 Return true if a nonempty predicate has been obtained. */
1757 predicate::init_from_phi_def (gphi
*phi
)
1759 gcc_assert (is_empty ());
1761 basic_block phi_bb
= gimple_bb (phi
);
1762 /* Find the closest dominating bb to be the control dependence root. */
1763 basic_block cd_root
= get_immediate_dominator (CDI_DOMINATORS
, phi_bb
);
1767 /* Set DEF_EDGES to the edges to the PHI from the bb's that provide
1768 definitions of each of the PHI operands for which M_EVAL is false. */
1769 auto_vec
<edge
> def_edges
;
1770 hash_set
<gimple
*> visited_phis
;
1771 collect_phi_def_edges (phi
, cd_root
, &def_edges
, m_eval
, &visited_phis
);
1773 unsigned nedges
= def_edges
.length ();
1777 unsigned num_chains
= 0;
1778 auto_vec
<edge
> dep_chains
[MAX_NUM_CHAINS
];
1779 auto_vec
<edge
, MAX_CHAIN_LEN
+ 1> cur_chain
;
1780 for (unsigned i
= 0; i
< nedges
; i
++)
1782 edge e
= def_edges
[i
];
1783 unsigned num_calls
= 0;
1784 unsigned prev_nc
= num_chains
;
1785 compute_control_dep_chain (cd_root
, e
->src
, dep_chains
,
1786 &num_chains
, cur_chain
, &num_calls
);
1788 /* Update the newly added chains with the phi operand edge. */
1789 if (EDGE_COUNT (e
->src
->succs
) > 1)
1791 if (prev_nc
== num_chains
&& num_chains
< MAX_NUM_CHAINS
)
1792 dep_chains
[num_chains
++] = vNULL
;
1793 for (unsigned j
= prev_nc
; j
< num_chains
; j
++)
1794 dep_chains
[j
].safe_push (e
);
1798 /* Convert control dependence chains to the predicate in *THIS under
1799 which the PHI operands are defined to values for which M_EVAL is
1801 init_from_control_deps (dep_chains
, num_chains
);
1802 return !is_empty ();
1805 /* Compute the predicates that guard the use USE_STMT and check if
1806 the incoming paths that have an empty (or possibly empty) definition
1807 can be pruned. Return true if it can be determined that the use of
1808 PHI's def in USE_STMT is guarded by a predicate set that does not
1809 overlap with the predicate sets of all runtime paths that do not
1812 Return false if the use is not guarded or if it cannot be determined.
1813 USE_BB is the bb of the use (for phi operand use, the bb is not the bb
1814 of the phi stmt, but the source bb of the operand edge).
1816 OPNDS is a bitmap with a bit set for each PHI operand of interest.
1818 THIS->M_PREDS contains the (memoized) defining predicate chains of
1819 a PHI. If THIS->M_PREDS is empty, the PHI's defining predicate
1820 chains are computed and stored into THIS->M_PREDS as needed.
1822 VISITED_PHIS is a pointer set of phis being visited. */
1825 predicate::is_use_guarded (gimple
*use_stmt
, basic_block use_bb
,
1826 gphi
*phi
, unsigned opnds
,
1827 hash_set
<gphi
*> *visited
)
1829 if (visited
->add (phi
))
1832 /* The basic block where the PHI is defined. */
1833 basic_block def_bb
= gimple_bb (phi
);
1835 /* Try to build the predicate expression under which the PHI flows
1836 into its use. This will be empty if the PHI is defined and used
1838 predicate
use_preds (def_bb
, use_bb
, m_eval
);
1840 if (is_non_loop_exit_postdominating (use_bb
, def_bb
))
1844 /* Lazily initialize *THIS from the PHI and build its use
1846 init_from_phi_def (phi
);
1847 m_use_expr
= build_pred_expr (use_preds
.m_preds
);
1850 /* The use is not guarded. */
1854 if (use_preds
.is_empty ())
1857 /* Try to prune the dead incoming phi edges. */
1858 if (!use_preds
.overlap (phi
, opnds
, visited
))
1860 if (DEBUG_PREDICATE_ANALYZER
&& dump_file
)
1861 fputs ("found predicate overlap\n", dump_file
);
1866 /* We might be able to prove that if the control dependencies for OPNDS
1867 are true, the control dependencies for USE_STMT can never be true. */
1868 if (use_preds
.use_cannot_happen (phi
, opnds
))
1873 /* Lazily initialize *THIS from PHI. */
1874 if (!init_from_phi_def (phi
))
1876 m_use_expr
= build_pred_expr (use_preds
.m_preds
);
1884 use_preds
.simplify (use_stmt
, /*is_use=*/true);
1885 use_preds
.normalize (use_stmt
, /*is_use=*/true);
1887 /* Return true if the predicate guarding the valid definition (i.e.,
1888 *THIS) is a superset of the predicate guarding the use (i.e.,
1890 if (superset_of (use_preds
))
1893 m_use_expr
= build_pred_expr (use_preds
.m_preds
);
1898 /* Public interface to the above. */
1901 predicate::is_use_guarded (gimple
*stmt
, basic_block use_bb
, gphi
*phi
,
1904 hash_set
<gphi
*> visited
;
1905 return is_use_guarded (stmt
, use_bb
, phi
, opnds
, &visited
);
1908 /* Normalize predicate PRED:
1909 1) if PRED can no longer be normalized, append it to *THIS.
1910 2) otherwise if PRED is of the form x != 0, follow x's definition
1911 and put normalized predicates into WORK_LIST. */
1914 predicate::normalize (pred_chain
*norm_chain
,
1916 tree_code and_or_code
,
1917 pred_chain
*work_list
,
1918 hash_set
<tree
> *mark_set
)
1920 if (!is_neq_zero_form_p (pred
))
1922 if (and_or_code
== BIT_IOR_EXPR
)
1925 norm_chain
->safe_push (pred
);
1929 gimple
*def_stmt
= SSA_NAME_DEF_STMT (pred
.pred_lhs
);
1931 if (gimple_code (def_stmt
) == GIMPLE_PHI
1932 && is_degenerate_phi (def_stmt
, &pred
))
1933 /* PRED has been modified above. */
1934 work_list
->safe_push (pred
);
1935 else if (gimple_code (def_stmt
) == GIMPLE_PHI
&& and_or_code
== BIT_IOR_EXPR
)
1937 unsigned n
= gimple_phi_num_args (def_stmt
);
1939 /* Punt for a nonzero constant. The predicate should be one guarding
1941 for (unsigned i
= 0; i
< n
; ++i
)
1943 tree op
= gimple_phi_arg_def (def_stmt
, i
);
1944 if (TREE_CODE (op
) == INTEGER_CST
&& !integer_zerop (op
))
1951 for (unsigned i
= 0; i
< n
; ++i
)
1953 tree op
= gimple_phi_arg_def (def_stmt
, i
);
1954 if (integer_zerop (op
))
1957 push_to_worklist (op
, work_list
, mark_set
);
1960 else if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
1962 if (and_or_code
== BIT_IOR_EXPR
)
1965 norm_chain
->safe_push (pred
);
1967 else if (gimple_assign_rhs_code (def_stmt
) == and_or_code
)
1969 /* Avoid splitting up bit manipulations like x & 3 or y | 1. */
1970 if (is_gimple_min_invariant (gimple_assign_rhs2 (def_stmt
)))
1972 /* But treat x & 3 as a condition. */
1973 if (and_or_code
== BIT_AND_EXPR
)
1976 n_pred
.pred_lhs
= gimple_assign_rhs1 (def_stmt
);
1977 n_pred
.pred_rhs
= gimple_assign_rhs2 (def_stmt
);
1978 n_pred
.cond_code
= and_or_code
;
1979 n_pred
.invert
= false;
1980 norm_chain
->safe_push (n_pred
);
1985 push_to_worklist (gimple_assign_rhs1 (def_stmt
), work_list
, mark_set
);
1986 push_to_worklist (gimple_assign_rhs2 (def_stmt
), work_list
, mark_set
);
1989 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
))
1992 pred_info n_pred
= get_pred_info_from_cmp (def_stmt
);
1993 if (and_or_code
== BIT_IOR_EXPR
)
1996 norm_chain
->safe_push (n_pred
);
2000 if (and_or_code
== BIT_IOR_EXPR
)
2003 norm_chain
->safe_push (pred
);
2007 /* Normalize PRED and store the normalized predicates in THIS->M_PREDS. */
2010 predicate::normalize (const pred_info
&pred
)
2012 if (!is_neq_zero_form_p (pred
))
2018 tree_code and_or_code
= ERROR_MARK
;
2020 gimple
*def_stmt
= SSA_NAME_DEF_STMT (pred
.pred_lhs
);
2021 if (gimple_code (def_stmt
) == GIMPLE_ASSIGN
)
2022 and_or_code
= gimple_assign_rhs_code (def_stmt
);
2023 if (and_or_code
!= BIT_IOR_EXPR
&& and_or_code
!= BIT_AND_EXPR
)
2025 if (TREE_CODE_CLASS (and_or_code
) == tcc_comparison
)
2027 pred_info n_pred
= get_pred_info_from_cmp (def_stmt
);
2036 pred_chain norm_chain
= vNULL
;
2037 pred_chain work_list
= vNULL
;
2038 work_list
.safe_push (pred
);
2039 hash_set
<tree
> mark_set
;
2041 while (!work_list
.is_empty ())
2043 pred_info a_pred
= work_list
.pop ();
2044 normalize (&norm_chain
, a_pred
, and_or_code
, &work_list
, &mark_set
);
2047 if (and_or_code
== BIT_AND_EXPR
)
2048 m_preds
.safe_push (norm_chain
);
2050 work_list
.release ();
2053 /* Normalize a single predicate PRED_CHAIN and append it to *THIS. */
2056 predicate::normalize (const pred_chain
&chain
)
2058 pred_chain work_list
= vNULL
;
2059 hash_set
<tree
> mark_set
;
2060 for (unsigned i
= 0; i
< chain
.length (); i
++)
2062 work_list
.safe_push (chain
[i
]);
2063 mark_set
.add (chain
[i
].pred_lhs
);
2066 /* Normalized chain of predicates built up below. */
2067 pred_chain norm_chain
= vNULL
;
2068 while (!work_list
.is_empty ())
2070 pred_info pi
= work_list
.pop ();
2071 predicate
pred (m_eval
);
2072 /* The predicate object is not modified here, only NORM_CHAIN and
2073 WORK_LIST are appended to. */
2074 pred
.normalize (&norm_chain
, pi
, BIT_AND_EXPR
, &work_list
, &mark_set
);
2077 m_preds
.safe_push (norm_chain
);
2078 work_list
.release ();
2081 /* Normalize predicate chains in THIS. */
2084 predicate::normalize (gimple
*use_or_def
, bool is_use
)
2086 if (dump_file
&& dump_flags
& TDF_DETAILS
)
2088 fprintf (dump_file
, "Before normalization ");
2089 dump (use_or_def
, is_use
? "[USE]:\n" : "[DEF]:\n");
2092 predicate
norm_preds (m_eval
);
2093 for (unsigned i
= 0; i
< m_preds
.length (); i
++)
2095 if (m_preds
[i
].length () != 1)
2096 norm_preds
.normalize (m_preds
[i
]);
2098 norm_preds
.normalize (m_preds
[i
][0]);
2105 fprintf (dump_file
, "After normalization ");
2106 dump (use_or_def
, is_use
? "[USE]:\n" : "[DEF]:\n");
2110 /* Convert the chains of control dependence edges into a set of predicates.
2111 A control dependence chain is represented by a vector edges. DEP_CHAINS
2112 points to an array of NUM_CHAINS dependence chains. One edge in
2113 a dependence chain is mapped to predicate expression represented by
2114 pred_info type. One dependence chain is converted to a composite
2115 predicate that is the result of AND operation of pred_info mapped to
2116 each edge. A composite predicate is represented by a vector of
2117 pred_info. Sets M_PREDS to the resulting composite predicates. */
2120 predicate::init_from_control_deps (const vec
<edge
> *dep_chains
,
2121 unsigned num_chains
)
2123 gcc_assert (is_empty ());
2125 bool has_valid_pred
= false;
2126 if (num_chains
== 0)
2129 if (num_chains
>= MAX_NUM_CHAINS
)
2132 fprintf (dump_file
, "MAX_NUM_CHAINS exceeded: %u\n", num_chains
);
2136 /* Convert the control dependency chain into a set of predicates. */
2137 m_preds
.reserve (num_chains
);
2139 for (unsigned i
= 0; i
< num_chains
; i
++)
2141 /* One path through the CFG represents a logical conjunction
2142 of the predicates. */
2143 const vec
<edge
> &path
= dep_chains
[i
];
2145 has_valid_pred
= false;
2146 /* The chain of predicates guarding the definition along this path. */
2147 pred_chain t_chain
{ };
2148 for (unsigned j
= 0; j
< path
.length (); j
++)
2151 basic_block guard_bb
= e
->src
;
2152 /* Ignore empty forwarder blocks. */
2153 if (empty_block_p (guard_bb
) && single_succ_p (guard_bb
))
2156 /* An empty basic block here is likely a PHI, and is not one
2157 of the cases we handle below. */
2158 gimple_stmt_iterator gsi
= gsi_last_bb (guard_bb
);
2159 if (gsi_end_p (gsi
))
2161 has_valid_pred
= false;
2164 /* Get the conditional controlling the bb exit edge. */
2165 gimple
*cond_stmt
= gsi_stmt (gsi
);
2166 if (is_gimple_call (cond_stmt
) && EDGE_COUNT (e
->src
->succs
) >= 2)
2167 /* Ignore EH edge. Can add assertion on the other edge's flag. */
2169 /* Skip if there is essentially one succesor. */
2170 if (EDGE_COUNT (e
->src
->succs
) == 2)
2176 FOR_EACH_EDGE (e1
, ei1
, e
->src
->succs
)
2178 if (EDGE_COUNT (e1
->dest
->succs
) == 0)
2187 if (gimple_code (cond_stmt
) == GIMPLE_COND
)
2189 /* The true edge corresponds to the uninteresting condition.
2190 Add the negated predicate(s) for the edge to record
2191 the interesting condition. */
2193 one_pred
.pred_lhs
= gimple_cond_lhs (cond_stmt
);
2194 one_pred
.pred_rhs
= gimple_cond_rhs (cond_stmt
);
2195 one_pred
.cond_code
= gimple_cond_code (cond_stmt
);
2196 one_pred
.invert
= !!(e
->flags
& EDGE_FALSE_VALUE
);
2198 t_chain
.safe_push (one_pred
);
2200 if (DEBUG_PREDICATE_ANALYZER
&& dump_file
)
2202 fprintf (dump_file
, "one_pred = ");
2203 dump_pred_info (one_pred
);
2204 fputc ('\n', dump_file
);
2207 has_valid_pred
= true;
2209 else if (gswitch
*gs
= dyn_cast
<gswitch
*> (cond_stmt
))
2211 /* Avoid quadratic behavior. */
2212 if (gimple_switch_num_labels (gs
) > MAX_SWITCH_CASES
)
2214 has_valid_pred
= false;
2217 /* Find the case label. */
2220 for (idx
= 0; idx
< gimple_switch_num_labels (gs
); ++idx
)
2222 tree tl
= gimple_switch_label (gs
, idx
);
2223 if (e
->dest
== label_to_block (cfun
, CASE_LABEL (tl
)))
2234 /* If more than one label reaches this block or the case
2235 label doesn't have a single value (like the default one)
2240 && !operand_equal_p (CASE_LOW (l
), CASE_HIGH (l
), 0)))
2242 has_valid_pred
= false;
2247 one_pred
.pred_lhs
= gimple_switch_index (gs
);
2248 one_pred
.pred_rhs
= CASE_LOW (l
);
2249 one_pred
.cond_code
= EQ_EXPR
;
2250 one_pred
.invert
= false;
2251 t_chain
.safe_push (one_pred
);
2252 has_valid_pred
= true;
2256 /* Disabled. See PR 90994.
2257 has_valid_pred = false; */
2262 if (!has_valid_pred
)
2265 m_preds
.safe_push (t_chain
);
2268 if (DEBUG_PREDICATE_ANALYZER
&& dump_file
)
2270 fprintf (dump_file
, "init_from_control_deps {%s}:\n",
2271 format_edge_vecs (dep_chains
, num_chains
).c_str ());
2276 gcc_assert (m_preds
.length () != 0);
2278 /* Clear M_PREDS to indicate failure. */
2282 /* Return the predicate expression guarding the definition of
2283 the interesting variable. When INVERT is set, return the logical
2284 NOT of the predicate. */
2287 predicate::def_expr (bool invert
/* = false */) const
2289 /* The predicate is stored in an inverted form. */
2290 return build_pred_expr (m_preds
, !invert
);
2293 /* Return the predicate expression guarding the use of the interesting
2294 variable or null if the use predicate hasn't been determined yet. */
2297 predicate::use_expr () const
2302 /* Return a logical AND expression with the (optionally inverted) predicate
2303 expression guarding the definition of the interesting variable and one
2304 guarding its use. Return null if the use predicate hasn't yet been
2308 predicate::expr (bool invert
/* = false */) const
2313 tree expr
= build_pred_expr (m_preds
, !invert
);
2314 return build2 (TRUTH_AND_EXPR
, boolean_type_node
, expr
, m_use_expr
);