2010-11-11 Jakub Jelinek <jakub@redhat.com>
[official-gcc.git] / gcc / tree-ssa-uninit.c
blob01ff43165d3ea9cb34529ce2b2b3e09c4196abc3
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 /* Returns true if all uninitialized opnds are pruned. Returns false
789 otherwise. PHI is the phi node with uninitialized operands,
790 UNINIT_OPNDS is the bitmap of the uninitialize operand positions,
791 FLAG_DEF is the statement defining the flag guarding the use of the
792 PHI output, BOUNDARY_CST is the const value used in the predicate
793 associated with the flag, CMP_CODE is the comparison code used in
794 the predicate, VISITED_PHIS is the pointer set of phis visited, and
795 VISITED_FLAG_PHIS is the pointer to the pointer set of flag definitions
796 that are also phis.
798 Example scenario:
800 BB1:
801 flag_1 = phi <0, 1> // (1)
802 var_1 = phi <undef, some_val>
805 BB2:
806 flag_2 = phi <0, flag_1, flag_1> // (2)
807 var_2 = phi <undef, var_1, var_1>
808 if (flag_2 == 1)
809 goto BB3;
811 BB3:
812 use of var_2 // (3)
814 Because some flag arg in (1) is not constant, if we do not look into the
815 flag phis recursively, it is conservatively treated as unknown and var_1
816 is thought to be flowed into use at (3). Since var_1 is potentially uninitialized
817 a false warning will be emitted. Checking recursively into (1), the compiler can
818 find out that only some_val (which is defined) can flow into (3) which is OK.
822 static bool
823 prune_uninit_phi_opnds_in_unrealizable_paths (
824 gimple phi, unsigned uninit_opnds,
825 gimple flag_def, tree boundary_cst,
826 enum tree_code cmp_code,
827 struct pointer_set_t *visited_phis,
828 bitmap *visited_flag_phis)
830 unsigned i;
832 for (i = 0; i < MIN (32, gimple_phi_num_args (flag_def)); i++)
834 tree flag_arg;
836 if (!MASK_TEST_BIT (uninit_opnds, i))
837 continue;
839 flag_arg = gimple_phi_arg_def (flag_def, i);
840 if (!is_gimple_constant (flag_arg))
842 gimple flag_arg_def, phi_arg_def;
843 tree phi_arg;
844 unsigned uninit_opnds_arg_phi;
846 if (TREE_CODE (flag_arg) != SSA_NAME)
847 return false;
848 flag_arg_def = SSA_NAME_DEF_STMT (flag_arg);
849 if (gimple_code (flag_arg_def) != GIMPLE_PHI)
850 return false;
852 phi_arg = gimple_phi_arg_def (phi, i);
853 if (TREE_CODE (phi_arg) != SSA_NAME)
854 return false;
856 phi_arg_def = SSA_NAME_DEF_STMT (phi_arg);
857 if (gimple_code (phi_arg_def) != GIMPLE_PHI)
858 return false;
860 if (gimple_bb (phi_arg_def) != gimple_bb (flag_arg_def))
861 return false;
863 if (!*visited_flag_phis)
864 *visited_flag_phis = BITMAP_ALLOC (NULL);
866 if (bitmap_bit_p (*visited_flag_phis,
867 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def))))
868 return false;
870 bitmap_set_bit (*visited_flag_phis,
871 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
873 /* Now recursively prune the uninitialized phi args. */
874 uninit_opnds_arg_phi = compute_uninit_opnds_pos (phi_arg_def);
875 if (!prune_uninit_phi_opnds_in_unrealizable_paths (
876 phi_arg_def, uninit_opnds_arg_phi,
877 flag_arg_def, boundary_cst, cmp_code,
878 visited_phis, visited_flag_phis))
879 return false;
881 bitmap_clear_bit (*visited_flag_phis,
882 SSA_NAME_VERSION (gimple_phi_result (flag_arg_def)));
883 continue;
886 /* Now check if the constant is in the guarded range. */
887 if (is_value_included_in (flag_arg, boundary_cst, cmp_code))
889 tree opnd;
890 gimple opnd_def;
892 /* Now that we know that this undefined edge is not
893 pruned. If the operand is defined by another phi,
894 we can further prune the incoming edges of that
895 phi by checking the predicates of this operands. */
897 opnd = gimple_phi_arg_def (phi, i);
898 opnd_def = SSA_NAME_DEF_STMT (opnd);
899 if (gimple_code (opnd_def) == GIMPLE_PHI)
901 edge opnd_edge;
902 unsigned uninit_opnds2
903 = compute_uninit_opnds_pos (opnd_def);
904 gcc_assert (!MASK_EMPTY (uninit_opnds2));
905 opnd_edge = gimple_phi_arg_edge (phi, i);
906 if (!is_use_properly_guarded (phi,
907 opnd_edge->src,
908 opnd_def,
909 uninit_opnds2,
910 visited_phis))
911 return false;
913 else
914 return false;
918 return true;
921 /* A helper function that determines if the predicate set
922 of the use is not overlapping with that of the uninit paths.
923 The most common senario of guarded use is in Example 1:
924 Example 1:
925 if (some_cond)
927 x = ...;
928 flag = true;
931 ... some code ...
933 if (flag)
934 use (x);
936 The real world examples are usually more complicated, but similar
937 and usually result from inlining:
939 bool init_func (int * x)
941 if (some_cond)
942 return false;
943 *x = ..
944 return true;
947 void foo(..)
949 int x;
951 if (!init_func(&x))
952 return;
954 .. some_code ...
955 use (x);
958 Another possible use scenario is in the following trivial example:
960 Example 2:
961 if (n > 0)
962 x = 1;
964 if (n > 0)
966 if (m < 2)
967 .. = x;
970 Predicate analysis needs to compute the composite predicate:
972 1) 'x' use predicate: (n > 0) .AND. (m < 2)
973 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
974 (the predicate chain for phi operand defs can be computed
975 starting from a bb that is control equivalent to the phi's
976 bb and is dominating the operand def.)
978 and check overlapping:
979 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
980 <==> false
982 This implementation provides framework that can handle
983 scenarios. (Note that many simple cases are handled properly
984 without the predicate analysis -- this is due to jump threading
985 transformation which eliminates the merge point thus makes
986 path sensitive analysis unnecessary.)
988 NUM_PREDS is the number is the number predicate chains, PREDS is
989 the array of chains, PHI is the phi node whose incoming (undefined)
990 paths need to be pruned, and UNINIT_OPNDS is the bitmap holding
991 uninit operand positions. VISITED_PHIS is the pointer set of phi
992 stmts being checked. */
995 static bool
996 use_pred_not_overlap_with_undef_path_pred (
997 size_t num_preds,
998 VEC(use_pred_info_t, heap) **preds,
999 gimple phi, unsigned uninit_opnds,
1000 struct pointer_set_t *visited_phis)
1002 unsigned int i, n;
1003 gimple flag_def = 0;
1004 tree boundary_cst = 0;
1005 enum tree_code cmp_code;
1006 bool swap_cond = false;
1007 bool invert = false;
1008 VEC(use_pred_info_t, heap) *the_pred_chain;
1009 bitmap visited_flag_phis = NULL;
1010 bool all_pruned = false;
1012 gcc_assert (num_preds > 0);
1013 /* Find within the common prefix of multiple predicate chains
1014 a predicate that is a comparison of a flag variable against
1015 a constant. */
1016 the_pred_chain = preds[0];
1017 n = VEC_length (use_pred_info_t, the_pred_chain);
1018 for (i = 0; i < n; i++)
1020 gimple cond;
1021 tree cond_lhs, cond_rhs, flag = 0;
1023 use_pred_info_t the_pred
1024 = VEC_index (use_pred_info_t, the_pred_chain, i);
1026 cond = the_pred->cond;
1027 invert = the_pred->invert;
1028 cond_lhs = gimple_cond_lhs (cond);
1029 cond_rhs = gimple_cond_rhs (cond);
1030 cmp_code = gimple_cond_code (cond);
1032 if (cond_lhs != NULL_TREE && TREE_CODE (cond_lhs) == SSA_NAME
1033 && cond_rhs != NULL_TREE && is_gimple_constant (cond_rhs))
1035 boundary_cst = cond_rhs;
1036 flag = cond_lhs;
1038 else if (cond_rhs != NULL_TREE && TREE_CODE (cond_rhs) == SSA_NAME
1039 && cond_lhs != NULL_TREE && is_gimple_constant (cond_lhs))
1041 boundary_cst = cond_lhs;
1042 flag = cond_rhs;
1043 swap_cond = true;
1046 if (!flag)
1047 continue;
1049 flag_def = SSA_NAME_DEF_STMT (flag);
1051 if (!flag_def)
1052 continue;
1054 if ((gimple_code (flag_def) == GIMPLE_PHI)
1055 && (gimple_bb (flag_def) == gimple_bb (phi))
1056 && find_matching_predicate_in_rest_chains (
1057 the_pred, preds, num_preds))
1058 break;
1060 flag_def = 0;
1063 if (!flag_def)
1064 return false;
1066 /* Now check all the uninit incoming edge has a constant flag value
1067 that is in conflict with the use guard/predicate. */
1068 cmp_code = get_cmp_code (cmp_code, swap_cond, invert);
1070 if (cmp_code == ERROR_MARK)
1071 return false;
1073 all_pruned = prune_uninit_phi_opnds_in_unrealizable_paths (phi,
1074 uninit_opnds,
1075 flag_def,
1076 boundary_cst,
1077 cmp_code,
1078 visited_phis,
1079 &visited_flag_phis);
1081 if (visited_flag_phis)
1082 BITMAP_FREE (visited_flag_phis);
1084 return all_pruned;
1087 /* Returns true if TC is AND or OR */
1089 static inline bool
1090 is_and_or_or (enum tree_code tc, tree typ)
1092 return (tc == TRUTH_AND_EXPR
1093 || tc == TRUTH_OR_EXPR
1094 || tc == BIT_IOR_EXPR
1095 || (tc == BIT_AND_EXPR
1096 && (typ == 0 || TREE_CODE (typ) == BOOLEAN_TYPE)));
1099 typedef struct norm_cond
1101 VEC(gimple, heap) *conds;
1102 enum tree_code cond_code;
1103 bool invert;
1104 } *norm_cond_t;
1107 /* Normalizes gimple condition COND. The normalization follows
1108 UD chains to form larger condition expression trees. NORM_COND
1109 holds the normalized result. COND_CODE is the logical opcode
1110 (AND or OR) of the normalized tree. */
1112 static void
1113 normalize_cond_1 (gimple cond,
1114 norm_cond_t norm_cond,
1115 enum tree_code cond_code)
1117 enum gimple_code gc;
1118 enum tree_code cur_cond_code;
1119 tree rhs1, rhs2;
1121 gc = gimple_code (cond);
1122 if (gc != GIMPLE_ASSIGN)
1124 VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1125 return;
1128 cur_cond_code = gimple_assign_rhs_code (cond);
1129 rhs1 = gimple_assign_rhs1 (cond);
1130 rhs2 = gimple_assign_rhs2 (cond);
1131 if (cur_cond_code == NE_EXPR)
1133 if (integer_zerop (rhs2)
1134 && (TREE_CODE (rhs1) == SSA_NAME))
1135 normalize_cond_1 (
1136 SSA_NAME_DEF_STMT (rhs1),
1137 norm_cond, cond_code);
1138 else if (integer_zerop (rhs1)
1139 && (TREE_CODE (rhs2) == SSA_NAME))
1140 normalize_cond_1 (
1141 SSA_NAME_DEF_STMT (rhs2),
1142 norm_cond, cond_code);
1143 else
1144 VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1146 return;
1149 if (is_and_or_or (cur_cond_code, TREE_TYPE (rhs1))
1150 && (cond_code == cur_cond_code || cond_code == ERROR_MARK)
1151 && (TREE_CODE (rhs1) == SSA_NAME && TREE_CODE (rhs2) == SSA_NAME))
1153 normalize_cond_1 (SSA_NAME_DEF_STMT (rhs1),
1154 norm_cond, cur_cond_code);
1155 normalize_cond_1 (SSA_NAME_DEF_STMT (rhs2),
1156 norm_cond, cur_cond_code);
1157 norm_cond->cond_code = cur_cond_code;
1159 else
1160 VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1163 /* See normalize_cond_1 for details. INVERT is a flag to indicate
1164 if COND needs to be inverted or not. */
1166 static void
1167 normalize_cond (gimple cond, norm_cond_t norm_cond, bool invert)
1169 enum tree_code cond_code;
1171 norm_cond->cond_code = ERROR_MARK;
1172 norm_cond->invert = false;
1173 norm_cond->conds = NULL;
1174 gcc_assert (gimple_code (cond) == GIMPLE_COND);
1175 cond_code = gimple_cond_code (cond);
1176 if (invert)
1177 cond_code = invert_tree_comparison (cond_code, false);
1179 if (cond_code == NE_EXPR)
1181 if (integer_zerop (gimple_cond_rhs (cond))
1182 && (TREE_CODE (gimple_cond_lhs (cond)) == SSA_NAME))
1183 normalize_cond_1 (
1184 SSA_NAME_DEF_STMT (gimple_cond_lhs (cond)),
1185 norm_cond, ERROR_MARK);
1186 else if (integer_zerop (gimple_cond_lhs (cond))
1187 && (TREE_CODE (gimple_cond_rhs (cond)) == SSA_NAME))
1188 normalize_cond_1 (
1189 SSA_NAME_DEF_STMT (gimple_cond_rhs (cond)),
1190 norm_cond, ERROR_MARK);
1191 else
1193 VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1194 norm_cond->invert = invert;
1197 else
1199 VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1200 norm_cond->invert = invert;
1203 gcc_assert (VEC_length (gimple, norm_cond->conds) == 1
1204 || is_and_or_or (norm_cond->cond_code, NULL));
1207 /* Returns true if the domain for condition COND1 is a subset of
1208 COND2. REVERSE is a flag. when it is true the function checks
1209 if COND1 is a superset of COND2. INVERT1 and INVERT2 are flags
1210 to indicate if COND1 and COND2 need to be inverted or not. */
1212 static bool
1213 is_gcond_subset_of (gimple cond1, bool invert1,
1214 gimple cond2, bool invert2,
1215 bool reverse)
1217 enum gimple_code gc1, gc2;
1218 enum tree_code cond1_code, cond2_code;
1219 gimple tmp;
1220 tree cond1_lhs, cond1_rhs, cond2_lhs, cond2_rhs;
1222 /* Take the short cut. */
1223 if (cond1 == cond2)
1224 return true;
1226 if (reverse)
1228 tmp = cond1;
1229 cond1 = cond2;
1230 cond2 = tmp;
1233 gc1 = gimple_code (cond1);
1234 gc2 = gimple_code (cond2);
1236 if ((gc1 != GIMPLE_ASSIGN && gc1 != GIMPLE_COND)
1237 || (gc2 != GIMPLE_ASSIGN && gc2 != GIMPLE_COND))
1238 return cond1 == cond2;
1240 cond1_code = ((gc1 == GIMPLE_ASSIGN)
1241 ? gimple_assign_rhs_code (cond1)
1242 : gimple_cond_code (cond1));
1244 cond2_code = ((gc2 == GIMPLE_ASSIGN)
1245 ? gimple_assign_rhs_code (cond2)
1246 : gimple_cond_code (cond2));
1248 if (TREE_CODE_CLASS (cond1_code) != tcc_comparison
1249 || TREE_CODE_CLASS (cond2_code) != tcc_comparison)
1250 return false;
1252 if (invert1)
1253 cond1_code = invert_tree_comparison (cond1_code, false);
1254 if (invert2)
1255 cond2_code = invert_tree_comparison (cond2_code, false);
1257 cond1_lhs = ((gc1 == GIMPLE_ASSIGN)
1258 ? gimple_assign_rhs1 (cond1)
1259 : gimple_cond_lhs (cond1));
1260 cond1_rhs = ((gc1 == GIMPLE_ASSIGN)
1261 ? gimple_assign_rhs2 (cond1)
1262 : gimple_cond_rhs (cond1));
1263 cond2_lhs = ((gc2 == GIMPLE_ASSIGN)
1264 ? gimple_assign_rhs1 (cond2)
1265 : gimple_cond_lhs (cond2));
1266 cond2_rhs = ((gc2 == GIMPLE_ASSIGN)
1267 ? gimple_assign_rhs2 (cond2)
1268 : gimple_cond_rhs (cond2));
1270 /* Assuming const operands have been swapped to the
1271 rhs at this point of the analysis. */
1273 if (cond1_lhs != cond2_lhs)
1274 return false;
1276 if (!is_gimple_constant (cond1_rhs)
1277 || TREE_CODE (cond1_rhs) != INTEGER_CST)
1278 return (cond1_rhs == cond2_rhs);
1280 if (!is_gimple_constant (cond2_rhs)
1281 || TREE_CODE (cond2_rhs) != INTEGER_CST)
1282 return (cond1_rhs == cond2_rhs);
1284 if (cond1_code == EQ_EXPR)
1285 return is_value_included_in (cond1_rhs,
1286 cond2_rhs, cond2_code);
1287 if (cond1_code == NE_EXPR || cond2_code == EQ_EXPR)
1288 return ((cond2_code == cond1_code)
1289 && tree_int_cst_equal (cond1_rhs, cond2_rhs));
1291 if (((cond1_code == GE_EXPR || cond1_code == GT_EXPR)
1292 && (cond2_code == LE_EXPR || cond2_code == LT_EXPR))
1293 || ((cond1_code == LE_EXPR || cond1_code == LT_EXPR)
1294 && (cond2_code == GE_EXPR || cond2_code == GT_EXPR)))
1295 return false;
1297 if (cond1_code != GE_EXPR && cond1_code != GT_EXPR
1298 && cond1_code != LE_EXPR && cond1_code != LT_EXPR)
1299 return false;
1301 if (cond1_code == GT_EXPR)
1303 cond1_code = GE_EXPR;
1304 cond1_rhs = fold_binary (PLUS_EXPR, TREE_TYPE (cond1_rhs),
1305 cond1_rhs,
1306 fold_convert (TREE_TYPE (cond1_rhs),
1307 integer_one_node));
1309 else if (cond1_code == LT_EXPR)
1311 cond1_code = LE_EXPR;
1312 cond1_rhs = fold_binary (MINUS_EXPR, TREE_TYPE (cond1_rhs),
1313 cond1_rhs,
1314 fold_convert (TREE_TYPE (cond1_rhs),
1315 integer_one_node));
1318 if (!cond1_rhs)
1319 return false;
1321 gcc_assert (cond1_code == GE_EXPR || cond1_code == LE_EXPR);
1323 if (cond2_code == GE_EXPR || cond2_code == GT_EXPR ||
1324 cond2_code == LE_EXPR || cond2_code == LT_EXPR)
1325 return is_value_included_in (cond1_rhs,
1326 cond2_rhs, cond2_code);
1327 else if (cond2_code == NE_EXPR)
1328 return
1329 (is_value_included_in (cond1_rhs,
1330 cond2_rhs, cond2_code)
1331 && !is_value_included_in (cond2_rhs,
1332 cond1_rhs, cond1_code));
1333 return false;
1336 /* Returns true if the domain of the condition expression
1337 in COND is a subset of any of the sub-conditions
1338 of the normalized condtion NORM_COND. INVERT is a flag
1339 to indicate of the COND needs to be inverted.
1340 REVERSE is a flag. When it is true, the check is reversed --
1341 it returns true if COND is a superset of any of the subconditions
1342 of NORM_COND. */
1344 static bool
1345 is_subset_of_any (gimple cond, bool invert,
1346 norm_cond_t norm_cond, bool reverse)
1348 size_t i;
1349 size_t len = VEC_length (gimple, norm_cond->conds);
1351 for (i = 0; i < len; i++)
1353 if (is_gcond_subset_of (cond, invert,
1354 VEC_index (gimple, norm_cond->conds, i),
1355 false, reverse))
1356 return true;
1358 return false;
1361 /* NORM_COND1 and NORM_COND2 are normalized logical/BIT OR
1362 expressions (formed by following UD chains not control
1363 dependence chains). The function returns true of domain
1364 of and expression NORM_COND1 is a subset of NORM_COND2's.
1365 The implementation is conservative, and it returns false if
1366 it the inclusion relationship may not hold. */
1368 static bool
1369 is_or_set_subset_of (norm_cond_t norm_cond1,
1370 norm_cond_t norm_cond2)
1372 size_t i;
1373 size_t len = VEC_length (gimple, norm_cond1->conds);
1375 for (i = 0; i < len; i++)
1377 if (!is_subset_of_any (VEC_index (gimple, norm_cond1->conds, i),
1378 false, norm_cond2, false))
1379 return false;
1381 return true;
1384 /* NORM_COND1 and NORM_COND2 are normalized logical AND
1385 expressions (formed by following UD chains not control
1386 dependence chains). The function returns true of domain
1387 of and expression NORM_COND1 is a subset of NORM_COND2's. */
1389 static bool
1390 is_and_set_subset_of (norm_cond_t norm_cond1,
1391 norm_cond_t norm_cond2)
1393 size_t i;
1394 size_t len = VEC_length (gimple, norm_cond2->conds);
1396 for (i = 0; i < len; i++)
1398 if (!is_subset_of_any (VEC_index (gimple, norm_cond2->conds, i),
1399 false, norm_cond1, true))
1400 return false;
1402 return true;
1405 /* Returns true of the domain if NORM_COND1 is a subset
1406 of that of NORM_COND2. Returns false if it can not be
1407 proved to be so. */
1409 static bool
1410 is_norm_cond_subset_of (norm_cond_t norm_cond1,
1411 norm_cond_t norm_cond2)
1413 size_t i;
1414 enum tree_code code1, code2;
1416 code1 = norm_cond1->cond_code;
1417 code2 = norm_cond2->cond_code;
1419 if (code1 == TRUTH_AND_EXPR || code1 == BIT_AND_EXPR)
1421 /* Both conditions are AND expressions. */
1422 if (code2 == TRUTH_AND_EXPR || code2 == BIT_AND_EXPR)
1423 return is_and_set_subset_of (norm_cond1, norm_cond2);
1424 /* NORM_COND1 is an AND expression, and NORM_COND2 is an OR
1425 expression. In this case, returns true if any subexpression
1426 of NORM_COND1 is a subset of any subexpression of NORM_COND2. */
1427 else if (code2 == TRUTH_OR_EXPR || code2 == BIT_IOR_EXPR)
1429 size_t len1;
1430 len1 = VEC_length (gimple, norm_cond1->conds);
1431 for (i = 0; i < len1; i++)
1433 gimple cond1 = VEC_index (gimple, norm_cond1->conds, i);
1434 if (is_subset_of_any (cond1, false, norm_cond2, false))
1435 return true;
1437 return false;
1439 else
1441 gcc_assert (code2 == ERROR_MARK);
1442 gcc_assert (VEC_length (gimple, norm_cond2->conds) == 1);
1443 return is_subset_of_any (VEC_index (gimple, norm_cond2->conds, 0),
1444 norm_cond2->invert, norm_cond1, true);
1447 /* NORM_COND1 is an OR expression */
1448 else if (code1 == TRUTH_OR_EXPR || code1 == BIT_IOR_EXPR)
1450 if (code2 != code1)
1451 return false;
1453 return is_or_set_subset_of (norm_cond1, norm_cond2);
1455 else
1457 gcc_assert (code1 == ERROR_MARK);
1458 gcc_assert (VEC_length (gimple, norm_cond1->conds) == 1);
1459 /* Conservatively returns false if NORM_COND1 is non-decomposible
1460 and NORM_COND2 is an AND expression. */
1461 if (code2 == TRUTH_AND_EXPR || code2 == BIT_AND_EXPR)
1462 return false;
1464 if (code2 == TRUTH_OR_EXPR || code2 == BIT_IOR_EXPR)
1465 return is_subset_of_any (VEC_index (gimple, norm_cond1->conds, 0),
1466 norm_cond1->invert, norm_cond2, false);
1468 gcc_assert (code2 == ERROR_MARK);
1469 gcc_assert (VEC_length (gimple, norm_cond2->conds) == 1);
1470 return is_gcond_subset_of (VEC_index (gimple, norm_cond1->conds, 0),
1471 norm_cond1->invert,
1472 VEC_index (gimple, norm_cond2->conds, 0),
1473 norm_cond2->invert, false);
1477 /* Returns true of the domain of single predicate expression
1478 EXPR1 is a subset of that of EXPR2. Returns false if it
1479 can not be proved. */
1481 static bool
1482 is_pred_expr_subset_of (use_pred_info_t expr1,
1483 use_pred_info_t expr2)
1485 gimple cond1, cond2;
1486 enum tree_code code1, code2;
1487 struct norm_cond norm_cond1, norm_cond2;
1488 bool is_subset = false;
1490 cond1 = expr1->cond;
1491 cond2 = expr2->cond;
1492 code1 = gimple_cond_code (cond1);
1493 code2 = gimple_cond_code (cond2);
1495 if (expr1->invert)
1496 code1 = invert_tree_comparison (code1, false);
1497 if (expr2->invert)
1498 code2 = invert_tree_comparison (code2, false);
1500 /* Fast path -- match exactly */
1501 if ((gimple_cond_lhs (cond1) == gimple_cond_lhs (cond2))
1502 && (gimple_cond_rhs (cond1) == gimple_cond_rhs (cond2))
1503 && (code1 == code2))
1504 return true;
1506 /* Normalize conditions. To keep NE_EXPR, do not invert
1507 with both need inversion. */
1508 normalize_cond (cond1, &norm_cond1, (expr1->invert));
1509 normalize_cond (cond2, &norm_cond2, (expr2->invert));
1511 is_subset = is_norm_cond_subset_of (&norm_cond1, &norm_cond2);
1513 /* Free memory */
1514 VEC_free (gimple, heap, norm_cond1.conds);
1515 VEC_free (gimple, heap, norm_cond2.conds);
1516 return is_subset ;
1519 /* Returns true if the domain of PRED1 is a subset
1520 of that of PRED2. Returns false if it can not be proved so. */
1522 static bool
1523 is_pred_chain_subset_of (VEC(use_pred_info_t, heap) *pred1,
1524 VEC(use_pred_info_t, heap) *pred2)
1526 size_t np1, np2, i1, i2;
1528 np1 = VEC_length (use_pred_info_t, pred1);
1529 np2 = VEC_length (use_pred_info_t, pred2);
1531 for (i2 = 0; i2 < np2; i2++)
1533 bool found = false;
1534 use_pred_info_t info2
1535 = VEC_index (use_pred_info_t, pred2, i2);
1536 for (i1 = 0; i1 < np1; i1++)
1538 use_pred_info_t info1
1539 = VEC_index (use_pred_info_t, pred1, i1);
1540 if (is_pred_expr_subset_of (info1, info2))
1542 found = true;
1543 break;
1546 if (!found)
1547 return false;
1549 return true;
1552 /* Returns true if the domain defined by
1553 one pred chain ONE_PRED is a subset of the domain
1554 of *PREDS. It returns false if ONE_PRED's domain is
1555 not a subset of any of the sub-domains of PREDS (
1556 corresponding to each individual chains in it), even
1557 though it may be still be a subset of whole domain
1558 of PREDS which is the union (ORed) of all its subdomains.
1559 In other words, the result is conservative. */
1561 static bool
1562 is_included_in (VEC(use_pred_info_t, heap) *one_pred,
1563 VEC(use_pred_info_t, heap) **preds,
1564 size_t n)
1566 size_t i;
1568 for (i = 0; i < n; i++)
1570 if (is_pred_chain_subset_of (one_pred, preds[i]))
1571 return true;
1574 return false;
1577 /* compares two predicate sets PREDS1 and PREDS2 and returns
1578 true if the domain defined by PREDS1 is a superset
1579 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1580 PREDS2 respectively. The implementation chooses not to build
1581 generic trees (and relying on the folding capability of the
1582 compiler), but instead performs brute force comparison of
1583 individual predicate chains (won't be a compile time problem
1584 as the chains are pretty short). When the function returns
1585 false, it does not necessarily mean *PREDS1 is not a superset
1586 of *PREDS2, but mean it may not be so since the analysis can
1587 not prove it. In such cases, false warnings may still be
1588 emitted. */
1590 static bool
1591 is_superset_of (VEC(use_pred_info_t, heap) **preds1,
1592 size_t n1,
1593 VEC(use_pred_info_t, heap) **preds2,
1594 size_t n2)
1596 size_t i;
1597 VEC(use_pred_info_t, heap) *one_pred_chain;
1599 for (i = 0; i < n2; i++)
1601 one_pred_chain = preds2[i];
1602 if (!is_included_in (one_pred_chain, preds1, n1))
1603 return false;
1606 return true;
1609 /* Computes the predicates that guard the use and checks
1610 if the incoming paths that have empty (or possibly
1611 empty) defintion can be pruned/filtered. The function returns
1612 true if it can be determined that the use of PHI's def in
1613 USE_STMT is guarded with a predicate set not overlapping with
1614 predicate sets of all runtime paths that do not have a definition.
1615 Returns false if it is not or it can not be determined. USE_BB is
1616 the bb of the use (for phi operand use, the bb is not the bb of
1617 the phi stmt, but the src bb of the operand edge). UNINIT_OPNDS
1618 is a bit vector. If an operand of PHI is uninitialized, the
1619 correponding bit in the vector is 1. VISIED_PHIS is a pointer
1620 set of phis being visted. */
1622 static bool
1623 is_use_properly_guarded (gimple use_stmt,
1624 basic_block use_bb,
1625 gimple phi,
1626 unsigned uninit_opnds,
1627 struct pointer_set_t *visited_phis)
1629 basic_block phi_bb;
1630 VEC(use_pred_info_t, heap) **preds = 0;
1631 VEC(use_pred_info_t, heap) **def_preds = 0;
1632 size_t num_preds = 0, num_def_preds = 0;
1633 bool has_valid_preds = false;
1634 bool is_properly_guarded = false;
1636 if (pointer_set_insert (visited_phis, phi))
1637 return false;
1639 phi_bb = gimple_bb (phi);
1641 if (is_non_loop_exit_postdominating (use_bb, phi_bb))
1642 return false;
1644 has_valid_preds = find_predicates (&preds, &num_preds,
1645 phi_bb, use_bb);
1647 if (!has_valid_preds)
1649 destroy_predicate_vecs (num_preds, preds);
1650 return false;
1653 if (dump_file)
1654 dump_predicates (use_stmt, num_preds, preds,
1655 "\nUse in stmt ");
1657 has_valid_preds = find_def_preds (&def_preds,
1658 &num_def_preds, phi);
1660 if (has_valid_preds)
1662 if (dump_file)
1663 dump_predicates (phi, num_def_preds, def_preds,
1664 "Operand defs of phi ");
1665 is_properly_guarded =
1666 is_superset_of (def_preds, num_def_preds,
1667 preds, num_preds);
1670 /* further prune the dead incoming phi edges. */
1671 if (!is_properly_guarded)
1672 is_properly_guarded
1673 = use_pred_not_overlap_with_undef_path_pred (
1674 num_preds, preds, phi, uninit_opnds, visited_phis);
1676 destroy_predicate_vecs (num_preds, preds);
1677 destroy_predicate_vecs (num_def_preds, def_preds);
1678 return is_properly_guarded;
1681 /* Searches through all uses of a potentially
1682 uninitialized variable defined by PHI and returns a use
1683 statement if the use is not properly guarded. It returns
1684 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
1685 holding the position(s) of uninit PHI operands. WORKLIST
1686 is the vector of candidate phis that may be updated by this
1687 function. ADDED_TO_WORKLIST is the pointer set tracking
1688 if the new phi is already in the worklist. */
1690 static gimple
1691 find_uninit_use (gimple phi, unsigned uninit_opnds,
1692 VEC(gimple, heap) **worklist,
1693 struct pointer_set_t *added_to_worklist)
1695 tree phi_result;
1696 use_operand_p use_p;
1697 gimple use_stmt;
1698 imm_use_iterator iter;
1700 phi_result = gimple_phi_result (phi);
1702 FOR_EACH_IMM_USE_FAST (use_p, iter, phi_result)
1704 struct pointer_set_t *visited_phis;
1705 basic_block use_bb;
1707 use_stmt = USE_STMT (use_p);
1708 if (is_gimple_debug (use_stmt))
1709 continue;
1711 visited_phis = pointer_set_create ();
1713 if (gimple_code (use_stmt) == GIMPLE_PHI)
1714 use_bb = gimple_phi_arg_edge (use_stmt,
1715 PHI_ARG_INDEX_FROM_USE (use_p))->src;
1716 else
1717 use_bb = gimple_bb (use_stmt);
1719 if (is_use_properly_guarded (use_stmt,
1720 use_bb,
1721 phi,
1722 uninit_opnds,
1723 visited_phis))
1725 pointer_set_destroy (visited_phis);
1726 continue;
1728 pointer_set_destroy (visited_phis);
1730 if (dump_file && (dump_flags & TDF_DETAILS))
1732 fprintf (dump_file, "[CHECK]: Found unguarded use: ");
1733 print_gimple_stmt (dump_file, use_stmt, 0, 0);
1735 /* Found one real use, return. */
1736 if (gimple_code (use_stmt) != GIMPLE_PHI)
1737 return use_stmt;
1739 /* Found a phi use that is not guarded,
1740 add the phi to the worklist. */
1741 if (!pointer_set_insert (added_to_worklist,
1742 use_stmt))
1744 if (dump_file && (dump_flags & TDF_DETAILS))
1746 fprintf (dump_file, "[WORKLIST]: Update worklist with phi: ");
1747 print_gimple_stmt (dump_file, use_stmt, 0, 0);
1750 VEC_safe_push (gimple, heap, *worklist, use_stmt);
1751 pointer_set_insert (possibly_undefined_names,
1752 phi_result);
1756 return NULL;
1759 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
1760 and gives warning if there exists a runtime path from the entry to a
1761 use of the PHI def that does not contain a definition. In other words,
1762 the warning is on the real use. The more dead paths that can be pruned
1763 by the compiler, the fewer false positives the warning is. WORKLIST
1764 is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
1765 a pointer set tracking if the new phi is added to the worklist or not. */
1767 static void
1768 warn_uninitialized_phi (gimple phi, VEC(gimple, heap) **worklist,
1769 struct pointer_set_t *added_to_worklist)
1771 unsigned uninit_opnds;
1772 gimple uninit_use_stmt = 0;
1773 tree uninit_op;
1775 /* Don't look at memory tags. */
1776 if (!is_gimple_reg (gimple_phi_result (phi)))
1777 return;
1779 uninit_opnds = compute_uninit_opnds_pos (phi);
1781 if (MASK_EMPTY (uninit_opnds))
1782 return;
1784 if (dump_file && (dump_flags & TDF_DETAILS))
1786 fprintf (dump_file, "[CHECK]: examining phi: ");
1787 print_gimple_stmt (dump_file, phi, 0, 0);
1790 /* Now check if we have any use of the value without proper guard. */
1791 uninit_use_stmt = find_uninit_use (phi, uninit_opnds,
1792 worklist, added_to_worklist);
1794 /* All uses are properly guarded. */
1795 if (!uninit_use_stmt)
1796 return;
1798 uninit_op = gimple_phi_arg_def (phi, MASK_FIRST_SET_BIT (uninit_opnds));
1799 warn_uninit (uninit_op,
1800 "%qD may be used uninitialized in this function",
1801 uninit_use_stmt);
1806 /* Entry point to the late uninitialized warning pass. */
1808 static unsigned int
1809 execute_late_warn_uninitialized (void)
1811 basic_block bb;
1812 gimple_stmt_iterator gsi;
1813 VEC(gimple, heap) *worklist = 0;
1814 struct pointer_set_t *added_to_worklist;
1816 calculate_dominance_info (CDI_DOMINATORS);
1817 calculate_dominance_info (CDI_POST_DOMINATORS);
1818 /* Re-do the plain uninitialized variable check, as optimization may have
1819 straightened control flow. Do this first so that we don't accidentally
1820 get a "may be" warning when we'd have seen an "is" warning later. */
1821 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
1823 timevar_push (TV_TREE_UNINIT);
1825 possibly_undefined_names = pointer_set_create ();
1826 added_to_worklist = pointer_set_create ();
1828 /* Initialize worklist */
1829 FOR_EACH_BB (bb)
1830 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1832 gimple phi = gsi_stmt (gsi);
1833 size_t n, i;
1835 n = gimple_phi_num_args (phi);
1837 /* Don't look at memory tags. */
1838 if (!is_gimple_reg (gimple_phi_result (phi)))
1839 continue;
1841 for (i = 0; i < n; ++i)
1843 tree op = gimple_phi_arg_def (phi, i);
1844 if (TREE_CODE (op) == SSA_NAME
1845 && ssa_undefined_value_p (op))
1847 VEC_safe_push (gimple, heap, worklist, phi);
1848 pointer_set_insert (added_to_worklist, phi);
1849 if (dump_file && (dump_flags & TDF_DETAILS))
1851 fprintf (dump_file, "[WORKLIST]: add to initial list: ");
1852 print_gimple_stmt (dump_file, phi, 0, 0);
1854 break;
1859 while (VEC_length (gimple, worklist) != 0)
1861 gimple cur_phi = 0;
1862 cur_phi = VEC_pop (gimple, worklist);
1863 warn_uninitialized_phi (cur_phi, &worklist, added_to_worklist);
1866 VEC_free (gimple, heap, worklist);
1867 pointer_set_destroy (added_to_worklist);
1868 pointer_set_destroy (possibly_undefined_names);
1869 possibly_undefined_names = NULL;
1870 free_dominance_info (CDI_POST_DOMINATORS);
1871 timevar_pop (TV_TREE_UNINIT);
1872 return 0;
1875 static bool
1876 gate_warn_uninitialized (void)
1878 return warn_uninitialized != 0;
1881 struct gimple_opt_pass pass_late_warn_uninitialized =
1884 GIMPLE_PASS,
1885 "uninit", /* name */
1886 gate_warn_uninitialized, /* gate */
1887 execute_late_warn_uninitialized, /* execute */
1888 NULL, /* sub */
1889 NULL, /* next */
1890 0, /* static_pass_number */
1891 TV_NONE, /* tv_id */
1892 PROP_ssa, /* properties_required */
1893 0, /* properties_provided */
1894 0, /* properties_destroyed */
1895 0, /* todo_flags_start */
1896 0 /* todo_flags_finish */