1 /* Predicate aware uninitialized variable warning.
2 Copyright (C) 2001-2015 Free Software Foundation, Inc.
3 Contributed by Xinliang David Li <davidxl@google.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "hard-reg-set.h"
30 #include "fold-const.h"
33 #include "gimple-pretty-print.h"
34 #include "internal-fn.h"
35 #include "gimple-iterator.h"
37 #include "tree-inline.h"
38 #include "tree-pass.h"
39 #include "diagnostic-core.h"
43 /* This implements the pass that does predicate aware warning on uses of
44 possibly uninitialized variables. The pass first collects the set of
45 possibly uninitialized SSA names. For each such name, it walks through
46 all its immediate uses. For each immediate use, it rebuilds the condition
47 expression (the predicate) that guards the use. The predicate is then
48 examined to see if the variable is always defined under that same condition.
49 This is done either by pruning the unrealizable paths that lead to the
50 default definitions or by checking if the predicate set that guards the
51 defining paths is a superset of the use predicate. */
54 /* Pointer set of potentially undefined ssa names, i.e.,
55 ssa names that are defined by phi with operands that
56 are not defined or potentially undefined. */
57 static hash_set
<tree
> *possibly_undefined_names
= 0;
59 /* Bit mask handling macros. */
60 #define MASK_SET_BIT(mask, pos) mask |= (1 << pos)
61 #define MASK_TEST_BIT(mask, pos) (mask & (1 << pos))
62 #define MASK_EMPTY(mask) (mask == 0)
64 /* Returns the first bit position (starting from LSB)
65 in mask that is non zero. Returns -1 if the mask is empty. */
67 get_mask_first_set_bit (unsigned mask
)
73 while ((mask
& (1 << pos
)) == 0)
78 #define MASK_FIRST_SET_BIT(mask) get_mask_first_set_bit (mask)
80 /* Return true if T, an SSA_NAME, has an undefined value. */
82 has_undefined_value_p (tree t
)
84 return (ssa_undefined_value_p (t
)
85 || (possibly_undefined_names
86 && possibly_undefined_names
->contains (t
)));
91 /* Like has_undefined_value_p, but don't return true if TREE_NO_WARNING
92 is set on SSA_NAME_VAR. */
95 uninit_undefined_value_p (tree t
) {
96 if (!has_undefined_value_p (t
))
98 if (SSA_NAME_VAR (t
) && TREE_NO_WARNING (SSA_NAME_VAR (t
)))
103 /* Emit warnings for uninitialized variables. This is done in two passes.
105 The first pass notices real uses of SSA names with undefined values.
106 Such uses are unconditionally uninitialized, and we can be certain that
107 such a use is a mistake. This pass is run before most optimizations,
108 so that we catch as many as we can.
110 The second pass follows PHI nodes to find uses that are potentially
111 uninitialized. In this case we can't necessarily prove that the use
112 is really uninitialized. This pass is run after most optimizations,
113 so that we thread as many jumps and possible, and delete as much dead
114 code as possible, in order to reduce false positives. We also look
115 again for plain uninitialized variables, since optimization may have
116 changed conditionally uninitialized to unconditionally uninitialized. */
118 /* Emit a warning for EXPR based on variable VAR at the point in the
119 program T, an SSA_NAME, is used being uninitialized. The exact
120 warning text is in MSGID and DATA is the gimple stmt with info about
121 the location in source code. When DATA is a GIMPLE_PHI, PHIARG_IDX
122 gives which argument of the phi node to take the location from. WC
123 is the warning code. */
126 warn_uninit (enum opt_code wc
, tree t
, tree expr
, tree var
,
127 const char *gmsgid
, void *data
, location_t phiarg_loc
)
129 gimple context
= (gimple
) data
;
130 location_t location
, cfun_loc
;
131 expanded_location xloc
, floc
;
133 /* Ignore COMPLEX_EXPR as initializing only a part of a complex
134 turns in a COMPLEX_EXPR with the not initialized part being
135 set to its previous (undefined) value. */
136 if (is_gimple_assign (context
)
137 && gimple_assign_rhs_code (context
) == COMPLEX_EXPR
)
139 if (!has_undefined_value_p (t
))
142 /* TREE_NO_WARNING either means we already warned, or the front end
143 wishes to suppress the warning. */
145 && (gimple_no_warning_p (context
)
146 || (gimple_assign_single_p (context
)
147 && TREE_NO_WARNING (gimple_assign_rhs1 (context
)))))
148 || TREE_NO_WARNING (expr
))
151 if (context
!= NULL
&& gimple_has_location (context
))
152 location
= gimple_location (context
);
153 else if (phiarg_loc
!= UNKNOWN_LOCATION
)
154 location
= phiarg_loc
;
156 location
= DECL_SOURCE_LOCATION (var
);
157 location
= linemap_resolve_location (line_table
, location
,
158 LRK_SPELLING_LOCATION
,
160 cfun_loc
= DECL_SOURCE_LOCATION (cfun
->decl
);
161 xloc
= expand_location (location
);
162 floc
= expand_location (cfun_loc
);
163 if (warning_at (location
, wc
, gmsgid
, expr
))
165 TREE_NO_WARNING (expr
) = 1;
167 if (location
== DECL_SOURCE_LOCATION (var
))
169 if (xloc
.file
!= floc
.file
170 || linemap_location_before_p (line_table
,
172 || linemap_location_before_p (line_table
,
173 cfun
->function_end_locus
,
175 inform (DECL_SOURCE_LOCATION (var
), "%qD was declared here", var
);
180 warn_uninitialized_vars (bool warn_possibly_uninitialized
)
182 gimple_stmt_iterator gsi
;
185 FOR_EACH_BB_FN (bb
, cfun
)
187 bool always_executed
= dominated_by_p (CDI_POST_DOMINATORS
,
188 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)), bb
);
189 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
191 gimple stmt
= gsi_stmt (gsi
);
196 if (is_gimple_debug (stmt
))
199 /* We only do data flow with SSA_NAMEs, so that's all we
201 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, op_iter
, SSA_OP_USE
)
203 use
= USE_FROM_PTR (use_p
);
205 warn_uninit (OPT_Wuninitialized
, use
,
206 SSA_NAME_VAR (use
), SSA_NAME_VAR (use
),
207 "%qD is used uninitialized in this function",
208 stmt
, UNKNOWN_LOCATION
);
209 else if (warn_possibly_uninitialized
)
210 warn_uninit (OPT_Wmaybe_uninitialized
, use
,
211 SSA_NAME_VAR (use
), SSA_NAME_VAR (use
),
212 "%qD may be used uninitialized in this function",
213 stmt
, UNKNOWN_LOCATION
);
216 /* For memory the only cheap thing we can do is see if we
217 have a use of the default def of the virtual operand.
218 ??? Not so cheap would be to use the alias oracle via
219 walk_aliased_vdefs, if we don't find any aliasing vdef
220 warn as is-used-uninitialized, if we don't find an aliasing
221 vdef that kills our use (stmt_kills_ref_p), warn as
222 may-be-used-uninitialized. But this walk is quadratic and
223 so must be limited which means we would miss warning
225 use
= gimple_vuse (stmt
);
227 && gimple_assign_single_p (stmt
)
228 && !gimple_vdef (stmt
)
229 && SSA_NAME_IS_DEFAULT_DEF (use
))
231 tree rhs
= gimple_assign_rhs1 (stmt
);
232 tree base
= get_base_address (rhs
);
234 /* Do not warn if it can be initialized outside this function. */
235 if (TREE_CODE (base
) != VAR_DECL
236 || DECL_HARD_REGISTER (base
)
237 || is_global_var (base
))
241 warn_uninit (OPT_Wuninitialized
, use
,
242 gimple_assign_rhs1 (stmt
), base
,
243 "%qE is used uninitialized in this function",
244 stmt
, UNKNOWN_LOCATION
);
245 else if (warn_possibly_uninitialized
)
246 warn_uninit (OPT_Wmaybe_uninitialized
, use
,
247 gimple_assign_rhs1 (stmt
), base
,
248 "%qE may be used uninitialized in this function",
249 stmt
, UNKNOWN_LOCATION
);
257 /* Checks if the operand OPND of PHI is defined by
258 another phi with one operand defined by this PHI,
259 but the rest operands are all defined. If yes,
260 returns true to skip this operand as being
261 redundant. Can be enhanced to be more general. */
264 can_skip_redundant_opnd (tree opnd
, gimple phi
)
270 phi_def
= gimple_phi_result (phi
);
271 op_def
= SSA_NAME_DEF_STMT (opnd
);
272 if (gimple_code (op_def
) != GIMPLE_PHI
)
274 n
= gimple_phi_num_args (op_def
);
275 for (i
= 0; i
< n
; ++i
)
277 tree op
= gimple_phi_arg_def (op_def
, i
);
278 if (TREE_CODE (op
) != SSA_NAME
)
280 if (op
!= phi_def
&& uninit_undefined_value_p (op
))
287 /* Returns a bit mask holding the positions of arguments in PHI
288 that have empty (or possibly empty) definitions. */
291 compute_uninit_opnds_pos (gphi
*phi
)
294 unsigned uninit_opnds
= 0;
296 n
= gimple_phi_num_args (phi
);
297 /* Bail out for phi with too many args. */
301 for (i
= 0; i
< n
; ++i
)
303 tree op
= gimple_phi_arg_def (phi
, i
);
304 if (TREE_CODE (op
) == SSA_NAME
305 && uninit_undefined_value_p (op
)
306 && !can_skip_redundant_opnd (op
, phi
))
308 if (cfun
->has_nonlocal_label
|| cfun
->calls_setjmp
)
310 /* Ignore SSA_NAMEs that appear on abnormal edges
312 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
315 MASK_SET_BIT (uninit_opnds
, i
);
321 /* Find the immediate postdominator PDOM of the specified
322 basic block BLOCK. */
324 static inline basic_block
325 find_pdom (basic_block block
)
327 if (block
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
328 return EXIT_BLOCK_PTR_FOR_FN (cfun
);
332 = get_immediate_dominator (CDI_POST_DOMINATORS
, block
);
334 return EXIT_BLOCK_PTR_FOR_FN (cfun
);
339 /* Find the immediate DOM of the specified
340 basic block BLOCK. */
342 static inline basic_block
343 find_dom (basic_block block
)
345 if (block
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
346 return ENTRY_BLOCK_PTR_FOR_FN (cfun
);
349 basic_block bb
= get_immediate_dominator (CDI_DOMINATORS
, block
);
351 return ENTRY_BLOCK_PTR_FOR_FN (cfun
);
356 /* Returns true if BB1 is postdominating BB2 and BB1 is
357 not a loop exit bb. The loop exit bb check is simple and does
358 not cover all cases. */
361 is_non_loop_exit_postdominating (basic_block bb1
, basic_block bb2
)
363 if (!dominated_by_p (CDI_POST_DOMINATORS
, bb2
, bb1
))
366 if (single_pred_p (bb1
) && !single_succ_p (bb2
))
372 /* Find the closest postdominator of a specified BB, which is control
375 static inline basic_block
376 find_control_equiv_block (basic_block bb
)
380 pdom
= find_pdom (bb
);
382 /* Skip the postdominating bb that is also loop exit. */
383 if (!is_non_loop_exit_postdominating (pdom
, bb
))
386 if (dominated_by_p (CDI_DOMINATORS
, pdom
, bb
))
392 #define MAX_NUM_CHAINS 8
393 #define MAX_CHAIN_LEN 5
394 #define MAX_POSTDOM_CHECK 8
395 #define MAX_SWITCH_CASES 40
397 /* Computes the control dependence chains (paths of edges)
398 for DEP_BB up to the dominating basic block BB (the head node of a
399 chain should be dominated by it). CD_CHAINS is pointer to an
400 array holding the result chains. CUR_CD_CHAIN is the current
401 chain being computed. *NUM_CHAINS is total number of chains. The
402 function returns true if the information is successfully computed,
403 return false if there is no control dependence or not computed. */
406 compute_control_dep_chain (basic_block bb
, basic_block dep_bb
,
407 vec
<edge
> *cd_chains
,
409 vec
<edge
> *cur_cd_chain
,
415 bool found_cd_chain
= false;
416 size_t cur_chain_len
= 0;
418 if (EDGE_COUNT (bb
->succs
) < 2)
421 if (*num_calls
> PARAM_VALUE (PARAM_UNINIT_CONTROL_DEP_ATTEMPTS
))
425 /* Could use a set instead. */
426 cur_chain_len
= cur_cd_chain
->length ();
427 if (cur_chain_len
> MAX_CHAIN_LEN
)
430 for (i
= 0; i
< cur_chain_len
; i
++)
432 edge e
= (*cur_cd_chain
)[i
];
433 /* Cycle detected. */
438 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
441 int post_dom_check
= 0;
442 if (e
->flags
& (EDGE_FAKE
| EDGE_ABNORMAL
))
446 cur_cd_chain
->safe_push (e
);
447 while (!is_non_loop_exit_postdominating (cd_bb
, bb
))
451 /* Found a direct control dependence. */
452 if (*num_chains
< MAX_NUM_CHAINS
)
454 cd_chains
[*num_chains
] = cur_cd_chain
->copy ();
457 found_cd_chain
= true;
458 /* Check path from next edge. */
462 /* Now check if DEP_BB is indirectly control dependent on BB. */
463 if (compute_control_dep_chain (cd_bb
, dep_bb
, cd_chains
,
464 num_chains
, cur_cd_chain
, num_calls
))
466 found_cd_chain
= true;
470 cd_bb
= find_pdom (cd_bb
);
472 if (cd_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
) || post_dom_check
>
476 cur_cd_chain
->pop ();
477 gcc_assert (cur_cd_chain
->length () == cur_chain_len
);
479 gcc_assert (cur_cd_chain
->length () == cur_chain_len
);
481 return found_cd_chain
;
484 /* The type to represent a simple predicate */
486 typedef struct use_def_pred_info
490 enum tree_code cond_code
;
494 /* The type to represent a sequence of predicates grouped
495 with .AND. operation. */
497 typedef vec
<pred_info
, va_heap
, vl_ptr
> pred_chain
;
499 /* The type to represent a sequence of pred_chains grouped
500 with .OR. operation. */
502 typedef vec
<pred_chain
, va_heap
, vl_ptr
> pred_chain_union
;
504 /* Converts the chains of control dependence edges into a set of
505 predicates. A control dependence chain is represented by a vector
506 edges. DEP_CHAINS points to an array of dependence chains.
507 NUM_CHAINS is the size of the chain array. One edge in a dependence
508 chain is mapped to predicate expression represented by pred_info
509 type. One dependence chain is converted to a composite predicate that
510 is the result of AND operation of pred_info mapped to each edge.
511 A composite predicate is presented by a vector of pred_info. On
512 return, *PREDS points to the resulting array of composite predicates.
513 *NUM_PREDS is the number of composite predictes. */
516 convert_control_dep_chain_into_preds (vec
<edge
> *dep_chains
,
518 pred_chain_union
*preds
)
520 bool has_valid_pred
= false;
522 if (num_chains
== 0 || num_chains
>= MAX_NUM_CHAINS
)
525 /* Now convert the control dep chain into a set
527 preds
->reserve (num_chains
);
529 for (i
= 0; i
< num_chains
; i
++)
531 vec
<edge
> one_cd_chain
= dep_chains
[i
];
533 has_valid_pred
= false;
534 pred_chain t_chain
= vNULL
;
535 for (j
= 0; j
< one_cd_chain
.length (); j
++)
538 gimple_stmt_iterator gsi
;
539 basic_block guard_bb
;
545 gsi
= gsi_last_bb (guard_bb
);
548 has_valid_pred
= false;
551 cond_stmt
= gsi_stmt (gsi
);
552 if (is_gimple_call (cond_stmt
)
553 && EDGE_COUNT (e
->src
->succs
) >= 2)
555 /* Ignore EH edge. Can add assertion
556 on the other edge's flag. */
559 /* Skip if there is essentially one succesor. */
560 if (EDGE_COUNT (e
->src
->succs
) == 2)
566 FOR_EACH_EDGE (e1
, ei1
, e
->src
->succs
)
568 if (EDGE_COUNT (e1
->dest
->succs
) == 0)
577 if (gimple_code (cond_stmt
) == GIMPLE_COND
)
579 one_pred
.pred_lhs
= gimple_cond_lhs (cond_stmt
);
580 one_pred
.pred_rhs
= gimple_cond_rhs (cond_stmt
);
581 one_pred
.cond_code
= gimple_cond_code (cond_stmt
);
582 one_pred
.invert
= !!(e
->flags
& EDGE_FALSE_VALUE
);
583 t_chain
.safe_push (one_pred
);
584 has_valid_pred
= true;
586 else if (gswitch
*gs
= dyn_cast
<gswitch
*> (cond_stmt
))
588 /* Avoid quadratic behavior. */
589 if (gimple_switch_num_labels (gs
) > MAX_SWITCH_CASES
)
591 has_valid_pred
= false;
594 /* Find the case label. */
597 for (idx
= 0; idx
< gimple_switch_num_labels (gs
); ++idx
)
599 tree tl
= gimple_switch_label (gs
, idx
);
600 if (e
->dest
== label_to_block (CASE_LABEL (tl
)))
611 /* If more than one label reaches this block or the case
612 label doesn't have a single value (like the default one)
616 || (CASE_HIGH (l
) && !operand_equal_p (CASE_LOW (l
),
619 has_valid_pred
= false;
622 one_pred
.pred_lhs
= gimple_switch_index (gs
);
623 one_pred
.pred_rhs
= CASE_LOW (l
);
624 one_pred
.cond_code
= EQ_EXPR
;
625 one_pred
.invert
= false;
626 t_chain
.safe_push (one_pred
);
627 has_valid_pred
= true;
631 has_valid_pred
= false;
639 preds
->safe_push (t_chain
);
641 return has_valid_pred
;
644 /* Computes all control dependence chains for USE_BB. The control
645 dependence chains are then converted to an array of composite
646 predicates pointed to by PREDS. PHI_BB is the basic block of
647 the phi whose result is used in USE_BB. */
650 find_predicates (pred_chain_union
*preds
,
654 size_t num_chains
= 0, i
;
656 vec
<edge
> dep_chains
[MAX_NUM_CHAINS
];
657 auto_vec
<edge
, MAX_CHAIN_LEN
+ 1> cur_chain
;
658 bool has_valid_pred
= false;
659 basic_block cd_root
= 0;
661 /* First find the closest bb that is control equivalent to PHI_BB
662 that also dominates USE_BB. */
664 while (dominated_by_p (CDI_DOMINATORS
, use_bb
, cd_root
))
666 basic_block ctrl_eq_bb
= find_control_equiv_block (cd_root
);
667 if (ctrl_eq_bb
&& dominated_by_p (CDI_DOMINATORS
, use_bb
, ctrl_eq_bb
))
668 cd_root
= ctrl_eq_bb
;
673 compute_control_dep_chain (cd_root
, use_bb
, dep_chains
, &num_chains
,
674 &cur_chain
, &num_calls
);
677 = convert_control_dep_chain_into_preds (dep_chains
, num_chains
, preds
);
678 for (i
= 0; i
< num_chains
; i
++)
679 dep_chains
[i
].release ();
680 return has_valid_pred
;
683 /* Computes the set of incoming edges of PHI that have non empty
684 definitions of a phi chain. The collection will be done
685 recursively on operands that are defined by phis. CD_ROOT
686 is the control dependence root. *EDGES holds the result, and
687 VISITED_PHIS is a pointer set for detecting cycles. */
690 collect_phi_def_edges (gphi
*phi
, basic_block cd_root
,
692 hash_set
<gimple
> *visited_phis
)
698 if (visited_phis
->add (phi
))
701 n
= gimple_phi_num_args (phi
);
702 for (i
= 0; i
< n
; i
++)
704 opnd_edge
= gimple_phi_arg_edge (phi
, i
);
705 opnd
= gimple_phi_arg_def (phi
, i
);
707 if (TREE_CODE (opnd
) != SSA_NAME
)
709 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
711 fprintf (dump_file
, "\n[CHECK] Found def edge %d in ", (int)i
);
712 print_gimple_stmt (dump_file
, phi
, 0, 0);
714 edges
->safe_push (opnd_edge
);
718 gimple def
= SSA_NAME_DEF_STMT (opnd
);
720 if (gimple_code (def
) == GIMPLE_PHI
721 && dominated_by_p (CDI_DOMINATORS
,
722 gimple_bb (def
), cd_root
))
723 collect_phi_def_edges (as_a
<gphi
*> (def
), cd_root
, edges
,
725 else if (!uninit_undefined_value_p (opnd
))
727 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
729 fprintf (dump_file
, "\n[CHECK] Found def edge %d in ", (int)i
);
730 print_gimple_stmt (dump_file
, phi
, 0, 0);
732 edges
->safe_push (opnd_edge
);
738 /* For each use edge of PHI, computes all control dependence chains.
739 The control dependence chains are then converted to an array of
740 composite predicates pointed to by PREDS. */
743 find_def_preds (pred_chain_union
*preds
, gphi
*phi
)
745 size_t num_chains
= 0, i
, n
;
746 vec
<edge
> dep_chains
[MAX_NUM_CHAINS
];
747 auto_vec
<edge
, MAX_CHAIN_LEN
+ 1> cur_chain
;
748 vec
<edge
> def_edges
= vNULL
;
749 bool has_valid_pred
= false;
750 basic_block phi_bb
, cd_root
= 0;
752 phi_bb
= gimple_bb (phi
);
753 /* First find the closest dominating bb to be
754 the control dependence root */
755 cd_root
= find_dom (phi_bb
);
759 hash_set
<gimple
> visited_phis
;
760 collect_phi_def_edges (phi
, cd_root
, &def_edges
, &visited_phis
);
762 n
= def_edges
.length ();
766 for (i
= 0; i
< n
; i
++)
772 opnd_edge
= def_edges
[i
];
773 prev_nc
= num_chains
;
774 compute_control_dep_chain (cd_root
, opnd_edge
->src
, dep_chains
,
775 &num_chains
, &cur_chain
, &num_calls
);
777 /* Now update the newly added chains with
778 the phi operand edge: */
779 if (EDGE_COUNT (opnd_edge
->src
->succs
) > 1)
781 if (prev_nc
== num_chains
&& num_chains
< MAX_NUM_CHAINS
)
782 dep_chains
[num_chains
++] = vNULL
;
783 for (j
= prev_nc
; j
< num_chains
; j
++)
784 dep_chains
[j
].safe_push (opnd_edge
);
789 = convert_control_dep_chain_into_preds (dep_chains
, num_chains
, preds
);
790 for (i
= 0; i
< num_chains
; i
++)
791 dep_chains
[i
].release ();
792 return has_valid_pred
;
795 /* Dumps the predicates (PREDS) for USESTMT. */
798 dump_predicates (gimple usestmt
, pred_chain_union preds
,
802 pred_chain one_pred_chain
= vNULL
;
803 fprintf (dump_file
, "%s", msg
);
804 print_gimple_stmt (dump_file
, usestmt
, 0, 0);
805 fprintf (dump_file
, "is guarded by :\n\n");
806 size_t num_preds
= preds
.length ();
807 /* Do some dumping here: */
808 for (i
= 0; i
< num_preds
; i
++)
812 one_pred_chain
= preds
[i
];
813 np
= one_pred_chain
.length ();
815 for (j
= 0; j
< np
; j
++)
817 pred_info one_pred
= one_pred_chain
[j
];
819 fprintf (dump_file
, " (.NOT.) ");
820 print_generic_expr (dump_file
, one_pred
.pred_lhs
, 0);
821 fprintf (dump_file
, " %s ", op_symbol_code (one_pred
.cond_code
));
822 print_generic_expr (dump_file
, one_pred
.pred_rhs
, 0);
824 fprintf (dump_file
, " (.AND.) ");
826 fprintf (dump_file
, "\n");
828 if (i
< num_preds
- 1)
829 fprintf (dump_file
, "(.OR.)\n");
831 fprintf (dump_file
, "\n\n");
835 /* Destroys the predicate set *PREDS. */
838 destroy_predicate_vecs (pred_chain_union preds
)
842 size_t n
= preds
.length ();
843 for (i
= 0; i
< n
; i
++)
849 /* Computes the 'normalized' conditional code with operand
850 swapping and condition inversion. */
852 static enum tree_code
853 get_cmp_code (enum tree_code orig_cmp_code
,
854 bool swap_cond
, bool invert
)
856 enum tree_code tc
= orig_cmp_code
;
859 tc
= swap_tree_comparison (orig_cmp_code
);
861 tc
= invert_tree_comparison (tc
, false);
878 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
879 all values in the range satisfies (x CMPC BOUNDARY) == true. */
882 is_value_included_in (tree val
, tree boundary
, enum tree_code cmpc
)
884 bool inverted
= false;
888 /* Only handle integer constant here. */
889 if (TREE_CODE (val
) != INTEGER_CST
890 || TREE_CODE (boundary
) != INTEGER_CST
)
893 is_unsigned
= TYPE_UNSIGNED (TREE_TYPE (val
));
895 if (cmpc
== GE_EXPR
|| cmpc
== GT_EXPR
898 cmpc
= invert_tree_comparison (cmpc
, false);
905 result
= tree_int_cst_equal (val
, boundary
);
906 else if (cmpc
== LT_EXPR
)
907 result
= tree_int_cst_lt (val
, boundary
);
910 gcc_assert (cmpc
== LE_EXPR
);
911 result
= tree_int_cst_le (val
, boundary
);
917 result
= tree_int_cst_equal (val
, boundary
);
918 else if (cmpc
== LT_EXPR
)
919 result
= tree_int_cst_lt (val
, boundary
);
922 gcc_assert (cmpc
== LE_EXPR
);
923 result
= (tree_int_cst_equal (val
, boundary
)
924 || tree_int_cst_lt (val
, boundary
));
934 /* Returns true if PRED is common among all the predicate
935 chains (PREDS) (and therefore can be factored out).
936 NUM_PRED_CHAIN is the size of array PREDS. */
939 find_matching_predicate_in_rest_chains (pred_info pred
,
940 pred_chain_union preds
,
941 size_t num_pred_chains
)
946 if (num_pred_chains
== 1)
949 for (i
= 1; i
< num_pred_chains
; i
++)
952 pred_chain one_chain
= preds
[i
];
953 n
= one_chain
.length ();
954 for (j
= 0; j
< n
; j
++)
956 pred_info pred2
= one_chain
[j
];
957 /* Can relax the condition comparison to not
958 use address comparison. However, the most common
959 case is that multiple control dependent paths share
960 a common path prefix, so address comparison should
963 if (operand_equal_p (pred2
.pred_lhs
, pred
.pred_lhs
, 0)
964 && operand_equal_p (pred2
.pred_rhs
, pred
.pred_rhs
, 0)
965 && pred2
.invert
== pred
.invert
)
977 /* Forward declaration. */
979 is_use_properly_guarded (gimple use_stmt
,
982 unsigned uninit_opnds
,
983 hash_set
<gphi
*> *visited_phis
);
985 /* Returns true if all uninitialized opnds are pruned. Returns false
986 otherwise. PHI is the phi node with uninitialized operands,
987 UNINIT_OPNDS is the bitmap of the uninitialize operand positions,
988 FLAG_DEF is the statement defining the flag guarding the use of the
989 PHI output, BOUNDARY_CST is the const value used in the predicate
990 associated with the flag, CMP_CODE is the comparison code used in
991 the predicate, VISITED_PHIS is the pointer set of phis visited, and
992 VISITED_FLAG_PHIS is the pointer to the pointer set of flag definitions
998 flag_1 = phi <0, 1> // (1)
999 var_1 = phi <undef, some_val>
1003 flag_2 = phi <0, flag_1, flag_1> // (2)
1004 var_2 = phi <undef, var_1, var_1>
1011 Because some flag arg in (1) is not constant, if we do not look into the
1012 flag phis recursively, it is conservatively treated as unknown and var_1
1013 is thought to be flowed into use at (3). Since var_1 is potentially uninitialized
1014 a false warning will be emitted. Checking recursively into (1), the compiler can
1015 find out that only some_val (which is defined) can flow into (3) which is OK.
1020 prune_uninit_phi_opnds_in_unrealizable_paths (gphi
*phi
,
1021 unsigned uninit_opnds
,
1024 enum tree_code cmp_code
,
1025 hash_set
<gphi
*> *visited_phis
,
1026 bitmap
*visited_flag_phis
)
1030 for (i
= 0; i
< MIN (32, gimple_phi_num_args (flag_def
)); i
++)
1034 if (!MASK_TEST_BIT (uninit_opnds
, i
))
1037 flag_arg
= gimple_phi_arg_def (flag_def
, i
);
1038 if (!is_gimple_constant (flag_arg
))
1040 gphi
*flag_arg_def
, *phi_arg_def
;
1042 unsigned uninit_opnds_arg_phi
;
1044 if (TREE_CODE (flag_arg
) != SSA_NAME
)
1046 flag_arg_def
= dyn_cast
<gphi
*> (SSA_NAME_DEF_STMT (flag_arg
));
1050 phi_arg
= gimple_phi_arg_def (phi
, i
);
1051 if (TREE_CODE (phi_arg
) != SSA_NAME
)
1054 phi_arg_def
= dyn_cast
<gphi
*> (SSA_NAME_DEF_STMT (phi_arg
));
1058 if (gimple_bb (phi_arg_def
) != gimple_bb (flag_arg_def
))
1061 if (!*visited_flag_phis
)
1062 *visited_flag_phis
= BITMAP_ALLOC (NULL
);
1064 if (bitmap_bit_p (*visited_flag_phis
,
1065 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def
))))
1068 bitmap_set_bit (*visited_flag_phis
,
1069 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def
)));
1071 /* Now recursively prune the uninitialized phi args. */
1072 uninit_opnds_arg_phi
= compute_uninit_opnds_pos (phi_arg_def
);
1073 if (!prune_uninit_phi_opnds_in_unrealizable_paths
1074 (phi_arg_def
, uninit_opnds_arg_phi
, flag_arg_def
,
1075 boundary_cst
, cmp_code
, visited_phis
, visited_flag_phis
))
1078 bitmap_clear_bit (*visited_flag_phis
,
1079 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def
)));
1083 /* Now check if the constant is in the guarded range. */
1084 if (is_value_included_in (flag_arg
, boundary_cst
, cmp_code
))
1089 /* Now that we know that this undefined edge is not
1090 pruned. If the operand is defined by another phi,
1091 we can further prune the incoming edges of that
1092 phi by checking the predicates of this operands. */
1094 opnd
= gimple_phi_arg_def (phi
, i
);
1095 opnd_def
= SSA_NAME_DEF_STMT (opnd
);
1096 if (gphi
*opnd_def_phi
= dyn_cast
<gphi
*> (opnd_def
))
1099 unsigned uninit_opnds2
1100 = compute_uninit_opnds_pos (opnd_def_phi
);
1101 gcc_assert (!MASK_EMPTY (uninit_opnds2
));
1102 opnd_edge
= gimple_phi_arg_edge (phi
, i
);
1103 if (!is_use_properly_guarded (phi
,
1118 /* A helper function that determines if the predicate set
1119 of the use is not overlapping with that of the uninit paths.
1120 The most common senario of guarded use is in Example 1:
1133 The real world examples are usually more complicated, but similar
1134 and usually result from inlining:
1136 bool init_func (int * x)
1155 Another possible use scenario is in the following trivial example:
1167 Predicate analysis needs to compute the composite predicate:
1169 1) 'x' use predicate: (n > 0) .AND. (m < 2)
1170 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
1171 (the predicate chain for phi operand defs can be computed
1172 starting from a bb that is control equivalent to the phi's
1173 bb and is dominating the operand def.)
1175 and check overlapping:
1176 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
1179 This implementation provides framework that can handle
1180 scenarios. (Note that many simple cases are handled properly
1181 without the predicate analysis -- this is due to jump threading
1182 transformation which eliminates the merge point thus makes
1183 path sensitive analysis unnecessary.)
1185 NUM_PREDS is the number is the number predicate chains, PREDS is
1186 the array of chains, PHI is the phi node whose incoming (undefined)
1187 paths need to be pruned, and UNINIT_OPNDS is the bitmap holding
1188 uninit operand positions. VISITED_PHIS is the pointer set of phi
1189 stmts being checked. */
1193 use_pred_not_overlap_with_undef_path_pred (pred_chain_union preds
,
1194 gphi
*phi
, unsigned uninit_opnds
,
1195 hash_set
<gphi
*> *visited_phis
)
1198 gimple flag_def
= 0;
1199 tree boundary_cst
= 0;
1200 enum tree_code cmp_code
;
1201 bool swap_cond
= false;
1202 bool invert
= false;
1203 pred_chain the_pred_chain
= vNULL
;
1204 bitmap visited_flag_phis
= NULL
;
1205 bool all_pruned
= false;
1206 size_t num_preds
= preds
.length ();
1208 gcc_assert (num_preds
> 0);
1209 /* Find within the common prefix of multiple predicate chains
1210 a predicate that is a comparison of a flag variable against
1212 the_pred_chain
= preds
[0];
1213 n
= the_pred_chain
.length ();
1214 for (i
= 0; i
< n
; i
++)
1216 tree cond_lhs
, cond_rhs
, flag
= 0;
1218 pred_info the_pred
= the_pred_chain
[i
];
1220 invert
= the_pred
.invert
;
1221 cond_lhs
= the_pred
.pred_lhs
;
1222 cond_rhs
= the_pred
.pred_rhs
;
1223 cmp_code
= the_pred
.cond_code
;
1225 if (cond_lhs
!= NULL_TREE
&& TREE_CODE (cond_lhs
) == SSA_NAME
1226 && cond_rhs
!= NULL_TREE
&& is_gimple_constant (cond_rhs
))
1228 boundary_cst
= cond_rhs
;
1231 else if (cond_rhs
!= NULL_TREE
&& TREE_CODE (cond_rhs
) == SSA_NAME
1232 && cond_lhs
!= NULL_TREE
&& is_gimple_constant (cond_lhs
))
1234 boundary_cst
= cond_lhs
;
1242 flag_def
= SSA_NAME_DEF_STMT (flag
);
1247 if ((gimple_code (flag_def
) == GIMPLE_PHI
)
1248 && (gimple_bb (flag_def
) == gimple_bb (phi
))
1249 && find_matching_predicate_in_rest_chains (the_pred
, preds
,
1259 /* Now check all the uninit incoming edge has a constant flag value
1260 that is in conflict with the use guard/predicate. */
1261 cmp_code
= get_cmp_code (cmp_code
, swap_cond
, invert
);
1263 if (cmp_code
== ERROR_MARK
)
1266 all_pruned
= prune_uninit_phi_opnds_in_unrealizable_paths (phi
,
1268 as_a
<gphi
*> (flag_def
),
1272 &visited_flag_phis
);
1274 if (visited_flag_phis
)
1275 BITMAP_FREE (visited_flag_phis
);
1280 /* The helper function returns true if two predicates X1 and X2
1281 are equivalent. It assumes the expressions have already
1282 properly re-associated. */
1285 pred_equal_p (pred_info x1
, pred_info x2
)
1287 enum tree_code c1
, c2
;
1288 if (!operand_equal_p (x1
.pred_lhs
, x2
.pred_lhs
, 0)
1289 || !operand_equal_p (x1
.pred_rhs
, x2
.pred_rhs
, 0))
1293 if (x1
.invert
!= x2
.invert
)
1294 c2
= invert_tree_comparison (x2
.cond_code
, false);
1301 /* Returns true if the predication is testing !=. */
1304 is_neq_relop_p (pred_info pred
)
1307 return (pred
.cond_code
== NE_EXPR
&& !pred
.invert
)
1308 || (pred
.cond_code
== EQ_EXPR
&& pred
.invert
);
1311 /* Returns true if pred is of the form X != 0. */
1314 is_neq_zero_form_p (pred_info pred
)
1316 if (!is_neq_relop_p (pred
) || !integer_zerop (pred
.pred_rhs
)
1317 || TREE_CODE (pred
.pred_lhs
) != SSA_NAME
)
1322 /* The helper function returns true if two predicates X1
1323 is equivalent to X2 != 0. */
1326 pred_expr_equal_p (pred_info x1
, tree x2
)
1328 if (!is_neq_zero_form_p (x1
))
1331 return operand_equal_p (x1
.pred_lhs
, x2
, 0);
1334 /* Returns true of the domain of single predicate expression
1335 EXPR1 is a subset of that of EXPR2. Returns false if it
1336 can not be proved. */
1339 is_pred_expr_subset_of (pred_info expr1
, pred_info expr2
)
1341 enum tree_code code1
, code2
;
1343 if (pred_equal_p (expr1
, expr2
))
1346 if ((TREE_CODE (expr1
.pred_rhs
) != INTEGER_CST
)
1347 || (TREE_CODE (expr2
.pred_rhs
) != INTEGER_CST
))
1350 if (!operand_equal_p (expr1
.pred_lhs
, expr2
.pred_lhs
, 0))
1353 code1
= expr1
.cond_code
;
1355 code1
= invert_tree_comparison (code1
, false);
1356 code2
= expr2
.cond_code
;
1358 code2
= invert_tree_comparison (code2
, false);
1360 if ((code1
== EQ_EXPR
|| code1
== BIT_AND_EXPR
)
1361 && code2
== BIT_AND_EXPR
)
1362 return wi::eq_p (expr1
.pred_rhs
,
1363 wi::bit_and (expr1
.pred_rhs
, expr2
.pred_rhs
));
1365 if (code1
!= code2
&& code2
!= NE_EXPR
)
1368 if (is_value_included_in (expr1
.pred_rhs
, expr2
.pred_rhs
, code2
))
1374 /* Returns true if the domain of PRED1 is a subset
1375 of that of PRED2. Returns false if it can not be proved so. */
1378 is_pred_chain_subset_of (pred_chain pred1
,
1381 size_t np1
, np2
, i1
, i2
;
1383 np1
= pred1
.length ();
1384 np2
= pred2
.length ();
1386 for (i2
= 0; i2
< np2
; i2
++)
1389 pred_info info2
= pred2
[i2
];
1390 for (i1
= 0; i1
< np1
; i1
++)
1392 pred_info info1
= pred1
[i1
];
1393 if (is_pred_expr_subset_of (info1
, info2
))
1405 /* Returns true if the domain defined by
1406 one pred chain ONE_PRED is a subset of the domain
1407 of *PREDS. It returns false if ONE_PRED's domain is
1408 not a subset of any of the sub-domains of PREDS
1409 (corresponding to each individual chains in it), even
1410 though it may be still be a subset of whole domain
1411 of PREDS which is the union (ORed) of all its subdomains.
1412 In other words, the result is conservative. */
1415 is_included_in (pred_chain one_pred
, pred_chain_union preds
)
1418 size_t n
= preds
.length ();
1420 for (i
= 0; i
< n
; i
++)
1422 if (is_pred_chain_subset_of (one_pred
, preds
[i
]))
1429 /* Compares two predicate sets PREDS1 and PREDS2 and returns
1430 true if the domain defined by PREDS1 is a superset
1431 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1432 PREDS2 respectively. The implementation chooses not to build
1433 generic trees (and relying on the folding capability of the
1434 compiler), but instead performs brute force comparison of
1435 individual predicate chains (won't be a compile time problem
1436 as the chains are pretty short). When the function returns
1437 false, it does not necessarily mean *PREDS1 is not a superset
1438 of *PREDS2, but mean it may not be so since the analysis can
1439 not prove it. In such cases, false warnings may still be
1443 is_superset_of (pred_chain_union preds1
, pred_chain_union preds2
)
1446 pred_chain one_pred_chain
= vNULL
;
1448 n2
= preds2
.length ();
1450 for (i
= 0; i
< n2
; i
++)
1452 one_pred_chain
= preds2
[i
];
1453 if (!is_included_in (one_pred_chain
, preds1
))
1460 /* Returns true if TC is AND or OR. */
1463 is_and_or_or_p (enum tree_code tc
, tree type
)
1465 return (tc
== BIT_IOR_EXPR
1466 || (tc
== BIT_AND_EXPR
1467 && (type
== 0 || TREE_CODE (type
) == BOOLEAN_TYPE
)));
1470 /* Returns true if X1 is the negate of X2. */
1473 pred_neg_p (pred_info x1
, pred_info x2
)
1475 enum tree_code c1
, c2
;
1476 if (!operand_equal_p (x1
.pred_lhs
, x2
.pred_lhs
, 0)
1477 || !operand_equal_p (x1
.pred_rhs
, x2
.pred_rhs
, 0))
1481 if (x1
.invert
== x2
.invert
)
1482 c2
= invert_tree_comparison (x2
.cond_code
, false);
1489 /* 1) ((x IOR y) != 0) AND (x != 0) is equivalent to (x != 0);
1490 2) (X AND Y) OR (!X AND Y) is equivalent to Y;
1491 3) X OR (!X AND Y) is equivalent to (X OR Y);
1492 4) ((x IAND y) != 0) || (x != 0 AND y != 0)) is equivalent to
1494 5) (X AND Y) OR (!X AND Z) OR (!Y AND Z) is equivalent to
1497 PREDS is the predicate chains, and N is the number of chains. */
1499 /* Helper function to implement rule 1 above. ONE_CHAIN is
1500 the AND predication to be simplified. */
1503 simplify_pred (pred_chain
*one_chain
)
1506 bool simplified
= false;
1507 pred_chain s_chain
= vNULL
;
1509 n
= one_chain
->length ();
1511 for (i
= 0; i
< n
; i
++)
1513 pred_info
*a_pred
= &(*one_chain
)[i
];
1515 if (!a_pred
->pred_lhs
)
1517 if (!is_neq_zero_form_p (*a_pred
))
1520 gimple def_stmt
= SSA_NAME_DEF_STMT (a_pred
->pred_lhs
);
1521 if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
1523 if (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
)
1525 for (j
= 0; j
< n
; j
++)
1527 pred_info
*b_pred
= &(*one_chain
)[j
];
1529 if (!b_pred
->pred_lhs
)
1531 if (!is_neq_zero_form_p (*b_pred
))
1534 if (pred_expr_equal_p (*b_pred
, gimple_assign_rhs1 (def_stmt
))
1535 || pred_expr_equal_p (*b_pred
, gimple_assign_rhs2 (def_stmt
)))
1537 /* Mark a_pred for removal. */
1538 a_pred
->pred_lhs
= NULL
;
1539 a_pred
->pred_rhs
= NULL
;
1550 for (i
= 0; i
< n
; i
++)
1552 pred_info
*a_pred
= &(*one_chain
)[i
];
1553 if (!a_pred
->pred_lhs
)
1555 s_chain
.safe_push (*a_pred
);
1558 one_chain
->release ();
1559 *one_chain
= s_chain
;
1562 /* The helper function implements the rule 2 for the
1565 2) (X AND Y) OR (!X AND Y) is equivalent to Y. */
1568 simplify_preds_2 (pred_chain_union
*preds
)
1571 bool simplified
= false;
1572 pred_chain_union s_preds
= vNULL
;
1574 /* (X AND Y) OR (!X AND Y) is equivalent to Y.
1575 (X AND Y) OR (X AND !Y) is equivalent to X. */
1577 n
= preds
->length ();
1578 for (i
= 0; i
< n
; i
++)
1581 pred_chain
*a_chain
= &(*preds
)[i
];
1583 if (a_chain
->length () != 2)
1589 for (j
= 0; j
< n
; j
++)
1591 pred_chain
*b_chain
;
1597 b_chain
= &(*preds
)[j
];
1598 if (b_chain
->length () != 2)
1604 if (pred_equal_p (x
, x2
) && pred_neg_p (y
, y2
))
1607 a_chain
->release ();
1608 b_chain
->release ();
1609 b_chain
->safe_push (x
);
1613 if (pred_neg_p (x
, x2
) && pred_equal_p (y
, y2
))
1616 a_chain
->release ();
1617 b_chain
->release ();
1618 b_chain
->safe_push (y
);
1624 /* Now clean up the chain. */
1627 for (i
= 0; i
< n
; i
++)
1629 if ((*preds
)[i
].is_empty ())
1631 s_preds
.safe_push ((*preds
)[i
]);
1641 /* The helper function implements the rule 2 for the
1644 3) x OR (!x AND y) is equivalent to x OR y. */
1647 simplify_preds_3 (pred_chain_union
*preds
)
1650 bool simplified
= false;
1652 /* Now iteratively simplify X OR (!X AND Z ..)
1653 into X OR (Z ...). */
1655 n
= preds
->length ();
1659 for (i
= 0; i
< n
; i
++)
1662 pred_chain
*a_chain
= &(*preds
)[i
];
1664 if (a_chain
->length () != 1)
1669 for (j
= 0; j
< n
; j
++)
1671 pred_chain
*b_chain
;
1678 b_chain
= &(*preds
)[j
];
1679 if (b_chain
->length () < 2)
1682 for (k
= 0; k
< b_chain
->length (); k
++)
1685 if (pred_neg_p (x
, x2
))
1687 b_chain
->unordered_remove (k
);
1697 /* The helper function implements the rule 4 for the
1700 2) ((x AND y) != 0) OR (x != 0 AND y != 0) is equivalent to
1701 (x != 0 ANd y != 0). */
1704 simplify_preds_4 (pred_chain_union
*preds
)
1707 bool simplified
= false;
1708 pred_chain_union s_preds
= vNULL
;
1711 n
= preds
->length ();
1712 for (i
= 0; i
< n
; i
++)
1715 pred_chain
*a_chain
= &(*preds
)[i
];
1717 if (a_chain
->length () != 1)
1722 if (!is_neq_zero_form_p (z
))
1725 def_stmt
= SSA_NAME_DEF_STMT (z
.pred_lhs
);
1726 if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
1729 if (gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
1732 for (j
= 0; j
< n
; j
++)
1734 pred_chain
*b_chain
;
1740 b_chain
= &(*preds
)[j
];
1741 if (b_chain
->length () != 2)
1746 if (!is_neq_zero_form_p (x2
)
1747 || !is_neq_zero_form_p (y2
))
1750 if ((pred_expr_equal_p (x2
, gimple_assign_rhs1 (def_stmt
))
1751 && pred_expr_equal_p (y2
, gimple_assign_rhs2 (def_stmt
)))
1752 || (pred_expr_equal_p (x2
, gimple_assign_rhs2 (def_stmt
))
1753 && pred_expr_equal_p (y2
, gimple_assign_rhs1 (def_stmt
))))
1756 a_chain
->release ();
1762 /* Now clean up the chain. */
1765 for (i
= 0; i
< n
; i
++)
1767 if ((*preds
)[i
].is_empty ())
1769 s_preds
.safe_push ((*preds
)[i
]);
1780 /* This function simplifies predicates in PREDS. */
1783 simplify_preds (pred_chain_union
*preds
, gimple use_or_def
, bool is_use
)
1786 bool changed
= false;
1788 if (dump_file
&& dump_flags
& TDF_DETAILS
)
1790 fprintf (dump_file
, "[BEFORE SIMPLICATION -- ");
1791 dump_predicates (use_or_def
, *preds
, is_use
? "[USE]:\n" : "[DEF]:\n");
1794 for (i
= 0; i
< preds
->length (); i
++)
1795 simplify_pred (&(*preds
)[i
]);
1797 n
= preds
->length ();
1804 if (simplify_preds_2 (preds
))
1807 /* Now iteratively simplify X OR (!X AND Z ..)
1808 into X OR (Z ...). */
1809 if (simplify_preds_3 (preds
))
1812 if (simplify_preds_4 (preds
))
1820 /* This is a helper function which attempts to normalize predicate chains
1821 by following UD chains. It basically builds up a big tree of either IOR
1822 operations or AND operations, and convert the IOR tree into a
1823 pred_chain_union or BIT_AND tree into a pred_chain.
1833 then _t != 0 will be normalized into a pred_chain_union
1835 (_2 RELOP1 _1) OR (_5 RELOP2 _4) OR (_8 RELOP3 _7) OR (_0 != 0)
1845 then _t != 0 will be normalized into a pred_chain:
1846 (_2 RELOP1 _1) AND (_5 RELOP2 _4) AND (_8 RELOP3 _7) AND (_0 != 0)
1850 /* This is a helper function that stores a PRED into NORM_PREDS. */
1853 push_pred (pred_chain_union
*norm_preds
, pred_info pred
)
1855 pred_chain pred_chain
= vNULL
;
1856 pred_chain
.safe_push (pred
);
1857 norm_preds
->safe_push (pred_chain
);
1860 /* A helper function that creates a predicate of the form
1861 OP != 0 and push it WORK_LIST. */
1864 push_to_worklist (tree op
, vec
<pred_info
, va_heap
, vl_ptr
> *work_list
,
1865 hash_set
<tree
> *mark_set
)
1867 if (mark_set
->contains (op
))
1872 arg_pred
.pred_lhs
= op
;
1873 arg_pred
.pred_rhs
= integer_zero_node
;
1874 arg_pred
.cond_code
= NE_EXPR
;
1875 arg_pred
.invert
= false;
1876 work_list
->safe_push (arg_pred
);
1879 /* A helper that generates a pred_info from a gimple assignment
1880 CMP_ASSIGN with comparison rhs. */
1883 get_pred_info_from_cmp (gimple cmp_assign
)
1886 n_pred
.pred_lhs
= gimple_assign_rhs1 (cmp_assign
);
1887 n_pred
.pred_rhs
= gimple_assign_rhs2 (cmp_assign
);
1888 n_pred
.cond_code
= gimple_assign_rhs_code (cmp_assign
);
1889 n_pred
.invert
= false;
1893 /* Returns true if the PHI is a degenerated phi with
1894 all args with the same value (relop). In that case, *PRED
1895 will be updated to that value. */
1898 is_degenerated_phi (gimple phi
, pred_info
*pred_p
)
1905 n
= gimple_phi_num_args (phi
);
1906 op0
= gimple_phi_arg_def (phi
, 0);
1908 if (TREE_CODE (op0
) != SSA_NAME
)
1911 def0
= SSA_NAME_DEF_STMT (op0
);
1912 if (gimple_code (def0
) != GIMPLE_ASSIGN
)
1914 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def0
))
1917 pred0
= get_pred_info_from_cmp (def0
);
1919 for (i
= 1; i
< n
; ++i
)
1923 tree op
= gimple_phi_arg_def (phi
, i
);
1925 if (TREE_CODE (op
) != SSA_NAME
)
1928 def
= SSA_NAME_DEF_STMT (op
);
1929 if (gimple_code (def
) != GIMPLE_ASSIGN
)
1931 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def
))
1934 pred
= get_pred_info_from_cmp (def
);
1935 if (!pred_equal_p (pred
, pred0
))
1943 /* Normalize one predicate PRED
1944 1) if PRED can no longer be normlized, put it into NORM_PREDS.
1945 2) otherwise if PRED is of the form x != 0, follow x's definition
1946 and put normalized predicates into WORK_LIST. */
1949 normalize_one_pred_1 (pred_chain_union
*norm_preds
,
1950 pred_chain
*norm_chain
,
1952 enum tree_code and_or_code
,
1953 vec
<pred_info
, va_heap
, vl_ptr
> *work_list
,
1954 hash_set
<tree
> *mark_set
)
1956 if (!is_neq_zero_form_p (pred
))
1958 if (and_or_code
== BIT_IOR_EXPR
)
1959 push_pred (norm_preds
, pred
);
1961 norm_chain
->safe_push (pred
);
1965 gimple def_stmt
= SSA_NAME_DEF_STMT (pred
.pred_lhs
);
1967 if (gimple_code (def_stmt
) == GIMPLE_PHI
1968 && is_degenerated_phi (def_stmt
, &pred
))
1969 work_list
->safe_push (pred
);
1970 else if (gimple_code (def_stmt
) == GIMPLE_PHI
1971 && and_or_code
== BIT_IOR_EXPR
)
1974 n
= gimple_phi_num_args (def_stmt
);
1976 /* If we see non zero constant, we should punt. The predicate
1977 * should be one guarding the phi edge. */
1978 for (i
= 0; i
< n
; ++i
)
1980 tree op
= gimple_phi_arg_def (def_stmt
, i
);
1981 if (TREE_CODE (op
) == INTEGER_CST
&& !integer_zerop (op
))
1983 push_pred (norm_preds
, pred
);
1988 for (i
= 0; i
< n
; ++i
)
1990 tree op
= gimple_phi_arg_def (def_stmt
, i
);
1991 if (integer_zerop (op
))
1994 push_to_worklist (op
, work_list
, mark_set
);
1997 else if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
1999 if (and_or_code
== BIT_IOR_EXPR
)
2000 push_pred (norm_preds
, pred
);
2002 norm_chain
->safe_push (pred
);
2004 else if (gimple_assign_rhs_code (def_stmt
) == and_or_code
)
2006 /* Avoid splitting up bit manipulations like x & 3 or y | 1. */
2007 if (is_gimple_min_invariant (gimple_assign_rhs2 (def_stmt
)))
2009 /* But treat x & 3 as condition. */
2010 if (and_or_code
== BIT_AND_EXPR
)
2013 n_pred
.pred_lhs
= gimple_assign_rhs1 (def_stmt
);
2014 n_pred
.pred_rhs
= gimple_assign_rhs2 (def_stmt
);
2015 n_pred
.cond_code
= and_or_code
;
2016 n_pred
.invert
= false;
2017 norm_chain
->safe_push (n_pred
);
2022 push_to_worklist (gimple_assign_rhs1 (def_stmt
), work_list
, mark_set
);
2023 push_to_worklist (gimple_assign_rhs2 (def_stmt
), work_list
, mark_set
);
2026 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
))
2029 pred_info n_pred
= get_pred_info_from_cmp (def_stmt
);
2030 if (and_or_code
== BIT_IOR_EXPR
)
2031 push_pred (norm_preds
, n_pred
);
2033 norm_chain
->safe_push (n_pred
);
2037 if (and_or_code
== BIT_IOR_EXPR
)
2038 push_pred (norm_preds
, pred
);
2040 norm_chain
->safe_push (pred
);
2044 /* Normalize PRED and store the normalized predicates into NORM_PREDS. */
2047 normalize_one_pred (pred_chain_union
*norm_preds
,
2050 vec
<pred_info
, va_heap
, vl_ptr
> work_list
= vNULL
;
2051 enum tree_code and_or_code
= ERROR_MARK
;
2052 pred_chain norm_chain
= vNULL
;
2054 if (!is_neq_zero_form_p (pred
))
2056 push_pred (norm_preds
, pred
);
2060 gimple def_stmt
= SSA_NAME_DEF_STMT (pred
.pred_lhs
);
2061 if (gimple_code (def_stmt
) == GIMPLE_ASSIGN
)
2062 and_or_code
= gimple_assign_rhs_code (def_stmt
);
2063 if (and_or_code
!= BIT_IOR_EXPR
2064 && and_or_code
!= BIT_AND_EXPR
)
2066 if (TREE_CODE_CLASS (and_or_code
)
2069 pred_info n_pred
= get_pred_info_from_cmp (def_stmt
);
2070 push_pred (norm_preds
, n_pred
);
2073 push_pred (norm_preds
, pred
);
2077 work_list
.safe_push (pred
);
2078 hash_set
<tree
> mark_set
;
2080 while (!work_list
.is_empty ())
2082 pred_info a_pred
= work_list
.pop ();
2083 normalize_one_pred_1 (norm_preds
, &norm_chain
, a_pred
,
2084 and_or_code
, &work_list
, &mark_set
);
2086 if (and_or_code
== BIT_AND_EXPR
)
2087 norm_preds
->safe_push (norm_chain
);
2089 work_list
.release ();
2093 normalize_one_pred_chain (pred_chain_union
*norm_preds
,
2094 pred_chain one_chain
)
2096 vec
<pred_info
, va_heap
, vl_ptr
> work_list
= vNULL
;
2097 hash_set
<tree
> mark_set
;
2098 pred_chain norm_chain
= vNULL
;
2101 for (i
= 0; i
< one_chain
.length (); i
++)
2103 work_list
.safe_push (one_chain
[i
]);
2104 mark_set
.add (one_chain
[i
].pred_lhs
);
2107 while (!work_list
.is_empty ())
2109 pred_info a_pred
= work_list
.pop ();
2110 normalize_one_pred_1 (0, &norm_chain
, a_pred
,
2111 BIT_AND_EXPR
, &work_list
, &mark_set
);
2114 norm_preds
->safe_push (norm_chain
);
2115 work_list
.release ();
2118 /* Normalize predicate chains PREDS and returns the normalized one. */
2120 static pred_chain_union
2121 normalize_preds (pred_chain_union preds
, gimple use_or_def
, bool is_use
)
2123 pred_chain_union norm_preds
= vNULL
;
2124 size_t n
= preds
.length ();
2127 if (dump_file
&& dump_flags
& TDF_DETAILS
)
2129 fprintf (dump_file
, "[BEFORE NORMALIZATION --");
2130 dump_predicates (use_or_def
, preds
, is_use
? "[USE]:\n" : "[DEF]:\n");
2133 for (i
= 0; i
< n
; i
++)
2135 if (preds
[i
].length () != 1)
2136 normalize_one_pred_chain (&norm_preds
, preds
[i
]);
2139 normalize_one_pred (&norm_preds
, preds
[i
][0]);
2140 preds
[i
].release ();
2146 fprintf (dump_file
, "[AFTER NORMALIZATION -- ");
2147 dump_predicates (use_or_def
, norm_preds
, is_use
? "[USE]:\n" : "[DEF]:\n");
2155 /* Computes the predicates that guard the use and checks
2156 if the incoming paths that have empty (or possibly
2157 empty) definition can be pruned/filtered. The function returns
2158 true if it can be determined that the use of PHI's def in
2159 USE_STMT is guarded with a predicate set not overlapping with
2160 predicate sets of all runtime paths that do not have a definition.
2161 Returns false if it is not or it can not be determined. USE_BB is
2162 the bb of the use (for phi operand use, the bb is not the bb of
2163 the phi stmt, but the src bb of the operand edge). UNINIT_OPNDS
2164 is a bit vector. If an operand of PHI is uninitialized, the
2165 corresponding bit in the vector is 1. VISIED_PHIS is a pointer
2166 set of phis being visted. */
2169 is_use_properly_guarded (gimple use_stmt
,
2172 unsigned uninit_opnds
,
2173 hash_set
<gphi
*> *visited_phis
)
2176 pred_chain_union preds
= vNULL
;
2177 pred_chain_union def_preds
= vNULL
;
2178 bool has_valid_preds
= false;
2179 bool is_properly_guarded
= false;
2181 if (visited_phis
->add (phi
))
2184 phi_bb
= gimple_bb (phi
);
2186 if (is_non_loop_exit_postdominating (use_bb
, phi_bb
))
2189 has_valid_preds
= find_predicates (&preds
, phi_bb
, use_bb
);
2191 if (!has_valid_preds
)
2193 destroy_predicate_vecs (preds
);
2197 /* Try to prune the dead incoming phi edges. */
2199 = use_pred_not_overlap_with_undef_path_pred (preds
, phi
, uninit_opnds
,
2202 if (is_properly_guarded
)
2204 destroy_predicate_vecs (preds
);
2208 has_valid_preds
= find_def_preds (&def_preds
, phi
);
2210 if (!has_valid_preds
)
2212 destroy_predicate_vecs (preds
);
2213 destroy_predicate_vecs (def_preds
);
2217 simplify_preds (&preds
, use_stmt
, true);
2218 preds
= normalize_preds (preds
, use_stmt
, true);
2220 simplify_preds (&def_preds
, phi
, false);
2221 def_preds
= normalize_preds (def_preds
, phi
, false);
2223 is_properly_guarded
= is_superset_of (def_preds
, preds
);
2225 destroy_predicate_vecs (preds
);
2226 destroy_predicate_vecs (def_preds
);
2227 return is_properly_guarded
;
2230 /* Searches through all uses of a potentially
2231 uninitialized variable defined by PHI and returns a use
2232 statement if the use is not properly guarded. It returns
2233 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
2234 holding the position(s) of uninit PHI operands. WORKLIST
2235 is the vector of candidate phis that may be updated by this
2236 function. ADDED_TO_WORKLIST is the pointer set tracking
2237 if the new phi is already in the worklist. */
2240 find_uninit_use (gphi
*phi
, unsigned uninit_opnds
,
2241 vec
<gphi
*> *worklist
,
2242 hash_set
<gphi
*> *added_to_worklist
)
2245 use_operand_p use_p
;
2247 imm_use_iterator iter
;
2249 phi_result
= gimple_phi_result (phi
);
2251 FOR_EACH_IMM_USE_FAST (use_p
, iter
, phi_result
)
2255 use_stmt
= USE_STMT (use_p
);
2256 if (is_gimple_debug (use_stmt
))
2259 if (gphi
*use_phi
= dyn_cast
<gphi
*> (use_stmt
))
2260 use_bb
= gimple_phi_arg_edge (use_phi
,
2261 PHI_ARG_INDEX_FROM_USE (use_p
))->src
;
2263 use_bb
= gimple_bb (use_stmt
);
2265 hash_set
<gphi
*> visited_phis
;
2266 if (is_use_properly_guarded (use_stmt
, use_bb
, phi
, uninit_opnds
,
2270 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2272 fprintf (dump_file
, "[CHECK]: Found unguarded use: ");
2273 print_gimple_stmt (dump_file
, use_stmt
, 0, 0);
2275 /* Found one real use, return. */
2276 if (gimple_code (use_stmt
) != GIMPLE_PHI
)
2279 /* Found a phi use that is not guarded,
2280 add the phi to the worklist. */
2281 if (!added_to_worklist
->add (as_a
<gphi
*> (use_stmt
)))
2283 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2285 fprintf (dump_file
, "[WORKLIST]: Update worklist with phi: ");
2286 print_gimple_stmt (dump_file
, use_stmt
, 0, 0);
2289 worklist
->safe_push (as_a
<gphi
*> (use_stmt
));
2290 possibly_undefined_names
->add (phi_result
);
2297 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
2298 and gives warning if there exists a runtime path from the entry to a
2299 use of the PHI def that does not contain a definition. In other words,
2300 the warning is on the real use. The more dead paths that can be pruned
2301 by the compiler, the fewer false positives the warning is. WORKLIST
2302 is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
2303 a pointer set tracking if the new phi is added to the worklist or not. */
2306 warn_uninitialized_phi (gphi
*phi
, vec
<gphi
*> *worklist
,
2307 hash_set
<gphi
*> *added_to_worklist
)
2309 unsigned uninit_opnds
;
2310 gimple uninit_use_stmt
= 0;
2315 /* Don't look at virtual operands. */
2316 if (virtual_operand_p (gimple_phi_result (phi
)))
2319 uninit_opnds
= compute_uninit_opnds_pos (phi
);
2321 if (MASK_EMPTY (uninit_opnds
))
2324 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2326 fprintf (dump_file
, "[CHECK]: examining phi: ");
2327 print_gimple_stmt (dump_file
, phi
, 0, 0);
2330 /* Now check if we have any use of the value without proper guard. */
2331 uninit_use_stmt
= find_uninit_use (phi
, uninit_opnds
,
2332 worklist
, added_to_worklist
);
2334 /* All uses are properly guarded. */
2335 if (!uninit_use_stmt
)
2338 phiarg_index
= MASK_FIRST_SET_BIT (uninit_opnds
);
2339 uninit_op
= gimple_phi_arg_def (phi
, phiarg_index
);
2340 if (SSA_NAME_VAR (uninit_op
) == NULL_TREE
)
2342 if (gimple_phi_arg_has_location (phi
, phiarg_index
))
2343 loc
= gimple_phi_arg_location (phi
, phiarg_index
);
2345 loc
= UNKNOWN_LOCATION
;
2346 warn_uninit (OPT_Wmaybe_uninitialized
, uninit_op
, SSA_NAME_VAR (uninit_op
),
2347 SSA_NAME_VAR (uninit_op
),
2348 "%qD may be used uninitialized in this function",
2349 uninit_use_stmt
, loc
);
2354 gate_warn_uninitialized (void)
2356 return warn_uninitialized
|| warn_maybe_uninitialized
;
2361 const pass_data pass_data_late_warn_uninitialized
=
2363 GIMPLE_PASS
, /* type */
2364 "uninit", /* name */
2365 OPTGROUP_NONE
, /* optinfo_flags */
2366 TV_NONE
, /* tv_id */
2367 PROP_ssa
, /* properties_required */
2368 0, /* properties_provided */
2369 0, /* properties_destroyed */
2370 0, /* todo_flags_start */
2371 0, /* todo_flags_finish */
2374 class pass_late_warn_uninitialized
: public gimple_opt_pass
2377 pass_late_warn_uninitialized (gcc::context
*ctxt
)
2378 : gimple_opt_pass (pass_data_late_warn_uninitialized
, ctxt
)
2381 /* opt_pass methods: */
2382 opt_pass
* clone () { return new pass_late_warn_uninitialized (m_ctxt
); }
2383 virtual bool gate (function
*) { return gate_warn_uninitialized (); }
2384 virtual unsigned int execute (function
*);
2386 }; // class pass_late_warn_uninitialized
2389 pass_late_warn_uninitialized::execute (function
*fun
)
2393 vec
<gphi
*> worklist
= vNULL
;
2395 calculate_dominance_info (CDI_DOMINATORS
);
2396 calculate_dominance_info (CDI_POST_DOMINATORS
);
2397 /* Re-do the plain uninitialized variable check, as optimization may have
2398 straightened control flow. Do this first so that we don't accidentally
2399 get a "may be" warning when we'd have seen an "is" warning later. */
2400 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
2402 timevar_push (TV_TREE_UNINIT
);
2404 possibly_undefined_names
= new hash_set
<tree
>;
2405 hash_set
<gphi
*> added_to_worklist
;
2407 /* Initialize worklist */
2408 FOR_EACH_BB_FN (bb
, fun
)
2409 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2411 gphi
*phi
= gsi
.phi ();
2414 n
= gimple_phi_num_args (phi
);
2416 /* Don't look at virtual operands. */
2417 if (virtual_operand_p (gimple_phi_result (phi
)))
2420 for (i
= 0; i
< n
; ++i
)
2422 tree op
= gimple_phi_arg_def (phi
, i
);
2423 if (TREE_CODE (op
) == SSA_NAME
2424 && uninit_undefined_value_p (op
))
2426 worklist
.safe_push (phi
);
2427 added_to_worklist
.add (phi
);
2428 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2430 fprintf (dump_file
, "[WORKLIST]: add to initial list: ");
2431 print_gimple_stmt (dump_file
, phi
, 0, 0);
2438 while (worklist
.length () != 0)
2441 cur_phi
= worklist
.pop ();
2442 warn_uninitialized_phi (cur_phi
, &worklist
, &added_to_worklist
);
2445 worklist
.release ();
2446 delete possibly_undefined_names
;
2447 possibly_undefined_names
= NULL
;
2448 free_dominance_info (CDI_POST_DOMINATORS
);
2449 timevar_pop (TV_TREE_UNINIT
);
2456 make_pass_late_warn_uninitialized (gcc::context
*ctxt
)
2458 return new pass_late_warn_uninitialized (ctxt
);
2463 execute_early_warn_uninitialized (void)
2465 /* Currently, this pass runs always but
2466 execute_late_warn_uninitialized only runs with optimization. With
2467 optimization we want to warn about possible uninitialized as late
2468 as possible, thus don't do it here. However, without
2469 optimization we need to warn here about "may be uninitialized". */
2470 calculate_dominance_info (CDI_POST_DOMINATORS
);
2472 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/!optimize
);
2474 /* Post-dominator information can not be reliably updated. Free it
2477 free_dominance_info (CDI_POST_DOMINATORS
);
2484 const pass_data pass_data_early_warn_uninitialized
=
2486 GIMPLE_PASS
, /* type */
2487 "*early_warn_uninitialized", /* name */
2488 OPTGROUP_NONE
, /* optinfo_flags */
2489 TV_TREE_UNINIT
, /* tv_id */
2490 PROP_ssa
, /* properties_required */
2491 0, /* properties_provided */
2492 0, /* properties_destroyed */
2493 0, /* todo_flags_start */
2494 0, /* todo_flags_finish */
2497 class pass_early_warn_uninitialized
: public gimple_opt_pass
2500 pass_early_warn_uninitialized (gcc::context
*ctxt
)
2501 : gimple_opt_pass (pass_data_early_warn_uninitialized
, ctxt
)
2504 /* opt_pass methods: */
2505 virtual bool gate (function
*) { return gate_warn_uninitialized (); }
2506 virtual unsigned int execute (function
*)
2508 return execute_early_warn_uninitialized ();
2511 }; // class pass_early_warn_uninitialized
2516 make_pass_early_warn_uninitialized (gcc::context
*ctxt
)
2518 return new pass_early_warn_uninitialized (ctxt
);