fix pr/45972
[official-gcc.git] / gcc / tree-ssa-uninit.c
blob78b88e9bd9345db54ef51e063ae3cda067b1bd19
1 /* Predicate aware uninitialized variable warning.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2010 Free Software
3 Foundation, Inc.
4 Contributed by Xinliang David Li <davidxl@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "tm_p.h"
29 #include "langhooks.h"
30 #include "basic-block.h"
31 #include "output.h"
32 #include "function.h"
33 #include "gimple-pretty-print.h"
34 #include "bitmap.h"
35 #include "pointer-set.h"
36 #include "tree-flow.h"
37 #include "gimple.h"
38 #include "tree-inline.h"
39 #include "timevar.h"
40 #include "hashtab.h"
41 #include "tree-dump.h"
42 #include "tree-pass.h"
43 #include "diagnostic-core.h"
44 #include "toplev.h"
45 #include "timevar.h"
47 /* This implements the pass that does predicate aware warning on uses of
48 possibly uninitialized variables. The pass first collects the set of
49 possibly uninitialized SSA names. For each such name, it walks through
50 all its immediate uses. For each immediate use, it rebuilds the condition
51 expression (the predicate) that guards the use. The predicate is then
52 examined to see if the variable is always defined under that same condition.
53 This is done either by pruning the unrealizable paths that lead to the
54 default definitions or by checking if the predicate set that guards the
55 defining paths is a superset of the use predicate. */
58 /* Pointer set of potentially undefined ssa names, i.e.,
59 ssa names that are defined by phi with operands that
60 are not defined or potentially undefined. */
61 static struct pointer_set_t *possibly_undefined_names = 0;
63 /* Bit mask handling macros. */
64 #define MASK_SET_BIT(mask, pos) mask |= (1 << pos)
65 #define MASK_TEST_BIT(mask, pos) (mask & (1 << pos))
66 #define MASK_EMPTY(mask) (mask == 0)
68 /* Returns the first bit position (starting from LSB)
69 in mask that is non zero. Returns -1 if the mask is empty. */
70 static int
71 get_mask_first_set_bit (unsigned mask)
73 int pos = 0;
74 if (mask == 0)
75 return -1;
77 while ((mask & (1 << pos)) == 0)
78 pos++;
80 return pos;
82 #define MASK_FIRST_SET_BIT(mask) get_mask_first_set_bit (mask)
85 /* Return true if T, an SSA_NAME, has an undefined value. */
87 bool
88 ssa_undefined_value_p (tree t)
90 tree var = SSA_NAME_VAR (t);
92 /* Parameters get their initial value from the function entry. */
93 if (TREE_CODE (var) == PARM_DECL)
94 return false;
96 /* When returning by reference the return address is actually a hidden
97 parameter. */
98 if (TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL
99 && DECL_BY_REFERENCE (SSA_NAME_VAR (t)))
100 return false;
102 /* Hard register variables get their initial value from the ether. */
103 if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
104 return false;
106 /* The value is undefined iff its definition statement is empty. */
107 return (gimple_nop_p (SSA_NAME_DEF_STMT (t))
108 || (possibly_undefined_names
109 && pointer_set_contains (possibly_undefined_names, t)));
112 /* Checks if the operand OPND of PHI is defined by
113 another phi with one operand defined by this PHI,
114 but the rest operands are all defined. If yes,
115 returns true to skip this this operand as being
116 redundant. Can be enhanced to be more general. */
118 static bool
119 can_skip_redundant_opnd (tree opnd, gimple phi)
121 gimple op_def;
122 tree phi_def;
123 int i, n;
125 phi_def = gimple_phi_result (phi);
126 op_def = SSA_NAME_DEF_STMT (opnd);
127 if (gimple_code (op_def) != GIMPLE_PHI)
128 return false;
129 n = gimple_phi_num_args (op_def);
130 for (i = 0; i < n; ++i)
132 tree op = gimple_phi_arg_def (op_def, i);
133 if (TREE_CODE (op) != SSA_NAME)
134 continue;
135 if (op != phi_def && ssa_undefined_value_p (op))
136 return false;
139 return true;
142 /* Returns a bit mask holding the positions of arguments in PHI
143 that have empty (or possibly empty) definitions. */
145 static unsigned
146 compute_uninit_opnds_pos (gimple phi)
148 size_t i, n;
149 unsigned uninit_opnds = 0;
151 n = gimple_phi_num_args (phi);
152 /* Bail out for phi with too many args. */
153 if (n > 32)
154 return 0;
156 for (i = 0; i < n; ++i)
158 tree op = gimple_phi_arg_def (phi, i);
159 if (TREE_CODE (op) == SSA_NAME
160 && ssa_undefined_value_p (op)
161 && !can_skip_redundant_opnd (op, phi))
162 MASK_SET_BIT (uninit_opnds, i);
164 return uninit_opnds;
167 /* Find the immediate postdominator PDOM of the specified
168 basic block BLOCK. */
170 static inline basic_block
171 find_pdom (basic_block block)
173 if (block == EXIT_BLOCK_PTR)
174 return EXIT_BLOCK_PTR;
175 else
177 basic_block bb
178 = get_immediate_dominator (CDI_POST_DOMINATORS, block);
179 if (! bb)
180 return EXIT_BLOCK_PTR;
181 return bb;
185 /* Find the immediate DOM of the specified
186 basic block BLOCK. */
188 static inline basic_block
189 find_dom (basic_block block)
191 if (block == ENTRY_BLOCK_PTR)
192 return ENTRY_BLOCK_PTR;
193 else
195 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, block);
196 if (! bb)
197 return ENTRY_BLOCK_PTR;
198 return bb;
202 /* Returns true if BB1 is postdominating BB2 and BB1 is
203 not a loop exit bb. The loop exit bb check is simple and does
204 not cover all cases. */
206 static bool
207 is_non_loop_exit_postdominating (basic_block bb1, basic_block bb2)
209 if (!dominated_by_p (CDI_POST_DOMINATORS, bb2, bb1))
210 return false;
212 if (single_pred_p (bb1) && !single_succ_p (bb2))
213 return false;
215 return true;
218 /* Find the closest postdominator of a specified BB, which is control
219 equivalent to BB. */
221 static inline basic_block
222 find_control_equiv_block (basic_block bb)
224 basic_block pdom;
226 pdom = find_pdom (bb);
228 /* Skip the postdominating bb that is also loop exit. */
229 if (!is_non_loop_exit_postdominating (pdom, bb))
230 return NULL;
232 if (dominated_by_p (CDI_DOMINATORS, pdom, bb))
233 return pdom;
235 return NULL;
238 #define MAX_NUM_CHAINS 8
239 #define MAX_CHAIN_LEN 5
241 /* Computes the control dependence chains (paths of edges)
242 for DEP_BB up to the dominating basic block BB (the head node of a
243 chain should be dominated by it). CD_CHAINS is pointer to a
244 dynamic array holding the result chains. CUR_CD_CHAIN is the current
245 chain being computed. *NUM_CHAINS is total number of chains. The
246 function returns true if the information is successfully computed,
247 return false if there is no control dependence or not computed. */
249 static bool
250 compute_control_dep_chain (basic_block bb, basic_block dep_bb,
251 VEC(edge, heap) **cd_chains,
252 size_t *num_chains,
253 VEC(edge, heap) **cur_cd_chain)
255 edge_iterator ei;
256 edge e;
257 size_t i;
258 bool found_cd_chain = false;
259 size_t cur_chain_len = 0;
261 if (EDGE_COUNT (bb->succs) < 2)
262 return false;
264 /* Could use a set instead. */
265 cur_chain_len = VEC_length (edge, *cur_cd_chain);
266 if (cur_chain_len > MAX_CHAIN_LEN)
267 return false;
269 for (i = 0; i < cur_chain_len; i++)
271 edge e = VEC_index (edge, *cur_cd_chain, i);
272 /* cycle detected. */
273 if (e->src == bb)
274 return false;
277 FOR_EACH_EDGE (e, ei, bb->succs)
279 basic_block cd_bb;
280 if (e->flags & (EDGE_FAKE | EDGE_ABNORMAL))
281 continue;
283 cd_bb = e->dest;
284 VEC_safe_push (edge, heap, *cur_cd_chain, e);
285 while (!is_non_loop_exit_postdominating (cd_bb, bb))
287 if (cd_bb == dep_bb)
289 /* Found a direct control dependence. */
290 if (*num_chains < MAX_NUM_CHAINS)
292 cd_chains[*num_chains]
293 = VEC_copy (edge, heap, *cur_cd_chain);
294 (*num_chains)++;
296 found_cd_chain = true;
297 /* check path from next edge. */
298 break;
301 /* Now check if DEP_BB is indirectly control dependent on BB. */
302 if (compute_control_dep_chain (cd_bb, dep_bb, cd_chains,
303 num_chains, cur_cd_chain))
305 found_cd_chain = true;
306 break;
309 cd_bb = find_pdom (cd_bb);
310 if (cd_bb == EXIT_BLOCK_PTR)
311 break;
313 VEC_pop (edge, *cur_cd_chain);
314 gcc_assert (VEC_length (edge, *cur_cd_chain) == cur_chain_len);
316 gcc_assert (VEC_length (edge, *cur_cd_chain) == cur_chain_len);
318 return found_cd_chain;
321 typedef struct use_pred_info
323 gimple cond;
324 bool invert;
325 } *use_pred_info_t;
327 DEF_VEC_P(use_pred_info_t);
328 DEF_VEC_ALLOC_P(use_pred_info_t, heap);
331 /* Converts the chains of control dependence edges into a set of
332 predicates. A control dependence chain is represented by a vector
333 edges. DEP_CHAINS points to an array of dependence chains.
334 NUM_CHAINS is the size of the chain array. One edge in a dependence
335 chain is mapped to predicate expression represented by use_pred_info_t
336 type. One dependence chain is converted to a composite predicate that
337 is the result of AND operation of use_pred_info_t mapped to each edge.
338 A composite predicate is presented by a vector of use_pred_info_t. On
339 return, *PREDS points to the resulting array of composite predicates.
340 *NUM_PREDS is the number of composite predictes. */
342 static bool
343 convert_control_dep_chain_into_preds (VEC(edge, heap) **dep_chains,
344 size_t num_chains,
345 VEC(use_pred_info_t, heap) ***preds,
346 size_t *num_preds)
348 bool has_valid_pred = false;
349 size_t i, j;
350 if (num_chains == 0 || num_chains >= MAX_NUM_CHAINS)
351 return false;
353 /* Now convert CD chains into predicates */
354 has_valid_pred = true;
356 /* Now convert the control dep chain into a set
357 of predicates. */
358 *preds = XCNEWVEC (VEC(use_pred_info_t, heap) *,
359 num_chains);
360 *num_preds = num_chains;
362 for (i = 0; i < num_chains; i++)
364 VEC(edge, heap) *one_cd_chain = dep_chains[i];
365 for (j = 0; j < VEC_length (edge, one_cd_chain); j++)
367 gimple cond_stmt;
368 gimple_stmt_iterator gsi;
369 basic_block guard_bb;
370 use_pred_info_t one_pred;
371 edge e;
373 e = VEC_index (edge, one_cd_chain, j);
374 guard_bb = e->src;
375 gsi = gsi_last_bb (guard_bb);
376 if (gsi_end_p (gsi))
378 has_valid_pred = false;
379 break;
381 cond_stmt = gsi_stmt (gsi);
382 if (gimple_code (cond_stmt) == GIMPLE_CALL
383 && EDGE_COUNT (e->src->succs) >= 2)
385 /* Ignore EH edge. Can add assertion
386 on the other edge's flag. */
387 continue;
389 /* Skip if there is essentially one succesor. */
390 if (EDGE_COUNT (e->src->succs) == 2)
392 edge e1;
393 edge_iterator ei1;
394 bool skip = false;
396 FOR_EACH_EDGE (e1, ei1, e->src->succs)
398 if (EDGE_COUNT (e1->dest->succs) == 0)
400 skip = true;
401 break;
404 if (skip)
405 continue;
407 if (gimple_code (cond_stmt) != GIMPLE_COND)
409 has_valid_pred = false;
410 break;
412 one_pred = XNEW (struct use_pred_info);
413 one_pred->cond = cond_stmt;
414 one_pred->invert = !!(e->flags & EDGE_FALSE_VALUE);
415 VEC_safe_push (use_pred_info_t, heap, (*preds)[i], one_pred);
418 if (!has_valid_pred)
419 break;
421 return has_valid_pred;
424 /* Computes all control dependence chains for USE_BB. The control
425 dependence chains are then converted to an array of composite
426 predicates pointed to by PREDS. PHI_BB is the basic block of
427 the phi whose result is used in USE_BB. */
429 static bool
430 find_predicates (VEC(use_pred_info_t, heap) ***preds,
431 size_t *num_preds,
432 basic_block phi_bb,
433 basic_block use_bb)
435 size_t num_chains = 0, i;
436 VEC(edge, heap) **dep_chains = 0;
437 VEC(edge, heap) *cur_chain = 0;
438 bool has_valid_pred = false;
439 basic_block cd_root = 0;
441 dep_chains = XCNEWVEC (VEC(edge, heap) *, MAX_NUM_CHAINS);
443 /* First find the closest bb that is control equivalent to PHI_BB
444 that also dominates USE_BB. */
445 cd_root = phi_bb;
446 while (dominated_by_p (CDI_DOMINATORS, use_bb, cd_root))
448 basic_block ctrl_eq_bb = find_control_equiv_block (cd_root);
449 if (ctrl_eq_bb && dominated_by_p (CDI_DOMINATORS, use_bb, ctrl_eq_bb))
450 cd_root = ctrl_eq_bb;
451 else
452 break;
455 compute_control_dep_chain (cd_root, use_bb,
456 dep_chains, &num_chains,
457 &cur_chain);
459 has_valid_pred
460 = convert_control_dep_chain_into_preds (dep_chains,
461 num_chains,
462 preds,
463 num_preds);
464 /* Free individual chain */
465 VEC_free (edge, heap, cur_chain);
466 for (i = 0; i < num_chains; i++)
467 VEC_free (edge, heap, dep_chains[i]);
468 free (dep_chains);
469 return has_valid_pred;
472 /* Computes the set of incoming edges of PHI that have non empty
473 definitions of a phi chain. The collection will be done
474 recursively on operands that are defined by phis. CD_ROOT
475 is the control dependence root. *EDGES holds the result, and
476 VISITED_PHIS is a pointer set for detecting cycles. */
478 static void
479 collect_phi_def_edges (gimple phi, basic_block cd_root,
480 VEC(edge, heap) **edges,
481 struct pointer_set_t *visited_phis)
483 size_t i, n;
484 edge opnd_edge;
485 tree opnd;
487 if (pointer_set_insert (visited_phis, phi))
488 return;
490 n = gimple_phi_num_args (phi);
491 for (i = 0; i < n; i++)
493 opnd_edge = gimple_phi_arg_edge (phi, i);
494 opnd = gimple_phi_arg_def (phi, i);
496 if (TREE_CODE (opnd) != SSA_NAME)
498 if (dump_file && (dump_flags & TDF_DETAILS))
500 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
501 print_gimple_stmt (dump_file, phi, 0, 0);
503 VEC_safe_push (edge, heap, *edges, opnd_edge);
505 else
507 gimple def = SSA_NAME_DEF_STMT (opnd);
509 if (gimple_code (def) == GIMPLE_PHI
510 && dominated_by_p (CDI_DOMINATORS,
511 gimple_bb (def), cd_root))
512 collect_phi_def_edges (def, cd_root, edges,
513 visited_phis);
514 else if (!ssa_undefined_value_p (opnd))
516 if (dump_file && (dump_flags & TDF_DETAILS))
518 fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int)i);
519 print_gimple_stmt (dump_file, phi, 0, 0);
521 VEC_safe_push (edge, heap, *edges, opnd_edge);
527 /* For each use edge of PHI, computes all control dependence chains.
528 The control dependence chains are then converted to an array of
529 composite predicates pointed to by PREDS. */
531 static bool
532 find_def_preds (VEC(use_pred_info_t, heap) ***preds,
533 size_t *num_preds, gimple phi)
535 size_t num_chains = 0, i, n;
536 VEC(edge, heap) **dep_chains = 0;
537 VEC(edge, heap) *cur_chain = 0;
538 VEC(edge, heap) *def_edges = 0;
539 bool has_valid_pred = false;
540 basic_block phi_bb, cd_root = 0;
541 struct pointer_set_t *visited_phis;
543 dep_chains = XCNEWVEC (VEC(edge, heap) *, MAX_NUM_CHAINS);
545 phi_bb = gimple_bb (phi);
546 /* First find the closest dominating bb to be
547 the control dependence root */
548 cd_root = find_dom (phi_bb);
549 if (!cd_root)
550 return false;
552 visited_phis = pointer_set_create ();
553 collect_phi_def_edges (phi, cd_root, &def_edges, visited_phis);
554 pointer_set_destroy (visited_phis);
556 n = VEC_length (edge, def_edges);
557 if (n == 0)
558 return false;
560 for (i = 0; i < n; i++)
562 size_t prev_nc, j;
563 edge opnd_edge;
565 opnd_edge = VEC_index (edge, def_edges, i);
566 prev_nc = num_chains;
567 compute_control_dep_chain (cd_root, opnd_edge->src,
568 dep_chains, &num_chains,
569 &cur_chain);
570 /* Free individual chain */
571 VEC_free (edge, heap, cur_chain);
572 cur_chain = 0;
574 /* Now update the newly added chains with
575 the phi operand edge: */
576 if (EDGE_COUNT (opnd_edge->src->succs) > 1)
578 if (prev_nc == num_chains
579 && num_chains < MAX_NUM_CHAINS)
580 num_chains++;
581 for (j = prev_nc; j < num_chains; j++)
583 VEC_safe_push (edge, heap, dep_chains[j], opnd_edge);
588 has_valid_pred
589 = convert_control_dep_chain_into_preds (dep_chains,
590 num_chains,
591 preds,
592 num_preds);
593 for (i = 0; i < num_chains; i++)
594 VEC_free (edge, heap, dep_chains[i]);
595 free (dep_chains);
596 return has_valid_pred;
599 /* Dumps the predicates (PREDS) for USESTMT. */
601 static void
602 dump_predicates (gimple usestmt, size_t num_preds,
603 VEC(use_pred_info_t, heap) **preds,
604 const char* msg)
606 size_t i, j;
607 VEC(use_pred_info_t, heap) *one_pred_chain;
608 fprintf (dump_file, msg);
609 print_gimple_stmt (dump_file, usestmt, 0, 0);
610 fprintf (dump_file, "is guarded by :\n");
611 /* do some dumping here: */
612 for (i = 0; i < num_preds; i++)
614 size_t np;
616 one_pred_chain = preds[i];
617 np = VEC_length (use_pred_info_t, one_pred_chain);
619 for (j = 0; j < np; j++)
621 use_pred_info_t one_pred
622 = VEC_index (use_pred_info_t, one_pred_chain, j);
623 if (one_pred->invert)
624 fprintf (dump_file, " (.NOT.) ");
625 print_gimple_stmt (dump_file, one_pred->cond, 0, 0);
626 if (j < np - 1)
627 fprintf (dump_file, "(.AND.)\n");
629 if (i < num_preds - 1)
630 fprintf (dump_file, "(.OR.)\n");
634 /* Destroys the predicate set *PREDS. */
636 static void
637 destroy_predicate_vecs (size_t n,
638 VEC(use_pred_info_t, heap) ** preds)
640 size_t i, j;
641 for (i = 0; i < n; i++)
643 for (j = 0; j < VEC_length (use_pred_info_t, preds[i]); j++)
644 free (VEC_index (use_pred_info_t, preds[i], j));
645 VEC_free (use_pred_info_t, heap, preds[i]);
647 free (preds);
651 /* Computes the 'normalized' conditional code with operand
652 swapping and condition inversion. */
654 static enum tree_code
655 get_cmp_code (enum tree_code orig_cmp_code,
656 bool swap_cond, bool invert)
658 enum tree_code tc = orig_cmp_code;
660 if (swap_cond)
661 tc = swap_tree_comparison (orig_cmp_code);
662 if (invert)
663 tc = invert_tree_comparison (tc, false);
665 switch (tc)
667 case LT_EXPR:
668 case LE_EXPR:
669 case GT_EXPR:
670 case GE_EXPR:
671 case EQ_EXPR:
672 case NE_EXPR:
673 break;
674 default:
675 return ERROR_MARK;
677 return tc;
680 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
681 all values in the range satisfies (x CMPC BOUNDARY) == true. */
683 static bool
684 is_value_included_in (tree val, tree boundary, enum tree_code cmpc)
686 bool inverted = false;
687 bool is_unsigned;
688 bool result;
690 /* Only handle integer constant here. */
691 if (TREE_CODE (val) != INTEGER_CST
692 || TREE_CODE (boundary) != INTEGER_CST)
693 return true;
695 is_unsigned = TYPE_UNSIGNED (TREE_TYPE (val));
697 if (cmpc == GE_EXPR || cmpc == GT_EXPR
698 || cmpc == NE_EXPR)
700 cmpc = invert_tree_comparison (cmpc, false);
701 inverted = true;
704 if (is_unsigned)
706 if (cmpc == EQ_EXPR)
707 result = tree_int_cst_equal (val, boundary);
708 else if (cmpc == LT_EXPR)
709 result = INT_CST_LT_UNSIGNED (val, boundary);
710 else
712 gcc_assert (cmpc == LE_EXPR);
713 result = (tree_int_cst_equal (val, boundary)
714 || INT_CST_LT_UNSIGNED (val, boundary));
717 else
719 if (cmpc == EQ_EXPR)
720 result = tree_int_cst_equal (val, boundary);
721 else if (cmpc == LT_EXPR)
722 result = INT_CST_LT (val, boundary);
723 else
725 gcc_assert (cmpc == LE_EXPR);
726 result = (tree_int_cst_equal (val, boundary)
727 || INT_CST_LT (val, boundary));
731 if (inverted)
732 result ^= 1;
734 return result;
737 /* Returns true if PRED is common among all the predicate
738 chains (PREDS) (and therefore can be factored out).
739 NUM_PRED_CHAIN is the size of array PREDS. */
741 static bool
742 find_matching_predicate_in_rest_chains (use_pred_info_t pred,
743 VEC(use_pred_info_t, heap) **preds,
744 size_t num_pred_chains)
746 size_t i, j, n;
748 /* trival case */
749 if (num_pred_chains == 1)
750 return true;
752 for (i = 1; i < num_pred_chains; i++)
754 bool found = false;
755 VEC(use_pred_info_t, heap) *one_chain = preds[i];
756 n = VEC_length (use_pred_info_t, one_chain);
757 for (j = 0; j < n; j++)
759 use_pred_info_t pred2
760 = VEC_index (use_pred_info_t, one_chain, j);
761 /* can relax the condition comparison to not
762 use address comparison. However, the most common
763 case is that multiple control dependent paths share
764 a common path prefix, so address comparison should
765 be ok. */
767 if (pred2->cond == pred->cond
768 && pred2->invert == pred->invert)
770 found = true;
771 break;
774 if (!found)
775 return false;
777 return true;
780 /* Forward declaration. */
781 static bool
782 is_use_properly_guarded (gimple use_stmt,
783 basic_block use_bb,
784 gimple phi,
785 unsigned uninit_opnds,
786 struct pointer_set_t *visited_phis);
788 /* A helper function that determines if the predicate set
789 of the use is not overlapping with that of the uninit paths.
790 The most common senario of guarded use is in Example 1:
791 Example 1:
792 if (some_cond)
794 x = ...;
795 flag = true;
798 ... some code ...
800 if (flag)
801 use (x);
803 The real world examples are usually more complicated, but similar
804 and usually result from inlining:
806 bool init_func (int * x)
808 if (some_cond)
809 return false;
810 *x = ..
811 return true;
814 void foo(..)
816 int x;
818 if (!init_func(&x))
819 return;
821 .. some_code ...
822 use (x);
825 Another possible use scenario is in the following trivial example:
827 Example 2:
828 if (n > 0)
829 x = 1;
831 if (n > 0)
833 if (m < 2)
834 .. = x;
837 Predicate analysis needs to compute the composite predicate:
839 1) 'x' use predicate: (n > 0) .AND. (m < 2)
840 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
841 (the predicate chain for phi operand defs can be computed
842 starting from a bb that is control equivalent to the phi's
843 bb and is dominating the operand def.)
845 and check overlapping:
846 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
847 <==> false
849 This implementation provides framework that can handle
850 scenarios. (Note that many simple cases are handled properly
851 without the predicate analysis -- this is due to jump threading
852 transformation which eliminates the merge point thus makes
853 path sensitive analysis unnecessary.)
855 NUM_PREDS is the number is the number predicate chains, PREDS is
856 the array of chains, PHI is the phi node whose incoming (undefined)
857 paths need to be pruned, and UNINIT_OPNDS is the bitmap holding
858 uninit operand positions. VISITED_PHIS is the pointer set of phi
859 stmts being checked. */
862 static bool
863 use_pred_not_overlap_with_undef_path_pred (
864 size_t num_preds,
865 VEC(use_pred_info_t, heap) **preds,
866 gimple phi, unsigned uninit_opnds,
867 struct pointer_set_t *visited_phis)
869 unsigned int i, n;
870 gimple flag_def = 0;
871 tree boundary_cst = 0;
872 enum tree_code cmp_code;
873 bool swap_cond = false;
874 bool invert = false;
875 VEC(use_pred_info_t, heap) *the_pred_chain;
877 gcc_assert (num_preds > 0);
878 /* Find within the common prefix of multiple predicate chains
879 a predicate that is a comparison of a flag variable against
880 a constant. */
881 the_pred_chain = preds[0];
882 n = VEC_length (use_pred_info_t, the_pred_chain);
883 for (i = 0; i < n; i++)
885 gimple cond;
886 tree cond_lhs, cond_rhs, flag = 0;
888 use_pred_info_t the_pred
889 = VEC_index (use_pred_info_t, the_pred_chain, i);
891 cond = the_pred->cond;
892 invert = the_pred->invert;
893 cond_lhs = gimple_cond_lhs (cond);
894 cond_rhs = gimple_cond_rhs (cond);
895 cmp_code = gimple_cond_code (cond);
897 if (cond_lhs != NULL_TREE && TREE_CODE (cond_lhs) == SSA_NAME
898 && cond_rhs != NULL_TREE && is_gimple_constant (cond_rhs))
900 boundary_cst = cond_rhs;
901 flag = cond_lhs;
903 else if (cond_rhs != NULL_TREE && TREE_CODE (cond_rhs) == SSA_NAME
904 && cond_lhs != NULL_TREE && is_gimple_constant (cond_lhs))
906 boundary_cst = cond_lhs;
907 flag = cond_rhs;
908 swap_cond = true;
911 if (!flag)
912 continue;
914 flag_def = SSA_NAME_DEF_STMT (flag);
916 if (!flag_def)
917 continue;
919 if ((gimple_code (flag_def) == GIMPLE_PHI)
920 && (gimple_bb (flag_def) == gimple_bb (phi))
921 && find_matching_predicate_in_rest_chains (
922 the_pred, preds, num_preds))
923 break;
925 flag_def = 0;
928 if (!flag_def)
929 return false;
931 /* Now check all the uninit incoming edge has a constant flag value
932 that is in conflict with the use guard/predicate. */
933 cmp_code = get_cmp_code (cmp_code, swap_cond, invert);
935 if (cmp_code == ERROR_MARK)
936 return false;
938 for (i = 0; i < sizeof (unsigned); i++)
940 tree flag_arg;
942 if (!MASK_TEST_BIT (uninit_opnds, i))
943 continue;
945 flag_arg = gimple_phi_arg_def (flag_def, i);
946 if (!is_gimple_constant (flag_arg))
947 return false;
949 /* Now check if the constant is in the guarded range. */
950 if (is_value_included_in (flag_arg, boundary_cst, cmp_code))
952 tree opnd;
953 gimple opnd_def;
955 /* Now that we know that this undefined edge is not
956 pruned. If the operand is defined by another phi,
957 we can further prune the incoming edges of that
958 phi by checking the predicates of this operands. */
960 opnd = gimple_phi_arg_def (phi, i);
961 opnd_def = SSA_NAME_DEF_STMT (opnd);
962 if (gimple_code (opnd_def) == GIMPLE_PHI)
964 edge opnd_edge;
965 unsigned uninit_opnds2
966 = compute_uninit_opnds_pos (opnd_def);
967 gcc_assert (!MASK_EMPTY (uninit_opnds2));
968 opnd_edge = gimple_phi_arg_edge (phi, i);
969 if (!is_use_properly_guarded (phi,
970 opnd_edge->src,
971 opnd_def,
972 uninit_opnds2,
973 visited_phis))
974 return false;
976 else
977 return false;
981 return true;
984 /* Returns true if TC is AND or OR */
986 static inline bool
987 is_and_or_or (enum tree_code tc, tree typ)
989 return (tc == TRUTH_AND_EXPR
990 || tc == TRUTH_OR_EXPR
991 || tc == BIT_IOR_EXPR
992 || (tc == BIT_AND_EXPR
993 && (typ == 0 || TREE_CODE (typ) == BOOLEAN_TYPE)));
996 typedef struct norm_cond
998 VEC(gimple, heap) *conds;
999 enum tree_code cond_code;
1000 bool invert;
1001 } *norm_cond_t;
1004 /* Normalizes gimple condition COND. The normalization follows
1005 UD chains to form larger condition expression trees. NORM_COND
1006 holds the normalized result. COND_CODE is the logical opcode
1007 (AND or OR) of the normalized tree. */
1009 static void
1010 normalize_cond_1 (gimple cond,
1011 norm_cond_t norm_cond,
1012 enum tree_code cond_code)
1014 enum gimple_code gc;
1015 enum tree_code cur_cond_code;
1016 tree rhs1, rhs2;
1018 gc = gimple_code (cond);
1019 if (gc != GIMPLE_ASSIGN)
1021 VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1022 return;
1025 cur_cond_code = gimple_assign_rhs_code (cond);
1026 rhs1 = gimple_assign_rhs1 (cond);
1027 rhs2 = gimple_assign_rhs2 (cond);
1028 if (cur_cond_code == NE_EXPR)
1030 if (integer_zerop (rhs2)
1031 && (TREE_CODE (rhs1) == SSA_NAME))
1032 normalize_cond_1 (
1033 SSA_NAME_DEF_STMT (rhs1),
1034 norm_cond, cond_code);
1035 else if (integer_zerop (rhs1)
1036 && (TREE_CODE (rhs2) == SSA_NAME))
1037 normalize_cond_1 (
1038 SSA_NAME_DEF_STMT (rhs2),
1039 norm_cond, cond_code);
1040 else
1041 VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1043 return;
1046 if (is_and_or_or (cur_cond_code, TREE_TYPE (rhs1))
1047 && (cond_code == cur_cond_code || cond_code == ERROR_MARK)
1048 && (TREE_CODE (rhs1) == SSA_NAME && TREE_CODE (rhs2) == SSA_NAME))
1050 normalize_cond_1 (SSA_NAME_DEF_STMT (rhs1),
1051 norm_cond, cur_cond_code);
1052 normalize_cond_1 (SSA_NAME_DEF_STMT (rhs2),
1053 norm_cond, cur_cond_code);
1054 norm_cond->cond_code = cur_cond_code;
1056 else
1057 VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1060 /* See normalize_cond_1 for details. INVERT is a flag to indicate
1061 if COND needs to be inverted or not. */
1063 static void
1064 normalize_cond (gimple cond, norm_cond_t norm_cond, bool invert)
1066 enum tree_code cond_code;
1068 norm_cond->cond_code = ERROR_MARK;
1069 norm_cond->invert = false;
1070 norm_cond->conds = NULL;
1071 gcc_assert (gimple_code (cond) == GIMPLE_COND);
1072 cond_code = gimple_cond_code (cond);
1073 if (invert)
1074 cond_code = invert_tree_comparison (cond_code, false);
1076 if (cond_code == NE_EXPR)
1078 if (integer_zerop (gimple_cond_rhs (cond))
1079 && (TREE_CODE (gimple_cond_lhs (cond)) == SSA_NAME))
1080 normalize_cond_1 (
1081 SSA_NAME_DEF_STMT (gimple_cond_lhs (cond)),
1082 norm_cond, ERROR_MARK);
1083 else if (integer_zerop (gimple_cond_lhs (cond))
1084 && (TREE_CODE (gimple_cond_rhs (cond)) == SSA_NAME))
1085 normalize_cond_1 (
1086 SSA_NAME_DEF_STMT (gimple_cond_rhs (cond)),
1087 norm_cond, ERROR_MARK);
1088 else
1090 VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1091 norm_cond->invert = invert;
1094 else
1096 VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1097 norm_cond->invert = invert;
1100 gcc_assert (VEC_length (gimple, norm_cond->conds) == 1
1101 || is_and_or_or (norm_cond->cond_code, NULL));
1104 /* Returns true if the domain for condition COND1 is a subset of
1105 COND2. REVERSE is a flag. when it is true the function checks
1106 if COND1 is a superset of COND2. INVERT1 and INVERT2 are flags
1107 to indicate if COND1 and COND2 need to be inverted or not. */
1109 static bool
1110 is_gcond_subset_of (gimple cond1, bool invert1,
1111 gimple cond2, bool invert2,
1112 bool reverse)
1114 enum gimple_code gc1, gc2;
1115 enum tree_code cond1_code, cond2_code;
1116 gimple tmp;
1117 tree cond1_lhs, cond1_rhs, cond2_lhs, cond2_rhs;
1119 /* Take the short cut. */
1120 if (cond1 == cond2)
1121 return true;
1123 if (reverse)
1125 tmp = cond1;
1126 cond1 = cond2;
1127 cond2 = tmp;
1130 gc1 = gimple_code (cond1);
1131 gc2 = gimple_code (cond2);
1133 if ((gc1 != GIMPLE_ASSIGN && gc1 != GIMPLE_COND)
1134 || (gc2 != GIMPLE_ASSIGN && gc2 != GIMPLE_COND))
1135 return cond1 == cond2;
1137 cond1_code = ((gc1 == GIMPLE_ASSIGN)
1138 ? gimple_assign_rhs_code (cond1)
1139 : gimple_cond_code (cond1));
1141 cond2_code = ((gc2 == GIMPLE_ASSIGN)
1142 ? gimple_assign_rhs_code (cond2)
1143 : gimple_cond_code (cond2));
1145 if (TREE_CODE_CLASS (cond1_code) != tcc_comparison
1146 || TREE_CODE_CLASS (cond2_code) != tcc_comparison)
1147 return false;
1149 if (invert1)
1150 cond1_code = invert_tree_comparison (cond1_code, false);
1151 if (invert2)
1152 cond2_code = invert_tree_comparison (cond2_code, false);
1154 cond1_lhs = ((gc1 == GIMPLE_ASSIGN)
1155 ? gimple_assign_rhs1 (cond1)
1156 : gimple_cond_lhs (cond1));
1157 cond1_rhs = ((gc1 == GIMPLE_ASSIGN)
1158 ? gimple_assign_rhs2 (cond1)
1159 : gimple_cond_rhs (cond1));
1160 cond2_lhs = ((gc2 == GIMPLE_ASSIGN)
1161 ? gimple_assign_rhs1 (cond2)
1162 : gimple_cond_lhs (cond2));
1163 cond2_rhs = ((gc2 == GIMPLE_ASSIGN)
1164 ? gimple_assign_rhs2 (cond2)
1165 : gimple_cond_rhs (cond2));
1167 /* Assuming const operands have been swapped to the
1168 rhs at this point of the analysis. */
1170 if (cond1_lhs != cond2_lhs)
1171 return false;
1173 if (!is_gimple_constant (cond1_rhs)
1174 || TREE_CODE (cond1_rhs) != INTEGER_CST)
1175 return (cond1_rhs == cond2_rhs);
1177 if (!is_gimple_constant (cond2_rhs)
1178 || TREE_CODE (cond2_rhs) != INTEGER_CST)
1179 return (cond1_rhs == cond2_rhs);
1181 if (cond1_code == EQ_EXPR)
1182 return is_value_included_in (cond1_rhs,
1183 cond2_rhs, cond2_code);
1184 if (cond1_code == NE_EXPR || cond2_code == EQ_EXPR)
1185 return ((cond2_code == cond1_code)
1186 && tree_int_cst_equal (cond1_rhs, cond2_rhs));
1188 if (((cond1_code == GE_EXPR || cond1_code == GT_EXPR)
1189 && (cond2_code == LE_EXPR || cond2_code == LT_EXPR))
1190 || ((cond1_code == LE_EXPR || cond1_code == LT_EXPR)
1191 && (cond2_code == GE_EXPR || cond2_code == GT_EXPR)))
1192 return false;
1194 if (cond1_code != GE_EXPR && cond1_code != GT_EXPR
1195 && cond1_code != LE_EXPR && cond1_code != LT_EXPR)
1196 return false;
1198 if (cond1_code == GT_EXPR)
1200 cond1_code = GE_EXPR;
1201 cond1_rhs = fold_binary (PLUS_EXPR, TREE_TYPE (cond1_rhs),
1202 cond1_rhs,
1203 fold_convert (TREE_TYPE (cond1_rhs),
1204 integer_one_node));
1206 else if (cond1_code == LT_EXPR)
1208 cond1_code = LE_EXPR;
1209 cond1_rhs = fold_binary (MINUS_EXPR, TREE_TYPE (cond1_rhs),
1210 cond1_rhs,
1211 fold_convert (TREE_TYPE (cond1_rhs),
1212 integer_one_node));
1215 if (!cond1_rhs)
1216 return false;
1218 gcc_assert (cond1_code == GE_EXPR || cond1_code == LE_EXPR);
1220 if (cond2_code == GE_EXPR || cond2_code == GT_EXPR ||
1221 cond2_code == LE_EXPR || cond2_code == LT_EXPR)
1222 return is_value_included_in (cond1_rhs,
1223 cond2_rhs, cond2_code);
1224 else if (cond2_code == NE_EXPR)
1225 return
1226 (is_value_included_in (cond1_rhs,
1227 cond2_rhs, cond2_code)
1228 && !is_value_included_in (cond2_rhs,
1229 cond1_rhs, cond1_code));
1230 return false;
1233 /* Returns true if the domain of the condition expression
1234 in COND is a subset of any of the sub-conditions
1235 of the normalized condtion NORM_COND. INVERT is a flag
1236 to indicate of the COND needs to be inverted.
1237 REVERSE is a flag. When it is true, the check is reversed --
1238 it returns true if COND is a superset of any of the subconditions
1239 of NORM_COND. */
1241 static bool
1242 is_subset_of_any (gimple cond, bool invert,
1243 norm_cond_t norm_cond, bool reverse)
1245 size_t i;
1246 size_t len = VEC_length (gimple, norm_cond->conds);
1248 for (i = 0; i < len; i++)
1250 if (is_gcond_subset_of (cond, invert,
1251 VEC_index (gimple, norm_cond->conds, i),
1252 false, reverse))
1253 return true;
1255 return false;
1258 /* NORM_COND1 and NORM_COND2 are normalized logical/BIT OR
1259 expressions (formed by following UD chains not control
1260 dependence chains). The function returns true of domain
1261 of and expression NORM_COND1 is a subset of NORM_COND2's.
1262 The implementation is conservative, and it returns false if
1263 it the inclusion relationship may not hold. */
1265 static bool
1266 is_or_set_subset_of (norm_cond_t norm_cond1,
1267 norm_cond_t norm_cond2)
1269 size_t i;
1270 size_t len = VEC_length (gimple, norm_cond1->conds);
1272 for (i = 0; i < len; i++)
1274 if (!is_subset_of_any (VEC_index (gimple, norm_cond1->conds, i),
1275 false, norm_cond2, false))
1276 return false;
1278 return true;
1281 /* NORM_COND1 and NORM_COND2 are normalized logical AND
1282 expressions (formed by following UD chains not control
1283 dependence chains). The function returns true of domain
1284 of and expression NORM_COND1 is a subset of NORM_COND2's. */
1286 static bool
1287 is_and_set_subset_of (norm_cond_t norm_cond1,
1288 norm_cond_t norm_cond2)
1290 size_t i;
1291 size_t len = VEC_length (gimple, norm_cond2->conds);
1293 for (i = 0; i < len; i++)
1295 if (!is_subset_of_any (VEC_index (gimple, norm_cond2->conds, i),
1296 false, norm_cond1, true))
1297 return false;
1299 return true;
1302 /* Returns true of the domain if NORM_COND1 is a subset
1303 of that of NORM_COND2. Returns false if it can not be
1304 proved to be so. */
1306 static bool
1307 is_norm_cond_subset_of (norm_cond_t norm_cond1,
1308 norm_cond_t norm_cond2)
1310 size_t i;
1311 enum tree_code code1, code2;
1313 code1 = norm_cond1->cond_code;
1314 code2 = norm_cond2->cond_code;
1316 if (code1 == TRUTH_AND_EXPR || code1 == BIT_AND_EXPR)
1318 /* Both conditions are AND expressions. */
1319 if (code2 == TRUTH_AND_EXPR || code2 == BIT_AND_EXPR)
1320 return is_and_set_subset_of (norm_cond1, norm_cond2);
1321 /* NORM_COND1 is an AND expression, and NORM_COND2 is an OR
1322 expression. In this case, returns true if any subexpression
1323 of NORM_COND1 is a subset of any subexpression of NORM_COND2. */
1324 else if (code2 == TRUTH_OR_EXPR || code2 == BIT_IOR_EXPR)
1326 size_t len1;
1327 len1 = VEC_length (gimple, norm_cond1->conds);
1328 for (i = 0; i < len1; i++)
1330 gimple cond1 = VEC_index (gimple, norm_cond1->conds, i);
1331 if (is_subset_of_any (cond1, false, norm_cond2, false))
1332 return true;
1334 return false;
1336 else
1338 gcc_assert (code2 == ERROR_MARK);
1339 gcc_assert (VEC_length (gimple, norm_cond2->conds) == 1);
1340 return is_subset_of_any (VEC_index (gimple, norm_cond2->conds, 0),
1341 norm_cond2->invert, norm_cond1, true);
1344 /* NORM_COND1 is an OR expression */
1345 else if (code1 == TRUTH_OR_EXPR || code1 == BIT_IOR_EXPR)
1347 if (code2 != code1)
1348 return false;
1350 return is_or_set_subset_of (norm_cond1, norm_cond2);
1352 else
1354 gcc_assert (code1 == ERROR_MARK);
1355 gcc_assert (VEC_length (gimple, norm_cond1->conds) == 1);
1356 /* Conservatively returns false if NORM_COND1 is non-decomposible
1357 and NORM_COND2 is an AND expression. */
1358 if (code2 == TRUTH_AND_EXPR || code2 == BIT_AND_EXPR)
1359 return false;
1361 if (code2 == TRUTH_OR_EXPR || code2 == BIT_IOR_EXPR)
1362 return is_subset_of_any (VEC_index (gimple, norm_cond1->conds, 0),
1363 norm_cond1->invert, norm_cond2, false);
1365 gcc_assert (code2 == ERROR_MARK);
1366 gcc_assert (VEC_length (gimple, norm_cond2->conds) == 1);
1367 return is_gcond_subset_of (VEC_index (gimple, norm_cond1->conds, 0),
1368 norm_cond1->invert,
1369 VEC_index (gimple, norm_cond2->conds, 0),
1370 norm_cond2->invert, false);
1374 /* Returns true of the domain of single predicate expression
1375 EXPR1 is a subset of that of EXPR2. Returns false if it
1376 can not be proved. */
1378 static bool
1379 is_pred_expr_subset_of (use_pred_info_t expr1,
1380 use_pred_info_t expr2)
1382 gimple cond1, cond2;
1383 enum tree_code code1, code2;
1384 struct norm_cond norm_cond1, norm_cond2;
1385 bool is_subset = false;
1387 cond1 = expr1->cond;
1388 cond2 = expr2->cond;
1389 code1 = gimple_cond_code (cond1);
1390 code2 = gimple_cond_code (cond2);
1392 if (expr1->invert)
1393 code1 = invert_tree_comparison (code1, false);
1394 if (expr2->invert)
1395 code2 = invert_tree_comparison (code2, false);
1397 /* Fast path -- match exactly */
1398 if ((gimple_cond_lhs (cond1) == gimple_cond_lhs (cond2))
1399 && (gimple_cond_rhs (cond1) == gimple_cond_rhs (cond2))
1400 && (code1 == code2))
1401 return true;
1403 /* Normalize conditions. To keep NE_EXPR, do not invert
1404 with both need inversion. */
1405 normalize_cond (cond1, &norm_cond1, (expr1->invert));
1406 normalize_cond (cond2, &norm_cond2, (expr2->invert));
1408 is_subset = is_norm_cond_subset_of (&norm_cond1, &norm_cond2);
1410 /* Free memory */
1411 VEC_free (gimple, heap, norm_cond1.conds);
1412 VEC_free (gimple, heap, norm_cond2.conds);
1413 return is_subset ;
1416 /* Returns true if the domain of PRED1 is a subset
1417 of that of PRED2. Returns false if it can not be proved so. */
1419 static bool
1420 is_pred_chain_subset_of (VEC(use_pred_info_t, heap) *pred1,
1421 VEC(use_pred_info_t, heap) *pred2)
1423 size_t np1, np2, i1, i2;
1425 np1 = VEC_length (use_pred_info_t, pred1);
1426 np2 = VEC_length (use_pred_info_t, pred2);
1428 for (i2 = 0; i2 < np2; i2++)
1430 bool found = false;
1431 use_pred_info_t info2
1432 = VEC_index (use_pred_info_t, pred2, i2);
1433 for (i1 = 0; i1 < np1; i1++)
1435 use_pred_info_t info1
1436 = VEC_index (use_pred_info_t, pred1, i1);
1437 if (is_pred_expr_subset_of (info1, info2))
1439 found = true;
1440 break;
1443 if (!found)
1444 return false;
1446 return true;
1449 /* Returns true if the domain defined by
1450 one pred chain ONE_PRED is a subset of the domain
1451 of *PREDS. It returns false if ONE_PRED's domain is
1452 not a subset of any of the sub-domains of PREDS (
1453 corresponding to each individual chains in it), even
1454 though it may be still be a subset of whole domain
1455 of PREDS which is the union (ORed) of all its subdomains.
1456 In other words, the result is conservative. */
1458 static bool
1459 is_included_in (VEC(use_pred_info_t, heap) *one_pred,
1460 VEC(use_pred_info_t, heap) **preds,
1461 size_t n)
1463 size_t i;
1465 for (i = 0; i < n; i++)
1467 if (is_pred_chain_subset_of (one_pred, preds[i]))
1468 return true;
1471 return false;
1474 /* compares two predicate sets PREDS1 and PREDS2 and returns
1475 true if the domain defined by PREDS1 is a superset
1476 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1477 PREDS2 respectively. The implementation chooses not to build
1478 generic trees (and relying on the folding capability of the
1479 compiler), but instead performs brute force comparison of
1480 individual predicate chains (won't be a compile time problem
1481 as the chains are pretty short). When the function returns
1482 false, it does not necessarily mean *PREDS1 is not a superset
1483 of *PREDS2, but mean it may not be so since the analysis can
1484 not prove it. In such cases, false warnings may still be
1485 emitted. */
1487 static bool
1488 is_superset_of (VEC(use_pred_info_t, heap) **preds1,
1489 size_t n1,
1490 VEC(use_pred_info_t, heap) **preds2,
1491 size_t n2)
1493 size_t i;
1494 VEC(use_pred_info_t, heap) *one_pred_chain;
1496 for (i = 0; i < n2; i++)
1498 one_pred_chain = preds2[i];
1499 if (!is_included_in (one_pred_chain, preds1, n1))
1500 return false;
1503 return true;
1506 /* Computes the predicates that guard the use and checks
1507 if the incoming paths that have empty (or possibly
1508 empty) defintion can be pruned/filtered. The function returns
1509 true if it can be determined that the use of PHI's def in
1510 USE_STMT is guarded with a predicate set not overlapping with
1511 predicate sets of all runtime paths that do not have a definition.
1512 Returns false if it is not or it can not be determined. USE_BB is
1513 the bb of the use (for phi operand use, the bb is not the bb of
1514 the phi stmt, but the src bb of the operand edge). UNINIT_OPNDS
1515 is a bit vector. If an operand of PHI is uninitialized, the
1516 correponding bit in the vector is 1. VISIED_PHIS is a pointer
1517 set of phis being visted. */
1519 static bool
1520 is_use_properly_guarded (gimple use_stmt,
1521 basic_block use_bb,
1522 gimple phi,
1523 unsigned uninit_opnds,
1524 struct pointer_set_t *visited_phis)
1526 basic_block phi_bb;
1527 VEC(use_pred_info_t, heap) **preds = 0;
1528 VEC(use_pred_info_t, heap) **def_preds = 0;
1529 size_t num_preds = 0, num_def_preds = 0;
1530 bool has_valid_preds = false;
1531 bool is_properly_guarded = false;
1533 if (pointer_set_insert (visited_phis, phi))
1534 return false;
1536 phi_bb = gimple_bb (phi);
1538 if (is_non_loop_exit_postdominating (use_bb, phi_bb))
1539 return false;
1541 has_valid_preds = find_predicates (&preds, &num_preds,
1542 phi_bb, use_bb);
1544 if (!has_valid_preds)
1546 destroy_predicate_vecs (num_preds, preds);
1547 return false;
1550 if (dump_file)
1551 dump_predicates (use_stmt, num_preds, preds,
1552 "\nUse in stmt ");
1554 has_valid_preds = find_def_preds (&def_preds,
1555 &num_def_preds, phi);
1557 if (has_valid_preds)
1559 if (dump_file)
1560 dump_predicates (phi, num_def_preds, def_preds,
1561 "Operand defs of phi ");
1562 is_properly_guarded =
1563 is_superset_of (def_preds, num_def_preds,
1564 preds, num_preds);
1567 /* further prune the dead incoming phi edges. */
1568 if (!is_properly_guarded)
1569 is_properly_guarded
1570 = use_pred_not_overlap_with_undef_path_pred (
1571 num_preds, preds, phi, uninit_opnds, visited_phis);
1573 destroy_predicate_vecs (num_preds, preds);
1574 destroy_predicate_vecs (num_def_preds, def_preds);
1575 return is_properly_guarded;
1578 /* Searches through all uses of a potentially
1579 uninitialized variable defined by PHI and returns a use
1580 statement if the use is not properly guarded. It returns
1581 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
1582 holding the position(s) of uninit PHI operands. WORKLIST
1583 is the vector of candidate phis that may be updated by this
1584 function. ADDED_TO_WORKLIST is the pointer set tracking
1585 if the new phi is already in the worklist. */
1587 static gimple
1588 find_uninit_use (gimple phi, unsigned uninit_opnds,
1589 VEC(gimple, heap) **worklist,
1590 struct pointer_set_t *added_to_worklist)
1592 tree phi_result;
1593 use_operand_p use_p;
1594 gimple use_stmt;
1595 imm_use_iterator iter;
1597 phi_result = gimple_phi_result (phi);
1599 FOR_EACH_IMM_USE_FAST (use_p, iter, phi_result)
1601 struct pointer_set_t *visited_phis;
1602 basic_block use_bb;
1604 use_stmt = USE_STMT (use_p);
1605 if (is_gimple_debug (use_stmt))
1606 continue;
1608 visited_phis = pointer_set_create ();
1610 if (gimple_code (use_stmt) == GIMPLE_PHI)
1611 use_bb = gimple_phi_arg_edge (use_stmt,
1612 PHI_ARG_INDEX_FROM_USE (use_p))->src;
1613 else
1614 use_bb = gimple_bb (use_stmt);
1616 if (is_use_properly_guarded (use_stmt,
1617 use_bb,
1618 phi,
1619 uninit_opnds,
1620 visited_phis))
1622 pointer_set_destroy (visited_phis);
1623 continue;
1625 pointer_set_destroy (visited_phis);
1627 if (dump_file && (dump_flags & TDF_DETAILS))
1629 fprintf (dump_file, "[CHECK]: Found unguarded use: ");
1630 print_gimple_stmt (dump_file, use_stmt, 0, 0);
1632 /* Found one real use, return. */
1633 if (gimple_code (use_stmt) != GIMPLE_PHI)
1634 return use_stmt;
1636 /* Found a phi use that is not guarded,
1637 add the phi to the worklist. */
1638 if (!pointer_set_insert (added_to_worklist,
1639 use_stmt))
1641 if (dump_file && (dump_flags & TDF_DETAILS))
1643 fprintf (dump_file, "[WORKLIST]: Update worklist with phi: ");
1644 print_gimple_stmt (dump_file, use_stmt, 0, 0);
1647 VEC_safe_push (gimple, heap, *worklist, use_stmt);
1648 pointer_set_insert (possibly_undefined_names,
1649 phi_result);
1653 return NULL;
1656 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
1657 and gives warning if there exists a runtime path from the entry to a
1658 use of the PHI def that does not contain a definition. In other words,
1659 the warning is on the real use. The more dead paths that can be pruned
1660 by the compiler, the fewer false positives the warning is. WORKLIST
1661 is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
1662 a pointer set tracking if the new phi is added to the worklist or not. */
1664 static void
1665 warn_uninitialized_phi (gimple phi, VEC(gimple, heap) **worklist,
1666 struct pointer_set_t *added_to_worklist)
1668 unsigned uninit_opnds;
1669 gimple uninit_use_stmt = 0;
1670 tree uninit_op;
1672 /* Don't look at memory tags. */
1673 if (!is_gimple_reg (gimple_phi_result (phi)))
1674 return;
1676 uninit_opnds = compute_uninit_opnds_pos (phi);
1678 if (MASK_EMPTY (uninit_opnds))
1679 return;
1681 if (dump_file && (dump_flags & TDF_DETAILS))
1683 fprintf (dump_file, "[CHECK]: examining phi: ");
1684 print_gimple_stmt (dump_file, phi, 0, 0);
1687 /* Now check if we have any use of the value without proper guard. */
1688 uninit_use_stmt = find_uninit_use (phi, uninit_opnds,
1689 worklist, added_to_worklist);
1691 /* All uses are properly guarded. */
1692 if (!uninit_use_stmt)
1693 return;
1695 uninit_op = gimple_phi_arg_def (phi, MASK_FIRST_SET_BIT (uninit_opnds));
1696 warn_uninit (uninit_op,
1697 "%qD may be used uninitialized in this function",
1698 uninit_use_stmt);
1703 /* Entry point to the late uninitialized warning pass. */
1705 static unsigned int
1706 execute_late_warn_uninitialized (void)
1708 basic_block bb;
1709 gimple_stmt_iterator gsi;
1710 VEC(gimple, heap) *worklist = 0;
1711 struct pointer_set_t *added_to_worklist;
1713 calculate_dominance_info (CDI_DOMINATORS);
1714 calculate_dominance_info (CDI_POST_DOMINATORS);
1715 /* Re-do the plain uninitialized variable check, as optimization may have
1716 straightened control flow. Do this first so that we don't accidentally
1717 get a "may be" warning when we'd have seen an "is" warning later. */
1718 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
1720 timevar_push (TV_TREE_UNINIT);
1722 possibly_undefined_names = pointer_set_create ();
1723 added_to_worklist = pointer_set_create ();
1725 /* Initialize worklist */
1726 FOR_EACH_BB (bb)
1727 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1729 gimple phi = gsi_stmt (gsi);
1730 size_t n, i;
1732 n = gimple_phi_num_args (phi);
1734 /* Don't look at memory tags. */
1735 if (!is_gimple_reg (gimple_phi_result (phi)))
1736 continue;
1738 for (i = 0; i < n; ++i)
1740 tree op = gimple_phi_arg_def (phi, i);
1741 if (TREE_CODE (op) == SSA_NAME
1742 && ssa_undefined_value_p (op))
1744 VEC_safe_push (gimple, heap, worklist, phi);
1745 pointer_set_insert (added_to_worklist, phi);
1746 if (dump_file && (dump_flags & TDF_DETAILS))
1748 fprintf (dump_file, "[WORKLIST]: add to initial list: ");
1749 print_gimple_stmt (dump_file, phi, 0, 0);
1751 break;
1756 while (VEC_length (gimple, worklist) != 0)
1758 gimple cur_phi = 0;
1759 cur_phi = VEC_pop (gimple, worklist);
1760 warn_uninitialized_phi (cur_phi, &worklist, added_to_worklist);
1763 VEC_free (gimple, heap, worklist);
1764 pointer_set_destroy (added_to_worklist);
1765 pointer_set_destroy (possibly_undefined_names);
1766 possibly_undefined_names = NULL;
1767 free_dominance_info (CDI_POST_DOMINATORS);
1768 timevar_pop (TV_TREE_UNINIT);
1769 return 0;
1772 static bool
1773 gate_warn_uninitialized (void)
1775 return warn_uninitialized != 0;
1778 struct gimple_opt_pass pass_late_warn_uninitialized =
1781 GIMPLE_PASS,
1782 "uninit", /* name */
1783 gate_warn_uninitialized, /* gate */
1784 execute_late_warn_uninitialized, /* execute */
1785 NULL, /* sub */
1786 NULL, /* next */
1787 0, /* static_pass_number */
1788 TV_NONE, /* tv_id */
1789 PROP_ssa, /* properties_required */
1790 0, /* properties_provided */
1791 0, /* properties_destroyed */
1792 0, /* todo_flags_start */
1793 0 /* todo_flags_finish */