Remove outermost loop parameter.
[official-gcc/graphite-test-results.git] / gcc / tree-ssa-uninit.c
blob6d58f5b16312ed715ba25883b8644607ee43c89b
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 "toplev.h"
44 #include "timevar.h"
46 /* This implements the pass that does predicate aware warning on uses of
47 possibly uninitialized variables. The pass first collects the set of
48 possibly uninitialized SSA names. For each such name, it walks through
49 all its immediate uses. For each immediate use, it rebuilds the condition
50 expression (the predicate) that guards the use. The predicate is then
51 examined to see if the variable is always defined under that same condition.
52 This is done either by pruning the unrealizable paths that lead to the
53 default definitions or by checking if the predicate set that guards the
54 defining paths is a superset of the use predicate. */
57 /* Pointer set of potentially undefined ssa names, i.e.,
58 ssa names that are defined by phi with operands that
59 are not defined or potentially undefined. */
60 static struct pointer_set_t *possibly_undefined_names = 0;
62 /* Bit mask handling macros. */
63 #define MASK_SET_BIT(mask, pos) mask |= (1 << pos)
64 #define MASK_TEST_BIT(mask, pos) (mask & (1 << pos))
65 #define MASK_EMPTY(mask) (mask == 0)
67 /* Returns the first bit position (starting from LSB)
68 in mask that is non zero. Returns -1 if the mask is empty. */
69 static int
70 get_mask_first_set_bit (unsigned mask)
72 int pos = 0;
73 if (mask == 0)
74 return -1;
76 while ((mask & (1 << pos)) == 0)
77 pos++;
79 return pos;
81 #define MASK_FIRST_SET_BIT(mask) get_mask_first_set_bit (mask)
84 /* Return true if T, an SSA_NAME, has an undefined value. */
86 bool
87 ssa_undefined_value_p (tree t)
89 tree var = SSA_NAME_VAR (t);
91 /* Parameters get their initial value from the function entry. */
92 if (TREE_CODE (var) == PARM_DECL)
93 return false;
95 /* Hard register variables get their initial value from the ether. */
96 if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
97 return false;
99 /* The value is undefined iff its definition statement is empty. */
100 return (gimple_nop_p (SSA_NAME_DEF_STMT (t))
101 || (possibly_undefined_names
102 && pointer_set_contains (possibly_undefined_names, t)));
105 /* Checks if the operand OPND of PHI is defined by
106 another phi with one operand defined by this PHI,
107 but the rest operands are all defined. If yes,
108 returns true to skip this this operand as being
109 redundant. Can be enhanced to be more general. */
111 static bool
112 can_skip_redundant_opnd (tree opnd, gimple phi)
114 gimple op_def;
115 tree phi_def;
116 int i, n;
118 phi_def = gimple_phi_result (phi);
119 op_def = SSA_NAME_DEF_STMT (opnd);
120 if (gimple_code (op_def) != GIMPLE_PHI)
121 return false;
122 n = gimple_phi_num_args (op_def);
123 for (i = 0; i < n; ++i)
125 tree op = gimple_phi_arg_def (op_def, i);
126 if (TREE_CODE (op) != SSA_NAME)
127 continue;
128 if (op != phi_def && ssa_undefined_value_p (op))
129 return false;
132 return true;
135 /* Returns a bit mask holding the positions of arguments in PHI
136 that have empty (or possibly empty) definitions. */
138 static unsigned
139 compute_uninit_opnds_pos (gimple phi)
141 size_t i, n;
142 unsigned uninit_opnds = 0;
144 n = gimple_phi_num_args (phi);
146 for (i = 0; i < n; ++i)
148 tree op = gimple_phi_arg_def (phi, i);
149 if (TREE_CODE (op) == SSA_NAME
150 && ssa_undefined_value_p (op)
151 && !can_skip_redundant_opnd (op, phi))
152 MASK_SET_BIT (uninit_opnds, i);
154 return uninit_opnds;
157 /* Find the immediate postdominator PDOM of the specified
158 basic block BLOCK. */
160 static inline basic_block
161 find_pdom (basic_block block)
163 if (block == EXIT_BLOCK_PTR)
164 return EXIT_BLOCK_PTR;
165 else
167 basic_block bb
168 = get_immediate_dominator (CDI_POST_DOMINATORS, block);
169 if (! bb)
170 return EXIT_BLOCK_PTR;
171 return bb;
175 /* Find the immediate DOM of the specified
176 basic block BLOCK. */
178 static inline basic_block
179 find_dom (basic_block block)
181 if (block == ENTRY_BLOCK_PTR)
182 return ENTRY_BLOCK_PTR;
183 else
185 basic_block bb = get_immediate_dominator (CDI_DOMINATORS, block);
186 if (! bb)
187 return ENTRY_BLOCK_PTR;
188 return bb;
192 /* Returns true if BB1 is postdominating BB2 and BB1 is
193 not a loop exit bb. The loop exit bb check is simple and does
194 not cover all cases. */
196 static bool
197 is_non_loop_exit_postdominating (basic_block bb1, basic_block bb2)
199 if (!dominated_by_p (CDI_POST_DOMINATORS, bb2, bb1))
200 return false;
202 if (single_pred_p (bb1) && !single_succ_p (bb2))
203 return false;
205 return true;
208 /* Find the closest postdominator of a specified BB, which is control
209 equivalent to BB. */
211 static inline basic_block
212 find_control_equiv_block (basic_block bb)
214 basic_block pdom;
216 pdom = find_pdom (bb);
218 /* Skip the postdominating bb that is also loop exit. */
219 if (!is_non_loop_exit_postdominating (pdom, bb))
220 return NULL;
222 if (dominated_by_p (CDI_DOMINATORS, pdom, bb))
223 return pdom;
225 return NULL;
228 #define MAX_NUM_CHAINS 8
229 #define MAX_CHAIN_LEN 5
231 /* Computes the control dependence chains (paths of edges)
232 for DEP_BB up to the dominating basic block BB (the head node of a
233 chain should be dominated by it). CD_CHAINS is pointer to a
234 dynamic array holding the result chains. CUR_CD_CHAIN is the current
235 chain being computed. *NUM_CHAINS is total number of chains. The
236 function returns true if the information is successfully computed,
237 return false if there is no control dependence or not computed. */
239 static bool
240 compute_control_dep_chain (basic_block bb, basic_block dep_bb,
241 VEC(edge, heap) **cd_chains,
242 size_t *num_chains,
243 VEC(edge, heap) **cur_cd_chain)
245 edge_iterator ei;
246 edge e;
247 size_t i;
248 bool found_cd_chain = false;
249 size_t cur_chain_len = 0;
251 if (EDGE_COUNT (bb->succs) < 2)
252 return false;
254 /* Could use a set instead. */
255 cur_chain_len = VEC_length (edge, *cur_cd_chain);
256 if (cur_chain_len > MAX_CHAIN_LEN)
257 return false;
259 for (i = 0; i < cur_chain_len; i++)
261 edge e = VEC_index (edge, *cur_cd_chain, i);
262 /* cycle detected. */
263 if (e->src == bb)
264 return false;
267 FOR_EACH_EDGE (e, ei, bb->succs)
269 basic_block cd_bb;
270 if (e->flags & (EDGE_FAKE | EDGE_ABNORMAL))
271 continue;
273 cd_bb = e->dest;
274 VEC_safe_push (edge, heap, *cur_cd_chain, e);
275 while (!is_non_loop_exit_postdominating (cd_bb, bb))
277 if (cd_bb == dep_bb)
279 /* Found a direct control dependence. */
280 if (*num_chains < MAX_NUM_CHAINS)
282 cd_chains[*num_chains]
283 = VEC_copy (edge, heap, *cur_cd_chain);
284 (*num_chains)++;
286 found_cd_chain = true;
287 /* check path from next edge. */
288 break;
291 /* Now check if DEP_BB is indirectly control dependent on BB. */
292 if (compute_control_dep_chain (cd_bb, dep_bb, cd_chains,
293 num_chains, cur_cd_chain))
295 found_cd_chain = true;
296 break;
299 cd_bb = find_pdom (cd_bb);
300 if (cd_bb == EXIT_BLOCK_PTR)
301 break;
303 VEC_pop (edge, *cur_cd_chain);
304 gcc_assert (VEC_length (edge, *cur_cd_chain) == cur_chain_len);
306 gcc_assert (VEC_length (edge, *cur_cd_chain) == cur_chain_len);
308 return found_cd_chain;
311 typedef struct use_pred_info
313 gimple cond;
314 bool invert;
315 } *use_pred_info_t;
317 DEF_VEC_P(use_pred_info_t);
318 DEF_VEC_ALLOC_P(use_pred_info_t, heap);
321 /* Converts the chains of control dependence edges into a set of
322 predicates. A control dependence chain is represented by a vector
323 edges. DEP_CHAINS points to an array of dependence chains.
324 NUM_CHAINS is the size of the chain array. One edge in a dependence
325 chain is mapped to predicate expression represented by use_pred_info_t
326 type. One dependence chain is converted to a composite predicate that
327 is the result of AND operation of use_pred_info_t mapped to each edge.
328 A composite predicate is presented by a vector of use_pred_info_t. On
329 return, *PREDS points to the resulting array of composite predicates.
330 *NUM_PREDS is the number of composite predictes. */
332 static bool
333 convert_control_dep_chain_into_preds (VEC(edge, heap) **dep_chains,
334 size_t num_chains,
335 VEC(use_pred_info_t, heap) ***preds,
336 size_t *num_preds)
338 bool has_valid_pred = false;
339 size_t i, j;
340 if (num_chains == 0 || num_chains >= MAX_NUM_CHAINS)
341 return false;
343 /* Now convert CD chains into predicates */
344 has_valid_pred = true;
346 /* Now convert the control dep chain into a set
347 of predicates. */
348 *preds = XCNEWVEC (VEC(use_pred_info_t, heap) *,
349 num_chains);
350 *num_preds = num_chains;
352 for (i = 0; i < num_chains; i++)
354 VEC(edge, heap) *one_cd_chain = dep_chains[i];
355 for (j = 0; j < VEC_length (edge, one_cd_chain); j++)
357 gimple cond_stmt;
358 gimple_stmt_iterator gsi;
359 basic_block guard_bb;
360 use_pred_info_t one_pred;
361 edge e;
363 e = VEC_index (edge, one_cd_chain, j);
364 guard_bb = e->src;
365 gsi = gsi_last_bb (guard_bb);
366 if (gsi_end_p (gsi))
368 has_valid_pred = false;
369 break;
371 cond_stmt = gsi_stmt (gsi);
372 if (gimple_code (cond_stmt) == GIMPLE_CALL
373 && EDGE_COUNT (e->src->succs) >= 2)
375 /* Ignore EH edge. Can add assertion
376 on the other edge's flag. */
377 continue;
379 /* Skip if there is essentially one succesor. */
380 if (EDGE_COUNT (e->src->succs) == 2)
382 edge e1;
383 edge_iterator ei1;
384 bool skip = false;
386 FOR_EACH_EDGE (e1, ei1, e->src->succs)
388 if (EDGE_COUNT (e1->dest->succs) == 0)
390 skip = true;
391 break;
394 if (skip)
395 continue;
397 if (gimple_code (cond_stmt) != GIMPLE_COND)
399 has_valid_pred = false;
400 break;
402 one_pred = XNEW (struct use_pred_info);
403 one_pred->cond = cond_stmt;
404 one_pred->invert = !!(e->flags & EDGE_FALSE_VALUE);
405 VEC_safe_push (use_pred_info_t, heap, (*preds)[i], one_pred);
408 if (!has_valid_pred)
409 break;
411 return has_valid_pred;
414 /* Computes all control dependence chains for USE_BB. The control
415 dependence chains are then converted to an array of composite
416 predicates pointed to by PREDS. PHI_BB is the basic block of
417 the phi whose result is used in USE_BB. */
419 static bool
420 find_predicates (VEC(use_pred_info_t, heap) ***preds,
421 size_t *num_preds,
422 basic_block phi_bb,
423 basic_block use_bb)
425 size_t num_chains = 0, i;
426 VEC(edge, heap) **dep_chains = 0;
427 VEC(edge, heap) *cur_chain = 0;
428 bool has_valid_pred = false;
429 basic_block cd_root = 0;
431 dep_chains = XCNEWVEC (VEC(edge, heap) *, MAX_NUM_CHAINS);
433 /* First find the closest bb that is control equivalent to PHI_BB
434 that also dominates USE_BB. */
435 cd_root = phi_bb;
436 while (dominated_by_p (CDI_DOMINATORS, use_bb, cd_root))
438 basic_block ctrl_eq_bb = find_control_equiv_block (cd_root);
439 if (ctrl_eq_bb && dominated_by_p (CDI_DOMINATORS, use_bb, ctrl_eq_bb))
440 cd_root = ctrl_eq_bb;
441 else
442 break;
445 compute_control_dep_chain (cd_root, use_bb,
446 dep_chains, &num_chains,
447 &cur_chain);
449 has_valid_pred
450 = convert_control_dep_chain_into_preds (dep_chains,
451 num_chains,
452 preds,
453 num_preds);
454 /* Free individual chain */
455 VEC_free (edge, heap, cur_chain);
456 for (i = 0; i < num_chains; i++)
457 VEC_free (edge, heap, dep_chains[i]);
458 free (dep_chains);
459 return has_valid_pred;
462 /* Computes the set of incoming edges of PHI that have non empty
463 definitions of a phi chain. The collection will be done
464 recursively on operands that are defined by phis. CD_ROOT
465 is the control dependence root. *EDGES holds the result, and
466 VISITED_PHIS is a pointer set for detecting cycles. */
468 static void
469 collect_phi_def_edges (gimple phi, basic_block cd_root,
470 VEC(edge, heap) **edges,
471 struct pointer_set_t *visited_phis)
473 size_t i, n;
474 edge opnd_edge;
475 tree opnd;
477 if (pointer_set_insert (visited_phis, phi))
478 return;
480 n = gimple_phi_num_args (phi);
481 for (i = 0; i < n; i++)
483 opnd_edge = gimple_phi_arg_edge (phi, i);
484 opnd = gimple_phi_arg_def (phi, i);
486 if (TREE_CODE (opnd) != SSA_NAME
487 || !ssa_undefined_value_p (opnd))
488 VEC_safe_push (edge, heap, *edges, opnd_edge);
489 else
491 gimple def = SSA_NAME_DEF_STMT (opnd);
492 if (gimple_code (def) == GIMPLE_PHI
493 && dominated_by_p (CDI_DOMINATORS,
494 gimple_bb (def), cd_root))
495 collect_phi_def_edges (def, cd_root, edges,
496 visited_phis);
501 /* For each use edge of PHI, computes all control dependence chains.
502 The control dependence chains are then converted to an array of
503 composite predicates pointed to by PREDS. */
505 static bool
506 find_def_preds (VEC(use_pred_info_t, heap) ***preds,
507 size_t *num_preds, gimple phi)
509 size_t num_chains = 0, i, n;
510 VEC(edge, heap) **dep_chains = 0;
511 VEC(edge, heap) *cur_chain = 0;
512 VEC(edge, heap) *def_edges = 0;
513 bool has_valid_pred = false;
514 basic_block phi_bb, cd_root = 0;
515 struct pointer_set_t *visited_phis;
517 dep_chains = XCNEWVEC (VEC(edge, heap) *, MAX_NUM_CHAINS);
519 phi_bb = gimple_bb (phi);
520 /* First find the closest dominating bb to be
521 the control dependence root */
522 cd_root = find_dom (phi_bb);
523 if (!cd_root)
524 return false;
526 visited_phis = pointer_set_create ();
527 collect_phi_def_edges (phi, cd_root, &def_edges, visited_phis);
528 pointer_set_destroy (visited_phis);
530 n = VEC_length (edge, def_edges);
531 if (n == 0)
532 return false;
534 for (i = 0; i < n; i++)
536 size_t prev_nc, j;
537 edge opnd_edge;
539 opnd_edge = VEC_index (edge, def_edges, i);
540 prev_nc = num_chains;
541 compute_control_dep_chain (cd_root, opnd_edge->src,
542 dep_chains, &num_chains,
543 &cur_chain);
544 /* Free individual chain */
545 VEC_free (edge, heap, cur_chain);
546 cur_chain = 0;
548 /* Now update the newly added chains with
549 the phi operand edge: */
550 if (EDGE_COUNT (opnd_edge->src->succs) > 1)
552 if (prev_nc == num_chains
553 && num_chains < MAX_NUM_CHAINS)
554 num_chains++;
555 for (j = prev_nc; j < num_chains; j++)
557 VEC_safe_push (edge, heap, dep_chains[j], opnd_edge);
562 has_valid_pred
563 = convert_control_dep_chain_into_preds (dep_chains,
564 num_chains,
565 preds,
566 num_preds);
567 for (i = 0; i < num_chains; i++)
568 VEC_free (edge, heap, dep_chains[i]);
569 free (dep_chains);
570 return has_valid_pred;
573 /* Dumps the predicates (PREDS) for USESTMT. */
575 static void
576 dump_predicates (gimple usestmt, size_t num_preds,
577 VEC(use_pred_info_t, heap) **preds,
578 const char* msg)
580 size_t i, j;
581 VEC(use_pred_info_t, heap) *one_pred_chain;
582 fprintf (dump_file, msg);
583 print_gimple_stmt (dump_file, usestmt, 0, 0);
584 fprintf (dump_file, "is guarded by :\n");
585 /* do some dumping here: */
586 for (i = 0; i < num_preds; i++)
588 size_t np;
590 one_pred_chain = preds[i];
591 np = VEC_length (use_pred_info_t, one_pred_chain);
593 for (j = 0; j < np; j++)
595 use_pred_info_t one_pred
596 = VEC_index (use_pred_info_t, one_pred_chain, j);
597 if (one_pred->invert)
598 fprintf (dump_file, " (.NOT.) ");
599 print_gimple_stmt (dump_file, one_pred->cond, 0, 0);
600 if (j < np - 1)
601 fprintf (dump_file, "(.AND.)\n");
603 if (i < num_preds - 1)
604 fprintf (dump_file, "(.OR.)\n");
608 /* Destroys the predicate set *PREDS. */
610 static void
611 destroy_predicate_vecs (size_t n,
612 VEC(use_pred_info_t, heap) ** preds)
614 size_t i, j;
615 for (i = 0; i < n; i++)
617 for (j = 0; j < VEC_length (use_pred_info_t, preds[i]); j++)
618 free (VEC_index (use_pred_info_t, preds[i], j));
619 VEC_free (use_pred_info_t, heap, preds[i]);
621 free (preds);
625 /* Computes the 'normalized' conditional code with operand
626 swapping and condition inversion. */
628 static enum tree_code
629 get_cmp_code (enum tree_code orig_cmp_code,
630 bool swap_cond, bool invert)
632 enum tree_code tc = orig_cmp_code;
634 if (swap_cond)
635 tc = swap_tree_comparison (orig_cmp_code);
636 if (invert)
637 tc = invert_tree_comparison (tc, false);
639 switch (tc)
641 case LT_EXPR:
642 case LE_EXPR:
643 case GT_EXPR:
644 case GE_EXPR:
645 case EQ_EXPR:
646 case NE_EXPR:
647 break;
648 default:
649 return ERROR_MARK;
651 return tc;
654 /* Returns true if VAL falls in the range defined by BOUNDARY and CMPC, i.e.
655 all values in the range satisfies (x CMPC BOUNDARY) == true. */
657 static bool
658 is_value_included_in (tree val, tree boundary, enum tree_code cmpc)
660 bool inverted = false;
661 bool is_unsigned;
662 bool result;
664 /* Only handle integer constant here. */
665 if (TREE_CODE (val) != INTEGER_CST
666 || TREE_CODE (boundary) != INTEGER_CST)
667 return true;
669 is_unsigned = TYPE_UNSIGNED (TREE_TYPE (val));
671 if (cmpc == GE_EXPR || cmpc == GT_EXPR
672 || cmpc == NE_EXPR)
674 cmpc = invert_tree_comparison (cmpc, false);
675 inverted = true;
678 if (is_unsigned)
680 if (cmpc == EQ_EXPR)
681 result = tree_int_cst_equal (val, boundary);
682 else if (cmpc == LT_EXPR)
683 result = INT_CST_LT_UNSIGNED (val, boundary);
684 else
686 gcc_assert (cmpc == LE_EXPR);
687 result = (tree_int_cst_equal (val, boundary)
688 || INT_CST_LT_UNSIGNED (val, boundary));
691 else
693 if (cmpc == EQ_EXPR)
694 result = tree_int_cst_equal (val, boundary);
695 else if (cmpc == LT_EXPR)
696 result = INT_CST_LT (val, boundary);
697 else
699 gcc_assert (cmpc == LE_EXPR);
700 result = (tree_int_cst_equal (val, boundary)
701 || INT_CST_LT (val, boundary));
705 if (inverted)
706 result ^= 1;
708 return result;
711 /* Returns true if PRED is common among all the predicate
712 chains (PREDS) (and therefore can be factored out).
713 NUM_PRED_CHAIN is the size of array PREDS. */
715 static bool
716 find_matching_predicate_in_rest_chains (use_pred_info_t pred,
717 VEC(use_pred_info_t, heap) **preds,
718 size_t num_pred_chains)
720 size_t i, j, n;
722 /* trival case */
723 if (num_pred_chains == 1)
724 return true;
726 for (i = 1; i < num_pred_chains; i++)
728 bool found = false;
729 VEC(use_pred_info_t, heap) *one_chain = preds[i];
730 n = VEC_length (use_pred_info_t, one_chain);
731 for (j = 0; j < n; j++)
733 use_pred_info_t pred2
734 = VEC_index (use_pred_info_t, one_chain, j);
735 /* can relax the condition comparison to not
736 use address comparison. However, the most common
737 case is that multiple control dependent paths share
738 a common path prefix, so address comparison should
739 be ok. */
741 if (pred2->cond == pred->cond
742 && pred2->invert == pred->invert)
744 found = true;
745 break;
748 if (!found)
749 return false;
751 return true;
754 /* Forward declaration. */
755 static bool
756 is_use_properly_guarded (gimple use_stmt,
757 basic_block use_bb,
758 gimple phi,
759 unsigned uninit_opnds,
760 struct pointer_set_t *visited_phis);
762 /* A helper function that determines if the predicate set
763 of the use is not overlapping with that of the uninit paths.
764 The most common senario of guarded use is in Example 1:
765 Example 1:
766 if (some_cond)
768 x = ...;
769 flag = true;
772 ... some code ...
774 if (flag)
775 use (x);
777 The real world examples are usually more complicated, but similar
778 and usually result from inlining:
780 bool init_func (int * x)
782 if (some_cond)
783 return false;
784 *x = ..
785 return true;
788 void foo(..)
790 int x;
792 if (!init_func(&x))
793 return;
795 .. some_code ...
796 use (x);
799 Another possible use scenario is in the following trivial example:
801 Example 2:
802 if (n > 0)
803 x = 1;
805 if (n > 0)
807 if (m < 2)
808 .. = x;
811 Predicate analysis needs to compute the composite predicate:
813 1) 'x' use predicate: (n > 0) .AND. (m < 2)
814 2) 'x' default value (non-def) predicate: .NOT. (n > 0)
815 (the predicate chain for phi operand defs can be computed
816 starting from a bb that is control equivalent to the phi's
817 bb and is dominating the operand def.)
819 and check overlapping:
820 (n > 0) .AND. (m < 2) .AND. (.NOT. (n > 0))
821 <==> false
823 This implementation provides framework that can handle
824 scenarios. (Note that many simple cases are handled properly
825 without the predicate analysis -- this is due to jump threading
826 transformation which eliminates the merge point thus makes
827 path sensitive analysis unnecessary.)
829 NUM_PREDS is the number is the number predicate chains, PREDS is
830 the array of chains, PHI is the phi node whose incoming (undefined)
831 paths need to be pruned, and UNINIT_OPNDS is the bitmap holding
832 uninit operand positions. VISITED_PHIS is the pointer set of phi
833 stmts being checked. */
836 static bool
837 use_pred_not_overlap_with_undef_path_pred (
838 size_t num_preds,
839 VEC(use_pred_info_t, heap) **preds,
840 gimple phi, unsigned uninit_opnds,
841 struct pointer_set_t *visited_phis)
843 unsigned int i, n;
844 gimple flag_def = 0;
845 tree boundary_cst = 0;
846 enum tree_code cmp_code;
847 bool swap_cond = false;
848 bool invert = false;
849 VEC(use_pred_info_t, heap) *the_pred_chain;
851 gcc_assert (num_preds > 0);
852 /* Find within the common prefix of multiple predicate chains
853 a predicate that is a comparison of a flag variable against
854 a constant. */
855 the_pred_chain = preds[0];
856 n = VEC_length (use_pred_info_t, the_pred_chain);
857 for (i = 0; i < n; i++)
859 gimple cond;
860 tree cond_lhs, cond_rhs, flag = 0;
862 use_pred_info_t the_pred
863 = VEC_index (use_pred_info_t, the_pred_chain, i);
865 cond = the_pred->cond;
866 invert = the_pred->invert;
867 cond_lhs = gimple_cond_lhs (cond);
868 cond_rhs = gimple_cond_rhs (cond);
869 cmp_code = gimple_cond_code (cond);
871 if (cond_lhs != NULL_TREE && TREE_CODE (cond_lhs) == SSA_NAME
872 && cond_rhs != NULL_TREE && is_gimple_constant (cond_rhs))
874 boundary_cst = cond_rhs;
875 flag = cond_lhs;
877 else if (cond_rhs != NULL_TREE && TREE_CODE (cond_rhs) == SSA_NAME
878 && cond_lhs != NULL_TREE && is_gimple_constant (cond_lhs))
880 boundary_cst = cond_lhs;
881 flag = cond_rhs;
882 swap_cond = true;
885 if (!flag)
886 continue;
888 flag_def = SSA_NAME_DEF_STMT (flag);
890 if (!flag_def)
891 continue;
893 if ((gimple_code (flag_def) == GIMPLE_PHI)
894 && (gimple_bb (flag_def) == gimple_bb (phi))
895 && find_matching_predicate_in_rest_chains (
896 the_pred, preds, num_preds))
897 break;
899 flag_def = 0;
902 if (!flag_def)
903 return false;
905 /* Now check all the uninit incoming edge has a constant flag value
906 that is in conflict with the use guard/predicate. */
907 cmp_code = get_cmp_code (cmp_code, swap_cond, invert);
909 if (cmp_code == ERROR_MARK)
910 return false;
912 for (i = 0; i < sizeof (unsigned); i++)
914 tree flag_arg;
916 if (!MASK_TEST_BIT (uninit_opnds, i))
917 continue;
919 flag_arg = gimple_phi_arg_def (flag_def, i);
920 if (!is_gimple_constant (flag_arg))
921 return false;
923 /* Now check if the constant is in the guarded range. */
924 if (is_value_included_in (flag_arg, boundary_cst, cmp_code))
926 tree opnd;
927 gimple opnd_def;
929 /* Now that we know that this undefined edge is not
930 pruned. If the operand is defined by another phi,
931 we can further prune the incoming edges of that
932 phi by checking the predicates of this operands. */
934 opnd = gimple_phi_arg_def (phi, i);
935 opnd_def = SSA_NAME_DEF_STMT (opnd);
936 if (gimple_code (opnd_def) == GIMPLE_PHI)
938 edge opnd_edge;
939 unsigned uninit_opnds2
940 = compute_uninit_opnds_pos (opnd_def);
941 gcc_assert (!MASK_EMPTY (uninit_opnds2));
942 opnd_edge = gimple_phi_arg_edge (phi, i);
943 if (!is_use_properly_guarded (phi,
944 opnd_edge->src,
945 opnd_def,
946 uninit_opnds2,
947 visited_phis))
948 return false;
950 else
951 return false;
955 return true;
958 /* Returns true if TC is AND or OR */
960 static inline bool
961 is_and_or_or (enum tree_code tc, tree typ)
963 return (tc == TRUTH_AND_EXPR
964 || tc == TRUTH_OR_EXPR
965 || tc == BIT_IOR_EXPR
966 || (tc == BIT_AND_EXPR
967 && (typ == 0 || TREE_CODE (typ) == BOOLEAN_TYPE)));
970 typedef struct norm_cond
972 VEC(gimple, heap) *conds;
973 enum tree_code cond_code;
974 bool invert;
975 } *norm_cond_t;
978 /* Normalizes gimple condition COND. The normalization follows
979 UD chains to form larger condition expression trees. NORM_COND
980 holds the normalized result. COND_CODE is the logical opcode
981 (AND or OR) of the normalized tree. */
983 static void
984 normalize_cond_1 (gimple cond,
985 norm_cond_t norm_cond,
986 enum tree_code cond_code)
988 enum gimple_code gc;
989 enum tree_code cur_cond_code;
990 tree rhs1, rhs2;
992 gc = gimple_code (cond);
993 if (gc != GIMPLE_ASSIGN)
995 VEC_safe_push (gimple, heap, norm_cond->conds, cond);
996 return;
999 cur_cond_code = gimple_assign_rhs_code (cond);
1000 rhs1 = gimple_assign_rhs1 (cond);
1001 rhs2 = gimple_assign_rhs2 (cond);
1002 if (cur_cond_code == NE_EXPR)
1004 if (integer_zerop (rhs2)
1005 && (TREE_CODE (rhs1) == SSA_NAME))
1006 normalize_cond_1 (
1007 SSA_NAME_DEF_STMT (rhs1),
1008 norm_cond, cond_code);
1009 else if (integer_zerop (rhs1)
1010 && (TREE_CODE (rhs2) == SSA_NAME))
1011 normalize_cond_1 (
1012 SSA_NAME_DEF_STMT (rhs2),
1013 norm_cond, cond_code);
1014 else
1015 VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1017 return;
1020 if (is_and_or_or (cur_cond_code, TREE_TYPE (rhs1))
1021 && (cond_code == cur_cond_code || cond_code == ERROR_MARK)
1022 && (TREE_CODE (rhs1) == SSA_NAME && TREE_CODE (rhs2) == SSA_NAME))
1024 normalize_cond_1 (SSA_NAME_DEF_STMT (rhs1),
1025 norm_cond, cur_cond_code);
1026 normalize_cond_1 (SSA_NAME_DEF_STMT (rhs2),
1027 norm_cond, cur_cond_code);
1028 norm_cond->cond_code = cur_cond_code;
1030 else
1031 VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1034 /* See normalize_cond_1 for details. INVERT is a flag to indicate
1035 if COND needs to be inverted or not. */
1037 static void
1038 normalize_cond (gimple cond, norm_cond_t norm_cond, bool invert)
1040 enum tree_code cond_code;
1042 norm_cond->cond_code = ERROR_MARK;
1043 norm_cond->invert = false;
1044 norm_cond->conds = NULL;
1045 gcc_assert (gimple_code (cond) == GIMPLE_COND);
1046 cond_code = gimple_cond_code (cond);
1047 if (invert)
1048 cond_code = invert_tree_comparison (cond_code, false);
1050 if (cond_code == NE_EXPR)
1052 if (integer_zerop (gimple_cond_rhs (cond))
1053 && (TREE_CODE (gimple_cond_lhs (cond)) == SSA_NAME))
1054 normalize_cond_1 (
1055 SSA_NAME_DEF_STMT (gimple_cond_lhs (cond)),
1056 norm_cond, ERROR_MARK);
1057 else if (integer_zerop (gimple_cond_lhs (cond))
1058 && (TREE_CODE (gimple_cond_rhs (cond)) == SSA_NAME))
1059 normalize_cond_1 (
1060 SSA_NAME_DEF_STMT (gimple_cond_rhs (cond)),
1061 norm_cond, ERROR_MARK);
1062 else
1064 VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1065 norm_cond->invert = invert;
1068 else
1070 VEC_safe_push (gimple, heap, norm_cond->conds, cond);
1071 norm_cond->invert = invert;
1074 gcc_assert (VEC_length (gimple, norm_cond->conds) == 1
1075 || is_and_or_or (norm_cond->cond_code, NULL));
1078 /* Returns true if the domain for condition COND1 is a subset of
1079 COND2. REVERSE is a flag. when it is true the function checks
1080 if COND1 is a superset of COND2. INVERT1 and INVERT2 are flags
1081 to indicate if COND1 and COND2 need to be inverted or not. */
1083 static bool
1084 is_gcond_subset_of (gimple cond1, bool invert1,
1085 gimple cond2, bool invert2,
1086 bool reverse)
1088 enum gimple_code gc1, gc2;
1089 enum tree_code cond1_code, cond2_code;
1090 gimple tmp;
1091 tree cond1_lhs, cond1_rhs, cond2_lhs, cond2_rhs;
1093 /* Take the short cut. */
1094 if (cond1 == cond2)
1095 return true;
1097 if (reverse)
1099 tmp = cond1;
1100 cond1 = cond2;
1101 cond2 = tmp;
1104 gc1 = gimple_code (cond1);
1105 gc2 = gimple_code (cond2);
1107 if ((gc1 != GIMPLE_ASSIGN && gc1 != GIMPLE_COND)
1108 || (gc2 != GIMPLE_ASSIGN && gc2 != GIMPLE_COND))
1109 return cond1 == cond2;
1111 cond1_code = ((gc1 == GIMPLE_ASSIGN)
1112 ? gimple_assign_rhs_code (cond1)
1113 : gimple_cond_code (cond1));
1115 cond2_code = ((gc2 == GIMPLE_ASSIGN)
1116 ? gimple_assign_rhs_code (cond2)
1117 : gimple_cond_code (cond2));
1119 if (TREE_CODE_CLASS (cond1_code) != tcc_comparison
1120 || TREE_CODE_CLASS (cond2_code) != tcc_comparison)
1121 return false;
1123 if (invert1)
1124 cond1_code = invert_tree_comparison (cond1_code, false);
1125 if (invert2)
1126 cond2_code = invert_tree_comparison (cond2_code, false);
1128 cond1_lhs = ((gc1 == GIMPLE_ASSIGN)
1129 ? gimple_assign_rhs1 (cond1)
1130 : gimple_cond_lhs (cond1));
1131 cond1_rhs = ((gc1 == GIMPLE_ASSIGN)
1132 ? gimple_assign_rhs2 (cond1)
1133 : gimple_cond_rhs (cond1));
1134 cond2_lhs = ((gc2 == GIMPLE_ASSIGN)
1135 ? gimple_assign_rhs1 (cond2)
1136 : gimple_cond_lhs (cond2));
1137 cond2_rhs = ((gc2 == GIMPLE_ASSIGN)
1138 ? gimple_assign_rhs2 (cond2)
1139 : gimple_cond_rhs (cond2));
1141 /* Assuming const operands have been swapped to the
1142 rhs at this point of the analysis. */
1144 if (cond1_lhs != cond2_lhs)
1145 return false;
1147 if (!is_gimple_constant (cond1_rhs)
1148 || TREE_CODE (cond1_rhs) != INTEGER_CST)
1149 return (cond1_rhs == cond2_rhs);
1151 if (!is_gimple_constant (cond2_rhs)
1152 || TREE_CODE (cond2_rhs) != INTEGER_CST)
1153 return (cond1_rhs == cond2_rhs);
1155 if (cond1_code == EQ_EXPR)
1156 return is_value_included_in (cond1_rhs,
1157 cond2_rhs, cond2_code);
1158 if (cond1_code == NE_EXPR || cond2_code == EQ_EXPR)
1159 return ((cond2_code == cond1_code)
1160 && tree_int_cst_equal (cond1_rhs, cond2_rhs));
1162 if (((cond1_code == GE_EXPR || cond1_code == GT_EXPR)
1163 && (cond2_code == LE_EXPR || cond2_code == LT_EXPR))
1164 || ((cond1_code == LE_EXPR || cond1_code == LT_EXPR)
1165 && (cond2_code == GE_EXPR || cond2_code == GT_EXPR)))
1166 return false;
1168 if (cond1_code != GE_EXPR && cond1_code != GT_EXPR
1169 && cond1_code != LE_EXPR && cond1_code != LT_EXPR)
1170 return false;
1172 if (cond1_code == GT_EXPR)
1174 cond1_code = GE_EXPR;
1175 cond1_rhs = fold_binary (PLUS_EXPR, TREE_TYPE (cond1_rhs),
1176 cond1_rhs,
1177 fold_convert (TREE_TYPE (cond1_rhs),
1178 integer_one_node));
1180 else if (cond1_code == LT_EXPR)
1182 cond1_code = LE_EXPR;
1183 cond1_rhs = fold_binary (MINUS_EXPR, TREE_TYPE (cond1_rhs),
1184 cond1_rhs,
1185 fold_convert (TREE_TYPE (cond1_rhs),
1186 integer_one_node));
1189 if (!cond1_rhs)
1190 return false;
1192 gcc_assert (cond1_code == GE_EXPR || cond1_code == LE_EXPR);
1194 if (cond2_code == GE_EXPR || cond2_code == GT_EXPR ||
1195 cond2_code == LE_EXPR || cond2_code == LT_EXPR)
1196 return is_value_included_in (cond1_rhs,
1197 cond2_rhs, cond2_code);
1198 else if (cond2_code == NE_EXPR)
1199 return
1200 (is_value_included_in (cond1_rhs,
1201 cond2_rhs, cond2_code)
1202 && !is_value_included_in (cond2_rhs,
1203 cond1_rhs, cond1_code));
1204 return false;
1207 /* Returns true if the domain of the condition expression
1208 in COND is a subset of any of the sub-conditions
1209 of the normalized condtion NORM_COND. INVERT is a flag
1210 to indicate of the COND needs to be inverted.
1211 REVERSE is a flag. When it is true, the check is reversed --
1212 it returns true if COND is a superset of any of the subconditions
1213 of NORM_COND. */
1215 static bool
1216 is_subset_of_any (gimple cond, bool invert,
1217 norm_cond_t norm_cond, bool reverse)
1219 size_t i;
1220 size_t len = VEC_length (gimple, norm_cond->conds);
1222 for (i = 0; i < len; i++)
1224 if (is_gcond_subset_of (cond, invert,
1225 VEC_index (gimple, norm_cond->conds, i),
1226 false, reverse))
1227 return true;
1229 return false;
1232 /* NORM_COND1 and NORM_COND2 are normalized logical/BIT OR
1233 expressions (formed by following UD chains not control
1234 dependence chains). The function returns true of domain
1235 of and expression NORM_COND1 is a subset of NORM_COND2's.
1236 The implementation is conservative, and it returns false if
1237 it the inclusion relationship may not hold. */
1239 static bool
1240 is_or_set_subset_of (norm_cond_t norm_cond1,
1241 norm_cond_t norm_cond2)
1243 size_t i;
1244 size_t len = VEC_length (gimple, norm_cond1->conds);
1246 for (i = 0; i < len; i++)
1248 if (!is_subset_of_any (VEC_index (gimple, norm_cond1->conds, i),
1249 false, norm_cond2, false))
1250 return false;
1252 return true;
1255 /* NORM_COND1 and NORM_COND2 are normalized logical AND
1256 expressions (formed by following UD chains not control
1257 dependence chains). The function returns true of domain
1258 of and expression NORM_COND1 is a subset of NORM_COND2's. */
1260 static bool
1261 is_and_set_subset_of (norm_cond_t norm_cond1,
1262 norm_cond_t norm_cond2)
1264 size_t i;
1265 size_t len = VEC_length (gimple, norm_cond2->conds);
1267 for (i = 0; i < len; i++)
1269 if (!is_subset_of_any (VEC_index (gimple, norm_cond2->conds, i),
1270 false, norm_cond1, true))
1271 return false;
1273 return true;
1276 /* Returns true of the domain if NORM_COND1 is a subset
1277 of that of NORM_COND2. Returns false if it can not be
1278 proved to be so. */
1280 static bool
1281 is_norm_cond_subset_of (norm_cond_t norm_cond1,
1282 norm_cond_t norm_cond2)
1284 size_t i;
1285 enum tree_code code1, code2;
1287 code1 = norm_cond1->cond_code;
1288 code2 = norm_cond2->cond_code;
1290 if (code1 == TRUTH_AND_EXPR || code1 == BIT_AND_EXPR)
1292 /* Both conditions are AND expressions. */
1293 if (code2 == TRUTH_AND_EXPR || code2 == BIT_AND_EXPR)
1294 return is_and_set_subset_of (norm_cond1, norm_cond2);
1295 /* NORM_COND1 is an AND expression, and NORM_COND2 is an OR
1296 expression. In this case, returns true if any subexpression
1297 of NORM_COND1 is a subset of any subexpression of NORM_COND2. */
1298 else if (code2 == TRUTH_OR_EXPR || code2 == BIT_IOR_EXPR)
1300 size_t len1;
1301 len1 = VEC_length (gimple, norm_cond1->conds);
1302 for (i = 0; i < len1; i++)
1304 gimple cond1 = VEC_index (gimple, norm_cond1->conds, i);
1305 if (is_subset_of_any (cond1, false, norm_cond2, false))
1306 return true;
1308 return false;
1310 else
1312 gcc_assert (code2 == ERROR_MARK);
1313 gcc_assert (VEC_length (gimple, norm_cond2->conds) == 1);
1314 return is_subset_of_any (VEC_index (gimple, norm_cond2->conds, 0),
1315 norm_cond2->invert, norm_cond1, true);
1318 /* NORM_COND1 is an OR expression */
1319 else if (code1 == TRUTH_OR_EXPR || code1 == BIT_IOR_EXPR)
1321 if (code2 != code1)
1322 return false;
1324 return is_or_set_subset_of (norm_cond1, norm_cond2);
1326 else
1328 gcc_assert (code1 == ERROR_MARK);
1329 gcc_assert (VEC_length (gimple, norm_cond1->conds) == 1);
1330 /* Conservatively returns false if NORM_COND1 is non-decomposible
1331 and NORM_COND2 is an AND expression. */
1332 if (code2 == TRUTH_AND_EXPR || code2 == BIT_AND_EXPR)
1333 return false;
1335 if (code2 == TRUTH_OR_EXPR || code2 == BIT_IOR_EXPR)
1336 return is_subset_of_any (VEC_index (gimple, norm_cond1->conds, 0),
1337 norm_cond1->invert, norm_cond2, false);
1339 gcc_assert (code2 == ERROR_MARK);
1340 gcc_assert (VEC_length (gimple, norm_cond2->conds) == 1);
1341 return is_gcond_subset_of (VEC_index (gimple, norm_cond1->conds, 0),
1342 norm_cond1->invert,
1343 VEC_index (gimple, norm_cond2->conds, 0),
1344 norm_cond2->invert, false);
1348 /* Returns true of the domain of single predicate expression
1349 EXPR1 is a subset of that of EXPR2. Returns false if it
1350 can not be proved. */
1352 static bool
1353 is_pred_expr_subset_of (use_pred_info_t expr1,
1354 use_pred_info_t expr2)
1356 gimple cond1, cond2;
1357 enum tree_code code1, code2;
1358 struct norm_cond norm_cond1, norm_cond2;
1359 bool is_subset = false;
1361 cond1 = expr1->cond;
1362 cond2 = expr2->cond;
1363 code1 = gimple_cond_code (cond1);
1364 code2 = gimple_cond_code (cond2);
1366 if (expr1->invert)
1367 code1 = invert_tree_comparison (code1, false);
1368 if (expr2->invert)
1369 code2 = invert_tree_comparison (code2, false);
1371 /* Fast path -- match exactly */
1372 if ((gimple_cond_lhs (cond1) == gimple_cond_lhs (cond2))
1373 && (gimple_cond_rhs (cond1) == gimple_cond_rhs (cond2))
1374 && (code1 == code2))
1375 return true;
1377 /* Normalize conditions. To keep NE_EXPR, do not invert
1378 with both need inversion. */
1379 normalize_cond (cond1, &norm_cond1, (expr1->invert));
1380 normalize_cond (cond2, &norm_cond2, (expr2->invert));
1382 is_subset = is_norm_cond_subset_of (&norm_cond1, &norm_cond2);
1384 /* Free memory */
1385 VEC_free (gimple, heap, norm_cond1.conds);
1386 VEC_free (gimple, heap, norm_cond2.conds);
1387 return is_subset ;
1390 /* Returns true if the domain of PRED1 is a subset
1391 of that of PRED2. Returns false if it can not be proved so. */
1393 static bool
1394 is_pred_chain_subset_of (VEC(use_pred_info_t, heap) *pred1,
1395 VEC(use_pred_info_t, heap) *pred2)
1397 size_t np1, np2, i1, i2;
1399 np1 = VEC_length (use_pred_info_t, pred1);
1400 np2 = VEC_length (use_pred_info_t, pred2);
1402 for (i2 = 0; i2 < np2; i2++)
1404 bool found = false;
1405 use_pred_info_t info2
1406 = VEC_index (use_pred_info_t, pred2, i2);
1407 for (i1 = 0; i1 < np1; i1++)
1409 use_pred_info_t info1
1410 = VEC_index (use_pred_info_t, pred1, i1);
1411 if (is_pred_expr_subset_of (info1, info2))
1413 found = true;
1414 break;
1417 if (!found)
1418 return false;
1420 return true;
1423 /* Returns true if the domain defined by
1424 one pred chain ONE_PRED is a subset of the domain
1425 of *PREDS. It returns false if ONE_PRED's domain is
1426 not a subset of any of the sub-domains of PREDS (
1427 corresponding to each individual chains in it), even
1428 though it may be still be a subset of whole domain
1429 of PREDS which is the union (ORed) of all its subdomains.
1430 In other words, the result is conservative. */
1432 static bool
1433 is_included_in (VEC(use_pred_info_t, heap) *one_pred,
1434 VEC(use_pred_info_t, heap) **preds,
1435 size_t n)
1437 size_t i;
1439 for (i = 0; i < n; i++)
1441 if (is_pred_chain_subset_of (one_pred, preds[i]))
1442 return true;
1445 return false;
1448 /* compares two predicate sets PREDS1 and PREDS2 and returns
1449 true if the domain defined by PREDS1 is a superset
1450 of PREDS2's domain. N1 and N2 are array sizes of PREDS1 and
1451 PREDS2 respectively. The implementation chooses not to build
1452 generic trees (and relying on the folding capability of the
1453 compiler), but instead performs brute force comparison of
1454 individual predicate chains (won't be a compile time problem
1455 as the chains are pretty short). When the function returns
1456 false, it does not necessarily mean *PREDS1 is not a superset
1457 of *PREDS2, but mean it may not be so since the analysis can
1458 not prove it. In such cases, false warnings may still be
1459 emitted. */
1461 static bool
1462 is_superset_of (VEC(use_pred_info_t, heap) **preds1,
1463 size_t n1,
1464 VEC(use_pred_info_t, heap) **preds2,
1465 size_t n2)
1467 size_t i;
1468 VEC(use_pred_info_t, heap) *one_pred_chain;
1470 for (i = 0; i < n2; i++)
1472 one_pred_chain = preds2[i];
1473 if (!is_included_in (one_pred_chain, preds1, n1))
1474 return false;
1477 return true;
1480 /* Computes the predicates that guard the use and checks
1481 if the incoming paths that have empty (or possibly
1482 empty) defintion can be pruned/filtered. The function returns
1483 true if it can be determined that the use of PHI's def in
1484 USE_STMT is guarded with a predicate set not overlapping with
1485 predicate sets of all runtime paths that do not have a definition.
1486 Returns false if it is not or it can not be determined. USE_BB is
1487 the bb of the use (for phi operand use, the bb is not the bb of
1488 the phi stmt, but the src bb of the operand edge). UNINIT_OPNDS
1489 is a bit vector. If an operand of PHI is uninitialized, the
1490 correponding bit in the vector is 1. VISIED_PHIS is a pointer
1491 set of phis being visted. */
1493 static bool
1494 is_use_properly_guarded (gimple use_stmt,
1495 basic_block use_bb,
1496 gimple phi,
1497 unsigned uninit_opnds,
1498 struct pointer_set_t *visited_phis)
1500 basic_block phi_bb;
1501 VEC(use_pred_info_t, heap) **preds = 0;
1502 VEC(use_pred_info_t, heap) **def_preds = 0;
1503 size_t num_preds = 0, num_def_preds = 0;
1504 bool has_valid_preds = false;
1505 bool is_properly_guarded = false;
1507 if (pointer_set_insert (visited_phis, phi))
1508 return false;
1510 phi_bb = gimple_bb (phi);
1512 if (is_non_loop_exit_postdominating (use_bb, phi_bb))
1513 return false;
1515 has_valid_preds = find_predicates (&preds, &num_preds,
1516 phi_bb, use_bb);
1518 if (!has_valid_preds)
1520 destroy_predicate_vecs (num_preds, preds);
1521 return false;
1524 if (dump_file)
1525 dump_predicates (use_stmt, num_preds, preds,
1526 "Use in stmt ");
1528 has_valid_preds = find_def_preds (&def_preds,
1529 &num_def_preds, phi);
1531 if (has_valid_preds)
1533 if (dump_file)
1534 dump_predicates (phi, num_def_preds, def_preds,
1535 "Operand defs of phi ");
1536 is_properly_guarded =
1537 is_superset_of (def_preds, num_def_preds,
1538 preds, num_preds);
1541 /* further prune the dead incoming phi edges. */
1542 if (!is_properly_guarded)
1543 is_properly_guarded
1544 = use_pred_not_overlap_with_undef_path_pred (
1545 num_preds, preds, phi, uninit_opnds, visited_phis);
1547 destroy_predicate_vecs (num_preds, preds);
1548 destroy_predicate_vecs (num_def_preds, def_preds);
1549 return is_properly_guarded;
1552 /* Searches through all uses of a potentially
1553 uninitialized variable defined by PHI and returns a use
1554 statement if the use is not properly guarded. It returns
1555 NULL if all uses are guarded. UNINIT_OPNDS is a bitvector
1556 holding the position(s) of uninit PHI operands. WORKLIST
1557 is the vector of candidate phis that may be updated by this
1558 function. ADDED_TO_WORKLIST is the pointer set tracking
1559 if the new phi is already in the worklist. */
1561 static gimple
1562 find_uninit_use (gimple phi, unsigned uninit_opnds,
1563 VEC(gimple, heap) **worklist,
1564 struct pointer_set_t *added_to_worklist)
1566 tree phi_result;
1567 use_operand_p use_p;
1568 gimple use_stmt;
1569 imm_use_iterator iter;
1571 phi_result = gimple_phi_result (phi);
1573 FOR_EACH_IMM_USE_FAST (use_p, iter, phi_result)
1575 struct pointer_set_t *visited_phis;
1576 basic_block use_bb;
1578 use_stmt = use_p->loc.stmt;
1580 visited_phis = pointer_set_create ();
1582 use_bb = gimple_bb (use_stmt);
1583 if (gimple_code (use_stmt) == GIMPLE_PHI)
1585 unsigned i, n;
1586 n = gimple_phi_num_args (use_stmt);
1588 /* Find the matching phi argument of the use. */
1589 for (i = 0; i < n; ++i)
1591 if (gimple_phi_arg_def_ptr (use_stmt, i) == use_p->use)
1593 edge e = gimple_phi_arg_edge (use_stmt, i);
1594 use_bb = e->src;
1595 break;
1600 if (is_use_properly_guarded (use_stmt,
1601 use_bb,
1602 phi,
1603 uninit_opnds,
1604 visited_phis))
1606 pointer_set_destroy (visited_phis);
1607 continue;
1609 pointer_set_destroy (visited_phis);
1611 /* Found one real use, return. */
1612 if (gimple_code (use_stmt) != GIMPLE_PHI)
1613 return use_stmt;
1615 /* Found a phi use that is not guarded,
1616 add the phi to the worklist. */
1617 if (!pointer_set_insert (added_to_worklist,
1618 use_stmt))
1620 VEC_safe_push (gimple, heap, *worklist, use_stmt);
1621 pointer_set_insert (possibly_undefined_names,
1622 phi_result);
1626 return NULL;
1629 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
1630 and gives warning if there exists a runtime path from the entry to a
1631 use of the PHI def that does not contain a definition. In other words,
1632 the warning is on the real use. The more dead paths that can be pruned
1633 by the compiler, the fewer false positives the warning is. WORKLIST
1634 is a vector of candidate phis to be examined. ADDED_TO_WORKLIST is
1635 a pointer set tracking if the new phi is added to the worklist or not. */
1637 static void
1638 warn_uninitialized_phi (gimple phi, VEC(gimple, heap) **worklist,
1639 struct pointer_set_t *added_to_worklist)
1641 unsigned uninit_opnds;
1642 gimple uninit_use_stmt = 0;
1643 tree uninit_op;
1645 /* Don't look at memory tags. */
1646 if (!is_gimple_reg (gimple_phi_result (phi)))
1647 return;
1649 uninit_opnds = compute_uninit_opnds_pos (phi);
1651 if (MASK_EMPTY (uninit_opnds))
1652 return;
1654 /* Now check if we have any use of the value without proper guard. */
1655 uninit_use_stmt = find_uninit_use (phi, uninit_opnds,
1656 worklist, added_to_worklist);
1658 /* All uses are properly guarded. */
1659 if (!uninit_use_stmt)
1660 return;
1662 uninit_op = gimple_phi_arg_def (phi, MASK_FIRST_SET_BIT (uninit_opnds));
1663 warn_uninit (uninit_op,
1664 "%qD may be used uninitialized in this function",
1665 uninit_use_stmt);
1670 /* Entry point to the late uninitialized warning pass. */
1672 static unsigned int
1673 execute_late_warn_uninitialized (void)
1675 basic_block bb;
1676 gimple_stmt_iterator gsi;
1677 VEC(gimple, heap) *worklist = 0;
1678 struct pointer_set_t *added_to_worklist;
1680 calculate_dominance_info (CDI_DOMINATORS);
1681 calculate_dominance_info (CDI_POST_DOMINATORS);
1682 /* Re-do the plain uninitialized variable check, as optimization may have
1683 straightened control flow. Do this first so that we don't accidentally
1684 get a "may be" warning when we'd have seen an "is" warning later. */
1685 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
1687 timevar_push (TV_TREE_UNINIT);
1689 possibly_undefined_names = pointer_set_create ();
1690 added_to_worklist = pointer_set_create ();
1692 /* Initialize worklist */
1693 FOR_EACH_BB (bb)
1694 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1696 gimple phi = gsi_stmt (gsi);
1697 size_t n, i;
1699 n = gimple_phi_num_args (phi);
1701 /* Don't look at memory tags. */
1702 if (!is_gimple_reg (gimple_phi_result (phi)))
1703 continue;
1705 for (i = 0; i < n; ++i)
1707 tree op = gimple_phi_arg_def (phi, i);
1708 if (TREE_CODE (op) == SSA_NAME
1709 && ssa_undefined_value_p (op))
1711 VEC_safe_push (gimple, heap, worklist, phi);
1712 pointer_set_insert (added_to_worklist, phi);
1713 break;
1718 while (VEC_length (gimple, worklist) != 0)
1720 gimple cur_phi = 0;
1721 cur_phi = VEC_pop (gimple, worklist);
1722 warn_uninitialized_phi (cur_phi, &worklist, added_to_worklist);
1725 VEC_free (gimple, heap, worklist);
1726 pointer_set_destroy (added_to_worklist);
1727 pointer_set_destroy (possibly_undefined_names);
1728 possibly_undefined_names = NULL;
1729 free_dominance_info (CDI_POST_DOMINATORS);
1730 timevar_pop (TV_TREE_UNINIT);
1731 return 0;
1734 static bool
1735 gate_warn_uninitialized (void)
1737 return warn_uninitialized != 0;
1740 struct gimple_opt_pass pass_late_warn_uninitialized =
1743 GIMPLE_PASS,
1744 "uninit", /* name */
1745 gate_warn_uninitialized, /* gate */
1746 execute_late_warn_uninitialized, /* execute */
1747 NULL, /* sub */
1748 NULL, /* next */
1749 0, /* static_pass_number */
1750 TV_NONE, /* tv_id */
1751 PROP_ssa, /* properties_required */
1752 0, /* properties_provided */
1753 0, /* properties_destroyed */
1754 0, /* todo_flags_start */
1755 0 /* todo_flags_finish */