1 /* Predicate aware uninitialized variable warning.
2 Copyright (C) 2001-2014 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"
28 #include "basic-block.h"
30 #include "gimple-pretty-print.h"
32 #include "pointer-set.h"
33 #include "tree-ssa-alias.h"
34 #include "internal-fn.h"
35 #include "gimple-expr.h"
38 #include "gimple-iterator.h"
39 #include "gimple-ssa.h"
40 #include "tree-phinodes.h"
41 #include "ssa-iterators.h"
43 #include "tree-inline.h"
45 #include "tree-pass.h"
46 #include "diagnostic-core.h"
49 /* This implements the pass that does predicate aware warning on uses of
50 possibly uninitialized variables. The pass first collects the set of
51 possibly uninitialized SSA names. For each such name, it walks through
52 all its immediate uses. For each immediate use, it rebuilds the condition
53 expression (the predicate) that guards the use. The predicate is then
54 examined to see if the variable is always defined under that same condition.
55 This is done either by pruning the unrealizable paths that lead to the
56 default definitions or by checking if the predicate set that guards the
57 defining paths is a superset of the use predicate. */
60 /* Pointer set of potentially undefined ssa names, i.e.,
61 ssa names that are defined by phi with operands that
62 are not defined or potentially undefined. */
63 static pointer_set_t
*possibly_undefined_names
= 0;
65 /* Bit mask handling macros. */
66 #define MASK_SET_BIT(mask, pos) mask |= (1 << pos)
67 #define MASK_TEST_BIT(mask, pos) (mask & (1 << pos))
68 #define MASK_EMPTY(mask) (mask == 0)
70 /* Returns the first bit position (starting from LSB)
71 in mask that is non zero. Returns -1 if the mask is empty. */
73 get_mask_first_set_bit (unsigned mask
)
79 while ((mask
& (1 << pos
)) == 0)
84 #define MASK_FIRST_SET_BIT(mask) get_mask_first_set_bit (mask)
86 /* Return true if T, an SSA_NAME, has an undefined value. */
88 has_undefined_value_p (tree t
)
90 return (ssa_undefined_value_p (t
)
91 || (possibly_undefined_names
92 && pointer_set_contains (possibly_undefined_names
, t
)));
97 /* Like has_undefined_value_p, but don't return true if TREE_NO_WARNING
98 is set on SSA_NAME_VAR. */
101 uninit_undefined_value_p (tree t
) {
102 if (!has_undefined_value_p (t
))
104 if (SSA_NAME_VAR (t
) && TREE_NO_WARNING (SSA_NAME_VAR (t
)))
109 /* Emit warnings for uninitialized variables. This is done in two passes.
111 The first pass notices real uses of SSA names with undefined values.
112 Such uses are unconditionally uninitialized, and we can be certain that
113 such a use is a mistake. This pass is run before most optimizations,
114 so that we catch as many as we can.
116 The second pass follows PHI nodes to find uses that are potentially
117 uninitialized. In this case we can't necessarily prove that the use
118 is really uninitialized. This pass is run after most optimizations,
119 so that we thread as many jumps and possible, and delete as much dead
120 code as possible, in order to reduce false positives. We also look
121 again for plain uninitialized variables, since optimization may have
122 changed conditionally uninitialized to unconditionally uninitialized. */
124 /* Emit a warning for EXPR based on variable VAR at the point in the
125 program T, an SSA_NAME, is used being uninitialized. The exact
126 warning text is in MSGID and LOCUS may contain a location or be null.
127 WC is the warning code. */
130 warn_uninit (enum opt_code wc
, tree t
,
131 tree expr
, tree var
, const char *gmsgid
, void *data
)
133 gimple context
= (gimple
) data
;
134 location_t location
, cfun_loc
;
135 expanded_location xloc
, floc
;
137 if (!has_undefined_value_p (t
))
140 /* TREE_NO_WARNING either means we already warned, or the front end
141 wishes to suppress the warning. */
143 && (gimple_no_warning_p (context
)
144 || (gimple_assign_single_p (context
)
145 && TREE_NO_WARNING (gimple_assign_rhs1 (context
)))))
146 || TREE_NO_WARNING (expr
))
149 location
= (context
!= NULL
&& gimple_has_location (context
))
150 ? gimple_location (context
)
151 : DECL_SOURCE_LOCATION (var
);
152 location
= linemap_resolve_location (line_table
, location
,
153 LRK_SPELLING_LOCATION
,
155 cfun_loc
= DECL_SOURCE_LOCATION (cfun
->decl
);
156 xloc
= expand_location (location
);
157 floc
= expand_location (cfun_loc
);
158 if (warning_at (location
, wc
, gmsgid
, expr
))
160 TREE_NO_WARNING (expr
) = 1;
162 if (location
== DECL_SOURCE_LOCATION (var
))
164 if (xloc
.file
!= floc
.file
165 || linemap_location_before_p (line_table
,
167 || linemap_location_before_p (line_table
,
168 cfun
->function_end_locus
,
170 inform (DECL_SOURCE_LOCATION (var
), "%qD was declared here", var
);
175 warn_uninitialized_vars (bool warn_possibly_uninitialized
)
177 gimple_stmt_iterator gsi
;
180 FOR_EACH_BB_FN (bb
, cfun
)
182 bool always_executed
= dominated_by_p (CDI_POST_DOMINATORS
,
183 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)), bb
);
184 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
186 gimple stmt
= gsi_stmt (gsi
);
191 if (is_gimple_debug (stmt
))
194 /* We only do data flow with SSA_NAMEs, so that's all we
196 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, op_iter
, SSA_OP_USE
)
198 use
= USE_FROM_PTR (use_p
);
200 warn_uninit (OPT_Wuninitialized
, use
,
201 SSA_NAME_VAR (use
), SSA_NAME_VAR (use
),
202 "%qD is used uninitialized in this function",
204 else if (warn_possibly_uninitialized
)
205 warn_uninit (OPT_Wmaybe_uninitialized
, use
,
206 SSA_NAME_VAR (use
), SSA_NAME_VAR (use
),
207 "%qD may be used uninitialized in this function",
211 /* For memory the only cheap thing we can do is see if we
212 have a use of the default def of the virtual operand.
213 ??? Note that at -O0 we do not have virtual operands.
214 ??? Not so cheap would be to use the alias oracle via
215 walk_aliased_vdefs, if we don't find any aliasing vdef
216 warn as is-used-uninitialized, if we don't find an aliasing
217 vdef that kills our use (stmt_kills_ref_p), warn as
218 may-be-used-uninitialized. But this walk is quadratic and
219 so must be limited which means we would miss warning
221 use
= gimple_vuse (stmt
);
223 && gimple_assign_single_p (stmt
)
224 && !gimple_vdef (stmt
)
225 && SSA_NAME_IS_DEFAULT_DEF (use
))
227 tree rhs
= gimple_assign_rhs1 (stmt
);
228 tree base
= get_base_address (rhs
);
230 /* Do not warn if it can be initialized outside this function. */
231 if (TREE_CODE (base
) != VAR_DECL
232 || DECL_HARD_REGISTER (base
)
233 || is_global_var (base
))
237 warn_uninit (OPT_Wuninitialized
, use
,
238 gimple_assign_rhs1 (stmt
), base
,
239 "%qE is used uninitialized in this function",
241 else if (warn_possibly_uninitialized
)
242 warn_uninit (OPT_Wmaybe_uninitialized
, use
,
243 gimple_assign_rhs1 (stmt
), base
,
244 "%qE may be used uninitialized in this function",
253 /* Checks if the operand OPND of PHI is defined by
254 another phi with one operand defined by this PHI,
255 but the rest operands are all defined. If yes,
256 returns true to skip this this operand as being
257 redundant. Can be enhanced to be more general. */
260 can_skip_redundant_opnd (tree opnd
, gimple phi
)
266 phi_def
= gimple_phi_result (phi
);
267 op_def
= SSA_NAME_DEF_STMT (opnd
);
268 if (gimple_code (op_def
) != GIMPLE_PHI
)
270 n
= gimple_phi_num_args (op_def
);
271 for (i
= 0; i
< n
; ++i
)
273 tree op
= gimple_phi_arg_def (op_def
, i
);
274 if (TREE_CODE (op
) != SSA_NAME
)
276 if (op
!= phi_def
&& uninit_undefined_value_p (op
))
283 /* Returns a bit mask holding the positions of arguments in PHI
284 that have empty (or possibly empty) definitions. */
287 compute_uninit_opnds_pos (gimple phi
)
290 unsigned uninit_opnds
= 0;
292 n
= gimple_phi_num_args (phi
);
293 /* Bail out for phi with too many args. */
297 for (i
= 0; i
< n
; ++i
)
299 tree op
= gimple_phi_arg_def (phi
, i
);
300 if (TREE_CODE (op
) == SSA_NAME
301 && uninit_undefined_value_p (op
)
302 && !can_skip_redundant_opnd (op
, phi
))
304 if (cfun
->has_nonlocal_label
|| cfun
->calls_setjmp
)
306 /* Ignore SSA_NAMEs that appear on abnormal edges
308 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
311 MASK_SET_BIT (uninit_opnds
, i
);
317 /* Find the immediate postdominator PDOM of the specified
318 basic block BLOCK. */
320 static inline basic_block
321 find_pdom (basic_block block
)
323 if (block
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
324 return EXIT_BLOCK_PTR_FOR_FN (cfun
);
328 = get_immediate_dominator (CDI_POST_DOMINATORS
, block
);
330 return EXIT_BLOCK_PTR_FOR_FN (cfun
);
335 /* Find the immediate DOM of the specified
336 basic block BLOCK. */
338 static inline basic_block
339 find_dom (basic_block block
)
341 if (block
== ENTRY_BLOCK_PTR_FOR_FN (cfun
))
342 return ENTRY_BLOCK_PTR_FOR_FN (cfun
);
345 basic_block bb
= get_immediate_dominator (CDI_DOMINATORS
, block
);
347 return ENTRY_BLOCK_PTR_FOR_FN (cfun
);
352 /* Returns true if BB1 is postdominating BB2 and BB1 is
353 not a loop exit bb. The loop exit bb check is simple and does
354 not cover all cases. */
357 is_non_loop_exit_postdominating (basic_block bb1
, basic_block bb2
)
359 if (!dominated_by_p (CDI_POST_DOMINATORS
, bb2
, bb1
))
362 if (single_pred_p (bb1
) && !single_succ_p (bb2
))
368 /* Find the closest postdominator of a specified BB, which is control
371 static inline basic_block
372 find_control_equiv_block (basic_block bb
)
376 pdom
= find_pdom (bb
);
378 /* Skip the postdominating bb that is also loop exit. */
379 if (!is_non_loop_exit_postdominating (pdom
, bb
))
382 if (dominated_by_p (CDI_DOMINATORS
, pdom
, bb
))
388 #define MAX_NUM_CHAINS 8
389 #define MAX_CHAIN_LEN 5
390 #define MAX_POSTDOM_CHECK 8
392 /* Computes the control dependence chains (paths of edges)
393 for DEP_BB up to the dominating basic block BB (the head node of a
394 chain should be dominated by it). CD_CHAINS is pointer to an
395 array holding the result chains. CUR_CD_CHAIN is the current
396 chain being computed. *NUM_CHAINS is total number of chains. The
397 function returns true if the information is successfully computed,
398 return false if there is no control dependence or not computed. */
401 compute_control_dep_chain (basic_block bb
, basic_block dep_bb
,
402 vec
<edge
> *cd_chains
,
404 vec
<edge
> *cur_cd_chain
,
410 bool found_cd_chain
= false;
411 size_t cur_chain_len
= 0;
413 if (EDGE_COUNT (bb
->succs
) < 2)
416 if (*num_calls
> PARAM_VALUE (PARAM_UNINIT_CONTROL_DEP_ATTEMPTS
))
420 /* Could use a set instead. */
421 cur_chain_len
= cur_cd_chain
->length ();
422 if (cur_chain_len
> MAX_CHAIN_LEN
)
425 for (i
= 0; i
< cur_chain_len
; i
++)
427 edge e
= (*cur_cd_chain
)[i
];
428 /* Cycle detected. */
433 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
436 int post_dom_check
= 0;
437 if (e
->flags
& (EDGE_FAKE
| EDGE_ABNORMAL
))
441 cur_cd_chain
->safe_push (e
);
442 while (!is_non_loop_exit_postdominating (cd_bb
, bb
))
446 /* Found a direct control dependence. */
447 if (*num_chains
< MAX_NUM_CHAINS
)
449 cd_chains
[*num_chains
] = cur_cd_chain
->copy ();
452 found_cd_chain
= true;
453 /* Check path from next edge. */
457 /* Now check if DEP_BB is indirectly control dependent on BB. */
458 if (compute_control_dep_chain (cd_bb
, dep_bb
, cd_chains
,
459 num_chains
, cur_cd_chain
, num_calls
))
461 found_cd_chain
= true;
465 cd_bb
= find_pdom (cd_bb
);
467 if (cd_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
) || post_dom_check
>
471 cur_cd_chain
->pop ();
472 gcc_assert (cur_cd_chain
->length () == cur_chain_len
);
474 gcc_assert (cur_cd_chain
->length () == cur_chain_len
);
476 return found_cd_chain
;
479 /* The type to represent a simple predicate */
481 typedef struct use_def_pred_info
485 enum tree_code cond_code
;
489 /* The type to represent a sequence of predicates grouped
490 with .AND. operation. */
492 typedef vec
<pred_info
, va_heap
, vl_ptr
> pred_chain
;
494 /* The type to represent a sequence of pred_chains grouped
495 with .OR. operation. */
497 typedef vec
<pred_chain
, va_heap
, vl_ptr
> pred_chain_union
;
499 /* Converts the chains of control dependence edges into a set of
500 predicates. A control dependence chain is represented by a vector
501 edges. DEP_CHAINS points to an array of dependence chains.
502 NUM_CHAINS is the size of the chain array. One edge in a dependence
503 chain is mapped to predicate expression represented by pred_info
504 type. One dependence chain is converted to a composite predicate that
505 is the result of AND operation of pred_info mapped to each edge.
506 A composite predicate is presented by a vector of pred_info. On
507 return, *PREDS points to the resulting array of composite predicates.
508 *NUM_PREDS is the number of composite predictes. */
511 convert_control_dep_chain_into_preds (vec
<edge
> *dep_chains
,
513 pred_chain_union
*preds
)
515 bool has_valid_pred
= false;
517 if (num_chains
== 0 || num_chains
>= MAX_NUM_CHAINS
)
520 /* Now convert the control dep chain into a set
522 preds
->reserve (num_chains
);
524 for (i
= 0; i
< num_chains
; i
++)
526 vec
<edge
> one_cd_chain
= dep_chains
[i
];
528 has_valid_pred
= false;
529 pred_chain t_chain
= vNULL
;
530 for (j
= 0; j
< one_cd_chain
.length (); j
++)
533 gimple_stmt_iterator gsi
;
534 basic_block guard_bb
;
540 gsi
= gsi_last_bb (guard_bb
);
543 has_valid_pred
= false;
546 cond_stmt
= gsi_stmt (gsi
);
547 if (is_gimple_call (cond_stmt
)
548 && EDGE_COUNT (e
->src
->succs
) >= 2)
550 /* Ignore EH edge. Can add assertion
551 on the other edge's flag. */
554 /* Skip if there is essentially one succesor. */
555 if (EDGE_COUNT (e
->src
->succs
) == 2)
561 FOR_EACH_EDGE (e1
, ei1
, e
->src
->succs
)
563 if (EDGE_COUNT (e1
->dest
->succs
) == 0)
572 if (gimple_code (cond_stmt
) != GIMPLE_COND
)
574 has_valid_pred
= false;
577 one_pred
.pred_lhs
= gimple_cond_lhs (cond_stmt
);
578 one_pred
.pred_rhs
= gimple_cond_rhs (cond_stmt
);
579 one_pred
.cond_code
= gimple_cond_code (cond_stmt
);
580 one_pred
.invert
= !!(e
->flags
& EDGE_FALSE_VALUE
);
581 t_chain
.safe_push (one_pred
);
582 has_valid_pred
= true;
588 preds
->safe_push (t_chain
);
590 return has_valid_pred
;
593 /* Computes all control dependence chains for USE_BB. The control
594 dependence chains are then converted to an array of composite
595 predicates pointed to by PREDS. PHI_BB is the basic block of
596 the phi whose result is used in USE_BB. */
599 find_predicates (pred_chain_union
*preds
,
603 size_t num_chains
= 0, i
;
605 vec
<edge
> dep_chains
[MAX_NUM_CHAINS
];
606 auto_vec
<edge
, MAX_CHAIN_LEN
+ 1> cur_chain
;
607 bool has_valid_pred
= false;
608 basic_block cd_root
= 0;
610 /* First find the closest bb that is control equivalent to PHI_BB
611 that also dominates USE_BB. */
613 while (dominated_by_p (CDI_DOMINATORS
, use_bb
, cd_root
))
615 basic_block ctrl_eq_bb
= find_control_equiv_block (cd_root
);
616 if (ctrl_eq_bb
&& dominated_by_p (CDI_DOMINATORS
, use_bb
, ctrl_eq_bb
))
617 cd_root
= ctrl_eq_bb
;
622 compute_control_dep_chain (cd_root
, use_bb
, dep_chains
, &num_chains
,
623 &cur_chain
, &num_calls
);
626 = convert_control_dep_chain_into_preds (dep_chains
, num_chains
, preds
);
627 for (i
= 0; i
< num_chains
; i
++)
628 dep_chains
[i
].release ();
629 return has_valid_pred
;
632 /* Computes the set of incoming edges of PHI that have non empty
633 definitions of a phi chain. The collection will be done
634 recursively on operands that are defined by phis. CD_ROOT
635 is the control dependence root. *EDGES holds the result, and
636 VISITED_PHIS is a pointer set for detecting cycles. */
639 collect_phi_def_edges (gimple phi
, basic_block cd_root
,
641 pointer_set_t
*visited_phis
)
647 if (pointer_set_insert (visited_phis
, phi
))
650 n
= gimple_phi_num_args (phi
);
651 for (i
= 0; i
< n
; i
++)
653 opnd_edge
= gimple_phi_arg_edge (phi
, i
);
654 opnd
= gimple_phi_arg_def (phi
, i
);
656 if (TREE_CODE (opnd
) != SSA_NAME
)
658 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
660 fprintf (dump_file
, "\n[CHECK] Found def edge %d in ", (int)i
);
661 print_gimple_stmt (dump_file
, phi
, 0, 0);
663 edges
->safe_push (opnd_edge
);
667 gimple def
= SSA_NAME_DEF_STMT (opnd
);
669 if (gimple_code (def
) == GIMPLE_PHI
670 && dominated_by_p (CDI_DOMINATORS
,
671 gimple_bb (def
), cd_root
))
672 collect_phi_def_edges (def
, cd_root
, edges
,
674 else if (!uninit_undefined_value_p (opnd
))
676 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
678 fprintf (dump_file
, "\n[CHECK] Found def edge %d in ", (int)i
);
679 print_gimple_stmt (dump_file
, phi
, 0, 0);
681 edges
->safe_push (opnd_edge
);
687 /* For each use edge of PHI, computes all control dependence chains.
688 The control dependence chains are then converted to an array of
689 composite predicates pointed to by PREDS. */
692 find_def_preds (pred_chain_union
*preds
, gimple phi
)
694 size_t num_chains
= 0, i
, n
;
695 vec
<edge
> dep_chains
[MAX_NUM_CHAINS
];
696 auto_vec
<edge
, MAX_CHAIN_LEN
+ 1> cur_chain
;
697 vec
<edge
> def_edges
= vNULL
;
698 bool has_valid_pred
= false;
699 basic_block phi_bb
, cd_root
= 0;
700 pointer_set_t
*visited_phis
;
702 phi_bb
= gimple_bb (phi
);
703 /* First find the closest dominating bb to be
704 the control dependence root */
705 cd_root
= find_dom (phi_bb
);
709 visited_phis
= pointer_set_create ();
710 collect_phi_def_edges (phi
, cd_root
, &def_edges
, visited_phis
);
711 pointer_set_destroy (visited_phis
);
713 n
= def_edges
.length ();
717 for (i
= 0; i
< n
; i
++)
723 opnd_edge
= def_edges
[i
];
724 prev_nc
= num_chains
;
725 compute_control_dep_chain (cd_root
, opnd_edge
->src
, dep_chains
,
726 &num_chains
, &cur_chain
, &num_calls
);
728 /* Now update the newly added chains with
729 the phi operand edge: */
730 if (EDGE_COUNT (opnd_edge
->src
->succs
) > 1)
732 if (prev_nc
== num_chains
&& num_chains
< MAX_NUM_CHAINS
)
733 dep_chains
[num_chains
++] = vNULL
;
734 for (j
= prev_nc
; j
< num_chains
; j
++)
735 dep_chains
[j
].safe_push (opnd_edge
);
740 = convert_control_dep_chain_into_preds (dep_chains
, num_chains
, preds
);
741 for (i
= 0; i
< num_chains
; i
++)
742 dep_chains
[i
].release ();
743 return has_valid_pred
;
746 /* Dumps the predicates (PREDS) for USESTMT. */
749 dump_predicates (gimple usestmt
, pred_chain_union preds
,
753 pred_chain one_pred_chain
= vNULL
;
754 fprintf (dump_file
, msg
);
755 print_gimple_stmt (dump_file
, usestmt
, 0, 0);
756 fprintf (dump_file
, "is guarded by :\n\n");
757 size_t num_preds
= preds
.length ();
758 /* Do some dumping here: */
759 for (i
= 0; i
< num_preds
; i
++)
763 one_pred_chain
= preds
[i
];
764 np
= one_pred_chain
.length ();
766 for (j
= 0; j
< np
; j
++)
768 pred_info one_pred
= one_pred_chain
[j
];
770 fprintf (dump_file
, " (.NOT.) ");
771 print_generic_expr (dump_file
, one_pred
.pred_lhs
, 0);
772 fprintf (dump_file
, " %s ", op_symbol_code (one_pred
.cond_code
));
773 print_generic_expr (dump_file
, one_pred
.pred_rhs
, 0);
775 fprintf (dump_file
, " (.AND.) ");
777 fprintf (dump_file
, "\n");
779 if (i
< num_preds
- 1)
780 fprintf (dump_file
, "(.OR.)\n");
782 fprintf (dump_file
, "\n\n");
786 /* Destroys the predicate set *PREDS. */
789 destroy_predicate_vecs (pred_chain_union preds
)
793 size_t n
= preds
.length ();
794 for (i
= 0; i
< n
; i
++)
800 /* Computes the 'normalized' conditional code with operand
801 swapping and condition inversion. */
803 static enum tree_code
804 get_cmp_code (enum tree_code orig_cmp_code
,
805 bool swap_cond
, bool invert
)
807 enum tree_code tc
= orig_cmp_code
;
810 tc
= swap_tree_comparison (orig_cmp_code
);
812 tc
= invert_tree_comparison (tc
, false);
829 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
830 all values in the range satisfies (x CMPC BOUNDARY) == true. */
833 is_value_included_in (tree val
, tree boundary
, enum tree_code cmpc
)
835 bool inverted
= false;
839 /* Only handle integer constant here. */
840 if (TREE_CODE (val
) != INTEGER_CST
841 || TREE_CODE (boundary
) != INTEGER_CST
)
844 is_unsigned
= TYPE_UNSIGNED (TREE_TYPE (val
));
846 if (cmpc
== GE_EXPR
|| cmpc
== GT_EXPR
849 cmpc
= invert_tree_comparison (cmpc
, false);
856 result
= tree_int_cst_equal (val
, boundary
);
857 else if (cmpc
== LT_EXPR
)
858 result
= INT_CST_LT_UNSIGNED (val
, boundary
);
861 gcc_assert (cmpc
== LE_EXPR
);
862 result
= (tree_int_cst_equal (val
, boundary
)
863 || INT_CST_LT_UNSIGNED (val
, boundary
));
869 result
= tree_int_cst_equal (val
, boundary
);
870 else if (cmpc
== LT_EXPR
)
871 result
= INT_CST_LT (val
, boundary
);
874 gcc_assert (cmpc
== LE_EXPR
);
875 result
= (tree_int_cst_equal (val
, boundary
)
876 || INT_CST_LT (val
, boundary
));
886 /* Returns true if PRED is common among all the predicate
887 chains (PREDS) (and therefore can be factored out).
888 NUM_PRED_CHAIN is the size of array PREDS. */
891 find_matching_predicate_in_rest_chains (pred_info pred
,
892 pred_chain_union preds
,
893 size_t num_pred_chains
)
898 if (num_pred_chains
== 1)
901 for (i
= 1; i
< num_pred_chains
; i
++)
904 pred_chain one_chain
= preds
[i
];
905 n
= one_chain
.length ();
906 for (j
= 0; j
< n
; j
++)
908 pred_info pred2
= one_chain
[j
];
909 /* Can relax the condition comparison to not
910 use address comparison. However, the most common
911 case is that multiple control dependent paths share
912 a common path prefix, so address comparison should
915 if (operand_equal_p (pred2
.pred_lhs
, pred
.pred_lhs
, 0)
916 && operand_equal_p (pred2
.pred_rhs
, pred
.pred_rhs
, 0)
917 && pred2
.invert
== pred
.invert
)
929 /* Forward declaration. */
931 is_use_properly_guarded (gimple use_stmt
,
934 unsigned uninit_opnds
,
935 pointer_set_t
*visited_phis
);
937 /* Returns true if all uninitialized opnds are pruned. Returns false
938 otherwise. PHI is the phi node with uninitialized operands,
939 UNINIT_OPNDS is the bitmap of the uninitialize operand positions,
940 FLAG_DEF is the statement defining the flag guarding the use of the
941 PHI output, BOUNDARY_CST is the const value used in the predicate
942 associated with the flag, CMP_CODE is the comparison code used in
943 the predicate, VISITED_PHIS is the pointer set of phis visited, and
944 VISITED_FLAG_PHIS is the pointer to the pointer set of flag definitions
950 flag_1 = phi <0, 1> // (1)
951 var_1 = phi <undef, some_val>
955 flag_2 = phi <0, flag_1, flag_1> // (2)
956 var_2 = phi <undef, var_1, var_1>
963 Because some flag arg in (1) is not constant, if we do not look into the
964 flag phis recursively, it is conservatively treated as unknown and var_1
965 is thought to be flowed into use at (3). Since var_1 is potentially uninitialized
966 a false warning will be emitted. Checking recursively into (1), the compiler can
967 find out that only some_val (which is defined) can flow into (3) which is OK.
972 prune_uninit_phi_opnds_in_unrealizable_paths (gimple phi
,
973 unsigned uninit_opnds
,
976 enum tree_code cmp_code
,
977 pointer_set_t
*visited_phis
,
978 bitmap
*visited_flag_phis
)
982 for (i
= 0; i
< MIN (32, gimple_phi_num_args (flag_def
)); i
++)
986 if (!MASK_TEST_BIT (uninit_opnds
, i
))
989 flag_arg
= gimple_phi_arg_def (flag_def
, i
);
990 if (!is_gimple_constant (flag_arg
))
992 gimple flag_arg_def
, phi_arg_def
;
994 unsigned uninit_opnds_arg_phi
;
996 if (TREE_CODE (flag_arg
) != SSA_NAME
)
998 flag_arg_def
= SSA_NAME_DEF_STMT (flag_arg
);
999 if (gimple_code (flag_arg_def
) != GIMPLE_PHI
)
1002 phi_arg
= gimple_phi_arg_def (phi
, i
);
1003 if (TREE_CODE (phi_arg
) != SSA_NAME
)
1006 phi_arg_def
= SSA_NAME_DEF_STMT (phi_arg
);
1007 if (gimple_code (phi_arg_def
) != GIMPLE_PHI
)
1010 if (gimple_bb (phi_arg_def
) != gimple_bb (flag_arg_def
))
1013 if (!*visited_flag_phis
)
1014 *visited_flag_phis
= BITMAP_ALLOC (NULL
);
1016 if (bitmap_bit_p (*visited_flag_phis
,
1017 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def
))))
1020 bitmap_set_bit (*visited_flag_phis
,
1021 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def
)));
1023 /* Now recursively prune the uninitialized phi args. */
1024 uninit_opnds_arg_phi
= compute_uninit_opnds_pos (phi_arg_def
);
1025 if (!prune_uninit_phi_opnds_in_unrealizable_paths
1026 (phi_arg_def
, uninit_opnds_arg_phi
, flag_arg_def
,
1027 boundary_cst
, cmp_code
, visited_phis
, visited_flag_phis
))
1030 bitmap_clear_bit (*visited_flag_phis
,
1031 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def
)));
1035 /* Now check if the constant is in the guarded range. */
1036 if (is_value_included_in (flag_arg
, boundary_cst
, cmp_code
))
1041 /* Now that we know that this undefined edge is not
1042 pruned. If the operand is defined by another phi,
1043 we can further prune the incoming edges of that
1044 phi by checking the predicates of this operands. */
1046 opnd
= gimple_phi_arg_def (phi
, i
);
1047 opnd_def
= SSA_NAME_DEF_STMT (opnd
);
1048 if (gimple_code (opnd_def
) == GIMPLE_PHI
)
1051 unsigned uninit_opnds2
1052 = compute_uninit_opnds_pos (opnd_def
);
1053 gcc_assert (!MASK_EMPTY (uninit_opnds2
));
1054 opnd_edge
= gimple_phi_arg_edge (phi
, i
);
1055 if (!is_use_properly_guarded (phi
,
1070 /* A helper function that determines if the predicate set
1071 of the use is not overlapping with that of the uninit paths.
1072 The most common senario of guarded use is in Example 1:
1085 The real world examples are usually more complicated, but similar
1086 and usually result from inlining:
1088 bool init_func (int * x)
1107 Another possible use scenario is in the following trivial example:
1119 Predicate analysis needs to compute the composite predicate:
1121 1) 'x' use predicate: (n > 0) .AND. (m < 2)
1122 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
1123 (the predicate chain for phi operand defs can be computed
1124 starting from a bb that is control equivalent to the phi's
1125 bb and is dominating the operand def.)
1127 and check overlapping:
1128 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
1131 This implementation provides framework that can handle
1132 scenarios. (Note that many simple cases are handled properly
1133 without the predicate analysis -- this is due to jump threading
1134 transformation which eliminates the merge point thus makes
1135 path sensitive analysis unnecessary.)
1137 NUM_PREDS is the number is the number predicate chains, PREDS is
1138 the array of chains, PHI is the phi node whose incoming (undefined)
1139 paths need to be pruned, and UNINIT_OPNDS is the bitmap holding
1140 uninit operand positions. VISITED_PHIS is the pointer set of phi
1141 stmts being checked. */
1145 use_pred_not_overlap_with_undef_path_pred (pred_chain_union preds
,
1146 gimple phi
, unsigned uninit_opnds
,
1147 pointer_set_t
*visited_phis
)
1150 gimple flag_def
= 0;
1151 tree boundary_cst
= 0;
1152 enum tree_code cmp_code
;
1153 bool swap_cond
= false;
1154 bool invert
= false;
1155 pred_chain the_pred_chain
= vNULL
;
1156 bitmap visited_flag_phis
= NULL
;
1157 bool all_pruned
= false;
1158 size_t num_preds
= preds
.length ();
1160 gcc_assert (num_preds
> 0);
1161 /* Find within the common prefix of multiple predicate chains
1162 a predicate that is a comparison of a flag variable against
1164 the_pred_chain
= preds
[0];
1165 n
= the_pred_chain
.length ();
1166 for (i
= 0; i
< n
; i
++)
1168 tree cond_lhs
, cond_rhs
, flag
= 0;
1170 pred_info the_pred
= the_pred_chain
[i
];
1172 invert
= the_pred
.invert
;
1173 cond_lhs
= the_pred
.pred_lhs
;
1174 cond_rhs
= the_pred
.pred_rhs
;
1175 cmp_code
= the_pred
.cond_code
;
1177 if (cond_lhs
!= NULL_TREE
&& TREE_CODE (cond_lhs
) == SSA_NAME
1178 && cond_rhs
!= NULL_TREE
&& is_gimple_constant (cond_rhs
))
1180 boundary_cst
= cond_rhs
;
1183 else if (cond_rhs
!= NULL_TREE
&& TREE_CODE (cond_rhs
) == SSA_NAME
1184 && cond_lhs
!= NULL_TREE
&& is_gimple_constant (cond_lhs
))
1186 boundary_cst
= cond_lhs
;
1194 flag_def
= SSA_NAME_DEF_STMT (flag
);
1199 if ((gimple_code (flag_def
) == GIMPLE_PHI
)
1200 && (gimple_bb (flag_def
) == gimple_bb (phi
))
1201 && find_matching_predicate_in_rest_chains (the_pred
, preds
,
1211 /* Now check all the uninit incoming edge has a constant flag value
1212 that is in conflict with the use guard/predicate. */
1213 cmp_code
= get_cmp_code (cmp_code
, swap_cond
, invert
);
1215 if (cmp_code
== ERROR_MARK
)
1218 all_pruned
= prune_uninit_phi_opnds_in_unrealizable_paths (phi
,
1224 &visited_flag_phis
);
1226 if (visited_flag_phis
)
1227 BITMAP_FREE (visited_flag_phis
);
1232 /* The helper function returns true if two predicates X1 and X2
1233 are equivalent. It assumes the expressions have already
1234 properly re-associated. */
1237 pred_equal_p (pred_info x1
, pred_info x2
)
1239 enum tree_code c1
, c2
;
1240 if (!operand_equal_p (x1
.pred_lhs
, x2
.pred_lhs
, 0)
1241 || !operand_equal_p (x1
.pred_rhs
, x2
.pred_rhs
, 0))
1245 if (x1
.invert
!= x2
.invert
)
1246 c2
= invert_tree_comparison (x2
.cond_code
, false);
1253 /* Returns true if the predication is testing !=. */
1256 is_neq_relop_p (pred_info pred
)
1259 return (pred
.cond_code
== NE_EXPR
&& !pred
.invert
)
1260 || (pred
.cond_code
== EQ_EXPR
&& pred
.invert
);
1263 /* Returns true if pred is of the form X != 0. */
1266 is_neq_zero_form_p (pred_info pred
)
1268 if (!is_neq_relop_p (pred
) || !integer_zerop (pred
.pred_rhs
)
1269 || TREE_CODE (pred
.pred_lhs
) != SSA_NAME
)
1274 /* The helper function returns true if two predicates X1
1275 is equivalent to X2 != 0. */
1278 pred_expr_equal_p (pred_info x1
, tree x2
)
1280 if (!is_neq_zero_form_p (x1
))
1283 return operand_equal_p (x1
.pred_lhs
, x2
, 0);
1286 /* Returns true of the domain of single predicate expression
1287 EXPR1 is a subset of that of EXPR2. Returns false if it
1288 can not be proved. */
1291 is_pred_expr_subset_of (pred_info expr1
, pred_info expr2
)
1293 enum tree_code code1
, code2
;
1295 if (pred_equal_p (expr1
, expr2
))
1298 if ((TREE_CODE (expr1
.pred_rhs
) != INTEGER_CST
)
1299 || (TREE_CODE (expr2
.pred_rhs
) != INTEGER_CST
))
1302 if (!operand_equal_p (expr1
.pred_lhs
, expr2
.pred_lhs
, 0))
1305 code1
= expr1
.cond_code
;
1307 code1
= invert_tree_comparison (code1
, false);
1308 code2
= expr2
.cond_code
;
1310 code2
= invert_tree_comparison (code2
, false);
1312 if (code1
!= code2
&& code2
!= NE_EXPR
)
1315 if (is_value_included_in (expr1
.pred_rhs
, expr2
.pred_rhs
, code2
))
1321 /* Returns true if the domain of PRED1 is a subset
1322 of that of PRED2. Returns false if it can not be proved so. */
1325 is_pred_chain_subset_of (pred_chain pred1
,
1328 size_t np1
, np2
, i1
, i2
;
1330 np1
= pred1
.length ();
1331 np2
= pred2
.length ();
1333 for (i2
= 0; i2
< np2
; i2
++)
1336 pred_info info2
= pred2
[i2
];
1337 for (i1
= 0; i1
< np1
; i1
++)
1339 pred_info info1
= pred1
[i1
];
1340 if (is_pred_expr_subset_of (info1
, info2
))
1352 /* Returns true if the domain defined by
1353 one pred chain ONE_PRED is a subset of the domain
1354 of *PREDS. It returns false if ONE_PRED's domain is
1355 not a subset of any of the sub-domains of PREDS
1356 (corresponding to each individual chains in it), even
1357 though it may be still be a subset of whole domain
1358 of PREDS which is the union (ORed) of all its subdomains.
1359 In other words, the result is conservative. */
1362 is_included_in (pred_chain one_pred
, pred_chain_union preds
)
1365 size_t n
= preds
.length ();
1367 for (i
= 0; i
< n
; i
++)
1369 if (is_pred_chain_subset_of (one_pred
, preds
[i
]))
1376 /* Compares two predicate sets PREDS1 and PREDS2 and returns
1377 true if the domain defined by PREDS1 is a superset
1378 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1379 PREDS2 respectively. The implementation chooses not to build
1380 generic trees (and relying on the folding capability of the
1381 compiler), but instead performs brute force comparison of
1382 individual predicate chains (won't be a compile time problem
1383 as the chains are pretty short). When the function returns
1384 false, it does not necessarily mean *PREDS1 is not a superset
1385 of *PREDS2, but mean it may not be so since the analysis can
1386 not prove it. In such cases, false warnings may still be
1390 is_superset_of (pred_chain_union preds1
, pred_chain_union preds2
)
1393 pred_chain one_pred_chain
= vNULL
;
1395 n2
= preds2
.length ();
1397 for (i
= 0; i
< n2
; i
++)
1399 one_pred_chain
= preds2
[i
];
1400 if (!is_included_in (one_pred_chain
, preds1
))
1407 /* Returns true if TC is AND or OR. */
1410 is_and_or_or_p (enum tree_code tc
, tree type
)
1412 return (tc
== BIT_IOR_EXPR
1413 || (tc
== BIT_AND_EXPR
1414 && (type
== 0 || TREE_CODE (type
) == BOOLEAN_TYPE
)));
1417 /* Returns true if X1 is the negate of X2. */
1420 pred_neg_p (pred_info x1
, pred_info x2
)
1422 enum tree_code c1
, c2
;
1423 if (!operand_equal_p (x1
.pred_lhs
, x2
.pred_lhs
, 0)
1424 || !operand_equal_p (x1
.pred_rhs
, x2
.pred_rhs
, 0))
1428 if (x1
.invert
== x2
.invert
)
1429 c2
= invert_tree_comparison (x2
.cond_code
, false);
1436 /* 1) ((x IOR y) != 0) AND (x != 0) is equivalent to (x != 0);
1437 2) (X AND Y) OR (!X AND Y) is equivalent to Y;
1438 3) X OR (!X AND Y) is equivalent to (X OR Y);
1439 4) ((x IAND y) != 0) || (x != 0 AND y != 0)) is equivalent to
1441 5) (X AND Y) OR (!X AND Z) OR (!Y AND Z) is equivalent to
1444 PREDS is the predicate chains, and N is the number of chains. */
1446 /* Helper function to implement rule 1 above. ONE_CHAIN is
1447 the AND predication to be simplified. */
1450 simplify_pred (pred_chain
*one_chain
)
1453 bool simplified
= false;
1454 pred_chain s_chain
= vNULL
;
1456 n
= one_chain
->length ();
1458 for (i
= 0; i
< n
; i
++)
1460 pred_info
*a_pred
= &(*one_chain
)[i
];
1462 if (!a_pred
->pred_lhs
)
1464 if (!is_neq_zero_form_p (*a_pred
))
1467 gimple def_stmt
= SSA_NAME_DEF_STMT (a_pred
->pred_lhs
);
1468 if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
1470 if (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
)
1472 for (j
= 0; j
< n
; j
++)
1474 pred_info
*b_pred
= &(*one_chain
)[j
];
1476 if (!b_pred
->pred_lhs
)
1478 if (!is_neq_zero_form_p (*b_pred
))
1481 if (pred_expr_equal_p (*b_pred
, gimple_assign_rhs1 (def_stmt
))
1482 || pred_expr_equal_p (*b_pred
, gimple_assign_rhs2 (def_stmt
)))
1484 /* Mark a_pred for removal. */
1485 a_pred
->pred_lhs
= NULL
;
1486 a_pred
->pred_rhs
= NULL
;
1497 for (i
= 0; i
< n
; i
++)
1499 pred_info
*a_pred
= &(*one_chain
)[i
];
1500 if (!a_pred
->pred_lhs
)
1502 s_chain
.safe_push (*a_pred
);
1505 one_chain
->release ();
1506 *one_chain
= s_chain
;
1509 /* The helper function implements the rule 2 for the
1512 2) (X AND Y) OR (!X AND Y) is equivalent to Y. */
1515 simplify_preds_2 (pred_chain_union
*preds
)
1518 bool simplified
= false;
1519 pred_chain_union s_preds
= vNULL
;
1521 /* (X AND Y) OR (!X AND Y) is equivalent to Y.
1522 (X AND Y) OR (X AND !Y) is equivalent to X. */
1524 n
= preds
->length ();
1525 for (i
= 0; i
< n
; i
++)
1528 pred_chain
*a_chain
= &(*preds
)[i
];
1530 if (a_chain
->length () != 2)
1536 for (j
= 0; j
< n
; j
++)
1538 pred_chain
*b_chain
;
1544 b_chain
= &(*preds
)[j
];
1545 if (b_chain
->length () != 2)
1551 if (pred_equal_p (x
, x2
) && pred_neg_p (y
, y2
))
1554 a_chain
->release ();
1555 b_chain
->release ();
1556 b_chain
->safe_push (x
);
1560 if (pred_neg_p (x
, x2
) && pred_equal_p (y
, y2
))
1563 a_chain
->release ();
1564 b_chain
->release ();
1565 b_chain
->safe_push (y
);
1571 /* Now clean up the chain. */
1574 for (i
= 0; i
< n
; i
++)
1576 if ((*preds
)[i
].is_empty ())
1578 s_preds
.safe_push ((*preds
)[i
]);
1588 /* The helper function implements the rule 2 for the
1591 3) x OR (!x AND y) is equivalent to x OR y. */
1594 simplify_preds_3 (pred_chain_union
*preds
)
1597 bool simplified
= false;
1599 /* Now iteratively simplify X OR (!X AND Z ..)
1600 into X OR (Z ...). */
1602 n
= preds
->length ();
1606 for (i
= 0; i
< n
; i
++)
1609 pred_chain
*a_chain
= &(*preds
)[i
];
1611 if (a_chain
->length () != 1)
1616 for (j
= 0; j
< n
; j
++)
1618 pred_chain
*b_chain
;
1625 b_chain
= &(*preds
)[j
];
1626 if (b_chain
->length () < 2)
1629 for (k
= 0; k
< b_chain
->length (); k
++)
1632 if (pred_neg_p (x
, x2
))
1634 b_chain
->unordered_remove (k
);
1644 /* The helper function implements the rule 4 for the
1647 2) ((x AND y) != 0) OR (x != 0 AND y != 0) is equivalent to
1648 (x != 0 ANd y != 0). */
1651 simplify_preds_4 (pred_chain_union
*preds
)
1654 bool simplified
= false;
1655 pred_chain_union s_preds
= vNULL
;
1658 n
= preds
->length ();
1659 for (i
= 0; i
< n
; i
++)
1662 pred_chain
*a_chain
= &(*preds
)[i
];
1664 if (a_chain
->length () != 1)
1669 if (!is_neq_zero_form_p (z
))
1672 def_stmt
= SSA_NAME_DEF_STMT (z
.pred_lhs
);
1673 if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
1676 if (gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
1679 for (j
= 0; j
< n
; j
++)
1681 pred_chain
*b_chain
;
1687 b_chain
= &(*preds
)[j
];
1688 if (b_chain
->length () != 2)
1693 if (!is_neq_zero_form_p (x2
)
1694 || !is_neq_zero_form_p (y2
))
1697 if ((pred_expr_equal_p (x2
, gimple_assign_rhs1 (def_stmt
))
1698 && pred_expr_equal_p (y2
, gimple_assign_rhs2 (def_stmt
)))
1699 || (pred_expr_equal_p (x2
, gimple_assign_rhs2 (def_stmt
))
1700 && pred_expr_equal_p (y2
, gimple_assign_rhs1 (def_stmt
))))
1703 a_chain
->release ();
1709 /* Now clean up the chain. */
1712 for (i
= 0; i
< n
; i
++)
1714 if ((*preds
)[i
].is_empty ())
1716 s_preds
.safe_push ((*preds
)[i
]);
1727 /* This function simplifies predicates in PREDS. */
1730 simplify_preds (pred_chain_union
*preds
, gimple use_or_def
, bool is_use
)
1733 bool changed
= false;
1735 if (dump_file
&& dump_flags
& TDF_DETAILS
)
1737 fprintf (dump_file
, "[BEFORE SIMPLICATION -- ");
1738 dump_predicates (use_or_def
, *preds
, is_use
? "[USE]:\n" : "[DEF]:\n");
1741 for (i
= 0; i
< preds
->length (); i
++)
1742 simplify_pred (&(*preds
)[i
]);
1744 n
= preds
->length ();
1751 if (simplify_preds_2 (preds
))
1754 /* Now iteratively simplify X OR (!X AND Z ..)
1755 into X OR (Z ...). */
1756 if (simplify_preds_3 (preds
))
1759 if (simplify_preds_4 (preds
))
1767 /* This is a helper function which attempts to normalize predicate chains
1768 by following UD chains. It basically builds up a big tree of either IOR
1769 operations or AND operations, and convert the IOR tree into a
1770 pred_chain_union or BIT_AND tree into a pred_chain.
1780 then _t != 0 will be normalized into a pred_chain_union
1782 (_2 RELOP1 _1) OR (_5 RELOP2 _4) OR (_8 RELOP3 _7) OR (_0 != 0)
1792 then _t != 0 will be normalized into a pred_chain:
1793 (_2 RELOP1 _1) AND (_5 RELOP2 _4) AND (_8 RELOP3 _7) AND (_0 != 0)
1797 /* This is a helper function that stores a PRED into NORM_PREDS. */
1800 push_pred (pred_chain_union
*norm_preds
, pred_info pred
)
1802 pred_chain pred_chain
= vNULL
;
1803 pred_chain
.safe_push (pred
);
1804 norm_preds
->safe_push (pred_chain
);
1807 /* A helper function that creates a predicate of the form
1808 OP != 0 and push it WORK_LIST. */
1811 push_to_worklist (tree op
, vec
<pred_info
, va_heap
, vl_ptr
> *work_list
,
1812 pointer_set_t
*mark_set
)
1814 if (pointer_set_contains (mark_set
, op
))
1816 pointer_set_insert (mark_set
, op
);
1819 arg_pred
.pred_lhs
= op
;
1820 arg_pred
.pred_rhs
= integer_zero_node
;
1821 arg_pred
.cond_code
= NE_EXPR
;
1822 arg_pred
.invert
= false;
1823 work_list
->safe_push (arg_pred
);
1826 /* A helper that generates a pred_info from a gimple assignment
1827 CMP_ASSIGN with comparison rhs. */
1830 get_pred_info_from_cmp (gimple cmp_assign
)
1833 n_pred
.pred_lhs
= gimple_assign_rhs1 (cmp_assign
);
1834 n_pred
.pred_rhs
= gimple_assign_rhs2 (cmp_assign
);
1835 n_pred
.cond_code
= gimple_assign_rhs_code (cmp_assign
);
1836 n_pred
.invert
= false;
1840 /* Returns true if the PHI is a degenerated phi with
1841 all args with the same value (relop). In that case, *PRED
1842 will be updated to that value. */
1845 is_degenerated_phi (gimple phi
, pred_info
*pred_p
)
1852 n
= gimple_phi_num_args (phi
);
1853 op0
= gimple_phi_arg_def (phi
, 0);
1855 if (TREE_CODE (op0
) != SSA_NAME
)
1858 def0
= SSA_NAME_DEF_STMT (op0
);
1859 if (gimple_code (def0
) != GIMPLE_ASSIGN
)
1861 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def0
))
1864 pred0
= get_pred_info_from_cmp (def0
);
1866 for (i
= 1; i
< n
; ++i
)
1870 tree op
= gimple_phi_arg_def (phi
, i
);
1872 if (TREE_CODE (op
) != SSA_NAME
)
1875 def
= SSA_NAME_DEF_STMT (op
);
1876 if (gimple_code (def
) != GIMPLE_ASSIGN
)
1878 if (TREE_CODE_CLASS (gimple_assign_rhs_code (def
))
1881 pred
= get_pred_info_from_cmp (def
);
1882 if (!pred_equal_p (pred
, pred0
))
1890 /* Normalize one predicate PRED
1891 1) if PRED can no longer be normlized, put it into NORM_PREDS.
1892 2) otherwise if PRED is of the form x != 0, follow x's definition
1893 and put normalized predicates into WORK_LIST. */
1896 normalize_one_pred_1 (pred_chain_union
*norm_preds
,
1897 pred_chain
*norm_chain
,
1899 enum tree_code and_or_code
,
1900 vec
<pred_info
, va_heap
, vl_ptr
> *work_list
,
1901 pointer_set_t
*mark_set
)
1903 if (!is_neq_zero_form_p (pred
))
1905 if (and_or_code
== BIT_IOR_EXPR
)
1906 push_pred (norm_preds
, pred
);
1908 norm_chain
->safe_push (pred
);
1912 gimple def_stmt
= SSA_NAME_DEF_STMT (pred
.pred_lhs
);
1914 if (gimple_code (def_stmt
) == GIMPLE_PHI
1915 && is_degenerated_phi (def_stmt
, &pred
))
1916 work_list
->safe_push (pred
);
1917 else if (gimple_code (def_stmt
) == GIMPLE_PHI
1918 && and_or_code
== BIT_IOR_EXPR
)
1921 n
= gimple_phi_num_args (def_stmt
);
1923 /* If we see non zero constant, we should punt. The predicate
1924 * should be one guarding the phi edge. */
1925 for (i
= 0; i
< n
; ++i
)
1927 tree op
= gimple_phi_arg_def (def_stmt
, i
);
1928 if (TREE_CODE (op
) == INTEGER_CST
&& !integer_zerop (op
))
1930 push_pred (norm_preds
, pred
);
1935 for (i
= 0; i
< n
; ++i
)
1937 tree op
= gimple_phi_arg_def (def_stmt
, i
);
1938 if (integer_zerop (op
))
1941 push_to_worklist (op
, work_list
, mark_set
);
1944 else if (gimple_code (def_stmt
) != GIMPLE_ASSIGN
)
1946 if (and_or_code
== BIT_IOR_EXPR
)
1947 push_pred (norm_preds
, pred
);
1949 norm_chain
->safe_push (pred
);
1951 else if (gimple_assign_rhs_code (def_stmt
) == and_or_code
)
1953 push_to_worklist (gimple_assign_rhs1 (def_stmt
), work_list
, mark_set
);
1954 push_to_worklist (gimple_assign_rhs2 (def_stmt
), work_list
, mark_set
);
1956 else if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
))
1959 pred_info n_pred
= get_pred_info_from_cmp (def_stmt
);
1960 if (and_or_code
== BIT_IOR_EXPR
)
1961 push_pred (norm_preds
, n_pred
);
1963 norm_chain
->safe_push (n_pred
);
1967 if (and_or_code
== BIT_IOR_EXPR
)
1968 push_pred (norm_preds
, pred
);
1970 norm_chain
->safe_push (pred
);
1974 /* Normalize PRED and store the normalized predicates into NORM_PREDS. */
1977 normalize_one_pred (pred_chain_union
*norm_preds
,
1980 vec
<pred_info
, va_heap
, vl_ptr
> work_list
= vNULL
;
1981 pointer_set_t
*mark_set
= NULL
;
1982 enum tree_code and_or_code
= ERROR_MARK
;
1983 pred_chain norm_chain
= vNULL
;
1985 if (!is_neq_zero_form_p (pred
))
1987 push_pred (norm_preds
, pred
);
1991 gimple def_stmt
= SSA_NAME_DEF_STMT (pred
.pred_lhs
);
1992 if (gimple_code (def_stmt
) == GIMPLE_ASSIGN
)
1993 and_or_code
= gimple_assign_rhs_code (def_stmt
);
1994 if (and_or_code
!= BIT_IOR_EXPR
1995 && and_or_code
!= BIT_AND_EXPR
)
1997 if (TREE_CODE_CLASS (and_or_code
)
2000 pred_info n_pred
= get_pred_info_from_cmp (def_stmt
);
2001 push_pred (norm_preds
, n_pred
);
2004 push_pred (norm_preds
, pred
);
2008 work_list
.safe_push (pred
);
2009 mark_set
= pointer_set_create ();
2011 while (!work_list
.is_empty ())
2013 pred_info a_pred
= work_list
.pop ();
2014 normalize_one_pred_1 (norm_preds
, &norm_chain
, a_pred
,
2015 and_or_code
, &work_list
, mark_set
);
2017 if (and_or_code
== BIT_AND_EXPR
)
2018 norm_preds
->safe_push (norm_chain
);
2020 work_list
.release ();
2021 pointer_set_destroy (mark_set
);
2025 normalize_one_pred_chain (pred_chain_union
*norm_preds
,
2026 pred_chain one_chain
)
2028 vec
<pred_info
, va_heap
, vl_ptr
> work_list
= vNULL
;
2029 pointer_set_t
*mark_set
= pointer_set_create ();
2030 pred_chain norm_chain
= vNULL
;
2033 for (i
= 0; i
< one_chain
.length (); i
++)
2035 work_list
.safe_push (one_chain
[i
]);
2036 pointer_set_insert (mark_set
, one_chain
[i
].pred_lhs
);
2039 while (!work_list
.is_empty ())
2041 pred_info a_pred
= work_list
.pop ();
2042 normalize_one_pred_1 (0, &norm_chain
, a_pred
,
2043 BIT_AND_EXPR
, &work_list
, mark_set
);
2046 norm_preds
->safe_push (norm_chain
);
2047 work_list
.release ();
2048 pointer_set_destroy (mark_set
);
2051 /* Normalize predicate chains PREDS and returns the normalized one. */
2053 static pred_chain_union
2054 normalize_preds (pred_chain_union preds
, gimple use_or_def
, bool is_use
)
2056 pred_chain_union norm_preds
= vNULL
;
2057 size_t n
= preds
.length ();
2060 if (dump_file
&& dump_flags
& TDF_DETAILS
)
2062 fprintf (dump_file
, "[BEFORE NORMALIZATION --");
2063 dump_predicates (use_or_def
, preds
, is_use
? "[USE]:\n" : "[DEF]:\n");
2066 for (i
= 0; i
< n
; i
++)
2068 if (preds
[i
].length () != 1)
2069 normalize_one_pred_chain (&norm_preds
, preds
[i
]);
2072 normalize_one_pred (&norm_preds
, preds
[i
][0]);
2073 preds
[i
].release ();
2079 fprintf (dump_file
, "[AFTER NORMALIZATION -- ");
2080 dump_predicates (use_or_def
, norm_preds
, is_use
? "[USE]:\n" : "[DEF]:\n");
2088 /* Computes the predicates that guard the use and checks
2089 if the incoming paths that have empty (or possibly
2090 empty) definition can be pruned/filtered. The function returns
2091 true if it can be determined that the use of PHI's def in
2092 USE_STMT is guarded with a predicate set not overlapping with
2093 predicate sets of all runtime paths that do not have a definition.
2094 Returns false if it is not or it can not be determined. USE_BB is
2095 the bb of the use (for phi operand use, the bb is not the bb of
2096 the phi stmt, but the src bb of the operand edge). UNINIT_OPNDS
2097 is a bit vector. If an operand of PHI is uninitialized, the
2098 corresponding bit in the vector is 1. VISIED_PHIS is a pointer
2099 set of phis being visted. */
2102 is_use_properly_guarded (gimple use_stmt
,
2105 unsigned uninit_opnds
,
2106 pointer_set_t
*visited_phis
)
2109 pred_chain_union preds
= vNULL
;
2110 pred_chain_union def_preds
= vNULL
;
2111 bool has_valid_preds
= false;
2112 bool is_properly_guarded
= false;
2114 if (pointer_set_insert (visited_phis
, phi
))
2117 phi_bb
= gimple_bb (phi
);
2119 if (is_non_loop_exit_postdominating (use_bb
, phi_bb
))
2122 has_valid_preds
= find_predicates (&preds
, phi_bb
, use_bb
);
2124 if (!has_valid_preds
)
2126 destroy_predicate_vecs (preds
);
2130 /* Try to prune the dead incoming phi edges. */
2132 = use_pred_not_overlap_with_undef_path_pred (preds
, phi
, uninit_opnds
,
2135 if (is_properly_guarded
)
2137 destroy_predicate_vecs (preds
);
2141 has_valid_preds
= find_def_preds (&def_preds
, phi
);
2143 if (!has_valid_preds
)
2145 destroy_predicate_vecs (preds
);
2146 destroy_predicate_vecs (def_preds
);
2150 simplify_preds (&preds
, use_stmt
, true);
2151 preds
= normalize_preds (preds
, use_stmt
, true);
2153 simplify_preds (&def_preds
, phi
, false);
2154 def_preds
= normalize_preds (def_preds
, phi
, false);
2156 is_properly_guarded
= is_superset_of (def_preds
, preds
);
2158 destroy_predicate_vecs (preds
);
2159 destroy_predicate_vecs (def_preds
);
2160 return is_properly_guarded
;
2163 /* Searches through all uses of a potentially
2164 uninitialized variable defined by PHI and returns a use
2165 statement if the use is not properly guarded. It returns
2166 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
2167 holding the position(s) of uninit PHI operands. WORKLIST
2168 is the vector of candidate phis that may be updated by this
2169 function. ADDED_TO_WORKLIST is the pointer set tracking
2170 if the new phi is already in the worklist. */
2173 find_uninit_use (gimple phi
, unsigned uninit_opnds
,
2174 vec
<gimple
> *worklist
,
2175 pointer_set_t
*added_to_worklist
)
2178 use_operand_p use_p
;
2180 imm_use_iterator iter
;
2182 phi_result
= gimple_phi_result (phi
);
2184 FOR_EACH_IMM_USE_FAST (use_p
, iter
, phi_result
)
2186 pointer_set_t
*visited_phis
;
2189 use_stmt
= USE_STMT (use_p
);
2190 if (is_gimple_debug (use_stmt
))
2193 visited_phis
= pointer_set_create ();
2195 if (gimple_code (use_stmt
) == GIMPLE_PHI
)
2196 use_bb
= gimple_phi_arg_edge (use_stmt
,
2197 PHI_ARG_INDEX_FROM_USE (use_p
))->src
;
2199 use_bb
= gimple_bb (use_stmt
);
2201 if (is_use_properly_guarded (use_stmt
, use_bb
, phi
, uninit_opnds
,
2204 pointer_set_destroy (visited_phis
);
2207 pointer_set_destroy (visited_phis
);
2209 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2211 fprintf (dump_file
, "[CHECK]: Found unguarded use: ");
2212 print_gimple_stmt (dump_file
, use_stmt
, 0, 0);
2214 /* Found one real use, return. */
2215 if (gimple_code (use_stmt
) != GIMPLE_PHI
)
2218 /* Found a phi use that is not guarded,
2219 add the phi to the worklist. */
2220 if (!pointer_set_insert (added_to_worklist
, use_stmt
))
2222 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2224 fprintf (dump_file
, "[WORKLIST]: Update worklist with phi: ");
2225 print_gimple_stmt (dump_file
, use_stmt
, 0, 0);
2228 worklist
->safe_push (use_stmt
);
2229 pointer_set_insert (possibly_undefined_names
, phi_result
);
2236 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
2237 and gives warning if there exists a runtime path from the entry to a
2238 use of the PHI def that does not contain a definition. In other words,
2239 the warning is on the real use. The more dead paths that can be pruned
2240 by the compiler, the fewer false positives the warning is. WORKLIST
2241 is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
2242 a pointer set tracking if the new phi is added to the worklist or not. */
2245 warn_uninitialized_phi (gimple phi
, vec
<gimple
> *worklist
,
2246 pointer_set_t
*added_to_worklist
)
2248 unsigned uninit_opnds
;
2249 gimple uninit_use_stmt
= 0;
2252 /* Don't look at virtual operands. */
2253 if (virtual_operand_p (gimple_phi_result (phi
)))
2256 uninit_opnds
= compute_uninit_opnds_pos (phi
);
2258 if (MASK_EMPTY (uninit_opnds
))
2261 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2263 fprintf (dump_file
, "[CHECK]: examining phi: ");
2264 print_gimple_stmt (dump_file
, phi
, 0, 0);
2267 /* Now check if we have any use of the value without proper guard. */
2268 uninit_use_stmt
= find_uninit_use (phi
, uninit_opnds
,
2269 worklist
, added_to_worklist
);
2271 /* All uses are properly guarded. */
2272 if (!uninit_use_stmt
)
2275 uninit_op
= gimple_phi_arg_def (phi
, MASK_FIRST_SET_BIT (uninit_opnds
));
2276 if (SSA_NAME_VAR (uninit_op
) == NULL_TREE
)
2278 warn_uninit (OPT_Wmaybe_uninitialized
, uninit_op
, SSA_NAME_VAR (uninit_op
),
2279 SSA_NAME_VAR (uninit_op
),
2280 "%qD may be used uninitialized in this function",
2286 /* Entry point to the late uninitialized warning pass. */
2289 execute_late_warn_uninitialized (void)
2292 gimple_stmt_iterator gsi
;
2293 vec
<gimple
> worklist
= vNULL
;
2294 pointer_set_t
*added_to_worklist
;
2296 calculate_dominance_info (CDI_DOMINATORS
);
2297 calculate_dominance_info (CDI_POST_DOMINATORS
);
2298 /* Re-do the plain uninitialized variable check, as optimization may have
2299 straightened control flow. Do this first so that we don't accidentally
2300 get a "may be" warning when we'd have seen an "is" warning later. */
2301 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
2303 timevar_push (TV_TREE_UNINIT
);
2305 possibly_undefined_names
= pointer_set_create ();
2306 added_to_worklist
= pointer_set_create ();
2308 /* Initialize worklist */
2309 FOR_EACH_BB_FN (bb
, cfun
)
2310 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2312 gimple phi
= gsi_stmt (gsi
);
2315 n
= gimple_phi_num_args (phi
);
2317 /* Don't look at virtual operands. */
2318 if (virtual_operand_p (gimple_phi_result (phi
)))
2321 for (i
= 0; i
< n
; ++i
)
2323 tree op
= gimple_phi_arg_def (phi
, i
);
2324 if (TREE_CODE (op
) == SSA_NAME
2325 && uninit_undefined_value_p (op
))
2327 worklist
.safe_push (phi
);
2328 pointer_set_insert (added_to_worklist
, phi
);
2329 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2331 fprintf (dump_file
, "[WORKLIST]: add to initial list: ");
2332 print_gimple_stmt (dump_file
, phi
, 0, 0);
2339 while (worklist
.length () != 0)
2342 cur_phi
= worklist
.pop ();
2343 warn_uninitialized_phi (cur_phi
, &worklist
, added_to_worklist
);
2346 worklist
.release ();
2347 pointer_set_destroy (added_to_worklist
);
2348 pointer_set_destroy (possibly_undefined_names
);
2349 possibly_undefined_names
= NULL
;
2350 free_dominance_info (CDI_POST_DOMINATORS
);
2351 timevar_pop (TV_TREE_UNINIT
);
2356 gate_warn_uninitialized (void)
2358 return warn_uninitialized
|| warn_maybe_uninitialized
;
2363 const pass_data pass_data_late_warn_uninitialized
=
2365 GIMPLE_PASS
, /* type */
2366 "uninit", /* name */
2367 OPTGROUP_NONE
, /* optinfo_flags */
2368 true, /* has_gate */
2369 true, /* has_execute */
2370 TV_NONE
, /* tv_id */
2371 PROP_ssa
, /* properties_required */
2372 0, /* properties_provided */
2373 0, /* properties_destroyed */
2374 0, /* todo_flags_start */
2375 0, /* todo_flags_finish */
2378 class pass_late_warn_uninitialized
: public gimple_opt_pass
2381 pass_late_warn_uninitialized (gcc::context
*ctxt
)
2382 : gimple_opt_pass (pass_data_late_warn_uninitialized
, ctxt
)
2385 /* opt_pass methods: */
2386 opt_pass
* clone () { return new pass_late_warn_uninitialized (m_ctxt
); }
2387 bool gate () { return gate_warn_uninitialized (); }
2388 unsigned int execute () { return execute_late_warn_uninitialized (); }
2390 }; // class pass_late_warn_uninitialized
2395 make_pass_late_warn_uninitialized (gcc::context
*ctxt
)
2397 return new pass_late_warn_uninitialized (ctxt
);
2402 execute_early_warn_uninitialized (void)
2404 /* Currently, this pass runs always but
2405 execute_late_warn_uninitialized only runs with optimization. With
2406 optimization we want to warn about possible uninitialized as late
2407 as possible, thus don't do it here. However, without
2408 optimization we need to warn here about "may be uninitialized". */
2409 calculate_dominance_info (CDI_POST_DOMINATORS
);
2411 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/!optimize
);
2413 /* Post-dominator information can not be reliably updated. Free it
2416 free_dominance_info (CDI_POST_DOMINATORS
);
2423 const pass_data pass_data_early_warn_uninitialized
=
2425 GIMPLE_PASS
, /* type */
2426 "*early_warn_uninitialized", /* name */
2427 OPTGROUP_NONE
, /* optinfo_flags */
2428 true, /* has_gate */
2429 true, /* has_execute */
2430 TV_TREE_UNINIT
, /* tv_id */
2431 PROP_ssa
, /* properties_required */
2432 0, /* properties_provided */
2433 0, /* properties_destroyed */
2434 0, /* todo_flags_start */
2435 0, /* todo_flags_finish */
2438 class pass_early_warn_uninitialized
: public gimple_opt_pass
2441 pass_early_warn_uninitialized (gcc::context
*ctxt
)
2442 : gimple_opt_pass (pass_data_early_warn_uninitialized
, ctxt
)
2445 /* opt_pass methods: */
2446 bool gate () { return gate_warn_uninitialized (); }
2447 unsigned int execute () { return execute_early_warn_uninitialized (); }
2449 }; // class pass_early_warn_uninitialized
2454 make_pass_early_warn_uninitialized (gcc::context
*ctxt
)
2456 return new pass_early_warn_uninitialized (ctxt
);