1 /* Predicate aware uninitialized variable warning.
2 Copyright (C) 2001-2018 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 "tree-pass.h"
29 #include "gimple-pretty-print.h"
30 #include "diagnostic-core.h"
31 #include "fold-const.h"
32 #include "gimple-iterator.h"
38 /* This implements the pass that does predicate aware warning on uses of
39 possibly uninitialized variables. The pass first collects the set of
40 possibly uninitialized SSA names. For each such name, it walks through
41 all its immediate uses. For each immediate use, it rebuilds the condition
42 expression (the predicate) that guards the use. The predicate is then
43 examined to see if the variable is always defined under that same condition.
44 This is done either by pruning the unrealizable paths that lead to the
45 default definitions or by checking if the predicate set that guards the
46 defining paths is a superset of the use predicate. */
48 /* Max PHI args we can handle in pass. */
49 const unsigned max_phi_args
= 32;
51 /* Pointer set of potentially undefined ssa names, i.e.,
52 ssa names that are defined by phi with operands that
53 are not defined or potentially undefined. */
54 static hash_set
<tree
> *possibly_undefined_names
= 0;
56 /* Bit mask handling macros. */
57 #define MASK_SET_BIT(mask, pos) mask |= (1 << pos)
58 #define MASK_TEST_BIT(mask, pos) (mask & (1 << pos))
59 #define MASK_EMPTY(mask) (mask == 0)
61 /* Returns the first bit position (starting from LSB)
62 in mask that is non zero. Returns -1 if the mask is empty. */
64 get_mask_first_set_bit (unsigned mask
)
70 while ((mask
& (1 << pos
)) == 0)
75 #define MASK_FIRST_SET_BIT(mask) get_mask_first_set_bit (mask)
77 /* Return true if T, an SSA_NAME, has an undefined value. */
79 has_undefined_value_p (tree t
)
81 return (ssa_undefined_value_p (t
)
82 || (possibly_undefined_names
83 && possibly_undefined_names
->contains (t
)));
86 /* Like has_undefined_value_p, but don't return true if TREE_NO_WARNING
87 is set on SSA_NAME_VAR. */
90 uninit_undefined_value_p (tree t
)
92 if (!has_undefined_value_p (t
))
94 if (SSA_NAME_VAR (t
) && TREE_NO_WARNING (SSA_NAME_VAR (t
)))
99 /* Emit warnings for uninitialized variables. This is done in two passes.
101 The first pass notices real uses of SSA names with undefined values.
102 Such uses are unconditionally uninitialized, and we can be certain that
103 such a use is a mistake. This pass is run before most optimizations,
104 so that we catch as many as we can.
106 The second pass follows PHI nodes to find uses that are potentially
107 uninitialized. In this case we can't necessarily prove that the use
108 is really uninitialized. This pass is run after most optimizations,
109 so that we thread as many jumps and possible, and delete as much dead
110 code as possible, in order to reduce false positives. We also look
111 again for plain uninitialized variables, since optimization may have
112 changed conditionally uninitialized to unconditionally uninitialized. */
114 /* Emit a warning for EXPR based on variable VAR at the point in the
115 program T, an SSA_NAME, is used being uninitialized. The exact
116 warning text is in MSGID and DATA is the gimple stmt with info about
117 the location in source code. When DATA is a GIMPLE_PHI, PHIARG_IDX
118 gives which argument of the phi node to take the location from. WC
119 is the warning code. */
122 warn_uninit (enum opt_code wc
, tree t
, tree expr
, tree var
,
123 const char *gmsgid
, void *data
, location_t phiarg_loc
)
125 gimple
*context
= (gimple
*) data
;
126 location_t location
, cfun_loc
;
127 expanded_location xloc
, floc
;
129 /* Ignore COMPLEX_EXPR as initializing only a part of a complex
130 turns in a COMPLEX_EXPR with the not initialized part being
131 set to its previous (undefined) value. */
132 if (is_gimple_assign (context
)
133 && gimple_assign_rhs_code (context
) == COMPLEX_EXPR
)
135 if (!has_undefined_value_p (t
))
138 /* Anonymous SSA_NAMEs shouldn't be uninitialized, but ssa_undefined_value_p
139 can return true if the def stmt of anonymous SSA_NAME is COMPLEX_EXPR
140 created for conversion from scalar to complex. Use the underlying var of
141 the COMPLEX_EXPRs real part in that case. See PR71581. */
142 if (expr
== NULL_TREE
144 && SSA_NAME_VAR (t
) == NULL_TREE
145 && is_gimple_assign (SSA_NAME_DEF_STMT (t
))
146 && gimple_assign_rhs_code (SSA_NAME_DEF_STMT (t
)) == COMPLEX_EXPR
)
148 tree v
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (t
));
149 if (TREE_CODE (v
) == SSA_NAME
150 && has_undefined_value_p (v
)
151 && zerop (gimple_assign_rhs2 (SSA_NAME_DEF_STMT (t
))))
153 expr
= SSA_NAME_VAR (v
);
158 if (expr
== NULL_TREE
)
161 /* TREE_NO_WARNING either means we already warned, or the front end
162 wishes to suppress the warning. */
164 && (gimple_no_warning_p (context
)
165 || (gimple_assign_single_p (context
)
166 && TREE_NO_WARNING (gimple_assign_rhs1 (context
)))))
167 || TREE_NO_WARNING (expr
))
170 if (context
!= NULL
&& gimple_has_location (context
))
171 location
= gimple_location (context
);
172 else if (phiarg_loc
!= UNKNOWN_LOCATION
)
173 location
= phiarg_loc
;
175 location
= DECL_SOURCE_LOCATION (var
);
176 location
= linemap_resolve_location (line_table
, location
,
177 LRK_SPELLING_LOCATION
, NULL
);
178 cfun_loc
= DECL_SOURCE_LOCATION (cfun
->decl
);
179 xloc
= expand_location (location
);
180 floc
= expand_location (cfun_loc
);
181 auto_diagnostic_group d
;
182 if (warning_at (location
, wc
, gmsgid
, expr
))
184 TREE_NO_WARNING (expr
) = 1;
186 if (location
== DECL_SOURCE_LOCATION (var
))
188 if (xloc
.file
!= floc
.file
189 || linemap_location_before_p (line_table
, location
, cfun_loc
)
190 || linemap_location_before_p (line_table
, cfun
->function_end_locus
,
192 inform (DECL_SOURCE_LOCATION (var
), "%qD was declared here", var
);
196 struct check_defs_data
198 /* If we found any may-defs besides must-def clobbers. */
202 /* Callback for walk_aliased_vdefs. */
205 check_defs (ao_ref
*ref
, tree vdef
, void *data_
)
207 check_defs_data
*data
= (check_defs_data
*)data_
;
208 gimple
*def_stmt
= SSA_NAME_DEF_STMT (vdef
);
209 /* If this is a clobber then if it is not a kill walk past it. */
210 if (gimple_clobber_p (def_stmt
))
212 if (stmt_kills_ref_p (def_stmt
, ref
))
216 /* Found a may-def on this path. */
217 data
->found_may_defs
= true;
222 warn_uninitialized_vars (bool warn_possibly_uninitialized
)
224 gimple_stmt_iterator gsi
;
226 unsigned int vdef_cnt
= 0;
227 unsigned int oracle_cnt
= 0;
230 FOR_EACH_BB_FN (bb
, cfun
)
232 basic_block succ
= single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
233 bool always_executed
= dominated_by_p (CDI_POST_DOMINATORS
, succ
, bb
);
234 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
236 gimple
*stmt
= gsi_stmt (gsi
);
241 if (is_gimple_debug (stmt
))
244 /* We only do data flow with SSA_NAMEs, so that's all we
246 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, op_iter
, SSA_OP_USE
)
248 /* BIT_INSERT_EXPR first operand should not be considered
249 a use for the purpose of uninit warnings. */
250 if (gassign
*ass
= dyn_cast
<gassign
*> (stmt
))
252 if (gimple_assign_rhs_code (ass
) == BIT_INSERT_EXPR
253 && use_p
->use
== gimple_assign_rhs1_ptr (ass
))
256 use
= USE_FROM_PTR (use_p
);
258 warn_uninit (OPT_Wuninitialized
, use
, SSA_NAME_VAR (use
),
260 "%qD is used uninitialized in this function", stmt
,
262 else if (warn_possibly_uninitialized
)
263 warn_uninit (OPT_Wmaybe_uninitialized
, use
, SSA_NAME_VAR (use
),
265 "%qD may be used uninitialized in this function",
266 stmt
, UNKNOWN_LOCATION
);
269 /* For limiting the alias walk below we count all
270 vdefs in the function. */
271 if (gimple_vdef (stmt
))
274 if (gimple_assign_load_p (stmt
)
275 && gimple_has_location (stmt
))
277 tree rhs
= gimple_assign_rhs1 (stmt
);
278 tree lhs
= gimple_assign_lhs (stmt
);
279 bool has_bit_insert
= false;
280 use_operand_p luse_p
;
281 imm_use_iterator liter
;
283 if (TREE_NO_WARNING (rhs
))
287 ao_ref_init (&ref
, rhs
);
289 /* Do not warn if the base was marked so or this is a
290 hard register var. */
291 tree base
= ao_ref_base (&ref
);
293 && DECL_HARD_REGISTER (base
))
294 || TREE_NO_WARNING (base
))
297 /* Do not warn if the access is fully outside of the
299 poly_int64 decl_size
;
301 && known_size_p (ref
.size
)
302 && ((known_eq (ref
.max_size
, ref
.size
)
303 && known_le (ref
.offset
+ ref
.size
, 0))
304 || (known_ge (ref
.offset
, 0)
306 && poly_int_tree_p (DECL_SIZE (base
), &decl_size
)
307 && known_le (decl_size
, ref
.offset
))))
310 /* Do not warn if the access is then used for a BIT_INSERT_EXPR. */
311 if (TREE_CODE (lhs
) == SSA_NAME
)
312 FOR_EACH_IMM_USE_FAST (luse_p
, liter
, lhs
)
314 gimple
*use_stmt
= USE_STMT (luse_p
);
315 /* BIT_INSERT_EXPR first operand should not be considered
316 a use for the purpose of uninit warnings. */
317 if (gassign
*ass
= dyn_cast
<gassign
*> (use_stmt
))
319 if (gimple_assign_rhs_code (ass
) == BIT_INSERT_EXPR
320 && luse_p
->use
== gimple_assign_rhs1_ptr (ass
))
322 has_bit_insert
= true;
330 /* Limit the walking to a constant number of stmts after
331 we overcommit quadratic behavior for small functions
332 and O(n) behavior. */
333 if (oracle_cnt
> 128 * 128
334 && oracle_cnt
> vdef_cnt
* 2)
336 check_defs_data data
;
337 bool fentry_reached
= false;
338 data
.found_may_defs
= false;
339 use
= gimple_vuse (stmt
);
340 int res
= walk_aliased_vdefs (&ref
, use
,
341 check_defs
, &data
, NULL
,
342 &fentry_reached
, limit
);
349 if (data
.found_may_defs
)
351 /* Do not warn if it can be initialized outside this function.
352 If we did not reach function entry then we found killing
353 clobbers on all paths to entry. */
355 /* ??? We'd like to use ref_may_alias_global_p but that
356 excludes global readonly memory and thus we get bougs
357 warnings from p = cond ? "a" : "b" for example. */
359 || is_global_var (base
)))
362 /* We didn't find any may-defs so on all paths either
363 reached function entry or a killing clobber. */
365 = linemap_resolve_location (line_table
, gimple_location (stmt
),
366 LRK_SPELLING_LOCATION
, NULL
);
369 if (warning_at (location
, OPT_Wuninitialized
,
370 "%qE is used uninitialized in this function",
372 /* ??? This is only effective for decls as in
373 gcc.dg/uninit-B-O0.c. Avoid doing this for
374 maybe-uninit uses as it may hide important
376 TREE_NO_WARNING (rhs
) = 1;
378 else if (warn_possibly_uninitialized
)
379 warning_at (location
, OPT_Wmaybe_uninitialized
,
380 "%qE may be used uninitialized in this function",
389 /* Checks if the operand OPND of PHI is defined by
390 another phi with one operand defined by this PHI,
391 but the rest operands are all defined. If yes,
392 returns true to skip this operand as being
393 redundant. Can be enhanced to be more general. */
396 can_skip_redundant_opnd (tree opnd
, gimple
*phi
)
402 phi_def
= gimple_phi_result (phi
);
403 op_def
= SSA_NAME_DEF_STMT (opnd
);
404 if (gimple_code (op_def
) != GIMPLE_PHI
)
406 n
= gimple_phi_num_args (op_def
);
407 for (i
= 0; i
< n
; ++i
)
409 tree op
= gimple_phi_arg_def (op_def
, i
);
410 if (TREE_CODE (op
) != SSA_NAME
)
412 if (op
!= phi_def
&& uninit_undefined_value_p (op
))
419 /* Returns a bit mask holding the positions of arguments in PHI
420 that have empty (or possibly empty) definitions. */
423 compute_uninit_opnds_pos (gphi
*phi
)
426 unsigned uninit_opnds
= 0;
428 n
= gimple_phi_num_args (phi
);
429 /* Bail out for phi with too many args. */
430 if (n
> max_phi_args
)
433 for (i
= 0; i
< n
; ++i
)
435 tree op
= gimple_phi_arg_def (phi
, i
);
436 if (TREE_CODE (op
) == SSA_NAME
437 && uninit_undefined_value_p (op
)
438 && !can_skip_redundant_opnd (op
, phi
))
440 if (cfun
->has_nonlocal_label
|| cfun
->calls_setjmp
)
442 /* Ignore SSA_NAMEs that appear on abnormal edges
444 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
447 MASK_SET_BIT (uninit_opnds
, i
);
453 /* Find the immediate postdominator PDOM of the specified
454 basic block BLOCK. */
456 static inline basic_block
457 find_pdom (basic_block block
)
459 if (block
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
460 return EXIT_BLOCK_PTR_FOR_FN (cfun
);
463 basic_block bb
= get_immediate_dominator (CDI_POST_DOMINATORS
, block
);
465 return EXIT_BLOCK_PTR_FOR_FN (cfun
);
470 /* Find the immediate DOM of the specified basic block BLOCK. */
472 static inline basic_block
473 find_dom (basic_block block
)
475 if (block
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
476 return ENTRY_BLOCK_PTR_FOR_FN (cfun
);
479 basic_block bb
= get_immediate_dominator (CDI_DOMINATORS
, block
);
481 return ENTRY_BLOCK_PTR_FOR_FN (cfun
);
486 /* Returns true if BB1 is postdominating BB2 and BB1 is
487 not a loop exit bb. The loop exit bb check is simple and does
488 not cover all cases. */
491 is_non_loop_exit_postdominating (basic_block bb1
, basic_block bb2
)
493 if (!dominated_by_p (CDI_POST_DOMINATORS
, bb2
, bb1
))
496 if (single_pred_p (bb1
) && !single_succ_p (bb2
))
502 /* Find the closest postdominator of a specified BB, which is control
505 static inline basic_block
506 find_control_equiv_block (basic_block bb
)
510 pdom
= find_pdom (bb
);
512 /* Skip the postdominating bb that is also loop exit. */
513 if (!is_non_loop_exit_postdominating (pdom
, bb
))
516 if (dominated_by_p (CDI_DOMINATORS
, pdom
, bb
))
522 #define MAX_NUM_CHAINS 8
523 #define MAX_CHAIN_LEN 5
524 #define MAX_POSTDOM_CHECK 8
525 #define MAX_SWITCH_CASES 40
527 /* Computes the control dependence chains (paths of edges)
528 for DEP_BB up to the dominating basic block BB (the head node of a
529 chain should be dominated by it). CD_CHAINS is pointer to an
530 array holding the result chains. CUR_CD_CHAIN is the current
531 chain being computed. *NUM_CHAINS is total number of chains. The
532 function returns true if the information is successfully computed,
533 return false if there is no control dependence or not computed. */
536 compute_control_dep_chain (basic_block bb
, basic_block dep_bb
,
537 vec
<edge
> *cd_chains
,
539 vec
<edge
> *cur_cd_chain
,
545 bool found_cd_chain
= false;
546 size_t cur_chain_len
= 0;
548 if (*num_calls
> PARAM_VALUE (PARAM_UNINIT_CONTROL_DEP_ATTEMPTS
))
552 /* Could use a set instead. */
553 cur_chain_len
= cur_cd_chain
->length ();
554 if (cur_chain_len
> MAX_CHAIN_LEN
)
557 for (i
= 0; i
< cur_chain_len
; i
++)
559 edge e
= (*cur_cd_chain
)[i
];
560 /* Cycle detected. */
565 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
568 int post_dom_check
= 0;
569 if (e
->flags
& (EDGE_FAKE
| EDGE_ABNORMAL
))
573 cur_cd_chain
->safe_push (e
);
574 while (!is_non_loop_exit_postdominating (cd_bb
, bb
))
578 /* Found a direct control dependence. */
579 if (*num_chains
< MAX_NUM_CHAINS
)
581 cd_chains
[*num_chains
] = cur_cd_chain
->copy ();
584 found_cd_chain
= true;
585 /* Check path from next edge. */
589 /* Now check if DEP_BB is indirectly control dependent on BB. */
590 if (compute_control_dep_chain (cd_bb
, dep_bb
, cd_chains
, num_chains
,
591 cur_cd_chain
, num_calls
))
593 found_cd_chain
= true;
597 cd_bb
= find_pdom (cd_bb
);
599 if (cd_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
)
600 || post_dom_check
> MAX_POSTDOM_CHECK
)
603 cur_cd_chain
->pop ();
604 gcc_assert (cur_cd_chain
->length () == cur_chain_len
);
606 gcc_assert (cur_cd_chain
->length () == cur_chain_len
);
608 return found_cd_chain
;
611 /* The type to represent a simple predicate. */
617 enum tree_code cond_code
;
621 /* The type to represent a sequence of predicates grouped
622 with .AND. operation. */
624 typedef vec
<pred_info
, va_heap
, vl_ptr
> pred_chain
;
626 /* The type to represent a sequence of pred_chains grouped
627 with .OR. operation. */
629 typedef vec
<pred_chain
, va_heap
, vl_ptr
> pred_chain_union
;
631 /* Converts the chains of control dependence edges into a set of
632 predicates. A control dependence chain is represented by a vector
633 edges. DEP_CHAINS points to an array of dependence chains.
634 NUM_CHAINS is the size of the chain array. One edge in a dependence
635 chain is mapped to predicate expression represented by pred_info
636 type. One dependence chain is converted to a composite predicate that
637 is the result of AND operation of pred_info mapped to each edge.
638 A composite predicate is presented by a vector of pred_info. On
639 return, *PREDS points to the resulting array of composite predicates.
640 *NUM_PREDS is the number of composite predictes. */
643 convert_control_dep_chain_into_preds (vec
<edge
> *dep_chains
,
645 pred_chain_union
*preds
)
647 bool has_valid_pred
= false;
649 if (num_chains
== 0 || num_chains
>= MAX_NUM_CHAINS
)
652 /* Now convert the control dep chain into a set
654 preds
->reserve (num_chains
);
656 for (i
= 0; i
< num_chains
; i
++)
658 vec
<edge
> one_cd_chain
= dep_chains
[i
];
660 has_valid_pred
= false;
661 pred_chain t_chain
= vNULL
;
662 for (j
= 0; j
< one_cd_chain
.length (); j
++)
665 gimple_stmt_iterator gsi
;
666 basic_block guard_bb
;
672 gsi
= gsi_last_bb (guard_bb
);
673 /* Ignore empty forwarder blocks. */
674 if (empty_block_p (guard_bb
) && single_succ_p (guard_bb
))
676 /* An empty basic block here is likely a PHI, and is not one
677 of the cases we handle below. */
680 has_valid_pred
= false;
683 cond_stmt
= gsi_stmt (gsi
);
684 if (is_gimple_call (cond_stmt
) && EDGE_COUNT (e
->src
->succs
) >= 2)
685 /* Ignore EH edge. Can add assertion on the other edge's flag. */
687 /* Skip if there is essentially one succesor. */
688 if (EDGE_COUNT (e
->src
->succs
) == 2)
694 FOR_EACH_EDGE (e1
, ei1
, e
->src
->succs
)
696 if (EDGE_COUNT (e1
->dest
->succs
) == 0)
705 if (gimple_code (cond_stmt
) == GIMPLE_COND
)
707 one_pred
.pred_lhs
= gimple_cond_lhs (cond_stmt
);
708 one_pred
.pred_rhs
= gimple_cond_rhs (cond_stmt
);
709 one_pred
.cond_code
= gimple_cond_code (cond_stmt
);
710 one_pred
.invert
= !!(e
->flags
& EDGE_FALSE_VALUE
);
711 t_chain
.safe_push (one_pred
);
712 has_valid_pred
= true;
714 else if (gswitch
*gs
= dyn_cast
<gswitch
*> (cond_stmt
))
716 /* Avoid quadratic behavior. */
717 if (gimple_switch_num_labels (gs
) > MAX_SWITCH_CASES
)
719 has_valid_pred
= false;
722 /* Find the case label. */
725 for (idx
= 0; idx
< gimple_switch_num_labels (gs
); ++idx
)
727 tree tl
= gimple_switch_label (gs
, idx
);
728 if (e
->dest
== label_to_block (cfun
, CASE_LABEL (tl
)))
739 /* If more than one label reaches this block or the case
740 label doesn't have a single value (like the default one)
745 && !operand_equal_p (CASE_LOW (l
), CASE_HIGH (l
), 0)))
747 has_valid_pred
= false;
750 one_pred
.pred_lhs
= gimple_switch_index (gs
);
751 one_pred
.pred_rhs
= CASE_LOW (l
);
752 one_pred
.cond_code
= EQ_EXPR
;
753 one_pred
.invert
= false;
754 t_chain
.safe_push (one_pred
);
755 has_valid_pred
= true;
759 has_valid_pred
= false;
767 preds
->safe_push (t_chain
);
769 return has_valid_pred
;
772 /* Computes all control dependence chains for USE_BB. The control
773 dependence chains are then converted to an array of composite
774 predicates pointed to by PREDS. PHI_BB is the basic block of
775 the phi whose result is used in USE_BB. */
778 find_predicates (pred_chain_union
*preds
,
782 size_t num_chains
= 0, i
;
784 vec
<edge
> dep_chains
[MAX_NUM_CHAINS
];
785 auto_vec
<edge
, MAX_CHAIN_LEN
+ 1> cur_chain
;
786 bool has_valid_pred
= false;
787 basic_block cd_root
= 0;
789 /* First find the closest bb that is control equivalent to PHI_BB
790 that also dominates USE_BB. */
792 while (dominated_by_p (CDI_DOMINATORS
, use_bb
, cd_root
))
794 basic_block ctrl_eq_bb
= find_control_equiv_block (cd_root
);
795 if (ctrl_eq_bb
&& dominated_by_p (CDI_DOMINATORS
, use_bb
, ctrl_eq_bb
))
796 cd_root
= ctrl_eq_bb
;
801 compute_control_dep_chain (cd_root
, use_bb
, dep_chains
, &num_chains
,
802 &cur_chain
, &num_calls
);
805 = convert_control_dep_chain_into_preds (dep_chains
, num_chains
, preds
);
806 for (i
= 0; i
< num_chains
; i
++)
807 dep_chains
[i
].release ();
808 return has_valid_pred
;
811 /* Computes the set of incoming edges of PHI that have non empty
812 definitions of a phi chain. The collection will be done
813 recursively on operands that are defined by phis. CD_ROOT
814 is the control dependence root. *EDGES holds the result, and
815 VISITED_PHIS is a pointer set for detecting cycles. */
818 collect_phi_def_edges (gphi
*phi
, basic_block cd_root
,
819 auto_vec
<edge
> *edges
,
820 hash_set
<gimple
*> *visited_phis
)
826 if (visited_phis
->add (phi
))
829 n
= gimple_phi_num_args (phi
);
830 for (i
= 0; i
< n
; i
++)
832 opnd_edge
= gimple_phi_arg_edge (phi
, i
);
833 opnd
= gimple_phi_arg_def (phi
, i
);
835 if (TREE_CODE (opnd
) != SSA_NAME
)
837 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
839 fprintf (dump_file
, "\n[CHECK] Found def edge %d in ", (int) i
);
840 print_gimple_stmt (dump_file
, phi
, 0);
842 edges
->safe_push (opnd_edge
);
846 gimple
*def
= SSA_NAME_DEF_STMT (opnd
);
848 if (gimple_code (def
) == GIMPLE_PHI
849 && dominated_by_p (CDI_DOMINATORS
, gimple_bb (def
), cd_root
))
850 collect_phi_def_edges (as_a
<gphi
*> (def
), cd_root
, edges
,
852 else if (!uninit_undefined_value_p (opnd
))
854 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
856 fprintf (dump_file
, "\n[CHECK] Found def edge %d in ",
858 print_gimple_stmt (dump_file
, phi
, 0);
860 edges
->safe_push (opnd_edge
);
866 /* For each use edge of PHI, computes all control dependence chains.
867 The control dependence chains are then converted to an array of
868 composite predicates pointed to by PREDS. */
871 find_def_preds (pred_chain_union
*preds
, gphi
*phi
)
873 size_t num_chains
= 0, i
, n
;
874 vec
<edge
> dep_chains
[MAX_NUM_CHAINS
];
875 auto_vec
<edge
, MAX_CHAIN_LEN
+ 1> cur_chain
;
876 auto_vec
<edge
> def_edges
;
877 bool has_valid_pred
= false;
878 basic_block phi_bb
, cd_root
= 0;
880 phi_bb
= gimple_bb (phi
);
881 /* First find the closest dominating bb to be
882 the control dependence root. */
883 cd_root
= find_dom (phi_bb
);
887 hash_set
<gimple
*> visited_phis
;
888 collect_phi_def_edges (phi
, cd_root
, &def_edges
, &visited_phis
);
890 n
= def_edges
.length ();
894 for (i
= 0; i
< n
; i
++)
900 opnd_edge
= def_edges
[i
];
901 prev_nc
= num_chains
;
902 compute_control_dep_chain (cd_root
, opnd_edge
->src
, dep_chains
,
903 &num_chains
, &cur_chain
, &num_calls
);
905 /* Now update the newly added chains with
906 the phi operand edge: */
907 if (EDGE_COUNT (opnd_edge
->src
->succs
) > 1)
909 if (prev_nc
== num_chains
&& num_chains
< MAX_NUM_CHAINS
)
910 dep_chains
[num_chains
++] = vNULL
;
911 for (j
= prev_nc
; j
< num_chains
; j
++)
912 dep_chains
[j
].safe_push (opnd_edge
);
917 = convert_control_dep_chain_into_preds (dep_chains
, num_chains
, preds
);
918 for (i
= 0; i
< num_chains
; i
++)
919 dep_chains
[i
].release ();
920 return has_valid_pred
;
923 /* Dump a pred_info. */
926 dump_pred_info (pred_info one_pred
)
929 fprintf (dump_file
, " (.NOT.) ");
930 print_generic_expr (dump_file
, one_pred
.pred_lhs
);
931 fprintf (dump_file
, " %s ", op_symbol_code (one_pred
.cond_code
));
932 print_generic_expr (dump_file
, one_pred
.pred_rhs
);
935 /* Dump a pred_chain. */
938 dump_pred_chain (pred_chain one_pred_chain
)
940 size_t np
= one_pred_chain
.length ();
941 for (size_t j
= 0; j
< np
; j
++)
943 dump_pred_info (one_pred_chain
[j
]);
945 fprintf (dump_file
, " (.AND.) ");
947 fprintf (dump_file
, "\n");
951 /* Dumps the predicates (PREDS) for USESTMT. */
954 dump_predicates (gimple
*usestmt
, pred_chain_union preds
, const char *msg
)
956 fprintf (dump_file
, "%s", msg
);
959 print_gimple_stmt (dump_file
, usestmt
, 0);
960 fprintf (dump_file
, "is guarded by :\n\n");
962 size_t num_preds
= preds
.length ();
963 for (size_t i
= 0; i
< num_preds
; i
++)
965 dump_pred_chain (preds
[i
]);
966 if (i
< num_preds
- 1)
967 fprintf (dump_file
, "(.OR.)\n");
969 fprintf (dump_file
, "\n\n");
973 /* Destroys the predicate set *PREDS. */
976 destroy_predicate_vecs (pred_chain_union
*preds
)
980 size_t n
= preds
->length ();
981 for (i
= 0; i
< n
; i
++)
982 (*preds
)[i
].release ();
986 /* Computes the 'normalized' conditional code with operand
987 swapping and condition inversion. */
989 static enum tree_code
990 get_cmp_code (enum tree_code orig_cmp_code
, bool swap_cond
, bool invert
)
992 enum tree_code tc
= orig_cmp_code
;
995 tc
= swap_tree_comparison (orig_cmp_code
);
997 tc
= invert_tree_comparison (tc
, false);
1014 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
1015 all values in the range satisfies (x CMPC BOUNDARY) == true. */
1018 is_value_included_in (tree val
, tree boundary
, enum tree_code cmpc
)
1020 bool inverted
= false;
1024 /* Only handle integer constant here. */
1025 if (TREE_CODE (val
) != INTEGER_CST
|| TREE_CODE (boundary
) != INTEGER_CST
)
1028 is_unsigned
= TYPE_UNSIGNED (TREE_TYPE (val
));
1030 if (cmpc
== GE_EXPR
|| cmpc
== GT_EXPR
|| cmpc
== NE_EXPR
)
1032 cmpc
= invert_tree_comparison (cmpc
, false);
1038 if (cmpc
== EQ_EXPR
)
1039 result
= tree_int_cst_equal (val
, boundary
);
1040 else if (cmpc
== LT_EXPR
)
1041 result
= tree_int_cst_lt (val
, boundary
);
1044 gcc_assert (cmpc
== LE_EXPR
);
1045 result
= tree_int_cst_le (val
, boundary
);
1050 if (cmpc
== EQ_EXPR
)
1051 result
= tree_int_cst_equal (val
, boundary
);
1052 else if (cmpc
== LT_EXPR
)
1053 result
= tree_int_cst_lt (val
, boundary
);
1056 gcc_assert (cmpc
== LE_EXPR
);
1057 result
= (tree_int_cst_equal (val
, boundary
)
1058 || tree_int_cst_lt (val
, boundary
));
1068 /* Returns true if PRED is common among all the predicate
1069 chains (PREDS) (and therefore can be factored out).
1070 NUM_PRED_CHAIN is the size of array PREDS. */
1073 find_matching_predicate_in_rest_chains (pred_info pred
,
1074 pred_chain_union preds
,
1075 size_t num_pred_chains
)
1080 if (num_pred_chains
== 1)
1083 for (i
= 1; i
< num_pred_chains
; i
++)
1086 pred_chain one_chain
= preds
[i
];
1087 n
= one_chain
.length ();
1088 for (j
= 0; j
< n
; j
++)
1090 pred_info pred2
= one_chain
[j
];
1091 /* Can relax the condition comparison to not
1092 use address comparison. However, the most common
1093 case is that multiple control dependent paths share
1094 a common path prefix, so address comparison should
1097 if (operand_equal_p (pred2
.pred_lhs
, pred
.pred_lhs
, 0)
1098 && operand_equal_p (pred2
.pred_rhs
, pred
.pred_rhs
, 0)
1099 && pred2
.invert
== pred
.invert
)
1111 /* Forward declaration. */
1112 static bool is_use_properly_guarded (gimple
*use_stmt
,
1115 unsigned uninit_opnds
,
1116 pred_chain_union
*def_preds
,
1117 hash_set
<gphi
*> *visited_phis
);
1119 /* Returns true if all uninitialized opnds are pruned. Returns false
1120 otherwise. PHI is the phi node with uninitialized operands,
1121 UNINIT_OPNDS is the bitmap of the uninitialize operand positions,
1122 FLAG_DEF is the statement defining the flag guarding the use of the
1123 PHI output, BOUNDARY_CST is the const value used in the predicate
1124 associated with the flag, CMP_CODE is the comparison code used in
1125 the predicate, VISITED_PHIS is the pointer set of phis visited, and
1126 VISITED_FLAG_PHIS is the pointer to the pointer set of flag definitions
1132 flag_1 = phi <0, 1> // (1)
1133 var_1 = phi <undef, some_val>
1137 flag_2 = phi <0, flag_1, flag_1> // (2)
1138 var_2 = phi <undef, var_1, var_1>
1145 Because some flag arg in (1) is not constant, if we do not look into the
1146 flag phis recursively, it is conservatively treated as unknown and var_1
1147 is thought to be flowed into use at (3). Since var_1 is potentially
1148 uninitialized a false warning will be emitted.
1149 Checking recursively into (1), the compiler can find out that only some_val
1150 (which is defined) can flow into (3) which is OK. */
1153 prune_uninit_phi_opnds (gphi
*phi
, unsigned uninit_opnds
, gphi
*flag_def
,
1154 tree boundary_cst
, enum tree_code cmp_code
,
1155 hash_set
<gphi
*> *visited_phis
,
1156 bitmap
*visited_flag_phis
)
1160 for (i
= 0; i
< MIN (max_phi_args
, gimple_phi_num_args (flag_def
)); i
++)
1164 if (!MASK_TEST_BIT (uninit_opnds
, i
))
1167 flag_arg
= gimple_phi_arg_def (flag_def
, i
);
1168 if (!is_gimple_constant (flag_arg
))
1170 gphi
*flag_arg_def
, *phi_arg_def
;
1172 unsigned uninit_opnds_arg_phi
;
1174 if (TREE_CODE (flag_arg
) != SSA_NAME
)
1176 flag_arg_def
= dyn_cast
<gphi
*> (SSA_NAME_DEF_STMT (flag_arg
));
1180 phi_arg
= gimple_phi_arg_def (phi
, i
);
1181 if (TREE_CODE (phi_arg
) != SSA_NAME
)
1184 phi_arg_def
= dyn_cast
<gphi
*> (SSA_NAME_DEF_STMT (phi_arg
));
1188 if (gimple_bb (phi_arg_def
) != gimple_bb (flag_arg_def
))
1191 if (!*visited_flag_phis
)
1192 *visited_flag_phis
= BITMAP_ALLOC (NULL
);
1194 tree phi_result
= gimple_phi_result (flag_arg_def
);
1195 if (bitmap_bit_p (*visited_flag_phis
, SSA_NAME_VERSION (phi_result
)))
1198 bitmap_set_bit (*visited_flag_phis
,
1199 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def
)));
1201 /* Now recursively prune the uninitialized phi args. */
1202 uninit_opnds_arg_phi
= compute_uninit_opnds_pos (phi_arg_def
);
1203 if (!prune_uninit_phi_opnds
1204 (phi_arg_def
, uninit_opnds_arg_phi
, flag_arg_def
, boundary_cst
,
1205 cmp_code
, visited_phis
, visited_flag_phis
))
1208 phi_result
= gimple_phi_result (flag_arg_def
);
1209 bitmap_clear_bit (*visited_flag_phis
, SSA_NAME_VERSION (phi_result
));
1213 /* Now check if the constant is in the guarded range. */
1214 if (is_value_included_in (flag_arg
, boundary_cst
, cmp_code
))
1219 /* Now that we know that this undefined edge is not
1220 pruned. If the operand is defined by another phi,
1221 we can further prune the incoming edges of that
1222 phi by checking the predicates of this operands. */
1224 opnd
= gimple_phi_arg_def (phi
, i
);
1225 opnd_def
= SSA_NAME_DEF_STMT (opnd
);
1226 if (gphi
*opnd_def_phi
= dyn_cast
<gphi
*> (opnd_def
))
1229 unsigned uninit_opnds2
= compute_uninit_opnds_pos (opnd_def_phi
);
1230 if (!MASK_EMPTY (uninit_opnds2
))
1232 pred_chain_union def_preds
= vNULL
;
1234 opnd_edge
= gimple_phi_arg_edge (phi
, i
);
1235 ok
= is_use_properly_guarded (phi
,
1241 destroy_predicate_vecs (&def_preds
);
1254 /* A helper function that determines if the predicate set
1255 of the use is not overlapping with that of the uninit paths.
1256 The most common senario of guarded use is in Example 1:
1269 The real world examples are usually more complicated, but similar
1270 and usually result from inlining:
1272 bool init_func (int * x)
1284 if (!init_func (&x))
1291 Another possible use scenario is in the following trivial example:
1303 Predicate analysis needs to compute the composite predicate:
1305 1) 'x' use predicate: (n > 0) .AND. (m < 2)
1306 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
1307 (the predicate chain for phi operand defs can be computed
1308 starting from a bb that is control equivalent to the phi's
1309 bb and is dominating the operand def.)
1311 and check overlapping:
1312 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
1315 This implementation provides framework that can handle
1316 scenarios. (Note that many simple cases are handled properly
1317 without the predicate analysis -- this is due to jump threading
1318 transformation which eliminates the merge point thus makes
1319 path sensitive analysis unnecessary.)
1321 PHI is the phi node whose incoming (undefined) paths need to be
1322 pruned, and UNINIT_OPNDS is the bitmap holding uninit operand
1323 positions. VISITED_PHIS is the pointer set of phi stmts being
1327 use_pred_not_overlap_with_undef_path_pred (pred_chain_union preds
,
1328 gphi
*phi
, unsigned uninit_opnds
,
1329 hash_set
<gphi
*> *visited_phis
)
1332 gimple
*flag_def
= 0;
1333 tree boundary_cst
= 0;
1334 enum tree_code cmp_code
;
1335 bool swap_cond
= false;
1336 bool invert
= false;
1337 pred_chain the_pred_chain
= vNULL
;
1338 bitmap visited_flag_phis
= NULL
;
1339 bool all_pruned
= false;
1340 size_t num_preds
= preds
.length ();
1342 gcc_assert (num_preds
> 0);
1343 /* Find within the common prefix of multiple predicate chains
1344 a predicate that is a comparison of a flag variable against
1346 the_pred_chain
= preds
[0];
1347 n
= the_pred_chain
.length ();
1348 for (i
= 0; i
< n
; i
++)
1350 tree cond_lhs
, cond_rhs
, flag
= 0;
1352 pred_info the_pred
= the_pred_chain
[i
];
1354 invert
= the_pred
.invert
;
1355 cond_lhs
= the_pred
.pred_lhs
;
1356 cond_rhs
= the_pred
.pred_rhs
;
1357 cmp_code
= the_pred
.cond_code
;
1359 if (cond_lhs
!= NULL_TREE
&& TREE_CODE (cond_lhs
) == SSA_NAME
1360 && cond_rhs
!= NULL_TREE
&& is_gimple_constant (cond_rhs
))
1362 boundary_cst
= cond_rhs
;
1365 else if (cond_rhs
!= NULL_TREE
&& TREE_CODE (cond_rhs
) == SSA_NAME
1366 && cond_lhs
!= NULL_TREE
&& is_gimple_constant (cond_lhs
))
1368 boundary_cst
= cond_lhs
;
1376 flag_def
= SSA_NAME_DEF_STMT (flag
);
1381 if ((gimple_code (flag_def
) == GIMPLE_PHI
)
1382 && (gimple_bb (flag_def
) == gimple_bb (phi
))
1383 && find_matching_predicate_in_rest_chains (the_pred
, preds
,
1393 /* Now check all the uninit incoming edge has a constant flag value
1394 that is in conflict with the use guard/predicate. */
1395 cmp_code
= get_cmp_code (cmp_code
, swap_cond
, invert
);
1397 if (cmp_code
== ERROR_MARK
)
1400 all_pruned
= prune_uninit_phi_opnds
1401 (phi
, uninit_opnds
, as_a
<gphi
*> (flag_def
), boundary_cst
, cmp_code
,
1402 visited_phis
, &visited_flag_phis
);
1404 if (visited_flag_phis
)
1405 BITMAP_FREE (visited_flag_phis
);
1410 /* The helper function returns true if two predicates X1 and X2
1411 are equivalent. It assumes the expressions have already
1412 properly re-associated. */
1415 pred_equal_p (pred_info x1
, pred_info x2
)
1417 enum tree_code c1
, c2
;
1418 if (!operand_equal_p (x1
.pred_lhs
, x2
.pred_lhs
, 0)
1419 || !operand_equal_p (x1
.pred_rhs
, x2
.pred_rhs
, 0))
1423 if (x1
.invert
!= x2
.invert
1424 && TREE_CODE_CLASS (x2
.cond_code
) == tcc_comparison
)
1425 c2
= invert_tree_comparison (x2
.cond_code
, false);
1432 /* Returns true if the predication is testing !=. */
1435 is_neq_relop_p (pred_info pred
)
1438 return ((pred
.cond_code
== NE_EXPR
&& !pred
.invert
)
1439 || (pred
.cond_code
== EQ_EXPR
&& pred
.invert
));
1442 /* Returns true if pred is of the form X != 0. */
1445 is_neq_zero_form_p (pred_info pred
)
1447 if (!is_neq_relop_p (pred
) || !integer_zerop (pred
.pred_rhs
)
1448 || TREE_CODE (pred
.pred_lhs
) != SSA_NAME
)
1453 /* The helper function returns true if two predicates X1
1454 is equivalent to X2 != 0. */
1457 pred_expr_equal_p (pred_info x1
, tree x2
)
1459 if (!is_neq_zero_form_p (x1
))
1462 return operand_equal_p (x1
.pred_lhs
, x2
, 0);
1465 /* Returns true of the domain of single predicate expression
1466 EXPR1 is a subset of that of EXPR2. Returns false if it
1467 can not be proved. */
1470 is_pred_expr_subset_of (pred_info expr1
, pred_info expr2
)
1472 enum tree_code code1
, code2
;
1474 if (pred_equal_p (expr1
, expr2
))
1477 if ((TREE_CODE (expr1
.pred_rhs
) != INTEGER_CST
)
1478 || (TREE_CODE (expr2
.pred_rhs
) != INTEGER_CST
))
1481 if (!operand_equal_p (expr1
.pred_lhs
, expr2
.pred_lhs
, 0))
1484 code1
= expr1
.cond_code
;
1486 code1
= invert_tree_comparison (code1
, false);
1487 code2
= expr2
.cond_code
;
1489 code2
= invert_tree_comparison (code2
, false);
1491 if ((code1
== EQ_EXPR
|| code1
== BIT_AND_EXPR
) && code2
== BIT_AND_EXPR
)
1492 return (wi::to_wide (expr1
.pred_rhs
)
1493 == (wi::to_wide (expr1
.pred_rhs
) & wi::to_wide (expr2
.pred_rhs
)));
1495 if (code1
!= code2
&& code2
!= NE_EXPR
)
1498 if (is_value_included_in (expr1
.pred_rhs
, expr2
.pred_rhs
, code2
))
1504 /* Returns true if the domain of PRED1 is a subset
1505 of that of PRED2. Returns false if it can not be proved so. */
1508 is_pred_chain_subset_of (pred_chain pred1
, pred_chain pred2
)
1510 size_t np1
, np2
, i1
, i2
;
1512 np1
= pred1
.length ();
1513 np2
= pred2
.length ();
1515 for (i2
= 0; i2
< np2
; i2
++)
1518 pred_info info2
= pred2
[i2
];
1519 for (i1
= 0; i1
< np1
; i1
++)
1521 pred_info info1
= pred1
[i1
];
1522 if (is_pred_expr_subset_of (info1
, info2
))
1534 /* Returns true if the domain defined by
1535 one pred chain ONE_PRED is a subset of the domain
1536 of *PREDS. It returns false if ONE_PRED's domain is
1537 not a subset of any of the sub-domains of PREDS
1538 (corresponding to each individual chains in it), even
1539 though it may be still be a subset of whole domain
1540 of PREDS which is the union (ORed) of all its subdomains.
1541 In other words, the result is conservative. */
1544 is_included_in (pred_chain one_pred
, pred_chain_union preds
)
1547 size_t n
= preds
.length ();
1549 for (i
= 0; i
< n
; i
++)
1551 if (is_pred_chain_subset_of (one_pred
, preds
[i
]))
1558 /* Compares two predicate sets PREDS1 and PREDS2 and returns
1559 true if the domain defined by PREDS1 is a superset
1560 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1561 PREDS2 respectively. The implementation chooses not to build
1562 generic trees (and relying on the folding capability of the
1563 compiler), but instead performs brute force comparison of
1564 individual predicate chains (won't be a compile time problem
1565 as the chains are pretty short). When the function returns
1566 false, it does not necessarily mean *PREDS1 is not a superset
1567 of *PREDS2, but mean it may not be so since the analysis can
1568 not prove it. In such cases, false warnings may still be
1572 is_superset_of (pred_chain_union preds1
, pred_chain_union preds2
)
1575 pred_chain one_pred_chain
= vNULL
;
1577 n2
= preds2
.length ();
1579 for (i
= 0; i
< n2
; i
++)
1581 one_pred_chain
= preds2
[i
];
1582 if (!is_included_in (one_pred_chain
, preds1
))
1589 /* Returns true if TC is AND or OR. */
1592 is_and_or_or_p (enum tree_code tc
, tree type
)
1594 return (tc
== BIT_IOR_EXPR
1595 || (tc
== BIT_AND_EXPR
1596 && (type
== 0 || TREE_CODE (type
) == BOOLEAN_TYPE
)));
1599 /* Returns true if X1 is the negate of X2. */
1602 pred_neg_p (pred_info x1
, pred_info x2
)
1604 enum tree_code c1
, c2
;
1605 if (!operand_equal_p (x1
.pred_lhs
, x2
.pred_lhs
, 0)
1606 || !operand_equal_p (x1
.pred_rhs
, x2
.pred_rhs
, 0))
1610 if (x1
.invert
== x2
.invert
)
1611 c2
= invert_tree_comparison (x2
.cond_code
, false);
1618 /* 1) ((x IOR y) != 0) AND (x != 0) is equivalent to (x != 0);
1619 2) (X AND Y) OR (!X AND Y) is equivalent to Y;
1620 3) X OR (!X AND Y) is equivalent to (X OR Y);
1621 4) ((x IAND y) != 0) || (x != 0 AND y != 0)) is equivalent to
1623 5) (X AND Y) OR (!X AND Z) OR (!Y AND Z) is equivalent to
1626 PREDS is the predicate chains, and N is the number of chains. */
1628 /* Helper function to implement rule 1 above. ONE_CHAIN is
1629 the AND predication to be simplified. */
1632 simplify_pred (pred_chain
*one_chain
)
1635 bool simplified
= false;
1636 pred_chain s_chain
= vNULL
;
1638 n
= one_chain
->length ();
1640 for (i
= 0; i
< n
; i
++)
1642 pred_info
*a_pred
= &(*one_chain
)[i
];
1644 if (!a_pred
->pred_lhs
)
1646 if (!is_neq_zero_form_p (*a_pred
))
1649 gimple
*def_stmt
= SSA_NAME_DEF_STMT (a_pred
->pred_lhs
);
1650 if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
1652 if (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
)
1654 for (j
= 0; j
< n
; j
++)
1656 pred_info
*b_pred
= &(*one_chain
)[j
];
1658 if (!b_pred
->pred_lhs
)
1660 if (!is_neq_zero_form_p (*b_pred
))
1663 if (pred_expr_equal_p (*b_pred
, gimple_assign_rhs1 (def_stmt
))
1664 || pred_expr_equal_p (*b_pred
, gimple_assign_rhs2 (def_stmt
)))
1666 /* Mark a_pred for removal. */
1667 a_pred
->pred_lhs
= NULL
;
1668 a_pred
->pred_rhs
= NULL
;
1679 for (i
= 0; i
< n
; i
++)
1681 pred_info
*a_pred
= &(*one_chain
)[i
];
1682 if (!a_pred
->pred_lhs
)
1684 s_chain
.safe_push (*a_pred
);
1687 one_chain
->release ();
1688 *one_chain
= s_chain
;
1691 /* The helper function implements the rule 2 for the
1694 2) (X AND Y) OR (!X AND Y) is equivalent to Y. */
1697 simplify_preds_2 (pred_chain_union
*preds
)
1700 bool simplified
= false;
1701 pred_chain_union s_preds
= vNULL
;
1703 /* (X AND Y) OR (!X AND Y) is equivalent to Y.
1704 (X AND Y) OR (X AND !Y) is equivalent to X. */
1706 n
= preds
->length ();
1707 for (i
= 0; i
< n
; i
++)
1710 pred_chain
*a_chain
= &(*preds
)[i
];
1712 if (a_chain
->length () != 2)
1718 for (j
= 0; j
< n
; j
++)
1720 pred_chain
*b_chain
;
1726 b_chain
= &(*preds
)[j
];
1727 if (b_chain
->length () != 2)
1733 if (pred_equal_p (x
, x2
) && pred_neg_p (y
, y2
))
1736 a_chain
->release ();
1737 b_chain
->release ();
1738 b_chain
->safe_push (x
);
1742 if (pred_neg_p (x
, x2
) && pred_equal_p (y
, y2
))
1745 a_chain
->release ();
1746 b_chain
->release ();
1747 b_chain
->safe_push (y
);
1753 /* Now clean up the chain. */
1756 for (i
= 0; i
< n
; i
++)
1758 if ((*preds
)[i
].is_empty ())
1760 s_preds
.safe_push ((*preds
)[i
]);
1770 /* The helper function implements the rule 2 for the
1773 3) x OR (!x AND y) is equivalent to x OR y. */
1776 simplify_preds_3 (pred_chain_union
*preds
)
1779 bool simplified
= false;
1781 /* Now iteratively simplify X OR (!X AND Z ..)
1782 into X OR (Z ...). */
1784 n
= preds
->length ();
1788 for (i
= 0; i
< n
; i
++)
1791 pred_chain
*a_chain
= &(*preds
)[i
];
1793 if (a_chain
->length () != 1)
1798 for (j
= 0; j
< n
; j
++)
1800 pred_chain
*b_chain
;
1807 b_chain
= &(*preds
)[j
];
1808 if (b_chain
->length () < 2)
1811 for (k
= 0; k
< b_chain
->length (); k
++)
1814 if (pred_neg_p (x
, x2
))
1816 b_chain
->unordered_remove (k
);
1826 /* The helper function implements the rule 4 for the
1829 2) ((x AND y) != 0) OR (x != 0 AND y != 0) is equivalent to
1830 (x != 0 ANd y != 0). */
1833 simplify_preds_4 (pred_chain_union
*preds
)
1836 bool simplified
= false;
1837 pred_chain_union s_preds
= vNULL
;
1840 n
= preds
->length ();
1841 for (i
= 0; i
< n
; i
++)
1844 pred_chain
*a_chain
= &(*preds
)[i
];
1846 if (a_chain
->length () != 1)
1851 if (!is_neq_zero_form_p (z
))
1854 def_stmt
= SSA_NAME_DEF_STMT (z
.pred_lhs
);
1855 if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
1858 if (gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
1861 for (j
= 0; j
< n
; j
++)
1863 pred_chain
*b_chain
;
1869 b_chain
= &(*preds
)[j
];
1870 if (b_chain
->length () != 2)
1875 if (!is_neq_zero_form_p (x2
) || !is_neq_zero_form_p (y2
))
1878 if ((pred_expr_equal_p (x2
, gimple_assign_rhs1 (def_stmt
))
1879 && pred_expr_equal_p (y2
, gimple_assign_rhs2 (def_stmt
)))
1880 || (pred_expr_equal_p (x2
, gimple_assign_rhs2 (def_stmt
))
1881 && pred_expr_equal_p (y2
, gimple_assign_rhs1 (def_stmt
))))
1884 a_chain
->release ();
1890 /* Now clean up the chain. */
1893 for (i
= 0; i
< n
; i
++)
1895 if ((*preds
)[i
].is_empty ())
1897 s_preds
.safe_push ((*preds
)[i
]);
1908 /* This function simplifies predicates in PREDS. */
1911 simplify_preds (pred_chain_union
*preds
, gimple
*use_or_def
, bool is_use
)
1914 bool changed
= false;
1916 if (dump_file
&& dump_flags
& TDF_DETAILS
)
1918 fprintf (dump_file
, "[BEFORE SIMPLICATION -- ");
1919 dump_predicates (use_or_def
, *preds
, is_use
? "[USE]:\n" : "[DEF]:\n");
1922 for (i
= 0; i
< preds
->length (); i
++)
1923 simplify_pred (&(*preds
)[i
]);
1925 n
= preds
->length ();
1932 if (simplify_preds_2 (preds
))
1935 /* Now iteratively simplify X OR (!X AND Z ..)
1936 into X OR (Z ...). */
1937 if (simplify_preds_3 (preds
))
1940 if (simplify_preds_4 (preds
))
1948 /* This is a helper function which attempts to normalize predicate chains
1949 by following UD chains. It basically builds up a big tree of either IOR
1950 operations or AND operations, and convert the IOR tree into a
1951 pred_chain_union or BIT_AND tree into a pred_chain.
1961 then _t != 0 will be normalized into a pred_chain_union
1963 (_2 RELOP1 _1) OR (_5 RELOP2 _4) OR (_8 RELOP3 _7) OR (_0 != 0)
1973 then _t != 0 will be normalized into a pred_chain:
1974 (_2 RELOP1 _1) AND (_5 RELOP2 _4) AND (_8 RELOP3 _7) AND (_0 != 0)
1978 /* This is a helper function that stores a PRED into NORM_PREDS. */
1981 push_pred (pred_chain_union
*norm_preds
, pred_info pred
)
1983 pred_chain pred_chain
= vNULL
;
1984 pred_chain
.safe_push (pred
);
1985 norm_preds
->safe_push (pred_chain
);
1988 /* A helper function that creates a predicate of the form
1989 OP != 0 and push it WORK_LIST. */
1992 push_to_worklist (tree op
, vec
<pred_info
, va_heap
, vl_ptr
> *work_list
,
1993 hash_set
<tree
> *mark_set
)
1995 if (mark_set
->contains (op
))
2000 arg_pred
.pred_lhs
= op
;
2001 arg_pred
.pred_rhs
= integer_zero_node
;
2002 arg_pred
.cond_code
= NE_EXPR
;
2003 arg_pred
.invert
= false;
2004 work_list
->safe_push (arg_pred
);
2007 /* A helper that generates a pred_info from a gimple assignment
2008 CMP_ASSIGN with comparison rhs. */
2011 get_pred_info_from_cmp (gimple
*cmp_assign
)
2014 n_pred
.pred_lhs
= gimple_assign_rhs1 (cmp_assign
);
2015 n_pred
.pred_rhs
= gimple_assign_rhs2 (cmp_assign
);
2016 n_pred
.cond_code
= gimple_assign_rhs_code (cmp_assign
);
2017 n_pred
.invert
= false;
2021 /* Returns true if the PHI is a degenerated phi with
2022 all args with the same value (relop). In that case, *PRED
2023 will be updated to that value. */
2026 is_degenerated_phi (gimple
*phi
, pred_info
*pred_p
)
2033 n
= gimple_phi_num_args (phi
);
2034 op0
= gimple_phi_arg_def (phi
, 0);
2036 if (TREE_CODE (op0
) != SSA_NAME
)
2039 def0
= SSA_NAME_DEF_STMT (op0
);
2040 if (gimple_code (def0
) != GIMPLE_ASSIGN
)
2042 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def0
)) != tcc_comparison
)
2044 pred0
= get_pred_info_from_cmp (def0
);
2046 for (i
= 1; i
< n
; ++i
)
2050 tree op
= gimple_phi_arg_def (phi
, i
);
2052 if (TREE_CODE (op
) != SSA_NAME
)
2055 def
= SSA_NAME_DEF_STMT (op
);
2056 if (gimple_code (def
) != GIMPLE_ASSIGN
)
2058 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def
)) != tcc_comparison
)
2060 pred
= get_pred_info_from_cmp (def
);
2061 if (!pred_equal_p (pred
, pred0
))
2069 /* Normalize one predicate PRED
2070 1) if PRED can no longer be normlized, put it into NORM_PREDS.
2071 2) otherwise if PRED is of the form x != 0, follow x's definition
2072 and put normalized predicates into WORK_LIST. */
2075 normalize_one_pred_1 (pred_chain_union
*norm_preds
,
2076 pred_chain
*norm_chain
,
2078 enum tree_code and_or_code
,
2079 vec
<pred_info
, va_heap
, vl_ptr
> *work_list
,
2080 hash_set
<tree
> *mark_set
)
2082 if (!is_neq_zero_form_p (pred
))
2084 if (and_or_code
== BIT_IOR_EXPR
)
2085 push_pred (norm_preds
, pred
);
2087 norm_chain
->safe_push (pred
);
2091 gimple
*def_stmt
= SSA_NAME_DEF_STMT (pred
.pred_lhs
);
2093 if (gimple_code (def_stmt
) == GIMPLE_PHI
2094 && is_degenerated_phi (def_stmt
, &pred
))
2095 work_list
->safe_push (pred
);
2096 else if (gimple_code (def_stmt
) == GIMPLE_PHI
&& and_or_code
== BIT_IOR_EXPR
)
2099 n
= gimple_phi_num_args (def_stmt
);
2101 /* If we see non zero constant, we should punt. The predicate
2102 * should be one guarding the phi edge. */
2103 for (i
= 0; i
< n
; ++i
)
2105 tree op
= gimple_phi_arg_def (def_stmt
, i
);
2106 if (TREE_CODE (op
) == INTEGER_CST
&& !integer_zerop (op
))
2108 push_pred (norm_preds
, pred
);
2113 for (i
= 0; i
< n
; ++i
)
2115 tree op
= gimple_phi_arg_def (def_stmt
, i
);
2116 if (integer_zerop (op
))
2119 push_to_worklist (op
, work_list
, mark_set
);
2122 else if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
2124 if (and_or_code
== BIT_IOR_EXPR
)
2125 push_pred (norm_preds
, pred
);
2127 norm_chain
->safe_push (pred
);
2129 else if (gimple_assign_rhs_code (def_stmt
) == and_or_code
)
2131 /* Avoid splitting up bit manipulations like x & 3 or y | 1. */
2132 if (is_gimple_min_invariant (gimple_assign_rhs2 (def_stmt
)))
2134 /* But treat x & 3 as condition. */
2135 if (and_or_code
== BIT_AND_EXPR
)
2138 n_pred
.pred_lhs
= gimple_assign_rhs1 (def_stmt
);
2139 n_pred
.pred_rhs
= gimple_assign_rhs2 (def_stmt
);
2140 n_pred
.cond_code
= and_or_code
;
2141 n_pred
.invert
= false;
2142 norm_chain
->safe_push (n_pred
);
2147 push_to_worklist (gimple_assign_rhs1 (def_stmt
), work_list
, mark_set
);
2148 push_to_worklist (gimple_assign_rhs2 (def_stmt
), work_list
, mark_set
);
2151 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
))
2154 pred_info n_pred
= get_pred_info_from_cmp (def_stmt
);
2155 if (and_or_code
== BIT_IOR_EXPR
)
2156 push_pred (norm_preds
, n_pred
);
2158 norm_chain
->safe_push (n_pred
);
2162 if (and_or_code
== BIT_IOR_EXPR
)
2163 push_pred (norm_preds
, pred
);
2165 norm_chain
->safe_push (pred
);
2169 /* Normalize PRED and store the normalized predicates into NORM_PREDS. */
2172 normalize_one_pred (pred_chain_union
*norm_preds
, pred_info pred
)
2174 vec
<pred_info
, va_heap
, vl_ptr
> work_list
= vNULL
;
2175 enum tree_code and_or_code
= ERROR_MARK
;
2176 pred_chain norm_chain
= vNULL
;
2178 if (!is_neq_zero_form_p (pred
))
2180 push_pred (norm_preds
, pred
);
2184 gimple
*def_stmt
= SSA_NAME_DEF_STMT (pred
.pred_lhs
);
2185 if (gimple_code (def_stmt
) == GIMPLE_ASSIGN
)
2186 and_or_code
= gimple_assign_rhs_code (def_stmt
);
2187 if (and_or_code
!= BIT_IOR_EXPR
&& and_or_code
!= BIT_AND_EXPR
)
2189 if (TREE_CODE_CLASS (and_or_code
) == tcc_comparison
)
2191 pred_info n_pred
= get_pred_info_from_cmp (def_stmt
);
2192 push_pred (norm_preds
, n_pred
);
2195 push_pred (norm_preds
, pred
);
2199 work_list
.safe_push (pred
);
2200 hash_set
<tree
> mark_set
;
2202 while (!work_list
.is_empty ())
2204 pred_info a_pred
= work_list
.pop ();
2205 normalize_one_pred_1 (norm_preds
, &norm_chain
, a_pred
, and_or_code
,
2206 &work_list
, &mark_set
);
2208 if (and_or_code
== BIT_AND_EXPR
)
2209 norm_preds
->safe_push (norm_chain
);
2211 work_list
.release ();
2215 normalize_one_pred_chain (pred_chain_union
*norm_preds
, pred_chain one_chain
)
2217 vec
<pred_info
, va_heap
, vl_ptr
> work_list
= vNULL
;
2218 hash_set
<tree
> mark_set
;
2219 pred_chain norm_chain
= vNULL
;
2222 for (i
= 0; i
< one_chain
.length (); i
++)
2224 work_list
.safe_push (one_chain
[i
]);
2225 mark_set
.add (one_chain
[i
].pred_lhs
);
2228 while (!work_list
.is_empty ())
2230 pred_info a_pred
= work_list
.pop ();
2231 normalize_one_pred_1 (0, &norm_chain
, a_pred
, BIT_AND_EXPR
, &work_list
,
2235 norm_preds
->safe_push (norm_chain
);
2236 work_list
.release ();
2239 /* Normalize predicate chains PREDS and returns the normalized one. */
2241 static pred_chain_union
2242 normalize_preds (pred_chain_union preds
, gimple
*use_or_def
, bool is_use
)
2244 pred_chain_union norm_preds
= vNULL
;
2245 size_t n
= preds
.length ();
2248 if (dump_file
&& dump_flags
& TDF_DETAILS
)
2250 fprintf (dump_file
, "[BEFORE NORMALIZATION --");
2251 dump_predicates (use_or_def
, preds
, is_use
? "[USE]:\n" : "[DEF]:\n");
2254 for (i
= 0; i
< n
; i
++)
2256 if (preds
[i
].length () != 1)
2257 normalize_one_pred_chain (&norm_preds
, preds
[i
]);
2260 normalize_one_pred (&norm_preds
, preds
[i
][0]);
2261 preds
[i
].release ();
2267 fprintf (dump_file
, "[AFTER NORMALIZATION -- ");
2268 dump_predicates (use_or_def
, norm_preds
,
2269 is_use
? "[USE]:\n" : "[DEF]:\n");
2272 destroy_predicate_vecs (&preds
);
2276 /* Return TRUE if PREDICATE can be invalidated by any individual
2277 predicate in USE_GUARD. */
2280 can_one_predicate_be_invalidated_p (pred_info predicate
,
2281 pred_chain use_guard
)
2283 if (dump_file
&& dump_flags
& TDF_DETAILS
)
2285 fprintf (dump_file
, "Testing if this predicate: ");
2286 dump_pred_info (predicate
);
2287 fprintf (dump_file
, "\n...can be invalidated by a USE guard of: ");
2288 dump_pred_chain (use_guard
);
2290 for (size_t i
= 0; i
< use_guard
.length (); ++i
)
2292 /* NOTE: This is a very simple check, and only understands an
2293 exact opposite. So, [i == 0] is currently only invalidated
2294 by [.NOT. i == 0] or [i != 0]. Ideally we should also
2295 invalidate with say [i > 5] or [i == 8]. There is certainly
2296 room for improvement here. */
2297 if (pred_neg_p (predicate
, use_guard
[i
]))
2299 if (dump_file
&& dump_flags
& TDF_DETAILS
)
2301 fprintf (dump_file
, " Predicate was invalidated by: ");
2302 dump_pred_info (use_guard
[i
]);
2303 fputc ('\n', dump_file
);
2311 /* Return TRUE if all predicates in UNINIT_PRED are invalidated by
2312 USE_GUARD being true. */
2315 can_chain_union_be_invalidated_p (pred_chain_union uninit_pred
,
2316 pred_chain use_guard
)
2318 if (uninit_pred
.is_empty ())
2320 if (dump_file
&& dump_flags
& TDF_DETAILS
)
2321 dump_predicates (NULL
, uninit_pred
,
2322 "Testing if anything here can be invalidated: ");
2323 for (size_t i
= 0; i
< uninit_pred
.length (); ++i
)
2325 pred_chain c
= uninit_pred
[i
];
2327 for (j
= 0; j
< c
.length (); ++j
)
2328 if (can_one_predicate_be_invalidated_p (c
[j
], use_guard
))
2331 /* If we were unable to invalidate any predicate in C, then there
2332 is a viable path from entry to the PHI where the PHI takes
2333 an uninitialized value and continues to a use of the PHI. */
2334 if (j
== c
.length ())
2340 /* Return TRUE if none of the uninitialized operands in UNINT_OPNDS
2341 can actually happen if we arrived at a use for PHI.
2343 PHI_USE_GUARDS are the guard conditions for the use of the PHI. */
2346 uninit_uses_cannot_happen (gphi
*phi
, unsigned uninit_opnds
,
2347 pred_chain_union phi_use_guards
)
2349 unsigned phi_args
= gimple_phi_num_args (phi
);
2350 if (phi_args
> max_phi_args
)
2353 /* PHI_USE_GUARDS are OR'ed together. If we have more than one
2354 possible guard, there's no way of knowing which guard was true.
2355 Since we need to be absolutely sure that the uninitialized
2356 operands will be invalidated, bail. */
2357 if (phi_use_guards
.length () != 1)
2360 /* Look for the control dependencies of all the uninitialized
2361 operands and build guard predicates describing them. */
2362 pred_chain_union uninit_preds
;
2364 for (unsigned i
= 0; i
< phi_args
; ++i
)
2366 if (!MASK_TEST_BIT (uninit_opnds
, i
))
2369 edge e
= gimple_phi_arg_edge (phi
, i
);
2370 vec
<edge
> dep_chains
[MAX_NUM_CHAINS
];
2371 auto_vec
<edge
, MAX_CHAIN_LEN
+ 1> cur_chain
;
2372 size_t num_chains
= 0;
2375 /* Build the control dependency chain for uninit operand `i'... */
2376 uninit_preds
= vNULL
;
2377 if (!compute_control_dep_chain (ENTRY_BLOCK_PTR_FOR_FN (cfun
),
2378 e
->src
, dep_chains
, &num_chains
,
2379 &cur_chain
, &num_calls
))
2384 /* ...and convert it into a set of predicates. */
2385 bool has_valid_preds
2386 = convert_control_dep_chain_into_preds (dep_chains
, num_chains
,
2388 for (size_t j
= 0; j
< num_chains
; ++j
)
2389 dep_chains
[j
].release ();
2390 if (!has_valid_preds
)
2395 simplify_preds (&uninit_preds
, NULL
, false);
2396 uninit_preds
= normalize_preds (uninit_preds
, NULL
, false);
2398 /* Can the guard for this uninitialized operand be invalidated
2400 if (!can_chain_union_be_invalidated_p (uninit_preds
, phi_use_guards
[0]))
2406 destroy_predicate_vecs (&uninit_preds
);
2410 /* Computes the predicates that guard the use and checks
2411 if the incoming paths that have empty (or possibly
2412 empty) definition can be pruned/filtered. The function returns
2413 true if it can be determined that the use of PHI's def in
2414 USE_STMT is guarded with a predicate set not overlapping with
2415 predicate sets of all runtime paths that do not have a definition.
2417 Returns false if it is not or it can not be determined. USE_BB is
2418 the bb of the use (for phi operand use, the bb is not the bb of
2419 the phi stmt, but the src bb of the operand edge).
2421 UNINIT_OPNDS is a bit vector. If an operand of PHI is uninitialized, the
2422 corresponding bit in the vector is 1. VISITED_PHIS is a pointer
2423 set of phis being visited.
2425 *DEF_PREDS contains the (memoized) defining predicate chains of PHI.
2426 If *DEF_PREDS is the empty vector, the defining predicate chains of
2427 PHI will be computed and stored into *DEF_PREDS as needed.
2429 VISITED_PHIS is a pointer set of phis being visited. */
2432 is_use_properly_guarded (gimple
*use_stmt
,
2435 unsigned uninit_opnds
,
2436 pred_chain_union
*def_preds
,
2437 hash_set
<gphi
*> *visited_phis
)
2440 pred_chain_union preds
= vNULL
;
2441 bool has_valid_preds
= false;
2442 bool is_properly_guarded
= false;
2444 if (visited_phis
->add (phi
))
2447 phi_bb
= gimple_bb (phi
);
2449 if (is_non_loop_exit_postdominating (use_bb
, phi_bb
))
2452 has_valid_preds
= find_predicates (&preds
, phi_bb
, use_bb
);
2454 if (!has_valid_preds
)
2456 destroy_predicate_vecs (&preds
);
2460 /* Try to prune the dead incoming phi edges. */
2462 = use_pred_not_overlap_with_undef_path_pred (preds
, phi
, uninit_opnds
,
2465 /* We might be able to prove that if the control dependencies
2466 for UNINIT_OPNDS are true, that the control dependencies for
2467 USE_STMT can never be true. */
2468 if (!is_properly_guarded
)
2469 is_properly_guarded
|= uninit_uses_cannot_happen (phi
, uninit_opnds
,
2472 if (is_properly_guarded
)
2474 destroy_predicate_vecs (&preds
);
2478 if (def_preds
->is_empty ())
2480 has_valid_preds
= find_def_preds (def_preds
, phi
);
2482 if (!has_valid_preds
)
2484 destroy_predicate_vecs (&preds
);
2488 simplify_preds (def_preds
, phi
, false);
2489 *def_preds
= normalize_preds (*def_preds
, phi
, false);
2492 simplify_preds (&preds
, use_stmt
, true);
2493 preds
= normalize_preds (preds
, use_stmt
, true);
2495 is_properly_guarded
= is_superset_of (*def_preds
, preds
);
2497 destroy_predicate_vecs (&preds
);
2498 return is_properly_guarded
;
2501 /* Searches through all uses of a potentially
2502 uninitialized variable defined by PHI and returns a use
2503 statement if the use is not properly guarded. It returns
2504 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
2505 holding the position(s) of uninit PHI operands. WORKLIST
2506 is the vector of candidate phis that may be updated by this
2507 function. ADDED_TO_WORKLIST is the pointer set tracking
2508 if the new phi is already in the worklist. */
2511 find_uninit_use (gphi
*phi
, unsigned uninit_opnds
,
2512 vec
<gphi
*> *worklist
,
2513 hash_set
<gphi
*> *added_to_worklist
)
2516 use_operand_p use_p
;
2518 imm_use_iterator iter
;
2519 pred_chain_union def_preds
= vNULL
;
2522 phi_result
= gimple_phi_result (phi
);
2524 FOR_EACH_IMM_USE_FAST (use_p
, iter
, phi_result
)
2528 use_stmt
= USE_STMT (use_p
);
2529 if (is_gimple_debug (use_stmt
))
2532 if (gphi
*use_phi
= dyn_cast
<gphi
*> (use_stmt
))
2533 use_bb
= gimple_phi_arg_edge (use_phi
,
2534 PHI_ARG_INDEX_FROM_USE (use_p
))->src
;
2536 use_bb
= gimple_bb (use_stmt
);
2538 hash_set
<gphi
*> visited_phis
;
2539 if (is_use_properly_guarded (use_stmt
, use_bb
, phi
, uninit_opnds
,
2540 &def_preds
, &visited_phis
))
2543 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2545 fprintf (dump_file
, "[CHECK]: Found unguarded use: ");
2546 print_gimple_stmt (dump_file
, use_stmt
, 0);
2548 /* Found one real use, return. */
2549 if (gimple_code (use_stmt
) != GIMPLE_PHI
)
2555 /* Found a phi use that is not guarded,
2556 add the phi to the worklist. */
2557 if (!added_to_worklist
->add (as_a
<gphi
*> (use_stmt
)))
2559 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2561 fprintf (dump_file
, "[WORKLIST]: Update worklist with phi: ");
2562 print_gimple_stmt (dump_file
, use_stmt
, 0);
2565 worklist
->safe_push (as_a
<gphi
*> (use_stmt
));
2566 possibly_undefined_names
->add (phi_result
);
2570 destroy_predicate_vecs (&def_preds
);
2574 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
2575 and gives warning if there exists a runtime path from the entry to a
2576 use of the PHI def that does not contain a definition. In other words,
2577 the warning is on the real use. The more dead paths that can be pruned
2578 by the compiler, the fewer false positives the warning is. WORKLIST
2579 is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
2580 a pointer set tracking if the new phi is added to the worklist or not. */
2583 warn_uninitialized_phi (gphi
*phi
, vec
<gphi
*> *worklist
,
2584 hash_set
<gphi
*> *added_to_worklist
)
2586 unsigned uninit_opnds
;
2587 gimple
*uninit_use_stmt
= 0;
2592 /* Don't look at virtual operands. */
2593 if (virtual_operand_p (gimple_phi_result (phi
)))
2596 uninit_opnds
= compute_uninit_opnds_pos (phi
);
2598 if (MASK_EMPTY (uninit_opnds
))
2601 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2603 fprintf (dump_file
, "[CHECK]: examining phi: ");
2604 print_gimple_stmt (dump_file
, phi
, 0);
2607 /* Now check if we have any use of the value without proper guard. */
2608 uninit_use_stmt
= find_uninit_use (phi
, uninit_opnds
,
2609 worklist
, added_to_worklist
);
2611 /* All uses are properly guarded. */
2612 if (!uninit_use_stmt
)
2615 phiarg_index
= MASK_FIRST_SET_BIT (uninit_opnds
);
2616 uninit_op
= gimple_phi_arg_def (phi
, phiarg_index
);
2617 if (SSA_NAME_VAR (uninit_op
) == NULL_TREE
)
2619 if (gimple_phi_arg_has_location (phi
, phiarg_index
))
2620 loc
= gimple_phi_arg_location (phi
, phiarg_index
);
2622 loc
= UNKNOWN_LOCATION
;
2623 warn_uninit (OPT_Wmaybe_uninitialized
, uninit_op
, SSA_NAME_VAR (uninit_op
),
2624 SSA_NAME_VAR (uninit_op
),
2625 "%qD may be used uninitialized in this function",
2626 uninit_use_stmt
, loc
);
2630 gate_warn_uninitialized (void)
2632 return warn_uninitialized
|| warn_maybe_uninitialized
;
2637 const pass_data pass_data_late_warn_uninitialized
=
2639 GIMPLE_PASS
, /* type */
2640 "uninit", /* name */
2641 OPTGROUP_NONE
, /* optinfo_flags */
2642 TV_NONE
, /* tv_id */
2643 PROP_ssa
, /* properties_required */
2644 0, /* properties_provided */
2645 0, /* properties_destroyed */
2646 0, /* todo_flags_start */
2647 0, /* todo_flags_finish */
2650 class pass_late_warn_uninitialized
: public gimple_opt_pass
2653 pass_late_warn_uninitialized (gcc::context
*ctxt
)
2654 : gimple_opt_pass (pass_data_late_warn_uninitialized
, ctxt
)
2657 /* opt_pass methods: */
2658 opt_pass
*clone () { return new pass_late_warn_uninitialized (m_ctxt
); }
2659 virtual bool gate (function
*) { return gate_warn_uninitialized (); }
2660 virtual unsigned int execute (function
*);
2662 }; // class pass_late_warn_uninitialized
2665 pass_late_warn_uninitialized::execute (function
*fun
)
2669 vec
<gphi
*> worklist
= vNULL
;
2671 calculate_dominance_info (CDI_DOMINATORS
);
2672 calculate_dominance_info (CDI_POST_DOMINATORS
);
2673 /* Re-do the plain uninitialized variable check, as optimization may have
2674 straightened control flow. Do this first so that we don't accidentally
2675 get a "may be" warning when we'd have seen an "is" warning later. */
2676 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
2678 timevar_push (TV_TREE_UNINIT
);
2680 possibly_undefined_names
= new hash_set
<tree
>;
2681 hash_set
<gphi
*> added_to_worklist
;
2683 /* Initialize worklist */
2684 FOR_EACH_BB_FN (bb
, fun
)
2685 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2687 gphi
*phi
= gsi
.phi ();
2690 n
= gimple_phi_num_args (phi
);
2692 /* Don't look at virtual operands. */
2693 if (virtual_operand_p (gimple_phi_result (phi
)))
2696 for (i
= 0; i
< n
; ++i
)
2698 tree op
= gimple_phi_arg_def (phi
, i
);
2699 if (TREE_CODE (op
) == SSA_NAME
&& uninit_undefined_value_p (op
))
2701 worklist
.safe_push (phi
);
2702 added_to_worklist
.add (phi
);
2703 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2705 fprintf (dump_file
, "[WORKLIST]: add to initial list: ");
2706 print_gimple_stmt (dump_file
, phi
, 0);
2713 while (worklist
.length () != 0)
2716 cur_phi
= worklist
.pop ();
2717 warn_uninitialized_phi (cur_phi
, &worklist
, &added_to_worklist
);
2720 worklist
.release ();
2721 delete possibly_undefined_names
;
2722 possibly_undefined_names
= NULL
;
2723 free_dominance_info (CDI_POST_DOMINATORS
);
2724 timevar_pop (TV_TREE_UNINIT
);
2731 make_pass_late_warn_uninitialized (gcc::context
*ctxt
)
2733 return new pass_late_warn_uninitialized (ctxt
);
2737 execute_early_warn_uninitialized (void)
2739 /* Currently, this pass runs always but
2740 execute_late_warn_uninitialized only runs with optimization. With
2741 optimization we want to warn about possible uninitialized as late
2742 as possible, thus don't do it here. However, without
2743 optimization we need to warn here about "may be uninitialized". */
2744 calculate_dominance_info (CDI_POST_DOMINATORS
);
2746 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/!optimize
);
2748 /* Post-dominator information can not be reliably updated. Free it
2751 free_dominance_info (CDI_POST_DOMINATORS
);
2757 const pass_data pass_data_early_warn_uninitialized
=
2759 GIMPLE_PASS
, /* type */
2760 "*early_warn_uninitialized", /* name */
2761 OPTGROUP_NONE
, /* optinfo_flags */
2762 TV_TREE_UNINIT
, /* tv_id */
2763 PROP_ssa
, /* properties_required */
2764 0, /* properties_provided */
2765 0, /* properties_destroyed */
2766 0, /* todo_flags_start */
2767 0, /* todo_flags_finish */
2770 class pass_early_warn_uninitialized
: public gimple_opt_pass
2773 pass_early_warn_uninitialized (gcc::context
*ctxt
)
2774 : gimple_opt_pass (pass_data_early_warn_uninitialized
, ctxt
)
2777 /* opt_pass methods: */
2778 virtual bool gate (function
*) { return gate_warn_uninitialized (); }
2779 virtual unsigned int execute (function
*)
2781 return execute_early_warn_uninitialized ();
2784 }; // class pass_early_warn_uninitialized
2789 make_pass_early_warn_uninitialized (gcc::context
*ctxt
)
2791 return new pass_early_warn_uninitialized (ctxt
);